blob: 3470b0b1d473c9caae60a91df4b23e06502da6a8 [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
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200924static int 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");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200927 if (mon_set_cpu(index) < 0) {
Markus Armbrustercc0c4182010-01-29 10:34:00 +0100928 qemu_error_new(QERR_INVALID_PARAMETER, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200929 return -1;
930 }
931 return 0;
bellard6a00d602005-11-21 23:25:50 +0000932}
933
aliguori376253e2009-03-05 23:01:23 +0000934static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000935{
aliguori376253e2009-03-05 23:01:23 +0000936 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000937}
938
aliguori376253e2009-03-05 23:01:23 +0000939static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000940{
941 int i;
bellard7e2515e2004-08-01 21:52:19 +0000942 const char *str;
ths3b46e622007-09-17 08:09:54 +0000943
aliguoricde76ee2009-03-05 23:01:51 +0000944 if (!mon->rs)
945 return;
bellard7e2515e2004-08-01 21:52:19 +0000946 i = 0;
947 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000948 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000949 if (!str)
950 break;
aliguori376253e2009-03-05 23:01:23 +0000951 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000952 i++;
bellardaa455482004-04-04 13:07:25 +0000953 }
954}
955
j_mayer76a66252007-03-07 08:32:30 +0000956#if defined(TARGET_PPC)
957/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000958static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000959{
960 CPUState *env;
961
962 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000963 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000964}
965#endif
966
Luiz Capitulinob223f352009-10-07 13:41:56 -0300967/**
968 * do_quit(): Quit QEMU execution
969 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200970static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000971{
972 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200973 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000974}
975
aliguori376253e2009-03-05 23:01:23 +0000976static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000977{
978 if (bdrv_is_inserted(bs)) {
979 if (!force) {
980 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100981 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
982 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000983 return -1;
984 }
985 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100986 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000987 return -1;
988 }
989 }
990 bdrv_close(bs);
991 }
992 return 0;
993}
994
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -0200995static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000996{
997 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300998 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200999 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +00001000
bellard9307c4c2004-04-04 12:57:25 +00001001 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +00001002 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001003 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001004 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001005 }
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001006 return eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001007}
1008
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001009static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001010 QObject **ret_data)
1011{
1012 BlockDriverState *bs;
1013
1014 bs = bdrv_find(qdict_get_str(qdict, "device"));
1015 if (!bs) {
1016 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001017 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001018 }
1019
1020 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
1021 qemu_error_new(QERR_INVALID_PASSWORD);
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001022 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001023 }
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001024
1025 return 0;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001026}
1027
aliguori376253e2009-03-05 23:01:23 +00001028static void do_change_block(Monitor *mon, const char *device,
1029 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001030{
1031 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001032 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001033
bellard9307c4c2004-04-04 12:57:25 +00001034 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001035 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001036 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001037 return;
1038 }
aurel322ecea9b2008-06-18 22:10:01 +00001039 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001040 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001041 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001042 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001043 return;
1044 }
1045 }
aliguori376253e2009-03-05 23:01:23 +00001046 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001047 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001048 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001049 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001050}
1051
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001052static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001053{
1054 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001055 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001056
Markus Armbruster2895e072009-12-07 21:37:01 +01001057}
1058
1059static void change_vnc_password_cb(Monitor *mon, const char *password,
1060 void *opaque)
1061{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001062 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001063 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001064}
1065
aliguori376253e2009-03-05 23:01:23 +00001066static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001067{
ths70848512007-08-25 01:37:05 +00001068 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001069 strcmp(target, "password") == 0) {
1070 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001071 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001072 strncpy(password, arg, sizeof(password));
1073 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001074 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001075 } else {
aliguori376253e2009-03-05 23:01:23 +00001076 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001077 }
ths70848512007-08-25 01:37:05 +00001078 } else {
aliguori28a76be2009-03-06 20:27:40 +00001079 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001080 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001081 }
thse25a5822007-08-25 01:36:20 +00001082}
1083
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001084/**
1085 * do_change(): Change a removable medium, or VNC configuration
1086 */
1087static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001088{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001089 const char *device = qdict_get_str(qdict, "device");
1090 const char *target = qdict_get_str(qdict, "target");
1091 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001092 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001093 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001094 } else {
aliguori28a76be2009-03-06 20:27:40 +00001095 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001096 }
1097}
1098
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001099static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001100{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001101 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001102}
1103
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001104static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001105{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001106 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001107}
1108
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001109static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001110{
1111 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001112 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001113
bellard9307c4c2004-04-04 12:57:25 +00001114 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001115 mask = 0;
1116 } else {
bellard9307c4c2004-04-04 12:57:25 +00001117 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001118 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001119 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001120 return;
1121 }
1122 }
1123 cpu_set_log(mask);
1124}
1125
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001126static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001127{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001128 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001129 if (!option || !strcmp(option, "on")) {
1130 singlestep = 1;
1131 } else if (!strcmp(option, "off")) {
1132 singlestep = 0;
1133 } else {
1134 monitor_printf(mon, "unexpected option %s\n", option);
1135 }
1136}
1137
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001138/**
1139 * do_stop(): Stop VM execution
1140 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001141static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001142{
1143 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001144 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001145}
1146
aliguoribb5fc202009-03-05 23:01:15 +00001147static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001148
aliguori376253e2009-03-05 23:01:23 +00001149struct bdrv_iterate_context {
1150 Monitor *mon;
1151 int err;
1152};
aliguoric0f4ce72009-03-05 23:01:01 +00001153
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001154/**
1155 * do_cont(): Resume emulation.
1156 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001157static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001158{
1159 struct bdrv_iterate_context context = { mon, 0 };
1160
1161 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001162 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001163 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001164 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001165 return 0;
1166 } else {
1167 return -1;
1168 }
bellard8a7ddc32004-03-31 19:00:16 +00001169}
1170
aliguoribb5fc202009-03-05 23:01:15 +00001171static void bdrv_key_cb(void *opaque, int err)
1172{
aliguori376253e2009-03-05 23:01:23 +00001173 Monitor *mon = opaque;
1174
aliguoribb5fc202009-03-05 23:01:15 +00001175 /* another key was set successfully, retry to continue */
1176 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001177 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001178}
1179
1180static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1181{
aliguori376253e2009-03-05 23:01:23 +00001182 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001183
aliguori376253e2009-03-05 23:01:23 +00001184 if (!context->err && bdrv_key_required(bs)) {
1185 context->err = -EBUSY;
1186 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1187 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001188 }
1189}
1190
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001191static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001192{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001194 if (!device)
1195 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1196 if (gdbserver_start(device) < 0) {
1197 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1198 device);
1199 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001200 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001201 } else {
aliguori59030a82009-04-05 18:43:41 +00001202 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1203 device);
bellard8a7ddc32004-03-31 19:00:16 +00001204 }
1205}
1206
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001207static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001208{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001209 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001210 if (select_watchdog_action(action) == -1) {
1211 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1212 }
1213}
1214
aliguori376253e2009-03-05 23:01:23 +00001215static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001216{
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001218 switch(c) {
1219 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001227 break;
1228 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001230 break;
1231 default:
1232 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001234 } else {
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001236 }
1237 break;
1238 }
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001240}
1241
aliguori376253e2009-03-05 23:01:23 +00001242static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001243 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001244{
bellard6a00d602005-11-21 23:25:50 +00001245 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001246 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001247 uint8_t buf[16];
1248 uint64_t v;
1249
1250 if (format == 'i') {
1251 int flags;
1252 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001253 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001254 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001255 return;
bellard9307c4c2004-04-04 12:57:25 +00001256#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001257 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001258 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001259 } else if (wsize == 4) {
1260 flags = 0;
1261 } else {
bellard6a15fd12006-04-12 21:07:07 +00001262 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001263 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001264 if (env) {
1265#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001266 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001267 (env->segs[R_CS].flags & DESC_L_MASK))
1268 flags = 2;
1269 else
1270#endif
1271 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1272 flags = 1;
1273 }
bellard4c27ba22004-04-25 18:05:08 +00001274 }
1275#endif
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001277 return;
1278 }
1279
1280 len = wsize * count;
1281 if (wsize == 1)
1282 line_size = 8;
1283 else
1284 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001285 max_digits = 0;
1286
1287 switch(format) {
1288 case 'o':
1289 max_digits = (wsize * 8 + 2) / 3;
1290 break;
1291 default:
1292 case 'x':
1293 max_digits = (wsize * 8) / 4;
1294 break;
1295 case 'u':
1296 case 'd':
1297 max_digits = (wsize * 8 * 10 + 32) / 33;
1298 break;
1299 case 'c':
1300 wsize = 1;
1301 break;
1302 }
1303
1304 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001305 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001307 else
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001309 l = len;
1310 if (l > line_size)
1311 l = line_size;
1312 if (is_physical) {
1313 cpu_physical_memory_rw(addr, buf, l, 0);
1314 } else {
bellard6a00d602005-11-21 23:25:50 +00001315 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001316 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001318 break;
1319 }
bellard9307c4c2004-04-04 12:57:25 +00001320 }
ths5fafdf22007-09-16 21:08:06 +00001321 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001322 while (i < l) {
1323 switch(wsize) {
1324 default:
1325 case 1:
1326 v = ldub_raw(buf + i);
1327 break;
1328 case 2:
1329 v = lduw_raw(buf + i);
1330 break;
1331 case 4:
bellard92a31b12005-02-10 22:00:52 +00001332 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001333 break;
1334 case 8:
1335 v = ldq_raw(buf + i);
1336 break;
1337 }
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001339 switch(format) {
1340 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001341 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001342 break;
1343 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001347 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001350 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001351 break;
1352 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001354 break;
1355 }
1356 i += wsize;
1357 }
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001359 addr += l;
1360 len -= l;
1361 }
1362}
1363
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001364static void do_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");
1369 target_long addr = qdict_get_int(qdict, "addr");
1370
aliguori376253e2009-03-05 23:01:23 +00001371 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001372}
1373
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001374static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001375{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001376 int count = qdict_get_int(qdict, "count");
1377 int format = qdict_get_int(qdict, "format");
1378 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001379 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001380
aliguori376253e2009-03-05 23:01:23 +00001381 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001382}
1383
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001384static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001385{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001386 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001387 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001388
blueswir17743e582007-09-24 18:39:04 +00001389#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001390 switch(format) {
1391 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001396 break;
1397 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001399 break;
1400 default:
1401 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001403 break;
1404 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001405 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001406 break;
1407 }
bellard92a31b12005-02-10 22:00:52 +00001408#else
1409 switch(format) {
1410 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001412 break;
1413 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001414 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001415 break;
1416 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001417 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001418 break;
1419 default:
1420 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001421 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001422 break;
1423 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001424 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001425 break;
1426 }
1427#endif
aliguori376253e2009-03-05 23:01:23 +00001428 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001429}
1430
Luiz Capitulino98696222010-02-10 23:49:58 -02001431static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001432{
1433 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001434 uint32_t size = qdict_get_int(qdict, "size");
1435 const char *filename = qdict_get_str(qdict, "filename");
1436 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001437 uint32_t l;
1438 CPUState *env;
1439 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001440 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001441
1442 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001443
1444 f = fopen(filename, "wb");
1445 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001446 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001447 return -1;
bellardb371dc52007-01-03 15:20:39 +00001448 }
1449 while (size != 0) {
1450 l = sizeof(buf);
1451 if (l > size)
1452 l = size;
1453 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001454 if (fwrite(buf, 1, l, f) != l) {
1455 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1456 goto exit;
1457 }
bellardb371dc52007-01-03 15:20:39 +00001458 addr += l;
1459 size -= l;
1460 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001461
1462 ret = 0;
1463
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001464exit:
bellardb371dc52007-01-03 15:20:39 +00001465 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001466 return ret;
bellardb371dc52007-01-03 15:20:39 +00001467}
1468
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001469static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001470 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001471{
1472 FILE *f;
1473 uint32_t l;
1474 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001475 uint32_t size = qdict_get_int(qdict, "size");
1476 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001477 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001478 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001479
1480 f = fopen(filename, "wb");
1481 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001482 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001483 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001484 }
1485 while (size != 0) {
1486 l = sizeof(buf);
1487 if (l > size)
1488 l = size;
1489 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001490 if (fwrite(buf, 1, l, f) != l) {
1491 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1492 goto exit;
1493 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001494 fflush(f);
1495 addr += l;
1496 size -= l;
1497 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001498
1499 ret = 0;
1500
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001501exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001502 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001503 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001504}
1505
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001506static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001507{
1508 uint32_t addr;
1509 uint8_t buf[1];
1510 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001511 uint32_t start = qdict_get_int(qdict, "start");
1512 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001513
1514 sum = 0;
1515 for(addr = start; addr < (start + size); addr++) {
1516 cpu_physical_memory_rw(addr, buf, 1, 0);
1517 /* BSD sum algorithm ('sum' Unix command) */
1518 sum = (sum >> 1) | (sum << 15);
1519 sum += buf[0];
1520 }
aliguori376253e2009-03-05 23:01:23 +00001521 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001522}
1523
bellarda3a91a32004-06-04 11:06:21 +00001524typedef struct {
1525 int keycode;
1526 const char *name;
1527} KeyDef;
1528
1529static const KeyDef key_defs[] = {
1530 { 0x2a, "shift" },
1531 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001532
bellarda3a91a32004-06-04 11:06:21 +00001533 { 0x38, "alt" },
1534 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001535 { 0x64, "altgr" },
1536 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001537 { 0x1d, "ctrl" },
1538 { 0x9d, "ctrl_r" },
1539
1540 { 0xdd, "menu" },
1541
1542 { 0x01, "esc" },
1543
1544 { 0x02, "1" },
1545 { 0x03, "2" },
1546 { 0x04, "3" },
1547 { 0x05, "4" },
1548 { 0x06, "5" },
1549 { 0x07, "6" },
1550 { 0x08, "7" },
1551 { 0x09, "8" },
1552 { 0x0a, "9" },
1553 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001554 { 0x0c, "minus" },
1555 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001556 { 0x0e, "backspace" },
1557
1558 { 0x0f, "tab" },
1559 { 0x10, "q" },
1560 { 0x11, "w" },
1561 { 0x12, "e" },
1562 { 0x13, "r" },
1563 { 0x14, "t" },
1564 { 0x15, "y" },
1565 { 0x16, "u" },
1566 { 0x17, "i" },
1567 { 0x18, "o" },
1568 { 0x19, "p" },
1569
1570 { 0x1c, "ret" },
1571
1572 { 0x1e, "a" },
1573 { 0x1f, "s" },
1574 { 0x20, "d" },
1575 { 0x21, "f" },
1576 { 0x22, "g" },
1577 { 0x23, "h" },
1578 { 0x24, "j" },
1579 { 0x25, "k" },
1580 { 0x26, "l" },
1581
1582 { 0x2c, "z" },
1583 { 0x2d, "x" },
1584 { 0x2e, "c" },
1585 { 0x2f, "v" },
1586 { 0x30, "b" },
1587 { 0x31, "n" },
1588 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001589 { 0x33, "comma" },
1590 { 0x34, "dot" },
1591 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001592
balrog4d3b6f62008-02-10 16:33:14 +00001593 { 0x37, "asterisk" },
1594
bellarda3a91a32004-06-04 11:06:21 +00001595 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001596 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001597 { 0x3b, "f1" },
1598 { 0x3c, "f2" },
1599 { 0x3d, "f3" },
1600 { 0x3e, "f4" },
1601 { 0x3f, "f5" },
1602 { 0x40, "f6" },
1603 { 0x41, "f7" },
1604 { 0x42, "f8" },
1605 { 0x43, "f9" },
1606 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001607 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001608 { 0x46, "scroll_lock" },
1609
bellard64866c32006-05-07 18:03:31 +00001610 { 0xb5, "kp_divide" },
1611 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001612 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001613 { 0x4e, "kp_add" },
1614 { 0x9c, "kp_enter" },
1615 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001616 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001617
1618 { 0x52, "kp_0" },
1619 { 0x4f, "kp_1" },
1620 { 0x50, "kp_2" },
1621 { 0x51, "kp_3" },
1622 { 0x4b, "kp_4" },
1623 { 0x4c, "kp_5" },
1624 { 0x4d, "kp_6" },
1625 { 0x47, "kp_7" },
1626 { 0x48, "kp_8" },
1627 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001628
bellarda3a91a32004-06-04 11:06:21 +00001629 { 0x56, "<" },
1630
1631 { 0x57, "f11" },
1632 { 0x58, "f12" },
1633
1634 { 0xb7, "print" },
1635
1636 { 0xc7, "home" },
1637 { 0xc9, "pgup" },
1638 { 0xd1, "pgdn" },
1639 { 0xcf, "end" },
1640
1641 { 0xcb, "left" },
1642 { 0xc8, "up" },
1643 { 0xd0, "down" },
1644 { 0xcd, "right" },
1645
1646 { 0xd2, "insert" },
1647 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001648#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1649 { 0xf0, "stop" },
1650 { 0xf1, "again" },
1651 { 0xf2, "props" },
1652 { 0xf3, "undo" },
1653 { 0xf4, "front" },
1654 { 0xf5, "copy" },
1655 { 0xf6, "open" },
1656 { 0xf7, "paste" },
1657 { 0xf8, "find" },
1658 { 0xf9, "cut" },
1659 { 0xfa, "lf" },
1660 { 0xfb, "help" },
1661 { 0xfc, "meta_l" },
1662 { 0xfd, "meta_r" },
1663 { 0xfe, "compose" },
1664#endif
bellarda3a91a32004-06-04 11:06:21 +00001665 { 0, NULL },
1666};
1667
1668static int get_keycode(const char *key)
1669{
1670 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001671 char *endp;
1672 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001673
1674 for(p = key_defs; p->name != NULL; p++) {
1675 if (!strcmp(key, p->name))
1676 return p->keycode;
1677 }
bellard64866c32006-05-07 18:03:31 +00001678 if (strstart(key, "0x", NULL)) {
1679 ret = strtoul(key, &endp, 0);
1680 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1681 return ret;
1682 }
bellarda3a91a32004-06-04 11:06:21 +00001683 return -1;
1684}
1685
balrogc8256f92008-06-08 22:45:01 +00001686#define MAX_KEYCODES 16
1687static uint8_t keycodes[MAX_KEYCODES];
1688static int nb_pending_keycodes;
1689static QEMUTimer *key_timer;
1690
1691static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001692{
balrogc8256f92008-06-08 22:45:01 +00001693 int keycode;
1694
1695 while (nb_pending_keycodes > 0) {
1696 nb_pending_keycodes--;
1697 keycode = keycodes[nb_pending_keycodes];
1698 if (keycode & 0x80)
1699 kbd_put_keycode(0xe0);
1700 kbd_put_keycode(keycode | 0x80);
1701 }
1702}
1703
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001704static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001705{
balrog3401c0d2008-06-04 10:05:59 +00001706 char keyname_buf[16];
1707 char *separator;
1708 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001709 const char *string = qdict_get_str(qdict, "string");
1710 int has_hold_time = qdict_haskey(qdict, "hold_time");
1711 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001712
balrogc8256f92008-06-08 22:45:01 +00001713 if (nb_pending_keycodes > 0) {
1714 qemu_del_timer(key_timer);
1715 release_keys(NULL);
1716 }
1717 if (!has_hold_time)
1718 hold_time = 100;
1719 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001720 while (1) {
1721 separator = strchr(string, '-');
1722 keyname_len = separator ? separator - string : strlen(string);
1723 if (keyname_len > 0) {
1724 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1725 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001726 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001727 return;
bellarda3a91a32004-06-04 11:06:21 +00001728 }
balrogc8256f92008-06-08 22:45:01 +00001729 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001730 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001731 return;
1732 }
1733 keyname_buf[keyname_len] = 0;
1734 keycode = get_keycode(keyname_buf);
1735 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001736 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001737 return;
1738 }
balrogc8256f92008-06-08 22:45:01 +00001739 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001740 }
balrog3401c0d2008-06-04 10:05:59 +00001741 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001742 break;
balrog3401c0d2008-06-04 10:05:59 +00001743 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001744 }
balrogc8256f92008-06-08 22:45:01 +00001745 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001746 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001747 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001748 keycode = keycodes[i];
1749 if (keycode & 0x80)
1750 kbd_put_keycode(0xe0);
1751 kbd_put_keycode(keycode & 0x7f);
1752 }
balrogc8256f92008-06-08 22:45:01 +00001753 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001754 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001755 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001756}
1757
bellard13224a82006-07-14 22:03:35 +00001758static int mouse_button_state;
1759
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001760static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001761{
1762 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001763 const char *dx_str = qdict_get_str(qdict, "dx_str");
1764 const char *dy_str = qdict_get_str(qdict, "dy_str");
1765 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001766 dx = strtol(dx_str, NULL, 0);
1767 dy = strtol(dy_str, NULL, 0);
1768 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001769 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001770 dz = strtol(dz_str, NULL, 0);
1771 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1772}
1773
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001774static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001775{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001776 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001777 mouse_button_state = button_state;
1778 kbd_mouse_event(0, 0, 0, mouse_button_state);
1779}
1780
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001781static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001782{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001783 int size = qdict_get_int(qdict, "size");
1784 int addr = qdict_get_int(qdict, "addr");
1785 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001786 uint32_t val;
1787 int suffix;
1788
1789 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001790 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001791 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001792 addr++;
1793 }
1794 addr &= 0xffff;
1795
1796 switch(size) {
1797 default:
1798 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001799 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001800 suffix = 'b';
1801 break;
1802 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001803 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001804 suffix = 'w';
1805 break;
1806 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001807 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001808 suffix = 'l';
1809 break;
1810 }
aliguori376253e2009-03-05 23:01:23 +00001811 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1812 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001813}
bellarda3a91a32004-06-04 11:06:21 +00001814
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001815static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001816{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001817 int size = qdict_get_int(qdict, "size");
1818 int addr = qdict_get_int(qdict, "addr");
1819 int val = qdict_get_int(qdict, "val");
1820
Jan Kiszkaf1147842009-07-14 10:20:11 +02001821 addr &= IOPORTS_MASK;
1822
1823 switch (size) {
1824 default:
1825 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001826 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001827 break;
1828 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001829 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001830 break;
1831 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001832 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001833 break;
1834 }
1835}
1836
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001837static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001838{
1839 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001840 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001841
Jan Kiszka76e30d02009-07-02 00:19:02 +02001842 res = qemu_boot_set(bootdevice);
1843 if (res == 0) {
1844 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1845 } else if (res > 0) {
1846 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001847 } else {
aliguori376253e2009-03-05 23:01:23 +00001848 monitor_printf(mon, "no function defined to set boot device list for "
1849 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001850 }
1851}
1852
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001853/**
1854 * do_system_reset(): Issue a machine reset
1855 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001856static int do_system_reset(Monitor *mon, const QDict *qdict,
1857 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001858{
1859 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001860 return 0;
bellarde4f90822004-06-20 12:35:44 +00001861}
1862
Luiz Capitulino43076662009-10-07 13:41:59 -03001863/**
1864 * do_system_powerdown(): Issue a machine powerdown
1865 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001866static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1867 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001868{
1869 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001870 return 0;
bellard34751872005-07-02 14:31:34 +00001871}
1872
bellardb86bda52004-09-18 19:32:46 +00001873#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001874static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001875{
aliguori376253e2009-03-05 23:01:23 +00001876 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1877 addr,
1878 pte & mask,
1879 pte & PG_GLOBAL_MASK ? 'G' : '-',
1880 pte & PG_PSE_MASK ? 'P' : '-',
1881 pte & PG_DIRTY_MASK ? 'D' : '-',
1882 pte & PG_ACCESSED_MASK ? 'A' : '-',
1883 pte & PG_PCD_MASK ? 'C' : '-',
1884 pte & PG_PWT_MASK ? 'T' : '-',
1885 pte & PG_USER_MASK ? 'U' : '-',
1886 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001887}
1888
aliguori376253e2009-03-05 23:01:23 +00001889static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001890{
bellard6a00d602005-11-21 23:25:50 +00001891 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001892 int l1, l2;
1893 uint32_t pgd, pde, pte;
1894
bellard6a00d602005-11-21 23:25:50 +00001895 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001896
bellardb86bda52004-09-18 19:32:46 +00001897 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001898 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001899 return;
1900 }
1901 pgd = env->cr[3] & ~0xfff;
1902 for(l1 = 0; l1 < 1024; l1++) {
1903 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1904 pde = le32_to_cpu(pde);
1905 if (pde & PG_PRESENT_MASK) {
1906 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001907 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001908 } else {
1909 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001910 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001911 (uint8_t *)&pte, 4);
1912 pte = le32_to_cpu(pte);
1913 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001914 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001915 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001916 ~0xfff);
1917 }
1918 }
1919 }
1920 }
1921 }
1922}
1923
aliguori376253e2009-03-05 23:01:23 +00001924static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001925 uint32_t end, int prot)
1926{
bellard9746b152004-11-11 18:30:24 +00001927 int prot1;
1928 prot1 = *plast_prot;
1929 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001930 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001931 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1932 *pstart, end, end - *pstart,
1933 prot1 & PG_USER_MASK ? 'u' : '-',
1934 'r',
1935 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001936 }
1937 if (prot != 0)
1938 *pstart = end;
1939 else
1940 *pstart = -1;
1941 *plast_prot = prot;
1942 }
1943}
1944
aliguori376253e2009-03-05 23:01:23 +00001945static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001946{
bellard6a00d602005-11-21 23:25:50 +00001947 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001948 int l1, l2, prot, last_prot;
1949 uint32_t pgd, pde, pte, start, end;
1950
bellard6a00d602005-11-21 23:25:50 +00001951 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001952
bellardb86bda52004-09-18 19:32:46 +00001953 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001954 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001955 return;
1956 }
1957 pgd = env->cr[3] & ~0xfff;
1958 last_prot = 0;
1959 start = -1;
1960 for(l1 = 0; l1 < 1024; l1++) {
1961 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1962 pde = le32_to_cpu(pde);
1963 end = l1 << 22;
1964 if (pde & PG_PRESENT_MASK) {
1965 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1966 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001967 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001968 } else {
1969 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001970 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001971 (uint8_t *)&pte, 4);
1972 pte = le32_to_cpu(pte);
1973 end = (l1 << 22) + (l2 << 12);
1974 if (pte & PG_PRESENT_MASK) {
1975 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1976 } else {
1977 prot = 0;
1978 }
aliguori376253e2009-03-05 23:01:23 +00001979 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001980 }
1981 }
1982 } else {
1983 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001984 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001985 }
1986 }
1987}
1988#endif
1989
aurel327c664e22009-03-03 06:12:22 +00001990#if defined(TARGET_SH4)
1991
aliguori376253e2009-03-05 23:01:23 +00001992static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001993{
aliguori376253e2009-03-05 23:01:23 +00001994 monitor_printf(mon, " tlb%i:\t"
1995 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1996 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1997 "dirty=%hhu writethrough=%hhu\n",
1998 idx,
1999 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2000 tlb->v, tlb->sh, tlb->c, tlb->pr,
2001 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002002}
2003
aliguori376253e2009-03-05 23:01:23 +00002004static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002005{
2006 CPUState *env = mon_get_cpu();
2007 int i;
2008
aliguori376253e2009-03-05 23:01:23 +00002009 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002010 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002011 print_tlb (mon, i, &env->itlb[i]);
2012 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002013 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002014 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002015}
2016
2017#endif
2018
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002019static void do_info_kvm_print(Monitor *mon, const QObject *data)
2020{
2021 QDict *qdict;
2022
2023 qdict = qobject_to_qdict(data);
2024
2025 monitor_printf(mon, "kvm support: ");
2026 if (qdict_get_bool(qdict, "present")) {
2027 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2028 "enabled" : "disabled");
2029 } else {
2030 monitor_printf(mon, "not compiled\n");
2031 }
2032}
2033
2034/**
2035 * do_info_kvm(): Show KVM information
2036 *
2037 * Return a QDict with the following information:
2038 *
2039 * - "enabled": true if KVM support is enabled, false otherwise
2040 * - "present": true if QEMU has KVM support, false otherwise
2041 *
2042 * Example:
2043 *
2044 * { "enabled": true, "present": true }
2045 */
2046static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002047{
2048#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002049 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2050 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002051#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002052 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002053#endif
2054}
2055
aliguori030ea372009-04-21 22:30:47 +00002056static void do_info_numa(Monitor *mon)
2057{
aliguorib28b6232009-04-22 20:20:29 +00002058 int i;
aliguori030ea372009-04-21 22:30:47 +00002059 CPUState *env;
2060
2061 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2062 for (i = 0; i < nb_numa_nodes; i++) {
2063 monitor_printf(mon, "node %d cpus:", i);
2064 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2065 if (env->numa_node == i) {
2066 monitor_printf(mon, " %d", env->cpu_index);
2067 }
2068 }
2069 monitor_printf(mon, "\n");
2070 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2071 node_mem[i] >> 20);
2072 }
2073}
2074
bellard5f1ce942006-02-08 22:40:15 +00002075#ifdef CONFIG_PROFILER
2076
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002077int64_t qemu_time;
2078int64_t dev_time;
2079
aliguori376253e2009-03-05 23:01:23 +00002080static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002081{
2082 int64_t total;
2083 total = qemu_time;
2084 if (total == 0)
2085 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002086 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002087 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002088 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002089 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002090 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002091 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002092}
2093#else
aliguori376253e2009-03-05 23:01:23 +00002094static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002095{
aliguori376253e2009-03-05 23:01:23 +00002096 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002097}
2098#endif
2099
bellardec36b692006-07-16 18:57:03 +00002100/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002101static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002102
aliguori376253e2009-03-05 23:01:23 +00002103static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002104{
2105 int i;
2106 CaptureState *s;
2107
2108 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002109 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002110 s->ops.info (s->opaque);
2111 }
2112}
2113
Blue Swirl23130862009-06-06 08:22:04 +00002114#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002115static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002116{
2117 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002118 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002119 CaptureState *s;
2120
2121 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2122 if (i == n) {
2123 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002124 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002125 qemu_free (s);
2126 return;
2127 }
2128 }
2129}
2130
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002131static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002132{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002133 const char *path = qdict_get_str(qdict, "path");
2134 int has_freq = qdict_haskey(qdict, "freq");
2135 int freq = qdict_get_try_int(qdict, "freq", -1);
2136 int has_bits = qdict_haskey(qdict, "bits");
2137 int bits = qdict_get_try_int(qdict, "bits", -1);
2138 int has_channels = qdict_haskey(qdict, "nchannels");
2139 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002140 CaptureState *s;
2141
2142 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002143
2144 freq = has_freq ? freq : 44100;
2145 bits = has_bits ? bits : 16;
2146 nchannels = has_channels ? nchannels : 2;
2147
2148 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002149 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002150 qemu_free (s);
2151 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002152 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002153}
2154#endif
2155
aurel32dc1c0b72008-04-27 23:52:12 +00002156#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002157static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002158{
2159 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002160 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002161
2162 for (env = first_cpu; env != NULL; env = env->next_cpu)
2163 if (env->cpu_index == cpu_index) {
2164 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2165 break;
2166 }
2167}
2168#endif
2169
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002170static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002171{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002172 QDict *qdict;
2173
2174 qdict = qobject_to_qdict(data);
2175
2176 monitor_printf(mon, "VM status: ");
2177 if (qdict_get_bool(qdict, "running")) {
2178 monitor_printf(mon, "running");
2179 if (qdict_get_bool(qdict, "singlestep")) {
2180 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002181 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002182 } else {
2183 monitor_printf(mon, "paused");
2184 }
2185
2186 monitor_printf(mon, "\n");
2187}
2188
2189/**
2190 * do_info_status(): VM status
2191 *
2192 * Return a QDict with the following information:
2193 *
2194 * - "running": true if the VM is running, or false if it is paused
2195 * - "singlestep": true if the VM is in single step mode, false otherwise
2196 *
2197 * Example:
2198 *
2199 * { "running": true, "singlestep": false }
2200 */
2201static void do_info_status(Monitor *mon, QObject **ret_data)
2202{
2203 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2204 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002205}
2206
Adam Litke625a5be2010-01-26 14:17:35 -06002207static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002208{
Adam Litke625a5be2010-01-26 14:17:35 -06002209 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002210
Adam Litke625a5be2010-01-26 14:17:35 -06002211 if (strcmp(key, "actual"))
2212 monitor_printf(mon, ",%s=%" PRId64, key,
2213 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002214}
2215
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002216static void monitor_print_balloon(Monitor *mon, const QObject *data)
2217{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002218 QDict *qdict;
2219
2220 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002221 if (!qdict_haskey(qdict, "actual"))
2222 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002223
Adam Litke625a5be2010-01-26 14:17:35 -06002224 monitor_printf(mon, "balloon: actual=%" PRId64,
2225 qdict_get_int(qdict, "actual") >> 20);
2226 qdict_iter(qdict, print_balloon_stat, mon);
2227 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002228}
2229
2230/**
2231 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002232 *
Adam Litke625a5be2010-01-26 14:17:35 -06002233 * Make an asynchronous request for balloon info. When the request completes
2234 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002235 *
Adam Litke625a5be2010-01-26 14:17:35 -06002236 * - "actual": current balloon value in bytes
2237 * The following fields may or may not be present:
2238 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2239 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2240 * - "major_page_faults": Number of major faults
2241 * - "minor_page_faults": Number of minor faults
2242 * - "free_mem": Total amount of free and unused memory (bytes)
2243 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002244 *
2245 * Example:
2246 *
Adam Litke625a5be2010-01-26 14:17:35 -06002247 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2248 * "major_page_faults": 142, "minor_page_faults": 239245,
2249 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002250 */
Adam Litke625a5be2010-01-26 14:17:35 -06002251static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002252{
Adam Litke625a5be2010-01-26 14:17:35 -06002253 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002254
Adam Litke625a5be2010-01-26 14:17:35 -06002255 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2256 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2257 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002258 }
Adam Litke625a5be2010-01-26 14:17:35 -06002259
2260 ret = qemu_balloon_status(cb, opaque);
2261 if (!ret) {
2262 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2263 return -1;
2264 }
2265
2266 return 0;
2267}
2268
2269/**
2270 * do_balloon(): Request VM to change its memory allocation
2271 */
2272static int do_balloon(Monitor *mon, const QDict *params,
2273 MonitorCompletion cb, void *opaque)
2274{
2275 int ret;
2276
2277 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2278 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2279 return -1;
2280 }
2281
2282 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2283 if (ret == 0) {
2284 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2285 return -1;
2286 }
2287
2288 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002289}
2290
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002291static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002292{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002293 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002294
aliguori76655d62009-03-06 20:27:37 +00002295 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002296 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002297 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002298 return acl;
2299}
aliguori76655d62009-03-06 20:27:37 +00002300
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002301static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002302{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002303 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002304 qemu_acl *acl = find_acl(mon, aclname);
2305 qemu_acl_entry *entry;
2306 int i = 0;
2307
2308 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002309 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002310 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002311 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002312 i++;
2313 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002314 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002315 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002316 }
2317}
2318
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002319static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002320{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002321 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002322 qemu_acl *acl = find_acl(mon, aclname);
2323
2324 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002325 qemu_acl_reset(acl);
2326 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002327 }
2328}
aliguori76655d62009-03-06 20:27:37 +00002329
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002330static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002331{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002332 const char *aclname = qdict_get_str(qdict, "aclname");
2333 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002334 qemu_acl *acl = find_acl(mon, aclname);
2335
2336 if (acl) {
2337 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002338 acl->defaultDeny = 0;
2339 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002340 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002341 acl->defaultDeny = 1;
2342 monitor_printf(mon, "acl: policy set to 'deny'\n");
2343 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002344 monitor_printf(mon, "acl: unknown policy '%s', "
2345 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002346 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002347 }
2348}
aliguori76655d62009-03-06 20:27:37 +00002349
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002350static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002351{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002352 const char *aclname = qdict_get_str(qdict, "aclname");
2353 const char *match = qdict_get_str(qdict, "match");
2354 const char *policy = qdict_get_str(qdict, "policy");
2355 int has_index = qdict_haskey(qdict, "index");
2356 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002357 qemu_acl *acl = find_acl(mon, aclname);
2358 int deny, ret;
2359
2360 if (acl) {
2361 if (strcmp(policy, "allow") == 0) {
2362 deny = 0;
2363 } else if (strcmp(policy, "deny") == 0) {
2364 deny = 1;
2365 } else {
2366 monitor_printf(mon, "acl: unknown policy '%s', "
2367 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002368 return;
2369 }
aliguori28a76be2009-03-06 20:27:40 +00002370 if (has_index)
2371 ret = qemu_acl_insert(acl, deny, match, index);
2372 else
2373 ret = qemu_acl_append(acl, deny, match);
2374 if (ret < 0)
2375 monitor_printf(mon, "acl: unable to add acl entry\n");
2376 else
2377 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002378 }
2379}
aliguori76655d62009-03-06 20:27:37 +00002380
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002381static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002382{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002383 const char *aclname = qdict_get_str(qdict, "aclname");
2384 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002385 qemu_acl *acl = find_acl(mon, aclname);
2386 int ret;
aliguori76655d62009-03-06 20:27:37 +00002387
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002388 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002389 ret = qemu_acl_remove(acl, match);
2390 if (ret < 0)
2391 monitor_printf(mon, "acl: no matching acl entry\n");
2392 else
2393 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002394 }
2395}
2396
Huang Ying79c4f6b2009-06-23 10:05:14 +08002397#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002398static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002399{
2400 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002401 int cpu_index = qdict_get_int(qdict, "cpu_index");
2402 int bank = qdict_get_int(qdict, "bank");
2403 uint64_t status = qdict_get_int(qdict, "status");
2404 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2405 uint64_t addr = qdict_get_int(qdict, "addr");
2406 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002407
2408 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2409 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2410 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2411 break;
2412 }
2413}
2414#endif
2415
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002416static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002417{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002418 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002419 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002420 int fd;
2421
2422 fd = qemu_chr_get_msgfd(mon->chr);
2423 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002424 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002425 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002426 }
2427
2428 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002429 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002430 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002431 }
2432
2433 fd = dup(fd);
2434 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002435 if (errno == EMFILE)
2436 qemu_error_new(QERR_TOO_MANY_FILES);
2437 else
2438 qemu_error_new(QERR_UNDEFINED_ERROR);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002439 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002440 }
2441
Blue Swirl72cf2d42009-09-12 07:36:22 +00002442 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002443 if (strcmp(monfd->name, fdname) != 0) {
2444 continue;
2445 }
2446
2447 close(monfd->fd);
2448 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002449 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002450 }
2451
Anthony Liguoric227f092009-10-01 16:12:16 -05002452 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002453 monfd->name = qemu_strdup(fdname);
2454 monfd->fd = fd;
2455
Blue Swirl72cf2d42009-09-12 07:36:22 +00002456 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002457 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002458}
2459
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002460static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002461{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002462 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002463 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002464
Blue Swirl72cf2d42009-09-12 07:36:22 +00002465 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002466 if (strcmp(monfd->name, fdname) != 0) {
2467 continue;
2468 }
2469
Blue Swirl72cf2d42009-09-12 07:36:22 +00002470 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002471 close(monfd->fd);
2472 qemu_free(monfd->name);
2473 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002474 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002475 }
2476
Markus Armbruster063c1a02009-12-07 21:37:11 +01002477 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002478 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002479}
2480
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002481static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002482{
2483 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002484 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002485
2486 vm_stop(0);
2487
Juan Quintela05f24012009-08-20 19:42:22 +02002488 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002489 vm_start();
2490}
2491
Mark McLoughlin7768e042009-07-22 09:11:41 +01002492int monitor_get_fd(Monitor *mon, const char *fdname)
2493{
Anthony Liguoric227f092009-10-01 16:12:16 -05002494 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002495
Blue Swirl72cf2d42009-09-12 07:36:22 +00002496 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002497 int fd;
2498
2499 if (strcmp(monfd->name, fdname) != 0) {
2500 continue;
2501 }
2502
2503 fd = monfd->fd;
2504
2505 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002506 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002507 qemu_free(monfd->name);
2508 qemu_free(monfd);
2509
2510 return fd;
2511 }
2512
2513 return -1;
2514}
2515
Anthony Liguoric227f092009-10-01 16:12:16 -05002516static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002517#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002518 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002519};
2520
Blue Swirl23130862009-06-06 08:22:04 +00002521/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002522static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 {
2524 .name = "version",
2525 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 .params = "",
2527 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002528 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002529 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 },
2531 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002532 .name = "commands",
2533 .args_type = "",
2534 .params = "",
2535 .help = "list QMP available commands",
2536 .user_print = monitor_user_noop,
2537 .mhandler.info_new = do_info_commands,
2538 },
2539 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 .name = "network",
2541 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 .params = "",
2543 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002544 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 },
2546 {
2547 .name = "chardev",
2548 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002549 .params = "",
2550 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002551 .user_print = qemu_chr_info_print,
2552 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 },
2554 {
2555 .name = "block",
2556 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 .params = "",
2558 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002559 .user_print = bdrv_info_print,
2560 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
2562 {
2563 .name = "blockstats",
2564 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .params = "",
2566 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002567 .user_print = bdrv_stats_print,
2568 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 },
2570 {
2571 .name = "registers",
2572 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 .params = "",
2574 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002575 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 },
2577 {
2578 .name = "cpus",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002582 .user_print = monitor_print_cpus,
2583 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 },
2585 {
2586 .name = "history",
2587 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 .params = "",
2589 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002590 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 },
2592 {
2593 .name = "irq",
2594 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 .params = "",
2596 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002597 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 },
2599 {
2600 .name = "pic",
2601 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 .params = "",
2603 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002604 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 },
2606 {
2607 .name = "pci",
2608 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 .params = "",
2610 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002611 .user_print = do_pci_info_print,
2612 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
aurel327c664e22009-03-03 06:12:22 +00002614#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 {
2616 .name = "tlb",
2617 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 .params = "",
2619 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002620 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 },
aurel327c664e22009-03-03 06:12:22 +00002622#endif
2623#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 {
2625 .name = "mem",
2626 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 .params = "",
2628 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002629 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 },
2631 {
2632 .name = "hpet",
2633 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002634 .params = "",
2635 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002636 .user_print = do_info_hpet_print,
2637 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 },
bellardb86bda52004-09-18 19:32:46 +00002639#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 {
2641 .name = "jit",
2642 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .params = "",
2644 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002645 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 },
2647 {
2648 .name = "kvm",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002652 .user_print = do_info_kvm_print,
2653 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 },
2655 {
2656 .name = "numa",
2657 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 .params = "",
2659 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002660 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 },
2662 {
2663 .name = "usb",
2664 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 .params = "",
2666 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002667 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 },
2669 {
2670 .name = "usbhost",
2671 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002672 .params = "",
2673 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002674 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 },
2676 {
2677 .name = "profile",
2678 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 .params = "",
2680 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002681 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 },
2683 {
2684 .name = "capture",
2685 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 .params = "",
2687 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002688 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 },
2690 {
2691 .name = "snapshots",
2692 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 .params = "",
2694 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002695 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 },
2697 {
2698 .name = "status",
2699 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 .params = "",
2701 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002702 .user_print = do_info_status_print,
2703 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "pcmcia",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002710 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
2713 .name = "mice",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002717 .user_print = do_info_mice_print,
2718 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
2720 {
2721 .name = "vnc",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002725 .user_print = do_info_vnc_print,
2726 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 },
2728 {
2729 .name = "name",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002733 .user_print = do_info_name_print,
2734 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 },
2736 {
2737 .name = "uuid",
2738 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 .params = "",
2740 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002741 .user_print = do_info_uuid_print,
2742 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 },
j_mayer76a66252007-03-07 08:32:30 +00002744#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 {
2746 .name = "cpustats",
2747 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 .params = "",
2749 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002750 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 },
j_mayer76a66252007-03-07 08:32:30 +00002752#endif
blueswir131a60e22007-10-26 18:42:59 +00002753#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 {
2755 .name = "usernet",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002759 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
blueswir131a60e22007-10-26 18:42:59 +00002761#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002762 {
2763 .name = "migrate",
2764 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 .params = "",
2766 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002767 .user_print = do_info_migrate_print,
2768 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002769 },
2770 {
2771 .name = "balloon",
2772 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 .params = "",
2774 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002775 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002776 .mhandler.info_async = do_info_balloon,
2777 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
2779 {
2780 .name = "qtree",
2781 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002782 .params = "",
2783 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002784 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 },
2786 {
2787 .name = "qdm",
2788 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .params = "",
2790 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002791 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 },
2793 {
2794 .name = "roms",
2795 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002796 .params = "",
2797 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002798 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 },
2800 {
2801 .name = NULL,
2802 },
bellard9dc39cb2004-03-14 21:38:27 +00002803};
2804
bellard9307c4c2004-04-04 12:57:25 +00002805/*******************************************************************/
2806
2807static const char *pch;
2808static jmp_buf expr_env;
2809
bellard92a31b12005-02-10 22:00:52 +00002810#define MD_TLONG 0
2811#define MD_I32 1
2812
bellard9307c4c2004-04-04 12:57:25 +00002813typedef struct MonitorDef {
2814 const char *name;
2815 int offset;
blueswir18662d652008-10-02 18:32:44 +00002816 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002817 int type;
bellard9307c4c2004-04-04 12:57:25 +00002818} MonitorDef;
2819
bellard57206fd2004-04-25 18:54:52 +00002820#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002821static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002822{
bellard6a00d602005-11-21 23:25:50 +00002823 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002824 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002825}
2826#endif
2827
bellarda541f292004-04-12 20:39:29 +00002828#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002829static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002830{
bellard6a00d602005-11-21 23:25:50 +00002831 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002832 unsigned int u;
2833 int i;
2834
2835 u = 0;
2836 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002837 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002838
2839 return u;
2840}
2841
blueswir18662d652008-10-02 18:32:44 +00002842static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002843{
bellard6a00d602005-11-21 23:25:50 +00002844 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002845 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002846}
2847
blueswir18662d652008-10-02 18:32:44 +00002848static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002849{
bellard6a00d602005-11-21 23:25:50 +00002850 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002851 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002852}
bellard9fddaa02004-05-21 12:59:32 +00002853
blueswir18662d652008-10-02 18:32:44 +00002854static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002855{
bellard6a00d602005-11-21 23:25:50 +00002856 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002857 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002858}
2859
blueswir18662d652008-10-02 18:32:44 +00002860static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002861{
bellard6a00d602005-11-21 23:25:50 +00002862 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002863 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002864}
2865
blueswir18662d652008-10-02 18:32:44 +00002866static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002867{
bellard6a00d602005-11-21 23:25:50 +00002868 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002869 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002870}
bellarda541f292004-04-12 20:39:29 +00002871#endif
2872
bellarde95c8d52004-09-30 22:22:08 +00002873#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002874#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002875static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002876{
bellard6a00d602005-11-21 23:25:50 +00002877 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002878 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002879}
bellard7b936c02005-10-30 17:05:13 +00002880#endif
bellarde95c8d52004-09-30 22:22:08 +00002881
blueswir18662d652008-10-02 18:32:44 +00002882static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002883{
bellard6a00d602005-11-21 23:25:50 +00002884 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002885 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002886}
2887#endif
2888
blueswir18662d652008-10-02 18:32:44 +00002889static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002890#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002891
2892#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002893 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002894 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002895 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002896
bellard9307c4c2004-04-04 12:57:25 +00002897 { "eax", offsetof(CPUState, regs[0]) },
2898 { "ecx", offsetof(CPUState, regs[1]) },
2899 { "edx", offsetof(CPUState, regs[2]) },
2900 { "ebx", offsetof(CPUState, regs[3]) },
2901 { "esp|sp", offsetof(CPUState, regs[4]) },
2902 { "ebp|fp", offsetof(CPUState, regs[5]) },
2903 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002904 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002905#ifdef TARGET_X86_64
2906 { "r8", offsetof(CPUState, regs[8]) },
2907 { "r9", offsetof(CPUState, regs[9]) },
2908 { "r10", offsetof(CPUState, regs[10]) },
2909 { "r11", offsetof(CPUState, regs[11]) },
2910 { "r12", offsetof(CPUState, regs[12]) },
2911 { "r13", offsetof(CPUState, regs[13]) },
2912 { "r14", offsetof(CPUState, regs[14]) },
2913 { "r15", offsetof(CPUState, regs[15]) },
2914#endif
bellard9307c4c2004-04-04 12:57:25 +00002915 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002916 { "eip", offsetof(CPUState, eip) },
2917 SEG("cs", R_CS)
2918 SEG("ds", R_DS)
2919 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002920 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002921 SEG("fs", R_FS)
2922 SEG("gs", R_GS)
2923 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002924#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002925 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002926 { "r0", offsetof(CPUState, gpr[0]) },
2927 { "r1", offsetof(CPUState, gpr[1]) },
2928 { "r2", offsetof(CPUState, gpr[2]) },
2929 { "r3", offsetof(CPUState, gpr[3]) },
2930 { "r4", offsetof(CPUState, gpr[4]) },
2931 { "r5", offsetof(CPUState, gpr[5]) },
2932 { "r6", offsetof(CPUState, gpr[6]) },
2933 { "r7", offsetof(CPUState, gpr[7]) },
2934 { "r8", offsetof(CPUState, gpr[8]) },
2935 { "r9", offsetof(CPUState, gpr[9]) },
2936 { "r10", offsetof(CPUState, gpr[10]) },
2937 { "r11", offsetof(CPUState, gpr[11]) },
2938 { "r12", offsetof(CPUState, gpr[12]) },
2939 { "r13", offsetof(CPUState, gpr[13]) },
2940 { "r14", offsetof(CPUState, gpr[14]) },
2941 { "r15", offsetof(CPUState, gpr[15]) },
2942 { "r16", offsetof(CPUState, gpr[16]) },
2943 { "r17", offsetof(CPUState, gpr[17]) },
2944 { "r18", offsetof(CPUState, gpr[18]) },
2945 { "r19", offsetof(CPUState, gpr[19]) },
2946 { "r20", offsetof(CPUState, gpr[20]) },
2947 { "r21", offsetof(CPUState, gpr[21]) },
2948 { "r22", offsetof(CPUState, gpr[22]) },
2949 { "r23", offsetof(CPUState, gpr[23]) },
2950 { "r24", offsetof(CPUState, gpr[24]) },
2951 { "r25", offsetof(CPUState, gpr[25]) },
2952 { "r26", offsetof(CPUState, gpr[26]) },
2953 { "r27", offsetof(CPUState, gpr[27]) },
2954 { "r28", offsetof(CPUState, gpr[28]) },
2955 { "r29", offsetof(CPUState, gpr[29]) },
2956 { "r30", offsetof(CPUState, gpr[30]) },
2957 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002958 /* Floating point registers */
2959 { "f0", offsetof(CPUState, fpr[0]) },
2960 { "f1", offsetof(CPUState, fpr[1]) },
2961 { "f2", offsetof(CPUState, fpr[2]) },
2962 { "f3", offsetof(CPUState, fpr[3]) },
2963 { "f4", offsetof(CPUState, fpr[4]) },
2964 { "f5", offsetof(CPUState, fpr[5]) },
2965 { "f6", offsetof(CPUState, fpr[6]) },
2966 { "f7", offsetof(CPUState, fpr[7]) },
2967 { "f8", offsetof(CPUState, fpr[8]) },
2968 { "f9", offsetof(CPUState, fpr[9]) },
2969 { "f10", offsetof(CPUState, fpr[10]) },
2970 { "f11", offsetof(CPUState, fpr[11]) },
2971 { "f12", offsetof(CPUState, fpr[12]) },
2972 { "f13", offsetof(CPUState, fpr[13]) },
2973 { "f14", offsetof(CPUState, fpr[14]) },
2974 { "f15", offsetof(CPUState, fpr[15]) },
2975 { "f16", offsetof(CPUState, fpr[16]) },
2976 { "f17", offsetof(CPUState, fpr[17]) },
2977 { "f18", offsetof(CPUState, fpr[18]) },
2978 { "f19", offsetof(CPUState, fpr[19]) },
2979 { "f20", offsetof(CPUState, fpr[20]) },
2980 { "f21", offsetof(CPUState, fpr[21]) },
2981 { "f22", offsetof(CPUState, fpr[22]) },
2982 { "f23", offsetof(CPUState, fpr[23]) },
2983 { "f24", offsetof(CPUState, fpr[24]) },
2984 { "f25", offsetof(CPUState, fpr[25]) },
2985 { "f26", offsetof(CPUState, fpr[26]) },
2986 { "f27", offsetof(CPUState, fpr[27]) },
2987 { "f28", offsetof(CPUState, fpr[28]) },
2988 { "f29", offsetof(CPUState, fpr[29]) },
2989 { "f30", offsetof(CPUState, fpr[30]) },
2990 { "f31", offsetof(CPUState, fpr[31]) },
2991 { "fpscr", offsetof(CPUState, fpscr) },
2992 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002993 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002994 { "lr", offsetof(CPUState, lr) },
2995 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002996 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002997 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002998 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002999 { "msr", 0, &monitor_get_msr, },
3000 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003001 { "tbu", 0, &monitor_get_tbu, },
3002 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003003#if defined(TARGET_PPC64)
3004 /* Address space register */
3005 { "asr", offsetof(CPUState, asr) },
3006#endif
3007 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00003008 { "sdr1", offsetof(CPUState, sdr1) },
3009 { "sr0", offsetof(CPUState, sr[0]) },
3010 { "sr1", offsetof(CPUState, sr[1]) },
3011 { "sr2", offsetof(CPUState, sr[2]) },
3012 { "sr3", offsetof(CPUState, sr[3]) },
3013 { "sr4", offsetof(CPUState, sr[4]) },
3014 { "sr5", offsetof(CPUState, sr[5]) },
3015 { "sr6", offsetof(CPUState, sr[6]) },
3016 { "sr7", offsetof(CPUState, sr[7]) },
3017 { "sr8", offsetof(CPUState, sr[8]) },
3018 { "sr9", offsetof(CPUState, sr[9]) },
3019 { "sr10", offsetof(CPUState, sr[10]) },
3020 { "sr11", offsetof(CPUState, sr[11]) },
3021 { "sr12", offsetof(CPUState, sr[12]) },
3022 { "sr13", offsetof(CPUState, sr[13]) },
3023 { "sr14", offsetof(CPUState, sr[14]) },
3024 { "sr15", offsetof(CPUState, sr[15]) },
3025 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003026#elif defined(TARGET_SPARC)
3027 { "g0", offsetof(CPUState, gregs[0]) },
3028 { "g1", offsetof(CPUState, gregs[1]) },
3029 { "g2", offsetof(CPUState, gregs[2]) },
3030 { "g3", offsetof(CPUState, gregs[3]) },
3031 { "g4", offsetof(CPUState, gregs[4]) },
3032 { "g5", offsetof(CPUState, gregs[5]) },
3033 { "g6", offsetof(CPUState, gregs[6]) },
3034 { "g7", offsetof(CPUState, gregs[7]) },
3035 { "o0", 0, monitor_get_reg },
3036 { "o1", 1, monitor_get_reg },
3037 { "o2", 2, monitor_get_reg },
3038 { "o3", 3, monitor_get_reg },
3039 { "o4", 4, monitor_get_reg },
3040 { "o5", 5, monitor_get_reg },
3041 { "o6", 6, monitor_get_reg },
3042 { "o7", 7, monitor_get_reg },
3043 { "l0", 8, monitor_get_reg },
3044 { "l1", 9, monitor_get_reg },
3045 { "l2", 10, monitor_get_reg },
3046 { "l3", 11, monitor_get_reg },
3047 { "l4", 12, monitor_get_reg },
3048 { "l5", 13, monitor_get_reg },
3049 { "l6", 14, monitor_get_reg },
3050 { "l7", 15, monitor_get_reg },
3051 { "i0", 16, monitor_get_reg },
3052 { "i1", 17, monitor_get_reg },
3053 { "i2", 18, monitor_get_reg },
3054 { "i3", 19, monitor_get_reg },
3055 { "i4", 20, monitor_get_reg },
3056 { "i5", 21, monitor_get_reg },
3057 { "i6", 22, monitor_get_reg },
3058 { "i7", 23, monitor_get_reg },
3059 { "pc", offsetof(CPUState, pc) },
3060 { "npc", offsetof(CPUState, npc) },
3061 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003062#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003063 { "psr", 0, &monitor_get_psr, },
3064 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003065#endif
bellarde95c8d52004-09-30 22:22:08 +00003066 { "tbr", offsetof(CPUState, tbr) },
3067 { "fsr", offsetof(CPUState, fsr) },
3068 { "f0", offsetof(CPUState, fpr[0]) },
3069 { "f1", offsetof(CPUState, fpr[1]) },
3070 { "f2", offsetof(CPUState, fpr[2]) },
3071 { "f3", offsetof(CPUState, fpr[3]) },
3072 { "f4", offsetof(CPUState, fpr[4]) },
3073 { "f5", offsetof(CPUState, fpr[5]) },
3074 { "f6", offsetof(CPUState, fpr[6]) },
3075 { "f7", offsetof(CPUState, fpr[7]) },
3076 { "f8", offsetof(CPUState, fpr[8]) },
3077 { "f9", offsetof(CPUState, fpr[9]) },
3078 { "f10", offsetof(CPUState, fpr[10]) },
3079 { "f11", offsetof(CPUState, fpr[11]) },
3080 { "f12", offsetof(CPUState, fpr[12]) },
3081 { "f13", offsetof(CPUState, fpr[13]) },
3082 { "f14", offsetof(CPUState, fpr[14]) },
3083 { "f15", offsetof(CPUState, fpr[15]) },
3084 { "f16", offsetof(CPUState, fpr[16]) },
3085 { "f17", offsetof(CPUState, fpr[17]) },
3086 { "f18", offsetof(CPUState, fpr[18]) },
3087 { "f19", offsetof(CPUState, fpr[19]) },
3088 { "f20", offsetof(CPUState, fpr[20]) },
3089 { "f21", offsetof(CPUState, fpr[21]) },
3090 { "f22", offsetof(CPUState, fpr[22]) },
3091 { "f23", offsetof(CPUState, fpr[23]) },
3092 { "f24", offsetof(CPUState, fpr[24]) },
3093 { "f25", offsetof(CPUState, fpr[25]) },
3094 { "f26", offsetof(CPUState, fpr[26]) },
3095 { "f27", offsetof(CPUState, fpr[27]) },
3096 { "f28", offsetof(CPUState, fpr[28]) },
3097 { "f29", offsetof(CPUState, fpr[29]) },
3098 { "f30", offsetof(CPUState, fpr[30]) },
3099 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003100#ifdef TARGET_SPARC64
3101 { "f32", offsetof(CPUState, fpr[32]) },
3102 { "f34", offsetof(CPUState, fpr[34]) },
3103 { "f36", offsetof(CPUState, fpr[36]) },
3104 { "f38", offsetof(CPUState, fpr[38]) },
3105 { "f40", offsetof(CPUState, fpr[40]) },
3106 { "f42", offsetof(CPUState, fpr[42]) },
3107 { "f44", offsetof(CPUState, fpr[44]) },
3108 { "f46", offsetof(CPUState, fpr[46]) },
3109 { "f48", offsetof(CPUState, fpr[48]) },
3110 { "f50", offsetof(CPUState, fpr[50]) },
3111 { "f52", offsetof(CPUState, fpr[52]) },
3112 { "f54", offsetof(CPUState, fpr[54]) },
3113 { "f56", offsetof(CPUState, fpr[56]) },
3114 { "f58", offsetof(CPUState, fpr[58]) },
3115 { "f60", offsetof(CPUState, fpr[60]) },
3116 { "f62", offsetof(CPUState, fpr[62]) },
3117 { "asi", offsetof(CPUState, asi) },
3118 { "pstate", offsetof(CPUState, pstate) },
3119 { "cansave", offsetof(CPUState, cansave) },
3120 { "canrestore", offsetof(CPUState, canrestore) },
3121 { "otherwin", offsetof(CPUState, otherwin) },
3122 { "wstate", offsetof(CPUState, wstate) },
3123 { "cleanwin", offsetof(CPUState, cleanwin) },
3124 { "fprs", offsetof(CPUState, fprs) },
3125#endif
bellard9307c4c2004-04-04 12:57:25 +00003126#endif
3127 { NULL },
3128};
3129
aliguori376253e2009-03-05 23:01:23 +00003130static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003131{
aliguori376253e2009-03-05 23:01:23 +00003132 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003133 longjmp(expr_env, 1);
3134}
3135
Markus Armbruster09b94182010-01-20 13:07:30 +01003136/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003137static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003138{
blueswir18662d652008-10-02 18:32:44 +00003139 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003140 void *ptr;
3141
bellard9307c4c2004-04-04 12:57:25 +00003142 for(md = monitor_defs; md->name != NULL; md++) {
3143 if (compare_cmd(name, md->name)) {
3144 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003145 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003146 } else {
bellard6a00d602005-11-21 23:25:50 +00003147 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003148 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003149 switch(md->type) {
3150 case MD_I32:
3151 *pval = *(int32_t *)ptr;
3152 break;
3153 case MD_TLONG:
3154 *pval = *(target_long *)ptr;
3155 break;
3156 default:
3157 *pval = 0;
3158 break;
3159 }
bellard9307c4c2004-04-04 12:57:25 +00003160 }
3161 return 0;
3162 }
3163 }
3164 return -1;
3165}
3166
3167static void next(void)
3168{
Blue Swirl660f11b2009-07-31 21:16:51 +00003169 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003170 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003171 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003172 pch++;
3173 }
3174}
3175
aliguori376253e2009-03-05 23:01:23 +00003176static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003177
aliguori376253e2009-03-05 23:01:23 +00003178static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003179{
blueswir1c2efc952007-09-25 17:28:42 +00003180 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003181 char *p;
bellard6a00d602005-11-21 23:25:50 +00003182 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003183
3184 switch(*pch) {
3185 case '+':
3186 next();
aliguori376253e2009-03-05 23:01:23 +00003187 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003188 break;
3189 case '-':
3190 next();
aliguori376253e2009-03-05 23:01:23 +00003191 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003192 break;
3193 case '~':
3194 next();
aliguori376253e2009-03-05 23:01:23 +00003195 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003196 break;
3197 case '(':
3198 next();
aliguori376253e2009-03-05 23:01:23 +00003199 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003200 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003201 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003202 }
3203 next();
3204 break;
bellard81d09122004-07-14 17:21:37 +00003205 case '\'':
3206 pch++;
3207 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003208 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003209 n = *pch;
3210 pch++;
3211 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003212 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003213 next();
3214 break;
bellard9307c4c2004-04-04 12:57:25 +00003215 case '$':
3216 {
3217 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003218 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003219
bellard9307c4c2004-04-04 12:57:25 +00003220 pch++;
3221 q = buf;
3222 while ((*pch >= 'a' && *pch <= 'z') ||
3223 (*pch >= 'A' && *pch <= 'Z') ||
3224 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003225 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003226 if ((q - buf) < sizeof(buf) - 1)
3227 *q++ = *pch;
3228 pch++;
3229 }
blueswir1cd390082008-11-16 13:53:32 +00003230 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003231 pch++;
3232 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003233 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003234 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003235 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003236 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003237 }
3238 break;
3239 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003240 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003241 n = 0;
3242 break;
3243 default:
blueswir17743e582007-09-24 18:39:04 +00003244#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003245 n = strtoull(pch, &p, 0);
3246#else
bellard9307c4c2004-04-04 12:57:25 +00003247 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003248#endif
bellard9307c4c2004-04-04 12:57:25 +00003249 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003250 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003251 }
3252 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003253 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003254 pch++;
3255 break;
3256 }
3257 return n;
3258}
3259
3260
aliguori376253e2009-03-05 23:01:23 +00003261static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003262{
blueswir1c2efc952007-09-25 17:28:42 +00003263 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003264 int op;
ths3b46e622007-09-17 08:09:54 +00003265
aliguori376253e2009-03-05 23:01:23 +00003266 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003267 for(;;) {
3268 op = *pch;
3269 if (op != '*' && op != '/' && op != '%')
3270 break;
3271 next();
aliguori376253e2009-03-05 23:01:23 +00003272 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003273 switch(op) {
3274 default:
3275 case '*':
3276 val *= val2;
3277 break;
3278 case '/':
3279 case '%':
ths5fafdf22007-09-16 21:08:06 +00003280 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003281 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003282 if (op == '/')
3283 val /= val2;
3284 else
3285 val %= val2;
3286 break;
3287 }
3288 }
3289 return val;
3290}
3291
aliguori376253e2009-03-05 23:01:23 +00003292static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003293{
blueswir1c2efc952007-09-25 17:28:42 +00003294 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003295 int op;
bellard9307c4c2004-04-04 12:57:25 +00003296
aliguori376253e2009-03-05 23:01:23 +00003297 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003298 for(;;) {
3299 op = *pch;
3300 if (op != '&' && op != '|' && op != '^')
3301 break;
3302 next();
aliguori376253e2009-03-05 23:01:23 +00003303 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003304 switch(op) {
3305 default:
3306 case '&':
3307 val &= val2;
3308 break;
3309 case '|':
3310 val |= val2;
3311 break;
3312 case '^':
3313 val ^= val2;
3314 break;
3315 }
3316 }
3317 return val;
3318}
3319
aliguori376253e2009-03-05 23:01:23 +00003320static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003321{
blueswir1c2efc952007-09-25 17:28:42 +00003322 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003323 int op;
bellard9307c4c2004-04-04 12:57:25 +00003324
aliguori376253e2009-03-05 23:01:23 +00003325 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003326 for(;;) {
3327 op = *pch;
3328 if (op != '+' && op != '-')
3329 break;
3330 next();
aliguori376253e2009-03-05 23:01:23 +00003331 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003332 if (op == '+')
3333 val += val2;
3334 else
3335 val -= val2;
3336 }
3337 return val;
3338}
3339
aliguori376253e2009-03-05 23:01:23 +00003340static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003341{
3342 pch = *pp;
3343 if (setjmp(expr_env)) {
3344 *pp = pch;
3345 return -1;
3346 }
blueswir1cd390082008-11-16 13:53:32 +00003347 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003348 pch++;
aliguori376253e2009-03-05 23:01:23 +00003349 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003350 *pp = pch;
3351 return 0;
3352}
3353
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003354static int get_double(Monitor *mon, double *pval, const char **pp)
3355{
3356 const char *p = *pp;
3357 char *tailp;
3358 double d;
3359
3360 d = strtod(p, &tailp);
3361 if (tailp == p) {
3362 monitor_printf(mon, "Number expected\n");
3363 return -1;
3364 }
3365 if (d != d || d - d != 0) {
3366 /* NaN or infinity */
3367 monitor_printf(mon, "Bad number\n");
3368 return -1;
3369 }
3370 *pval = d;
3371 *pp = tailp;
3372 return 0;
3373}
3374
bellard9307c4c2004-04-04 12:57:25 +00003375static int get_str(char *buf, int buf_size, const char **pp)
3376{
3377 const char *p;
3378 char *q;
3379 int c;
3380
bellard81d09122004-07-14 17:21:37 +00003381 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003382 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003383 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003384 p++;
3385 if (*p == '\0') {
3386 fail:
bellard81d09122004-07-14 17:21:37 +00003387 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003388 *pp = p;
3389 return -1;
3390 }
bellard9307c4c2004-04-04 12:57:25 +00003391 if (*p == '\"') {
3392 p++;
3393 while (*p != '\0' && *p != '\"') {
3394 if (*p == '\\') {
3395 p++;
3396 c = *p++;
3397 switch(c) {
3398 case 'n':
3399 c = '\n';
3400 break;
3401 case 'r':
3402 c = '\r';
3403 break;
3404 case '\\':
3405 case '\'':
3406 case '\"':
3407 break;
3408 default:
3409 qemu_printf("unsupported escape code: '\\%c'\n", c);
3410 goto fail;
3411 }
3412 if ((q - buf) < buf_size - 1) {
3413 *q++ = c;
3414 }
3415 } else {
3416 if ((q - buf) < buf_size - 1) {
3417 *q++ = *p;
3418 }
3419 p++;
3420 }
3421 }
3422 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003423 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003424 goto fail;
3425 }
3426 p++;
3427 } else {
blueswir1cd390082008-11-16 13:53:32 +00003428 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003429 if ((q - buf) < buf_size - 1) {
3430 *q++ = *p;
3431 }
3432 p++;
3433 }
bellard9307c4c2004-04-04 12:57:25 +00003434 }
bellard81d09122004-07-14 17:21:37 +00003435 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003436 *pp = p;
3437 return 0;
3438}
3439
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003440/*
3441 * Store the command-name in cmdname, and return a pointer to
3442 * the remaining of the command string.
3443 */
3444static const char *get_command_name(const char *cmdline,
3445 char *cmdname, size_t nlen)
3446{
3447 size_t len;
3448 const char *p, *pstart;
3449
3450 p = cmdline;
3451 while (qemu_isspace(*p))
3452 p++;
3453 if (*p == '\0')
3454 return NULL;
3455 pstart = p;
3456 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3457 p++;
3458 len = p - pstart;
3459 if (len > nlen - 1)
3460 len = nlen - 1;
3461 memcpy(cmdname, pstart, len);
3462 cmdname[len] = '\0';
3463 return p;
3464}
3465
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003466/**
3467 * Read key of 'type' into 'key' and return the current
3468 * 'type' pointer.
3469 */
3470static char *key_get_info(const char *type, char **key)
3471{
3472 size_t len;
3473 char *p, *str;
3474
3475 if (*type == ',')
3476 type++;
3477
3478 p = strchr(type, ':');
3479 if (!p) {
3480 *key = NULL;
3481 return NULL;
3482 }
3483 len = p - type;
3484
3485 str = qemu_malloc(len + 1);
3486 memcpy(str, type, len);
3487 str[len] = '\0';
3488
3489 *key = str;
3490 return ++p;
3491}
3492
bellard9307c4c2004-04-04 12:57:25 +00003493static int default_fmt_format = 'x';
3494static int default_fmt_size = 4;
3495
3496#define MAX_ARGS 16
3497
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003498static int is_valid_option(const char *c, const char *typestr)
3499{
3500 char option[3];
3501
3502 option[0] = '-';
3503 option[1] = *c;
3504 option[2] = '\0';
3505
3506 typestr = strstr(typestr, option);
3507 return (typestr != NULL);
3508}
3509
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003510static const mon_cmd_t *monitor_find_command(const char *cmdname)
3511{
3512 const mon_cmd_t *cmd;
3513
3514 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3515 if (compare_cmd(cmdname, cmd->name)) {
3516 return cmd;
3517 }
3518 }
3519
3520 return NULL;
3521}
3522
Anthony Liguoric227f092009-10-01 16:12:16 -05003523static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003524 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003525 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003526{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003527 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003528 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003529 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003530 char cmdname[256];
3531 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003532 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003533
3534#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003535 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003536#endif
ths3b46e622007-09-17 08:09:54 +00003537
bellard9307c4c2004-04-04 12:57:25 +00003538 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003539 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3540 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003541 return NULL;
ths3b46e622007-09-17 08:09:54 +00003542
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003543 cmd = monitor_find_command(cmdname);
3544 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003545 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003546 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003547 }
bellard9307c4c2004-04-04 12:57:25 +00003548
bellard9307c4c2004-04-04 12:57:25 +00003549 /* parse the parameters */
3550 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003551 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003552 typestr = key_get_info(typestr, &key);
3553 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003554 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003555 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003556 typestr++;
3557 switch(c) {
3558 case 'F':
bellard81d09122004-07-14 17:21:37 +00003559 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003560 case 's':
3561 {
3562 int ret;
ths3b46e622007-09-17 08:09:54 +00003563
blueswir1cd390082008-11-16 13:53:32 +00003564 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003565 p++;
3566 if (*typestr == '?') {
3567 typestr++;
3568 if (*p == '\0') {
3569 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003570 break;
bellard9307c4c2004-04-04 12:57:25 +00003571 }
3572 }
3573 ret = get_str(buf, sizeof(buf), &p);
3574 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003575 switch(c) {
3576 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003577 monitor_printf(mon, "%s: filename expected\n",
3578 cmdname);
bellard81d09122004-07-14 17:21:37 +00003579 break;
3580 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003581 monitor_printf(mon, "%s: block device name expected\n",
3582 cmdname);
bellard81d09122004-07-14 17:21:37 +00003583 break;
3584 default:
aliguori376253e2009-03-05 23:01:23 +00003585 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003586 break;
3587 }
bellard9307c4c2004-04-04 12:57:25 +00003588 goto fail;
3589 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003590 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003591 }
3592 break;
3593 case '/':
3594 {
3595 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003596
blueswir1cd390082008-11-16 13:53:32 +00003597 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003598 p++;
3599 if (*p == '/') {
3600 /* format found */
3601 p++;
3602 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003603 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003604 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003605 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003606 count = count * 10 + (*p - '0');
3607 p++;
3608 }
3609 }
3610 size = -1;
3611 format = -1;
3612 for(;;) {
3613 switch(*p) {
3614 case 'o':
3615 case 'd':
3616 case 'u':
3617 case 'x':
3618 case 'i':
3619 case 'c':
3620 format = *p++;
3621 break;
3622 case 'b':
3623 size = 1;
3624 p++;
3625 break;
3626 case 'h':
3627 size = 2;
3628 p++;
3629 break;
3630 case 'w':
3631 size = 4;
3632 p++;
3633 break;
3634 case 'g':
3635 case 'L':
3636 size = 8;
3637 p++;
3638 break;
3639 default:
3640 goto next;
3641 }
3642 }
3643 next:
blueswir1cd390082008-11-16 13:53:32 +00003644 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003645 monitor_printf(mon, "invalid char in format: '%c'\n",
3646 *p);
bellard9307c4c2004-04-04 12:57:25 +00003647 goto fail;
3648 }
bellard9307c4c2004-04-04 12:57:25 +00003649 if (format < 0)
3650 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003651 if (format != 'i') {
3652 /* for 'i', not specifying a size gives -1 as size */
3653 if (size < 0)
3654 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003655 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003656 }
bellard9307c4c2004-04-04 12:57:25 +00003657 default_fmt_format = format;
3658 } else {
3659 count = 1;
3660 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003661 if (format != 'i') {
3662 size = default_fmt_size;
3663 } else {
3664 size = -1;
3665 }
bellard9307c4c2004-04-04 12:57:25 +00003666 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003667 qdict_put(qdict, "count", qint_from_int(count));
3668 qdict_put(qdict, "format", qint_from_int(format));
3669 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003670 }
3671 break;
3672 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003673 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003674 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003675 {
blueswir1c2efc952007-09-25 17:28:42 +00003676 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003677
blueswir1cd390082008-11-16 13:53:32 +00003678 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003679 p++;
bellard34405572004-06-08 00:55:58 +00003680 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003681 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003682 if (*p == '\0') {
3683 typestr++;
3684 break;
3685 }
bellard34405572004-06-08 00:55:58 +00003686 } else {
3687 if (*p == '.') {
3688 p++;
blueswir1cd390082008-11-16 13:53:32 +00003689 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003690 p++;
bellard34405572004-06-08 00:55:58 +00003691 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003692 typestr++;
3693 break;
bellard34405572004-06-08 00:55:58 +00003694 }
3695 }
bellard13224a82006-07-14 22:03:35 +00003696 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003697 }
aliguori376253e2009-03-05 23:01:23 +00003698 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003699 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003700 /* Check if 'i' is greater than 32-bit */
3701 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3702 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3703 monitor_printf(mon, "integer is for 32-bit values\n");
3704 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003705 } else if (c == 'M') {
3706 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003707 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003708 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003709 }
3710 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003711 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003712 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003713 {
3714 double val;
3715
3716 while (qemu_isspace(*p))
3717 p++;
3718 if (*typestr == '?') {
3719 typestr++;
3720 if (*p == '\0') {
3721 break;
3722 }
3723 }
3724 if (get_double(mon, &val, &p) < 0) {
3725 goto fail;
3726 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003727 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003728 switch (*p) {
3729 case 'K': case 'k':
3730 val *= 1 << 10; p++; break;
3731 case 'M': case 'm':
3732 val *= 1 << 20; p++; break;
3733 case 'G': case 'g':
3734 val *= 1 << 30; p++; break;
3735 }
3736 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003737 if (c == 'T' && p[0] && p[1] == 's') {
3738 switch (*p) {
3739 case 'm':
3740 val /= 1e3; p += 2; break;
3741 case 'u':
3742 val /= 1e6; p += 2; break;
3743 case 'n':
3744 val /= 1e9; p += 2; break;
3745 }
3746 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003747 if (*p && !qemu_isspace(*p)) {
3748 monitor_printf(mon, "Unknown unit suffix\n");
3749 goto fail;
3750 }
3751 qdict_put(qdict, key, qfloat_from_double(val));
3752 }
3753 break;
bellard9307c4c2004-04-04 12:57:25 +00003754 case '-':
3755 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003756 const char *tmp = p;
3757 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003758 /* option */
ths3b46e622007-09-17 08:09:54 +00003759
bellard9307c4c2004-04-04 12:57:25 +00003760 c = *typestr++;
3761 if (c == '\0')
3762 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003763 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003764 p++;
3765 has_option = 0;
3766 if (*p == '-') {
3767 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003768 if(c != *p) {
3769 if(!is_valid_option(p, typestr)) {
3770
3771 monitor_printf(mon, "%s: unsupported option -%c\n",
3772 cmdname, *p);
3773 goto fail;
3774 } else {
3775 skip_key = 1;
3776 }
bellard9307c4c2004-04-04 12:57:25 +00003777 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003778 if(skip_key) {
3779 p = tmp;
3780 } else {
3781 p++;
3782 has_option = 1;
3783 }
bellard9307c4c2004-04-04 12:57:25 +00003784 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003785 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003786 }
3787 break;
3788 default:
3789 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003790 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003791 goto fail;
3792 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003793 qemu_free(key);
3794 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003795 }
3796 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003797 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003798 p++;
3799 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003800 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3801 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003802 goto fail;
3803 }
3804
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003805 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003806
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003807fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003808 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003809 return NULL;
3810}
3811
Luiz Capitulino8204a912009-11-18 23:05:31 -02003812static void monitor_print_error(Monitor *mon)
3813{
3814 qerror_print(mon->error);
3815 QDECREF(mon->error);
3816 mon->error = NULL;
3817}
3818
Adam Litke940cc302010-01-25 12:18:44 -06003819static int is_async_return(const QObject *data)
3820{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003821 if (data && qobject_type(data) == QTYPE_QDICT) {
3822 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3823 }
3824
3825 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003826}
3827
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003828static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3829 const QDict *params)
3830{
3831 QObject *data = NULL;
3832
Luiz Capitulino97536cf2010-02-10 23:49:47 -02003833 if (cmd->cmd_new_ret) {
3834 cmd->cmd_new_ret(mon, params, &data);
3835 } else {
3836 cmd->mhandler.cmd_new(mon, params, &data);
3837 }
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003838
Adam Litke940cc302010-01-25 12:18:44 -06003839 if (is_async_return(data)) {
3840 /*
3841 * Asynchronous commands have no initial return data but they can
3842 * generate errors. Data is returned via the async completion handler.
3843 */
3844 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3845 monitor_protocol_emitter(mon, NULL);
3846 }
3847 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003848 /* Monitor Protocol */
3849 monitor_protocol_emitter(mon, data);
3850 } else {
3851 /* User Protocol */
3852 if (data)
3853 cmd->user_print(mon, data);
3854 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003855
3856 qobject_decref(data);
3857}
3858
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003859static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003860{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003861 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003862 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003863
3864 qdict = qdict_new();
3865
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003866 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003867 if (!cmd)
3868 goto out;
3869
3870 qemu_errors_to_mon(mon);
3871
Adam Litke940cc302010-01-25 12:18:44 -06003872 if (monitor_handler_is_async(cmd)) {
3873 user_async_cmd_handler(mon, cmd, qdict);
3874 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003875 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003876 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003877 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003878 }
3879
Luiz Capitulino8204a912009-11-18 23:05:31 -02003880 if (monitor_has_error(mon))
3881 monitor_print_error(mon);
3882
3883 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003884
3885out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003886 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003887}
3888
bellard81d09122004-07-14 17:21:37 +00003889static void cmd_completion(const char *name, const char *list)
3890{
3891 const char *p, *pstart;
3892 char cmd[128];
3893 int len;
3894
3895 p = list;
3896 for(;;) {
3897 pstart = p;
3898 p = strchr(p, '|');
3899 if (!p)
3900 p = pstart + strlen(pstart);
3901 len = p - pstart;
3902 if (len > sizeof(cmd) - 2)
3903 len = sizeof(cmd) - 2;
3904 memcpy(cmd, pstart, len);
3905 cmd[len] = '\0';
3906 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003907 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003908 }
3909 if (*p == '\0')
3910 break;
3911 p++;
3912 }
3913}
3914
3915static void file_completion(const char *input)
3916{
3917 DIR *ffs;
3918 struct dirent *d;
3919 char path[1024];
3920 char file[1024], file_prefix[1024];
3921 int input_path_len;
3922 const char *p;
3923
ths5fafdf22007-09-16 21:08:06 +00003924 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003925 if (!p) {
3926 input_path_len = 0;
3927 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003928 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003929 } else {
3930 input_path_len = p - input + 1;
3931 memcpy(path, input, input_path_len);
3932 if (input_path_len > sizeof(path) - 1)
3933 input_path_len = sizeof(path) - 1;
3934 path[input_path_len] = '\0';
3935 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3936 }
3937#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003938 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3939 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003940#endif
3941 ffs = opendir(path);
3942 if (!ffs)
3943 return;
3944 for(;;) {
3945 struct stat sb;
3946 d = readdir(ffs);
3947 if (!d)
3948 break;
3949 if (strstart(d->d_name, file_prefix, NULL)) {
3950 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003951 if (input_path_len < sizeof(file))
3952 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3953 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003954 /* stat the file to find out if it's a directory.
3955 * In that case add a slash to speed up typing long paths
3956 */
3957 stat(file, &sb);
3958 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003959 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003960 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003961 }
3962 }
3963 closedir(ffs);
3964}
3965
aliguori51de9762009-03-05 23:00:43 +00003966static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003967{
aliguori51de9762009-03-05 23:00:43 +00003968 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003969 const char *input = opaque;
3970
3971 if (input[0] == '\0' ||
3972 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003973 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003974 }
3975}
3976
3977/* NOTE: this parser is an approximate form of the real command parser */
3978static void parse_cmdline(const char *cmdline,
3979 int *pnb_args, char **args)
3980{
3981 const char *p;
3982 int nb_args, ret;
3983 char buf[1024];
3984
3985 p = cmdline;
3986 nb_args = 0;
3987 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003988 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003989 p++;
3990 if (*p == '\0')
3991 break;
3992 if (nb_args >= MAX_ARGS)
3993 break;
3994 ret = get_str(buf, sizeof(buf), &p);
3995 args[nb_args] = qemu_strdup(buf);
3996 nb_args++;
3997 if (ret < 0)
3998 break;
3999 }
4000 *pnb_args = nb_args;
4001}
4002
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004003static const char *next_arg_type(const char *typestr)
4004{
4005 const char *p = strchr(typestr, ':');
4006 return (p != NULL ? ++p : typestr);
4007}
4008
aliguori4c36ba32009-03-05 23:01:37 +00004009static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004010{
4011 const char *cmdname;
4012 char *args[MAX_ARGS];
4013 int nb_args, i, len;
4014 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004015 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004016 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004017
4018 parse_cmdline(cmdline, &nb_args, args);
4019#ifdef DEBUG_COMPLETION
4020 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004021 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004022 }
4023#endif
4024
4025 /* if the line ends with a space, it means we want to complete the
4026 next arg */
4027 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004028 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004029 if (nb_args >= MAX_ARGS)
4030 return;
4031 args[nb_args++] = qemu_strdup("");
4032 }
4033 if (nb_args <= 1) {
4034 /* command completion */
4035 if (nb_args == 0)
4036 cmdname = "";
4037 else
4038 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004039 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004040 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004041 cmd_completion(cmdname, cmd->name);
4042 }
4043 } else {
4044 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004045 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004046 if (compare_cmd(args[0], cmd->name))
4047 goto found;
4048 }
4049 return;
4050 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004051 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004052 for(i = 0; i < nb_args - 2; i++) {
4053 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004054 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004055 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004056 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004057 }
4058 }
4059 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004060 if (*ptype == '-' && ptype[1] != '\0') {
4061 ptype += 2;
4062 }
bellard81d09122004-07-14 17:21:37 +00004063 switch(*ptype) {
4064 case 'F':
4065 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004066 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004067 file_completion(str);
4068 break;
4069 case 'B':
4070 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004071 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004072 bdrv_iterate(block_completion_it, (void *)str);
4073 break;
bellard7fe48482004-10-09 18:08:01 +00004074 case 's':
4075 /* XXX: more generic ? */
4076 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004077 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004078 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4079 cmd_completion(str, cmd->name);
4080 }
bellard64866c32006-05-07 18:03:31 +00004081 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004082 char *sep = strrchr(str, '-');
4083 if (sep)
4084 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004085 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004086 for(key = key_defs; key->name != NULL; key++) {
4087 cmd_completion(str, key->name);
4088 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004089 } else if (!strcmp(cmd->name, "help|?")) {
4090 readline_set_completion_index(cur_mon->rs, strlen(str));
4091 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4092 cmd_completion(str, cmd->name);
4093 }
bellard7fe48482004-10-09 18:08:01 +00004094 }
4095 break;
bellard81d09122004-07-14 17:21:37 +00004096 default:
4097 break;
4098 }
4099 }
4100 for(i = 0; i < nb_args; i++)
4101 qemu_free(args[i]);
4102}
4103
aliguori731b0362009-03-05 23:01:42 +00004104static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004105{
aliguori731b0362009-03-05 23:01:42 +00004106 Monitor *mon = opaque;
4107
Jan Kiszkac62313b2009-12-04 14:05:29 +01004108 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004109}
4110
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004111typedef struct CmdArgs {
4112 QString *name;
4113 int type;
4114 int flag;
4115 int optional;
4116} CmdArgs;
4117
4118static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4119{
4120 if (!cmd_args->optional) {
4121 qemu_error_new(QERR_MISSING_PARAMETER, name);
4122 return -1;
4123 }
4124
4125 if (cmd_args->type == '-') {
4126 /* handlers expect a value, they need to be changed */
4127 qdict_put(args, name, qint_from_int(0));
4128 }
4129
4130 return 0;
4131}
4132
4133static int check_arg(const CmdArgs *cmd_args, QDict *args)
4134{
4135 QObject *value;
4136 const char *name;
4137
4138 name = qstring_get_str(cmd_args->name);
4139
4140 if (!args) {
4141 return check_opt(cmd_args, name, args);
4142 }
4143
4144 value = qdict_get(args, name);
4145 if (!value) {
4146 return check_opt(cmd_args, name, args);
4147 }
4148
4149 switch (cmd_args->type) {
4150 case 'F':
4151 case 'B':
4152 case 's':
4153 if (qobject_type(value) != QTYPE_QSTRING) {
4154 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4155 return -1;
4156 }
4157 break;
4158 case '/': {
4159 int i;
4160 const char *keys[] = { "count", "format", "size", NULL };
4161
4162 for (i = 0; keys[i]; i++) {
4163 QObject *obj = qdict_get(args, keys[i]);
4164 if (!obj) {
4165 qemu_error_new(QERR_MISSING_PARAMETER, name);
4166 return -1;
4167 }
4168 if (qobject_type(obj) != QTYPE_QINT) {
4169 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4170 return -1;
4171 }
4172 }
4173 break;
4174 }
4175 case 'i':
4176 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004177 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004178 if (qobject_type(value) != QTYPE_QINT) {
4179 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4180 return -1;
4181 }
4182 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004183 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004184 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004185 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4186 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4187 return -1;
4188 }
4189 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004190 case '-':
4191 if (qobject_type(value) != QTYPE_QINT &&
4192 qobject_type(value) != QTYPE_QBOOL) {
4193 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4194 return -1;
4195 }
4196 if (qobject_type(value) == QTYPE_QBOOL) {
4197 /* handlers expect a QInt, they need to be changed */
4198 qdict_put(args, name,
4199 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4200 }
4201 break;
4202 default:
4203 /* impossible */
4204 abort();
4205 }
4206
4207 return 0;
4208}
4209
4210static void cmd_args_init(CmdArgs *cmd_args)
4211{
4212 cmd_args->name = qstring_new();
4213 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4214}
4215
4216/*
4217 * This is not trivial, we have to parse Monitor command's argument
4218 * type syntax to be able to check the arguments provided by clients.
4219 *
4220 * In the near future we will be using an array for that and will be
4221 * able to drop all this parsing...
4222 */
4223static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4224{
4225 int err;
4226 const char *p;
4227 CmdArgs cmd_args;
4228
Blue Swirldd5121b2009-12-04 20:52:02 +00004229 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004230 return (qdict_size(args) == 0 ? 0 : -1);
4231 }
4232
4233 err = 0;
4234 cmd_args_init(&cmd_args);
4235
4236 for (p = cmd->args_type;; p++) {
4237 if (*p == ':') {
4238 cmd_args.type = *++p;
4239 p++;
4240 if (cmd_args.type == '-') {
4241 cmd_args.flag = *p++;
4242 cmd_args.optional = 1;
4243 } else if (*p == '?') {
4244 cmd_args.optional = 1;
4245 p++;
4246 }
4247
4248 assert(*p == ',' || *p == '\0');
4249 err = check_arg(&cmd_args, args);
4250
4251 QDECREF(cmd_args.name);
4252 cmd_args_init(&cmd_args);
4253
4254 if (err < 0) {
4255 break;
4256 }
4257 } else {
4258 qstring_append_chr(cmd_args.name, *p);
4259 }
4260
4261 if (*p == '\0') {
4262 break;
4263 }
4264 }
4265
4266 QDECREF(cmd_args.name);
4267 return err;
4268}
4269
Luiz Capitulino09069b12010-02-04 18:10:06 -02004270static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4271{
4272 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4273 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4274}
4275
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004276static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4277{
4278 int err;
4279 QObject *obj;
4280 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004281 const mon_cmd_t *cmd;
4282 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004283 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004284
4285 args = NULL;
4286 qemu_errors_to_mon(mon);
4287
4288 obj = json_parser_parse(tokens, NULL);
4289 if (!obj) {
4290 // FIXME: should be triggered in json_parser_parse()
4291 qemu_error_new(QERR_JSON_PARSING);
4292 goto err_out;
4293 } else if (qobject_type(obj) != QTYPE_QDICT) {
4294 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4295 qobject_decref(obj);
4296 goto err_out;
4297 }
4298
4299 input = qobject_to_qdict(obj);
4300
4301 mon->mc->id = qdict_get(input, "id");
4302 qobject_incref(mon->mc->id);
4303
4304 obj = qdict_get(input, "execute");
4305 if (!obj) {
4306 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4307 goto err_input;
4308 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4309 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4310 goto err_input;
4311 }
4312
4313 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004314
Luiz Capitulino09069b12010-02-04 18:10:06 -02004315 if (invalid_qmp_mode(mon, cmd_name)) {
4316 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4317 goto err_input;
4318 }
4319
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004320 /*
4321 * XXX: We need this special case until we get info handlers
4322 * converted into 'query-' commands
4323 */
4324 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004325 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4326 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004327 } else if (strstart(cmd_name, "query-", &info_item)) {
4328 cmd = monitor_find_command("info");
4329 qdict_put_obj(input, "arguments",
4330 qobject_from_jsonf("{ 'item': %s }", info_item));
4331 } else {
4332 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004333 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004334 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4335 goto err_input;
4336 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004337 }
4338
4339 obj = qdict_get(input, "arguments");
4340 if (!obj) {
4341 args = qdict_new();
4342 } else {
4343 args = qobject_to_qdict(obj);
4344 QINCREF(args);
4345 }
4346
4347 QDECREF(input);
4348
4349 err = monitor_check_qmp_args(cmd, args);
4350 if (err < 0) {
4351 goto err_out;
4352 }
4353
Adam Litke940cc302010-01-25 12:18:44 -06004354 if (monitor_handler_is_async(cmd)) {
4355 qmp_async_cmd_handler(mon, cmd, args);
4356 } else {
4357 monitor_call_handler(mon, cmd, args);
4358 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004359 goto out;
4360
4361err_input:
4362 QDECREF(input);
4363err_out:
4364 monitor_protocol_emitter(mon, NULL);
4365out:
4366 QDECREF(args);
4367 qemu_errors_to_previous();
4368}
4369
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004370/**
4371 * monitor_control_read(): Read and handle QMP input
4372 */
4373static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4374{
4375 Monitor *old_mon = cur_mon;
4376
4377 cur_mon = opaque;
4378
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004379 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004380
4381 cur_mon = old_mon;
4382}
4383
aliguori731b0362009-03-05 23:01:42 +00004384static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004385{
aliguori731b0362009-03-05 23:01:42 +00004386 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004387 int i;
aliguori376253e2009-03-05 23:01:23 +00004388
aliguori731b0362009-03-05 23:01:42 +00004389 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004390
aliguoricde76ee2009-03-05 23:01:51 +00004391 if (cur_mon->rs) {
4392 for (i = 0; i < size; i++)
4393 readline_handle_byte(cur_mon->rs, buf[i]);
4394 } else {
4395 if (size == 0 || buf[size - 1] != 0)
4396 monitor_printf(cur_mon, "corrupted command\n");
4397 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004398 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004399 }
aliguori731b0362009-03-05 23:01:42 +00004400
4401 cur_mon = old_mon;
4402}
aliguorid8f44602008-10-06 13:52:44 +00004403
aliguori376253e2009-03-05 23:01:23 +00004404static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004405{
aliguori731b0362009-03-05 23:01:42 +00004406 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004407 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004408 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004409}
4410
aliguoricde76ee2009-03-05 23:01:51 +00004411int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004412{
aliguoricde76ee2009-03-05 23:01:51 +00004413 if (!mon->rs)
4414 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004415 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004416 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004417}
4418
aliguori376253e2009-03-05 23:01:23 +00004419void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004420{
aliguoricde76ee2009-03-05 23:01:51 +00004421 if (!mon->rs)
4422 return;
aliguori731b0362009-03-05 23:01:42 +00004423 if (--mon->suspend_cnt == 0)
4424 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004425}
4426
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004427static QObject *get_qmp_greeting(void)
4428{
4429 QObject *ver;
4430
4431 do_info_version(NULL, &ver);
4432 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4433}
4434
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004435/**
4436 * monitor_control_event(): Print QMP gretting
4437 */
4438static void monitor_control_event(void *opaque, int event)
4439{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004440 QObject *data;
4441 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004442
Luiz Capitulino47116d12010-02-08 17:01:30 -02004443 switch (event) {
4444 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004445 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004446 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004447 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004448 monitor_json_emitter(mon, data);
4449 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004450 break;
4451 case CHR_EVENT_CLOSED:
4452 json_message_parser_destroy(&mon->mc->parser);
4453 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004454 }
4455}
4456
aliguori731b0362009-03-05 23:01:42 +00004457static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004458{
aliguori376253e2009-03-05 23:01:23 +00004459 Monitor *mon = opaque;
4460
aliguori2724b182009-03-05 23:01:47 +00004461 switch (event) {
4462 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004463 mon->mux_out = 0;
4464 if (mon->reset_seen) {
4465 readline_restart(mon->rs);
4466 monitor_resume(mon);
4467 monitor_flush(mon);
4468 } else {
4469 mon->suspend_cnt = 0;
4470 }
aliguori2724b182009-03-05 23:01:47 +00004471 break;
ths86e94de2007-01-05 22:01:59 +00004472
aliguori2724b182009-03-05 23:01:47 +00004473 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004474 if (mon->reset_seen) {
4475 if (mon->suspend_cnt == 0) {
4476 monitor_printf(mon, "\n");
4477 }
4478 monitor_flush(mon);
4479 monitor_suspend(mon);
4480 } else {
4481 mon->suspend_cnt++;
4482 }
4483 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004484 break;
4485
Amit Shahb6b8df52009-10-07 18:31:16 +05304486 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004487 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4488 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004489 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004490 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004491 }
4492 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004493 break;
4494 }
ths86e94de2007-01-05 22:01:59 +00004495}
4496
aliguori76655d62009-03-06 20:27:37 +00004497
4498/*
4499 * Local variables:
4500 * c-indent-level: 4
4501 * c-basic-offset: 4
4502 * tab-width: 8
4503 * End:
4504 */
4505
aliguori731b0362009-03-05 23:01:42 +00004506void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004507{
aliguori731b0362009-03-05 23:01:42 +00004508 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004509 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004510
4511 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004512 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004513 is_first_init = 0;
4514 }
aliguori87127162009-03-05 23:01:29 +00004515
4516 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004517
aliguori87127162009-03-05 23:01:29 +00004518 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004519 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004520 if (flags & MONITOR_USE_READLINE) {
4521 mon->rs = readline_init(mon, monitor_find_completion);
4522 monitor_read_command(mon, 0);
4523 }
aliguori87127162009-03-05 23:01:29 +00004524
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004525 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004526 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004527 /* Control mode requires special handlers */
4528 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4529 monitor_control_event, mon);
4530 } else {
4531 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4532 monitor_event, mon);
4533 }
aliguori87127162009-03-05 23:01:29 +00004534
Blue Swirl72cf2d42009-09-12 07:36:22 +00004535 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004536 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004537 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004538}
4539
aliguori376253e2009-03-05 23:01:23 +00004540static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004541{
aliguoribb5fc202009-03-05 23:01:15 +00004542 BlockDriverState *bs = opaque;
4543 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004544
aliguoribb5fc202009-03-05 23:01:15 +00004545 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004546 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004547 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004548 }
aliguori731b0362009-03-05 23:01:42 +00004549 if (mon->password_completion_cb)
4550 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004551
aliguori731b0362009-03-05 23:01:42 +00004552 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004553}
aliguoric0f4ce72009-03-05 23:01:01 +00004554
aliguori376253e2009-03-05 23:01:23 +00004555void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004556 BlockDriverCompletionFunc *completion_cb,
4557 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004558{
aliguoricde76ee2009-03-05 23:01:51 +00004559 int err;
4560
aliguoribb5fc202009-03-05 23:01:15 +00004561 if (!bdrv_key_required(bs)) {
4562 if (completion_cb)
4563 completion_cb(opaque, 0);
4564 return;
4565 }
aliguoric0f4ce72009-03-05 23:01:01 +00004566
Luiz Capitulino94171e12009-12-07 21:37:00 +01004567 if (monitor_ctrl_mode(mon)) {
4568 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4569 return;
4570 }
4571
aliguori376253e2009-03-05 23:01:23 +00004572 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4573 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004574
aliguori731b0362009-03-05 23:01:42 +00004575 mon->password_completion_cb = completion_cb;
4576 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004577
aliguoricde76ee2009-03-05 23:01:51 +00004578 err = monitor_read_password(mon, bdrv_password_cb, bs);
4579
4580 if (err && completion_cb)
4581 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004582}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004583
4584typedef struct QemuErrorSink QemuErrorSink;
4585struct QemuErrorSink {
4586 enum {
4587 ERR_SINK_FILE,
4588 ERR_SINK_MONITOR,
4589 } dest;
4590 union {
4591 FILE *fp;
4592 Monitor *mon;
4593 };
4594 QemuErrorSink *previous;
4595};
4596
Blue Swirl528e93a2009-08-31 15:14:40 +00004597static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004598
4599void qemu_errors_to_file(FILE *fp)
4600{
4601 QemuErrorSink *sink;
4602
4603 sink = qemu_mallocz(sizeof(*sink));
4604 sink->dest = ERR_SINK_FILE;
4605 sink->fp = fp;
4606 sink->previous = qemu_error_sink;
4607 qemu_error_sink = sink;
4608}
4609
4610void qemu_errors_to_mon(Monitor *mon)
4611{
4612 QemuErrorSink *sink;
4613
4614 sink = qemu_mallocz(sizeof(*sink));
4615 sink->dest = ERR_SINK_MONITOR;
4616 sink->mon = mon;
4617 sink->previous = qemu_error_sink;
4618 qemu_error_sink = sink;
4619}
4620
4621void qemu_errors_to_previous(void)
4622{
4623 QemuErrorSink *sink;
4624
4625 assert(qemu_error_sink != NULL);
4626 sink = qemu_error_sink;
4627 qemu_error_sink = sink->previous;
4628 qemu_free(sink);
4629}
4630
4631void qemu_error(const char *fmt, ...)
4632{
4633 va_list args;
4634
4635 assert(qemu_error_sink != NULL);
4636 switch (qemu_error_sink->dest) {
4637 case ERR_SINK_FILE:
4638 va_start(args, fmt);
4639 vfprintf(qemu_error_sink->fp, fmt, args);
4640 va_end(args);
4641 break;
4642 case ERR_SINK_MONITOR:
4643 va_start(args, fmt);
4644 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4645 va_end(args);
4646 break;
4647 }
4648}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004649
4650void qemu_error_internal(const char *file, int linenr, const char *func,
4651 const char *fmt, ...)
4652{
4653 va_list va;
4654 QError *qerror;
4655
4656 assert(qemu_error_sink != NULL);
4657
4658 va_start(va, fmt);
4659 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4660 va_end(va);
4661
4662 switch (qemu_error_sink->dest) {
4663 case ERR_SINK_FILE:
4664 qerror_print(qerror);
4665 QDECREF(qerror);
4666 break;
4667 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004668 /* report only the first error */
4669 if (!qemu_error_sink->mon->error) {
4670 qemu_error_sink->mon->error = qerror;
4671 } else {
4672 /* XXX: warn the programmer */
4673 QDECREF(qerror);
4674 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004675 break;
4676 }
4677}