blob: baedc34a3f0ee5a49c009ce9d5cb98aa43f34a36 [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 Capitulino4fdc94b2010-02-10 23:50:00 -0200559static int 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);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200577 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200578 }
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);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200609 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200610 } else {
611 cmd->mhandler.info(mon);
612 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300613 }
614
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200615 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300616
617help:
618 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200619 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000620}
621
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200622static void do_info_version_print(Monitor *mon, const QObject *data)
623{
624 QDict *qdict;
625
626 qdict = qobject_to_qdict(data);
627
628 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
629 qdict_get_str(qdict, "package"));
630}
631
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300632/**
633 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200634 *
635 * Return a QDict with the following information:
636 *
637 * - "qemu": QEMU's version
638 * - "package": package's version
639 *
640 * Example:
641 *
642 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300643 */
644static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000645{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200646 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
647 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000648}
649
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200650static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000651{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200652 QDict *qdict;
653
654 qdict = qobject_to_qdict(data);
655 if (qdict_size(qdict) == 0) {
656 return;
657 }
658
659 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
660}
661
662/**
663 * do_info_name(): Show VM name
664 *
665 * Return a QDict with the following information:
666 *
667 * - "name": VM's name (optional)
668 *
669 * Example:
670 *
671 * { "name": "qemu-name" }
672 */
673static void do_info_name(Monitor *mon, QObject **ret_data)
674{
675 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
676 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000677}
678
Luiz Capitulino1a728672009-12-10 17:15:56 -0200679static QObject *get_cmd_dict(const char *name)
680{
681 const char *p;
682
683 /* Remove '|' from some commands */
684 p = strchr(name, '|');
685 if (p) {
686 p++;
687 } else {
688 p = name;
689 }
690
691 return qobject_from_jsonf("{ 'name': %s }", p);
692}
693
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200694/**
695 * do_info_commands(): List QMP available commands
696 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200697 * Each command is represented by a QDict, the returned QObject is a QList
698 * of all commands.
699 *
700 * The QDict contains:
701 *
702 * - "name": command's name
703 *
704 * Example:
705 *
706 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200707 */
708static void do_info_commands(Monitor *mon, QObject **ret_data)
709{
710 QList *cmd_list;
711 const mon_cmd_t *cmd;
712
713 cmd_list = qlist_new();
714
715 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
716 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200717 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200718 }
719 }
720
721 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
722 if (monitor_handler_ported(cmd)) {
723 char buf[128];
724 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200725 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200726 }
727 }
728
729 *ret_data = QOBJECT(cmd_list);
730}
731
aurel32bf4f74c2008-12-18 22:42:34 +0000732#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200733static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000734{
aliguori376253e2009-03-05 23:01:23 +0000735 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200736 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
737 "enabled" : "disabled");
738}
739
740/**
741 * do_info_hpet(): Show HPET state
742 *
743 * Return a QDict with the following information:
744 *
745 * - "enabled": true if hpet if enabled, false otherwise
746 *
747 * Example:
748 *
749 * { "enabled": true }
750 */
751static void do_info_hpet(Monitor *mon, QObject **ret_data)
752{
753 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000754}
aurel32bf4f74c2008-12-18 22:42:34 +0000755#endif
aliguori16b29ae2008-12-17 23:28:44 +0000756
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200757static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000758{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200759 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
760}
761
762/**
763 * do_info_uuid(): Show VM UUID
764 *
765 * Return a QDict with the following information:
766 *
767 * - "UUID": Universally Unique Identifier
768 *
769 * Example:
770 *
771 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
772 */
773static void do_info_uuid(Monitor *mon, QObject **ret_data)
774{
775 char uuid[64];
776
777 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000778 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
779 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
780 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
781 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200782 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000783}
784
bellard6a00d602005-11-21 23:25:50 +0000785/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000786static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000787{
788 CPUState *env;
789
790 for(env = first_cpu; env != NULL; env = env->next_cpu) {
791 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000792 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000793 return 0;
794 }
795 }
796 return -1;
797}
798
pbrook9596ebb2007-11-18 01:44:38 +0000799static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000800{
aliguori731b0362009-03-05 23:01:42 +0000801 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000802 mon_set_cpu(0);
803 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300804 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000805 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000806}
807
aliguori376253e2009-03-05 23:01:23 +0000808static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000809{
bellard6a00d602005-11-21 23:25:50 +0000810 CPUState *env;
811 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000812#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000813 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000814 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000815#else
aliguori376253e2009-03-05 23:01:23 +0000816 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000817 0);
bellard9307c4c2004-04-04 12:57:25 +0000818#endif
819}
820
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300821static void print_cpu_iter(QObject *obj, void *opaque)
822{
823 QDict *cpu;
824 int active = ' ';
825 Monitor *mon = opaque;
826
827 assert(qobject_type(obj) == QTYPE_QDICT);
828 cpu = qobject_to_qdict(obj);
829
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200830 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300831 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200832 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300833
834 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
835
836#if defined(TARGET_I386)
837 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
838 (target_ulong) qdict_get_int(cpu, "pc"));
839#elif defined(TARGET_PPC)
840 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
841 (target_long) qdict_get_int(cpu, "nip"));
842#elif defined(TARGET_SPARC)
843 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
844 (target_long) qdict_get_int(cpu, "pc"));
845 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
846 (target_long) qdict_get_int(cpu, "npc"));
847#elif defined(TARGET_MIPS)
848 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
849 (target_long) qdict_get_int(cpu, "PC"));
850#endif
851
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200852 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300853 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200854 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300855
856 monitor_printf(mon, "\n");
857}
858
859static void monitor_print_cpus(Monitor *mon, const QObject *data)
860{
861 QList *cpu_list;
862
863 assert(qobject_type(data) == QTYPE_QLIST);
864 cpu_list = qobject_to_qlist(data);
865 qlist_iter(cpu_list, print_cpu_iter, mon);
866}
867
868/**
869 * do_info_cpus(): Show CPU information
870 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200871 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300872 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200873 * - "cpu": CPU index
874 * - "current": true if this is the current CPU, false otherwise
875 * - "halted": true if the cpu is halted, false otherwise
876 * - Current program counter. The key's name depends on the architecture:
877 * "pc": i386/x86)64
878 * "nip": PPC
879 * "pc" and "npc": sparc
880 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200882 * Example:
883 *
884 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
885 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300886 */
887static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000888{
889 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890 QList *cpu_list;
891
892 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000893
894 /* just to set the default cpu if not already done */
895 mon_get_cpu();
896
897 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200898 QDict *cpu;
899 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300900
Avi Kivity4c0960c2009-08-17 23:19:53 +0300901 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300902
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200903 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
904 env->cpu_index, env == mon->mon_cpu,
905 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200906
907 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300908
bellard6a00d602005-11-21 23:25:50 +0000909#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300910 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000911#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300912 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000913#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300914 qdict_put(cpu, "pc", qint_from_int(env->pc));
915 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000916#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000918#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300919
920 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000921 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300922
923 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000924}
925
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200926static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000927{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300928 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200929 if (mon_set_cpu(index) < 0) {
Markus Armbrustercc0c4182010-01-29 10:34:00 +0100930 qemu_error_new(QERR_INVALID_PARAMETER, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200931 return -1;
932 }
933 return 0;
bellard6a00d602005-11-21 23:25:50 +0000934}
935
aliguori376253e2009-03-05 23:01:23 +0000936static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000937{
aliguori376253e2009-03-05 23:01:23 +0000938 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000939}
940
aliguori376253e2009-03-05 23:01:23 +0000941static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000942{
943 int i;
bellard7e2515e2004-08-01 21:52:19 +0000944 const char *str;
ths3b46e622007-09-17 08:09:54 +0000945
aliguoricde76ee2009-03-05 23:01:51 +0000946 if (!mon->rs)
947 return;
bellard7e2515e2004-08-01 21:52:19 +0000948 i = 0;
949 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000950 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000951 if (!str)
952 break;
aliguori376253e2009-03-05 23:01:23 +0000953 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000954 i++;
bellardaa455482004-04-04 13:07:25 +0000955 }
956}
957
j_mayer76a66252007-03-07 08:32:30 +0000958#if defined(TARGET_PPC)
959/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000960static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000961{
962 CPUState *env;
963
964 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000965 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000966}
967#endif
968
Luiz Capitulinob223f352009-10-07 13:41:56 -0300969/**
970 * do_quit(): Quit QEMU execution
971 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200972static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000973{
974 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200975 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000976}
977
aliguori376253e2009-03-05 23:01:23 +0000978static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000979{
980 if (bdrv_is_inserted(bs)) {
981 if (!force) {
982 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100983 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
984 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000985 return -1;
986 }
987 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100988 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000989 return -1;
990 }
991 }
992 bdrv_close(bs);
993 }
994 return 0;
995}
996
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -0200997static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000998{
999 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001000 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -02001001 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +00001002
bellard9307c4c2004-04-04 12:57:25 +00001003 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +00001004 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001005 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001006 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001007 }
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001008 return eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001009}
1010
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001011static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001012 QObject **ret_data)
1013{
1014 BlockDriverState *bs;
1015
1016 bs = bdrv_find(qdict_get_str(qdict, "device"));
1017 if (!bs) {
1018 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001019 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001020 }
1021
1022 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
1023 qemu_error_new(QERR_INVALID_PASSWORD);
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001024 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001025 }
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001026
1027 return 0;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001028}
1029
aliguori376253e2009-03-05 23:01:23 +00001030static void do_change_block(Monitor *mon, const char *device,
1031 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001032{
1033 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001034 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001035
bellard9307c4c2004-04-04 12:57:25 +00001036 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001037 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001038 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001039 return;
1040 }
aurel322ecea9b2008-06-18 22:10:01 +00001041 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001042 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001043 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001044 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001045 return;
1046 }
1047 }
aliguori376253e2009-03-05 23:01:23 +00001048 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001049 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001050 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001051 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001052}
1053
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001054static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001055{
1056 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001057 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001058
Markus Armbruster2895e072009-12-07 21:37:01 +01001059}
1060
1061static void change_vnc_password_cb(Monitor *mon, const char *password,
1062 void *opaque)
1063{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001064 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001065 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001066}
1067
aliguori376253e2009-03-05 23:01:23 +00001068static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001069{
ths70848512007-08-25 01:37:05 +00001070 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001071 strcmp(target, "password") == 0) {
1072 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001073 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001074 strncpy(password, arg, sizeof(password));
1075 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001076 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001077 } else {
aliguori376253e2009-03-05 23:01:23 +00001078 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001079 }
ths70848512007-08-25 01:37:05 +00001080 } else {
aliguori28a76be2009-03-06 20:27:40 +00001081 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001082 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001083 }
thse25a5822007-08-25 01:36:20 +00001084}
1085
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001086/**
1087 * do_change(): Change a removable medium, or VNC configuration
1088 */
1089static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001090{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001091 const char *device = qdict_get_str(qdict, "device");
1092 const char *target = qdict_get_str(qdict, "target");
1093 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001094 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001095 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001096 } else {
aliguori28a76be2009-03-06 20:27:40 +00001097 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001098 }
1099}
1100
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001101static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001102{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001103 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001104}
1105
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001106static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001107{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001108 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001109}
1110
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001111static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001112{
1113 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001114 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001115
bellard9307c4c2004-04-04 12:57:25 +00001116 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001117 mask = 0;
1118 } else {
bellard9307c4c2004-04-04 12:57:25 +00001119 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001120 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001121 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001122 return;
1123 }
1124 }
1125 cpu_set_log(mask);
1126}
1127
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001128static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001129{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001130 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001131 if (!option || !strcmp(option, "on")) {
1132 singlestep = 1;
1133 } else if (!strcmp(option, "off")) {
1134 singlestep = 0;
1135 } else {
1136 monitor_printf(mon, "unexpected option %s\n", option);
1137 }
1138}
1139
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001140/**
1141 * do_stop(): Stop VM execution
1142 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001143static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001144{
1145 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001146 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001147}
1148
aliguoribb5fc202009-03-05 23:01:15 +00001149static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001150
aliguori376253e2009-03-05 23:01:23 +00001151struct bdrv_iterate_context {
1152 Monitor *mon;
1153 int err;
1154};
aliguoric0f4ce72009-03-05 23:01:01 +00001155
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001156/**
1157 * do_cont(): Resume emulation.
1158 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001159static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001160{
1161 struct bdrv_iterate_context context = { mon, 0 };
1162
1163 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001164 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001165 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001166 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001167 return 0;
1168 } else {
1169 return -1;
1170 }
bellard8a7ddc32004-03-31 19:00:16 +00001171}
1172
aliguoribb5fc202009-03-05 23:01:15 +00001173static void bdrv_key_cb(void *opaque, int err)
1174{
aliguori376253e2009-03-05 23:01:23 +00001175 Monitor *mon = opaque;
1176
aliguoribb5fc202009-03-05 23:01:15 +00001177 /* another key was set successfully, retry to continue */
1178 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001179 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001180}
1181
1182static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1183{
aliguori376253e2009-03-05 23:01:23 +00001184 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001185
aliguori376253e2009-03-05 23:01:23 +00001186 if (!context->err && bdrv_key_required(bs)) {
1187 context->err = -EBUSY;
1188 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1189 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001190 }
1191}
1192
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001194{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001195 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001196 if (!device)
1197 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1198 if (gdbserver_start(device) < 0) {
1199 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1200 device);
1201 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001202 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001203 } else {
aliguori59030a82009-04-05 18:43:41 +00001204 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1205 device);
bellard8a7ddc32004-03-31 19:00:16 +00001206 }
1207}
1208
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001209static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001210{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001211 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001212 if (select_watchdog_action(action) == -1) {
1213 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1214 }
1215}
1216
aliguori376253e2009-03-05 23:01:23 +00001217static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001218{
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001220 switch(c) {
1221 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001222 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001223 break;
1224 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001226 break;
1227 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001228 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001232 break;
1233 default:
1234 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001236 } else {
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001238 }
1239 break;
1240 }
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001242}
1243
aliguori376253e2009-03-05 23:01:23 +00001244static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001245 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001246{
bellard6a00d602005-11-21 23:25:50 +00001247 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001248 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001249 uint8_t buf[16];
1250 uint64_t v;
1251
1252 if (format == 'i') {
1253 int flags;
1254 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001255 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001256 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001257 return;
bellard9307c4c2004-04-04 12:57:25 +00001258#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001259 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001260 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001261 } else if (wsize == 4) {
1262 flags = 0;
1263 } else {
bellard6a15fd12006-04-12 21:07:07 +00001264 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001265 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001266 if (env) {
1267#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001268 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001269 (env->segs[R_CS].flags & DESC_L_MASK))
1270 flags = 2;
1271 else
1272#endif
1273 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1274 flags = 1;
1275 }
bellard4c27ba22004-04-25 18:05:08 +00001276 }
1277#endif
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001279 return;
1280 }
1281
1282 len = wsize * count;
1283 if (wsize == 1)
1284 line_size = 8;
1285 else
1286 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001287 max_digits = 0;
1288
1289 switch(format) {
1290 case 'o':
1291 max_digits = (wsize * 8 + 2) / 3;
1292 break;
1293 default:
1294 case 'x':
1295 max_digits = (wsize * 8) / 4;
1296 break;
1297 case 'u':
1298 case 'd':
1299 max_digits = (wsize * 8 * 10 + 32) / 33;
1300 break;
1301 case 'c':
1302 wsize = 1;
1303 break;
1304 }
1305
1306 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001307 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001309 else
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001311 l = len;
1312 if (l > line_size)
1313 l = line_size;
1314 if (is_physical) {
1315 cpu_physical_memory_rw(addr, buf, l, 0);
1316 } else {
bellard6a00d602005-11-21 23:25:50 +00001317 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001318 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001320 break;
1321 }
bellard9307c4c2004-04-04 12:57:25 +00001322 }
ths5fafdf22007-09-16 21:08:06 +00001323 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001324 while (i < l) {
1325 switch(wsize) {
1326 default:
1327 case 1:
1328 v = ldub_raw(buf + i);
1329 break;
1330 case 2:
1331 v = lduw_raw(buf + i);
1332 break;
1333 case 4:
bellard92a31b12005-02-10 22:00:52 +00001334 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001335 break;
1336 case 8:
1337 v = ldq_raw(buf + i);
1338 break;
1339 }
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001341 switch(format) {
1342 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001344 break;
1345 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001346 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001347 break;
1348 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001350 break;
1351 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001353 break;
1354 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001356 break;
1357 }
1358 i += wsize;
1359 }
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001361 addr += l;
1362 len -= l;
1363 }
1364}
1365
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001366static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001367{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001368 int count = qdict_get_int(qdict, "count");
1369 int format = qdict_get_int(qdict, "format");
1370 int size = qdict_get_int(qdict, "size");
1371 target_long addr = qdict_get_int(qdict, "addr");
1372
aliguori376253e2009-03-05 23:01:23 +00001373 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001374}
1375
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001376static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001377{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001378 int count = qdict_get_int(qdict, "count");
1379 int format = qdict_get_int(qdict, "format");
1380 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001381 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001382
aliguori376253e2009-03-05 23:01:23 +00001383 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001384}
1385
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001386static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001387{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001388 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001389 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001390
blueswir17743e582007-09-24 18:39:04 +00001391#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001392 switch(format) {
1393 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001398 break;
1399 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001401 break;
1402 default:
1403 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001405 break;
1406 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001408 break;
1409 }
bellard92a31b12005-02-10 22:00:52 +00001410#else
1411 switch(format) {
1412 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001414 break;
1415 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001417 break;
1418 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001419 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001420 break;
1421 default:
1422 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001423 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001424 break;
1425 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001426 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001427 break;
1428 }
1429#endif
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001431}
1432
Luiz Capitulino98696222010-02-10 23:49:58 -02001433static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001434{
1435 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001436 uint32_t size = qdict_get_int(qdict, "size");
1437 const char *filename = qdict_get_str(qdict, "filename");
1438 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001439 uint32_t l;
1440 CPUState *env;
1441 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001442 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001443
1444 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001445
1446 f = fopen(filename, "wb");
1447 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001448 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001449 return -1;
bellardb371dc52007-01-03 15:20:39 +00001450 }
1451 while (size != 0) {
1452 l = sizeof(buf);
1453 if (l > size)
1454 l = size;
1455 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001456 if (fwrite(buf, 1, l, f) != l) {
1457 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1458 goto exit;
1459 }
bellardb371dc52007-01-03 15:20:39 +00001460 addr += l;
1461 size -= l;
1462 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001463
1464 ret = 0;
1465
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001466exit:
bellardb371dc52007-01-03 15:20:39 +00001467 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001468 return ret;
bellardb371dc52007-01-03 15:20:39 +00001469}
1470
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001471static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001472 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001473{
1474 FILE *f;
1475 uint32_t l;
1476 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001477 uint32_t size = qdict_get_int(qdict, "size");
1478 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001479 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001480 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001481
1482 f = fopen(filename, "wb");
1483 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001484 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001485 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001486 }
1487 while (size != 0) {
1488 l = sizeof(buf);
1489 if (l > size)
1490 l = size;
1491 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001492 if (fwrite(buf, 1, l, f) != l) {
1493 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1494 goto exit;
1495 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001496 fflush(f);
1497 addr += l;
1498 size -= l;
1499 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001500
1501 ret = 0;
1502
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001503exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001504 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001505 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001506}
1507
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001508static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001509{
1510 uint32_t addr;
1511 uint8_t buf[1];
1512 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001513 uint32_t start = qdict_get_int(qdict, "start");
1514 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001515
1516 sum = 0;
1517 for(addr = start; addr < (start + size); addr++) {
1518 cpu_physical_memory_rw(addr, buf, 1, 0);
1519 /* BSD sum algorithm ('sum' Unix command) */
1520 sum = (sum >> 1) | (sum << 15);
1521 sum += buf[0];
1522 }
aliguori376253e2009-03-05 23:01:23 +00001523 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001524}
1525
bellarda3a91a32004-06-04 11:06:21 +00001526typedef struct {
1527 int keycode;
1528 const char *name;
1529} KeyDef;
1530
1531static const KeyDef key_defs[] = {
1532 { 0x2a, "shift" },
1533 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001534
bellarda3a91a32004-06-04 11:06:21 +00001535 { 0x38, "alt" },
1536 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001537 { 0x64, "altgr" },
1538 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001539 { 0x1d, "ctrl" },
1540 { 0x9d, "ctrl_r" },
1541
1542 { 0xdd, "menu" },
1543
1544 { 0x01, "esc" },
1545
1546 { 0x02, "1" },
1547 { 0x03, "2" },
1548 { 0x04, "3" },
1549 { 0x05, "4" },
1550 { 0x06, "5" },
1551 { 0x07, "6" },
1552 { 0x08, "7" },
1553 { 0x09, "8" },
1554 { 0x0a, "9" },
1555 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001556 { 0x0c, "minus" },
1557 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001558 { 0x0e, "backspace" },
1559
1560 { 0x0f, "tab" },
1561 { 0x10, "q" },
1562 { 0x11, "w" },
1563 { 0x12, "e" },
1564 { 0x13, "r" },
1565 { 0x14, "t" },
1566 { 0x15, "y" },
1567 { 0x16, "u" },
1568 { 0x17, "i" },
1569 { 0x18, "o" },
1570 { 0x19, "p" },
1571
1572 { 0x1c, "ret" },
1573
1574 { 0x1e, "a" },
1575 { 0x1f, "s" },
1576 { 0x20, "d" },
1577 { 0x21, "f" },
1578 { 0x22, "g" },
1579 { 0x23, "h" },
1580 { 0x24, "j" },
1581 { 0x25, "k" },
1582 { 0x26, "l" },
1583
1584 { 0x2c, "z" },
1585 { 0x2d, "x" },
1586 { 0x2e, "c" },
1587 { 0x2f, "v" },
1588 { 0x30, "b" },
1589 { 0x31, "n" },
1590 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001591 { 0x33, "comma" },
1592 { 0x34, "dot" },
1593 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001594
balrog4d3b6f62008-02-10 16:33:14 +00001595 { 0x37, "asterisk" },
1596
bellarda3a91a32004-06-04 11:06:21 +00001597 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001598 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001599 { 0x3b, "f1" },
1600 { 0x3c, "f2" },
1601 { 0x3d, "f3" },
1602 { 0x3e, "f4" },
1603 { 0x3f, "f5" },
1604 { 0x40, "f6" },
1605 { 0x41, "f7" },
1606 { 0x42, "f8" },
1607 { 0x43, "f9" },
1608 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001609 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001610 { 0x46, "scroll_lock" },
1611
bellard64866c32006-05-07 18:03:31 +00001612 { 0xb5, "kp_divide" },
1613 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001614 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001615 { 0x4e, "kp_add" },
1616 { 0x9c, "kp_enter" },
1617 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001618 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001619
1620 { 0x52, "kp_0" },
1621 { 0x4f, "kp_1" },
1622 { 0x50, "kp_2" },
1623 { 0x51, "kp_3" },
1624 { 0x4b, "kp_4" },
1625 { 0x4c, "kp_5" },
1626 { 0x4d, "kp_6" },
1627 { 0x47, "kp_7" },
1628 { 0x48, "kp_8" },
1629 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001630
bellarda3a91a32004-06-04 11:06:21 +00001631 { 0x56, "<" },
1632
1633 { 0x57, "f11" },
1634 { 0x58, "f12" },
1635
1636 { 0xb7, "print" },
1637
1638 { 0xc7, "home" },
1639 { 0xc9, "pgup" },
1640 { 0xd1, "pgdn" },
1641 { 0xcf, "end" },
1642
1643 { 0xcb, "left" },
1644 { 0xc8, "up" },
1645 { 0xd0, "down" },
1646 { 0xcd, "right" },
1647
1648 { 0xd2, "insert" },
1649 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001650#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1651 { 0xf0, "stop" },
1652 { 0xf1, "again" },
1653 { 0xf2, "props" },
1654 { 0xf3, "undo" },
1655 { 0xf4, "front" },
1656 { 0xf5, "copy" },
1657 { 0xf6, "open" },
1658 { 0xf7, "paste" },
1659 { 0xf8, "find" },
1660 { 0xf9, "cut" },
1661 { 0xfa, "lf" },
1662 { 0xfb, "help" },
1663 { 0xfc, "meta_l" },
1664 { 0xfd, "meta_r" },
1665 { 0xfe, "compose" },
1666#endif
bellarda3a91a32004-06-04 11:06:21 +00001667 { 0, NULL },
1668};
1669
1670static int get_keycode(const char *key)
1671{
1672 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001673 char *endp;
1674 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001675
1676 for(p = key_defs; p->name != NULL; p++) {
1677 if (!strcmp(key, p->name))
1678 return p->keycode;
1679 }
bellard64866c32006-05-07 18:03:31 +00001680 if (strstart(key, "0x", NULL)) {
1681 ret = strtoul(key, &endp, 0);
1682 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1683 return ret;
1684 }
bellarda3a91a32004-06-04 11:06:21 +00001685 return -1;
1686}
1687
balrogc8256f92008-06-08 22:45:01 +00001688#define MAX_KEYCODES 16
1689static uint8_t keycodes[MAX_KEYCODES];
1690static int nb_pending_keycodes;
1691static QEMUTimer *key_timer;
1692
1693static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001694{
balrogc8256f92008-06-08 22:45:01 +00001695 int keycode;
1696
1697 while (nb_pending_keycodes > 0) {
1698 nb_pending_keycodes--;
1699 keycode = keycodes[nb_pending_keycodes];
1700 if (keycode & 0x80)
1701 kbd_put_keycode(0xe0);
1702 kbd_put_keycode(keycode | 0x80);
1703 }
1704}
1705
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001706static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001707{
balrog3401c0d2008-06-04 10:05:59 +00001708 char keyname_buf[16];
1709 char *separator;
1710 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001711 const char *string = qdict_get_str(qdict, "string");
1712 int has_hold_time = qdict_haskey(qdict, "hold_time");
1713 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001714
balrogc8256f92008-06-08 22:45:01 +00001715 if (nb_pending_keycodes > 0) {
1716 qemu_del_timer(key_timer);
1717 release_keys(NULL);
1718 }
1719 if (!has_hold_time)
1720 hold_time = 100;
1721 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001722 while (1) {
1723 separator = strchr(string, '-');
1724 keyname_len = separator ? separator - string : strlen(string);
1725 if (keyname_len > 0) {
1726 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1727 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001728 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001729 return;
bellarda3a91a32004-06-04 11:06:21 +00001730 }
balrogc8256f92008-06-08 22:45:01 +00001731 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001732 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001733 return;
1734 }
1735 keyname_buf[keyname_len] = 0;
1736 keycode = get_keycode(keyname_buf);
1737 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001738 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001739 return;
1740 }
balrogc8256f92008-06-08 22:45:01 +00001741 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001742 }
balrog3401c0d2008-06-04 10:05:59 +00001743 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001744 break;
balrog3401c0d2008-06-04 10:05:59 +00001745 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001746 }
balrogc8256f92008-06-08 22:45:01 +00001747 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001748 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001749 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001750 keycode = keycodes[i];
1751 if (keycode & 0x80)
1752 kbd_put_keycode(0xe0);
1753 kbd_put_keycode(keycode & 0x7f);
1754 }
balrogc8256f92008-06-08 22:45:01 +00001755 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001756 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001757 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001758}
1759
bellard13224a82006-07-14 22:03:35 +00001760static int mouse_button_state;
1761
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001762static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001763{
1764 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001765 const char *dx_str = qdict_get_str(qdict, "dx_str");
1766 const char *dy_str = qdict_get_str(qdict, "dy_str");
1767 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001768 dx = strtol(dx_str, NULL, 0);
1769 dy = strtol(dy_str, NULL, 0);
1770 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001771 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001772 dz = strtol(dz_str, NULL, 0);
1773 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1774}
1775
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001776static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001777{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001778 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001779 mouse_button_state = button_state;
1780 kbd_mouse_event(0, 0, 0, mouse_button_state);
1781}
1782
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001783static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001784{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001785 int size = qdict_get_int(qdict, "size");
1786 int addr = qdict_get_int(qdict, "addr");
1787 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001788 uint32_t val;
1789 int suffix;
1790
1791 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001792 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001793 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001794 addr++;
1795 }
1796 addr &= 0xffff;
1797
1798 switch(size) {
1799 default:
1800 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001801 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001802 suffix = 'b';
1803 break;
1804 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001805 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001806 suffix = 'w';
1807 break;
1808 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001809 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001810 suffix = 'l';
1811 break;
1812 }
aliguori376253e2009-03-05 23:01:23 +00001813 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1814 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001815}
bellarda3a91a32004-06-04 11:06:21 +00001816
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001817static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001818{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001819 int size = qdict_get_int(qdict, "size");
1820 int addr = qdict_get_int(qdict, "addr");
1821 int val = qdict_get_int(qdict, "val");
1822
Jan Kiszkaf1147842009-07-14 10:20:11 +02001823 addr &= IOPORTS_MASK;
1824
1825 switch (size) {
1826 default:
1827 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001828 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001829 break;
1830 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001831 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001832 break;
1833 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001834 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001835 break;
1836 }
1837}
1838
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001839static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001840{
1841 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001842 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001843
Jan Kiszka76e30d02009-07-02 00:19:02 +02001844 res = qemu_boot_set(bootdevice);
1845 if (res == 0) {
1846 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1847 } else if (res > 0) {
1848 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001849 } else {
aliguori376253e2009-03-05 23:01:23 +00001850 monitor_printf(mon, "no function defined to set boot device list for "
1851 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001852 }
1853}
1854
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001855/**
1856 * do_system_reset(): Issue a machine reset
1857 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001858static int do_system_reset(Monitor *mon, const QDict *qdict,
1859 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001860{
1861 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001862 return 0;
bellarde4f90822004-06-20 12:35:44 +00001863}
1864
Luiz Capitulino43076662009-10-07 13:41:59 -03001865/**
1866 * do_system_powerdown(): Issue a machine powerdown
1867 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001868static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1869 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001870{
1871 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001872 return 0;
bellard34751872005-07-02 14:31:34 +00001873}
1874
bellardb86bda52004-09-18 19:32:46 +00001875#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001876static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001877{
aliguori376253e2009-03-05 23:01:23 +00001878 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1879 addr,
1880 pte & mask,
1881 pte & PG_GLOBAL_MASK ? 'G' : '-',
1882 pte & PG_PSE_MASK ? 'P' : '-',
1883 pte & PG_DIRTY_MASK ? 'D' : '-',
1884 pte & PG_ACCESSED_MASK ? 'A' : '-',
1885 pte & PG_PCD_MASK ? 'C' : '-',
1886 pte & PG_PWT_MASK ? 'T' : '-',
1887 pte & PG_USER_MASK ? 'U' : '-',
1888 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001889}
1890
aliguori376253e2009-03-05 23:01:23 +00001891static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001892{
bellard6a00d602005-11-21 23:25:50 +00001893 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001894 int l1, l2;
1895 uint32_t pgd, pde, pte;
1896
bellard6a00d602005-11-21 23:25:50 +00001897 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001898
bellardb86bda52004-09-18 19:32:46 +00001899 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001900 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001901 return;
1902 }
1903 pgd = env->cr[3] & ~0xfff;
1904 for(l1 = 0; l1 < 1024; l1++) {
1905 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1906 pde = le32_to_cpu(pde);
1907 if (pde & PG_PRESENT_MASK) {
1908 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001909 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001910 } else {
1911 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001912 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001913 (uint8_t *)&pte, 4);
1914 pte = le32_to_cpu(pte);
1915 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001916 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001917 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001918 ~0xfff);
1919 }
1920 }
1921 }
1922 }
1923 }
1924}
1925
aliguori376253e2009-03-05 23:01:23 +00001926static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001927 uint32_t end, int prot)
1928{
bellard9746b152004-11-11 18:30:24 +00001929 int prot1;
1930 prot1 = *plast_prot;
1931 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001932 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001933 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1934 *pstart, end, end - *pstart,
1935 prot1 & PG_USER_MASK ? 'u' : '-',
1936 'r',
1937 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001938 }
1939 if (prot != 0)
1940 *pstart = end;
1941 else
1942 *pstart = -1;
1943 *plast_prot = prot;
1944 }
1945}
1946
aliguori376253e2009-03-05 23:01:23 +00001947static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001948{
bellard6a00d602005-11-21 23:25:50 +00001949 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001950 int l1, l2, prot, last_prot;
1951 uint32_t pgd, pde, pte, start, end;
1952
bellard6a00d602005-11-21 23:25:50 +00001953 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001954
bellardb86bda52004-09-18 19:32:46 +00001955 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001956 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001957 return;
1958 }
1959 pgd = env->cr[3] & ~0xfff;
1960 last_prot = 0;
1961 start = -1;
1962 for(l1 = 0; l1 < 1024; l1++) {
1963 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1964 pde = le32_to_cpu(pde);
1965 end = l1 << 22;
1966 if (pde & PG_PRESENT_MASK) {
1967 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1968 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001969 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001970 } else {
1971 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001972 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001973 (uint8_t *)&pte, 4);
1974 pte = le32_to_cpu(pte);
1975 end = (l1 << 22) + (l2 << 12);
1976 if (pte & PG_PRESENT_MASK) {
1977 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1978 } else {
1979 prot = 0;
1980 }
aliguori376253e2009-03-05 23:01:23 +00001981 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001982 }
1983 }
1984 } else {
1985 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001986 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001987 }
1988 }
1989}
1990#endif
1991
aurel327c664e22009-03-03 06:12:22 +00001992#if defined(TARGET_SH4)
1993
aliguori376253e2009-03-05 23:01:23 +00001994static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001995{
aliguori376253e2009-03-05 23:01:23 +00001996 monitor_printf(mon, " tlb%i:\t"
1997 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1998 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1999 "dirty=%hhu writethrough=%hhu\n",
2000 idx,
2001 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2002 tlb->v, tlb->sh, tlb->c, tlb->pr,
2003 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002004}
2005
aliguori376253e2009-03-05 23:01:23 +00002006static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002007{
2008 CPUState *env = mon_get_cpu();
2009 int i;
2010
aliguori376253e2009-03-05 23:01:23 +00002011 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002012 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002013 print_tlb (mon, i, &env->itlb[i]);
2014 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002015 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002016 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002017}
2018
2019#endif
2020
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002021static void do_info_kvm_print(Monitor *mon, const QObject *data)
2022{
2023 QDict *qdict;
2024
2025 qdict = qobject_to_qdict(data);
2026
2027 monitor_printf(mon, "kvm support: ");
2028 if (qdict_get_bool(qdict, "present")) {
2029 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2030 "enabled" : "disabled");
2031 } else {
2032 monitor_printf(mon, "not compiled\n");
2033 }
2034}
2035
2036/**
2037 * do_info_kvm(): Show KVM information
2038 *
2039 * Return a QDict with the following information:
2040 *
2041 * - "enabled": true if KVM support is enabled, false otherwise
2042 * - "present": true if QEMU has KVM support, false otherwise
2043 *
2044 * Example:
2045 *
2046 * { "enabled": true, "present": true }
2047 */
2048static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002049{
2050#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002051 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2052 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002053#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002054 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002055#endif
2056}
2057
aliguori030ea372009-04-21 22:30:47 +00002058static void do_info_numa(Monitor *mon)
2059{
aliguorib28b6232009-04-22 20:20:29 +00002060 int i;
aliguori030ea372009-04-21 22:30:47 +00002061 CPUState *env;
2062
2063 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2064 for (i = 0; i < nb_numa_nodes; i++) {
2065 monitor_printf(mon, "node %d cpus:", i);
2066 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2067 if (env->numa_node == i) {
2068 monitor_printf(mon, " %d", env->cpu_index);
2069 }
2070 }
2071 monitor_printf(mon, "\n");
2072 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2073 node_mem[i] >> 20);
2074 }
2075}
2076
bellard5f1ce942006-02-08 22:40:15 +00002077#ifdef CONFIG_PROFILER
2078
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002079int64_t qemu_time;
2080int64_t dev_time;
2081
aliguori376253e2009-03-05 23:01:23 +00002082static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002083{
2084 int64_t total;
2085 total = qemu_time;
2086 if (total == 0)
2087 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002088 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002089 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002090 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002091 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002092 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002093 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002094}
2095#else
aliguori376253e2009-03-05 23:01:23 +00002096static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002097{
aliguori376253e2009-03-05 23:01:23 +00002098 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002099}
2100#endif
2101
bellardec36b692006-07-16 18:57:03 +00002102/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002103static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002104
aliguori376253e2009-03-05 23:01:23 +00002105static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002106{
2107 int i;
2108 CaptureState *s;
2109
2110 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002111 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002112 s->ops.info (s->opaque);
2113 }
2114}
2115
Blue Swirl23130862009-06-06 08:22:04 +00002116#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002117static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002118{
2119 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002120 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002121 CaptureState *s;
2122
2123 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2124 if (i == n) {
2125 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002126 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002127 qemu_free (s);
2128 return;
2129 }
2130 }
2131}
2132
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002133static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002134{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002135 const char *path = qdict_get_str(qdict, "path");
2136 int has_freq = qdict_haskey(qdict, "freq");
2137 int freq = qdict_get_try_int(qdict, "freq", -1);
2138 int has_bits = qdict_haskey(qdict, "bits");
2139 int bits = qdict_get_try_int(qdict, "bits", -1);
2140 int has_channels = qdict_haskey(qdict, "nchannels");
2141 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002142 CaptureState *s;
2143
2144 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002145
2146 freq = has_freq ? freq : 44100;
2147 bits = has_bits ? bits : 16;
2148 nchannels = has_channels ? nchannels : 2;
2149
2150 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002151 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002152 qemu_free (s);
2153 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002154 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002155}
2156#endif
2157
aurel32dc1c0b72008-04-27 23:52:12 +00002158#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002159static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002160{
2161 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002162 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002163
2164 for (env = first_cpu; env != NULL; env = env->next_cpu)
2165 if (env->cpu_index == cpu_index) {
2166 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2167 break;
2168 }
2169}
2170#endif
2171
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002172static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002173{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002174 QDict *qdict;
2175
2176 qdict = qobject_to_qdict(data);
2177
2178 monitor_printf(mon, "VM status: ");
2179 if (qdict_get_bool(qdict, "running")) {
2180 monitor_printf(mon, "running");
2181 if (qdict_get_bool(qdict, "singlestep")) {
2182 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002183 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002184 } else {
2185 monitor_printf(mon, "paused");
2186 }
2187
2188 monitor_printf(mon, "\n");
2189}
2190
2191/**
2192 * do_info_status(): VM status
2193 *
2194 * Return a QDict with the following information:
2195 *
2196 * - "running": true if the VM is running, or false if it is paused
2197 * - "singlestep": true if the VM is in single step mode, false otherwise
2198 *
2199 * Example:
2200 *
2201 * { "running": true, "singlestep": false }
2202 */
2203static void do_info_status(Monitor *mon, QObject **ret_data)
2204{
2205 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2206 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002207}
2208
Adam Litke625a5be2010-01-26 14:17:35 -06002209static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002210{
Adam Litke625a5be2010-01-26 14:17:35 -06002211 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002212
Adam Litke625a5be2010-01-26 14:17:35 -06002213 if (strcmp(key, "actual"))
2214 monitor_printf(mon, ",%s=%" PRId64, key,
2215 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002216}
2217
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002218static void monitor_print_balloon(Monitor *mon, const QObject *data)
2219{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002220 QDict *qdict;
2221
2222 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002223 if (!qdict_haskey(qdict, "actual"))
2224 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002225
Adam Litke625a5be2010-01-26 14:17:35 -06002226 monitor_printf(mon, "balloon: actual=%" PRId64,
2227 qdict_get_int(qdict, "actual") >> 20);
2228 qdict_iter(qdict, print_balloon_stat, mon);
2229 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002230}
2231
2232/**
2233 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002234 *
Adam Litke625a5be2010-01-26 14:17:35 -06002235 * Make an asynchronous request for balloon info. When the request completes
2236 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002237 *
Adam Litke625a5be2010-01-26 14:17:35 -06002238 * - "actual": current balloon value in bytes
2239 * The following fields may or may not be present:
2240 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2241 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2242 * - "major_page_faults": Number of major faults
2243 * - "minor_page_faults": Number of minor faults
2244 * - "free_mem": Total amount of free and unused memory (bytes)
2245 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002246 *
2247 * Example:
2248 *
Adam Litke625a5be2010-01-26 14:17:35 -06002249 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2250 * "major_page_faults": 142, "minor_page_faults": 239245,
2251 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002252 */
Adam Litke625a5be2010-01-26 14:17:35 -06002253static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002254{
Adam Litke625a5be2010-01-26 14:17:35 -06002255 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002256
Adam Litke625a5be2010-01-26 14:17:35 -06002257 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2258 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2259 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002260 }
Adam Litke625a5be2010-01-26 14:17:35 -06002261
2262 ret = qemu_balloon_status(cb, opaque);
2263 if (!ret) {
2264 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2265 return -1;
2266 }
2267
2268 return 0;
2269}
2270
2271/**
2272 * do_balloon(): Request VM to change its memory allocation
2273 */
2274static int do_balloon(Monitor *mon, const QDict *params,
2275 MonitorCompletion cb, void *opaque)
2276{
2277 int ret;
2278
2279 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2280 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2281 return -1;
2282 }
2283
2284 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2285 if (ret == 0) {
2286 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2287 return -1;
2288 }
2289
2290 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002291}
2292
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002293static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002294{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002295 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002296
aliguori76655d62009-03-06 20:27:37 +00002297 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002298 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002299 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002300 return acl;
2301}
aliguori76655d62009-03-06 20:27:37 +00002302
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002303static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002304{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002305 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002306 qemu_acl *acl = find_acl(mon, aclname);
2307 qemu_acl_entry *entry;
2308 int i = 0;
2309
2310 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002311 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002312 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002313 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002314 i++;
2315 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002316 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002317 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002318 }
2319}
2320
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002321static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002322{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002323 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002324 qemu_acl *acl = find_acl(mon, aclname);
2325
2326 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002327 qemu_acl_reset(acl);
2328 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002329 }
2330}
aliguori76655d62009-03-06 20:27:37 +00002331
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002332static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002333{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002334 const char *aclname = qdict_get_str(qdict, "aclname");
2335 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002336 qemu_acl *acl = find_acl(mon, aclname);
2337
2338 if (acl) {
2339 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002340 acl->defaultDeny = 0;
2341 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002342 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002343 acl->defaultDeny = 1;
2344 monitor_printf(mon, "acl: policy set to 'deny'\n");
2345 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002346 monitor_printf(mon, "acl: unknown policy '%s', "
2347 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002348 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002349 }
2350}
aliguori76655d62009-03-06 20:27:37 +00002351
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002352static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002353{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002354 const char *aclname = qdict_get_str(qdict, "aclname");
2355 const char *match = qdict_get_str(qdict, "match");
2356 const char *policy = qdict_get_str(qdict, "policy");
2357 int has_index = qdict_haskey(qdict, "index");
2358 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002359 qemu_acl *acl = find_acl(mon, aclname);
2360 int deny, ret;
2361
2362 if (acl) {
2363 if (strcmp(policy, "allow") == 0) {
2364 deny = 0;
2365 } else if (strcmp(policy, "deny") == 0) {
2366 deny = 1;
2367 } else {
2368 monitor_printf(mon, "acl: unknown policy '%s', "
2369 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002370 return;
2371 }
aliguori28a76be2009-03-06 20:27:40 +00002372 if (has_index)
2373 ret = qemu_acl_insert(acl, deny, match, index);
2374 else
2375 ret = qemu_acl_append(acl, deny, match);
2376 if (ret < 0)
2377 monitor_printf(mon, "acl: unable to add acl entry\n");
2378 else
2379 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002380 }
2381}
aliguori76655d62009-03-06 20:27:37 +00002382
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002383static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002384{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002385 const char *aclname = qdict_get_str(qdict, "aclname");
2386 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002387 qemu_acl *acl = find_acl(mon, aclname);
2388 int ret;
aliguori76655d62009-03-06 20:27:37 +00002389
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002390 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002391 ret = qemu_acl_remove(acl, match);
2392 if (ret < 0)
2393 monitor_printf(mon, "acl: no matching acl entry\n");
2394 else
2395 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002396 }
2397}
2398
Huang Ying79c4f6b2009-06-23 10:05:14 +08002399#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002400static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002401{
2402 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002403 int cpu_index = qdict_get_int(qdict, "cpu_index");
2404 int bank = qdict_get_int(qdict, "bank");
2405 uint64_t status = qdict_get_int(qdict, "status");
2406 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2407 uint64_t addr = qdict_get_int(qdict, "addr");
2408 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002409
2410 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2411 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2412 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2413 break;
2414 }
2415}
2416#endif
2417
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002418static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002419{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002420 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002421 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002422 int fd;
2423
2424 fd = qemu_chr_get_msgfd(mon->chr);
2425 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002426 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002427 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002428 }
2429
2430 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002431 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002432 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002433 }
2434
2435 fd = dup(fd);
2436 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002437 if (errno == EMFILE)
2438 qemu_error_new(QERR_TOO_MANY_FILES);
2439 else
2440 qemu_error_new(QERR_UNDEFINED_ERROR);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002441 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002442 }
2443
Blue Swirl72cf2d42009-09-12 07:36:22 +00002444 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002445 if (strcmp(monfd->name, fdname) != 0) {
2446 continue;
2447 }
2448
2449 close(monfd->fd);
2450 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002451 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002452 }
2453
Anthony Liguoric227f092009-10-01 16:12:16 -05002454 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002455 monfd->name = qemu_strdup(fdname);
2456 monfd->fd = fd;
2457
Blue Swirl72cf2d42009-09-12 07:36:22 +00002458 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002459 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002460}
2461
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002462static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002463{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002464 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002465 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002466
Blue Swirl72cf2d42009-09-12 07:36:22 +00002467 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002468 if (strcmp(monfd->name, fdname) != 0) {
2469 continue;
2470 }
2471
Blue Swirl72cf2d42009-09-12 07:36:22 +00002472 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002473 close(monfd->fd);
2474 qemu_free(monfd->name);
2475 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002476 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002477 }
2478
Markus Armbruster063c1a02009-12-07 21:37:11 +01002479 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002480 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002481}
2482
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002483static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002484{
2485 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002486 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002487
2488 vm_stop(0);
2489
Juan Quintela05f24012009-08-20 19:42:22 +02002490 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002491 vm_start();
2492}
2493
Mark McLoughlin7768e042009-07-22 09:11:41 +01002494int monitor_get_fd(Monitor *mon, const char *fdname)
2495{
Anthony Liguoric227f092009-10-01 16:12:16 -05002496 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002497
Blue Swirl72cf2d42009-09-12 07:36:22 +00002498 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002499 int fd;
2500
2501 if (strcmp(monfd->name, fdname) != 0) {
2502 continue;
2503 }
2504
2505 fd = monfd->fd;
2506
2507 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002508 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002509 qemu_free(monfd->name);
2510 qemu_free(monfd);
2511
2512 return fd;
2513 }
2514
2515 return -1;
2516}
2517
Anthony Liguoric227f092009-10-01 16:12:16 -05002518static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002519#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002520 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002521};
2522
Blue Swirl23130862009-06-06 08:22:04 +00002523/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002524static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 {
2526 .name = "version",
2527 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 .params = "",
2529 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002530 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002531 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002534 .name = "commands",
2535 .args_type = "",
2536 .params = "",
2537 .help = "list QMP available commands",
2538 .user_print = monitor_user_noop,
2539 .mhandler.info_new = do_info_commands,
2540 },
2541 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 .name = "network",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002546 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
2549 .name = "chardev",
2550 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 .params = "",
2552 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002553 .user_print = qemu_chr_info_print,
2554 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 },
2556 {
2557 .name = "block",
2558 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 .params = "",
2560 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002561 .user_print = bdrv_info_print,
2562 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 },
2564 {
2565 .name = "blockstats",
2566 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .params = "",
2568 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002569 .user_print = bdrv_stats_print,
2570 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 },
2572 {
2573 .name = "registers",
2574 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 .params = "",
2576 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002577 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 },
2579 {
2580 .name = "cpus",
2581 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 .params = "",
2583 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002584 .user_print = monitor_print_cpus,
2585 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 },
2587 {
2588 .name = "history",
2589 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 .params = "",
2591 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002592 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 },
2594 {
2595 .name = "irq",
2596 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 .params = "",
2598 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002599 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 },
2601 {
2602 .name = "pic",
2603 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 .params = "",
2605 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002606 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 },
2608 {
2609 .name = "pci",
2610 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 .params = "",
2612 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002613 .user_print = do_pci_info_print,
2614 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 },
aurel327c664e22009-03-03 06:12:22 +00002616#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 {
2618 .name = "tlb",
2619 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 .params = "",
2621 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002622 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 },
aurel327c664e22009-03-03 06:12:22 +00002624#endif
2625#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 {
2627 .name = "mem",
2628 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 .params = "",
2630 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002631 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 },
2633 {
2634 .name = "hpet",
2635 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 .params = "",
2637 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002638 .user_print = do_info_hpet_print,
2639 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 },
bellardb86bda52004-09-18 19:32:46 +00002641#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 {
2643 .name = "jit",
2644 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 .params = "",
2646 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002647 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 },
2649 {
2650 .name = "kvm",
2651 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 .params = "",
2653 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002654 .user_print = do_info_kvm_print,
2655 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 },
2657 {
2658 .name = "numa",
2659 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 .params = "",
2661 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002662 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002663 },
2664 {
2665 .name = "usb",
2666 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 .params = "",
2668 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002669 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 },
2671 {
2672 .name = "usbhost",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002676 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
2679 .name = "profile",
2680 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 .params = "",
2682 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002683 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
2685 {
2686 .name = "capture",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002690 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
2693 .name = "snapshots",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002697 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "status",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002704 .user_print = do_info_status_print,
2705 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
2707 {
2708 .name = "pcmcia",
2709 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 .params = "",
2711 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002712 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 },
2714 {
2715 .name = "mice",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002719 .user_print = do_info_mice_print,
2720 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
2722 {
2723 .name = "vnc",
2724 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 .params = "",
2726 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002727 .user_print = do_info_vnc_print,
2728 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 },
2730 {
2731 .name = "name",
2732 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 .params = "",
2734 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002735 .user_print = do_info_name_print,
2736 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002737 },
2738 {
2739 .name = "uuid",
2740 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 .params = "",
2742 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002743 .user_print = do_info_uuid_print,
2744 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 },
j_mayer76a66252007-03-07 08:32:30 +00002746#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 {
2748 .name = "cpustats",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002752 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
j_mayer76a66252007-03-07 08:32:30 +00002754#endif
blueswir131a60e22007-10-26 18:42:59 +00002755#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 {
2757 .name = "usernet",
2758 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002759 .params = "",
2760 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002761 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002762 },
blueswir131a60e22007-10-26 18:42:59 +00002763#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 {
2765 .name = "migrate",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002769 .user_print = do_info_migrate_print,
2770 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
2772 {
2773 .name = "balloon",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002777 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002778 .mhandler.info_async = do_info_balloon,
2779 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 },
2781 {
2782 .name = "qtree",
2783 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 .params = "",
2785 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002786 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 },
2788 {
2789 .name = "qdm",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002793 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 },
2795 {
2796 .name = "roms",
2797 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002798 .params = "",
2799 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002800 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 },
2802 {
2803 .name = NULL,
2804 },
bellard9dc39cb2004-03-14 21:38:27 +00002805};
2806
bellard9307c4c2004-04-04 12:57:25 +00002807/*******************************************************************/
2808
2809static const char *pch;
2810static jmp_buf expr_env;
2811
bellard92a31b12005-02-10 22:00:52 +00002812#define MD_TLONG 0
2813#define MD_I32 1
2814
bellard9307c4c2004-04-04 12:57:25 +00002815typedef struct MonitorDef {
2816 const char *name;
2817 int offset;
blueswir18662d652008-10-02 18:32:44 +00002818 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002819 int type;
bellard9307c4c2004-04-04 12:57:25 +00002820} MonitorDef;
2821
bellard57206fd2004-04-25 18:54:52 +00002822#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002823static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002824{
bellard6a00d602005-11-21 23:25:50 +00002825 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002826 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002827}
2828#endif
2829
bellarda541f292004-04-12 20:39:29 +00002830#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002831static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002832{
bellard6a00d602005-11-21 23:25:50 +00002833 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002834 unsigned int u;
2835 int i;
2836
2837 u = 0;
2838 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002839 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002840
2841 return u;
2842}
2843
blueswir18662d652008-10-02 18:32:44 +00002844static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002845{
bellard6a00d602005-11-21 23:25:50 +00002846 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002847 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002848}
2849
blueswir18662d652008-10-02 18:32:44 +00002850static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002851{
bellard6a00d602005-11-21 23:25:50 +00002852 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002853 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002854}
bellard9fddaa02004-05-21 12:59:32 +00002855
blueswir18662d652008-10-02 18:32:44 +00002856static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002857{
bellard6a00d602005-11-21 23:25:50 +00002858 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002859 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002860}
2861
blueswir18662d652008-10-02 18:32:44 +00002862static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002863{
bellard6a00d602005-11-21 23:25:50 +00002864 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002865 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002866}
2867
blueswir18662d652008-10-02 18:32:44 +00002868static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002869{
bellard6a00d602005-11-21 23:25:50 +00002870 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002871 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002872}
bellarda541f292004-04-12 20:39:29 +00002873#endif
2874
bellarde95c8d52004-09-30 22:22:08 +00002875#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002876#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002877static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002878{
bellard6a00d602005-11-21 23:25:50 +00002879 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002880 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002881}
bellard7b936c02005-10-30 17:05:13 +00002882#endif
bellarde95c8d52004-09-30 22:22:08 +00002883
blueswir18662d652008-10-02 18:32:44 +00002884static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002885{
bellard6a00d602005-11-21 23:25:50 +00002886 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002887 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002888}
2889#endif
2890
blueswir18662d652008-10-02 18:32:44 +00002891static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002892#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002893
2894#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002895 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002896 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002897 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002898
bellard9307c4c2004-04-04 12:57:25 +00002899 { "eax", offsetof(CPUState, regs[0]) },
2900 { "ecx", offsetof(CPUState, regs[1]) },
2901 { "edx", offsetof(CPUState, regs[2]) },
2902 { "ebx", offsetof(CPUState, regs[3]) },
2903 { "esp|sp", offsetof(CPUState, regs[4]) },
2904 { "ebp|fp", offsetof(CPUState, regs[5]) },
2905 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002906 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002907#ifdef TARGET_X86_64
2908 { "r8", offsetof(CPUState, regs[8]) },
2909 { "r9", offsetof(CPUState, regs[9]) },
2910 { "r10", offsetof(CPUState, regs[10]) },
2911 { "r11", offsetof(CPUState, regs[11]) },
2912 { "r12", offsetof(CPUState, regs[12]) },
2913 { "r13", offsetof(CPUState, regs[13]) },
2914 { "r14", offsetof(CPUState, regs[14]) },
2915 { "r15", offsetof(CPUState, regs[15]) },
2916#endif
bellard9307c4c2004-04-04 12:57:25 +00002917 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002918 { "eip", offsetof(CPUState, eip) },
2919 SEG("cs", R_CS)
2920 SEG("ds", R_DS)
2921 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002922 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002923 SEG("fs", R_FS)
2924 SEG("gs", R_GS)
2925 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002926#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002927 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002928 { "r0", offsetof(CPUState, gpr[0]) },
2929 { "r1", offsetof(CPUState, gpr[1]) },
2930 { "r2", offsetof(CPUState, gpr[2]) },
2931 { "r3", offsetof(CPUState, gpr[3]) },
2932 { "r4", offsetof(CPUState, gpr[4]) },
2933 { "r5", offsetof(CPUState, gpr[5]) },
2934 { "r6", offsetof(CPUState, gpr[6]) },
2935 { "r7", offsetof(CPUState, gpr[7]) },
2936 { "r8", offsetof(CPUState, gpr[8]) },
2937 { "r9", offsetof(CPUState, gpr[9]) },
2938 { "r10", offsetof(CPUState, gpr[10]) },
2939 { "r11", offsetof(CPUState, gpr[11]) },
2940 { "r12", offsetof(CPUState, gpr[12]) },
2941 { "r13", offsetof(CPUState, gpr[13]) },
2942 { "r14", offsetof(CPUState, gpr[14]) },
2943 { "r15", offsetof(CPUState, gpr[15]) },
2944 { "r16", offsetof(CPUState, gpr[16]) },
2945 { "r17", offsetof(CPUState, gpr[17]) },
2946 { "r18", offsetof(CPUState, gpr[18]) },
2947 { "r19", offsetof(CPUState, gpr[19]) },
2948 { "r20", offsetof(CPUState, gpr[20]) },
2949 { "r21", offsetof(CPUState, gpr[21]) },
2950 { "r22", offsetof(CPUState, gpr[22]) },
2951 { "r23", offsetof(CPUState, gpr[23]) },
2952 { "r24", offsetof(CPUState, gpr[24]) },
2953 { "r25", offsetof(CPUState, gpr[25]) },
2954 { "r26", offsetof(CPUState, gpr[26]) },
2955 { "r27", offsetof(CPUState, gpr[27]) },
2956 { "r28", offsetof(CPUState, gpr[28]) },
2957 { "r29", offsetof(CPUState, gpr[29]) },
2958 { "r30", offsetof(CPUState, gpr[30]) },
2959 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002960 /* Floating point registers */
2961 { "f0", offsetof(CPUState, fpr[0]) },
2962 { "f1", offsetof(CPUState, fpr[1]) },
2963 { "f2", offsetof(CPUState, fpr[2]) },
2964 { "f3", offsetof(CPUState, fpr[3]) },
2965 { "f4", offsetof(CPUState, fpr[4]) },
2966 { "f5", offsetof(CPUState, fpr[5]) },
2967 { "f6", offsetof(CPUState, fpr[6]) },
2968 { "f7", offsetof(CPUState, fpr[7]) },
2969 { "f8", offsetof(CPUState, fpr[8]) },
2970 { "f9", offsetof(CPUState, fpr[9]) },
2971 { "f10", offsetof(CPUState, fpr[10]) },
2972 { "f11", offsetof(CPUState, fpr[11]) },
2973 { "f12", offsetof(CPUState, fpr[12]) },
2974 { "f13", offsetof(CPUState, fpr[13]) },
2975 { "f14", offsetof(CPUState, fpr[14]) },
2976 { "f15", offsetof(CPUState, fpr[15]) },
2977 { "f16", offsetof(CPUState, fpr[16]) },
2978 { "f17", offsetof(CPUState, fpr[17]) },
2979 { "f18", offsetof(CPUState, fpr[18]) },
2980 { "f19", offsetof(CPUState, fpr[19]) },
2981 { "f20", offsetof(CPUState, fpr[20]) },
2982 { "f21", offsetof(CPUState, fpr[21]) },
2983 { "f22", offsetof(CPUState, fpr[22]) },
2984 { "f23", offsetof(CPUState, fpr[23]) },
2985 { "f24", offsetof(CPUState, fpr[24]) },
2986 { "f25", offsetof(CPUState, fpr[25]) },
2987 { "f26", offsetof(CPUState, fpr[26]) },
2988 { "f27", offsetof(CPUState, fpr[27]) },
2989 { "f28", offsetof(CPUState, fpr[28]) },
2990 { "f29", offsetof(CPUState, fpr[29]) },
2991 { "f30", offsetof(CPUState, fpr[30]) },
2992 { "f31", offsetof(CPUState, fpr[31]) },
2993 { "fpscr", offsetof(CPUState, fpscr) },
2994 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002995 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002996 { "lr", offsetof(CPUState, lr) },
2997 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002998 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002999 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003000 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003001 { "msr", 0, &monitor_get_msr, },
3002 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003003 { "tbu", 0, &monitor_get_tbu, },
3004 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003005#if defined(TARGET_PPC64)
3006 /* Address space register */
3007 { "asr", offsetof(CPUState, asr) },
3008#endif
3009 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00003010 { "sdr1", offsetof(CPUState, sdr1) },
3011 { "sr0", offsetof(CPUState, sr[0]) },
3012 { "sr1", offsetof(CPUState, sr[1]) },
3013 { "sr2", offsetof(CPUState, sr[2]) },
3014 { "sr3", offsetof(CPUState, sr[3]) },
3015 { "sr4", offsetof(CPUState, sr[4]) },
3016 { "sr5", offsetof(CPUState, sr[5]) },
3017 { "sr6", offsetof(CPUState, sr[6]) },
3018 { "sr7", offsetof(CPUState, sr[7]) },
3019 { "sr8", offsetof(CPUState, sr[8]) },
3020 { "sr9", offsetof(CPUState, sr[9]) },
3021 { "sr10", offsetof(CPUState, sr[10]) },
3022 { "sr11", offsetof(CPUState, sr[11]) },
3023 { "sr12", offsetof(CPUState, sr[12]) },
3024 { "sr13", offsetof(CPUState, sr[13]) },
3025 { "sr14", offsetof(CPUState, sr[14]) },
3026 { "sr15", offsetof(CPUState, sr[15]) },
3027 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003028#elif defined(TARGET_SPARC)
3029 { "g0", offsetof(CPUState, gregs[0]) },
3030 { "g1", offsetof(CPUState, gregs[1]) },
3031 { "g2", offsetof(CPUState, gregs[2]) },
3032 { "g3", offsetof(CPUState, gregs[3]) },
3033 { "g4", offsetof(CPUState, gregs[4]) },
3034 { "g5", offsetof(CPUState, gregs[5]) },
3035 { "g6", offsetof(CPUState, gregs[6]) },
3036 { "g7", offsetof(CPUState, gregs[7]) },
3037 { "o0", 0, monitor_get_reg },
3038 { "o1", 1, monitor_get_reg },
3039 { "o2", 2, monitor_get_reg },
3040 { "o3", 3, monitor_get_reg },
3041 { "o4", 4, monitor_get_reg },
3042 { "o5", 5, monitor_get_reg },
3043 { "o6", 6, monitor_get_reg },
3044 { "o7", 7, monitor_get_reg },
3045 { "l0", 8, monitor_get_reg },
3046 { "l1", 9, monitor_get_reg },
3047 { "l2", 10, monitor_get_reg },
3048 { "l3", 11, monitor_get_reg },
3049 { "l4", 12, monitor_get_reg },
3050 { "l5", 13, monitor_get_reg },
3051 { "l6", 14, monitor_get_reg },
3052 { "l7", 15, monitor_get_reg },
3053 { "i0", 16, monitor_get_reg },
3054 { "i1", 17, monitor_get_reg },
3055 { "i2", 18, monitor_get_reg },
3056 { "i3", 19, monitor_get_reg },
3057 { "i4", 20, monitor_get_reg },
3058 { "i5", 21, monitor_get_reg },
3059 { "i6", 22, monitor_get_reg },
3060 { "i7", 23, monitor_get_reg },
3061 { "pc", offsetof(CPUState, pc) },
3062 { "npc", offsetof(CPUState, npc) },
3063 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003064#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003065 { "psr", 0, &monitor_get_psr, },
3066 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003067#endif
bellarde95c8d52004-09-30 22:22:08 +00003068 { "tbr", offsetof(CPUState, tbr) },
3069 { "fsr", offsetof(CPUState, fsr) },
3070 { "f0", offsetof(CPUState, fpr[0]) },
3071 { "f1", offsetof(CPUState, fpr[1]) },
3072 { "f2", offsetof(CPUState, fpr[2]) },
3073 { "f3", offsetof(CPUState, fpr[3]) },
3074 { "f4", offsetof(CPUState, fpr[4]) },
3075 { "f5", offsetof(CPUState, fpr[5]) },
3076 { "f6", offsetof(CPUState, fpr[6]) },
3077 { "f7", offsetof(CPUState, fpr[7]) },
3078 { "f8", offsetof(CPUState, fpr[8]) },
3079 { "f9", offsetof(CPUState, fpr[9]) },
3080 { "f10", offsetof(CPUState, fpr[10]) },
3081 { "f11", offsetof(CPUState, fpr[11]) },
3082 { "f12", offsetof(CPUState, fpr[12]) },
3083 { "f13", offsetof(CPUState, fpr[13]) },
3084 { "f14", offsetof(CPUState, fpr[14]) },
3085 { "f15", offsetof(CPUState, fpr[15]) },
3086 { "f16", offsetof(CPUState, fpr[16]) },
3087 { "f17", offsetof(CPUState, fpr[17]) },
3088 { "f18", offsetof(CPUState, fpr[18]) },
3089 { "f19", offsetof(CPUState, fpr[19]) },
3090 { "f20", offsetof(CPUState, fpr[20]) },
3091 { "f21", offsetof(CPUState, fpr[21]) },
3092 { "f22", offsetof(CPUState, fpr[22]) },
3093 { "f23", offsetof(CPUState, fpr[23]) },
3094 { "f24", offsetof(CPUState, fpr[24]) },
3095 { "f25", offsetof(CPUState, fpr[25]) },
3096 { "f26", offsetof(CPUState, fpr[26]) },
3097 { "f27", offsetof(CPUState, fpr[27]) },
3098 { "f28", offsetof(CPUState, fpr[28]) },
3099 { "f29", offsetof(CPUState, fpr[29]) },
3100 { "f30", offsetof(CPUState, fpr[30]) },
3101 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003102#ifdef TARGET_SPARC64
3103 { "f32", offsetof(CPUState, fpr[32]) },
3104 { "f34", offsetof(CPUState, fpr[34]) },
3105 { "f36", offsetof(CPUState, fpr[36]) },
3106 { "f38", offsetof(CPUState, fpr[38]) },
3107 { "f40", offsetof(CPUState, fpr[40]) },
3108 { "f42", offsetof(CPUState, fpr[42]) },
3109 { "f44", offsetof(CPUState, fpr[44]) },
3110 { "f46", offsetof(CPUState, fpr[46]) },
3111 { "f48", offsetof(CPUState, fpr[48]) },
3112 { "f50", offsetof(CPUState, fpr[50]) },
3113 { "f52", offsetof(CPUState, fpr[52]) },
3114 { "f54", offsetof(CPUState, fpr[54]) },
3115 { "f56", offsetof(CPUState, fpr[56]) },
3116 { "f58", offsetof(CPUState, fpr[58]) },
3117 { "f60", offsetof(CPUState, fpr[60]) },
3118 { "f62", offsetof(CPUState, fpr[62]) },
3119 { "asi", offsetof(CPUState, asi) },
3120 { "pstate", offsetof(CPUState, pstate) },
3121 { "cansave", offsetof(CPUState, cansave) },
3122 { "canrestore", offsetof(CPUState, canrestore) },
3123 { "otherwin", offsetof(CPUState, otherwin) },
3124 { "wstate", offsetof(CPUState, wstate) },
3125 { "cleanwin", offsetof(CPUState, cleanwin) },
3126 { "fprs", offsetof(CPUState, fprs) },
3127#endif
bellard9307c4c2004-04-04 12:57:25 +00003128#endif
3129 { NULL },
3130};
3131
aliguori376253e2009-03-05 23:01:23 +00003132static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003133{
aliguori376253e2009-03-05 23:01:23 +00003134 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003135 longjmp(expr_env, 1);
3136}
3137
Markus Armbruster09b94182010-01-20 13:07:30 +01003138/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003139static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003140{
blueswir18662d652008-10-02 18:32:44 +00003141 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003142 void *ptr;
3143
bellard9307c4c2004-04-04 12:57:25 +00003144 for(md = monitor_defs; md->name != NULL; md++) {
3145 if (compare_cmd(name, md->name)) {
3146 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003147 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003148 } else {
bellard6a00d602005-11-21 23:25:50 +00003149 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003150 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003151 switch(md->type) {
3152 case MD_I32:
3153 *pval = *(int32_t *)ptr;
3154 break;
3155 case MD_TLONG:
3156 *pval = *(target_long *)ptr;
3157 break;
3158 default:
3159 *pval = 0;
3160 break;
3161 }
bellard9307c4c2004-04-04 12:57:25 +00003162 }
3163 return 0;
3164 }
3165 }
3166 return -1;
3167}
3168
3169static void next(void)
3170{
Blue Swirl660f11b2009-07-31 21:16:51 +00003171 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003172 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003173 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003174 pch++;
3175 }
3176}
3177
aliguori376253e2009-03-05 23:01:23 +00003178static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003179
aliguori376253e2009-03-05 23:01:23 +00003180static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003181{
blueswir1c2efc952007-09-25 17:28:42 +00003182 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003183 char *p;
bellard6a00d602005-11-21 23:25:50 +00003184 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003185
3186 switch(*pch) {
3187 case '+':
3188 next();
aliguori376253e2009-03-05 23:01:23 +00003189 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003190 break;
3191 case '-':
3192 next();
aliguori376253e2009-03-05 23:01:23 +00003193 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003194 break;
3195 case '~':
3196 next();
aliguori376253e2009-03-05 23:01:23 +00003197 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003198 break;
3199 case '(':
3200 next();
aliguori376253e2009-03-05 23:01:23 +00003201 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003202 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003203 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003204 }
3205 next();
3206 break;
bellard81d09122004-07-14 17:21:37 +00003207 case '\'':
3208 pch++;
3209 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003210 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003211 n = *pch;
3212 pch++;
3213 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003214 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003215 next();
3216 break;
bellard9307c4c2004-04-04 12:57:25 +00003217 case '$':
3218 {
3219 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003220 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003221
bellard9307c4c2004-04-04 12:57:25 +00003222 pch++;
3223 q = buf;
3224 while ((*pch >= 'a' && *pch <= 'z') ||
3225 (*pch >= 'A' && *pch <= 'Z') ||
3226 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003227 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003228 if ((q - buf) < sizeof(buf) - 1)
3229 *q++ = *pch;
3230 pch++;
3231 }
blueswir1cd390082008-11-16 13:53:32 +00003232 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003233 pch++;
3234 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003235 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003236 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003237 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003238 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003239 }
3240 break;
3241 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003242 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003243 n = 0;
3244 break;
3245 default:
blueswir17743e582007-09-24 18:39:04 +00003246#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003247 n = strtoull(pch, &p, 0);
3248#else
bellard9307c4c2004-04-04 12:57:25 +00003249 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003250#endif
bellard9307c4c2004-04-04 12:57:25 +00003251 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003252 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003253 }
3254 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003255 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003256 pch++;
3257 break;
3258 }
3259 return n;
3260}
3261
3262
aliguori376253e2009-03-05 23:01:23 +00003263static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003264{
blueswir1c2efc952007-09-25 17:28:42 +00003265 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003266 int op;
ths3b46e622007-09-17 08:09:54 +00003267
aliguori376253e2009-03-05 23:01:23 +00003268 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003269 for(;;) {
3270 op = *pch;
3271 if (op != '*' && op != '/' && op != '%')
3272 break;
3273 next();
aliguori376253e2009-03-05 23:01:23 +00003274 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003275 switch(op) {
3276 default:
3277 case '*':
3278 val *= val2;
3279 break;
3280 case '/':
3281 case '%':
ths5fafdf22007-09-16 21:08:06 +00003282 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003283 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003284 if (op == '/')
3285 val /= val2;
3286 else
3287 val %= val2;
3288 break;
3289 }
3290 }
3291 return val;
3292}
3293
aliguori376253e2009-03-05 23:01:23 +00003294static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003295{
blueswir1c2efc952007-09-25 17:28:42 +00003296 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003297 int op;
bellard9307c4c2004-04-04 12:57:25 +00003298
aliguori376253e2009-03-05 23:01:23 +00003299 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003300 for(;;) {
3301 op = *pch;
3302 if (op != '&' && op != '|' && op != '^')
3303 break;
3304 next();
aliguori376253e2009-03-05 23:01:23 +00003305 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003306 switch(op) {
3307 default:
3308 case '&':
3309 val &= val2;
3310 break;
3311 case '|':
3312 val |= val2;
3313 break;
3314 case '^':
3315 val ^= val2;
3316 break;
3317 }
3318 }
3319 return val;
3320}
3321
aliguori376253e2009-03-05 23:01:23 +00003322static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003323{
blueswir1c2efc952007-09-25 17:28:42 +00003324 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003325 int op;
bellard9307c4c2004-04-04 12:57:25 +00003326
aliguori376253e2009-03-05 23:01:23 +00003327 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003328 for(;;) {
3329 op = *pch;
3330 if (op != '+' && op != '-')
3331 break;
3332 next();
aliguori376253e2009-03-05 23:01:23 +00003333 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003334 if (op == '+')
3335 val += val2;
3336 else
3337 val -= val2;
3338 }
3339 return val;
3340}
3341
aliguori376253e2009-03-05 23:01:23 +00003342static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003343{
3344 pch = *pp;
3345 if (setjmp(expr_env)) {
3346 *pp = pch;
3347 return -1;
3348 }
blueswir1cd390082008-11-16 13:53:32 +00003349 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003350 pch++;
aliguori376253e2009-03-05 23:01:23 +00003351 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003352 *pp = pch;
3353 return 0;
3354}
3355
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003356static int get_double(Monitor *mon, double *pval, const char **pp)
3357{
3358 const char *p = *pp;
3359 char *tailp;
3360 double d;
3361
3362 d = strtod(p, &tailp);
3363 if (tailp == p) {
3364 monitor_printf(mon, "Number expected\n");
3365 return -1;
3366 }
3367 if (d != d || d - d != 0) {
3368 /* NaN or infinity */
3369 monitor_printf(mon, "Bad number\n");
3370 return -1;
3371 }
3372 *pval = d;
3373 *pp = tailp;
3374 return 0;
3375}
3376
bellard9307c4c2004-04-04 12:57:25 +00003377static int get_str(char *buf, int buf_size, const char **pp)
3378{
3379 const char *p;
3380 char *q;
3381 int c;
3382
bellard81d09122004-07-14 17:21:37 +00003383 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003384 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003385 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003386 p++;
3387 if (*p == '\0') {
3388 fail:
bellard81d09122004-07-14 17:21:37 +00003389 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003390 *pp = p;
3391 return -1;
3392 }
bellard9307c4c2004-04-04 12:57:25 +00003393 if (*p == '\"') {
3394 p++;
3395 while (*p != '\0' && *p != '\"') {
3396 if (*p == '\\') {
3397 p++;
3398 c = *p++;
3399 switch(c) {
3400 case 'n':
3401 c = '\n';
3402 break;
3403 case 'r':
3404 c = '\r';
3405 break;
3406 case '\\':
3407 case '\'':
3408 case '\"':
3409 break;
3410 default:
3411 qemu_printf("unsupported escape code: '\\%c'\n", c);
3412 goto fail;
3413 }
3414 if ((q - buf) < buf_size - 1) {
3415 *q++ = c;
3416 }
3417 } else {
3418 if ((q - buf) < buf_size - 1) {
3419 *q++ = *p;
3420 }
3421 p++;
3422 }
3423 }
3424 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003425 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003426 goto fail;
3427 }
3428 p++;
3429 } else {
blueswir1cd390082008-11-16 13:53:32 +00003430 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003431 if ((q - buf) < buf_size - 1) {
3432 *q++ = *p;
3433 }
3434 p++;
3435 }
bellard9307c4c2004-04-04 12:57:25 +00003436 }
bellard81d09122004-07-14 17:21:37 +00003437 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003438 *pp = p;
3439 return 0;
3440}
3441
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003442/*
3443 * Store the command-name in cmdname, and return a pointer to
3444 * the remaining of the command string.
3445 */
3446static const char *get_command_name(const char *cmdline,
3447 char *cmdname, size_t nlen)
3448{
3449 size_t len;
3450 const char *p, *pstart;
3451
3452 p = cmdline;
3453 while (qemu_isspace(*p))
3454 p++;
3455 if (*p == '\0')
3456 return NULL;
3457 pstart = p;
3458 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3459 p++;
3460 len = p - pstart;
3461 if (len > nlen - 1)
3462 len = nlen - 1;
3463 memcpy(cmdname, pstart, len);
3464 cmdname[len] = '\0';
3465 return p;
3466}
3467
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003468/**
3469 * Read key of 'type' into 'key' and return the current
3470 * 'type' pointer.
3471 */
3472static char *key_get_info(const char *type, char **key)
3473{
3474 size_t len;
3475 char *p, *str;
3476
3477 if (*type == ',')
3478 type++;
3479
3480 p = strchr(type, ':');
3481 if (!p) {
3482 *key = NULL;
3483 return NULL;
3484 }
3485 len = p - type;
3486
3487 str = qemu_malloc(len + 1);
3488 memcpy(str, type, len);
3489 str[len] = '\0';
3490
3491 *key = str;
3492 return ++p;
3493}
3494
bellard9307c4c2004-04-04 12:57:25 +00003495static int default_fmt_format = 'x';
3496static int default_fmt_size = 4;
3497
3498#define MAX_ARGS 16
3499
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003500static int is_valid_option(const char *c, const char *typestr)
3501{
3502 char option[3];
3503
3504 option[0] = '-';
3505 option[1] = *c;
3506 option[2] = '\0';
3507
3508 typestr = strstr(typestr, option);
3509 return (typestr != NULL);
3510}
3511
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003512static const mon_cmd_t *monitor_find_command(const char *cmdname)
3513{
3514 const mon_cmd_t *cmd;
3515
3516 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3517 if (compare_cmd(cmdname, cmd->name)) {
3518 return cmd;
3519 }
3520 }
3521
3522 return NULL;
3523}
3524
Anthony Liguoric227f092009-10-01 16:12:16 -05003525static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003526 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003527 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003528{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003529 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003530 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003531 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003532 char cmdname[256];
3533 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003534 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003535
3536#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003537 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003538#endif
ths3b46e622007-09-17 08:09:54 +00003539
bellard9307c4c2004-04-04 12:57:25 +00003540 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003541 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3542 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003543 return NULL;
ths3b46e622007-09-17 08:09:54 +00003544
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003545 cmd = monitor_find_command(cmdname);
3546 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003547 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003548 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003549 }
bellard9307c4c2004-04-04 12:57:25 +00003550
bellard9307c4c2004-04-04 12:57:25 +00003551 /* parse the parameters */
3552 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003553 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003554 typestr = key_get_info(typestr, &key);
3555 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003556 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003557 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003558 typestr++;
3559 switch(c) {
3560 case 'F':
bellard81d09122004-07-14 17:21:37 +00003561 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003562 case 's':
3563 {
3564 int ret;
ths3b46e622007-09-17 08:09:54 +00003565
blueswir1cd390082008-11-16 13:53:32 +00003566 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003567 p++;
3568 if (*typestr == '?') {
3569 typestr++;
3570 if (*p == '\0') {
3571 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003572 break;
bellard9307c4c2004-04-04 12:57:25 +00003573 }
3574 }
3575 ret = get_str(buf, sizeof(buf), &p);
3576 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003577 switch(c) {
3578 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003579 monitor_printf(mon, "%s: filename expected\n",
3580 cmdname);
bellard81d09122004-07-14 17:21:37 +00003581 break;
3582 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003583 monitor_printf(mon, "%s: block device name expected\n",
3584 cmdname);
bellard81d09122004-07-14 17:21:37 +00003585 break;
3586 default:
aliguori376253e2009-03-05 23:01:23 +00003587 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003588 break;
3589 }
bellard9307c4c2004-04-04 12:57:25 +00003590 goto fail;
3591 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003592 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003593 }
3594 break;
3595 case '/':
3596 {
3597 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003598
blueswir1cd390082008-11-16 13:53:32 +00003599 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003600 p++;
3601 if (*p == '/') {
3602 /* format found */
3603 p++;
3604 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003605 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003606 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003607 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003608 count = count * 10 + (*p - '0');
3609 p++;
3610 }
3611 }
3612 size = -1;
3613 format = -1;
3614 for(;;) {
3615 switch(*p) {
3616 case 'o':
3617 case 'd':
3618 case 'u':
3619 case 'x':
3620 case 'i':
3621 case 'c':
3622 format = *p++;
3623 break;
3624 case 'b':
3625 size = 1;
3626 p++;
3627 break;
3628 case 'h':
3629 size = 2;
3630 p++;
3631 break;
3632 case 'w':
3633 size = 4;
3634 p++;
3635 break;
3636 case 'g':
3637 case 'L':
3638 size = 8;
3639 p++;
3640 break;
3641 default:
3642 goto next;
3643 }
3644 }
3645 next:
blueswir1cd390082008-11-16 13:53:32 +00003646 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003647 monitor_printf(mon, "invalid char in format: '%c'\n",
3648 *p);
bellard9307c4c2004-04-04 12:57:25 +00003649 goto fail;
3650 }
bellard9307c4c2004-04-04 12:57:25 +00003651 if (format < 0)
3652 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003653 if (format != 'i') {
3654 /* for 'i', not specifying a size gives -1 as size */
3655 if (size < 0)
3656 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003657 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003658 }
bellard9307c4c2004-04-04 12:57:25 +00003659 default_fmt_format = format;
3660 } else {
3661 count = 1;
3662 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003663 if (format != 'i') {
3664 size = default_fmt_size;
3665 } else {
3666 size = -1;
3667 }
bellard9307c4c2004-04-04 12:57:25 +00003668 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003669 qdict_put(qdict, "count", qint_from_int(count));
3670 qdict_put(qdict, "format", qint_from_int(format));
3671 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003672 }
3673 break;
3674 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003675 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003676 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003677 {
blueswir1c2efc952007-09-25 17:28:42 +00003678 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003679
blueswir1cd390082008-11-16 13:53:32 +00003680 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003681 p++;
bellard34405572004-06-08 00:55:58 +00003682 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003683 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003684 if (*p == '\0') {
3685 typestr++;
3686 break;
3687 }
bellard34405572004-06-08 00:55:58 +00003688 } else {
3689 if (*p == '.') {
3690 p++;
blueswir1cd390082008-11-16 13:53:32 +00003691 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003692 p++;
bellard34405572004-06-08 00:55:58 +00003693 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003694 typestr++;
3695 break;
bellard34405572004-06-08 00:55:58 +00003696 }
3697 }
bellard13224a82006-07-14 22:03:35 +00003698 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003699 }
aliguori376253e2009-03-05 23:01:23 +00003700 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003701 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003702 /* Check if 'i' is greater than 32-bit */
3703 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3704 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3705 monitor_printf(mon, "integer is for 32-bit values\n");
3706 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003707 } else if (c == 'M') {
3708 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003709 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003710 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003711 }
3712 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003713 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003714 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003715 {
3716 double val;
3717
3718 while (qemu_isspace(*p))
3719 p++;
3720 if (*typestr == '?') {
3721 typestr++;
3722 if (*p == '\0') {
3723 break;
3724 }
3725 }
3726 if (get_double(mon, &val, &p) < 0) {
3727 goto fail;
3728 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003729 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003730 switch (*p) {
3731 case 'K': case 'k':
3732 val *= 1 << 10; p++; break;
3733 case 'M': case 'm':
3734 val *= 1 << 20; p++; break;
3735 case 'G': case 'g':
3736 val *= 1 << 30; p++; break;
3737 }
3738 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003739 if (c == 'T' && p[0] && p[1] == 's') {
3740 switch (*p) {
3741 case 'm':
3742 val /= 1e3; p += 2; break;
3743 case 'u':
3744 val /= 1e6; p += 2; break;
3745 case 'n':
3746 val /= 1e9; p += 2; break;
3747 }
3748 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003749 if (*p && !qemu_isspace(*p)) {
3750 monitor_printf(mon, "Unknown unit suffix\n");
3751 goto fail;
3752 }
3753 qdict_put(qdict, key, qfloat_from_double(val));
3754 }
3755 break;
bellard9307c4c2004-04-04 12:57:25 +00003756 case '-':
3757 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003758 const char *tmp = p;
3759 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003760 /* option */
ths3b46e622007-09-17 08:09:54 +00003761
bellard9307c4c2004-04-04 12:57:25 +00003762 c = *typestr++;
3763 if (c == '\0')
3764 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003765 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003766 p++;
3767 has_option = 0;
3768 if (*p == '-') {
3769 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003770 if(c != *p) {
3771 if(!is_valid_option(p, typestr)) {
3772
3773 monitor_printf(mon, "%s: unsupported option -%c\n",
3774 cmdname, *p);
3775 goto fail;
3776 } else {
3777 skip_key = 1;
3778 }
bellard9307c4c2004-04-04 12:57:25 +00003779 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003780 if(skip_key) {
3781 p = tmp;
3782 } else {
3783 p++;
3784 has_option = 1;
3785 }
bellard9307c4c2004-04-04 12:57:25 +00003786 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003787 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003788 }
3789 break;
3790 default:
3791 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003792 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003793 goto fail;
3794 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003795 qemu_free(key);
3796 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003797 }
3798 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003799 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003800 p++;
3801 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003802 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3803 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003804 goto fail;
3805 }
3806
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003807 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003808
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003809fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003810 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003811 return NULL;
3812}
3813
Luiz Capitulino8204a912009-11-18 23:05:31 -02003814static void monitor_print_error(Monitor *mon)
3815{
3816 qerror_print(mon->error);
3817 QDECREF(mon->error);
3818 mon->error = NULL;
3819}
3820
Adam Litke940cc302010-01-25 12:18:44 -06003821static int is_async_return(const QObject *data)
3822{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003823 if (data && qobject_type(data) == QTYPE_QDICT) {
3824 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3825 }
3826
3827 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003828}
3829
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003830static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3831 const QDict *params)
3832{
3833 QObject *data = NULL;
3834
Luiz Capitulino97536cf2010-02-10 23:49:47 -02003835 if (cmd->cmd_new_ret) {
3836 cmd->cmd_new_ret(mon, params, &data);
3837 } else {
3838 cmd->mhandler.cmd_new(mon, params, &data);
3839 }
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003840
Adam Litke940cc302010-01-25 12:18:44 -06003841 if (is_async_return(data)) {
3842 /*
3843 * Asynchronous commands have no initial return data but they can
3844 * generate errors. Data is returned via the async completion handler.
3845 */
3846 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3847 monitor_protocol_emitter(mon, NULL);
3848 }
3849 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003850 /* Monitor Protocol */
3851 monitor_protocol_emitter(mon, data);
3852 } else {
3853 /* User Protocol */
3854 if (data)
3855 cmd->user_print(mon, data);
3856 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003857
3858 qobject_decref(data);
3859}
3860
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003861static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003862{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003863 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003864 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003865
3866 qdict = qdict_new();
3867
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003868 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003869 if (!cmd)
3870 goto out;
3871
3872 qemu_errors_to_mon(mon);
3873
Adam Litke940cc302010-01-25 12:18:44 -06003874 if (monitor_handler_is_async(cmd)) {
3875 user_async_cmd_handler(mon, cmd, qdict);
3876 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003877 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003878 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003879 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003880 }
3881
Luiz Capitulino8204a912009-11-18 23:05:31 -02003882 if (monitor_has_error(mon))
3883 monitor_print_error(mon);
3884
3885 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003886
3887out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003888 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003889}
3890
bellard81d09122004-07-14 17:21:37 +00003891static void cmd_completion(const char *name, const char *list)
3892{
3893 const char *p, *pstart;
3894 char cmd[128];
3895 int len;
3896
3897 p = list;
3898 for(;;) {
3899 pstart = p;
3900 p = strchr(p, '|');
3901 if (!p)
3902 p = pstart + strlen(pstart);
3903 len = p - pstart;
3904 if (len > sizeof(cmd) - 2)
3905 len = sizeof(cmd) - 2;
3906 memcpy(cmd, pstart, len);
3907 cmd[len] = '\0';
3908 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003909 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003910 }
3911 if (*p == '\0')
3912 break;
3913 p++;
3914 }
3915}
3916
3917static void file_completion(const char *input)
3918{
3919 DIR *ffs;
3920 struct dirent *d;
3921 char path[1024];
3922 char file[1024], file_prefix[1024];
3923 int input_path_len;
3924 const char *p;
3925
ths5fafdf22007-09-16 21:08:06 +00003926 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003927 if (!p) {
3928 input_path_len = 0;
3929 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003930 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003931 } else {
3932 input_path_len = p - input + 1;
3933 memcpy(path, input, input_path_len);
3934 if (input_path_len > sizeof(path) - 1)
3935 input_path_len = sizeof(path) - 1;
3936 path[input_path_len] = '\0';
3937 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3938 }
3939#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003940 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3941 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003942#endif
3943 ffs = opendir(path);
3944 if (!ffs)
3945 return;
3946 for(;;) {
3947 struct stat sb;
3948 d = readdir(ffs);
3949 if (!d)
3950 break;
3951 if (strstart(d->d_name, file_prefix, NULL)) {
3952 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003953 if (input_path_len < sizeof(file))
3954 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3955 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003956 /* stat the file to find out if it's a directory.
3957 * In that case add a slash to speed up typing long paths
3958 */
3959 stat(file, &sb);
3960 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003961 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003962 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003963 }
3964 }
3965 closedir(ffs);
3966}
3967
aliguori51de9762009-03-05 23:00:43 +00003968static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003969{
aliguori51de9762009-03-05 23:00:43 +00003970 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003971 const char *input = opaque;
3972
3973 if (input[0] == '\0' ||
3974 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003975 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003976 }
3977}
3978
3979/* NOTE: this parser is an approximate form of the real command parser */
3980static void parse_cmdline(const char *cmdline,
3981 int *pnb_args, char **args)
3982{
3983 const char *p;
3984 int nb_args, ret;
3985 char buf[1024];
3986
3987 p = cmdline;
3988 nb_args = 0;
3989 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003990 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003991 p++;
3992 if (*p == '\0')
3993 break;
3994 if (nb_args >= MAX_ARGS)
3995 break;
3996 ret = get_str(buf, sizeof(buf), &p);
3997 args[nb_args] = qemu_strdup(buf);
3998 nb_args++;
3999 if (ret < 0)
4000 break;
4001 }
4002 *pnb_args = nb_args;
4003}
4004
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004005static const char *next_arg_type(const char *typestr)
4006{
4007 const char *p = strchr(typestr, ':');
4008 return (p != NULL ? ++p : typestr);
4009}
4010
aliguori4c36ba32009-03-05 23:01:37 +00004011static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004012{
4013 const char *cmdname;
4014 char *args[MAX_ARGS];
4015 int nb_args, i, len;
4016 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004017 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004018 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004019
4020 parse_cmdline(cmdline, &nb_args, args);
4021#ifdef DEBUG_COMPLETION
4022 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004023 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004024 }
4025#endif
4026
4027 /* if the line ends with a space, it means we want to complete the
4028 next arg */
4029 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004030 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004031 if (nb_args >= MAX_ARGS)
4032 return;
4033 args[nb_args++] = qemu_strdup("");
4034 }
4035 if (nb_args <= 1) {
4036 /* command completion */
4037 if (nb_args == 0)
4038 cmdname = "";
4039 else
4040 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004041 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004042 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004043 cmd_completion(cmdname, cmd->name);
4044 }
4045 } else {
4046 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004047 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004048 if (compare_cmd(args[0], cmd->name))
4049 goto found;
4050 }
4051 return;
4052 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004053 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004054 for(i = 0; i < nb_args - 2; i++) {
4055 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004056 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004057 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004058 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004059 }
4060 }
4061 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004062 if (*ptype == '-' && ptype[1] != '\0') {
4063 ptype += 2;
4064 }
bellard81d09122004-07-14 17:21:37 +00004065 switch(*ptype) {
4066 case 'F':
4067 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004068 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004069 file_completion(str);
4070 break;
4071 case 'B':
4072 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004073 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004074 bdrv_iterate(block_completion_it, (void *)str);
4075 break;
bellard7fe48482004-10-09 18:08:01 +00004076 case 's':
4077 /* XXX: more generic ? */
4078 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004079 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004080 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4081 cmd_completion(str, cmd->name);
4082 }
bellard64866c32006-05-07 18:03:31 +00004083 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004084 char *sep = strrchr(str, '-');
4085 if (sep)
4086 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004087 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004088 for(key = key_defs; key->name != NULL; key++) {
4089 cmd_completion(str, key->name);
4090 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004091 } else if (!strcmp(cmd->name, "help|?")) {
4092 readline_set_completion_index(cur_mon->rs, strlen(str));
4093 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4094 cmd_completion(str, cmd->name);
4095 }
bellard7fe48482004-10-09 18:08:01 +00004096 }
4097 break;
bellard81d09122004-07-14 17:21:37 +00004098 default:
4099 break;
4100 }
4101 }
4102 for(i = 0; i < nb_args; i++)
4103 qemu_free(args[i]);
4104}
4105
aliguori731b0362009-03-05 23:01:42 +00004106static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004107{
aliguori731b0362009-03-05 23:01:42 +00004108 Monitor *mon = opaque;
4109
Jan Kiszkac62313b2009-12-04 14:05:29 +01004110 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004111}
4112
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004113typedef struct CmdArgs {
4114 QString *name;
4115 int type;
4116 int flag;
4117 int optional;
4118} CmdArgs;
4119
4120static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4121{
4122 if (!cmd_args->optional) {
4123 qemu_error_new(QERR_MISSING_PARAMETER, name);
4124 return -1;
4125 }
4126
4127 if (cmd_args->type == '-') {
4128 /* handlers expect a value, they need to be changed */
4129 qdict_put(args, name, qint_from_int(0));
4130 }
4131
4132 return 0;
4133}
4134
4135static int check_arg(const CmdArgs *cmd_args, QDict *args)
4136{
4137 QObject *value;
4138 const char *name;
4139
4140 name = qstring_get_str(cmd_args->name);
4141
4142 if (!args) {
4143 return check_opt(cmd_args, name, args);
4144 }
4145
4146 value = qdict_get(args, name);
4147 if (!value) {
4148 return check_opt(cmd_args, name, args);
4149 }
4150
4151 switch (cmd_args->type) {
4152 case 'F':
4153 case 'B':
4154 case 's':
4155 if (qobject_type(value) != QTYPE_QSTRING) {
4156 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4157 return -1;
4158 }
4159 break;
4160 case '/': {
4161 int i;
4162 const char *keys[] = { "count", "format", "size", NULL };
4163
4164 for (i = 0; keys[i]; i++) {
4165 QObject *obj = qdict_get(args, keys[i]);
4166 if (!obj) {
4167 qemu_error_new(QERR_MISSING_PARAMETER, name);
4168 return -1;
4169 }
4170 if (qobject_type(obj) != QTYPE_QINT) {
4171 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4172 return -1;
4173 }
4174 }
4175 break;
4176 }
4177 case 'i':
4178 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004179 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004180 if (qobject_type(value) != QTYPE_QINT) {
4181 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4182 return -1;
4183 }
4184 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004185 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004186 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004187 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4188 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4189 return -1;
4190 }
4191 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004192 case '-':
4193 if (qobject_type(value) != QTYPE_QINT &&
4194 qobject_type(value) != QTYPE_QBOOL) {
4195 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4196 return -1;
4197 }
4198 if (qobject_type(value) == QTYPE_QBOOL) {
4199 /* handlers expect a QInt, they need to be changed */
4200 qdict_put(args, name,
4201 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4202 }
4203 break;
4204 default:
4205 /* impossible */
4206 abort();
4207 }
4208
4209 return 0;
4210}
4211
4212static void cmd_args_init(CmdArgs *cmd_args)
4213{
4214 cmd_args->name = qstring_new();
4215 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4216}
4217
4218/*
4219 * This is not trivial, we have to parse Monitor command's argument
4220 * type syntax to be able to check the arguments provided by clients.
4221 *
4222 * In the near future we will be using an array for that and will be
4223 * able to drop all this parsing...
4224 */
4225static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4226{
4227 int err;
4228 const char *p;
4229 CmdArgs cmd_args;
4230
Blue Swirldd5121b2009-12-04 20:52:02 +00004231 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004232 return (qdict_size(args) == 0 ? 0 : -1);
4233 }
4234
4235 err = 0;
4236 cmd_args_init(&cmd_args);
4237
4238 for (p = cmd->args_type;; p++) {
4239 if (*p == ':') {
4240 cmd_args.type = *++p;
4241 p++;
4242 if (cmd_args.type == '-') {
4243 cmd_args.flag = *p++;
4244 cmd_args.optional = 1;
4245 } else if (*p == '?') {
4246 cmd_args.optional = 1;
4247 p++;
4248 }
4249
4250 assert(*p == ',' || *p == '\0');
4251 err = check_arg(&cmd_args, args);
4252
4253 QDECREF(cmd_args.name);
4254 cmd_args_init(&cmd_args);
4255
4256 if (err < 0) {
4257 break;
4258 }
4259 } else {
4260 qstring_append_chr(cmd_args.name, *p);
4261 }
4262
4263 if (*p == '\0') {
4264 break;
4265 }
4266 }
4267
4268 QDECREF(cmd_args.name);
4269 return err;
4270}
4271
Luiz Capitulino09069b12010-02-04 18:10:06 -02004272static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4273{
4274 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4275 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4276}
4277
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004278static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4279{
4280 int err;
4281 QObject *obj;
4282 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004283 const mon_cmd_t *cmd;
4284 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004285 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004286
4287 args = NULL;
4288 qemu_errors_to_mon(mon);
4289
4290 obj = json_parser_parse(tokens, NULL);
4291 if (!obj) {
4292 // FIXME: should be triggered in json_parser_parse()
4293 qemu_error_new(QERR_JSON_PARSING);
4294 goto err_out;
4295 } else if (qobject_type(obj) != QTYPE_QDICT) {
4296 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4297 qobject_decref(obj);
4298 goto err_out;
4299 }
4300
4301 input = qobject_to_qdict(obj);
4302
4303 mon->mc->id = qdict_get(input, "id");
4304 qobject_incref(mon->mc->id);
4305
4306 obj = qdict_get(input, "execute");
4307 if (!obj) {
4308 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4309 goto err_input;
4310 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4311 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4312 goto err_input;
4313 }
4314
4315 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004316
Luiz Capitulino09069b12010-02-04 18:10:06 -02004317 if (invalid_qmp_mode(mon, cmd_name)) {
4318 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4319 goto err_input;
4320 }
4321
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004322 /*
4323 * XXX: We need this special case until we get info handlers
4324 * converted into 'query-' commands
4325 */
4326 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004327 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4328 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004329 } else if (strstart(cmd_name, "query-", &info_item)) {
4330 cmd = monitor_find_command("info");
4331 qdict_put_obj(input, "arguments",
4332 qobject_from_jsonf("{ 'item': %s }", info_item));
4333 } else {
4334 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004335 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004336 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4337 goto err_input;
4338 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004339 }
4340
4341 obj = qdict_get(input, "arguments");
4342 if (!obj) {
4343 args = qdict_new();
4344 } else {
4345 args = qobject_to_qdict(obj);
4346 QINCREF(args);
4347 }
4348
4349 QDECREF(input);
4350
4351 err = monitor_check_qmp_args(cmd, args);
4352 if (err < 0) {
4353 goto err_out;
4354 }
4355
Adam Litke940cc302010-01-25 12:18:44 -06004356 if (monitor_handler_is_async(cmd)) {
4357 qmp_async_cmd_handler(mon, cmd, args);
4358 } else {
4359 monitor_call_handler(mon, cmd, args);
4360 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004361 goto out;
4362
4363err_input:
4364 QDECREF(input);
4365err_out:
4366 monitor_protocol_emitter(mon, NULL);
4367out:
4368 QDECREF(args);
4369 qemu_errors_to_previous();
4370}
4371
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004372/**
4373 * monitor_control_read(): Read and handle QMP input
4374 */
4375static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4376{
4377 Monitor *old_mon = cur_mon;
4378
4379 cur_mon = opaque;
4380
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004381 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004382
4383 cur_mon = old_mon;
4384}
4385
aliguori731b0362009-03-05 23:01:42 +00004386static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004387{
aliguori731b0362009-03-05 23:01:42 +00004388 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004389 int i;
aliguori376253e2009-03-05 23:01:23 +00004390
aliguori731b0362009-03-05 23:01:42 +00004391 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004392
aliguoricde76ee2009-03-05 23:01:51 +00004393 if (cur_mon->rs) {
4394 for (i = 0; i < size; i++)
4395 readline_handle_byte(cur_mon->rs, buf[i]);
4396 } else {
4397 if (size == 0 || buf[size - 1] != 0)
4398 monitor_printf(cur_mon, "corrupted command\n");
4399 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004400 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004401 }
aliguori731b0362009-03-05 23:01:42 +00004402
4403 cur_mon = old_mon;
4404}
aliguorid8f44602008-10-06 13:52:44 +00004405
aliguori376253e2009-03-05 23:01:23 +00004406static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004407{
aliguori731b0362009-03-05 23:01:42 +00004408 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004409 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004410 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004411}
4412
aliguoricde76ee2009-03-05 23:01:51 +00004413int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004414{
aliguoricde76ee2009-03-05 23:01:51 +00004415 if (!mon->rs)
4416 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004417 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004418 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004419}
4420
aliguori376253e2009-03-05 23:01:23 +00004421void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004422{
aliguoricde76ee2009-03-05 23:01:51 +00004423 if (!mon->rs)
4424 return;
aliguori731b0362009-03-05 23:01:42 +00004425 if (--mon->suspend_cnt == 0)
4426 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004427}
4428
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004429static QObject *get_qmp_greeting(void)
4430{
4431 QObject *ver;
4432
4433 do_info_version(NULL, &ver);
4434 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4435}
4436
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004437/**
4438 * monitor_control_event(): Print QMP gretting
4439 */
4440static void monitor_control_event(void *opaque, int event)
4441{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004442 QObject *data;
4443 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004444
Luiz Capitulino47116d12010-02-08 17:01:30 -02004445 switch (event) {
4446 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004447 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004448 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004449 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004450 monitor_json_emitter(mon, data);
4451 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004452 break;
4453 case CHR_EVENT_CLOSED:
4454 json_message_parser_destroy(&mon->mc->parser);
4455 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004456 }
4457}
4458
aliguori731b0362009-03-05 23:01:42 +00004459static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004460{
aliguori376253e2009-03-05 23:01:23 +00004461 Monitor *mon = opaque;
4462
aliguori2724b182009-03-05 23:01:47 +00004463 switch (event) {
4464 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004465 mon->mux_out = 0;
4466 if (mon->reset_seen) {
4467 readline_restart(mon->rs);
4468 monitor_resume(mon);
4469 monitor_flush(mon);
4470 } else {
4471 mon->suspend_cnt = 0;
4472 }
aliguori2724b182009-03-05 23:01:47 +00004473 break;
ths86e94de2007-01-05 22:01:59 +00004474
aliguori2724b182009-03-05 23:01:47 +00004475 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004476 if (mon->reset_seen) {
4477 if (mon->suspend_cnt == 0) {
4478 monitor_printf(mon, "\n");
4479 }
4480 monitor_flush(mon);
4481 monitor_suspend(mon);
4482 } else {
4483 mon->suspend_cnt++;
4484 }
4485 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004486 break;
4487
Amit Shahb6b8df52009-10-07 18:31:16 +05304488 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004489 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4490 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004491 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004492 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004493 }
4494 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004495 break;
4496 }
ths86e94de2007-01-05 22:01:59 +00004497}
4498
aliguori76655d62009-03-06 20:27:37 +00004499
4500/*
4501 * Local variables:
4502 * c-indent-level: 4
4503 * c-basic-offset: 4
4504 * tab-width: 8
4505 * End:
4506 */
4507
aliguori731b0362009-03-05 23:01:42 +00004508void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004509{
aliguori731b0362009-03-05 23:01:42 +00004510 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004511 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004512
4513 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004514 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004515 is_first_init = 0;
4516 }
aliguori87127162009-03-05 23:01:29 +00004517
4518 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004519
aliguori87127162009-03-05 23:01:29 +00004520 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004521 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004522 if (flags & MONITOR_USE_READLINE) {
4523 mon->rs = readline_init(mon, monitor_find_completion);
4524 monitor_read_command(mon, 0);
4525 }
aliguori87127162009-03-05 23:01:29 +00004526
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004527 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004528 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004529 /* Control mode requires special handlers */
4530 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4531 monitor_control_event, mon);
4532 } else {
4533 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4534 monitor_event, mon);
4535 }
aliguori87127162009-03-05 23:01:29 +00004536
Blue Swirl72cf2d42009-09-12 07:36:22 +00004537 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004538 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004539 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004540}
4541
aliguori376253e2009-03-05 23:01:23 +00004542static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004543{
aliguoribb5fc202009-03-05 23:01:15 +00004544 BlockDriverState *bs = opaque;
4545 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004546
aliguoribb5fc202009-03-05 23:01:15 +00004547 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004548 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004549 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004550 }
aliguori731b0362009-03-05 23:01:42 +00004551 if (mon->password_completion_cb)
4552 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004553
aliguori731b0362009-03-05 23:01:42 +00004554 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004555}
aliguoric0f4ce72009-03-05 23:01:01 +00004556
aliguori376253e2009-03-05 23:01:23 +00004557void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004558 BlockDriverCompletionFunc *completion_cb,
4559 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004560{
aliguoricde76ee2009-03-05 23:01:51 +00004561 int err;
4562
aliguoribb5fc202009-03-05 23:01:15 +00004563 if (!bdrv_key_required(bs)) {
4564 if (completion_cb)
4565 completion_cb(opaque, 0);
4566 return;
4567 }
aliguoric0f4ce72009-03-05 23:01:01 +00004568
Luiz Capitulino94171e12009-12-07 21:37:00 +01004569 if (monitor_ctrl_mode(mon)) {
4570 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4571 return;
4572 }
4573
aliguori376253e2009-03-05 23:01:23 +00004574 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4575 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004576
aliguori731b0362009-03-05 23:01:42 +00004577 mon->password_completion_cb = completion_cb;
4578 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004579
aliguoricde76ee2009-03-05 23:01:51 +00004580 err = monitor_read_password(mon, bdrv_password_cb, bs);
4581
4582 if (err && completion_cb)
4583 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004584}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004585
4586typedef struct QemuErrorSink QemuErrorSink;
4587struct QemuErrorSink {
4588 enum {
4589 ERR_SINK_FILE,
4590 ERR_SINK_MONITOR,
4591 } dest;
4592 union {
4593 FILE *fp;
4594 Monitor *mon;
4595 };
4596 QemuErrorSink *previous;
4597};
4598
Blue Swirl528e93a2009-08-31 15:14:40 +00004599static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004600
4601void qemu_errors_to_file(FILE *fp)
4602{
4603 QemuErrorSink *sink;
4604
4605 sink = qemu_mallocz(sizeof(*sink));
4606 sink->dest = ERR_SINK_FILE;
4607 sink->fp = fp;
4608 sink->previous = qemu_error_sink;
4609 qemu_error_sink = sink;
4610}
4611
4612void qemu_errors_to_mon(Monitor *mon)
4613{
4614 QemuErrorSink *sink;
4615
4616 sink = qemu_mallocz(sizeof(*sink));
4617 sink->dest = ERR_SINK_MONITOR;
4618 sink->mon = mon;
4619 sink->previous = qemu_error_sink;
4620 qemu_error_sink = sink;
4621}
4622
4623void qemu_errors_to_previous(void)
4624{
4625 QemuErrorSink *sink;
4626
4627 assert(qemu_error_sink != NULL);
4628 sink = qemu_error_sink;
4629 qemu_error_sink = sink->previous;
4630 qemu_free(sink);
4631}
4632
4633void qemu_error(const char *fmt, ...)
4634{
4635 va_list args;
4636
4637 assert(qemu_error_sink != NULL);
4638 switch (qemu_error_sink->dest) {
4639 case ERR_SINK_FILE:
4640 va_start(args, fmt);
4641 vfprintf(qemu_error_sink->fp, fmt, args);
4642 va_end(args);
4643 break;
4644 case ERR_SINK_MONITOR:
4645 va_start(args, fmt);
4646 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4647 va_end(args);
4648 break;
4649 }
4650}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004651
4652void qemu_error_internal(const char *file, int linenr, const char *func,
4653 const char *fmt, ...)
4654{
4655 va_list va;
4656 QError *qerror;
4657
4658 assert(qemu_error_sink != NULL);
4659
4660 va_start(va, fmt);
4661 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4662 va_end(va);
4663
4664 switch (qemu_error_sink->dest) {
4665 case ERR_SINK_FILE:
4666 qerror_print(qerror);
4667 QDECREF(qerror);
4668 break;
4669 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004670 /* report only the first error */
4671 if (!qemu_error_sink->mon->error) {
4672 qemu_error_sink->mon->error = qerror;
4673 } else {
4674 /* XXX: warn the programmer */
4675 QDECREF(qerror);
4676 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004677 break;
4678 }
4679}