blob: 8657c869be4b873e142851a04032f57983cd552b [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;
Adam Litke940cc302010-01-25 12:18:44 -0600116 int async;
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
Blue Swirl72cf2d42009-09-12 07:36:22 +0000181static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000182
Anthony Liguoric227f092009-10-01 16:12:16 -0500183static const mon_cmd_t mon_cmds[];
184static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000185
Markus Armbruster8631b602010-02-18 11:41:55 +0100186Monitor *cur_mon;
187Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000188
aliguori731b0362009-03-05 23:01:42 +0000189static void monitor_command_cb(Monitor *mon, const char *cmdline,
190 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000191
Luiz Capitulino09069b12010-02-04 18:10:06 -0200192static inline int qmp_cmd_mode(const Monitor *mon)
193{
194 return (mon->mc ? mon->mc->command_mode : 0);
195}
196
Luiz Capitulino418173c2009-11-26 22:58:51 -0200197/* Return true if in control mode, false otherwise */
198static inline int monitor_ctrl_mode(const Monitor *mon)
199{
200 return (mon->flags & MONITOR_USE_CONTROL);
201}
202
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100203/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
204int monitor_cur_is_qmp(void)
205{
206 return cur_mon && monitor_ctrl_mode(cur_mon);
207}
208
aliguori731b0362009-03-05 23:01:42 +0000209static void monitor_read_command(Monitor *mon, int show_prompt)
210{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200211 if (!mon->rs)
212 return;
213
aliguori731b0362009-03-05 23:01:42 +0000214 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
215 if (show_prompt)
216 readline_show_prompt(mon->rs);
217}
bellard6a00d602005-11-21 23:25:50 +0000218
aliguoricde76ee2009-03-05 23:01:51 +0000219static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
220 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000221{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100222 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100223 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100224 return -EINVAL;
225 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000226 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
227 /* prompt is printed on return from the command handler */
228 return 0;
229 } else {
230 monitor_printf(mon, "terminal does not support password prompting\n");
231 return -ENOTTY;
232 }
aliguoribb5fc202009-03-05 23:01:15 +0000233}
234
aliguori376253e2009-03-05 23:01:23 +0000235void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000236{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200237 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000238 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
239 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000240 }
241}
242
243/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000244static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000245{
ths60fe76f2007-12-16 03:02:09 +0000246 char c;
aliguori731b0362009-03-05 23:01:42 +0000247
bellard7e2515e2004-08-01 21:52:19 +0000248 for(;;) {
249 c = *str++;
250 if (c == '\0')
251 break;
bellard7ba12602006-07-14 20:26:42 +0000252 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000253 mon->outbuf[mon->outbuf_index++] = '\r';
254 mon->outbuf[mon->outbuf_index++] = c;
255 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
256 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000257 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000258 }
259}
260
aliguori376253e2009-03-05 23:01:23 +0000261void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000262{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200263 char buf[4096];
264
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200265 if (!mon)
266 return;
267
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200268 mon_print_count_inc(mon);
269
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200270 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200271 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200272 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200273
274 vsnprintf(buf, sizeof(buf), fmt, ap);
275 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000276}
277
aliguori376253e2009-03-05 23:01:23 +0000278void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000279{
280 va_list ap;
281 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000282 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000283 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000284}
285
aliguori376253e2009-03-05 23:01:23 +0000286void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000287{
288 int i;
289
290 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000291 switch (filename[i]) {
292 case ' ':
293 case '"':
294 case '\\':
295 monitor_printf(mon, "\\%c", filename[i]);
296 break;
297 case '\t':
298 monitor_printf(mon, "\\t");
299 break;
300 case '\r':
301 monitor_printf(mon, "\\r");
302 break;
303 case '\n':
304 monitor_printf(mon, "\\n");
305 break;
306 default:
307 monitor_printf(mon, "%c", filename[i]);
308 break;
309 }
thsfef30742006-12-22 14:11:32 +0000310 }
311}
312
bellard7fe48482004-10-09 18:08:01 +0000313static int monitor_fprintf(FILE *stream, const char *fmt, ...)
314{
315 va_list ap;
316 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000317 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000318 va_end(ap);
319 return 0;
320}
321
Luiz Capitulino13c74252009-10-07 13:41:55 -0300322static void monitor_user_noop(Monitor *mon, const QObject *data) { }
323
Luiz Capitulino13917be2009-10-07 13:41:54 -0300324static inline int monitor_handler_ported(const mon_cmd_t *cmd)
325{
326 return cmd->user_print != NULL;
327}
328
Adam Litke940cc302010-01-25 12:18:44 -0600329static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
330{
331 return cmd->async != 0;
332}
333
Luiz Capitulino8204a912009-11-18 23:05:31 -0200334static inline int monitor_has_error(const Monitor *mon)
335{
336 return mon->error != NULL;
337}
338
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200339static void monitor_json_emitter(Monitor *mon, const QObject *data)
340{
341 QString *json;
342
343 json = qobject_to_json(data);
344 assert(json != NULL);
345
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200346 qstring_append_chr(json, '\n');
347 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200348
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200349 QDECREF(json);
350}
351
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200352static void monitor_protocol_emitter(Monitor *mon, QObject *data)
353{
354 QDict *qmp;
355
356 qmp = qdict_new();
357
358 if (!monitor_has_error(mon)) {
359 /* success response */
360 if (data) {
361 qobject_incref(data);
362 qdict_put_obj(qmp, "return", data);
363 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200364 /* return an empty QDict by default */
365 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200366 }
367 } else {
368 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100369 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200370 qdict_put(qmp, "error", mon->error->error);
371 QINCREF(mon->error->error);
372 QDECREF(mon->error);
373 mon->error = NULL;
374 }
375
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200376 if (mon->mc->id) {
377 qdict_put_obj(qmp, "id", mon->mc->id);
378 mon->mc->id = NULL;
379 }
380
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200381 monitor_json_emitter(mon, QOBJECT(qmp));
382 QDECREF(qmp);
383}
384
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200385static void timestamp_put(QDict *qdict)
386{
387 int err;
388 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000389 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200390
Blue Swirld08d6f02009-12-04 18:06:39 +0000391 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200392 if (err < 0)
393 return;
394
395 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
396 "'microseconds': %" PRId64 " }",
397 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200398 qdict_put_obj(qdict, "timestamp", obj);
399}
400
401/**
402 * monitor_protocol_event(): Generate a Monitor event
403 *
404 * Event-specific data can be emitted through the (optional) 'data' parameter.
405 */
406void monitor_protocol_event(MonitorEvent event, QObject *data)
407{
408 QDict *qmp;
409 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600410 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200411
Blue Swirl242cd002009-12-04 18:05:45 +0000412 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200413
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200414 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000415 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200416 event_name = "SHUTDOWN";
417 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000418 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200419 event_name = "RESET";
420 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000421 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200422 event_name = "POWERDOWN";
423 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000424 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200425 event_name = "STOP";
426 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300427 case QEVENT_RESUME:
428 event_name = "RESUME";
429 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200430 case QEVENT_VNC_CONNECTED:
431 event_name = "VNC_CONNECTED";
432 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200433 case QEVENT_VNC_INITIALIZED:
434 event_name = "VNC_INITIALIZED";
435 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200436 case QEVENT_VNC_DISCONNECTED:
437 event_name = "VNC_DISCONNECTED";
438 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200439 case QEVENT_BLOCK_IO_ERROR:
440 event_name = "BLOCK_IO_ERROR";
441 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300442 case QEVENT_RTC_CHANGE:
443 event_name = "RTC_CHANGE";
444 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300445 case QEVENT_WATCHDOG:
446 event_name = "WATCHDOG";
447 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200448 default:
449 abort();
450 break;
451 }
452
453 qmp = qdict_new();
454 timestamp_put(qmp);
455 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200456 if (data) {
457 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200458 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200459 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200460
Adam Litkef039a562010-01-15 08:34:02 -0600461 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200462 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200463 monitor_json_emitter(mon, QOBJECT(qmp));
464 }
Adam Litkef039a562010-01-15 08:34:02 -0600465 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200466 QDECREF(qmp);
467}
468
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200469static int do_qmp_capabilities(Monitor *mon, const QDict *params,
470 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200471{
472 /* Will setup QMP capabilities in the future */
473 if (monitor_ctrl_mode(mon)) {
474 mon->mc->command_mode = 1;
475 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200476
477 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200478}
479
bellard9dc39cb2004-03-14 21:38:27 +0000480static int compare_cmd(const char *name, const char *list)
481{
482 const char *p, *pstart;
483 int len;
484 len = strlen(name);
485 p = list;
486 for(;;) {
487 pstart = p;
488 p = strchr(p, '|');
489 if (!p)
490 p = pstart + strlen(pstart);
491 if ((p - pstart) == len && !memcmp(pstart, name, len))
492 return 1;
493 if (*p == '\0')
494 break;
495 p++;
496 }
497 return 0;
498}
499
Anthony Liguoric227f092009-10-01 16:12:16 -0500500static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000501 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000502{
Anthony Liguoric227f092009-10-01 16:12:16 -0500503 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000504
505 for(cmd = cmds; cmd->name != NULL; cmd++) {
506 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000507 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
508 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000509 }
510}
511
aliguori376253e2009-03-05 23:01:23 +0000512static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000513{
514 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000515 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000516 } else {
aliguori376253e2009-03-05 23:01:23 +0000517 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000518 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000519 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000520 monitor_printf(mon, "Log items (comma separated):\n");
521 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000522 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000523 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000524 }
525 }
bellard9dc39cb2004-03-14 21:38:27 +0000526 }
527}
528
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300529static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300530{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300531 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300532}
533
Adam Litke940cc302010-01-25 12:18:44 -0600534static void user_monitor_complete(void *opaque, QObject *ret_data)
535{
536 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
537
538 if (ret_data) {
539 data->user_print(data->mon, ret_data);
540 }
541 monitor_resume(data->mon);
542 qemu_free(data);
543}
544
545static void qmp_monitor_complete(void *opaque, QObject *ret_data)
546{
547 monitor_protocol_emitter(opaque, ret_data);
548}
549
550static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
551 const QDict *params)
552{
553 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
554}
555
556static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
557{
558 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
559}
560
561static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
562 const QDict *params)
563{
564 int ret;
565
566 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
567 cb_data->mon = mon;
568 cb_data->user_print = cmd->user_print;
569 monitor_suspend(mon);
570 ret = cmd->mhandler.cmd_async(mon, params,
571 user_monitor_complete, cb_data);
572 if (ret < 0) {
573 monitor_resume(mon);
574 qemu_free(cb_data);
575 }
576}
577
578static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
579{
580 int ret;
581
582 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
583 cb_data->mon = mon;
584 cb_data->user_print = cmd->user_print;
585 monitor_suspend(mon);
586 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
587 if (ret < 0) {
588 monitor_resume(mon);
589 qemu_free(cb_data);
590 }
591}
592
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200593static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000594{
Anthony Liguoric227f092009-10-01 16:12:16 -0500595 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300596 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000597
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200598 if (!item) {
599 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000600 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200601 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300602
603 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000604 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300605 break;
bellard9dc39cb2004-03-14 21:38:27 +0000606 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300607
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200608 if (cmd->name == NULL) {
609 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100610 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200611 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200612 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300613 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200614 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300615
Adam Litke940cc302010-01-25 12:18:44 -0600616 if (monitor_handler_is_async(cmd)) {
617 if (monitor_ctrl_mode(mon)) {
618 qmp_async_info_handler(mon, cmd);
619 } else {
620 user_async_info_handler(mon, cmd);
621 }
622 /*
623 * Indicate that this command is asynchronous and will not return any
624 * data (not even empty). Instead, the data will be returned via a
625 * completion callback.
626 */
627 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
628 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300629 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200630
631 if (!monitor_ctrl_mode(mon)) {
632 /*
633 * User Protocol function is called here, Monitor Protocol is
634 * handled by monitor_call_handler()
635 */
636 if (*ret_data)
637 cmd->user_print(mon, *ret_data);
638 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300639 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200640 if (monitor_ctrl_mode(mon)) {
641 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100642 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200643 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200644 } else {
645 cmd->mhandler.info(mon);
646 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300647 }
648
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200649 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300650
651help:
652 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200653 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000654}
655
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200656static void do_info_version_print(Monitor *mon, const QObject *data)
657{
658 QDict *qdict;
659
660 qdict = qobject_to_qdict(data);
661
662 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
663 qdict_get_str(qdict, "package"));
664}
665
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300666static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000667{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200668 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
669 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000670}
671
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200672static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000673{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200674 QDict *qdict;
675
676 qdict = qobject_to_qdict(data);
677 if (qdict_size(qdict) == 0) {
678 return;
679 }
680
681 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
682}
683
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200684static void do_info_name(Monitor *mon, QObject **ret_data)
685{
686 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
687 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000688}
689
Luiz Capitulino1a728672009-12-10 17:15:56 -0200690static QObject *get_cmd_dict(const char *name)
691{
692 const char *p;
693
694 /* Remove '|' from some commands */
695 p = strchr(name, '|');
696 if (p) {
697 p++;
698 } else {
699 p = name;
700 }
701
702 return qobject_from_jsonf("{ 'name': %s }", p);
703}
704
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200705static void do_info_commands(Monitor *mon, QObject **ret_data)
706{
707 QList *cmd_list;
708 const mon_cmd_t *cmd;
709
710 cmd_list = qlist_new();
711
712 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
713 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200714 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200715 }
716 }
717
718 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
719 if (monitor_handler_ported(cmd)) {
720 char buf[128];
721 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200722 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200723 }
724 }
725
726 *ret_data = QOBJECT(cmd_list);
727}
728
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200729static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000730{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200731 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
732}
733
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200734static void do_info_uuid(Monitor *mon, QObject **ret_data)
735{
736 char uuid[64];
737
738 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000739 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
740 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
741 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
742 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200743 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000744}
745
bellard6a00d602005-11-21 23:25:50 +0000746/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000747static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000748{
749 CPUState *env;
750
751 for(env = first_cpu; env != NULL; env = env->next_cpu) {
752 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000753 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000754 return 0;
755 }
756 }
757 return -1;
758}
759
pbrook9596ebb2007-11-18 01:44:38 +0000760static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000761{
aliguori731b0362009-03-05 23:01:42 +0000762 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000763 mon_set_cpu(0);
764 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300765 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000766 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000767}
768
aliguori376253e2009-03-05 23:01:23 +0000769static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000770{
bellard6a00d602005-11-21 23:25:50 +0000771 CPUState *env;
772 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000773#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000774 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000775 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000776#else
aliguori376253e2009-03-05 23:01:23 +0000777 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000778 0);
bellard9307c4c2004-04-04 12:57:25 +0000779#endif
780}
781
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300782static void print_cpu_iter(QObject *obj, void *opaque)
783{
784 QDict *cpu;
785 int active = ' ';
786 Monitor *mon = opaque;
787
788 assert(qobject_type(obj) == QTYPE_QDICT);
789 cpu = qobject_to_qdict(obj);
790
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200791 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300792 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200793 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300794
795 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
796
797#if defined(TARGET_I386)
798 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
799 (target_ulong) qdict_get_int(cpu, "pc"));
800#elif defined(TARGET_PPC)
801 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
802 (target_long) qdict_get_int(cpu, "nip"));
803#elif defined(TARGET_SPARC)
804 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
805 (target_long) qdict_get_int(cpu, "pc"));
806 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
807 (target_long) qdict_get_int(cpu, "npc"));
808#elif defined(TARGET_MIPS)
809 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
810 (target_long) qdict_get_int(cpu, "PC"));
811#endif
812
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200813 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300814 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200815 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300816
817 monitor_printf(mon, "\n");
818}
819
820static void monitor_print_cpus(Monitor *mon, const QObject *data)
821{
822 QList *cpu_list;
823
824 assert(qobject_type(data) == QTYPE_QLIST);
825 cpu_list = qobject_to_qlist(data);
826 qlist_iter(cpu_list, print_cpu_iter, mon);
827}
828
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300829static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000830{
831 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300832 QList *cpu_list;
833
834 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000835
836 /* just to set the default cpu if not already done */
837 mon_get_cpu();
838
839 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200840 QDict *cpu;
841 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300842
Avi Kivity4c0960c2009-08-17 23:19:53 +0300843 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300844
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200845 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
846 env->cpu_index, env == mon->mon_cpu,
847 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200848
849 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300850
bellard6a00d602005-11-21 23:25:50 +0000851#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300852 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000853#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300854 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000855#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300856 qdict_put(cpu, "pc", qint_from_int(env->pc));
857 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000858#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300859 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000860#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300861
862 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000863 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300864
865 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000866}
867
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200868static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000869{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300870 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200871 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100872 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
873 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200874 return -1;
875 }
876 return 0;
bellard6a00d602005-11-21 23:25:50 +0000877}
878
aliguori376253e2009-03-05 23:01:23 +0000879static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000880{
aliguori376253e2009-03-05 23:01:23 +0000881 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000882}
883
aliguori376253e2009-03-05 23:01:23 +0000884static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000885{
886 int i;
bellard7e2515e2004-08-01 21:52:19 +0000887 const char *str;
ths3b46e622007-09-17 08:09:54 +0000888
aliguoricde76ee2009-03-05 23:01:51 +0000889 if (!mon->rs)
890 return;
bellard7e2515e2004-08-01 21:52:19 +0000891 i = 0;
892 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000893 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000894 if (!str)
895 break;
aliguori376253e2009-03-05 23:01:23 +0000896 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000897 i++;
bellardaa455482004-04-04 13:07:25 +0000898 }
899}
900
j_mayer76a66252007-03-07 08:32:30 +0000901#if defined(TARGET_PPC)
902/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000903static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000904{
905 CPUState *env;
906
907 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000908 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000909}
910#endif
911
Luiz Capitulinob223f352009-10-07 13:41:56 -0300912/**
913 * do_quit(): Quit QEMU execution
914 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200915static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000916{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300917 monitor_suspend(mon);
918 no_shutdown = 0;
919 qemu_system_shutdown_request();
920
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200921 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000922}
923
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200924static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000925{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200926 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100927 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200928 return -1;
929 }
aliguoribb5fc202009-03-05 23:01:15 +0000930
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200931 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100932}
933
934static void change_vnc_password_cb(Monitor *mon, const char *password,
935 void *opaque)
936{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100937 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000938 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000939}
940
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200941static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000942{
ths70848512007-08-25 01:37:05 +0000943 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000944 strcmp(target, "password") == 0) {
945 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000946 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000947 strncpy(password, arg, sizeof(password));
948 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200949 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000950 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200951 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000952 }
ths70848512007-08-25 01:37:05 +0000953 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200954 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100955 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200956 return -1;
957 }
ths70848512007-08-25 01:37:05 +0000958 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200959
960 return 0;
thse25a5822007-08-25 01:36:20 +0000961}
962
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100963/**
964 * do_change(): Change a removable medium, or VNC configuration
965 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200966static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000967{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300968 const char *device = qdict_get_str(qdict, "device");
969 const char *target = qdict_get_str(qdict, "target");
970 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200971 int ret;
972
thse25a5822007-08-25 01:36:20 +0000973 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200974 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000975 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200976 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000977 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200978
979 return ret;
thse25a5822007-08-25 01:36:20 +0000980}
981
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300982static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +0000983{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300984 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300985 return 0;
bellard59a983b2004-03-17 23:17:16 +0000986}
987
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300988static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000989{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300990 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000991}
992
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300993static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000994{
995 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300996 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000997
bellard9307c4c2004-04-04 12:57:25 +0000998 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000999 mask = 0;
1000 } else {
bellard9307c4c2004-04-04 12:57:25 +00001001 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001002 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001003 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001004 return;
1005 }
1006 }
1007 cpu_set_log(mask);
1008}
1009
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001010static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001011{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001012 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001013 if (!option || !strcmp(option, "on")) {
1014 singlestep = 1;
1015 } else if (!strcmp(option, "off")) {
1016 singlestep = 0;
1017 } else {
1018 monitor_printf(mon, "unexpected option %s\n", option);
1019 }
1020}
1021
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001022/**
1023 * do_stop(): Stop VM execution
1024 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001025static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001026{
1027 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001028 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001029}
1030
aliguoribb5fc202009-03-05 23:01:15 +00001031static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001032
aliguori376253e2009-03-05 23:01:23 +00001033struct bdrv_iterate_context {
1034 Monitor *mon;
1035 int err;
1036};
aliguoric0f4ce72009-03-05 23:01:01 +00001037
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001038/**
1039 * do_cont(): Resume emulation.
1040 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001041static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001042{
1043 struct bdrv_iterate_context context = { mon, 0 };
1044
1045 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001046 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001047 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001048 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001049 return 0;
1050 } else {
1051 return -1;
1052 }
bellard8a7ddc32004-03-31 19:00:16 +00001053}
1054
aliguoribb5fc202009-03-05 23:01:15 +00001055static void bdrv_key_cb(void *opaque, int err)
1056{
aliguori376253e2009-03-05 23:01:23 +00001057 Monitor *mon = opaque;
1058
aliguoribb5fc202009-03-05 23:01:15 +00001059 /* another key was set successfully, retry to continue */
1060 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001061 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001062}
1063
1064static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1065{
aliguori376253e2009-03-05 23:01:23 +00001066 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001067
aliguori376253e2009-03-05 23:01:23 +00001068 if (!context->err && bdrv_key_required(bs)) {
1069 context->err = -EBUSY;
1070 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1071 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001072 }
1073}
1074
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001075static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001076{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001077 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001078 if (!device)
1079 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1080 if (gdbserver_start(device) < 0) {
1081 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1082 device);
1083 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001084 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001085 } else {
aliguori59030a82009-04-05 18:43:41 +00001086 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1087 device);
bellard8a7ddc32004-03-31 19:00:16 +00001088 }
1089}
1090
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001091static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001092{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001093 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001094 if (select_watchdog_action(action) == -1) {
1095 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1096 }
1097}
1098
aliguori376253e2009-03-05 23:01:23 +00001099static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001100{
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001102 switch(c) {
1103 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001105 break;
1106 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001107 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001108 break;
1109 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001111 break;
1112 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001114 break;
1115 default:
1116 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001117 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001118 } else {
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001120 }
1121 break;
1122 }
aliguori376253e2009-03-05 23:01:23 +00001123 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001124}
1125
aliguori376253e2009-03-05 23:01:23 +00001126static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001127 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001128{
bellard6a00d602005-11-21 23:25:50 +00001129 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001130 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001131 uint8_t buf[16];
1132 uint64_t v;
1133
1134 if (format == 'i') {
1135 int flags;
1136 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001137 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001138#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001139 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001140 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001141 } else if (wsize == 4) {
1142 flags = 0;
1143 } else {
bellard6a15fd12006-04-12 21:07:07 +00001144 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001145 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001146 if (env) {
1147#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001148 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001149 (env->segs[R_CS].flags & DESC_L_MASK))
1150 flags = 2;
1151 else
1152#endif
1153 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1154 flags = 1;
1155 }
bellard4c27ba22004-04-25 18:05:08 +00001156 }
1157#endif
aliguori376253e2009-03-05 23:01:23 +00001158 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001159 return;
1160 }
1161
1162 len = wsize * count;
1163 if (wsize == 1)
1164 line_size = 8;
1165 else
1166 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001167 max_digits = 0;
1168
1169 switch(format) {
1170 case 'o':
1171 max_digits = (wsize * 8 + 2) / 3;
1172 break;
1173 default:
1174 case 'x':
1175 max_digits = (wsize * 8) / 4;
1176 break;
1177 case 'u':
1178 case 'd':
1179 max_digits = (wsize * 8 * 10 + 32) / 33;
1180 break;
1181 case 'c':
1182 wsize = 1;
1183 break;
1184 }
1185
1186 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001187 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001189 else
aliguori376253e2009-03-05 23:01:23 +00001190 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001191 l = len;
1192 if (l > line_size)
1193 l = line_size;
1194 if (is_physical) {
1195 cpu_physical_memory_rw(addr, buf, l, 0);
1196 } else {
bellard6a00d602005-11-21 23:25:50 +00001197 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001198 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001200 break;
1201 }
bellard9307c4c2004-04-04 12:57:25 +00001202 }
ths5fafdf22007-09-16 21:08:06 +00001203 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001204 while (i < l) {
1205 switch(wsize) {
1206 default:
1207 case 1:
1208 v = ldub_raw(buf + i);
1209 break;
1210 case 2:
1211 v = lduw_raw(buf + i);
1212 break;
1213 case 4:
bellard92a31b12005-02-10 22:00:52 +00001214 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case 8:
1217 v = ldq_raw(buf + i);
1218 break;
1219 }
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001221 switch(format) {
1222 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001227 break;
1228 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001230 break;
1231 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001233 break;
1234 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001236 break;
1237 }
1238 i += wsize;
1239 }
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001241 addr += l;
1242 len -= l;
1243 }
1244}
1245
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001246static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001247{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001248 int count = qdict_get_int(qdict, "count");
1249 int format = qdict_get_int(qdict, "format");
1250 int size = qdict_get_int(qdict, "size");
1251 target_long addr = qdict_get_int(qdict, "addr");
1252
aliguori376253e2009-03-05 23:01:23 +00001253 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001254}
1255
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001256static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001257{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001258 int count = qdict_get_int(qdict, "count");
1259 int format = qdict_get_int(qdict, "format");
1260 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001261 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001262
aliguori376253e2009-03-05 23:01:23 +00001263 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001264}
1265
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001266static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001267{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001268 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001269 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001270
blueswir17743e582007-09-24 18:39:04 +00001271#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001272 switch(format) {
1273 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001274 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001275 break;
1276 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001278 break;
1279 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001281 break;
1282 default:
1283 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001284 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 }
bellard92a31b12005-02-10 22:00:52 +00001290#else
1291 switch(format) {
1292 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001294 break;
1295 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001296 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001297 break;
1298 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001299 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001300 break;
1301 default:
1302 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001304 break;
1305 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001307 break;
1308 }
1309#endif
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001311}
1312
Luiz Capitulino98696222010-02-10 23:49:58 -02001313static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001314{
1315 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001316 uint32_t size = qdict_get_int(qdict, "size");
1317 const char *filename = qdict_get_str(qdict, "filename");
1318 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001319 uint32_t l;
1320 CPUState *env;
1321 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001322 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001323
1324 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001325
1326 f = fopen(filename, "wb");
1327 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001328 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001329 return -1;
bellardb371dc52007-01-03 15:20:39 +00001330 }
1331 while (size != 0) {
1332 l = sizeof(buf);
1333 if (l > size)
1334 l = size;
1335 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001336 if (fwrite(buf, 1, l, f) != l) {
1337 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1338 goto exit;
1339 }
bellardb371dc52007-01-03 15:20:39 +00001340 addr += l;
1341 size -= l;
1342 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001343
1344 ret = 0;
1345
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001346exit:
bellardb371dc52007-01-03 15:20:39 +00001347 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001348 return ret;
bellardb371dc52007-01-03 15:20:39 +00001349}
1350
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001351static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001352 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001353{
1354 FILE *f;
1355 uint32_t l;
1356 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001357 uint32_t size = qdict_get_int(qdict, "size");
1358 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001359 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001360 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001361
1362 f = fopen(filename, "wb");
1363 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001364 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001365 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001366 }
1367 while (size != 0) {
1368 l = sizeof(buf);
1369 if (l > size)
1370 l = size;
1371 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001372 if (fwrite(buf, 1, l, f) != l) {
1373 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1374 goto exit;
1375 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001376 fflush(f);
1377 addr += l;
1378 size -= l;
1379 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001380
1381 ret = 0;
1382
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001383exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001384 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001385 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001386}
1387
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001388static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001389{
1390 uint32_t addr;
1391 uint8_t buf[1];
1392 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001393 uint32_t start = qdict_get_int(qdict, "start");
1394 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001395
1396 sum = 0;
1397 for(addr = start; addr < (start + size); addr++) {
1398 cpu_physical_memory_rw(addr, buf, 1, 0);
1399 /* BSD sum algorithm ('sum' Unix command) */
1400 sum = (sum >> 1) | (sum << 15);
1401 sum += buf[0];
1402 }
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001404}
1405
bellarda3a91a32004-06-04 11:06:21 +00001406typedef struct {
1407 int keycode;
1408 const char *name;
1409} KeyDef;
1410
1411static const KeyDef key_defs[] = {
1412 { 0x2a, "shift" },
1413 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001414
bellarda3a91a32004-06-04 11:06:21 +00001415 { 0x38, "alt" },
1416 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001417 { 0x64, "altgr" },
1418 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001419 { 0x1d, "ctrl" },
1420 { 0x9d, "ctrl_r" },
1421
1422 { 0xdd, "menu" },
1423
1424 { 0x01, "esc" },
1425
1426 { 0x02, "1" },
1427 { 0x03, "2" },
1428 { 0x04, "3" },
1429 { 0x05, "4" },
1430 { 0x06, "5" },
1431 { 0x07, "6" },
1432 { 0x08, "7" },
1433 { 0x09, "8" },
1434 { 0x0a, "9" },
1435 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001436 { 0x0c, "minus" },
1437 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001438 { 0x0e, "backspace" },
1439
1440 { 0x0f, "tab" },
1441 { 0x10, "q" },
1442 { 0x11, "w" },
1443 { 0x12, "e" },
1444 { 0x13, "r" },
1445 { 0x14, "t" },
1446 { 0x15, "y" },
1447 { 0x16, "u" },
1448 { 0x17, "i" },
1449 { 0x18, "o" },
1450 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001451 { 0x1a, "bracket_left" },
1452 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001453 { 0x1c, "ret" },
1454
1455 { 0x1e, "a" },
1456 { 0x1f, "s" },
1457 { 0x20, "d" },
1458 { 0x21, "f" },
1459 { 0x22, "g" },
1460 { 0x23, "h" },
1461 { 0x24, "j" },
1462 { 0x25, "k" },
1463 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001464 { 0x27, "semicolon" },
1465 { 0x28, "apostrophe" },
1466 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001467
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001468 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001469 { 0x2c, "z" },
1470 { 0x2d, "x" },
1471 { 0x2e, "c" },
1472 { 0x2f, "v" },
1473 { 0x30, "b" },
1474 { 0x31, "n" },
1475 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001476 { 0x33, "comma" },
1477 { 0x34, "dot" },
1478 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001479
balrog4d3b6f62008-02-10 16:33:14 +00001480 { 0x37, "asterisk" },
1481
bellarda3a91a32004-06-04 11:06:21 +00001482 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001483 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001484 { 0x3b, "f1" },
1485 { 0x3c, "f2" },
1486 { 0x3d, "f3" },
1487 { 0x3e, "f4" },
1488 { 0x3f, "f5" },
1489 { 0x40, "f6" },
1490 { 0x41, "f7" },
1491 { 0x42, "f8" },
1492 { 0x43, "f9" },
1493 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001494 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001495 { 0x46, "scroll_lock" },
1496
bellard64866c32006-05-07 18:03:31 +00001497 { 0xb5, "kp_divide" },
1498 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001499 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001500 { 0x4e, "kp_add" },
1501 { 0x9c, "kp_enter" },
1502 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001503 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001504
1505 { 0x52, "kp_0" },
1506 { 0x4f, "kp_1" },
1507 { 0x50, "kp_2" },
1508 { 0x51, "kp_3" },
1509 { 0x4b, "kp_4" },
1510 { 0x4c, "kp_5" },
1511 { 0x4d, "kp_6" },
1512 { 0x47, "kp_7" },
1513 { 0x48, "kp_8" },
1514 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001515
bellarda3a91a32004-06-04 11:06:21 +00001516 { 0x56, "<" },
1517
1518 { 0x57, "f11" },
1519 { 0x58, "f12" },
1520
1521 { 0xb7, "print" },
1522
1523 { 0xc7, "home" },
1524 { 0xc9, "pgup" },
1525 { 0xd1, "pgdn" },
1526 { 0xcf, "end" },
1527
1528 { 0xcb, "left" },
1529 { 0xc8, "up" },
1530 { 0xd0, "down" },
1531 { 0xcd, "right" },
1532
1533 { 0xd2, "insert" },
1534 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001535#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1536 { 0xf0, "stop" },
1537 { 0xf1, "again" },
1538 { 0xf2, "props" },
1539 { 0xf3, "undo" },
1540 { 0xf4, "front" },
1541 { 0xf5, "copy" },
1542 { 0xf6, "open" },
1543 { 0xf7, "paste" },
1544 { 0xf8, "find" },
1545 { 0xf9, "cut" },
1546 { 0xfa, "lf" },
1547 { 0xfb, "help" },
1548 { 0xfc, "meta_l" },
1549 { 0xfd, "meta_r" },
1550 { 0xfe, "compose" },
1551#endif
bellarda3a91a32004-06-04 11:06:21 +00001552 { 0, NULL },
1553};
1554
1555static int get_keycode(const char *key)
1556{
1557 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001558 char *endp;
1559 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001560
1561 for(p = key_defs; p->name != NULL; p++) {
1562 if (!strcmp(key, p->name))
1563 return p->keycode;
1564 }
bellard64866c32006-05-07 18:03:31 +00001565 if (strstart(key, "0x", NULL)) {
1566 ret = strtoul(key, &endp, 0);
1567 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1568 return ret;
1569 }
bellarda3a91a32004-06-04 11:06:21 +00001570 return -1;
1571}
1572
balrogc8256f92008-06-08 22:45:01 +00001573#define MAX_KEYCODES 16
1574static uint8_t keycodes[MAX_KEYCODES];
1575static int nb_pending_keycodes;
1576static QEMUTimer *key_timer;
1577
1578static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001579{
balrogc8256f92008-06-08 22:45:01 +00001580 int keycode;
1581
1582 while (nb_pending_keycodes > 0) {
1583 nb_pending_keycodes--;
1584 keycode = keycodes[nb_pending_keycodes];
1585 if (keycode & 0x80)
1586 kbd_put_keycode(0xe0);
1587 kbd_put_keycode(keycode | 0x80);
1588 }
1589}
1590
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001591static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001592{
balrog3401c0d2008-06-04 10:05:59 +00001593 char keyname_buf[16];
1594 char *separator;
1595 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001596 const char *string = qdict_get_str(qdict, "string");
1597 int has_hold_time = qdict_haskey(qdict, "hold_time");
1598 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001599
balrogc8256f92008-06-08 22:45:01 +00001600 if (nb_pending_keycodes > 0) {
1601 qemu_del_timer(key_timer);
1602 release_keys(NULL);
1603 }
1604 if (!has_hold_time)
1605 hold_time = 100;
1606 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001607 while (1) {
1608 separator = strchr(string, '-');
1609 keyname_len = separator ? separator - string : strlen(string);
1610 if (keyname_len > 0) {
1611 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1612 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001613 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001614 return;
bellarda3a91a32004-06-04 11:06:21 +00001615 }
balrogc8256f92008-06-08 22:45:01 +00001616 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001617 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001618 return;
1619 }
1620 keyname_buf[keyname_len] = 0;
1621 keycode = get_keycode(keyname_buf);
1622 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001623 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001624 return;
1625 }
balrogc8256f92008-06-08 22:45:01 +00001626 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001627 }
balrog3401c0d2008-06-04 10:05:59 +00001628 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001629 break;
balrog3401c0d2008-06-04 10:05:59 +00001630 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001631 }
balrogc8256f92008-06-08 22:45:01 +00001632 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001633 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001634 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001635 keycode = keycodes[i];
1636 if (keycode & 0x80)
1637 kbd_put_keycode(0xe0);
1638 kbd_put_keycode(keycode & 0x7f);
1639 }
balrogc8256f92008-06-08 22:45:01 +00001640 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001641 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001642 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001643}
1644
bellard13224a82006-07-14 22:03:35 +00001645static int mouse_button_state;
1646
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001647static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001648{
1649 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001650 const char *dx_str = qdict_get_str(qdict, "dx_str");
1651 const char *dy_str = qdict_get_str(qdict, "dy_str");
1652 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001653 dx = strtol(dx_str, NULL, 0);
1654 dy = strtol(dy_str, NULL, 0);
1655 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001656 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001657 dz = strtol(dz_str, NULL, 0);
1658 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1659}
1660
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001661static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001662{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001663 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001664 mouse_button_state = button_state;
1665 kbd_mouse_event(0, 0, 0, mouse_button_state);
1666}
1667
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001668static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001669{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001670 int size = qdict_get_int(qdict, "size");
1671 int addr = qdict_get_int(qdict, "addr");
1672 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001673 uint32_t val;
1674 int suffix;
1675
1676 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001677 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001678 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001679 addr++;
1680 }
1681 addr &= 0xffff;
1682
1683 switch(size) {
1684 default:
1685 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001686 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001687 suffix = 'b';
1688 break;
1689 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001690 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001691 suffix = 'w';
1692 break;
1693 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001694 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001695 suffix = 'l';
1696 break;
1697 }
aliguori376253e2009-03-05 23:01:23 +00001698 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1699 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001700}
bellarda3a91a32004-06-04 11:06:21 +00001701
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001702static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001703{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001704 int size = qdict_get_int(qdict, "size");
1705 int addr = qdict_get_int(qdict, "addr");
1706 int val = qdict_get_int(qdict, "val");
1707
Jan Kiszkaf1147842009-07-14 10:20:11 +02001708 addr &= IOPORTS_MASK;
1709
1710 switch (size) {
1711 default:
1712 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001713 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001714 break;
1715 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001716 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001717 break;
1718 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001719 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001720 break;
1721 }
1722}
1723
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001724static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001725{
1726 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001727 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001728
Jan Kiszka76e30d02009-07-02 00:19:02 +02001729 res = qemu_boot_set(bootdevice);
1730 if (res == 0) {
1731 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1732 } else if (res > 0) {
1733 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001734 } else {
aliguori376253e2009-03-05 23:01:23 +00001735 monitor_printf(mon, "no function defined to set boot device list for "
1736 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001737 }
1738}
1739
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001740/**
1741 * do_system_reset(): Issue a machine reset
1742 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001743static int do_system_reset(Monitor *mon, const QDict *qdict,
1744 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001745{
1746 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001747 return 0;
bellarde4f90822004-06-20 12:35:44 +00001748}
1749
Luiz Capitulino43076662009-10-07 13:41:59 -03001750/**
1751 * do_system_powerdown(): Issue a machine powerdown
1752 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001753static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1754 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001755{
1756 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001757 return 0;
bellard34751872005-07-02 14:31:34 +00001758}
1759
bellardb86bda52004-09-18 19:32:46 +00001760#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001761static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001762{
aliguori376253e2009-03-05 23:01:23 +00001763 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1764 addr,
1765 pte & mask,
1766 pte & PG_GLOBAL_MASK ? 'G' : '-',
1767 pte & PG_PSE_MASK ? 'P' : '-',
1768 pte & PG_DIRTY_MASK ? 'D' : '-',
1769 pte & PG_ACCESSED_MASK ? 'A' : '-',
1770 pte & PG_PCD_MASK ? 'C' : '-',
1771 pte & PG_PWT_MASK ? 'T' : '-',
1772 pte & PG_USER_MASK ? 'U' : '-',
1773 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001774}
1775
aliguori376253e2009-03-05 23:01:23 +00001776static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001777{
bellard6a00d602005-11-21 23:25:50 +00001778 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001779 int l1, l2;
1780 uint32_t pgd, pde, pte;
1781
bellard6a00d602005-11-21 23:25:50 +00001782 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001783
bellardb86bda52004-09-18 19:32:46 +00001784 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001785 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001786 return;
1787 }
1788 pgd = env->cr[3] & ~0xfff;
1789 for(l1 = 0; l1 < 1024; l1++) {
1790 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1791 pde = le32_to_cpu(pde);
1792 if (pde & PG_PRESENT_MASK) {
1793 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001794 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001795 } else {
1796 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001797 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001798 (uint8_t *)&pte, 4);
1799 pte = le32_to_cpu(pte);
1800 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001801 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001802 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001803 ~0xfff);
1804 }
1805 }
1806 }
1807 }
1808 }
1809}
1810
aliguori376253e2009-03-05 23:01:23 +00001811static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001812 uint32_t end, int prot)
1813{
bellard9746b152004-11-11 18:30:24 +00001814 int prot1;
1815 prot1 = *plast_prot;
1816 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001817 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001818 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1819 *pstart, end, end - *pstart,
1820 prot1 & PG_USER_MASK ? 'u' : '-',
1821 'r',
1822 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001823 }
1824 if (prot != 0)
1825 *pstart = end;
1826 else
1827 *pstart = -1;
1828 *plast_prot = prot;
1829 }
1830}
1831
aliguori376253e2009-03-05 23:01:23 +00001832static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001833{
bellard6a00d602005-11-21 23:25:50 +00001834 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001835 int l1, l2, prot, last_prot;
1836 uint32_t pgd, pde, pte, start, end;
1837
bellard6a00d602005-11-21 23:25:50 +00001838 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001839
bellardb86bda52004-09-18 19:32:46 +00001840 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001841 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001842 return;
1843 }
1844 pgd = env->cr[3] & ~0xfff;
1845 last_prot = 0;
1846 start = -1;
1847 for(l1 = 0; l1 < 1024; l1++) {
1848 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1849 pde = le32_to_cpu(pde);
1850 end = l1 << 22;
1851 if (pde & PG_PRESENT_MASK) {
1852 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1853 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001854 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001855 } else {
1856 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001857 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001858 (uint8_t *)&pte, 4);
1859 pte = le32_to_cpu(pte);
1860 end = (l1 << 22) + (l2 << 12);
1861 if (pte & PG_PRESENT_MASK) {
1862 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1863 } else {
1864 prot = 0;
1865 }
aliguori376253e2009-03-05 23:01:23 +00001866 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001867 }
1868 }
1869 } else {
1870 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001871 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001872 }
1873 }
1874}
1875#endif
1876
aurel327c664e22009-03-03 06:12:22 +00001877#if defined(TARGET_SH4)
1878
aliguori376253e2009-03-05 23:01:23 +00001879static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001880{
aliguori376253e2009-03-05 23:01:23 +00001881 monitor_printf(mon, " tlb%i:\t"
1882 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1883 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1884 "dirty=%hhu writethrough=%hhu\n",
1885 idx,
1886 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1887 tlb->v, tlb->sh, tlb->c, tlb->pr,
1888 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001889}
1890
aliguori376253e2009-03-05 23:01:23 +00001891static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001892{
1893 CPUState *env = mon_get_cpu();
1894 int i;
1895
aliguori376253e2009-03-05 23:01:23 +00001896 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001897 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001898 print_tlb (mon, i, &env->itlb[i]);
1899 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001900 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001901 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001902}
1903
1904#endif
1905
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001906static void do_info_kvm_print(Monitor *mon, const QObject *data)
1907{
1908 QDict *qdict;
1909
1910 qdict = qobject_to_qdict(data);
1911
1912 monitor_printf(mon, "kvm support: ");
1913 if (qdict_get_bool(qdict, "present")) {
1914 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1915 "enabled" : "disabled");
1916 } else {
1917 monitor_printf(mon, "not compiled\n");
1918 }
1919}
1920
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001921static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001922{
1923#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001924 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1925 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001926#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001927 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001928#endif
1929}
1930
aliguori030ea372009-04-21 22:30:47 +00001931static void do_info_numa(Monitor *mon)
1932{
aliguorib28b6232009-04-22 20:20:29 +00001933 int i;
aliguori030ea372009-04-21 22:30:47 +00001934 CPUState *env;
1935
1936 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1937 for (i = 0; i < nb_numa_nodes; i++) {
1938 monitor_printf(mon, "node %d cpus:", i);
1939 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1940 if (env->numa_node == i) {
1941 monitor_printf(mon, " %d", env->cpu_index);
1942 }
1943 }
1944 monitor_printf(mon, "\n");
1945 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1946 node_mem[i] >> 20);
1947 }
1948}
1949
bellard5f1ce942006-02-08 22:40:15 +00001950#ifdef CONFIG_PROFILER
1951
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001952int64_t qemu_time;
1953int64_t dev_time;
1954
aliguori376253e2009-03-05 23:01:23 +00001955static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001956{
1957 int64_t total;
1958 total = qemu_time;
1959 if (total == 0)
1960 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001961 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001962 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001963 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001964 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001965 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001966 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001967}
1968#else
aliguori376253e2009-03-05 23:01:23 +00001969static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001970{
aliguori376253e2009-03-05 23:01:23 +00001971 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001972}
1973#endif
1974
bellardec36b692006-07-16 18:57:03 +00001975/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001976static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001977
aliguori376253e2009-03-05 23:01:23 +00001978static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001979{
1980 int i;
1981 CaptureState *s;
1982
1983 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001984 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001985 s->ops.info (s->opaque);
1986 }
1987}
1988
Blue Swirl23130862009-06-06 08:22:04 +00001989#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001990static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001991{
1992 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001993 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001994 CaptureState *s;
1995
1996 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1997 if (i == n) {
1998 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001999 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002000 qemu_free (s);
2001 return;
2002 }
2003 }
2004}
2005
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002006static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002007{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002008 const char *path = qdict_get_str(qdict, "path");
2009 int has_freq = qdict_haskey(qdict, "freq");
2010 int freq = qdict_get_try_int(qdict, "freq", -1);
2011 int has_bits = qdict_haskey(qdict, "bits");
2012 int bits = qdict_get_try_int(qdict, "bits", -1);
2013 int has_channels = qdict_haskey(qdict, "nchannels");
2014 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002015 CaptureState *s;
2016
2017 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002018
2019 freq = has_freq ? freq : 44100;
2020 bits = has_bits ? bits : 16;
2021 nchannels = has_channels ? nchannels : 2;
2022
2023 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002024 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002025 qemu_free (s);
2026 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002027 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002028}
2029#endif
2030
aurel32dc1c0b72008-04-27 23:52:12 +00002031#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002032static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002033{
2034 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002035 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002036
2037 for (env = first_cpu; env != NULL; env = env->next_cpu)
2038 if (env->cpu_index == cpu_index) {
2039 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2040 break;
2041 }
2042}
2043#endif
2044
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002045static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002046{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002047 QDict *qdict;
2048
2049 qdict = qobject_to_qdict(data);
2050
2051 monitor_printf(mon, "VM status: ");
2052 if (qdict_get_bool(qdict, "running")) {
2053 monitor_printf(mon, "running");
2054 if (qdict_get_bool(qdict, "singlestep")) {
2055 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002056 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002057 } else {
2058 monitor_printf(mon, "paused");
2059 }
2060
2061 monitor_printf(mon, "\n");
2062}
2063
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002064static void do_info_status(Monitor *mon, QObject **ret_data)
2065{
2066 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2067 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002068}
2069
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002070static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002071{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002072 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002073
aliguori76655d62009-03-06 20:27:37 +00002074 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002075 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002076 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002077 return acl;
2078}
aliguori76655d62009-03-06 20:27:37 +00002079
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002080static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002081{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002082 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002083 qemu_acl *acl = find_acl(mon, aclname);
2084 qemu_acl_entry *entry;
2085 int i = 0;
2086
2087 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002088 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002089 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002090 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002091 i++;
2092 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002093 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002094 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002095 }
2096}
2097
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002098static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002099{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002100 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002101 qemu_acl *acl = find_acl(mon, aclname);
2102
2103 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002104 qemu_acl_reset(acl);
2105 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002106 }
2107}
aliguori76655d62009-03-06 20:27:37 +00002108
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002109static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002110{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002111 const char *aclname = qdict_get_str(qdict, "aclname");
2112 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002113 qemu_acl *acl = find_acl(mon, aclname);
2114
2115 if (acl) {
2116 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002117 acl->defaultDeny = 0;
2118 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002119 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002120 acl->defaultDeny = 1;
2121 monitor_printf(mon, "acl: policy set to 'deny'\n");
2122 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002123 monitor_printf(mon, "acl: unknown policy '%s', "
2124 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002125 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126 }
2127}
aliguori76655d62009-03-06 20:27:37 +00002128
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002129static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002130{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002131 const char *aclname = qdict_get_str(qdict, "aclname");
2132 const char *match = qdict_get_str(qdict, "match");
2133 const char *policy = qdict_get_str(qdict, "policy");
2134 int has_index = qdict_haskey(qdict, "index");
2135 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002136 qemu_acl *acl = find_acl(mon, aclname);
2137 int deny, ret;
2138
2139 if (acl) {
2140 if (strcmp(policy, "allow") == 0) {
2141 deny = 0;
2142 } else if (strcmp(policy, "deny") == 0) {
2143 deny = 1;
2144 } else {
2145 monitor_printf(mon, "acl: unknown policy '%s', "
2146 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002147 return;
2148 }
aliguori28a76be2009-03-06 20:27:40 +00002149 if (has_index)
2150 ret = qemu_acl_insert(acl, deny, match, index);
2151 else
2152 ret = qemu_acl_append(acl, deny, match);
2153 if (ret < 0)
2154 monitor_printf(mon, "acl: unable to add acl entry\n");
2155 else
2156 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157 }
2158}
aliguori76655d62009-03-06 20:27:37 +00002159
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002160static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002161{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002162 const char *aclname = qdict_get_str(qdict, "aclname");
2163 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002164 qemu_acl *acl = find_acl(mon, aclname);
2165 int ret;
aliguori76655d62009-03-06 20:27:37 +00002166
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002167 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002168 ret = qemu_acl_remove(acl, match);
2169 if (ret < 0)
2170 monitor_printf(mon, "acl: no matching acl entry\n");
2171 else
2172 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002173 }
2174}
2175
Huang Ying79c4f6b2009-06-23 10:05:14 +08002176#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002177static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002178{
2179 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002180 int cpu_index = qdict_get_int(qdict, "cpu_index");
2181 int bank = qdict_get_int(qdict, "bank");
2182 uint64_t status = qdict_get_int(qdict, "status");
2183 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2184 uint64_t addr = qdict_get_int(qdict, "addr");
2185 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002186
2187 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2188 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2189 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2190 break;
2191 }
2192}
2193#endif
2194
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002195static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002196{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002197 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002198 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002199 int fd;
2200
2201 fd = qemu_chr_get_msgfd(mon->chr);
2202 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002203 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002204 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002205 }
2206
2207 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002208 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2209 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002210 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002211 }
2212
Blue Swirl72cf2d42009-09-12 07:36:22 +00002213 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002214 if (strcmp(monfd->name, fdname) != 0) {
2215 continue;
2216 }
2217
2218 close(monfd->fd);
2219 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002220 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002221 }
2222
Anthony Liguoric227f092009-10-01 16:12:16 -05002223 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002224 monfd->name = qemu_strdup(fdname);
2225 monfd->fd = fd;
2226
Blue Swirl72cf2d42009-09-12 07:36:22 +00002227 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002228 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229}
2230
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002231static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002232{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002233 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002234 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002235
Blue Swirl72cf2d42009-09-12 07:36:22 +00002236 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002237 if (strcmp(monfd->name, fdname) != 0) {
2238 continue;
2239 }
2240
Blue Swirl72cf2d42009-09-12 07:36:22 +00002241 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242 close(monfd->fd);
2243 qemu_free(monfd->name);
2244 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002245 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002246 }
2247
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002248 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002249 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250}
2251
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002252static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002253{
2254 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002255 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002256
2257 vm_stop(0);
2258
Markus Armbruster03cd4652010-02-17 16:24:10 +01002259 if (load_vmstate(name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002260 vm_start();
2261}
2262
Mark McLoughlin7768e042009-07-22 09:11:41 +01002263int monitor_get_fd(Monitor *mon, const char *fdname)
2264{
Anthony Liguoric227f092009-10-01 16:12:16 -05002265 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002266
Blue Swirl72cf2d42009-09-12 07:36:22 +00002267 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002268 int fd;
2269
2270 if (strcmp(monfd->name, fdname) != 0) {
2271 continue;
2272 }
2273
2274 fd = monfd->fd;
2275
2276 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002277 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002278 qemu_free(monfd->name);
2279 qemu_free(monfd);
2280
2281 return fd;
2282 }
2283
2284 return -1;
2285}
2286
Anthony Liguoric227f092009-10-01 16:12:16 -05002287static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002288#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002289 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002290};
2291
Blue Swirl23130862009-06-06 08:22:04 +00002292/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002293static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002294 {
2295 .name = "version",
2296 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002297 .params = "",
2298 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002299 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002300 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002301 },
2302 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002303 .name = "commands",
2304 .args_type = "",
2305 .params = "",
2306 .help = "list QMP available commands",
2307 .user_print = monitor_user_noop,
2308 .mhandler.info_new = do_info_commands,
2309 },
2310 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002311 .name = "network",
2312 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002313 .params = "",
2314 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002315 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002316 },
2317 {
2318 .name = "chardev",
2319 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002320 .params = "",
2321 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002322 .user_print = qemu_chr_info_print,
2323 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002324 },
2325 {
2326 .name = "block",
2327 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002328 .params = "",
2329 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002330 .user_print = bdrv_info_print,
2331 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002332 },
2333 {
2334 .name = "blockstats",
2335 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002336 .params = "",
2337 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002338 .user_print = bdrv_stats_print,
2339 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002340 },
2341 {
2342 .name = "registers",
2343 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002344 .params = "",
2345 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002346 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002347 },
2348 {
2349 .name = "cpus",
2350 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002351 .params = "",
2352 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002353 .user_print = monitor_print_cpus,
2354 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002355 },
2356 {
2357 .name = "history",
2358 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002359 .params = "",
2360 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002361 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002362 },
2363 {
2364 .name = "irq",
2365 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002366 .params = "",
2367 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002368 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002369 },
2370 {
2371 .name = "pic",
2372 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002373 .params = "",
2374 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002375 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002376 },
2377 {
2378 .name = "pci",
2379 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002380 .params = "",
2381 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002382 .user_print = do_pci_info_print,
2383 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002384 },
aurel327c664e22009-03-03 06:12:22 +00002385#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002386 {
2387 .name = "tlb",
2388 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002389 .params = "",
2390 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002391 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002392 },
aurel327c664e22009-03-03 06:12:22 +00002393#endif
2394#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002395 {
2396 .name = "mem",
2397 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 .params = "",
2399 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002400 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002401 },
bellardb86bda52004-09-18 19:32:46 +00002402#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002403 {
2404 .name = "jit",
2405 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002406 .params = "",
2407 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002408 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002409 },
2410 {
2411 .name = "kvm",
2412 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 .params = "",
2414 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002415 .user_print = do_info_kvm_print,
2416 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002417 },
2418 {
2419 .name = "numa",
2420 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002421 .params = "",
2422 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002423 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002424 },
2425 {
2426 .name = "usb",
2427 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002428 .params = "",
2429 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002430 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002431 },
2432 {
2433 .name = "usbhost",
2434 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002435 .params = "",
2436 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002437 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002438 },
2439 {
2440 .name = "profile",
2441 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002442 .params = "",
2443 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002444 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002445 },
2446 {
2447 .name = "capture",
2448 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002449 .params = "",
2450 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002451 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002452 },
2453 {
2454 .name = "snapshots",
2455 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002456 .params = "",
2457 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002458 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002459 },
2460 {
2461 .name = "status",
2462 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 .params = "",
2464 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002465 .user_print = do_info_status_print,
2466 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 },
2468 {
2469 .name = "pcmcia",
2470 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 .params = "",
2472 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002473 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 },
2475 {
2476 .name = "mice",
2477 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002478 .params = "",
2479 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002480 .user_print = do_info_mice_print,
2481 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 },
2483 {
2484 .name = "vnc",
2485 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002486 .params = "",
2487 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002488 .user_print = do_info_vnc_print,
2489 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002490 },
2491 {
2492 .name = "name",
2493 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002494 .params = "",
2495 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002496 .user_print = do_info_name_print,
2497 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002498 },
2499 {
2500 .name = "uuid",
2501 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002502 .params = "",
2503 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002504 .user_print = do_info_uuid_print,
2505 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002506 },
j_mayer76a66252007-03-07 08:32:30 +00002507#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002508 {
2509 .name = "cpustats",
2510 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 .params = "",
2512 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002513 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 },
j_mayer76a66252007-03-07 08:32:30 +00002515#endif
blueswir131a60e22007-10-26 18:42:59 +00002516#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 {
2518 .name = "usernet",
2519 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 .params = "",
2521 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002522 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 },
blueswir131a60e22007-10-26 18:42:59 +00002524#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 {
2526 .name = "migrate",
2527 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 .params = "",
2529 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002530 .user_print = do_info_migrate_print,
2531 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "balloon",
2535 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 .params = "",
2537 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002538 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002539 .mhandler.info_async = do_info_balloon,
2540 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 },
2542 {
2543 .name = "qtree",
2544 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 .params = "",
2546 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002547 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 },
2549 {
2550 .name = "qdm",
2551 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 .params = "",
2553 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002554 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 },
2556 {
2557 .name = "roms",
2558 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 .params = "",
2560 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002561 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 },
2563 {
2564 .name = NULL,
2565 },
bellard9dc39cb2004-03-14 21:38:27 +00002566};
2567
bellard9307c4c2004-04-04 12:57:25 +00002568/*******************************************************************/
2569
2570static const char *pch;
2571static jmp_buf expr_env;
2572
bellard92a31b12005-02-10 22:00:52 +00002573#define MD_TLONG 0
2574#define MD_I32 1
2575
bellard9307c4c2004-04-04 12:57:25 +00002576typedef struct MonitorDef {
2577 const char *name;
2578 int offset;
blueswir18662d652008-10-02 18:32:44 +00002579 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002580 int type;
bellard9307c4c2004-04-04 12:57:25 +00002581} MonitorDef;
2582
bellard57206fd2004-04-25 18:54:52 +00002583#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002584static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002585{
bellard6a00d602005-11-21 23:25:50 +00002586 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002587 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002588}
2589#endif
2590
bellarda541f292004-04-12 20:39:29 +00002591#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002592static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002593{
bellard6a00d602005-11-21 23:25:50 +00002594 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002595 unsigned int u;
2596 int i;
2597
2598 u = 0;
2599 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002600 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002601
2602 return u;
2603}
2604
blueswir18662d652008-10-02 18:32:44 +00002605static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002606{
bellard6a00d602005-11-21 23:25:50 +00002607 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002608 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002609}
2610
blueswir18662d652008-10-02 18:32:44 +00002611static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002612{
bellard6a00d602005-11-21 23:25:50 +00002613 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002614 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002615}
bellard9fddaa02004-05-21 12:59:32 +00002616
blueswir18662d652008-10-02 18:32:44 +00002617static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002618{
bellard6a00d602005-11-21 23:25:50 +00002619 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002620 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002621}
2622
blueswir18662d652008-10-02 18:32:44 +00002623static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002624{
bellard6a00d602005-11-21 23:25:50 +00002625 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002626 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002627}
2628
blueswir18662d652008-10-02 18:32:44 +00002629static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002630{
bellard6a00d602005-11-21 23:25:50 +00002631 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002632 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002633}
bellarda541f292004-04-12 20:39:29 +00002634#endif
2635
bellarde95c8d52004-09-30 22:22:08 +00002636#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002637#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002638static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002639{
bellard6a00d602005-11-21 23:25:50 +00002640 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002641
2642 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002643}
bellard7b936c02005-10-30 17:05:13 +00002644#endif
bellarde95c8d52004-09-30 22:22:08 +00002645
blueswir18662d652008-10-02 18:32:44 +00002646static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002647{
bellard6a00d602005-11-21 23:25:50 +00002648 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002649 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002650}
2651#endif
2652
blueswir18662d652008-10-02 18:32:44 +00002653static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002654#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002655
2656#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002657 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002658 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002659 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002660
bellard9307c4c2004-04-04 12:57:25 +00002661 { "eax", offsetof(CPUState, regs[0]) },
2662 { "ecx", offsetof(CPUState, regs[1]) },
2663 { "edx", offsetof(CPUState, regs[2]) },
2664 { "ebx", offsetof(CPUState, regs[3]) },
2665 { "esp|sp", offsetof(CPUState, regs[4]) },
2666 { "ebp|fp", offsetof(CPUState, regs[5]) },
2667 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002668 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002669#ifdef TARGET_X86_64
2670 { "r8", offsetof(CPUState, regs[8]) },
2671 { "r9", offsetof(CPUState, regs[9]) },
2672 { "r10", offsetof(CPUState, regs[10]) },
2673 { "r11", offsetof(CPUState, regs[11]) },
2674 { "r12", offsetof(CPUState, regs[12]) },
2675 { "r13", offsetof(CPUState, regs[13]) },
2676 { "r14", offsetof(CPUState, regs[14]) },
2677 { "r15", offsetof(CPUState, regs[15]) },
2678#endif
bellard9307c4c2004-04-04 12:57:25 +00002679 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002680 { "eip", offsetof(CPUState, eip) },
2681 SEG("cs", R_CS)
2682 SEG("ds", R_DS)
2683 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002684 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002685 SEG("fs", R_FS)
2686 SEG("gs", R_GS)
2687 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002688#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002689 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002690 { "r0", offsetof(CPUState, gpr[0]) },
2691 { "r1", offsetof(CPUState, gpr[1]) },
2692 { "r2", offsetof(CPUState, gpr[2]) },
2693 { "r3", offsetof(CPUState, gpr[3]) },
2694 { "r4", offsetof(CPUState, gpr[4]) },
2695 { "r5", offsetof(CPUState, gpr[5]) },
2696 { "r6", offsetof(CPUState, gpr[6]) },
2697 { "r7", offsetof(CPUState, gpr[7]) },
2698 { "r8", offsetof(CPUState, gpr[8]) },
2699 { "r9", offsetof(CPUState, gpr[9]) },
2700 { "r10", offsetof(CPUState, gpr[10]) },
2701 { "r11", offsetof(CPUState, gpr[11]) },
2702 { "r12", offsetof(CPUState, gpr[12]) },
2703 { "r13", offsetof(CPUState, gpr[13]) },
2704 { "r14", offsetof(CPUState, gpr[14]) },
2705 { "r15", offsetof(CPUState, gpr[15]) },
2706 { "r16", offsetof(CPUState, gpr[16]) },
2707 { "r17", offsetof(CPUState, gpr[17]) },
2708 { "r18", offsetof(CPUState, gpr[18]) },
2709 { "r19", offsetof(CPUState, gpr[19]) },
2710 { "r20", offsetof(CPUState, gpr[20]) },
2711 { "r21", offsetof(CPUState, gpr[21]) },
2712 { "r22", offsetof(CPUState, gpr[22]) },
2713 { "r23", offsetof(CPUState, gpr[23]) },
2714 { "r24", offsetof(CPUState, gpr[24]) },
2715 { "r25", offsetof(CPUState, gpr[25]) },
2716 { "r26", offsetof(CPUState, gpr[26]) },
2717 { "r27", offsetof(CPUState, gpr[27]) },
2718 { "r28", offsetof(CPUState, gpr[28]) },
2719 { "r29", offsetof(CPUState, gpr[29]) },
2720 { "r30", offsetof(CPUState, gpr[30]) },
2721 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002722 /* Floating point registers */
2723 { "f0", offsetof(CPUState, fpr[0]) },
2724 { "f1", offsetof(CPUState, fpr[1]) },
2725 { "f2", offsetof(CPUState, fpr[2]) },
2726 { "f3", offsetof(CPUState, fpr[3]) },
2727 { "f4", offsetof(CPUState, fpr[4]) },
2728 { "f5", offsetof(CPUState, fpr[5]) },
2729 { "f6", offsetof(CPUState, fpr[6]) },
2730 { "f7", offsetof(CPUState, fpr[7]) },
2731 { "f8", offsetof(CPUState, fpr[8]) },
2732 { "f9", offsetof(CPUState, fpr[9]) },
2733 { "f10", offsetof(CPUState, fpr[10]) },
2734 { "f11", offsetof(CPUState, fpr[11]) },
2735 { "f12", offsetof(CPUState, fpr[12]) },
2736 { "f13", offsetof(CPUState, fpr[13]) },
2737 { "f14", offsetof(CPUState, fpr[14]) },
2738 { "f15", offsetof(CPUState, fpr[15]) },
2739 { "f16", offsetof(CPUState, fpr[16]) },
2740 { "f17", offsetof(CPUState, fpr[17]) },
2741 { "f18", offsetof(CPUState, fpr[18]) },
2742 { "f19", offsetof(CPUState, fpr[19]) },
2743 { "f20", offsetof(CPUState, fpr[20]) },
2744 { "f21", offsetof(CPUState, fpr[21]) },
2745 { "f22", offsetof(CPUState, fpr[22]) },
2746 { "f23", offsetof(CPUState, fpr[23]) },
2747 { "f24", offsetof(CPUState, fpr[24]) },
2748 { "f25", offsetof(CPUState, fpr[25]) },
2749 { "f26", offsetof(CPUState, fpr[26]) },
2750 { "f27", offsetof(CPUState, fpr[27]) },
2751 { "f28", offsetof(CPUState, fpr[28]) },
2752 { "f29", offsetof(CPUState, fpr[29]) },
2753 { "f30", offsetof(CPUState, fpr[30]) },
2754 { "f31", offsetof(CPUState, fpr[31]) },
2755 { "fpscr", offsetof(CPUState, fpscr) },
2756 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002757 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002758 { "lr", offsetof(CPUState, lr) },
2759 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002760 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002761 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002762 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002763 { "msr", 0, &monitor_get_msr, },
2764 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002765 { "tbu", 0, &monitor_get_tbu, },
2766 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002767#if defined(TARGET_PPC64)
2768 /* Address space register */
2769 { "asr", offsetof(CPUState, asr) },
2770#endif
2771 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002772 { "sdr1", offsetof(CPUState, sdr1) },
2773 { "sr0", offsetof(CPUState, sr[0]) },
2774 { "sr1", offsetof(CPUState, sr[1]) },
2775 { "sr2", offsetof(CPUState, sr[2]) },
2776 { "sr3", offsetof(CPUState, sr[3]) },
2777 { "sr4", offsetof(CPUState, sr[4]) },
2778 { "sr5", offsetof(CPUState, sr[5]) },
2779 { "sr6", offsetof(CPUState, sr[6]) },
2780 { "sr7", offsetof(CPUState, sr[7]) },
2781 { "sr8", offsetof(CPUState, sr[8]) },
2782 { "sr9", offsetof(CPUState, sr[9]) },
2783 { "sr10", offsetof(CPUState, sr[10]) },
2784 { "sr11", offsetof(CPUState, sr[11]) },
2785 { "sr12", offsetof(CPUState, sr[12]) },
2786 { "sr13", offsetof(CPUState, sr[13]) },
2787 { "sr14", offsetof(CPUState, sr[14]) },
2788 { "sr15", offsetof(CPUState, sr[15]) },
2789 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002790#elif defined(TARGET_SPARC)
2791 { "g0", offsetof(CPUState, gregs[0]) },
2792 { "g1", offsetof(CPUState, gregs[1]) },
2793 { "g2", offsetof(CPUState, gregs[2]) },
2794 { "g3", offsetof(CPUState, gregs[3]) },
2795 { "g4", offsetof(CPUState, gregs[4]) },
2796 { "g5", offsetof(CPUState, gregs[5]) },
2797 { "g6", offsetof(CPUState, gregs[6]) },
2798 { "g7", offsetof(CPUState, gregs[7]) },
2799 { "o0", 0, monitor_get_reg },
2800 { "o1", 1, monitor_get_reg },
2801 { "o2", 2, monitor_get_reg },
2802 { "o3", 3, monitor_get_reg },
2803 { "o4", 4, monitor_get_reg },
2804 { "o5", 5, monitor_get_reg },
2805 { "o6", 6, monitor_get_reg },
2806 { "o7", 7, monitor_get_reg },
2807 { "l0", 8, monitor_get_reg },
2808 { "l1", 9, monitor_get_reg },
2809 { "l2", 10, monitor_get_reg },
2810 { "l3", 11, monitor_get_reg },
2811 { "l4", 12, monitor_get_reg },
2812 { "l5", 13, monitor_get_reg },
2813 { "l6", 14, monitor_get_reg },
2814 { "l7", 15, monitor_get_reg },
2815 { "i0", 16, monitor_get_reg },
2816 { "i1", 17, monitor_get_reg },
2817 { "i2", 18, monitor_get_reg },
2818 { "i3", 19, monitor_get_reg },
2819 { "i4", 20, monitor_get_reg },
2820 { "i5", 21, monitor_get_reg },
2821 { "i6", 22, monitor_get_reg },
2822 { "i7", 23, monitor_get_reg },
2823 { "pc", offsetof(CPUState, pc) },
2824 { "npc", offsetof(CPUState, npc) },
2825 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002826#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002827 { "psr", 0, &monitor_get_psr, },
2828 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002829#endif
bellarde95c8d52004-09-30 22:22:08 +00002830 { "tbr", offsetof(CPUState, tbr) },
2831 { "fsr", offsetof(CPUState, fsr) },
2832 { "f0", offsetof(CPUState, fpr[0]) },
2833 { "f1", offsetof(CPUState, fpr[1]) },
2834 { "f2", offsetof(CPUState, fpr[2]) },
2835 { "f3", offsetof(CPUState, fpr[3]) },
2836 { "f4", offsetof(CPUState, fpr[4]) },
2837 { "f5", offsetof(CPUState, fpr[5]) },
2838 { "f6", offsetof(CPUState, fpr[6]) },
2839 { "f7", offsetof(CPUState, fpr[7]) },
2840 { "f8", offsetof(CPUState, fpr[8]) },
2841 { "f9", offsetof(CPUState, fpr[9]) },
2842 { "f10", offsetof(CPUState, fpr[10]) },
2843 { "f11", offsetof(CPUState, fpr[11]) },
2844 { "f12", offsetof(CPUState, fpr[12]) },
2845 { "f13", offsetof(CPUState, fpr[13]) },
2846 { "f14", offsetof(CPUState, fpr[14]) },
2847 { "f15", offsetof(CPUState, fpr[15]) },
2848 { "f16", offsetof(CPUState, fpr[16]) },
2849 { "f17", offsetof(CPUState, fpr[17]) },
2850 { "f18", offsetof(CPUState, fpr[18]) },
2851 { "f19", offsetof(CPUState, fpr[19]) },
2852 { "f20", offsetof(CPUState, fpr[20]) },
2853 { "f21", offsetof(CPUState, fpr[21]) },
2854 { "f22", offsetof(CPUState, fpr[22]) },
2855 { "f23", offsetof(CPUState, fpr[23]) },
2856 { "f24", offsetof(CPUState, fpr[24]) },
2857 { "f25", offsetof(CPUState, fpr[25]) },
2858 { "f26", offsetof(CPUState, fpr[26]) },
2859 { "f27", offsetof(CPUState, fpr[27]) },
2860 { "f28", offsetof(CPUState, fpr[28]) },
2861 { "f29", offsetof(CPUState, fpr[29]) },
2862 { "f30", offsetof(CPUState, fpr[30]) },
2863 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002864#ifdef TARGET_SPARC64
2865 { "f32", offsetof(CPUState, fpr[32]) },
2866 { "f34", offsetof(CPUState, fpr[34]) },
2867 { "f36", offsetof(CPUState, fpr[36]) },
2868 { "f38", offsetof(CPUState, fpr[38]) },
2869 { "f40", offsetof(CPUState, fpr[40]) },
2870 { "f42", offsetof(CPUState, fpr[42]) },
2871 { "f44", offsetof(CPUState, fpr[44]) },
2872 { "f46", offsetof(CPUState, fpr[46]) },
2873 { "f48", offsetof(CPUState, fpr[48]) },
2874 { "f50", offsetof(CPUState, fpr[50]) },
2875 { "f52", offsetof(CPUState, fpr[52]) },
2876 { "f54", offsetof(CPUState, fpr[54]) },
2877 { "f56", offsetof(CPUState, fpr[56]) },
2878 { "f58", offsetof(CPUState, fpr[58]) },
2879 { "f60", offsetof(CPUState, fpr[60]) },
2880 { "f62", offsetof(CPUState, fpr[62]) },
2881 { "asi", offsetof(CPUState, asi) },
2882 { "pstate", offsetof(CPUState, pstate) },
2883 { "cansave", offsetof(CPUState, cansave) },
2884 { "canrestore", offsetof(CPUState, canrestore) },
2885 { "otherwin", offsetof(CPUState, otherwin) },
2886 { "wstate", offsetof(CPUState, wstate) },
2887 { "cleanwin", offsetof(CPUState, cleanwin) },
2888 { "fprs", offsetof(CPUState, fprs) },
2889#endif
bellard9307c4c2004-04-04 12:57:25 +00002890#endif
2891 { NULL },
2892};
2893
aliguori376253e2009-03-05 23:01:23 +00002894static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002895{
aliguori376253e2009-03-05 23:01:23 +00002896 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002897 longjmp(expr_env, 1);
2898}
2899
Markus Armbruster09b94182010-01-20 13:07:30 +01002900/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002901static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002902{
blueswir18662d652008-10-02 18:32:44 +00002903 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002904 void *ptr;
2905
bellard9307c4c2004-04-04 12:57:25 +00002906 for(md = monitor_defs; md->name != NULL; md++) {
2907 if (compare_cmd(name, md->name)) {
2908 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002909 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002910 } else {
bellard6a00d602005-11-21 23:25:50 +00002911 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002912 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002913 switch(md->type) {
2914 case MD_I32:
2915 *pval = *(int32_t *)ptr;
2916 break;
2917 case MD_TLONG:
2918 *pval = *(target_long *)ptr;
2919 break;
2920 default:
2921 *pval = 0;
2922 break;
2923 }
bellard9307c4c2004-04-04 12:57:25 +00002924 }
2925 return 0;
2926 }
2927 }
2928 return -1;
2929}
2930
2931static void next(void)
2932{
Blue Swirl660f11b2009-07-31 21:16:51 +00002933 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002934 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002935 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002936 pch++;
2937 }
2938}
2939
aliguori376253e2009-03-05 23:01:23 +00002940static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002941
aliguori376253e2009-03-05 23:01:23 +00002942static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002943{
blueswir1c2efc952007-09-25 17:28:42 +00002944 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002945 char *p;
bellard6a00d602005-11-21 23:25:50 +00002946 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002947
2948 switch(*pch) {
2949 case '+':
2950 next();
aliguori376253e2009-03-05 23:01:23 +00002951 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002952 break;
2953 case '-':
2954 next();
aliguori376253e2009-03-05 23:01:23 +00002955 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002956 break;
2957 case '~':
2958 next();
aliguori376253e2009-03-05 23:01:23 +00002959 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002960 break;
2961 case '(':
2962 next();
aliguori376253e2009-03-05 23:01:23 +00002963 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002964 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002965 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002966 }
2967 next();
2968 break;
bellard81d09122004-07-14 17:21:37 +00002969 case '\'':
2970 pch++;
2971 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002972 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002973 n = *pch;
2974 pch++;
2975 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002976 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002977 next();
2978 break;
bellard9307c4c2004-04-04 12:57:25 +00002979 case '$':
2980 {
2981 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002982 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002983
bellard9307c4c2004-04-04 12:57:25 +00002984 pch++;
2985 q = buf;
2986 while ((*pch >= 'a' && *pch <= 'z') ||
2987 (*pch >= 'A' && *pch <= 'Z') ||
2988 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002989 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002990 if ((q - buf) < sizeof(buf) - 1)
2991 *q++ = *pch;
2992 pch++;
2993 }
blueswir1cd390082008-11-16 13:53:32 +00002994 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002995 pch++;
2996 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002997 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002998 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002999 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003000 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003001 }
3002 break;
3003 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003004 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003005 n = 0;
3006 break;
3007 default:
blueswir17743e582007-09-24 18:39:04 +00003008#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003009 n = strtoull(pch, &p, 0);
3010#else
bellard9307c4c2004-04-04 12:57:25 +00003011 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003012#endif
bellard9307c4c2004-04-04 12:57:25 +00003013 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003014 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003015 }
3016 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003017 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003018 pch++;
3019 break;
3020 }
3021 return n;
3022}
3023
3024
aliguori376253e2009-03-05 23:01:23 +00003025static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003026{
blueswir1c2efc952007-09-25 17:28:42 +00003027 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003028 int op;
ths3b46e622007-09-17 08:09:54 +00003029
aliguori376253e2009-03-05 23:01:23 +00003030 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003031 for(;;) {
3032 op = *pch;
3033 if (op != '*' && op != '/' && op != '%')
3034 break;
3035 next();
aliguori376253e2009-03-05 23:01:23 +00003036 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003037 switch(op) {
3038 default:
3039 case '*':
3040 val *= val2;
3041 break;
3042 case '/':
3043 case '%':
ths5fafdf22007-09-16 21:08:06 +00003044 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003045 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003046 if (op == '/')
3047 val /= val2;
3048 else
3049 val %= val2;
3050 break;
3051 }
3052 }
3053 return val;
3054}
3055
aliguori376253e2009-03-05 23:01:23 +00003056static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003057{
blueswir1c2efc952007-09-25 17:28:42 +00003058 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003059 int op;
bellard9307c4c2004-04-04 12:57:25 +00003060
aliguori376253e2009-03-05 23:01:23 +00003061 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003062 for(;;) {
3063 op = *pch;
3064 if (op != '&' && op != '|' && op != '^')
3065 break;
3066 next();
aliguori376253e2009-03-05 23:01:23 +00003067 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003068 switch(op) {
3069 default:
3070 case '&':
3071 val &= val2;
3072 break;
3073 case '|':
3074 val |= val2;
3075 break;
3076 case '^':
3077 val ^= val2;
3078 break;
3079 }
3080 }
3081 return val;
3082}
3083
aliguori376253e2009-03-05 23:01:23 +00003084static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003085{
blueswir1c2efc952007-09-25 17:28:42 +00003086 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003087 int op;
bellard9307c4c2004-04-04 12:57:25 +00003088
aliguori376253e2009-03-05 23:01:23 +00003089 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003090 for(;;) {
3091 op = *pch;
3092 if (op != '+' && op != '-')
3093 break;
3094 next();
aliguori376253e2009-03-05 23:01:23 +00003095 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003096 if (op == '+')
3097 val += val2;
3098 else
3099 val -= val2;
3100 }
3101 return val;
3102}
3103
aliguori376253e2009-03-05 23:01:23 +00003104static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003105{
3106 pch = *pp;
3107 if (setjmp(expr_env)) {
3108 *pp = pch;
3109 return -1;
3110 }
blueswir1cd390082008-11-16 13:53:32 +00003111 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003112 pch++;
aliguori376253e2009-03-05 23:01:23 +00003113 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003114 *pp = pch;
3115 return 0;
3116}
3117
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003118static int get_double(Monitor *mon, double *pval, const char **pp)
3119{
3120 const char *p = *pp;
3121 char *tailp;
3122 double d;
3123
3124 d = strtod(p, &tailp);
3125 if (tailp == p) {
3126 monitor_printf(mon, "Number expected\n");
3127 return -1;
3128 }
3129 if (d != d || d - d != 0) {
3130 /* NaN or infinity */
3131 monitor_printf(mon, "Bad number\n");
3132 return -1;
3133 }
3134 *pval = d;
3135 *pp = tailp;
3136 return 0;
3137}
3138
bellard9307c4c2004-04-04 12:57:25 +00003139static int get_str(char *buf, int buf_size, const char **pp)
3140{
3141 const char *p;
3142 char *q;
3143 int c;
3144
bellard81d09122004-07-14 17:21:37 +00003145 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003146 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003147 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003148 p++;
3149 if (*p == '\0') {
3150 fail:
bellard81d09122004-07-14 17:21:37 +00003151 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003152 *pp = p;
3153 return -1;
3154 }
bellard9307c4c2004-04-04 12:57:25 +00003155 if (*p == '\"') {
3156 p++;
3157 while (*p != '\0' && *p != '\"') {
3158 if (*p == '\\') {
3159 p++;
3160 c = *p++;
3161 switch(c) {
3162 case 'n':
3163 c = '\n';
3164 break;
3165 case 'r':
3166 c = '\r';
3167 break;
3168 case '\\':
3169 case '\'':
3170 case '\"':
3171 break;
3172 default:
3173 qemu_printf("unsupported escape code: '\\%c'\n", c);
3174 goto fail;
3175 }
3176 if ((q - buf) < buf_size - 1) {
3177 *q++ = c;
3178 }
3179 } else {
3180 if ((q - buf) < buf_size - 1) {
3181 *q++ = *p;
3182 }
3183 p++;
3184 }
3185 }
3186 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003187 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003188 goto fail;
3189 }
3190 p++;
3191 } else {
blueswir1cd390082008-11-16 13:53:32 +00003192 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003193 if ((q - buf) < buf_size - 1) {
3194 *q++ = *p;
3195 }
3196 p++;
3197 }
bellard9307c4c2004-04-04 12:57:25 +00003198 }
bellard81d09122004-07-14 17:21:37 +00003199 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003200 *pp = p;
3201 return 0;
3202}
3203
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003204/*
3205 * Store the command-name in cmdname, and return a pointer to
3206 * the remaining of the command string.
3207 */
3208static const char *get_command_name(const char *cmdline,
3209 char *cmdname, size_t nlen)
3210{
3211 size_t len;
3212 const char *p, *pstart;
3213
3214 p = cmdline;
3215 while (qemu_isspace(*p))
3216 p++;
3217 if (*p == '\0')
3218 return NULL;
3219 pstart = p;
3220 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3221 p++;
3222 len = p - pstart;
3223 if (len > nlen - 1)
3224 len = nlen - 1;
3225 memcpy(cmdname, pstart, len);
3226 cmdname[len] = '\0';
3227 return p;
3228}
3229
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003230/**
3231 * Read key of 'type' into 'key' and return the current
3232 * 'type' pointer.
3233 */
3234static char *key_get_info(const char *type, char **key)
3235{
3236 size_t len;
3237 char *p, *str;
3238
3239 if (*type == ',')
3240 type++;
3241
3242 p = strchr(type, ':');
3243 if (!p) {
3244 *key = NULL;
3245 return NULL;
3246 }
3247 len = p - type;
3248
3249 str = qemu_malloc(len + 1);
3250 memcpy(str, type, len);
3251 str[len] = '\0';
3252
3253 *key = str;
3254 return ++p;
3255}
3256
bellard9307c4c2004-04-04 12:57:25 +00003257static int default_fmt_format = 'x';
3258static int default_fmt_size = 4;
3259
3260#define MAX_ARGS 16
3261
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003262static int is_valid_option(const char *c, const char *typestr)
3263{
3264 char option[3];
3265
3266 option[0] = '-';
3267 option[1] = *c;
3268 option[2] = '\0';
3269
3270 typestr = strstr(typestr, option);
3271 return (typestr != NULL);
3272}
3273
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003274static const mon_cmd_t *monitor_find_command(const char *cmdname)
3275{
3276 const mon_cmd_t *cmd;
3277
3278 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3279 if (compare_cmd(cmdname, cmd->name)) {
3280 return cmd;
3281 }
3282 }
3283
3284 return NULL;
3285}
3286
Anthony Liguoric227f092009-10-01 16:12:16 -05003287static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003288 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003289 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003290{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003291 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003292 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003293 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003294 char cmdname[256];
3295 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003296 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003297
3298#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003299 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003300#endif
ths3b46e622007-09-17 08:09:54 +00003301
bellard9307c4c2004-04-04 12:57:25 +00003302 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003303 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3304 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003305 return NULL;
ths3b46e622007-09-17 08:09:54 +00003306
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003307 cmd = monitor_find_command(cmdname);
3308 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003309 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003310 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003311 }
bellard9307c4c2004-04-04 12:57:25 +00003312
bellard9307c4c2004-04-04 12:57:25 +00003313 /* parse the parameters */
3314 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003315 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003316 typestr = key_get_info(typestr, &key);
3317 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003318 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003319 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003320 typestr++;
3321 switch(c) {
3322 case 'F':
bellard81d09122004-07-14 17:21:37 +00003323 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003324 case 's':
3325 {
3326 int ret;
ths3b46e622007-09-17 08:09:54 +00003327
blueswir1cd390082008-11-16 13:53:32 +00003328 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003329 p++;
3330 if (*typestr == '?') {
3331 typestr++;
3332 if (*p == '\0') {
3333 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003334 break;
bellard9307c4c2004-04-04 12:57:25 +00003335 }
3336 }
3337 ret = get_str(buf, sizeof(buf), &p);
3338 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003339 switch(c) {
3340 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003341 monitor_printf(mon, "%s: filename expected\n",
3342 cmdname);
bellard81d09122004-07-14 17:21:37 +00003343 break;
3344 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003345 monitor_printf(mon, "%s: block device name expected\n",
3346 cmdname);
bellard81d09122004-07-14 17:21:37 +00003347 break;
3348 default:
aliguori376253e2009-03-05 23:01:23 +00003349 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003350 break;
3351 }
bellard9307c4c2004-04-04 12:57:25 +00003352 goto fail;
3353 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003354 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003355 }
3356 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003357 case 'O':
3358 {
3359 QemuOptsList *opts_list;
3360 QemuOpts *opts;
3361
3362 opts_list = qemu_find_opts(key);
3363 if (!opts_list || opts_list->desc->name) {
3364 goto bad_type;
3365 }
3366 while (qemu_isspace(*p)) {
3367 p++;
3368 }
3369 if (!*p)
3370 break;
3371 if (get_str(buf, sizeof(buf), &p) < 0) {
3372 goto fail;
3373 }
3374 opts = qemu_opts_parse(opts_list, buf, 1);
3375 if (!opts) {
3376 goto fail;
3377 }
3378 qemu_opts_to_qdict(opts, qdict);
3379 qemu_opts_del(opts);
3380 }
3381 break;
bellard9307c4c2004-04-04 12:57:25 +00003382 case '/':
3383 {
3384 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003385
blueswir1cd390082008-11-16 13:53:32 +00003386 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003387 p++;
3388 if (*p == '/') {
3389 /* format found */
3390 p++;
3391 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003392 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003393 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003394 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003395 count = count * 10 + (*p - '0');
3396 p++;
3397 }
3398 }
3399 size = -1;
3400 format = -1;
3401 for(;;) {
3402 switch(*p) {
3403 case 'o':
3404 case 'd':
3405 case 'u':
3406 case 'x':
3407 case 'i':
3408 case 'c':
3409 format = *p++;
3410 break;
3411 case 'b':
3412 size = 1;
3413 p++;
3414 break;
3415 case 'h':
3416 size = 2;
3417 p++;
3418 break;
3419 case 'w':
3420 size = 4;
3421 p++;
3422 break;
3423 case 'g':
3424 case 'L':
3425 size = 8;
3426 p++;
3427 break;
3428 default:
3429 goto next;
3430 }
3431 }
3432 next:
blueswir1cd390082008-11-16 13:53:32 +00003433 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003434 monitor_printf(mon, "invalid char in format: '%c'\n",
3435 *p);
bellard9307c4c2004-04-04 12:57:25 +00003436 goto fail;
3437 }
bellard9307c4c2004-04-04 12:57:25 +00003438 if (format < 0)
3439 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003440 if (format != 'i') {
3441 /* for 'i', not specifying a size gives -1 as size */
3442 if (size < 0)
3443 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003444 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003445 }
bellard9307c4c2004-04-04 12:57:25 +00003446 default_fmt_format = format;
3447 } else {
3448 count = 1;
3449 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003450 if (format != 'i') {
3451 size = default_fmt_size;
3452 } else {
3453 size = -1;
3454 }
bellard9307c4c2004-04-04 12:57:25 +00003455 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003456 qdict_put(qdict, "count", qint_from_int(count));
3457 qdict_put(qdict, "format", qint_from_int(format));
3458 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003459 }
3460 break;
3461 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003462 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003463 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003464 {
blueswir1c2efc952007-09-25 17:28:42 +00003465 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003466
blueswir1cd390082008-11-16 13:53:32 +00003467 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003468 p++;
bellard34405572004-06-08 00:55:58 +00003469 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003470 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003471 if (*p == '\0') {
3472 typestr++;
3473 break;
3474 }
bellard34405572004-06-08 00:55:58 +00003475 } else {
3476 if (*p == '.') {
3477 p++;
blueswir1cd390082008-11-16 13:53:32 +00003478 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003479 p++;
bellard34405572004-06-08 00:55:58 +00003480 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003481 typestr++;
3482 break;
bellard34405572004-06-08 00:55:58 +00003483 }
3484 }
bellard13224a82006-07-14 22:03:35 +00003485 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003486 }
aliguori376253e2009-03-05 23:01:23 +00003487 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003488 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003489 /* Check if 'i' is greater than 32-bit */
3490 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3491 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3492 monitor_printf(mon, "integer is for 32-bit values\n");
3493 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003494 } else if (c == 'M') {
3495 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003496 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003497 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003498 }
3499 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003500 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003501 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003502 {
3503 double val;
3504
3505 while (qemu_isspace(*p))
3506 p++;
3507 if (*typestr == '?') {
3508 typestr++;
3509 if (*p == '\0') {
3510 break;
3511 }
3512 }
3513 if (get_double(mon, &val, &p) < 0) {
3514 goto fail;
3515 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003516 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003517 switch (*p) {
3518 case 'K': case 'k':
3519 val *= 1 << 10; p++; break;
3520 case 'M': case 'm':
3521 val *= 1 << 20; p++; break;
3522 case 'G': case 'g':
3523 val *= 1 << 30; p++; break;
3524 }
3525 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003526 if (c == 'T' && p[0] && p[1] == 's') {
3527 switch (*p) {
3528 case 'm':
3529 val /= 1e3; p += 2; break;
3530 case 'u':
3531 val /= 1e6; p += 2; break;
3532 case 'n':
3533 val /= 1e9; p += 2; break;
3534 }
3535 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003536 if (*p && !qemu_isspace(*p)) {
3537 monitor_printf(mon, "Unknown unit suffix\n");
3538 goto fail;
3539 }
3540 qdict_put(qdict, key, qfloat_from_double(val));
3541 }
3542 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003543 case 'b':
3544 {
3545 const char *beg;
3546 int val;
3547
3548 while (qemu_isspace(*p)) {
3549 p++;
3550 }
3551 beg = p;
3552 while (qemu_isgraph(*p)) {
3553 p++;
3554 }
3555 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3556 val = 1;
3557 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3558 val = 0;
3559 } else {
3560 monitor_printf(mon, "Expected 'on' or 'off'\n");
3561 goto fail;
3562 }
3563 qdict_put(qdict, key, qbool_from_int(val));
3564 }
3565 break;
bellard9307c4c2004-04-04 12:57:25 +00003566 case '-':
3567 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003568 const char *tmp = p;
3569 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003570 /* option */
ths3b46e622007-09-17 08:09:54 +00003571
bellard9307c4c2004-04-04 12:57:25 +00003572 c = *typestr++;
3573 if (c == '\0')
3574 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003575 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003576 p++;
3577 has_option = 0;
3578 if (*p == '-') {
3579 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003580 if(c != *p) {
3581 if(!is_valid_option(p, typestr)) {
3582
3583 monitor_printf(mon, "%s: unsupported option -%c\n",
3584 cmdname, *p);
3585 goto fail;
3586 } else {
3587 skip_key = 1;
3588 }
bellard9307c4c2004-04-04 12:57:25 +00003589 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003590 if(skip_key) {
3591 p = tmp;
3592 } else {
3593 p++;
3594 has_option = 1;
3595 }
bellard9307c4c2004-04-04 12:57:25 +00003596 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003597 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003598 }
3599 break;
3600 default:
3601 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003602 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003603 goto fail;
3604 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003605 qemu_free(key);
3606 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003607 }
3608 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003609 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003610 p++;
3611 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003612 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3613 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003614 goto fail;
3615 }
3616
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003617 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003618
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003619fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003620 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003621 return NULL;
3622}
3623
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003624void monitor_set_error(Monitor *mon, QError *qerror)
3625{
3626 /* report only the first error */
3627 if (!mon->error) {
3628 mon->error = qerror;
3629 } else {
3630 MON_DEBUG("Additional error report at %s:%d\n",
3631 qerror->file, qerror->linenr);
3632 QDECREF(qerror);
3633 }
3634}
3635
Adam Litke940cc302010-01-25 12:18:44 -06003636static int is_async_return(const QObject *data)
3637{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003638 if (data && qobject_type(data) == QTYPE_QDICT) {
3639 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3640 }
3641
3642 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003643}
3644
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003645static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3646{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003647 if (monitor_ctrl_mode(mon)) {
3648 if (ret && !monitor_has_error(mon)) {
3649 /*
3650 * If it returns failure, it must have passed on error.
3651 *
3652 * Action: Report an internal error to the client if in QMP.
3653 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003654 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003655 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3656 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003657 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003658
3659#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003660 if (!ret && monitor_has_error(mon)) {
3661 /*
3662 * If it returns success, it must not have passed an error.
3663 *
3664 * Action: Report the passed error to the client.
3665 */
3666 MON_DEBUG("command '%s' returned success but passed an error\n",
3667 cmd->name);
3668 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003669
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003670 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3671 /*
3672 * Handlers should not call Monitor print functions.
3673 *
3674 * Action: Ignore them in QMP.
3675 *
3676 * (XXX: we don't check any 'info' or 'query' command here
3677 * because the user print function _is_ called by do_info(), hence
3678 * we will trigger this check. This problem will go away when we
3679 * make 'query' commands real and kill do_info())
3680 */
3681 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3682 cmd->name, mon_print_count_get(mon));
3683 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003684#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003685 } else {
3686 assert(!monitor_has_error(mon));
3687 QDECREF(mon->error);
3688 mon->error = NULL;
3689 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003690}
3691
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003692static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3693 const QDict *params)
3694{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003695 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003696 QObject *data = NULL;
3697
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003698 mon_print_count_init(mon);
3699
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003700 ret = cmd->mhandler.cmd_new(mon, params, &data);
3701 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003702
Adam Litke940cc302010-01-25 12:18:44 -06003703 if (is_async_return(data)) {
3704 /*
3705 * Asynchronous commands have no initial return data but they can
3706 * generate errors. Data is returned via the async completion handler.
3707 */
3708 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3709 monitor_protocol_emitter(mon, NULL);
3710 }
3711 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003712 /* Monitor Protocol */
3713 monitor_protocol_emitter(mon, data);
3714 } else {
3715 /* User Protocol */
3716 if (data)
3717 cmd->user_print(mon, data);
3718 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003719
3720 qobject_decref(data);
3721}
3722
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003723static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003724{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003725 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003726 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003727
3728 qdict = qdict_new();
3729
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003730 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003731 if (!cmd)
3732 goto out;
3733
Adam Litke940cc302010-01-25 12:18:44 -06003734 if (monitor_handler_is_async(cmd)) {
3735 user_async_cmd_handler(mon, cmd, qdict);
3736 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003737 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003738 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003739 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003740 }
3741
Luiz Capitulino13917be2009-10-07 13:41:54 -03003742out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003743 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003744}
3745
bellard81d09122004-07-14 17:21:37 +00003746static void cmd_completion(const char *name, const char *list)
3747{
3748 const char *p, *pstart;
3749 char cmd[128];
3750 int len;
3751
3752 p = list;
3753 for(;;) {
3754 pstart = p;
3755 p = strchr(p, '|');
3756 if (!p)
3757 p = pstart + strlen(pstart);
3758 len = p - pstart;
3759 if (len > sizeof(cmd) - 2)
3760 len = sizeof(cmd) - 2;
3761 memcpy(cmd, pstart, len);
3762 cmd[len] = '\0';
3763 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003764 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003765 }
3766 if (*p == '\0')
3767 break;
3768 p++;
3769 }
3770}
3771
3772static void file_completion(const char *input)
3773{
3774 DIR *ffs;
3775 struct dirent *d;
3776 char path[1024];
3777 char file[1024], file_prefix[1024];
3778 int input_path_len;
3779 const char *p;
3780
ths5fafdf22007-09-16 21:08:06 +00003781 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003782 if (!p) {
3783 input_path_len = 0;
3784 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003785 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003786 } else {
3787 input_path_len = p - input + 1;
3788 memcpy(path, input, input_path_len);
3789 if (input_path_len > sizeof(path) - 1)
3790 input_path_len = sizeof(path) - 1;
3791 path[input_path_len] = '\0';
3792 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3793 }
3794#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003795 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3796 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003797#endif
3798 ffs = opendir(path);
3799 if (!ffs)
3800 return;
3801 for(;;) {
3802 struct stat sb;
3803 d = readdir(ffs);
3804 if (!d)
3805 break;
3806 if (strstart(d->d_name, file_prefix, NULL)) {
3807 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003808 if (input_path_len < sizeof(file))
3809 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3810 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003811 /* stat the file to find out if it's a directory.
3812 * In that case add a slash to speed up typing long paths
3813 */
3814 stat(file, &sb);
3815 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003816 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003817 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003818 }
3819 }
3820 closedir(ffs);
3821}
3822
aliguori51de9762009-03-05 23:00:43 +00003823static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003824{
aliguori51de9762009-03-05 23:00:43 +00003825 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003826 const char *input = opaque;
3827
3828 if (input[0] == '\0' ||
3829 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003830 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003831 }
3832}
3833
3834/* NOTE: this parser is an approximate form of the real command parser */
3835static void parse_cmdline(const char *cmdline,
3836 int *pnb_args, char **args)
3837{
3838 const char *p;
3839 int nb_args, ret;
3840 char buf[1024];
3841
3842 p = cmdline;
3843 nb_args = 0;
3844 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003845 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003846 p++;
3847 if (*p == '\0')
3848 break;
3849 if (nb_args >= MAX_ARGS)
3850 break;
3851 ret = get_str(buf, sizeof(buf), &p);
3852 args[nb_args] = qemu_strdup(buf);
3853 nb_args++;
3854 if (ret < 0)
3855 break;
3856 }
3857 *pnb_args = nb_args;
3858}
3859
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003860static const char *next_arg_type(const char *typestr)
3861{
3862 const char *p = strchr(typestr, ':');
3863 return (p != NULL ? ++p : typestr);
3864}
3865
aliguori4c36ba32009-03-05 23:01:37 +00003866static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003867{
3868 const char *cmdname;
3869 char *args[MAX_ARGS];
3870 int nb_args, i, len;
3871 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003872 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003873 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003874
3875 parse_cmdline(cmdline, &nb_args, args);
3876#ifdef DEBUG_COMPLETION
3877 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003878 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003879 }
3880#endif
3881
3882 /* if the line ends with a space, it means we want to complete the
3883 next arg */
3884 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003885 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003886 if (nb_args >= MAX_ARGS)
3887 return;
3888 args[nb_args++] = qemu_strdup("");
3889 }
3890 if (nb_args <= 1) {
3891 /* command completion */
3892 if (nb_args == 0)
3893 cmdname = "";
3894 else
3895 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003896 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003897 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003898 cmd_completion(cmdname, cmd->name);
3899 }
3900 } else {
3901 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003902 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003903 if (compare_cmd(args[0], cmd->name))
3904 goto found;
3905 }
3906 return;
3907 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003908 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003909 for(i = 0; i < nb_args - 2; i++) {
3910 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003911 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003912 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003913 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003914 }
3915 }
3916 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003917 if (*ptype == '-' && ptype[1] != '\0') {
3918 ptype += 2;
3919 }
bellard81d09122004-07-14 17:21:37 +00003920 switch(*ptype) {
3921 case 'F':
3922 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003923 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003924 file_completion(str);
3925 break;
3926 case 'B':
3927 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003928 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003929 bdrv_iterate(block_completion_it, (void *)str);
3930 break;
bellard7fe48482004-10-09 18:08:01 +00003931 case 's':
3932 /* XXX: more generic ? */
3933 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003934 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003935 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3936 cmd_completion(str, cmd->name);
3937 }
bellard64866c32006-05-07 18:03:31 +00003938 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003939 char *sep = strrchr(str, '-');
3940 if (sep)
3941 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003942 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003943 for(key = key_defs; key->name != NULL; key++) {
3944 cmd_completion(str, key->name);
3945 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003946 } else if (!strcmp(cmd->name, "help|?")) {
3947 readline_set_completion_index(cur_mon->rs, strlen(str));
3948 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3949 cmd_completion(str, cmd->name);
3950 }
bellard7fe48482004-10-09 18:08:01 +00003951 }
3952 break;
bellard81d09122004-07-14 17:21:37 +00003953 default:
3954 break;
3955 }
3956 }
3957 for(i = 0; i < nb_args; i++)
3958 qemu_free(args[i]);
3959}
3960
aliguori731b0362009-03-05 23:01:42 +00003961static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003962{
aliguori731b0362009-03-05 23:01:42 +00003963 Monitor *mon = opaque;
3964
Jan Kiszkac62313b2009-12-04 14:05:29 +01003965 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003966}
3967
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003968typedef struct CmdArgs {
3969 QString *name;
3970 int type;
3971 int flag;
3972 int optional;
3973} CmdArgs;
3974
3975static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
3976{
3977 if (!cmd_args->optional) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003978 qerror_report(QERR_MISSING_PARAMETER, name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003979 return -1;
3980 }
3981
3982 if (cmd_args->type == '-') {
3983 /* handlers expect a value, they need to be changed */
3984 qdict_put(args, name, qint_from_int(0));
3985 }
3986
3987 return 0;
3988}
3989
3990static int check_arg(const CmdArgs *cmd_args, QDict *args)
3991{
3992 QObject *value;
3993 const char *name;
3994
3995 name = qstring_get_str(cmd_args->name);
3996
3997 if (!args) {
3998 return check_opt(cmd_args, name, args);
3999 }
4000
4001 value = qdict_get(args, name);
4002 if (!value) {
4003 return check_opt(cmd_args, name, args);
4004 }
4005
4006 switch (cmd_args->type) {
4007 case 'F':
4008 case 'B':
4009 case 's':
4010 if (qobject_type(value) != QTYPE_QSTRING) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004011 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "string");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004012 return -1;
4013 }
4014 break;
4015 case '/': {
4016 int i;
4017 const char *keys[] = { "count", "format", "size", NULL };
4018
4019 for (i = 0; keys[i]; i++) {
4020 QObject *obj = qdict_get(args, keys[i]);
4021 if (!obj) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004022 qerror_report(QERR_MISSING_PARAMETER, name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004023 return -1;
4024 }
4025 if (qobject_type(obj) != QTYPE_QINT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004026 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004027 return -1;
4028 }
4029 }
4030 break;
4031 }
4032 case 'i':
4033 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004034 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004035 if (qobject_type(value) != QTYPE_QINT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004036 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004037 return -1;
4038 }
4039 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01004040 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004041 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004042 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004043 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "number");
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004044 return -1;
4045 }
4046 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004047 case 'b':
4048 if (qobject_type(value) != QTYPE_QBOOL) {
4049 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4050 return -1;
4051 }
4052 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004053 case '-':
4054 if (qobject_type(value) != QTYPE_QINT &&
4055 qobject_type(value) != QTYPE_QBOOL) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004056 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "bool");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004057 return -1;
4058 }
4059 if (qobject_type(value) == QTYPE_QBOOL) {
4060 /* handlers expect a QInt, they need to be changed */
4061 qdict_put(args, name,
4062 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4063 }
4064 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004065 case 'O':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004066 default:
4067 /* impossible */
4068 abort();
4069 }
4070
4071 return 0;
4072}
4073
4074static void cmd_args_init(CmdArgs *cmd_args)
4075{
4076 cmd_args->name = qstring_new();
4077 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4078}
4079
Markus Armbruster361127d2010-02-10 20:24:35 +01004080static int check_opts(QemuOptsList *opts_list, QDict *args)
4081{
4082 assert(!opts_list->desc->name);
4083 return 0;
4084}
4085
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004086/*
4087 * This is not trivial, we have to parse Monitor command's argument
4088 * type syntax to be able to check the arguments provided by clients.
4089 *
4090 * In the near future we will be using an array for that and will be
4091 * able to drop all this parsing...
4092 */
4093static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4094{
4095 int err;
4096 const char *p;
4097 CmdArgs cmd_args;
Markus Armbruster361127d2010-02-10 20:24:35 +01004098 QemuOptsList *opts_list;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004099
Blue Swirldd5121b2009-12-04 20:52:02 +00004100 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004101 return (qdict_size(args) == 0 ? 0 : -1);
4102 }
4103
4104 err = 0;
4105 cmd_args_init(&cmd_args);
Markus Armbruster361127d2010-02-10 20:24:35 +01004106 opts_list = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004107
4108 for (p = cmd->args_type;; p++) {
4109 if (*p == ':') {
4110 cmd_args.type = *++p;
4111 p++;
4112 if (cmd_args.type == '-') {
4113 cmd_args.flag = *p++;
4114 cmd_args.optional = 1;
Markus Armbruster361127d2010-02-10 20:24:35 +01004115 } else if (cmd_args.type == 'O') {
4116 opts_list = qemu_find_opts(qstring_get_str(cmd_args.name));
4117 assert(opts_list);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004118 } else if (*p == '?') {
4119 cmd_args.optional = 1;
4120 p++;
4121 }
4122
4123 assert(*p == ',' || *p == '\0');
Markus Armbruster361127d2010-02-10 20:24:35 +01004124 if (opts_list) {
4125 err = check_opts(opts_list, args);
4126 opts_list = NULL;
4127 } else {
4128 err = check_arg(&cmd_args, args);
4129 QDECREF(cmd_args.name);
4130 cmd_args_init(&cmd_args);
4131 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004132
4133 if (err < 0) {
4134 break;
4135 }
4136 } else {
4137 qstring_append_chr(cmd_args.name, *p);
4138 }
4139
4140 if (*p == '\0') {
4141 break;
4142 }
4143 }
4144
4145 QDECREF(cmd_args.name);
4146 return err;
4147}
4148
Luiz Capitulino09069b12010-02-04 18:10:06 -02004149static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4150{
4151 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4152 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4153}
4154
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004155static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4156{
4157 int err;
4158 QObject *obj;
4159 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004160 const mon_cmd_t *cmd;
4161 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004162 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004163
4164 args = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004165
4166 obj = json_parser_parse(tokens, NULL);
4167 if (!obj) {
4168 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004169 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004170 goto err_out;
4171 } else if (qobject_type(obj) != QTYPE_QDICT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004172 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004173 qobject_decref(obj);
4174 goto err_out;
4175 }
4176
4177 input = qobject_to_qdict(obj);
4178
4179 mon->mc->id = qdict_get(input, "id");
4180 qobject_incref(mon->mc->id);
4181
4182 obj = qdict_get(input, "execute");
4183 if (!obj) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004184 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004185 goto err_input;
4186 } else if (qobject_type(obj) != QTYPE_QSTRING) {
Luiz Capitulino88f7db82010-04-07 14:49:37 -03004187 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute", "string");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004188 goto err_input;
4189 }
4190
4191 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004192
Luiz Capitulino09069b12010-02-04 18:10:06 -02004193 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004194 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004195 goto err_input;
4196 }
4197
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004198 /*
4199 * XXX: We need this special case until we get info handlers
4200 * converted into 'query-' commands
4201 */
4202 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004203 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004204 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004205 } else if (strstart(cmd_name, "query-", &info_item)) {
4206 cmd = monitor_find_command("info");
4207 qdict_put_obj(input, "arguments",
4208 qobject_from_jsonf("{ 'item': %s }", info_item));
4209 } else {
4210 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004211 if (!cmd || !monitor_handler_ported(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004212 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004213 goto err_input;
4214 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004215 }
4216
4217 obj = qdict_get(input, "arguments");
4218 if (!obj) {
4219 args = qdict_new();
Luiz Capitulino04f8c052010-04-06 16:39:42 -03004220 } else if (qobject_type(obj) != QTYPE_QDICT) {
4221 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments", "object");
4222 goto err_input;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004223 } else {
4224 args = qobject_to_qdict(obj);
4225 QINCREF(args);
4226 }
4227
4228 QDECREF(input);
4229
4230 err = monitor_check_qmp_args(cmd, args);
4231 if (err < 0) {
4232 goto err_out;
4233 }
4234
Adam Litke940cc302010-01-25 12:18:44 -06004235 if (monitor_handler_is_async(cmd)) {
4236 qmp_async_cmd_handler(mon, cmd, args);
4237 } else {
4238 monitor_call_handler(mon, cmd, args);
4239 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004240 goto out;
4241
4242err_input:
4243 QDECREF(input);
4244err_out:
4245 monitor_protocol_emitter(mon, NULL);
4246out:
4247 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004248}
4249
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004250/**
4251 * monitor_control_read(): Read and handle QMP input
4252 */
4253static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4254{
4255 Monitor *old_mon = cur_mon;
4256
4257 cur_mon = opaque;
4258
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004259 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004260
4261 cur_mon = old_mon;
4262}
4263
aliguori731b0362009-03-05 23:01:42 +00004264static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004265{
aliguori731b0362009-03-05 23:01:42 +00004266 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004267 int i;
aliguori376253e2009-03-05 23:01:23 +00004268
aliguori731b0362009-03-05 23:01:42 +00004269 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004270
aliguoricde76ee2009-03-05 23:01:51 +00004271 if (cur_mon->rs) {
4272 for (i = 0; i < size; i++)
4273 readline_handle_byte(cur_mon->rs, buf[i]);
4274 } else {
4275 if (size == 0 || buf[size - 1] != 0)
4276 monitor_printf(cur_mon, "corrupted command\n");
4277 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004278 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004279 }
aliguori731b0362009-03-05 23:01:42 +00004280
4281 cur_mon = old_mon;
4282}
aliguorid8f44602008-10-06 13:52:44 +00004283
aliguori376253e2009-03-05 23:01:23 +00004284static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004285{
aliguori731b0362009-03-05 23:01:42 +00004286 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004287 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004288 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004289}
4290
aliguoricde76ee2009-03-05 23:01:51 +00004291int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004292{
aliguoricde76ee2009-03-05 23:01:51 +00004293 if (!mon->rs)
4294 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004295 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004296 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004297}
4298
aliguori376253e2009-03-05 23:01:23 +00004299void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004300{
aliguoricde76ee2009-03-05 23:01:51 +00004301 if (!mon->rs)
4302 return;
aliguori731b0362009-03-05 23:01:42 +00004303 if (--mon->suspend_cnt == 0)
4304 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004305}
4306
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004307static QObject *get_qmp_greeting(void)
4308{
4309 QObject *ver;
4310
4311 do_info_version(NULL, &ver);
4312 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4313}
4314
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004315/**
4316 * monitor_control_event(): Print QMP gretting
4317 */
4318static void monitor_control_event(void *opaque, int event)
4319{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004320 QObject *data;
4321 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004322
Luiz Capitulino47116d12010-02-08 17:01:30 -02004323 switch (event) {
4324 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004325 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004326 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004327 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004328 monitor_json_emitter(mon, data);
4329 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004330 break;
4331 case CHR_EVENT_CLOSED:
4332 json_message_parser_destroy(&mon->mc->parser);
4333 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004334 }
4335}
4336
aliguori731b0362009-03-05 23:01:42 +00004337static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004338{
aliguori376253e2009-03-05 23:01:23 +00004339 Monitor *mon = opaque;
4340
aliguori2724b182009-03-05 23:01:47 +00004341 switch (event) {
4342 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004343 mon->mux_out = 0;
4344 if (mon->reset_seen) {
4345 readline_restart(mon->rs);
4346 monitor_resume(mon);
4347 monitor_flush(mon);
4348 } else {
4349 mon->suspend_cnt = 0;
4350 }
aliguori2724b182009-03-05 23:01:47 +00004351 break;
ths86e94de2007-01-05 22:01:59 +00004352
aliguori2724b182009-03-05 23:01:47 +00004353 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004354 if (mon->reset_seen) {
4355 if (mon->suspend_cnt == 0) {
4356 monitor_printf(mon, "\n");
4357 }
4358 monitor_flush(mon);
4359 monitor_suspend(mon);
4360 } else {
4361 mon->suspend_cnt++;
4362 }
4363 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004364 break;
4365
Amit Shahb6b8df52009-10-07 18:31:16 +05304366 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004367 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4368 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004369 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004370 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004371 }
4372 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004373 break;
4374 }
ths86e94de2007-01-05 22:01:59 +00004375}
4376
aliguori76655d62009-03-06 20:27:37 +00004377
4378/*
4379 * Local variables:
4380 * c-indent-level: 4
4381 * c-basic-offset: 4
4382 * tab-width: 8
4383 * End:
4384 */
4385
aliguori731b0362009-03-05 23:01:42 +00004386void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004387{
aliguori731b0362009-03-05 23:01:42 +00004388 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004389 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004390
4391 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004392 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004393 is_first_init = 0;
4394 }
aliguori87127162009-03-05 23:01:29 +00004395
4396 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004397
aliguori87127162009-03-05 23:01:29 +00004398 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004399 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004400 if (flags & MONITOR_USE_READLINE) {
4401 mon->rs = readline_init(mon, monitor_find_completion);
4402 monitor_read_command(mon, 0);
4403 }
aliguori87127162009-03-05 23:01:29 +00004404
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004405 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004406 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004407 /* Control mode requires special handlers */
4408 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4409 monitor_control_event, mon);
4410 } else {
4411 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4412 monitor_event, mon);
4413 }
aliguori87127162009-03-05 23:01:29 +00004414
Blue Swirl72cf2d42009-09-12 07:36:22 +00004415 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004416 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4417 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004418}
4419
aliguori376253e2009-03-05 23:01:23 +00004420static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004421{
aliguoribb5fc202009-03-05 23:01:15 +00004422 BlockDriverState *bs = opaque;
4423 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004424
aliguoribb5fc202009-03-05 23:01:15 +00004425 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004426 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004427 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004428 }
aliguori731b0362009-03-05 23:01:42 +00004429 if (mon->password_completion_cb)
4430 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004431
aliguori731b0362009-03-05 23:01:42 +00004432 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004433}
aliguoric0f4ce72009-03-05 23:01:01 +00004434
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004435int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4436 BlockDriverCompletionFunc *completion_cb,
4437 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004438{
aliguoricde76ee2009-03-05 23:01:51 +00004439 int err;
4440
aliguoribb5fc202009-03-05 23:01:15 +00004441 if (!bdrv_key_required(bs)) {
4442 if (completion_cb)
4443 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004444 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004445 }
aliguoric0f4ce72009-03-05 23:01:01 +00004446
Luiz Capitulino94171e12009-12-07 21:37:00 +01004447 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004448 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004449 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004450 }
4451
aliguori376253e2009-03-05 23:01:23 +00004452 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4453 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004454
aliguori731b0362009-03-05 23:01:42 +00004455 mon->password_completion_cb = completion_cb;
4456 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004457
aliguoricde76ee2009-03-05 23:01:51 +00004458 err = monitor_read_password(mon, bdrv_password_cb, bs);
4459
4460 if (err && completion_cb)
4461 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004462
4463 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004464}