blob: 6eb0e2cfa917c2f81b114e9aa9dc73abb6cd90a3 [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 Capitulino910df892009-10-07 13:41:51 -0300101 union {
102 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300103 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600104 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300105 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200106 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600107 int (*cmd_async)(Monitor *mon, const QDict *params,
108 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300109 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600110 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500111} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000112
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100113/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500114typedef struct mon_fd_t mon_fd_t;
115struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100116 char *name;
117 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500118 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100119};
120
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200121typedef struct MonitorControl {
122 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200123 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200124 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200125 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200126} MonitorControl;
127
aliguori87127162009-03-05 23:01:29 +0000128struct Monitor {
129 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200130 int mux_out;
131 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000132 int flags;
133 int suspend_cnt;
134 uint8_t outbuf[1024];
135 int outbuf_index;
136 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200137 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000138 CPUState *mon_cpu;
139 BlockDriverCompletionFunc *password_completion_cb;
140 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200141 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000143 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000144};
145
Blue Swirl72cf2d42009-09-12 07:36:22 +0000146static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000147
Anthony Liguoric227f092009-10-01 16:12:16 -0500148static const mon_cmd_t mon_cmds[];
149static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000150
aliguori87127162009-03-05 23:01:29 +0000151Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000152
aliguori731b0362009-03-05 23:01:42 +0000153static void monitor_command_cb(Monitor *mon, const char *cmdline,
154 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000155
Luiz Capitulino09069b12010-02-04 18:10:06 -0200156static inline int qmp_cmd_mode(const Monitor *mon)
157{
158 return (mon->mc ? mon->mc->command_mode : 0);
159}
160
Luiz Capitulino418173c2009-11-26 22:58:51 -0200161/* Return true if in control mode, false otherwise */
162static inline int monitor_ctrl_mode(const Monitor *mon)
163{
164 return (mon->flags & MONITOR_USE_CONTROL);
165}
166
aliguori731b0362009-03-05 23:01:42 +0000167static void monitor_read_command(Monitor *mon, int show_prompt)
168{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200169 if (!mon->rs)
170 return;
171
aliguori731b0362009-03-05 23:01:42 +0000172 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
173 if (show_prompt)
174 readline_show_prompt(mon->rs);
175}
bellard6a00d602005-11-21 23:25:50 +0000176
aliguoricde76ee2009-03-05 23:01:51 +0000177static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
178 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000179{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100180 if (monitor_ctrl_mode(mon)) {
181 qemu_error_new(QERR_MISSING_PARAMETER, "password");
182 return -EINVAL;
183 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000184 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
185 /* prompt is printed on return from the command handler */
186 return 0;
187 } else {
188 monitor_printf(mon, "terminal does not support password prompting\n");
189 return -ENOTTY;
190 }
aliguoribb5fc202009-03-05 23:01:15 +0000191}
192
aliguori376253e2009-03-05 23:01:23 +0000193void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000194{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200195 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000196 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
197 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000198 }
199}
200
201/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000202static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000203{
ths60fe76f2007-12-16 03:02:09 +0000204 char c;
aliguori731b0362009-03-05 23:01:42 +0000205
bellard7e2515e2004-08-01 21:52:19 +0000206 for(;;) {
207 c = *str++;
208 if (c == '\0')
209 break;
bellard7ba12602006-07-14 20:26:42 +0000210 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000211 mon->outbuf[mon->outbuf_index++] = '\r';
212 mon->outbuf[mon->outbuf_index++] = c;
213 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
214 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000215 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000216 }
217}
218
aliguori376253e2009-03-05 23:01:23 +0000219void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000220{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200221 if (!mon)
222 return;
223
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200224 if (mon->mc && !mon->mc->print_enabled) {
225 qemu_error_new(QERR_UNDEFINED_ERROR);
226 } else {
227 char buf[4096];
228 vsnprintf(buf, sizeof(buf), fmt, ap);
229 monitor_puts(mon, buf);
230 }
bellard7e2515e2004-08-01 21:52:19 +0000231}
232
aliguori376253e2009-03-05 23:01:23 +0000233void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000234{
235 va_list ap;
236 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000237 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000238 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000239}
240
aliguori376253e2009-03-05 23:01:23 +0000241void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000242{
243 int i;
244
245 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000246 switch (filename[i]) {
247 case ' ':
248 case '"':
249 case '\\':
250 monitor_printf(mon, "\\%c", filename[i]);
251 break;
252 case '\t':
253 monitor_printf(mon, "\\t");
254 break;
255 case '\r':
256 monitor_printf(mon, "\\r");
257 break;
258 case '\n':
259 monitor_printf(mon, "\\n");
260 break;
261 default:
262 monitor_printf(mon, "%c", filename[i]);
263 break;
264 }
thsfef30742006-12-22 14:11:32 +0000265 }
266}
267
bellard7fe48482004-10-09 18:08:01 +0000268static int monitor_fprintf(FILE *stream, const char *fmt, ...)
269{
270 va_list ap;
271 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000272 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000273 va_end(ap);
274 return 0;
275}
276
Luiz Capitulino13c74252009-10-07 13:41:55 -0300277static void monitor_user_noop(Monitor *mon, const QObject *data) { }
278
Luiz Capitulino13917be2009-10-07 13:41:54 -0300279static inline int monitor_handler_ported(const mon_cmd_t *cmd)
280{
281 return cmd->user_print != NULL;
282}
283
Adam Litke940cc302010-01-25 12:18:44 -0600284static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
285{
286 return cmd->async != 0;
287}
288
Luiz Capitulino8204a912009-11-18 23:05:31 -0200289static inline int monitor_has_error(const Monitor *mon)
290{
291 return mon->error != NULL;
292}
293
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200294static void monitor_json_emitter(Monitor *mon, const QObject *data)
295{
296 QString *json;
297
298 json = qobject_to_json(data);
299 assert(json != NULL);
300
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200301 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200302 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200303 mon->mc->print_enabled = 0;
304
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200305 QDECREF(json);
306}
307
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200308static void monitor_protocol_emitter(Monitor *mon, QObject *data)
309{
310 QDict *qmp;
311
312 qmp = qdict_new();
313
314 if (!monitor_has_error(mon)) {
315 /* success response */
316 if (data) {
317 qobject_incref(data);
318 qdict_put_obj(qmp, "return", data);
319 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200320 /* return an empty QDict by default */
321 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200322 }
323 } else {
324 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100325 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200326 qdict_put(qmp, "error", mon->error->error);
327 QINCREF(mon->error->error);
328 QDECREF(mon->error);
329 mon->error = NULL;
330 }
331
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200332 if (mon->mc->id) {
333 qdict_put_obj(qmp, "id", mon->mc->id);
334 mon->mc->id = NULL;
335 }
336
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200337 monitor_json_emitter(mon, QOBJECT(qmp));
338 QDECREF(qmp);
339}
340
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200341static void timestamp_put(QDict *qdict)
342{
343 int err;
344 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000345 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200346
Blue Swirld08d6f02009-12-04 18:06:39 +0000347 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200348 if (err < 0)
349 return;
350
351 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
352 "'microseconds': %" PRId64 " }",
353 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200354 qdict_put_obj(qdict, "timestamp", obj);
355}
356
357/**
358 * monitor_protocol_event(): Generate a Monitor event
359 *
360 * Event-specific data can be emitted through the (optional) 'data' parameter.
361 */
362void monitor_protocol_event(MonitorEvent event, QObject *data)
363{
364 QDict *qmp;
365 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600366 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200367
Blue Swirl242cd002009-12-04 18:05:45 +0000368 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200369
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200370 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000371 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200372 event_name = "DEBUG";
373 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000374 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200375 event_name = "SHUTDOWN";
376 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000377 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200378 event_name = "RESET";
379 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000380 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200381 event_name = "POWERDOWN";
382 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000383 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200384 event_name = "STOP";
385 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200386 case QEVENT_VNC_CONNECTED:
387 event_name = "VNC_CONNECTED";
388 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200389 case QEVENT_VNC_INITIALIZED:
390 event_name = "VNC_INITIALIZED";
391 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200392 case QEVENT_VNC_DISCONNECTED:
393 event_name = "VNC_DISCONNECTED";
394 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200395 case QEVENT_BLOCK_IO_ERROR:
396 event_name = "BLOCK_IO_ERROR";
397 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200398 default:
399 abort();
400 break;
401 }
402
403 qmp = qdict_new();
404 timestamp_put(qmp);
405 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200406 if (data) {
407 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200408 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200409 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410
Adam Litkef039a562010-01-15 08:34:02 -0600411 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200412 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200413 monitor_json_emitter(mon, QOBJECT(qmp));
414 }
Adam Litkef039a562010-01-15 08:34:02 -0600415 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200416 QDECREF(qmp);
417}
418
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200419static int do_qmp_capabilities(Monitor *mon, const QDict *params,
420 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200421{
422 /* Will setup QMP capabilities in the future */
423 if (monitor_ctrl_mode(mon)) {
424 mon->mc->command_mode = 1;
425 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200426
427 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200428}
429
bellard9dc39cb2004-03-14 21:38:27 +0000430static int compare_cmd(const char *name, const char *list)
431{
432 const char *p, *pstart;
433 int len;
434 len = strlen(name);
435 p = list;
436 for(;;) {
437 pstart = p;
438 p = strchr(p, '|');
439 if (!p)
440 p = pstart + strlen(pstart);
441 if ((p - pstart) == len && !memcmp(pstart, name, len))
442 return 1;
443 if (*p == '\0')
444 break;
445 p++;
446 }
447 return 0;
448}
449
Anthony Liguoric227f092009-10-01 16:12:16 -0500450static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000451 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000452{
Anthony Liguoric227f092009-10-01 16:12:16 -0500453 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000454
455 for(cmd = cmds; cmd->name != NULL; cmd++) {
456 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000457 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
458 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000459 }
460}
461
aliguori376253e2009-03-05 23:01:23 +0000462static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000463{
464 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000465 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000466 } else {
aliguori376253e2009-03-05 23:01:23 +0000467 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000468 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000469 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000470 monitor_printf(mon, "Log items (comma separated):\n");
471 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000472 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000473 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000474 }
475 }
bellard9dc39cb2004-03-14 21:38:27 +0000476 }
477}
478
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300479static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300480{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300481 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300482}
483
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300484static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000485{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200486 int all_devices;
487 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300488 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000489
bellard7954c732006-08-01 15:52:40 +0000490 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000491 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200492 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300493 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200494 continue;
495 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000496 }
497}
498
Adam Litke940cc302010-01-25 12:18:44 -0600499static void user_monitor_complete(void *opaque, QObject *ret_data)
500{
501 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
502
503 if (ret_data) {
504 data->user_print(data->mon, ret_data);
505 }
506 monitor_resume(data->mon);
507 qemu_free(data);
508}
509
510static void qmp_monitor_complete(void *opaque, QObject *ret_data)
511{
512 monitor_protocol_emitter(opaque, ret_data);
513}
514
515static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
516 const QDict *params)
517{
518 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
519}
520
521static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
522{
523 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
524}
525
526static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
527 const QDict *params)
528{
529 int ret;
530
531 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
532 cb_data->mon = mon;
533 cb_data->user_print = cmd->user_print;
534 monitor_suspend(mon);
535 ret = cmd->mhandler.cmd_async(mon, params,
536 user_monitor_complete, cb_data);
537 if (ret < 0) {
538 monitor_resume(mon);
539 qemu_free(cb_data);
540 }
541}
542
543static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
544{
545 int ret;
546
547 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
548 cb_data->mon = mon;
549 cb_data->user_print = cmd->user_print;
550 monitor_suspend(mon);
551 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
552 if (ret < 0) {
553 monitor_resume(mon);
554 qemu_free(cb_data);
555 }
556}
557
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200558static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000559{
Anthony Liguoric227f092009-10-01 16:12:16 -0500560 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300561 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000562
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200563 if (!item) {
564 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000565 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200566 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300567
568 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000569 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300570 break;
bellard9dc39cb2004-03-14 21:38:27 +0000571 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300572
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200573 if (cmd->name == NULL) {
574 if (monitor_ctrl_mode(mon)) {
575 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200576 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200577 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300578 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200579 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300580
Adam Litke940cc302010-01-25 12:18:44 -0600581 if (monitor_handler_is_async(cmd)) {
582 if (monitor_ctrl_mode(mon)) {
583 qmp_async_info_handler(mon, cmd);
584 } else {
585 user_async_info_handler(mon, cmd);
586 }
587 /*
588 * Indicate that this command is asynchronous and will not return any
589 * data (not even empty). Instead, the data will be returned via a
590 * completion callback.
591 */
592 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
593 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300594 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200595
596 if (!monitor_ctrl_mode(mon)) {
597 /*
598 * User Protocol function is called here, Monitor Protocol is
599 * handled by monitor_call_handler()
600 */
601 if (*ret_data)
602 cmd->user_print(mon, *ret_data);
603 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300604 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200605 if (monitor_ctrl_mode(mon)) {
606 /* handler not converted yet */
607 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200608 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200609 } else {
610 cmd->mhandler.info(mon);
611 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300612 }
613
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200614 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300615
616help:
617 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200618 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000619}
620
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200621static void do_info_version_print(Monitor *mon, const QObject *data)
622{
623 QDict *qdict;
624
625 qdict = qobject_to_qdict(data);
626
627 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
628 qdict_get_str(qdict, "package"));
629}
630
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300631/**
632 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200633 *
634 * Return a QDict with the following information:
635 *
636 * - "qemu": QEMU's version
637 * - "package": package's version
638 *
639 * Example:
640 *
641 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300642 */
643static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000644{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200645 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
646 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000647}
648
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200649static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000650{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200651 QDict *qdict;
652
653 qdict = qobject_to_qdict(data);
654 if (qdict_size(qdict) == 0) {
655 return;
656 }
657
658 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
659}
660
661/**
662 * do_info_name(): Show VM name
663 *
664 * Return a QDict with the following information:
665 *
666 * - "name": VM's name (optional)
667 *
668 * Example:
669 *
670 * { "name": "qemu-name" }
671 */
672static void do_info_name(Monitor *mon, QObject **ret_data)
673{
674 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
675 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000676}
677
Luiz Capitulino1a728672009-12-10 17:15:56 -0200678static QObject *get_cmd_dict(const char *name)
679{
680 const char *p;
681
682 /* Remove '|' from some commands */
683 p = strchr(name, '|');
684 if (p) {
685 p++;
686 } else {
687 p = name;
688 }
689
690 return qobject_from_jsonf("{ 'name': %s }", p);
691}
692
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200693/**
694 * do_info_commands(): List QMP available commands
695 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200696 * Each command is represented by a QDict, the returned QObject is a QList
697 * of all commands.
698 *
699 * The QDict contains:
700 *
701 * - "name": command's name
702 *
703 * Example:
704 *
705 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200706 */
707static void do_info_commands(Monitor *mon, QObject **ret_data)
708{
709 QList *cmd_list;
710 const mon_cmd_t *cmd;
711
712 cmd_list = qlist_new();
713
714 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
715 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200716 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200717 }
718 }
719
720 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
721 if (monitor_handler_ported(cmd)) {
722 char buf[128];
723 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200724 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200725 }
726 }
727
728 *ret_data = QOBJECT(cmd_list);
729}
730
aurel32bf4f74c2008-12-18 22:42:34 +0000731#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200732static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000733{
aliguori376253e2009-03-05 23:01:23 +0000734 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200735 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
736 "enabled" : "disabled");
737}
738
739/**
740 * do_info_hpet(): Show HPET state
741 *
742 * Return a QDict with the following information:
743 *
744 * - "enabled": true if hpet if enabled, false otherwise
745 *
746 * Example:
747 *
748 * { "enabled": true }
749 */
750static void do_info_hpet(Monitor *mon, QObject **ret_data)
751{
752 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000753}
aurel32bf4f74c2008-12-18 22:42:34 +0000754#endif
aliguori16b29ae2008-12-17 23:28:44 +0000755
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200756static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000757{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200758 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
759}
760
761/**
762 * do_info_uuid(): Show VM UUID
763 *
764 * Return a QDict with the following information:
765 *
766 * - "UUID": Universally Unique Identifier
767 *
768 * Example:
769 *
770 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
771 */
772static void do_info_uuid(Monitor *mon, QObject **ret_data)
773{
774 char uuid[64];
775
776 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000777 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
778 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
779 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
780 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200781 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000782}
783
bellard6a00d602005-11-21 23:25:50 +0000784/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000785static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000786{
787 CPUState *env;
788
789 for(env = first_cpu; env != NULL; env = env->next_cpu) {
790 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000791 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000792 return 0;
793 }
794 }
795 return -1;
796}
797
pbrook9596ebb2007-11-18 01:44:38 +0000798static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000799{
aliguori731b0362009-03-05 23:01:42 +0000800 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000801 mon_set_cpu(0);
802 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300803 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000804 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000805}
806
aliguori376253e2009-03-05 23:01:23 +0000807static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000808{
bellard6a00d602005-11-21 23:25:50 +0000809 CPUState *env;
810 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000811#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000812 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000813 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000814#else
aliguori376253e2009-03-05 23:01:23 +0000815 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000816 0);
bellard9307c4c2004-04-04 12:57:25 +0000817#endif
818}
819
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300820static void print_cpu_iter(QObject *obj, void *opaque)
821{
822 QDict *cpu;
823 int active = ' ';
824 Monitor *mon = opaque;
825
826 assert(qobject_type(obj) == QTYPE_QDICT);
827 cpu = qobject_to_qdict(obj);
828
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200829 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300830 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200831 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300832
833 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
834
835#if defined(TARGET_I386)
836 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
837 (target_ulong) qdict_get_int(cpu, "pc"));
838#elif defined(TARGET_PPC)
839 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
840 (target_long) qdict_get_int(cpu, "nip"));
841#elif defined(TARGET_SPARC)
842 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
843 (target_long) qdict_get_int(cpu, "pc"));
844 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
845 (target_long) qdict_get_int(cpu, "npc"));
846#elif defined(TARGET_MIPS)
847 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
848 (target_long) qdict_get_int(cpu, "PC"));
849#endif
850
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200851 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300852 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200853 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300854
855 monitor_printf(mon, "\n");
856}
857
858static void monitor_print_cpus(Monitor *mon, const QObject *data)
859{
860 QList *cpu_list;
861
862 assert(qobject_type(data) == QTYPE_QLIST);
863 cpu_list = qobject_to_qlist(data);
864 qlist_iter(cpu_list, print_cpu_iter, mon);
865}
866
867/**
868 * do_info_cpus(): Show CPU information
869 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200870 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300871 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200872 * - "cpu": CPU index
873 * - "current": true if this is the current CPU, false otherwise
874 * - "halted": true if the cpu is halted, false otherwise
875 * - Current program counter. The key's name depends on the architecture:
876 * "pc": i386/x86)64
877 * "nip": PPC
878 * "pc" and "npc": sparc
879 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200881 * Example:
882 *
883 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
884 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300885 */
886static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000887{
888 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300889 QList *cpu_list;
890
891 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000892
893 /* just to set the default cpu if not already done */
894 mon_get_cpu();
895
896 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200897 QDict *cpu;
898 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300899
Avi Kivity4c0960c2009-08-17 23:19:53 +0300900 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300901
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200902 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
903 env->cpu_index, env == mon->mon_cpu,
904 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200905
906 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300907
bellard6a00d602005-11-21 23:25:50 +0000908#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300909 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000910#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300911 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000912#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300913 qdict_put(cpu, "pc", qint_from_int(env->pc));
914 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000915#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300916 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000917#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300918
919 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000920 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300921
922 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000923}
924
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200925static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000926{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300927 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200928 if (mon_set_cpu(index) < 0) {
Markus Armbrustercc0c4182010-01-29 10:34:00 +0100929 qemu_error_new(QERR_INVALID_PARAMETER, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200930 return -1;
931 }
932 return 0;
bellard6a00d602005-11-21 23:25:50 +0000933}
934
aliguori376253e2009-03-05 23:01:23 +0000935static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000936{
aliguori376253e2009-03-05 23:01:23 +0000937 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000938}
939
aliguori376253e2009-03-05 23:01:23 +0000940static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000941{
942 int i;
bellard7e2515e2004-08-01 21:52:19 +0000943 const char *str;
ths3b46e622007-09-17 08:09:54 +0000944
aliguoricde76ee2009-03-05 23:01:51 +0000945 if (!mon->rs)
946 return;
bellard7e2515e2004-08-01 21:52:19 +0000947 i = 0;
948 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000949 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000950 if (!str)
951 break;
aliguori376253e2009-03-05 23:01:23 +0000952 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000953 i++;
bellardaa455482004-04-04 13:07:25 +0000954 }
955}
956
j_mayer76a66252007-03-07 08:32:30 +0000957#if defined(TARGET_PPC)
958/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000959static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000960{
961 CPUState *env;
962
963 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000964 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000965}
966#endif
967
Luiz Capitulinob223f352009-10-07 13:41:56 -0300968/**
969 * do_quit(): Quit QEMU execution
970 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200971static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000972{
973 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200974 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000975}
976
aliguori376253e2009-03-05 23:01:23 +0000977static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000978{
979 if (bdrv_is_inserted(bs)) {
980 if (!force) {
981 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100982 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
983 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000984 return -1;
985 }
986 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100987 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000988 return -1;
989 }
990 }
991 bdrv_close(bs);
992 }
993 return 0;
994}
995
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -0200996static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000997{
998 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300999 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -02001000 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +00001001
bellard9307c4c2004-04-04 12:57:25 +00001002 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +00001003 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001004 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001005 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001006 }
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001007 return eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001008}
1009
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001010static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001011 QObject **ret_data)
1012{
1013 BlockDriverState *bs;
1014
1015 bs = bdrv_find(qdict_get_str(qdict, "device"));
1016 if (!bs) {
1017 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001018 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001019 }
1020
1021 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
1022 qemu_error_new(QERR_INVALID_PASSWORD);
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001023 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001024 }
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001025
1026 return 0;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001027}
1028
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001029static int do_change_block(Monitor *mon, const char *device,
1030 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001031{
1032 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001033 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001034
bellard9307c4c2004-04-04 12:57:25 +00001035 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001036 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001037 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001038 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001039 }
aurel322ecea9b2008-06-18 22:10:01 +00001040 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001041 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001042 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001043 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001044 return -1;
aurel322ecea9b2008-06-18 22:10:01 +00001045 }
1046 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001047 if (eject_device(mon, bs, 0) < 0) {
1048 return -1;
1049 }
1050 if (bdrv_open2(bs, filename, BDRV_O_RDWR, drv) < 0) {
1051 return -1;
1052 }
1053 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001054}
1055
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001056static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001057{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001058 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001059 qemu_error_new(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001060 return -1;
1061 }
aliguoribb5fc202009-03-05 23:01:15 +00001062
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001063 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001064}
1065
1066static void change_vnc_password_cb(Monitor *mon, const char *password,
1067 void *opaque)
1068{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001069 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001070 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001071}
1072
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001073static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001074{
ths70848512007-08-25 01:37:05 +00001075 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001076 strcmp(target, "password") == 0) {
1077 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001078 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001079 strncpy(password, arg, sizeof(password));
1080 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001081 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001082 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001083 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001084 }
ths70848512007-08-25 01:37:05 +00001085 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001086 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001087 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001088 return -1;
1089 }
ths70848512007-08-25 01:37:05 +00001090 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001091
1092 return 0;
thse25a5822007-08-25 01:36:20 +00001093}
1094
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001095/**
1096 * do_change(): Change a removable medium, or VNC configuration
1097 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001098static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001099{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001100 const char *device = qdict_get_str(qdict, "device");
1101 const char *target = qdict_get_str(qdict, "target");
1102 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001103 int ret;
1104
thse25a5822007-08-25 01:36:20 +00001105 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001106 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001107 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001108 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001109 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001110
1111 return ret;
thse25a5822007-08-25 01:36:20 +00001112}
1113
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001114static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001115{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001116 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001117}
1118
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001119static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001120{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001121 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001122}
1123
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001124static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001125{
1126 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001127 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001128
bellard9307c4c2004-04-04 12:57:25 +00001129 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001130 mask = 0;
1131 } else {
bellard9307c4c2004-04-04 12:57:25 +00001132 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001133 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001134 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001135 return;
1136 }
1137 }
1138 cpu_set_log(mask);
1139}
1140
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001141static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001142{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001143 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001144 if (!option || !strcmp(option, "on")) {
1145 singlestep = 1;
1146 } else if (!strcmp(option, "off")) {
1147 singlestep = 0;
1148 } else {
1149 monitor_printf(mon, "unexpected option %s\n", option);
1150 }
1151}
1152
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001153/**
1154 * do_stop(): Stop VM execution
1155 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001156static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001157{
1158 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001159 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001160}
1161
aliguoribb5fc202009-03-05 23:01:15 +00001162static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001163
aliguori376253e2009-03-05 23:01:23 +00001164struct bdrv_iterate_context {
1165 Monitor *mon;
1166 int err;
1167};
aliguoric0f4ce72009-03-05 23:01:01 +00001168
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001169/**
1170 * do_cont(): Resume emulation.
1171 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001172static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001173{
1174 struct bdrv_iterate_context context = { mon, 0 };
1175
1176 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001177 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001178 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001179 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001180 return 0;
1181 } else {
1182 return -1;
1183 }
bellard8a7ddc32004-03-31 19:00:16 +00001184}
1185
aliguoribb5fc202009-03-05 23:01:15 +00001186static void bdrv_key_cb(void *opaque, int err)
1187{
aliguori376253e2009-03-05 23:01:23 +00001188 Monitor *mon = opaque;
1189
aliguoribb5fc202009-03-05 23:01:15 +00001190 /* another key was set successfully, retry to continue */
1191 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001192 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001193}
1194
1195static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1196{
aliguori376253e2009-03-05 23:01:23 +00001197 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001198
aliguori376253e2009-03-05 23:01:23 +00001199 if (!context->err && bdrv_key_required(bs)) {
1200 context->err = -EBUSY;
1201 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1202 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001203 }
1204}
1205
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001206static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001207{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001208 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001209 if (!device)
1210 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1211 if (gdbserver_start(device) < 0) {
1212 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1213 device);
1214 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001215 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001216 } else {
aliguori59030a82009-04-05 18:43:41 +00001217 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1218 device);
bellard8a7ddc32004-03-31 19:00:16 +00001219 }
1220}
1221
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001222static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001223{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001224 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001225 if (select_watchdog_action(action) == -1) {
1226 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1227 }
1228}
1229
aliguori376253e2009-03-05 23:01:23 +00001230static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001231{
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001233 switch(c) {
1234 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001236 break;
1237 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001238 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001239 break;
1240 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001242 break;
1243 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001245 break;
1246 default:
1247 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001249 } else {
aliguori376253e2009-03-05 23:01:23 +00001250 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001251 }
1252 break;
1253 }
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001255}
1256
aliguori376253e2009-03-05 23:01:23 +00001257static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001258 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001259{
bellard6a00d602005-11-21 23:25:50 +00001260 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001261 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001262 uint8_t buf[16];
1263 uint64_t v;
1264
1265 if (format == 'i') {
1266 int flags;
1267 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001268 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001269 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001270 return;
bellard9307c4c2004-04-04 12:57:25 +00001271#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001272 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001273 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001274 } else if (wsize == 4) {
1275 flags = 0;
1276 } else {
bellard6a15fd12006-04-12 21:07:07 +00001277 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001278 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001279 if (env) {
1280#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001281 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001282 (env->segs[R_CS].flags & DESC_L_MASK))
1283 flags = 2;
1284 else
1285#endif
1286 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1287 flags = 1;
1288 }
bellard4c27ba22004-04-25 18:05:08 +00001289 }
1290#endif
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001292 return;
1293 }
1294
1295 len = wsize * count;
1296 if (wsize == 1)
1297 line_size = 8;
1298 else
1299 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001300 max_digits = 0;
1301
1302 switch(format) {
1303 case 'o':
1304 max_digits = (wsize * 8 + 2) / 3;
1305 break;
1306 default:
1307 case 'x':
1308 max_digits = (wsize * 8) / 4;
1309 break;
1310 case 'u':
1311 case 'd':
1312 max_digits = (wsize * 8 * 10 + 32) / 33;
1313 break;
1314 case 'c':
1315 wsize = 1;
1316 break;
1317 }
1318
1319 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001320 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001321 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001322 else
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001324 l = len;
1325 if (l > line_size)
1326 l = line_size;
1327 if (is_physical) {
1328 cpu_physical_memory_rw(addr, buf, l, 0);
1329 } else {
bellard6a00d602005-11-21 23:25:50 +00001330 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001331 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001333 break;
1334 }
bellard9307c4c2004-04-04 12:57:25 +00001335 }
ths5fafdf22007-09-16 21:08:06 +00001336 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001337 while (i < l) {
1338 switch(wsize) {
1339 default:
1340 case 1:
1341 v = ldub_raw(buf + i);
1342 break;
1343 case 2:
1344 v = lduw_raw(buf + i);
1345 break;
1346 case 4:
bellard92a31b12005-02-10 22:00:52 +00001347 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 case 8:
1350 v = ldq_raw(buf + i);
1351 break;
1352 }
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001354 switch(format) {
1355 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001357 break;
1358 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001360 break;
1361 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001363 break;
1364 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001366 break;
1367 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001369 break;
1370 }
1371 i += wsize;
1372 }
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001374 addr += l;
1375 len -= l;
1376 }
1377}
1378
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001379static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001380{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001381 int count = qdict_get_int(qdict, "count");
1382 int format = qdict_get_int(qdict, "format");
1383 int size = qdict_get_int(qdict, "size");
1384 target_long addr = qdict_get_int(qdict, "addr");
1385
aliguori376253e2009-03-05 23:01:23 +00001386 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001387}
1388
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001389static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001390{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001391 int count = qdict_get_int(qdict, "count");
1392 int format = qdict_get_int(qdict, "format");
1393 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001394 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001395
aliguori376253e2009-03-05 23:01:23 +00001396 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001397}
1398
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001399static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001400{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001401 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001402 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001403
blueswir17743e582007-09-24 18:39:04 +00001404#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001405 switch(format) {
1406 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001408 break;
1409 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001411 break;
1412 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001414 break;
1415 default:
1416 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001417 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001418 break;
1419 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001420 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001421 break;
1422 }
bellard92a31b12005-02-10 22:00:52 +00001423#else
1424 switch(format) {
1425 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001426 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001427 break;
1428 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001430 break;
1431 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001432 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001433 break;
1434 default:
1435 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001436 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001437 break;
1438 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001439 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001440 break;
1441 }
1442#endif
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001444}
1445
Luiz Capitulino98696222010-02-10 23:49:58 -02001446static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001447{
1448 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001449 uint32_t size = qdict_get_int(qdict, "size");
1450 const char *filename = qdict_get_str(qdict, "filename");
1451 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001452 uint32_t l;
1453 CPUState *env;
1454 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001455 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001456
1457 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001458
1459 f = fopen(filename, "wb");
1460 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001461 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001462 return -1;
bellardb371dc52007-01-03 15:20:39 +00001463 }
1464 while (size != 0) {
1465 l = sizeof(buf);
1466 if (l > size)
1467 l = size;
1468 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001469 if (fwrite(buf, 1, l, f) != l) {
1470 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1471 goto exit;
1472 }
bellardb371dc52007-01-03 15:20:39 +00001473 addr += l;
1474 size -= l;
1475 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001476
1477 ret = 0;
1478
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001479exit:
bellardb371dc52007-01-03 15:20:39 +00001480 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001481 return ret;
bellardb371dc52007-01-03 15:20:39 +00001482}
1483
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001484static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001485 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001486{
1487 FILE *f;
1488 uint32_t l;
1489 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001490 uint32_t size = qdict_get_int(qdict, "size");
1491 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001492 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001493 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001494
1495 f = fopen(filename, "wb");
1496 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001497 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001498 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001499 }
1500 while (size != 0) {
1501 l = sizeof(buf);
1502 if (l > size)
1503 l = size;
1504 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001505 if (fwrite(buf, 1, l, f) != l) {
1506 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1507 goto exit;
1508 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001509 fflush(f);
1510 addr += l;
1511 size -= l;
1512 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001513
1514 ret = 0;
1515
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001516exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001517 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001518 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001519}
1520
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001521static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001522{
1523 uint32_t addr;
1524 uint8_t buf[1];
1525 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001526 uint32_t start = qdict_get_int(qdict, "start");
1527 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001528
1529 sum = 0;
1530 for(addr = start; addr < (start + size); addr++) {
1531 cpu_physical_memory_rw(addr, buf, 1, 0);
1532 /* BSD sum algorithm ('sum' Unix command) */
1533 sum = (sum >> 1) | (sum << 15);
1534 sum += buf[0];
1535 }
aliguori376253e2009-03-05 23:01:23 +00001536 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001537}
1538
bellarda3a91a32004-06-04 11:06:21 +00001539typedef struct {
1540 int keycode;
1541 const char *name;
1542} KeyDef;
1543
1544static const KeyDef key_defs[] = {
1545 { 0x2a, "shift" },
1546 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001547
bellarda3a91a32004-06-04 11:06:21 +00001548 { 0x38, "alt" },
1549 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001550 { 0x64, "altgr" },
1551 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001552 { 0x1d, "ctrl" },
1553 { 0x9d, "ctrl_r" },
1554
1555 { 0xdd, "menu" },
1556
1557 { 0x01, "esc" },
1558
1559 { 0x02, "1" },
1560 { 0x03, "2" },
1561 { 0x04, "3" },
1562 { 0x05, "4" },
1563 { 0x06, "5" },
1564 { 0x07, "6" },
1565 { 0x08, "7" },
1566 { 0x09, "8" },
1567 { 0x0a, "9" },
1568 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001569 { 0x0c, "minus" },
1570 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001571 { 0x0e, "backspace" },
1572
1573 { 0x0f, "tab" },
1574 { 0x10, "q" },
1575 { 0x11, "w" },
1576 { 0x12, "e" },
1577 { 0x13, "r" },
1578 { 0x14, "t" },
1579 { 0x15, "y" },
1580 { 0x16, "u" },
1581 { 0x17, "i" },
1582 { 0x18, "o" },
1583 { 0x19, "p" },
1584
1585 { 0x1c, "ret" },
1586
1587 { 0x1e, "a" },
1588 { 0x1f, "s" },
1589 { 0x20, "d" },
1590 { 0x21, "f" },
1591 { 0x22, "g" },
1592 { 0x23, "h" },
1593 { 0x24, "j" },
1594 { 0x25, "k" },
1595 { 0x26, "l" },
1596
1597 { 0x2c, "z" },
1598 { 0x2d, "x" },
1599 { 0x2e, "c" },
1600 { 0x2f, "v" },
1601 { 0x30, "b" },
1602 { 0x31, "n" },
1603 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001604 { 0x33, "comma" },
1605 { 0x34, "dot" },
1606 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001607
balrog4d3b6f62008-02-10 16:33:14 +00001608 { 0x37, "asterisk" },
1609
bellarda3a91a32004-06-04 11:06:21 +00001610 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001611 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001612 { 0x3b, "f1" },
1613 { 0x3c, "f2" },
1614 { 0x3d, "f3" },
1615 { 0x3e, "f4" },
1616 { 0x3f, "f5" },
1617 { 0x40, "f6" },
1618 { 0x41, "f7" },
1619 { 0x42, "f8" },
1620 { 0x43, "f9" },
1621 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001622 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001623 { 0x46, "scroll_lock" },
1624
bellard64866c32006-05-07 18:03:31 +00001625 { 0xb5, "kp_divide" },
1626 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001627 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001628 { 0x4e, "kp_add" },
1629 { 0x9c, "kp_enter" },
1630 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001631 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001632
1633 { 0x52, "kp_0" },
1634 { 0x4f, "kp_1" },
1635 { 0x50, "kp_2" },
1636 { 0x51, "kp_3" },
1637 { 0x4b, "kp_4" },
1638 { 0x4c, "kp_5" },
1639 { 0x4d, "kp_6" },
1640 { 0x47, "kp_7" },
1641 { 0x48, "kp_8" },
1642 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001643
bellarda3a91a32004-06-04 11:06:21 +00001644 { 0x56, "<" },
1645
1646 { 0x57, "f11" },
1647 { 0x58, "f12" },
1648
1649 { 0xb7, "print" },
1650
1651 { 0xc7, "home" },
1652 { 0xc9, "pgup" },
1653 { 0xd1, "pgdn" },
1654 { 0xcf, "end" },
1655
1656 { 0xcb, "left" },
1657 { 0xc8, "up" },
1658 { 0xd0, "down" },
1659 { 0xcd, "right" },
1660
1661 { 0xd2, "insert" },
1662 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001663#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1664 { 0xf0, "stop" },
1665 { 0xf1, "again" },
1666 { 0xf2, "props" },
1667 { 0xf3, "undo" },
1668 { 0xf4, "front" },
1669 { 0xf5, "copy" },
1670 { 0xf6, "open" },
1671 { 0xf7, "paste" },
1672 { 0xf8, "find" },
1673 { 0xf9, "cut" },
1674 { 0xfa, "lf" },
1675 { 0xfb, "help" },
1676 { 0xfc, "meta_l" },
1677 { 0xfd, "meta_r" },
1678 { 0xfe, "compose" },
1679#endif
bellarda3a91a32004-06-04 11:06:21 +00001680 { 0, NULL },
1681};
1682
1683static int get_keycode(const char *key)
1684{
1685 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001686 char *endp;
1687 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001688
1689 for(p = key_defs; p->name != NULL; p++) {
1690 if (!strcmp(key, p->name))
1691 return p->keycode;
1692 }
bellard64866c32006-05-07 18:03:31 +00001693 if (strstart(key, "0x", NULL)) {
1694 ret = strtoul(key, &endp, 0);
1695 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1696 return ret;
1697 }
bellarda3a91a32004-06-04 11:06:21 +00001698 return -1;
1699}
1700
balrogc8256f92008-06-08 22:45:01 +00001701#define MAX_KEYCODES 16
1702static uint8_t keycodes[MAX_KEYCODES];
1703static int nb_pending_keycodes;
1704static QEMUTimer *key_timer;
1705
1706static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001707{
balrogc8256f92008-06-08 22:45:01 +00001708 int keycode;
1709
1710 while (nb_pending_keycodes > 0) {
1711 nb_pending_keycodes--;
1712 keycode = keycodes[nb_pending_keycodes];
1713 if (keycode & 0x80)
1714 kbd_put_keycode(0xe0);
1715 kbd_put_keycode(keycode | 0x80);
1716 }
1717}
1718
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001719static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001720{
balrog3401c0d2008-06-04 10:05:59 +00001721 char keyname_buf[16];
1722 char *separator;
1723 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001724 const char *string = qdict_get_str(qdict, "string");
1725 int has_hold_time = qdict_haskey(qdict, "hold_time");
1726 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001727
balrogc8256f92008-06-08 22:45:01 +00001728 if (nb_pending_keycodes > 0) {
1729 qemu_del_timer(key_timer);
1730 release_keys(NULL);
1731 }
1732 if (!has_hold_time)
1733 hold_time = 100;
1734 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001735 while (1) {
1736 separator = strchr(string, '-');
1737 keyname_len = separator ? separator - string : strlen(string);
1738 if (keyname_len > 0) {
1739 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1740 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001741 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001742 return;
bellarda3a91a32004-06-04 11:06:21 +00001743 }
balrogc8256f92008-06-08 22:45:01 +00001744 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001745 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001746 return;
1747 }
1748 keyname_buf[keyname_len] = 0;
1749 keycode = get_keycode(keyname_buf);
1750 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001751 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001752 return;
1753 }
balrogc8256f92008-06-08 22:45:01 +00001754 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001755 }
balrog3401c0d2008-06-04 10:05:59 +00001756 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001757 break;
balrog3401c0d2008-06-04 10:05:59 +00001758 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001759 }
balrogc8256f92008-06-08 22:45:01 +00001760 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001761 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001762 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001763 keycode = keycodes[i];
1764 if (keycode & 0x80)
1765 kbd_put_keycode(0xe0);
1766 kbd_put_keycode(keycode & 0x7f);
1767 }
balrogc8256f92008-06-08 22:45:01 +00001768 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001769 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001770 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001771}
1772
bellard13224a82006-07-14 22:03:35 +00001773static int mouse_button_state;
1774
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001775static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001776{
1777 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001778 const char *dx_str = qdict_get_str(qdict, "dx_str");
1779 const char *dy_str = qdict_get_str(qdict, "dy_str");
1780 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001781 dx = strtol(dx_str, NULL, 0);
1782 dy = strtol(dy_str, NULL, 0);
1783 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001784 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001785 dz = strtol(dz_str, NULL, 0);
1786 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1787}
1788
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001789static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001790{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001791 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001792 mouse_button_state = button_state;
1793 kbd_mouse_event(0, 0, 0, mouse_button_state);
1794}
1795
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001796static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001797{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001798 int size = qdict_get_int(qdict, "size");
1799 int addr = qdict_get_int(qdict, "addr");
1800 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001801 uint32_t val;
1802 int suffix;
1803
1804 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001805 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001806 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001807 addr++;
1808 }
1809 addr &= 0xffff;
1810
1811 switch(size) {
1812 default:
1813 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001814 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001815 suffix = 'b';
1816 break;
1817 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001818 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001819 suffix = 'w';
1820 break;
1821 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001822 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001823 suffix = 'l';
1824 break;
1825 }
aliguori376253e2009-03-05 23:01:23 +00001826 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1827 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001828}
bellarda3a91a32004-06-04 11:06:21 +00001829
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001830static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001831{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001832 int size = qdict_get_int(qdict, "size");
1833 int addr = qdict_get_int(qdict, "addr");
1834 int val = qdict_get_int(qdict, "val");
1835
Jan Kiszkaf1147842009-07-14 10:20:11 +02001836 addr &= IOPORTS_MASK;
1837
1838 switch (size) {
1839 default:
1840 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001841 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001842 break;
1843 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001844 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001845 break;
1846 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001847 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001848 break;
1849 }
1850}
1851
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001852static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001853{
1854 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001855 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001856
Jan Kiszka76e30d02009-07-02 00:19:02 +02001857 res = qemu_boot_set(bootdevice);
1858 if (res == 0) {
1859 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1860 } else if (res > 0) {
1861 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001862 } else {
aliguori376253e2009-03-05 23:01:23 +00001863 monitor_printf(mon, "no function defined to set boot device list for "
1864 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001865 }
1866}
1867
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001868/**
1869 * do_system_reset(): Issue a machine reset
1870 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001871static int do_system_reset(Monitor *mon, const QDict *qdict,
1872 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001873{
1874 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001875 return 0;
bellarde4f90822004-06-20 12:35:44 +00001876}
1877
Luiz Capitulino43076662009-10-07 13:41:59 -03001878/**
1879 * do_system_powerdown(): Issue a machine powerdown
1880 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001881static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1882 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001883{
1884 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001885 return 0;
bellard34751872005-07-02 14:31:34 +00001886}
1887
bellardb86bda52004-09-18 19:32:46 +00001888#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001889static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001890{
aliguori376253e2009-03-05 23:01:23 +00001891 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1892 addr,
1893 pte & mask,
1894 pte & PG_GLOBAL_MASK ? 'G' : '-',
1895 pte & PG_PSE_MASK ? 'P' : '-',
1896 pte & PG_DIRTY_MASK ? 'D' : '-',
1897 pte & PG_ACCESSED_MASK ? 'A' : '-',
1898 pte & PG_PCD_MASK ? 'C' : '-',
1899 pte & PG_PWT_MASK ? 'T' : '-',
1900 pte & PG_USER_MASK ? 'U' : '-',
1901 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001902}
1903
aliguori376253e2009-03-05 23:01:23 +00001904static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001905{
bellard6a00d602005-11-21 23:25:50 +00001906 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001907 int l1, l2;
1908 uint32_t pgd, pde, pte;
1909
bellard6a00d602005-11-21 23:25:50 +00001910 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001911
bellardb86bda52004-09-18 19:32:46 +00001912 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001913 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001914 return;
1915 }
1916 pgd = env->cr[3] & ~0xfff;
1917 for(l1 = 0; l1 < 1024; l1++) {
1918 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1919 pde = le32_to_cpu(pde);
1920 if (pde & PG_PRESENT_MASK) {
1921 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001922 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001923 } else {
1924 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001925 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001926 (uint8_t *)&pte, 4);
1927 pte = le32_to_cpu(pte);
1928 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001929 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001930 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001931 ~0xfff);
1932 }
1933 }
1934 }
1935 }
1936 }
1937}
1938
aliguori376253e2009-03-05 23:01:23 +00001939static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001940 uint32_t end, int prot)
1941{
bellard9746b152004-11-11 18:30:24 +00001942 int prot1;
1943 prot1 = *plast_prot;
1944 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001945 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001946 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1947 *pstart, end, end - *pstart,
1948 prot1 & PG_USER_MASK ? 'u' : '-',
1949 'r',
1950 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001951 }
1952 if (prot != 0)
1953 *pstart = end;
1954 else
1955 *pstart = -1;
1956 *plast_prot = prot;
1957 }
1958}
1959
aliguori376253e2009-03-05 23:01:23 +00001960static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001961{
bellard6a00d602005-11-21 23:25:50 +00001962 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001963 int l1, l2, prot, last_prot;
1964 uint32_t pgd, pde, pte, start, end;
1965
bellard6a00d602005-11-21 23:25:50 +00001966 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001967
bellardb86bda52004-09-18 19:32:46 +00001968 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001969 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001970 return;
1971 }
1972 pgd = env->cr[3] & ~0xfff;
1973 last_prot = 0;
1974 start = -1;
1975 for(l1 = 0; l1 < 1024; l1++) {
1976 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1977 pde = le32_to_cpu(pde);
1978 end = l1 << 22;
1979 if (pde & PG_PRESENT_MASK) {
1980 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1981 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001982 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001983 } else {
1984 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001985 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001986 (uint8_t *)&pte, 4);
1987 pte = le32_to_cpu(pte);
1988 end = (l1 << 22) + (l2 << 12);
1989 if (pte & PG_PRESENT_MASK) {
1990 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1991 } else {
1992 prot = 0;
1993 }
aliguori376253e2009-03-05 23:01:23 +00001994 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001995 }
1996 }
1997 } else {
1998 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001999 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002000 }
2001 }
2002}
2003#endif
2004
aurel327c664e22009-03-03 06:12:22 +00002005#if defined(TARGET_SH4)
2006
aliguori376253e2009-03-05 23:01:23 +00002007static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002008{
aliguori376253e2009-03-05 23:01:23 +00002009 monitor_printf(mon, " tlb%i:\t"
2010 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2011 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2012 "dirty=%hhu writethrough=%hhu\n",
2013 idx,
2014 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2015 tlb->v, tlb->sh, tlb->c, tlb->pr,
2016 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002017}
2018
aliguori376253e2009-03-05 23:01:23 +00002019static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002020{
2021 CPUState *env = mon_get_cpu();
2022 int i;
2023
aliguori376253e2009-03-05 23:01:23 +00002024 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002025 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002026 print_tlb (mon, i, &env->itlb[i]);
2027 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002028 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002029 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002030}
2031
2032#endif
2033
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002034static void do_info_kvm_print(Monitor *mon, const QObject *data)
2035{
2036 QDict *qdict;
2037
2038 qdict = qobject_to_qdict(data);
2039
2040 monitor_printf(mon, "kvm support: ");
2041 if (qdict_get_bool(qdict, "present")) {
2042 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2043 "enabled" : "disabled");
2044 } else {
2045 monitor_printf(mon, "not compiled\n");
2046 }
2047}
2048
2049/**
2050 * do_info_kvm(): Show KVM information
2051 *
2052 * Return a QDict with the following information:
2053 *
2054 * - "enabled": true if KVM support is enabled, false otherwise
2055 * - "present": true if QEMU has KVM support, false otherwise
2056 *
2057 * Example:
2058 *
2059 * { "enabled": true, "present": true }
2060 */
2061static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002062{
2063#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002064 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2065 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002066#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002067 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002068#endif
2069}
2070
aliguori030ea372009-04-21 22:30:47 +00002071static void do_info_numa(Monitor *mon)
2072{
aliguorib28b6232009-04-22 20:20:29 +00002073 int i;
aliguori030ea372009-04-21 22:30:47 +00002074 CPUState *env;
2075
2076 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2077 for (i = 0; i < nb_numa_nodes; i++) {
2078 monitor_printf(mon, "node %d cpus:", i);
2079 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2080 if (env->numa_node == i) {
2081 monitor_printf(mon, " %d", env->cpu_index);
2082 }
2083 }
2084 monitor_printf(mon, "\n");
2085 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2086 node_mem[i] >> 20);
2087 }
2088}
2089
bellard5f1ce942006-02-08 22:40:15 +00002090#ifdef CONFIG_PROFILER
2091
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002092int64_t qemu_time;
2093int64_t dev_time;
2094
aliguori376253e2009-03-05 23:01:23 +00002095static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002096{
2097 int64_t total;
2098 total = qemu_time;
2099 if (total == 0)
2100 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002101 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002102 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002103 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002104 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002105 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002106 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002107}
2108#else
aliguori376253e2009-03-05 23:01:23 +00002109static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002110{
aliguori376253e2009-03-05 23:01:23 +00002111 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002112}
2113#endif
2114
bellardec36b692006-07-16 18:57:03 +00002115/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002116static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002117
aliguori376253e2009-03-05 23:01:23 +00002118static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002119{
2120 int i;
2121 CaptureState *s;
2122
2123 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002124 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002125 s->ops.info (s->opaque);
2126 }
2127}
2128
Blue Swirl23130862009-06-06 08:22:04 +00002129#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002130static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002131{
2132 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002133 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002134 CaptureState *s;
2135
2136 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2137 if (i == n) {
2138 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002139 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002140 qemu_free (s);
2141 return;
2142 }
2143 }
2144}
2145
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002146static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002147{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002148 const char *path = qdict_get_str(qdict, "path");
2149 int has_freq = qdict_haskey(qdict, "freq");
2150 int freq = qdict_get_try_int(qdict, "freq", -1);
2151 int has_bits = qdict_haskey(qdict, "bits");
2152 int bits = qdict_get_try_int(qdict, "bits", -1);
2153 int has_channels = qdict_haskey(qdict, "nchannels");
2154 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002155 CaptureState *s;
2156
2157 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002158
2159 freq = has_freq ? freq : 44100;
2160 bits = has_bits ? bits : 16;
2161 nchannels = has_channels ? nchannels : 2;
2162
2163 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002164 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002165 qemu_free (s);
2166 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002167 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002168}
2169#endif
2170
aurel32dc1c0b72008-04-27 23:52:12 +00002171#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002172static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002173{
2174 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002175 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002176
2177 for (env = first_cpu; env != NULL; env = env->next_cpu)
2178 if (env->cpu_index == cpu_index) {
2179 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2180 break;
2181 }
2182}
2183#endif
2184
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002185static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002186{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002187 QDict *qdict;
2188
2189 qdict = qobject_to_qdict(data);
2190
2191 monitor_printf(mon, "VM status: ");
2192 if (qdict_get_bool(qdict, "running")) {
2193 monitor_printf(mon, "running");
2194 if (qdict_get_bool(qdict, "singlestep")) {
2195 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002196 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002197 } else {
2198 monitor_printf(mon, "paused");
2199 }
2200
2201 monitor_printf(mon, "\n");
2202}
2203
2204/**
2205 * do_info_status(): VM status
2206 *
2207 * Return a QDict with the following information:
2208 *
2209 * - "running": true if the VM is running, or false if it is paused
2210 * - "singlestep": true if the VM is in single step mode, false otherwise
2211 *
2212 * Example:
2213 *
2214 * { "running": true, "singlestep": false }
2215 */
2216static void do_info_status(Monitor *mon, QObject **ret_data)
2217{
2218 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2219 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002220}
2221
Adam Litke625a5be2010-01-26 14:17:35 -06002222static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002223{
Adam Litke625a5be2010-01-26 14:17:35 -06002224 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002225
Adam Litke625a5be2010-01-26 14:17:35 -06002226 if (strcmp(key, "actual"))
2227 monitor_printf(mon, ",%s=%" PRId64, key,
2228 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002229}
2230
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002231static void monitor_print_balloon(Monitor *mon, const QObject *data)
2232{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002233 QDict *qdict;
2234
2235 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002236 if (!qdict_haskey(qdict, "actual"))
2237 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002238
Adam Litke625a5be2010-01-26 14:17:35 -06002239 monitor_printf(mon, "balloon: actual=%" PRId64,
2240 qdict_get_int(qdict, "actual") >> 20);
2241 qdict_iter(qdict, print_balloon_stat, mon);
2242 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002243}
2244
2245/**
2246 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002247 *
Adam Litke625a5be2010-01-26 14:17:35 -06002248 * Make an asynchronous request for balloon info. When the request completes
2249 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002250 *
Adam Litke625a5be2010-01-26 14:17:35 -06002251 * - "actual": current balloon value in bytes
2252 * The following fields may or may not be present:
2253 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2254 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2255 * - "major_page_faults": Number of major faults
2256 * - "minor_page_faults": Number of minor faults
2257 * - "free_mem": Total amount of free and unused memory (bytes)
2258 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002259 *
2260 * Example:
2261 *
Adam Litke625a5be2010-01-26 14:17:35 -06002262 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2263 * "major_page_faults": 142, "minor_page_faults": 239245,
2264 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002265 */
Adam Litke625a5be2010-01-26 14:17:35 -06002266static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002267{
Adam Litke625a5be2010-01-26 14:17:35 -06002268 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002269
Adam Litke625a5be2010-01-26 14:17:35 -06002270 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2271 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2272 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002273 }
Adam Litke625a5be2010-01-26 14:17:35 -06002274
2275 ret = qemu_balloon_status(cb, opaque);
2276 if (!ret) {
2277 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2278 return -1;
2279 }
2280
2281 return 0;
2282}
2283
2284/**
2285 * do_balloon(): Request VM to change its memory allocation
2286 */
2287static int do_balloon(Monitor *mon, const QDict *params,
2288 MonitorCompletion cb, void *opaque)
2289{
2290 int ret;
2291
2292 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2293 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2294 return -1;
2295 }
2296
2297 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2298 if (ret == 0) {
2299 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2300 return -1;
2301 }
2302
2303 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002304}
2305
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002306static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002307{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002308 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002309
aliguori76655d62009-03-06 20:27:37 +00002310 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002311 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002312 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002313 return acl;
2314}
aliguori76655d62009-03-06 20:27:37 +00002315
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002316static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002317{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002318 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002319 qemu_acl *acl = find_acl(mon, aclname);
2320 qemu_acl_entry *entry;
2321 int i = 0;
2322
2323 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002324 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002325 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002326 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002327 i++;
2328 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002329 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002330 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002331 }
2332}
2333
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002334static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002335{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002336 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002337 qemu_acl *acl = find_acl(mon, aclname);
2338
2339 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002340 qemu_acl_reset(acl);
2341 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002342 }
2343}
aliguori76655d62009-03-06 20:27:37 +00002344
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002345static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002346{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002347 const char *aclname = qdict_get_str(qdict, "aclname");
2348 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002349 qemu_acl *acl = find_acl(mon, aclname);
2350
2351 if (acl) {
2352 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002353 acl->defaultDeny = 0;
2354 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002355 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002356 acl->defaultDeny = 1;
2357 monitor_printf(mon, "acl: policy set to 'deny'\n");
2358 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002359 monitor_printf(mon, "acl: unknown policy '%s', "
2360 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002361 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002362 }
2363}
aliguori76655d62009-03-06 20:27:37 +00002364
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002365static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002366{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002367 const char *aclname = qdict_get_str(qdict, "aclname");
2368 const char *match = qdict_get_str(qdict, "match");
2369 const char *policy = qdict_get_str(qdict, "policy");
2370 int has_index = qdict_haskey(qdict, "index");
2371 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002372 qemu_acl *acl = find_acl(mon, aclname);
2373 int deny, ret;
2374
2375 if (acl) {
2376 if (strcmp(policy, "allow") == 0) {
2377 deny = 0;
2378 } else if (strcmp(policy, "deny") == 0) {
2379 deny = 1;
2380 } else {
2381 monitor_printf(mon, "acl: unknown policy '%s', "
2382 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002383 return;
2384 }
aliguori28a76be2009-03-06 20:27:40 +00002385 if (has_index)
2386 ret = qemu_acl_insert(acl, deny, match, index);
2387 else
2388 ret = qemu_acl_append(acl, deny, match);
2389 if (ret < 0)
2390 monitor_printf(mon, "acl: unable to add acl entry\n");
2391 else
2392 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002393 }
2394}
aliguori76655d62009-03-06 20:27:37 +00002395
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002396static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002397{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002398 const char *aclname = qdict_get_str(qdict, "aclname");
2399 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002400 qemu_acl *acl = find_acl(mon, aclname);
2401 int ret;
aliguori76655d62009-03-06 20:27:37 +00002402
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002403 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002404 ret = qemu_acl_remove(acl, match);
2405 if (ret < 0)
2406 monitor_printf(mon, "acl: no matching acl entry\n");
2407 else
2408 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002409 }
2410}
2411
Huang Ying79c4f6b2009-06-23 10:05:14 +08002412#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002413static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002414{
2415 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002416 int cpu_index = qdict_get_int(qdict, "cpu_index");
2417 int bank = qdict_get_int(qdict, "bank");
2418 uint64_t status = qdict_get_int(qdict, "status");
2419 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2420 uint64_t addr = qdict_get_int(qdict, "addr");
2421 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002422
2423 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2424 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2425 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2426 break;
2427 }
2428}
2429#endif
2430
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002431static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002432{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002433 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002434 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002435 int fd;
2436
2437 fd = qemu_chr_get_msgfd(mon->chr);
2438 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002439 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002440 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002441 }
2442
2443 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002444 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002445 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002446 }
2447
2448 fd = dup(fd);
2449 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002450 if (errno == EMFILE)
2451 qemu_error_new(QERR_TOO_MANY_FILES);
2452 else
2453 qemu_error_new(QERR_UNDEFINED_ERROR);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002454 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002455 }
2456
Blue Swirl72cf2d42009-09-12 07:36:22 +00002457 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002458 if (strcmp(monfd->name, fdname) != 0) {
2459 continue;
2460 }
2461
2462 close(monfd->fd);
2463 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002464 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002465 }
2466
Anthony Liguoric227f092009-10-01 16:12:16 -05002467 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002468 monfd->name = qemu_strdup(fdname);
2469 monfd->fd = fd;
2470
Blue Swirl72cf2d42009-09-12 07:36:22 +00002471 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002472 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002473}
2474
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002475static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002476{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002477 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002478 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002479
Blue Swirl72cf2d42009-09-12 07:36:22 +00002480 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002481 if (strcmp(monfd->name, fdname) != 0) {
2482 continue;
2483 }
2484
Blue Swirl72cf2d42009-09-12 07:36:22 +00002485 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002486 close(monfd->fd);
2487 qemu_free(monfd->name);
2488 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002489 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002490 }
2491
Markus Armbruster063c1a02009-12-07 21:37:11 +01002492 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002493 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002494}
2495
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002496static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002497{
2498 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002499 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002500
2501 vm_stop(0);
2502
Juan Quintela05f24012009-08-20 19:42:22 +02002503 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002504 vm_start();
2505}
2506
Mark McLoughlin7768e042009-07-22 09:11:41 +01002507int monitor_get_fd(Monitor *mon, const char *fdname)
2508{
Anthony Liguoric227f092009-10-01 16:12:16 -05002509 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002510
Blue Swirl72cf2d42009-09-12 07:36:22 +00002511 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002512 int fd;
2513
2514 if (strcmp(monfd->name, fdname) != 0) {
2515 continue;
2516 }
2517
2518 fd = monfd->fd;
2519
2520 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002521 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002522 qemu_free(monfd->name);
2523 qemu_free(monfd);
2524
2525 return fd;
2526 }
2527
2528 return -1;
2529}
2530
Anthony Liguoric227f092009-10-01 16:12:16 -05002531static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002532#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002533 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002534};
2535
Blue Swirl23130862009-06-06 08:22:04 +00002536/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002537static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 {
2539 .name = "version",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002543 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002544 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 },
2546 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002547 .name = "commands",
2548 .args_type = "",
2549 .params = "",
2550 .help = "list QMP available commands",
2551 .user_print = monitor_user_noop,
2552 .mhandler.info_new = do_info_commands,
2553 },
2554 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .name = "network",
2556 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 .params = "",
2558 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002559 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 },
2561 {
2562 .name = "chardev",
2563 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 .params = "",
2565 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002566 .user_print = qemu_chr_info_print,
2567 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 },
2569 {
2570 .name = "block",
2571 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 .params = "",
2573 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002574 .user_print = bdrv_info_print,
2575 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 },
2577 {
2578 .name = "blockstats",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002582 .user_print = bdrv_stats_print,
2583 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 },
2585 {
2586 .name = "registers",
2587 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 .params = "",
2589 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002590 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 },
2592 {
2593 .name = "cpus",
2594 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 .params = "",
2596 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002597 .user_print = monitor_print_cpus,
2598 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 },
2600 {
2601 .name = "history",
2602 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 .params = "",
2604 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002605 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 },
2607 {
2608 .name = "irq",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002612 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
2614 {
2615 .name = "pic",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002619 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 },
2621 {
2622 .name = "pci",
2623 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 .params = "",
2625 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002626 .user_print = do_pci_info_print,
2627 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 },
aurel327c664e22009-03-03 06:12:22 +00002629#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 {
2631 .name = "tlb",
2632 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 .params = "",
2634 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002635 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
aurel327c664e22009-03-03 06:12:22 +00002637#endif
2638#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 {
2640 .name = "mem",
2641 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 .params = "",
2643 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002644 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 },
2646 {
2647 .name = "hpet",
2648 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002649 .params = "",
2650 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002651 .user_print = do_info_hpet_print,
2652 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
bellardb86bda52004-09-18 19:32:46 +00002654#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 {
2656 .name = "jit",
2657 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 .params = "",
2659 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002660 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 },
2662 {
2663 .name = "kvm",
2664 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 .params = "",
2666 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002667 .user_print = do_info_kvm_print,
2668 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "numa",
2672 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 .params = "",
2674 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002675 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 },
2677 {
2678 .name = "usb",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002682 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
2684 {
2685 .name = "usbhost",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002689 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "profile",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002696 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
2699 .name = "capture",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002703 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "snapshots",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002710 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
2713 .name = "status",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002717 .user_print = do_info_status_print,
2718 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
2720 {
2721 .name = "pcmcia",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002725 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 },
2727 {
2728 .name = "mice",
2729 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 .params = "",
2731 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002732 .user_print = do_info_mice_print,
2733 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
2735 {
2736 .name = "vnc",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002740 .user_print = do_info_vnc_print,
2741 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 },
2743 {
2744 .name = "name",
2745 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 .params = "",
2747 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002748 .user_print = do_info_name_print,
2749 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 },
2751 {
2752 .name = "uuid",
2753 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 .params = "",
2755 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002756 .user_print = do_info_uuid_print,
2757 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 },
j_mayer76a66252007-03-07 08:32:30 +00002759#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 {
2761 .name = "cpustats",
2762 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 .params = "",
2764 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002765 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 },
j_mayer76a66252007-03-07 08:32:30 +00002767#endif
blueswir131a60e22007-10-26 18:42:59 +00002768#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002769 {
2770 .name = "usernet",
2771 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 .params = "",
2773 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002774 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 },
blueswir131a60e22007-10-26 18:42:59 +00002776#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 {
2778 .name = "migrate",
2779 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 .params = "",
2781 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002782 .user_print = do_info_migrate_print,
2783 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 },
2785 {
2786 .name = "balloon",
2787 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 .params = "",
2789 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002790 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002791 .mhandler.info_async = do_info_balloon,
2792 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002793 },
2794 {
2795 .name = "qtree",
2796 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 .params = "",
2798 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002799 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002800 },
2801 {
2802 .name = "qdm",
2803 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002804 .params = "",
2805 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002806 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002807 },
2808 {
2809 .name = "roms",
2810 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 .params = "",
2812 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002813 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 },
2815 {
2816 .name = NULL,
2817 },
bellard9dc39cb2004-03-14 21:38:27 +00002818};
2819
bellard9307c4c2004-04-04 12:57:25 +00002820/*******************************************************************/
2821
2822static const char *pch;
2823static jmp_buf expr_env;
2824
bellard92a31b12005-02-10 22:00:52 +00002825#define MD_TLONG 0
2826#define MD_I32 1
2827
bellard9307c4c2004-04-04 12:57:25 +00002828typedef struct MonitorDef {
2829 const char *name;
2830 int offset;
blueswir18662d652008-10-02 18:32:44 +00002831 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002832 int type;
bellard9307c4c2004-04-04 12:57:25 +00002833} MonitorDef;
2834
bellard57206fd2004-04-25 18:54:52 +00002835#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002836static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002837{
bellard6a00d602005-11-21 23:25:50 +00002838 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002839 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002840}
2841#endif
2842
bellarda541f292004-04-12 20:39:29 +00002843#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002844static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002845{
bellard6a00d602005-11-21 23:25:50 +00002846 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002847 unsigned int u;
2848 int i;
2849
2850 u = 0;
2851 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002852 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002853
2854 return u;
2855}
2856
blueswir18662d652008-10-02 18:32:44 +00002857static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002858{
bellard6a00d602005-11-21 23:25:50 +00002859 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002860 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002861}
2862
blueswir18662d652008-10-02 18:32:44 +00002863static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002864{
bellard6a00d602005-11-21 23:25:50 +00002865 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002866 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002867}
bellard9fddaa02004-05-21 12:59:32 +00002868
blueswir18662d652008-10-02 18:32:44 +00002869static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002870{
bellard6a00d602005-11-21 23:25:50 +00002871 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002872 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002873}
2874
blueswir18662d652008-10-02 18:32:44 +00002875static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002876{
bellard6a00d602005-11-21 23:25:50 +00002877 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002878 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002879}
2880
blueswir18662d652008-10-02 18:32:44 +00002881static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002882{
bellard6a00d602005-11-21 23:25:50 +00002883 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002884 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002885}
bellarda541f292004-04-12 20:39:29 +00002886#endif
2887
bellarde95c8d52004-09-30 22:22:08 +00002888#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002889#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002890static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002891{
bellard6a00d602005-11-21 23:25:50 +00002892 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002893 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002894}
bellard7b936c02005-10-30 17:05:13 +00002895#endif
bellarde95c8d52004-09-30 22:22:08 +00002896
blueswir18662d652008-10-02 18:32:44 +00002897static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002898{
bellard6a00d602005-11-21 23:25:50 +00002899 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002900 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002901}
2902#endif
2903
blueswir18662d652008-10-02 18:32:44 +00002904static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002905#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002906
2907#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002908 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002909 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002910 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002911
bellard9307c4c2004-04-04 12:57:25 +00002912 { "eax", offsetof(CPUState, regs[0]) },
2913 { "ecx", offsetof(CPUState, regs[1]) },
2914 { "edx", offsetof(CPUState, regs[2]) },
2915 { "ebx", offsetof(CPUState, regs[3]) },
2916 { "esp|sp", offsetof(CPUState, regs[4]) },
2917 { "ebp|fp", offsetof(CPUState, regs[5]) },
2918 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002919 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002920#ifdef TARGET_X86_64
2921 { "r8", offsetof(CPUState, regs[8]) },
2922 { "r9", offsetof(CPUState, regs[9]) },
2923 { "r10", offsetof(CPUState, regs[10]) },
2924 { "r11", offsetof(CPUState, regs[11]) },
2925 { "r12", offsetof(CPUState, regs[12]) },
2926 { "r13", offsetof(CPUState, regs[13]) },
2927 { "r14", offsetof(CPUState, regs[14]) },
2928 { "r15", offsetof(CPUState, regs[15]) },
2929#endif
bellard9307c4c2004-04-04 12:57:25 +00002930 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002931 { "eip", offsetof(CPUState, eip) },
2932 SEG("cs", R_CS)
2933 SEG("ds", R_DS)
2934 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002935 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002936 SEG("fs", R_FS)
2937 SEG("gs", R_GS)
2938 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002939#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002940 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002941 { "r0", offsetof(CPUState, gpr[0]) },
2942 { "r1", offsetof(CPUState, gpr[1]) },
2943 { "r2", offsetof(CPUState, gpr[2]) },
2944 { "r3", offsetof(CPUState, gpr[3]) },
2945 { "r4", offsetof(CPUState, gpr[4]) },
2946 { "r5", offsetof(CPUState, gpr[5]) },
2947 { "r6", offsetof(CPUState, gpr[6]) },
2948 { "r7", offsetof(CPUState, gpr[7]) },
2949 { "r8", offsetof(CPUState, gpr[8]) },
2950 { "r9", offsetof(CPUState, gpr[9]) },
2951 { "r10", offsetof(CPUState, gpr[10]) },
2952 { "r11", offsetof(CPUState, gpr[11]) },
2953 { "r12", offsetof(CPUState, gpr[12]) },
2954 { "r13", offsetof(CPUState, gpr[13]) },
2955 { "r14", offsetof(CPUState, gpr[14]) },
2956 { "r15", offsetof(CPUState, gpr[15]) },
2957 { "r16", offsetof(CPUState, gpr[16]) },
2958 { "r17", offsetof(CPUState, gpr[17]) },
2959 { "r18", offsetof(CPUState, gpr[18]) },
2960 { "r19", offsetof(CPUState, gpr[19]) },
2961 { "r20", offsetof(CPUState, gpr[20]) },
2962 { "r21", offsetof(CPUState, gpr[21]) },
2963 { "r22", offsetof(CPUState, gpr[22]) },
2964 { "r23", offsetof(CPUState, gpr[23]) },
2965 { "r24", offsetof(CPUState, gpr[24]) },
2966 { "r25", offsetof(CPUState, gpr[25]) },
2967 { "r26", offsetof(CPUState, gpr[26]) },
2968 { "r27", offsetof(CPUState, gpr[27]) },
2969 { "r28", offsetof(CPUState, gpr[28]) },
2970 { "r29", offsetof(CPUState, gpr[29]) },
2971 { "r30", offsetof(CPUState, gpr[30]) },
2972 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002973 /* Floating point registers */
2974 { "f0", offsetof(CPUState, fpr[0]) },
2975 { "f1", offsetof(CPUState, fpr[1]) },
2976 { "f2", offsetof(CPUState, fpr[2]) },
2977 { "f3", offsetof(CPUState, fpr[3]) },
2978 { "f4", offsetof(CPUState, fpr[4]) },
2979 { "f5", offsetof(CPUState, fpr[5]) },
2980 { "f6", offsetof(CPUState, fpr[6]) },
2981 { "f7", offsetof(CPUState, fpr[7]) },
2982 { "f8", offsetof(CPUState, fpr[8]) },
2983 { "f9", offsetof(CPUState, fpr[9]) },
2984 { "f10", offsetof(CPUState, fpr[10]) },
2985 { "f11", offsetof(CPUState, fpr[11]) },
2986 { "f12", offsetof(CPUState, fpr[12]) },
2987 { "f13", offsetof(CPUState, fpr[13]) },
2988 { "f14", offsetof(CPUState, fpr[14]) },
2989 { "f15", offsetof(CPUState, fpr[15]) },
2990 { "f16", offsetof(CPUState, fpr[16]) },
2991 { "f17", offsetof(CPUState, fpr[17]) },
2992 { "f18", offsetof(CPUState, fpr[18]) },
2993 { "f19", offsetof(CPUState, fpr[19]) },
2994 { "f20", offsetof(CPUState, fpr[20]) },
2995 { "f21", offsetof(CPUState, fpr[21]) },
2996 { "f22", offsetof(CPUState, fpr[22]) },
2997 { "f23", offsetof(CPUState, fpr[23]) },
2998 { "f24", offsetof(CPUState, fpr[24]) },
2999 { "f25", offsetof(CPUState, fpr[25]) },
3000 { "f26", offsetof(CPUState, fpr[26]) },
3001 { "f27", offsetof(CPUState, fpr[27]) },
3002 { "f28", offsetof(CPUState, fpr[28]) },
3003 { "f29", offsetof(CPUState, fpr[29]) },
3004 { "f30", offsetof(CPUState, fpr[30]) },
3005 { "f31", offsetof(CPUState, fpr[31]) },
3006 { "fpscr", offsetof(CPUState, fpscr) },
3007 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003008 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003009 { "lr", offsetof(CPUState, lr) },
3010 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003011 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003012 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003013 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003014 { "msr", 0, &monitor_get_msr, },
3015 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003016 { "tbu", 0, &monitor_get_tbu, },
3017 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003018#if defined(TARGET_PPC64)
3019 /* Address space register */
3020 { "asr", offsetof(CPUState, asr) },
3021#endif
3022 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00003023 { "sdr1", offsetof(CPUState, sdr1) },
3024 { "sr0", offsetof(CPUState, sr[0]) },
3025 { "sr1", offsetof(CPUState, sr[1]) },
3026 { "sr2", offsetof(CPUState, sr[2]) },
3027 { "sr3", offsetof(CPUState, sr[3]) },
3028 { "sr4", offsetof(CPUState, sr[4]) },
3029 { "sr5", offsetof(CPUState, sr[5]) },
3030 { "sr6", offsetof(CPUState, sr[6]) },
3031 { "sr7", offsetof(CPUState, sr[7]) },
3032 { "sr8", offsetof(CPUState, sr[8]) },
3033 { "sr9", offsetof(CPUState, sr[9]) },
3034 { "sr10", offsetof(CPUState, sr[10]) },
3035 { "sr11", offsetof(CPUState, sr[11]) },
3036 { "sr12", offsetof(CPUState, sr[12]) },
3037 { "sr13", offsetof(CPUState, sr[13]) },
3038 { "sr14", offsetof(CPUState, sr[14]) },
3039 { "sr15", offsetof(CPUState, sr[15]) },
3040 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003041#elif defined(TARGET_SPARC)
3042 { "g0", offsetof(CPUState, gregs[0]) },
3043 { "g1", offsetof(CPUState, gregs[1]) },
3044 { "g2", offsetof(CPUState, gregs[2]) },
3045 { "g3", offsetof(CPUState, gregs[3]) },
3046 { "g4", offsetof(CPUState, gregs[4]) },
3047 { "g5", offsetof(CPUState, gregs[5]) },
3048 { "g6", offsetof(CPUState, gregs[6]) },
3049 { "g7", offsetof(CPUState, gregs[7]) },
3050 { "o0", 0, monitor_get_reg },
3051 { "o1", 1, monitor_get_reg },
3052 { "o2", 2, monitor_get_reg },
3053 { "o3", 3, monitor_get_reg },
3054 { "o4", 4, monitor_get_reg },
3055 { "o5", 5, monitor_get_reg },
3056 { "o6", 6, monitor_get_reg },
3057 { "o7", 7, monitor_get_reg },
3058 { "l0", 8, monitor_get_reg },
3059 { "l1", 9, monitor_get_reg },
3060 { "l2", 10, monitor_get_reg },
3061 { "l3", 11, monitor_get_reg },
3062 { "l4", 12, monitor_get_reg },
3063 { "l5", 13, monitor_get_reg },
3064 { "l6", 14, monitor_get_reg },
3065 { "l7", 15, monitor_get_reg },
3066 { "i0", 16, monitor_get_reg },
3067 { "i1", 17, monitor_get_reg },
3068 { "i2", 18, monitor_get_reg },
3069 { "i3", 19, monitor_get_reg },
3070 { "i4", 20, monitor_get_reg },
3071 { "i5", 21, monitor_get_reg },
3072 { "i6", 22, monitor_get_reg },
3073 { "i7", 23, monitor_get_reg },
3074 { "pc", offsetof(CPUState, pc) },
3075 { "npc", offsetof(CPUState, npc) },
3076 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003077#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003078 { "psr", 0, &monitor_get_psr, },
3079 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003080#endif
bellarde95c8d52004-09-30 22:22:08 +00003081 { "tbr", offsetof(CPUState, tbr) },
3082 { "fsr", offsetof(CPUState, fsr) },
3083 { "f0", offsetof(CPUState, fpr[0]) },
3084 { "f1", offsetof(CPUState, fpr[1]) },
3085 { "f2", offsetof(CPUState, fpr[2]) },
3086 { "f3", offsetof(CPUState, fpr[3]) },
3087 { "f4", offsetof(CPUState, fpr[4]) },
3088 { "f5", offsetof(CPUState, fpr[5]) },
3089 { "f6", offsetof(CPUState, fpr[6]) },
3090 { "f7", offsetof(CPUState, fpr[7]) },
3091 { "f8", offsetof(CPUState, fpr[8]) },
3092 { "f9", offsetof(CPUState, fpr[9]) },
3093 { "f10", offsetof(CPUState, fpr[10]) },
3094 { "f11", offsetof(CPUState, fpr[11]) },
3095 { "f12", offsetof(CPUState, fpr[12]) },
3096 { "f13", offsetof(CPUState, fpr[13]) },
3097 { "f14", offsetof(CPUState, fpr[14]) },
3098 { "f15", offsetof(CPUState, fpr[15]) },
3099 { "f16", offsetof(CPUState, fpr[16]) },
3100 { "f17", offsetof(CPUState, fpr[17]) },
3101 { "f18", offsetof(CPUState, fpr[18]) },
3102 { "f19", offsetof(CPUState, fpr[19]) },
3103 { "f20", offsetof(CPUState, fpr[20]) },
3104 { "f21", offsetof(CPUState, fpr[21]) },
3105 { "f22", offsetof(CPUState, fpr[22]) },
3106 { "f23", offsetof(CPUState, fpr[23]) },
3107 { "f24", offsetof(CPUState, fpr[24]) },
3108 { "f25", offsetof(CPUState, fpr[25]) },
3109 { "f26", offsetof(CPUState, fpr[26]) },
3110 { "f27", offsetof(CPUState, fpr[27]) },
3111 { "f28", offsetof(CPUState, fpr[28]) },
3112 { "f29", offsetof(CPUState, fpr[29]) },
3113 { "f30", offsetof(CPUState, fpr[30]) },
3114 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003115#ifdef TARGET_SPARC64
3116 { "f32", offsetof(CPUState, fpr[32]) },
3117 { "f34", offsetof(CPUState, fpr[34]) },
3118 { "f36", offsetof(CPUState, fpr[36]) },
3119 { "f38", offsetof(CPUState, fpr[38]) },
3120 { "f40", offsetof(CPUState, fpr[40]) },
3121 { "f42", offsetof(CPUState, fpr[42]) },
3122 { "f44", offsetof(CPUState, fpr[44]) },
3123 { "f46", offsetof(CPUState, fpr[46]) },
3124 { "f48", offsetof(CPUState, fpr[48]) },
3125 { "f50", offsetof(CPUState, fpr[50]) },
3126 { "f52", offsetof(CPUState, fpr[52]) },
3127 { "f54", offsetof(CPUState, fpr[54]) },
3128 { "f56", offsetof(CPUState, fpr[56]) },
3129 { "f58", offsetof(CPUState, fpr[58]) },
3130 { "f60", offsetof(CPUState, fpr[60]) },
3131 { "f62", offsetof(CPUState, fpr[62]) },
3132 { "asi", offsetof(CPUState, asi) },
3133 { "pstate", offsetof(CPUState, pstate) },
3134 { "cansave", offsetof(CPUState, cansave) },
3135 { "canrestore", offsetof(CPUState, canrestore) },
3136 { "otherwin", offsetof(CPUState, otherwin) },
3137 { "wstate", offsetof(CPUState, wstate) },
3138 { "cleanwin", offsetof(CPUState, cleanwin) },
3139 { "fprs", offsetof(CPUState, fprs) },
3140#endif
bellard9307c4c2004-04-04 12:57:25 +00003141#endif
3142 { NULL },
3143};
3144
aliguori376253e2009-03-05 23:01:23 +00003145static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003146{
aliguori376253e2009-03-05 23:01:23 +00003147 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003148 longjmp(expr_env, 1);
3149}
3150
Markus Armbruster09b94182010-01-20 13:07:30 +01003151/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003152static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003153{
blueswir18662d652008-10-02 18:32:44 +00003154 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003155 void *ptr;
3156
bellard9307c4c2004-04-04 12:57:25 +00003157 for(md = monitor_defs; md->name != NULL; md++) {
3158 if (compare_cmd(name, md->name)) {
3159 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003160 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003161 } else {
bellard6a00d602005-11-21 23:25:50 +00003162 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003163 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003164 switch(md->type) {
3165 case MD_I32:
3166 *pval = *(int32_t *)ptr;
3167 break;
3168 case MD_TLONG:
3169 *pval = *(target_long *)ptr;
3170 break;
3171 default:
3172 *pval = 0;
3173 break;
3174 }
bellard9307c4c2004-04-04 12:57:25 +00003175 }
3176 return 0;
3177 }
3178 }
3179 return -1;
3180}
3181
3182static void next(void)
3183{
Blue Swirl660f11b2009-07-31 21:16:51 +00003184 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003185 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003186 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003187 pch++;
3188 }
3189}
3190
aliguori376253e2009-03-05 23:01:23 +00003191static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003192
aliguori376253e2009-03-05 23:01:23 +00003193static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003194{
blueswir1c2efc952007-09-25 17:28:42 +00003195 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003196 char *p;
bellard6a00d602005-11-21 23:25:50 +00003197 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003198
3199 switch(*pch) {
3200 case '+':
3201 next();
aliguori376253e2009-03-05 23:01:23 +00003202 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003203 break;
3204 case '-':
3205 next();
aliguori376253e2009-03-05 23:01:23 +00003206 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003207 break;
3208 case '~':
3209 next();
aliguori376253e2009-03-05 23:01:23 +00003210 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003211 break;
3212 case '(':
3213 next();
aliguori376253e2009-03-05 23:01:23 +00003214 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003215 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003216 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003217 }
3218 next();
3219 break;
bellard81d09122004-07-14 17:21:37 +00003220 case '\'':
3221 pch++;
3222 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003223 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003224 n = *pch;
3225 pch++;
3226 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003227 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003228 next();
3229 break;
bellard9307c4c2004-04-04 12:57:25 +00003230 case '$':
3231 {
3232 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003233 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003234
bellard9307c4c2004-04-04 12:57:25 +00003235 pch++;
3236 q = buf;
3237 while ((*pch >= 'a' && *pch <= 'z') ||
3238 (*pch >= 'A' && *pch <= 'Z') ||
3239 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003240 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003241 if ((q - buf) < sizeof(buf) - 1)
3242 *q++ = *pch;
3243 pch++;
3244 }
blueswir1cd390082008-11-16 13:53:32 +00003245 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003246 pch++;
3247 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003248 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003249 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003250 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003251 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003252 }
3253 break;
3254 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003255 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003256 n = 0;
3257 break;
3258 default:
blueswir17743e582007-09-24 18:39:04 +00003259#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003260 n = strtoull(pch, &p, 0);
3261#else
bellard9307c4c2004-04-04 12:57:25 +00003262 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003263#endif
bellard9307c4c2004-04-04 12:57:25 +00003264 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003265 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003266 }
3267 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003268 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003269 pch++;
3270 break;
3271 }
3272 return n;
3273}
3274
3275
aliguori376253e2009-03-05 23:01:23 +00003276static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003277{
blueswir1c2efc952007-09-25 17:28:42 +00003278 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003279 int op;
ths3b46e622007-09-17 08:09:54 +00003280
aliguori376253e2009-03-05 23:01:23 +00003281 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003282 for(;;) {
3283 op = *pch;
3284 if (op != '*' && op != '/' && op != '%')
3285 break;
3286 next();
aliguori376253e2009-03-05 23:01:23 +00003287 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003288 switch(op) {
3289 default:
3290 case '*':
3291 val *= val2;
3292 break;
3293 case '/':
3294 case '%':
ths5fafdf22007-09-16 21:08:06 +00003295 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003296 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003297 if (op == '/')
3298 val /= val2;
3299 else
3300 val %= val2;
3301 break;
3302 }
3303 }
3304 return val;
3305}
3306
aliguori376253e2009-03-05 23:01:23 +00003307static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003308{
blueswir1c2efc952007-09-25 17:28:42 +00003309 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003310 int op;
bellard9307c4c2004-04-04 12:57:25 +00003311
aliguori376253e2009-03-05 23:01:23 +00003312 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003313 for(;;) {
3314 op = *pch;
3315 if (op != '&' && op != '|' && op != '^')
3316 break;
3317 next();
aliguori376253e2009-03-05 23:01:23 +00003318 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003319 switch(op) {
3320 default:
3321 case '&':
3322 val &= val2;
3323 break;
3324 case '|':
3325 val |= val2;
3326 break;
3327 case '^':
3328 val ^= val2;
3329 break;
3330 }
3331 }
3332 return val;
3333}
3334
aliguori376253e2009-03-05 23:01:23 +00003335static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003336{
blueswir1c2efc952007-09-25 17:28:42 +00003337 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003338 int op;
bellard9307c4c2004-04-04 12:57:25 +00003339
aliguori376253e2009-03-05 23:01:23 +00003340 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003341 for(;;) {
3342 op = *pch;
3343 if (op != '+' && op != '-')
3344 break;
3345 next();
aliguori376253e2009-03-05 23:01:23 +00003346 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003347 if (op == '+')
3348 val += val2;
3349 else
3350 val -= val2;
3351 }
3352 return val;
3353}
3354
aliguori376253e2009-03-05 23:01:23 +00003355static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003356{
3357 pch = *pp;
3358 if (setjmp(expr_env)) {
3359 *pp = pch;
3360 return -1;
3361 }
blueswir1cd390082008-11-16 13:53:32 +00003362 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003363 pch++;
aliguori376253e2009-03-05 23:01:23 +00003364 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003365 *pp = pch;
3366 return 0;
3367}
3368
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003369static int get_double(Monitor *mon, double *pval, const char **pp)
3370{
3371 const char *p = *pp;
3372 char *tailp;
3373 double d;
3374
3375 d = strtod(p, &tailp);
3376 if (tailp == p) {
3377 monitor_printf(mon, "Number expected\n");
3378 return -1;
3379 }
3380 if (d != d || d - d != 0) {
3381 /* NaN or infinity */
3382 monitor_printf(mon, "Bad number\n");
3383 return -1;
3384 }
3385 *pval = d;
3386 *pp = tailp;
3387 return 0;
3388}
3389
bellard9307c4c2004-04-04 12:57:25 +00003390static int get_str(char *buf, int buf_size, const char **pp)
3391{
3392 const char *p;
3393 char *q;
3394 int c;
3395
bellard81d09122004-07-14 17:21:37 +00003396 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003397 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003398 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003399 p++;
3400 if (*p == '\0') {
3401 fail:
bellard81d09122004-07-14 17:21:37 +00003402 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003403 *pp = p;
3404 return -1;
3405 }
bellard9307c4c2004-04-04 12:57:25 +00003406 if (*p == '\"') {
3407 p++;
3408 while (*p != '\0' && *p != '\"') {
3409 if (*p == '\\') {
3410 p++;
3411 c = *p++;
3412 switch(c) {
3413 case 'n':
3414 c = '\n';
3415 break;
3416 case 'r':
3417 c = '\r';
3418 break;
3419 case '\\':
3420 case '\'':
3421 case '\"':
3422 break;
3423 default:
3424 qemu_printf("unsupported escape code: '\\%c'\n", c);
3425 goto fail;
3426 }
3427 if ((q - buf) < buf_size - 1) {
3428 *q++ = c;
3429 }
3430 } else {
3431 if ((q - buf) < buf_size - 1) {
3432 *q++ = *p;
3433 }
3434 p++;
3435 }
3436 }
3437 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003438 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003439 goto fail;
3440 }
3441 p++;
3442 } else {
blueswir1cd390082008-11-16 13:53:32 +00003443 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003444 if ((q - buf) < buf_size - 1) {
3445 *q++ = *p;
3446 }
3447 p++;
3448 }
bellard9307c4c2004-04-04 12:57:25 +00003449 }
bellard81d09122004-07-14 17:21:37 +00003450 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003451 *pp = p;
3452 return 0;
3453}
3454
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003455/*
3456 * Store the command-name in cmdname, and return a pointer to
3457 * the remaining of the command string.
3458 */
3459static const char *get_command_name(const char *cmdline,
3460 char *cmdname, size_t nlen)
3461{
3462 size_t len;
3463 const char *p, *pstart;
3464
3465 p = cmdline;
3466 while (qemu_isspace(*p))
3467 p++;
3468 if (*p == '\0')
3469 return NULL;
3470 pstart = p;
3471 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3472 p++;
3473 len = p - pstart;
3474 if (len > nlen - 1)
3475 len = nlen - 1;
3476 memcpy(cmdname, pstart, len);
3477 cmdname[len] = '\0';
3478 return p;
3479}
3480
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003481/**
3482 * Read key of 'type' into 'key' and return the current
3483 * 'type' pointer.
3484 */
3485static char *key_get_info(const char *type, char **key)
3486{
3487 size_t len;
3488 char *p, *str;
3489
3490 if (*type == ',')
3491 type++;
3492
3493 p = strchr(type, ':');
3494 if (!p) {
3495 *key = NULL;
3496 return NULL;
3497 }
3498 len = p - type;
3499
3500 str = qemu_malloc(len + 1);
3501 memcpy(str, type, len);
3502 str[len] = '\0';
3503
3504 *key = str;
3505 return ++p;
3506}
3507
bellard9307c4c2004-04-04 12:57:25 +00003508static int default_fmt_format = 'x';
3509static int default_fmt_size = 4;
3510
3511#define MAX_ARGS 16
3512
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003513static int is_valid_option(const char *c, const char *typestr)
3514{
3515 char option[3];
3516
3517 option[0] = '-';
3518 option[1] = *c;
3519 option[2] = '\0';
3520
3521 typestr = strstr(typestr, option);
3522 return (typestr != NULL);
3523}
3524
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003525static const mon_cmd_t *monitor_find_command(const char *cmdname)
3526{
3527 const mon_cmd_t *cmd;
3528
3529 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3530 if (compare_cmd(cmdname, cmd->name)) {
3531 return cmd;
3532 }
3533 }
3534
3535 return NULL;
3536}
3537
Anthony Liguoric227f092009-10-01 16:12:16 -05003538static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003539 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003540 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003541{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003542 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003543 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003544 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003545 char cmdname[256];
3546 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003547 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003548
3549#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003550 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003551#endif
ths3b46e622007-09-17 08:09:54 +00003552
bellard9307c4c2004-04-04 12:57:25 +00003553 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003554 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3555 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003556 return NULL;
ths3b46e622007-09-17 08:09:54 +00003557
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003558 cmd = monitor_find_command(cmdname);
3559 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003560 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003561 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003562 }
bellard9307c4c2004-04-04 12:57:25 +00003563
bellard9307c4c2004-04-04 12:57:25 +00003564 /* parse the parameters */
3565 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003566 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003567 typestr = key_get_info(typestr, &key);
3568 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003569 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003570 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003571 typestr++;
3572 switch(c) {
3573 case 'F':
bellard81d09122004-07-14 17:21:37 +00003574 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003575 case 's':
3576 {
3577 int ret;
ths3b46e622007-09-17 08:09:54 +00003578
blueswir1cd390082008-11-16 13:53:32 +00003579 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003580 p++;
3581 if (*typestr == '?') {
3582 typestr++;
3583 if (*p == '\0') {
3584 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003585 break;
bellard9307c4c2004-04-04 12:57:25 +00003586 }
3587 }
3588 ret = get_str(buf, sizeof(buf), &p);
3589 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003590 switch(c) {
3591 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003592 monitor_printf(mon, "%s: filename expected\n",
3593 cmdname);
bellard81d09122004-07-14 17:21:37 +00003594 break;
3595 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003596 monitor_printf(mon, "%s: block device name expected\n",
3597 cmdname);
bellard81d09122004-07-14 17:21:37 +00003598 break;
3599 default:
aliguori376253e2009-03-05 23:01:23 +00003600 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003601 break;
3602 }
bellard9307c4c2004-04-04 12:57:25 +00003603 goto fail;
3604 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003605 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003606 }
3607 break;
3608 case '/':
3609 {
3610 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003611
blueswir1cd390082008-11-16 13:53:32 +00003612 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003613 p++;
3614 if (*p == '/') {
3615 /* format found */
3616 p++;
3617 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003618 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003619 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003620 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003621 count = count * 10 + (*p - '0');
3622 p++;
3623 }
3624 }
3625 size = -1;
3626 format = -1;
3627 for(;;) {
3628 switch(*p) {
3629 case 'o':
3630 case 'd':
3631 case 'u':
3632 case 'x':
3633 case 'i':
3634 case 'c':
3635 format = *p++;
3636 break;
3637 case 'b':
3638 size = 1;
3639 p++;
3640 break;
3641 case 'h':
3642 size = 2;
3643 p++;
3644 break;
3645 case 'w':
3646 size = 4;
3647 p++;
3648 break;
3649 case 'g':
3650 case 'L':
3651 size = 8;
3652 p++;
3653 break;
3654 default:
3655 goto next;
3656 }
3657 }
3658 next:
blueswir1cd390082008-11-16 13:53:32 +00003659 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003660 monitor_printf(mon, "invalid char in format: '%c'\n",
3661 *p);
bellard9307c4c2004-04-04 12:57:25 +00003662 goto fail;
3663 }
bellard9307c4c2004-04-04 12:57:25 +00003664 if (format < 0)
3665 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003666 if (format != 'i') {
3667 /* for 'i', not specifying a size gives -1 as size */
3668 if (size < 0)
3669 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003670 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003671 }
bellard9307c4c2004-04-04 12:57:25 +00003672 default_fmt_format = format;
3673 } else {
3674 count = 1;
3675 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003676 if (format != 'i') {
3677 size = default_fmt_size;
3678 } else {
3679 size = -1;
3680 }
bellard9307c4c2004-04-04 12:57:25 +00003681 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003682 qdict_put(qdict, "count", qint_from_int(count));
3683 qdict_put(qdict, "format", qint_from_int(format));
3684 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003685 }
3686 break;
3687 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003688 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003689 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003690 {
blueswir1c2efc952007-09-25 17:28:42 +00003691 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003692
blueswir1cd390082008-11-16 13:53:32 +00003693 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003694 p++;
bellard34405572004-06-08 00:55:58 +00003695 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003696 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003697 if (*p == '\0') {
3698 typestr++;
3699 break;
3700 }
bellard34405572004-06-08 00:55:58 +00003701 } else {
3702 if (*p == '.') {
3703 p++;
blueswir1cd390082008-11-16 13:53:32 +00003704 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003705 p++;
bellard34405572004-06-08 00:55:58 +00003706 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003707 typestr++;
3708 break;
bellard34405572004-06-08 00:55:58 +00003709 }
3710 }
bellard13224a82006-07-14 22:03:35 +00003711 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003712 }
aliguori376253e2009-03-05 23:01:23 +00003713 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003714 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003715 /* Check if 'i' is greater than 32-bit */
3716 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3717 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3718 monitor_printf(mon, "integer is for 32-bit values\n");
3719 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003720 } else if (c == 'M') {
3721 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003722 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003723 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003724 }
3725 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003726 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003727 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003728 {
3729 double val;
3730
3731 while (qemu_isspace(*p))
3732 p++;
3733 if (*typestr == '?') {
3734 typestr++;
3735 if (*p == '\0') {
3736 break;
3737 }
3738 }
3739 if (get_double(mon, &val, &p) < 0) {
3740 goto fail;
3741 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003742 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003743 switch (*p) {
3744 case 'K': case 'k':
3745 val *= 1 << 10; p++; break;
3746 case 'M': case 'm':
3747 val *= 1 << 20; p++; break;
3748 case 'G': case 'g':
3749 val *= 1 << 30; p++; break;
3750 }
3751 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003752 if (c == 'T' && p[0] && p[1] == 's') {
3753 switch (*p) {
3754 case 'm':
3755 val /= 1e3; p += 2; break;
3756 case 'u':
3757 val /= 1e6; p += 2; break;
3758 case 'n':
3759 val /= 1e9; p += 2; break;
3760 }
3761 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003762 if (*p && !qemu_isspace(*p)) {
3763 monitor_printf(mon, "Unknown unit suffix\n");
3764 goto fail;
3765 }
3766 qdict_put(qdict, key, qfloat_from_double(val));
3767 }
3768 break;
bellard9307c4c2004-04-04 12:57:25 +00003769 case '-':
3770 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003771 const char *tmp = p;
3772 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003773 /* option */
ths3b46e622007-09-17 08:09:54 +00003774
bellard9307c4c2004-04-04 12:57:25 +00003775 c = *typestr++;
3776 if (c == '\0')
3777 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003778 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003779 p++;
3780 has_option = 0;
3781 if (*p == '-') {
3782 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003783 if(c != *p) {
3784 if(!is_valid_option(p, typestr)) {
3785
3786 monitor_printf(mon, "%s: unsupported option -%c\n",
3787 cmdname, *p);
3788 goto fail;
3789 } else {
3790 skip_key = 1;
3791 }
bellard9307c4c2004-04-04 12:57:25 +00003792 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003793 if(skip_key) {
3794 p = tmp;
3795 } else {
3796 p++;
3797 has_option = 1;
3798 }
bellard9307c4c2004-04-04 12:57:25 +00003799 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003800 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003801 }
3802 break;
3803 default:
3804 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003805 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003806 goto fail;
3807 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003808 qemu_free(key);
3809 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003810 }
3811 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003812 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003813 p++;
3814 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003815 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3816 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003817 goto fail;
3818 }
3819
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003820 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003821
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003822fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003823 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003824 return NULL;
3825}
3826
Luiz Capitulino8204a912009-11-18 23:05:31 -02003827static void monitor_print_error(Monitor *mon)
3828{
3829 qerror_print(mon->error);
3830 QDECREF(mon->error);
3831 mon->error = NULL;
3832}
3833
Adam Litke940cc302010-01-25 12:18:44 -06003834static int is_async_return(const QObject *data)
3835{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003836 if (data && qobject_type(data) == QTYPE_QDICT) {
3837 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3838 }
3839
3840 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003841}
3842
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003843static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3844 const QDict *params)
3845{
3846 QObject *data = NULL;
3847
Luiz Capitulino261394d2010-02-10 23:50:02 -02003848 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003849
Adam Litke940cc302010-01-25 12:18:44 -06003850 if (is_async_return(data)) {
3851 /*
3852 * Asynchronous commands have no initial return data but they can
3853 * generate errors. Data is returned via the async completion handler.
3854 */
3855 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3856 monitor_protocol_emitter(mon, NULL);
3857 }
3858 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003859 /* Monitor Protocol */
3860 monitor_protocol_emitter(mon, data);
3861 } else {
3862 /* User Protocol */
3863 if (data)
3864 cmd->user_print(mon, data);
3865 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003866
3867 qobject_decref(data);
3868}
3869
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003870static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003871{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003872 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003873 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003874
3875 qdict = qdict_new();
3876
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003877 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003878 if (!cmd)
3879 goto out;
3880
3881 qemu_errors_to_mon(mon);
3882
Adam Litke940cc302010-01-25 12:18:44 -06003883 if (monitor_handler_is_async(cmd)) {
3884 user_async_cmd_handler(mon, cmd, qdict);
3885 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003886 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003887 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003888 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003889 }
3890
Luiz Capitulino8204a912009-11-18 23:05:31 -02003891 if (monitor_has_error(mon))
3892 monitor_print_error(mon);
3893
3894 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003895
3896out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003897 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003898}
3899
bellard81d09122004-07-14 17:21:37 +00003900static void cmd_completion(const char *name, const char *list)
3901{
3902 const char *p, *pstart;
3903 char cmd[128];
3904 int len;
3905
3906 p = list;
3907 for(;;) {
3908 pstart = p;
3909 p = strchr(p, '|');
3910 if (!p)
3911 p = pstart + strlen(pstart);
3912 len = p - pstart;
3913 if (len > sizeof(cmd) - 2)
3914 len = sizeof(cmd) - 2;
3915 memcpy(cmd, pstart, len);
3916 cmd[len] = '\0';
3917 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003918 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003919 }
3920 if (*p == '\0')
3921 break;
3922 p++;
3923 }
3924}
3925
3926static void file_completion(const char *input)
3927{
3928 DIR *ffs;
3929 struct dirent *d;
3930 char path[1024];
3931 char file[1024], file_prefix[1024];
3932 int input_path_len;
3933 const char *p;
3934
ths5fafdf22007-09-16 21:08:06 +00003935 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003936 if (!p) {
3937 input_path_len = 0;
3938 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003939 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003940 } else {
3941 input_path_len = p - input + 1;
3942 memcpy(path, input, input_path_len);
3943 if (input_path_len > sizeof(path) - 1)
3944 input_path_len = sizeof(path) - 1;
3945 path[input_path_len] = '\0';
3946 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3947 }
3948#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003949 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3950 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003951#endif
3952 ffs = opendir(path);
3953 if (!ffs)
3954 return;
3955 for(;;) {
3956 struct stat sb;
3957 d = readdir(ffs);
3958 if (!d)
3959 break;
3960 if (strstart(d->d_name, file_prefix, NULL)) {
3961 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003962 if (input_path_len < sizeof(file))
3963 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3964 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003965 /* stat the file to find out if it's a directory.
3966 * In that case add a slash to speed up typing long paths
3967 */
3968 stat(file, &sb);
3969 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003970 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003971 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003972 }
3973 }
3974 closedir(ffs);
3975}
3976
aliguori51de9762009-03-05 23:00:43 +00003977static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003978{
aliguori51de9762009-03-05 23:00:43 +00003979 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003980 const char *input = opaque;
3981
3982 if (input[0] == '\0' ||
3983 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003984 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003985 }
3986}
3987
3988/* NOTE: this parser is an approximate form of the real command parser */
3989static void parse_cmdline(const char *cmdline,
3990 int *pnb_args, char **args)
3991{
3992 const char *p;
3993 int nb_args, ret;
3994 char buf[1024];
3995
3996 p = cmdline;
3997 nb_args = 0;
3998 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003999 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004000 p++;
4001 if (*p == '\0')
4002 break;
4003 if (nb_args >= MAX_ARGS)
4004 break;
4005 ret = get_str(buf, sizeof(buf), &p);
4006 args[nb_args] = qemu_strdup(buf);
4007 nb_args++;
4008 if (ret < 0)
4009 break;
4010 }
4011 *pnb_args = nb_args;
4012}
4013
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004014static const char *next_arg_type(const char *typestr)
4015{
4016 const char *p = strchr(typestr, ':');
4017 return (p != NULL ? ++p : typestr);
4018}
4019
aliguori4c36ba32009-03-05 23:01:37 +00004020static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004021{
4022 const char *cmdname;
4023 char *args[MAX_ARGS];
4024 int nb_args, i, len;
4025 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004026 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004027 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004028
4029 parse_cmdline(cmdline, &nb_args, args);
4030#ifdef DEBUG_COMPLETION
4031 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004032 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004033 }
4034#endif
4035
4036 /* if the line ends with a space, it means we want to complete the
4037 next arg */
4038 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004039 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004040 if (nb_args >= MAX_ARGS)
4041 return;
4042 args[nb_args++] = qemu_strdup("");
4043 }
4044 if (nb_args <= 1) {
4045 /* command completion */
4046 if (nb_args == 0)
4047 cmdname = "";
4048 else
4049 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004050 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004051 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004052 cmd_completion(cmdname, cmd->name);
4053 }
4054 } else {
4055 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004056 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004057 if (compare_cmd(args[0], cmd->name))
4058 goto found;
4059 }
4060 return;
4061 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004062 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004063 for(i = 0; i < nb_args - 2; i++) {
4064 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004065 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004066 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004067 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004068 }
4069 }
4070 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004071 if (*ptype == '-' && ptype[1] != '\0') {
4072 ptype += 2;
4073 }
bellard81d09122004-07-14 17:21:37 +00004074 switch(*ptype) {
4075 case 'F':
4076 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004077 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004078 file_completion(str);
4079 break;
4080 case 'B':
4081 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004082 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004083 bdrv_iterate(block_completion_it, (void *)str);
4084 break;
bellard7fe48482004-10-09 18:08:01 +00004085 case 's':
4086 /* XXX: more generic ? */
4087 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004088 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004089 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4090 cmd_completion(str, cmd->name);
4091 }
bellard64866c32006-05-07 18:03:31 +00004092 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004093 char *sep = strrchr(str, '-');
4094 if (sep)
4095 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004096 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004097 for(key = key_defs; key->name != NULL; key++) {
4098 cmd_completion(str, key->name);
4099 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004100 } else if (!strcmp(cmd->name, "help|?")) {
4101 readline_set_completion_index(cur_mon->rs, strlen(str));
4102 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4103 cmd_completion(str, cmd->name);
4104 }
bellard7fe48482004-10-09 18:08:01 +00004105 }
4106 break;
bellard81d09122004-07-14 17:21:37 +00004107 default:
4108 break;
4109 }
4110 }
4111 for(i = 0; i < nb_args; i++)
4112 qemu_free(args[i]);
4113}
4114
aliguori731b0362009-03-05 23:01:42 +00004115static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004116{
aliguori731b0362009-03-05 23:01:42 +00004117 Monitor *mon = opaque;
4118
Jan Kiszkac62313b2009-12-04 14:05:29 +01004119 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004120}
4121
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004122typedef struct CmdArgs {
4123 QString *name;
4124 int type;
4125 int flag;
4126 int optional;
4127} CmdArgs;
4128
4129static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4130{
4131 if (!cmd_args->optional) {
4132 qemu_error_new(QERR_MISSING_PARAMETER, name);
4133 return -1;
4134 }
4135
4136 if (cmd_args->type == '-') {
4137 /* handlers expect a value, they need to be changed */
4138 qdict_put(args, name, qint_from_int(0));
4139 }
4140
4141 return 0;
4142}
4143
4144static int check_arg(const CmdArgs *cmd_args, QDict *args)
4145{
4146 QObject *value;
4147 const char *name;
4148
4149 name = qstring_get_str(cmd_args->name);
4150
4151 if (!args) {
4152 return check_opt(cmd_args, name, args);
4153 }
4154
4155 value = qdict_get(args, name);
4156 if (!value) {
4157 return check_opt(cmd_args, name, args);
4158 }
4159
4160 switch (cmd_args->type) {
4161 case 'F':
4162 case 'B':
4163 case 's':
4164 if (qobject_type(value) != QTYPE_QSTRING) {
4165 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4166 return -1;
4167 }
4168 break;
4169 case '/': {
4170 int i;
4171 const char *keys[] = { "count", "format", "size", NULL };
4172
4173 for (i = 0; keys[i]; i++) {
4174 QObject *obj = qdict_get(args, keys[i]);
4175 if (!obj) {
4176 qemu_error_new(QERR_MISSING_PARAMETER, name);
4177 return -1;
4178 }
4179 if (qobject_type(obj) != QTYPE_QINT) {
4180 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4181 return -1;
4182 }
4183 }
4184 break;
4185 }
4186 case 'i':
4187 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004188 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004189 if (qobject_type(value) != QTYPE_QINT) {
4190 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4191 return -1;
4192 }
4193 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004194 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004195 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004196 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4197 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4198 return -1;
4199 }
4200 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004201 case '-':
4202 if (qobject_type(value) != QTYPE_QINT &&
4203 qobject_type(value) != QTYPE_QBOOL) {
4204 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4205 return -1;
4206 }
4207 if (qobject_type(value) == QTYPE_QBOOL) {
4208 /* handlers expect a QInt, they need to be changed */
4209 qdict_put(args, name,
4210 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4211 }
4212 break;
4213 default:
4214 /* impossible */
4215 abort();
4216 }
4217
4218 return 0;
4219}
4220
4221static void cmd_args_init(CmdArgs *cmd_args)
4222{
4223 cmd_args->name = qstring_new();
4224 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4225}
4226
4227/*
4228 * This is not trivial, we have to parse Monitor command's argument
4229 * type syntax to be able to check the arguments provided by clients.
4230 *
4231 * In the near future we will be using an array for that and will be
4232 * able to drop all this parsing...
4233 */
4234static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4235{
4236 int err;
4237 const char *p;
4238 CmdArgs cmd_args;
4239
Blue Swirldd5121b2009-12-04 20:52:02 +00004240 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004241 return (qdict_size(args) == 0 ? 0 : -1);
4242 }
4243
4244 err = 0;
4245 cmd_args_init(&cmd_args);
4246
4247 for (p = cmd->args_type;; p++) {
4248 if (*p == ':') {
4249 cmd_args.type = *++p;
4250 p++;
4251 if (cmd_args.type == '-') {
4252 cmd_args.flag = *p++;
4253 cmd_args.optional = 1;
4254 } else if (*p == '?') {
4255 cmd_args.optional = 1;
4256 p++;
4257 }
4258
4259 assert(*p == ',' || *p == '\0');
4260 err = check_arg(&cmd_args, args);
4261
4262 QDECREF(cmd_args.name);
4263 cmd_args_init(&cmd_args);
4264
4265 if (err < 0) {
4266 break;
4267 }
4268 } else {
4269 qstring_append_chr(cmd_args.name, *p);
4270 }
4271
4272 if (*p == '\0') {
4273 break;
4274 }
4275 }
4276
4277 QDECREF(cmd_args.name);
4278 return err;
4279}
4280
Luiz Capitulino09069b12010-02-04 18:10:06 -02004281static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4282{
4283 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4284 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4285}
4286
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004287static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4288{
4289 int err;
4290 QObject *obj;
4291 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004292 const mon_cmd_t *cmd;
4293 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004294 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004295
4296 args = NULL;
4297 qemu_errors_to_mon(mon);
4298
4299 obj = json_parser_parse(tokens, NULL);
4300 if (!obj) {
4301 // FIXME: should be triggered in json_parser_parse()
4302 qemu_error_new(QERR_JSON_PARSING);
4303 goto err_out;
4304 } else if (qobject_type(obj) != QTYPE_QDICT) {
4305 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4306 qobject_decref(obj);
4307 goto err_out;
4308 }
4309
4310 input = qobject_to_qdict(obj);
4311
4312 mon->mc->id = qdict_get(input, "id");
4313 qobject_incref(mon->mc->id);
4314
4315 obj = qdict_get(input, "execute");
4316 if (!obj) {
4317 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4318 goto err_input;
4319 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4320 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4321 goto err_input;
4322 }
4323
4324 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004325
Luiz Capitulino09069b12010-02-04 18:10:06 -02004326 if (invalid_qmp_mode(mon, cmd_name)) {
4327 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4328 goto err_input;
4329 }
4330
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004331 /*
4332 * XXX: We need this special case until we get info handlers
4333 * converted into 'query-' commands
4334 */
4335 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004336 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4337 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004338 } else if (strstart(cmd_name, "query-", &info_item)) {
4339 cmd = monitor_find_command("info");
4340 qdict_put_obj(input, "arguments",
4341 qobject_from_jsonf("{ 'item': %s }", info_item));
4342 } else {
4343 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004344 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004345 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4346 goto err_input;
4347 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004348 }
4349
4350 obj = qdict_get(input, "arguments");
4351 if (!obj) {
4352 args = qdict_new();
4353 } else {
4354 args = qobject_to_qdict(obj);
4355 QINCREF(args);
4356 }
4357
4358 QDECREF(input);
4359
4360 err = monitor_check_qmp_args(cmd, args);
4361 if (err < 0) {
4362 goto err_out;
4363 }
4364
Adam Litke940cc302010-01-25 12:18:44 -06004365 if (monitor_handler_is_async(cmd)) {
4366 qmp_async_cmd_handler(mon, cmd, args);
4367 } else {
4368 monitor_call_handler(mon, cmd, args);
4369 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004370 goto out;
4371
4372err_input:
4373 QDECREF(input);
4374err_out:
4375 monitor_protocol_emitter(mon, NULL);
4376out:
4377 QDECREF(args);
4378 qemu_errors_to_previous();
4379}
4380
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004381/**
4382 * monitor_control_read(): Read and handle QMP input
4383 */
4384static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4385{
4386 Monitor *old_mon = cur_mon;
4387
4388 cur_mon = opaque;
4389
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004390 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004391
4392 cur_mon = old_mon;
4393}
4394
aliguori731b0362009-03-05 23:01:42 +00004395static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004396{
aliguori731b0362009-03-05 23:01:42 +00004397 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004398 int i;
aliguori376253e2009-03-05 23:01:23 +00004399
aliguori731b0362009-03-05 23:01:42 +00004400 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004401
aliguoricde76ee2009-03-05 23:01:51 +00004402 if (cur_mon->rs) {
4403 for (i = 0; i < size; i++)
4404 readline_handle_byte(cur_mon->rs, buf[i]);
4405 } else {
4406 if (size == 0 || buf[size - 1] != 0)
4407 monitor_printf(cur_mon, "corrupted command\n");
4408 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004409 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004410 }
aliguori731b0362009-03-05 23:01:42 +00004411
4412 cur_mon = old_mon;
4413}
aliguorid8f44602008-10-06 13:52:44 +00004414
aliguori376253e2009-03-05 23:01:23 +00004415static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004416{
aliguori731b0362009-03-05 23:01:42 +00004417 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004418 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004419 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004420}
4421
aliguoricde76ee2009-03-05 23:01:51 +00004422int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004423{
aliguoricde76ee2009-03-05 23:01:51 +00004424 if (!mon->rs)
4425 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004426 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004427 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004428}
4429
aliguori376253e2009-03-05 23:01:23 +00004430void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004431{
aliguoricde76ee2009-03-05 23:01:51 +00004432 if (!mon->rs)
4433 return;
aliguori731b0362009-03-05 23:01:42 +00004434 if (--mon->suspend_cnt == 0)
4435 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004436}
4437
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004438static QObject *get_qmp_greeting(void)
4439{
4440 QObject *ver;
4441
4442 do_info_version(NULL, &ver);
4443 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4444}
4445
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004446/**
4447 * monitor_control_event(): Print QMP gretting
4448 */
4449static void monitor_control_event(void *opaque, int event)
4450{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004451 QObject *data;
4452 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004453
Luiz Capitulino47116d12010-02-08 17:01:30 -02004454 switch (event) {
4455 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004456 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004457 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004458 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004459 monitor_json_emitter(mon, data);
4460 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004461 break;
4462 case CHR_EVENT_CLOSED:
4463 json_message_parser_destroy(&mon->mc->parser);
4464 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004465 }
4466}
4467
aliguori731b0362009-03-05 23:01:42 +00004468static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004469{
aliguori376253e2009-03-05 23:01:23 +00004470 Monitor *mon = opaque;
4471
aliguori2724b182009-03-05 23:01:47 +00004472 switch (event) {
4473 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004474 mon->mux_out = 0;
4475 if (mon->reset_seen) {
4476 readline_restart(mon->rs);
4477 monitor_resume(mon);
4478 monitor_flush(mon);
4479 } else {
4480 mon->suspend_cnt = 0;
4481 }
aliguori2724b182009-03-05 23:01:47 +00004482 break;
ths86e94de2007-01-05 22:01:59 +00004483
aliguori2724b182009-03-05 23:01:47 +00004484 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004485 if (mon->reset_seen) {
4486 if (mon->suspend_cnt == 0) {
4487 monitor_printf(mon, "\n");
4488 }
4489 monitor_flush(mon);
4490 monitor_suspend(mon);
4491 } else {
4492 mon->suspend_cnt++;
4493 }
4494 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004495 break;
4496
Amit Shahb6b8df52009-10-07 18:31:16 +05304497 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004498 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4499 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004500 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004501 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004502 }
4503 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004504 break;
4505 }
ths86e94de2007-01-05 22:01:59 +00004506}
4507
aliguori76655d62009-03-06 20:27:37 +00004508
4509/*
4510 * Local variables:
4511 * c-indent-level: 4
4512 * c-basic-offset: 4
4513 * tab-width: 8
4514 * End:
4515 */
4516
aliguori731b0362009-03-05 23:01:42 +00004517void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004518{
aliguori731b0362009-03-05 23:01:42 +00004519 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004520 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004521
4522 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004523 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004524 is_first_init = 0;
4525 }
aliguori87127162009-03-05 23:01:29 +00004526
4527 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004528
aliguori87127162009-03-05 23:01:29 +00004529 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004530 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004531 if (flags & MONITOR_USE_READLINE) {
4532 mon->rs = readline_init(mon, monitor_find_completion);
4533 monitor_read_command(mon, 0);
4534 }
aliguori87127162009-03-05 23:01:29 +00004535
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004536 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004537 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004538 /* Control mode requires special handlers */
4539 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4540 monitor_control_event, mon);
4541 } else {
4542 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4543 monitor_event, mon);
4544 }
aliguori87127162009-03-05 23:01:29 +00004545
Blue Swirl72cf2d42009-09-12 07:36:22 +00004546 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004547 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004548 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004549}
4550
aliguori376253e2009-03-05 23:01:23 +00004551static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004552{
aliguoribb5fc202009-03-05 23:01:15 +00004553 BlockDriverState *bs = opaque;
4554 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004555
aliguoribb5fc202009-03-05 23:01:15 +00004556 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004557 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004558 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004559 }
aliguori731b0362009-03-05 23:01:42 +00004560 if (mon->password_completion_cb)
4561 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004562
aliguori731b0362009-03-05 23:01:42 +00004563 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004564}
aliguoric0f4ce72009-03-05 23:01:01 +00004565
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004566int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4567 BlockDriverCompletionFunc *completion_cb,
4568 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004569{
aliguoricde76ee2009-03-05 23:01:51 +00004570 int err;
4571
aliguoribb5fc202009-03-05 23:01:15 +00004572 if (!bdrv_key_required(bs)) {
4573 if (completion_cb)
4574 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004575 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004576 }
aliguoric0f4ce72009-03-05 23:01:01 +00004577
Luiz Capitulino94171e12009-12-07 21:37:00 +01004578 if (monitor_ctrl_mode(mon)) {
4579 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004580 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004581 }
4582
aliguori376253e2009-03-05 23:01:23 +00004583 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4584 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004585
aliguori731b0362009-03-05 23:01:42 +00004586 mon->password_completion_cb = completion_cb;
4587 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004588
aliguoricde76ee2009-03-05 23:01:51 +00004589 err = monitor_read_password(mon, bdrv_password_cb, bs);
4590
4591 if (err && completion_cb)
4592 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004593
4594 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004595}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004596
4597typedef struct QemuErrorSink QemuErrorSink;
4598struct QemuErrorSink {
4599 enum {
4600 ERR_SINK_FILE,
4601 ERR_SINK_MONITOR,
4602 } dest;
4603 union {
4604 FILE *fp;
4605 Monitor *mon;
4606 };
4607 QemuErrorSink *previous;
4608};
4609
Blue Swirl528e93a2009-08-31 15:14:40 +00004610static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004611
4612void qemu_errors_to_file(FILE *fp)
4613{
4614 QemuErrorSink *sink;
4615
4616 sink = qemu_mallocz(sizeof(*sink));
4617 sink->dest = ERR_SINK_FILE;
4618 sink->fp = fp;
4619 sink->previous = qemu_error_sink;
4620 qemu_error_sink = sink;
4621}
4622
4623void qemu_errors_to_mon(Monitor *mon)
4624{
4625 QemuErrorSink *sink;
4626
4627 sink = qemu_mallocz(sizeof(*sink));
4628 sink->dest = ERR_SINK_MONITOR;
4629 sink->mon = mon;
4630 sink->previous = qemu_error_sink;
4631 qemu_error_sink = sink;
4632}
4633
4634void qemu_errors_to_previous(void)
4635{
4636 QemuErrorSink *sink;
4637
4638 assert(qemu_error_sink != NULL);
4639 sink = qemu_error_sink;
4640 qemu_error_sink = sink->previous;
4641 qemu_free(sink);
4642}
4643
4644void qemu_error(const char *fmt, ...)
4645{
4646 va_list args;
4647
4648 assert(qemu_error_sink != NULL);
4649 switch (qemu_error_sink->dest) {
4650 case ERR_SINK_FILE:
4651 va_start(args, fmt);
4652 vfprintf(qemu_error_sink->fp, fmt, args);
4653 va_end(args);
4654 break;
4655 case ERR_SINK_MONITOR:
4656 va_start(args, fmt);
4657 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4658 va_end(args);
4659 break;
4660 }
4661}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004662
4663void qemu_error_internal(const char *file, int linenr, const char *func,
4664 const char *fmt, ...)
4665{
4666 va_list va;
4667 QError *qerror;
4668
4669 assert(qemu_error_sink != NULL);
4670
4671 va_start(va, fmt);
4672 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4673 va_end(va);
4674
4675 switch (qemu_error_sink->dest) {
4676 case ERR_SINK_FILE:
4677 qerror_print(qerror);
4678 QDECREF(qerror);
4679 break;
4680 case ERR_SINK_MONITOR:
Luiz Capitulino27a749f2010-02-08 17:01:29 -02004681 /* report only the first error */
4682 if (!qemu_error_sink->mon->error) {
4683 qemu_error_sink->mon->error = qerror;
4684 } else {
4685 /* XXX: warn the programmer */
4686 QDECREF(qerror);
4687 }
Luiz Capitulino8204a912009-11-18 23:05:31 -02004688 break;
4689 }
4690}