blob: c1e0af8cdc96160b2b23f700241a34acada8d1c8 [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 */
1151static void 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 */
aliguori376253e2009-03-05 23:01:23 +00001157 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +00001158 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +00001159}
1160
aliguoribb5fc202009-03-05 23:01:15 +00001161static void bdrv_key_cb(void *opaque, int err)
1162{
aliguori376253e2009-03-05 23:01:23 +00001163 Monitor *mon = opaque;
1164
aliguoribb5fc202009-03-05 23:01:15 +00001165 /* another key was set successfully, retry to continue */
1166 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001167 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001168}
1169
1170static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1171{
aliguori376253e2009-03-05 23:01:23 +00001172 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001173
aliguori376253e2009-03-05 23:01:23 +00001174 if (!context->err && bdrv_key_required(bs)) {
1175 context->err = -EBUSY;
1176 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1177 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001178 }
1179}
1180
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001181static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001182{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001183 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001184 if (!device)
1185 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1186 if (gdbserver_start(device) < 0) {
1187 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1188 device);
1189 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001190 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001191 } else {
aliguori59030a82009-04-05 18:43:41 +00001192 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1193 device);
bellard8a7ddc32004-03-31 19:00:16 +00001194 }
1195}
1196
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001197static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001198{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001199 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001200 if (select_watchdog_action(action) == -1) {
1201 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1202 }
1203}
1204
aliguori376253e2009-03-05 23:01:23 +00001205static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001206{
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001208 switch(c) {
1209 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001210 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001211 break;
1212 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001213 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001214 break;
1215 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001216 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001217 break;
1218 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 default:
1222 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001224 } else {
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001226 }
1227 break;
1228 }
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001230}
1231
aliguori376253e2009-03-05 23:01:23 +00001232static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001233 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001234{
bellard6a00d602005-11-21 23:25:50 +00001235 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001236 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001237 uint8_t buf[16];
1238 uint64_t v;
1239
1240 if (format == 'i') {
1241 int flags;
1242 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001243 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001244 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001245 return;
bellard9307c4c2004-04-04 12:57:25 +00001246#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001247 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001248 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001249 } else if (wsize == 4) {
1250 flags = 0;
1251 } else {
bellard6a15fd12006-04-12 21:07:07 +00001252 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001253 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001254 if (env) {
1255#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001256 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001257 (env->segs[R_CS].flags & DESC_L_MASK))
1258 flags = 2;
1259 else
1260#endif
1261 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1262 flags = 1;
1263 }
bellard4c27ba22004-04-25 18:05:08 +00001264 }
1265#endif
aliguori376253e2009-03-05 23:01:23 +00001266 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001267 return;
1268 }
1269
1270 len = wsize * count;
1271 if (wsize == 1)
1272 line_size = 8;
1273 else
1274 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001275 max_digits = 0;
1276
1277 switch(format) {
1278 case 'o':
1279 max_digits = (wsize * 8 + 2) / 3;
1280 break;
1281 default:
1282 case 'x':
1283 max_digits = (wsize * 8) / 4;
1284 break;
1285 case 'u':
1286 case 'd':
1287 max_digits = (wsize * 8 * 10 + 32) / 33;
1288 break;
1289 case 'c':
1290 wsize = 1;
1291 break;
1292 }
1293
1294 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001295 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001296 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001297 else
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001299 l = len;
1300 if (l > line_size)
1301 l = line_size;
1302 if (is_physical) {
1303 cpu_physical_memory_rw(addr, buf, l, 0);
1304 } else {
bellard6a00d602005-11-21 23:25:50 +00001305 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001306 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001308 break;
1309 }
bellard9307c4c2004-04-04 12:57:25 +00001310 }
ths5fafdf22007-09-16 21:08:06 +00001311 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001312 while (i < l) {
1313 switch(wsize) {
1314 default:
1315 case 1:
1316 v = ldub_raw(buf + i);
1317 break;
1318 case 2:
1319 v = lduw_raw(buf + i);
1320 break;
1321 case 4:
bellard92a31b12005-02-10 22:00:52 +00001322 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001323 break;
1324 case 8:
1325 v = ldq_raw(buf + i);
1326 break;
1327 }
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001329 switch(format) {
1330 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001331 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001332 break;
1333 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001335 break;
1336 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001341 break;
1342 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001344 break;
1345 }
1346 i += wsize;
1347 }
aliguori376253e2009-03-05 23:01:23 +00001348 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001349 addr += l;
1350 len -= l;
1351 }
1352}
1353
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001354static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001355{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356 int count = qdict_get_int(qdict, "count");
1357 int format = qdict_get_int(qdict, "format");
1358 int size = qdict_get_int(qdict, "size");
1359 target_long addr = qdict_get_int(qdict, "addr");
1360
aliguori376253e2009-03-05 23:01:23 +00001361 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001362}
1363
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001364static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001365{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001366 int count = qdict_get_int(qdict, "count");
1367 int format = qdict_get_int(qdict, "format");
1368 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001369 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001370
aliguori376253e2009-03-05 23:01:23 +00001371 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001372}
1373
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001374static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001375{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001376 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001377 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001378
blueswir17743e582007-09-24 18:39:04 +00001379#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001380 switch(format) {
1381 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001382 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001383 break;
1384 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001386 break;
1387 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001389 break;
1390 default:
1391 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001396 break;
1397 }
bellard92a31b12005-02-10 22:00:52 +00001398#else
1399 switch(format) {
1400 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001402 break;
1403 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001405 break;
1406 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001408 break;
1409 default:
1410 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001412 break;
1413 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001414 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001415 break;
1416 }
1417#endif
aliguori376253e2009-03-05 23:01:23 +00001418 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001419}
1420
Luiz Capitulino57e09452009-10-16 12:23:43 -03001421static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001422{
1423 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001424 uint32_t size = qdict_get_int(qdict, "size");
1425 const char *filename = qdict_get_str(qdict, "filename");
1426 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001427 uint32_t l;
1428 CPUState *env;
1429 uint8_t buf[1024];
1430
1431 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001432
1433 f = fopen(filename, "wb");
1434 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001435 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
bellardb371dc52007-01-03 15:20:39 +00001436 return;
1437 }
1438 while (size != 0) {
1439 l = sizeof(buf);
1440 if (l > size)
1441 l = size;
1442 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001443 if (fwrite(buf, 1, l, f) != l) {
1444 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1445 goto exit;
1446 }
bellardb371dc52007-01-03 15:20:39 +00001447 addr += l;
1448 size -= l;
1449 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001450exit:
bellardb371dc52007-01-03 15:20:39 +00001451 fclose(f);
1452}
1453
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001454static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1455 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001456{
1457 FILE *f;
1458 uint32_t l;
1459 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001460 uint32_t size = qdict_get_int(qdict, "size");
1461 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001462 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001463
1464 f = fopen(filename, "wb");
1465 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001466 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001467 return;
1468 }
1469 while (size != 0) {
1470 l = sizeof(buf);
1471 if (l > size)
1472 l = size;
1473 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001474 if (fwrite(buf, 1, l, f) != l) {
1475 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1476 goto exit;
1477 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001478 fflush(f);
1479 addr += l;
1480 size -= l;
1481 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001482exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001483 fclose(f);
1484}
1485
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001486static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001487{
1488 uint32_t addr;
1489 uint8_t buf[1];
1490 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001491 uint32_t start = qdict_get_int(qdict, "start");
1492 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001493
1494 sum = 0;
1495 for(addr = start; addr < (start + size); addr++) {
1496 cpu_physical_memory_rw(addr, buf, 1, 0);
1497 /* BSD sum algorithm ('sum' Unix command) */
1498 sum = (sum >> 1) | (sum << 15);
1499 sum += buf[0];
1500 }
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001502}
1503
bellarda3a91a32004-06-04 11:06:21 +00001504typedef struct {
1505 int keycode;
1506 const char *name;
1507} KeyDef;
1508
1509static const KeyDef key_defs[] = {
1510 { 0x2a, "shift" },
1511 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001512
bellarda3a91a32004-06-04 11:06:21 +00001513 { 0x38, "alt" },
1514 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001515 { 0x64, "altgr" },
1516 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001517 { 0x1d, "ctrl" },
1518 { 0x9d, "ctrl_r" },
1519
1520 { 0xdd, "menu" },
1521
1522 { 0x01, "esc" },
1523
1524 { 0x02, "1" },
1525 { 0x03, "2" },
1526 { 0x04, "3" },
1527 { 0x05, "4" },
1528 { 0x06, "5" },
1529 { 0x07, "6" },
1530 { 0x08, "7" },
1531 { 0x09, "8" },
1532 { 0x0a, "9" },
1533 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001534 { 0x0c, "minus" },
1535 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001536 { 0x0e, "backspace" },
1537
1538 { 0x0f, "tab" },
1539 { 0x10, "q" },
1540 { 0x11, "w" },
1541 { 0x12, "e" },
1542 { 0x13, "r" },
1543 { 0x14, "t" },
1544 { 0x15, "y" },
1545 { 0x16, "u" },
1546 { 0x17, "i" },
1547 { 0x18, "o" },
1548 { 0x19, "p" },
1549
1550 { 0x1c, "ret" },
1551
1552 { 0x1e, "a" },
1553 { 0x1f, "s" },
1554 { 0x20, "d" },
1555 { 0x21, "f" },
1556 { 0x22, "g" },
1557 { 0x23, "h" },
1558 { 0x24, "j" },
1559 { 0x25, "k" },
1560 { 0x26, "l" },
1561
1562 { 0x2c, "z" },
1563 { 0x2d, "x" },
1564 { 0x2e, "c" },
1565 { 0x2f, "v" },
1566 { 0x30, "b" },
1567 { 0x31, "n" },
1568 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001569 { 0x33, "comma" },
1570 { 0x34, "dot" },
1571 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001572
balrog4d3b6f62008-02-10 16:33:14 +00001573 { 0x37, "asterisk" },
1574
bellarda3a91a32004-06-04 11:06:21 +00001575 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001576 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001577 { 0x3b, "f1" },
1578 { 0x3c, "f2" },
1579 { 0x3d, "f3" },
1580 { 0x3e, "f4" },
1581 { 0x3f, "f5" },
1582 { 0x40, "f6" },
1583 { 0x41, "f7" },
1584 { 0x42, "f8" },
1585 { 0x43, "f9" },
1586 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001587 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001588 { 0x46, "scroll_lock" },
1589
bellard64866c32006-05-07 18:03:31 +00001590 { 0xb5, "kp_divide" },
1591 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001592 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001593 { 0x4e, "kp_add" },
1594 { 0x9c, "kp_enter" },
1595 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001596 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001597
1598 { 0x52, "kp_0" },
1599 { 0x4f, "kp_1" },
1600 { 0x50, "kp_2" },
1601 { 0x51, "kp_3" },
1602 { 0x4b, "kp_4" },
1603 { 0x4c, "kp_5" },
1604 { 0x4d, "kp_6" },
1605 { 0x47, "kp_7" },
1606 { 0x48, "kp_8" },
1607 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001608
bellarda3a91a32004-06-04 11:06:21 +00001609 { 0x56, "<" },
1610
1611 { 0x57, "f11" },
1612 { 0x58, "f12" },
1613
1614 { 0xb7, "print" },
1615
1616 { 0xc7, "home" },
1617 { 0xc9, "pgup" },
1618 { 0xd1, "pgdn" },
1619 { 0xcf, "end" },
1620
1621 { 0xcb, "left" },
1622 { 0xc8, "up" },
1623 { 0xd0, "down" },
1624 { 0xcd, "right" },
1625
1626 { 0xd2, "insert" },
1627 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001628#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1629 { 0xf0, "stop" },
1630 { 0xf1, "again" },
1631 { 0xf2, "props" },
1632 { 0xf3, "undo" },
1633 { 0xf4, "front" },
1634 { 0xf5, "copy" },
1635 { 0xf6, "open" },
1636 { 0xf7, "paste" },
1637 { 0xf8, "find" },
1638 { 0xf9, "cut" },
1639 { 0xfa, "lf" },
1640 { 0xfb, "help" },
1641 { 0xfc, "meta_l" },
1642 { 0xfd, "meta_r" },
1643 { 0xfe, "compose" },
1644#endif
bellarda3a91a32004-06-04 11:06:21 +00001645 { 0, NULL },
1646};
1647
1648static int get_keycode(const char *key)
1649{
1650 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001651 char *endp;
1652 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001653
1654 for(p = key_defs; p->name != NULL; p++) {
1655 if (!strcmp(key, p->name))
1656 return p->keycode;
1657 }
bellard64866c32006-05-07 18:03:31 +00001658 if (strstart(key, "0x", NULL)) {
1659 ret = strtoul(key, &endp, 0);
1660 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1661 return ret;
1662 }
bellarda3a91a32004-06-04 11:06:21 +00001663 return -1;
1664}
1665
balrogc8256f92008-06-08 22:45:01 +00001666#define MAX_KEYCODES 16
1667static uint8_t keycodes[MAX_KEYCODES];
1668static int nb_pending_keycodes;
1669static QEMUTimer *key_timer;
1670
1671static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001672{
balrogc8256f92008-06-08 22:45:01 +00001673 int keycode;
1674
1675 while (nb_pending_keycodes > 0) {
1676 nb_pending_keycodes--;
1677 keycode = keycodes[nb_pending_keycodes];
1678 if (keycode & 0x80)
1679 kbd_put_keycode(0xe0);
1680 kbd_put_keycode(keycode | 0x80);
1681 }
1682}
1683
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001684static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001685{
balrog3401c0d2008-06-04 10:05:59 +00001686 char keyname_buf[16];
1687 char *separator;
1688 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001689 const char *string = qdict_get_str(qdict, "string");
1690 int has_hold_time = qdict_haskey(qdict, "hold_time");
1691 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001692
balrogc8256f92008-06-08 22:45:01 +00001693 if (nb_pending_keycodes > 0) {
1694 qemu_del_timer(key_timer);
1695 release_keys(NULL);
1696 }
1697 if (!has_hold_time)
1698 hold_time = 100;
1699 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001700 while (1) {
1701 separator = strchr(string, '-');
1702 keyname_len = separator ? separator - string : strlen(string);
1703 if (keyname_len > 0) {
1704 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1705 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001706 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001707 return;
bellarda3a91a32004-06-04 11:06:21 +00001708 }
balrogc8256f92008-06-08 22:45:01 +00001709 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001710 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001711 return;
1712 }
1713 keyname_buf[keyname_len] = 0;
1714 keycode = get_keycode(keyname_buf);
1715 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001716 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001717 return;
1718 }
balrogc8256f92008-06-08 22:45:01 +00001719 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001720 }
balrog3401c0d2008-06-04 10:05:59 +00001721 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001722 break;
balrog3401c0d2008-06-04 10:05:59 +00001723 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001724 }
balrogc8256f92008-06-08 22:45:01 +00001725 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001726 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001727 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001728 keycode = keycodes[i];
1729 if (keycode & 0x80)
1730 kbd_put_keycode(0xe0);
1731 kbd_put_keycode(keycode & 0x7f);
1732 }
balrogc8256f92008-06-08 22:45:01 +00001733 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001734 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001735 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001736}
1737
bellard13224a82006-07-14 22:03:35 +00001738static int mouse_button_state;
1739
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001740static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001741{
1742 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001743 const char *dx_str = qdict_get_str(qdict, "dx_str");
1744 const char *dy_str = qdict_get_str(qdict, "dy_str");
1745 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001746 dx = strtol(dx_str, NULL, 0);
1747 dy = strtol(dy_str, NULL, 0);
1748 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001749 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001750 dz = strtol(dz_str, NULL, 0);
1751 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1752}
1753
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001754static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001755{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001756 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001757 mouse_button_state = button_state;
1758 kbd_mouse_event(0, 0, 0, mouse_button_state);
1759}
1760
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001761static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001762{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001763 int size = qdict_get_int(qdict, "size");
1764 int addr = qdict_get_int(qdict, "addr");
1765 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001766 uint32_t val;
1767 int suffix;
1768
1769 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001770 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001771 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001772 addr++;
1773 }
1774 addr &= 0xffff;
1775
1776 switch(size) {
1777 default:
1778 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001779 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001780 suffix = 'b';
1781 break;
1782 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001783 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001784 suffix = 'w';
1785 break;
1786 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001787 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001788 suffix = 'l';
1789 break;
1790 }
aliguori376253e2009-03-05 23:01:23 +00001791 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1792 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001793}
bellarda3a91a32004-06-04 11:06:21 +00001794
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001795static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001796{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001797 int size = qdict_get_int(qdict, "size");
1798 int addr = qdict_get_int(qdict, "addr");
1799 int val = qdict_get_int(qdict, "val");
1800
Jan Kiszkaf1147842009-07-14 10:20:11 +02001801 addr &= IOPORTS_MASK;
1802
1803 switch (size) {
1804 default:
1805 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001806 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001807 break;
1808 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001809 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001810 break;
1811 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001812 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001813 break;
1814 }
1815}
1816
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001817static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001818{
1819 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001820 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001821
Jan Kiszka76e30d02009-07-02 00:19:02 +02001822 res = qemu_boot_set(bootdevice);
1823 if (res == 0) {
1824 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1825 } else if (res > 0) {
1826 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001827 } else {
aliguori376253e2009-03-05 23:01:23 +00001828 monitor_printf(mon, "no function defined to set boot device list for "
1829 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001830 }
1831}
1832
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001833/**
1834 * do_system_reset(): Issue a machine reset
1835 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001836static int do_system_reset(Monitor *mon, const QDict *qdict,
1837 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001838{
1839 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001840 return 0;
bellarde4f90822004-06-20 12:35:44 +00001841}
1842
Luiz Capitulino43076662009-10-07 13:41:59 -03001843/**
1844 * do_system_powerdown(): Issue a machine powerdown
1845 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001846static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1847 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001848{
1849 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001850 return 0;
bellard34751872005-07-02 14:31:34 +00001851}
1852
bellardb86bda52004-09-18 19:32:46 +00001853#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001854static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001855{
aliguori376253e2009-03-05 23:01:23 +00001856 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1857 addr,
1858 pte & mask,
1859 pte & PG_GLOBAL_MASK ? 'G' : '-',
1860 pte & PG_PSE_MASK ? 'P' : '-',
1861 pte & PG_DIRTY_MASK ? 'D' : '-',
1862 pte & PG_ACCESSED_MASK ? 'A' : '-',
1863 pte & PG_PCD_MASK ? 'C' : '-',
1864 pte & PG_PWT_MASK ? 'T' : '-',
1865 pte & PG_USER_MASK ? 'U' : '-',
1866 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001867}
1868
aliguori376253e2009-03-05 23:01:23 +00001869static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001870{
bellard6a00d602005-11-21 23:25:50 +00001871 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001872 int l1, l2;
1873 uint32_t pgd, pde, pte;
1874
bellard6a00d602005-11-21 23:25:50 +00001875 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001876
bellardb86bda52004-09-18 19:32:46 +00001877 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001878 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001879 return;
1880 }
1881 pgd = env->cr[3] & ~0xfff;
1882 for(l1 = 0; l1 < 1024; l1++) {
1883 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1884 pde = le32_to_cpu(pde);
1885 if (pde & PG_PRESENT_MASK) {
1886 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001887 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001888 } else {
1889 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001890 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001891 (uint8_t *)&pte, 4);
1892 pte = le32_to_cpu(pte);
1893 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001894 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001895 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001896 ~0xfff);
1897 }
1898 }
1899 }
1900 }
1901 }
1902}
1903
aliguori376253e2009-03-05 23:01:23 +00001904static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001905 uint32_t end, int prot)
1906{
bellard9746b152004-11-11 18:30:24 +00001907 int prot1;
1908 prot1 = *plast_prot;
1909 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001910 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001911 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1912 *pstart, end, end - *pstart,
1913 prot1 & PG_USER_MASK ? 'u' : '-',
1914 'r',
1915 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001916 }
1917 if (prot != 0)
1918 *pstart = end;
1919 else
1920 *pstart = -1;
1921 *plast_prot = prot;
1922 }
1923}
1924
aliguori376253e2009-03-05 23:01:23 +00001925static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001926{
bellard6a00d602005-11-21 23:25:50 +00001927 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001928 int l1, l2, prot, last_prot;
1929 uint32_t pgd, pde, pte, start, end;
1930
bellard6a00d602005-11-21 23:25:50 +00001931 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001932
bellardb86bda52004-09-18 19:32:46 +00001933 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001934 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001935 return;
1936 }
1937 pgd = env->cr[3] & ~0xfff;
1938 last_prot = 0;
1939 start = -1;
1940 for(l1 = 0; l1 < 1024; l1++) {
1941 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1942 pde = le32_to_cpu(pde);
1943 end = l1 << 22;
1944 if (pde & PG_PRESENT_MASK) {
1945 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1946 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001947 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001948 } else {
1949 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001950 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001951 (uint8_t *)&pte, 4);
1952 pte = le32_to_cpu(pte);
1953 end = (l1 << 22) + (l2 << 12);
1954 if (pte & PG_PRESENT_MASK) {
1955 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1956 } else {
1957 prot = 0;
1958 }
aliguori376253e2009-03-05 23:01:23 +00001959 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001960 }
1961 }
1962 } else {
1963 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001964 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001965 }
1966 }
1967}
1968#endif
1969
aurel327c664e22009-03-03 06:12:22 +00001970#if defined(TARGET_SH4)
1971
aliguori376253e2009-03-05 23:01:23 +00001972static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001973{
aliguori376253e2009-03-05 23:01:23 +00001974 monitor_printf(mon, " tlb%i:\t"
1975 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1976 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1977 "dirty=%hhu writethrough=%hhu\n",
1978 idx,
1979 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1980 tlb->v, tlb->sh, tlb->c, tlb->pr,
1981 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001982}
1983
aliguori376253e2009-03-05 23:01:23 +00001984static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001985{
1986 CPUState *env = mon_get_cpu();
1987 int i;
1988
aliguori376253e2009-03-05 23:01:23 +00001989 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001990 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001991 print_tlb (mon, i, &env->itlb[i]);
1992 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001993 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001994 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001995}
1996
1997#endif
1998
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001999static void do_info_kvm_print(Monitor *mon, const QObject *data)
2000{
2001 QDict *qdict;
2002
2003 qdict = qobject_to_qdict(data);
2004
2005 monitor_printf(mon, "kvm support: ");
2006 if (qdict_get_bool(qdict, "present")) {
2007 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2008 "enabled" : "disabled");
2009 } else {
2010 monitor_printf(mon, "not compiled\n");
2011 }
2012}
2013
2014/**
2015 * do_info_kvm(): Show KVM information
2016 *
2017 * Return a QDict with the following information:
2018 *
2019 * - "enabled": true if KVM support is enabled, false otherwise
2020 * - "present": true if QEMU has KVM support, false otherwise
2021 *
2022 * Example:
2023 *
2024 * { "enabled": true, "present": true }
2025 */
2026static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002027{
2028#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002029 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2030 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002031#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002032 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002033#endif
2034}
2035
aliguori030ea372009-04-21 22:30:47 +00002036static void do_info_numa(Monitor *mon)
2037{
aliguorib28b6232009-04-22 20:20:29 +00002038 int i;
aliguori030ea372009-04-21 22:30:47 +00002039 CPUState *env;
2040
2041 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2042 for (i = 0; i < nb_numa_nodes; i++) {
2043 monitor_printf(mon, "node %d cpus:", i);
2044 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2045 if (env->numa_node == i) {
2046 monitor_printf(mon, " %d", env->cpu_index);
2047 }
2048 }
2049 monitor_printf(mon, "\n");
2050 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2051 node_mem[i] >> 20);
2052 }
2053}
2054
bellard5f1ce942006-02-08 22:40:15 +00002055#ifdef CONFIG_PROFILER
2056
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002057int64_t qemu_time;
2058int64_t dev_time;
2059
aliguori376253e2009-03-05 23:01:23 +00002060static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002061{
2062 int64_t total;
2063 total = qemu_time;
2064 if (total == 0)
2065 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002066 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002067 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002068 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002069 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002070 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002071 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002072}
2073#else
aliguori376253e2009-03-05 23:01:23 +00002074static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002075{
aliguori376253e2009-03-05 23:01:23 +00002076 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002077}
2078#endif
2079
bellardec36b692006-07-16 18:57:03 +00002080/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002081static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002082
aliguori376253e2009-03-05 23:01:23 +00002083static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002084{
2085 int i;
2086 CaptureState *s;
2087
2088 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002089 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002090 s->ops.info (s->opaque);
2091 }
2092}
2093
Blue Swirl23130862009-06-06 08:22:04 +00002094#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002095static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002096{
2097 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002098 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002099 CaptureState *s;
2100
2101 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2102 if (i == n) {
2103 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002104 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002105 qemu_free (s);
2106 return;
2107 }
2108 }
2109}
2110
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002111static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002112{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002113 const char *path = qdict_get_str(qdict, "path");
2114 int has_freq = qdict_haskey(qdict, "freq");
2115 int freq = qdict_get_try_int(qdict, "freq", -1);
2116 int has_bits = qdict_haskey(qdict, "bits");
2117 int bits = qdict_get_try_int(qdict, "bits", -1);
2118 int has_channels = qdict_haskey(qdict, "nchannels");
2119 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002120 CaptureState *s;
2121
2122 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002123
2124 freq = has_freq ? freq : 44100;
2125 bits = has_bits ? bits : 16;
2126 nchannels = has_channels ? nchannels : 2;
2127
2128 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002129 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002130 qemu_free (s);
2131 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002132 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002133}
2134#endif
2135
aurel32dc1c0b72008-04-27 23:52:12 +00002136#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002137static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002138{
2139 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002140 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002141
2142 for (env = first_cpu; env != NULL; env = env->next_cpu)
2143 if (env->cpu_index == cpu_index) {
2144 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2145 break;
2146 }
2147}
2148#endif
2149
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002150static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002151{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002152 QDict *qdict;
2153
2154 qdict = qobject_to_qdict(data);
2155
2156 monitor_printf(mon, "VM status: ");
2157 if (qdict_get_bool(qdict, "running")) {
2158 monitor_printf(mon, "running");
2159 if (qdict_get_bool(qdict, "singlestep")) {
2160 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002161 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002162 } else {
2163 monitor_printf(mon, "paused");
2164 }
2165
2166 monitor_printf(mon, "\n");
2167}
2168
2169/**
2170 * do_info_status(): VM status
2171 *
2172 * Return a QDict with the following information:
2173 *
2174 * - "running": true if the VM is running, or false if it is paused
2175 * - "singlestep": true if the VM is in single step mode, false otherwise
2176 *
2177 * Example:
2178 *
2179 * { "running": true, "singlestep": false }
2180 */
2181static void do_info_status(Monitor *mon, QObject **ret_data)
2182{
2183 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2184 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002185}
2186
Adam Litke625a5be2010-01-26 14:17:35 -06002187static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002188{
Adam Litke625a5be2010-01-26 14:17:35 -06002189 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002190
Adam Litke625a5be2010-01-26 14:17:35 -06002191 if (strcmp(key, "actual"))
2192 monitor_printf(mon, ",%s=%" PRId64, key,
2193 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002194}
2195
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002196static void monitor_print_balloon(Monitor *mon, const QObject *data)
2197{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002198 QDict *qdict;
2199
2200 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002201 if (!qdict_haskey(qdict, "actual"))
2202 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002203
Adam Litke625a5be2010-01-26 14:17:35 -06002204 monitor_printf(mon, "balloon: actual=%" PRId64,
2205 qdict_get_int(qdict, "actual") >> 20);
2206 qdict_iter(qdict, print_balloon_stat, mon);
2207 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002208}
2209
2210/**
2211 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002212 *
Adam Litke625a5be2010-01-26 14:17:35 -06002213 * Make an asynchronous request for balloon info. When the request completes
2214 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002215 *
Adam Litke625a5be2010-01-26 14:17:35 -06002216 * - "actual": current balloon value in bytes
2217 * The following fields may or may not be present:
2218 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2219 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2220 * - "major_page_faults": Number of major faults
2221 * - "minor_page_faults": Number of minor faults
2222 * - "free_mem": Total amount of free and unused memory (bytes)
2223 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002224 *
2225 * Example:
2226 *
Adam Litke625a5be2010-01-26 14:17:35 -06002227 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2228 * "major_page_faults": 142, "minor_page_faults": 239245,
2229 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002230 */
Adam Litke625a5be2010-01-26 14:17:35 -06002231static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002232{
Adam Litke625a5be2010-01-26 14:17:35 -06002233 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002234
Adam Litke625a5be2010-01-26 14:17:35 -06002235 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2236 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2237 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002238 }
Adam Litke625a5be2010-01-26 14:17:35 -06002239
2240 ret = qemu_balloon_status(cb, opaque);
2241 if (!ret) {
2242 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2243 return -1;
2244 }
2245
2246 return 0;
2247}
2248
2249/**
2250 * do_balloon(): Request VM to change its memory allocation
2251 */
2252static int do_balloon(Monitor *mon, const QDict *params,
2253 MonitorCompletion cb, void *opaque)
2254{
2255 int ret;
2256
2257 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2258 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2259 return -1;
2260 }
2261
2262 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2263 if (ret == 0) {
2264 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2265 return -1;
2266 }
2267
2268 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002269}
2270
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002271static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002272{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002273 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002274
aliguori76655d62009-03-06 20:27:37 +00002275 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002276 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002277 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002278 return acl;
2279}
aliguori76655d62009-03-06 20:27:37 +00002280
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002281static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002282{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002283 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002284 qemu_acl *acl = find_acl(mon, aclname);
2285 qemu_acl_entry *entry;
2286 int i = 0;
2287
2288 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002289 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002290 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002291 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002292 i++;
2293 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002294 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002295 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002296 }
2297}
2298
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002299static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002300{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002301 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002302 qemu_acl *acl = find_acl(mon, aclname);
2303
2304 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002305 qemu_acl_reset(acl);
2306 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002307 }
2308}
aliguori76655d62009-03-06 20:27:37 +00002309
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002310static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002311{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002312 const char *aclname = qdict_get_str(qdict, "aclname");
2313 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002314 qemu_acl *acl = find_acl(mon, aclname);
2315
2316 if (acl) {
2317 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002318 acl->defaultDeny = 0;
2319 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002320 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002321 acl->defaultDeny = 1;
2322 monitor_printf(mon, "acl: policy set to 'deny'\n");
2323 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002324 monitor_printf(mon, "acl: unknown policy '%s', "
2325 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002326 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002327 }
2328}
aliguori76655d62009-03-06 20:27:37 +00002329
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002330static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002331{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002332 const char *aclname = qdict_get_str(qdict, "aclname");
2333 const char *match = qdict_get_str(qdict, "match");
2334 const char *policy = qdict_get_str(qdict, "policy");
2335 int has_index = qdict_haskey(qdict, "index");
2336 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002337 qemu_acl *acl = find_acl(mon, aclname);
2338 int deny, ret;
2339
2340 if (acl) {
2341 if (strcmp(policy, "allow") == 0) {
2342 deny = 0;
2343 } else if (strcmp(policy, "deny") == 0) {
2344 deny = 1;
2345 } else {
2346 monitor_printf(mon, "acl: unknown policy '%s', "
2347 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002348 return;
2349 }
aliguori28a76be2009-03-06 20:27:40 +00002350 if (has_index)
2351 ret = qemu_acl_insert(acl, deny, match, index);
2352 else
2353 ret = qemu_acl_append(acl, deny, match);
2354 if (ret < 0)
2355 monitor_printf(mon, "acl: unable to add acl entry\n");
2356 else
2357 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002358 }
2359}
aliguori76655d62009-03-06 20:27:37 +00002360
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002361static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002362{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002363 const char *aclname = qdict_get_str(qdict, "aclname");
2364 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002365 qemu_acl *acl = find_acl(mon, aclname);
2366 int ret;
aliguori76655d62009-03-06 20:27:37 +00002367
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002368 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002369 ret = qemu_acl_remove(acl, match);
2370 if (ret < 0)
2371 monitor_printf(mon, "acl: no matching acl entry\n");
2372 else
2373 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002374 }
2375}
2376
Huang Ying79c4f6b2009-06-23 10:05:14 +08002377#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002378static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002379{
2380 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002381 int cpu_index = qdict_get_int(qdict, "cpu_index");
2382 int bank = qdict_get_int(qdict, "bank");
2383 uint64_t status = qdict_get_int(qdict, "status");
2384 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2385 uint64_t addr = qdict_get_int(qdict, "addr");
2386 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002387
2388 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2389 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2390 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2391 break;
2392 }
2393}
2394#endif
2395
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002396static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002397{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002398 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002399 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002400 int fd;
2401
2402 fd = qemu_chr_get_msgfd(mon->chr);
2403 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002404 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002405 return;
2406 }
2407
2408 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002409 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002410 return;
2411 }
2412
2413 fd = dup(fd);
2414 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002415 if (errno == EMFILE)
2416 qemu_error_new(QERR_TOO_MANY_FILES);
2417 else
2418 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002419 return;
2420 }
2421
Blue Swirl72cf2d42009-09-12 07:36:22 +00002422 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002423 if (strcmp(monfd->name, fdname) != 0) {
2424 continue;
2425 }
2426
2427 close(monfd->fd);
2428 monfd->fd = fd;
2429 return;
2430 }
2431
Anthony Liguoric227f092009-10-01 16:12:16 -05002432 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002433 monfd->name = qemu_strdup(fdname);
2434 monfd->fd = fd;
2435
Blue Swirl72cf2d42009-09-12 07:36:22 +00002436 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002437}
2438
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002439static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002440{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002441 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002442 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002443
Blue Swirl72cf2d42009-09-12 07:36:22 +00002444 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002445 if (strcmp(monfd->name, fdname) != 0) {
2446 continue;
2447 }
2448
Blue Swirl72cf2d42009-09-12 07:36:22 +00002449 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002450 close(monfd->fd);
2451 qemu_free(monfd->name);
2452 qemu_free(monfd);
2453 return;
2454 }
2455
Markus Armbruster063c1a02009-12-07 21:37:11 +01002456 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002457}
2458
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002459static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002460{
2461 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002462 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002463
2464 vm_stop(0);
2465
Juan Quintela05f24012009-08-20 19:42:22 +02002466 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002467 vm_start();
2468}
2469
Mark McLoughlin7768e042009-07-22 09:11:41 +01002470int monitor_get_fd(Monitor *mon, const char *fdname)
2471{
Anthony Liguoric227f092009-10-01 16:12:16 -05002472 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002473
Blue Swirl72cf2d42009-09-12 07:36:22 +00002474 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002475 int fd;
2476
2477 if (strcmp(monfd->name, fdname) != 0) {
2478 continue;
2479 }
2480
2481 fd = monfd->fd;
2482
2483 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002484 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002485 qemu_free(monfd->name);
2486 qemu_free(monfd);
2487
2488 return fd;
2489 }
2490
2491 return -1;
2492}
2493
Anthony Liguoric227f092009-10-01 16:12:16 -05002494static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002495#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002496 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002497};
2498
Blue Swirl23130862009-06-06 08:22:04 +00002499/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002500static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002501 {
2502 .name = "version",
2503 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002504 .params = "",
2505 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002506 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002507 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002508 },
2509 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002510 .name = "commands",
2511 .args_type = "",
2512 .params = "",
2513 .help = "list QMP available commands",
2514 .user_print = monitor_user_noop,
2515 .mhandler.info_new = do_info_commands,
2516 },
2517 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002518 .name = "network",
2519 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 .params = "",
2521 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002522 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 },
2524 {
2525 .name = "chardev",
2526 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 .params = "",
2528 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002529 .user_print = qemu_chr_info_print,
2530 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 },
2532 {
2533 .name = "block",
2534 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002535 .params = "",
2536 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002537 .user_print = bdrv_info_print,
2538 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002539 },
2540 {
2541 .name = "blockstats",
2542 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002543 .params = "",
2544 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002545 .user_print = bdrv_stats_print,
2546 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
2549 .name = "registers",
2550 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 .params = "",
2552 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002553 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 },
2555 {
2556 .name = "cpus",
2557 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .params = "",
2559 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002560 .user_print = monitor_print_cpus,
2561 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 },
2563 {
2564 .name = "history",
2565 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 .params = "",
2567 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002568 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 },
2570 {
2571 .name = "irq",
2572 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 .params = "",
2574 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002575 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 },
2577 {
2578 .name = "pic",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002582 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 },
2584 {
2585 .name = "pci",
2586 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 .params = "",
2588 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002589 .user_print = do_pci_info_print,
2590 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 },
aurel327c664e22009-03-03 06:12:22 +00002592#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 {
2594 .name = "tlb",
2595 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 .params = "",
2597 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002598 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 },
aurel327c664e22009-03-03 06:12:22 +00002600#endif
2601#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 {
2603 .name = "mem",
2604 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 .params = "",
2606 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002607 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 },
2609 {
2610 .name = "hpet",
2611 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 .params = "",
2613 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002614 .user_print = do_info_hpet_print,
2615 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 },
bellardb86bda52004-09-18 19:32:46 +00002617#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 {
2619 .name = "jit",
2620 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 .params = "",
2622 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002623 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 },
2625 {
2626 .name = "kvm",
2627 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 .params = "",
2629 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002630 .user_print = do_info_kvm_print,
2631 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 },
2633 {
2634 .name = "numa",
2635 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 .params = "",
2637 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002638 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 },
2640 {
2641 .name = "usb",
2642 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .params = "",
2644 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002645 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 },
2647 {
2648 .name = "usbhost",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002652 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
2654 {
2655 .name = "profile",
2656 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 .params = "",
2658 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002659 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
2661 {
2662 .name = "capture",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002666 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "snapshots",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002673 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
2675 {
2676 .name = "status",
2677 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 .params = "",
2679 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002680 .user_print = do_info_status_print,
2681 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 },
2683 {
2684 .name = "pcmcia",
2685 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 .params = "",
2687 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002688 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 },
2690 {
2691 .name = "mice",
2692 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 .params = "",
2694 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002695 .user_print = do_info_mice_print,
2696 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
2699 .name = "vnc",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002703 .user_print = do_info_vnc_print,
2704 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "name",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002711 .user_print = do_info_name_print,
2712 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 },
2714 {
2715 .name = "uuid",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002719 .user_print = do_info_uuid_print,
2720 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
j_mayer76a66252007-03-07 08:32:30 +00002722#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 {
2724 .name = "cpustats",
2725 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 .params = "",
2727 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002728 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 },
j_mayer76a66252007-03-07 08:32:30 +00002730#endif
blueswir131a60e22007-10-26 18:42:59 +00002731#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 {
2733 .name = "usernet",
2734 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 .params = "",
2736 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002737 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 },
blueswir131a60e22007-10-26 18:42:59 +00002739#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 {
2741 .name = "migrate",
2742 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 .params = "",
2744 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002745 .user_print = do_info_migrate_print,
2746 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 },
2748 {
2749 .name = "balloon",
2750 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 .params = "",
2752 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002753 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002754 .mhandler.info_async = do_info_balloon,
2755 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
2757 {
2758 .name = "qtree",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002762 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
2764 {
2765 .name = "qdm",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002769 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
2771 {
2772 .name = "roms",
2773 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 .params = "",
2775 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002776 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 },
2778 {
2779 .name = NULL,
2780 },
bellard9dc39cb2004-03-14 21:38:27 +00002781};
2782
bellard9307c4c2004-04-04 12:57:25 +00002783/*******************************************************************/
2784
2785static const char *pch;
2786static jmp_buf expr_env;
2787
bellard92a31b12005-02-10 22:00:52 +00002788#define MD_TLONG 0
2789#define MD_I32 1
2790
bellard9307c4c2004-04-04 12:57:25 +00002791typedef struct MonitorDef {
2792 const char *name;
2793 int offset;
blueswir18662d652008-10-02 18:32:44 +00002794 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002795 int type;
bellard9307c4c2004-04-04 12:57:25 +00002796} MonitorDef;
2797
bellard57206fd2004-04-25 18:54:52 +00002798#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002799static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002800{
bellard6a00d602005-11-21 23:25:50 +00002801 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002802 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002803}
2804#endif
2805
bellarda541f292004-04-12 20:39:29 +00002806#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002807static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002808{
bellard6a00d602005-11-21 23:25:50 +00002809 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002810 unsigned int u;
2811 int i;
2812
2813 u = 0;
2814 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002815 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002816
2817 return u;
2818}
2819
blueswir18662d652008-10-02 18:32:44 +00002820static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002821{
bellard6a00d602005-11-21 23:25:50 +00002822 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002823 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002824}
2825
blueswir18662d652008-10-02 18:32:44 +00002826static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002827{
bellard6a00d602005-11-21 23:25:50 +00002828 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002829 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002830}
bellard9fddaa02004-05-21 12:59:32 +00002831
blueswir18662d652008-10-02 18:32:44 +00002832static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002833{
bellard6a00d602005-11-21 23:25:50 +00002834 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002835 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002836}
2837
blueswir18662d652008-10-02 18:32:44 +00002838static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002839{
bellard6a00d602005-11-21 23:25:50 +00002840 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002841 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002842}
2843
blueswir18662d652008-10-02 18:32:44 +00002844static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002845{
bellard6a00d602005-11-21 23:25:50 +00002846 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002847 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002848}
bellarda541f292004-04-12 20:39:29 +00002849#endif
2850
bellarde95c8d52004-09-30 22:22:08 +00002851#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002852#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002853static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002854{
bellard6a00d602005-11-21 23:25:50 +00002855 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002856 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002857}
bellard7b936c02005-10-30 17:05:13 +00002858#endif
bellarde95c8d52004-09-30 22:22:08 +00002859
blueswir18662d652008-10-02 18:32:44 +00002860static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002861{
bellard6a00d602005-11-21 23:25:50 +00002862 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002863 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002864}
2865#endif
2866
blueswir18662d652008-10-02 18:32:44 +00002867static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002868#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002869
2870#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002871 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002872 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002873 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002874
bellard9307c4c2004-04-04 12:57:25 +00002875 { "eax", offsetof(CPUState, regs[0]) },
2876 { "ecx", offsetof(CPUState, regs[1]) },
2877 { "edx", offsetof(CPUState, regs[2]) },
2878 { "ebx", offsetof(CPUState, regs[3]) },
2879 { "esp|sp", offsetof(CPUState, regs[4]) },
2880 { "ebp|fp", offsetof(CPUState, regs[5]) },
2881 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002882 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002883#ifdef TARGET_X86_64
2884 { "r8", offsetof(CPUState, regs[8]) },
2885 { "r9", offsetof(CPUState, regs[9]) },
2886 { "r10", offsetof(CPUState, regs[10]) },
2887 { "r11", offsetof(CPUState, regs[11]) },
2888 { "r12", offsetof(CPUState, regs[12]) },
2889 { "r13", offsetof(CPUState, regs[13]) },
2890 { "r14", offsetof(CPUState, regs[14]) },
2891 { "r15", offsetof(CPUState, regs[15]) },
2892#endif
bellard9307c4c2004-04-04 12:57:25 +00002893 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002894 { "eip", offsetof(CPUState, eip) },
2895 SEG("cs", R_CS)
2896 SEG("ds", R_DS)
2897 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002898 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002899 SEG("fs", R_FS)
2900 SEG("gs", R_GS)
2901 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002902#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002903 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002904 { "r0", offsetof(CPUState, gpr[0]) },
2905 { "r1", offsetof(CPUState, gpr[1]) },
2906 { "r2", offsetof(CPUState, gpr[2]) },
2907 { "r3", offsetof(CPUState, gpr[3]) },
2908 { "r4", offsetof(CPUState, gpr[4]) },
2909 { "r5", offsetof(CPUState, gpr[5]) },
2910 { "r6", offsetof(CPUState, gpr[6]) },
2911 { "r7", offsetof(CPUState, gpr[7]) },
2912 { "r8", offsetof(CPUState, gpr[8]) },
2913 { "r9", offsetof(CPUState, gpr[9]) },
2914 { "r10", offsetof(CPUState, gpr[10]) },
2915 { "r11", offsetof(CPUState, gpr[11]) },
2916 { "r12", offsetof(CPUState, gpr[12]) },
2917 { "r13", offsetof(CPUState, gpr[13]) },
2918 { "r14", offsetof(CPUState, gpr[14]) },
2919 { "r15", offsetof(CPUState, gpr[15]) },
2920 { "r16", offsetof(CPUState, gpr[16]) },
2921 { "r17", offsetof(CPUState, gpr[17]) },
2922 { "r18", offsetof(CPUState, gpr[18]) },
2923 { "r19", offsetof(CPUState, gpr[19]) },
2924 { "r20", offsetof(CPUState, gpr[20]) },
2925 { "r21", offsetof(CPUState, gpr[21]) },
2926 { "r22", offsetof(CPUState, gpr[22]) },
2927 { "r23", offsetof(CPUState, gpr[23]) },
2928 { "r24", offsetof(CPUState, gpr[24]) },
2929 { "r25", offsetof(CPUState, gpr[25]) },
2930 { "r26", offsetof(CPUState, gpr[26]) },
2931 { "r27", offsetof(CPUState, gpr[27]) },
2932 { "r28", offsetof(CPUState, gpr[28]) },
2933 { "r29", offsetof(CPUState, gpr[29]) },
2934 { "r30", offsetof(CPUState, gpr[30]) },
2935 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002936 /* Floating point registers */
2937 { "f0", offsetof(CPUState, fpr[0]) },
2938 { "f1", offsetof(CPUState, fpr[1]) },
2939 { "f2", offsetof(CPUState, fpr[2]) },
2940 { "f3", offsetof(CPUState, fpr[3]) },
2941 { "f4", offsetof(CPUState, fpr[4]) },
2942 { "f5", offsetof(CPUState, fpr[5]) },
2943 { "f6", offsetof(CPUState, fpr[6]) },
2944 { "f7", offsetof(CPUState, fpr[7]) },
2945 { "f8", offsetof(CPUState, fpr[8]) },
2946 { "f9", offsetof(CPUState, fpr[9]) },
2947 { "f10", offsetof(CPUState, fpr[10]) },
2948 { "f11", offsetof(CPUState, fpr[11]) },
2949 { "f12", offsetof(CPUState, fpr[12]) },
2950 { "f13", offsetof(CPUState, fpr[13]) },
2951 { "f14", offsetof(CPUState, fpr[14]) },
2952 { "f15", offsetof(CPUState, fpr[15]) },
2953 { "f16", offsetof(CPUState, fpr[16]) },
2954 { "f17", offsetof(CPUState, fpr[17]) },
2955 { "f18", offsetof(CPUState, fpr[18]) },
2956 { "f19", offsetof(CPUState, fpr[19]) },
2957 { "f20", offsetof(CPUState, fpr[20]) },
2958 { "f21", offsetof(CPUState, fpr[21]) },
2959 { "f22", offsetof(CPUState, fpr[22]) },
2960 { "f23", offsetof(CPUState, fpr[23]) },
2961 { "f24", offsetof(CPUState, fpr[24]) },
2962 { "f25", offsetof(CPUState, fpr[25]) },
2963 { "f26", offsetof(CPUState, fpr[26]) },
2964 { "f27", offsetof(CPUState, fpr[27]) },
2965 { "f28", offsetof(CPUState, fpr[28]) },
2966 { "f29", offsetof(CPUState, fpr[29]) },
2967 { "f30", offsetof(CPUState, fpr[30]) },
2968 { "f31", offsetof(CPUState, fpr[31]) },
2969 { "fpscr", offsetof(CPUState, fpscr) },
2970 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002971 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002972 { "lr", offsetof(CPUState, lr) },
2973 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002974 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002975 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002976 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002977 { "msr", 0, &monitor_get_msr, },
2978 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002979 { "tbu", 0, &monitor_get_tbu, },
2980 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002981#if defined(TARGET_PPC64)
2982 /* Address space register */
2983 { "asr", offsetof(CPUState, asr) },
2984#endif
2985 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002986 { "sdr1", offsetof(CPUState, sdr1) },
2987 { "sr0", offsetof(CPUState, sr[0]) },
2988 { "sr1", offsetof(CPUState, sr[1]) },
2989 { "sr2", offsetof(CPUState, sr[2]) },
2990 { "sr3", offsetof(CPUState, sr[3]) },
2991 { "sr4", offsetof(CPUState, sr[4]) },
2992 { "sr5", offsetof(CPUState, sr[5]) },
2993 { "sr6", offsetof(CPUState, sr[6]) },
2994 { "sr7", offsetof(CPUState, sr[7]) },
2995 { "sr8", offsetof(CPUState, sr[8]) },
2996 { "sr9", offsetof(CPUState, sr[9]) },
2997 { "sr10", offsetof(CPUState, sr[10]) },
2998 { "sr11", offsetof(CPUState, sr[11]) },
2999 { "sr12", offsetof(CPUState, sr[12]) },
3000 { "sr13", offsetof(CPUState, sr[13]) },
3001 { "sr14", offsetof(CPUState, sr[14]) },
3002 { "sr15", offsetof(CPUState, sr[15]) },
3003 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003004#elif defined(TARGET_SPARC)
3005 { "g0", offsetof(CPUState, gregs[0]) },
3006 { "g1", offsetof(CPUState, gregs[1]) },
3007 { "g2", offsetof(CPUState, gregs[2]) },
3008 { "g3", offsetof(CPUState, gregs[3]) },
3009 { "g4", offsetof(CPUState, gregs[4]) },
3010 { "g5", offsetof(CPUState, gregs[5]) },
3011 { "g6", offsetof(CPUState, gregs[6]) },
3012 { "g7", offsetof(CPUState, gregs[7]) },
3013 { "o0", 0, monitor_get_reg },
3014 { "o1", 1, monitor_get_reg },
3015 { "o2", 2, monitor_get_reg },
3016 { "o3", 3, monitor_get_reg },
3017 { "o4", 4, monitor_get_reg },
3018 { "o5", 5, monitor_get_reg },
3019 { "o6", 6, monitor_get_reg },
3020 { "o7", 7, monitor_get_reg },
3021 { "l0", 8, monitor_get_reg },
3022 { "l1", 9, monitor_get_reg },
3023 { "l2", 10, monitor_get_reg },
3024 { "l3", 11, monitor_get_reg },
3025 { "l4", 12, monitor_get_reg },
3026 { "l5", 13, monitor_get_reg },
3027 { "l6", 14, monitor_get_reg },
3028 { "l7", 15, monitor_get_reg },
3029 { "i0", 16, monitor_get_reg },
3030 { "i1", 17, monitor_get_reg },
3031 { "i2", 18, monitor_get_reg },
3032 { "i3", 19, monitor_get_reg },
3033 { "i4", 20, monitor_get_reg },
3034 { "i5", 21, monitor_get_reg },
3035 { "i6", 22, monitor_get_reg },
3036 { "i7", 23, monitor_get_reg },
3037 { "pc", offsetof(CPUState, pc) },
3038 { "npc", offsetof(CPUState, npc) },
3039 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003040#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003041 { "psr", 0, &monitor_get_psr, },
3042 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003043#endif
bellarde95c8d52004-09-30 22:22:08 +00003044 { "tbr", offsetof(CPUState, tbr) },
3045 { "fsr", offsetof(CPUState, fsr) },
3046 { "f0", offsetof(CPUState, fpr[0]) },
3047 { "f1", offsetof(CPUState, fpr[1]) },
3048 { "f2", offsetof(CPUState, fpr[2]) },
3049 { "f3", offsetof(CPUState, fpr[3]) },
3050 { "f4", offsetof(CPUState, fpr[4]) },
3051 { "f5", offsetof(CPUState, fpr[5]) },
3052 { "f6", offsetof(CPUState, fpr[6]) },
3053 { "f7", offsetof(CPUState, fpr[7]) },
3054 { "f8", offsetof(CPUState, fpr[8]) },
3055 { "f9", offsetof(CPUState, fpr[9]) },
3056 { "f10", offsetof(CPUState, fpr[10]) },
3057 { "f11", offsetof(CPUState, fpr[11]) },
3058 { "f12", offsetof(CPUState, fpr[12]) },
3059 { "f13", offsetof(CPUState, fpr[13]) },
3060 { "f14", offsetof(CPUState, fpr[14]) },
3061 { "f15", offsetof(CPUState, fpr[15]) },
3062 { "f16", offsetof(CPUState, fpr[16]) },
3063 { "f17", offsetof(CPUState, fpr[17]) },
3064 { "f18", offsetof(CPUState, fpr[18]) },
3065 { "f19", offsetof(CPUState, fpr[19]) },
3066 { "f20", offsetof(CPUState, fpr[20]) },
3067 { "f21", offsetof(CPUState, fpr[21]) },
3068 { "f22", offsetof(CPUState, fpr[22]) },
3069 { "f23", offsetof(CPUState, fpr[23]) },
3070 { "f24", offsetof(CPUState, fpr[24]) },
3071 { "f25", offsetof(CPUState, fpr[25]) },
3072 { "f26", offsetof(CPUState, fpr[26]) },
3073 { "f27", offsetof(CPUState, fpr[27]) },
3074 { "f28", offsetof(CPUState, fpr[28]) },
3075 { "f29", offsetof(CPUState, fpr[29]) },
3076 { "f30", offsetof(CPUState, fpr[30]) },
3077 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003078#ifdef TARGET_SPARC64
3079 { "f32", offsetof(CPUState, fpr[32]) },
3080 { "f34", offsetof(CPUState, fpr[34]) },
3081 { "f36", offsetof(CPUState, fpr[36]) },
3082 { "f38", offsetof(CPUState, fpr[38]) },
3083 { "f40", offsetof(CPUState, fpr[40]) },
3084 { "f42", offsetof(CPUState, fpr[42]) },
3085 { "f44", offsetof(CPUState, fpr[44]) },
3086 { "f46", offsetof(CPUState, fpr[46]) },
3087 { "f48", offsetof(CPUState, fpr[48]) },
3088 { "f50", offsetof(CPUState, fpr[50]) },
3089 { "f52", offsetof(CPUState, fpr[52]) },
3090 { "f54", offsetof(CPUState, fpr[54]) },
3091 { "f56", offsetof(CPUState, fpr[56]) },
3092 { "f58", offsetof(CPUState, fpr[58]) },
3093 { "f60", offsetof(CPUState, fpr[60]) },
3094 { "f62", offsetof(CPUState, fpr[62]) },
3095 { "asi", offsetof(CPUState, asi) },
3096 { "pstate", offsetof(CPUState, pstate) },
3097 { "cansave", offsetof(CPUState, cansave) },
3098 { "canrestore", offsetof(CPUState, canrestore) },
3099 { "otherwin", offsetof(CPUState, otherwin) },
3100 { "wstate", offsetof(CPUState, wstate) },
3101 { "cleanwin", offsetof(CPUState, cleanwin) },
3102 { "fprs", offsetof(CPUState, fprs) },
3103#endif
bellard9307c4c2004-04-04 12:57:25 +00003104#endif
3105 { NULL },
3106};
3107
aliguori376253e2009-03-05 23:01:23 +00003108static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003109{
aliguori376253e2009-03-05 23:01:23 +00003110 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003111 longjmp(expr_env, 1);
3112}
3113
Markus Armbruster09b94182010-01-20 13:07:30 +01003114/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003115static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003116{
blueswir18662d652008-10-02 18:32:44 +00003117 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003118 void *ptr;
3119
bellard9307c4c2004-04-04 12:57:25 +00003120 for(md = monitor_defs; md->name != NULL; md++) {
3121 if (compare_cmd(name, md->name)) {
3122 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003123 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003124 } else {
bellard6a00d602005-11-21 23:25:50 +00003125 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003126 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003127 switch(md->type) {
3128 case MD_I32:
3129 *pval = *(int32_t *)ptr;
3130 break;
3131 case MD_TLONG:
3132 *pval = *(target_long *)ptr;
3133 break;
3134 default:
3135 *pval = 0;
3136 break;
3137 }
bellard9307c4c2004-04-04 12:57:25 +00003138 }
3139 return 0;
3140 }
3141 }
3142 return -1;
3143}
3144
3145static void next(void)
3146{
Blue Swirl660f11b2009-07-31 21:16:51 +00003147 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003148 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003149 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003150 pch++;
3151 }
3152}
3153
aliguori376253e2009-03-05 23:01:23 +00003154static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003155
aliguori376253e2009-03-05 23:01:23 +00003156static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003157{
blueswir1c2efc952007-09-25 17:28:42 +00003158 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003159 char *p;
bellard6a00d602005-11-21 23:25:50 +00003160 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003161
3162 switch(*pch) {
3163 case '+':
3164 next();
aliguori376253e2009-03-05 23:01:23 +00003165 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003166 break;
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_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003178 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003179 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003180 }
3181 next();
3182 break;
bellard81d09122004-07-14 17:21:37 +00003183 case '\'':
3184 pch++;
3185 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003186 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003187 n = *pch;
3188 pch++;
3189 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003190 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003191 next();
3192 break;
bellard9307c4c2004-04-04 12:57:25 +00003193 case '$':
3194 {
3195 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003196 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003197
bellard9307c4c2004-04-04 12:57:25 +00003198 pch++;
3199 q = buf;
3200 while ((*pch >= 'a' && *pch <= 'z') ||
3201 (*pch >= 'A' && *pch <= 'Z') ||
3202 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003203 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003204 if ((q - buf) < sizeof(buf) - 1)
3205 *q++ = *pch;
3206 pch++;
3207 }
blueswir1cd390082008-11-16 13:53:32 +00003208 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003209 pch++;
3210 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003211 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003212 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003213 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003214 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003215 }
3216 break;
3217 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003218 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003219 n = 0;
3220 break;
3221 default:
blueswir17743e582007-09-24 18:39:04 +00003222#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003223 n = strtoull(pch, &p, 0);
3224#else
bellard9307c4c2004-04-04 12:57:25 +00003225 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003226#endif
bellard9307c4c2004-04-04 12:57:25 +00003227 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003228 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003229 }
3230 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003231 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003232 pch++;
3233 break;
3234 }
3235 return n;
3236}
3237
3238
aliguori376253e2009-03-05 23:01:23 +00003239static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003240{
blueswir1c2efc952007-09-25 17:28:42 +00003241 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003242 int op;
ths3b46e622007-09-17 08:09:54 +00003243
aliguori376253e2009-03-05 23:01:23 +00003244 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003245 for(;;) {
3246 op = *pch;
3247 if (op != '*' && op != '/' && op != '%')
3248 break;
3249 next();
aliguori376253e2009-03-05 23:01:23 +00003250 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003251 switch(op) {
3252 default:
3253 case '*':
3254 val *= val2;
3255 break;
3256 case '/':
3257 case '%':
ths5fafdf22007-09-16 21:08:06 +00003258 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003259 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003260 if (op == '/')
3261 val /= val2;
3262 else
3263 val %= val2;
3264 break;
3265 }
3266 }
3267 return val;
3268}
3269
aliguori376253e2009-03-05 23:01:23 +00003270static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003271{
blueswir1c2efc952007-09-25 17:28:42 +00003272 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003273 int op;
bellard9307c4c2004-04-04 12:57:25 +00003274
aliguori376253e2009-03-05 23:01:23 +00003275 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003276 for(;;) {
3277 op = *pch;
3278 if (op != '&' && op != '|' && op != '^')
3279 break;
3280 next();
aliguori376253e2009-03-05 23:01:23 +00003281 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003282 switch(op) {
3283 default:
3284 case '&':
3285 val &= val2;
3286 break;
3287 case '|':
3288 val |= val2;
3289 break;
3290 case '^':
3291 val ^= val2;
3292 break;
3293 }
3294 }
3295 return val;
3296}
3297
aliguori376253e2009-03-05 23:01:23 +00003298static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003299{
blueswir1c2efc952007-09-25 17:28:42 +00003300 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003301 int op;
bellard9307c4c2004-04-04 12:57:25 +00003302
aliguori376253e2009-03-05 23:01:23 +00003303 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003304 for(;;) {
3305 op = *pch;
3306 if (op != '+' && op != '-')
3307 break;
3308 next();
aliguori376253e2009-03-05 23:01:23 +00003309 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003310 if (op == '+')
3311 val += val2;
3312 else
3313 val -= val2;
3314 }
3315 return val;
3316}
3317
aliguori376253e2009-03-05 23:01:23 +00003318static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003319{
3320 pch = *pp;
3321 if (setjmp(expr_env)) {
3322 *pp = pch;
3323 return -1;
3324 }
blueswir1cd390082008-11-16 13:53:32 +00003325 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003326 pch++;
aliguori376253e2009-03-05 23:01:23 +00003327 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003328 *pp = pch;
3329 return 0;
3330}
3331
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003332static int get_double(Monitor *mon, double *pval, const char **pp)
3333{
3334 const char *p = *pp;
3335 char *tailp;
3336 double d;
3337
3338 d = strtod(p, &tailp);
3339 if (tailp == p) {
3340 monitor_printf(mon, "Number expected\n");
3341 return -1;
3342 }
3343 if (d != d || d - d != 0) {
3344 /* NaN or infinity */
3345 monitor_printf(mon, "Bad number\n");
3346 return -1;
3347 }
3348 *pval = d;
3349 *pp = tailp;
3350 return 0;
3351}
3352
bellard9307c4c2004-04-04 12:57:25 +00003353static int get_str(char *buf, int buf_size, const char **pp)
3354{
3355 const char *p;
3356 char *q;
3357 int c;
3358
bellard81d09122004-07-14 17:21:37 +00003359 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003360 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003361 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003362 p++;
3363 if (*p == '\0') {
3364 fail:
bellard81d09122004-07-14 17:21:37 +00003365 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003366 *pp = p;
3367 return -1;
3368 }
bellard9307c4c2004-04-04 12:57:25 +00003369 if (*p == '\"') {
3370 p++;
3371 while (*p != '\0' && *p != '\"') {
3372 if (*p == '\\') {
3373 p++;
3374 c = *p++;
3375 switch(c) {
3376 case 'n':
3377 c = '\n';
3378 break;
3379 case 'r':
3380 c = '\r';
3381 break;
3382 case '\\':
3383 case '\'':
3384 case '\"':
3385 break;
3386 default:
3387 qemu_printf("unsupported escape code: '\\%c'\n", c);
3388 goto fail;
3389 }
3390 if ((q - buf) < buf_size - 1) {
3391 *q++ = c;
3392 }
3393 } else {
3394 if ((q - buf) < buf_size - 1) {
3395 *q++ = *p;
3396 }
3397 p++;
3398 }
3399 }
3400 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003401 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003402 goto fail;
3403 }
3404 p++;
3405 } else {
blueswir1cd390082008-11-16 13:53:32 +00003406 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003407 if ((q - buf) < buf_size - 1) {
3408 *q++ = *p;
3409 }
3410 p++;
3411 }
bellard9307c4c2004-04-04 12:57:25 +00003412 }
bellard81d09122004-07-14 17:21:37 +00003413 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003414 *pp = p;
3415 return 0;
3416}
3417
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003418/*
3419 * Store the command-name in cmdname, and return a pointer to
3420 * the remaining of the command string.
3421 */
3422static const char *get_command_name(const char *cmdline,
3423 char *cmdname, size_t nlen)
3424{
3425 size_t len;
3426 const char *p, *pstart;
3427
3428 p = cmdline;
3429 while (qemu_isspace(*p))
3430 p++;
3431 if (*p == '\0')
3432 return NULL;
3433 pstart = p;
3434 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3435 p++;
3436 len = p - pstart;
3437 if (len > nlen - 1)
3438 len = nlen - 1;
3439 memcpy(cmdname, pstart, len);
3440 cmdname[len] = '\0';
3441 return p;
3442}
3443
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003444/**
3445 * Read key of 'type' into 'key' and return the current
3446 * 'type' pointer.
3447 */
3448static char *key_get_info(const char *type, char **key)
3449{
3450 size_t len;
3451 char *p, *str;
3452
3453 if (*type == ',')
3454 type++;
3455
3456 p = strchr(type, ':');
3457 if (!p) {
3458 *key = NULL;
3459 return NULL;
3460 }
3461 len = p - type;
3462
3463 str = qemu_malloc(len + 1);
3464 memcpy(str, type, len);
3465 str[len] = '\0';
3466
3467 *key = str;
3468 return ++p;
3469}
3470
bellard9307c4c2004-04-04 12:57:25 +00003471static int default_fmt_format = 'x';
3472static int default_fmt_size = 4;
3473
3474#define MAX_ARGS 16
3475
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003476static int is_valid_option(const char *c, const char *typestr)
3477{
3478 char option[3];
3479
3480 option[0] = '-';
3481 option[1] = *c;
3482 option[2] = '\0';
3483
3484 typestr = strstr(typestr, option);
3485 return (typestr != NULL);
3486}
3487
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003488static const mon_cmd_t *monitor_find_command(const char *cmdname)
3489{
3490 const mon_cmd_t *cmd;
3491
3492 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3493 if (compare_cmd(cmdname, cmd->name)) {
3494 return cmd;
3495 }
3496 }
3497
3498 return NULL;
3499}
3500
Anthony Liguoric227f092009-10-01 16:12:16 -05003501static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003502 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003503 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003504{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003505 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003506 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003507 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003508 char cmdname[256];
3509 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003510 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003511
3512#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003513 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003514#endif
ths3b46e622007-09-17 08:09:54 +00003515
bellard9307c4c2004-04-04 12:57:25 +00003516 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003517 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3518 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003519 return NULL;
ths3b46e622007-09-17 08:09:54 +00003520
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003521 cmd = monitor_find_command(cmdname);
3522 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003523 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003524 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003525 }
bellard9307c4c2004-04-04 12:57:25 +00003526
bellard9307c4c2004-04-04 12:57:25 +00003527 /* parse the parameters */
3528 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003529 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003530 typestr = key_get_info(typestr, &key);
3531 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003532 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003533 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003534 typestr++;
3535 switch(c) {
3536 case 'F':
bellard81d09122004-07-14 17:21:37 +00003537 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003538 case 's':
3539 {
3540 int ret;
ths3b46e622007-09-17 08:09:54 +00003541
blueswir1cd390082008-11-16 13:53:32 +00003542 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003543 p++;
3544 if (*typestr == '?') {
3545 typestr++;
3546 if (*p == '\0') {
3547 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003548 break;
bellard9307c4c2004-04-04 12:57:25 +00003549 }
3550 }
3551 ret = get_str(buf, sizeof(buf), &p);
3552 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003553 switch(c) {
3554 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003555 monitor_printf(mon, "%s: filename expected\n",
3556 cmdname);
bellard81d09122004-07-14 17:21:37 +00003557 break;
3558 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003559 monitor_printf(mon, "%s: block device name expected\n",
3560 cmdname);
bellard81d09122004-07-14 17:21:37 +00003561 break;
3562 default:
aliguori376253e2009-03-05 23:01:23 +00003563 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003564 break;
3565 }
bellard9307c4c2004-04-04 12:57:25 +00003566 goto fail;
3567 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003568 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003569 }
3570 break;
3571 case '/':
3572 {
3573 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003574
blueswir1cd390082008-11-16 13:53:32 +00003575 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003576 p++;
3577 if (*p == '/') {
3578 /* format found */
3579 p++;
3580 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003581 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003582 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003583 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003584 count = count * 10 + (*p - '0');
3585 p++;
3586 }
3587 }
3588 size = -1;
3589 format = -1;
3590 for(;;) {
3591 switch(*p) {
3592 case 'o':
3593 case 'd':
3594 case 'u':
3595 case 'x':
3596 case 'i':
3597 case 'c':
3598 format = *p++;
3599 break;
3600 case 'b':
3601 size = 1;
3602 p++;
3603 break;
3604 case 'h':
3605 size = 2;
3606 p++;
3607 break;
3608 case 'w':
3609 size = 4;
3610 p++;
3611 break;
3612 case 'g':
3613 case 'L':
3614 size = 8;
3615 p++;
3616 break;
3617 default:
3618 goto next;
3619 }
3620 }
3621 next:
blueswir1cd390082008-11-16 13:53:32 +00003622 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003623 monitor_printf(mon, "invalid char in format: '%c'\n",
3624 *p);
bellard9307c4c2004-04-04 12:57:25 +00003625 goto fail;
3626 }
bellard9307c4c2004-04-04 12:57:25 +00003627 if (format < 0)
3628 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003629 if (format != 'i') {
3630 /* for 'i', not specifying a size gives -1 as size */
3631 if (size < 0)
3632 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003633 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003634 }
bellard9307c4c2004-04-04 12:57:25 +00003635 default_fmt_format = format;
3636 } else {
3637 count = 1;
3638 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003639 if (format != 'i') {
3640 size = default_fmt_size;
3641 } else {
3642 size = -1;
3643 }
bellard9307c4c2004-04-04 12:57:25 +00003644 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003645 qdict_put(qdict, "count", qint_from_int(count));
3646 qdict_put(qdict, "format", qint_from_int(format));
3647 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003648 }
3649 break;
3650 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003651 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003652 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003653 {
blueswir1c2efc952007-09-25 17:28:42 +00003654 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003655
blueswir1cd390082008-11-16 13:53:32 +00003656 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003657 p++;
bellard34405572004-06-08 00:55:58 +00003658 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003659 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003660 if (*p == '\0') {
3661 typestr++;
3662 break;
3663 }
bellard34405572004-06-08 00:55:58 +00003664 } else {
3665 if (*p == '.') {
3666 p++;
blueswir1cd390082008-11-16 13:53:32 +00003667 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003668 p++;
bellard34405572004-06-08 00:55:58 +00003669 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003670 typestr++;
3671 break;
bellard34405572004-06-08 00:55:58 +00003672 }
3673 }
bellard13224a82006-07-14 22:03:35 +00003674 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003675 }
aliguori376253e2009-03-05 23:01:23 +00003676 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003677 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003678 /* Check if 'i' is greater than 32-bit */
3679 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3680 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3681 monitor_printf(mon, "integer is for 32-bit values\n");
3682 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003683 } else if (c == 'M') {
3684 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003685 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003686 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003687 }
3688 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003689 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003690 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003691 {
3692 double val;
3693
3694 while (qemu_isspace(*p))
3695 p++;
3696 if (*typestr == '?') {
3697 typestr++;
3698 if (*p == '\0') {
3699 break;
3700 }
3701 }
3702 if (get_double(mon, &val, &p) < 0) {
3703 goto fail;
3704 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003705 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003706 switch (*p) {
3707 case 'K': case 'k':
3708 val *= 1 << 10; p++; break;
3709 case 'M': case 'm':
3710 val *= 1 << 20; p++; break;
3711 case 'G': case 'g':
3712 val *= 1 << 30; p++; break;
3713 }
3714 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003715 if (c == 'T' && p[0] && p[1] == 's') {
3716 switch (*p) {
3717 case 'm':
3718 val /= 1e3; p += 2; break;
3719 case 'u':
3720 val /= 1e6; p += 2; break;
3721 case 'n':
3722 val /= 1e9; p += 2; break;
3723 }
3724 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003725 if (*p && !qemu_isspace(*p)) {
3726 monitor_printf(mon, "Unknown unit suffix\n");
3727 goto fail;
3728 }
3729 qdict_put(qdict, key, qfloat_from_double(val));
3730 }
3731 break;
bellard9307c4c2004-04-04 12:57:25 +00003732 case '-':
3733 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003734 const char *tmp = p;
3735 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003736 /* option */
ths3b46e622007-09-17 08:09:54 +00003737
bellard9307c4c2004-04-04 12:57:25 +00003738 c = *typestr++;
3739 if (c == '\0')
3740 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003741 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003742 p++;
3743 has_option = 0;
3744 if (*p == '-') {
3745 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003746 if(c != *p) {
3747 if(!is_valid_option(p, typestr)) {
3748
3749 monitor_printf(mon, "%s: unsupported option -%c\n",
3750 cmdname, *p);
3751 goto fail;
3752 } else {
3753 skip_key = 1;
3754 }
bellard9307c4c2004-04-04 12:57:25 +00003755 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003756 if(skip_key) {
3757 p = tmp;
3758 } else {
3759 p++;
3760 has_option = 1;
3761 }
bellard9307c4c2004-04-04 12:57:25 +00003762 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003763 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003764 }
3765 break;
3766 default:
3767 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003768 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003769 goto fail;
3770 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003771 qemu_free(key);
3772 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003773 }
3774 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003775 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003776 p++;
3777 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003778 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3779 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003780 goto fail;
3781 }
3782
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003783 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003784
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003785fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003786 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003787 return NULL;
3788}
3789
Luiz Capitulino8204a912009-11-18 23:05:31 -02003790static void monitor_print_error(Monitor *mon)
3791{
3792 qerror_print(mon->error);
3793 QDECREF(mon->error);
3794 mon->error = NULL;
3795}
3796
Adam Litke940cc302010-01-25 12:18:44 -06003797static int is_async_return(const QObject *data)
3798{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003799 if (data && qobject_type(data) == QTYPE_QDICT) {
3800 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3801 }
3802
3803 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003804}
3805
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003806static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3807 const QDict *params)
3808{
3809 QObject *data = NULL;
3810
Luiz Capitulino97536cf2010-02-10 23:49:47 -02003811 if (cmd->cmd_new_ret) {
3812 cmd->cmd_new_ret(mon, params, &data);
3813 } else {
3814 cmd->mhandler.cmd_new(mon, params, &data);
3815 }
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003816
Adam Litke940cc302010-01-25 12:18:44 -06003817 if (is_async_return(data)) {
3818 /*
3819 * Asynchronous commands have no initial return data but they can
3820 * generate errors. Data is returned via the async completion handler.
3821 */
3822 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3823 monitor_protocol_emitter(mon, NULL);
3824 }
3825 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003826 /* Monitor Protocol */
3827 monitor_protocol_emitter(mon, data);
3828 } else {
3829 /* User Protocol */
3830 if (data)
3831 cmd->user_print(mon, data);
3832 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003833
3834 qobject_decref(data);
3835}
3836
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003837static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003838{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003839 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003840 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003841
3842 qdict = qdict_new();
3843
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003844 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003845 if (!cmd)
3846 goto out;
3847
3848 qemu_errors_to_mon(mon);
3849
Adam Litke940cc302010-01-25 12:18:44 -06003850 if (monitor_handler_is_async(cmd)) {
3851 user_async_cmd_handler(mon, cmd, qdict);
3852 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003853 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003854 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003855 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003856 }
3857
Luiz Capitulino8204a912009-11-18 23:05:31 -02003858 if (monitor_has_error(mon))
3859 monitor_print_error(mon);
3860
3861 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003862
3863out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003864 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003865}
3866
bellard81d09122004-07-14 17:21:37 +00003867static void cmd_completion(const char *name, const char *list)
3868{
3869 const char *p, *pstart;
3870 char cmd[128];
3871 int len;
3872
3873 p = list;
3874 for(;;) {
3875 pstart = p;
3876 p = strchr(p, '|');
3877 if (!p)
3878 p = pstart + strlen(pstart);
3879 len = p - pstart;
3880 if (len > sizeof(cmd) - 2)
3881 len = sizeof(cmd) - 2;
3882 memcpy(cmd, pstart, len);
3883 cmd[len] = '\0';
3884 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003885 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003886 }
3887 if (*p == '\0')
3888 break;
3889 p++;
3890 }
3891}
3892
3893static void file_completion(const char *input)
3894{
3895 DIR *ffs;
3896 struct dirent *d;
3897 char path[1024];
3898 char file[1024], file_prefix[1024];
3899 int input_path_len;
3900 const char *p;
3901
ths5fafdf22007-09-16 21:08:06 +00003902 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003903 if (!p) {
3904 input_path_len = 0;
3905 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003906 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003907 } else {
3908 input_path_len = p - input + 1;
3909 memcpy(path, input, input_path_len);
3910 if (input_path_len > sizeof(path) - 1)
3911 input_path_len = sizeof(path) - 1;
3912 path[input_path_len] = '\0';
3913 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3914 }
3915#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003916 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3917 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003918#endif
3919 ffs = opendir(path);
3920 if (!ffs)
3921 return;
3922 for(;;) {
3923 struct stat sb;
3924 d = readdir(ffs);
3925 if (!d)
3926 break;
3927 if (strstart(d->d_name, file_prefix, NULL)) {
3928 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003929 if (input_path_len < sizeof(file))
3930 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3931 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003932 /* stat the file to find out if it's a directory.
3933 * In that case add a slash to speed up typing long paths
3934 */
3935 stat(file, &sb);
3936 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003937 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003938 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003939 }
3940 }
3941 closedir(ffs);
3942}
3943
aliguori51de9762009-03-05 23:00:43 +00003944static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003945{
aliguori51de9762009-03-05 23:00:43 +00003946 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003947 const char *input = opaque;
3948
3949 if (input[0] == '\0' ||
3950 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003951 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003952 }
3953}
3954
3955/* NOTE: this parser is an approximate form of the real command parser */
3956static void parse_cmdline(const char *cmdline,
3957 int *pnb_args, char **args)
3958{
3959 const char *p;
3960 int nb_args, ret;
3961 char buf[1024];
3962
3963 p = cmdline;
3964 nb_args = 0;
3965 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003966 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003967 p++;
3968 if (*p == '\0')
3969 break;
3970 if (nb_args >= MAX_ARGS)
3971 break;
3972 ret = get_str(buf, sizeof(buf), &p);
3973 args[nb_args] = qemu_strdup(buf);
3974 nb_args++;
3975 if (ret < 0)
3976 break;
3977 }
3978 *pnb_args = nb_args;
3979}
3980
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003981static const char *next_arg_type(const char *typestr)
3982{
3983 const char *p = strchr(typestr, ':');
3984 return (p != NULL ? ++p : typestr);
3985}
3986
aliguori4c36ba32009-03-05 23:01:37 +00003987static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003988{
3989 const char *cmdname;
3990 char *args[MAX_ARGS];
3991 int nb_args, i, len;
3992 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003993 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003994 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003995
3996 parse_cmdline(cmdline, &nb_args, args);
3997#ifdef DEBUG_COMPLETION
3998 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003999 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004000 }
4001#endif
4002
4003 /* if the line ends with a space, it means we want to complete the
4004 next arg */
4005 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004006 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004007 if (nb_args >= MAX_ARGS)
4008 return;
4009 args[nb_args++] = qemu_strdup("");
4010 }
4011 if (nb_args <= 1) {
4012 /* command completion */
4013 if (nb_args == 0)
4014 cmdname = "";
4015 else
4016 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004017 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004018 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004019 cmd_completion(cmdname, cmd->name);
4020 }
4021 } else {
4022 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004023 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004024 if (compare_cmd(args[0], cmd->name))
4025 goto found;
4026 }
4027 return;
4028 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004029 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004030 for(i = 0; i < nb_args - 2; i++) {
4031 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004032 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004033 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004034 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004035 }
4036 }
4037 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004038 if (*ptype == '-' && ptype[1] != '\0') {
4039 ptype += 2;
4040 }
bellard81d09122004-07-14 17:21:37 +00004041 switch(*ptype) {
4042 case 'F':
4043 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004044 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004045 file_completion(str);
4046 break;
4047 case 'B':
4048 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004049 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004050 bdrv_iterate(block_completion_it, (void *)str);
4051 break;
bellard7fe48482004-10-09 18:08:01 +00004052 case 's':
4053 /* XXX: more generic ? */
4054 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004055 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004056 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4057 cmd_completion(str, cmd->name);
4058 }
bellard64866c32006-05-07 18:03:31 +00004059 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004060 char *sep = strrchr(str, '-');
4061 if (sep)
4062 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004063 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004064 for(key = key_defs; key->name != NULL; key++) {
4065 cmd_completion(str, key->name);
4066 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004067 } else if (!strcmp(cmd->name, "help|?")) {
4068 readline_set_completion_index(cur_mon->rs, strlen(str));
4069 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4070 cmd_completion(str, cmd->name);
4071 }
bellard7fe48482004-10-09 18:08:01 +00004072 }
4073 break;
bellard81d09122004-07-14 17:21:37 +00004074 default:
4075 break;
4076 }
4077 }
4078 for(i = 0; i < nb_args; i++)
4079 qemu_free(args[i]);
4080}
4081
aliguori731b0362009-03-05 23:01:42 +00004082static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004083{
aliguori731b0362009-03-05 23:01:42 +00004084 Monitor *mon = opaque;
4085
Jan Kiszkac62313b2009-12-04 14:05:29 +01004086 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004087}
4088
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004089typedef struct CmdArgs {
4090 QString *name;
4091 int type;
4092 int flag;
4093 int optional;
4094} CmdArgs;
4095
4096static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4097{
4098 if (!cmd_args->optional) {
4099 qemu_error_new(QERR_MISSING_PARAMETER, name);
4100 return -1;
4101 }
4102
4103 if (cmd_args->type == '-') {
4104 /* handlers expect a value, they need to be changed */
4105 qdict_put(args, name, qint_from_int(0));
4106 }
4107
4108 return 0;
4109}
4110
4111static int check_arg(const CmdArgs *cmd_args, QDict *args)
4112{
4113 QObject *value;
4114 const char *name;
4115
4116 name = qstring_get_str(cmd_args->name);
4117
4118 if (!args) {
4119 return check_opt(cmd_args, name, args);
4120 }
4121
4122 value = qdict_get(args, name);
4123 if (!value) {
4124 return check_opt(cmd_args, name, args);
4125 }
4126
4127 switch (cmd_args->type) {
4128 case 'F':
4129 case 'B':
4130 case 's':
4131 if (qobject_type(value) != QTYPE_QSTRING) {
4132 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4133 return -1;
4134 }
4135 break;
4136 case '/': {
4137 int i;
4138 const char *keys[] = { "count", "format", "size", NULL };
4139
4140 for (i = 0; keys[i]; i++) {
4141 QObject *obj = qdict_get(args, keys[i]);
4142 if (!obj) {
4143 qemu_error_new(QERR_MISSING_PARAMETER, name);
4144 return -1;
4145 }
4146 if (qobject_type(obj) != QTYPE_QINT) {
4147 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4148 return -1;
4149 }
4150 }
4151 break;
4152 }
4153 case 'i':
4154 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004155 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004156 if (qobject_type(value) != QTYPE_QINT) {
4157 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4158 return -1;
4159 }
4160 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004161 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004162 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004163 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4164 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4165 return -1;
4166 }
4167 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004168 case '-':
4169 if (qobject_type(value) != QTYPE_QINT &&
4170 qobject_type(value) != QTYPE_QBOOL) {
4171 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4172 return -1;
4173 }
4174 if (qobject_type(value) == QTYPE_QBOOL) {
4175 /* handlers expect a QInt, they need to be changed */
4176 qdict_put(args, name,
4177 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4178 }
4179 break;
4180 default:
4181 /* impossible */
4182 abort();
4183 }
4184
4185 return 0;
4186}
4187
4188static void cmd_args_init(CmdArgs *cmd_args)
4189{
4190 cmd_args->name = qstring_new();
4191 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4192}
4193
4194/*
4195 * This is not trivial, we have to parse Monitor command's argument
4196 * type syntax to be able to check the arguments provided by clients.
4197 *
4198 * In the near future we will be using an array for that and will be
4199 * able to drop all this parsing...
4200 */
4201static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4202{
4203 int err;
4204 const char *p;
4205 CmdArgs cmd_args;
4206
Blue Swirldd5121b2009-12-04 20:52:02 +00004207 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004208 return (qdict_size(args) == 0 ? 0 : -1);
4209 }
4210
4211 err = 0;
4212 cmd_args_init(&cmd_args);
4213
4214 for (p = cmd->args_type;; p++) {
4215 if (*p == ':') {
4216 cmd_args.type = *++p;
4217 p++;
4218 if (cmd_args.type == '-') {
4219 cmd_args.flag = *p++;
4220 cmd_args.optional = 1;
4221 } else if (*p == '?') {
4222 cmd_args.optional = 1;
4223 p++;
4224 }
4225
4226 assert(*p == ',' || *p == '\0');
4227 err = check_arg(&cmd_args, args);
4228
4229 QDECREF(cmd_args.name);
4230 cmd_args_init(&cmd_args);
4231
4232 if (err < 0) {
4233 break;
4234 }
4235 } else {
4236 qstring_append_chr(cmd_args.name, *p);
4237 }
4238
4239 if (*p == '\0') {
4240 break;
4241 }
4242 }
4243
4244 QDECREF(cmd_args.name);
4245 return err;
4246}
4247
Luiz Capitulino09069b12010-02-04 18:10:06 -02004248static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4249{
4250 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4251 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4252}
4253
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004254static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4255{
4256 int err;
4257 QObject *obj;
4258 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004259 const mon_cmd_t *cmd;
4260 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004261 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004262
4263 args = NULL;
4264 qemu_errors_to_mon(mon);
4265
4266 obj = json_parser_parse(tokens, NULL);
4267 if (!obj) {
4268 // FIXME: should be triggered in json_parser_parse()
4269 qemu_error_new(QERR_JSON_PARSING);
4270 goto err_out;
4271 } else if (qobject_type(obj) != QTYPE_QDICT) {
4272 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4273 qobject_decref(obj);
4274 goto err_out;
4275 }
4276
4277 input = qobject_to_qdict(obj);
4278
4279 mon->mc->id = qdict_get(input, "id");
4280 qobject_incref(mon->mc->id);
4281
4282 obj = qdict_get(input, "execute");
4283 if (!obj) {
4284 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4285 goto err_input;
4286 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4287 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4288 goto err_input;
4289 }
4290
4291 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004292
Luiz Capitulino09069b12010-02-04 18:10:06 -02004293 if (invalid_qmp_mode(mon, cmd_name)) {
4294 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4295 goto err_input;
4296 }
4297
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004298 /*
4299 * XXX: We need this special case until we get info handlers
4300 * converted into 'query-' commands
4301 */
4302 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004303 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4304 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004305 } else if (strstart(cmd_name, "query-", &info_item)) {
4306 cmd = monitor_find_command("info");
4307 qdict_put_obj(input, "arguments",
4308 qobject_from_jsonf("{ 'item': %s }", info_item));
4309 } else {
4310 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004311 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004312 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4313 goto err_input;
4314 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004315 }
4316
4317 obj = qdict_get(input, "arguments");
4318 if (!obj) {
4319 args = qdict_new();
4320 } else {
4321 args = qobject_to_qdict(obj);
4322 QINCREF(args);
4323 }
4324
4325 QDECREF(input);
4326
4327 err = monitor_check_qmp_args(cmd, args);
4328 if (err < 0) {
4329 goto err_out;
4330 }
4331
Adam Litke940cc302010-01-25 12:18:44 -06004332 if (monitor_handler_is_async(cmd)) {
4333 qmp_async_cmd_handler(mon, cmd, args);
4334 } else {
4335 monitor_call_handler(mon, cmd, args);
4336 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004337 goto out;
4338
4339err_input:
4340 QDECREF(input);
4341err_out:
4342 monitor_protocol_emitter(mon, NULL);
4343out:
4344 QDECREF(args);
4345 qemu_errors_to_previous();
4346}
4347
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004348/**
4349 * monitor_control_read(): Read and handle QMP input
4350 */
4351static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4352{
4353 Monitor *old_mon = cur_mon;
4354
4355 cur_mon = opaque;
4356
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004357 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004358
4359 cur_mon = old_mon;
4360}
4361
aliguori731b0362009-03-05 23:01:42 +00004362static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004363{
aliguori731b0362009-03-05 23:01:42 +00004364 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004365 int i;
aliguori376253e2009-03-05 23:01:23 +00004366
aliguori731b0362009-03-05 23:01:42 +00004367 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004368
aliguoricde76ee2009-03-05 23:01:51 +00004369 if (cur_mon->rs) {
4370 for (i = 0; i < size; i++)
4371 readline_handle_byte(cur_mon->rs, buf[i]);
4372 } else {
4373 if (size == 0 || buf[size - 1] != 0)
4374 monitor_printf(cur_mon, "corrupted command\n");
4375 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004376 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004377 }
aliguori731b0362009-03-05 23:01:42 +00004378
4379 cur_mon = old_mon;
4380}
aliguorid8f44602008-10-06 13:52:44 +00004381
aliguori376253e2009-03-05 23:01:23 +00004382static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004383{
aliguori731b0362009-03-05 23:01:42 +00004384 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004385 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004386 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004387}
4388
aliguoricde76ee2009-03-05 23:01:51 +00004389int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004390{
aliguoricde76ee2009-03-05 23:01:51 +00004391 if (!mon->rs)
4392 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004393 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004394 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004395}
4396
aliguori376253e2009-03-05 23:01:23 +00004397void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004398{
aliguoricde76ee2009-03-05 23:01:51 +00004399 if (!mon->rs)
4400 return;
aliguori731b0362009-03-05 23:01:42 +00004401 if (--mon->suspend_cnt == 0)
4402 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004403}
4404
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004405static QObject *get_qmp_greeting(void)
4406{
4407 QObject *ver;
4408
4409 do_info_version(NULL, &ver);
4410 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4411}
4412
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004413/**
4414 * monitor_control_event(): Print QMP gretting
4415 */
4416static void monitor_control_event(void *opaque, int event)
4417{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004418 QObject *data;
4419 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004420
Luiz Capitulino47116d12010-02-08 17:01:30 -02004421 switch (event) {
4422 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004423 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004424 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004425 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004426 monitor_json_emitter(mon, data);
4427 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004428 break;
4429 case CHR_EVENT_CLOSED:
4430 json_message_parser_destroy(&mon->mc->parser);
4431 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004432 }
4433}
4434
aliguori731b0362009-03-05 23:01:42 +00004435static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004436{
aliguori376253e2009-03-05 23:01:23 +00004437 Monitor *mon = opaque;
4438
aliguori2724b182009-03-05 23:01:47 +00004439 switch (event) {
4440 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004441 mon->mux_out = 0;
4442 if (mon->reset_seen) {
4443 readline_restart(mon->rs);
4444 monitor_resume(mon);
4445 monitor_flush(mon);
4446 } else {
4447 mon->suspend_cnt = 0;
4448 }
aliguori2724b182009-03-05 23:01:47 +00004449 break;
ths86e94de2007-01-05 22:01:59 +00004450
aliguori2724b182009-03-05 23:01:47 +00004451 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004452 if (mon->reset_seen) {
4453 if (mon->suspend_cnt == 0) {
4454 monitor_printf(mon, "\n");
4455 }
4456 monitor_flush(mon);
4457 monitor_suspend(mon);
4458 } else {
4459 mon->suspend_cnt++;
4460 }
4461 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004462 break;
4463
Amit Shahb6b8df52009-10-07 18:31:16 +05304464 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004465 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4466 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004467 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004468 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004469 }
4470 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004471 break;
4472 }
ths86e94de2007-01-05 22:01:59 +00004473}
4474
aliguori76655d62009-03-06 20:27:37 +00004475
4476/*
4477 * Local variables:
4478 * c-indent-level: 4
4479 * c-basic-offset: 4
4480 * tab-width: 8
4481 * End:
4482 */
4483
aliguori731b0362009-03-05 23:01:42 +00004484void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004485{
aliguori731b0362009-03-05 23:01:42 +00004486 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004487 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004488
4489 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004490 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004491 is_first_init = 0;
4492 }
aliguori87127162009-03-05 23:01:29 +00004493
4494 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004495
aliguori87127162009-03-05 23:01:29 +00004496 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004497 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004498 if (flags & MONITOR_USE_READLINE) {
4499 mon->rs = readline_init(mon, monitor_find_completion);
4500 monitor_read_command(mon, 0);
4501 }
aliguori87127162009-03-05 23:01:29 +00004502
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004503 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004504 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004505 /* Control mode requires special handlers */
4506 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4507 monitor_control_event, mon);
4508 } else {
4509 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4510 monitor_event, mon);
4511 }
aliguori87127162009-03-05 23:01:29 +00004512
Blue Swirl72cf2d42009-09-12 07:36:22 +00004513 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004514 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004515 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004516}
4517
aliguori376253e2009-03-05 23:01:23 +00004518static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004519{
aliguoribb5fc202009-03-05 23:01:15 +00004520 BlockDriverState *bs = opaque;
4521 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004522
aliguoribb5fc202009-03-05 23:01:15 +00004523 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004524 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004525 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004526 }
aliguori731b0362009-03-05 23:01:42 +00004527 if (mon->password_completion_cb)
4528 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004529
aliguori731b0362009-03-05 23:01:42 +00004530 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004531}
aliguoric0f4ce72009-03-05 23:01:01 +00004532
aliguori376253e2009-03-05 23:01:23 +00004533void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004534 BlockDriverCompletionFunc *completion_cb,
4535 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004536{
aliguoricde76ee2009-03-05 23:01:51 +00004537 int err;
4538
aliguoribb5fc202009-03-05 23:01:15 +00004539 if (!bdrv_key_required(bs)) {
4540 if (completion_cb)
4541 completion_cb(opaque, 0);
4542 return;
4543 }
aliguoric0f4ce72009-03-05 23:01:01 +00004544
Luiz Capitulino94171e12009-12-07 21:37:00 +01004545 if (monitor_ctrl_mode(mon)) {
4546 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4547 return;
4548 }
4549
aliguori376253e2009-03-05 23:01:23 +00004550 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4551 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004552
aliguori731b0362009-03-05 23:01:42 +00004553 mon->password_completion_cb = completion_cb;
4554 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004555
aliguoricde76ee2009-03-05 23:01:51 +00004556 err = monitor_read_password(mon, bdrv_password_cb, bs);
4557
4558 if (err && completion_cb)
4559 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004560}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004561
4562typedef struct QemuErrorSink QemuErrorSink;
4563struct QemuErrorSink {
4564 enum {
4565 ERR_SINK_FILE,
4566 ERR_SINK_MONITOR,
4567 } dest;
4568 union {
4569 FILE *fp;
4570 Monitor *mon;
4571 };
4572 QemuErrorSink *previous;
4573};
4574
Blue Swirl528e93a2009-08-31 15:14:40 +00004575static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004576
4577void qemu_errors_to_file(FILE *fp)
4578{
4579 QemuErrorSink *sink;
4580
4581 sink = qemu_mallocz(sizeof(*sink));
4582 sink->dest = ERR_SINK_FILE;
4583 sink->fp = fp;
4584 sink->previous = qemu_error_sink;
4585 qemu_error_sink = sink;
4586}
4587
4588void qemu_errors_to_mon(Monitor *mon)
4589{
4590 QemuErrorSink *sink;
4591
4592 sink = qemu_mallocz(sizeof(*sink));
4593 sink->dest = ERR_SINK_MONITOR;
4594 sink->mon = mon;
4595 sink->previous = qemu_error_sink;
4596 qemu_error_sink = sink;
4597}
4598
4599void qemu_errors_to_previous(void)
4600{
4601 QemuErrorSink *sink;
4602
4603 assert(qemu_error_sink != NULL);
4604 sink = qemu_error_sink;
4605 qemu_error_sink = sink->previous;
4606 qemu_free(sink);
4607}
4608
4609void qemu_error(const char *fmt, ...)
4610{
4611 va_list args;
4612
4613 assert(qemu_error_sink != NULL);
4614 switch (qemu_error_sink->dest) {
4615 case ERR_SINK_FILE:
4616 va_start(args, fmt);
4617 vfprintf(qemu_error_sink->fp, fmt, args);
4618 va_end(args);
4619 break;
4620 case ERR_SINK_MONITOR:
4621 va_start(args, fmt);
4622 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4623 va_end(args);
4624 break;
4625 }
4626}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004627
4628void qemu_error_internal(const char *file, int linenr, const char *func,
4629 const char *fmt, ...)
4630{
4631 va_list va;
4632 QError *qerror;
4633
4634 assert(qemu_error_sink != NULL);
4635
4636 va_start(va, fmt);
4637 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4638 va_end(va);
4639
4640 switch (qemu_error_sink->dest) {
4641 case ERR_SINK_FILE:
4642 qerror_print(qerror);
4643 QDECREF(qerror);
4644 break;
4645 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004646 /* report only the first error */
4647 if (!qemu_error_sink->mon->error) {
4648 qemu_error_sink->mon->error = qerror;
4649 } else {
4650 /* XXX: warn the programmer */
4651 QDECREF(qerror);
4652 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004653 break;
4654 }
4655}