blob: 5366c36525fd1ea1c80c64c21cb47186c3b144c8 [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"
Markus Armbruster666daa62010-06-02 18:48:27 +020041#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000042#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 Capitulino5fa737a2009-11-26 22:59:01 -020052#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030053#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020054#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020055#include "json-streamer.h"
56#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000057#include "osdep.h"
Paolo Bonzinia88790a2010-06-29 09:58:51 +020058#include "exec-all.h"
ths6a5bd302007-12-03 17:05:38 +000059
bellard9dc39cb2004-03-14 21:38:27 +000060//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000061//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000062
bellard9307c4c2004-04-04 12:57:25 +000063/*
64 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000065 *
bellard9307c4c2004-04-04 12:57:25 +000066 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000067 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000068 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010069 * 'O' option string of the form NAME=VALUE,...
70 * parsed according to QemuOptsList given by its name
71 * Example: 'device:O' uses qemu_device_opts.
72 * Restriction: only lists with empty desc are supported
73 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000074 * 'i' 32 bit integer
75 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010076 * 'M' just like 'l', except in user mode the value is
77 * multiplied by 2^20 (think Mebibyte)
Markus Armbrusteree9545d2010-03-26 09:07:08 +010078 * 'f' double
Markus Armbruster3350a4d2010-01-25 14:23:03 +010079 * user mode accepts an optional G, g, M, m, K, k suffix,
80 * which multiplies the value by 2^30 for suffixes G and
81 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010082 * 'T' double
83 * user mode accepts an optional ms, us, ns suffix,
84 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000085 * '/' optional gdb-like print format (like "/10x")
86 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030087 * '?' optional type (for all types, except '/')
88 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010089 * 'b' boolean
90 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -030091 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000092 *
93 */
94
Adam Litke940cc302010-01-25 12:18:44 -060095typedef struct MonitorCompletionData MonitorCompletionData;
96struct MonitorCompletionData {
97 Monitor *mon;
98 void (*user_print)(Monitor *mon, const QObject *data);
99};
100
Anthony Liguoric227f092009-10-01 16:12:16 -0500101typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000102 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000103 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000104 const char *params;
105 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300106 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300107 union {
108 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300109 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600110 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300111 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200112 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600113 int (*cmd_async)(Monitor *mon, const QDict *params,
114 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300115 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200116 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500117} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000118
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100119/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500120typedef struct mon_fd_t mon_fd_t;
121struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100122 char *name;
123 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500124 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100125};
126
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200127typedef struct MonitorControl {
128 QObject *id;
129 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200130 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200131} MonitorControl;
132
aliguori87127162009-03-05 23:01:29 +0000133struct Monitor {
134 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200135 int mux_out;
136 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000137 int flags;
138 int suspend_cnt;
139 uint8_t outbuf[1024];
140 int outbuf_index;
141 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200142 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000143 CPUState *mon_cpu;
144 BlockDriverCompletionFunc *password_completion_cb;
145 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200146#ifdef CONFIG_DEBUG_MONITOR
147 int print_calls_nr;
148#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200149 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500150 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000151 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000152};
153
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200154#ifdef CONFIG_DEBUG_MONITOR
155#define MON_DEBUG(fmt, ...) do { \
156 fprintf(stderr, "Monitor: "); \
157 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200158
159static inline void mon_print_count_inc(Monitor *mon)
160{
161 mon->print_calls_nr++;
162}
163
164static inline void mon_print_count_init(Monitor *mon)
165{
166 mon->print_calls_nr = 0;
167}
168
169static inline int mon_print_count_get(const Monitor *mon)
170{
171 return mon->print_calls_nr;
172}
173
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200174#else /* !CONFIG_DEBUG_MONITOR */
175#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200176static inline void mon_print_count_inc(Monitor *mon) { }
177static inline void mon_print_count_init(Monitor *mon) { }
178static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200179#endif /* CONFIG_DEBUG_MONITOR */
180
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300181/* QMP checker flags */
182#define QMP_ACCEPT_UNKNOWNS 1
183
Blue Swirl72cf2d42009-09-12 07:36:22 +0000184static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000185
Anthony Liguoric227f092009-10-01 16:12:16 -0500186static const mon_cmd_t mon_cmds[];
187static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000188
Markus Armbruster8631b602010-02-18 11:41:55 +0100189Monitor *cur_mon;
190Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000191
aliguori731b0362009-03-05 23:01:42 +0000192static void monitor_command_cb(Monitor *mon, const char *cmdline,
193 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000194
Luiz Capitulino09069b12010-02-04 18:10:06 -0200195static inline int qmp_cmd_mode(const Monitor *mon)
196{
197 return (mon->mc ? mon->mc->command_mode : 0);
198}
199
Luiz Capitulino418173c2009-11-26 22:58:51 -0200200/* Return true if in control mode, false otherwise */
201static inline int monitor_ctrl_mode(const Monitor *mon)
202{
203 return (mon->flags & MONITOR_USE_CONTROL);
204}
205
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100206/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
207int monitor_cur_is_qmp(void)
208{
209 return cur_mon && monitor_ctrl_mode(cur_mon);
210}
211
aliguori731b0362009-03-05 23:01:42 +0000212static void monitor_read_command(Monitor *mon, int show_prompt)
213{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200214 if (!mon->rs)
215 return;
216
aliguori731b0362009-03-05 23:01:42 +0000217 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
218 if (show_prompt)
219 readline_show_prompt(mon->rs);
220}
bellard6a00d602005-11-21 23:25:50 +0000221
aliguoricde76ee2009-03-05 23:01:51 +0000222static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
223 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000224{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100225 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100226 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100227 return -EINVAL;
228 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000229 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
230 /* prompt is printed on return from the command handler */
231 return 0;
232 } else {
233 monitor_printf(mon, "terminal does not support password prompting\n");
234 return -ENOTTY;
235 }
aliguoribb5fc202009-03-05 23:01:15 +0000236}
237
aliguori376253e2009-03-05 23:01:23 +0000238void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000239{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200240 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000241 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
242 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000243 }
244}
245
246/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000247static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000248{
ths60fe76f2007-12-16 03:02:09 +0000249 char c;
aliguori731b0362009-03-05 23:01:42 +0000250
bellard7e2515e2004-08-01 21:52:19 +0000251 for(;;) {
252 c = *str++;
253 if (c == '\0')
254 break;
bellard7ba12602006-07-14 20:26:42 +0000255 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000256 mon->outbuf[mon->outbuf_index++] = '\r';
257 mon->outbuf[mon->outbuf_index++] = c;
258 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
259 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000260 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000261 }
262}
263
aliguori376253e2009-03-05 23:01:23 +0000264void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000265{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200266 char buf[4096];
267
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200268 if (!mon)
269 return;
270
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200271 mon_print_count_inc(mon);
272
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200273 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200274 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200275 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200276
277 vsnprintf(buf, sizeof(buf), fmt, ap);
278 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000279}
280
aliguori376253e2009-03-05 23:01:23 +0000281void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000282{
283 va_list ap;
284 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000285 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000286 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000287}
288
aliguori376253e2009-03-05 23:01:23 +0000289void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000290{
291 int i;
292
293 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000294 switch (filename[i]) {
295 case ' ':
296 case '"':
297 case '\\':
298 monitor_printf(mon, "\\%c", filename[i]);
299 break;
300 case '\t':
301 monitor_printf(mon, "\\t");
302 break;
303 case '\r':
304 monitor_printf(mon, "\\r");
305 break;
306 case '\n':
307 monitor_printf(mon, "\\n");
308 break;
309 default:
310 monitor_printf(mon, "%c", filename[i]);
311 break;
312 }
thsfef30742006-12-22 14:11:32 +0000313 }
314}
315
bellard7fe48482004-10-09 18:08:01 +0000316static int monitor_fprintf(FILE *stream, const char *fmt, ...)
317{
318 va_list ap;
319 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000320 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000321 va_end(ap);
322 return 0;
323}
324
Luiz Capitulino13c74252009-10-07 13:41:55 -0300325static void monitor_user_noop(Monitor *mon, const QObject *data) { }
326
Luiz Capitulino13917be2009-10-07 13:41:54 -0300327static inline int monitor_handler_ported(const mon_cmd_t *cmd)
328{
329 return cmd->user_print != NULL;
330}
331
Adam Litke940cc302010-01-25 12:18:44 -0600332static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
333{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200334 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600335}
336
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200337static inline bool monitor_cmd_user_only(const mon_cmd_t *cmd)
338{
339 return (cmd->flags & MONITOR_CMD_USER_ONLY);
bellard9dc39cb2004-03-14 21:38:27 +0000340}
341
Luiz Capitulino8204a912009-11-18 23:05:31 -0200342static inline int monitor_has_error(const Monitor *mon)
343{
344 return mon->error != NULL;
345}
346
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200347static void monitor_json_emitter(Monitor *mon, const QObject *data)
348{
349 QString *json;
350
351 json = qobject_to_json(data);
352 assert(json != NULL);
353
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 qstring_append_chr(json, '\n');
355 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200356
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200357 QDECREF(json);
358}
359
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200360static void monitor_protocol_emitter(Monitor *mon, QObject *data)
361{
362 QDict *qmp;
363
364 qmp = qdict_new();
365
366 if (!monitor_has_error(mon)) {
367 /* success response */
368 if (data) {
369 qobject_incref(data);
370 qdict_put_obj(qmp, "return", data);
371 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200372 /* return an empty QDict by default */
373 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200374 }
375 } else {
376 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100377 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378 qdict_put(qmp, "error", mon->error->error);
379 QINCREF(mon->error->error);
380 QDECREF(mon->error);
381 mon->error = NULL;
382 }
383
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200384 if (mon->mc->id) {
385 qdict_put_obj(qmp, "id", mon->mc->id);
386 mon->mc->id = NULL;
387 }
388
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200389 monitor_json_emitter(mon, QOBJECT(qmp));
390 QDECREF(qmp);
391}
392
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200393static void timestamp_put(QDict *qdict)
394{
395 int err;
396 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000397 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200398
Blue Swirld08d6f02009-12-04 18:06:39 +0000399 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200400 if (err < 0)
401 return;
402
403 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
404 "'microseconds': %" PRId64 " }",
405 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200406 qdict_put_obj(qdict, "timestamp", obj);
407}
408
409/**
410 * monitor_protocol_event(): Generate a Monitor event
411 *
412 * Event-specific data can be emitted through the (optional) 'data' parameter.
413 */
414void monitor_protocol_event(MonitorEvent event, QObject *data)
415{
416 QDict *qmp;
417 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600418 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200419
Blue Swirl242cd002009-12-04 18:05:45 +0000420 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200421
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200422 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000423 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200424 event_name = "SHUTDOWN";
425 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000426 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200427 event_name = "RESET";
428 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000429 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200430 event_name = "POWERDOWN";
431 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000432 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200433 event_name = "STOP";
434 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300435 case QEVENT_RESUME:
436 event_name = "RESUME";
437 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200438 case QEVENT_VNC_CONNECTED:
439 event_name = "VNC_CONNECTED";
440 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200441 case QEVENT_VNC_INITIALIZED:
442 event_name = "VNC_INITIALIZED";
443 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200444 case QEVENT_VNC_DISCONNECTED:
445 event_name = "VNC_DISCONNECTED";
446 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200447 case QEVENT_BLOCK_IO_ERROR:
448 event_name = "BLOCK_IO_ERROR";
449 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300450 case QEVENT_RTC_CHANGE:
451 event_name = "RTC_CHANGE";
452 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300453 case QEVENT_WATCHDOG:
454 event_name = "WATCHDOG";
455 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200456 default:
457 abort();
458 break;
459 }
460
461 qmp = qdict_new();
462 timestamp_put(qmp);
463 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200464 if (data) {
465 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200466 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200467 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200468
Adam Litkef039a562010-01-15 08:34:02 -0600469 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200470 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200471 monitor_json_emitter(mon, QOBJECT(qmp));
472 }
Adam Litkef039a562010-01-15 08:34:02 -0600473 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200474 QDECREF(qmp);
475}
476
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200477static int do_qmp_capabilities(Monitor *mon, const QDict *params,
478 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200479{
480 /* Will setup QMP capabilities in the future */
481 if (monitor_ctrl_mode(mon)) {
482 mon->mc->command_mode = 1;
483 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200484
485 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200486}
487
bellard9dc39cb2004-03-14 21:38:27 +0000488static int compare_cmd(const char *name, const char *list)
489{
490 const char *p, *pstart;
491 int len;
492 len = strlen(name);
493 p = list;
494 for(;;) {
495 pstart = p;
496 p = strchr(p, '|');
497 if (!p)
498 p = pstart + strlen(pstart);
499 if ((p - pstart) == len && !memcmp(pstart, name, len))
500 return 1;
501 if (*p == '\0')
502 break;
503 p++;
504 }
505 return 0;
506}
507
Anthony Liguoric227f092009-10-01 16:12:16 -0500508static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000509 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000510{
Anthony Liguoric227f092009-10-01 16:12:16 -0500511 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000512
513 for(cmd = cmds; cmd->name != NULL; cmd++) {
514 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000515 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
516 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000517 }
518}
519
aliguori376253e2009-03-05 23:01:23 +0000520static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000521{
522 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000523 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000524 } else {
aliguori376253e2009-03-05 23:01:23 +0000525 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000526 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000527 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000528 monitor_printf(mon, "Log items (comma separated):\n");
529 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000530 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000531 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000532 }
533 }
bellard9dc39cb2004-03-14 21:38:27 +0000534 }
535}
536
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300537static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300538{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300539 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300540}
541
Adam Litke940cc302010-01-25 12:18:44 -0600542static void user_monitor_complete(void *opaque, QObject *ret_data)
543{
544 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
545
546 if (ret_data) {
547 data->user_print(data->mon, ret_data);
548 }
549 monitor_resume(data->mon);
550 qemu_free(data);
551}
552
553static void qmp_monitor_complete(void *opaque, QObject *ret_data)
554{
555 monitor_protocol_emitter(opaque, ret_data);
556}
557
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300558static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
559 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600560{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300561 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600562}
563
564static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
565{
566 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
567}
568
569static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
570 const QDict *params)
571{
572 int ret;
573
574 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
575 cb_data->mon = mon;
576 cb_data->user_print = cmd->user_print;
577 monitor_suspend(mon);
578 ret = cmd->mhandler.cmd_async(mon, params,
579 user_monitor_complete, cb_data);
580 if (ret < 0) {
581 monitor_resume(mon);
582 qemu_free(cb_data);
583 }
584}
585
586static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
587{
588 int ret;
589
590 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
591 cb_data->mon = mon;
592 cb_data->user_print = cmd->user_print;
593 monitor_suspend(mon);
594 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
595 if (ret < 0) {
596 monitor_resume(mon);
597 qemu_free(cb_data);
598 }
599}
600
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200601static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000602{
Anthony Liguoric227f092009-10-01 16:12:16 -0500603 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300604 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000605
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200606 if (!item) {
607 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000608 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200609 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300610
611 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000612 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300613 break;
bellard9dc39cb2004-03-14 21:38:27 +0000614 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300615
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200616 if (cmd->name == NULL) {
617 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100618 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200619 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200620 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300621 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200622 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300623
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200624 if (monitor_ctrl_mode(mon) && monitor_cmd_user_only(cmd)) {
625 qerror_report(QERR_COMMAND_NOT_FOUND, item);
626 return -1;
627 }
628
Adam Litke940cc302010-01-25 12:18:44 -0600629 if (monitor_handler_is_async(cmd)) {
630 if (monitor_ctrl_mode(mon)) {
631 qmp_async_info_handler(mon, cmd);
632 } else {
633 user_async_info_handler(mon, cmd);
634 }
635 /*
636 * Indicate that this command is asynchronous and will not return any
637 * data (not even empty). Instead, the data will be returned via a
638 * completion callback.
639 */
640 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
641 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300642 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200643
644 if (!monitor_ctrl_mode(mon)) {
645 /*
646 * User Protocol function is called here, Monitor Protocol is
647 * handled by monitor_call_handler()
648 */
649 if (*ret_data)
650 cmd->user_print(mon, *ret_data);
651 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300652 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200653 if (monitor_ctrl_mode(mon)) {
654 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100655 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200656 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200657 } else {
658 cmd->mhandler.info(mon);
659 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300660 }
661
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200662 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300663
664help:
665 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200666 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000667}
668
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200669static void do_info_version_print(Monitor *mon, const QObject *data)
670{
671 QDict *qdict;
672
673 qdict = qobject_to_qdict(data);
674
675 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
676 qdict_get_str(qdict, "package"));
677}
678
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300679static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000680{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200681 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
682 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000683}
684
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200685static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000686{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200687 QDict *qdict;
688
689 qdict = qobject_to_qdict(data);
690 if (qdict_size(qdict) == 0) {
691 return;
692 }
693
694 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
695}
696
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200697static void do_info_name(Monitor *mon, QObject **ret_data)
698{
699 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
700 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000701}
702
Luiz Capitulino1a728672009-12-10 17:15:56 -0200703static QObject *get_cmd_dict(const char *name)
704{
705 const char *p;
706
707 /* Remove '|' from some commands */
708 p = strchr(name, '|');
709 if (p) {
710 p++;
711 } else {
712 p = name;
713 }
714
715 return qobject_from_jsonf("{ 'name': %s }", p);
716}
717
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200718static void do_info_commands(Monitor *mon, QObject **ret_data)
719{
720 QList *cmd_list;
721 const mon_cmd_t *cmd;
722
723 cmd_list = qlist_new();
724
725 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200726 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
727 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200728 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200729 }
730 }
731
732 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200733 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200734 char buf[128];
735 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200736 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200737 }
738 }
739
740 *ret_data = QOBJECT(cmd_list);
741}
742
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200743static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000744{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200745 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
746}
747
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200748static void do_info_uuid(Monitor *mon, QObject **ret_data)
749{
750 char uuid[64];
751
752 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000753 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
754 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
755 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
756 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200757 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000758}
759
bellard6a00d602005-11-21 23:25:50 +0000760/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000761static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000762{
763 CPUState *env;
764
765 for(env = first_cpu; env != NULL; env = env->next_cpu) {
766 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000767 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000768 return 0;
769 }
770 }
771 return -1;
772}
773
pbrook9596ebb2007-11-18 01:44:38 +0000774static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000775{
aliguori731b0362009-03-05 23:01:42 +0000776 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000777 mon_set_cpu(0);
778 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300779 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000780 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000781}
782
aliguori376253e2009-03-05 23:01:23 +0000783static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000784{
bellard6a00d602005-11-21 23:25:50 +0000785 CPUState *env;
786 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000787#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000788 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000789 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000790#else
aliguori376253e2009-03-05 23:01:23 +0000791 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000792 0);
bellard9307c4c2004-04-04 12:57:25 +0000793#endif
794}
795
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300796static void print_cpu_iter(QObject *obj, void *opaque)
797{
798 QDict *cpu;
799 int active = ' ';
800 Monitor *mon = opaque;
801
802 assert(qobject_type(obj) == QTYPE_QDICT);
803 cpu = qobject_to_qdict(obj);
804
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200805 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300806 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200807 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300808
809 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
810
811#if defined(TARGET_I386)
812 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
813 (target_ulong) qdict_get_int(cpu, "pc"));
814#elif defined(TARGET_PPC)
815 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
816 (target_long) qdict_get_int(cpu, "nip"));
817#elif defined(TARGET_SPARC)
818 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
819 (target_long) qdict_get_int(cpu, "pc"));
820 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
821 (target_long) qdict_get_int(cpu, "npc"));
822#elif defined(TARGET_MIPS)
823 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
824 (target_long) qdict_get_int(cpu, "PC"));
825#endif
826
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200827 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300828 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200829 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300830
831 monitor_printf(mon, "\n");
832}
833
834static void monitor_print_cpus(Monitor *mon, const QObject *data)
835{
836 QList *cpu_list;
837
838 assert(qobject_type(data) == QTYPE_QLIST);
839 cpu_list = qobject_to_qlist(data);
840 qlist_iter(cpu_list, print_cpu_iter, mon);
841}
842
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300843static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000844{
845 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300846 QList *cpu_list;
847
848 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000849
850 /* just to set the default cpu if not already done */
851 mon_get_cpu();
852
853 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200854 QDict *cpu;
855 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300856
Avi Kivity4c0960c2009-08-17 23:19:53 +0300857 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300858
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200859 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
860 env->cpu_index, env == mon->mon_cpu,
861 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200862
863 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300864
bellard6a00d602005-11-21 23:25:50 +0000865#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300866 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000867#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300868 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000869#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300870 qdict_put(cpu, "pc", qint_from_int(env->pc));
871 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000872#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300873 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000874#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300875
876 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000877 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878
879 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000880}
881
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200882static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000883{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300884 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200885 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100886 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
887 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200888 return -1;
889 }
890 return 0;
bellard6a00d602005-11-21 23:25:50 +0000891}
892
aliguori376253e2009-03-05 23:01:23 +0000893static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000894{
aliguori376253e2009-03-05 23:01:23 +0000895 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000896}
897
aliguori376253e2009-03-05 23:01:23 +0000898static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000899{
900 int i;
bellard7e2515e2004-08-01 21:52:19 +0000901 const char *str;
ths3b46e622007-09-17 08:09:54 +0000902
aliguoricde76ee2009-03-05 23:01:51 +0000903 if (!mon->rs)
904 return;
bellard7e2515e2004-08-01 21:52:19 +0000905 i = 0;
906 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000907 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000908 if (!str)
909 break;
aliguori376253e2009-03-05 23:01:23 +0000910 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000911 i++;
bellardaa455482004-04-04 13:07:25 +0000912 }
913}
914
j_mayer76a66252007-03-07 08:32:30 +0000915#if defined(TARGET_PPC)
916/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000917static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000918{
919 CPUState *env;
920
921 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000922 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000923}
924#endif
925
Luiz Capitulinob223f352009-10-07 13:41:56 -0300926/**
927 * do_quit(): Quit QEMU execution
928 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200929static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000930{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300931 monitor_suspend(mon);
932 no_shutdown = 0;
933 qemu_system_shutdown_request();
934
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200935 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000936}
937
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200938static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000939{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200940 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100941 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200942 return -1;
943 }
aliguoribb5fc202009-03-05 23:01:15 +0000944
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200945 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100946}
947
948static void change_vnc_password_cb(Monitor *mon, const char *password,
949 void *opaque)
950{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100951 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000952 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000953}
954
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200955static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000956{
ths70848512007-08-25 01:37:05 +0000957 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000958 strcmp(target, "password") == 0) {
959 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000960 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000961 strncpy(password, arg, sizeof(password));
962 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200963 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000964 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200965 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000966 }
ths70848512007-08-25 01:37:05 +0000967 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200968 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100969 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200970 return -1;
971 }
ths70848512007-08-25 01:37:05 +0000972 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200973
974 return 0;
thse25a5822007-08-25 01:36:20 +0000975}
976
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100977/**
978 * do_change(): Change a removable medium, or VNC configuration
979 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200980static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000981{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300982 const char *device = qdict_get_str(qdict, "device");
983 const char *target = qdict_get_str(qdict, "target");
984 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200985 int ret;
986
thse25a5822007-08-25 01:36:20 +0000987 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200988 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000989 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200990 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000991 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200992
993 return ret;
thse25a5822007-08-25 01:36:20 +0000994}
995
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300996static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +0000997{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300998 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300999 return 0;
bellard59a983b2004-03-17 23:17:16 +00001000}
1001
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001002static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001003{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001004 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001005}
1006
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001007static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001008{
1009 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001010 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001011
bellard9307c4c2004-04-04 12:57:25 +00001012 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001013 mask = 0;
1014 } else {
bellard9307c4c2004-04-04 12:57:25 +00001015 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001016 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001017 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001018 return;
1019 }
1020 }
1021 cpu_set_log(mask);
1022}
1023
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001024static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001025{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001026 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001027 if (!option || !strcmp(option, "on")) {
1028 singlestep = 1;
1029 } else if (!strcmp(option, "off")) {
1030 singlestep = 0;
1031 } else {
1032 monitor_printf(mon, "unexpected option %s\n", option);
1033 }
1034}
1035
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001036/**
1037 * do_stop(): Stop VM execution
1038 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001039static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001040{
1041 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001042 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001043}
1044
aliguoribb5fc202009-03-05 23:01:15 +00001045static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001046
aliguori376253e2009-03-05 23:01:23 +00001047struct bdrv_iterate_context {
1048 Monitor *mon;
1049 int err;
1050};
aliguoric0f4ce72009-03-05 23:01:01 +00001051
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001052/**
1053 * do_cont(): Resume emulation.
1054 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001055static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001056{
1057 struct bdrv_iterate_context context = { mon, 0 };
1058
Amit Shah8e848652010-07-27 15:49:19 +05301059 if (incoming_expected) {
1060 qerror_report(QERR_MIGRATION_EXPECTED);
1061 return -1;
1062 }
aliguori376253e2009-03-05 23:01:23 +00001063 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001064 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001065 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001066 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001067 return 0;
1068 } else {
1069 return -1;
1070 }
bellard8a7ddc32004-03-31 19:00:16 +00001071}
1072
aliguoribb5fc202009-03-05 23:01:15 +00001073static void bdrv_key_cb(void *opaque, int err)
1074{
aliguori376253e2009-03-05 23:01:23 +00001075 Monitor *mon = opaque;
1076
aliguoribb5fc202009-03-05 23:01:15 +00001077 /* another key was set successfully, retry to continue */
1078 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001079 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001080}
1081
1082static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1083{
aliguori376253e2009-03-05 23:01:23 +00001084 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001085
aliguori376253e2009-03-05 23:01:23 +00001086 if (!context->err && bdrv_key_required(bs)) {
1087 context->err = -EBUSY;
1088 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1089 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001090 }
1091}
1092
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001093static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001095 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001096 if (!device)
1097 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1098 if (gdbserver_start(device) < 0) {
1099 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1100 device);
1101 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001102 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001103 } else {
aliguori59030a82009-04-05 18:43:41 +00001104 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1105 device);
bellard8a7ddc32004-03-31 19:00:16 +00001106 }
1107}
1108
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001109static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001110{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001111 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001112 if (select_watchdog_action(action) == -1) {
1113 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1114 }
1115}
1116
aliguori376253e2009-03-05 23:01:23 +00001117static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001118{
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001120 switch(c) {
1121 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001123 break;
1124 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001126 break;
1127 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001129 break;
1130 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001132 break;
1133 default:
1134 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001135 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001136 } else {
aliguori376253e2009-03-05 23:01:23 +00001137 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001138 }
1139 break;
1140 }
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001142}
1143
aliguori376253e2009-03-05 23:01:23 +00001144static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001145 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001146{
bellard6a00d602005-11-21 23:25:50 +00001147 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001148 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001149 uint8_t buf[16];
1150 uint64_t v;
1151
1152 if (format == 'i') {
1153 int flags;
1154 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001155 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001156#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001157 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001158 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001159 } else if (wsize == 4) {
1160 flags = 0;
1161 } else {
bellard6a15fd12006-04-12 21:07:07 +00001162 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001163 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001164 if (env) {
1165#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001166 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001167 (env->segs[R_CS].flags & DESC_L_MASK))
1168 flags = 2;
1169 else
1170#endif
1171 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1172 flags = 1;
1173 }
bellard4c27ba22004-04-25 18:05:08 +00001174 }
1175#endif
aliguori376253e2009-03-05 23:01:23 +00001176 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001177 return;
1178 }
1179
1180 len = wsize * count;
1181 if (wsize == 1)
1182 line_size = 8;
1183 else
1184 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001185 max_digits = 0;
1186
1187 switch(format) {
1188 case 'o':
1189 max_digits = (wsize * 8 + 2) / 3;
1190 break;
1191 default:
1192 case 'x':
1193 max_digits = (wsize * 8) / 4;
1194 break;
1195 case 'u':
1196 case 'd':
1197 max_digits = (wsize * 8 * 10 + 32) / 33;
1198 break;
1199 case 'c':
1200 wsize = 1;
1201 break;
1202 }
1203
1204 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001205 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001206 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001207 else
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001209 l = len;
1210 if (l > line_size)
1211 l = line_size;
1212 if (is_physical) {
1213 cpu_physical_memory_rw(addr, buf, l, 0);
1214 } else {
bellard6a00d602005-11-21 23:25:50 +00001215 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001216 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001218 break;
1219 }
bellard9307c4c2004-04-04 12:57:25 +00001220 }
ths5fafdf22007-09-16 21:08:06 +00001221 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001222 while (i < l) {
1223 switch(wsize) {
1224 default:
1225 case 1:
1226 v = ldub_raw(buf + i);
1227 break;
1228 case 2:
1229 v = lduw_raw(buf + i);
1230 break;
1231 case 4:
bellard92a31b12005-02-10 22:00:52 +00001232 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001233 break;
1234 case 8:
1235 v = ldq_raw(buf + i);
1236 break;
1237 }
aliguori376253e2009-03-05 23:01:23 +00001238 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001239 switch(format) {
1240 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001242 break;
1243 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001245 break;
1246 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001247 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001248 break;
1249 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001250 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001251 break;
1252 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001254 break;
1255 }
1256 i += wsize;
1257 }
aliguori376253e2009-03-05 23:01:23 +00001258 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001259 addr += l;
1260 len -= l;
1261 }
1262}
1263
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001264static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001265{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001266 int count = qdict_get_int(qdict, "count");
1267 int format = qdict_get_int(qdict, "format");
1268 int size = qdict_get_int(qdict, "size");
1269 target_long addr = qdict_get_int(qdict, "addr");
1270
aliguori376253e2009-03-05 23:01:23 +00001271 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001272}
1273
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001274static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001275{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001276 int count = qdict_get_int(qdict, "count");
1277 int format = qdict_get_int(qdict, "format");
1278 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001279 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001280
aliguori376253e2009-03-05 23:01:23 +00001281 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001282}
1283
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001284static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001285{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001286 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001287 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001288
blueswir17743e582007-09-24 18:39:04 +00001289#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001290 switch(format) {
1291 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001292 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 default:
1301 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001306 break;
1307 }
bellard92a31b12005-02-10 22:00:52 +00001308#else
1309 switch(format) {
1310 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001312 break;
1313 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001314 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001315 break;
1316 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001318 break;
1319 default:
1320 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001321 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001322 break;
1323 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001325 break;
1326 }
1327#endif
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001329}
1330
Luiz Capitulino98696222010-02-10 23:49:58 -02001331static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001332{
1333 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001334 uint32_t size = qdict_get_int(qdict, "size");
1335 const char *filename = qdict_get_str(qdict, "filename");
1336 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001337 uint32_t l;
1338 CPUState *env;
1339 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001340 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001341
1342 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001343
1344 f = fopen(filename, "wb");
1345 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001346 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001347 return -1;
bellardb371dc52007-01-03 15:20:39 +00001348 }
1349 while (size != 0) {
1350 l = sizeof(buf);
1351 if (l > size)
1352 l = size;
1353 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001354 if (fwrite(buf, 1, l, f) != l) {
1355 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1356 goto exit;
1357 }
bellardb371dc52007-01-03 15:20:39 +00001358 addr += l;
1359 size -= l;
1360 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001361
1362 ret = 0;
1363
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001364exit:
bellardb371dc52007-01-03 15:20:39 +00001365 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001366 return ret;
bellardb371dc52007-01-03 15:20:39 +00001367}
1368
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001369static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001370 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001371{
1372 FILE *f;
1373 uint32_t l;
1374 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001375 uint32_t size = qdict_get_int(qdict, "size");
1376 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001377 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001378 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001379
1380 f = fopen(filename, "wb");
1381 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001382 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001383 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001384 }
1385 while (size != 0) {
1386 l = sizeof(buf);
1387 if (l > size)
1388 l = size;
1389 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001390 if (fwrite(buf, 1, l, f) != l) {
1391 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1392 goto exit;
1393 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001394 fflush(f);
1395 addr += l;
1396 size -= l;
1397 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001398
1399 ret = 0;
1400
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001401exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001402 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001403 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001404}
1405
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001406static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001407{
1408 uint32_t addr;
1409 uint8_t buf[1];
1410 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001411 uint32_t start = qdict_get_int(qdict, "start");
1412 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001413
1414 sum = 0;
1415 for(addr = start; addr < (start + size); addr++) {
1416 cpu_physical_memory_rw(addr, buf, 1, 0);
1417 /* BSD sum algorithm ('sum' Unix command) */
1418 sum = (sum >> 1) | (sum << 15);
1419 sum += buf[0];
1420 }
aliguori376253e2009-03-05 23:01:23 +00001421 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001422}
1423
bellarda3a91a32004-06-04 11:06:21 +00001424typedef struct {
1425 int keycode;
1426 const char *name;
1427} KeyDef;
1428
1429static const KeyDef key_defs[] = {
1430 { 0x2a, "shift" },
1431 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001432
bellarda3a91a32004-06-04 11:06:21 +00001433 { 0x38, "alt" },
1434 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001435 { 0x64, "altgr" },
1436 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001437 { 0x1d, "ctrl" },
1438 { 0x9d, "ctrl_r" },
1439
1440 { 0xdd, "menu" },
1441
1442 { 0x01, "esc" },
1443
1444 { 0x02, "1" },
1445 { 0x03, "2" },
1446 { 0x04, "3" },
1447 { 0x05, "4" },
1448 { 0x06, "5" },
1449 { 0x07, "6" },
1450 { 0x08, "7" },
1451 { 0x09, "8" },
1452 { 0x0a, "9" },
1453 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001454 { 0x0c, "minus" },
1455 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001456 { 0x0e, "backspace" },
1457
1458 { 0x0f, "tab" },
1459 { 0x10, "q" },
1460 { 0x11, "w" },
1461 { 0x12, "e" },
1462 { 0x13, "r" },
1463 { 0x14, "t" },
1464 { 0x15, "y" },
1465 { 0x16, "u" },
1466 { 0x17, "i" },
1467 { 0x18, "o" },
1468 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001469 { 0x1a, "bracket_left" },
1470 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001471 { 0x1c, "ret" },
1472
1473 { 0x1e, "a" },
1474 { 0x1f, "s" },
1475 { 0x20, "d" },
1476 { 0x21, "f" },
1477 { 0x22, "g" },
1478 { 0x23, "h" },
1479 { 0x24, "j" },
1480 { 0x25, "k" },
1481 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001482 { 0x27, "semicolon" },
1483 { 0x28, "apostrophe" },
1484 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001485
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001486 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001487 { 0x2c, "z" },
1488 { 0x2d, "x" },
1489 { 0x2e, "c" },
1490 { 0x2f, "v" },
1491 { 0x30, "b" },
1492 { 0x31, "n" },
1493 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001494 { 0x33, "comma" },
1495 { 0x34, "dot" },
1496 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001497
balrog4d3b6f62008-02-10 16:33:14 +00001498 { 0x37, "asterisk" },
1499
bellarda3a91a32004-06-04 11:06:21 +00001500 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001501 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001502 { 0x3b, "f1" },
1503 { 0x3c, "f2" },
1504 { 0x3d, "f3" },
1505 { 0x3e, "f4" },
1506 { 0x3f, "f5" },
1507 { 0x40, "f6" },
1508 { 0x41, "f7" },
1509 { 0x42, "f8" },
1510 { 0x43, "f9" },
1511 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001512 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001513 { 0x46, "scroll_lock" },
1514
bellard64866c32006-05-07 18:03:31 +00001515 { 0xb5, "kp_divide" },
1516 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001517 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001518 { 0x4e, "kp_add" },
1519 { 0x9c, "kp_enter" },
1520 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001521 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001522
1523 { 0x52, "kp_0" },
1524 { 0x4f, "kp_1" },
1525 { 0x50, "kp_2" },
1526 { 0x51, "kp_3" },
1527 { 0x4b, "kp_4" },
1528 { 0x4c, "kp_5" },
1529 { 0x4d, "kp_6" },
1530 { 0x47, "kp_7" },
1531 { 0x48, "kp_8" },
1532 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001533
bellarda3a91a32004-06-04 11:06:21 +00001534 { 0x56, "<" },
1535
1536 { 0x57, "f11" },
1537 { 0x58, "f12" },
1538
1539 { 0xb7, "print" },
1540
1541 { 0xc7, "home" },
1542 { 0xc9, "pgup" },
1543 { 0xd1, "pgdn" },
1544 { 0xcf, "end" },
1545
1546 { 0xcb, "left" },
1547 { 0xc8, "up" },
1548 { 0xd0, "down" },
1549 { 0xcd, "right" },
1550
1551 { 0xd2, "insert" },
1552 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001553#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1554 { 0xf0, "stop" },
1555 { 0xf1, "again" },
1556 { 0xf2, "props" },
1557 { 0xf3, "undo" },
1558 { 0xf4, "front" },
1559 { 0xf5, "copy" },
1560 { 0xf6, "open" },
1561 { 0xf7, "paste" },
1562 { 0xf8, "find" },
1563 { 0xf9, "cut" },
1564 { 0xfa, "lf" },
1565 { 0xfb, "help" },
1566 { 0xfc, "meta_l" },
1567 { 0xfd, "meta_r" },
1568 { 0xfe, "compose" },
1569#endif
bellarda3a91a32004-06-04 11:06:21 +00001570 { 0, NULL },
1571};
1572
1573static int get_keycode(const char *key)
1574{
1575 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001576 char *endp;
1577 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001578
1579 for(p = key_defs; p->name != NULL; p++) {
1580 if (!strcmp(key, p->name))
1581 return p->keycode;
1582 }
bellard64866c32006-05-07 18:03:31 +00001583 if (strstart(key, "0x", NULL)) {
1584 ret = strtoul(key, &endp, 0);
1585 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1586 return ret;
1587 }
bellarda3a91a32004-06-04 11:06:21 +00001588 return -1;
1589}
1590
balrogc8256f92008-06-08 22:45:01 +00001591#define MAX_KEYCODES 16
1592static uint8_t keycodes[MAX_KEYCODES];
1593static int nb_pending_keycodes;
1594static QEMUTimer *key_timer;
1595
1596static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001597{
balrogc8256f92008-06-08 22:45:01 +00001598 int keycode;
1599
1600 while (nb_pending_keycodes > 0) {
1601 nb_pending_keycodes--;
1602 keycode = keycodes[nb_pending_keycodes];
1603 if (keycode & 0x80)
1604 kbd_put_keycode(0xe0);
1605 kbd_put_keycode(keycode | 0x80);
1606 }
1607}
1608
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001609static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001610{
balrog3401c0d2008-06-04 10:05:59 +00001611 char keyname_buf[16];
1612 char *separator;
1613 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001614 const char *string = qdict_get_str(qdict, "string");
1615 int has_hold_time = qdict_haskey(qdict, "hold_time");
1616 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001617
balrogc8256f92008-06-08 22:45:01 +00001618 if (nb_pending_keycodes > 0) {
1619 qemu_del_timer(key_timer);
1620 release_keys(NULL);
1621 }
1622 if (!has_hold_time)
1623 hold_time = 100;
1624 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001625 while (1) {
1626 separator = strchr(string, '-');
1627 keyname_len = separator ? separator - string : strlen(string);
1628 if (keyname_len > 0) {
1629 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1630 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001631 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001632 return;
bellarda3a91a32004-06-04 11:06:21 +00001633 }
balrogc8256f92008-06-08 22:45:01 +00001634 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001635 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001636 return;
1637 }
1638 keyname_buf[keyname_len] = 0;
1639 keycode = get_keycode(keyname_buf);
1640 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001641 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001642 return;
1643 }
balrogc8256f92008-06-08 22:45:01 +00001644 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001645 }
balrog3401c0d2008-06-04 10:05:59 +00001646 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001647 break;
balrog3401c0d2008-06-04 10:05:59 +00001648 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001649 }
balrogc8256f92008-06-08 22:45:01 +00001650 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001651 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001652 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001653 keycode = keycodes[i];
1654 if (keycode & 0x80)
1655 kbd_put_keycode(0xe0);
1656 kbd_put_keycode(keycode & 0x7f);
1657 }
balrogc8256f92008-06-08 22:45:01 +00001658 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001659 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001660 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001661}
1662
bellard13224a82006-07-14 22:03:35 +00001663static int mouse_button_state;
1664
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001665static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001666{
1667 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001668 const char *dx_str = qdict_get_str(qdict, "dx_str");
1669 const char *dy_str = qdict_get_str(qdict, "dy_str");
1670 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001671 dx = strtol(dx_str, NULL, 0);
1672 dy = strtol(dy_str, NULL, 0);
1673 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001674 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001675 dz = strtol(dz_str, NULL, 0);
1676 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1677}
1678
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001679static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001680{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001681 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001682 mouse_button_state = button_state;
1683 kbd_mouse_event(0, 0, 0, mouse_button_state);
1684}
1685
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001686static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001687{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001688 int size = qdict_get_int(qdict, "size");
1689 int addr = qdict_get_int(qdict, "addr");
1690 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001691 uint32_t val;
1692 int suffix;
1693
1694 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001695 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001696 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001697 addr++;
1698 }
1699 addr &= 0xffff;
1700
1701 switch(size) {
1702 default:
1703 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001704 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001705 suffix = 'b';
1706 break;
1707 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001708 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001709 suffix = 'w';
1710 break;
1711 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001712 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001713 suffix = 'l';
1714 break;
1715 }
aliguori376253e2009-03-05 23:01:23 +00001716 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1717 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001718}
bellarda3a91a32004-06-04 11:06:21 +00001719
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001720static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001721{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001722 int size = qdict_get_int(qdict, "size");
1723 int addr = qdict_get_int(qdict, "addr");
1724 int val = qdict_get_int(qdict, "val");
1725
Jan Kiszkaf1147842009-07-14 10:20:11 +02001726 addr &= IOPORTS_MASK;
1727
1728 switch (size) {
1729 default:
1730 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001731 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001732 break;
1733 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001734 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001735 break;
1736 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001737 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001738 break;
1739 }
1740}
1741
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001742static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001743{
1744 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001745 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001746
Jan Kiszka76e30d02009-07-02 00:19:02 +02001747 res = qemu_boot_set(bootdevice);
1748 if (res == 0) {
1749 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1750 } else if (res > 0) {
1751 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001752 } else {
aliguori376253e2009-03-05 23:01:23 +00001753 monitor_printf(mon, "no function defined to set boot device list for "
1754 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001755 }
1756}
1757
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001758/**
1759 * do_system_reset(): Issue a machine reset
1760 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001761static int do_system_reset(Monitor *mon, const QDict *qdict,
1762 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001763{
1764 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001765 return 0;
bellarde4f90822004-06-20 12:35:44 +00001766}
1767
Luiz Capitulino43076662009-10-07 13:41:59 -03001768/**
1769 * do_system_powerdown(): Issue a machine powerdown
1770 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001771static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1772 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001773{
1774 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001775 return 0;
bellard34751872005-07-02 14:31:34 +00001776}
1777
bellardb86bda52004-09-18 19:32:46 +00001778#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001779static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001780{
aliguori376253e2009-03-05 23:01:23 +00001781 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1782 addr,
1783 pte & mask,
1784 pte & PG_GLOBAL_MASK ? 'G' : '-',
1785 pte & PG_PSE_MASK ? 'P' : '-',
1786 pte & PG_DIRTY_MASK ? 'D' : '-',
1787 pte & PG_ACCESSED_MASK ? 'A' : '-',
1788 pte & PG_PCD_MASK ? 'C' : '-',
1789 pte & PG_PWT_MASK ? 'T' : '-',
1790 pte & PG_USER_MASK ? 'U' : '-',
1791 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001792}
1793
aliguori376253e2009-03-05 23:01:23 +00001794static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001795{
bellard6a00d602005-11-21 23:25:50 +00001796 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001797 int l1, l2;
1798 uint32_t pgd, pde, pte;
1799
bellard6a00d602005-11-21 23:25:50 +00001800 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001801
bellardb86bda52004-09-18 19:32:46 +00001802 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001803 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001804 return;
1805 }
1806 pgd = env->cr[3] & ~0xfff;
1807 for(l1 = 0; l1 < 1024; l1++) {
1808 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1809 pde = le32_to_cpu(pde);
1810 if (pde & PG_PRESENT_MASK) {
1811 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001812 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001813 } else {
1814 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001815 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001816 (uint8_t *)&pte, 4);
1817 pte = le32_to_cpu(pte);
1818 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001819 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001820 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001821 ~0xfff);
1822 }
1823 }
1824 }
1825 }
1826 }
1827}
1828
aliguori376253e2009-03-05 23:01:23 +00001829static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001830 uint32_t end, int prot)
1831{
bellard9746b152004-11-11 18:30:24 +00001832 int prot1;
1833 prot1 = *plast_prot;
1834 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001835 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001836 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1837 *pstart, end, end - *pstart,
1838 prot1 & PG_USER_MASK ? 'u' : '-',
1839 'r',
1840 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001841 }
1842 if (prot != 0)
1843 *pstart = end;
1844 else
1845 *pstart = -1;
1846 *plast_prot = prot;
1847 }
1848}
1849
aliguori376253e2009-03-05 23:01:23 +00001850static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001851{
bellard6a00d602005-11-21 23:25:50 +00001852 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001853 int l1, l2, prot, last_prot;
1854 uint32_t pgd, pde, pte, start, end;
1855
bellard6a00d602005-11-21 23:25:50 +00001856 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001857
bellardb86bda52004-09-18 19:32:46 +00001858 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001859 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001860 return;
1861 }
1862 pgd = env->cr[3] & ~0xfff;
1863 last_prot = 0;
1864 start = -1;
1865 for(l1 = 0; l1 < 1024; l1++) {
1866 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1867 pde = le32_to_cpu(pde);
1868 end = l1 << 22;
1869 if (pde & PG_PRESENT_MASK) {
1870 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1871 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001872 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001873 } else {
1874 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001875 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001876 (uint8_t *)&pte, 4);
1877 pte = le32_to_cpu(pte);
1878 end = (l1 << 22) + (l2 << 12);
1879 if (pte & PG_PRESENT_MASK) {
1880 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1881 } else {
1882 prot = 0;
1883 }
aliguori376253e2009-03-05 23:01:23 +00001884 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001885 }
1886 }
1887 } else {
1888 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001889 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001890 }
1891 }
1892}
1893#endif
1894
aurel327c664e22009-03-03 06:12:22 +00001895#if defined(TARGET_SH4)
1896
aliguori376253e2009-03-05 23:01:23 +00001897static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001898{
aliguori376253e2009-03-05 23:01:23 +00001899 monitor_printf(mon, " tlb%i:\t"
1900 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1901 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1902 "dirty=%hhu writethrough=%hhu\n",
1903 idx,
1904 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1905 tlb->v, tlb->sh, tlb->c, tlb->pr,
1906 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001907}
1908
aliguori376253e2009-03-05 23:01:23 +00001909static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001910{
1911 CPUState *env = mon_get_cpu();
1912 int i;
1913
aliguori376253e2009-03-05 23:01:23 +00001914 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001915 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001916 print_tlb (mon, i, &env->itlb[i]);
1917 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001918 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001919 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001920}
1921
1922#endif
1923
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001924static void do_info_kvm_print(Monitor *mon, const QObject *data)
1925{
1926 QDict *qdict;
1927
1928 qdict = qobject_to_qdict(data);
1929
1930 monitor_printf(mon, "kvm support: ");
1931 if (qdict_get_bool(qdict, "present")) {
1932 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1933 "enabled" : "disabled");
1934 } else {
1935 monitor_printf(mon, "not compiled\n");
1936 }
1937}
1938
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001939static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001940{
1941#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001942 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1943 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001944#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001945 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001946#endif
1947}
1948
aliguori030ea372009-04-21 22:30:47 +00001949static void do_info_numa(Monitor *mon)
1950{
aliguorib28b6232009-04-22 20:20:29 +00001951 int i;
aliguori030ea372009-04-21 22:30:47 +00001952 CPUState *env;
1953
1954 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1955 for (i = 0; i < nb_numa_nodes; i++) {
1956 monitor_printf(mon, "node %d cpus:", i);
1957 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1958 if (env->numa_node == i) {
1959 monitor_printf(mon, " %d", env->cpu_index);
1960 }
1961 }
1962 monitor_printf(mon, "\n");
1963 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1964 node_mem[i] >> 20);
1965 }
1966}
1967
bellard5f1ce942006-02-08 22:40:15 +00001968#ifdef CONFIG_PROFILER
1969
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001970int64_t qemu_time;
1971int64_t dev_time;
1972
aliguori376253e2009-03-05 23:01:23 +00001973static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001974{
1975 int64_t total;
1976 total = qemu_time;
1977 if (total == 0)
1978 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001979 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001980 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001981 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001982 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001983 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001984 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001985}
1986#else
aliguori376253e2009-03-05 23:01:23 +00001987static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001988{
aliguori376253e2009-03-05 23:01:23 +00001989 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001990}
1991#endif
1992
bellardec36b692006-07-16 18:57:03 +00001993/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001994static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001995
aliguori376253e2009-03-05 23:01:23 +00001996static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001997{
1998 int i;
1999 CaptureState *s;
2000
2001 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002002 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002003 s->ops.info (s->opaque);
2004 }
2005}
2006
Blue Swirl23130862009-06-06 08:22:04 +00002007#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002008static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002009{
2010 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002011 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002012 CaptureState *s;
2013
2014 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2015 if (i == n) {
2016 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002017 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002018 qemu_free (s);
2019 return;
2020 }
2021 }
2022}
2023
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002024static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002025{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002026 const char *path = qdict_get_str(qdict, "path");
2027 int has_freq = qdict_haskey(qdict, "freq");
2028 int freq = qdict_get_try_int(qdict, "freq", -1);
2029 int has_bits = qdict_haskey(qdict, "bits");
2030 int bits = qdict_get_try_int(qdict, "bits", -1);
2031 int has_channels = qdict_haskey(qdict, "nchannels");
2032 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002033 CaptureState *s;
2034
2035 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002036
2037 freq = has_freq ? freq : 44100;
2038 bits = has_bits ? bits : 16;
2039 nchannels = has_channels ? nchannels : 2;
2040
2041 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002042 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002043 qemu_free (s);
2044 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002045 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002046}
2047#endif
2048
aurel32dc1c0b72008-04-27 23:52:12 +00002049#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002050static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002051{
2052 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002053 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002054
2055 for (env = first_cpu; env != NULL; env = env->next_cpu)
2056 if (env->cpu_index == cpu_index) {
2057 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2058 break;
2059 }
2060}
2061#endif
2062
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002063static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002064{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002065 QDict *qdict;
2066
2067 qdict = qobject_to_qdict(data);
2068
2069 monitor_printf(mon, "VM status: ");
2070 if (qdict_get_bool(qdict, "running")) {
2071 monitor_printf(mon, "running");
2072 if (qdict_get_bool(qdict, "singlestep")) {
2073 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002074 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002075 } else {
2076 monitor_printf(mon, "paused");
2077 }
2078
2079 monitor_printf(mon, "\n");
2080}
2081
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002082static void do_info_status(Monitor *mon, QObject **ret_data)
2083{
2084 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2085 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002086}
2087
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002088static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002089{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002090 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002091
aliguori76655d62009-03-06 20:27:37 +00002092 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002093 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002094 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002095 return acl;
2096}
aliguori76655d62009-03-06 20:27:37 +00002097
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002098static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002099{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002100 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002101 qemu_acl *acl = find_acl(mon, aclname);
2102 qemu_acl_entry *entry;
2103 int i = 0;
2104
2105 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002106 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002107 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002108 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002109 i++;
2110 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002111 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002112 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002113 }
2114}
2115
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002116static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002117{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002118 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002119 qemu_acl *acl = find_acl(mon, aclname);
2120
2121 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002122 qemu_acl_reset(acl);
2123 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002124 }
2125}
aliguori76655d62009-03-06 20:27:37 +00002126
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002127static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002129 const char *aclname = qdict_get_str(qdict, "aclname");
2130 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002131 qemu_acl *acl = find_acl(mon, aclname);
2132
2133 if (acl) {
2134 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002135 acl->defaultDeny = 0;
2136 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002137 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002138 acl->defaultDeny = 1;
2139 monitor_printf(mon, "acl: policy set to 'deny'\n");
2140 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002141 monitor_printf(mon, "acl: unknown policy '%s', "
2142 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002143 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002144 }
2145}
aliguori76655d62009-03-06 20:27:37 +00002146
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002147static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002148{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002149 const char *aclname = qdict_get_str(qdict, "aclname");
2150 const char *match = qdict_get_str(qdict, "match");
2151 const char *policy = qdict_get_str(qdict, "policy");
2152 int has_index = qdict_haskey(qdict, "index");
2153 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002154 qemu_acl *acl = find_acl(mon, aclname);
2155 int deny, ret;
2156
2157 if (acl) {
2158 if (strcmp(policy, "allow") == 0) {
2159 deny = 0;
2160 } else if (strcmp(policy, "deny") == 0) {
2161 deny = 1;
2162 } else {
2163 monitor_printf(mon, "acl: unknown policy '%s', "
2164 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002165 return;
2166 }
aliguori28a76be2009-03-06 20:27:40 +00002167 if (has_index)
2168 ret = qemu_acl_insert(acl, deny, match, index);
2169 else
2170 ret = qemu_acl_append(acl, deny, match);
2171 if (ret < 0)
2172 monitor_printf(mon, "acl: unable to add acl entry\n");
2173 else
2174 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002175 }
2176}
aliguori76655d62009-03-06 20:27:37 +00002177
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002178static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002179{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002180 const char *aclname = qdict_get_str(qdict, "aclname");
2181 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002182 qemu_acl *acl = find_acl(mon, aclname);
2183 int ret;
aliguori76655d62009-03-06 20:27:37 +00002184
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002185 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002186 ret = qemu_acl_remove(acl, match);
2187 if (ret < 0)
2188 monitor_printf(mon, "acl: no matching acl entry\n");
2189 else
2190 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002191 }
2192}
2193
Huang Ying79c4f6b2009-06-23 10:05:14 +08002194#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002195static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002196{
2197 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002198 int cpu_index = qdict_get_int(qdict, "cpu_index");
2199 int bank = qdict_get_int(qdict, "bank");
2200 uint64_t status = qdict_get_int(qdict, "status");
2201 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2202 uint64_t addr = qdict_get_int(qdict, "addr");
2203 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002204
2205 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2206 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2207 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2208 break;
2209 }
2210}
2211#endif
2212
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002213static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002214{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002215 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002216 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002217 int fd;
2218
2219 fd = qemu_chr_get_msgfd(mon->chr);
2220 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002221 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002222 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002223 }
2224
2225 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002226 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2227 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002228 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229 }
2230
Blue Swirl72cf2d42009-09-12 07:36:22 +00002231 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002232 if (strcmp(monfd->name, fdname) != 0) {
2233 continue;
2234 }
2235
2236 close(monfd->fd);
2237 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002238 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002239 }
2240
Anthony Liguoric227f092009-10-01 16:12:16 -05002241 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242 monfd->name = qemu_strdup(fdname);
2243 monfd->fd = fd;
2244
Blue Swirl72cf2d42009-09-12 07:36:22 +00002245 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002246 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002247}
2248
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002249static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002251 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002252 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253
Blue Swirl72cf2d42009-09-12 07:36:22 +00002254 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002255 if (strcmp(monfd->name, fdname) != 0) {
2256 continue;
2257 }
2258
Blue Swirl72cf2d42009-09-12 07:36:22 +00002259 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002260 close(monfd->fd);
2261 qemu_free(monfd->name);
2262 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002263 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002264 }
2265
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002266 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002267 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002268}
2269
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002270static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002271{
2272 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002273 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002274
2275 vm_stop(0);
2276
Markus Armbruster03cd4652010-02-17 16:24:10 +01002277 if (load_vmstate(name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002278 vm_start();
2279}
2280
Mark McLoughlin7768e042009-07-22 09:11:41 +01002281int monitor_get_fd(Monitor *mon, const char *fdname)
2282{
Anthony Liguoric227f092009-10-01 16:12:16 -05002283 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002284
Blue Swirl72cf2d42009-09-12 07:36:22 +00002285 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002286 int fd;
2287
2288 if (strcmp(monfd->name, fdname) != 0) {
2289 continue;
2290 }
2291
2292 fd = monfd->fd;
2293
2294 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002295 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002296 qemu_free(monfd->name);
2297 qemu_free(monfd);
2298
2299 return fd;
2300 }
2301
2302 return -1;
2303}
2304
Anthony Liguoric227f092009-10-01 16:12:16 -05002305static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002306#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002307 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002308};
2309
Blue Swirl23130862009-06-06 08:22:04 +00002310/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002311static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002312 {
2313 .name = "version",
2314 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002315 .params = "",
2316 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002317 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002318 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002319 },
2320 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002321 .name = "commands",
2322 .args_type = "",
2323 .params = "",
2324 .help = "list QMP available commands",
2325 .user_print = monitor_user_noop,
2326 .mhandler.info_new = do_info_commands,
2327 },
2328 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002329 .name = "network",
2330 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002331 .params = "",
2332 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002333 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002334 },
2335 {
2336 .name = "chardev",
2337 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002338 .params = "",
2339 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002340 .user_print = qemu_chr_info_print,
2341 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002342 },
2343 {
2344 .name = "block",
2345 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002346 .params = "",
2347 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002348 .user_print = bdrv_info_print,
2349 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002350 },
2351 {
2352 .name = "blockstats",
2353 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002354 .params = "",
2355 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002356 .user_print = bdrv_stats_print,
2357 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002358 },
2359 {
2360 .name = "registers",
2361 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002362 .params = "",
2363 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002364 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002365 },
2366 {
2367 .name = "cpus",
2368 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002369 .params = "",
2370 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002371 .user_print = monitor_print_cpus,
2372 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002373 },
2374 {
2375 .name = "history",
2376 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002377 .params = "",
2378 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002379 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002380 },
2381 {
2382 .name = "irq",
2383 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002384 .params = "",
2385 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002386 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002387 },
2388 {
2389 .name = "pic",
2390 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002391 .params = "",
2392 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002393 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002394 },
2395 {
2396 .name = "pci",
2397 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 .params = "",
2399 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002400 .user_print = do_pci_info_print,
2401 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002402 },
aurel327c664e22009-03-03 06:12:22 +00002403#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002404 {
2405 .name = "tlb",
2406 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002407 .params = "",
2408 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002409 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002410 },
aurel327c664e22009-03-03 06:12:22 +00002411#endif
2412#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 {
2414 .name = "mem",
2415 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002416 .params = "",
2417 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002418 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002419 },
bellardb86bda52004-09-18 19:32:46 +00002420#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002421 {
2422 .name = "jit",
2423 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002424 .params = "",
2425 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002426 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002427 },
2428 {
2429 .name = "kvm",
2430 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002431 .params = "",
2432 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002433 .user_print = do_info_kvm_print,
2434 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002435 },
2436 {
2437 .name = "numa",
2438 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002439 .params = "",
2440 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002441 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002442 },
2443 {
2444 .name = "usb",
2445 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002446 .params = "",
2447 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002448 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002449 },
2450 {
2451 .name = "usbhost",
2452 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002453 .params = "",
2454 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002455 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002456 },
2457 {
2458 .name = "profile",
2459 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002460 .params = "",
2461 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002462 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 },
2464 {
2465 .name = "capture",
2466 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 .params = "",
2468 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002469 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002470 },
2471 {
2472 .name = "snapshots",
2473 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 .params = "",
2475 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002476 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002477 },
2478 {
2479 .name = "status",
2480 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002481 .params = "",
2482 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002483 .user_print = do_info_status_print,
2484 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002485 },
2486 {
2487 .name = "pcmcia",
2488 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002489 .params = "",
2490 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002491 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002492 },
2493 {
2494 .name = "mice",
2495 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 .params = "",
2497 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002498 .user_print = do_info_mice_print,
2499 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002500 },
2501 {
2502 .name = "vnc",
2503 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002504 .params = "",
2505 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002506 .user_print = do_info_vnc_print,
2507 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002508 },
2509 {
2510 .name = "name",
2511 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 .params = "",
2513 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002514 .user_print = do_info_name_print,
2515 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 },
2517 {
2518 .name = "uuid",
2519 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 .params = "",
2521 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002522 .user_print = do_info_uuid_print,
2523 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 },
j_mayer76a66252007-03-07 08:32:30 +00002525#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 {
2527 .name = "cpustats",
2528 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 .params = "",
2530 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002531 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
j_mayer76a66252007-03-07 08:32:30 +00002533#endif
blueswir131a60e22007-10-26 18:42:59 +00002534#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002535 {
2536 .name = "usernet",
2537 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 .params = "",
2539 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002540 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 },
blueswir131a60e22007-10-26 18:42:59 +00002542#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002543 {
2544 .name = "migrate",
2545 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002546 .params = "",
2547 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002548 .user_print = do_info_migrate_print,
2549 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002550 },
2551 {
2552 .name = "balloon",
2553 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 .params = "",
2555 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002556 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002557 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002558 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 },
2560 {
2561 .name = "qtree",
2562 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 .params = "",
2564 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002565 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 },
2567 {
2568 .name = "qdm",
2569 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 .params = "",
2571 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002572 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 },
2574 {
2575 .name = "roms",
2576 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 .params = "",
2578 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002579 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = NULL,
2583 },
bellard9dc39cb2004-03-14 21:38:27 +00002584};
2585
bellard9307c4c2004-04-04 12:57:25 +00002586/*******************************************************************/
2587
2588static const char *pch;
2589static jmp_buf expr_env;
2590
bellard92a31b12005-02-10 22:00:52 +00002591#define MD_TLONG 0
2592#define MD_I32 1
2593
bellard9307c4c2004-04-04 12:57:25 +00002594typedef struct MonitorDef {
2595 const char *name;
2596 int offset;
blueswir18662d652008-10-02 18:32:44 +00002597 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002598 int type;
bellard9307c4c2004-04-04 12:57:25 +00002599} MonitorDef;
2600
bellard57206fd2004-04-25 18:54:52 +00002601#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002602static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002603{
bellard6a00d602005-11-21 23:25:50 +00002604 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002605 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002606}
2607#endif
2608
bellarda541f292004-04-12 20:39:29 +00002609#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002610static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002611{
bellard6a00d602005-11-21 23:25:50 +00002612 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002613 unsigned int u;
2614 int i;
2615
2616 u = 0;
2617 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002618 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002619
2620 return u;
2621}
2622
blueswir18662d652008-10-02 18:32:44 +00002623static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002624{
bellard6a00d602005-11-21 23:25:50 +00002625 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002626 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002627}
2628
blueswir18662d652008-10-02 18:32:44 +00002629static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002630{
bellard6a00d602005-11-21 23:25:50 +00002631 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002632 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002633}
bellard9fddaa02004-05-21 12:59:32 +00002634
blueswir18662d652008-10-02 18:32:44 +00002635static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002636{
bellard6a00d602005-11-21 23:25:50 +00002637 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002638 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002639}
2640
blueswir18662d652008-10-02 18:32:44 +00002641static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002642{
bellard6a00d602005-11-21 23:25:50 +00002643 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002644 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002645}
2646
blueswir18662d652008-10-02 18:32:44 +00002647static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002648{
bellard6a00d602005-11-21 23:25:50 +00002649 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002650 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002651}
bellarda541f292004-04-12 20:39:29 +00002652#endif
2653
bellarde95c8d52004-09-30 22:22:08 +00002654#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002655#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002656static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002657{
bellard6a00d602005-11-21 23:25:50 +00002658 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002659
2660 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002661}
bellard7b936c02005-10-30 17:05:13 +00002662#endif
bellarde95c8d52004-09-30 22:22:08 +00002663
blueswir18662d652008-10-02 18:32:44 +00002664static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002665{
bellard6a00d602005-11-21 23:25:50 +00002666 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002667 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002668}
2669#endif
2670
blueswir18662d652008-10-02 18:32:44 +00002671static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002672#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002673
2674#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002675 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002676 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002677 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002678
bellard9307c4c2004-04-04 12:57:25 +00002679 { "eax", offsetof(CPUState, regs[0]) },
2680 { "ecx", offsetof(CPUState, regs[1]) },
2681 { "edx", offsetof(CPUState, regs[2]) },
2682 { "ebx", offsetof(CPUState, regs[3]) },
2683 { "esp|sp", offsetof(CPUState, regs[4]) },
2684 { "ebp|fp", offsetof(CPUState, regs[5]) },
2685 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002686 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002687#ifdef TARGET_X86_64
2688 { "r8", offsetof(CPUState, regs[8]) },
2689 { "r9", offsetof(CPUState, regs[9]) },
2690 { "r10", offsetof(CPUState, regs[10]) },
2691 { "r11", offsetof(CPUState, regs[11]) },
2692 { "r12", offsetof(CPUState, regs[12]) },
2693 { "r13", offsetof(CPUState, regs[13]) },
2694 { "r14", offsetof(CPUState, regs[14]) },
2695 { "r15", offsetof(CPUState, regs[15]) },
2696#endif
bellard9307c4c2004-04-04 12:57:25 +00002697 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002698 { "eip", offsetof(CPUState, eip) },
2699 SEG("cs", R_CS)
2700 SEG("ds", R_DS)
2701 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002702 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002703 SEG("fs", R_FS)
2704 SEG("gs", R_GS)
2705 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002706#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002707 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002708 { "r0", offsetof(CPUState, gpr[0]) },
2709 { "r1", offsetof(CPUState, gpr[1]) },
2710 { "r2", offsetof(CPUState, gpr[2]) },
2711 { "r3", offsetof(CPUState, gpr[3]) },
2712 { "r4", offsetof(CPUState, gpr[4]) },
2713 { "r5", offsetof(CPUState, gpr[5]) },
2714 { "r6", offsetof(CPUState, gpr[6]) },
2715 { "r7", offsetof(CPUState, gpr[7]) },
2716 { "r8", offsetof(CPUState, gpr[8]) },
2717 { "r9", offsetof(CPUState, gpr[9]) },
2718 { "r10", offsetof(CPUState, gpr[10]) },
2719 { "r11", offsetof(CPUState, gpr[11]) },
2720 { "r12", offsetof(CPUState, gpr[12]) },
2721 { "r13", offsetof(CPUState, gpr[13]) },
2722 { "r14", offsetof(CPUState, gpr[14]) },
2723 { "r15", offsetof(CPUState, gpr[15]) },
2724 { "r16", offsetof(CPUState, gpr[16]) },
2725 { "r17", offsetof(CPUState, gpr[17]) },
2726 { "r18", offsetof(CPUState, gpr[18]) },
2727 { "r19", offsetof(CPUState, gpr[19]) },
2728 { "r20", offsetof(CPUState, gpr[20]) },
2729 { "r21", offsetof(CPUState, gpr[21]) },
2730 { "r22", offsetof(CPUState, gpr[22]) },
2731 { "r23", offsetof(CPUState, gpr[23]) },
2732 { "r24", offsetof(CPUState, gpr[24]) },
2733 { "r25", offsetof(CPUState, gpr[25]) },
2734 { "r26", offsetof(CPUState, gpr[26]) },
2735 { "r27", offsetof(CPUState, gpr[27]) },
2736 { "r28", offsetof(CPUState, gpr[28]) },
2737 { "r29", offsetof(CPUState, gpr[29]) },
2738 { "r30", offsetof(CPUState, gpr[30]) },
2739 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002740 /* Floating point registers */
2741 { "f0", offsetof(CPUState, fpr[0]) },
2742 { "f1", offsetof(CPUState, fpr[1]) },
2743 { "f2", offsetof(CPUState, fpr[2]) },
2744 { "f3", offsetof(CPUState, fpr[3]) },
2745 { "f4", offsetof(CPUState, fpr[4]) },
2746 { "f5", offsetof(CPUState, fpr[5]) },
2747 { "f6", offsetof(CPUState, fpr[6]) },
2748 { "f7", offsetof(CPUState, fpr[7]) },
2749 { "f8", offsetof(CPUState, fpr[8]) },
2750 { "f9", offsetof(CPUState, fpr[9]) },
2751 { "f10", offsetof(CPUState, fpr[10]) },
2752 { "f11", offsetof(CPUState, fpr[11]) },
2753 { "f12", offsetof(CPUState, fpr[12]) },
2754 { "f13", offsetof(CPUState, fpr[13]) },
2755 { "f14", offsetof(CPUState, fpr[14]) },
2756 { "f15", offsetof(CPUState, fpr[15]) },
2757 { "f16", offsetof(CPUState, fpr[16]) },
2758 { "f17", offsetof(CPUState, fpr[17]) },
2759 { "f18", offsetof(CPUState, fpr[18]) },
2760 { "f19", offsetof(CPUState, fpr[19]) },
2761 { "f20", offsetof(CPUState, fpr[20]) },
2762 { "f21", offsetof(CPUState, fpr[21]) },
2763 { "f22", offsetof(CPUState, fpr[22]) },
2764 { "f23", offsetof(CPUState, fpr[23]) },
2765 { "f24", offsetof(CPUState, fpr[24]) },
2766 { "f25", offsetof(CPUState, fpr[25]) },
2767 { "f26", offsetof(CPUState, fpr[26]) },
2768 { "f27", offsetof(CPUState, fpr[27]) },
2769 { "f28", offsetof(CPUState, fpr[28]) },
2770 { "f29", offsetof(CPUState, fpr[29]) },
2771 { "f30", offsetof(CPUState, fpr[30]) },
2772 { "f31", offsetof(CPUState, fpr[31]) },
2773 { "fpscr", offsetof(CPUState, fpscr) },
2774 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002775 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002776 { "lr", offsetof(CPUState, lr) },
2777 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002778 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002779 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002780 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002781 { "msr", 0, &monitor_get_msr, },
2782 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002783 { "tbu", 0, &monitor_get_tbu, },
2784 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002785#if defined(TARGET_PPC64)
2786 /* Address space register */
2787 { "asr", offsetof(CPUState, asr) },
2788#endif
2789 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002790 { "sdr1", offsetof(CPUState, sdr1) },
2791 { "sr0", offsetof(CPUState, sr[0]) },
2792 { "sr1", offsetof(CPUState, sr[1]) },
2793 { "sr2", offsetof(CPUState, sr[2]) },
2794 { "sr3", offsetof(CPUState, sr[3]) },
2795 { "sr4", offsetof(CPUState, sr[4]) },
2796 { "sr5", offsetof(CPUState, sr[5]) },
2797 { "sr6", offsetof(CPUState, sr[6]) },
2798 { "sr7", offsetof(CPUState, sr[7]) },
2799 { "sr8", offsetof(CPUState, sr[8]) },
2800 { "sr9", offsetof(CPUState, sr[9]) },
2801 { "sr10", offsetof(CPUState, sr[10]) },
2802 { "sr11", offsetof(CPUState, sr[11]) },
2803 { "sr12", offsetof(CPUState, sr[12]) },
2804 { "sr13", offsetof(CPUState, sr[13]) },
2805 { "sr14", offsetof(CPUState, sr[14]) },
2806 { "sr15", offsetof(CPUState, sr[15]) },
2807 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002808#elif defined(TARGET_SPARC)
2809 { "g0", offsetof(CPUState, gregs[0]) },
2810 { "g1", offsetof(CPUState, gregs[1]) },
2811 { "g2", offsetof(CPUState, gregs[2]) },
2812 { "g3", offsetof(CPUState, gregs[3]) },
2813 { "g4", offsetof(CPUState, gregs[4]) },
2814 { "g5", offsetof(CPUState, gregs[5]) },
2815 { "g6", offsetof(CPUState, gregs[6]) },
2816 { "g7", offsetof(CPUState, gregs[7]) },
2817 { "o0", 0, monitor_get_reg },
2818 { "o1", 1, monitor_get_reg },
2819 { "o2", 2, monitor_get_reg },
2820 { "o3", 3, monitor_get_reg },
2821 { "o4", 4, monitor_get_reg },
2822 { "o5", 5, monitor_get_reg },
2823 { "o6", 6, monitor_get_reg },
2824 { "o7", 7, monitor_get_reg },
2825 { "l0", 8, monitor_get_reg },
2826 { "l1", 9, monitor_get_reg },
2827 { "l2", 10, monitor_get_reg },
2828 { "l3", 11, monitor_get_reg },
2829 { "l4", 12, monitor_get_reg },
2830 { "l5", 13, monitor_get_reg },
2831 { "l6", 14, monitor_get_reg },
2832 { "l7", 15, monitor_get_reg },
2833 { "i0", 16, monitor_get_reg },
2834 { "i1", 17, monitor_get_reg },
2835 { "i2", 18, monitor_get_reg },
2836 { "i3", 19, monitor_get_reg },
2837 { "i4", 20, monitor_get_reg },
2838 { "i5", 21, monitor_get_reg },
2839 { "i6", 22, monitor_get_reg },
2840 { "i7", 23, monitor_get_reg },
2841 { "pc", offsetof(CPUState, pc) },
2842 { "npc", offsetof(CPUState, npc) },
2843 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002844#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002845 { "psr", 0, &monitor_get_psr, },
2846 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002847#endif
bellarde95c8d52004-09-30 22:22:08 +00002848 { "tbr", offsetof(CPUState, tbr) },
2849 { "fsr", offsetof(CPUState, fsr) },
2850 { "f0", offsetof(CPUState, fpr[0]) },
2851 { "f1", offsetof(CPUState, fpr[1]) },
2852 { "f2", offsetof(CPUState, fpr[2]) },
2853 { "f3", offsetof(CPUState, fpr[3]) },
2854 { "f4", offsetof(CPUState, fpr[4]) },
2855 { "f5", offsetof(CPUState, fpr[5]) },
2856 { "f6", offsetof(CPUState, fpr[6]) },
2857 { "f7", offsetof(CPUState, fpr[7]) },
2858 { "f8", offsetof(CPUState, fpr[8]) },
2859 { "f9", offsetof(CPUState, fpr[9]) },
2860 { "f10", offsetof(CPUState, fpr[10]) },
2861 { "f11", offsetof(CPUState, fpr[11]) },
2862 { "f12", offsetof(CPUState, fpr[12]) },
2863 { "f13", offsetof(CPUState, fpr[13]) },
2864 { "f14", offsetof(CPUState, fpr[14]) },
2865 { "f15", offsetof(CPUState, fpr[15]) },
2866 { "f16", offsetof(CPUState, fpr[16]) },
2867 { "f17", offsetof(CPUState, fpr[17]) },
2868 { "f18", offsetof(CPUState, fpr[18]) },
2869 { "f19", offsetof(CPUState, fpr[19]) },
2870 { "f20", offsetof(CPUState, fpr[20]) },
2871 { "f21", offsetof(CPUState, fpr[21]) },
2872 { "f22", offsetof(CPUState, fpr[22]) },
2873 { "f23", offsetof(CPUState, fpr[23]) },
2874 { "f24", offsetof(CPUState, fpr[24]) },
2875 { "f25", offsetof(CPUState, fpr[25]) },
2876 { "f26", offsetof(CPUState, fpr[26]) },
2877 { "f27", offsetof(CPUState, fpr[27]) },
2878 { "f28", offsetof(CPUState, fpr[28]) },
2879 { "f29", offsetof(CPUState, fpr[29]) },
2880 { "f30", offsetof(CPUState, fpr[30]) },
2881 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002882#ifdef TARGET_SPARC64
2883 { "f32", offsetof(CPUState, fpr[32]) },
2884 { "f34", offsetof(CPUState, fpr[34]) },
2885 { "f36", offsetof(CPUState, fpr[36]) },
2886 { "f38", offsetof(CPUState, fpr[38]) },
2887 { "f40", offsetof(CPUState, fpr[40]) },
2888 { "f42", offsetof(CPUState, fpr[42]) },
2889 { "f44", offsetof(CPUState, fpr[44]) },
2890 { "f46", offsetof(CPUState, fpr[46]) },
2891 { "f48", offsetof(CPUState, fpr[48]) },
2892 { "f50", offsetof(CPUState, fpr[50]) },
2893 { "f52", offsetof(CPUState, fpr[52]) },
2894 { "f54", offsetof(CPUState, fpr[54]) },
2895 { "f56", offsetof(CPUState, fpr[56]) },
2896 { "f58", offsetof(CPUState, fpr[58]) },
2897 { "f60", offsetof(CPUState, fpr[60]) },
2898 { "f62", offsetof(CPUState, fpr[62]) },
2899 { "asi", offsetof(CPUState, asi) },
2900 { "pstate", offsetof(CPUState, pstate) },
2901 { "cansave", offsetof(CPUState, cansave) },
2902 { "canrestore", offsetof(CPUState, canrestore) },
2903 { "otherwin", offsetof(CPUState, otherwin) },
2904 { "wstate", offsetof(CPUState, wstate) },
2905 { "cleanwin", offsetof(CPUState, cleanwin) },
2906 { "fprs", offsetof(CPUState, fprs) },
2907#endif
bellard9307c4c2004-04-04 12:57:25 +00002908#endif
2909 { NULL },
2910};
2911
aliguori376253e2009-03-05 23:01:23 +00002912static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002913{
aliguori376253e2009-03-05 23:01:23 +00002914 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002915 longjmp(expr_env, 1);
2916}
2917
Markus Armbruster09b94182010-01-20 13:07:30 +01002918/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002919static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002920{
blueswir18662d652008-10-02 18:32:44 +00002921 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002922 void *ptr;
2923
bellard9307c4c2004-04-04 12:57:25 +00002924 for(md = monitor_defs; md->name != NULL; md++) {
2925 if (compare_cmd(name, md->name)) {
2926 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002927 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002928 } else {
bellard6a00d602005-11-21 23:25:50 +00002929 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002930 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002931 switch(md->type) {
2932 case MD_I32:
2933 *pval = *(int32_t *)ptr;
2934 break;
2935 case MD_TLONG:
2936 *pval = *(target_long *)ptr;
2937 break;
2938 default:
2939 *pval = 0;
2940 break;
2941 }
bellard9307c4c2004-04-04 12:57:25 +00002942 }
2943 return 0;
2944 }
2945 }
2946 return -1;
2947}
2948
2949static void next(void)
2950{
Blue Swirl660f11b2009-07-31 21:16:51 +00002951 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002952 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002953 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002954 pch++;
2955 }
2956}
2957
aliguori376253e2009-03-05 23:01:23 +00002958static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002959
aliguori376253e2009-03-05 23:01:23 +00002960static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002961{
blueswir1c2efc952007-09-25 17:28:42 +00002962 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002963 char *p;
bellard6a00d602005-11-21 23:25:50 +00002964 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002965
2966 switch(*pch) {
2967 case '+':
2968 next();
aliguori376253e2009-03-05 23:01:23 +00002969 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002970 break;
2971 case '-':
2972 next();
aliguori376253e2009-03-05 23:01:23 +00002973 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002974 break;
2975 case '~':
2976 next();
aliguori376253e2009-03-05 23:01:23 +00002977 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002978 break;
2979 case '(':
2980 next();
aliguori376253e2009-03-05 23:01:23 +00002981 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002982 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002983 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002984 }
2985 next();
2986 break;
bellard81d09122004-07-14 17:21:37 +00002987 case '\'':
2988 pch++;
2989 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002990 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002991 n = *pch;
2992 pch++;
2993 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002994 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002995 next();
2996 break;
bellard9307c4c2004-04-04 12:57:25 +00002997 case '$':
2998 {
2999 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003000 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003001
bellard9307c4c2004-04-04 12:57:25 +00003002 pch++;
3003 q = buf;
3004 while ((*pch >= 'a' && *pch <= 'z') ||
3005 (*pch >= 'A' && *pch <= 'Z') ||
3006 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003007 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003008 if ((q - buf) < sizeof(buf) - 1)
3009 *q++ = *pch;
3010 pch++;
3011 }
blueswir1cd390082008-11-16 13:53:32 +00003012 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003013 pch++;
3014 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003015 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003016 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003017 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003018 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003019 }
3020 break;
3021 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003022 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003023 n = 0;
3024 break;
3025 default:
blueswir17743e582007-09-24 18:39:04 +00003026#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003027 n = strtoull(pch, &p, 0);
3028#else
bellard9307c4c2004-04-04 12:57:25 +00003029 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003030#endif
bellard9307c4c2004-04-04 12:57:25 +00003031 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003032 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003033 }
3034 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003035 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003036 pch++;
3037 break;
3038 }
3039 return n;
3040}
3041
3042
aliguori376253e2009-03-05 23:01:23 +00003043static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003044{
blueswir1c2efc952007-09-25 17:28:42 +00003045 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003046 int op;
ths3b46e622007-09-17 08:09:54 +00003047
aliguori376253e2009-03-05 23:01:23 +00003048 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003049 for(;;) {
3050 op = *pch;
3051 if (op != '*' && op != '/' && op != '%')
3052 break;
3053 next();
aliguori376253e2009-03-05 23:01:23 +00003054 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003055 switch(op) {
3056 default:
3057 case '*':
3058 val *= val2;
3059 break;
3060 case '/':
3061 case '%':
ths5fafdf22007-09-16 21:08:06 +00003062 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003063 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003064 if (op == '/')
3065 val /= val2;
3066 else
3067 val %= val2;
3068 break;
3069 }
3070 }
3071 return val;
3072}
3073
aliguori376253e2009-03-05 23:01:23 +00003074static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003075{
blueswir1c2efc952007-09-25 17:28:42 +00003076 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003077 int op;
bellard9307c4c2004-04-04 12:57:25 +00003078
aliguori376253e2009-03-05 23:01:23 +00003079 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003080 for(;;) {
3081 op = *pch;
3082 if (op != '&' && op != '|' && op != '^')
3083 break;
3084 next();
aliguori376253e2009-03-05 23:01:23 +00003085 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003086 switch(op) {
3087 default:
3088 case '&':
3089 val &= val2;
3090 break;
3091 case '|':
3092 val |= val2;
3093 break;
3094 case '^':
3095 val ^= val2;
3096 break;
3097 }
3098 }
3099 return val;
3100}
3101
aliguori376253e2009-03-05 23:01:23 +00003102static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003103{
blueswir1c2efc952007-09-25 17:28:42 +00003104 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003105 int op;
bellard9307c4c2004-04-04 12:57:25 +00003106
aliguori376253e2009-03-05 23:01:23 +00003107 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003108 for(;;) {
3109 op = *pch;
3110 if (op != '+' && op != '-')
3111 break;
3112 next();
aliguori376253e2009-03-05 23:01:23 +00003113 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003114 if (op == '+')
3115 val += val2;
3116 else
3117 val -= val2;
3118 }
3119 return val;
3120}
3121
aliguori376253e2009-03-05 23:01:23 +00003122static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003123{
3124 pch = *pp;
3125 if (setjmp(expr_env)) {
3126 *pp = pch;
3127 return -1;
3128 }
blueswir1cd390082008-11-16 13:53:32 +00003129 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003130 pch++;
aliguori376253e2009-03-05 23:01:23 +00003131 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003132 *pp = pch;
3133 return 0;
3134}
3135
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003136static int get_double(Monitor *mon, double *pval, const char **pp)
3137{
3138 const char *p = *pp;
3139 char *tailp;
3140 double d;
3141
3142 d = strtod(p, &tailp);
3143 if (tailp == p) {
3144 monitor_printf(mon, "Number expected\n");
3145 return -1;
3146 }
3147 if (d != d || d - d != 0) {
3148 /* NaN or infinity */
3149 monitor_printf(mon, "Bad number\n");
3150 return -1;
3151 }
3152 *pval = d;
3153 *pp = tailp;
3154 return 0;
3155}
3156
bellard9307c4c2004-04-04 12:57:25 +00003157static int get_str(char *buf, int buf_size, const char **pp)
3158{
3159 const char *p;
3160 char *q;
3161 int c;
3162
bellard81d09122004-07-14 17:21:37 +00003163 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003164 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003165 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003166 p++;
3167 if (*p == '\0') {
3168 fail:
bellard81d09122004-07-14 17:21:37 +00003169 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003170 *pp = p;
3171 return -1;
3172 }
bellard9307c4c2004-04-04 12:57:25 +00003173 if (*p == '\"') {
3174 p++;
3175 while (*p != '\0' && *p != '\"') {
3176 if (*p == '\\') {
3177 p++;
3178 c = *p++;
3179 switch(c) {
3180 case 'n':
3181 c = '\n';
3182 break;
3183 case 'r':
3184 c = '\r';
3185 break;
3186 case '\\':
3187 case '\'':
3188 case '\"':
3189 break;
3190 default:
3191 qemu_printf("unsupported escape code: '\\%c'\n", c);
3192 goto fail;
3193 }
3194 if ((q - buf) < buf_size - 1) {
3195 *q++ = c;
3196 }
3197 } else {
3198 if ((q - buf) < buf_size - 1) {
3199 *q++ = *p;
3200 }
3201 p++;
3202 }
3203 }
3204 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003205 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003206 goto fail;
3207 }
3208 p++;
3209 } else {
blueswir1cd390082008-11-16 13:53:32 +00003210 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003211 if ((q - buf) < buf_size - 1) {
3212 *q++ = *p;
3213 }
3214 p++;
3215 }
bellard9307c4c2004-04-04 12:57:25 +00003216 }
bellard81d09122004-07-14 17:21:37 +00003217 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003218 *pp = p;
3219 return 0;
3220}
3221
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003222/*
3223 * Store the command-name in cmdname, and return a pointer to
3224 * the remaining of the command string.
3225 */
3226static const char *get_command_name(const char *cmdline,
3227 char *cmdname, size_t nlen)
3228{
3229 size_t len;
3230 const char *p, *pstart;
3231
3232 p = cmdline;
3233 while (qemu_isspace(*p))
3234 p++;
3235 if (*p == '\0')
3236 return NULL;
3237 pstart = p;
3238 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3239 p++;
3240 len = p - pstart;
3241 if (len > nlen - 1)
3242 len = nlen - 1;
3243 memcpy(cmdname, pstart, len);
3244 cmdname[len] = '\0';
3245 return p;
3246}
3247
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003248/**
3249 * Read key of 'type' into 'key' and return the current
3250 * 'type' pointer.
3251 */
3252static char *key_get_info(const char *type, char **key)
3253{
3254 size_t len;
3255 char *p, *str;
3256
3257 if (*type == ',')
3258 type++;
3259
3260 p = strchr(type, ':');
3261 if (!p) {
3262 *key = NULL;
3263 return NULL;
3264 }
3265 len = p - type;
3266
3267 str = qemu_malloc(len + 1);
3268 memcpy(str, type, len);
3269 str[len] = '\0';
3270
3271 *key = str;
3272 return ++p;
3273}
3274
bellard9307c4c2004-04-04 12:57:25 +00003275static int default_fmt_format = 'x';
3276static int default_fmt_size = 4;
3277
3278#define MAX_ARGS 16
3279
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003280static int is_valid_option(const char *c, const char *typestr)
3281{
3282 char option[3];
3283
3284 option[0] = '-';
3285 option[1] = *c;
3286 option[2] = '\0';
3287
3288 typestr = strstr(typestr, option);
3289 return (typestr != NULL);
3290}
3291
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003292static const mon_cmd_t *monitor_find_command(const char *cmdname)
3293{
3294 const mon_cmd_t *cmd;
3295
3296 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3297 if (compare_cmd(cmdname, cmd->name)) {
3298 return cmd;
3299 }
3300 }
3301
3302 return NULL;
3303}
3304
Anthony Liguoric227f092009-10-01 16:12:16 -05003305static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003306 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003307 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003308{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003309 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003310 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003311 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003312 char cmdname[256];
3313 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003314 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003315
3316#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003317 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003318#endif
ths3b46e622007-09-17 08:09:54 +00003319
bellard9307c4c2004-04-04 12:57:25 +00003320 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003321 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3322 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003323 return NULL;
ths3b46e622007-09-17 08:09:54 +00003324
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003325 cmd = monitor_find_command(cmdname);
3326 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003327 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003328 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003329 }
bellard9307c4c2004-04-04 12:57:25 +00003330
bellard9307c4c2004-04-04 12:57:25 +00003331 /* parse the parameters */
3332 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003333 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003334 typestr = key_get_info(typestr, &key);
3335 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003336 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003337 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003338 typestr++;
3339 switch(c) {
3340 case 'F':
bellard81d09122004-07-14 17:21:37 +00003341 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003342 case 's':
3343 {
3344 int ret;
ths3b46e622007-09-17 08:09:54 +00003345
blueswir1cd390082008-11-16 13:53:32 +00003346 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003347 p++;
3348 if (*typestr == '?') {
3349 typestr++;
3350 if (*p == '\0') {
3351 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003352 break;
bellard9307c4c2004-04-04 12:57:25 +00003353 }
3354 }
3355 ret = get_str(buf, sizeof(buf), &p);
3356 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003357 switch(c) {
3358 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003359 monitor_printf(mon, "%s: filename expected\n",
3360 cmdname);
bellard81d09122004-07-14 17:21:37 +00003361 break;
3362 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003363 monitor_printf(mon, "%s: block device name expected\n",
3364 cmdname);
bellard81d09122004-07-14 17:21:37 +00003365 break;
3366 default:
aliguori376253e2009-03-05 23:01:23 +00003367 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003368 break;
3369 }
bellard9307c4c2004-04-04 12:57:25 +00003370 goto fail;
3371 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003372 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003373 }
3374 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003375 case 'O':
3376 {
3377 QemuOptsList *opts_list;
3378 QemuOpts *opts;
3379
3380 opts_list = qemu_find_opts(key);
3381 if (!opts_list || opts_list->desc->name) {
3382 goto bad_type;
3383 }
3384 while (qemu_isspace(*p)) {
3385 p++;
3386 }
3387 if (!*p)
3388 break;
3389 if (get_str(buf, sizeof(buf), &p) < 0) {
3390 goto fail;
3391 }
3392 opts = qemu_opts_parse(opts_list, buf, 1);
3393 if (!opts) {
3394 goto fail;
3395 }
3396 qemu_opts_to_qdict(opts, qdict);
3397 qemu_opts_del(opts);
3398 }
3399 break;
bellard9307c4c2004-04-04 12:57:25 +00003400 case '/':
3401 {
3402 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003403
blueswir1cd390082008-11-16 13:53:32 +00003404 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003405 p++;
3406 if (*p == '/') {
3407 /* format found */
3408 p++;
3409 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003410 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003411 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003412 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003413 count = count * 10 + (*p - '0');
3414 p++;
3415 }
3416 }
3417 size = -1;
3418 format = -1;
3419 for(;;) {
3420 switch(*p) {
3421 case 'o':
3422 case 'd':
3423 case 'u':
3424 case 'x':
3425 case 'i':
3426 case 'c':
3427 format = *p++;
3428 break;
3429 case 'b':
3430 size = 1;
3431 p++;
3432 break;
3433 case 'h':
3434 size = 2;
3435 p++;
3436 break;
3437 case 'w':
3438 size = 4;
3439 p++;
3440 break;
3441 case 'g':
3442 case 'L':
3443 size = 8;
3444 p++;
3445 break;
3446 default:
3447 goto next;
3448 }
3449 }
3450 next:
blueswir1cd390082008-11-16 13:53:32 +00003451 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003452 monitor_printf(mon, "invalid char in format: '%c'\n",
3453 *p);
bellard9307c4c2004-04-04 12:57:25 +00003454 goto fail;
3455 }
bellard9307c4c2004-04-04 12:57:25 +00003456 if (format < 0)
3457 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003458 if (format != 'i') {
3459 /* for 'i', not specifying a size gives -1 as size */
3460 if (size < 0)
3461 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003462 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003463 }
bellard9307c4c2004-04-04 12:57:25 +00003464 default_fmt_format = format;
3465 } else {
3466 count = 1;
3467 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003468 if (format != 'i') {
3469 size = default_fmt_size;
3470 } else {
3471 size = -1;
3472 }
bellard9307c4c2004-04-04 12:57:25 +00003473 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003474 qdict_put(qdict, "count", qint_from_int(count));
3475 qdict_put(qdict, "format", qint_from_int(format));
3476 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003477 }
3478 break;
3479 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003480 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003481 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003482 {
blueswir1c2efc952007-09-25 17:28:42 +00003483 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003484
blueswir1cd390082008-11-16 13:53:32 +00003485 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003486 p++;
bellard34405572004-06-08 00:55:58 +00003487 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003488 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003489 if (*p == '\0') {
3490 typestr++;
3491 break;
3492 }
bellard34405572004-06-08 00:55:58 +00003493 } else {
3494 if (*p == '.') {
3495 p++;
blueswir1cd390082008-11-16 13:53:32 +00003496 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003497 p++;
bellard34405572004-06-08 00:55:58 +00003498 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003499 typestr++;
3500 break;
bellard34405572004-06-08 00:55:58 +00003501 }
3502 }
bellard13224a82006-07-14 22:03:35 +00003503 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003504 }
aliguori376253e2009-03-05 23:01:23 +00003505 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003506 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003507 /* Check if 'i' is greater than 32-bit */
3508 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3509 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3510 monitor_printf(mon, "integer is for 32-bit values\n");
3511 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003512 } else if (c == 'M') {
3513 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003514 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003515 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003516 }
3517 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003518 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003519 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003520 {
3521 double val;
3522
3523 while (qemu_isspace(*p))
3524 p++;
3525 if (*typestr == '?') {
3526 typestr++;
3527 if (*p == '\0') {
3528 break;
3529 }
3530 }
3531 if (get_double(mon, &val, &p) < 0) {
3532 goto fail;
3533 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003534 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003535 switch (*p) {
3536 case 'K': case 'k':
3537 val *= 1 << 10; p++; break;
3538 case 'M': case 'm':
3539 val *= 1 << 20; p++; break;
3540 case 'G': case 'g':
3541 val *= 1 << 30; p++; break;
3542 }
3543 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003544 if (c == 'T' && p[0] && p[1] == 's') {
3545 switch (*p) {
3546 case 'm':
3547 val /= 1e3; p += 2; break;
3548 case 'u':
3549 val /= 1e6; p += 2; break;
3550 case 'n':
3551 val /= 1e9; p += 2; break;
3552 }
3553 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003554 if (*p && !qemu_isspace(*p)) {
3555 monitor_printf(mon, "Unknown unit suffix\n");
3556 goto fail;
3557 }
3558 qdict_put(qdict, key, qfloat_from_double(val));
3559 }
3560 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003561 case 'b':
3562 {
3563 const char *beg;
3564 int val;
3565
3566 while (qemu_isspace(*p)) {
3567 p++;
3568 }
3569 beg = p;
3570 while (qemu_isgraph(*p)) {
3571 p++;
3572 }
3573 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3574 val = 1;
3575 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3576 val = 0;
3577 } else {
3578 monitor_printf(mon, "Expected 'on' or 'off'\n");
3579 goto fail;
3580 }
3581 qdict_put(qdict, key, qbool_from_int(val));
3582 }
3583 break;
bellard9307c4c2004-04-04 12:57:25 +00003584 case '-':
3585 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003586 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003587 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003588 /* option */
ths3b46e622007-09-17 08:09:54 +00003589
bellard9307c4c2004-04-04 12:57:25 +00003590 c = *typestr++;
3591 if (c == '\0')
3592 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003593 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003594 p++;
bellard9307c4c2004-04-04 12:57:25 +00003595 if (*p == '-') {
3596 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003597 if(c != *p) {
3598 if(!is_valid_option(p, typestr)) {
3599
3600 monitor_printf(mon, "%s: unsupported option -%c\n",
3601 cmdname, *p);
3602 goto fail;
3603 } else {
3604 skip_key = 1;
3605 }
bellard9307c4c2004-04-04 12:57:25 +00003606 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003607 if(skip_key) {
3608 p = tmp;
3609 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003610 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003611 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003612 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003613 }
bellard9307c4c2004-04-04 12:57:25 +00003614 }
bellard9307c4c2004-04-04 12:57:25 +00003615 }
3616 break;
3617 default:
3618 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003619 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003620 goto fail;
3621 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003622 qemu_free(key);
3623 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003624 }
3625 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003626 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003627 p++;
3628 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003629 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3630 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003631 goto fail;
3632 }
3633
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003634 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003635
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003636fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003637 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003638 return NULL;
3639}
3640
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003641void monitor_set_error(Monitor *mon, QError *qerror)
3642{
3643 /* report only the first error */
3644 if (!mon->error) {
3645 mon->error = qerror;
3646 } else {
3647 MON_DEBUG("Additional error report at %s:%d\n",
3648 qerror->file, qerror->linenr);
3649 QDECREF(qerror);
3650 }
3651}
3652
Adam Litke940cc302010-01-25 12:18:44 -06003653static int is_async_return(const QObject *data)
3654{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003655 if (data && qobject_type(data) == QTYPE_QDICT) {
3656 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3657 }
3658
3659 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003660}
3661
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003662static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3663{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003664 if (monitor_ctrl_mode(mon)) {
3665 if (ret && !monitor_has_error(mon)) {
3666 /*
3667 * If it returns failure, it must have passed on error.
3668 *
3669 * Action: Report an internal error to the client if in QMP.
3670 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003671 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003672 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3673 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003674 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003675
3676#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003677 if (!ret && monitor_has_error(mon)) {
3678 /*
3679 * If it returns success, it must not have passed an error.
3680 *
3681 * Action: Report the passed error to the client.
3682 */
3683 MON_DEBUG("command '%s' returned success but passed an error\n",
3684 cmd->name);
3685 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003686
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003687 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3688 /*
3689 * Handlers should not call Monitor print functions.
3690 *
3691 * Action: Ignore them in QMP.
3692 *
3693 * (XXX: we don't check any 'info' or 'query' command here
3694 * because the user print function _is_ called by do_info(), hence
3695 * we will trigger this check. This problem will go away when we
3696 * make 'query' commands real and kill do_info())
3697 */
3698 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3699 cmd->name, mon_print_count_get(mon));
3700 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003701#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003702 } else {
3703 assert(!monitor_has_error(mon));
3704 QDECREF(mon->error);
3705 mon->error = NULL;
3706 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003707}
3708
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003709static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3710 const QDict *params)
3711{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003712 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003713 QObject *data = NULL;
3714
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003715 mon_print_count_init(mon);
3716
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003717 ret = cmd->mhandler.cmd_new(mon, params, &data);
3718 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003719
Adam Litke940cc302010-01-25 12:18:44 -06003720 if (is_async_return(data)) {
3721 /*
3722 * Asynchronous commands have no initial return data but they can
3723 * generate errors. Data is returned via the async completion handler.
3724 */
3725 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3726 monitor_protocol_emitter(mon, NULL);
3727 }
3728 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003729 /* Monitor Protocol */
3730 monitor_protocol_emitter(mon, data);
3731 } else {
3732 /* User Protocol */
3733 if (data)
3734 cmd->user_print(mon, data);
3735 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003736
3737 qobject_decref(data);
3738}
3739
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003740static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003741{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003742 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003743 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003744
3745 qdict = qdict_new();
3746
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003747 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003748 if (!cmd)
3749 goto out;
3750
Adam Litke940cc302010-01-25 12:18:44 -06003751 if (monitor_handler_is_async(cmd)) {
3752 user_async_cmd_handler(mon, cmd, qdict);
3753 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003754 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003755 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003756 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003757 }
3758
Luiz Capitulino13917be2009-10-07 13:41:54 -03003759out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003760 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003761}
3762
bellard81d09122004-07-14 17:21:37 +00003763static void cmd_completion(const char *name, const char *list)
3764{
3765 const char *p, *pstart;
3766 char cmd[128];
3767 int len;
3768
3769 p = list;
3770 for(;;) {
3771 pstart = p;
3772 p = strchr(p, '|');
3773 if (!p)
3774 p = pstart + strlen(pstart);
3775 len = p - pstart;
3776 if (len > sizeof(cmd) - 2)
3777 len = sizeof(cmd) - 2;
3778 memcpy(cmd, pstart, len);
3779 cmd[len] = '\0';
3780 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003781 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003782 }
3783 if (*p == '\0')
3784 break;
3785 p++;
3786 }
3787}
3788
3789static void file_completion(const char *input)
3790{
3791 DIR *ffs;
3792 struct dirent *d;
3793 char path[1024];
3794 char file[1024], file_prefix[1024];
3795 int input_path_len;
3796 const char *p;
3797
ths5fafdf22007-09-16 21:08:06 +00003798 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003799 if (!p) {
3800 input_path_len = 0;
3801 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003802 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003803 } else {
3804 input_path_len = p - input + 1;
3805 memcpy(path, input, input_path_len);
3806 if (input_path_len > sizeof(path) - 1)
3807 input_path_len = sizeof(path) - 1;
3808 path[input_path_len] = '\0';
3809 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3810 }
3811#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003812 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3813 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003814#endif
3815 ffs = opendir(path);
3816 if (!ffs)
3817 return;
3818 for(;;) {
3819 struct stat sb;
3820 d = readdir(ffs);
3821 if (!d)
3822 break;
3823 if (strstart(d->d_name, file_prefix, NULL)) {
3824 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003825 if (input_path_len < sizeof(file))
3826 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3827 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003828 /* stat the file to find out if it's a directory.
3829 * In that case add a slash to speed up typing long paths
3830 */
3831 stat(file, &sb);
3832 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003833 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003834 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003835 }
3836 }
3837 closedir(ffs);
3838}
3839
aliguori51de9762009-03-05 23:00:43 +00003840static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003841{
aliguori51de9762009-03-05 23:00:43 +00003842 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003843 const char *input = opaque;
3844
3845 if (input[0] == '\0' ||
3846 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003847 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003848 }
3849}
3850
3851/* NOTE: this parser is an approximate form of the real command parser */
3852static void parse_cmdline(const char *cmdline,
3853 int *pnb_args, char **args)
3854{
3855 const char *p;
3856 int nb_args, ret;
3857 char buf[1024];
3858
3859 p = cmdline;
3860 nb_args = 0;
3861 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003862 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003863 p++;
3864 if (*p == '\0')
3865 break;
3866 if (nb_args >= MAX_ARGS)
3867 break;
3868 ret = get_str(buf, sizeof(buf), &p);
3869 args[nb_args] = qemu_strdup(buf);
3870 nb_args++;
3871 if (ret < 0)
3872 break;
3873 }
3874 *pnb_args = nb_args;
3875}
3876
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003877static const char *next_arg_type(const char *typestr)
3878{
3879 const char *p = strchr(typestr, ':');
3880 return (p != NULL ? ++p : typestr);
3881}
3882
aliguori4c36ba32009-03-05 23:01:37 +00003883static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003884{
3885 const char *cmdname;
3886 char *args[MAX_ARGS];
3887 int nb_args, i, len;
3888 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003889 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003890 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003891
3892 parse_cmdline(cmdline, &nb_args, args);
3893#ifdef DEBUG_COMPLETION
3894 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003895 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003896 }
3897#endif
3898
3899 /* if the line ends with a space, it means we want to complete the
3900 next arg */
3901 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003902 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003903 if (nb_args >= MAX_ARGS) {
3904 goto cleanup;
3905 }
bellard81d09122004-07-14 17:21:37 +00003906 args[nb_args++] = qemu_strdup("");
3907 }
3908 if (nb_args <= 1) {
3909 /* command completion */
3910 if (nb_args == 0)
3911 cmdname = "";
3912 else
3913 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003914 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003915 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003916 cmd_completion(cmdname, cmd->name);
3917 }
3918 } else {
3919 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02003920 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3921 if (compare_cmd(args[0], cmd->name)) {
3922 break;
3923 }
bellard81d09122004-07-14 17:21:37 +00003924 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003925 if (!cmd->name) {
3926 goto cleanup;
3927 }
3928
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003929 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003930 for(i = 0; i < nb_args - 2; i++) {
3931 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003932 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003933 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003934 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003935 }
3936 }
3937 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003938 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003939 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003940 }
bellard81d09122004-07-14 17:21:37 +00003941 switch(*ptype) {
3942 case 'F':
3943 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003944 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003945 file_completion(str);
3946 break;
3947 case 'B':
3948 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003949 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003950 bdrv_iterate(block_completion_it, (void *)str);
3951 break;
bellard7fe48482004-10-09 18:08:01 +00003952 case 's':
3953 /* XXX: more generic ? */
3954 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003955 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003956 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3957 cmd_completion(str, cmd->name);
3958 }
bellard64866c32006-05-07 18:03:31 +00003959 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003960 char *sep = strrchr(str, '-');
3961 if (sep)
3962 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003963 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003964 for(key = key_defs; key->name != NULL; key++) {
3965 cmd_completion(str, key->name);
3966 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003967 } else if (!strcmp(cmd->name, "help|?")) {
3968 readline_set_completion_index(cur_mon->rs, strlen(str));
3969 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3970 cmd_completion(str, cmd->name);
3971 }
bellard7fe48482004-10-09 18:08:01 +00003972 }
3973 break;
bellard81d09122004-07-14 17:21:37 +00003974 default:
3975 break;
3976 }
3977 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003978
3979cleanup:
3980 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00003981 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02003982 }
bellard81d09122004-07-14 17:21:37 +00003983}
3984
aliguori731b0362009-03-05 23:01:42 +00003985static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003986{
aliguori731b0362009-03-05 23:01:42 +00003987 Monitor *mon = opaque;
3988
Jan Kiszkac62313b2009-12-04 14:05:29 +01003989 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003990}
3991
Luiz Capitulino09069b12010-02-04 18:10:06 -02003992static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
3993{
3994 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
3995 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
3996}
3997
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003998/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003999 * Argument validation rules:
4000 *
4001 * 1. The argument must exist in cmd_args qdict
4002 * 2. The argument type must be the expected one
4003 *
4004 * Special case: If the argument doesn't exist in cmd_args and
4005 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4006 * checking is skipped for it.
4007 */
4008static int check_client_args_type(const QDict *client_args,
4009 const QDict *cmd_args, int flags)
4010{
4011 const QDictEntry *ent;
4012
4013 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4014 QObject *obj;
4015 QString *arg_type;
4016 const QObject *client_arg = qdict_entry_value(ent);
4017 const char *client_arg_name = qdict_entry_key(ent);
4018
4019 obj = qdict_get(cmd_args, client_arg_name);
4020 if (!obj) {
4021 if (flags & QMP_ACCEPT_UNKNOWNS) {
4022 /* handler accepts unknowns */
4023 continue;
4024 }
4025 /* client arg doesn't exist */
4026 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4027 return -1;
4028 }
4029
4030 arg_type = qobject_to_qstring(obj);
4031 assert(arg_type != NULL);
4032
4033 /* check if argument's type is correct */
4034 switch (qstring_get_str(arg_type)[0]) {
4035 case 'F':
4036 case 'B':
4037 case 's':
4038 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4039 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4040 "string");
4041 return -1;
4042 }
4043 break;
4044 case 'i':
4045 case 'l':
4046 case 'M':
4047 if (qobject_type(client_arg) != QTYPE_QINT) {
4048 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4049 "int");
4050 return -1;
4051 }
4052 break;
4053 case 'f':
4054 case 'T':
4055 if (qobject_type(client_arg) != QTYPE_QINT &&
4056 qobject_type(client_arg) != QTYPE_QFLOAT) {
4057 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4058 "number");
4059 return -1;
4060 }
4061 break;
4062 case 'b':
4063 case '-':
4064 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4065 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4066 "bool");
4067 return -1;
4068 }
4069 break;
4070 case 'O':
4071 assert(flags & QMP_ACCEPT_UNKNOWNS);
4072 break;
4073 case '/':
4074 case '.':
4075 /*
4076 * These types are not supported by QMP and thus are not
4077 * handled here. Fall through.
4078 */
4079 default:
4080 abort();
4081 }
4082 }
4083
4084 return 0;
4085}
4086
4087/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004088 * - Check if the client has passed all mandatory args
4089 * - Set special flags for argument validation
4090 */
4091static int check_mandatory_args(const QDict *cmd_args,
4092 const QDict *client_args, int *flags)
4093{
4094 const QDictEntry *ent;
4095
4096 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4097 const char *cmd_arg_name = qdict_entry_key(ent);
4098 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4099 assert(type != NULL);
4100
4101 if (qstring_get_str(type)[0] == 'O') {
4102 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4103 *flags |= QMP_ACCEPT_UNKNOWNS;
4104 } else if (qstring_get_str(type)[0] != '-' &&
4105 qstring_get_str(type)[1] != '?' &&
4106 !qdict_haskey(client_args, cmd_arg_name)) {
4107 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4108 return -1;
4109 }
4110 }
4111
4112 return 0;
4113}
4114
4115static QDict *qdict_from_args_type(const char *args_type)
4116{
4117 int i;
4118 QDict *qdict;
4119 QString *key, *type, *cur_qs;
4120
4121 assert(args_type != NULL);
4122
4123 qdict = qdict_new();
4124
4125 if (args_type == NULL || args_type[0] == '\0') {
4126 /* no args, empty qdict */
4127 goto out;
4128 }
4129
4130 key = qstring_new();
4131 type = qstring_new();
4132
4133 cur_qs = key;
4134
4135 for (i = 0;; i++) {
4136 switch (args_type[i]) {
4137 case ',':
4138 case '\0':
4139 qdict_put(qdict, qstring_get_str(key), type);
4140 QDECREF(key);
4141 if (args_type[i] == '\0') {
4142 goto out;
4143 }
4144 type = qstring_new(); /* qdict has ref */
4145 cur_qs = key = qstring_new();
4146 break;
4147 case ':':
4148 cur_qs = type;
4149 break;
4150 default:
4151 qstring_append_chr(cur_qs, args_type[i]);
4152 break;
4153 }
4154 }
4155
4156out:
4157 return qdict;
4158}
4159
4160/*
4161 * Client argument checking rules:
4162 *
4163 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004164 * 2. Each argument provided by the client must be expected
4165 * 3. Each argument provided by the client must have the type expected
4166 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004167 */
4168static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4169{
4170 int flags, err;
4171 QDict *cmd_args;
4172
4173 cmd_args = qdict_from_args_type(cmd->args_type);
4174
4175 flags = 0;
4176 err = check_mandatory_args(cmd_args, client_args, &flags);
4177 if (err) {
4178 goto out;
4179 }
4180
Luiz Capitulino4af91932010-06-22 11:44:05 -03004181 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004182
4183out:
4184 QDECREF(cmd_args);
4185 return err;
4186}
4187
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004188/*
4189 * Input object checking rules
4190 *
4191 * 1. Input object must be a dict
4192 * 2. The "execute" key must exist
4193 * 3. The "execute" key must be a string
4194 * 4. If the "arguments" key exists, it must be a dict
4195 * 5. If the "id" key exists, it can be anything (ie. json-value)
4196 * 6. Any argument not listed above is considered invalid
4197 */
4198static QDict *qmp_check_input_obj(QObject *input_obj)
4199{
4200 const QDictEntry *ent;
4201 int has_exec_key = 0;
4202 QDict *input_dict;
4203
4204 if (qobject_type(input_obj) != QTYPE_QDICT) {
4205 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4206 return NULL;
4207 }
4208
4209 input_dict = qobject_to_qdict(input_obj);
4210
4211 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4212 const char *arg_name = qdict_entry_key(ent);
4213 const QObject *arg_obj = qdict_entry_value(ent);
4214
4215 if (!strcmp(arg_name, "execute")) {
4216 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4217 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4218 "string");
4219 return NULL;
4220 }
4221 has_exec_key = 1;
4222 } else if (!strcmp(arg_name, "arguments")) {
4223 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4224 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4225 "object");
4226 return NULL;
4227 }
4228 } else if (!strcmp(arg_name, "id")) {
4229 /* FIXME: check duplicated IDs for async commands */
4230 } else {
4231 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4232 return NULL;
4233 }
4234 }
4235
4236 if (!has_exec_key) {
4237 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4238 return NULL;
4239 }
4240
4241 return input_dict;
4242}
4243
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004244static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4245{
4246 int err;
4247 QObject *obj;
4248 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004249 const mon_cmd_t *cmd;
4250 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004251 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004252
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004253 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004254
4255 obj = json_parser_parse(tokens, NULL);
4256 if (!obj) {
4257 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004258 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004259 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004260 }
4261
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004262 input = qmp_check_input_obj(obj);
4263 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004264 qobject_decref(obj);
4265 goto err_out;
4266 }
4267
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004268 mon->mc->id = qdict_get(input, "id");
4269 qobject_incref(mon->mc->id);
4270
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004271 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004272 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004273 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004274 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004275 }
4276
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004277 /*
4278 * XXX: We need this special case until we get info handlers
4279 * converted into 'query-' commands
4280 */
4281 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004282 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004283 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004284 } else if (strstart(cmd_name, "query-", &info_item)) {
4285 cmd = monitor_find_command("info");
4286 qdict_put_obj(input, "arguments",
4287 qobject_from_jsonf("{ 'item': %s }", info_item));
4288 } else {
4289 cmd = monitor_find_command(cmd_name);
Jan Kiszkaa6c4d362010-06-28 18:27:47 +02004290 if (!cmd || !monitor_handler_ported(cmd)
4291 || monitor_cmd_user_only(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004292 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004293 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004294 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004295 }
4296
4297 obj = qdict_get(input, "arguments");
4298 if (!obj) {
4299 args = qdict_new();
4300 } else {
4301 args = qobject_to_qdict(obj);
4302 QINCREF(args);
4303 }
4304
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004305 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004306 if (err < 0) {
4307 goto err_out;
4308 }
4309
Adam Litke940cc302010-01-25 12:18:44 -06004310 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004311 err = qmp_async_cmd_handler(mon, cmd, args);
4312 if (err) {
4313 /* emit the error response */
4314 goto err_out;
4315 }
Adam Litke940cc302010-01-25 12:18:44 -06004316 } else {
4317 monitor_call_handler(mon, cmd, args);
4318 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004319
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004320 goto out;
4321
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004322err_out:
4323 monitor_protocol_emitter(mon, NULL);
4324out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004325 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004326 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004327}
4328
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004329/**
4330 * monitor_control_read(): Read and handle QMP input
4331 */
4332static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4333{
4334 Monitor *old_mon = cur_mon;
4335
4336 cur_mon = opaque;
4337
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004338 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004339
4340 cur_mon = old_mon;
4341}
4342
aliguori731b0362009-03-05 23:01:42 +00004343static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004344{
aliguori731b0362009-03-05 23:01:42 +00004345 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004346 int i;
aliguori376253e2009-03-05 23:01:23 +00004347
aliguori731b0362009-03-05 23:01:42 +00004348 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004349
aliguoricde76ee2009-03-05 23:01:51 +00004350 if (cur_mon->rs) {
4351 for (i = 0; i < size; i++)
4352 readline_handle_byte(cur_mon->rs, buf[i]);
4353 } else {
4354 if (size == 0 || buf[size - 1] != 0)
4355 monitor_printf(cur_mon, "corrupted command\n");
4356 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004357 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004358 }
aliguori731b0362009-03-05 23:01:42 +00004359
4360 cur_mon = old_mon;
4361}
aliguorid8f44602008-10-06 13:52:44 +00004362
aliguori376253e2009-03-05 23:01:23 +00004363static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004364{
aliguori731b0362009-03-05 23:01:42 +00004365 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004366 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004367 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004368}
4369
aliguoricde76ee2009-03-05 23:01:51 +00004370int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004371{
aliguoricde76ee2009-03-05 23:01:51 +00004372 if (!mon->rs)
4373 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004374 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004375 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004376}
4377
aliguori376253e2009-03-05 23:01:23 +00004378void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004379{
aliguoricde76ee2009-03-05 23:01:51 +00004380 if (!mon->rs)
4381 return;
aliguori731b0362009-03-05 23:01:42 +00004382 if (--mon->suspend_cnt == 0)
4383 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004384}
4385
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004386static QObject *get_qmp_greeting(void)
4387{
4388 QObject *ver;
4389
4390 do_info_version(NULL, &ver);
4391 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4392}
4393
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004394/**
4395 * monitor_control_event(): Print QMP gretting
4396 */
4397static void monitor_control_event(void *opaque, int event)
4398{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004399 QObject *data;
4400 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004401
Luiz Capitulino47116d12010-02-08 17:01:30 -02004402 switch (event) {
4403 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004404 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004405 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004406 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004407 monitor_json_emitter(mon, data);
4408 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004409 break;
4410 case CHR_EVENT_CLOSED:
4411 json_message_parser_destroy(&mon->mc->parser);
4412 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004413 }
4414}
4415
aliguori731b0362009-03-05 23:01:42 +00004416static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004417{
aliguori376253e2009-03-05 23:01:23 +00004418 Monitor *mon = opaque;
4419
aliguori2724b182009-03-05 23:01:47 +00004420 switch (event) {
4421 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004422 mon->mux_out = 0;
4423 if (mon->reset_seen) {
4424 readline_restart(mon->rs);
4425 monitor_resume(mon);
4426 monitor_flush(mon);
4427 } else {
4428 mon->suspend_cnt = 0;
4429 }
aliguori2724b182009-03-05 23:01:47 +00004430 break;
ths86e94de2007-01-05 22:01:59 +00004431
aliguori2724b182009-03-05 23:01:47 +00004432 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004433 if (mon->reset_seen) {
4434 if (mon->suspend_cnt == 0) {
4435 monitor_printf(mon, "\n");
4436 }
4437 monitor_flush(mon);
4438 monitor_suspend(mon);
4439 } else {
4440 mon->suspend_cnt++;
4441 }
4442 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004443 break;
4444
Amit Shahb6b8df52009-10-07 18:31:16 +05304445 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004446 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4447 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004448 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004449 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004450 }
4451 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004452 break;
4453 }
ths86e94de2007-01-05 22:01:59 +00004454}
4455
aliguori76655d62009-03-06 20:27:37 +00004456
4457/*
4458 * Local variables:
4459 * c-indent-level: 4
4460 * c-basic-offset: 4
4461 * tab-width: 8
4462 * End:
4463 */
4464
aliguori731b0362009-03-05 23:01:42 +00004465void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004466{
aliguori731b0362009-03-05 23:01:42 +00004467 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004468 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004469
4470 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004471 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004472 is_first_init = 0;
4473 }
aliguori87127162009-03-05 23:01:29 +00004474
4475 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004476
aliguori87127162009-03-05 23:01:29 +00004477 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004478 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004479 if (flags & MONITOR_USE_READLINE) {
4480 mon->rs = readline_init(mon, monitor_find_completion);
4481 monitor_read_command(mon, 0);
4482 }
aliguori87127162009-03-05 23:01:29 +00004483
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004484 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004485 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004486 /* Control mode requires special handlers */
4487 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4488 monitor_control_event, mon);
4489 } else {
4490 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4491 monitor_event, mon);
4492 }
aliguori87127162009-03-05 23:01:29 +00004493
Blue Swirl72cf2d42009-09-12 07:36:22 +00004494 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004495 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4496 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004497}
4498
aliguori376253e2009-03-05 23:01:23 +00004499static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004500{
aliguoribb5fc202009-03-05 23:01:15 +00004501 BlockDriverState *bs = opaque;
4502 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004503
aliguoribb5fc202009-03-05 23:01:15 +00004504 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004505 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004506 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004507 }
aliguori731b0362009-03-05 23:01:42 +00004508 if (mon->password_completion_cb)
4509 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004510
aliguori731b0362009-03-05 23:01:42 +00004511 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004512}
aliguoric0f4ce72009-03-05 23:01:01 +00004513
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004514int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4515 BlockDriverCompletionFunc *completion_cb,
4516 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004517{
aliguoricde76ee2009-03-05 23:01:51 +00004518 int err;
4519
aliguoribb5fc202009-03-05 23:01:15 +00004520 if (!bdrv_key_required(bs)) {
4521 if (completion_cb)
4522 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004523 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004524 }
aliguoric0f4ce72009-03-05 23:01:01 +00004525
Luiz Capitulino94171e12009-12-07 21:37:00 +01004526 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004527 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004528 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004529 }
4530
aliguori376253e2009-03-05 23:01:23 +00004531 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4532 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004533
aliguori731b0362009-03-05 23:01:42 +00004534 mon->password_completion_cb = completion_cb;
4535 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004536
aliguoricde76ee2009-03-05 23:01:51 +00004537 err = monitor_read_password(mon, bdrv_password_cb, bs);
4538
4539 if (err && completion_cb)
4540 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004541
4542 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004543}