blob: 598dbfe5f2d8ef40d2b14637ac88e2158037cc59 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
37#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000038#include "monitor.h"
39#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "console.h"
41#include "block.h"
42#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000043#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000045#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000046#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000047#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000048#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030049#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010050#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030051#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030052#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020055#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020056#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020057#include "json-streamer.h"
58#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000059#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000060
bellard9dc39cb2004-03-14 21:38:27 +000061//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000062//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000063
bellard9307c4c2004-04-04 12:57:25 +000064/*
65 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000066 *
bellard9307c4c2004-04-04 12:57:25 +000067 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000068 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000069 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000070 * 'i' 32 bit integer
71 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010072 * 'M' just like 'l', except in user mode the value is
73 * multiplied by 2^20 (think Mebibyte)
Markus Armbruster3350a4d2010-01-25 14:23:03 +010074 * 'b' double
75 * user mode accepts an optional G, g, M, m, K, k suffix,
76 * which multiplies the value by 2^30 for suffixes G and
77 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010078 * 'T' double
79 * user mode accepts an optional ms, us, ns suffix,
80 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000081 * '/' optional gdb-like print format (like "/10x")
82 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030083 * '?' optional type (for all types, except '/')
84 * '.' other form of optional type (for 'i' and 'l')
85 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000086 *
87 */
88
Adam Litke940cc302010-01-25 12:18:44 -060089typedef struct MonitorCompletionData MonitorCompletionData;
90struct MonitorCompletionData {
91 Monitor *mon;
92 void (*user_print)(Monitor *mon, const QObject *data);
93};
94
Anthony Liguoric227f092009-10-01 16:12:16 -050095typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000096 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000097 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000098 const char *params;
99 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300100 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino97536cf2010-02-10 23:49:47 -0200101 int (*cmd_new_ret)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300102 union {
103 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300104 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600105 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300106 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -0300107 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600108 int (*cmd_async)(Monitor *mon, const QDict *params,
109 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300110 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600111 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500112} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000113
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100114/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500115typedef struct mon_fd_t mon_fd_t;
116struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100117 char *name;
118 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500119 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100120};
121
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200122typedef struct MonitorControl {
123 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200124 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200125 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200126 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200127} MonitorControl;
128
aliguori87127162009-03-05 23:01:29 +0000129struct Monitor {
130 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200131 int mux_out;
132 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000133 int flags;
134 int suspend_cnt;
135 uint8_t outbuf[1024];
136 int outbuf_index;
137 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200138 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000139 CPUState *mon_cpu;
140 BlockDriverCompletionFunc *password_completion_cb;
141 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200142 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000144 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000145};
146
Blue Swirl72cf2d42009-09-12 07:36:22 +0000147static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000148
Anthony Liguoric227f092009-10-01 16:12:16 -0500149static const mon_cmd_t mon_cmds[];
150static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000151
aliguori87127162009-03-05 23:01:29 +0000152Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000153
aliguori731b0362009-03-05 23:01:42 +0000154static void monitor_command_cb(Monitor *mon, const char *cmdline,
155 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000156
Luiz Capitulino09069b12010-02-04 18:10:06 -0200157static inline int qmp_cmd_mode(const Monitor *mon)
158{
159 return (mon->mc ? mon->mc->command_mode : 0);
160}
161
Luiz Capitulino418173c2009-11-26 22:58:51 -0200162/* Return true if in control mode, false otherwise */
163static inline int monitor_ctrl_mode(const Monitor *mon)
164{
165 return (mon->flags & MONITOR_USE_CONTROL);
166}
167
aliguori731b0362009-03-05 23:01:42 +0000168static void monitor_read_command(Monitor *mon, int show_prompt)
169{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200170 if (!mon->rs)
171 return;
172
aliguori731b0362009-03-05 23:01:42 +0000173 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
174 if (show_prompt)
175 readline_show_prompt(mon->rs);
176}
bellard6a00d602005-11-21 23:25:50 +0000177
aliguoricde76ee2009-03-05 23:01:51 +0000178static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
179 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000180{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100181 if (monitor_ctrl_mode(mon)) {
182 qemu_error_new(QERR_MISSING_PARAMETER, "password");
183 return -EINVAL;
184 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000185 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
186 /* prompt is printed on return from the command handler */
187 return 0;
188 } else {
189 monitor_printf(mon, "terminal does not support password prompting\n");
190 return -ENOTTY;
191 }
aliguoribb5fc202009-03-05 23:01:15 +0000192}
193
aliguori376253e2009-03-05 23:01:23 +0000194void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000195{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200196 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000197 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
198 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000199 }
200}
201
202/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000203static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000204{
ths60fe76f2007-12-16 03:02:09 +0000205 char c;
aliguori731b0362009-03-05 23:01:42 +0000206
bellard7e2515e2004-08-01 21:52:19 +0000207 for(;;) {
208 c = *str++;
209 if (c == '\0')
210 break;
bellard7ba12602006-07-14 20:26:42 +0000211 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000212 mon->outbuf[mon->outbuf_index++] = '\r';
213 mon->outbuf[mon->outbuf_index++] = c;
214 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
215 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000216 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000217 }
218}
219
aliguori376253e2009-03-05 23:01:23 +0000220void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000221{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200222 if (!mon)
223 return;
224
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200225 if (mon->mc && !mon->mc->print_enabled) {
226 qemu_error_new(QERR_UNDEFINED_ERROR);
227 } else {
228 char buf[4096];
229 vsnprintf(buf, sizeof(buf), fmt, ap);
230 monitor_puts(mon, buf);
231 }
bellard7e2515e2004-08-01 21:52:19 +0000232}
233
aliguori376253e2009-03-05 23:01:23 +0000234void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000235{
236 va_list ap;
237 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000238 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000239 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000240}
241
aliguori376253e2009-03-05 23:01:23 +0000242void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000243{
244 int i;
245
246 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000247 switch (filename[i]) {
248 case ' ':
249 case '"':
250 case '\\':
251 monitor_printf(mon, "\\%c", filename[i]);
252 break;
253 case '\t':
254 monitor_printf(mon, "\\t");
255 break;
256 case '\r':
257 monitor_printf(mon, "\\r");
258 break;
259 case '\n':
260 monitor_printf(mon, "\\n");
261 break;
262 default:
263 monitor_printf(mon, "%c", filename[i]);
264 break;
265 }
thsfef30742006-12-22 14:11:32 +0000266 }
267}
268
bellard7fe48482004-10-09 18:08:01 +0000269static int monitor_fprintf(FILE *stream, const char *fmt, ...)
270{
271 va_list ap;
272 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000273 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000274 va_end(ap);
275 return 0;
276}
277
Luiz Capitulino13c74252009-10-07 13:41:55 -0300278static void monitor_user_noop(Monitor *mon, const QObject *data) { }
279
Luiz Capitulino13917be2009-10-07 13:41:54 -0300280static inline int monitor_handler_ported(const mon_cmd_t *cmd)
281{
282 return cmd->user_print != NULL;
283}
284
Adam Litke940cc302010-01-25 12:18:44 -0600285static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
286{
287 return cmd->async != 0;
288}
289
Luiz Capitulino8204a912009-11-18 23:05:31 -0200290static inline int monitor_has_error(const Monitor *mon)
291{
292 return mon->error != NULL;
293}
294
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200295static void monitor_json_emitter(Monitor *mon, const QObject *data)
296{
297 QString *json;
298
299 json = qobject_to_json(data);
300 assert(json != NULL);
301
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200302 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200303 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200304 mon->mc->print_enabled = 0;
305
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200306 QDECREF(json);
307}
308
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200309static void monitor_protocol_emitter(Monitor *mon, QObject *data)
310{
311 QDict *qmp;
312
313 qmp = qdict_new();
314
315 if (!monitor_has_error(mon)) {
316 /* success response */
317 if (data) {
318 qobject_incref(data);
319 qdict_put_obj(qmp, "return", data);
320 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200321 /* return an empty QDict by default */
322 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200323 }
324 } else {
325 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100326 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200327 qdict_put(qmp, "error", mon->error->error);
328 QINCREF(mon->error->error);
329 QDECREF(mon->error);
330 mon->error = NULL;
331 }
332
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200333 if (mon->mc->id) {
334 qdict_put_obj(qmp, "id", mon->mc->id);
335 mon->mc->id = NULL;
336 }
337
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200338 monitor_json_emitter(mon, QOBJECT(qmp));
339 QDECREF(qmp);
340}
341
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200342static void timestamp_put(QDict *qdict)
343{
344 int err;
345 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000346 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200347
Blue Swirld08d6f02009-12-04 18:06:39 +0000348 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200349 if (err < 0)
350 return;
351
352 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
353 "'microseconds': %" PRId64 " }",
354 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200355 qdict_put_obj(qdict, "timestamp", obj);
356}
357
358/**
359 * monitor_protocol_event(): Generate a Monitor event
360 *
361 * Event-specific data can be emitted through the (optional) 'data' parameter.
362 */
363void monitor_protocol_event(MonitorEvent event, QObject *data)
364{
365 QDict *qmp;
366 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600367 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200368
Blue Swirl242cd002009-12-04 18:05:45 +0000369 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200370
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200371 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000372 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200373 event_name = "DEBUG";
374 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000375 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200376 event_name = "SHUTDOWN";
377 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000378 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200379 event_name = "RESET";
380 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000381 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200382 event_name = "POWERDOWN";
383 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000384 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200385 event_name = "STOP";
386 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200387 case QEVENT_VNC_CONNECTED:
388 event_name = "VNC_CONNECTED";
389 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200390 case QEVENT_VNC_INITIALIZED:
391 event_name = "VNC_INITIALIZED";
392 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200393 case QEVENT_VNC_DISCONNECTED:
394 event_name = "VNC_DISCONNECTED";
395 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200396 case QEVENT_BLOCK_IO_ERROR:
397 event_name = "BLOCK_IO_ERROR";
398 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399 default:
400 abort();
401 break;
402 }
403
404 qmp = qdict_new();
405 timestamp_put(qmp);
406 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200407 if (data) {
408 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200410 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200411
Adam Litkef039a562010-01-15 08:34:02 -0600412 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200413 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200414 monitor_json_emitter(mon, QOBJECT(qmp));
415 }
Adam Litkef039a562010-01-15 08:34:02 -0600416 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200417 QDECREF(qmp);
418}
419
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200420static int do_qmp_capabilities(Monitor *mon, const QDict *params,
421 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200422{
423 /* Will setup QMP capabilities in the future */
424 if (monitor_ctrl_mode(mon)) {
425 mon->mc->command_mode = 1;
426 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200427
428 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200429}
430
bellard9dc39cb2004-03-14 21:38:27 +0000431static int compare_cmd(const char *name, const char *list)
432{
433 const char *p, *pstart;
434 int len;
435 len = strlen(name);
436 p = list;
437 for(;;) {
438 pstart = p;
439 p = strchr(p, '|');
440 if (!p)
441 p = pstart + strlen(pstart);
442 if ((p - pstart) == len && !memcmp(pstart, name, len))
443 return 1;
444 if (*p == '\0')
445 break;
446 p++;
447 }
448 return 0;
449}
450
Anthony Liguoric227f092009-10-01 16:12:16 -0500451static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000452 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000453{
Anthony Liguoric227f092009-10-01 16:12:16 -0500454 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000455
456 for(cmd = cmds; cmd->name != NULL; cmd++) {
457 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000458 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
459 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000460 }
461}
462
aliguori376253e2009-03-05 23:01:23 +0000463static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000464{
465 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000466 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000467 } else {
aliguori376253e2009-03-05 23:01:23 +0000468 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000469 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000470 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "Log items (comma separated):\n");
472 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000473 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000474 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000475 }
476 }
bellard9dc39cb2004-03-14 21:38:27 +0000477 }
478}
479
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300480static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300481{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300482 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300483}
484
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300485static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000486{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200487 int all_devices;
488 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300489 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000490
bellard7954c732006-08-01 15:52:40 +0000491 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000492 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200493 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300494 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200495 continue;
496 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000497 }
498}
499
Adam Litke940cc302010-01-25 12:18:44 -0600500static void user_monitor_complete(void *opaque, QObject *ret_data)
501{
502 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
503
504 if (ret_data) {
505 data->user_print(data->mon, ret_data);
506 }
507 monitor_resume(data->mon);
508 qemu_free(data);
509}
510
511static void qmp_monitor_complete(void *opaque, QObject *ret_data)
512{
513 monitor_protocol_emitter(opaque, ret_data);
514}
515
516static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
517 const QDict *params)
518{
519 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
520}
521
522static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
523{
524 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
525}
526
527static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
528 const QDict *params)
529{
530 int ret;
531
532 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
533 cb_data->mon = mon;
534 cb_data->user_print = cmd->user_print;
535 monitor_suspend(mon);
536 ret = cmd->mhandler.cmd_async(mon, params,
537 user_monitor_complete, cb_data);
538 if (ret < 0) {
539 monitor_resume(mon);
540 qemu_free(cb_data);
541 }
542}
543
544static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
545{
546 int ret;
547
548 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
549 cb_data->mon = mon;
550 cb_data->user_print = cmd->user_print;
551 monitor_suspend(mon);
552 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
553 if (ret < 0) {
554 monitor_resume(mon);
555 qemu_free(cb_data);
556 }
557}
558
Luiz Capitulino13c74252009-10-07 13:41:55 -0300559static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000560{
Anthony Liguoric227f092009-10-01 16:12:16 -0500561 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300562 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000563
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200564 if (!item) {
565 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000566 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200567 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300568
569 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000570 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300571 break;
bellard9dc39cb2004-03-14 21:38:27 +0000572 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300573
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200574 if (cmd->name == NULL) {
575 if (monitor_ctrl_mode(mon)) {
576 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
577 return;
578 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300579 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200580 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300581
Adam Litke940cc302010-01-25 12:18:44 -0600582 if (monitor_handler_is_async(cmd)) {
583 if (monitor_ctrl_mode(mon)) {
584 qmp_async_info_handler(mon, cmd);
585 } else {
586 user_async_info_handler(mon, cmd);
587 }
588 /*
589 * Indicate that this command is asynchronous and will not return any
590 * data (not even empty). Instead, the data will be returned via a
591 * completion callback.
592 */
593 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
594 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300595 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200596
597 if (!monitor_ctrl_mode(mon)) {
598 /*
599 * User Protocol function is called here, Monitor Protocol is
600 * handled by monitor_call_handler()
601 */
602 if (*ret_data)
603 cmd->user_print(mon, *ret_data);
604 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300605 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200606 if (monitor_ctrl_mode(mon)) {
607 /* handler not converted yet */
608 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
609 } else {
610 cmd->mhandler.info(mon);
611 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300612 }
613
bellard9dc39cb2004-03-14 21:38:27 +0000614 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300615
616help:
617 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000618}
619
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200620static void do_info_version_print(Monitor *mon, const QObject *data)
621{
622 QDict *qdict;
623
624 qdict = qobject_to_qdict(data);
625
626 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
627 qdict_get_str(qdict, "package"));
628}
629
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300630/**
631 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200632 *
633 * Return a QDict with the following information:
634 *
635 * - "qemu": QEMU's version
636 * - "package": package's version
637 *
638 * Example:
639 *
640 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300641 */
642static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000643{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200644 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
645 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000646}
647
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200648static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000649{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200650 QDict *qdict;
651
652 qdict = qobject_to_qdict(data);
653 if (qdict_size(qdict) == 0) {
654 return;
655 }
656
657 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
658}
659
660/**
661 * do_info_name(): Show VM name
662 *
663 * Return a QDict with the following information:
664 *
665 * - "name": VM's name (optional)
666 *
667 * Example:
668 *
669 * { "name": "qemu-name" }
670 */
671static void do_info_name(Monitor *mon, QObject **ret_data)
672{
673 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
674 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000675}
676
Luiz Capitulino1a728672009-12-10 17:15:56 -0200677static QObject *get_cmd_dict(const char *name)
678{
679 const char *p;
680
681 /* Remove '|' from some commands */
682 p = strchr(name, '|');
683 if (p) {
684 p++;
685 } else {
686 p = name;
687 }
688
689 return qobject_from_jsonf("{ 'name': %s }", p);
690}
691
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200692/**
693 * do_info_commands(): List QMP available commands
694 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200695 * Each command is represented by a QDict, the returned QObject is a QList
696 * of all commands.
697 *
698 * The QDict contains:
699 *
700 * - "name": command's name
701 *
702 * Example:
703 *
704 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200705 */
706static void do_info_commands(Monitor *mon, QObject **ret_data)
707{
708 QList *cmd_list;
709 const mon_cmd_t *cmd;
710
711 cmd_list = qlist_new();
712
713 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
714 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200715 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200716 }
717 }
718
719 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
720 if (monitor_handler_ported(cmd)) {
721 char buf[128];
722 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200723 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200724 }
725 }
726
727 *ret_data = QOBJECT(cmd_list);
728}
729
aurel32bf4f74c2008-12-18 22:42:34 +0000730#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200731static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000732{
aliguori376253e2009-03-05 23:01:23 +0000733 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200734 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
735 "enabled" : "disabled");
736}
737
738/**
739 * do_info_hpet(): Show HPET state
740 *
741 * Return a QDict with the following information:
742 *
743 * - "enabled": true if hpet if enabled, false otherwise
744 *
745 * Example:
746 *
747 * { "enabled": true }
748 */
749static void do_info_hpet(Monitor *mon, QObject **ret_data)
750{
751 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000752}
aurel32bf4f74c2008-12-18 22:42:34 +0000753#endif
aliguori16b29ae2008-12-17 23:28:44 +0000754
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200755static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000756{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200757 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
758}
759
760/**
761 * do_info_uuid(): Show VM UUID
762 *
763 * Return a QDict with the following information:
764 *
765 * - "UUID": Universally Unique Identifier
766 *
767 * Example:
768 *
769 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
770 */
771static void do_info_uuid(Monitor *mon, QObject **ret_data)
772{
773 char uuid[64];
774
775 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000776 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
777 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
778 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
779 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200780 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000781}
782
bellard6a00d602005-11-21 23:25:50 +0000783/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000784static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000785{
786 CPUState *env;
787
788 for(env = first_cpu; env != NULL; env = env->next_cpu) {
789 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000790 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000791 return 0;
792 }
793 }
794 return -1;
795}
796
pbrook9596ebb2007-11-18 01:44:38 +0000797static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000798{
aliguori731b0362009-03-05 23:01:42 +0000799 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000800 mon_set_cpu(0);
801 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300802 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000803 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000804}
805
aliguori376253e2009-03-05 23:01:23 +0000806static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000807{
bellard6a00d602005-11-21 23:25:50 +0000808 CPUState *env;
809 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000810#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000811 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000812 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000813#else
aliguori376253e2009-03-05 23:01:23 +0000814 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000815 0);
bellard9307c4c2004-04-04 12:57:25 +0000816#endif
817}
818
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300819static void print_cpu_iter(QObject *obj, void *opaque)
820{
821 QDict *cpu;
822 int active = ' ';
823 Monitor *mon = opaque;
824
825 assert(qobject_type(obj) == QTYPE_QDICT);
826 cpu = qobject_to_qdict(obj);
827
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200828 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300829 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200830 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300831
832 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
833
834#if defined(TARGET_I386)
835 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
836 (target_ulong) qdict_get_int(cpu, "pc"));
837#elif defined(TARGET_PPC)
838 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "nip"));
840#elif defined(TARGET_SPARC)
841 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
842 (target_long) qdict_get_int(cpu, "pc"));
843 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
844 (target_long) qdict_get_int(cpu, "npc"));
845#elif defined(TARGET_MIPS)
846 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
847 (target_long) qdict_get_int(cpu, "PC"));
848#endif
849
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200850 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300851 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200852 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300853
854 monitor_printf(mon, "\n");
855}
856
857static void monitor_print_cpus(Monitor *mon, const QObject *data)
858{
859 QList *cpu_list;
860
861 assert(qobject_type(data) == QTYPE_QLIST);
862 cpu_list = qobject_to_qlist(data);
863 qlist_iter(cpu_list, print_cpu_iter, mon);
864}
865
866/**
867 * do_info_cpus(): Show CPU information
868 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200869 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300870 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200871 * - "cpu": CPU index
872 * - "current": true if this is the current CPU, false otherwise
873 * - "halted": true if the cpu is halted, false otherwise
874 * - Current program counter. The key's name depends on the architecture:
875 * "pc": i386/x86)64
876 * "nip": PPC
877 * "pc" and "npc": sparc
878 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200880 * Example:
881 *
882 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
883 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300884 */
885static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000886{
887 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888 QList *cpu_list;
889
890 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000891
892 /* just to set the default cpu if not already done */
893 mon_get_cpu();
894
895 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200896 QDict *cpu;
897 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300898
Avi Kivity4c0960c2009-08-17 23:19:53 +0300899 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300900
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200901 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
902 env->cpu_index, env == mon->mon_cpu,
903 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200904
905 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300906
bellard6a00d602005-11-21 23:25:50 +0000907#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300908 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000909#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300910 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000911#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300912 qdict_put(cpu, "pc", qint_from_int(env->pc));
913 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000914#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300915 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000916#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917
918 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000919 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300920
921 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000922}
923
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200924static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000925{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300926 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200927 if (mon_set_cpu(index) < 0) {
Markus Armbrustercc0c4182010-01-29 10:34:00 +0100928 qemu_error_new(QERR_INVALID_PARAMETER, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200929 return -1;
930 }
931 return 0;
bellard6a00d602005-11-21 23:25:50 +0000932}
933
aliguori376253e2009-03-05 23:01:23 +0000934static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000935{
aliguori376253e2009-03-05 23:01:23 +0000936 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000937}
938
aliguori376253e2009-03-05 23:01:23 +0000939static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000940{
941 int i;
bellard7e2515e2004-08-01 21:52:19 +0000942 const char *str;
ths3b46e622007-09-17 08:09:54 +0000943
aliguoricde76ee2009-03-05 23:01:51 +0000944 if (!mon->rs)
945 return;
bellard7e2515e2004-08-01 21:52:19 +0000946 i = 0;
947 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000948 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000949 if (!str)
950 break;
aliguori376253e2009-03-05 23:01:23 +0000951 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000952 i++;
bellardaa455482004-04-04 13:07:25 +0000953 }
954}
955
j_mayer76a66252007-03-07 08:32:30 +0000956#if defined(TARGET_PPC)
957/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000958static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000959{
960 CPUState *env;
961
962 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000963 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000964}
965#endif
966
Luiz Capitulinob223f352009-10-07 13:41:56 -0300967/**
968 * do_quit(): Quit QEMU execution
969 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200970static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000971{
972 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200973 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000974}
975
aliguori376253e2009-03-05 23:01:23 +0000976static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000977{
978 if (bdrv_is_inserted(bs)) {
979 if (!force) {
980 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100981 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
982 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000983 return -1;
984 }
985 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100986 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000987 return -1;
988 }
989 }
990 bdrv_close(bs);
991 }
992 return 0;
993}
994
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -0200995static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000996{
997 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300998 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200999 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +00001000
bellard9307c4c2004-04-04 12:57:25 +00001001 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +00001002 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001003 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001004 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001005 }
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001006 return eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001007}
1008
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001009static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
1010 QObject **ret_data)
1011{
1012 BlockDriverState *bs;
1013
1014 bs = bdrv_find(qdict_get_str(qdict, "device"));
1015 if (!bs) {
1016 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
1017 return;
1018 }
1019
1020 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
1021 qemu_error_new(QERR_INVALID_PASSWORD);
1022 }
1023}
1024
aliguori376253e2009-03-05 23:01:23 +00001025static void do_change_block(Monitor *mon, const char *device,
1026 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001027{
1028 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001029 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001030
bellard9307c4c2004-04-04 12:57:25 +00001031 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001032 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001033 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001034 return;
1035 }
aurel322ecea9b2008-06-18 22:10:01 +00001036 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001037 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001038 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001039 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001040 return;
1041 }
1042 }
aliguori376253e2009-03-05 23:01:23 +00001043 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001044 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001045 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001046 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001047}
1048
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001049static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001050{
1051 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001052 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001053
Markus Armbruster2895e072009-12-07 21:37:01 +01001054}
1055
1056static void change_vnc_password_cb(Monitor *mon, const char *password,
1057 void *opaque)
1058{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001059 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001060 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001061}
1062
aliguori376253e2009-03-05 23:01:23 +00001063static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001064{
ths70848512007-08-25 01:37:05 +00001065 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001066 strcmp(target, "password") == 0) {
1067 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001068 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001069 strncpy(password, arg, sizeof(password));
1070 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001071 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001072 } else {
aliguori376253e2009-03-05 23:01:23 +00001073 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001074 }
ths70848512007-08-25 01:37:05 +00001075 } else {
aliguori28a76be2009-03-06 20:27:40 +00001076 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001077 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001078 }
thse25a5822007-08-25 01:36:20 +00001079}
1080
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001081/**
1082 * do_change(): Change a removable medium, or VNC configuration
1083 */
1084static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001085{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001086 const char *device = qdict_get_str(qdict, "device");
1087 const char *target = qdict_get_str(qdict, "target");
1088 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001089 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001090 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001091 } else {
aliguori28a76be2009-03-06 20:27:40 +00001092 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001093 }
1094}
1095
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001096static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001097{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001098 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001099}
1100
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001101static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001102{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001103 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001104}
1105
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001106static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001107{
1108 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001109 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001110
bellard9307c4c2004-04-04 12:57:25 +00001111 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001112 mask = 0;
1113 } else {
bellard9307c4c2004-04-04 12:57:25 +00001114 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001115 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001116 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001117 return;
1118 }
1119 }
1120 cpu_set_log(mask);
1121}
1122
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001123static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001124{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001125 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001126 if (!option || !strcmp(option, "on")) {
1127 singlestep = 1;
1128 } else if (!strcmp(option, "off")) {
1129 singlestep = 0;
1130 } else {
1131 monitor_printf(mon, "unexpected option %s\n", option);
1132 }
1133}
1134
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001135/**
1136 * do_stop(): Stop VM execution
1137 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001138static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001139{
1140 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001141 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001142}
1143
aliguoribb5fc202009-03-05 23:01:15 +00001144static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001145
aliguori376253e2009-03-05 23:01:23 +00001146struct bdrv_iterate_context {
1147 Monitor *mon;
1148 int err;
1149};
aliguoric0f4ce72009-03-05 23:01:01 +00001150
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001151/**
1152 * do_cont(): Resume emulation.
1153 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001154static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001155{
1156 struct bdrv_iterate_context context = { mon, 0 };
1157
1158 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001159 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001160 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001161 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001162 return 0;
1163 } else {
1164 return -1;
1165 }
bellard8a7ddc32004-03-31 19:00:16 +00001166}
1167
aliguoribb5fc202009-03-05 23:01:15 +00001168static void bdrv_key_cb(void *opaque, int err)
1169{
aliguori376253e2009-03-05 23:01:23 +00001170 Monitor *mon = opaque;
1171
aliguoribb5fc202009-03-05 23:01:15 +00001172 /* another key was set successfully, retry to continue */
1173 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001174 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001175}
1176
1177static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1178{
aliguori376253e2009-03-05 23:01:23 +00001179 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001180
aliguori376253e2009-03-05 23:01:23 +00001181 if (!context->err && bdrv_key_required(bs)) {
1182 context->err = -EBUSY;
1183 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1184 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001185 }
1186}
1187
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001188static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001189{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001190 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001191 if (!device)
1192 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1193 if (gdbserver_start(device) < 0) {
1194 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1195 device);
1196 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001197 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001198 } else {
aliguori59030a82009-04-05 18:43:41 +00001199 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1200 device);
bellard8a7ddc32004-03-31 19:00:16 +00001201 }
1202}
1203
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001204static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001205{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001206 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001207 if (select_watchdog_action(action) == -1) {
1208 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1209 }
1210}
1211
aliguori376253e2009-03-05 23:01:23 +00001212static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001213{
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001215 switch(c) {
1216 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001227 break;
1228 default:
1229 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001231 } else {
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001233 }
1234 break;
1235 }
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001237}
1238
aliguori376253e2009-03-05 23:01:23 +00001239static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001240 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001241{
bellard6a00d602005-11-21 23:25:50 +00001242 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001243 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001244 uint8_t buf[16];
1245 uint64_t v;
1246
1247 if (format == 'i') {
1248 int flags;
1249 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001250 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001251 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001252 return;
bellard9307c4c2004-04-04 12:57:25 +00001253#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001254 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001255 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001256 } else if (wsize == 4) {
1257 flags = 0;
1258 } else {
bellard6a15fd12006-04-12 21:07:07 +00001259 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001260 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001261 if (env) {
1262#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001263 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001264 (env->segs[R_CS].flags & DESC_L_MASK))
1265 flags = 2;
1266 else
1267#endif
1268 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1269 flags = 1;
1270 }
bellard4c27ba22004-04-25 18:05:08 +00001271 }
1272#endif
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001274 return;
1275 }
1276
1277 len = wsize * count;
1278 if (wsize == 1)
1279 line_size = 8;
1280 else
1281 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001282 max_digits = 0;
1283
1284 switch(format) {
1285 case 'o':
1286 max_digits = (wsize * 8 + 2) / 3;
1287 break;
1288 default:
1289 case 'x':
1290 max_digits = (wsize * 8) / 4;
1291 break;
1292 case 'u':
1293 case 'd':
1294 max_digits = (wsize * 8 * 10 + 32) / 33;
1295 break;
1296 case 'c':
1297 wsize = 1;
1298 break;
1299 }
1300
1301 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001302 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001304 else
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001306 l = len;
1307 if (l > line_size)
1308 l = line_size;
1309 if (is_physical) {
1310 cpu_physical_memory_rw(addr, buf, l, 0);
1311 } else {
bellard6a00d602005-11-21 23:25:50 +00001312 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001313 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001314 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001315 break;
1316 }
bellard9307c4c2004-04-04 12:57:25 +00001317 }
ths5fafdf22007-09-16 21:08:06 +00001318 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001319 while (i < l) {
1320 switch(wsize) {
1321 default:
1322 case 1:
1323 v = ldub_raw(buf + i);
1324 break;
1325 case 2:
1326 v = lduw_raw(buf + i);
1327 break;
1328 case 4:
bellard92a31b12005-02-10 22:00:52 +00001329 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001330 break;
1331 case 8:
1332 v = ldq_raw(buf + i);
1333 break;
1334 }
aliguori376253e2009-03-05 23:01:23 +00001335 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001336 switch(format) {
1337 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001338 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001339 break;
1340 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001341 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001342 break;
1343 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001347 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001350 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001351 break;
1352 }
1353 i += wsize;
1354 }
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001356 addr += l;
1357 len -= l;
1358 }
1359}
1360
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001361static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001362{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001363 int count = qdict_get_int(qdict, "count");
1364 int format = qdict_get_int(qdict, "format");
1365 int size = qdict_get_int(qdict, "size");
1366 target_long addr = qdict_get_int(qdict, "addr");
1367
aliguori376253e2009-03-05 23:01:23 +00001368 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001369}
1370
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001371static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001372{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001373 int count = qdict_get_int(qdict, "count");
1374 int format = qdict_get_int(qdict, "format");
1375 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001376 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001377
aliguori376253e2009-03-05 23:01:23 +00001378 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001379}
1380
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001381static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001382{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001383 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001384 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001385
blueswir17743e582007-09-24 18:39:04 +00001386#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001387 switch(format) {
1388 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001396 break;
1397 default:
1398 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001400 break;
1401 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001403 break;
1404 }
bellard92a31b12005-02-10 22:00:52 +00001405#else
1406 switch(format) {
1407 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001409 break;
1410 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001412 break;
1413 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001414 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001415 break;
1416 default:
1417 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001418 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001419 break;
1420 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001421 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001422 break;
1423 }
1424#endif
aliguori376253e2009-03-05 23:01:23 +00001425 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001426}
1427
Luiz Capitulino57e09452009-10-16 12:23:43 -03001428static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001429{
1430 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001431 uint32_t size = qdict_get_int(qdict, "size");
1432 const char *filename = qdict_get_str(qdict, "filename");
1433 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001434 uint32_t l;
1435 CPUState *env;
1436 uint8_t buf[1024];
1437
1438 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001439
1440 f = fopen(filename, "wb");
1441 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001442 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
bellardb371dc52007-01-03 15:20:39 +00001443 return;
1444 }
1445 while (size != 0) {
1446 l = sizeof(buf);
1447 if (l > size)
1448 l = size;
1449 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001450 if (fwrite(buf, 1, l, f) != l) {
1451 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1452 goto exit;
1453 }
bellardb371dc52007-01-03 15:20:39 +00001454 addr += l;
1455 size -= l;
1456 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001457exit:
bellardb371dc52007-01-03 15:20:39 +00001458 fclose(f);
1459}
1460
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001461static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1462 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001463{
1464 FILE *f;
1465 uint32_t l;
1466 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001467 uint32_t size = qdict_get_int(qdict, "size");
1468 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001469 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001470
1471 f = fopen(filename, "wb");
1472 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001473 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001474 return;
1475 }
1476 while (size != 0) {
1477 l = sizeof(buf);
1478 if (l > size)
1479 l = size;
1480 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001481 if (fwrite(buf, 1, l, f) != l) {
1482 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1483 goto exit;
1484 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001485 fflush(f);
1486 addr += l;
1487 size -= l;
1488 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001489exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001490 fclose(f);
1491}
1492
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001493static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001494{
1495 uint32_t addr;
1496 uint8_t buf[1];
1497 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001498 uint32_t start = qdict_get_int(qdict, "start");
1499 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001500
1501 sum = 0;
1502 for(addr = start; addr < (start + size); addr++) {
1503 cpu_physical_memory_rw(addr, buf, 1, 0);
1504 /* BSD sum algorithm ('sum' Unix command) */
1505 sum = (sum >> 1) | (sum << 15);
1506 sum += buf[0];
1507 }
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001509}
1510
bellarda3a91a32004-06-04 11:06:21 +00001511typedef struct {
1512 int keycode;
1513 const char *name;
1514} KeyDef;
1515
1516static const KeyDef key_defs[] = {
1517 { 0x2a, "shift" },
1518 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001519
bellarda3a91a32004-06-04 11:06:21 +00001520 { 0x38, "alt" },
1521 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001522 { 0x64, "altgr" },
1523 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001524 { 0x1d, "ctrl" },
1525 { 0x9d, "ctrl_r" },
1526
1527 { 0xdd, "menu" },
1528
1529 { 0x01, "esc" },
1530
1531 { 0x02, "1" },
1532 { 0x03, "2" },
1533 { 0x04, "3" },
1534 { 0x05, "4" },
1535 { 0x06, "5" },
1536 { 0x07, "6" },
1537 { 0x08, "7" },
1538 { 0x09, "8" },
1539 { 0x0a, "9" },
1540 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001541 { 0x0c, "minus" },
1542 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001543 { 0x0e, "backspace" },
1544
1545 { 0x0f, "tab" },
1546 { 0x10, "q" },
1547 { 0x11, "w" },
1548 { 0x12, "e" },
1549 { 0x13, "r" },
1550 { 0x14, "t" },
1551 { 0x15, "y" },
1552 { 0x16, "u" },
1553 { 0x17, "i" },
1554 { 0x18, "o" },
1555 { 0x19, "p" },
1556
1557 { 0x1c, "ret" },
1558
1559 { 0x1e, "a" },
1560 { 0x1f, "s" },
1561 { 0x20, "d" },
1562 { 0x21, "f" },
1563 { 0x22, "g" },
1564 { 0x23, "h" },
1565 { 0x24, "j" },
1566 { 0x25, "k" },
1567 { 0x26, "l" },
1568
1569 { 0x2c, "z" },
1570 { 0x2d, "x" },
1571 { 0x2e, "c" },
1572 { 0x2f, "v" },
1573 { 0x30, "b" },
1574 { 0x31, "n" },
1575 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001576 { 0x33, "comma" },
1577 { 0x34, "dot" },
1578 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001579
balrog4d3b6f62008-02-10 16:33:14 +00001580 { 0x37, "asterisk" },
1581
bellarda3a91a32004-06-04 11:06:21 +00001582 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001583 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001584 { 0x3b, "f1" },
1585 { 0x3c, "f2" },
1586 { 0x3d, "f3" },
1587 { 0x3e, "f4" },
1588 { 0x3f, "f5" },
1589 { 0x40, "f6" },
1590 { 0x41, "f7" },
1591 { 0x42, "f8" },
1592 { 0x43, "f9" },
1593 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001594 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001595 { 0x46, "scroll_lock" },
1596
bellard64866c32006-05-07 18:03:31 +00001597 { 0xb5, "kp_divide" },
1598 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001599 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001600 { 0x4e, "kp_add" },
1601 { 0x9c, "kp_enter" },
1602 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001603 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001604
1605 { 0x52, "kp_0" },
1606 { 0x4f, "kp_1" },
1607 { 0x50, "kp_2" },
1608 { 0x51, "kp_3" },
1609 { 0x4b, "kp_4" },
1610 { 0x4c, "kp_5" },
1611 { 0x4d, "kp_6" },
1612 { 0x47, "kp_7" },
1613 { 0x48, "kp_8" },
1614 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001615
bellarda3a91a32004-06-04 11:06:21 +00001616 { 0x56, "<" },
1617
1618 { 0x57, "f11" },
1619 { 0x58, "f12" },
1620
1621 { 0xb7, "print" },
1622
1623 { 0xc7, "home" },
1624 { 0xc9, "pgup" },
1625 { 0xd1, "pgdn" },
1626 { 0xcf, "end" },
1627
1628 { 0xcb, "left" },
1629 { 0xc8, "up" },
1630 { 0xd0, "down" },
1631 { 0xcd, "right" },
1632
1633 { 0xd2, "insert" },
1634 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001635#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1636 { 0xf0, "stop" },
1637 { 0xf1, "again" },
1638 { 0xf2, "props" },
1639 { 0xf3, "undo" },
1640 { 0xf4, "front" },
1641 { 0xf5, "copy" },
1642 { 0xf6, "open" },
1643 { 0xf7, "paste" },
1644 { 0xf8, "find" },
1645 { 0xf9, "cut" },
1646 { 0xfa, "lf" },
1647 { 0xfb, "help" },
1648 { 0xfc, "meta_l" },
1649 { 0xfd, "meta_r" },
1650 { 0xfe, "compose" },
1651#endif
bellarda3a91a32004-06-04 11:06:21 +00001652 { 0, NULL },
1653};
1654
1655static int get_keycode(const char *key)
1656{
1657 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001658 char *endp;
1659 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001660
1661 for(p = key_defs; p->name != NULL; p++) {
1662 if (!strcmp(key, p->name))
1663 return p->keycode;
1664 }
bellard64866c32006-05-07 18:03:31 +00001665 if (strstart(key, "0x", NULL)) {
1666 ret = strtoul(key, &endp, 0);
1667 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1668 return ret;
1669 }
bellarda3a91a32004-06-04 11:06:21 +00001670 return -1;
1671}
1672
balrogc8256f92008-06-08 22:45:01 +00001673#define MAX_KEYCODES 16
1674static uint8_t keycodes[MAX_KEYCODES];
1675static int nb_pending_keycodes;
1676static QEMUTimer *key_timer;
1677
1678static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001679{
balrogc8256f92008-06-08 22:45:01 +00001680 int keycode;
1681
1682 while (nb_pending_keycodes > 0) {
1683 nb_pending_keycodes--;
1684 keycode = keycodes[nb_pending_keycodes];
1685 if (keycode & 0x80)
1686 kbd_put_keycode(0xe0);
1687 kbd_put_keycode(keycode | 0x80);
1688 }
1689}
1690
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001691static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001692{
balrog3401c0d2008-06-04 10:05:59 +00001693 char keyname_buf[16];
1694 char *separator;
1695 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001696 const char *string = qdict_get_str(qdict, "string");
1697 int has_hold_time = qdict_haskey(qdict, "hold_time");
1698 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001699
balrogc8256f92008-06-08 22:45:01 +00001700 if (nb_pending_keycodes > 0) {
1701 qemu_del_timer(key_timer);
1702 release_keys(NULL);
1703 }
1704 if (!has_hold_time)
1705 hold_time = 100;
1706 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001707 while (1) {
1708 separator = strchr(string, '-');
1709 keyname_len = separator ? separator - string : strlen(string);
1710 if (keyname_len > 0) {
1711 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1712 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001713 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001714 return;
bellarda3a91a32004-06-04 11:06:21 +00001715 }
balrogc8256f92008-06-08 22:45:01 +00001716 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001717 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001718 return;
1719 }
1720 keyname_buf[keyname_len] = 0;
1721 keycode = get_keycode(keyname_buf);
1722 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001723 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001724 return;
1725 }
balrogc8256f92008-06-08 22:45:01 +00001726 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001727 }
balrog3401c0d2008-06-04 10:05:59 +00001728 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001729 break;
balrog3401c0d2008-06-04 10:05:59 +00001730 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001731 }
balrogc8256f92008-06-08 22:45:01 +00001732 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001733 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001734 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001735 keycode = keycodes[i];
1736 if (keycode & 0x80)
1737 kbd_put_keycode(0xe0);
1738 kbd_put_keycode(keycode & 0x7f);
1739 }
balrogc8256f92008-06-08 22:45:01 +00001740 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001741 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001742 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001743}
1744
bellard13224a82006-07-14 22:03:35 +00001745static int mouse_button_state;
1746
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001747static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001748{
1749 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001750 const char *dx_str = qdict_get_str(qdict, "dx_str");
1751 const char *dy_str = qdict_get_str(qdict, "dy_str");
1752 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001753 dx = strtol(dx_str, NULL, 0);
1754 dy = strtol(dy_str, NULL, 0);
1755 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001756 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001757 dz = strtol(dz_str, NULL, 0);
1758 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1759}
1760
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001761static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001762{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001763 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001764 mouse_button_state = button_state;
1765 kbd_mouse_event(0, 0, 0, mouse_button_state);
1766}
1767
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001768static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001769{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001770 int size = qdict_get_int(qdict, "size");
1771 int addr = qdict_get_int(qdict, "addr");
1772 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001773 uint32_t val;
1774 int suffix;
1775
1776 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001777 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001778 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001779 addr++;
1780 }
1781 addr &= 0xffff;
1782
1783 switch(size) {
1784 default:
1785 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001786 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001787 suffix = 'b';
1788 break;
1789 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001790 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001791 suffix = 'w';
1792 break;
1793 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001794 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001795 suffix = 'l';
1796 break;
1797 }
aliguori376253e2009-03-05 23:01:23 +00001798 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1799 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001800}
bellarda3a91a32004-06-04 11:06:21 +00001801
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001802static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001803{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001804 int size = qdict_get_int(qdict, "size");
1805 int addr = qdict_get_int(qdict, "addr");
1806 int val = qdict_get_int(qdict, "val");
1807
Jan Kiszkaf1147842009-07-14 10:20:11 +02001808 addr &= IOPORTS_MASK;
1809
1810 switch (size) {
1811 default:
1812 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001813 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001814 break;
1815 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001816 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001817 break;
1818 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001819 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001820 break;
1821 }
1822}
1823
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001824static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001825{
1826 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001827 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001828
Jan Kiszka76e30d02009-07-02 00:19:02 +02001829 res = qemu_boot_set(bootdevice);
1830 if (res == 0) {
1831 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1832 } else if (res > 0) {
1833 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001834 } else {
aliguori376253e2009-03-05 23:01:23 +00001835 monitor_printf(mon, "no function defined to set boot device list for "
1836 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001837 }
1838}
1839
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001840/**
1841 * do_system_reset(): Issue a machine reset
1842 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001843static int do_system_reset(Monitor *mon, const QDict *qdict,
1844 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001845{
1846 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001847 return 0;
bellarde4f90822004-06-20 12:35:44 +00001848}
1849
Luiz Capitulino43076662009-10-07 13:41:59 -03001850/**
1851 * do_system_powerdown(): Issue a machine powerdown
1852 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001853static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1854 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001855{
1856 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001857 return 0;
bellard34751872005-07-02 14:31:34 +00001858}
1859
bellardb86bda52004-09-18 19:32:46 +00001860#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001861static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001862{
aliguori376253e2009-03-05 23:01:23 +00001863 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1864 addr,
1865 pte & mask,
1866 pte & PG_GLOBAL_MASK ? 'G' : '-',
1867 pte & PG_PSE_MASK ? 'P' : '-',
1868 pte & PG_DIRTY_MASK ? 'D' : '-',
1869 pte & PG_ACCESSED_MASK ? 'A' : '-',
1870 pte & PG_PCD_MASK ? 'C' : '-',
1871 pte & PG_PWT_MASK ? 'T' : '-',
1872 pte & PG_USER_MASK ? 'U' : '-',
1873 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001874}
1875
aliguori376253e2009-03-05 23:01:23 +00001876static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001877{
bellard6a00d602005-11-21 23:25:50 +00001878 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001879 int l1, l2;
1880 uint32_t pgd, pde, pte;
1881
bellard6a00d602005-11-21 23:25:50 +00001882 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001883
bellardb86bda52004-09-18 19:32:46 +00001884 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001885 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001886 return;
1887 }
1888 pgd = env->cr[3] & ~0xfff;
1889 for(l1 = 0; l1 < 1024; l1++) {
1890 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1891 pde = le32_to_cpu(pde);
1892 if (pde & PG_PRESENT_MASK) {
1893 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001894 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001895 } else {
1896 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001897 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001898 (uint8_t *)&pte, 4);
1899 pte = le32_to_cpu(pte);
1900 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001901 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001902 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001903 ~0xfff);
1904 }
1905 }
1906 }
1907 }
1908 }
1909}
1910
aliguori376253e2009-03-05 23:01:23 +00001911static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001912 uint32_t end, int prot)
1913{
bellard9746b152004-11-11 18:30:24 +00001914 int prot1;
1915 prot1 = *plast_prot;
1916 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001917 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001918 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1919 *pstart, end, end - *pstart,
1920 prot1 & PG_USER_MASK ? 'u' : '-',
1921 'r',
1922 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001923 }
1924 if (prot != 0)
1925 *pstart = end;
1926 else
1927 *pstart = -1;
1928 *plast_prot = prot;
1929 }
1930}
1931
aliguori376253e2009-03-05 23:01:23 +00001932static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001933{
bellard6a00d602005-11-21 23:25:50 +00001934 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001935 int l1, l2, prot, last_prot;
1936 uint32_t pgd, pde, pte, start, end;
1937
bellard6a00d602005-11-21 23:25:50 +00001938 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001939
bellardb86bda52004-09-18 19:32:46 +00001940 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001941 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001942 return;
1943 }
1944 pgd = env->cr[3] & ~0xfff;
1945 last_prot = 0;
1946 start = -1;
1947 for(l1 = 0; l1 < 1024; l1++) {
1948 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1949 pde = le32_to_cpu(pde);
1950 end = l1 << 22;
1951 if (pde & PG_PRESENT_MASK) {
1952 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1953 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001954 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001955 } else {
1956 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001957 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001958 (uint8_t *)&pte, 4);
1959 pte = le32_to_cpu(pte);
1960 end = (l1 << 22) + (l2 << 12);
1961 if (pte & PG_PRESENT_MASK) {
1962 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1963 } else {
1964 prot = 0;
1965 }
aliguori376253e2009-03-05 23:01:23 +00001966 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001967 }
1968 }
1969 } else {
1970 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001971 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001972 }
1973 }
1974}
1975#endif
1976
aurel327c664e22009-03-03 06:12:22 +00001977#if defined(TARGET_SH4)
1978
aliguori376253e2009-03-05 23:01:23 +00001979static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001980{
aliguori376253e2009-03-05 23:01:23 +00001981 monitor_printf(mon, " tlb%i:\t"
1982 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1983 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1984 "dirty=%hhu writethrough=%hhu\n",
1985 idx,
1986 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1987 tlb->v, tlb->sh, tlb->c, tlb->pr,
1988 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001989}
1990
aliguori376253e2009-03-05 23:01:23 +00001991static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001992{
1993 CPUState *env = mon_get_cpu();
1994 int i;
1995
aliguori376253e2009-03-05 23:01:23 +00001996 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001997 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001998 print_tlb (mon, i, &env->itlb[i]);
1999 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002000 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002001 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002002}
2003
2004#endif
2005
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002006static void do_info_kvm_print(Monitor *mon, const QObject *data)
2007{
2008 QDict *qdict;
2009
2010 qdict = qobject_to_qdict(data);
2011
2012 monitor_printf(mon, "kvm support: ");
2013 if (qdict_get_bool(qdict, "present")) {
2014 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2015 "enabled" : "disabled");
2016 } else {
2017 monitor_printf(mon, "not compiled\n");
2018 }
2019}
2020
2021/**
2022 * do_info_kvm(): Show KVM information
2023 *
2024 * Return a QDict with the following information:
2025 *
2026 * - "enabled": true if KVM support is enabled, false otherwise
2027 * - "present": true if QEMU has KVM support, false otherwise
2028 *
2029 * Example:
2030 *
2031 * { "enabled": true, "present": true }
2032 */
2033static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002034{
2035#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002036 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2037 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002038#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002039 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002040#endif
2041}
2042
aliguori030ea372009-04-21 22:30:47 +00002043static void do_info_numa(Monitor *mon)
2044{
aliguorib28b6232009-04-22 20:20:29 +00002045 int i;
aliguori030ea372009-04-21 22:30:47 +00002046 CPUState *env;
2047
2048 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2049 for (i = 0; i < nb_numa_nodes; i++) {
2050 monitor_printf(mon, "node %d cpus:", i);
2051 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2052 if (env->numa_node == i) {
2053 monitor_printf(mon, " %d", env->cpu_index);
2054 }
2055 }
2056 monitor_printf(mon, "\n");
2057 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2058 node_mem[i] >> 20);
2059 }
2060}
2061
bellard5f1ce942006-02-08 22:40:15 +00002062#ifdef CONFIG_PROFILER
2063
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002064int64_t qemu_time;
2065int64_t dev_time;
2066
aliguori376253e2009-03-05 23:01:23 +00002067static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002068{
2069 int64_t total;
2070 total = qemu_time;
2071 if (total == 0)
2072 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002073 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002074 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002075 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002076 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002077 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002078 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002079}
2080#else
aliguori376253e2009-03-05 23:01:23 +00002081static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002082{
aliguori376253e2009-03-05 23:01:23 +00002083 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002084}
2085#endif
2086
bellardec36b692006-07-16 18:57:03 +00002087/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002088static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002089
aliguori376253e2009-03-05 23:01:23 +00002090static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002091{
2092 int i;
2093 CaptureState *s;
2094
2095 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002096 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002097 s->ops.info (s->opaque);
2098 }
2099}
2100
Blue Swirl23130862009-06-06 08:22:04 +00002101#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002102static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002103{
2104 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002105 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002106 CaptureState *s;
2107
2108 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2109 if (i == n) {
2110 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002111 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002112 qemu_free (s);
2113 return;
2114 }
2115 }
2116}
2117
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002118static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002119{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002120 const char *path = qdict_get_str(qdict, "path");
2121 int has_freq = qdict_haskey(qdict, "freq");
2122 int freq = qdict_get_try_int(qdict, "freq", -1);
2123 int has_bits = qdict_haskey(qdict, "bits");
2124 int bits = qdict_get_try_int(qdict, "bits", -1);
2125 int has_channels = qdict_haskey(qdict, "nchannels");
2126 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002127 CaptureState *s;
2128
2129 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002130
2131 freq = has_freq ? freq : 44100;
2132 bits = has_bits ? bits : 16;
2133 nchannels = has_channels ? nchannels : 2;
2134
2135 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002136 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002137 qemu_free (s);
2138 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002139 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002140}
2141#endif
2142
aurel32dc1c0b72008-04-27 23:52:12 +00002143#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002144static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002145{
2146 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002147 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002148
2149 for (env = first_cpu; env != NULL; env = env->next_cpu)
2150 if (env->cpu_index == cpu_index) {
2151 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2152 break;
2153 }
2154}
2155#endif
2156
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002157static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002158{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002159 QDict *qdict;
2160
2161 qdict = qobject_to_qdict(data);
2162
2163 monitor_printf(mon, "VM status: ");
2164 if (qdict_get_bool(qdict, "running")) {
2165 monitor_printf(mon, "running");
2166 if (qdict_get_bool(qdict, "singlestep")) {
2167 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002168 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002169 } else {
2170 monitor_printf(mon, "paused");
2171 }
2172
2173 monitor_printf(mon, "\n");
2174}
2175
2176/**
2177 * do_info_status(): VM status
2178 *
2179 * Return a QDict with the following information:
2180 *
2181 * - "running": true if the VM is running, or false if it is paused
2182 * - "singlestep": true if the VM is in single step mode, false otherwise
2183 *
2184 * Example:
2185 *
2186 * { "running": true, "singlestep": false }
2187 */
2188static void do_info_status(Monitor *mon, QObject **ret_data)
2189{
2190 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2191 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002192}
2193
Adam Litke625a5be2010-01-26 14:17:35 -06002194static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002195{
Adam Litke625a5be2010-01-26 14:17:35 -06002196 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002197
Adam Litke625a5be2010-01-26 14:17:35 -06002198 if (strcmp(key, "actual"))
2199 monitor_printf(mon, ",%s=%" PRId64, key,
2200 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002201}
2202
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002203static void monitor_print_balloon(Monitor *mon, const QObject *data)
2204{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002205 QDict *qdict;
2206
2207 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002208 if (!qdict_haskey(qdict, "actual"))
2209 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002210
Adam Litke625a5be2010-01-26 14:17:35 -06002211 monitor_printf(mon, "balloon: actual=%" PRId64,
2212 qdict_get_int(qdict, "actual") >> 20);
2213 qdict_iter(qdict, print_balloon_stat, mon);
2214 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002215}
2216
2217/**
2218 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002219 *
Adam Litke625a5be2010-01-26 14:17:35 -06002220 * Make an asynchronous request for balloon info. When the request completes
2221 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002222 *
Adam Litke625a5be2010-01-26 14:17:35 -06002223 * - "actual": current balloon value in bytes
2224 * The following fields may or may not be present:
2225 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2226 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2227 * - "major_page_faults": Number of major faults
2228 * - "minor_page_faults": Number of minor faults
2229 * - "free_mem": Total amount of free and unused memory (bytes)
2230 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002231 *
2232 * Example:
2233 *
Adam Litke625a5be2010-01-26 14:17:35 -06002234 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2235 * "major_page_faults": 142, "minor_page_faults": 239245,
2236 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002237 */
Adam Litke625a5be2010-01-26 14:17:35 -06002238static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002239{
Adam Litke625a5be2010-01-26 14:17:35 -06002240 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002241
Adam Litke625a5be2010-01-26 14:17:35 -06002242 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2243 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2244 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002245 }
Adam Litke625a5be2010-01-26 14:17:35 -06002246
2247 ret = qemu_balloon_status(cb, opaque);
2248 if (!ret) {
2249 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2250 return -1;
2251 }
2252
2253 return 0;
2254}
2255
2256/**
2257 * do_balloon(): Request VM to change its memory allocation
2258 */
2259static int do_balloon(Monitor *mon, const QDict *params,
2260 MonitorCompletion cb, void *opaque)
2261{
2262 int ret;
2263
2264 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2265 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2266 return -1;
2267 }
2268
2269 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2270 if (ret == 0) {
2271 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2272 return -1;
2273 }
2274
2275 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002276}
2277
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002278static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002279{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002280 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002281
aliguori76655d62009-03-06 20:27:37 +00002282 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002283 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002284 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002285 return acl;
2286}
aliguori76655d62009-03-06 20:27:37 +00002287
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002288static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002289{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002290 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002291 qemu_acl *acl = find_acl(mon, aclname);
2292 qemu_acl_entry *entry;
2293 int i = 0;
2294
2295 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002296 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002297 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002298 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002299 i++;
2300 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002301 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002302 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002303 }
2304}
2305
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002306static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002307{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002308 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002309 qemu_acl *acl = find_acl(mon, aclname);
2310
2311 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002312 qemu_acl_reset(acl);
2313 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002314 }
2315}
aliguori76655d62009-03-06 20:27:37 +00002316
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002317static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002318{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002319 const char *aclname = qdict_get_str(qdict, "aclname");
2320 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002321 qemu_acl *acl = find_acl(mon, aclname);
2322
2323 if (acl) {
2324 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002325 acl->defaultDeny = 0;
2326 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002327 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002328 acl->defaultDeny = 1;
2329 monitor_printf(mon, "acl: policy set to 'deny'\n");
2330 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002331 monitor_printf(mon, "acl: unknown policy '%s', "
2332 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002333 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002334 }
2335}
aliguori76655d62009-03-06 20:27:37 +00002336
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002337static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002338{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002339 const char *aclname = qdict_get_str(qdict, "aclname");
2340 const char *match = qdict_get_str(qdict, "match");
2341 const char *policy = qdict_get_str(qdict, "policy");
2342 int has_index = qdict_haskey(qdict, "index");
2343 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002344 qemu_acl *acl = find_acl(mon, aclname);
2345 int deny, ret;
2346
2347 if (acl) {
2348 if (strcmp(policy, "allow") == 0) {
2349 deny = 0;
2350 } else if (strcmp(policy, "deny") == 0) {
2351 deny = 1;
2352 } else {
2353 monitor_printf(mon, "acl: unknown policy '%s', "
2354 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002355 return;
2356 }
aliguori28a76be2009-03-06 20:27:40 +00002357 if (has_index)
2358 ret = qemu_acl_insert(acl, deny, match, index);
2359 else
2360 ret = qemu_acl_append(acl, deny, match);
2361 if (ret < 0)
2362 monitor_printf(mon, "acl: unable to add acl entry\n");
2363 else
2364 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002365 }
2366}
aliguori76655d62009-03-06 20:27:37 +00002367
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002368static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002369{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002370 const char *aclname = qdict_get_str(qdict, "aclname");
2371 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002372 qemu_acl *acl = find_acl(mon, aclname);
2373 int ret;
aliguori76655d62009-03-06 20:27:37 +00002374
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002375 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002376 ret = qemu_acl_remove(acl, match);
2377 if (ret < 0)
2378 monitor_printf(mon, "acl: no matching acl entry\n");
2379 else
2380 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002381 }
2382}
2383
Huang Ying79c4f6b2009-06-23 10:05:14 +08002384#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002385static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002386{
2387 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002388 int cpu_index = qdict_get_int(qdict, "cpu_index");
2389 int bank = qdict_get_int(qdict, "bank");
2390 uint64_t status = qdict_get_int(qdict, "status");
2391 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2392 uint64_t addr = qdict_get_int(qdict, "addr");
2393 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002394
2395 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2396 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2397 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2398 break;
2399 }
2400}
2401#endif
2402
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002403static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002404{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002405 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002406 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002407 int fd;
2408
2409 fd = qemu_chr_get_msgfd(mon->chr);
2410 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002411 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002412 return;
2413 }
2414
2415 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002416 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002417 return;
2418 }
2419
2420 fd = dup(fd);
2421 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002422 if (errno == EMFILE)
2423 qemu_error_new(QERR_TOO_MANY_FILES);
2424 else
2425 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002426 return;
2427 }
2428
Blue Swirl72cf2d42009-09-12 07:36:22 +00002429 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002430 if (strcmp(monfd->name, fdname) != 0) {
2431 continue;
2432 }
2433
2434 close(monfd->fd);
2435 monfd->fd = fd;
2436 return;
2437 }
2438
Anthony Liguoric227f092009-10-01 16:12:16 -05002439 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002440 monfd->name = qemu_strdup(fdname);
2441 monfd->fd = fd;
2442
Blue Swirl72cf2d42009-09-12 07:36:22 +00002443 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002444}
2445
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002446static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002447{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002448 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002449 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002450
Blue Swirl72cf2d42009-09-12 07:36:22 +00002451 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002452 if (strcmp(monfd->name, fdname) != 0) {
2453 continue;
2454 }
2455
Blue Swirl72cf2d42009-09-12 07:36:22 +00002456 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002457 close(monfd->fd);
2458 qemu_free(monfd->name);
2459 qemu_free(monfd);
2460 return;
2461 }
2462
Markus Armbruster063c1a02009-12-07 21:37:11 +01002463 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002464}
2465
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002466static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002467{
2468 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002469 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002470
2471 vm_stop(0);
2472
Juan Quintela05f24012009-08-20 19:42:22 +02002473 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002474 vm_start();
2475}
2476
Mark McLoughlin7768e042009-07-22 09:11:41 +01002477int monitor_get_fd(Monitor *mon, const char *fdname)
2478{
Anthony Liguoric227f092009-10-01 16:12:16 -05002479 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002480
Blue Swirl72cf2d42009-09-12 07:36:22 +00002481 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002482 int fd;
2483
2484 if (strcmp(monfd->name, fdname) != 0) {
2485 continue;
2486 }
2487
2488 fd = monfd->fd;
2489
2490 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002491 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002492 qemu_free(monfd->name);
2493 qemu_free(monfd);
2494
2495 return fd;
2496 }
2497
2498 return -1;
2499}
2500
Anthony Liguoric227f092009-10-01 16:12:16 -05002501static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002502#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002503 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002504};
2505
Blue Swirl23130862009-06-06 08:22:04 +00002506/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002507static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002508 {
2509 .name = "version",
2510 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 .params = "",
2512 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002513 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002514 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002515 },
2516 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002517 .name = "commands",
2518 .args_type = "",
2519 .params = "",
2520 .help = "list QMP available commands",
2521 .user_print = monitor_user_noop,
2522 .mhandler.info_new = do_info_commands,
2523 },
2524 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 .name = "network",
2526 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 .params = "",
2528 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002529 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 },
2531 {
2532 .name = "chardev",
2533 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 .params = "",
2535 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002536 .user_print = qemu_chr_info_print,
2537 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 },
2539 {
2540 .name = "block",
2541 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 .params = "",
2543 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002544 .user_print = bdrv_info_print,
2545 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002546 },
2547 {
2548 .name = "blockstats",
2549 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002550 .params = "",
2551 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002552 .user_print = bdrv_stats_print,
2553 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 },
2555 {
2556 .name = "registers",
2557 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .params = "",
2559 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002560 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
2562 {
2563 .name = "cpus",
2564 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .params = "",
2566 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002567 .user_print = monitor_print_cpus,
2568 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 },
2570 {
2571 .name = "history",
2572 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 .params = "",
2574 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002575 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 },
2577 {
2578 .name = "irq",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002582 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 },
2584 {
2585 .name = "pic",
2586 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 .params = "",
2588 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002589 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 },
2591 {
2592 .name = "pci",
2593 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 .params = "",
2595 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002596 .user_print = do_pci_info_print,
2597 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 },
aurel327c664e22009-03-03 06:12:22 +00002599#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 {
2601 .name = "tlb",
2602 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 .params = "",
2604 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002605 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 },
aurel327c664e22009-03-03 06:12:22 +00002607#endif
2608#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 {
2610 .name = "mem",
2611 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 .params = "",
2613 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002614 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 },
2616 {
2617 .name = "hpet",
2618 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002619 .params = "",
2620 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002621 .user_print = do_info_hpet_print,
2622 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 },
bellardb86bda52004-09-18 19:32:46 +00002624#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 {
2626 .name = "jit",
2627 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 .params = "",
2629 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002630 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 },
2632 {
2633 .name = "kvm",
2634 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 .params = "",
2636 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002637 .user_print = do_info_kvm_print,
2638 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 },
2640 {
2641 .name = "numa",
2642 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .params = "",
2644 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002645 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 },
2647 {
2648 .name = "usb",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002652 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
2654 {
2655 .name = "usbhost",
2656 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 .params = "",
2658 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002659 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
2661 {
2662 .name = "profile",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002666 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "capture",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002673 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
2675 {
2676 .name = "snapshots",
2677 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 .params = "",
2679 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002680 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 },
2682 {
2683 .name = "status",
2684 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 .params = "",
2686 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002687 .user_print = do_info_status_print,
2688 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 },
2690 {
2691 .name = "pcmcia",
2692 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 .params = "",
2694 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002695 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 },
2697 {
2698 .name = "mice",
2699 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 .params = "",
2701 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002702 .user_print = do_info_mice_print,
2703 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "vnc",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002710 .user_print = do_info_vnc_print,
2711 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
2713 {
2714 .name = "name",
2715 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 .params = "",
2717 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002718 .user_print = do_info_name_print,
2719 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 },
2721 {
2722 .name = "uuid",
2723 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002724 .params = "",
2725 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002726 .user_print = do_info_uuid_print,
2727 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 },
j_mayer76a66252007-03-07 08:32:30 +00002729#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 {
2731 .name = "cpustats",
2732 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 .params = "",
2734 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002735 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 },
j_mayer76a66252007-03-07 08:32:30 +00002737#endif
blueswir131a60e22007-10-26 18:42:59 +00002738#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 {
2740 .name = "usernet",
2741 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 .params = "",
2743 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002744 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 },
blueswir131a60e22007-10-26 18:42:59 +00002746#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 {
2748 .name = "migrate",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002752 .user_print = do_info_migrate_print,
2753 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 },
2755 {
2756 .name = "balloon",
2757 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 .params = "",
2759 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002760 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002761 .mhandler.info_async = do_info_balloon,
2762 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
2764 {
2765 .name = "qtree",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002769 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
2771 {
2772 .name = "qdm",
2773 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 .params = "",
2775 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002776 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 },
2778 {
2779 .name = "roms",
2780 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 .params = "",
2782 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002783 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 },
2785 {
2786 .name = NULL,
2787 },
bellard9dc39cb2004-03-14 21:38:27 +00002788};
2789
bellard9307c4c2004-04-04 12:57:25 +00002790/*******************************************************************/
2791
2792static const char *pch;
2793static jmp_buf expr_env;
2794
bellard92a31b12005-02-10 22:00:52 +00002795#define MD_TLONG 0
2796#define MD_I32 1
2797
bellard9307c4c2004-04-04 12:57:25 +00002798typedef struct MonitorDef {
2799 const char *name;
2800 int offset;
blueswir18662d652008-10-02 18:32:44 +00002801 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002802 int type;
bellard9307c4c2004-04-04 12:57:25 +00002803} MonitorDef;
2804
bellard57206fd2004-04-25 18:54:52 +00002805#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002806static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002807{
bellard6a00d602005-11-21 23:25:50 +00002808 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002809 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002810}
2811#endif
2812
bellarda541f292004-04-12 20:39:29 +00002813#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002814static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002815{
bellard6a00d602005-11-21 23:25:50 +00002816 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002817 unsigned int u;
2818 int i;
2819
2820 u = 0;
2821 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002822 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002823
2824 return u;
2825}
2826
blueswir18662d652008-10-02 18:32:44 +00002827static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002828{
bellard6a00d602005-11-21 23:25:50 +00002829 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002830 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002831}
2832
blueswir18662d652008-10-02 18:32:44 +00002833static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002834{
bellard6a00d602005-11-21 23:25:50 +00002835 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002836 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002837}
bellard9fddaa02004-05-21 12:59:32 +00002838
blueswir18662d652008-10-02 18:32:44 +00002839static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002840{
bellard6a00d602005-11-21 23:25:50 +00002841 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002842 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002843}
2844
blueswir18662d652008-10-02 18:32:44 +00002845static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002846{
bellard6a00d602005-11-21 23:25:50 +00002847 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002848 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002849}
2850
blueswir18662d652008-10-02 18:32:44 +00002851static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002852{
bellard6a00d602005-11-21 23:25:50 +00002853 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002854 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002855}
bellarda541f292004-04-12 20:39:29 +00002856#endif
2857
bellarde95c8d52004-09-30 22:22:08 +00002858#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002859#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002860static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002861{
bellard6a00d602005-11-21 23:25:50 +00002862 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002863 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002864}
bellard7b936c02005-10-30 17:05:13 +00002865#endif
bellarde95c8d52004-09-30 22:22:08 +00002866
blueswir18662d652008-10-02 18:32:44 +00002867static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002868{
bellard6a00d602005-11-21 23:25:50 +00002869 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002870 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002871}
2872#endif
2873
blueswir18662d652008-10-02 18:32:44 +00002874static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002875#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002876
2877#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002878 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002879 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002880 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002881
bellard9307c4c2004-04-04 12:57:25 +00002882 { "eax", offsetof(CPUState, regs[0]) },
2883 { "ecx", offsetof(CPUState, regs[1]) },
2884 { "edx", offsetof(CPUState, regs[2]) },
2885 { "ebx", offsetof(CPUState, regs[3]) },
2886 { "esp|sp", offsetof(CPUState, regs[4]) },
2887 { "ebp|fp", offsetof(CPUState, regs[5]) },
2888 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002889 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002890#ifdef TARGET_X86_64
2891 { "r8", offsetof(CPUState, regs[8]) },
2892 { "r9", offsetof(CPUState, regs[9]) },
2893 { "r10", offsetof(CPUState, regs[10]) },
2894 { "r11", offsetof(CPUState, regs[11]) },
2895 { "r12", offsetof(CPUState, regs[12]) },
2896 { "r13", offsetof(CPUState, regs[13]) },
2897 { "r14", offsetof(CPUState, regs[14]) },
2898 { "r15", offsetof(CPUState, regs[15]) },
2899#endif
bellard9307c4c2004-04-04 12:57:25 +00002900 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002901 { "eip", offsetof(CPUState, eip) },
2902 SEG("cs", R_CS)
2903 SEG("ds", R_DS)
2904 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002905 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002906 SEG("fs", R_FS)
2907 SEG("gs", R_GS)
2908 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002909#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002910 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002911 { "r0", offsetof(CPUState, gpr[0]) },
2912 { "r1", offsetof(CPUState, gpr[1]) },
2913 { "r2", offsetof(CPUState, gpr[2]) },
2914 { "r3", offsetof(CPUState, gpr[3]) },
2915 { "r4", offsetof(CPUState, gpr[4]) },
2916 { "r5", offsetof(CPUState, gpr[5]) },
2917 { "r6", offsetof(CPUState, gpr[6]) },
2918 { "r7", offsetof(CPUState, gpr[7]) },
2919 { "r8", offsetof(CPUState, gpr[8]) },
2920 { "r9", offsetof(CPUState, gpr[9]) },
2921 { "r10", offsetof(CPUState, gpr[10]) },
2922 { "r11", offsetof(CPUState, gpr[11]) },
2923 { "r12", offsetof(CPUState, gpr[12]) },
2924 { "r13", offsetof(CPUState, gpr[13]) },
2925 { "r14", offsetof(CPUState, gpr[14]) },
2926 { "r15", offsetof(CPUState, gpr[15]) },
2927 { "r16", offsetof(CPUState, gpr[16]) },
2928 { "r17", offsetof(CPUState, gpr[17]) },
2929 { "r18", offsetof(CPUState, gpr[18]) },
2930 { "r19", offsetof(CPUState, gpr[19]) },
2931 { "r20", offsetof(CPUState, gpr[20]) },
2932 { "r21", offsetof(CPUState, gpr[21]) },
2933 { "r22", offsetof(CPUState, gpr[22]) },
2934 { "r23", offsetof(CPUState, gpr[23]) },
2935 { "r24", offsetof(CPUState, gpr[24]) },
2936 { "r25", offsetof(CPUState, gpr[25]) },
2937 { "r26", offsetof(CPUState, gpr[26]) },
2938 { "r27", offsetof(CPUState, gpr[27]) },
2939 { "r28", offsetof(CPUState, gpr[28]) },
2940 { "r29", offsetof(CPUState, gpr[29]) },
2941 { "r30", offsetof(CPUState, gpr[30]) },
2942 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002943 /* Floating point registers */
2944 { "f0", offsetof(CPUState, fpr[0]) },
2945 { "f1", offsetof(CPUState, fpr[1]) },
2946 { "f2", offsetof(CPUState, fpr[2]) },
2947 { "f3", offsetof(CPUState, fpr[3]) },
2948 { "f4", offsetof(CPUState, fpr[4]) },
2949 { "f5", offsetof(CPUState, fpr[5]) },
2950 { "f6", offsetof(CPUState, fpr[6]) },
2951 { "f7", offsetof(CPUState, fpr[7]) },
2952 { "f8", offsetof(CPUState, fpr[8]) },
2953 { "f9", offsetof(CPUState, fpr[9]) },
2954 { "f10", offsetof(CPUState, fpr[10]) },
2955 { "f11", offsetof(CPUState, fpr[11]) },
2956 { "f12", offsetof(CPUState, fpr[12]) },
2957 { "f13", offsetof(CPUState, fpr[13]) },
2958 { "f14", offsetof(CPUState, fpr[14]) },
2959 { "f15", offsetof(CPUState, fpr[15]) },
2960 { "f16", offsetof(CPUState, fpr[16]) },
2961 { "f17", offsetof(CPUState, fpr[17]) },
2962 { "f18", offsetof(CPUState, fpr[18]) },
2963 { "f19", offsetof(CPUState, fpr[19]) },
2964 { "f20", offsetof(CPUState, fpr[20]) },
2965 { "f21", offsetof(CPUState, fpr[21]) },
2966 { "f22", offsetof(CPUState, fpr[22]) },
2967 { "f23", offsetof(CPUState, fpr[23]) },
2968 { "f24", offsetof(CPUState, fpr[24]) },
2969 { "f25", offsetof(CPUState, fpr[25]) },
2970 { "f26", offsetof(CPUState, fpr[26]) },
2971 { "f27", offsetof(CPUState, fpr[27]) },
2972 { "f28", offsetof(CPUState, fpr[28]) },
2973 { "f29", offsetof(CPUState, fpr[29]) },
2974 { "f30", offsetof(CPUState, fpr[30]) },
2975 { "f31", offsetof(CPUState, fpr[31]) },
2976 { "fpscr", offsetof(CPUState, fpscr) },
2977 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002978 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002979 { "lr", offsetof(CPUState, lr) },
2980 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002981 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002982 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002983 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002984 { "msr", 0, &monitor_get_msr, },
2985 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002986 { "tbu", 0, &monitor_get_tbu, },
2987 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002988#if defined(TARGET_PPC64)
2989 /* Address space register */
2990 { "asr", offsetof(CPUState, asr) },
2991#endif
2992 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002993 { "sdr1", offsetof(CPUState, sdr1) },
2994 { "sr0", offsetof(CPUState, sr[0]) },
2995 { "sr1", offsetof(CPUState, sr[1]) },
2996 { "sr2", offsetof(CPUState, sr[2]) },
2997 { "sr3", offsetof(CPUState, sr[3]) },
2998 { "sr4", offsetof(CPUState, sr[4]) },
2999 { "sr5", offsetof(CPUState, sr[5]) },
3000 { "sr6", offsetof(CPUState, sr[6]) },
3001 { "sr7", offsetof(CPUState, sr[7]) },
3002 { "sr8", offsetof(CPUState, sr[8]) },
3003 { "sr9", offsetof(CPUState, sr[9]) },
3004 { "sr10", offsetof(CPUState, sr[10]) },
3005 { "sr11", offsetof(CPUState, sr[11]) },
3006 { "sr12", offsetof(CPUState, sr[12]) },
3007 { "sr13", offsetof(CPUState, sr[13]) },
3008 { "sr14", offsetof(CPUState, sr[14]) },
3009 { "sr15", offsetof(CPUState, sr[15]) },
3010 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003011#elif defined(TARGET_SPARC)
3012 { "g0", offsetof(CPUState, gregs[0]) },
3013 { "g1", offsetof(CPUState, gregs[1]) },
3014 { "g2", offsetof(CPUState, gregs[2]) },
3015 { "g3", offsetof(CPUState, gregs[3]) },
3016 { "g4", offsetof(CPUState, gregs[4]) },
3017 { "g5", offsetof(CPUState, gregs[5]) },
3018 { "g6", offsetof(CPUState, gregs[6]) },
3019 { "g7", offsetof(CPUState, gregs[7]) },
3020 { "o0", 0, monitor_get_reg },
3021 { "o1", 1, monitor_get_reg },
3022 { "o2", 2, monitor_get_reg },
3023 { "o3", 3, monitor_get_reg },
3024 { "o4", 4, monitor_get_reg },
3025 { "o5", 5, monitor_get_reg },
3026 { "o6", 6, monitor_get_reg },
3027 { "o7", 7, monitor_get_reg },
3028 { "l0", 8, monitor_get_reg },
3029 { "l1", 9, monitor_get_reg },
3030 { "l2", 10, monitor_get_reg },
3031 { "l3", 11, monitor_get_reg },
3032 { "l4", 12, monitor_get_reg },
3033 { "l5", 13, monitor_get_reg },
3034 { "l6", 14, monitor_get_reg },
3035 { "l7", 15, monitor_get_reg },
3036 { "i0", 16, monitor_get_reg },
3037 { "i1", 17, monitor_get_reg },
3038 { "i2", 18, monitor_get_reg },
3039 { "i3", 19, monitor_get_reg },
3040 { "i4", 20, monitor_get_reg },
3041 { "i5", 21, monitor_get_reg },
3042 { "i6", 22, monitor_get_reg },
3043 { "i7", 23, monitor_get_reg },
3044 { "pc", offsetof(CPUState, pc) },
3045 { "npc", offsetof(CPUState, npc) },
3046 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003047#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003048 { "psr", 0, &monitor_get_psr, },
3049 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003050#endif
bellarde95c8d52004-09-30 22:22:08 +00003051 { "tbr", offsetof(CPUState, tbr) },
3052 { "fsr", offsetof(CPUState, fsr) },
3053 { "f0", offsetof(CPUState, fpr[0]) },
3054 { "f1", offsetof(CPUState, fpr[1]) },
3055 { "f2", offsetof(CPUState, fpr[2]) },
3056 { "f3", offsetof(CPUState, fpr[3]) },
3057 { "f4", offsetof(CPUState, fpr[4]) },
3058 { "f5", offsetof(CPUState, fpr[5]) },
3059 { "f6", offsetof(CPUState, fpr[6]) },
3060 { "f7", offsetof(CPUState, fpr[7]) },
3061 { "f8", offsetof(CPUState, fpr[8]) },
3062 { "f9", offsetof(CPUState, fpr[9]) },
3063 { "f10", offsetof(CPUState, fpr[10]) },
3064 { "f11", offsetof(CPUState, fpr[11]) },
3065 { "f12", offsetof(CPUState, fpr[12]) },
3066 { "f13", offsetof(CPUState, fpr[13]) },
3067 { "f14", offsetof(CPUState, fpr[14]) },
3068 { "f15", offsetof(CPUState, fpr[15]) },
3069 { "f16", offsetof(CPUState, fpr[16]) },
3070 { "f17", offsetof(CPUState, fpr[17]) },
3071 { "f18", offsetof(CPUState, fpr[18]) },
3072 { "f19", offsetof(CPUState, fpr[19]) },
3073 { "f20", offsetof(CPUState, fpr[20]) },
3074 { "f21", offsetof(CPUState, fpr[21]) },
3075 { "f22", offsetof(CPUState, fpr[22]) },
3076 { "f23", offsetof(CPUState, fpr[23]) },
3077 { "f24", offsetof(CPUState, fpr[24]) },
3078 { "f25", offsetof(CPUState, fpr[25]) },
3079 { "f26", offsetof(CPUState, fpr[26]) },
3080 { "f27", offsetof(CPUState, fpr[27]) },
3081 { "f28", offsetof(CPUState, fpr[28]) },
3082 { "f29", offsetof(CPUState, fpr[29]) },
3083 { "f30", offsetof(CPUState, fpr[30]) },
3084 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003085#ifdef TARGET_SPARC64
3086 { "f32", offsetof(CPUState, fpr[32]) },
3087 { "f34", offsetof(CPUState, fpr[34]) },
3088 { "f36", offsetof(CPUState, fpr[36]) },
3089 { "f38", offsetof(CPUState, fpr[38]) },
3090 { "f40", offsetof(CPUState, fpr[40]) },
3091 { "f42", offsetof(CPUState, fpr[42]) },
3092 { "f44", offsetof(CPUState, fpr[44]) },
3093 { "f46", offsetof(CPUState, fpr[46]) },
3094 { "f48", offsetof(CPUState, fpr[48]) },
3095 { "f50", offsetof(CPUState, fpr[50]) },
3096 { "f52", offsetof(CPUState, fpr[52]) },
3097 { "f54", offsetof(CPUState, fpr[54]) },
3098 { "f56", offsetof(CPUState, fpr[56]) },
3099 { "f58", offsetof(CPUState, fpr[58]) },
3100 { "f60", offsetof(CPUState, fpr[60]) },
3101 { "f62", offsetof(CPUState, fpr[62]) },
3102 { "asi", offsetof(CPUState, asi) },
3103 { "pstate", offsetof(CPUState, pstate) },
3104 { "cansave", offsetof(CPUState, cansave) },
3105 { "canrestore", offsetof(CPUState, canrestore) },
3106 { "otherwin", offsetof(CPUState, otherwin) },
3107 { "wstate", offsetof(CPUState, wstate) },
3108 { "cleanwin", offsetof(CPUState, cleanwin) },
3109 { "fprs", offsetof(CPUState, fprs) },
3110#endif
bellard9307c4c2004-04-04 12:57:25 +00003111#endif
3112 { NULL },
3113};
3114
aliguori376253e2009-03-05 23:01:23 +00003115static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003116{
aliguori376253e2009-03-05 23:01:23 +00003117 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003118 longjmp(expr_env, 1);
3119}
3120
Markus Armbruster09b94182010-01-20 13:07:30 +01003121/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003122static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003123{
blueswir18662d652008-10-02 18:32:44 +00003124 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003125 void *ptr;
3126
bellard9307c4c2004-04-04 12:57:25 +00003127 for(md = monitor_defs; md->name != NULL; md++) {
3128 if (compare_cmd(name, md->name)) {
3129 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003130 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003131 } else {
bellard6a00d602005-11-21 23:25:50 +00003132 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003133 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003134 switch(md->type) {
3135 case MD_I32:
3136 *pval = *(int32_t *)ptr;
3137 break;
3138 case MD_TLONG:
3139 *pval = *(target_long *)ptr;
3140 break;
3141 default:
3142 *pval = 0;
3143 break;
3144 }
bellard9307c4c2004-04-04 12:57:25 +00003145 }
3146 return 0;
3147 }
3148 }
3149 return -1;
3150}
3151
3152static void next(void)
3153{
Blue Swirl660f11b2009-07-31 21:16:51 +00003154 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003155 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003156 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003157 pch++;
3158 }
3159}
3160
aliguori376253e2009-03-05 23:01:23 +00003161static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003162
aliguori376253e2009-03-05 23:01:23 +00003163static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003164{
blueswir1c2efc952007-09-25 17:28:42 +00003165 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003166 char *p;
bellard6a00d602005-11-21 23:25:50 +00003167 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003168
3169 switch(*pch) {
3170 case '+':
3171 next();
aliguori376253e2009-03-05 23:01:23 +00003172 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003173 break;
3174 case '-':
3175 next();
aliguori376253e2009-03-05 23:01:23 +00003176 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003177 break;
3178 case '~':
3179 next();
aliguori376253e2009-03-05 23:01:23 +00003180 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003181 break;
3182 case '(':
3183 next();
aliguori376253e2009-03-05 23:01:23 +00003184 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003185 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003186 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003187 }
3188 next();
3189 break;
bellard81d09122004-07-14 17:21:37 +00003190 case '\'':
3191 pch++;
3192 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003193 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003194 n = *pch;
3195 pch++;
3196 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003197 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003198 next();
3199 break;
bellard9307c4c2004-04-04 12:57:25 +00003200 case '$':
3201 {
3202 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003203 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003204
bellard9307c4c2004-04-04 12:57:25 +00003205 pch++;
3206 q = buf;
3207 while ((*pch >= 'a' && *pch <= 'z') ||
3208 (*pch >= 'A' && *pch <= 'Z') ||
3209 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003210 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003211 if ((q - buf) < sizeof(buf) - 1)
3212 *q++ = *pch;
3213 pch++;
3214 }
blueswir1cd390082008-11-16 13:53:32 +00003215 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003216 pch++;
3217 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003218 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003219 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003220 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003221 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003222 }
3223 break;
3224 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003225 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003226 n = 0;
3227 break;
3228 default:
blueswir17743e582007-09-24 18:39:04 +00003229#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003230 n = strtoull(pch, &p, 0);
3231#else
bellard9307c4c2004-04-04 12:57:25 +00003232 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003233#endif
bellard9307c4c2004-04-04 12:57:25 +00003234 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003235 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003236 }
3237 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003238 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003239 pch++;
3240 break;
3241 }
3242 return n;
3243}
3244
3245
aliguori376253e2009-03-05 23:01:23 +00003246static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003247{
blueswir1c2efc952007-09-25 17:28:42 +00003248 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003249 int op;
ths3b46e622007-09-17 08:09:54 +00003250
aliguori376253e2009-03-05 23:01:23 +00003251 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003252 for(;;) {
3253 op = *pch;
3254 if (op != '*' && op != '/' && op != '%')
3255 break;
3256 next();
aliguori376253e2009-03-05 23:01:23 +00003257 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003258 switch(op) {
3259 default:
3260 case '*':
3261 val *= val2;
3262 break;
3263 case '/':
3264 case '%':
ths5fafdf22007-09-16 21:08:06 +00003265 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003266 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003267 if (op == '/')
3268 val /= val2;
3269 else
3270 val %= val2;
3271 break;
3272 }
3273 }
3274 return val;
3275}
3276
aliguori376253e2009-03-05 23:01:23 +00003277static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003278{
blueswir1c2efc952007-09-25 17:28:42 +00003279 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003280 int op;
bellard9307c4c2004-04-04 12:57:25 +00003281
aliguori376253e2009-03-05 23:01:23 +00003282 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003283 for(;;) {
3284 op = *pch;
3285 if (op != '&' && op != '|' && op != '^')
3286 break;
3287 next();
aliguori376253e2009-03-05 23:01:23 +00003288 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003289 switch(op) {
3290 default:
3291 case '&':
3292 val &= val2;
3293 break;
3294 case '|':
3295 val |= val2;
3296 break;
3297 case '^':
3298 val ^= val2;
3299 break;
3300 }
3301 }
3302 return val;
3303}
3304
aliguori376253e2009-03-05 23:01:23 +00003305static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003306{
blueswir1c2efc952007-09-25 17:28:42 +00003307 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003308 int op;
bellard9307c4c2004-04-04 12:57:25 +00003309
aliguori376253e2009-03-05 23:01:23 +00003310 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003311 for(;;) {
3312 op = *pch;
3313 if (op != '+' && op != '-')
3314 break;
3315 next();
aliguori376253e2009-03-05 23:01:23 +00003316 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003317 if (op == '+')
3318 val += val2;
3319 else
3320 val -= val2;
3321 }
3322 return val;
3323}
3324
aliguori376253e2009-03-05 23:01:23 +00003325static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003326{
3327 pch = *pp;
3328 if (setjmp(expr_env)) {
3329 *pp = pch;
3330 return -1;
3331 }
blueswir1cd390082008-11-16 13:53:32 +00003332 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003333 pch++;
aliguori376253e2009-03-05 23:01:23 +00003334 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003335 *pp = pch;
3336 return 0;
3337}
3338
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003339static int get_double(Monitor *mon, double *pval, const char **pp)
3340{
3341 const char *p = *pp;
3342 char *tailp;
3343 double d;
3344
3345 d = strtod(p, &tailp);
3346 if (tailp == p) {
3347 monitor_printf(mon, "Number expected\n");
3348 return -1;
3349 }
3350 if (d != d || d - d != 0) {
3351 /* NaN or infinity */
3352 monitor_printf(mon, "Bad number\n");
3353 return -1;
3354 }
3355 *pval = d;
3356 *pp = tailp;
3357 return 0;
3358}
3359
bellard9307c4c2004-04-04 12:57:25 +00003360static int get_str(char *buf, int buf_size, const char **pp)
3361{
3362 const char *p;
3363 char *q;
3364 int c;
3365
bellard81d09122004-07-14 17:21:37 +00003366 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003367 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003368 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003369 p++;
3370 if (*p == '\0') {
3371 fail:
bellard81d09122004-07-14 17:21:37 +00003372 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003373 *pp = p;
3374 return -1;
3375 }
bellard9307c4c2004-04-04 12:57:25 +00003376 if (*p == '\"') {
3377 p++;
3378 while (*p != '\0' && *p != '\"') {
3379 if (*p == '\\') {
3380 p++;
3381 c = *p++;
3382 switch(c) {
3383 case 'n':
3384 c = '\n';
3385 break;
3386 case 'r':
3387 c = '\r';
3388 break;
3389 case '\\':
3390 case '\'':
3391 case '\"':
3392 break;
3393 default:
3394 qemu_printf("unsupported escape code: '\\%c'\n", c);
3395 goto fail;
3396 }
3397 if ((q - buf) < buf_size - 1) {
3398 *q++ = c;
3399 }
3400 } else {
3401 if ((q - buf) < buf_size - 1) {
3402 *q++ = *p;
3403 }
3404 p++;
3405 }
3406 }
3407 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003408 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003409 goto fail;
3410 }
3411 p++;
3412 } else {
blueswir1cd390082008-11-16 13:53:32 +00003413 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003414 if ((q - buf) < buf_size - 1) {
3415 *q++ = *p;
3416 }
3417 p++;
3418 }
bellard9307c4c2004-04-04 12:57:25 +00003419 }
bellard81d09122004-07-14 17:21:37 +00003420 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003421 *pp = p;
3422 return 0;
3423}
3424
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003425/*
3426 * Store the command-name in cmdname, and return a pointer to
3427 * the remaining of the command string.
3428 */
3429static const char *get_command_name(const char *cmdline,
3430 char *cmdname, size_t nlen)
3431{
3432 size_t len;
3433 const char *p, *pstart;
3434
3435 p = cmdline;
3436 while (qemu_isspace(*p))
3437 p++;
3438 if (*p == '\0')
3439 return NULL;
3440 pstart = p;
3441 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3442 p++;
3443 len = p - pstart;
3444 if (len > nlen - 1)
3445 len = nlen - 1;
3446 memcpy(cmdname, pstart, len);
3447 cmdname[len] = '\0';
3448 return p;
3449}
3450
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003451/**
3452 * Read key of 'type' into 'key' and return the current
3453 * 'type' pointer.
3454 */
3455static char *key_get_info(const char *type, char **key)
3456{
3457 size_t len;
3458 char *p, *str;
3459
3460 if (*type == ',')
3461 type++;
3462
3463 p = strchr(type, ':');
3464 if (!p) {
3465 *key = NULL;
3466 return NULL;
3467 }
3468 len = p - type;
3469
3470 str = qemu_malloc(len + 1);
3471 memcpy(str, type, len);
3472 str[len] = '\0';
3473
3474 *key = str;
3475 return ++p;
3476}
3477
bellard9307c4c2004-04-04 12:57:25 +00003478static int default_fmt_format = 'x';
3479static int default_fmt_size = 4;
3480
3481#define MAX_ARGS 16
3482
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003483static int is_valid_option(const char *c, const char *typestr)
3484{
3485 char option[3];
3486
3487 option[0] = '-';
3488 option[1] = *c;
3489 option[2] = '\0';
3490
3491 typestr = strstr(typestr, option);
3492 return (typestr != NULL);
3493}
3494
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003495static const mon_cmd_t *monitor_find_command(const char *cmdname)
3496{
3497 const mon_cmd_t *cmd;
3498
3499 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3500 if (compare_cmd(cmdname, cmd->name)) {
3501 return cmd;
3502 }
3503 }
3504
3505 return NULL;
3506}
3507
Anthony Liguoric227f092009-10-01 16:12:16 -05003508static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003509 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003510 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003511{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003512 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003513 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003514 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003515 char cmdname[256];
3516 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003517 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003518
3519#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003520 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003521#endif
ths3b46e622007-09-17 08:09:54 +00003522
bellard9307c4c2004-04-04 12:57:25 +00003523 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003524 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3525 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003526 return NULL;
ths3b46e622007-09-17 08:09:54 +00003527
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003528 cmd = monitor_find_command(cmdname);
3529 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003530 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003531 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003532 }
bellard9307c4c2004-04-04 12:57:25 +00003533
bellard9307c4c2004-04-04 12:57:25 +00003534 /* parse the parameters */
3535 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003536 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003537 typestr = key_get_info(typestr, &key);
3538 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003539 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003540 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003541 typestr++;
3542 switch(c) {
3543 case 'F':
bellard81d09122004-07-14 17:21:37 +00003544 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003545 case 's':
3546 {
3547 int ret;
ths3b46e622007-09-17 08:09:54 +00003548
blueswir1cd390082008-11-16 13:53:32 +00003549 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003550 p++;
3551 if (*typestr == '?') {
3552 typestr++;
3553 if (*p == '\0') {
3554 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003555 break;
bellard9307c4c2004-04-04 12:57:25 +00003556 }
3557 }
3558 ret = get_str(buf, sizeof(buf), &p);
3559 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003560 switch(c) {
3561 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003562 monitor_printf(mon, "%s: filename expected\n",
3563 cmdname);
bellard81d09122004-07-14 17:21:37 +00003564 break;
3565 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003566 monitor_printf(mon, "%s: block device name expected\n",
3567 cmdname);
bellard81d09122004-07-14 17:21:37 +00003568 break;
3569 default:
aliguori376253e2009-03-05 23:01:23 +00003570 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003571 break;
3572 }
bellard9307c4c2004-04-04 12:57:25 +00003573 goto fail;
3574 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003575 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003576 }
3577 break;
3578 case '/':
3579 {
3580 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003581
blueswir1cd390082008-11-16 13:53:32 +00003582 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003583 p++;
3584 if (*p == '/') {
3585 /* format found */
3586 p++;
3587 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003588 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003589 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003590 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003591 count = count * 10 + (*p - '0');
3592 p++;
3593 }
3594 }
3595 size = -1;
3596 format = -1;
3597 for(;;) {
3598 switch(*p) {
3599 case 'o':
3600 case 'd':
3601 case 'u':
3602 case 'x':
3603 case 'i':
3604 case 'c':
3605 format = *p++;
3606 break;
3607 case 'b':
3608 size = 1;
3609 p++;
3610 break;
3611 case 'h':
3612 size = 2;
3613 p++;
3614 break;
3615 case 'w':
3616 size = 4;
3617 p++;
3618 break;
3619 case 'g':
3620 case 'L':
3621 size = 8;
3622 p++;
3623 break;
3624 default:
3625 goto next;
3626 }
3627 }
3628 next:
blueswir1cd390082008-11-16 13:53:32 +00003629 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003630 monitor_printf(mon, "invalid char in format: '%c'\n",
3631 *p);
bellard9307c4c2004-04-04 12:57:25 +00003632 goto fail;
3633 }
bellard9307c4c2004-04-04 12:57:25 +00003634 if (format < 0)
3635 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003636 if (format != 'i') {
3637 /* for 'i', not specifying a size gives -1 as size */
3638 if (size < 0)
3639 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003640 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003641 }
bellard9307c4c2004-04-04 12:57:25 +00003642 default_fmt_format = format;
3643 } else {
3644 count = 1;
3645 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003646 if (format != 'i') {
3647 size = default_fmt_size;
3648 } else {
3649 size = -1;
3650 }
bellard9307c4c2004-04-04 12:57:25 +00003651 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003652 qdict_put(qdict, "count", qint_from_int(count));
3653 qdict_put(qdict, "format", qint_from_int(format));
3654 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003655 }
3656 break;
3657 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003658 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003659 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003660 {
blueswir1c2efc952007-09-25 17:28:42 +00003661 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003662
blueswir1cd390082008-11-16 13:53:32 +00003663 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003664 p++;
bellard34405572004-06-08 00:55:58 +00003665 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003666 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003667 if (*p == '\0') {
3668 typestr++;
3669 break;
3670 }
bellard34405572004-06-08 00:55:58 +00003671 } else {
3672 if (*p == '.') {
3673 p++;
blueswir1cd390082008-11-16 13:53:32 +00003674 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003675 p++;
bellard34405572004-06-08 00:55:58 +00003676 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003677 typestr++;
3678 break;
bellard34405572004-06-08 00:55:58 +00003679 }
3680 }
bellard13224a82006-07-14 22:03:35 +00003681 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003682 }
aliguori376253e2009-03-05 23:01:23 +00003683 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003684 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003685 /* Check if 'i' is greater than 32-bit */
3686 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3687 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3688 monitor_printf(mon, "integer is for 32-bit values\n");
3689 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003690 } else if (c == 'M') {
3691 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003692 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003693 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003694 }
3695 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003696 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003697 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003698 {
3699 double val;
3700
3701 while (qemu_isspace(*p))
3702 p++;
3703 if (*typestr == '?') {
3704 typestr++;
3705 if (*p == '\0') {
3706 break;
3707 }
3708 }
3709 if (get_double(mon, &val, &p) < 0) {
3710 goto fail;
3711 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003712 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003713 switch (*p) {
3714 case 'K': case 'k':
3715 val *= 1 << 10; p++; break;
3716 case 'M': case 'm':
3717 val *= 1 << 20; p++; break;
3718 case 'G': case 'g':
3719 val *= 1 << 30; p++; break;
3720 }
3721 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003722 if (c == 'T' && p[0] && p[1] == 's') {
3723 switch (*p) {
3724 case 'm':
3725 val /= 1e3; p += 2; break;
3726 case 'u':
3727 val /= 1e6; p += 2; break;
3728 case 'n':
3729 val /= 1e9; p += 2; break;
3730 }
3731 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003732 if (*p && !qemu_isspace(*p)) {
3733 monitor_printf(mon, "Unknown unit suffix\n");
3734 goto fail;
3735 }
3736 qdict_put(qdict, key, qfloat_from_double(val));
3737 }
3738 break;
bellard9307c4c2004-04-04 12:57:25 +00003739 case '-':
3740 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003741 const char *tmp = p;
3742 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003743 /* option */
ths3b46e622007-09-17 08:09:54 +00003744
bellard9307c4c2004-04-04 12:57:25 +00003745 c = *typestr++;
3746 if (c == '\0')
3747 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003748 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003749 p++;
3750 has_option = 0;
3751 if (*p == '-') {
3752 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003753 if(c != *p) {
3754 if(!is_valid_option(p, typestr)) {
3755
3756 monitor_printf(mon, "%s: unsupported option -%c\n",
3757 cmdname, *p);
3758 goto fail;
3759 } else {
3760 skip_key = 1;
3761 }
bellard9307c4c2004-04-04 12:57:25 +00003762 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003763 if(skip_key) {
3764 p = tmp;
3765 } else {
3766 p++;
3767 has_option = 1;
3768 }
bellard9307c4c2004-04-04 12:57:25 +00003769 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003770 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003771 }
3772 break;
3773 default:
3774 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003775 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003776 goto fail;
3777 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003778 qemu_free(key);
3779 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003780 }
3781 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003782 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003783 p++;
3784 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003785 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3786 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003787 goto fail;
3788 }
3789
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003790 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003791
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003792fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003793 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003794 return NULL;
3795}
3796
Luiz Capitulino8204a912009-11-18 23:05:31 -02003797static void monitor_print_error(Monitor *mon)
3798{
3799 qerror_print(mon->error);
3800 QDECREF(mon->error);
3801 mon->error = NULL;
3802}
3803
Adam Litke940cc302010-01-25 12:18:44 -06003804static int is_async_return(const QObject *data)
3805{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003806 if (data && qobject_type(data) == QTYPE_QDICT) {
3807 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3808 }
3809
3810 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003811}
3812
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003813static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3814 const QDict *params)
3815{
3816 QObject *data = NULL;
3817
Luiz Capitulino97536cf2010-02-10 23:49:47 -02003818 if (cmd->cmd_new_ret) {
3819 cmd->cmd_new_ret(mon, params, &data);
3820 } else {
3821 cmd->mhandler.cmd_new(mon, params, &data);
3822 }
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003823
Adam Litke940cc302010-01-25 12:18:44 -06003824 if (is_async_return(data)) {
3825 /*
3826 * Asynchronous commands have no initial return data but they can
3827 * generate errors. Data is returned via the async completion handler.
3828 */
3829 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3830 monitor_protocol_emitter(mon, NULL);
3831 }
3832 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003833 /* Monitor Protocol */
3834 monitor_protocol_emitter(mon, data);
3835 } else {
3836 /* User Protocol */
3837 if (data)
3838 cmd->user_print(mon, data);
3839 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003840
3841 qobject_decref(data);
3842}
3843
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003844static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003845{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003846 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003847 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003848
3849 qdict = qdict_new();
3850
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003851 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003852 if (!cmd)
3853 goto out;
3854
3855 qemu_errors_to_mon(mon);
3856
Adam Litke940cc302010-01-25 12:18:44 -06003857 if (monitor_handler_is_async(cmd)) {
3858 user_async_cmd_handler(mon, cmd, qdict);
3859 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003860 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003861 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003862 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003863 }
3864
Luiz Capitulino8204a912009-11-18 23:05:31 -02003865 if (monitor_has_error(mon))
3866 monitor_print_error(mon);
3867
3868 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003869
3870out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003871 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003872}
3873
bellard81d09122004-07-14 17:21:37 +00003874static void cmd_completion(const char *name, const char *list)
3875{
3876 const char *p, *pstart;
3877 char cmd[128];
3878 int len;
3879
3880 p = list;
3881 for(;;) {
3882 pstart = p;
3883 p = strchr(p, '|');
3884 if (!p)
3885 p = pstart + strlen(pstart);
3886 len = p - pstart;
3887 if (len > sizeof(cmd) - 2)
3888 len = sizeof(cmd) - 2;
3889 memcpy(cmd, pstart, len);
3890 cmd[len] = '\0';
3891 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003892 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003893 }
3894 if (*p == '\0')
3895 break;
3896 p++;
3897 }
3898}
3899
3900static void file_completion(const char *input)
3901{
3902 DIR *ffs;
3903 struct dirent *d;
3904 char path[1024];
3905 char file[1024], file_prefix[1024];
3906 int input_path_len;
3907 const char *p;
3908
ths5fafdf22007-09-16 21:08:06 +00003909 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003910 if (!p) {
3911 input_path_len = 0;
3912 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003913 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003914 } else {
3915 input_path_len = p - input + 1;
3916 memcpy(path, input, input_path_len);
3917 if (input_path_len > sizeof(path) - 1)
3918 input_path_len = sizeof(path) - 1;
3919 path[input_path_len] = '\0';
3920 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3921 }
3922#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003923 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3924 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003925#endif
3926 ffs = opendir(path);
3927 if (!ffs)
3928 return;
3929 for(;;) {
3930 struct stat sb;
3931 d = readdir(ffs);
3932 if (!d)
3933 break;
3934 if (strstart(d->d_name, file_prefix, NULL)) {
3935 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003936 if (input_path_len < sizeof(file))
3937 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3938 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003939 /* stat the file to find out if it's a directory.
3940 * In that case add a slash to speed up typing long paths
3941 */
3942 stat(file, &sb);
3943 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003944 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003945 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003946 }
3947 }
3948 closedir(ffs);
3949}
3950
aliguori51de9762009-03-05 23:00:43 +00003951static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003952{
aliguori51de9762009-03-05 23:00:43 +00003953 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003954 const char *input = opaque;
3955
3956 if (input[0] == '\0' ||
3957 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003958 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003959 }
3960}
3961
3962/* NOTE: this parser is an approximate form of the real command parser */
3963static void parse_cmdline(const char *cmdline,
3964 int *pnb_args, char **args)
3965{
3966 const char *p;
3967 int nb_args, ret;
3968 char buf[1024];
3969
3970 p = cmdline;
3971 nb_args = 0;
3972 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003973 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003974 p++;
3975 if (*p == '\0')
3976 break;
3977 if (nb_args >= MAX_ARGS)
3978 break;
3979 ret = get_str(buf, sizeof(buf), &p);
3980 args[nb_args] = qemu_strdup(buf);
3981 nb_args++;
3982 if (ret < 0)
3983 break;
3984 }
3985 *pnb_args = nb_args;
3986}
3987
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003988static const char *next_arg_type(const char *typestr)
3989{
3990 const char *p = strchr(typestr, ':');
3991 return (p != NULL ? ++p : typestr);
3992}
3993
aliguori4c36ba32009-03-05 23:01:37 +00003994static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003995{
3996 const char *cmdname;
3997 char *args[MAX_ARGS];
3998 int nb_args, i, len;
3999 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004000 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004001 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004002
4003 parse_cmdline(cmdline, &nb_args, args);
4004#ifdef DEBUG_COMPLETION
4005 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004006 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004007 }
4008#endif
4009
4010 /* if the line ends with a space, it means we want to complete the
4011 next arg */
4012 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004013 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004014 if (nb_args >= MAX_ARGS)
4015 return;
4016 args[nb_args++] = qemu_strdup("");
4017 }
4018 if (nb_args <= 1) {
4019 /* command completion */
4020 if (nb_args == 0)
4021 cmdname = "";
4022 else
4023 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004024 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004025 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004026 cmd_completion(cmdname, cmd->name);
4027 }
4028 } else {
4029 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004030 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004031 if (compare_cmd(args[0], cmd->name))
4032 goto found;
4033 }
4034 return;
4035 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004036 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004037 for(i = 0; i < nb_args - 2; i++) {
4038 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004039 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004040 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004041 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004042 }
4043 }
4044 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004045 if (*ptype == '-' && ptype[1] != '\0') {
4046 ptype += 2;
4047 }
bellard81d09122004-07-14 17:21:37 +00004048 switch(*ptype) {
4049 case 'F':
4050 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004051 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004052 file_completion(str);
4053 break;
4054 case 'B':
4055 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004056 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004057 bdrv_iterate(block_completion_it, (void *)str);
4058 break;
bellard7fe48482004-10-09 18:08:01 +00004059 case 's':
4060 /* XXX: more generic ? */
4061 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004062 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004063 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4064 cmd_completion(str, cmd->name);
4065 }
bellard64866c32006-05-07 18:03:31 +00004066 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004067 char *sep = strrchr(str, '-');
4068 if (sep)
4069 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004070 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004071 for(key = key_defs; key->name != NULL; key++) {
4072 cmd_completion(str, key->name);
4073 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004074 } else if (!strcmp(cmd->name, "help|?")) {
4075 readline_set_completion_index(cur_mon->rs, strlen(str));
4076 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4077 cmd_completion(str, cmd->name);
4078 }
bellard7fe48482004-10-09 18:08:01 +00004079 }
4080 break;
bellard81d09122004-07-14 17:21:37 +00004081 default:
4082 break;
4083 }
4084 }
4085 for(i = 0; i < nb_args; i++)
4086 qemu_free(args[i]);
4087}
4088
aliguori731b0362009-03-05 23:01:42 +00004089static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004090{
aliguori731b0362009-03-05 23:01:42 +00004091 Monitor *mon = opaque;
4092
Jan Kiszkac62313b2009-12-04 14:05:29 +01004093 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004094}
4095
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004096typedef struct CmdArgs {
4097 QString *name;
4098 int type;
4099 int flag;
4100 int optional;
4101} CmdArgs;
4102
4103static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4104{
4105 if (!cmd_args->optional) {
4106 qemu_error_new(QERR_MISSING_PARAMETER, name);
4107 return -1;
4108 }
4109
4110 if (cmd_args->type == '-') {
4111 /* handlers expect a value, they need to be changed */
4112 qdict_put(args, name, qint_from_int(0));
4113 }
4114
4115 return 0;
4116}
4117
4118static int check_arg(const CmdArgs *cmd_args, QDict *args)
4119{
4120 QObject *value;
4121 const char *name;
4122
4123 name = qstring_get_str(cmd_args->name);
4124
4125 if (!args) {
4126 return check_opt(cmd_args, name, args);
4127 }
4128
4129 value = qdict_get(args, name);
4130 if (!value) {
4131 return check_opt(cmd_args, name, args);
4132 }
4133
4134 switch (cmd_args->type) {
4135 case 'F':
4136 case 'B':
4137 case 's':
4138 if (qobject_type(value) != QTYPE_QSTRING) {
4139 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4140 return -1;
4141 }
4142 break;
4143 case '/': {
4144 int i;
4145 const char *keys[] = { "count", "format", "size", NULL };
4146
4147 for (i = 0; keys[i]; i++) {
4148 QObject *obj = qdict_get(args, keys[i]);
4149 if (!obj) {
4150 qemu_error_new(QERR_MISSING_PARAMETER, name);
4151 return -1;
4152 }
4153 if (qobject_type(obj) != QTYPE_QINT) {
4154 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4155 return -1;
4156 }
4157 }
4158 break;
4159 }
4160 case 'i':
4161 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004162 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004163 if (qobject_type(value) != QTYPE_QINT) {
4164 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4165 return -1;
4166 }
4167 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004168 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004169 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004170 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4171 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4172 return -1;
4173 }
4174 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004175 case '-':
4176 if (qobject_type(value) != QTYPE_QINT &&
4177 qobject_type(value) != QTYPE_QBOOL) {
4178 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4179 return -1;
4180 }
4181 if (qobject_type(value) == QTYPE_QBOOL) {
4182 /* handlers expect a QInt, they need to be changed */
4183 qdict_put(args, name,
4184 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4185 }
4186 break;
4187 default:
4188 /* impossible */
4189 abort();
4190 }
4191
4192 return 0;
4193}
4194
4195static void cmd_args_init(CmdArgs *cmd_args)
4196{
4197 cmd_args->name = qstring_new();
4198 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4199}
4200
4201/*
4202 * This is not trivial, we have to parse Monitor command's argument
4203 * type syntax to be able to check the arguments provided by clients.
4204 *
4205 * In the near future we will be using an array for that and will be
4206 * able to drop all this parsing...
4207 */
4208static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4209{
4210 int err;
4211 const char *p;
4212 CmdArgs cmd_args;
4213
Blue Swirldd5121b2009-12-04 20:52:02 +00004214 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004215 return (qdict_size(args) == 0 ? 0 : -1);
4216 }
4217
4218 err = 0;
4219 cmd_args_init(&cmd_args);
4220
4221 for (p = cmd->args_type;; p++) {
4222 if (*p == ':') {
4223 cmd_args.type = *++p;
4224 p++;
4225 if (cmd_args.type == '-') {
4226 cmd_args.flag = *p++;
4227 cmd_args.optional = 1;
4228 } else if (*p == '?') {
4229 cmd_args.optional = 1;
4230 p++;
4231 }
4232
4233 assert(*p == ',' || *p == '\0');
4234 err = check_arg(&cmd_args, args);
4235
4236 QDECREF(cmd_args.name);
4237 cmd_args_init(&cmd_args);
4238
4239 if (err < 0) {
4240 break;
4241 }
4242 } else {
4243 qstring_append_chr(cmd_args.name, *p);
4244 }
4245
4246 if (*p == '\0') {
4247 break;
4248 }
4249 }
4250
4251 QDECREF(cmd_args.name);
4252 return err;
4253}
4254
Luiz Capitulino09069b12010-02-04 18:10:06 -02004255static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4256{
4257 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4258 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4259}
4260
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004261static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4262{
4263 int err;
4264 QObject *obj;
4265 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004266 const mon_cmd_t *cmd;
4267 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004268 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004269
4270 args = NULL;
4271 qemu_errors_to_mon(mon);
4272
4273 obj = json_parser_parse(tokens, NULL);
4274 if (!obj) {
4275 // FIXME: should be triggered in json_parser_parse()
4276 qemu_error_new(QERR_JSON_PARSING);
4277 goto err_out;
4278 } else if (qobject_type(obj) != QTYPE_QDICT) {
4279 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4280 qobject_decref(obj);
4281 goto err_out;
4282 }
4283
4284 input = qobject_to_qdict(obj);
4285
4286 mon->mc->id = qdict_get(input, "id");
4287 qobject_incref(mon->mc->id);
4288
4289 obj = qdict_get(input, "execute");
4290 if (!obj) {
4291 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4292 goto err_input;
4293 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4294 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4295 goto err_input;
4296 }
4297
4298 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004299
Luiz Capitulino09069b12010-02-04 18:10:06 -02004300 if (invalid_qmp_mode(mon, cmd_name)) {
4301 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4302 goto err_input;
4303 }
4304
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004305 /*
4306 * XXX: We need this special case until we get info handlers
4307 * converted into 'query-' commands
4308 */
4309 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004310 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4311 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004312 } else if (strstart(cmd_name, "query-", &info_item)) {
4313 cmd = monitor_find_command("info");
4314 qdict_put_obj(input, "arguments",
4315 qobject_from_jsonf("{ 'item': %s }", info_item));
4316 } else {
4317 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004318 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004319 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4320 goto err_input;
4321 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004322 }
4323
4324 obj = qdict_get(input, "arguments");
4325 if (!obj) {
4326 args = qdict_new();
4327 } else {
4328 args = qobject_to_qdict(obj);
4329 QINCREF(args);
4330 }
4331
4332 QDECREF(input);
4333
4334 err = monitor_check_qmp_args(cmd, args);
4335 if (err < 0) {
4336 goto err_out;
4337 }
4338
Adam Litke940cc302010-01-25 12:18:44 -06004339 if (monitor_handler_is_async(cmd)) {
4340 qmp_async_cmd_handler(mon, cmd, args);
4341 } else {
4342 monitor_call_handler(mon, cmd, args);
4343 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004344 goto out;
4345
4346err_input:
4347 QDECREF(input);
4348err_out:
4349 monitor_protocol_emitter(mon, NULL);
4350out:
4351 QDECREF(args);
4352 qemu_errors_to_previous();
4353}
4354
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004355/**
4356 * monitor_control_read(): Read and handle QMP input
4357 */
4358static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4359{
4360 Monitor *old_mon = cur_mon;
4361
4362 cur_mon = opaque;
4363
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004364 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004365
4366 cur_mon = old_mon;
4367}
4368
aliguori731b0362009-03-05 23:01:42 +00004369static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004370{
aliguori731b0362009-03-05 23:01:42 +00004371 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004372 int i;
aliguori376253e2009-03-05 23:01:23 +00004373
aliguori731b0362009-03-05 23:01:42 +00004374 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004375
aliguoricde76ee2009-03-05 23:01:51 +00004376 if (cur_mon->rs) {
4377 for (i = 0; i < size; i++)
4378 readline_handle_byte(cur_mon->rs, buf[i]);
4379 } else {
4380 if (size == 0 || buf[size - 1] != 0)
4381 monitor_printf(cur_mon, "corrupted command\n");
4382 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004383 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004384 }
aliguori731b0362009-03-05 23:01:42 +00004385
4386 cur_mon = old_mon;
4387}
aliguorid8f44602008-10-06 13:52:44 +00004388
aliguori376253e2009-03-05 23:01:23 +00004389static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004390{
aliguori731b0362009-03-05 23:01:42 +00004391 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004392 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004393 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004394}
4395
aliguoricde76ee2009-03-05 23:01:51 +00004396int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004397{
aliguoricde76ee2009-03-05 23:01:51 +00004398 if (!mon->rs)
4399 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004400 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004401 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004402}
4403
aliguori376253e2009-03-05 23:01:23 +00004404void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004405{
aliguoricde76ee2009-03-05 23:01:51 +00004406 if (!mon->rs)
4407 return;
aliguori731b0362009-03-05 23:01:42 +00004408 if (--mon->suspend_cnt == 0)
4409 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004410}
4411
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004412static QObject *get_qmp_greeting(void)
4413{
4414 QObject *ver;
4415
4416 do_info_version(NULL, &ver);
4417 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4418}
4419
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004420/**
4421 * monitor_control_event(): Print QMP gretting
4422 */
4423static void monitor_control_event(void *opaque, int event)
4424{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004425 QObject *data;
4426 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004427
Luiz Capitulino47116d12010-02-08 17:01:30 -02004428 switch (event) {
4429 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004430 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004431 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004432 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004433 monitor_json_emitter(mon, data);
4434 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004435 break;
4436 case CHR_EVENT_CLOSED:
4437 json_message_parser_destroy(&mon->mc->parser);
4438 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004439 }
4440}
4441
aliguori731b0362009-03-05 23:01:42 +00004442static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004443{
aliguori376253e2009-03-05 23:01:23 +00004444 Monitor *mon = opaque;
4445
aliguori2724b182009-03-05 23:01:47 +00004446 switch (event) {
4447 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004448 mon->mux_out = 0;
4449 if (mon->reset_seen) {
4450 readline_restart(mon->rs);
4451 monitor_resume(mon);
4452 monitor_flush(mon);
4453 } else {
4454 mon->suspend_cnt = 0;
4455 }
aliguori2724b182009-03-05 23:01:47 +00004456 break;
ths86e94de2007-01-05 22:01:59 +00004457
aliguori2724b182009-03-05 23:01:47 +00004458 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004459 if (mon->reset_seen) {
4460 if (mon->suspend_cnt == 0) {
4461 monitor_printf(mon, "\n");
4462 }
4463 monitor_flush(mon);
4464 monitor_suspend(mon);
4465 } else {
4466 mon->suspend_cnt++;
4467 }
4468 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004469 break;
4470
Amit Shahb6b8df52009-10-07 18:31:16 +05304471 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004472 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4473 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004474 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004475 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004476 }
4477 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004478 break;
4479 }
ths86e94de2007-01-05 22:01:59 +00004480}
4481
aliguori76655d62009-03-06 20:27:37 +00004482
4483/*
4484 * Local variables:
4485 * c-indent-level: 4
4486 * c-basic-offset: 4
4487 * tab-width: 8
4488 * End:
4489 */
4490
aliguori731b0362009-03-05 23:01:42 +00004491void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004492{
aliguori731b0362009-03-05 23:01:42 +00004493 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004494 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004495
4496 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004497 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004498 is_first_init = 0;
4499 }
aliguori87127162009-03-05 23:01:29 +00004500
4501 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004502
aliguori87127162009-03-05 23:01:29 +00004503 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004504 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004505 if (flags & MONITOR_USE_READLINE) {
4506 mon->rs = readline_init(mon, monitor_find_completion);
4507 monitor_read_command(mon, 0);
4508 }
aliguori87127162009-03-05 23:01:29 +00004509
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004510 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004511 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004512 /* Control mode requires special handlers */
4513 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4514 monitor_control_event, mon);
4515 } else {
4516 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4517 monitor_event, mon);
4518 }
aliguori87127162009-03-05 23:01:29 +00004519
Blue Swirl72cf2d42009-09-12 07:36:22 +00004520 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004521 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004522 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004523}
4524
aliguori376253e2009-03-05 23:01:23 +00004525static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004526{
aliguoribb5fc202009-03-05 23:01:15 +00004527 BlockDriverState *bs = opaque;
4528 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004529
aliguoribb5fc202009-03-05 23:01:15 +00004530 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004531 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004532 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004533 }
aliguori731b0362009-03-05 23:01:42 +00004534 if (mon->password_completion_cb)
4535 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004536
aliguori731b0362009-03-05 23:01:42 +00004537 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004538}
aliguoric0f4ce72009-03-05 23:01:01 +00004539
aliguori376253e2009-03-05 23:01:23 +00004540void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004541 BlockDriverCompletionFunc *completion_cb,
4542 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004543{
aliguoricde76ee2009-03-05 23:01:51 +00004544 int err;
4545
aliguoribb5fc202009-03-05 23:01:15 +00004546 if (!bdrv_key_required(bs)) {
4547 if (completion_cb)
4548 completion_cb(opaque, 0);
4549 return;
4550 }
aliguoric0f4ce72009-03-05 23:01:01 +00004551
Luiz Capitulino94171e12009-12-07 21:37:00 +01004552 if (monitor_ctrl_mode(mon)) {
4553 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4554 return;
4555 }
4556
aliguori376253e2009-03-05 23:01:23 +00004557 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4558 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004559
aliguori731b0362009-03-05 23:01:42 +00004560 mon->password_completion_cb = completion_cb;
4561 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004562
aliguoricde76ee2009-03-05 23:01:51 +00004563 err = monitor_read_password(mon, bdrv_password_cb, bs);
4564
4565 if (err && completion_cb)
4566 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004567}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004568
4569typedef struct QemuErrorSink QemuErrorSink;
4570struct QemuErrorSink {
4571 enum {
4572 ERR_SINK_FILE,
4573 ERR_SINK_MONITOR,
4574 } dest;
4575 union {
4576 FILE *fp;
4577 Monitor *mon;
4578 };
4579 QemuErrorSink *previous;
4580};
4581
Blue Swirl528e93a2009-08-31 15:14:40 +00004582static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004583
4584void qemu_errors_to_file(FILE *fp)
4585{
4586 QemuErrorSink *sink;
4587
4588 sink = qemu_mallocz(sizeof(*sink));
4589 sink->dest = ERR_SINK_FILE;
4590 sink->fp = fp;
4591 sink->previous = qemu_error_sink;
4592 qemu_error_sink = sink;
4593}
4594
4595void qemu_errors_to_mon(Monitor *mon)
4596{
4597 QemuErrorSink *sink;
4598
4599 sink = qemu_mallocz(sizeof(*sink));
4600 sink->dest = ERR_SINK_MONITOR;
4601 sink->mon = mon;
4602 sink->previous = qemu_error_sink;
4603 qemu_error_sink = sink;
4604}
4605
4606void qemu_errors_to_previous(void)
4607{
4608 QemuErrorSink *sink;
4609
4610 assert(qemu_error_sink != NULL);
4611 sink = qemu_error_sink;
4612 qemu_error_sink = sink->previous;
4613 qemu_free(sink);
4614}
4615
4616void qemu_error(const char *fmt, ...)
4617{
4618 va_list args;
4619
4620 assert(qemu_error_sink != NULL);
4621 switch (qemu_error_sink->dest) {
4622 case ERR_SINK_FILE:
4623 va_start(args, fmt);
4624 vfprintf(qemu_error_sink->fp, fmt, args);
4625 va_end(args);
4626 break;
4627 case ERR_SINK_MONITOR:
4628 va_start(args, fmt);
4629 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4630 va_end(args);
4631 break;
4632 }
4633}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004634
4635void qemu_error_internal(const char *file, int linenr, const char *func,
4636 const char *fmt, ...)
4637{
4638 va_list va;
4639 QError *qerror;
4640
4641 assert(qemu_error_sink != NULL);
4642
4643 va_start(va, fmt);
4644 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4645 va_end(va);
4646
4647 switch (qemu_error_sink->dest) {
4648 case ERR_SINK_FILE:
4649 qerror_print(qerror);
4650 QDECREF(qerror);
4651 break;
4652 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004653 /* report only the first error */
4654 if (!qemu_error_sink->mon->error) {
4655 qemu_error_sink->mon->error = qerror;
4656 } else {
4657 /* XXX: warn the programmer */
4658 QDECREF(qerror);
4659 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004660 break;
4661 }
4662}