blob: e27f8d8d3be500dfdc0adc215decffad3df2339a [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;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300672 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200673
674 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300675 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200676
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300677 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
678 qdict_get_int(qemu, "major"),
679 qdict_get_int(qemu, "minor"),
680 qdict_get_int(qemu, "micro"),
681 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200682}
683
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300684static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000685{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300686 const char *version = QEMU_VERSION;
687 int major = 0, minor = 0, micro = 0;
688 char *tmp;
689
690 major = strtol(version, &tmp, 10);
691 tmp++;
692 minor = strtol(tmp, &tmp, 10);
693 tmp++;
694 micro = strtol(tmp, &tmp, 10);
695
696 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
697 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000698}
699
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200700static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000701{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200702 QDict *qdict;
703
704 qdict = qobject_to_qdict(data);
705 if (qdict_size(qdict) == 0) {
706 return;
707 }
708
709 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
710}
711
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200712static void do_info_name(Monitor *mon, QObject **ret_data)
713{
714 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
715 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000716}
717
Luiz Capitulino1a728672009-12-10 17:15:56 -0200718static QObject *get_cmd_dict(const char *name)
719{
720 const char *p;
721
722 /* Remove '|' from some commands */
723 p = strchr(name, '|');
724 if (p) {
725 p++;
726 } else {
727 p = name;
728 }
729
730 return qobject_from_jsonf("{ 'name': %s }", p);
731}
732
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200733static void do_info_commands(Monitor *mon, QObject **ret_data)
734{
735 QList *cmd_list;
736 const mon_cmd_t *cmd;
737
738 cmd_list = qlist_new();
739
740 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200741 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
742 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200743 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200744 }
745 }
746
747 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200748 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200749 char buf[128];
750 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200751 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200752 }
753 }
754
755 *ret_data = QOBJECT(cmd_list);
756}
757
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200758static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000759{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200760 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
761}
762
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200763static void do_info_uuid(Monitor *mon, QObject **ret_data)
764{
765 char uuid[64];
766
767 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000768 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
769 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
770 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
771 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200772 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000773}
774
bellard6a00d602005-11-21 23:25:50 +0000775/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000776static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000777{
778 CPUState *env;
779
780 for(env = first_cpu; env != NULL; env = env->next_cpu) {
781 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000782 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000783 return 0;
784 }
785 }
786 return -1;
787}
788
pbrook9596ebb2007-11-18 01:44:38 +0000789static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000790{
aliguori731b0362009-03-05 23:01:42 +0000791 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000792 mon_set_cpu(0);
793 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300794 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000795 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000796}
797
aliguori376253e2009-03-05 23:01:23 +0000798static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000799{
bellard6a00d602005-11-21 23:25:50 +0000800 CPUState *env;
801 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000802#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000803 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000804 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000805#else
aliguori376253e2009-03-05 23:01:23 +0000806 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000807 0);
bellard9307c4c2004-04-04 12:57:25 +0000808#endif
809}
810
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300811static void print_cpu_iter(QObject *obj, void *opaque)
812{
813 QDict *cpu;
814 int active = ' ';
815 Monitor *mon = opaque;
816
817 assert(qobject_type(obj) == QTYPE_QDICT);
818 cpu = qobject_to_qdict(obj);
819
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200820 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300821 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200822 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300823
824 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
825
826#if defined(TARGET_I386)
827 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
828 (target_ulong) qdict_get_int(cpu, "pc"));
829#elif defined(TARGET_PPC)
830 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
831 (target_long) qdict_get_int(cpu, "nip"));
832#elif defined(TARGET_SPARC)
833 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
834 (target_long) qdict_get_int(cpu, "pc"));
835 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
836 (target_long) qdict_get_int(cpu, "npc"));
837#elif defined(TARGET_MIPS)
838 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "PC"));
840#endif
841
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200842 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300843 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200844 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300845
846 monitor_printf(mon, "\n");
847}
848
849static void monitor_print_cpus(Monitor *mon, const QObject *data)
850{
851 QList *cpu_list;
852
853 assert(qobject_type(data) == QTYPE_QLIST);
854 cpu_list = qobject_to_qlist(data);
855 qlist_iter(cpu_list, print_cpu_iter, mon);
856}
857
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300858static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000859{
860 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300861 QList *cpu_list;
862
863 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000864
865 /* just to set the default cpu if not already done */
866 mon_get_cpu();
867
868 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200869 QDict *cpu;
870 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300871
Avi Kivity4c0960c2009-08-17 23:19:53 +0300872 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300873
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200874 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
875 env->cpu_index, env == mon->mon_cpu,
876 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877
878 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879
bellard6a00d602005-11-21 23:25:50 +0000880#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000882#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300883 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000884#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300885 qdict_put(cpu, "pc", qint_from_int(env->pc));
886 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000887#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000889#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890
891 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000892 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300893
894 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000895}
896
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200897static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000898{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300899 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200900 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100901 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
902 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200903 return -1;
904 }
905 return 0;
bellard6a00d602005-11-21 23:25:50 +0000906}
907
aliguori376253e2009-03-05 23:01:23 +0000908static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000909{
aliguori376253e2009-03-05 23:01:23 +0000910 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000911}
912
aliguori376253e2009-03-05 23:01:23 +0000913static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000914{
915 int i;
bellard7e2515e2004-08-01 21:52:19 +0000916 const char *str;
ths3b46e622007-09-17 08:09:54 +0000917
aliguoricde76ee2009-03-05 23:01:51 +0000918 if (!mon->rs)
919 return;
bellard7e2515e2004-08-01 21:52:19 +0000920 i = 0;
921 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000922 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000923 if (!str)
924 break;
aliguori376253e2009-03-05 23:01:23 +0000925 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000926 i++;
bellardaa455482004-04-04 13:07:25 +0000927 }
928}
929
j_mayer76a66252007-03-07 08:32:30 +0000930#if defined(TARGET_PPC)
931/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000932static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000933{
934 CPUState *env;
935
936 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000937 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000938}
939#endif
940
Luiz Capitulinob223f352009-10-07 13:41:56 -0300941/**
942 * do_quit(): Quit QEMU execution
943 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200944static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000945{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300946 monitor_suspend(mon);
947 no_shutdown = 0;
948 qemu_system_shutdown_request();
949
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200950 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000951}
952
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200953static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000954{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200955 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100956 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200957 return -1;
958 }
aliguoribb5fc202009-03-05 23:01:15 +0000959
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200960 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100961}
962
963static void change_vnc_password_cb(Monitor *mon, const char *password,
964 void *opaque)
965{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100966 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000967 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000968}
969
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200970static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000971{
ths70848512007-08-25 01:37:05 +0000972 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000973 strcmp(target, "password") == 0) {
974 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000975 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000976 strncpy(password, arg, sizeof(password));
977 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200978 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000979 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200980 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000981 }
ths70848512007-08-25 01:37:05 +0000982 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200983 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100984 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200985 return -1;
986 }
ths70848512007-08-25 01:37:05 +0000987 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200988
989 return 0;
thse25a5822007-08-25 01:36:20 +0000990}
991
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100992/**
993 * do_change(): Change a removable medium, or VNC configuration
994 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200995static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000996{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300997 const char *device = qdict_get_str(qdict, "device");
998 const char *target = qdict_get_str(qdict, "target");
999 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001000 int ret;
1001
thse25a5822007-08-25 01:36:20 +00001002 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001003 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001004 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001005 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001006 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001007
1008 return ret;
thse25a5822007-08-25 01:36:20 +00001009}
1010
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001011static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001012{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001013 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001014 return 0;
bellard59a983b2004-03-17 23:17:16 +00001015}
1016
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001017static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001018{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001019 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001020}
1021
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001022static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001023{
1024 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001025 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001026
bellard9307c4c2004-04-04 12:57:25 +00001027 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001028 mask = 0;
1029 } else {
bellard9307c4c2004-04-04 12:57:25 +00001030 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001031 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001032 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001033 return;
1034 }
1035 }
1036 cpu_set_log(mask);
1037}
1038
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001039static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001040{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001041 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001042 if (!option || !strcmp(option, "on")) {
1043 singlestep = 1;
1044 } else if (!strcmp(option, "off")) {
1045 singlestep = 0;
1046 } else {
1047 monitor_printf(mon, "unexpected option %s\n", option);
1048 }
1049}
1050
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001051/**
1052 * do_stop(): Stop VM execution
1053 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001054static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001055{
1056 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001057 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001058}
1059
aliguoribb5fc202009-03-05 23:01:15 +00001060static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001061
aliguori376253e2009-03-05 23:01:23 +00001062struct bdrv_iterate_context {
1063 Monitor *mon;
1064 int err;
1065};
aliguoric0f4ce72009-03-05 23:01:01 +00001066
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001067/**
1068 * do_cont(): Resume emulation.
1069 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001070static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001071{
1072 struct bdrv_iterate_context context = { mon, 0 };
1073
Amit Shah8e848652010-07-27 15:49:19 +05301074 if (incoming_expected) {
1075 qerror_report(QERR_MIGRATION_EXPECTED);
1076 return -1;
1077 }
aliguori376253e2009-03-05 23:01:23 +00001078 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001079 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001080 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001081 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001082 return 0;
1083 } else {
1084 return -1;
1085 }
bellard8a7ddc32004-03-31 19:00:16 +00001086}
1087
aliguoribb5fc202009-03-05 23:01:15 +00001088static void bdrv_key_cb(void *opaque, int err)
1089{
aliguori376253e2009-03-05 23:01:23 +00001090 Monitor *mon = opaque;
1091
aliguoribb5fc202009-03-05 23:01:15 +00001092 /* another key was set successfully, retry to continue */
1093 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001094 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001095}
1096
1097static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1098{
aliguori376253e2009-03-05 23:01:23 +00001099 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001100
aliguori376253e2009-03-05 23:01:23 +00001101 if (!context->err && bdrv_key_required(bs)) {
1102 context->err = -EBUSY;
1103 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1104 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001105 }
1106}
1107
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001108static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001109{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001110 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001111 if (!device)
1112 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1113 if (gdbserver_start(device) < 0) {
1114 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1115 device);
1116 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001117 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001118 } else {
aliguori59030a82009-04-05 18:43:41 +00001119 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1120 device);
bellard8a7ddc32004-03-31 19:00:16 +00001121 }
1122}
1123
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001124static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001125{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001126 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001127 if (select_watchdog_action(action) == -1) {
1128 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1129 }
1130}
1131
aliguori376253e2009-03-05 23:01:23 +00001132static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001133{
aliguori376253e2009-03-05 23:01:23 +00001134 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001135 switch(c) {
1136 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001137 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001138 break;
1139 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001141 break;
1142 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001143 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001144 break;
1145 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001147 break;
1148 default:
1149 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001150 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001151 } else {
aliguori376253e2009-03-05 23:01:23 +00001152 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001153 }
1154 break;
1155 }
aliguori376253e2009-03-05 23:01:23 +00001156 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001157}
1158
aliguori376253e2009-03-05 23:01:23 +00001159static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001160 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001161{
bellard6a00d602005-11-21 23:25:50 +00001162 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001163 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001164 uint8_t buf[16];
1165 uint64_t v;
1166
1167 if (format == 'i') {
1168 int flags;
1169 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001170 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001171#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001172 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001173 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001174 } else if (wsize == 4) {
1175 flags = 0;
1176 } else {
bellard6a15fd12006-04-12 21:07:07 +00001177 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001178 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001179 if (env) {
1180#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001181 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001182 (env->segs[R_CS].flags & DESC_L_MASK))
1183 flags = 2;
1184 else
1185#endif
1186 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1187 flags = 1;
1188 }
bellard4c27ba22004-04-25 18:05:08 +00001189 }
1190#endif
aliguori376253e2009-03-05 23:01:23 +00001191 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001192 return;
1193 }
1194
1195 len = wsize * count;
1196 if (wsize == 1)
1197 line_size = 8;
1198 else
1199 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001200 max_digits = 0;
1201
1202 switch(format) {
1203 case 'o':
1204 max_digits = (wsize * 8 + 2) / 3;
1205 break;
1206 default:
1207 case 'x':
1208 max_digits = (wsize * 8) / 4;
1209 break;
1210 case 'u':
1211 case 'd':
1212 max_digits = (wsize * 8 * 10 + 32) / 33;
1213 break;
1214 case 'c':
1215 wsize = 1;
1216 break;
1217 }
1218
1219 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001220 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001222 else
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001224 l = len;
1225 if (l > line_size)
1226 l = line_size;
1227 if (is_physical) {
1228 cpu_physical_memory_rw(addr, buf, l, 0);
1229 } else {
bellard6a00d602005-11-21 23:25:50 +00001230 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001231 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001233 break;
1234 }
bellard9307c4c2004-04-04 12:57:25 +00001235 }
ths5fafdf22007-09-16 21:08:06 +00001236 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001237 while (i < l) {
1238 switch(wsize) {
1239 default:
1240 case 1:
1241 v = ldub_raw(buf + i);
1242 break;
1243 case 2:
1244 v = lduw_raw(buf + i);
1245 break;
1246 case 4:
bellard92a31b12005-02-10 22:00:52 +00001247 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001248 break;
1249 case 8:
1250 v = ldq_raw(buf + i);
1251 break;
1252 }
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001254 switch(format) {
1255 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001257 break;
1258 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001265 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001269 break;
1270 }
1271 i += wsize;
1272 }
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001274 addr += l;
1275 len -= l;
1276 }
1277}
1278
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001279static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001280{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281 int count = qdict_get_int(qdict, "count");
1282 int format = qdict_get_int(qdict, "format");
1283 int size = qdict_get_int(qdict, "size");
1284 target_long addr = qdict_get_int(qdict, "addr");
1285
aliguori376253e2009-03-05 23:01:23 +00001286 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001287}
1288
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001289static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001290{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001291 int count = qdict_get_int(qdict, "count");
1292 int format = qdict_get_int(qdict, "format");
1293 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001294 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001295
aliguori376253e2009-03-05 23:01:23 +00001296 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001297}
1298
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001299static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001300{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001301 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001302 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001303
blueswir17743e582007-09-24 18:39:04 +00001304#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001305 switch(format) {
1306 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001308 break;
1309 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001311 break;
1312 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 default:
1316 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001318 break;
1319 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001321 break;
1322 }
bellard92a31b12005-02-10 22:00:52 +00001323#else
1324 switch(format) {
1325 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001327 break;
1328 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001329 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001330 break;
1331 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001333 break;
1334 default:
1335 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001337 break;
1338 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001339 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001340 break;
1341 }
1342#endif
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001344}
1345
Luiz Capitulino98696222010-02-10 23:49:58 -02001346static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001347{
1348 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001349 uint32_t size = qdict_get_int(qdict, "size");
1350 const char *filename = qdict_get_str(qdict, "filename");
1351 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001352 uint32_t l;
1353 CPUState *env;
1354 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001355 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001356
1357 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001358
1359 f = fopen(filename, "wb");
1360 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001361 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001362 return -1;
bellardb371dc52007-01-03 15:20:39 +00001363 }
1364 while (size != 0) {
1365 l = sizeof(buf);
1366 if (l > size)
1367 l = size;
1368 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001369 if (fwrite(buf, 1, l, f) != l) {
1370 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1371 goto exit;
1372 }
bellardb371dc52007-01-03 15:20:39 +00001373 addr += l;
1374 size -= l;
1375 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001376
1377 ret = 0;
1378
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001379exit:
bellardb371dc52007-01-03 15:20:39 +00001380 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001381 return ret;
bellardb371dc52007-01-03 15:20:39 +00001382}
1383
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001384static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001385 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001386{
1387 FILE *f;
1388 uint32_t l;
1389 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001390 uint32_t size = qdict_get_int(qdict, "size");
1391 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001392 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001393 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001394
1395 f = fopen(filename, "wb");
1396 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001397 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001398 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001399 }
1400 while (size != 0) {
1401 l = sizeof(buf);
1402 if (l > size)
1403 l = size;
1404 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001405 if (fwrite(buf, 1, l, f) != l) {
1406 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1407 goto exit;
1408 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001409 fflush(f);
1410 addr += l;
1411 size -= l;
1412 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001413
1414 ret = 0;
1415
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001416exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001417 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001418 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001419}
1420
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001421static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001422{
1423 uint32_t addr;
1424 uint8_t buf[1];
1425 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001426 uint32_t start = qdict_get_int(qdict, "start");
1427 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001428
1429 sum = 0;
1430 for(addr = start; addr < (start + size); addr++) {
1431 cpu_physical_memory_rw(addr, buf, 1, 0);
1432 /* BSD sum algorithm ('sum' Unix command) */
1433 sum = (sum >> 1) | (sum << 15);
1434 sum += buf[0];
1435 }
aliguori376253e2009-03-05 23:01:23 +00001436 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001437}
1438
bellarda3a91a32004-06-04 11:06:21 +00001439typedef struct {
1440 int keycode;
1441 const char *name;
1442} KeyDef;
1443
1444static const KeyDef key_defs[] = {
1445 { 0x2a, "shift" },
1446 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001447
bellarda3a91a32004-06-04 11:06:21 +00001448 { 0x38, "alt" },
1449 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001450 { 0x64, "altgr" },
1451 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001452 { 0x1d, "ctrl" },
1453 { 0x9d, "ctrl_r" },
1454
1455 { 0xdd, "menu" },
1456
1457 { 0x01, "esc" },
1458
1459 { 0x02, "1" },
1460 { 0x03, "2" },
1461 { 0x04, "3" },
1462 { 0x05, "4" },
1463 { 0x06, "5" },
1464 { 0x07, "6" },
1465 { 0x08, "7" },
1466 { 0x09, "8" },
1467 { 0x0a, "9" },
1468 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001469 { 0x0c, "minus" },
1470 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001471 { 0x0e, "backspace" },
1472
1473 { 0x0f, "tab" },
1474 { 0x10, "q" },
1475 { 0x11, "w" },
1476 { 0x12, "e" },
1477 { 0x13, "r" },
1478 { 0x14, "t" },
1479 { 0x15, "y" },
1480 { 0x16, "u" },
1481 { 0x17, "i" },
1482 { 0x18, "o" },
1483 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001484 { 0x1a, "bracket_left" },
1485 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001486 { 0x1c, "ret" },
1487
1488 { 0x1e, "a" },
1489 { 0x1f, "s" },
1490 { 0x20, "d" },
1491 { 0x21, "f" },
1492 { 0x22, "g" },
1493 { 0x23, "h" },
1494 { 0x24, "j" },
1495 { 0x25, "k" },
1496 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001497 { 0x27, "semicolon" },
1498 { 0x28, "apostrophe" },
1499 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001500
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001501 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001502 { 0x2c, "z" },
1503 { 0x2d, "x" },
1504 { 0x2e, "c" },
1505 { 0x2f, "v" },
1506 { 0x30, "b" },
1507 { 0x31, "n" },
1508 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001509 { 0x33, "comma" },
1510 { 0x34, "dot" },
1511 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001512
balrog4d3b6f62008-02-10 16:33:14 +00001513 { 0x37, "asterisk" },
1514
bellarda3a91a32004-06-04 11:06:21 +00001515 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001516 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001517 { 0x3b, "f1" },
1518 { 0x3c, "f2" },
1519 { 0x3d, "f3" },
1520 { 0x3e, "f4" },
1521 { 0x3f, "f5" },
1522 { 0x40, "f6" },
1523 { 0x41, "f7" },
1524 { 0x42, "f8" },
1525 { 0x43, "f9" },
1526 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001527 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001528 { 0x46, "scroll_lock" },
1529
bellard64866c32006-05-07 18:03:31 +00001530 { 0xb5, "kp_divide" },
1531 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001532 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001533 { 0x4e, "kp_add" },
1534 { 0x9c, "kp_enter" },
1535 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001536 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001537
1538 { 0x52, "kp_0" },
1539 { 0x4f, "kp_1" },
1540 { 0x50, "kp_2" },
1541 { 0x51, "kp_3" },
1542 { 0x4b, "kp_4" },
1543 { 0x4c, "kp_5" },
1544 { 0x4d, "kp_6" },
1545 { 0x47, "kp_7" },
1546 { 0x48, "kp_8" },
1547 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001548
bellarda3a91a32004-06-04 11:06:21 +00001549 { 0x56, "<" },
1550
1551 { 0x57, "f11" },
1552 { 0x58, "f12" },
1553
1554 { 0xb7, "print" },
1555
1556 { 0xc7, "home" },
1557 { 0xc9, "pgup" },
1558 { 0xd1, "pgdn" },
1559 { 0xcf, "end" },
1560
1561 { 0xcb, "left" },
1562 { 0xc8, "up" },
1563 { 0xd0, "down" },
1564 { 0xcd, "right" },
1565
1566 { 0xd2, "insert" },
1567 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001568#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1569 { 0xf0, "stop" },
1570 { 0xf1, "again" },
1571 { 0xf2, "props" },
1572 { 0xf3, "undo" },
1573 { 0xf4, "front" },
1574 { 0xf5, "copy" },
1575 { 0xf6, "open" },
1576 { 0xf7, "paste" },
1577 { 0xf8, "find" },
1578 { 0xf9, "cut" },
1579 { 0xfa, "lf" },
1580 { 0xfb, "help" },
1581 { 0xfc, "meta_l" },
1582 { 0xfd, "meta_r" },
1583 { 0xfe, "compose" },
1584#endif
bellarda3a91a32004-06-04 11:06:21 +00001585 { 0, NULL },
1586};
1587
1588static int get_keycode(const char *key)
1589{
1590 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001591 char *endp;
1592 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001593
1594 for(p = key_defs; p->name != NULL; p++) {
1595 if (!strcmp(key, p->name))
1596 return p->keycode;
1597 }
bellard64866c32006-05-07 18:03:31 +00001598 if (strstart(key, "0x", NULL)) {
1599 ret = strtoul(key, &endp, 0);
1600 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1601 return ret;
1602 }
bellarda3a91a32004-06-04 11:06:21 +00001603 return -1;
1604}
1605
balrogc8256f92008-06-08 22:45:01 +00001606#define MAX_KEYCODES 16
1607static uint8_t keycodes[MAX_KEYCODES];
1608static int nb_pending_keycodes;
1609static QEMUTimer *key_timer;
1610
1611static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001612{
balrogc8256f92008-06-08 22:45:01 +00001613 int keycode;
1614
1615 while (nb_pending_keycodes > 0) {
1616 nb_pending_keycodes--;
1617 keycode = keycodes[nb_pending_keycodes];
1618 if (keycode & 0x80)
1619 kbd_put_keycode(0xe0);
1620 kbd_put_keycode(keycode | 0x80);
1621 }
1622}
1623
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001624static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001625{
balrog3401c0d2008-06-04 10:05:59 +00001626 char keyname_buf[16];
1627 char *separator;
1628 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001629 const char *string = qdict_get_str(qdict, "string");
1630 int has_hold_time = qdict_haskey(qdict, "hold_time");
1631 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001632
balrogc8256f92008-06-08 22:45:01 +00001633 if (nb_pending_keycodes > 0) {
1634 qemu_del_timer(key_timer);
1635 release_keys(NULL);
1636 }
1637 if (!has_hold_time)
1638 hold_time = 100;
1639 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001640 while (1) {
1641 separator = strchr(string, '-');
1642 keyname_len = separator ? separator - string : strlen(string);
1643 if (keyname_len > 0) {
1644 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1645 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001646 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001647 return;
bellarda3a91a32004-06-04 11:06:21 +00001648 }
balrogc8256f92008-06-08 22:45:01 +00001649 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001650 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001651 return;
1652 }
1653 keyname_buf[keyname_len] = 0;
1654 keycode = get_keycode(keyname_buf);
1655 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001656 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001657 return;
1658 }
balrogc8256f92008-06-08 22:45:01 +00001659 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001660 }
balrog3401c0d2008-06-04 10:05:59 +00001661 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001662 break;
balrog3401c0d2008-06-04 10:05:59 +00001663 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001664 }
balrogc8256f92008-06-08 22:45:01 +00001665 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001666 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001667 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001668 keycode = keycodes[i];
1669 if (keycode & 0x80)
1670 kbd_put_keycode(0xe0);
1671 kbd_put_keycode(keycode & 0x7f);
1672 }
balrogc8256f92008-06-08 22:45:01 +00001673 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001674 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001675 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001676}
1677
bellard13224a82006-07-14 22:03:35 +00001678static int mouse_button_state;
1679
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001680static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001681{
1682 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001683 const char *dx_str = qdict_get_str(qdict, "dx_str");
1684 const char *dy_str = qdict_get_str(qdict, "dy_str");
1685 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001686 dx = strtol(dx_str, NULL, 0);
1687 dy = strtol(dy_str, NULL, 0);
1688 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001689 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001690 dz = strtol(dz_str, NULL, 0);
1691 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1692}
1693
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001694static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001695{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001696 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001697 mouse_button_state = button_state;
1698 kbd_mouse_event(0, 0, 0, mouse_button_state);
1699}
1700
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001701static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001702{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001703 int size = qdict_get_int(qdict, "size");
1704 int addr = qdict_get_int(qdict, "addr");
1705 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001706 uint32_t val;
1707 int suffix;
1708
1709 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001710 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001711 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001712 addr++;
1713 }
1714 addr &= 0xffff;
1715
1716 switch(size) {
1717 default:
1718 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001719 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001720 suffix = 'b';
1721 break;
1722 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001723 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001724 suffix = 'w';
1725 break;
1726 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001727 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001728 suffix = 'l';
1729 break;
1730 }
aliguori376253e2009-03-05 23:01:23 +00001731 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1732 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001733}
bellarda3a91a32004-06-04 11:06:21 +00001734
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001735static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001736{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001737 int size = qdict_get_int(qdict, "size");
1738 int addr = qdict_get_int(qdict, "addr");
1739 int val = qdict_get_int(qdict, "val");
1740
Jan Kiszkaf1147842009-07-14 10:20:11 +02001741 addr &= IOPORTS_MASK;
1742
1743 switch (size) {
1744 default:
1745 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001746 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001747 break;
1748 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001749 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001750 break;
1751 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001752 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001753 break;
1754 }
1755}
1756
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001757static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001758{
1759 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001760 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001761
Jan Kiszka76e30d02009-07-02 00:19:02 +02001762 res = qemu_boot_set(bootdevice);
1763 if (res == 0) {
1764 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1765 } else if (res > 0) {
1766 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001767 } else {
aliguori376253e2009-03-05 23:01:23 +00001768 monitor_printf(mon, "no function defined to set boot device list for "
1769 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001770 }
1771}
1772
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001773/**
1774 * do_system_reset(): Issue a machine reset
1775 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001776static int do_system_reset(Monitor *mon, const QDict *qdict,
1777 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001778{
1779 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001780 return 0;
bellarde4f90822004-06-20 12:35:44 +00001781}
1782
Luiz Capitulino43076662009-10-07 13:41:59 -03001783/**
1784 * do_system_powerdown(): Issue a machine powerdown
1785 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001786static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1787 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001788{
1789 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001790 return 0;
bellard34751872005-07-02 14:31:34 +00001791}
1792
bellardb86bda52004-09-18 19:32:46 +00001793#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001794static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001795{
aliguori376253e2009-03-05 23:01:23 +00001796 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1797 addr,
1798 pte & mask,
1799 pte & PG_GLOBAL_MASK ? 'G' : '-',
1800 pte & PG_PSE_MASK ? 'P' : '-',
1801 pte & PG_DIRTY_MASK ? 'D' : '-',
1802 pte & PG_ACCESSED_MASK ? 'A' : '-',
1803 pte & PG_PCD_MASK ? 'C' : '-',
1804 pte & PG_PWT_MASK ? 'T' : '-',
1805 pte & PG_USER_MASK ? 'U' : '-',
1806 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001807}
1808
aliguori376253e2009-03-05 23:01:23 +00001809static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001810{
bellard6a00d602005-11-21 23:25:50 +00001811 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001812 int l1, l2;
1813 uint32_t pgd, pde, pte;
1814
bellard6a00d602005-11-21 23:25:50 +00001815 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001816
bellardb86bda52004-09-18 19:32:46 +00001817 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001818 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001819 return;
1820 }
1821 pgd = env->cr[3] & ~0xfff;
1822 for(l1 = 0; l1 < 1024; l1++) {
1823 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1824 pde = le32_to_cpu(pde);
1825 if (pde & PG_PRESENT_MASK) {
1826 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001827 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001828 } else {
1829 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001830 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001831 (uint8_t *)&pte, 4);
1832 pte = le32_to_cpu(pte);
1833 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001834 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001835 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001836 ~0xfff);
1837 }
1838 }
1839 }
1840 }
1841 }
1842}
1843
aliguori376253e2009-03-05 23:01:23 +00001844static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001845 uint32_t end, int prot)
1846{
bellard9746b152004-11-11 18:30:24 +00001847 int prot1;
1848 prot1 = *plast_prot;
1849 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001850 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001851 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1852 *pstart, end, end - *pstart,
1853 prot1 & PG_USER_MASK ? 'u' : '-',
1854 'r',
1855 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001856 }
1857 if (prot != 0)
1858 *pstart = end;
1859 else
1860 *pstart = -1;
1861 *plast_prot = prot;
1862 }
1863}
1864
aliguori376253e2009-03-05 23:01:23 +00001865static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001866{
bellard6a00d602005-11-21 23:25:50 +00001867 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001868 int l1, l2, prot, last_prot;
1869 uint32_t pgd, pde, pte, start, end;
1870
bellard6a00d602005-11-21 23:25:50 +00001871 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001872
bellardb86bda52004-09-18 19:32:46 +00001873 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001874 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001875 return;
1876 }
1877 pgd = env->cr[3] & ~0xfff;
1878 last_prot = 0;
1879 start = -1;
1880 for(l1 = 0; l1 < 1024; l1++) {
1881 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1882 pde = le32_to_cpu(pde);
1883 end = l1 << 22;
1884 if (pde & PG_PRESENT_MASK) {
1885 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1886 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001887 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001888 } else {
1889 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001890 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001891 (uint8_t *)&pte, 4);
1892 pte = le32_to_cpu(pte);
1893 end = (l1 << 22) + (l2 << 12);
1894 if (pte & PG_PRESENT_MASK) {
1895 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1896 } else {
1897 prot = 0;
1898 }
aliguori376253e2009-03-05 23:01:23 +00001899 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001900 }
1901 }
1902 } else {
1903 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001904 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001905 }
1906 }
1907}
1908#endif
1909
aurel327c664e22009-03-03 06:12:22 +00001910#if defined(TARGET_SH4)
1911
aliguori376253e2009-03-05 23:01:23 +00001912static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001913{
aliguori376253e2009-03-05 23:01:23 +00001914 monitor_printf(mon, " tlb%i:\t"
1915 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1916 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1917 "dirty=%hhu writethrough=%hhu\n",
1918 idx,
1919 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1920 tlb->v, tlb->sh, tlb->c, tlb->pr,
1921 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001922}
1923
aliguori376253e2009-03-05 23:01:23 +00001924static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001925{
1926 CPUState *env = mon_get_cpu();
1927 int i;
1928
aliguori376253e2009-03-05 23:01:23 +00001929 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001930 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001931 print_tlb (mon, i, &env->itlb[i]);
1932 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001933 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001934 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001935}
1936
1937#endif
1938
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001939static void do_info_kvm_print(Monitor *mon, const QObject *data)
1940{
1941 QDict *qdict;
1942
1943 qdict = qobject_to_qdict(data);
1944
1945 monitor_printf(mon, "kvm support: ");
1946 if (qdict_get_bool(qdict, "present")) {
1947 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1948 "enabled" : "disabled");
1949 } else {
1950 monitor_printf(mon, "not compiled\n");
1951 }
1952}
1953
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001954static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001955{
1956#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001957 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1958 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001959#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001960 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001961#endif
1962}
1963
aliguori030ea372009-04-21 22:30:47 +00001964static void do_info_numa(Monitor *mon)
1965{
aliguorib28b6232009-04-22 20:20:29 +00001966 int i;
aliguori030ea372009-04-21 22:30:47 +00001967 CPUState *env;
1968
1969 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1970 for (i = 0; i < nb_numa_nodes; i++) {
1971 monitor_printf(mon, "node %d cpus:", i);
1972 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1973 if (env->numa_node == i) {
1974 monitor_printf(mon, " %d", env->cpu_index);
1975 }
1976 }
1977 monitor_printf(mon, "\n");
1978 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1979 node_mem[i] >> 20);
1980 }
1981}
1982
bellard5f1ce942006-02-08 22:40:15 +00001983#ifdef CONFIG_PROFILER
1984
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001985int64_t qemu_time;
1986int64_t dev_time;
1987
aliguori376253e2009-03-05 23:01:23 +00001988static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001989{
1990 int64_t total;
1991 total = qemu_time;
1992 if (total == 0)
1993 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001994 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001995 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001996 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001997 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001998 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001999 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002000}
2001#else
aliguori376253e2009-03-05 23:01:23 +00002002static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002003{
aliguori376253e2009-03-05 23:01:23 +00002004 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002005}
2006#endif
2007
bellardec36b692006-07-16 18:57:03 +00002008/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002009static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002010
aliguori376253e2009-03-05 23:01:23 +00002011static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002012{
2013 int i;
2014 CaptureState *s;
2015
2016 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002017 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002018 s->ops.info (s->opaque);
2019 }
2020}
2021
Blue Swirl23130862009-06-06 08:22:04 +00002022#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002023static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002024{
2025 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002026 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002027 CaptureState *s;
2028
2029 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2030 if (i == n) {
2031 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002032 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002033 qemu_free (s);
2034 return;
2035 }
2036 }
2037}
2038
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002039static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002040{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002041 const char *path = qdict_get_str(qdict, "path");
2042 int has_freq = qdict_haskey(qdict, "freq");
2043 int freq = qdict_get_try_int(qdict, "freq", -1);
2044 int has_bits = qdict_haskey(qdict, "bits");
2045 int bits = qdict_get_try_int(qdict, "bits", -1);
2046 int has_channels = qdict_haskey(qdict, "nchannels");
2047 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002048 CaptureState *s;
2049
2050 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002051
2052 freq = has_freq ? freq : 44100;
2053 bits = has_bits ? bits : 16;
2054 nchannels = has_channels ? nchannels : 2;
2055
2056 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002057 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002058 qemu_free (s);
2059 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002060 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002061}
2062#endif
2063
aurel32dc1c0b72008-04-27 23:52:12 +00002064#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002065static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002066{
2067 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002068 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002069
2070 for (env = first_cpu; env != NULL; env = env->next_cpu)
2071 if (env->cpu_index == cpu_index) {
2072 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2073 break;
2074 }
2075}
2076#endif
2077
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002078static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002079{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002080 QDict *qdict;
2081
2082 qdict = qobject_to_qdict(data);
2083
2084 monitor_printf(mon, "VM status: ");
2085 if (qdict_get_bool(qdict, "running")) {
2086 monitor_printf(mon, "running");
2087 if (qdict_get_bool(qdict, "singlestep")) {
2088 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002089 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002090 } else {
2091 monitor_printf(mon, "paused");
2092 }
2093
2094 monitor_printf(mon, "\n");
2095}
2096
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002097static void do_info_status(Monitor *mon, QObject **ret_data)
2098{
2099 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2100 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002101}
2102
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002103static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002104{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002105 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002106
aliguori76655d62009-03-06 20:27:37 +00002107 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002108 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002109 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002110 return acl;
2111}
aliguori76655d62009-03-06 20:27:37 +00002112
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002113static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002114{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002115 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002116 qemu_acl *acl = find_acl(mon, aclname);
2117 qemu_acl_entry *entry;
2118 int i = 0;
2119
2120 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002121 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002122 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002123 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002124 i++;
2125 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002127 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128 }
2129}
2130
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002131static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002132{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002133 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002134 qemu_acl *acl = find_acl(mon, aclname);
2135
2136 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002137 qemu_acl_reset(acl);
2138 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139 }
2140}
aliguori76655d62009-03-06 20:27:37 +00002141
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002142static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002143{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002144 const char *aclname = qdict_get_str(qdict, "aclname");
2145 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002146 qemu_acl *acl = find_acl(mon, aclname);
2147
2148 if (acl) {
2149 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002150 acl->defaultDeny = 0;
2151 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002153 acl->defaultDeny = 1;
2154 monitor_printf(mon, "acl: policy set to 'deny'\n");
2155 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156 monitor_printf(mon, "acl: unknown policy '%s', "
2157 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002158 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002159 }
2160}
aliguori76655d62009-03-06 20:27:37 +00002161
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002162static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002163{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002164 const char *aclname = qdict_get_str(qdict, "aclname");
2165 const char *match = qdict_get_str(qdict, "match");
2166 const char *policy = qdict_get_str(qdict, "policy");
2167 int has_index = qdict_haskey(qdict, "index");
2168 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002169 qemu_acl *acl = find_acl(mon, aclname);
2170 int deny, ret;
2171
2172 if (acl) {
2173 if (strcmp(policy, "allow") == 0) {
2174 deny = 0;
2175 } else if (strcmp(policy, "deny") == 0) {
2176 deny = 1;
2177 } else {
2178 monitor_printf(mon, "acl: unknown policy '%s', "
2179 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002180 return;
2181 }
aliguori28a76be2009-03-06 20:27:40 +00002182 if (has_index)
2183 ret = qemu_acl_insert(acl, deny, match, index);
2184 else
2185 ret = qemu_acl_append(acl, deny, match);
2186 if (ret < 0)
2187 monitor_printf(mon, "acl: unable to add acl entry\n");
2188 else
2189 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002190 }
2191}
aliguori76655d62009-03-06 20:27:37 +00002192
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002193static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002194{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002195 const char *aclname = qdict_get_str(qdict, "aclname");
2196 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002197 qemu_acl *acl = find_acl(mon, aclname);
2198 int ret;
aliguori76655d62009-03-06 20:27:37 +00002199
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002200 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002201 ret = qemu_acl_remove(acl, match);
2202 if (ret < 0)
2203 monitor_printf(mon, "acl: no matching acl entry\n");
2204 else
2205 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002206 }
2207}
2208
Huang Ying79c4f6b2009-06-23 10:05:14 +08002209#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002210static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002211{
2212 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002213 int cpu_index = qdict_get_int(qdict, "cpu_index");
2214 int bank = qdict_get_int(qdict, "bank");
2215 uint64_t status = qdict_get_int(qdict, "status");
2216 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2217 uint64_t addr = qdict_get_int(qdict, "addr");
2218 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002219
2220 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2221 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2222 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2223 break;
2224 }
2225}
2226#endif
2227
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002228static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002230 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002231 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002232 int fd;
2233
2234 fd = qemu_chr_get_msgfd(mon->chr);
2235 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002236 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002237 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002238 }
2239
2240 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002241 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2242 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002243 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002244 }
2245
Blue Swirl72cf2d42009-09-12 07:36:22 +00002246 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002247 if (strcmp(monfd->name, fdname) != 0) {
2248 continue;
2249 }
2250
2251 close(monfd->fd);
2252 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002253 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002254 }
2255
Anthony Liguoric227f092009-10-01 16:12:16 -05002256 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002257 monfd->name = qemu_strdup(fdname);
2258 monfd->fd = fd;
2259
Blue Swirl72cf2d42009-09-12 07:36:22 +00002260 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002261 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002262}
2263
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002264static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002265{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002266 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002267 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002268
Blue Swirl72cf2d42009-09-12 07:36:22 +00002269 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002270 if (strcmp(monfd->name, fdname) != 0) {
2271 continue;
2272 }
2273
Blue Swirl72cf2d42009-09-12 07:36:22 +00002274 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002275 close(monfd->fd);
2276 qemu_free(monfd->name);
2277 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002278 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002279 }
2280
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002281 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002282 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002283}
2284
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002285static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002286{
2287 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002288 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002289
2290 vm_stop(0);
2291
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002292 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002293 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002294 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002295}
2296
Mark McLoughlin7768e042009-07-22 09:11:41 +01002297int monitor_get_fd(Monitor *mon, const char *fdname)
2298{
Anthony Liguoric227f092009-10-01 16:12:16 -05002299 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002300
Blue Swirl72cf2d42009-09-12 07:36:22 +00002301 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002302 int fd;
2303
2304 if (strcmp(monfd->name, fdname) != 0) {
2305 continue;
2306 }
2307
2308 fd = monfd->fd;
2309
2310 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002311 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002312 qemu_free(monfd->name);
2313 qemu_free(monfd);
2314
2315 return fd;
2316 }
2317
2318 return -1;
2319}
2320
Anthony Liguoric227f092009-10-01 16:12:16 -05002321static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002322#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002323 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002324};
2325
Blue Swirl23130862009-06-06 08:22:04 +00002326/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002327static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002328 {
2329 .name = "version",
2330 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002331 .params = "",
2332 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002333 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002334 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002335 },
2336 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002337 .name = "commands",
2338 .args_type = "",
2339 .params = "",
2340 .help = "list QMP available commands",
2341 .user_print = monitor_user_noop,
2342 .mhandler.info_new = do_info_commands,
2343 },
2344 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002345 .name = "network",
2346 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002347 .params = "",
2348 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002349 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002350 },
2351 {
2352 .name = "chardev",
2353 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002354 .params = "",
2355 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002356 .user_print = qemu_chr_info_print,
2357 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002358 },
2359 {
2360 .name = "block",
2361 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002362 .params = "",
2363 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002364 .user_print = bdrv_info_print,
2365 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002366 },
2367 {
2368 .name = "blockstats",
2369 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002370 .params = "",
2371 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002372 .user_print = bdrv_stats_print,
2373 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002374 },
2375 {
2376 .name = "registers",
2377 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 .params = "",
2379 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002380 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002381 },
2382 {
2383 .name = "cpus",
2384 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002385 .params = "",
2386 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002387 .user_print = monitor_print_cpus,
2388 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002389 },
2390 {
2391 .name = "history",
2392 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002393 .params = "",
2394 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002395 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002396 },
2397 {
2398 .name = "irq",
2399 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002400 .params = "",
2401 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002402 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002403 },
2404 {
2405 .name = "pic",
2406 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002407 .params = "",
2408 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002409 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002410 },
2411 {
2412 .name = "pci",
2413 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002414 .params = "",
2415 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002416 .user_print = do_pci_info_print,
2417 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002418 },
aurel327c664e22009-03-03 06:12:22 +00002419#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002420 {
2421 .name = "tlb",
2422 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002423 .params = "",
2424 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002425 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002426 },
aurel327c664e22009-03-03 06:12:22 +00002427#endif
2428#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002429 {
2430 .name = "mem",
2431 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002432 .params = "",
2433 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002434 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002435 },
bellardb86bda52004-09-18 19:32:46 +00002436#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002437 {
2438 .name = "jit",
2439 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002440 .params = "",
2441 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002442 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002443 },
2444 {
2445 .name = "kvm",
2446 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002447 .params = "",
2448 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002449 .user_print = do_info_kvm_print,
2450 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002451 },
2452 {
2453 .name = "numa",
2454 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 .params = "",
2456 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002457 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002458 },
2459 {
2460 .name = "usb",
2461 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002462 .params = "",
2463 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002464 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002465 },
2466 {
2467 .name = "usbhost",
2468 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002469 .params = "",
2470 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002471 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002472 },
2473 {
2474 .name = "profile",
2475 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002476 .params = "",
2477 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002478 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002479 },
2480 {
2481 .name = "capture",
2482 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002483 .params = "",
2484 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002485 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002486 },
2487 {
2488 .name = "snapshots",
2489 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002490 .params = "",
2491 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002492 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002493 },
2494 {
2495 .name = "status",
2496 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002497 .params = "",
2498 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002499 .user_print = do_info_status_print,
2500 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002501 },
2502 {
2503 .name = "pcmcia",
2504 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002505 .params = "",
2506 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002507 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002508 },
2509 {
2510 .name = "mice",
2511 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 .params = "",
2513 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002514 .user_print = do_info_mice_print,
2515 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 },
2517 {
2518 .name = "vnc",
2519 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 .params = "",
2521 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002522 .user_print = do_info_vnc_print,
2523 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 },
2525 {
2526 .name = "name",
2527 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 .params = "",
2529 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002530 .user_print = do_info_name_print,
2531 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "uuid",
2535 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 .params = "",
2537 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002538 .user_print = do_info_uuid_print,
2539 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
j_mayer76a66252007-03-07 08:32:30 +00002541#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 {
2543 .name = "cpustats",
2544 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 .params = "",
2546 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002547 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 },
j_mayer76a66252007-03-07 08:32:30 +00002549#endif
blueswir131a60e22007-10-26 18:42:59 +00002550#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 {
2552 .name = "usernet",
2553 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 .params = "",
2555 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002556 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 },
blueswir131a60e22007-10-26 18:42:59 +00002558#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 {
2560 .name = "migrate",
2561 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 .params = "",
2563 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002564 .user_print = do_info_migrate_print,
2565 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 },
2567 {
2568 .name = "balloon",
2569 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 .params = "",
2571 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002572 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002573 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002574 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 },
2576 {
2577 .name = "qtree",
2578 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 .params = "",
2580 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002581 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 },
2583 {
2584 .name = "qdm",
2585 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 .params = "",
2587 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002588 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 },
2590 {
2591 .name = "roms",
2592 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 .params = "",
2594 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002595 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 },
2597 {
2598 .name = NULL,
2599 },
bellard9dc39cb2004-03-14 21:38:27 +00002600};
2601
bellard9307c4c2004-04-04 12:57:25 +00002602/*******************************************************************/
2603
2604static const char *pch;
2605static jmp_buf expr_env;
2606
bellard92a31b12005-02-10 22:00:52 +00002607#define MD_TLONG 0
2608#define MD_I32 1
2609
bellard9307c4c2004-04-04 12:57:25 +00002610typedef struct MonitorDef {
2611 const char *name;
2612 int offset;
blueswir18662d652008-10-02 18:32:44 +00002613 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002614 int type;
bellard9307c4c2004-04-04 12:57:25 +00002615} MonitorDef;
2616
bellard57206fd2004-04-25 18:54:52 +00002617#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002618static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002619{
bellard6a00d602005-11-21 23:25:50 +00002620 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002621 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002622}
2623#endif
2624
bellarda541f292004-04-12 20:39:29 +00002625#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002626static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002627{
bellard6a00d602005-11-21 23:25:50 +00002628 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002629 unsigned int u;
2630 int i;
2631
2632 u = 0;
2633 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002634 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002635
2636 return u;
2637}
2638
blueswir18662d652008-10-02 18:32:44 +00002639static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002640{
bellard6a00d602005-11-21 23:25:50 +00002641 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002642 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002643}
2644
blueswir18662d652008-10-02 18:32:44 +00002645static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002646{
bellard6a00d602005-11-21 23:25:50 +00002647 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002648 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002649}
bellard9fddaa02004-05-21 12:59:32 +00002650
blueswir18662d652008-10-02 18:32:44 +00002651static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002652{
bellard6a00d602005-11-21 23:25:50 +00002653 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002654 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002655}
2656
blueswir18662d652008-10-02 18:32:44 +00002657static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002658{
bellard6a00d602005-11-21 23:25:50 +00002659 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002660 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002661}
2662
blueswir18662d652008-10-02 18:32:44 +00002663static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002664{
bellard6a00d602005-11-21 23:25:50 +00002665 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002666 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002667}
bellarda541f292004-04-12 20:39:29 +00002668#endif
2669
bellarde95c8d52004-09-30 22:22:08 +00002670#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002671#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002672static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002673{
bellard6a00d602005-11-21 23:25:50 +00002674 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002675
2676 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002677}
bellard7b936c02005-10-30 17:05:13 +00002678#endif
bellarde95c8d52004-09-30 22:22:08 +00002679
blueswir18662d652008-10-02 18:32:44 +00002680static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002681{
bellard6a00d602005-11-21 23:25:50 +00002682 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002683 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002684}
2685#endif
2686
blueswir18662d652008-10-02 18:32:44 +00002687static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002688#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002689
2690#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002691 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002692 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002693 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002694
bellard9307c4c2004-04-04 12:57:25 +00002695 { "eax", offsetof(CPUState, regs[0]) },
2696 { "ecx", offsetof(CPUState, regs[1]) },
2697 { "edx", offsetof(CPUState, regs[2]) },
2698 { "ebx", offsetof(CPUState, regs[3]) },
2699 { "esp|sp", offsetof(CPUState, regs[4]) },
2700 { "ebp|fp", offsetof(CPUState, regs[5]) },
2701 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002702 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002703#ifdef TARGET_X86_64
2704 { "r8", offsetof(CPUState, regs[8]) },
2705 { "r9", offsetof(CPUState, regs[9]) },
2706 { "r10", offsetof(CPUState, regs[10]) },
2707 { "r11", offsetof(CPUState, regs[11]) },
2708 { "r12", offsetof(CPUState, regs[12]) },
2709 { "r13", offsetof(CPUState, regs[13]) },
2710 { "r14", offsetof(CPUState, regs[14]) },
2711 { "r15", offsetof(CPUState, regs[15]) },
2712#endif
bellard9307c4c2004-04-04 12:57:25 +00002713 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002714 { "eip", offsetof(CPUState, eip) },
2715 SEG("cs", R_CS)
2716 SEG("ds", R_DS)
2717 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002718 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002719 SEG("fs", R_FS)
2720 SEG("gs", R_GS)
2721 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002722#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002723 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002724 { "r0", offsetof(CPUState, gpr[0]) },
2725 { "r1", offsetof(CPUState, gpr[1]) },
2726 { "r2", offsetof(CPUState, gpr[2]) },
2727 { "r3", offsetof(CPUState, gpr[3]) },
2728 { "r4", offsetof(CPUState, gpr[4]) },
2729 { "r5", offsetof(CPUState, gpr[5]) },
2730 { "r6", offsetof(CPUState, gpr[6]) },
2731 { "r7", offsetof(CPUState, gpr[7]) },
2732 { "r8", offsetof(CPUState, gpr[8]) },
2733 { "r9", offsetof(CPUState, gpr[9]) },
2734 { "r10", offsetof(CPUState, gpr[10]) },
2735 { "r11", offsetof(CPUState, gpr[11]) },
2736 { "r12", offsetof(CPUState, gpr[12]) },
2737 { "r13", offsetof(CPUState, gpr[13]) },
2738 { "r14", offsetof(CPUState, gpr[14]) },
2739 { "r15", offsetof(CPUState, gpr[15]) },
2740 { "r16", offsetof(CPUState, gpr[16]) },
2741 { "r17", offsetof(CPUState, gpr[17]) },
2742 { "r18", offsetof(CPUState, gpr[18]) },
2743 { "r19", offsetof(CPUState, gpr[19]) },
2744 { "r20", offsetof(CPUState, gpr[20]) },
2745 { "r21", offsetof(CPUState, gpr[21]) },
2746 { "r22", offsetof(CPUState, gpr[22]) },
2747 { "r23", offsetof(CPUState, gpr[23]) },
2748 { "r24", offsetof(CPUState, gpr[24]) },
2749 { "r25", offsetof(CPUState, gpr[25]) },
2750 { "r26", offsetof(CPUState, gpr[26]) },
2751 { "r27", offsetof(CPUState, gpr[27]) },
2752 { "r28", offsetof(CPUState, gpr[28]) },
2753 { "r29", offsetof(CPUState, gpr[29]) },
2754 { "r30", offsetof(CPUState, gpr[30]) },
2755 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002756 /* Floating point registers */
2757 { "f0", offsetof(CPUState, fpr[0]) },
2758 { "f1", offsetof(CPUState, fpr[1]) },
2759 { "f2", offsetof(CPUState, fpr[2]) },
2760 { "f3", offsetof(CPUState, fpr[3]) },
2761 { "f4", offsetof(CPUState, fpr[4]) },
2762 { "f5", offsetof(CPUState, fpr[5]) },
2763 { "f6", offsetof(CPUState, fpr[6]) },
2764 { "f7", offsetof(CPUState, fpr[7]) },
2765 { "f8", offsetof(CPUState, fpr[8]) },
2766 { "f9", offsetof(CPUState, fpr[9]) },
2767 { "f10", offsetof(CPUState, fpr[10]) },
2768 { "f11", offsetof(CPUState, fpr[11]) },
2769 { "f12", offsetof(CPUState, fpr[12]) },
2770 { "f13", offsetof(CPUState, fpr[13]) },
2771 { "f14", offsetof(CPUState, fpr[14]) },
2772 { "f15", offsetof(CPUState, fpr[15]) },
2773 { "f16", offsetof(CPUState, fpr[16]) },
2774 { "f17", offsetof(CPUState, fpr[17]) },
2775 { "f18", offsetof(CPUState, fpr[18]) },
2776 { "f19", offsetof(CPUState, fpr[19]) },
2777 { "f20", offsetof(CPUState, fpr[20]) },
2778 { "f21", offsetof(CPUState, fpr[21]) },
2779 { "f22", offsetof(CPUState, fpr[22]) },
2780 { "f23", offsetof(CPUState, fpr[23]) },
2781 { "f24", offsetof(CPUState, fpr[24]) },
2782 { "f25", offsetof(CPUState, fpr[25]) },
2783 { "f26", offsetof(CPUState, fpr[26]) },
2784 { "f27", offsetof(CPUState, fpr[27]) },
2785 { "f28", offsetof(CPUState, fpr[28]) },
2786 { "f29", offsetof(CPUState, fpr[29]) },
2787 { "f30", offsetof(CPUState, fpr[30]) },
2788 { "f31", offsetof(CPUState, fpr[31]) },
2789 { "fpscr", offsetof(CPUState, fpscr) },
2790 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002791 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002792 { "lr", offsetof(CPUState, lr) },
2793 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002794 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002795 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002796 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002797 { "msr", 0, &monitor_get_msr, },
2798 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002799 { "tbu", 0, &monitor_get_tbu, },
2800 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002801#if defined(TARGET_PPC64)
2802 /* Address space register */
2803 { "asr", offsetof(CPUState, asr) },
2804#endif
2805 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002806 { "sdr1", offsetof(CPUState, sdr1) },
2807 { "sr0", offsetof(CPUState, sr[0]) },
2808 { "sr1", offsetof(CPUState, sr[1]) },
2809 { "sr2", offsetof(CPUState, sr[2]) },
2810 { "sr3", offsetof(CPUState, sr[3]) },
2811 { "sr4", offsetof(CPUState, sr[4]) },
2812 { "sr5", offsetof(CPUState, sr[5]) },
2813 { "sr6", offsetof(CPUState, sr[6]) },
2814 { "sr7", offsetof(CPUState, sr[7]) },
2815 { "sr8", offsetof(CPUState, sr[8]) },
2816 { "sr9", offsetof(CPUState, sr[9]) },
2817 { "sr10", offsetof(CPUState, sr[10]) },
2818 { "sr11", offsetof(CPUState, sr[11]) },
2819 { "sr12", offsetof(CPUState, sr[12]) },
2820 { "sr13", offsetof(CPUState, sr[13]) },
2821 { "sr14", offsetof(CPUState, sr[14]) },
2822 { "sr15", offsetof(CPUState, sr[15]) },
2823 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002824#elif defined(TARGET_SPARC)
2825 { "g0", offsetof(CPUState, gregs[0]) },
2826 { "g1", offsetof(CPUState, gregs[1]) },
2827 { "g2", offsetof(CPUState, gregs[2]) },
2828 { "g3", offsetof(CPUState, gregs[3]) },
2829 { "g4", offsetof(CPUState, gregs[4]) },
2830 { "g5", offsetof(CPUState, gregs[5]) },
2831 { "g6", offsetof(CPUState, gregs[6]) },
2832 { "g7", offsetof(CPUState, gregs[7]) },
2833 { "o0", 0, monitor_get_reg },
2834 { "o1", 1, monitor_get_reg },
2835 { "o2", 2, monitor_get_reg },
2836 { "o3", 3, monitor_get_reg },
2837 { "o4", 4, monitor_get_reg },
2838 { "o5", 5, monitor_get_reg },
2839 { "o6", 6, monitor_get_reg },
2840 { "o7", 7, monitor_get_reg },
2841 { "l0", 8, monitor_get_reg },
2842 { "l1", 9, monitor_get_reg },
2843 { "l2", 10, monitor_get_reg },
2844 { "l3", 11, monitor_get_reg },
2845 { "l4", 12, monitor_get_reg },
2846 { "l5", 13, monitor_get_reg },
2847 { "l6", 14, monitor_get_reg },
2848 { "l7", 15, monitor_get_reg },
2849 { "i0", 16, monitor_get_reg },
2850 { "i1", 17, monitor_get_reg },
2851 { "i2", 18, monitor_get_reg },
2852 { "i3", 19, monitor_get_reg },
2853 { "i4", 20, monitor_get_reg },
2854 { "i5", 21, monitor_get_reg },
2855 { "i6", 22, monitor_get_reg },
2856 { "i7", 23, monitor_get_reg },
2857 { "pc", offsetof(CPUState, pc) },
2858 { "npc", offsetof(CPUState, npc) },
2859 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002860#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002861 { "psr", 0, &monitor_get_psr, },
2862 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002863#endif
bellarde95c8d52004-09-30 22:22:08 +00002864 { "tbr", offsetof(CPUState, tbr) },
2865 { "fsr", offsetof(CPUState, fsr) },
2866 { "f0", offsetof(CPUState, fpr[0]) },
2867 { "f1", offsetof(CPUState, fpr[1]) },
2868 { "f2", offsetof(CPUState, fpr[2]) },
2869 { "f3", offsetof(CPUState, fpr[3]) },
2870 { "f4", offsetof(CPUState, fpr[4]) },
2871 { "f5", offsetof(CPUState, fpr[5]) },
2872 { "f6", offsetof(CPUState, fpr[6]) },
2873 { "f7", offsetof(CPUState, fpr[7]) },
2874 { "f8", offsetof(CPUState, fpr[8]) },
2875 { "f9", offsetof(CPUState, fpr[9]) },
2876 { "f10", offsetof(CPUState, fpr[10]) },
2877 { "f11", offsetof(CPUState, fpr[11]) },
2878 { "f12", offsetof(CPUState, fpr[12]) },
2879 { "f13", offsetof(CPUState, fpr[13]) },
2880 { "f14", offsetof(CPUState, fpr[14]) },
2881 { "f15", offsetof(CPUState, fpr[15]) },
2882 { "f16", offsetof(CPUState, fpr[16]) },
2883 { "f17", offsetof(CPUState, fpr[17]) },
2884 { "f18", offsetof(CPUState, fpr[18]) },
2885 { "f19", offsetof(CPUState, fpr[19]) },
2886 { "f20", offsetof(CPUState, fpr[20]) },
2887 { "f21", offsetof(CPUState, fpr[21]) },
2888 { "f22", offsetof(CPUState, fpr[22]) },
2889 { "f23", offsetof(CPUState, fpr[23]) },
2890 { "f24", offsetof(CPUState, fpr[24]) },
2891 { "f25", offsetof(CPUState, fpr[25]) },
2892 { "f26", offsetof(CPUState, fpr[26]) },
2893 { "f27", offsetof(CPUState, fpr[27]) },
2894 { "f28", offsetof(CPUState, fpr[28]) },
2895 { "f29", offsetof(CPUState, fpr[29]) },
2896 { "f30", offsetof(CPUState, fpr[30]) },
2897 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002898#ifdef TARGET_SPARC64
2899 { "f32", offsetof(CPUState, fpr[32]) },
2900 { "f34", offsetof(CPUState, fpr[34]) },
2901 { "f36", offsetof(CPUState, fpr[36]) },
2902 { "f38", offsetof(CPUState, fpr[38]) },
2903 { "f40", offsetof(CPUState, fpr[40]) },
2904 { "f42", offsetof(CPUState, fpr[42]) },
2905 { "f44", offsetof(CPUState, fpr[44]) },
2906 { "f46", offsetof(CPUState, fpr[46]) },
2907 { "f48", offsetof(CPUState, fpr[48]) },
2908 { "f50", offsetof(CPUState, fpr[50]) },
2909 { "f52", offsetof(CPUState, fpr[52]) },
2910 { "f54", offsetof(CPUState, fpr[54]) },
2911 { "f56", offsetof(CPUState, fpr[56]) },
2912 { "f58", offsetof(CPUState, fpr[58]) },
2913 { "f60", offsetof(CPUState, fpr[60]) },
2914 { "f62", offsetof(CPUState, fpr[62]) },
2915 { "asi", offsetof(CPUState, asi) },
2916 { "pstate", offsetof(CPUState, pstate) },
2917 { "cansave", offsetof(CPUState, cansave) },
2918 { "canrestore", offsetof(CPUState, canrestore) },
2919 { "otherwin", offsetof(CPUState, otherwin) },
2920 { "wstate", offsetof(CPUState, wstate) },
2921 { "cleanwin", offsetof(CPUState, cleanwin) },
2922 { "fprs", offsetof(CPUState, fprs) },
2923#endif
bellard9307c4c2004-04-04 12:57:25 +00002924#endif
2925 { NULL },
2926};
2927
aliguori376253e2009-03-05 23:01:23 +00002928static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002929{
aliguori376253e2009-03-05 23:01:23 +00002930 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002931 longjmp(expr_env, 1);
2932}
2933
Markus Armbruster09b94182010-01-20 13:07:30 +01002934/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002935static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002936{
blueswir18662d652008-10-02 18:32:44 +00002937 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002938 void *ptr;
2939
bellard9307c4c2004-04-04 12:57:25 +00002940 for(md = monitor_defs; md->name != NULL; md++) {
2941 if (compare_cmd(name, md->name)) {
2942 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002943 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002944 } else {
bellard6a00d602005-11-21 23:25:50 +00002945 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002946 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002947 switch(md->type) {
2948 case MD_I32:
2949 *pval = *(int32_t *)ptr;
2950 break;
2951 case MD_TLONG:
2952 *pval = *(target_long *)ptr;
2953 break;
2954 default:
2955 *pval = 0;
2956 break;
2957 }
bellard9307c4c2004-04-04 12:57:25 +00002958 }
2959 return 0;
2960 }
2961 }
2962 return -1;
2963}
2964
2965static void next(void)
2966{
Blue Swirl660f11b2009-07-31 21:16:51 +00002967 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002968 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002969 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002970 pch++;
2971 }
2972}
2973
aliguori376253e2009-03-05 23:01:23 +00002974static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002975
aliguori376253e2009-03-05 23:01:23 +00002976static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002977{
blueswir1c2efc952007-09-25 17:28:42 +00002978 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002979 char *p;
bellard6a00d602005-11-21 23:25:50 +00002980 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002981
2982 switch(*pch) {
2983 case '+':
2984 next();
aliguori376253e2009-03-05 23:01:23 +00002985 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002986 break;
2987 case '-':
2988 next();
aliguori376253e2009-03-05 23:01:23 +00002989 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002990 break;
2991 case '~':
2992 next();
aliguori376253e2009-03-05 23:01:23 +00002993 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002994 break;
2995 case '(':
2996 next();
aliguori376253e2009-03-05 23:01:23 +00002997 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002998 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002999 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003000 }
3001 next();
3002 break;
bellard81d09122004-07-14 17:21:37 +00003003 case '\'':
3004 pch++;
3005 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003006 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003007 n = *pch;
3008 pch++;
3009 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003010 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003011 next();
3012 break;
bellard9307c4c2004-04-04 12:57:25 +00003013 case '$':
3014 {
3015 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003016 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003017
bellard9307c4c2004-04-04 12:57:25 +00003018 pch++;
3019 q = buf;
3020 while ((*pch >= 'a' && *pch <= 'z') ||
3021 (*pch >= 'A' && *pch <= 'Z') ||
3022 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003023 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003024 if ((q - buf) < sizeof(buf) - 1)
3025 *q++ = *pch;
3026 pch++;
3027 }
blueswir1cd390082008-11-16 13:53:32 +00003028 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003029 pch++;
3030 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003031 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003032 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003033 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003034 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003035 }
3036 break;
3037 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003038 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003039 n = 0;
3040 break;
3041 default:
blueswir17743e582007-09-24 18:39:04 +00003042#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003043 n = strtoull(pch, &p, 0);
3044#else
bellard9307c4c2004-04-04 12:57:25 +00003045 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003046#endif
bellard9307c4c2004-04-04 12:57:25 +00003047 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003048 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003049 }
3050 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003051 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003052 pch++;
3053 break;
3054 }
3055 return n;
3056}
3057
3058
aliguori376253e2009-03-05 23:01:23 +00003059static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003060{
blueswir1c2efc952007-09-25 17:28:42 +00003061 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003062 int op;
ths3b46e622007-09-17 08:09:54 +00003063
aliguori376253e2009-03-05 23:01:23 +00003064 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003065 for(;;) {
3066 op = *pch;
3067 if (op != '*' && op != '/' && op != '%')
3068 break;
3069 next();
aliguori376253e2009-03-05 23:01:23 +00003070 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003071 switch(op) {
3072 default:
3073 case '*':
3074 val *= val2;
3075 break;
3076 case '/':
3077 case '%':
ths5fafdf22007-09-16 21:08:06 +00003078 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003079 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003080 if (op == '/')
3081 val /= val2;
3082 else
3083 val %= val2;
3084 break;
3085 }
3086 }
3087 return val;
3088}
3089
aliguori376253e2009-03-05 23:01:23 +00003090static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003091{
blueswir1c2efc952007-09-25 17:28:42 +00003092 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003093 int op;
bellard9307c4c2004-04-04 12:57:25 +00003094
aliguori376253e2009-03-05 23:01:23 +00003095 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003096 for(;;) {
3097 op = *pch;
3098 if (op != '&' && op != '|' && op != '^')
3099 break;
3100 next();
aliguori376253e2009-03-05 23:01:23 +00003101 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003102 switch(op) {
3103 default:
3104 case '&':
3105 val &= val2;
3106 break;
3107 case '|':
3108 val |= val2;
3109 break;
3110 case '^':
3111 val ^= val2;
3112 break;
3113 }
3114 }
3115 return val;
3116}
3117
aliguori376253e2009-03-05 23:01:23 +00003118static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003119{
blueswir1c2efc952007-09-25 17:28:42 +00003120 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003121 int op;
bellard9307c4c2004-04-04 12:57:25 +00003122
aliguori376253e2009-03-05 23:01:23 +00003123 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003124 for(;;) {
3125 op = *pch;
3126 if (op != '+' && op != '-')
3127 break;
3128 next();
aliguori376253e2009-03-05 23:01:23 +00003129 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003130 if (op == '+')
3131 val += val2;
3132 else
3133 val -= val2;
3134 }
3135 return val;
3136}
3137
aliguori376253e2009-03-05 23:01:23 +00003138static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003139{
3140 pch = *pp;
3141 if (setjmp(expr_env)) {
3142 *pp = pch;
3143 return -1;
3144 }
blueswir1cd390082008-11-16 13:53:32 +00003145 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003146 pch++;
aliguori376253e2009-03-05 23:01:23 +00003147 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003148 *pp = pch;
3149 return 0;
3150}
3151
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003152static int get_double(Monitor *mon, double *pval, const char **pp)
3153{
3154 const char *p = *pp;
3155 char *tailp;
3156 double d;
3157
3158 d = strtod(p, &tailp);
3159 if (tailp == p) {
3160 monitor_printf(mon, "Number expected\n");
3161 return -1;
3162 }
3163 if (d != d || d - d != 0) {
3164 /* NaN or infinity */
3165 monitor_printf(mon, "Bad number\n");
3166 return -1;
3167 }
3168 *pval = d;
3169 *pp = tailp;
3170 return 0;
3171}
3172
bellard9307c4c2004-04-04 12:57:25 +00003173static int get_str(char *buf, int buf_size, const char **pp)
3174{
3175 const char *p;
3176 char *q;
3177 int c;
3178
bellard81d09122004-07-14 17:21:37 +00003179 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003180 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003181 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003182 p++;
3183 if (*p == '\0') {
3184 fail:
bellard81d09122004-07-14 17:21:37 +00003185 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003186 *pp = p;
3187 return -1;
3188 }
bellard9307c4c2004-04-04 12:57:25 +00003189 if (*p == '\"') {
3190 p++;
3191 while (*p != '\0' && *p != '\"') {
3192 if (*p == '\\') {
3193 p++;
3194 c = *p++;
3195 switch(c) {
3196 case 'n':
3197 c = '\n';
3198 break;
3199 case 'r':
3200 c = '\r';
3201 break;
3202 case '\\':
3203 case '\'':
3204 case '\"':
3205 break;
3206 default:
3207 qemu_printf("unsupported escape code: '\\%c'\n", c);
3208 goto fail;
3209 }
3210 if ((q - buf) < buf_size - 1) {
3211 *q++ = c;
3212 }
3213 } else {
3214 if ((q - buf) < buf_size - 1) {
3215 *q++ = *p;
3216 }
3217 p++;
3218 }
3219 }
3220 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003221 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003222 goto fail;
3223 }
3224 p++;
3225 } else {
blueswir1cd390082008-11-16 13:53:32 +00003226 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003227 if ((q - buf) < buf_size - 1) {
3228 *q++ = *p;
3229 }
3230 p++;
3231 }
bellard9307c4c2004-04-04 12:57:25 +00003232 }
bellard81d09122004-07-14 17:21:37 +00003233 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003234 *pp = p;
3235 return 0;
3236}
3237
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003238/*
3239 * Store the command-name in cmdname, and return a pointer to
3240 * the remaining of the command string.
3241 */
3242static const char *get_command_name(const char *cmdline,
3243 char *cmdname, size_t nlen)
3244{
3245 size_t len;
3246 const char *p, *pstart;
3247
3248 p = cmdline;
3249 while (qemu_isspace(*p))
3250 p++;
3251 if (*p == '\0')
3252 return NULL;
3253 pstart = p;
3254 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3255 p++;
3256 len = p - pstart;
3257 if (len > nlen - 1)
3258 len = nlen - 1;
3259 memcpy(cmdname, pstart, len);
3260 cmdname[len] = '\0';
3261 return p;
3262}
3263
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003264/**
3265 * Read key of 'type' into 'key' and return the current
3266 * 'type' pointer.
3267 */
3268static char *key_get_info(const char *type, char **key)
3269{
3270 size_t len;
3271 char *p, *str;
3272
3273 if (*type == ',')
3274 type++;
3275
3276 p = strchr(type, ':');
3277 if (!p) {
3278 *key = NULL;
3279 return NULL;
3280 }
3281 len = p - type;
3282
3283 str = qemu_malloc(len + 1);
3284 memcpy(str, type, len);
3285 str[len] = '\0';
3286
3287 *key = str;
3288 return ++p;
3289}
3290
bellard9307c4c2004-04-04 12:57:25 +00003291static int default_fmt_format = 'x';
3292static int default_fmt_size = 4;
3293
3294#define MAX_ARGS 16
3295
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003296static int is_valid_option(const char *c, const char *typestr)
3297{
3298 char option[3];
3299
3300 option[0] = '-';
3301 option[1] = *c;
3302 option[2] = '\0';
3303
3304 typestr = strstr(typestr, option);
3305 return (typestr != NULL);
3306}
3307
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003308static const mon_cmd_t *monitor_find_command(const char *cmdname)
3309{
3310 const mon_cmd_t *cmd;
3311
3312 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3313 if (compare_cmd(cmdname, cmd->name)) {
3314 return cmd;
3315 }
3316 }
3317
3318 return NULL;
3319}
3320
Anthony Liguoric227f092009-10-01 16:12:16 -05003321static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003322 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003323 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003324{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003325 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003326 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003327 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003328 char cmdname[256];
3329 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003330 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003331
3332#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003333 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003334#endif
ths3b46e622007-09-17 08:09:54 +00003335
bellard9307c4c2004-04-04 12:57:25 +00003336 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003337 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3338 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003339 return NULL;
ths3b46e622007-09-17 08:09:54 +00003340
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003341 cmd = monitor_find_command(cmdname);
3342 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003343 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003344 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003345 }
bellard9307c4c2004-04-04 12:57:25 +00003346
bellard9307c4c2004-04-04 12:57:25 +00003347 /* parse the parameters */
3348 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003349 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003350 typestr = key_get_info(typestr, &key);
3351 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003352 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003353 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003354 typestr++;
3355 switch(c) {
3356 case 'F':
bellard81d09122004-07-14 17:21:37 +00003357 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003358 case 's':
3359 {
3360 int ret;
ths3b46e622007-09-17 08:09:54 +00003361
blueswir1cd390082008-11-16 13:53:32 +00003362 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003363 p++;
3364 if (*typestr == '?') {
3365 typestr++;
3366 if (*p == '\0') {
3367 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003368 break;
bellard9307c4c2004-04-04 12:57:25 +00003369 }
3370 }
3371 ret = get_str(buf, sizeof(buf), &p);
3372 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003373 switch(c) {
3374 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003375 monitor_printf(mon, "%s: filename expected\n",
3376 cmdname);
bellard81d09122004-07-14 17:21:37 +00003377 break;
3378 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003379 monitor_printf(mon, "%s: block device name expected\n",
3380 cmdname);
bellard81d09122004-07-14 17:21:37 +00003381 break;
3382 default:
aliguori376253e2009-03-05 23:01:23 +00003383 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003384 break;
3385 }
bellard9307c4c2004-04-04 12:57:25 +00003386 goto fail;
3387 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003388 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003389 }
3390 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003391 case 'O':
3392 {
3393 QemuOptsList *opts_list;
3394 QemuOpts *opts;
3395
3396 opts_list = qemu_find_opts(key);
3397 if (!opts_list || opts_list->desc->name) {
3398 goto bad_type;
3399 }
3400 while (qemu_isspace(*p)) {
3401 p++;
3402 }
3403 if (!*p)
3404 break;
3405 if (get_str(buf, sizeof(buf), &p) < 0) {
3406 goto fail;
3407 }
3408 opts = qemu_opts_parse(opts_list, buf, 1);
3409 if (!opts) {
3410 goto fail;
3411 }
3412 qemu_opts_to_qdict(opts, qdict);
3413 qemu_opts_del(opts);
3414 }
3415 break;
bellard9307c4c2004-04-04 12:57:25 +00003416 case '/':
3417 {
3418 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003419
blueswir1cd390082008-11-16 13:53:32 +00003420 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003421 p++;
3422 if (*p == '/') {
3423 /* format found */
3424 p++;
3425 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003426 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003427 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003428 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003429 count = count * 10 + (*p - '0');
3430 p++;
3431 }
3432 }
3433 size = -1;
3434 format = -1;
3435 for(;;) {
3436 switch(*p) {
3437 case 'o':
3438 case 'd':
3439 case 'u':
3440 case 'x':
3441 case 'i':
3442 case 'c':
3443 format = *p++;
3444 break;
3445 case 'b':
3446 size = 1;
3447 p++;
3448 break;
3449 case 'h':
3450 size = 2;
3451 p++;
3452 break;
3453 case 'w':
3454 size = 4;
3455 p++;
3456 break;
3457 case 'g':
3458 case 'L':
3459 size = 8;
3460 p++;
3461 break;
3462 default:
3463 goto next;
3464 }
3465 }
3466 next:
blueswir1cd390082008-11-16 13:53:32 +00003467 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003468 monitor_printf(mon, "invalid char in format: '%c'\n",
3469 *p);
bellard9307c4c2004-04-04 12:57:25 +00003470 goto fail;
3471 }
bellard9307c4c2004-04-04 12:57:25 +00003472 if (format < 0)
3473 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003474 if (format != 'i') {
3475 /* for 'i', not specifying a size gives -1 as size */
3476 if (size < 0)
3477 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003478 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003479 }
bellard9307c4c2004-04-04 12:57:25 +00003480 default_fmt_format = format;
3481 } else {
3482 count = 1;
3483 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003484 if (format != 'i') {
3485 size = default_fmt_size;
3486 } else {
3487 size = -1;
3488 }
bellard9307c4c2004-04-04 12:57:25 +00003489 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003490 qdict_put(qdict, "count", qint_from_int(count));
3491 qdict_put(qdict, "format", qint_from_int(format));
3492 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003493 }
3494 break;
3495 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003496 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003497 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003498 {
blueswir1c2efc952007-09-25 17:28:42 +00003499 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003500
blueswir1cd390082008-11-16 13:53:32 +00003501 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003502 p++;
bellard34405572004-06-08 00:55:58 +00003503 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003504 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003505 if (*p == '\0') {
3506 typestr++;
3507 break;
3508 }
bellard34405572004-06-08 00:55:58 +00003509 } else {
3510 if (*p == '.') {
3511 p++;
blueswir1cd390082008-11-16 13:53:32 +00003512 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003513 p++;
bellard34405572004-06-08 00:55:58 +00003514 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003515 typestr++;
3516 break;
bellard34405572004-06-08 00:55:58 +00003517 }
3518 }
bellard13224a82006-07-14 22:03:35 +00003519 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003520 }
aliguori376253e2009-03-05 23:01:23 +00003521 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003522 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003523 /* Check if 'i' is greater than 32-bit */
3524 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3525 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3526 monitor_printf(mon, "integer is for 32-bit values\n");
3527 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003528 } else if (c == 'M') {
3529 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003530 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003531 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003532 }
3533 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003534 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003535 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003536 {
3537 double val;
3538
3539 while (qemu_isspace(*p))
3540 p++;
3541 if (*typestr == '?') {
3542 typestr++;
3543 if (*p == '\0') {
3544 break;
3545 }
3546 }
3547 if (get_double(mon, &val, &p) < 0) {
3548 goto fail;
3549 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003550 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003551 switch (*p) {
3552 case 'K': case 'k':
3553 val *= 1 << 10; p++; break;
3554 case 'M': case 'm':
3555 val *= 1 << 20; p++; break;
3556 case 'G': case 'g':
3557 val *= 1 << 30; p++; break;
3558 }
3559 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003560 if (c == 'T' && p[0] && p[1] == 's') {
3561 switch (*p) {
3562 case 'm':
3563 val /= 1e3; p += 2; break;
3564 case 'u':
3565 val /= 1e6; p += 2; break;
3566 case 'n':
3567 val /= 1e9; p += 2; break;
3568 }
3569 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003570 if (*p && !qemu_isspace(*p)) {
3571 monitor_printf(mon, "Unknown unit suffix\n");
3572 goto fail;
3573 }
3574 qdict_put(qdict, key, qfloat_from_double(val));
3575 }
3576 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003577 case 'b':
3578 {
3579 const char *beg;
3580 int val;
3581
3582 while (qemu_isspace(*p)) {
3583 p++;
3584 }
3585 beg = p;
3586 while (qemu_isgraph(*p)) {
3587 p++;
3588 }
3589 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3590 val = 1;
3591 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3592 val = 0;
3593 } else {
3594 monitor_printf(mon, "Expected 'on' or 'off'\n");
3595 goto fail;
3596 }
3597 qdict_put(qdict, key, qbool_from_int(val));
3598 }
3599 break;
bellard9307c4c2004-04-04 12:57:25 +00003600 case '-':
3601 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003602 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003603 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003604 /* option */
ths3b46e622007-09-17 08:09:54 +00003605
bellard9307c4c2004-04-04 12:57:25 +00003606 c = *typestr++;
3607 if (c == '\0')
3608 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003609 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003610 p++;
bellard9307c4c2004-04-04 12:57:25 +00003611 if (*p == '-') {
3612 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003613 if(c != *p) {
3614 if(!is_valid_option(p, typestr)) {
3615
3616 monitor_printf(mon, "%s: unsupported option -%c\n",
3617 cmdname, *p);
3618 goto fail;
3619 } else {
3620 skip_key = 1;
3621 }
bellard9307c4c2004-04-04 12:57:25 +00003622 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003623 if(skip_key) {
3624 p = tmp;
3625 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003626 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003627 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003628 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003629 }
bellard9307c4c2004-04-04 12:57:25 +00003630 }
bellard9307c4c2004-04-04 12:57:25 +00003631 }
3632 break;
3633 default:
3634 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003635 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003636 goto fail;
3637 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003638 qemu_free(key);
3639 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003640 }
3641 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003642 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003643 p++;
3644 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003645 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3646 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003647 goto fail;
3648 }
3649
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003650 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003651
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003652fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003653 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003654 return NULL;
3655}
3656
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003657void monitor_set_error(Monitor *mon, QError *qerror)
3658{
3659 /* report only the first error */
3660 if (!mon->error) {
3661 mon->error = qerror;
3662 } else {
3663 MON_DEBUG("Additional error report at %s:%d\n",
3664 qerror->file, qerror->linenr);
3665 QDECREF(qerror);
3666 }
3667}
3668
Adam Litke940cc302010-01-25 12:18:44 -06003669static int is_async_return(const QObject *data)
3670{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003671 if (data && qobject_type(data) == QTYPE_QDICT) {
3672 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3673 }
3674
3675 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003676}
3677
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003678static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3679{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003680 if (monitor_ctrl_mode(mon)) {
3681 if (ret && !monitor_has_error(mon)) {
3682 /*
3683 * If it returns failure, it must have passed on error.
3684 *
3685 * Action: Report an internal error to the client if in QMP.
3686 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003687 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003688 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3689 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003690 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003691
3692#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003693 if (!ret && monitor_has_error(mon)) {
3694 /*
3695 * If it returns success, it must not have passed an error.
3696 *
3697 * Action: Report the passed error to the client.
3698 */
3699 MON_DEBUG("command '%s' returned success but passed an error\n",
3700 cmd->name);
3701 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003702
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003703 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3704 /*
3705 * Handlers should not call Monitor print functions.
3706 *
3707 * Action: Ignore them in QMP.
3708 *
3709 * (XXX: we don't check any 'info' or 'query' command here
3710 * because the user print function _is_ called by do_info(), hence
3711 * we will trigger this check. This problem will go away when we
3712 * make 'query' commands real and kill do_info())
3713 */
3714 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3715 cmd->name, mon_print_count_get(mon));
3716 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003717#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003718 } else {
3719 assert(!monitor_has_error(mon));
3720 QDECREF(mon->error);
3721 mon->error = NULL;
3722 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003723}
3724
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003725static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3726 const QDict *params)
3727{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003728 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003729 QObject *data = NULL;
3730
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003731 mon_print_count_init(mon);
3732
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003733 ret = cmd->mhandler.cmd_new(mon, params, &data);
3734 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003735
Adam Litke940cc302010-01-25 12:18:44 -06003736 if (is_async_return(data)) {
3737 /*
3738 * Asynchronous commands have no initial return data but they can
3739 * generate errors. Data is returned via the async completion handler.
3740 */
3741 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3742 monitor_protocol_emitter(mon, NULL);
3743 }
3744 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003745 /* Monitor Protocol */
3746 monitor_protocol_emitter(mon, data);
3747 } else {
3748 /* User Protocol */
3749 if (data)
3750 cmd->user_print(mon, data);
3751 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003752
3753 qobject_decref(data);
3754}
3755
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003756static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003757{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003758 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003759 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003760
3761 qdict = qdict_new();
3762
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003763 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003764 if (!cmd)
3765 goto out;
3766
Adam Litke940cc302010-01-25 12:18:44 -06003767 if (monitor_handler_is_async(cmd)) {
3768 user_async_cmd_handler(mon, cmd, qdict);
3769 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003770 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003771 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003772 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003773 }
3774
Luiz Capitulino13917be2009-10-07 13:41:54 -03003775out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003776 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003777}
3778
bellard81d09122004-07-14 17:21:37 +00003779static void cmd_completion(const char *name, const char *list)
3780{
3781 const char *p, *pstart;
3782 char cmd[128];
3783 int len;
3784
3785 p = list;
3786 for(;;) {
3787 pstart = p;
3788 p = strchr(p, '|');
3789 if (!p)
3790 p = pstart + strlen(pstart);
3791 len = p - pstart;
3792 if (len > sizeof(cmd) - 2)
3793 len = sizeof(cmd) - 2;
3794 memcpy(cmd, pstart, len);
3795 cmd[len] = '\0';
3796 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003797 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003798 }
3799 if (*p == '\0')
3800 break;
3801 p++;
3802 }
3803}
3804
3805static void file_completion(const char *input)
3806{
3807 DIR *ffs;
3808 struct dirent *d;
3809 char path[1024];
3810 char file[1024], file_prefix[1024];
3811 int input_path_len;
3812 const char *p;
3813
ths5fafdf22007-09-16 21:08:06 +00003814 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003815 if (!p) {
3816 input_path_len = 0;
3817 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003818 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003819 } else {
3820 input_path_len = p - input + 1;
3821 memcpy(path, input, input_path_len);
3822 if (input_path_len > sizeof(path) - 1)
3823 input_path_len = sizeof(path) - 1;
3824 path[input_path_len] = '\0';
3825 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3826 }
3827#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003828 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3829 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003830#endif
3831 ffs = opendir(path);
3832 if (!ffs)
3833 return;
3834 for(;;) {
3835 struct stat sb;
3836 d = readdir(ffs);
3837 if (!d)
3838 break;
3839 if (strstart(d->d_name, file_prefix, NULL)) {
3840 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003841 if (input_path_len < sizeof(file))
3842 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3843 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003844 /* stat the file to find out if it's a directory.
3845 * In that case add a slash to speed up typing long paths
3846 */
3847 stat(file, &sb);
3848 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003849 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003850 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003851 }
3852 }
3853 closedir(ffs);
3854}
3855
aliguori51de9762009-03-05 23:00:43 +00003856static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003857{
aliguori51de9762009-03-05 23:00:43 +00003858 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003859 const char *input = opaque;
3860
3861 if (input[0] == '\0' ||
3862 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003863 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003864 }
3865}
3866
3867/* NOTE: this parser is an approximate form of the real command parser */
3868static void parse_cmdline(const char *cmdline,
3869 int *pnb_args, char **args)
3870{
3871 const char *p;
3872 int nb_args, ret;
3873 char buf[1024];
3874
3875 p = cmdline;
3876 nb_args = 0;
3877 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003878 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003879 p++;
3880 if (*p == '\0')
3881 break;
3882 if (nb_args >= MAX_ARGS)
3883 break;
3884 ret = get_str(buf, sizeof(buf), &p);
3885 args[nb_args] = qemu_strdup(buf);
3886 nb_args++;
3887 if (ret < 0)
3888 break;
3889 }
3890 *pnb_args = nb_args;
3891}
3892
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003893static const char *next_arg_type(const char *typestr)
3894{
3895 const char *p = strchr(typestr, ':');
3896 return (p != NULL ? ++p : typestr);
3897}
3898
aliguori4c36ba32009-03-05 23:01:37 +00003899static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003900{
3901 const char *cmdname;
3902 char *args[MAX_ARGS];
3903 int nb_args, i, len;
3904 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003905 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003906 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003907
3908 parse_cmdline(cmdline, &nb_args, args);
3909#ifdef DEBUG_COMPLETION
3910 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003911 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003912 }
3913#endif
3914
3915 /* if the line ends with a space, it means we want to complete the
3916 next arg */
3917 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003918 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003919 if (nb_args >= MAX_ARGS) {
3920 goto cleanup;
3921 }
bellard81d09122004-07-14 17:21:37 +00003922 args[nb_args++] = qemu_strdup("");
3923 }
3924 if (nb_args <= 1) {
3925 /* command completion */
3926 if (nb_args == 0)
3927 cmdname = "";
3928 else
3929 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003930 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003931 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003932 cmd_completion(cmdname, cmd->name);
3933 }
3934 } else {
3935 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02003936 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3937 if (compare_cmd(args[0], cmd->name)) {
3938 break;
3939 }
bellard81d09122004-07-14 17:21:37 +00003940 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003941 if (!cmd->name) {
3942 goto cleanup;
3943 }
3944
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003945 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003946 for(i = 0; i < nb_args - 2; i++) {
3947 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003948 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003949 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003950 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003951 }
3952 }
3953 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003954 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003955 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003956 }
bellard81d09122004-07-14 17:21:37 +00003957 switch(*ptype) {
3958 case 'F':
3959 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003960 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003961 file_completion(str);
3962 break;
3963 case 'B':
3964 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003965 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003966 bdrv_iterate(block_completion_it, (void *)str);
3967 break;
bellard7fe48482004-10-09 18:08:01 +00003968 case 's':
3969 /* XXX: more generic ? */
3970 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003971 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003972 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3973 cmd_completion(str, cmd->name);
3974 }
bellard64866c32006-05-07 18:03:31 +00003975 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003976 char *sep = strrchr(str, '-');
3977 if (sep)
3978 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003979 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003980 for(key = key_defs; key->name != NULL; key++) {
3981 cmd_completion(str, key->name);
3982 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003983 } else if (!strcmp(cmd->name, "help|?")) {
3984 readline_set_completion_index(cur_mon->rs, strlen(str));
3985 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3986 cmd_completion(str, cmd->name);
3987 }
bellard7fe48482004-10-09 18:08:01 +00003988 }
3989 break;
bellard81d09122004-07-14 17:21:37 +00003990 default:
3991 break;
3992 }
3993 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003994
3995cleanup:
3996 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00003997 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02003998 }
bellard81d09122004-07-14 17:21:37 +00003999}
4000
aliguori731b0362009-03-05 23:01:42 +00004001static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004002{
aliguori731b0362009-03-05 23:01:42 +00004003 Monitor *mon = opaque;
4004
Jan Kiszkac62313b2009-12-04 14:05:29 +01004005 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004006}
4007
Luiz Capitulino09069b12010-02-04 18:10:06 -02004008static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4009{
4010 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4011 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4012}
4013
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004014/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004015 * Argument validation rules:
4016 *
4017 * 1. The argument must exist in cmd_args qdict
4018 * 2. The argument type must be the expected one
4019 *
4020 * Special case: If the argument doesn't exist in cmd_args and
4021 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4022 * checking is skipped for it.
4023 */
4024static int check_client_args_type(const QDict *client_args,
4025 const QDict *cmd_args, int flags)
4026{
4027 const QDictEntry *ent;
4028
4029 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4030 QObject *obj;
4031 QString *arg_type;
4032 const QObject *client_arg = qdict_entry_value(ent);
4033 const char *client_arg_name = qdict_entry_key(ent);
4034
4035 obj = qdict_get(cmd_args, client_arg_name);
4036 if (!obj) {
4037 if (flags & QMP_ACCEPT_UNKNOWNS) {
4038 /* handler accepts unknowns */
4039 continue;
4040 }
4041 /* client arg doesn't exist */
4042 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4043 return -1;
4044 }
4045
4046 arg_type = qobject_to_qstring(obj);
4047 assert(arg_type != NULL);
4048
4049 /* check if argument's type is correct */
4050 switch (qstring_get_str(arg_type)[0]) {
4051 case 'F':
4052 case 'B':
4053 case 's':
4054 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4055 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4056 "string");
4057 return -1;
4058 }
4059 break;
4060 case 'i':
4061 case 'l':
4062 case 'M':
4063 if (qobject_type(client_arg) != QTYPE_QINT) {
4064 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4065 "int");
4066 return -1;
4067 }
4068 break;
4069 case 'f':
4070 case 'T':
4071 if (qobject_type(client_arg) != QTYPE_QINT &&
4072 qobject_type(client_arg) != QTYPE_QFLOAT) {
4073 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4074 "number");
4075 return -1;
4076 }
4077 break;
4078 case 'b':
4079 case '-':
4080 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4081 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4082 "bool");
4083 return -1;
4084 }
4085 break;
4086 case 'O':
4087 assert(flags & QMP_ACCEPT_UNKNOWNS);
4088 break;
4089 case '/':
4090 case '.':
4091 /*
4092 * These types are not supported by QMP and thus are not
4093 * handled here. Fall through.
4094 */
4095 default:
4096 abort();
4097 }
4098 }
4099
4100 return 0;
4101}
4102
4103/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004104 * - Check if the client has passed all mandatory args
4105 * - Set special flags for argument validation
4106 */
4107static int check_mandatory_args(const QDict *cmd_args,
4108 const QDict *client_args, int *flags)
4109{
4110 const QDictEntry *ent;
4111
4112 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4113 const char *cmd_arg_name = qdict_entry_key(ent);
4114 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4115 assert(type != NULL);
4116
4117 if (qstring_get_str(type)[0] == 'O') {
4118 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4119 *flags |= QMP_ACCEPT_UNKNOWNS;
4120 } else if (qstring_get_str(type)[0] != '-' &&
4121 qstring_get_str(type)[1] != '?' &&
4122 !qdict_haskey(client_args, cmd_arg_name)) {
4123 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4124 return -1;
4125 }
4126 }
4127
4128 return 0;
4129}
4130
4131static QDict *qdict_from_args_type(const char *args_type)
4132{
4133 int i;
4134 QDict *qdict;
4135 QString *key, *type, *cur_qs;
4136
4137 assert(args_type != NULL);
4138
4139 qdict = qdict_new();
4140
4141 if (args_type == NULL || args_type[0] == '\0') {
4142 /* no args, empty qdict */
4143 goto out;
4144 }
4145
4146 key = qstring_new();
4147 type = qstring_new();
4148
4149 cur_qs = key;
4150
4151 for (i = 0;; i++) {
4152 switch (args_type[i]) {
4153 case ',':
4154 case '\0':
4155 qdict_put(qdict, qstring_get_str(key), type);
4156 QDECREF(key);
4157 if (args_type[i] == '\0') {
4158 goto out;
4159 }
4160 type = qstring_new(); /* qdict has ref */
4161 cur_qs = key = qstring_new();
4162 break;
4163 case ':':
4164 cur_qs = type;
4165 break;
4166 default:
4167 qstring_append_chr(cur_qs, args_type[i]);
4168 break;
4169 }
4170 }
4171
4172out:
4173 return qdict;
4174}
4175
4176/*
4177 * Client argument checking rules:
4178 *
4179 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004180 * 2. Each argument provided by the client must be expected
4181 * 3. Each argument provided by the client must have the type expected
4182 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004183 */
4184static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4185{
4186 int flags, err;
4187 QDict *cmd_args;
4188
4189 cmd_args = qdict_from_args_type(cmd->args_type);
4190
4191 flags = 0;
4192 err = check_mandatory_args(cmd_args, client_args, &flags);
4193 if (err) {
4194 goto out;
4195 }
4196
Luiz Capitulino4af91932010-06-22 11:44:05 -03004197 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004198
4199out:
4200 QDECREF(cmd_args);
4201 return err;
4202}
4203
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004204/*
4205 * Input object checking rules
4206 *
4207 * 1. Input object must be a dict
4208 * 2. The "execute" key must exist
4209 * 3. The "execute" key must be a string
4210 * 4. If the "arguments" key exists, it must be a dict
4211 * 5. If the "id" key exists, it can be anything (ie. json-value)
4212 * 6. Any argument not listed above is considered invalid
4213 */
4214static QDict *qmp_check_input_obj(QObject *input_obj)
4215{
4216 const QDictEntry *ent;
4217 int has_exec_key = 0;
4218 QDict *input_dict;
4219
4220 if (qobject_type(input_obj) != QTYPE_QDICT) {
4221 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4222 return NULL;
4223 }
4224
4225 input_dict = qobject_to_qdict(input_obj);
4226
4227 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4228 const char *arg_name = qdict_entry_key(ent);
4229 const QObject *arg_obj = qdict_entry_value(ent);
4230
4231 if (!strcmp(arg_name, "execute")) {
4232 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4233 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4234 "string");
4235 return NULL;
4236 }
4237 has_exec_key = 1;
4238 } else if (!strcmp(arg_name, "arguments")) {
4239 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4240 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4241 "object");
4242 return NULL;
4243 }
4244 } else if (!strcmp(arg_name, "id")) {
4245 /* FIXME: check duplicated IDs for async commands */
4246 } else {
4247 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4248 return NULL;
4249 }
4250 }
4251
4252 if (!has_exec_key) {
4253 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4254 return NULL;
4255 }
4256
4257 return input_dict;
4258}
4259
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004260static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4261{
4262 int err;
4263 QObject *obj;
4264 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004265 const mon_cmd_t *cmd;
4266 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004267 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004268
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004269 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004270
4271 obj = json_parser_parse(tokens, NULL);
4272 if (!obj) {
4273 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004274 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004275 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004276 }
4277
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004278 input = qmp_check_input_obj(obj);
4279 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004280 qobject_decref(obj);
4281 goto err_out;
4282 }
4283
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004284 mon->mc->id = qdict_get(input, "id");
4285 qobject_incref(mon->mc->id);
4286
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004287 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004288 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004289 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004290 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004291 }
4292
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004293 /*
4294 * XXX: We need this special case until we get info handlers
4295 * converted into 'query-' commands
4296 */
4297 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004298 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004299 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004300 } else if (strstart(cmd_name, "query-", &info_item)) {
4301 cmd = monitor_find_command("info");
4302 qdict_put_obj(input, "arguments",
4303 qobject_from_jsonf("{ 'item': %s }", info_item));
4304 } else {
4305 cmd = monitor_find_command(cmd_name);
Jan Kiszkaa6c4d362010-06-28 18:27:47 +02004306 if (!cmd || !monitor_handler_ported(cmd)
4307 || monitor_cmd_user_only(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004308 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004309 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004310 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004311 }
4312
4313 obj = qdict_get(input, "arguments");
4314 if (!obj) {
4315 args = qdict_new();
4316 } else {
4317 args = qobject_to_qdict(obj);
4318 QINCREF(args);
4319 }
4320
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004321 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004322 if (err < 0) {
4323 goto err_out;
4324 }
4325
Adam Litke940cc302010-01-25 12:18:44 -06004326 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004327 err = qmp_async_cmd_handler(mon, cmd, args);
4328 if (err) {
4329 /* emit the error response */
4330 goto err_out;
4331 }
Adam Litke940cc302010-01-25 12:18:44 -06004332 } else {
4333 monitor_call_handler(mon, cmd, args);
4334 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004335
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004336 goto out;
4337
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004338err_out:
4339 monitor_protocol_emitter(mon, NULL);
4340out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004341 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004342 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004343}
4344
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004345/**
4346 * monitor_control_read(): Read and handle QMP input
4347 */
4348static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4349{
4350 Monitor *old_mon = cur_mon;
4351
4352 cur_mon = opaque;
4353
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004354 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004355
4356 cur_mon = old_mon;
4357}
4358
aliguori731b0362009-03-05 23:01:42 +00004359static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004360{
aliguori731b0362009-03-05 23:01:42 +00004361 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004362 int i;
aliguori376253e2009-03-05 23:01:23 +00004363
aliguori731b0362009-03-05 23:01:42 +00004364 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004365
aliguoricde76ee2009-03-05 23:01:51 +00004366 if (cur_mon->rs) {
4367 for (i = 0; i < size; i++)
4368 readline_handle_byte(cur_mon->rs, buf[i]);
4369 } else {
4370 if (size == 0 || buf[size - 1] != 0)
4371 monitor_printf(cur_mon, "corrupted command\n");
4372 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004373 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004374 }
aliguori731b0362009-03-05 23:01:42 +00004375
4376 cur_mon = old_mon;
4377}
aliguorid8f44602008-10-06 13:52:44 +00004378
aliguori376253e2009-03-05 23:01:23 +00004379static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004380{
aliguori731b0362009-03-05 23:01:42 +00004381 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004382 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004383 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004384}
4385
aliguoricde76ee2009-03-05 23:01:51 +00004386int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004387{
aliguoricde76ee2009-03-05 23:01:51 +00004388 if (!mon->rs)
4389 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004390 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004391 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004392}
4393
aliguori376253e2009-03-05 23:01:23 +00004394void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004395{
aliguoricde76ee2009-03-05 23:01:51 +00004396 if (!mon->rs)
4397 return;
aliguori731b0362009-03-05 23:01:42 +00004398 if (--mon->suspend_cnt == 0)
4399 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004400}
4401
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004402static QObject *get_qmp_greeting(void)
4403{
4404 QObject *ver;
4405
4406 do_info_version(NULL, &ver);
4407 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4408}
4409
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004410/**
4411 * monitor_control_event(): Print QMP gretting
4412 */
4413static void monitor_control_event(void *opaque, int event)
4414{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004415 QObject *data;
4416 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004417
Luiz Capitulino47116d12010-02-08 17:01:30 -02004418 switch (event) {
4419 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004420 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004421 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004422 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004423 monitor_json_emitter(mon, data);
4424 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004425 break;
4426 case CHR_EVENT_CLOSED:
4427 json_message_parser_destroy(&mon->mc->parser);
4428 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004429 }
4430}
4431
aliguori731b0362009-03-05 23:01:42 +00004432static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004433{
aliguori376253e2009-03-05 23:01:23 +00004434 Monitor *mon = opaque;
4435
aliguori2724b182009-03-05 23:01:47 +00004436 switch (event) {
4437 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004438 mon->mux_out = 0;
4439 if (mon->reset_seen) {
4440 readline_restart(mon->rs);
4441 monitor_resume(mon);
4442 monitor_flush(mon);
4443 } else {
4444 mon->suspend_cnt = 0;
4445 }
aliguori2724b182009-03-05 23:01:47 +00004446 break;
ths86e94de2007-01-05 22:01:59 +00004447
aliguori2724b182009-03-05 23:01:47 +00004448 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004449 if (mon->reset_seen) {
4450 if (mon->suspend_cnt == 0) {
4451 monitor_printf(mon, "\n");
4452 }
4453 monitor_flush(mon);
4454 monitor_suspend(mon);
4455 } else {
4456 mon->suspend_cnt++;
4457 }
4458 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004459 break;
4460
Amit Shahb6b8df52009-10-07 18:31:16 +05304461 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004462 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4463 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004464 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004465 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004466 }
4467 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004468 break;
4469 }
ths86e94de2007-01-05 22:01:59 +00004470}
4471
aliguori76655d62009-03-06 20:27:37 +00004472
4473/*
4474 * Local variables:
4475 * c-indent-level: 4
4476 * c-basic-offset: 4
4477 * tab-width: 8
4478 * End:
4479 */
4480
aliguori731b0362009-03-05 23:01:42 +00004481void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004482{
aliguori731b0362009-03-05 23:01:42 +00004483 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004484 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004485
4486 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004487 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004488 is_first_init = 0;
4489 }
aliguori87127162009-03-05 23:01:29 +00004490
4491 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004492
aliguori87127162009-03-05 23:01:29 +00004493 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004494 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004495 if (flags & MONITOR_USE_READLINE) {
4496 mon->rs = readline_init(mon, monitor_find_completion);
4497 monitor_read_command(mon, 0);
4498 }
aliguori87127162009-03-05 23:01:29 +00004499
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004500 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004501 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004502 /* Control mode requires special handlers */
4503 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4504 monitor_control_event, mon);
4505 } else {
4506 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4507 monitor_event, mon);
4508 }
aliguori87127162009-03-05 23:01:29 +00004509
Blue Swirl72cf2d42009-09-12 07:36:22 +00004510 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004511 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4512 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004513}
4514
aliguori376253e2009-03-05 23:01:23 +00004515static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004516{
aliguoribb5fc202009-03-05 23:01:15 +00004517 BlockDriverState *bs = opaque;
4518 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004519
aliguoribb5fc202009-03-05 23:01:15 +00004520 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004521 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004522 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004523 }
aliguori731b0362009-03-05 23:01:42 +00004524 if (mon->password_completion_cb)
4525 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004526
aliguori731b0362009-03-05 23:01:42 +00004527 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004528}
aliguoric0f4ce72009-03-05 23:01:01 +00004529
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004530int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4531 BlockDriverCompletionFunc *completion_cb,
4532 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004533{
aliguoricde76ee2009-03-05 23:01:51 +00004534 int err;
4535
aliguoribb5fc202009-03-05 23:01:15 +00004536 if (!bdrv_key_required(bs)) {
4537 if (completion_cb)
4538 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004539 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004540 }
aliguoric0f4ce72009-03-05 23:01:01 +00004541
Luiz Capitulino94171e12009-12-07 21:37:00 +01004542 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004543 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004544 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004545 }
4546
aliguori376253e2009-03-05 23:01:23 +00004547 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4548 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004549
aliguori731b0362009-03-05 23:01:42 +00004550 mon->password_completion_cb = completion_cb;
4551 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004552
aliguoricde76ee2009-03-05 23:01:51 +00004553 err = monitor_read_password(mon, bdrv_password_cb, bs);
4554
4555 if (err && completion_cb)
4556 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004557
4558 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004559}