blob: cede36879e1bf876f6be954dea7dd4ebe43183cc [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
37#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000038#include "monitor.h"
39#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "console.h"
41#include "block.h"
42#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000043#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000045#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000046#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000047#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000048#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030049#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010050#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030051#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030052#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020055#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020056#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020057#include "json-streamer.h"
58#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000059#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000060
bellard9dc39cb2004-03-14 21:38:27 +000061//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000062//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000063
bellard9307c4c2004-04-04 12:57:25 +000064/*
65 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000066 *
bellard9307c4c2004-04-04 12:57:25 +000067 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000068 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000069 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000070 * 'i' 32 bit integer
71 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010072 * 'M' just like 'l', except in user mode the value is
73 * multiplied by 2^20 (think Mebibyte)
Markus Armbruster3350a4d2010-01-25 14:23:03 +010074 * 'b' double
75 * user mode accepts an optional G, g, M, m, K, k suffix,
76 * which multiplies the value by 2^30 for suffixes G and
77 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010078 * 'T' double
79 * user mode accepts an optional ms, us, ns suffix,
80 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000081 * '/' optional gdb-like print format (like "/10x")
82 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030083 * '?' optional type (for all types, except '/')
84 * '.' other form of optional type (for 'i' and 'l')
85 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000086 *
87 */
88
Adam Litke940cc302010-01-25 12:18:44 -060089typedef struct MonitorCompletionData MonitorCompletionData;
90struct MonitorCompletionData {
91 Monitor *mon;
92 void (*user_print)(Monitor *mon, const QObject *data);
93};
94
Anthony Liguoric227f092009-10-01 16:12:16 -050095typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000096 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000097 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000098 const char *params;
99 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300100 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino97536cf2010-02-10 23:49:47 -0200101 int (*cmd_new_ret)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300102 union {
103 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300104 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600105 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300106 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -0300107 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600108 int (*cmd_async)(Monitor *mon, const QDict *params,
109 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300110 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600111 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500112} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000113
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100114/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500115typedef struct mon_fd_t mon_fd_t;
116struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100117 char *name;
118 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500119 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100120};
121
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200122typedef struct MonitorControl {
123 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200124 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200125 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200126 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200127} MonitorControl;
128
aliguori87127162009-03-05 23:01:29 +0000129struct Monitor {
130 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200131 int mux_out;
132 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000133 int flags;
134 int suspend_cnt;
135 uint8_t outbuf[1024];
136 int outbuf_index;
137 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200138 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000139 CPUState *mon_cpu;
140 BlockDriverCompletionFunc *password_completion_cb;
141 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200142 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000144 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000145};
146
Blue Swirl72cf2d42009-09-12 07:36:22 +0000147static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000148
Anthony Liguoric227f092009-10-01 16:12:16 -0500149static const mon_cmd_t mon_cmds[];
150static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000151
aliguori87127162009-03-05 23:01:29 +0000152Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000153
aliguori731b0362009-03-05 23:01:42 +0000154static void monitor_command_cb(Monitor *mon, const char *cmdline,
155 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000156
Luiz Capitulino09069b12010-02-04 18:10:06 -0200157static inline int qmp_cmd_mode(const Monitor *mon)
158{
159 return (mon->mc ? mon->mc->command_mode : 0);
160}
161
Luiz Capitulino418173c2009-11-26 22:58:51 -0200162/* Return true if in control mode, false otherwise */
163static inline int monitor_ctrl_mode(const Monitor *mon)
164{
165 return (mon->flags & MONITOR_USE_CONTROL);
166}
167
aliguori731b0362009-03-05 23:01:42 +0000168static void monitor_read_command(Monitor *mon, int show_prompt)
169{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200170 if (!mon->rs)
171 return;
172
aliguori731b0362009-03-05 23:01:42 +0000173 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
174 if (show_prompt)
175 readline_show_prompt(mon->rs);
176}
bellard6a00d602005-11-21 23:25:50 +0000177
aliguoricde76ee2009-03-05 23:01:51 +0000178static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
179 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000180{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100181 if (monitor_ctrl_mode(mon)) {
182 qemu_error_new(QERR_MISSING_PARAMETER, "password");
183 return -EINVAL;
184 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000185 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
186 /* prompt is printed on return from the command handler */
187 return 0;
188 } else {
189 monitor_printf(mon, "terminal does not support password prompting\n");
190 return -ENOTTY;
191 }
aliguoribb5fc202009-03-05 23:01:15 +0000192}
193
aliguori376253e2009-03-05 23:01:23 +0000194void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000195{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200196 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000197 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
198 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000199 }
200}
201
202/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000203static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000204{
ths60fe76f2007-12-16 03:02:09 +0000205 char c;
aliguori731b0362009-03-05 23:01:42 +0000206
bellard7e2515e2004-08-01 21:52:19 +0000207 for(;;) {
208 c = *str++;
209 if (c == '\0')
210 break;
bellard7ba12602006-07-14 20:26:42 +0000211 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000212 mon->outbuf[mon->outbuf_index++] = '\r';
213 mon->outbuf[mon->outbuf_index++] = c;
214 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
215 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000216 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000217 }
218}
219
aliguori376253e2009-03-05 23:01:23 +0000220void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000221{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200222 if (!mon)
223 return;
224
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200225 if (mon->mc && !mon->mc->print_enabled) {
226 qemu_error_new(QERR_UNDEFINED_ERROR);
227 } else {
228 char buf[4096];
229 vsnprintf(buf, sizeof(buf), fmt, ap);
230 monitor_puts(mon, buf);
231 }
bellard7e2515e2004-08-01 21:52:19 +0000232}
233
aliguori376253e2009-03-05 23:01:23 +0000234void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000235{
236 va_list ap;
237 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000238 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000239 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000240}
241
aliguori376253e2009-03-05 23:01:23 +0000242void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000243{
244 int i;
245
246 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000247 switch (filename[i]) {
248 case ' ':
249 case '"':
250 case '\\':
251 monitor_printf(mon, "\\%c", filename[i]);
252 break;
253 case '\t':
254 monitor_printf(mon, "\\t");
255 break;
256 case '\r':
257 monitor_printf(mon, "\\r");
258 break;
259 case '\n':
260 monitor_printf(mon, "\\n");
261 break;
262 default:
263 monitor_printf(mon, "%c", filename[i]);
264 break;
265 }
thsfef30742006-12-22 14:11:32 +0000266 }
267}
268
bellard7fe48482004-10-09 18:08:01 +0000269static int monitor_fprintf(FILE *stream, const char *fmt, ...)
270{
271 va_list ap;
272 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000273 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000274 va_end(ap);
275 return 0;
276}
277
Luiz Capitulino13c74252009-10-07 13:41:55 -0300278static void monitor_user_noop(Monitor *mon, const QObject *data) { }
279
Luiz Capitulino13917be2009-10-07 13:41:54 -0300280static inline int monitor_handler_ported(const mon_cmd_t *cmd)
281{
282 return cmd->user_print != NULL;
283}
284
Adam Litke940cc302010-01-25 12:18:44 -0600285static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
286{
287 return cmd->async != 0;
288}
289
Luiz Capitulino8204a912009-11-18 23:05:31 -0200290static inline int monitor_has_error(const Monitor *mon)
291{
292 return mon->error != NULL;
293}
294
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200295static void monitor_json_emitter(Monitor *mon, const QObject *data)
296{
297 QString *json;
298
299 json = qobject_to_json(data);
300 assert(json != NULL);
301
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200302 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200303 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200304 mon->mc->print_enabled = 0;
305
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200306 QDECREF(json);
307}
308
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200309static void monitor_protocol_emitter(Monitor *mon, QObject *data)
310{
311 QDict *qmp;
312
313 qmp = qdict_new();
314
315 if (!monitor_has_error(mon)) {
316 /* success response */
317 if (data) {
318 qobject_incref(data);
319 qdict_put_obj(qmp, "return", data);
320 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200321 /* return an empty QDict by default */
322 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200323 }
324 } else {
325 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100326 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200327 qdict_put(qmp, "error", mon->error->error);
328 QINCREF(mon->error->error);
329 QDECREF(mon->error);
330 mon->error = NULL;
331 }
332
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200333 if (mon->mc->id) {
334 qdict_put_obj(qmp, "id", mon->mc->id);
335 mon->mc->id = NULL;
336 }
337
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200338 monitor_json_emitter(mon, QOBJECT(qmp));
339 QDECREF(qmp);
340}
341
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200342static void timestamp_put(QDict *qdict)
343{
344 int err;
345 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000346 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200347
Blue Swirld08d6f02009-12-04 18:06:39 +0000348 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200349 if (err < 0)
350 return;
351
352 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
353 "'microseconds': %" PRId64 " }",
354 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200355 qdict_put_obj(qdict, "timestamp", obj);
356}
357
358/**
359 * monitor_protocol_event(): Generate a Monitor event
360 *
361 * Event-specific data can be emitted through the (optional) 'data' parameter.
362 */
363void monitor_protocol_event(MonitorEvent event, QObject *data)
364{
365 QDict *qmp;
366 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600367 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200368
Blue Swirl242cd002009-12-04 18:05:45 +0000369 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200370
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200371 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000372 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200373 event_name = "DEBUG";
374 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000375 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200376 event_name = "SHUTDOWN";
377 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000378 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200379 event_name = "RESET";
380 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000381 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200382 event_name = "POWERDOWN";
383 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000384 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200385 event_name = "STOP";
386 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200387 case QEVENT_VNC_CONNECTED:
388 event_name = "VNC_CONNECTED";
389 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200390 case QEVENT_VNC_INITIALIZED:
391 event_name = "VNC_INITIALIZED";
392 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200393 case QEVENT_VNC_DISCONNECTED:
394 event_name = "VNC_DISCONNECTED";
395 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200396 case QEVENT_BLOCK_IO_ERROR:
397 event_name = "BLOCK_IO_ERROR";
398 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399 default:
400 abort();
401 break;
402 }
403
404 qmp = qdict_new();
405 timestamp_put(qmp);
406 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200407 if (data) {
408 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200410 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200411
Adam Litkef039a562010-01-15 08:34:02 -0600412 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200413 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200414 monitor_json_emitter(mon, QOBJECT(qmp));
415 }
Adam Litkef039a562010-01-15 08:34:02 -0600416 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200417 QDECREF(qmp);
418}
419
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200420static int do_qmp_capabilities(Monitor *mon, const QDict *params,
421 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200422{
423 /* Will setup QMP capabilities in the future */
424 if (monitor_ctrl_mode(mon)) {
425 mon->mc->command_mode = 1;
426 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200427
428 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200429}
430
bellard9dc39cb2004-03-14 21:38:27 +0000431static int compare_cmd(const char *name, const char *list)
432{
433 const char *p, *pstart;
434 int len;
435 len = strlen(name);
436 p = list;
437 for(;;) {
438 pstart = p;
439 p = strchr(p, '|');
440 if (!p)
441 p = pstart + strlen(pstart);
442 if ((p - pstart) == len && !memcmp(pstart, name, len))
443 return 1;
444 if (*p == '\0')
445 break;
446 p++;
447 }
448 return 0;
449}
450
Anthony Liguoric227f092009-10-01 16:12:16 -0500451static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000452 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000453{
Anthony Liguoric227f092009-10-01 16:12:16 -0500454 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000455
456 for(cmd = cmds; cmd->name != NULL; cmd++) {
457 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000458 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
459 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000460 }
461}
462
aliguori376253e2009-03-05 23:01:23 +0000463static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000464{
465 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000466 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000467 } else {
aliguori376253e2009-03-05 23:01:23 +0000468 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000469 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000470 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "Log items (comma separated):\n");
472 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000473 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000474 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000475 }
476 }
bellard9dc39cb2004-03-14 21:38:27 +0000477 }
478}
479
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300480static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300481{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300482 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300483}
484
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300485static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000486{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200487 int all_devices;
488 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300489 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000490
bellard7954c732006-08-01 15:52:40 +0000491 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000492 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200493 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300494 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200495 continue;
496 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000497 }
498}
499
Adam Litke940cc302010-01-25 12:18:44 -0600500static void user_monitor_complete(void *opaque, QObject *ret_data)
501{
502 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
503
504 if (ret_data) {
505 data->user_print(data->mon, ret_data);
506 }
507 monitor_resume(data->mon);
508 qemu_free(data);
509}
510
511static void qmp_monitor_complete(void *opaque, QObject *ret_data)
512{
513 monitor_protocol_emitter(opaque, ret_data);
514}
515
516static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
517 const QDict *params)
518{
519 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
520}
521
522static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
523{
524 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
525}
526
527static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
528 const QDict *params)
529{
530 int ret;
531
532 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
533 cb_data->mon = mon;
534 cb_data->user_print = cmd->user_print;
535 monitor_suspend(mon);
536 ret = cmd->mhandler.cmd_async(mon, params,
537 user_monitor_complete, cb_data);
538 if (ret < 0) {
539 monitor_resume(mon);
540 qemu_free(cb_data);
541 }
542}
543
544static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
545{
546 int ret;
547
548 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
549 cb_data->mon = mon;
550 cb_data->user_print = cmd->user_print;
551 monitor_suspend(mon);
552 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
553 if (ret < 0) {
554 monitor_resume(mon);
555 qemu_free(cb_data);
556 }
557}
558
Luiz Capitulino13c74252009-10-07 13:41:55 -0300559static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000560{
Anthony Liguoric227f092009-10-01 16:12:16 -0500561 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300562 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000563
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200564 if (!item) {
565 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000566 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200567 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300568
569 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000570 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300571 break;
bellard9dc39cb2004-03-14 21:38:27 +0000572 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300573
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200574 if (cmd->name == NULL) {
575 if (monitor_ctrl_mode(mon)) {
576 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
577 return;
578 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300579 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200580 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300581
Adam Litke940cc302010-01-25 12:18:44 -0600582 if (monitor_handler_is_async(cmd)) {
583 if (monitor_ctrl_mode(mon)) {
584 qmp_async_info_handler(mon, cmd);
585 } else {
586 user_async_info_handler(mon, cmd);
587 }
588 /*
589 * Indicate that this command is asynchronous and will not return any
590 * data (not even empty). Instead, the data will be returned via a
591 * completion callback.
592 */
593 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
594 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300595 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200596
597 if (!monitor_ctrl_mode(mon)) {
598 /*
599 * User Protocol function is called here, Monitor Protocol is
600 * handled by monitor_call_handler()
601 */
602 if (*ret_data)
603 cmd->user_print(mon, *ret_data);
604 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300605 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200606 if (monitor_ctrl_mode(mon)) {
607 /* handler not converted yet */
608 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
609 } else {
610 cmd->mhandler.info(mon);
611 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300612 }
613
bellard9dc39cb2004-03-14 21:38:27 +0000614 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300615
616help:
617 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000618}
619
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200620static void do_info_version_print(Monitor *mon, const QObject *data)
621{
622 QDict *qdict;
623
624 qdict = qobject_to_qdict(data);
625
626 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
627 qdict_get_str(qdict, "package"));
628}
629
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300630/**
631 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200632 *
633 * Return a QDict with the following information:
634 *
635 * - "qemu": QEMU's version
636 * - "package": package's version
637 *
638 * Example:
639 *
640 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300641 */
642static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000643{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200644 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
645 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000646}
647
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200648static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000649{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200650 QDict *qdict;
651
652 qdict = qobject_to_qdict(data);
653 if (qdict_size(qdict) == 0) {
654 return;
655 }
656
657 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
658}
659
660/**
661 * do_info_name(): Show VM name
662 *
663 * Return a QDict with the following information:
664 *
665 * - "name": VM's name (optional)
666 *
667 * Example:
668 *
669 * { "name": "qemu-name" }
670 */
671static void do_info_name(Monitor *mon, QObject **ret_data)
672{
673 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
674 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000675}
676
Luiz Capitulino1a728672009-12-10 17:15:56 -0200677static QObject *get_cmd_dict(const char *name)
678{
679 const char *p;
680
681 /* Remove '|' from some commands */
682 p = strchr(name, '|');
683 if (p) {
684 p++;
685 } else {
686 p = name;
687 }
688
689 return qobject_from_jsonf("{ 'name': %s }", p);
690}
691
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200692/**
693 * do_info_commands(): List QMP available commands
694 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200695 * Each command is represented by a QDict, the returned QObject is a QList
696 * of all commands.
697 *
698 * The QDict contains:
699 *
700 * - "name": command's name
701 *
702 * Example:
703 *
704 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200705 */
706static void do_info_commands(Monitor *mon, QObject **ret_data)
707{
708 QList *cmd_list;
709 const mon_cmd_t *cmd;
710
711 cmd_list = qlist_new();
712
713 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
714 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200715 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200716 }
717 }
718
719 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
720 if (monitor_handler_ported(cmd)) {
721 char buf[128];
722 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200723 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200724 }
725 }
726
727 *ret_data = QOBJECT(cmd_list);
728}
729
aurel32bf4f74c2008-12-18 22:42:34 +0000730#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200731static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000732{
aliguori376253e2009-03-05 23:01:23 +0000733 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200734 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
735 "enabled" : "disabled");
736}
737
738/**
739 * do_info_hpet(): Show HPET state
740 *
741 * Return a QDict with the following information:
742 *
743 * - "enabled": true if hpet if enabled, false otherwise
744 *
745 * Example:
746 *
747 * { "enabled": true }
748 */
749static void do_info_hpet(Monitor *mon, QObject **ret_data)
750{
751 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000752}
aurel32bf4f74c2008-12-18 22:42:34 +0000753#endif
aliguori16b29ae2008-12-17 23:28:44 +0000754
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200755static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000756{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200757 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
758}
759
760/**
761 * do_info_uuid(): Show VM UUID
762 *
763 * Return a QDict with the following information:
764 *
765 * - "UUID": Universally Unique Identifier
766 *
767 * Example:
768 *
769 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
770 */
771static void do_info_uuid(Monitor *mon, QObject **ret_data)
772{
773 char uuid[64];
774
775 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000776 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
777 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
778 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
779 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200780 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000781}
782
bellard6a00d602005-11-21 23:25:50 +0000783/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000784static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000785{
786 CPUState *env;
787
788 for(env = first_cpu; env != NULL; env = env->next_cpu) {
789 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000790 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000791 return 0;
792 }
793 }
794 return -1;
795}
796
pbrook9596ebb2007-11-18 01:44:38 +0000797static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000798{
aliguori731b0362009-03-05 23:01:42 +0000799 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000800 mon_set_cpu(0);
801 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300802 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000803 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000804}
805
aliguori376253e2009-03-05 23:01:23 +0000806static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000807{
bellard6a00d602005-11-21 23:25:50 +0000808 CPUState *env;
809 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000810#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000811 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000812 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000813#else
aliguori376253e2009-03-05 23:01:23 +0000814 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000815 0);
bellard9307c4c2004-04-04 12:57:25 +0000816#endif
817}
818
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300819static void print_cpu_iter(QObject *obj, void *opaque)
820{
821 QDict *cpu;
822 int active = ' ';
823 Monitor *mon = opaque;
824
825 assert(qobject_type(obj) == QTYPE_QDICT);
826 cpu = qobject_to_qdict(obj);
827
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200828 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300829 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200830 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300831
832 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
833
834#if defined(TARGET_I386)
835 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
836 (target_ulong) qdict_get_int(cpu, "pc"));
837#elif defined(TARGET_PPC)
838 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "nip"));
840#elif defined(TARGET_SPARC)
841 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
842 (target_long) qdict_get_int(cpu, "pc"));
843 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
844 (target_long) qdict_get_int(cpu, "npc"));
845#elif defined(TARGET_MIPS)
846 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
847 (target_long) qdict_get_int(cpu, "PC"));
848#endif
849
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200850 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300851 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200852 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300853
854 monitor_printf(mon, "\n");
855}
856
857static void monitor_print_cpus(Monitor *mon, const QObject *data)
858{
859 QList *cpu_list;
860
861 assert(qobject_type(data) == QTYPE_QLIST);
862 cpu_list = qobject_to_qlist(data);
863 qlist_iter(cpu_list, print_cpu_iter, mon);
864}
865
866/**
867 * do_info_cpus(): Show CPU information
868 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200869 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300870 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200871 * - "cpu": CPU index
872 * - "current": true if this is the current CPU, false otherwise
873 * - "halted": true if the cpu is halted, false otherwise
874 * - Current program counter. The key's name depends on the architecture:
875 * "pc": i386/x86)64
876 * "nip": PPC
877 * "pc" and "npc": sparc
878 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200880 * Example:
881 *
882 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
883 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300884 */
885static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000886{
887 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888 QList *cpu_list;
889
890 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000891
892 /* just to set the default cpu if not already done */
893 mon_get_cpu();
894
895 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200896 QDict *cpu;
897 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300898
Avi Kivity4c0960c2009-08-17 23:19:53 +0300899 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300900
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200901 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
902 env->cpu_index, env == mon->mon_cpu,
903 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200904
905 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300906
bellard6a00d602005-11-21 23:25:50 +0000907#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300908 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000909#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300910 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000911#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300912 qdict_put(cpu, "pc", qint_from_int(env->pc));
913 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000914#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300915 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000916#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917
918 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000919 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300920
921 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000922}
923
Markus Armbruster81a1b452010-01-20 13:07:35 +0100924static void do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000925{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300926 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000927 if (mon_set_cpu(index) < 0)
Markus Armbrustercc0c4182010-01-29 10:34:00 +0100928 qemu_error_new(QERR_INVALID_PARAMETER, "index");
bellard6a00d602005-11-21 23:25:50 +0000929}
930
aliguori376253e2009-03-05 23:01:23 +0000931static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000932{
aliguori376253e2009-03-05 23:01:23 +0000933 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000934}
935
aliguori376253e2009-03-05 23:01:23 +0000936static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000937{
938 int i;
bellard7e2515e2004-08-01 21:52:19 +0000939 const char *str;
ths3b46e622007-09-17 08:09:54 +0000940
aliguoricde76ee2009-03-05 23:01:51 +0000941 if (!mon->rs)
942 return;
bellard7e2515e2004-08-01 21:52:19 +0000943 i = 0;
944 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000945 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000946 if (!str)
947 break;
aliguori376253e2009-03-05 23:01:23 +0000948 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000949 i++;
bellardaa455482004-04-04 13:07:25 +0000950 }
951}
952
j_mayer76a66252007-03-07 08:32:30 +0000953#if defined(TARGET_PPC)
954/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000955static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000956{
957 CPUState *env;
958
959 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000960 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000961}
962#endif
963
Luiz Capitulinob223f352009-10-07 13:41:56 -0300964/**
965 * do_quit(): Quit QEMU execution
966 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200967static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000968{
969 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200970 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000971}
972
aliguori376253e2009-03-05 23:01:23 +0000973static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000974{
975 if (bdrv_is_inserted(bs)) {
976 if (!force) {
977 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100978 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
979 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000980 return -1;
981 }
982 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100983 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000984 return -1;
985 }
986 }
987 bdrv_close(bs);
988 }
989 return 0;
990}
991
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300992static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000993{
994 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300995 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200996 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +0000997
bellard9307c4c2004-04-04 12:57:25 +0000998 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000999 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001000 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +00001001 return;
1002 }
aliguori376253e2009-03-05 23:01:23 +00001003 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001004}
1005
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001006static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
1007 QObject **ret_data)
1008{
1009 BlockDriverState *bs;
1010
1011 bs = bdrv_find(qdict_get_str(qdict, "device"));
1012 if (!bs) {
1013 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
1014 return;
1015 }
1016
1017 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
1018 qemu_error_new(QERR_INVALID_PASSWORD);
1019 }
1020}
1021
aliguori376253e2009-03-05 23:01:23 +00001022static void do_change_block(Monitor *mon, const char *device,
1023 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001024{
1025 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001026 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001027
bellard9307c4c2004-04-04 12:57:25 +00001028 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001029 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001030 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001031 return;
1032 }
aurel322ecea9b2008-06-18 22:10:01 +00001033 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001034 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001035 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001036 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001037 return;
1038 }
1039 }
aliguori376253e2009-03-05 23:01:23 +00001040 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001041 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001042 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001043 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001044}
1045
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001046static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001047{
1048 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001049 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001050
Markus Armbruster2895e072009-12-07 21:37:01 +01001051}
1052
1053static void change_vnc_password_cb(Monitor *mon, const char *password,
1054 void *opaque)
1055{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001056 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001057 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001058}
1059
aliguori376253e2009-03-05 23:01:23 +00001060static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001061{
ths70848512007-08-25 01:37:05 +00001062 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001063 strcmp(target, "password") == 0) {
1064 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001065 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001066 strncpy(password, arg, sizeof(password));
1067 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001068 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001069 } else {
aliguori376253e2009-03-05 23:01:23 +00001070 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001071 }
ths70848512007-08-25 01:37:05 +00001072 } else {
aliguori28a76be2009-03-06 20:27:40 +00001073 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001074 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001075 }
thse25a5822007-08-25 01:36:20 +00001076}
1077
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001078/**
1079 * do_change(): Change a removable medium, or VNC configuration
1080 */
1081static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001082{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001083 const char *device = qdict_get_str(qdict, "device");
1084 const char *target = qdict_get_str(qdict, "target");
1085 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001086 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001087 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001088 } else {
aliguori28a76be2009-03-06 20:27:40 +00001089 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001090 }
1091}
1092
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001093static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001095 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001096}
1097
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001098static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001099{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001100 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001101}
1102
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001103static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001104{
1105 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001106 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001107
bellard9307c4c2004-04-04 12:57:25 +00001108 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001109 mask = 0;
1110 } else {
bellard9307c4c2004-04-04 12:57:25 +00001111 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001112 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001113 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001114 return;
1115 }
1116 }
1117 cpu_set_log(mask);
1118}
1119
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001120static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001121{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001122 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001123 if (!option || !strcmp(option, "on")) {
1124 singlestep = 1;
1125 } else if (!strcmp(option, "off")) {
1126 singlestep = 0;
1127 } else {
1128 monitor_printf(mon, "unexpected option %s\n", option);
1129 }
1130}
1131
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001132/**
1133 * do_stop(): Stop VM execution
1134 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001135static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001136{
1137 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001138 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001139}
1140
aliguoribb5fc202009-03-05 23:01:15 +00001141static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001142
aliguori376253e2009-03-05 23:01:23 +00001143struct bdrv_iterate_context {
1144 Monitor *mon;
1145 int err;
1146};
aliguoric0f4ce72009-03-05 23:01:01 +00001147
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001148/**
1149 * do_cont(): Resume emulation.
1150 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001151static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001152{
1153 struct bdrv_iterate_context context = { mon, 0 };
1154
1155 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001156 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001157 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001158 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001159 return 0;
1160 } else {
1161 return -1;
1162 }
bellard8a7ddc32004-03-31 19:00:16 +00001163}
1164
aliguoribb5fc202009-03-05 23:01:15 +00001165static void bdrv_key_cb(void *opaque, int err)
1166{
aliguori376253e2009-03-05 23:01:23 +00001167 Monitor *mon = opaque;
1168
aliguoribb5fc202009-03-05 23:01:15 +00001169 /* another key was set successfully, retry to continue */
1170 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001171 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001172}
1173
1174static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1175{
aliguori376253e2009-03-05 23:01:23 +00001176 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001177
aliguori376253e2009-03-05 23:01:23 +00001178 if (!context->err && bdrv_key_required(bs)) {
1179 context->err = -EBUSY;
1180 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1181 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001182 }
1183}
1184
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001185static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001186{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001188 if (!device)
1189 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1190 if (gdbserver_start(device) < 0) {
1191 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1192 device);
1193 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001194 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001195 } else {
aliguori59030a82009-04-05 18:43:41 +00001196 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1197 device);
bellard8a7ddc32004-03-31 19:00:16 +00001198 }
1199}
1200
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001202{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001203 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001204 if (select_watchdog_action(action) == -1) {
1205 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1206 }
1207}
1208
aliguori376253e2009-03-05 23:01:23 +00001209static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001210{
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001212 switch(c) {
1213 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 default:
1226 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001228 } else {
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001230 }
1231 break;
1232 }
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001234}
1235
aliguori376253e2009-03-05 23:01:23 +00001236static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001237 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001238{
bellard6a00d602005-11-21 23:25:50 +00001239 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001240 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001241 uint8_t buf[16];
1242 uint64_t v;
1243
1244 if (format == 'i') {
1245 int flags;
1246 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001247 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001248 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001249 return;
bellard9307c4c2004-04-04 12:57:25 +00001250#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001251 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001252 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001253 } else if (wsize == 4) {
1254 flags = 0;
1255 } else {
bellard6a15fd12006-04-12 21:07:07 +00001256 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001257 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001258 if (env) {
1259#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001260 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001261 (env->segs[R_CS].flags & DESC_L_MASK))
1262 flags = 2;
1263 else
1264#endif
1265 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1266 flags = 1;
1267 }
bellard4c27ba22004-04-25 18:05:08 +00001268 }
1269#endif
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001271 return;
1272 }
1273
1274 len = wsize * count;
1275 if (wsize == 1)
1276 line_size = 8;
1277 else
1278 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001279 max_digits = 0;
1280
1281 switch(format) {
1282 case 'o':
1283 max_digits = (wsize * 8 + 2) / 3;
1284 break;
1285 default:
1286 case 'x':
1287 max_digits = (wsize * 8) / 4;
1288 break;
1289 case 'u':
1290 case 'd':
1291 max_digits = (wsize * 8 * 10 + 32) / 33;
1292 break;
1293 case 'c':
1294 wsize = 1;
1295 break;
1296 }
1297
1298 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001299 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001301 else
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001303 l = len;
1304 if (l > line_size)
1305 l = line_size;
1306 if (is_physical) {
1307 cpu_physical_memory_rw(addr, buf, l, 0);
1308 } else {
bellard6a00d602005-11-21 23:25:50 +00001309 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001310 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001312 break;
1313 }
bellard9307c4c2004-04-04 12:57:25 +00001314 }
ths5fafdf22007-09-16 21:08:06 +00001315 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001316 while (i < l) {
1317 switch(wsize) {
1318 default:
1319 case 1:
1320 v = ldub_raw(buf + i);
1321 break;
1322 case 2:
1323 v = lduw_raw(buf + i);
1324 break;
1325 case 4:
bellard92a31b12005-02-10 22:00:52 +00001326 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001327 break;
1328 case 8:
1329 v = ldq_raw(buf + i);
1330 break;
1331 }
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001333 switch(format) {
1334 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001335 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001336 break;
1337 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001339 break;
1340 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001341 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001342 break;
1343 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001347 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 }
1350 i += wsize;
1351 }
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001353 addr += l;
1354 len -= l;
1355 }
1356}
1357
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001358static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001359{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001360 int count = qdict_get_int(qdict, "count");
1361 int format = qdict_get_int(qdict, "format");
1362 int size = qdict_get_int(qdict, "size");
1363 target_long addr = qdict_get_int(qdict, "addr");
1364
aliguori376253e2009-03-05 23:01:23 +00001365 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001366}
1367
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001368static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001369{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001370 int count = qdict_get_int(qdict, "count");
1371 int format = qdict_get_int(qdict, "format");
1372 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001373 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001374
aliguori376253e2009-03-05 23:01:23 +00001375 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001376}
1377
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001378static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001379{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001380 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001381 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001382
blueswir17743e582007-09-24 18:39:04 +00001383#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001384 switch(format) {
1385 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 default:
1395 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001397 break;
1398 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001400 break;
1401 }
bellard92a31b12005-02-10 22:00:52 +00001402#else
1403 switch(format) {
1404 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001405 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001406 break;
1407 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001409 break;
1410 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001412 break;
1413 default:
1414 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001416 break;
1417 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001418 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001419 break;
1420 }
1421#endif
aliguori376253e2009-03-05 23:01:23 +00001422 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001423}
1424
Luiz Capitulino57e09452009-10-16 12:23:43 -03001425static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001426{
1427 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001428 uint32_t size = qdict_get_int(qdict, "size");
1429 const char *filename = qdict_get_str(qdict, "filename");
1430 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001431 uint32_t l;
1432 CPUState *env;
1433 uint8_t buf[1024];
1434
1435 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001436
1437 f = fopen(filename, "wb");
1438 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001439 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
bellardb371dc52007-01-03 15:20:39 +00001440 return;
1441 }
1442 while (size != 0) {
1443 l = sizeof(buf);
1444 if (l > size)
1445 l = size;
1446 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001447 if (fwrite(buf, 1, l, f) != l) {
1448 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1449 goto exit;
1450 }
bellardb371dc52007-01-03 15:20:39 +00001451 addr += l;
1452 size -= l;
1453 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001454exit:
bellardb371dc52007-01-03 15:20:39 +00001455 fclose(f);
1456}
1457
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001458static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1459 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001460{
1461 FILE *f;
1462 uint32_t l;
1463 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001464 uint32_t size = qdict_get_int(qdict, "size");
1465 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001466 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001467
1468 f = fopen(filename, "wb");
1469 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001470 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001471 return;
1472 }
1473 while (size != 0) {
1474 l = sizeof(buf);
1475 if (l > size)
1476 l = size;
1477 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001478 if (fwrite(buf, 1, l, f) != l) {
1479 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1480 goto exit;
1481 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001482 fflush(f);
1483 addr += l;
1484 size -= l;
1485 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001486exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001487 fclose(f);
1488}
1489
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001490static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001491{
1492 uint32_t addr;
1493 uint8_t buf[1];
1494 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001495 uint32_t start = qdict_get_int(qdict, "start");
1496 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001497
1498 sum = 0;
1499 for(addr = start; addr < (start + size); addr++) {
1500 cpu_physical_memory_rw(addr, buf, 1, 0);
1501 /* BSD sum algorithm ('sum' Unix command) */
1502 sum = (sum >> 1) | (sum << 15);
1503 sum += buf[0];
1504 }
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001506}
1507
bellarda3a91a32004-06-04 11:06:21 +00001508typedef struct {
1509 int keycode;
1510 const char *name;
1511} KeyDef;
1512
1513static const KeyDef key_defs[] = {
1514 { 0x2a, "shift" },
1515 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001516
bellarda3a91a32004-06-04 11:06:21 +00001517 { 0x38, "alt" },
1518 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001519 { 0x64, "altgr" },
1520 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001521 { 0x1d, "ctrl" },
1522 { 0x9d, "ctrl_r" },
1523
1524 { 0xdd, "menu" },
1525
1526 { 0x01, "esc" },
1527
1528 { 0x02, "1" },
1529 { 0x03, "2" },
1530 { 0x04, "3" },
1531 { 0x05, "4" },
1532 { 0x06, "5" },
1533 { 0x07, "6" },
1534 { 0x08, "7" },
1535 { 0x09, "8" },
1536 { 0x0a, "9" },
1537 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001538 { 0x0c, "minus" },
1539 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001540 { 0x0e, "backspace" },
1541
1542 { 0x0f, "tab" },
1543 { 0x10, "q" },
1544 { 0x11, "w" },
1545 { 0x12, "e" },
1546 { 0x13, "r" },
1547 { 0x14, "t" },
1548 { 0x15, "y" },
1549 { 0x16, "u" },
1550 { 0x17, "i" },
1551 { 0x18, "o" },
1552 { 0x19, "p" },
1553
1554 { 0x1c, "ret" },
1555
1556 { 0x1e, "a" },
1557 { 0x1f, "s" },
1558 { 0x20, "d" },
1559 { 0x21, "f" },
1560 { 0x22, "g" },
1561 { 0x23, "h" },
1562 { 0x24, "j" },
1563 { 0x25, "k" },
1564 { 0x26, "l" },
1565
1566 { 0x2c, "z" },
1567 { 0x2d, "x" },
1568 { 0x2e, "c" },
1569 { 0x2f, "v" },
1570 { 0x30, "b" },
1571 { 0x31, "n" },
1572 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001573 { 0x33, "comma" },
1574 { 0x34, "dot" },
1575 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001576
balrog4d3b6f62008-02-10 16:33:14 +00001577 { 0x37, "asterisk" },
1578
bellarda3a91a32004-06-04 11:06:21 +00001579 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001580 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001581 { 0x3b, "f1" },
1582 { 0x3c, "f2" },
1583 { 0x3d, "f3" },
1584 { 0x3e, "f4" },
1585 { 0x3f, "f5" },
1586 { 0x40, "f6" },
1587 { 0x41, "f7" },
1588 { 0x42, "f8" },
1589 { 0x43, "f9" },
1590 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001591 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001592 { 0x46, "scroll_lock" },
1593
bellard64866c32006-05-07 18:03:31 +00001594 { 0xb5, "kp_divide" },
1595 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001596 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001597 { 0x4e, "kp_add" },
1598 { 0x9c, "kp_enter" },
1599 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001600 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001601
1602 { 0x52, "kp_0" },
1603 { 0x4f, "kp_1" },
1604 { 0x50, "kp_2" },
1605 { 0x51, "kp_3" },
1606 { 0x4b, "kp_4" },
1607 { 0x4c, "kp_5" },
1608 { 0x4d, "kp_6" },
1609 { 0x47, "kp_7" },
1610 { 0x48, "kp_8" },
1611 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001612
bellarda3a91a32004-06-04 11:06:21 +00001613 { 0x56, "<" },
1614
1615 { 0x57, "f11" },
1616 { 0x58, "f12" },
1617
1618 { 0xb7, "print" },
1619
1620 { 0xc7, "home" },
1621 { 0xc9, "pgup" },
1622 { 0xd1, "pgdn" },
1623 { 0xcf, "end" },
1624
1625 { 0xcb, "left" },
1626 { 0xc8, "up" },
1627 { 0xd0, "down" },
1628 { 0xcd, "right" },
1629
1630 { 0xd2, "insert" },
1631 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001632#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1633 { 0xf0, "stop" },
1634 { 0xf1, "again" },
1635 { 0xf2, "props" },
1636 { 0xf3, "undo" },
1637 { 0xf4, "front" },
1638 { 0xf5, "copy" },
1639 { 0xf6, "open" },
1640 { 0xf7, "paste" },
1641 { 0xf8, "find" },
1642 { 0xf9, "cut" },
1643 { 0xfa, "lf" },
1644 { 0xfb, "help" },
1645 { 0xfc, "meta_l" },
1646 { 0xfd, "meta_r" },
1647 { 0xfe, "compose" },
1648#endif
bellarda3a91a32004-06-04 11:06:21 +00001649 { 0, NULL },
1650};
1651
1652static int get_keycode(const char *key)
1653{
1654 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001655 char *endp;
1656 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001657
1658 for(p = key_defs; p->name != NULL; p++) {
1659 if (!strcmp(key, p->name))
1660 return p->keycode;
1661 }
bellard64866c32006-05-07 18:03:31 +00001662 if (strstart(key, "0x", NULL)) {
1663 ret = strtoul(key, &endp, 0);
1664 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1665 return ret;
1666 }
bellarda3a91a32004-06-04 11:06:21 +00001667 return -1;
1668}
1669
balrogc8256f92008-06-08 22:45:01 +00001670#define MAX_KEYCODES 16
1671static uint8_t keycodes[MAX_KEYCODES];
1672static int nb_pending_keycodes;
1673static QEMUTimer *key_timer;
1674
1675static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001676{
balrogc8256f92008-06-08 22:45:01 +00001677 int keycode;
1678
1679 while (nb_pending_keycodes > 0) {
1680 nb_pending_keycodes--;
1681 keycode = keycodes[nb_pending_keycodes];
1682 if (keycode & 0x80)
1683 kbd_put_keycode(0xe0);
1684 kbd_put_keycode(keycode | 0x80);
1685 }
1686}
1687
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001688static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001689{
balrog3401c0d2008-06-04 10:05:59 +00001690 char keyname_buf[16];
1691 char *separator;
1692 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001693 const char *string = qdict_get_str(qdict, "string");
1694 int has_hold_time = qdict_haskey(qdict, "hold_time");
1695 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001696
balrogc8256f92008-06-08 22:45:01 +00001697 if (nb_pending_keycodes > 0) {
1698 qemu_del_timer(key_timer);
1699 release_keys(NULL);
1700 }
1701 if (!has_hold_time)
1702 hold_time = 100;
1703 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001704 while (1) {
1705 separator = strchr(string, '-');
1706 keyname_len = separator ? separator - string : strlen(string);
1707 if (keyname_len > 0) {
1708 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1709 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001710 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001711 return;
bellarda3a91a32004-06-04 11:06:21 +00001712 }
balrogc8256f92008-06-08 22:45:01 +00001713 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001714 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001715 return;
1716 }
1717 keyname_buf[keyname_len] = 0;
1718 keycode = get_keycode(keyname_buf);
1719 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001720 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001721 return;
1722 }
balrogc8256f92008-06-08 22:45:01 +00001723 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001724 }
balrog3401c0d2008-06-04 10:05:59 +00001725 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001726 break;
balrog3401c0d2008-06-04 10:05:59 +00001727 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001728 }
balrogc8256f92008-06-08 22:45:01 +00001729 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001730 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001731 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001732 keycode = keycodes[i];
1733 if (keycode & 0x80)
1734 kbd_put_keycode(0xe0);
1735 kbd_put_keycode(keycode & 0x7f);
1736 }
balrogc8256f92008-06-08 22:45:01 +00001737 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001738 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001739 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001740}
1741
bellard13224a82006-07-14 22:03:35 +00001742static int mouse_button_state;
1743
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001744static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001745{
1746 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001747 const char *dx_str = qdict_get_str(qdict, "dx_str");
1748 const char *dy_str = qdict_get_str(qdict, "dy_str");
1749 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001750 dx = strtol(dx_str, NULL, 0);
1751 dy = strtol(dy_str, NULL, 0);
1752 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001753 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001754 dz = strtol(dz_str, NULL, 0);
1755 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1756}
1757
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001758static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001759{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001760 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001761 mouse_button_state = button_state;
1762 kbd_mouse_event(0, 0, 0, mouse_button_state);
1763}
1764
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001765static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001766{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001767 int size = qdict_get_int(qdict, "size");
1768 int addr = qdict_get_int(qdict, "addr");
1769 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001770 uint32_t val;
1771 int suffix;
1772
1773 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001774 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001775 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001776 addr++;
1777 }
1778 addr &= 0xffff;
1779
1780 switch(size) {
1781 default:
1782 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001783 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001784 suffix = 'b';
1785 break;
1786 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001787 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001788 suffix = 'w';
1789 break;
1790 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001791 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001792 suffix = 'l';
1793 break;
1794 }
aliguori376253e2009-03-05 23:01:23 +00001795 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1796 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001797}
bellarda3a91a32004-06-04 11:06:21 +00001798
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001799static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001800{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001801 int size = qdict_get_int(qdict, "size");
1802 int addr = qdict_get_int(qdict, "addr");
1803 int val = qdict_get_int(qdict, "val");
1804
Jan Kiszkaf1147842009-07-14 10:20:11 +02001805 addr &= IOPORTS_MASK;
1806
1807 switch (size) {
1808 default:
1809 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001810 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001811 break;
1812 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001813 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001814 break;
1815 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001816 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001817 break;
1818 }
1819}
1820
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001821static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001822{
1823 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001824 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001825
Jan Kiszka76e30d02009-07-02 00:19:02 +02001826 res = qemu_boot_set(bootdevice);
1827 if (res == 0) {
1828 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1829 } else if (res > 0) {
1830 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001831 } else {
aliguori376253e2009-03-05 23:01:23 +00001832 monitor_printf(mon, "no function defined to set boot device list for "
1833 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001834 }
1835}
1836
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001837/**
1838 * do_system_reset(): Issue a machine reset
1839 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001840static int do_system_reset(Monitor *mon, const QDict *qdict,
1841 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001842{
1843 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001844 return 0;
bellarde4f90822004-06-20 12:35:44 +00001845}
1846
Luiz Capitulino43076662009-10-07 13:41:59 -03001847/**
1848 * do_system_powerdown(): Issue a machine powerdown
1849 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001850static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1851 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001852{
1853 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001854 return 0;
bellard34751872005-07-02 14:31:34 +00001855}
1856
bellardb86bda52004-09-18 19:32:46 +00001857#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001858static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001859{
aliguori376253e2009-03-05 23:01:23 +00001860 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1861 addr,
1862 pte & mask,
1863 pte & PG_GLOBAL_MASK ? 'G' : '-',
1864 pte & PG_PSE_MASK ? 'P' : '-',
1865 pte & PG_DIRTY_MASK ? 'D' : '-',
1866 pte & PG_ACCESSED_MASK ? 'A' : '-',
1867 pte & PG_PCD_MASK ? 'C' : '-',
1868 pte & PG_PWT_MASK ? 'T' : '-',
1869 pte & PG_USER_MASK ? 'U' : '-',
1870 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001871}
1872
aliguori376253e2009-03-05 23:01:23 +00001873static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001874{
bellard6a00d602005-11-21 23:25:50 +00001875 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001876 int l1, l2;
1877 uint32_t pgd, pde, pte;
1878
bellard6a00d602005-11-21 23:25:50 +00001879 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001880
bellardb86bda52004-09-18 19:32:46 +00001881 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001882 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001883 return;
1884 }
1885 pgd = env->cr[3] & ~0xfff;
1886 for(l1 = 0; l1 < 1024; l1++) {
1887 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1888 pde = le32_to_cpu(pde);
1889 if (pde & PG_PRESENT_MASK) {
1890 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001891 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001892 } else {
1893 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001894 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001895 (uint8_t *)&pte, 4);
1896 pte = le32_to_cpu(pte);
1897 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001898 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001899 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001900 ~0xfff);
1901 }
1902 }
1903 }
1904 }
1905 }
1906}
1907
aliguori376253e2009-03-05 23:01:23 +00001908static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001909 uint32_t end, int prot)
1910{
bellard9746b152004-11-11 18:30:24 +00001911 int prot1;
1912 prot1 = *plast_prot;
1913 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001914 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001915 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1916 *pstart, end, end - *pstart,
1917 prot1 & PG_USER_MASK ? 'u' : '-',
1918 'r',
1919 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001920 }
1921 if (prot != 0)
1922 *pstart = end;
1923 else
1924 *pstart = -1;
1925 *plast_prot = prot;
1926 }
1927}
1928
aliguori376253e2009-03-05 23:01:23 +00001929static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001930{
bellard6a00d602005-11-21 23:25:50 +00001931 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001932 int l1, l2, prot, last_prot;
1933 uint32_t pgd, pde, pte, start, end;
1934
bellard6a00d602005-11-21 23:25:50 +00001935 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001936
bellardb86bda52004-09-18 19:32:46 +00001937 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001938 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001939 return;
1940 }
1941 pgd = env->cr[3] & ~0xfff;
1942 last_prot = 0;
1943 start = -1;
1944 for(l1 = 0; l1 < 1024; l1++) {
1945 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1946 pde = le32_to_cpu(pde);
1947 end = l1 << 22;
1948 if (pde & PG_PRESENT_MASK) {
1949 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1950 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001951 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001952 } else {
1953 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001954 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001955 (uint8_t *)&pte, 4);
1956 pte = le32_to_cpu(pte);
1957 end = (l1 << 22) + (l2 << 12);
1958 if (pte & PG_PRESENT_MASK) {
1959 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1960 } else {
1961 prot = 0;
1962 }
aliguori376253e2009-03-05 23:01:23 +00001963 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001964 }
1965 }
1966 } else {
1967 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001968 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001969 }
1970 }
1971}
1972#endif
1973
aurel327c664e22009-03-03 06:12:22 +00001974#if defined(TARGET_SH4)
1975
aliguori376253e2009-03-05 23:01:23 +00001976static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001977{
aliguori376253e2009-03-05 23:01:23 +00001978 monitor_printf(mon, " tlb%i:\t"
1979 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1980 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1981 "dirty=%hhu writethrough=%hhu\n",
1982 idx,
1983 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1984 tlb->v, tlb->sh, tlb->c, tlb->pr,
1985 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001986}
1987
aliguori376253e2009-03-05 23:01:23 +00001988static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001989{
1990 CPUState *env = mon_get_cpu();
1991 int i;
1992
aliguori376253e2009-03-05 23:01:23 +00001993 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001994 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001995 print_tlb (mon, i, &env->itlb[i]);
1996 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001997 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001998 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001999}
2000
2001#endif
2002
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002003static void do_info_kvm_print(Monitor *mon, const QObject *data)
2004{
2005 QDict *qdict;
2006
2007 qdict = qobject_to_qdict(data);
2008
2009 monitor_printf(mon, "kvm support: ");
2010 if (qdict_get_bool(qdict, "present")) {
2011 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2012 "enabled" : "disabled");
2013 } else {
2014 monitor_printf(mon, "not compiled\n");
2015 }
2016}
2017
2018/**
2019 * do_info_kvm(): Show KVM information
2020 *
2021 * Return a QDict with the following information:
2022 *
2023 * - "enabled": true if KVM support is enabled, false otherwise
2024 * - "present": true if QEMU has KVM support, false otherwise
2025 *
2026 * Example:
2027 *
2028 * { "enabled": true, "present": true }
2029 */
2030static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002031{
2032#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002033 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2034 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002035#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002036 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002037#endif
2038}
2039
aliguori030ea372009-04-21 22:30:47 +00002040static void do_info_numa(Monitor *mon)
2041{
aliguorib28b6232009-04-22 20:20:29 +00002042 int i;
aliguori030ea372009-04-21 22:30:47 +00002043 CPUState *env;
2044
2045 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2046 for (i = 0; i < nb_numa_nodes; i++) {
2047 monitor_printf(mon, "node %d cpus:", i);
2048 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2049 if (env->numa_node == i) {
2050 monitor_printf(mon, " %d", env->cpu_index);
2051 }
2052 }
2053 monitor_printf(mon, "\n");
2054 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2055 node_mem[i] >> 20);
2056 }
2057}
2058
bellard5f1ce942006-02-08 22:40:15 +00002059#ifdef CONFIG_PROFILER
2060
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002061int64_t qemu_time;
2062int64_t dev_time;
2063
aliguori376253e2009-03-05 23:01:23 +00002064static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002065{
2066 int64_t total;
2067 total = qemu_time;
2068 if (total == 0)
2069 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002070 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002071 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002072 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002073 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002074 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002075 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002076}
2077#else
aliguori376253e2009-03-05 23:01:23 +00002078static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002079{
aliguori376253e2009-03-05 23:01:23 +00002080 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002081}
2082#endif
2083
bellardec36b692006-07-16 18:57:03 +00002084/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002085static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002086
aliguori376253e2009-03-05 23:01:23 +00002087static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002088{
2089 int i;
2090 CaptureState *s;
2091
2092 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002093 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002094 s->ops.info (s->opaque);
2095 }
2096}
2097
Blue Swirl23130862009-06-06 08:22:04 +00002098#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002099static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002100{
2101 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002102 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002103 CaptureState *s;
2104
2105 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2106 if (i == n) {
2107 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002108 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002109 qemu_free (s);
2110 return;
2111 }
2112 }
2113}
2114
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002115static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002116{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002117 const char *path = qdict_get_str(qdict, "path");
2118 int has_freq = qdict_haskey(qdict, "freq");
2119 int freq = qdict_get_try_int(qdict, "freq", -1);
2120 int has_bits = qdict_haskey(qdict, "bits");
2121 int bits = qdict_get_try_int(qdict, "bits", -1);
2122 int has_channels = qdict_haskey(qdict, "nchannels");
2123 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002124 CaptureState *s;
2125
2126 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002127
2128 freq = has_freq ? freq : 44100;
2129 bits = has_bits ? bits : 16;
2130 nchannels = has_channels ? nchannels : 2;
2131
2132 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002133 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002134 qemu_free (s);
2135 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002136 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002137}
2138#endif
2139
aurel32dc1c0b72008-04-27 23:52:12 +00002140#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002141static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002142{
2143 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002144 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002145
2146 for (env = first_cpu; env != NULL; env = env->next_cpu)
2147 if (env->cpu_index == cpu_index) {
2148 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2149 break;
2150 }
2151}
2152#endif
2153
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002154static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002155{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002156 QDict *qdict;
2157
2158 qdict = qobject_to_qdict(data);
2159
2160 monitor_printf(mon, "VM status: ");
2161 if (qdict_get_bool(qdict, "running")) {
2162 monitor_printf(mon, "running");
2163 if (qdict_get_bool(qdict, "singlestep")) {
2164 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002165 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002166 } else {
2167 monitor_printf(mon, "paused");
2168 }
2169
2170 monitor_printf(mon, "\n");
2171}
2172
2173/**
2174 * do_info_status(): VM status
2175 *
2176 * Return a QDict with the following information:
2177 *
2178 * - "running": true if the VM is running, or false if it is paused
2179 * - "singlestep": true if the VM is in single step mode, false otherwise
2180 *
2181 * Example:
2182 *
2183 * { "running": true, "singlestep": false }
2184 */
2185static void do_info_status(Monitor *mon, QObject **ret_data)
2186{
2187 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2188 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002189}
2190
Adam Litke625a5be2010-01-26 14:17:35 -06002191static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002192{
Adam Litke625a5be2010-01-26 14:17:35 -06002193 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002194
Adam Litke625a5be2010-01-26 14:17:35 -06002195 if (strcmp(key, "actual"))
2196 monitor_printf(mon, ",%s=%" PRId64, key,
2197 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002198}
2199
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002200static void monitor_print_balloon(Monitor *mon, const QObject *data)
2201{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002202 QDict *qdict;
2203
2204 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002205 if (!qdict_haskey(qdict, "actual"))
2206 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002207
Adam Litke625a5be2010-01-26 14:17:35 -06002208 monitor_printf(mon, "balloon: actual=%" PRId64,
2209 qdict_get_int(qdict, "actual") >> 20);
2210 qdict_iter(qdict, print_balloon_stat, mon);
2211 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002212}
2213
2214/**
2215 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002216 *
Adam Litke625a5be2010-01-26 14:17:35 -06002217 * Make an asynchronous request for balloon info. When the request completes
2218 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002219 *
Adam Litke625a5be2010-01-26 14:17:35 -06002220 * - "actual": current balloon value in bytes
2221 * The following fields may or may not be present:
2222 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2223 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2224 * - "major_page_faults": Number of major faults
2225 * - "minor_page_faults": Number of minor faults
2226 * - "free_mem": Total amount of free and unused memory (bytes)
2227 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002228 *
2229 * Example:
2230 *
Adam Litke625a5be2010-01-26 14:17:35 -06002231 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2232 * "major_page_faults": 142, "minor_page_faults": 239245,
2233 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002234 */
Adam Litke625a5be2010-01-26 14:17:35 -06002235static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002236{
Adam Litke625a5be2010-01-26 14:17:35 -06002237 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002238
Adam Litke625a5be2010-01-26 14:17:35 -06002239 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2240 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2241 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002242 }
Adam Litke625a5be2010-01-26 14:17:35 -06002243
2244 ret = qemu_balloon_status(cb, opaque);
2245 if (!ret) {
2246 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2247 return -1;
2248 }
2249
2250 return 0;
2251}
2252
2253/**
2254 * do_balloon(): Request VM to change its memory allocation
2255 */
2256static int do_balloon(Monitor *mon, const QDict *params,
2257 MonitorCompletion cb, void *opaque)
2258{
2259 int ret;
2260
2261 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2262 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2263 return -1;
2264 }
2265
2266 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2267 if (ret == 0) {
2268 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2269 return -1;
2270 }
2271
2272 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002273}
2274
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002275static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002276{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002277 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002278
aliguori76655d62009-03-06 20:27:37 +00002279 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002280 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002281 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002282 return acl;
2283}
aliguori76655d62009-03-06 20:27:37 +00002284
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002285static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002286{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002287 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002288 qemu_acl *acl = find_acl(mon, aclname);
2289 qemu_acl_entry *entry;
2290 int i = 0;
2291
2292 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002293 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002294 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002295 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002296 i++;
2297 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002298 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002299 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002300 }
2301}
2302
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002303static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002304{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002305 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002306 qemu_acl *acl = find_acl(mon, aclname);
2307
2308 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002309 qemu_acl_reset(acl);
2310 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002311 }
2312}
aliguori76655d62009-03-06 20:27:37 +00002313
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002314static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002315{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002316 const char *aclname = qdict_get_str(qdict, "aclname");
2317 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002318 qemu_acl *acl = find_acl(mon, aclname);
2319
2320 if (acl) {
2321 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002322 acl->defaultDeny = 0;
2323 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002324 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002325 acl->defaultDeny = 1;
2326 monitor_printf(mon, "acl: policy set to 'deny'\n");
2327 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002328 monitor_printf(mon, "acl: unknown policy '%s', "
2329 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002330 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002331 }
2332}
aliguori76655d62009-03-06 20:27:37 +00002333
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002334static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002335{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002336 const char *aclname = qdict_get_str(qdict, "aclname");
2337 const char *match = qdict_get_str(qdict, "match");
2338 const char *policy = qdict_get_str(qdict, "policy");
2339 int has_index = qdict_haskey(qdict, "index");
2340 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002341 qemu_acl *acl = find_acl(mon, aclname);
2342 int deny, ret;
2343
2344 if (acl) {
2345 if (strcmp(policy, "allow") == 0) {
2346 deny = 0;
2347 } else if (strcmp(policy, "deny") == 0) {
2348 deny = 1;
2349 } else {
2350 monitor_printf(mon, "acl: unknown policy '%s', "
2351 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002352 return;
2353 }
aliguori28a76be2009-03-06 20:27:40 +00002354 if (has_index)
2355 ret = qemu_acl_insert(acl, deny, match, index);
2356 else
2357 ret = qemu_acl_append(acl, deny, match);
2358 if (ret < 0)
2359 monitor_printf(mon, "acl: unable to add acl entry\n");
2360 else
2361 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002362 }
2363}
aliguori76655d62009-03-06 20:27:37 +00002364
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002365static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002366{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002367 const char *aclname = qdict_get_str(qdict, "aclname");
2368 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002369 qemu_acl *acl = find_acl(mon, aclname);
2370 int ret;
aliguori76655d62009-03-06 20:27:37 +00002371
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002372 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002373 ret = qemu_acl_remove(acl, match);
2374 if (ret < 0)
2375 monitor_printf(mon, "acl: no matching acl entry\n");
2376 else
2377 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002378 }
2379}
2380
Huang Ying79c4f6b2009-06-23 10:05:14 +08002381#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002382static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002383{
2384 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002385 int cpu_index = qdict_get_int(qdict, "cpu_index");
2386 int bank = qdict_get_int(qdict, "bank");
2387 uint64_t status = qdict_get_int(qdict, "status");
2388 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2389 uint64_t addr = qdict_get_int(qdict, "addr");
2390 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002391
2392 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2393 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2394 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2395 break;
2396 }
2397}
2398#endif
2399
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002400static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002401{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002402 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002403 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002404 int fd;
2405
2406 fd = qemu_chr_get_msgfd(mon->chr);
2407 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002408 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002409 return;
2410 }
2411
2412 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002413 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002414 return;
2415 }
2416
2417 fd = dup(fd);
2418 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002419 if (errno == EMFILE)
2420 qemu_error_new(QERR_TOO_MANY_FILES);
2421 else
2422 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002423 return;
2424 }
2425
Blue Swirl72cf2d42009-09-12 07:36:22 +00002426 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002427 if (strcmp(monfd->name, fdname) != 0) {
2428 continue;
2429 }
2430
2431 close(monfd->fd);
2432 monfd->fd = fd;
2433 return;
2434 }
2435
Anthony Liguoric227f092009-10-01 16:12:16 -05002436 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002437 monfd->name = qemu_strdup(fdname);
2438 monfd->fd = fd;
2439
Blue Swirl72cf2d42009-09-12 07:36:22 +00002440 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002441}
2442
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002443static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002444{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002445 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002446 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002447
Blue Swirl72cf2d42009-09-12 07:36:22 +00002448 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002449 if (strcmp(monfd->name, fdname) != 0) {
2450 continue;
2451 }
2452
Blue Swirl72cf2d42009-09-12 07:36:22 +00002453 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002454 close(monfd->fd);
2455 qemu_free(monfd->name);
2456 qemu_free(monfd);
2457 return;
2458 }
2459
Markus Armbruster063c1a02009-12-07 21:37:11 +01002460 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002461}
2462
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002463static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002464{
2465 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002466 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002467
2468 vm_stop(0);
2469
Juan Quintela05f24012009-08-20 19:42:22 +02002470 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002471 vm_start();
2472}
2473
Mark McLoughlin7768e042009-07-22 09:11:41 +01002474int monitor_get_fd(Monitor *mon, const char *fdname)
2475{
Anthony Liguoric227f092009-10-01 16:12:16 -05002476 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002477
Blue Swirl72cf2d42009-09-12 07:36:22 +00002478 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002479 int fd;
2480
2481 if (strcmp(monfd->name, fdname) != 0) {
2482 continue;
2483 }
2484
2485 fd = monfd->fd;
2486
2487 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002488 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002489 qemu_free(monfd->name);
2490 qemu_free(monfd);
2491
2492 return fd;
2493 }
2494
2495 return -1;
2496}
2497
Anthony Liguoric227f092009-10-01 16:12:16 -05002498static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002499#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002500 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002501};
2502
Blue Swirl23130862009-06-06 08:22:04 +00002503/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002504static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002505 {
2506 .name = "version",
2507 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002508 .params = "",
2509 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002510 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002511 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 },
2513 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002514 .name = "commands",
2515 .args_type = "",
2516 .params = "",
2517 .help = "list QMP available commands",
2518 .user_print = monitor_user_noop,
2519 .mhandler.info_new = do_info_commands,
2520 },
2521 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002522 .name = "network",
2523 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 .params = "",
2525 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002526 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 },
2528 {
2529 .name = "chardev",
2530 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 .params = "",
2532 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002533 .user_print = qemu_chr_info_print,
2534 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002535 },
2536 {
2537 .name = "block",
2538 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002539 .params = "",
2540 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002541 .user_print = bdrv_info_print,
2542 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002543 },
2544 {
2545 .name = "blockstats",
2546 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 .params = "",
2548 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002549 .user_print = bdrv_stats_print,
2550 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
2553 .name = "registers",
2554 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .params = "",
2556 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002557 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 },
2559 {
2560 .name = "cpus",
2561 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 .params = "",
2563 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002564 .user_print = monitor_print_cpus,
2565 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 },
2567 {
2568 .name = "history",
2569 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 .params = "",
2571 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002572 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 },
2574 {
2575 .name = "irq",
2576 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 .params = "",
2578 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002579 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = "pic",
2583 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 .params = "",
2585 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002586 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
2588 {
2589 .name = "pci",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002593 .user_print = do_pci_info_print,
2594 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 },
aurel327c664e22009-03-03 06:12:22 +00002596#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 {
2598 .name = "tlb",
2599 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 .params = "",
2601 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002602 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 },
aurel327c664e22009-03-03 06:12:22 +00002604#endif
2605#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 {
2607 .name = "mem",
2608 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 .params = "",
2610 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002611 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 },
2613 {
2614 .name = "hpet",
2615 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 .params = "",
2617 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002618 .user_print = do_info_hpet_print,
2619 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 },
bellardb86bda52004-09-18 19:32:46 +00002621#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 {
2623 .name = "jit",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002627 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 },
2629 {
2630 .name = "kvm",
2631 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 .params = "",
2633 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002634 .user_print = do_info_kvm_print,
2635 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
2637 {
2638 .name = "numa",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002642 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
2644 {
2645 .name = "usb",
2646 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 .params = "",
2648 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002649 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 },
2651 {
2652 .name = "usbhost",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002656 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
2659 .name = "profile",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002663 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 },
2665 {
2666 .name = "capture",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002670 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
2673 .name = "snapshots",
2674 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 .params = "",
2676 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002677 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 },
2679 {
2680 .name = "status",
2681 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .params = "",
2683 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002684 .user_print = do_info_status_print,
2685 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 },
2687 {
2688 .name = "pcmcia",
2689 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 .params = "",
2691 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002692 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 },
2694 {
2695 .name = "mice",
2696 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 .params = "",
2698 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002699 .user_print = do_info_mice_print,
2700 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 },
2702 {
2703 .name = "vnc",
2704 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 .params = "",
2706 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002707 .user_print = do_info_vnc_print,
2708 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 },
2710 {
2711 .name = "name",
2712 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 .params = "",
2714 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002715 .user_print = do_info_name_print,
2716 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 },
2718 {
2719 .name = "uuid",
2720 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 .params = "",
2722 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002723 .user_print = do_info_uuid_print,
2724 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 },
j_mayer76a66252007-03-07 08:32:30 +00002726#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 {
2728 .name = "cpustats",
2729 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 .params = "",
2731 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002732 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 },
j_mayer76a66252007-03-07 08:32:30 +00002734#endif
blueswir131a60e22007-10-26 18:42:59 +00002735#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 {
2737 .name = "usernet",
2738 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 .params = "",
2740 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002741 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 },
blueswir131a60e22007-10-26 18:42:59 +00002743#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 {
2745 .name = "migrate",
2746 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 .params = "",
2748 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002749 .user_print = do_info_migrate_print,
2750 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 },
2752 {
2753 .name = "balloon",
2754 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 .params = "",
2756 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002757 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002758 .mhandler.info_async = do_info_balloon,
2759 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
2761 {
2762 .name = "qtree",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002766 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 },
2768 {
2769 .name = "qdm",
2770 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .params = "",
2772 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002773 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 },
2775 {
2776 .name = "roms",
2777 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 .params = "",
2779 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002780 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 },
2782 {
2783 .name = NULL,
2784 },
bellard9dc39cb2004-03-14 21:38:27 +00002785};
2786
bellard9307c4c2004-04-04 12:57:25 +00002787/*******************************************************************/
2788
2789static const char *pch;
2790static jmp_buf expr_env;
2791
bellard92a31b12005-02-10 22:00:52 +00002792#define MD_TLONG 0
2793#define MD_I32 1
2794
bellard9307c4c2004-04-04 12:57:25 +00002795typedef struct MonitorDef {
2796 const char *name;
2797 int offset;
blueswir18662d652008-10-02 18:32:44 +00002798 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002799 int type;
bellard9307c4c2004-04-04 12:57:25 +00002800} MonitorDef;
2801
bellard57206fd2004-04-25 18:54:52 +00002802#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002803static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002804{
bellard6a00d602005-11-21 23:25:50 +00002805 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002806 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002807}
2808#endif
2809
bellarda541f292004-04-12 20:39:29 +00002810#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002811static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002812{
bellard6a00d602005-11-21 23:25:50 +00002813 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002814 unsigned int u;
2815 int i;
2816
2817 u = 0;
2818 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002819 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002820
2821 return u;
2822}
2823
blueswir18662d652008-10-02 18:32:44 +00002824static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002825{
bellard6a00d602005-11-21 23:25:50 +00002826 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002827 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002828}
2829
blueswir18662d652008-10-02 18:32:44 +00002830static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002831{
bellard6a00d602005-11-21 23:25:50 +00002832 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002833 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002834}
bellard9fddaa02004-05-21 12:59:32 +00002835
blueswir18662d652008-10-02 18:32:44 +00002836static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002837{
bellard6a00d602005-11-21 23:25:50 +00002838 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002839 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002840}
2841
blueswir18662d652008-10-02 18:32:44 +00002842static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002843{
bellard6a00d602005-11-21 23:25:50 +00002844 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002845 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002846}
2847
blueswir18662d652008-10-02 18:32:44 +00002848static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002849{
bellard6a00d602005-11-21 23:25:50 +00002850 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002851 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002852}
bellarda541f292004-04-12 20:39:29 +00002853#endif
2854
bellarde95c8d52004-09-30 22:22:08 +00002855#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002856#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002857static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002858{
bellard6a00d602005-11-21 23:25:50 +00002859 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002860 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002861}
bellard7b936c02005-10-30 17:05:13 +00002862#endif
bellarde95c8d52004-09-30 22:22:08 +00002863
blueswir18662d652008-10-02 18:32:44 +00002864static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002865{
bellard6a00d602005-11-21 23:25:50 +00002866 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002867 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002868}
2869#endif
2870
blueswir18662d652008-10-02 18:32:44 +00002871static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002872#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002873
2874#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002875 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002876 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002877 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002878
bellard9307c4c2004-04-04 12:57:25 +00002879 { "eax", offsetof(CPUState, regs[0]) },
2880 { "ecx", offsetof(CPUState, regs[1]) },
2881 { "edx", offsetof(CPUState, regs[2]) },
2882 { "ebx", offsetof(CPUState, regs[3]) },
2883 { "esp|sp", offsetof(CPUState, regs[4]) },
2884 { "ebp|fp", offsetof(CPUState, regs[5]) },
2885 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002886 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002887#ifdef TARGET_X86_64
2888 { "r8", offsetof(CPUState, regs[8]) },
2889 { "r9", offsetof(CPUState, regs[9]) },
2890 { "r10", offsetof(CPUState, regs[10]) },
2891 { "r11", offsetof(CPUState, regs[11]) },
2892 { "r12", offsetof(CPUState, regs[12]) },
2893 { "r13", offsetof(CPUState, regs[13]) },
2894 { "r14", offsetof(CPUState, regs[14]) },
2895 { "r15", offsetof(CPUState, regs[15]) },
2896#endif
bellard9307c4c2004-04-04 12:57:25 +00002897 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002898 { "eip", offsetof(CPUState, eip) },
2899 SEG("cs", R_CS)
2900 SEG("ds", R_DS)
2901 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002902 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002903 SEG("fs", R_FS)
2904 SEG("gs", R_GS)
2905 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002906#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002907 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002908 { "r0", offsetof(CPUState, gpr[0]) },
2909 { "r1", offsetof(CPUState, gpr[1]) },
2910 { "r2", offsetof(CPUState, gpr[2]) },
2911 { "r3", offsetof(CPUState, gpr[3]) },
2912 { "r4", offsetof(CPUState, gpr[4]) },
2913 { "r5", offsetof(CPUState, gpr[5]) },
2914 { "r6", offsetof(CPUState, gpr[6]) },
2915 { "r7", offsetof(CPUState, gpr[7]) },
2916 { "r8", offsetof(CPUState, gpr[8]) },
2917 { "r9", offsetof(CPUState, gpr[9]) },
2918 { "r10", offsetof(CPUState, gpr[10]) },
2919 { "r11", offsetof(CPUState, gpr[11]) },
2920 { "r12", offsetof(CPUState, gpr[12]) },
2921 { "r13", offsetof(CPUState, gpr[13]) },
2922 { "r14", offsetof(CPUState, gpr[14]) },
2923 { "r15", offsetof(CPUState, gpr[15]) },
2924 { "r16", offsetof(CPUState, gpr[16]) },
2925 { "r17", offsetof(CPUState, gpr[17]) },
2926 { "r18", offsetof(CPUState, gpr[18]) },
2927 { "r19", offsetof(CPUState, gpr[19]) },
2928 { "r20", offsetof(CPUState, gpr[20]) },
2929 { "r21", offsetof(CPUState, gpr[21]) },
2930 { "r22", offsetof(CPUState, gpr[22]) },
2931 { "r23", offsetof(CPUState, gpr[23]) },
2932 { "r24", offsetof(CPUState, gpr[24]) },
2933 { "r25", offsetof(CPUState, gpr[25]) },
2934 { "r26", offsetof(CPUState, gpr[26]) },
2935 { "r27", offsetof(CPUState, gpr[27]) },
2936 { "r28", offsetof(CPUState, gpr[28]) },
2937 { "r29", offsetof(CPUState, gpr[29]) },
2938 { "r30", offsetof(CPUState, gpr[30]) },
2939 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002940 /* Floating point registers */
2941 { "f0", offsetof(CPUState, fpr[0]) },
2942 { "f1", offsetof(CPUState, fpr[1]) },
2943 { "f2", offsetof(CPUState, fpr[2]) },
2944 { "f3", offsetof(CPUState, fpr[3]) },
2945 { "f4", offsetof(CPUState, fpr[4]) },
2946 { "f5", offsetof(CPUState, fpr[5]) },
2947 { "f6", offsetof(CPUState, fpr[6]) },
2948 { "f7", offsetof(CPUState, fpr[7]) },
2949 { "f8", offsetof(CPUState, fpr[8]) },
2950 { "f9", offsetof(CPUState, fpr[9]) },
2951 { "f10", offsetof(CPUState, fpr[10]) },
2952 { "f11", offsetof(CPUState, fpr[11]) },
2953 { "f12", offsetof(CPUState, fpr[12]) },
2954 { "f13", offsetof(CPUState, fpr[13]) },
2955 { "f14", offsetof(CPUState, fpr[14]) },
2956 { "f15", offsetof(CPUState, fpr[15]) },
2957 { "f16", offsetof(CPUState, fpr[16]) },
2958 { "f17", offsetof(CPUState, fpr[17]) },
2959 { "f18", offsetof(CPUState, fpr[18]) },
2960 { "f19", offsetof(CPUState, fpr[19]) },
2961 { "f20", offsetof(CPUState, fpr[20]) },
2962 { "f21", offsetof(CPUState, fpr[21]) },
2963 { "f22", offsetof(CPUState, fpr[22]) },
2964 { "f23", offsetof(CPUState, fpr[23]) },
2965 { "f24", offsetof(CPUState, fpr[24]) },
2966 { "f25", offsetof(CPUState, fpr[25]) },
2967 { "f26", offsetof(CPUState, fpr[26]) },
2968 { "f27", offsetof(CPUState, fpr[27]) },
2969 { "f28", offsetof(CPUState, fpr[28]) },
2970 { "f29", offsetof(CPUState, fpr[29]) },
2971 { "f30", offsetof(CPUState, fpr[30]) },
2972 { "f31", offsetof(CPUState, fpr[31]) },
2973 { "fpscr", offsetof(CPUState, fpscr) },
2974 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002975 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002976 { "lr", offsetof(CPUState, lr) },
2977 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002978 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002979 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002980 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002981 { "msr", 0, &monitor_get_msr, },
2982 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002983 { "tbu", 0, &monitor_get_tbu, },
2984 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002985#if defined(TARGET_PPC64)
2986 /* Address space register */
2987 { "asr", offsetof(CPUState, asr) },
2988#endif
2989 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002990 { "sdr1", offsetof(CPUState, sdr1) },
2991 { "sr0", offsetof(CPUState, sr[0]) },
2992 { "sr1", offsetof(CPUState, sr[1]) },
2993 { "sr2", offsetof(CPUState, sr[2]) },
2994 { "sr3", offsetof(CPUState, sr[3]) },
2995 { "sr4", offsetof(CPUState, sr[4]) },
2996 { "sr5", offsetof(CPUState, sr[5]) },
2997 { "sr6", offsetof(CPUState, sr[6]) },
2998 { "sr7", offsetof(CPUState, sr[7]) },
2999 { "sr8", offsetof(CPUState, sr[8]) },
3000 { "sr9", offsetof(CPUState, sr[9]) },
3001 { "sr10", offsetof(CPUState, sr[10]) },
3002 { "sr11", offsetof(CPUState, sr[11]) },
3003 { "sr12", offsetof(CPUState, sr[12]) },
3004 { "sr13", offsetof(CPUState, sr[13]) },
3005 { "sr14", offsetof(CPUState, sr[14]) },
3006 { "sr15", offsetof(CPUState, sr[15]) },
3007 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003008#elif defined(TARGET_SPARC)
3009 { "g0", offsetof(CPUState, gregs[0]) },
3010 { "g1", offsetof(CPUState, gregs[1]) },
3011 { "g2", offsetof(CPUState, gregs[2]) },
3012 { "g3", offsetof(CPUState, gregs[3]) },
3013 { "g4", offsetof(CPUState, gregs[4]) },
3014 { "g5", offsetof(CPUState, gregs[5]) },
3015 { "g6", offsetof(CPUState, gregs[6]) },
3016 { "g7", offsetof(CPUState, gregs[7]) },
3017 { "o0", 0, monitor_get_reg },
3018 { "o1", 1, monitor_get_reg },
3019 { "o2", 2, monitor_get_reg },
3020 { "o3", 3, monitor_get_reg },
3021 { "o4", 4, monitor_get_reg },
3022 { "o5", 5, monitor_get_reg },
3023 { "o6", 6, monitor_get_reg },
3024 { "o7", 7, monitor_get_reg },
3025 { "l0", 8, monitor_get_reg },
3026 { "l1", 9, monitor_get_reg },
3027 { "l2", 10, monitor_get_reg },
3028 { "l3", 11, monitor_get_reg },
3029 { "l4", 12, monitor_get_reg },
3030 { "l5", 13, monitor_get_reg },
3031 { "l6", 14, monitor_get_reg },
3032 { "l7", 15, monitor_get_reg },
3033 { "i0", 16, monitor_get_reg },
3034 { "i1", 17, monitor_get_reg },
3035 { "i2", 18, monitor_get_reg },
3036 { "i3", 19, monitor_get_reg },
3037 { "i4", 20, monitor_get_reg },
3038 { "i5", 21, monitor_get_reg },
3039 { "i6", 22, monitor_get_reg },
3040 { "i7", 23, monitor_get_reg },
3041 { "pc", offsetof(CPUState, pc) },
3042 { "npc", offsetof(CPUState, npc) },
3043 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003044#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003045 { "psr", 0, &monitor_get_psr, },
3046 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003047#endif
bellarde95c8d52004-09-30 22:22:08 +00003048 { "tbr", offsetof(CPUState, tbr) },
3049 { "fsr", offsetof(CPUState, fsr) },
3050 { "f0", offsetof(CPUState, fpr[0]) },
3051 { "f1", offsetof(CPUState, fpr[1]) },
3052 { "f2", offsetof(CPUState, fpr[2]) },
3053 { "f3", offsetof(CPUState, fpr[3]) },
3054 { "f4", offsetof(CPUState, fpr[4]) },
3055 { "f5", offsetof(CPUState, fpr[5]) },
3056 { "f6", offsetof(CPUState, fpr[6]) },
3057 { "f7", offsetof(CPUState, fpr[7]) },
3058 { "f8", offsetof(CPUState, fpr[8]) },
3059 { "f9", offsetof(CPUState, fpr[9]) },
3060 { "f10", offsetof(CPUState, fpr[10]) },
3061 { "f11", offsetof(CPUState, fpr[11]) },
3062 { "f12", offsetof(CPUState, fpr[12]) },
3063 { "f13", offsetof(CPUState, fpr[13]) },
3064 { "f14", offsetof(CPUState, fpr[14]) },
3065 { "f15", offsetof(CPUState, fpr[15]) },
3066 { "f16", offsetof(CPUState, fpr[16]) },
3067 { "f17", offsetof(CPUState, fpr[17]) },
3068 { "f18", offsetof(CPUState, fpr[18]) },
3069 { "f19", offsetof(CPUState, fpr[19]) },
3070 { "f20", offsetof(CPUState, fpr[20]) },
3071 { "f21", offsetof(CPUState, fpr[21]) },
3072 { "f22", offsetof(CPUState, fpr[22]) },
3073 { "f23", offsetof(CPUState, fpr[23]) },
3074 { "f24", offsetof(CPUState, fpr[24]) },
3075 { "f25", offsetof(CPUState, fpr[25]) },
3076 { "f26", offsetof(CPUState, fpr[26]) },
3077 { "f27", offsetof(CPUState, fpr[27]) },
3078 { "f28", offsetof(CPUState, fpr[28]) },
3079 { "f29", offsetof(CPUState, fpr[29]) },
3080 { "f30", offsetof(CPUState, fpr[30]) },
3081 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003082#ifdef TARGET_SPARC64
3083 { "f32", offsetof(CPUState, fpr[32]) },
3084 { "f34", offsetof(CPUState, fpr[34]) },
3085 { "f36", offsetof(CPUState, fpr[36]) },
3086 { "f38", offsetof(CPUState, fpr[38]) },
3087 { "f40", offsetof(CPUState, fpr[40]) },
3088 { "f42", offsetof(CPUState, fpr[42]) },
3089 { "f44", offsetof(CPUState, fpr[44]) },
3090 { "f46", offsetof(CPUState, fpr[46]) },
3091 { "f48", offsetof(CPUState, fpr[48]) },
3092 { "f50", offsetof(CPUState, fpr[50]) },
3093 { "f52", offsetof(CPUState, fpr[52]) },
3094 { "f54", offsetof(CPUState, fpr[54]) },
3095 { "f56", offsetof(CPUState, fpr[56]) },
3096 { "f58", offsetof(CPUState, fpr[58]) },
3097 { "f60", offsetof(CPUState, fpr[60]) },
3098 { "f62", offsetof(CPUState, fpr[62]) },
3099 { "asi", offsetof(CPUState, asi) },
3100 { "pstate", offsetof(CPUState, pstate) },
3101 { "cansave", offsetof(CPUState, cansave) },
3102 { "canrestore", offsetof(CPUState, canrestore) },
3103 { "otherwin", offsetof(CPUState, otherwin) },
3104 { "wstate", offsetof(CPUState, wstate) },
3105 { "cleanwin", offsetof(CPUState, cleanwin) },
3106 { "fprs", offsetof(CPUState, fprs) },
3107#endif
bellard9307c4c2004-04-04 12:57:25 +00003108#endif
3109 { NULL },
3110};
3111
aliguori376253e2009-03-05 23:01:23 +00003112static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003113{
aliguori376253e2009-03-05 23:01:23 +00003114 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003115 longjmp(expr_env, 1);
3116}
3117
Markus Armbruster09b94182010-01-20 13:07:30 +01003118/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003119static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003120{
blueswir18662d652008-10-02 18:32:44 +00003121 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003122 void *ptr;
3123
bellard9307c4c2004-04-04 12:57:25 +00003124 for(md = monitor_defs; md->name != NULL; md++) {
3125 if (compare_cmd(name, md->name)) {
3126 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003127 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003128 } else {
bellard6a00d602005-11-21 23:25:50 +00003129 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003130 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003131 switch(md->type) {
3132 case MD_I32:
3133 *pval = *(int32_t *)ptr;
3134 break;
3135 case MD_TLONG:
3136 *pval = *(target_long *)ptr;
3137 break;
3138 default:
3139 *pval = 0;
3140 break;
3141 }
bellard9307c4c2004-04-04 12:57:25 +00003142 }
3143 return 0;
3144 }
3145 }
3146 return -1;
3147}
3148
3149static void next(void)
3150{
Blue Swirl660f11b2009-07-31 21:16:51 +00003151 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003152 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003153 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003154 pch++;
3155 }
3156}
3157
aliguori376253e2009-03-05 23:01:23 +00003158static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003159
aliguori376253e2009-03-05 23:01:23 +00003160static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003161{
blueswir1c2efc952007-09-25 17:28:42 +00003162 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003163 char *p;
bellard6a00d602005-11-21 23:25:50 +00003164 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003165
3166 switch(*pch) {
3167 case '+':
3168 next();
aliguori376253e2009-03-05 23:01:23 +00003169 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003170 break;
3171 case '-':
3172 next();
aliguori376253e2009-03-05 23:01:23 +00003173 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003174 break;
3175 case '~':
3176 next();
aliguori376253e2009-03-05 23:01:23 +00003177 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003178 break;
3179 case '(':
3180 next();
aliguori376253e2009-03-05 23:01:23 +00003181 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003182 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003183 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003184 }
3185 next();
3186 break;
bellard81d09122004-07-14 17:21:37 +00003187 case '\'':
3188 pch++;
3189 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003190 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003191 n = *pch;
3192 pch++;
3193 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003194 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003195 next();
3196 break;
bellard9307c4c2004-04-04 12:57:25 +00003197 case '$':
3198 {
3199 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003200 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003201
bellard9307c4c2004-04-04 12:57:25 +00003202 pch++;
3203 q = buf;
3204 while ((*pch >= 'a' && *pch <= 'z') ||
3205 (*pch >= 'A' && *pch <= 'Z') ||
3206 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003207 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003208 if ((q - buf) < sizeof(buf) - 1)
3209 *q++ = *pch;
3210 pch++;
3211 }
blueswir1cd390082008-11-16 13:53:32 +00003212 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003213 pch++;
3214 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003215 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003216 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003217 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003218 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003219 }
3220 break;
3221 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003222 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003223 n = 0;
3224 break;
3225 default:
blueswir17743e582007-09-24 18:39:04 +00003226#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003227 n = strtoull(pch, &p, 0);
3228#else
bellard9307c4c2004-04-04 12:57:25 +00003229 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003230#endif
bellard9307c4c2004-04-04 12:57:25 +00003231 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003232 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003233 }
3234 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003235 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003236 pch++;
3237 break;
3238 }
3239 return n;
3240}
3241
3242
aliguori376253e2009-03-05 23:01:23 +00003243static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003244{
blueswir1c2efc952007-09-25 17:28:42 +00003245 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003246 int op;
ths3b46e622007-09-17 08:09:54 +00003247
aliguori376253e2009-03-05 23:01:23 +00003248 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003249 for(;;) {
3250 op = *pch;
3251 if (op != '*' && op != '/' && op != '%')
3252 break;
3253 next();
aliguori376253e2009-03-05 23:01:23 +00003254 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003255 switch(op) {
3256 default:
3257 case '*':
3258 val *= val2;
3259 break;
3260 case '/':
3261 case '%':
ths5fafdf22007-09-16 21:08:06 +00003262 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003263 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003264 if (op == '/')
3265 val /= val2;
3266 else
3267 val %= val2;
3268 break;
3269 }
3270 }
3271 return val;
3272}
3273
aliguori376253e2009-03-05 23:01:23 +00003274static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003275{
blueswir1c2efc952007-09-25 17:28:42 +00003276 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003277 int op;
bellard9307c4c2004-04-04 12:57:25 +00003278
aliguori376253e2009-03-05 23:01:23 +00003279 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003280 for(;;) {
3281 op = *pch;
3282 if (op != '&' && op != '|' && op != '^')
3283 break;
3284 next();
aliguori376253e2009-03-05 23:01:23 +00003285 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003286 switch(op) {
3287 default:
3288 case '&':
3289 val &= val2;
3290 break;
3291 case '|':
3292 val |= val2;
3293 break;
3294 case '^':
3295 val ^= val2;
3296 break;
3297 }
3298 }
3299 return val;
3300}
3301
aliguori376253e2009-03-05 23:01:23 +00003302static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003303{
blueswir1c2efc952007-09-25 17:28:42 +00003304 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003305 int op;
bellard9307c4c2004-04-04 12:57:25 +00003306
aliguori376253e2009-03-05 23:01:23 +00003307 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003308 for(;;) {
3309 op = *pch;
3310 if (op != '+' && op != '-')
3311 break;
3312 next();
aliguori376253e2009-03-05 23:01:23 +00003313 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003314 if (op == '+')
3315 val += val2;
3316 else
3317 val -= val2;
3318 }
3319 return val;
3320}
3321
aliguori376253e2009-03-05 23:01:23 +00003322static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003323{
3324 pch = *pp;
3325 if (setjmp(expr_env)) {
3326 *pp = pch;
3327 return -1;
3328 }
blueswir1cd390082008-11-16 13:53:32 +00003329 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003330 pch++;
aliguori376253e2009-03-05 23:01:23 +00003331 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003332 *pp = pch;
3333 return 0;
3334}
3335
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003336static int get_double(Monitor *mon, double *pval, const char **pp)
3337{
3338 const char *p = *pp;
3339 char *tailp;
3340 double d;
3341
3342 d = strtod(p, &tailp);
3343 if (tailp == p) {
3344 monitor_printf(mon, "Number expected\n");
3345 return -1;
3346 }
3347 if (d != d || d - d != 0) {
3348 /* NaN or infinity */
3349 monitor_printf(mon, "Bad number\n");
3350 return -1;
3351 }
3352 *pval = d;
3353 *pp = tailp;
3354 return 0;
3355}
3356
bellard9307c4c2004-04-04 12:57:25 +00003357static int get_str(char *buf, int buf_size, const char **pp)
3358{
3359 const char *p;
3360 char *q;
3361 int c;
3362
bellard81d09122004-07-14 17:21:37 +00003363 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003364 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003365 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003366 p++;
3367 if (*p == '\0') {
3368 fail:
bellard81d09122004-07-14 17:21:37 +00003369 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003370 *pp = p;
3371 return -1;
3372 }
bellard9307c4c2004-04-04 12:57:25 +00003373 if (*p == '\"') {
3374 p++;
3375 while (*p != '\0' && *p != '\"') {
3376 if (*p == '\\') {
3377 p++;
3378 c = *p++;
3379 switch(c) {
3380 case 'n':
3381 c = '\n';
3382 break;
3383 case 'r':
3384 c = '\r';
3385 break;
3386 case '\\':
3387 case '\'':
3388 case '\"':
3389 break;
3390 default:
3391 qemu_printf("unsupported escape code: '\\%c'\n", c);
3392 goto fail;
3393 }
3394 if ((q - buf) < buf_size - 1) {
3395 *q++ = c;
3396 }
3397 } else {
3398 if ((q - buf) < buf_size - 1) {
3399 *q++ = *p;
3400 }
3401 p++;
3402 }
3403 }
3404 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003405 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003406 goto fail;
3407 }
3408 p++;
3409 } else {
blueswir1cd390082008-11-16 13:53:32 +00003410 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003411 if ((q - buf) < buf_size - 1) {
3412 *q++ = *p;
3413 }
3414 p++;
3415 }
bellard9307c4c2004-04-04 12:57:25 +00003416 }
bellard81d09122004-07-14 17:21:37 +00003417 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003418 *pp = p;
3419 return 0;
3420}
3421
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003422/*
3423 * Store the command-name in cmdname, and return a pointer to
3424 * the remaining of the command string.
3425 */
3426static const char *get_command_name(const char *cmdline,
3427 char *cmdname, size_t nlen)
3428{
3429 size_t len;
3430 const char *p, *pstart;
3431
3432 p = cmdline;
3433 while (qemu_isspace(*p))
3434 p++;
3435 if (*p == '\0')
3436 return NULL;
3437 pstart = p;
3438 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3439 p++;
3440 len = p - pstart;
3441 if (len > nlen - 1)
3442 len = nlen - 1;
3443 memcpy(cmdname, pstart, len);
3444 cmdname[len] = '\0';
3445 return p;
3446}
3447
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003448/**
3449 * Read key of 'type' into 'key' and return the current
3450 * 'type' pointer.
3451 */
3452static char *key_get_info(const char *type, char **key)
3453{
3454 size_t len;
3455 char *p, *str;
3456
3457 if (*type == ',')
3458 type++;
3459
3460 p = strchr(type, ':');
3461 if (!p) {
3462 *key = NULL;
3463 return NULL;
3464 }
3465 len = p - type;
3466
3467 str = qemu_malloc(len + 1);
3468 memcpy(str, type, len);
3469 str[len] = '\0';
3470
3471 *key = str;
3472 return ++p;
3473}
3474
bellard9307c4c2004-04-04 12:57:25 +00003475static int default_fmt_format = 'x';
3476static int default_fmt_size = 4;
3477
3478#define MAX_ARGS 16
3479
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003480static int is_valid_option(const char *c, const char *typestr)
3481{
3482 char option[3];
3483
3484 option[0] = '-';
3485 option[1] = *c;
3486 option[2] = '\0';
3487
3488 typestr = strstr(typestr, option);
3489 return (typestr != NULL);
3490}
3491
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003492static const mon_cmd_t *monitor_find_command(const char *cmdname)
3493{
3494 const mon_cmd_t *cmd;
3495
3496 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3497 if (compare_cmd(cmdname, cmd->name)) {
3498 return cmd;
3499 }
3500 }
3501
3502 return NULL;
3503}
3504
Anthony Liguoric227f092009-10-01 16:12:16 -05003505static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003506 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003507 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003508{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003509 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003510 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003511 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003512 char cmdname[256];
3513 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003514 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003515
3516#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003517 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003518#endif
ths3b46e622007-09-17 08:09:54 +00003519
bellard9307c4c2004-04-04 12:57:25 +00003520 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003521 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3522 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003523 return NULL;
ths3b46e622007-09-17 08:09:54 +00003524
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003525 cmd = monitor_find_command(cmdname);
3526 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003527 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003528 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003529 }
bellard9307c4c2004-04-04 12:57:25 +00003530
bellard9307c4c2004-04-04 12:57:25 +00003531 /* parse the parameters */
3532 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003533 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003534 typestr = key_get_info(typestr, &key);
3535 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003536 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003537 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003538 typestr++;
3539 switch(c) {
3540 case 'F':
bellard81d09122004-07-14 17:21:37 +00003541 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003542 case 's':
3543 {
3544 int ret;
ths3b46e622007-09-17 08:09:54 +00003545
blueswir1cd390082008-11-16 13:53:32 +00003546 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003547 p++;
3548 if (*typestr == '?') {
3549 typestr++;
3550 if (*p == '\0') {
3551 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003552 break;
bellard9307c4c2004-04-04 12:57:25 +00003553 }
3554 }
3555 ret = get_str(buf, sizeof(buf), &p);
3556 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003557 switch(c) {
3558 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003559 monitor_printf(mon, "%s: filename expected\n",
3560 cmdname);
bellard81d09122004-07-14 17:21:37 +00003561 break;
3562 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003563 monitor_printf(mon, "%s: block device name expected\n",
3564 cmdname);
bellard81d09122004-07-14 17:21:37 +00003565 break;
3566 default:
aliguori376253e2009-03-05 23:01:23 +00003567 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003568 break;
3569 }
bellard9307c4c2004-04-04 12:57:25 +00003570 goto fail;
3571 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003572 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003573 }
3574 break;
3575 case '/':
3576 {
3577 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003578
blueswir1cd390082008-11-16 13:53:32 +00003579 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003580 p++;
3581 if (*p == '/') {
3582 /* format found */
3583 p++;
3584 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003585 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003586 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003587 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003588 count = count * 10 + (*p - '0');
3589 p++;
3590 }
3591 }
3592 size = -1;
3593 format = -1;
3594 for(;;) {
3595 switch(*p) {
3596 case 'o':
3597 case 'd':
3598 case 'u':
3599 case 'x':
3600 case 'i':
3601 case 'c':
3602 format = *p++;
3603 break;
3604 case 'b':
3605 size = 1;
3606 p++;
3607 break;
3608 case 'h':
3609 size = 2;
3610 p++;
3611 break;
3612 case 'w':
3613 size = 4;
3614 p++;
3615 break;
3616 case 'g':
3617 case 'L':
3618 size = 8;
3619 p++;
3620 break;
3621 default:
3622 goto next;
3623 }
3624 }
3625 next:
blueswir1cd390082008-11-16 13:53:32 +00003626 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003627 monitor_printf(mon, "invalid char in format: '%c'\n",
3628 *p);
bellard9307c4c2004-04-04 12:57:25 +00003629 goto fail;
3630 }
bellard9307c4c2004-04-04 12:57:25 +00003631 if (format < 0)
3632 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003633 if (format != 'i') {
3634 /* for 'i', not specifying a size gives -1 as size */
3635 if (size < 0)
3636 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003637 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003638 }
bellard9307c4c2004-04-04 12:57:25 +00003639 default_fmt_format = format;
3640 } else {
3641 count = 1;
3642 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003643 if (format != 'i') {
3644 size = default_fmt_size;
3645 } else {
3646 size = -1;
3647 }
bellard9307c4c2004-04-04 12:57:25 +00003648 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003649 qdict_put(qdict, "count", qint_from_int(count));
3650 qdict_put(qdict, "format", qint_from_int(format));
3651 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003652 }
3653 break;
3654 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003655 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003656 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003657 {
blueswir1c2efc952007-09-25 17:28:42 +00003658 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003659
blueswir1cd390082008-11-16 13:53:32 +00003660 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003661 p++;
bellard34405572004-06-08 00:55:58 +00003662 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003663 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003664 if (*p == '\0') {
3665 typestr++;
3666 break;
3667 }
bellard34405572004-06-08 00:55:58 +00003668 } else {
3669 if (*p == '.') {
3670 p++;
blueswir1cd390082008-11-16 13:53:32 +00003671 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003672 p++;
bellard34405572004-06-08 00:55:58 +00003673 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003674 typestr++;
3675 break;
bellard34405572004-06-08 00:55:58 +00003676 }
3677 }
bellard13224a82006-07-14 22:03:35 +00003678 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003679 }
aliguori376253e2009-03-05 23:01:23 +00003680 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003681 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003682 /* Check if 'i' is greater than 32-bit */
3683 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3684 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3685 monitor_printf(mon, "integer is for 32-bit values\n");
3686 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003687 } else if (c == 'M') {
3688 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003689 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003690 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003691 }
3692 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003693 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003694 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003695 {
3696 double val;
3697
3698 while (qemu_isspace(*p))
3699 p++;
3700 if (*typestr == '?') {
3701 typestr++;
3702 if (*p == '\0') {
3703 break;
3704 }
3705 }
3706 if (get_double(mon, &val, &p) < 0) {
3707 goto fail;
3708 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003709 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003710 switch (*p) {
3711 case 'K': case 'k':
3712 val *= 1 << 10; p++; break;
3713 case 'M': case 'm':
3714 val *= 1 << 20; p++; break;
3715 case 'G': case 'g':
3716 val *= 1 << 30; p++; break;
3717 }
3718 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003719 if (c == 'T' && p[0] && p[1] == 's') {
3720 switch (*p) {
3721 case 'm':
3722 val /= 1e3; p += 2; break;
3723 case 'u':
3724 val /= 1e6; p += 2; break;
3725 case 'n':
3726 val /= 1e9; p += 2; break;
3727 }
3728 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003729 if (*p && !qemu_isspace(*p)) {
3730 monitor_printf(mon, "Unknown unit suffix\n");
3731 goto fail;
3732 }
3733 qdict_put(qdict, key, qfloat_from_double(val));
3734 }
3735 break;
bellard9307c4c2004-04-04 12:57:25 +00003736 case '-':
3737 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003738 const char *tmp = p;
3739 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003740 /* option */
ths3b46e622007-09-17 08:09:54 +00003741
bellard9307c4c2004-04-04 12:57:25 +00003742 c = *typestr++;
3743 if (c == '\0')
3744 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003745 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003746 p++;
3747 has_option = 0;
3748 if (*p == '-') {
3749 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003750 if(c != *p) {
3751 if(!is_valid_option(p, typestr)) {
3752
3753 monitor_printf(mon, "%s: unsupported option -%c\n",
3754 cmdname, *p);
3755 goto fail;
3756 } else {
3757 skip_key = 1;
3758 }
bellard9307c4c2004-04-04 12:57:25 +00003759 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003760 if(skip_key) {
3761 p = tmp;
3762 } else {
3763 p++;
3764 has_option = 1;
3765 }
bellard9307c4c2004-04-04 12:57:25 +00003766 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003767 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003768 }
3769 break;
3770 default:
3771 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003772 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003773 goto fail;
3774 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003775 qemu_free(key);
3776 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003777 }
3778 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003779 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003780 p++;
3781 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003782 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3783 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003784 goto fail;
3785 }
3786
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003787 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003788
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003789fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003790 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003791 return NULL;
3792}
3793
Luiz Capitulino8204a912009-11-18 23:05:31 -02003794static void monitor_print_error(Monitor *mon)
3795{
3796 qerror_print(mon->error);
3797 QDECREF(mon->error);
3798 mon->error = NULL;
3799}
3800
Adam Litke940cc302010-01-25 12:18:44 -06003801static int is_async_return(const QObject *data)
3802{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003803 if (data && qobject_type(data) == QTYPE_QDICT) {
3804 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3805 }
3806
3807 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003808}
3809
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003810static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3811 const QDict *params)
3812{
3813 QObject *data = NULL;
3814
Luiz Capitulino97536cf2010-02-10 23:49:47 -02003815 if (cmd->cmd_new_ret) {
3816 cmd->cmd_new_ret(mon, params, &data);
3817 } else {
3818 cmd->mhandler.cmd_new(mon, params, &data);
3819 }
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003820
Adam Litke940cc302010-01-25 12:18:44 -06003821 if (is_async_return(data)) {
3822 /*
3823 * Asynchronous commands have no initial return data but they can
3824 * generate errors. Data is returned via the async completion handler.
3825 */
3826 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3827 monitor_protocol_emitter(mon, NULL);
3828 }
3829 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003830 /* Monitor Protocol */
3831 monitor_protocol_emitter(mon, data);
3832 } else {
3833 /* User Protocol */
3834 if (data)
3835 cmd->user_print(mon, data);
3836 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003837
3838 qobject_decref(data);
3839}
3840
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003841static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003842{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003843 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003844 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003845
3846 qdict = qdict_new();
3847
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003848 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003849 if (!cmd)
3850 goto out;
3851
3852 qemu_errors_to_mon(mon);
3853
Adam Litke940cc302010-01-25 12:18:44 -06003854 if (monitor_handler_is_async(cmd)) {
3855 user_async_cmd_handler(mon, cmd, qdict);
3856 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003857 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003858 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003859 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003860 }
3861
Luiz Capitulino8204a912009-11-18 23:05:31 -02003862 if (monitor_has_error(mon))
3863 monitor_print_error(mon);
3864
3865 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003866
3867out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003868 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003869}
3870
bellard81d09122004-07-14 17:21:37 +00003871static void cmd_completion(const char *name, const char *list)
3872{
3873 const char *p, *pstart;
3874 char cmd[128];
3875 int len;
3876
3877 p = list;
3878 for(;;) {
3879 pstart = p;
3880 p = strchr(p, '|');
3881 if (!p)
3882 p = pstart + strlen(pstart);
3883 len = p - pstart;
3884 if (len > sizeof(cmd) - 2)
3885 len = sizeof(cmd) - 2;
3886 memcpy(cmd, pstart, len);
3887 cmd[len] = '\0';
3888 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003889 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003890 }
3891 if (*p == '\0')
3892 break;
3893 p++;
3894 }
3895}
3896
3897static void file_completion(const char *input)
3898{
3899 DIR *ffs;
3900 struct dirent *d;
3901 char path[1024];
3902 char file[1024], file_prefix[1024];
3903 int input_path_len;
3904 const char *p;
3905
ths5fafdf22007-09-16 21:08:06 +00003906 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003907 if (!p) {
3908 input_path_len = 0;
3909 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003910 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003911 } else {
3912 input_path_len = p - input + 1;
3913 memcpy(path, input, input_path_len);
3914 if (input_path_len > sizeof(path) - 1)
3915 input_path_len = sizeof(path) - 1;
3916 path[input_path_len] = '\0';
3917 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3918 }
3919#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003920 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3921 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003922#endif
3923 ffs = opendir(path);
3924 if (!ffs)
3925 return;
3926 for(;;) {
3927 struct stat sb;
3928 d = readdir(ffs);
3929 if (!d)
3930 break;
3931 if (strstart(d->d_name, file_prefix, NULL)) {
3932 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003933 if (input_path_len < sizeof(file))
3934 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3935 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003936 /* stat the file to find out if it's a directory.
3937 * In that case add a slash to speed up typing long paths
3938 */
3939 stat(file, &sb);
3940 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003941 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003942 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003943 }
3944 }
3945 closedir(ffs);
3946}
3947
aliguori51de9762009-03-05 23:00:43 +00003948static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003949{
aliguori51de9762009-03-05 23:00:43 +00003950 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003951 const char *input = opaque;
3952
3953 if (input[0] == '\0' ||
3954 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003955 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003956 }
3957}
3958
3959/* NOTE: this parser is an approximate form of the real command parser */
3960static void parse_cmdline(const char *cmdline,
3961 int *pnb_args, char **args)
3962{
3963 const char *p;
3964 int nb_args, ret;
3965 char buf[1024];
3966
3967 p = cmdline;
3968 nb_args = 0;
3969 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003970 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003971 p++;
3972 if (*p == '\0')
3973 break;
3974 if (nb_args >= MAX_ARGS)
3975 break;
3976 ret = get_str(buf, sizeof(buf), &p);
3977 args[nb_args] = qemu_strdup(buf);
3978 nb_args++;
3979 if (ret < 0)
3980 break;
3981 }
3982 *pnb_args = nb_args;
3983}
3984
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003985static const char *next_arg_type(const char *typestr)
3986{
3987 const char *p = strchr(typestr, ':');
3988 return (p != NULL ? ++p : typestr);
3989}
3990
aliguori4c36ba32009-03-05 23:01:37 +00003991static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003992{
3993 const char *cmdname;
3994 char *args[MAX_ARGS];
3995 int nb_args, i, len;
3996 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003997 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003998 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003999
4000 parse_cmdline(cmdline, &nb_args, args);
4001#ifdef DEBUG_COMPLETION
4002 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004003 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004004 }
4005#endif
4006
4007 /* if the line ends with a space, it means we want to complete the
4008 next arg */
4009 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004010 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004011 if (nb_args >= MAX_ARGS)
4012 return;
4013 args[nb_args++] = qemu_strdup("");
4014 }
4015 if (nb_args <= 1) {
4016 /* command completion */
4017 if (nb_args == 0)
4018 cmdname = "";
4019 else
4020 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004021 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004022 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004023 cmd_completion(cmdname, cmd->name);
4024 }
4025 } else {
4026 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004027 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004028 if (compare_cmd(args[0], cmd->name))
4029 goto found;
4030 }
4031 return;
4032 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004033 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004034 for(i = 0; i < nb_args - 2; i++) {
4035 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004036 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004037 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004038 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004039 }
4040 }
4041 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004042 if (*ptype == '-' && ptype[1] != '\0') {
4043 ptype += 2;
4044 }
bellard81d09122004-07-14 17:21:37 +00004045 switch(*ptype) {
4046 case 'F':
4047 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004048 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004049 file_completion(str);
4050 break;
4051 case 'B':
4052 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004053 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004054 bdrv_iterate(block_completion_it, (void *)str);
4055 break;
bellard7fe48482004-10-09 18:08:01 +00004056 case 's':
4057 /* XXX: more generic ? */
4058 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004059 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004060 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4061 cmd_completion(str, cmd->name);
4062 }
bellard64866c32006-05-07 18:03:31 +00004063 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004064 char *sep = strrchr(str, '-');
4065 if (sep)
4066 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004067 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004068 for(key = key_defs; key->name != NULL; key++) {
4069 cmd_completion(str, key->name);
4070 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004071 } else if (!strcmp(cmd->name, "help|?")) {
4072 readline_set_completion_index(cur_mon->rs, strlen(str));
4073 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4074 cmd_completion(str, cmd->name);
4075 }
bellard7fe48482004-10-09 18:08:01 +00004076 }
4077 break;
bellard81d09122004-07-14 17:21:37 +00004078 default:
4079 break;
4080 }
4081 }
4082 for(i = 0; i < nb_args; i++)
4083 qemu_free(args[i]);
4084}
4085
aliguori731b0362009-03-05 23:01:42 +00004086static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004087{
aliguori731b0362009-03-05 23:01:42 +00004088 Monitor *mon = opaque;
4089
Jan Kiszkac62313b2009-12-04 14:05:29 +01004090 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004091}
4092
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004093typedef struct CmdArgs {
4094 QString *name;
4095 int type;
4096 int flag;
4097 int optional;
4098} CmdArgs;
4099
4100static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4101{
4102 if (!cmd_args->optional) {
4103 qemu_error_new(QERR_MISSING_PARAMETER, name);
4104 return -1;
4105 }
4106
4107 if (cmd_args->type == '-') {
4108 /* handlers expect a value, they need to be changed */
4109 qdict_put(args, name, qint_from_int(0));
4110 }
4111
4112 return 0;
4113}
4114
4115static int check_arg(const CmdArgs *cmd_args, QDict *args)
4116{
4117 QObject *value;
4118 const char *name;
4119
4120 name = qstring_get_str(cmd_args->name);
4121
4122 if (!args) {
4123 return check_opt(cmd_args, name, args);
4124 }
4125
4126 value = qdict_get(args, name);
4127 if (!value) {
4128 return check_opt(cmd_args, name, args);
4129 }
4130
4131 switch (cmd_args->type) {
4132 case 'F':
4133 case 'B':
4134 case 's':
4135 if (qobject_type(value) != QTYPE_QSTRING) {
4136 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4137 return -1;
4138 }
4139 break;
4140 case '/': {
4141 int i;
4142 const char *keys[] = { "count", "format", "size", NULL };
4143
4144 for (i = 0; keys[i]; i++) {
4145 QObject *obj = qdict_get(args, keys[i]);
4146 if (!obj) {
4147 qemu_error_new(QERR_MISSING_PARAMETER, name);
4148 return -1;
4149 }
4150 if (qobject_type(obj) != QTYPE_QINT) {
4151 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4152 return -1;
4153 }
4154 }
4155 break;
4156 }
4157 case 'i':
4158 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004159 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004160 if (qobject_type(value) != QTYPE_QINT) {
4161 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4162 return -1;
4163 }
4164 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004165 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004166 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004167 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4168 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4169 return -1;
4170 }
4171 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004172 case '-':
4173 if (qobject_type(value) != QTYPE_QINT &&
4174 qobject_type(value) != QTYPE_QBOOL) {
4175 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4176 return -1;
4177 }
4178 if (qobject_type(value) == QTYPE_QBOOL) {
4179 /* handlers expect a QInt, they need to be changed */
4180 qdict_put(args, name,
4181 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4182 }
4183 break;
4184 default:
4185 /* impossible */
4186 abort();
4187 }
4188
4189 return 0;
4190}
4191
4192static void cmd_args_init(CmdArgs *cmd_args)
4193{
4194 cmd_args->name = qstring_new();
4195 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4196}
4197
4198/*
4199 * This is not trivial, we have to parse Monitor command's argument
4200 * type syntax to be able to check the arguments provided by clients.
4201 *
4202 * In the near future we will be using an array for that and will be
4203 * able to drop all this parsing...
4204 */
4205static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4206{
4207 int err;
4208 const char *p;
4209 CmdArgs cmd_args;
4210
Blue Swirldd5121b2009-12-04 20:52:02 +00004211 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004212 return (qdict_size(args) == 0 ? 0 : -1);
4213 }
4214
4215 err = 0;
4216 cmd_args_init(&cmd_args);
4217
4218 for (p = cmd->args_type;; p++) {
4219 if (*p == ':') {
4220 cmd_args.type = *++p;
4221 p++;
4222 if (cmd_args.type == '-') {
4223 cmd_args.flag = *p++;
4224 cmd_args.optional = 1;
4225 } else if (*p == '?') {
4226 cmd_args.optional = 1;
4227 p++;
4228 }
4229
4230 assert(*p == ',' || *p == '\0');
4231 err = check_arg(&cmd_args, args);
4232
4233 QDECREF(cmd_args.name);
4234 cmd_args_init(&cmd_args);
4235
4236 if (err < 0) {
4237 break;
4238 }
4239 } else {
4240 qstring_append_chr(cmd_args.name, *p);
4241 }
4242
4243 if (*p == '\0') {
4244 break;
4245 }
4246 }
4247
4248 QDECREF(cmd_args.name);
4249 return err;
4250}
4251
Luiz Capitulino09069b12010-02-04 18:10:06 -02004252static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4253{
4254 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4255 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4256}
4257
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004258static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4259{
4260 int err;
4261 QObject *obj;
4262 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004263 const mon_cmd_t *cmd;
4264 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004265 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004266
4267 args = NULL;
4268 qemu_errors_to_mon(mon);
4269
4270 obj = json_parser_parse(tokens, NULL);
4271 if (!obj) {
4272 // FIXME: should be triggered in json_parser_parse()
4273 qemu_error_new(QERR_JSON_PARSING);
4274 goto err_out;
4275 } else if (qobject_type(obj) != QTYPE_QDICT) {
4276 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4277 qobject_decref(obj);
4278 goto err_out;
4279 }
4280
4281 input = qobject_to_qdict(obj);
4282
4283 mon->mc->id = qdict_get(input, "id");
4284 qobject_incref(mon->mc->id);
4285
4286 obj = qdict_get(input, "execute");
4287 if (!obj) {
4288 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4289 goto err_input;
4290 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4291 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4292 goto err_input;
4293 }
4294
4295 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004296
Luiz Capitulino09069b12010-02-04 18:10:06 -02004297 if (invalid_qmp_mode(mon, cmd_name)) {
4298 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4299 goto err_input;
4300 }
4301
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004302 /*
4303 * XXX: We need this special case until we get info handlers
4304 * converted into 'query-' commands
4305 */
4306 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004307 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4308 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004309 } else if (strstart(cmd_name, "query-", &info_item)) {
4310 cmd = monitor_find_command("info");
4311 qdict_put_obj(input, "arguments",
4312 qobject_from_jsonf("{ 'item': %s }", info_item));
4313 } else {
4314 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004315 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004316 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4317 goto err_input;
4318 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004319 }
4320
4321 obj = qdict_get(input, "arguments");
4322 if (!obj) {
4323 args = qdict_new();
4324 } else {
4325 args = qobject_to_qdict(obj);
4326 QINCREF(args);
4327 }
4328
4329 QDECREF(input);
4330
4331 err = monitor_check_qmp_args(cmd, args);
4332 if (err < 0) {
4333 goto err_out;
4334 }
4335
Adam Litke940cc302010-01-25 12:18:44 -06004336 if (monitor_handler_is_async(cmd)) {
4337 qmp_async_cmd_handler(mon, cmd, args);
4338 } else {
4339 monitor_call_handler(mon, cmd, args);
4340 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004341 goto out;
4342
4343err_input:
4344 QDECREF(input);
4345err_out:
4346 monitor_protocol_emitter(mon, NULL);
4347out:
4348 QDECREF(args);
4349 qemu_errors_to_previous();
4350}
4351
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004352/**
4353 * monitor_control_read(): Read and handle QMP input
4354 */
4355static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4356{
4357 Monitor *old_mon = cur_mon;
4358
4359 cur_mon = opaque;
4360
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004361 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004362
4363 cur_mon = old_mon;
4364}
4365
aliguori731b0362009-03-05 23:01:42 +00004366static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004367{
aliguori731b0362009-03-05 23:01:42 +00004368 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004369 int i;
aliguori376253e2009-03-05 23:01:23 +00004370
aliguori731b0362009-03-05 23:01:42 +00004371 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004372
aliguoricde76ee2009-03-05 23:01:51 +00004373 if (cur_mon->rs) {
4374 for (i = 0; i < size; i++)
4375 readline_handle_byte(cur_mon->rs, buf[i]);
4376 } else {
4377 if (size == 0 || buf[size - 1] != 0)
4378 monitor_printf(cur_mon, "corrupted command\n");
4379 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004380 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004381 }
aliguori731b0362009-03-05 23:01:42 +00004382
4383 cur_mon = old_mon;
4384}
aliguorid8f44602008-10-06 13:52:44 +00004385
aliguori376253e2009-03-05 23:01:23 +00004386static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004387{
aliguori731b0362009-03-05 23:01:42 +00004388 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004389 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004390 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004391}
4392
aliguoricde76ee2009-03-05 23:01:51 +00004393int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004394{
aliguoricde76ee2009-03-05 23:01:51 +00004395 if (!mon->rs)
4396 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004397 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004398 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004399}
4400
aliguori376253e2009-03-05 23:01:23 +00004401void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004402{
aliguoricde76ee2009-03-05 23:01:51 +00004403 if (!mon->rs)
4404 return;
aliguori731b0362009-03-05 23:01:42 +00004405 if (--mon->suspend_cnt == 0)
4406 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004407}
4408
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004409static QObject *get_qmp_greeting(void)
4410{
4411 QObject *ver;
4412
4413 do_info_version(NULL, &ver);
4414 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4415}
4416
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004417/**
4418 * monitor_control_event(): Print QMP gretting
4419 */
4420static void monitor_control_event(void *opaque, int event)
4421{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004422 QObject *data;
4423 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004424
Luiz Capitulino47116d12010-02-08 17:01:30 -02004425 switch (event) {
4426 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004427 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004428 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004429 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004430 monitor_json_emitter(mon, data);
4431 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004432 break;
4433 case CHR_EVENT_CLOSED:
4434 json_message_parser_destroy(&mon->mc->parser);
4435 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004436 }
4437}
4438
aliguori731b0362009-03-05 23:01:42 +00004439static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004440{
aliguori376253e2009-03-05 23:01:23 +00004441 Monitor *mon = opaque;
4442
aliguori2724b182009-03-05 23:01:47 +00004443 switch (event) {
4444 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004445 mon->mux_out = 0;
4446 if (mon->reset_seen) {
4447 readline_restart(mon->rs);
4448 monitor_resume(mon);
4449 monitor_flush(mon);
4450 } else {
4451 mon->suspend_cnt = 0;
4452 }
aliguori2724b182009-03-05 23:01:47 +00004453 break;
ths86e94de2007-01-05 22:01:59 +00004454
aliguori2724b182009-03-05 23:01:47 +00004455 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004456 if (mon->reset_seen) {
4457 if (mon->suspend_cnt == 0) {
4458 monitor_printf(mon, "\n");
4459 }
4460 monitor_flush(mon);
4461 monitor_suspend(mon);
4462 } else {
4463 mon->suspend_cnt++;
4464 }
4465 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004466 break;
4467
Amit Shahb6b8df52009-10-07 18:31:16 +05304468 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004469 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4470 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004471 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004472 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004473 }
4474 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004475 break;
4476 }
ths86e94de2007-01-05 22:01:59 +00004477}
4478
aliguori76655d62009-03-06 20:27:37 +00004479
4480/*
4481 * Local variables:
4482 * c-indent-level: 4
4483 * c-basic-offset: 4
4484 * tab-width: 8
4485 * End:
4486 */
4487
aliguori731b0362009-03-05 23:01:42 +00004488void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004489{
aliguori731b0362009-03-05 23:01:42 +00004490 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004491 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004492
4493 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004494 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004495 is_first_init = 0;
4496 }
aliguori87127162009-03-05 23:01:29 +00004497
4498 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004499
aliguori87127162009-03-05 23:01:29 +00004500 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004501 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004502 if (flags & MONITOR_USE_READLINE) {
4503 mon->rs = readline_init(mon, monitor_find_completion);
4504 monitor_read_command(mon, 0);
4505 }
aliguori87127162009-03-05 23:01:29 +00004506
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004507 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004508 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004509 /* Control mode requires special handlers */
4510 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4511 monitor_control_event, mon);
4512 } else {
4513 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4514 monitor_event, mon);
4515 }
aliguori87127162009-03-05 23:01:29 +00004516
Blue Swirl72cf2d42009-09-12 07:36:22 +00004517 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004518 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004519 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004520}
4521
aliguori376253e2009-03-05 23:01:23 +00004522static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004523{
aliguoribb5fc202009-03-05 23:01:15 +00004524 BlockDriverState *bs = opaque;
4525 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004526
aliguoribb5fc202009-03-05 23:01:15 +00004527 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004528 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004529 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004530 }
aliguori731b0362009-03-05 23:01:42 +00004531 if (mon->password_completion_cb)
4532 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004533
aliguori731b0362009-03-05 23:01:42 +00004534 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004535}
aliguoric0f4ce72009-03-05 23:01:01 +00004536
aliguori376253e2009-03-05 23:01:23 +00004537void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004538 BlockDriverCompletionFunc *completion_cb,
4539 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004540{
aliguoricde76ee2009-03-05 23:01:51 +00004541 int err;
4542
aliguoribb5fc202009-03-05 23:01:15 +00004543 if (!bdrv_key_required(bs)) {
4544 if (completion_cb)
4545 completion_cb(opaque, 0);
4546 return;
4547 }
aliguoric0f4ce72009-03-05 23:01:01 +00004548
Luiz Capitulino94171e12009-12-07 21:37:00 +01004549 if (monitor_ctrl_mode(mon)) {
4550 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4551 return;
4552 }
4553
aliguori376253e2009-03-05 23:01:23 +00004554 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4555 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004556
aliguori731b0362009-03-05 23:01:42 +00004557 mon->password_completion_cb = completion_cb;
4558 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004559
aliguoricde76ee2009-03-05 23:01:51 +00004560 err = monitor_read_password(mon, bdrv_password_cb, bs);
4561
4562 if (err && completion_cb)
4563 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004564}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004565
4566typedef struct QemuErrorSink QemuErrorSink;
4567struct QemuErrorSink {
4568 enum {
4569 ERR_SINK_FILE,
4570 ERR_SINK_MONITOR,
4571 } dest;
4572 union {
4573 FILE *fp;
4574 Monitor *mon;
4575 };
4576 QemuErrorSink *previous;
4577};
4578
Blue Swirl528e93a2009-08-31 15:14:40 +00004579static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004580
4581void qemu_errors_to_file(FILE *fp)
4582{
4583 QemuErrorSink *sink;
4584
4585 sink = qemu_mallocz(sizeof(*sink));
4586 sink->dest = ERR_SINK_FILE;
4587 sink->fp = fp;
4588 sink->previous = qemu_error_sink;
4589 qemu_error_sink = sink;
4590}
4591
4592void qemu_errors_to_mon(Monitor *mon)
4593{
4594 QemuErrorSink *sink;
4595
4596 sink = qemu_mallocz(sizeof(*sink));
4597 sink->dest = ERR_SINK_MONITOR;
4598 sink->mon = mon;
4599 sink->previous = qemu_error_sink;
4600 qemu_error_sink = sink;
4601}
4602
4603void qemu_errors_to_previous(void)
4604{
4605 QemuErrorSink *sink;
4606
4607 assert(qemu_error_sink != NULL);
4608 sink = qemu_error_sink;
4609 qemu_error_sink = sink->previous;
4610 qemu_free(sink);
4611}
4612
4613void qemu_error(const char *fmt, ...)
4614{
4615 va_list args;
4616
4617 assert(qemu_error_sink != NULL);
4618 switch (qemu_error_sink->dest) {
4619 case ERR_SINK_FILE:
4620 va_start(args, fmt);
4621 vfprintf(qemu_error_sink->fp, fmt, args);
4622 va_end(args);
4623 break;
4624 case ERR_SINK_MONITOR:
4625 va_start(args, fmt);
4626 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4627 va_end(args);
4628 break;
4629 }
4630}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004631
4632void qemu_error_internal(const char *file, int linenr, const char *func,
4633 const char *fmt, ...)
4634{
4635 va_list va;
4636 QError *qerror;
4637
4638 assert(qemu_error_sink != NULL);
4639
4640 va_start(va, fmt);
4641 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4642 va_end(va);
4643
4644 switch (qemu_error_sink->dest) {
4645 case ERR_SINK_FILE:
4646 qerror_print(qerror);
4647 QDECREF(qerror);
4648 break;
4649 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004650 /* report only the first error */
4651 if (!qemu_error_sink->mon->error) {
4652 qemu_error_sink->mon->error = qerror;
4653 } else {
4654 /* XXX: warn the programmer */
4655 QDECREF(qerror);
4656 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004657 break;
4658 }
4659}