blob: 0e69bc8b9dedc61436b5c9e13f22e09192f43d30 [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"
Prerna Saxena22890ab2010-06-24 17:04:53 +053059#ifdef CONFIG_SIMPLE_TRACE
60#include "trace.h"
61#endif
ths6a5bd302007-12-03 17:05:38 +000062
bellard9dc39cb2004-03-14 21:38:27 +000063//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000064//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000065
bellard9307c4c2004-04-04 12:57:25 +000066/*
67 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000068 *
bellard9307c4c2004-04-04 12:57:25 +000069 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000070 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000071 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010072 * 'O' option string of the form NAME=VALUE,...
73 * parsed according to QemuOptsList given by its name
74 * Example: 'device:O' uses qemu_device_opts.
75 * Restriction: only lists with empty desc are supported
76 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000077 * 'i' 32 bit integer
78 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010079 * 'M' just like 'l', except in user mode the value is
80 * multiplied by 2^20 (think Mebibyte)
Markus Armbrusteree9545d2010-03-26 09:07:08 +010081 * 'f' double
Markus Armbruster3350a4d2010-01-25 14:23:03 +010082 * user mode accepts an optional G, g, M, m, K, k suffix,
83 * which multiplies the value by 2^30 for suffixes G and
84 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010085 * 'T' double
86 * user mode accepts an optional ms, us, ns suffix,
87 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000088 * '/' optional gdb-like print format (like "/10x")
89 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030090 * '?' optional type (for all types, except '/')
91 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010092 * 'b' boolean
93 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -030094 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000095 *
96 */
97
Adam Litke940cc302010-01-25 12:18:44 -060098typedef struct MonitorCompletionData MonitorCompletionData;
99struct MonitorCompletionData {
100 Monitor *mon;
101 void (*user_print)(Monitor *mon, const QObject *data);
102};
103
Anthony Liguoric227f092009-10-01 16:12:16 -0500104typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000105 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000106 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000107 const char *params;
108 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300109 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300110 union {
111 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300112 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600113 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300114 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200115 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600116 int (*cmd_async)(Monitor *mon, const QDict *params,
117 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300118 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200119 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500120} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000121
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100122/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500123typedef struct mon_fd_t mon_fd_t;
124struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100125 char *name;
126 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500127 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100128};
129
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200130typedef struct MonitorControl {
131 QObject *id;
132 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200133 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200134} MonitorControl;
135
aliguori87127162009-03-05 23:01:29 +0000136struct Monitor {
137 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200138 int mux_out;
139 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000140 int flags;
141 int suspend_cnt;
142 uint8_t outbuf[1024];
143 int outbuf_index;
144 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000146 CPUState *mon_cpu;
147 BlockDriverCompletionFunc *password_completion_cb;
148 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200149#ifdef CONFIG_DEBUG_MONITOR
150 int print_calls_nr;
151#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200152 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500153 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000154 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000155};
156
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200157#ifdef CONFIG_DEBUG_MONITOR
158#define MON_DEBUG(fmt, ...) do { \
159 fprintf(stderr, "Monitor: "); \
160 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200161
162static inline void mon_print_count_inc(Monitor *mon)
163{
164 mon->print_calls_nr++;
165}
166
167static inline void mon_print_count_init(Monitor *mon)
168{
169 mon->print_calls_nr = 0;
170}
171
172static inline int mon_print_count_get(const Monitor *mon)
173{
174 return mon->print_calls_nr;
175}
176
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200177#else /* !CONFIG_DEBUG_MONITOR */
178#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200179static inline void mon_print_count_inc(Monitor *mon) { }
180static inline void mon_print_count_init(Monitor *mon) { }
181static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200182#endif /* CONFIG_DEBUG_MONITOR */
183
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300184/* QMP checker flags */
185#define QMP_ACCEPT_UNKNOWNS 1
186
Blue Swirl72cf2d42009-09-12 07:36:22 +0000187static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000188
Anthony Liguoric227f092009-10-01 16:12:16 -0500189static const mon_cmd_t mon_cmds[];
190static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000191
Markus Armbruster8631b602010-02-18 11:41:55 +0100192Monitor *cur_mon;
193Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000194
aliguori731b0362009-03-05 23:01:42 +0000195static void monitor_command_cb(Monitor *mon, const char *cmdline,
196 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000197
Luiz Capitulino09069b12010-02-04 18:10:06 -0200198static inline int qmp_cmd_mode(const Monitor *mon)
199{
200 return (mon->mc ? mon->mc->command_mode : 0);
201}
202
Luiz Capitulino418173c2009-11-26 22:58:51 -0200203/* Return true if in control mode, false otherwise */
204static inline int monitor_ctrl_mode(const Monitor *mon)
205{
206 return (mon->flags & MONITOR_USE_CONTROL);
207}
208
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100209/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
210int monitor_cur_is_qmp(void)
211{
212 return cur_mon && monitor_ctrl_mode(cur_mon);
213}
214
aliguori731b0362009-03-05 23:01:42 +0000215static void monitor_read_command(Monitor *mon, int show_prompt)
216{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200217 if (!mon->rs)
218 return;
219
aliguori731b0362009-03-05 23:01:42 +0000220 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
221 if (show_prompt)
222 readline_show_prompt(mon->rs);
223}
bellard6a00d602005-11-21 23:25:50 +0000224
aliguoricde76ee2009-03-05 23:01:51 +0000225static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
226 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000227{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100228 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100229 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100230 return -EINVAL;
231 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000232 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
233 /* prompt is printed on return from the command handler */
234 return 0;
235 } else {
236 monitor_printf(mon, "terminal does not support password prompting\n");
237 return -ENOTTY;
238 }
aliguoribb5fc202009-03-05 23:01:15 +0000239}
240
aliguori376253e2009-03-05 23:01:23 +0000241void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000242{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200243 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000244 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
245 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000246 }
247}
248
249/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000250static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000251{
ths60fe76f2007-12-16 03:02:09 +0000252 char c;
aliguori731b0362009-03-05 23:01:42 +0000253
bellard7e2515e2004-08-01 21:52:19 +0000254 for(;;) {
255 c = *str++;
256 if (c == '\0')
257 break;
bellard7ba12602006-07-14 20:26:42 +0000258 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000259 mon->outbuf[mon->outbuf_index++] = '\r';
260 mon->outbuf[mon->outbuf_index++] = c;
261 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
262 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000263 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
aliguori376253e2009-03-05 23:01:23 +0000267void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000268{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200269 char buf[4096];
270
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200271 if (!mon)
272 return;
273
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200274 mon_print_count_inc(mon);
275
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200276 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200277 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200278 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200279
280 vsnprintf(buf, sizeof(buf), fmt, ap);
281 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000282}
283
aliguori376253e2009-03-05 23:01:23 +0000284void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000285{
286 va_list ap;
287 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000288 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000289 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000290}
291
aliguori376253e2009-03-05 23:01:23 +0000292void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000293{
294 int i;
295
296 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000297 switch (filename[i]) {
298 case ' ':
299 case '"':
300 case '\\':
301 monitor_printf(mon, "\\%c", filename[i]);
302 break;
303 case '\t':
304 monitor_printf(mon, "\\t");
305 break;
306 case '\r':
307 monitor_printf(mon, "\\r");
308 break;
309 case '\n':
310 monitor_printf(mon, "\\n");
311 break;
312 default:
313 monitor_printf(mon, "%c", filename[i]);
314 break;
315 }
thsfef30742006-12-22 14:11:32 +0000316 }
317}
318
bellard7fe48482004-10-09 18:08:01 +0000319static int monitor_fprintf(FILE *stream, const char *fmt, ...)
320{
321 va_list ap;
322 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000323 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000324 va_end(ap);
325 return 0;
326}
327
Luiz Capitulino13c74252009-10-07 13:41:55 -0300328static void monitor_user_noop(Monitor *mon, const QObject *data) { }
329
Luiz Capitulino13917be2009-10-07 13:41:54 -0300330static inline int monitor_handler_ported(const mon_cmd_t *cmd)
331{
332 return cmd->user_print != NULL;
333}
334
Adam Litke940cc302010-01-25 12:18:44 -0600335static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
336{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200337 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600338}
339
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200340static inline bool monitor_cmd_user_only(const mon_cmd_t *cmd)
341{
342 return (cmd->flags & MONITOR_CMD_USER_ONLY);
bellard9dc39cb2004-03-14 21:38:27 +0000343}
344
Luiz Capitulino8204a912009-11-18 23:05:31 -0200345static inline int monitor_has_error(const Monitor *mon)
346{
347 return mon->error != NULL;
348}
349
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200350static void monitor_json_emitter(Monitor *mon, const QObject *data)
351{
352 QString *json;
353
354 json = qobject_to_json(data);
355 assert(json != NULL);
356
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357 qstring_append_chr(json, '\n');
358 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200359
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200360 QDECREF(json);
361}
362
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200363static void monitor_protocol_emitter(Monitor *mon, QObject *data)
364{
365 QDict *qmp;
366
367 qmp = qdict_new();
368
369 if (!monitor_has_error(mon)) {
370 /* success response */
371 if (data) {
372 qobject_incref(data);
373 qdict_put_obj(qmp, "return", data);
374 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200375 /* return an empty QDict by default */
376 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200377 }
378 } else {
379 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100380 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200381 qdict_put(qmp, "error", mon->error->error);
382 QINCREF(mon->error->error);
383 QDECREF(mon->error);
384 mon->error = NULL;
385 }
386
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200387 if (mon->mc->id) {
388 qdict_put_obj(qmp, "id", mon->mc->id);
389 mon->mc->id = NULL;
390 }
391
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200392 monitor_json_emitter(mon, QOBJECT(qmp));
393 QDECREF(qmp);
394}
395
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200396static void timestamp_put(QDict *qdict)
397{
398 int err;
399 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000400 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200401
Blue Swirld08d6f02009-12-04 18:06:39 +0000402 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200403 if (err < 0)
404 return;
405
406 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
407 "'microseconds': %" PRId64 " }",
408 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409 qdict_put_obj(qdict, "timestamp", obj);
410}
411
412/**
413 * monitor_protocol_event(): Generate a Monitor event
414 *
415 * Event-specific data can be emitted through the (optional) 'data' parameter.
416 */
417void monitor_protocol_event(MonitorEvent event, QObject *data)
418{
419 QDict *qmp;
420 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600421 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200422
Blue Swirl242cd002009-12-04 18:05:45 +0000423 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200424
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000426 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200427 event_name = "SHUTDOWN";
428 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000429 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200430 event_name = "RESET";
431 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000432 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200433 event_name = "POWERDOWN";
434 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000435 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200436 event_name = "STOP";
437 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300438 case QEVENT_RESUME:
439 event_name = "RESUME";
440 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200441 case QEVENT_VNC_CONNECTED:
442 event_name = "VNC_CONNECTED";
443 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200444 case QEVENT_VNC_INITIALIZED:
445 event_name = "VNC_INITIALIZED";
446 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200447 case QEVENT_VNC_DISCONNECTED:
448 event_name = "VNC_DISCONNECTED";
449 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200450 case QEVENT_BLOCK_IO_ERROR:
451 event_name = "BLOCK_IO_ERROR";
452 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300453 case QEVENT_RTC_CHANGE:
454 event_name = "RTC_CHANGE";
455 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300456 case QEVENT_WATCHDOG:
457 event_name = "WATCHDOG";
458 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200459 default:
460 abort();
461 break;
462 }
463
464 qmp = qdict_new();
465 timestamp_put(qmp);
466 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200467 if (data) {
468 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200469 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200470 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200471
Adam Litkef039a562010-01-15 08:34:02 -0600472 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200473 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200474 monitor_json_emitter(mon, QOBJECT(qmp));
475 }
Adam Litkef039a562010-01-15 08:34:02 -0600476 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200477 QDECREF(qmp);
478}
479
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200480static int do_qmp_capabilities(Monitor *mon, const QDict *params,
481 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200482{
483 /* Will setup QMP capabilities in the future */
484 if (monitor_ctrl_mode(mon)) {
485 mon->mc->command_mode = 1;
486 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200487
488 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200489}
490
bellard9dc39cb2004-03-14 21:38:27 +0000491static int compare_cmd(const char *name, const char *list)
492{
493 const char *p, *pstart;
494 int len;
495 len = strlen(name);
496 p = list;
497 for(;;) {
498 pstart = p;
499 p = strchr(p, '|');
500 if (!p)
501 p = pstart + strlen(pstart);
502 if ((p - pstart) == len && !memcmp(pstart, name, len))
503 return 1;
504 if (*p == '\0')
505 break;
506 p++;
507 }
508 return 0;
509}
510
Anthony Liguoric227f092009-10-01 16:12:16 -0500511static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000512 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000513{
Anthony Liguoric227f092009-10-01 16:12:16 -0500514 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000515
516 for(cmd = cmds; cmd->name != NULL; cmd++) {
517 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
519 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000520 }
521}
522
aliguori376253e2009-03-05 23:01:23 +0000523static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000524{
525 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000526 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000527 } else {
aliguori376253e2009-03-05 23:01:23 +0000528 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000529 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000530 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000531 monitor_printf(mon, "Log items (comma separated):\n");
532 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000533 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000534 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000535 }
536 }
bellard9dc39cb2004-03-14 21:38:27 +0000537 }
538}
539
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300540static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300541{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300542 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300543}
544
Prerna Saxena22890ab2010-06-24 17:04:53 +0530545#ifdef CONFIG_SIMPLE_TRACE
546static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
547{
548 const char *tp_name = qdict_get_str(qdict, "name");
549 bool new_state = qdict_get_bool(qdict, "option");
550 st_change_trace_event_state(tp_name, new_state);
551}
552#endif
553
Adam Litke940cc302010-01-25 12:18:44 -0600554static void user_monitor_complete(void *opaque, QObject *ret_data)
555{
556 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
557
558 if (ret_data) {
559 data->user_print(data->mon, ret_data);
560 }
561 monitor_resume(data->mon);
562 qemu_free(data);
563}
564
565static void qmp_monitor_complete(void *opaque, QObject *ret_data)
566{
567 monitor_protocol_emitter(opaque, ret_data);
568}
569
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300570static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
571 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600572{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300573 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600574}
575
576static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
577{
578 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
579}
580
581static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
582 const QDict *params)
583{
584 int ret;
585
586 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
587 cb_data->mon = mon;
588 cb_data->user_print = cmd->user_print;
589 monitor_suspend(mon);
590 ret = cmd->mhandler.cmd_async(mon, params,
591 user_monitor_complete, cb_data);
592 if (ret < 0) {
593 monitor_resume(mon);
594 qemu_free(cb_data);
595 }
596}
597
598static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
599{
600 int ret;
601
602 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
603 cb_data->mon = mon;
604 cb_data->user_print = cmd->user_print;
605 monitor_suspend(mon);
606 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
607 if (ret < 0) {
608 monitor_resume(mon);
609 qemu_free(cb_data);
610 }
611}
612
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200613static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000614{
Anthony Liguoric227f092009-10-01 16:12:16 -0500615 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300616 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000617
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200618 if (!item) {
619 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000620 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200621 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300622
623 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000624 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300625 break;
bellard9dc39cb2004-03-14 21:38:27 +0000626 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300627
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200628 if (cmd->name == NULL) {
629 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100630 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200631 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200632 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300633 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200634 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300635
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200636 if (monitor_ctrl_mode(mon) && monitor_cmd_user_only(cmd)) {
637 qerror_report(QERR_COMMAND_NOT_FOUND, item);
638 return -1;
639 }
640
Adam Litke940cc302010-01-25 12:18:44 -0600641 if (monitor_handler_is_async(cmd)) {
642 if (monitor_ctrl_mode(mon)) {
643 qmp_async_info_handler(mon, cmd);
644 } else {
645 user_async_info_handler(mon, cmd);
646 }
647 /*
648 * Indicate that this command is asynchronous and will not return any
649 * data (not even empty). Instead, the data will be returned via a
650 * completion callback.
651 */
652 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
653 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300654 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200655
656 if (!monitor_ctrl_mode(mon)) {
657 /*
658 * User Protocol function is called here, Monitor Protocol is
659 * handled by monitor_call_handler()
660 */
661 if (*ret_data)
662 cmd->user_print(mon, *ret_data);
663 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300664 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200665 if (monitor_ctrl_mode(mon)) {
666 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100667 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200668 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200669 } else {
670 cmd->mhandler.info(mon);
671 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300672 }
673
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200674 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300675
676help:
677 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200678 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000679}
680
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200681static void do_info_version_print(Monitor *mon, const QObject *data)
682{
683 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300684 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200685
686 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300687 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200688
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300689 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
690 qdict_get_int(qemu, "major"),
691 qdict_get_int(qemu, "minor"),
692 qdict_get_int(qemu, "micro"),
693 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200694}
695
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300696static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000697{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300698 const char *version = QEMU_VERSION;
699 int major = 0, minor = 0, micro = 0;
700 char *tmp;
701
702 major = strtol(version, &tmp, 10);
703 tmp++;
704 minor = strtol(tmp, &tmp, 10);
705 tmp++;
706 micro = strtol(tmp, &tmp, 10);
707
708 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
709 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000710}
711
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200712static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000713{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200714 QDict *qdict;
715
716 qdict = qobject_to_qdict(data);
717 if (qdict_size(qdict) == 0) {
718 return;
719 }
720
721 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
722}
723
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200724static void do_info_name(Monitor *mon, QObject **ret_data)
725{
726 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
727 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000728}
729
Luiz Capitulino1a728672009-12-10 17:15:56 -0200730static QObject *get_cmd_dict(const char *name)
731{
732 const char *p;
733
734 /* Remove '|' from some commands */
735 p = strchr(name, '|');
736 if (p) {
737 p++;
738 } else {
739 p = name;
740 }
741
742 return qobject_from_jsonf("{ 'name': %s }", p);
743}
744
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200745static void do_info_commands(Monitor *mon, QObject **ret_data)
746{
747 QList *cmd_list;
748 const mon_cmd_t *cmd;
749
750 cmd_list = qlist_new();
751
752 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200753 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
754 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200755 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200756 }
757 }
758
759 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200760 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200761 char buf[128];
762 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200763 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200764 }
765 }
766
767 *ret_data = QOBJECT(cmd_list);
768}
769
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200770static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000771{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200772 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
773}
774
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200775static void do_info_uuid(Monitor *mon, QObject **ret_data)
776{
777 char uuid[64];
778
779 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000780 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
781 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
782 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
783 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200784 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000785}
786
bellard6a00d602005-11-21 23:25:50 +0000787/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000788static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000789{
790 CPUState *env;
791
792 for(env = first_cpu; env != NULL; env = env->next_cpu) {
793 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000794 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000795 return 0;
796 }
797 }
798 return -1;
799}
800
pbrook9596ebb2007-11-18 01:44:38 +0000801static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000802{
aliguori731b0362009-03-05 23:01:42 +0000803 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000804 mon_set_cpu(0);
805 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300806 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000807 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000808}
809
aliguori376253e2009-03-05 23:01:23 +0000810static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000811{
bellard6a00d602005-11-21 23:25:50 +0000812 CPUState *env;
813 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000814#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000815 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000816 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000817#else
aliguori376253e2009-03-05 23:01:23 +0000818 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000819 0);
bellard9307c4c2004-04-04 12:57:25 +0000820#endif
821}
822
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300823static void print_cpu_iter(QObject *obj, void *opaque)
824{
825 QDict *cpu;
826 int active = ' ';
827 Monitor *mon = opaque;
828
829 assert(qobject_type(obj) == QTYPE_QDICT);
830 cpu = qobject_to_qdict(obj);
831
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200832 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300833 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200834 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300835
836 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
837
838#if defined(TARGET_I386)
839 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
840 (target_ulong) qdict_get_int(cpu, "pc"));
841#elif defined(TARGET_PPC)
842 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
843 (target_long) qdict_get_int(cpu, "nip"));
844#elif defined(TARGET_SPARC)
845 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
846 (target_long) qdict_get_int(cpu, "pc"));
847 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
848 (target_long) qdict_get_int(cpu, "npc"));
849#elif defined(TARGET_MIPS)
850 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
851 (target_long) qdict_get_int(cpu, "PC"));
852#endif
853
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200854 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300855 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200856 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300857
858 monitor_printf(mon, "\n");
859}
860
861static void monitor_print_cpus(Monitor *mon, const QObject *data)
862{
863 QList *cpu_list;
864
865 assert(qobject_type(data) == QTYPE_QLIST);
866 cpu_list = qobject_to_qlist(data);
867 qlist_iter(cpu_list, print_cpu_iter, mon);
868}
869
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300870static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000871{
872 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300873 QList *cpu_list;
874
875 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000876
877 /* just to set the default cpu if not already done */
878 mon_get_cpu();
879
880 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200881 QDict *cpu;
882 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300883
Avi Kivity4c0960c2009-08-17 23:19:53 +0300884 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300885
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200886 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
887 env->cpu_index, env == mon->mon_cpu,
888 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200889
890 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300891
bellard6a00d602005-11-21 23:25:50 +0000892#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300893 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000894#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300895 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000896#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300897 qdict_put(cpu, "pc", qint_from_int(env->pc));
898 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000899#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300900 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000901#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300902
903 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000904 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300905
906 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000907}
908
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200909static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000910{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300911 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200912 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100913 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
914 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200915 return -1;
916 }
917 return 0;
bellard6a00d602005-11-21 23:25:50 +0000918}
919
aliguori376253e2009-03-05 23:01:23 +0000920static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000921{
aliguori376253e2009-03-05 23:01:23 +0000922 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000923}
924
aliguori376253e2009-03-05 23:01:23 +0000925static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000926{
927 int i;
bellard7e2515e2004-08-01 21:52:19 +0000928 const char *str;
ths3b46e622007-09-17 08:09:54 +0000929
aliguoricde76ee2009-03-05 23:01:51 +0000930 if (!mon->rs)
931 return;
bellard7e2515e2004-08-01 21:52:19 +0000932 i = 0;
933 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000934 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000935 if (!str)
936 break;
aliguori376253e2009-03-05 23:01:23 +0000937 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000938 i++;
bellardaa455482004-04-04 13:07:25 +0000939 }
940}
941
j_mayer76a66252007-03-07 08:32:30 +0000942#if defined(TARGET_PPC)
943/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000944static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000945{
946 CPUState *env;
947
948 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000949 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000950}
951#endif
952
Prerna Saxena22890ab2010-06-24 17:04:53 +0530953#if defined(CONFIG_SIMPLE_TRACE)
954static void do_info_trace(Monitor *mon)
955{
956 st_print_trace((FILE *)mon, &monitor_fprintf);
957}
958
959static void do_info_trace_events(Monitor *mon)
960{
961 st_print_trace_events((FILE *)mon, &monitor_fprintf);
962}
963#endif
964
Luiz Capitulinob223f352009-10-07 13:41:56 -0300965/**
966 * do_quit(): Quit QEMU execution
967 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200968static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000969{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300970 monitor_suspend(mon);
971 no_shutdown = 0;
972 qemu_system_shutdown_request();
973
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200974 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000975}
976
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200977static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000978{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200979 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100980 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200981 return -1;
982 }
aliguoribb5fc202009-03-05 23:01:15 +0000983
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200984 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100985}
986
987static void change_vnc_password_cb(Monitor *mon, const char *password,
988 void *opaque)
989{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100990 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000991 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000992}
993
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200994static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000995{
ths70848512007-08-25 01:37:05 +0000996 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000997 strcmp(target, "password") == 0) {
998 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000999 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001000 strncpy(password, arg, sizeof(password));
1001 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001002 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001003 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001004 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001005 }
ths70848512007-08-25 01:37:05 +00001006 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001007 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001008 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001009 return -1;
1010 }
ths70848512007-08-25 01:37:05 +00001011 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001012
1013 return 0;
thse25a5822007-08-25 01:36:20 +00001014}
1015
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001016/**
1017 * do_change(): Change a removable medium, or VNC configuration
1018 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001019static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001020{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001021 const char *device = qdict_get_str(qdict, "device");
1022 const char *target = qdict_get_str(qdict, "target");
1023 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001024 int ret;
1025
thse25a5822007-08-25 01:36:20 +00001026 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001028 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001029 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001030 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001031
1032 return ret;
thse25a5822007-08-25 01:36:20 +00001033}
1034
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001035static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001036{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001037 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001038 return 0;
bellard59a983b2004-03-17 23:17:16 +00001039}
1040
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001041static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001042{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001043 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001044}
1045
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001046static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001047{
1048 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001049 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001050
bellard9307c4c2004-04-04 12:57:25 +00001051 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001052 mask = 0;
1053 } else {
bellard9307c4c2004-04-04 12:57:25 +00001054 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001055 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001056 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001057 return;
1058 }
1059 }
1060 cpu_set_log(mask);
1061}
1062
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001063static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001064{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001065 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001066 if (!option || !strcmp(option, "on")) {
1067 singlestep = 1;
1068 } else if (!strcmp(option, "off")) {
1069 singlestep = 0;
1070 } else {
1071 monitor_printf(mon, "unexpected option %s\n", option);
1072 }
1073}
1074
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001075/**
1076 * do_stop(): Stop VM execution
1077 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001078static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001079{
1080 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001081 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001082}
1083
aliguoribb5fc202009-03-05 23:01:15 +00001084static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001085
aliguori376253e2009-03-05 23:01:23 +00001086struct bdrv_iterate_context {
1087 Monitor *mon;
1088 int err;
1089};
aliguoric0f4ce72009-03-05 23:01:01 +00001090
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001091/**
1092 * do_cont(): Resume emulation.
1093 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001094static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001095{
1096 struct bdrv_iterate_context context = { mon, 0 };
1097
Amit Shah8e848652010-07-27 15:49:19 +05301098 if (incoming_expected) {
1099 qerror_report(QERR_MIGRATION_EXPECTED);
1100 return -1;
1101 }
aliguori376253e2009-03-05 23:01:23 +00001102 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001103 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001104 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001105 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001106 return 0;
1107 } else {
1108 return -1;
1109 }
bellard8a7ddc32004-03-31 19:00:16 +00001110}
1111
aliguoribb5fc202009-03-05 23:01:15 +00001112static void bdrv_key_cb(void *opaque, int err)
1113{
aliguori376253e2009-03-05 23:01:23 +00001114 Monitor *mon = opaque;
1115
aliguoribb5fc202009-03-05 23:01:15 +00001116 /* another key was set successfully, retry to continue */
1117 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001118 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001119}
1120
1121static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1122{
aliguori376253e2009-03-05 23:01:23 +00001123 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001124
aliguori376253e2009-03-05 23:01:23 +00001125 if (!context->err && bdrv_key_required(bs)) {
1126 context->err = -EBUSY;
1127 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1128 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001129 }
1130}
1131
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001132static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001133{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001134 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001135 if (!device)
1136 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1137 if (gdbserver_start(device) < 0) {
1138 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1139 device);
1140 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001141 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001142 } else {
aliguori59030a82009-04-05 18:43:41 +00001143 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1144 device);
bellard8a7ddc32004-03-31 19:00:16 +00001145 }
1146}
1147
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001148static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001149{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001150 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001151 if (select_watchdog_action(action) == -1) {
1152 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1153 }
1154}
1155
aliguori376253e2009-03-05 23:01:23 +00001156static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001157{
aliguori376253e2009-03-05 23:01:23 +00001158 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001159 switch(c) {
1160 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001161 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001162 break;
1163 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001164 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001165 break;
1166 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001167 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001168 break;
1169 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001171 break;
1172 default:
1173 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001174 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001175 } else {
aliguori376253e2009-03-05 23:01:23 +00001176 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001177 }
1178 break;
1179 }
aliguori376253e2009-03-05 23:01:23 +00001180 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001181}
1182
aliguori376253e2009-03-05 23:01:23 +00001183static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001184 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001185{
bellard6a00d602005-11-21 23:25:50 +00001186 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001187 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001188 uint8_t buf[16];
1189 uint64_t v;
1190
1191 if (format == 'i') {
1192 int flags;
1193 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001194 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001195#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001196 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001197 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001198 } else if (wsize == 4) {
1199 flags = 0;
1200 } else {
bellard6a15fd12006-04-12 21:07:07 +00001201 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001202 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001203 if (env) {
1204#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001205 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001206 (env->segs[R_CS].flags & DESC_L_MASK))
1207 flags = 2;
1208 else
1209#endif
1210 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1211 flags = 1;
1212 }
bellard4c27ba22004-04-25 18:05:08 +00001213 }
1214#endif
aliguori376253e2009-03-05 23:01:23 +00001215 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001216 return;
1217 }
1218
1219 len = wsize * count;
1220 if (wsize == 1)
1221 line_size = 8;
1222 else
1223 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001224 max_digits = 0;
1225
1226 switch(format) {
1227 case 'o':
1228 max_digits = (wsize * 8 + 2) / 3;
1229 break;
1230 default:
1231 case 'x':
1232 max_digits = (wsize * 8) / 4;
1233 break;
1234 case 'u':
1235 case 'd':
1236 max_digits = (wsize * 8 * 10 + 32) / 33;
1237 break;
1238 case 'c':
1239 wsize = 1;
1240 break;
1241 }
1242
1243 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001244 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001246 else
aliguori376253e2009-03-05 23:01:23 +00001247 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001248 l = len;
1249 if (l > line_size)
1250 l = line_size;
1251 if (is_physical) {
1252 cpu_physical_memory_rw(addr, buf, l, 0);
1253 } else {
bellard6a00d602005-11-21 23:25:50 +00001254 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001255 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001257 break;
1258 }
bellard9307c4c2004-04-04 12:57:25 +00001259 }
ths5fafdf22007-09-16 21:08:06 +00001260 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001261 while (i < l) {
1262 switch(wsize) {
1263 default:
1264 case 1:
1265 v = ldub_raw(buf + i);
1266 break;
1267 case 2:
1268 v = lduw_raw(buf + i);
1269 break;
1270 case 4:
bellard92a31b12005-02-10 22:00:52 +00001271 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001272 break;
1273 case 8:
1274 v = ldq_raw(buf + i);
1275 break;
1276 }
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001278 switch(format) {
1279 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001281 break;
1282 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001283 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001286 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001290 break;
1291 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001292 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 }
1295 i += wsize;
1296 }
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001298 addr += l;
1299 len -= l;
1300 }
1301}
1302
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001303static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001304{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001305 int count = qdict_get_int(qdict, "count");
1306 int format = qdict_get_int(qdict, "format");
1307 int size = qdict_get_int(qdict, "size");
1308 target_long addr = qdict_get_int(qdict, "addr");
1309
aliguori376253e2009-03-05 23:01:23 +00001310 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001311}
1312
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001313static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001314{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001315 int count = qdict_get_int(qdict, "count");
1316 int format = qdict_get_int(qdict, "format");
1317 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001318 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001319
aliguori376253e2009-03-05 23:01:23 +00001320 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001321}
1322
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001323static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001324{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001325 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001326 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001327
blueswir17743e582007-09-24 18:39:04 +00001328#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001329 switch(format) {
1330 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001331 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001332 break;
1333 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001335 break;
1336 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 default:
1340 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001341 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001342 break;
1343 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 }
bellard92a31b12005-02-10 22:00:52 +00001347#else
1348 switch(format) {
1349 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001350 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001351 break;
1352 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001354 break;
1355 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001357 break;
1358 default:
1359 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001361 break;
1362 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001363 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001364 break;
1365 }
1366#endif
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001368}
1369
Luiz Capitulino98696222010-02-10 23:49:58 -02001370static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001371{
1372 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001373 uint32_t size = qdict_get_int(qdict, "size");
1374 const char *filename = qdict_get_str(qdict, "filename");
1375 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001376 uint32_t l;
1377 CPUState *env;
1378 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001379 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001380
1381 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001382
1383 f = fopen(filename, "wb");
1384 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001385 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001386 return -1;
bellardb371dc52007-01-03 15:20:39 +00001387 }
1388 while (size != 0) {
1389 l = sizeof(buf);
1390 if (l > size)
1391 l = size;
1392 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001393 if (fwrite(buf, 1, l, f) != l) {
1394 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1395 goto exit;
1396 }
bellardb371dc52007-01-03 15:20:39 +00001397 addr += l;
1398 size -= l;
1399 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001400
1401 ret = 0;
1402
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001403exit:
bellardb371dc52007-01-03 15:20:39 +00001404 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001405 return ret;
bellardb371dc52007-01-03 15:20:39 +00001406}
1407
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001408static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001409 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001410{
1411 FILE *f;
1412 uint32_t l;
1413 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001414 uint32_t size = qdict_get_int(qdict, "size");
1415 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001416 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001417 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001418
1419 f = fopen(filename, "wb");
1420 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001421 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001422 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001423 }
1424 while (size != 0) {
1425 l = sizeof(buf);
1426 if (l > size)
1427 l = size;
1428 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001429 if (fwrite(buf, 1, l, f) != l) {
1430 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1431 goto exit;
1432 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001433 fflush(f);
1434 addr += l;
1435 size -= l;
1436 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001437
1438 ret = 0;
1439
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001440exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001441 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001442 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001443}
1444
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001445static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001446{
1447 uint32_t addr;
1448 uint8_t buf[1];
1449 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001450 uint32_t start = qdict_get_int(qdict, "start");
1451 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001452
1453 sum = 0;
1454 for(addr = start; addr < (start + size); addr++) {
1455 cpu_physical_memory_rw(addr, buf, 1, 0);
1456 /* BSD sum algorithm ('sum' Unix command) */
1457 sum = (sum >> 1) | (sum << 15);
1458 sum += buf[0];
1459 }
aliguori376253e2009-03-05 23:01:23 +00001460 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001461}
1462
bellarda3a91a32004-06-04 11:06:21 +00001463typedef struct {
1464 int keycode;
1465 const char *name;
1466} KeyDef;
1467
1468static const KeyDef key_defs[] = {
1469 { 0x2a, "shift" },
1470 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001471
bellarda3a91a32004-06-04 11:06:21 +00001472 { 0x38, "alt" },
1473 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001474 { 0x64, "altgr" },
1475 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001476 { 0x1d, "ctrl" },
1477 { 0x9d, "ctrl_r" },
1478
1479 { 0xdd, "menu" },
1480
1481 { 0x01, "esc" },
1482
1483 { 0x02, "1" },
1484 { 0x03, "2" },
1485 { 0x04, "3" },
1486 { 0x05, "4" },
1487 { 0x06, "5" },
1488 { 0x07, "6" },
1489 { 0x08, "7" },
1490 { 0x09, "8" },
1491 { 0x0a, "9" },
1492 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001493 { 0x0c, "minus" },
1494 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001495 { 0x0e, "backspace" },
1496
1497 { 0x0f, "tab" },
1498 { 0x10, "q" },
1499 { 0x11, "w" },
1500 { 0x12, "e" },
1501 { 0x13, "r" },
1502 { 0x14, "t" },
1503 { 0x15, "y" },
1504 { 0x16, "u" },
1505 { 0x17, "i" },
1506 { 0x18, "o" },
1507 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001508 { 0x1a, "bracket_left" },
1509 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001510 { 0x1c, "ret" },
1511
1512 { 0x1e, "a" },
1513 { 0x1f, "s" },
1514 { 0x20, "d" },
1515 { 0x21, "f" },
1516 { 0x22, "g" },
1517 { 0x23, "h" },
1518 { 0x24, "j" },
1519 { 0x25, "k" },
1520 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001521 { 0x27, "semicolon" },
1522 { 0x28, "apostrophe" },
1523 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001524
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001525 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001526 { 0x2c, "z" },
1527 { 0x2d, "x" },
1528 { 0x2e, "c" },
1529 { 0x2f, "v" },
1530 { 0x30, "b" },
1531 { 0x31, "n" },
1532 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001533 { 0x33, "comma" },
1534 { 0x34, "dot" },
1535 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001536
balrog4d3b6f62008-02-10 16:33:14 +00001537 { 0x37, "asterisk" },
1538
bellarda3a91a32004-06-04 11:06:21 +00001539 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001540 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001541 { 0x3b, "f1" },
1542 { 0x3c, "f2" },
1543 { 0x3d, "f3" },
1544 { 0x3e, "f4" },
1545 { 0x3f, "f5" },
1546 { 0x40, "f6" },
1547 { 0x41, "f7" },
1548 { 0x42, "f8" },
1549 { 0x43, "f9" },
1550 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001551 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001552 { 0x46, "scroll_lock" },
1553
bellard64866c32006-05-07 18:03:31 +00001554 { 0xb5, "kp_divide" },
1555 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001556 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001557 { 0x4e, "kp_add" },
1558 { 0x9c, "kp_enter" },
1559 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001560 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001561
1562 { 0x52, "kp_0" },
1563 { 0x4f, "kp_1" },
1564 { 0x50, "kp_2" },
1565 { 0x51, "kp_3" },
1566 { 0x4b, "kp_4" },
1567 { 0x4c, "kp_5" },
1568 { 0x4d, "kp_6" },
1569 { 0x47, "kp_7" },
1570 { 0x48, "kp_8" },
1571 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001572
bellarda3a91a32004-06-04 11:06:21 +00001573 { 0x56, "<" },
1574
1575 { 0x57, "f11" },
1576 { 0x58, "f12" },
1577
1578 { 0xb7, "print" },
1579
1580 { 0xc7, "home" },
1581 { 0xc9, "pgup" },
1582 { 0xd1, "pgdn" },
1583 { 0xcf, "end" },
1584
1585 { 0xcb, "left" },
1586 { 0xc8, "up" },
1587 { 0xd0, "down" },
1588 { 0xcd, "right" },
1589
1590 { 0xd2, "insert" },
1591 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001592#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1593 { 0xf0, "stop" },
1594 { 0xf1, "again" },
1595 { 0xf2, "props" },
1596 { 0xf3, "undo" },
1597 { 0xf4, "front" },
1598 { 0xf5, "copy" },
1599 { 0xf6, "open" },
1600 { 0xf7, "paste" },
1601 { 0xf8, "find" },
1602 { 0xf9, "cut" },
1603 { 0xfa, "lf" },
1604 { 0xfb, "help" },
1605 { 0xfc, "meta_l" },
1606 { 0xfd, "meta_r" },
1607 { 0xfe, "compose" },
1608#endif
bellarda3a91a32004-06-04 11:06:21 +00001609 { 0, NULL },
1610};
1611
1612static int get_keycode(const char *key)
1613{
1614 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001615 char *endp;
1616 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001617
1618 for(p = key_defs; p->name != NULL; p++) {
1619 if (!strcmp(key, p->name))
1620 return p->keycode;
1621 }
bellard64866c32006-05-07 18:03:31 +00001622 if (strstart(key, "0x", NULL)) {
1623 ret = strtoul(key, &endp, 0);
1624 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1625 return ret;
1626 }
bellarda3a91a32004-06-04 11:06:21 +00001627 return -1;
1628}
1629
balrogc8256f92008-06-08 22:45:01 +00001630#define MAX_KEYCODES 16
1631static uint8_t keycodes[MAX_KEYCODES];
1632static int nb_pending_keycodes;
1633static QEMUTimer *key_timer;
1634
1635static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001636{
balrogc8256f92008-06-08 22:45:01 +00001637 int keycode;
1638
1639 while (nb_pending_keycodes > 0) {
1640 nb_pending_keycodes--;
1641 keycode = keycodes[nb_pending_keycodes];
1642 if (keycode & 0x80)
1643 kbd_put_keycode(0xe0);
1644 kbd_put_keycode(keycode | 0x80);
1645 }
1646}
1647
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001648static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001649{
balrog3401c0d2008-06-04 10:05:59 +00001650 char keyname_buf[16];
1651 char *separator;
1652 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001653 const char *string = qdict_get_str(qdict, "string");
1654 int has_hold_time = qdict_haskey(qdict, "hold_time");
1655 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001656
balrogc8256f92008-06-08 22:45:01 +00001657 if (nb_pending_keycodes > 0) {
1658 qemu_del_timer(key_timer);
1659 release_keys(NULL);
1660 }
1661 if (!has_hold_time)
1662 hold_time = 100;
1663 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001664 while (1) {
1665 separator = strchr(string, '-');
1666 keyname_len = separator ? separator - string : strlen(string);
1667 if (keyname_len > 0) {
1668 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1669 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001670 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001671 return;
bellarda3a91a32004-06-04 11:06:21 +00001672 }
balrogc8256f92008-06-08 22:45:01 +00001673 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001674 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001675 return;
1676 }
1677 keyname_buf[keyname_len] = 0;
1678 keycode = get_keycode(keyname_buf);
1679 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001680 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001681 return;
1682 }
balrogc8256f92008-06-08 22:45:01 +00001683 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001684 }
balrog3401c0d2008-06-04 10:05:59 +00001685 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001686 break;
balrog3401c0d2008-06-04 10:05:59 +00001687 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001688 }
balrogc8256f92008-06-08 22:45:01 +00001689 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001690 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001691 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001692 keycode = keycodes[i];
1693 if (keycode & 0x80)
1694 kbd_put_keycode(0xe0);
1695 kbd_put_keycode(keycode & 0x7f);
1696 }
balrogc8256f92008-06-08 22:45:01 +00001697 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001698 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001699 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001700}
1701
bellard13224a82006-07-14 22:03:35 +00001702static int mouse_button_state;
1703
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001704static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001705{
1706 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001707 const char *dx_str = qdict_get_str(qdict, "dx_str");
1708 const char *dy_str = qdict_get_str(qdict, "dy_str");
1709 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001710 dx = strtol(dx_str, NULL, 0);
1711 dy = strtol(dy_str, NULL, 0);
1712 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001713 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001714 dz = strtol(dz_str, NULL, 0);
1715 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1716}
1717
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001718static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001719{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001720 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001721 mouse_button_state = button_state;
1722 kbd_mouse_event(0, 0, 0, mouse_button_state);
1723}
1724
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001725static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001726{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001727 int size = qdict_get_int(qdict, "size");
1728 int addr = qdict_get_int(qdict, "addr");
1729 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001730 uint32_t val;
1731 int suffix;
1732
1733 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001734 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001735 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001736 addr++;
1737 }
1738 addr &= 0xffff;
1739
1740 switch(size) {
1741 default:
1742 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001743 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001744 suffix = 'b';
1745 break;
1746 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001747 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001748 suffix = 'w';
1749 break;
1750 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001751 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001752 suffix = 'l';
1753 break;
1754 }
aliguori376253e2009-03-05 23:01:23 +00001755 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1756 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001757}
bellarda3a91a32004-06-04 11:06:21 +00001758
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001759static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001760{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001761 int size = qdict_get_int(qdict, "size");
1762 int addr = qdict_get_int(qdict, "addr");
1763 int val = qdict_get_int(qdict, "val");
1764
Jan Kiszkaf1147842009-07-14 10:20:11 +02001765 addr &= IOPORTS_MASK;
1766
1767 switch (size) {
1768 default:
1769 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001770 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001771 break;
1772 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001773 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001774 break;
1775 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001776 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001777 break;
1778 }
1779}
1780
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001781static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001782{
1783 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001784 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001785
Jan Kiszka76e30d02009-07-02 00:19:02 +02001786 res = qemu_boot_set(bootdevice);
1787 if (res == 0) {
1788 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1789 } else if (res > 0) {
1790 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001791 } else {
aliguori376253e2009-03-05 23:01:23 +00001792 monitor_printf(mon, "no function defined to set boot device list for "
1793 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001794 }
1795}
1796
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001797/**
1798 * do_system_reset(): Issue a machine reset
1799 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001800static int do_system_reset(Monitor *mon, const QDict *qdict,
1801 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001802{
1803 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001804 return 0;
bellarde4f90822004-06-20 12:35:44 +00001805}
1806
Luiz Capitulino43076662009-10-07 13:41:59 -03001807/**
1808 * do_system_powerdown(): Issue a machine powerdown
1809 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001810static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1811 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001812{
1813 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001814 return 0;
bellard34751872005-07-02 14:31:34 +00001815}
1816
bellardb86bda52004-09-18 19:32:46 +00001817#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001818static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001819{
aliguori376253e2009-03-05 23:01:23 +00001820 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1821 addr,
1822 pte & mask,
1823 pte & PG_GLOBAL_MASK ? 'G' : '-',
1824 pte & PG_PSE_MASK ? 'P' : '-',
1825 pte & PG_DIRTY_MASK ? 'D' : '-',
1826 pte & PG_ACCESSED_MASK ? 'A' : '-',
1827 pte & PG_PCD_MASK ? 'C' : '-',
1828 pte & PG_PWT_MASK ? 'T' : '-',
1829 pte & PG_USER_MASK ? 'U' : '-',
1830 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001831}
1832
aliguori376253e2009-03-05 23:01:23 +00001833static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001834{
bellard6a00d602005-11-21 23:25:50 +00001835 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001836 int l1, l2;
1837 uint32_t pgd, pde, pte;
1838
bellard6a00d602005-11-21 23:25:50 +00001839 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001840
bellardb86bda52004-09-18 19:32:46 +00001841 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001842 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001843 return;
1844 }
1845 pgd = env->cr[3] & ~0xfff;
1846 for(l1 = 0; l1 < 1024; l1++) {
1847 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1848 pde = le32_to_cpu(pde);
1849 if (pde & PG_PRESENT_MASK) {
1850 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001851 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001852 } else {
1853 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001854 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001855 (uint8_t *)&pte, 4);
1856 pte = le32_to_cpu(pte);
1857 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001858 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001859 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001860 ~0xfff);
1861 }
1862 }
1863 }
1864 }
1865 }
1866}
1867
aliguori376253e2009-03-05 23:01:23 +00001868static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001869 uint32_t end, int prot)
1870{
bellard9746b152004-11-11 18:30:24 +00001871 int prot1;
1872 prot1 = *plast_prot;
1873 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001874 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001875 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1876 *pstart, end, end - *pstart,
1877 prot1 & PG_USER_MASK ? 'u' : '-',
1878 'r',
1879 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001880 }
1881 if (prot != 0)
1882 *pstart = end;
1883 else
1884 *pstart = -1;
1885 *plast_prot = prot;
1886 }
1887}
1888
aliguori376253e2009-03-05 23:01:23 +00001889static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001890{
bellard6a00d602005-11-21 23:25:50 +00001891 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001892 int l1, l2, prot, last_prot;
1893 uint32_t pgd, pde, pte, start, end;
1894
bellard6a00d602005-11-21 23:25:50 +00001895 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001896
bellardb86bda52004-09-18 19:32:46 +00001897 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001898 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001899 return;
1900 }
1901 pgd = env->cr[3] & ~0xfff;
1902 last_prot = 0;
1903 start = -1;
1904 for(l1 = 0; l1 < 1024; l1++) {
1905 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1906 pde = le32_to_cpu(pde);
1907 end = l1 << 22;
1908 if (pde & PG_PRESENT_MASK) {
1909 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1910 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001911 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001912 } else {
1913 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001914 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001915 (uint8_t *)&pte, 4);
1916 pte = le32_to_cpu(pte);
1917 end = (l1 << 22) + (l2 << 12);
1918 if (pte & PG_PRESENT_MASK) {
1919 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1920 } else {
1921 prot = 0;
1922 }
aliguori376253e2009-03-05 23:01:23 +00001923 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001924 }
1925 }
1926 } else {
1927 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001928 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001929 }
1930 }
1931}
1932#endif
1933
aurel327c664e22009-03-03 06:12:22 +00001934#if defined(TARGET_SH4)
1935
aliguori376253e2009-03-05 23:01:23 +00001936static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001937{
aliguori376253e2009-03-05 23:01:23 +00001938 monitor_printf(mon, " tlb%i:\t"
1939 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1940 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1941 "dirty=%hhu writethrough=%hhu\n",
1942 idx,
1943 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1944 tlb->v, tlb->sh, tlb->c, tlb->pr,
1945 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001946}
1947
aliguori376253e2009-03-05 23:01:23 +00001948static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001949{
1950 CPUState *env = mon_get_cpu();
1951 int i;
1952
aliguori376253e2009-03-05 23:01:23 +00001953 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001954 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001955 print_tlb (mon, i, &env->itlb[i]);
1956 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001957 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001958 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001959}
1960
1961#endif
1962
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001963static void do_info_kvm_print(Monitor *mon, const QObject *data)
1964{
1965 QDict *qdict;
1966
1967 qdict = qobject_to_qdict(data);
1968
1969 monitor_printf(mon, "kvm support: ");
1970 if (qdict_get_bool(qdict, "present")) {
1971 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1972 "enabled" : "disabled");
1973 } else {
1974 monitor_printf(mon, "not compiled\n");
1975 }
1976}
1977
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001978static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001979{
1980#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001981 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1982 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001983#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001984 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001985#endif
1986}
1987
aliguori030ea372009-04-21 22:30:47 +00001988static void do_info_numa(Monitor *mon)
1989{
aliguorib28b6232009-04-22 20:20:29 +00001990 int i;
aliguori030ea372009-04-21 22:30:47 +00001991 CPUState *env;
1992
1993 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1994 for (i = 0; i < nb_numa_nodes; i++) {
1995 monitor_printf(mon, "node %d cpus:", i);
1996 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1997 if (env->numa_node == i) {
1998 monitor_printf(mon, " %d", env->cpu_index);
1999 }
2000 }
2001 monitor_printf(mon, "\n");
2002 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2003 node_mem[i] >> 20);
2004 }
2005}
2006
bellard5f1ce942006-02-08 22:40:15 +00002007#ifdef CONFIG_PROFILER
2008
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002009int64_t qemu_time;
2010int64_t dev_time;
2011
aliguori376253e2009-03-05 23:01:23 +00002012static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002013{
2014 int64_t total;
2015 total = qemu_time;
2016 if (total == 0)
2017 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002018 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002019 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002020 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002021 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002022 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002023 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002024}
2025#else
aliguori376253e2009-03-05 23:01:23 +00002026static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002027{
aliguori376253e2009-03-05 23:01:23 +00002028 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002029}
2030#endif
2031
bellardec36b692006-07-16 18:57:03 +00002032/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002033static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002034
aliguori376253e2009-03-05 23:01:23 +00002035static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002036{
2037 int i;
2038 CaptureState *s;
2039
2040 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002041 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002042 s->ops.info (s->opaque);
2043 }
2044}
2045
Blue Swirl23130862009-06-06 08:22:04 +00002046#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002047static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002048{
2049 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002050 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002051 CaptureState *s;
2052
2053 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2054 if (i == n) {
2055 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002056 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002057 qemu_free (s);
2058 return;
2059 }
2060 }
2061}
2062
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002063static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002064{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002065 const char *path = qdict_get_str(qdict, "path");
2066 int has_freq = qdict_haskey(qdict, "freq");
2067 int freq = qdict_get_try_int(qdict, "freq", -1);
2068 int has_bits = qdict_haskey(qdict, "bits");
2069 int bits = qdict_get_try_int(qdict, "bits", -1);
2070 int has_channels = qdict_haskey(qdict, "nchannels");
2071 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002072 CaptureState *s;
2073
2074 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002075
2076 freq = has_freq ? freq : 44100;
2077 bits = has_bits ? bits : 16;
2078 nchannels = has_channels ? nchannels : 2;
2079
2080 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002081 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002082 qemu_free (s);
2083 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002084 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002085}
2086#endif
2087
aurel32dc1c0b72008-04-27 23:52:12 +00002088#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002089static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002090{
2091 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002092 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002093
2094 for (env = first_cpu; env != NULL; env = env->next_cpu)
2095 if (env->cpu_index == cpu_index) {
2096 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2097 break;
2098 }
2099}
2100#endif
2101
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002102static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002103{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002104 QDict *qdict;
2105
2106 qdict = qobject_to_qdict(data);
2107
2108 monitor_printf(mon, "VM status: ");
2109 if (qdict_get_bool(qdict, "running")) {
2110 monitor_printf(mon, "running");
2111 if (qdict_get_bool(qdict, "singlestep")) {
2112 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002113 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002114 } else {
2115 monitor_printf(mon, "paused");
2116 }
2117
2118 monitor_printf(mon, "\n");
2119}
2120
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002121static void do_info_status(Monitor *mon, QObject **ret_data)
2122{
2123 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2124 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002125}
2126
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002127static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002128{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002129 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002130
aliguori76655d62009-03-06 20:27:37 +00002131 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002132 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002133 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002134 return acl;
2135}
aliguori76655d62009-03-06 20:27:37 +00002136
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002137static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002138{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002139 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002140 qemu_acl *acl = find_acl(mon, aclname);
2141 qemu_acl_entry *entry;
2142 int i = 0;
2143
2144 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002145 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002146 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002147 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002148 i++;
2149 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002150 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002151 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 }
2153}
2154
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002155static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002157 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002158 qemu_acl *acl = find_acl(mon, aclname);
2159
2160 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002161 qemu_acl_reset(acl);
2162 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002163 }
2164}
aliguori76655d62009-03-06 20:27:37 +00002165
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002166static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002167{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002168 const char *aclname = qdict_get_str(qdict, "aclname");
2169 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170 qemu_acl *acl = find_acl(mon, aclname);
2171
2172 if (acl) {
2173 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002174 acl->defaultDeny = 0;
2175 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002176 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002177 acl->defaultDeny = 1;
2178 monitor_printf(mon, "acl: policy set to 'deny'\n");
2179 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180 monitor_printf(mon, "acl: unknown policy '%s', "
2181 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002182 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183 }
2184}
aliguori76655d62009-03-06 20:27:37 +00002185
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002186static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002187{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002188 const char *aclname = qdict_get_str(qdict, "aclname");
2189 const char *match = qdict_get_str(qdict, "match");
2190 const char *policy = qdict_get_str(qdict, "policy");
2191 int has_index = qdict_haskey(qdict, "index");
2192 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002193 qemu_acl *acl = find_acl(mon, aclname);
2194 int deny, ret;
2195
2196 if (acl) {
2197 if (strcmp(policy, "allow") == 0) {
2198 deny = 0;
2199 } else if (strcmp(policy, "deny") == 0) {
2200 deny = 1;
2201 } else {
2202 monitor_printf(mon, "acl: unknown policy '%s', "
2203 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002204 return;
2205 }
aliguori28a76be2009-03-06 20:27:40 +00002206 if (has_index)
2207 ret = qemu_acl_insert(acl, deny, match, index);
2208 else
2209 ret = qemu_acl_append(acl, deny, match);
2210 if (ret < 0)
2211 monitor_printf(mon, "acl: unable to add acl entry\n");
2212 else
2213 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002214 }
2215}
aliguori76655d62009-03-06 20:27:37 +00002216
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002217static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002218{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002219 const char *aclname = qdict_get_str(qdict, "aclname");
2220 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002221 qemu_acl *acl = find_acl(mon, aclname);
2222 int ret;
aliguori76655d62009-03-06 20:27:37 +00002223
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002224 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002225 ret = qemu_acl_remove(acl, match);
2226 if (ret < 0)
2227 monitor_printf(mon, "acl: no matching acl entry\n");
2228 else
2229 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002230 }
2231}
2232
Huang Ying79c4f6b2009-06-23 10:05:14 +08002233#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002234static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002235{
2236 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002237 int cpu_index = qdict_get_int(qdict, "cpu_index");
2238 int bank = qdict_get_int(qdict, "bank");
2239 uint64_t status = qdict_get_int(qdict, "status");
2240 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2241 uint64_t addr = qdict_get_int(qdict, "addr");
2242 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002243
2244 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2245 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2246 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2247 break;
2248 }
2249}
2250#endif
2251
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002252static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002254 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002255 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002256 int fd;
2257
2258 fd = qemu_chr_get_msgfd(mon->chr);
2259 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002260 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002261 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002262 }
2263
2264 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002265 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2266 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002267 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002268 }
2269
Blue Swirl72cf2d42009-09-12 07:36:22 +00002270 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002271 if (strcmp(monfd->name, fdname) != 0) {
2272 continue;
2273 }
2274
2275 close(monfd->fd);
2276 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002277 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002278 }
2279
Anthony Liguoric227f092009-10-01 16:12:16 -05002280 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002281 monfd->name = qemu_strdup(fdname);
2282 monfd->fd = fd;
2283
Blue Swirl72cf2d42009-09-12 07:36:22 +00002284 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002285 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002286}
2287
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002288static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002289{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002290 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002291 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002292
Blue Swirl72cf2d42009-09-12 07:36:22 +00002293 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002294 if (strcmp(monfd->name, fdname) != 0) {
2295 continue;
2296 }
2297
Blue Swirl72cf2d42009-09-12 07:36:22 +00002298 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002299 close(monfd->fd);
2300 qemu_free(monfd->name);
2301 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002302 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002303 }
2304
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002305 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002306 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002307}
2308
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002309static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002310{
2311 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002312 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002313
2314 vm_stop(0);
2315
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002316 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002317 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002318 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002319}
2320
Mark McLoughlin7768e042009-07-22 09:11:41 +01002321int monitor_get_fd(Monitor *mon, const char *fdname)
2322{
Anthony Liguoric227f092009-10-01 16:12:16 -05002323 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002324
Blue Swirl72cf2d42009-09-12 07:36:22 +00002325 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002326 int fd;
2327
2328 if (strcmp(monfd->name, fdname) != 0) {
2329 continue;
2330 }
2331
2332 fd = monfd->fd;
2333
2334 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002335 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002336 qemu_free(monfd->name);
2337 qemu_free(monfd);
2338
2339 return fd;
2340 }
2341
2342 return -1;
2343}
2344
Anthony Liguoric227f092009-10-01 16:12:16 -05002345static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002346#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002347 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002348};
2349
Blue Swirl23130862009-06-06 08:22:04 +00002350/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002351static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002352 {
2353 .name = "version",
2354 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002355 .params = "",
2356 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002357 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002358 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002359 },
2360 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002361 .name = "commands",
2362 .args_type = "",
2363 .params = "",
2364 .help = "list QMP available commands",
2365 .user_print = monitor_user_noop,
2366 .mhandler.info_new = do_info_commands,
2367 },
2368 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002369 .name = "network",
2370 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002371 .params = "",
2372 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002373 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002374 },
2375 {
2376 .name = "chardev",
2377 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 .params = "",
2379 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002380 .user_print = qemu_chr_info_print,
2381 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002382 },
2383 {
2384 .name = "block",
2385 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002386 .params = "",
2387 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002388 .user_print = bdrv_info_print,
2389 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002390 },
2391 {
2392 .name = "blockstats",
2393 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002394 .params = "",
2395 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002396 .user_print = bdrv_stats_print,
2397 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 },
2399 {
2400 .name = "registers",
2401 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002402 .params = "",
2403 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002404 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 },
2406 {
2407 .name = "cpus",
2408 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002409 .params = "",
2410 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002411 .user_print = monitor_print_cpus,
2412 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 },
2414 {
2415 .name = "history",
2416 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002417 .params = "",
2418 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002419 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002420 },
2421 {
2422 .name = "irq",
2423 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002424 .params = "",
2425 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002426 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002427 },
2428 {
2429 .name = "pic",
2430 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002431 .params = "",
2432 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002433 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002434 },
2435 {
2436 .name = "pci",
2437 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002438 .params = "",
2439 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002440 .user_print = do_pci_info_print,
2441 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002442 },
aurel327c664e22009-03-03 06:12:22 +00002443#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002444 {
2445 .name = "tlb",
2446 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002447 .params = "",
2448 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002449 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002450 },
aurel327c664e22009-03-03 06:12:22 +00002451#endif
2452#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002453 {
2454 .name = "mem",
2455 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002456 .params = "",
2457 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002458 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002459 },
bellardb86bda52004-09-18 19:32:46 +00002460#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002461 {
2462 .name = "jit",
2463 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002464 .params = "",
2465 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002466 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 },
2468 {
2469 .name = "kvm",
2470 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 .params = "",
2472 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002473 .user_print = do_info_kvm_print,
2474 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002475 },
2476 {
2477 .name = "numa",
2478 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002479 .params = "",
2480 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002481 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 },
2483 {
2484 .name = "usb",
2485 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002486 .params = "",
2487 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002488 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002489 },
2490 {
2491 .name = "usbhost",
2492 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002493 .params = "",
2494 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002495 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 },
2497 {
2498 .name = "profile",
2499 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002500 .params = "",
2501 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002502 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 },
2504 {
2505 .name = "capture",
2506 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002507 .params = "",
2508 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002509 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002510 },
2511 {
2512 .name = "snapshots",
2513 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 .params = "",
2515 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002516 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 },
2518 {
2519 .name = "status",
2520 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 .params = "",
2522 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002523 .user_print = do_info_status_print,
2524 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 },
2526 {
2527 .name = "pcmcia",
2528 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 .params = "",
2530 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002531 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "mice",
2535 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 .params = "",
2537 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002538 .user_print = do_info_mice_print,
2539 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "vnc",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002546 .user_print = do_info_vnc_print,
2547 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 },
2549 {
2550 .name = "name",
2551 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 .params = "",
2553 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002554 .user_print = do_info_name_print,
2555 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 },
2557 {
2558 .name = "uuid",
2559 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 .params = "",
2561 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002562 .user_print = do_info_uuid_print,
2563 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 },
j_mayer76a66252007-03-07 08:32:30 +00002565#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 {
2567 .name = "cpustats",
2568 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 .params = "",
2570 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002571 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
j_mayer76a66252007-03-07 08:32:30 +00002573#endif
blueswir131a60e22007-10-26 18:42:59 +00002574#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 {
2576 .name = "usernet",
2577 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 .params = "",
2579 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002580 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002581 },
blueswir131a60e22007-10-26 18:42:59 +00002582#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 {
2584 .name = "migrate",
2585 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 .params = "",
2587 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002588 .user_print = do_info_migrate_print,
2589 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 },
2591 {
2592 .name = "balloon",
2593 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 .params = "",
2595 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002596 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002597 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002598 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 },
2600 {
2601 .name = "qtree",
2602 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 .params = "",
2604 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002605 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 },
2607 {
2608 .name = "qdm",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002612 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
2614 {
2615 .name = "roms",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002619 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302621#if defined(CONFIG_SIMPLE_TRACE)
2622 {
2623 .name = "trace",
2624 .args_type = "",
2625 .params = "",
2626 .help = "show current contents of trace buffer",
2627 .mhandler.info = do_info_trace,
2628 },
2629 {
2630 .name = "trace-events",
2631 .args_type = "",
2632 .params = "",
2633 .help = "show available trace-events & their state",
2634 .mhandler.info = do_info_trace_events,
2635 },
2636#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002637 {
2638 .name = NULL,
2639 },
bellard9dc39cb2004-03-14 21:38:27 +00002640};
2641
bellard9307c4c2004-04-04 12:57:25 +00002642/*******************************************************************/
2643
2644static const char *pch;
2645static jmp_buf expr_env;
2646
bellard92a31b12005-02-10 22:00:52 +00002647#define MD_TLONG 0
2648#define MD_I32 1
2649
bellard9307c4c2004-04-04 12:57:25 +00002650typedef struct MonitorDef {
2651 const char *name;
2652 int offset;
blueswir18662d652008-10-02 18:32:44 +00002653 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002654 int type;
bellard9307c4c2004-04-04 12:57:25 +00002655} MonitorDef;
2656
bellard57206fd2004-04-25 18:54:52 +00002657#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002658static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002659{
bellard6a00d602005-11-21 23:25:50 +00002660 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002661 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002662}
2663#endif
2664
bellarda541f292004-04-12 20:39:29 +00002665#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002666static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002667{
bellard6a00d602005-11-21 23:25:50 +00002668 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002669 unsigned int u;
2670 int i;
2671
2672 u = 0;
2673 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002674 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002675
2676 return u;
2677}
2678
blueswir18662d652008-10-02 18:32:44 +00002679static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002680{
bellard6a00d602005-11-21 23:25:50 +00002681 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002682 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002683}
2684
blueswir18662d652008-10-02 18:32:44 +00002685static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002686{
bellard6a00d602005-11-21 23:25:50 +00002687 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002688 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002689}
bellard9fddaa02004-05-21 12:59:32 +00002690
blueswir18662d652008-10-02 18:32:44 +00002691static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002692{
bellard6a00d602005-11-21 23:25:50 +00002693 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002694 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002695}
2696
blueswir18662d652008-10-02 18:32:44 +00002697static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002698{
bellard6a00d602005-11-21 23:25:50 +00002699 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002700 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002701}
2702
blueswir18662d652008-10-02 18:32:44 +00002703static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002704{
bellard6a00d602005-11-21 23:25:50 +00002705 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002706 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002707}
bellarda541f292004-04-12 20:39:29 +00002708#endif
2709
bellarde95c8d52004-09-30 22:22:08 +00002710#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002711#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002712static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002713{
bellard6a00d602005-11-21 23:25:50 +00002714 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002715
2716 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002717}
bellard7b936c02005-10-30 17:05:13 +00002718#endif
bellarde95c8d52004-09-30 22:22:08 +00002719
blueswir18662d652008-10-02 18:32:44 +00002720static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002721{
bellard6a00d602005-11-21 23:25:50 +00002722 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002723 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002724}
2725#endif
2726
blueswir18662d652008-10-02 18:32:44 +00002727static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002728#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002729
2730#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002731 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002732 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002733 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002734
bellard9307c4c2004-04-04 12:57:25 +00002735 { "eax", offsetof(CPUState, regs[0]) },
2736 { "ecx", offsetof(CPUState, regs[1]) },
2737 { "edx", offsetof(CPUState, regs[2]) },
2738 { "ebx", offsetof(CPUState, regs[3]) },
2739 { "esp|sp", offsetof(CPUState, regs[4]) },
2740 { "ebp|fp", offsetof(CPUState, regs[5]) },
2741 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002742 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002743#ifdef TARGET_X86_64
2744 { "r8", offsetof(CPUState, regs[8]) },
2745 { "r9", offsetof(CPUState, regs[9]) },
2746 { "r10", offsetof(CPUState, regs[10]) },
2747 { "r11", offsetof(CPUState, regs[11]) },
2748 { "r12", offsetof(CPUState, regs[12]) },
2749 { "r13", offsetof(CPUState, regs[13]) },
2750 { "r14", offsetof(CPUState, regs[14]) },
2751 { "r15", offsetof(CPUState, regs[15]) },
2752#endif
bellard9307c4c2004-04-04 12:57:25 +00002753 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002754 { "eip", offsetof(CPUState, eip) },
2755 SEG("cs", R_CS)
2756 SEG("ds", R_DS)
2757 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002758 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002759 SEG("fs", R_FS)
2760 SEG("gs", R_GS)
2761 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002762#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002763 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002764 { "r0", offsetof(CPUState, gpr[0]) },
2765 { "r1", offsetof(CPUState, gpr[1]) },
2766 { "r2", offsetof(CPUState, gpr[2]) },
2767 { "r3", offsetof(CPUState, gpr[3]) },
2768 { "r4", offsetof(CPUState, gpr[4]) },
2769 { "r5", offsetof(CPUState, gpr[5]) },
2770 { "r6", offsetof(CPUState, gpr[6]) },
2771 { "r7", offsetof(CPUState, gpr[7]) },
2772 { "r8", offsetof(CPUState, gpr[8]) },
2773 { "r9", offsetof(CPUState, gpr[9]) },
2774 { "r10", offsetof(CPUState, gpr[10]) },
2775 { "r11", offsetof(CPUState, gpr[11]) },
2776 { "r12", offsetof(CPUState, gpr[12]) },
2777 { "r13", offsetof(CPUState, gpr[13]) },
2778 { "r14", offsetof(CPUState, gpr[14]) },
2779 { "r15", offsetof(CPUState, gpr[15]) },
2780 { "r16", offsetof(CPUState, gpr[16]) },
2781 { "r17", offsetof(CPUState, gpr[17]) },
2782 { "r18", offsetof(CPUState, gpr[18]) },
2783 { "r19", offsetof(CPUState, gpr[19]) },
2784 { "r20", offsetof(CPUState, gpr[20]) },
2785 { "r21", offsetof(CPUState, gpr[21]) },
2786 { "r22", offsetof(CPUState, gpr[22]) },
2787 { "r23", offsetof(CPUState, gpr[23]) },
2788 { "r24", offsetof(CPUState, gpr[24]) },
2789 { "r25", offsetof(CPUState, gpr[25]) },
2790 { "r26", offsetof(CPUState, gpr[26]) },
2791 { "r27", offsetof(CPUState, gpr[27]) },
2792 { "r28", offsetof(CPUState, gpr[28]) },
2793 { "r29", offsetof(CPUState, gpr[29]) },
2794 { "r30", offsetof(CPUState, gpr[30]) },
2795 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002796 /* Floating point registers */
2797 { "f0", offsetof(CPUState, fpr[0]) },
2798 { "f1", offsetof(CPUState, fpr[1]) },
2799 { "f2", offsetof(CPUState, fpr[2]) },
2800 { "f3", offsetof(CPUState, fpr[3]) },
2801 { "f4", offsetof(CPUState, fpr[4]) },
2802 { "f5", offsetof(CPUState, fpr[5]) },
2803 { "f6", offsetof(CPUState, fpr[6]) },
2804 { "f7", offsetof(CPUState, fpr[7]) },
2805 { "f8", offsetof(CPUState, fpr[8]) },
2806 { "f9", offsetof(CPUState, fpr[9]) },
2807 { "f10", offsetof(CPUState, fpr[10]) },
2808 { "f11", offsetof(CPUState, fpr[11]) },
2809 { "f12", offsetof(CPUState, fpr[12]) },
2810 { "f13", offsetof(CPUState, fpr[13]) },
2811 { "f14", offsetof(CPUState, fpr[14]) },
2812 { "f15", offsetof(CPUState, fpr[15]) },
2813 { "f16", offsetof(CPUState, fpr[16]) },
2814 { "f17", offsetof(CPUState, fpr[17]) },
2815 { "f18", offsetof(CPUState, fpr[18]) },
2816 { "f19", offsetof(CPUState, fpr[19]) },
2817 { "f20", offsetof(CPUState, fpr[20]) },
2818 { "f21", offsetof(CPUState, fpr[21]) },
2819 { "f22", offsetof(CPUState, fpr[22]) },
2820 { "f23", offsetof(CPUState, fpr[23]) },
2821 { "f24", offsetof(CPUState, fpr[24]) },
2822 { "f25", offsetof(CPUState, fpr[25]) },
2823 { "f26", offsetof(CPUState, fpr[26]) },
2824 { "f27", offsetof(CPUState, fpr[27]) },
2825 { "f28", offsetof(CPUState, fpr[28]) },
2826 { "f29", offsetof(CPUState, fpr[29]) },
2827 { "f30", offsetof(CPUState, fpr[30]) },
2828 { "f31", offsetof(CPUState, fpr[31]) },
2829 { "fpscr", offsetof(CPUState, fpscr) },
2830 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002831 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002832 { "lr", offsetof(CPUState, lr) },
2833 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002834 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002835 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002836 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002837 { "msr", 0, &monitor_get_msr, },
2838 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002839 { "tbu", 0, &monitor_get_tbu, },
2840 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002841#if defined(TARGET_PPC64)
2842 /* Address space register */
2843 { "asr", offsetof(CPUState, asr) },
2844#endif
2845 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002846 { "sdr1", offsetof(CPUState, sdr1) },
2847 { "sr0", offsetof(CPUState, sr[0]) },
2848 { "sr1", offsetof(CPUState, sr[1]) },
2849 { "sr2", offsetof(CPUState, sr[2]) },
2850 { "sr3", offsetof(CPUState, sr[3]) },
2851 { "sr4", offsetof(CPUState, sr[4]) },
2852 { "sr5", offsetof(CPUState, sr[5]) },
2853 { "sr6", offsetof(CPUState, sr[6]) },
2854 { "sr7", offsetof(CPUState, sr[7]) },
2855 { "sr8", offsetof(CPUState, sr[8]) },
2856 { "sr9", offsetof(CPUState, sr[9]) },
2857 { "sr10", offsetof(CPUState, sr[10]) },
2858 { "sr11", offsetof(CPUState, sr[11]) },
2859 { "sr12", offsetof(CPUState, sr[12]) },
2860 { "sr13", offsetof(CPUState, sr[13]) },
2861 { "sr14", offsetof(CPUState, sr[14]) },
2862 { "sr15", offsetof(CPUState, sr[15]) },
2863 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002864#elif defined(TARGET_SPARC)
2865 { "g0", offsetof(CPUState, gregs[0]) },
2866 { "g1", offsetof(CPUState, gregs[1]) },
2867 { "g2", offsetof(CPUState, gregs[2]) },
2868 { "g3", offsetof(CPUState, gregs[3]) },
2869 { "g4", offsetof(CPUState, gregs[4]) },
2870 { "g5", offsetof(CPUState, gregs[5]) },
2871 { "g6", offsetof(CPUState, gregs[6]) },
2872 { "g7", offsetof(CPUState, gregs[7]) },
2873 { "o0", 0, monitor_get_reg },
2874 { "o1", 1, monitor_get_reg },
2875 { "o2", 2, monitor_get_reg },
2876 { "o3", 3, monitor_get_reg },
2877 { "o4", 4, monitor_get_reg },
2878 { "o5", 5, monitor_get_reg },
2879 { "o6", 6, monitor_get_reg },
2880 { "o7", 7, monitor_get_reg },
2881 { "l0", 8, monitor_get_reg },
2882 { "l1", 9, monitor_get_reg },
2883 { "l2", 10, monitor_get_reg },
2884 { "l3", 11, monitor_get_reg },
2885 { "l4", 12, monitor_get_reg },
2886 { "l5", 13, monitor_get_reg },
2887 { "l6", 14, monitor_get_reg },
2888 { "l7", 15, monitor_get_reg },
2889 { "i0", 16, monitor_get_reg },
2890 { "i1", 17, monitor_get_reg },
2891 { "i2", 18, monitor_get_reg },
2892 { "i3", 19, monitor_get_reg },
2893 { "i4", 20, monitor_get_reg },
2894 { "i5", 21, monitor_get_reg },
2895 { "i6", 22, monitor_get_reg },
2896 { "i7", 23, monitor_get_reg },
2897 { "pc", offsetof(CPUState, pc) },
2898 { "npc", offsetof(CPUState, npc) },
2899 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002900#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002901 { "psr", 0, &monitor_get_psr, },
2902 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002903#endif
bellarde95c8d52004-09-30 22:22:08 +00002904 { "tbr", offsetof(CPUState, tbr) },
2905 { "fsr", offsetof(CPUState, fsr) },
2906 { "f0", offsetof(CPUState, fpr[0]) },
2907 { "f1", offsetof(CPUState, fpr[1]) },
2908 { "f2", offsetof(CPUState, fpr[2]) },
2909 { "f3", offsetof(CPUState, fpr[3]) },
2910 { "f4", offsetof(CPUState, fpr[4]) },
2911 { "f5", offsetof(CPUState, fpr[5]) },
2912 { "f6", offsetof(CPUState, fpr[6]) },
2913 { "f7", offsetof(CPUState, fpr[7]) },
2914 { "f8", offsetof(CPUState, fpr[8]) },
2915 { "f9", offsetof(CPUState, fpr[9]) },
2916 { "f10", offsetof(CPUState, fpr[10]) },
2917 { "f11", offsetof(CPUState, fpr[11]) },
2918 { "f12", offsetof(CPUState, fpr[12]) },
2919 { "f13", offsetof(CPUState, fpr[13]) },
2920 { "f14", offsetof(CPUState, fpr[14]) },
2921 { "f15", offsetof(CPUState, fpr[15]) },
2922 { "f16", offsetof(CPUState, fpr[16]) },
2923 { "f17", offsetof(CPUState, fpr[17]) },
2924 { "f18", offsetof(CPUState, fpr[18]) },
2925 { "f19", offsetof(CPUState, fpr[19]) },
2926 { "f20", offsetof(CPUState, fpr[20]) },
2927 { "f21", offsetof(CPUState, fpr[21]) },
2928 { "f22", offsetof(CPUState, fpr[22]) },
2929 { "f23", offsetof(CPUState, fpr[23]) },
2930 { "f24", offsetof(CPUState, fpr[24]) },
2931 { "f25", offsetof(CPUState, fpr[25]) },
2932 { "f26", offsetof(CPUState, fpr[26]) },
2933 { "f27", offsetof(CPUState, fpr[27]) },
2934 { "f28", offsetof(CPUState, fpr[28]) },
2935 { "f29", offsetof(CPUState, fpr[29]) },
2936 { "f30", offsetof(CPUState, fpr[30]) },
2937 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002938#ifdef TARGET_SPARC64
2939 { "f32", offsetof(CPUState, fpr[32]) },
2940 { "f34", offsetof(CPUState, fpr[34]) },
2941 { "f36", offsetof(CPUState, fpr[36]) },
2942 { "f38", offsetof(CPUState, fpr[38]) },
2943 { "f40", offsetof(CPUState, fpr[40]) },
2944 { "f42", offsetof(CPUState, fpr[42]) },
2945 { "f44", offsetof(CPUState, fpr[44]) },
2946 { "f46", offsetof(CPUState, fpr[46]) },
2947 { "f48", offsetof(CPUState, fpr[48]) },
2948 { "f50", offsetof(CPUState, fpr[50]) },
2949 { "f52", offsetof(CPUState, fpr[52]) },
2950 { "f54", offsetof(CPUState, fpr[54]) },
2951 { "f56", offsetof(CPUState, fpr[56]) },
2952 { "f58", offsetof(CPUState, fpr[58]) },
2953 { "f60", offsetof(CPUState, fpr[60]) },
2954 { "f62", offsetof(CPUState, fpr[62]) },
2955 { "asi", offsetof(CPUState, asi) },
2956 { "pstate", offsetof(CPUState, pstate) },
2957 { "cansave", offsetof(CPUState, cansave) },
2958 { "canrestore", offsetof(CPUState, canrestore) },
2959 { "otherwin", offsetof(CPUState, otherwin) },
2960 { "wstate", offsetof(CPUState, wstate) },
2961 { "cleanwin", offsetof(CPUState, cleanwin) },
2962 { "fprs", offsetof(CPUState, fprs) },
2963#endif
bellard9307c4c2004-04-04 12:57:25 +00002964#endif
2965 { NULL },
2966};
2967
aliguori376253e2009-03-05 23:01:23 +00002968static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002969{
aliguori376253e2009-03-05 23:01:23 +00002970 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002971 longjmp(expr_env, 1);
2972}
2973
Markus Armbruster09b94182010-01-20 13:07:30 +01002974/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002975static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002976{
blueswir18662d652008-10-02 18:32:44 +00002977 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002978 void *ptr;
2979
bellard9307c4c2004-04-04 12:57:25 +00002980 for(md = monitor_defs; md->name != NULL; md++) {
2981 if (compare_cmd(name, md->name)) {
2982 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002983 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002984 } else {
bellard6a00d602005-11-21 23:25:50 +00002985 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002986 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002987 switch(md->type) {
2988 case MD_I32:
2989 *pval = *(int32_t *)ptr;
2990 break;
2991 case MD_TLONG:
2992 *pval = *(target_long *)ptr;
2993 break;
2994 default:
2995 *pval = 0;
2996 break;
2997 }
bellard9307c4c2004-04-04 12:57:25 +00002998 }
2999 return 0;
3000 }
3001 }
3002 return -1;
3003}
3004
3005static void next(void)
3006{
Blue Swirl660f11b2009-07-31 21:16:51 +00003007 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003008 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003009 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003010 pch++;
3011 }
3012}
3013
aliguori376253e2009-03-05 23:01:23 +00003014static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003015
aliguori376253e2009-03-05 23:01:23 +00003016static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003017{
blueswir1c2efc952007-09-25 17:28:42 +00003018 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003019 char *p;
bellard6a00d602005-11-21 23:25:50 +00003020 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003021
3022 switch(*pch) {
3023 case '+':
3024 next();
aliguori376253e2009-03-05 23:01:23 +00003025 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003026 break;
3027 case '-':
3028 next();
aliguori376253e2009-03-05 23:01:23 +00003029 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003030 break;
3031 case '~':
3032 next();
aliguori376253e2009-03-05 23:01:23 +00003033 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003034 break;
3035 case '(':
3036 next();
aliguori376253e2009-03-05 23:01:23 +00003037 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003038 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003039 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003040 }
3041 next();
3042 break;
bellard81d09122004-07-14 17:21:37 +00003043 case '\'':
3044 pch++;
3045 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003046 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003047 n = *pch;
3048 pch++;
3049 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003050 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003051 next();
3052 break;
bellard9307c4c2004-04-04 12:57:25 +00003053 case '$':
3054 {
3055 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003056 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003057
bellard9307c4c2004-04-04 12:57:25 +00003058 pch++;
3059 q = buf;
3060 while ((*pch >= 'a' && *pch <= 'z') ||
3061 (*pch >= 'A' && *pch <= 'Z') ||
3062 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003063 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003064 if ((q - buf) < sizeof(buf) - 1)
3065 *q++ = *pch;
3066 pch++;
3067 }
blueswir1cd390082008-11-16 13:53:32 +00003068 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003069 pch++;
3070 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003071 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003072 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003073 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003074 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003075 }
3076 break;
3077 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003078 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003079 n = 0;
3080 break;
3081 default:
blueswir17743e582007-09-24 18:39:04 +00003082#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003083 n = strtoull(pch, &p, 0);
3084#else
bellard9307c4c2004-04-04 12:57:25 +00003085 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003086#endif
bellard9307c4c2004-04-04 12:57:25 +00003087 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003088 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003089 }
3090 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003091 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003092 pch++;
3093 break;
3094 }
3095 return n;
3096}
3097
3098
aliguori376253e2009-03-05 23:01:23 +00003099static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003100{
blueswir1c2efc952007-09-25 17:28:42 +00003101 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003102 int op;
ths3b46e622007-09-17 08:09:54 +00003103
aliguori376253e2009-03-05 23:01:23 +00003104 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003105 for(;;) {
3106 op = *pch;
3107 if (op != '*' && op != '/' && op != '%')
3108 break;
3109 next();
aliguori376253e2009-03-05 23:01:23 +00003110 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003111 switch(op) {
3112 default:
3113 case '*':
3114 val *= val2;
3115 break;
3116 case '/':
3117 case '%':
ths5fafdf22007-09-16 21:08:06 +00003118 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003119 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003120 if (op == '/')
3121 val /= val2;
3122 else
3123 val %= val2;
3124 break;
3125 }
3126 }
3127 return val;
3128}
3129
aliguori376253e2009-03-05 23:01:23 +00003130static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003131{
blueswir1c2efc952007-09-25 17:28:42 +00003132 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003133 int op;
bellard9307c4c2004-04-04 12:57:25 +00003134
aliguori376253e2009-03-05 23:01:23 +00003135 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003136 for(;;) {
3137 op = *pch;
3138 if (op != '&' && op != '|' && op != '^')
3139 break;
3140 next();
aliguori376253e2009-03-05 23:01:23 +00003141 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003142 switch(op) {
3143 default:
3144 case '&':
3145 val &= val2;
3146 break;
3147 case '|':
3148 val |= val2;
3149 break;
3150 case '^':
3151 val ^= val2;
3152 break;
3153 }
3154 }
3155 return val;
3156}
3157
aliguori376253e2009-03-05 23:01:23 +00003158static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003159{
blueswir1c2efc952007-09-25 17:28:42 +00003160 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003161 int op;
bellard9307c4c2004-04-04 12:57:25 +00003162
aliguori376253e2009-03-05 23:01:23 +00003163 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003164 for(;;) {
3165 op = *pch;
3166 if (op != '+' && op != '-')
3167 break;
3168 next();
aliguori376253e2009-03-05 23:01:23 +00003169 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003170 if (op == '+')
3171 val += val2;
3172 else
3173 val -= val2;
3174 }
3175 return val;
3176}
3177
aliguori376253e2009-03-05 23:01:23 +00003178static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003179{
3180 pch = *pp;
3181 if (setjmp(expr_env)) {
3182 *pp = pch;
3183 return -1;
3184 }
blueswir1cd390082008-11-16 13:53:32 +00003185 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003186 pch++;
aliguori376253e2009-03-05 23:01:23 +00003187 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003188 *pp = pch;
3189 return 0;
3190}
3191
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003192static int get_double(Monitor *mon, double *pval, const char **pp)
3193{
3194 const char *p = *pp;
3195 char *tailp;
3196 double d;
3197
3198 d = strtod(p, &tailp);
3199 if (tailp == p) {
3200 monitor_printf(mon, "Number expected\n");
3201 return -1;
3202 }
3203 if (d != d || d - d != 0) {
3204 /* NaN or infinity */
3205 monitor_printf(mon, "Bad number\n");
3206 return -1;
3207 }
3208 *pval = d;
3209 *pp = tailp;
3210 return 0;
3211}
3212
bellard9307c4c2004-04-04 12:57:25 +00003213static int get_str(char *buf, int buf_size, const char **pp)
3214{
3215 const char *p;
3216 char *q;
3217 int c;
3218
bellard81d09122004-07-14 17:21:37 +00003219 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003220 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003221 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003222 p++;
3223 if (*p == '\0') {
3224 fail:
bellard81d09122004-07-14 17:21:37 +00003225 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003226 *pp = p;
3227 return -1;
3228 }
bellard9307c4c2004-04-04 12:57:25 +00003229 if (*p == '\"') {
3230 p++;
3231 while (*p != '\0' && *p != '\"') {
3232 if (*p == '\\') {
3233 p++;
3234 c = *p++;
3235 switch(c) {
3236 case 'n':
3237 c = '\n';
3238 break;
3239 case 'r':
3240 c = '\r';
3241 break;
3242 case '\\':
3243 case '\'':
3244 case '\"':
3245 break;
3246 default:
3247 qemu_printf("unsupported escape code: '\\%c'\n", c);
3248 goto fail;
3249 }
3250 if ((q - buf) < buf_size - 1) {
3251 *q++ = c;
3252 }
3253 } else {
3254 if ((q - buf) < buf_size - 1) {
3255 *q++ = *p;
3256 }
3257 p++;
3258 }
3259 }
3260 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003261 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003262 goto fail;
3263 }
3264 p++;
3265 } else {
blueswir1cd390082008-11-16 13:53:32 +00003266 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003267 if ((q - buf) < buf_size - 1) {
3268 *q++ = *p;
3269 }
3270 p++;
3271 }
bellard9307c4c2004-04-04 12:57:25 +00003272 }
bellard81d09122004-07-14 17:21:37 +00003273 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003274 *pp = p;
3275 return 0;
3276}
3277
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003278/*
3279 * Store the command-name in cmdname, and return a pointer to
3280 * the remaining of the command string.
3281 */
3282static const char *get_command_name(const char *cmdline,
3283 char *cmdname, size_t nlen)
3284{
3285 size_t len;
3286 const char *p, *pstart;
3287
3288 p = cmdline;
3289 while (qemu_isspace(*p))
3290 p++;
3291 if (*p == '\0')
3292 return NULL;
3293 pstart = p;
3294 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3295 p++;
3296 len = p - pstart;
3297 if (len > nlen - 1)
3298 len = nlen - 1;
3299 memcpy(cmdname, pstart, len);
3300 cmdname[len] = '\0';
3301 return p;
3302}
3303
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003304/**
3305 * Read key of 'type' into 'key' and return the current
3306 * 'type' pointer.
3307 */
3308static char *key_get_info(const char *type, char **key)
3309{
3310 size_t len;
3311 char *p, *str;
3312
3313 if (*type == ',')
3314 type++;
3315
3316 p = strchr(type, ':');
3317 if (!p) {
3318 *key = NULL;
3319 return NULL;
3320 }
3321 len = p - type;
3322
3323 str = qemu_malloc(len + 1);
3324 memcpy(str, type, len);
3325 str[len] = '\0';
3326
3327 *key = str;
3328 return ++p;
3329}
3330
bellard9307c4c2004-04-04 12:57:25 +00003331static int default_fmt_format = 'x';
3332static int default_fmt_size = 4;
3333
3334#define MAX_ARGS 16
3335
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003336static int is_valid_option(const char *c, const char *typestr)
3337{
3338 char option[3];
3339
3340 option[0] = '-';
3341 option[1] = *c;
3342 option[2] = '\0';
3343
3344 typestr = strstr(typestr, option);
3345 return (typestr != NULL);
3346}
3347
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003348static const mon_cmd_t *monitor_find_command(const char *cmdname)
3349{
3350 const mon_cmd_t *cmd;
3351
3352 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3353 if (compare_cmd(cmdname, cmd->name)) {
3354 return cmd;
3355 }
3356 }
3357
3358 return NULL;
3359}
3360
Anthony Liguoric227f092009-10-01 16:12:16 -05003361static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003362 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003363 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003364{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003365 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003366 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003367 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003368 char cmdname[256];
3369 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003370 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003371
3372#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003373 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003374#endif
ths3b46e622007-09-17 08:09:54 +00003375
bellard9307c4c2004-04-04 12:57:25 +00003376 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003377 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3378 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003379 return NULL;
ths3b46e622007-09-17 08:09:54 +00003380
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003381 cmd = monitor_find_command(cmdname);
3382 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003383 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003384 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003385 }
bellard9307c4c2004-04-04 12:57:25 +00003386
bellard9307c4c2004-04-04 12:57:25 +00003387 /* parse the parameters */
3388 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003389 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003390 typestr = key_get_info(typestr, &key);
3391 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003392 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003393 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003394 typestr++;
3395 switch(c) {
3396 case 'F':
bellard81d09122004-07-14 17:21:37 +00003397 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003398 case 's':
3399 {
3400 int ret;
ths3b46e622007-09-17 08:09:54 +00003401
blueswir1cd390082008-11-16 13:53:32 +00003402 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003403 p++;
3404 if (*typestr == '?') {
3405 typestr++;
3406 if (*p == '\0') {
3407 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003408 break;
bellard9307c4c2004-04-04 12:57:25 +00003409 }
3410 }
3411 ret = get_str(buf, sizeof(buf), &p);
3412 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003413 switch(c) {
3414 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003415 monitor_printf(mon, "%s: filename expected\n",
3416 cmdname);
bellard81d09122004-07-14 17:21:37 +00003417 break;
3418 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003419 monitor_printf(mon, "%s: block device name expected\n",
3420 cmdname);
bellard81d09122004-07-14 17:21:37 +00003421 break;
3422 default:
aliguori376253e2009-03-05 23:01:23 +00003423 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003424 break;
3425 }
bellard9307c4c2004-04-04 12:57:25 +00003426 goto fail;
3427 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003428 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003429 }
3430 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003431 case 'O':
3432 {
3433 QemuOptsList *opts_list;
3434 QemuOpts *opts;
3435
3436 opts_list = qemu_find_opts(key);
3437 if (!opts_list || opts_list->desc->name) {
3438 goto bad_type;
3439 }
3440 while (qemu_isspace(*p)) {
3441 p++;
3442 }
3443 if (!*p)
3444 break;
3445 if (get_str(buf, sizeof(buf), &p) < 0) {
3446 goto fail;
3447 }
3448 opts = qemu_opts_parse(opts_list, buf, 1);
3449 if (!opts) {
3450 goto fail;
3451 }
3452 qemu_opts_to_qdict(opts, qdict);
3453 qemu_opts_del(opts);
3454 }
3455 break;
bellard9307c4c2004-04-04 12:57:25 +00003456 case '/':
3457 {
3458 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003459
blueswir1cd390082008-11-16 13:53:32 +00003460 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003461 p++;
3462 if (*p == '/') {
3463 /* format found */
3464 p++;
3465 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003466 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003467 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003468 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003469 count = count * 10 + (*p - '0');
3470 p++;
3471 }
3472 }
3473 size = -1;
3474 format = -1;
3475 for(;;) {
3476 switch(*p) {
3477 case 'o':
3478 case 'd':
3479 case 'u':
3480 case 'x':
3481 case 'i':
3482 case 'c':
3483 format = *p++;
3484 break;
3485 case 'b':
3486 size = 1;
3487 p++;
3488 break;
3489 case 'h':
3490 size = 2;
3491 p++;
3492 break;
3493 case 'w':
3494 size = 4;
3495 p++;
3496 break;
3497 case 'g':
3498 case 'L':
3499 size = 8;
3500 p++;
3501 break;
3502 default:
3503 goto next;
3504 }
3505 }
3506 next:
blueswir1cd390082008-11-16 13:53:32 +00003507 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003508 monitor_printf(mon, "invalid char in format: '%c'\n",
3509 *p);
bellard9307c4c2004-04-04 12:57:25 +00003510 goto fail;
3511 }
bellard9307c4c2004-04-04 12:57:25 +00003512 if (format < 0)
3513 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003514 if (format != 'i') {
3515 /* for 'i', not specifying a size gives -1 as size */
3516 if (size < 0)
3517 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003518 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003519 }
bellard9307c4c2004-04-04 12:57:25 +00003520 default_fmt_format = format;
3521 } else {
3522 count = 1;
3523 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003524 if (format != 'i') {
3525 size = default_fmt_size;
3526 } else {
3527 size = -1;
3528 }
bellard9307c4c2004-04-04 12:57:25 +00003529 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003530 qdict_put(qdict, "count", qint_from_int(count));
3531 qdict_put(qdict, "format", qint_from_int(format));
3532 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003533 }
3534 break;
3535 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003536 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003537 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003538 {
blueswir1c2efc952007-09-25 17:28:42 +00003539 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003540
blueswir1cd390082008-11-16 13:53:32 +00003541 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003542 p++;
bellard34405572004-06-08 00:55:58 +00003543 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003544 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003545 if (*p == '\0') {
3546 typestr++;
3547 break;
3548 }
bellard34405572004-06-08 00:55:58 +00003549 } else {
3550 if (*p == '.') {
3551 p++;
blueswir1cd390082008-11-16 13:53:32 +00003552 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003553 p++;
bellard34405572004-06-08 00:55:58 +00003554 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003555 typestr++;
3556 break;
bellard34405572004-06-08 00:55:58 +00003557 }
3558 }
bellard13224a82006-07-14 22:03:35 +00003559 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003560 }
aliguori376253e2009-03-05 23:01:23 +00003561 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003562 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003563 /* Check if 'i' is greater than 32-bit */
3564 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3565 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3566 monitor_printf(mon, "integer is for 32-bit values\n");
3567 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003568 } else if (c == 'M') {
3569 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003570 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003571 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003572 }
3573 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003574 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003575 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003576 {
3577 double val;
3578
3579 while (qemu_isspace(*p))
3580 p++;
3581 if (*typestr == '?') {
3582 typestr++;
3583 if (*p == '\0') {
3584 break;
3585 }
3586 }
3587 if (get_double(mon, &val, &p) < 0) {
3588 goto fail;
3589 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003590 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003591 switch (*p) {
3592 case 'K': case 'k':
3593 val *= 1 << 10; p++; break;
3594 case 'M': case 'm':
3595 val *= 1 << 20; p++; break;
3596 case 'G': case 'g':
3597 val *= 1 << 30; p++; break;
3598 }
3599 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003600 if (c == 'T' && p[0] && p[1] == 's') {
3601 switch (*p) {
3602 case 'm':
3603 val /= 1e3; p += 2; break;
3604 case 'u':
3605 val /= 1e6; p += 2; break;
3606 case 'n':
3607 val /= 1e9; p += 2; break;
3608 }
3609 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003610 if (*p && !qemu_isspace(*p)) {
3611 monitor_printf(mon, "Unknown unit suffix\n");
3612 goto fail;
3613 }
3614 qdict_put(qdict, key, qfloat_from_double(val));
3615 }
3616 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003617 case 'b':
3618 {
3619 const char *beg;
3620 int val;
3621
3622 while (qemu_isspace(*p)) {
3623 p++;
3624 }
3625 beg = p;
3626 while (qemu_isgraph(*p)) {
3627 p++;
3628 }
3629 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3630 val = 1;
3631 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3632 val = 0;
3633 } else {
3634 monitor_printf(mon, "Expected 'on' or 'off'\n");
3635 goto fail;
3636 }
3637 qdict_put(qdict, key, qbool_from_int(val));
3638 }
3639 break;
bellard9307c4c2004-04-04 12:57:25 +00003640 case '-':
3641 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003642 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003643 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003644 /* option */
ths3b46e622007-09-17 08:09:54 +00003645
bellard9307c4c2004-04-04 12:57:25 +00003646 c = *typestr++;
3647 if (c == '\0')
3648 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003649 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003650 p++;
bellard9307c4c2004-04-04 12:57:25 +00003651 if (*p == '-') {
3652 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003653 if(c != *p) {
3654 if(!is_valid_option(p, typestr)) {
3655
3656 monitor_printf(mon, "%s: unsupported option -%c\n",
3657 cmdname, *p);
3658 goto fail;
3659 } else {
3660 skip_key = 1;
3661 }
bellard9307c4c2004-04-04 12:57:25 +00003662 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003663 if(skip_key) {
3664 p = tmp;
3665 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003666 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003667 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003668 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003669 }
bellard9307c4c2004-04-04 12:57:25 +00003670 }
bellard9307c4c2004-04-04 12:57:25 +00003671 }
3672 break;
3673 default:
3674 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003675 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003676 goto fail;
3677 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003678 qemu_free(key);
3679 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003680 }
3681 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003682 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003683 p++;
3684 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003685 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3686 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003687 goto fail;
3688 }
3689
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003690 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003691
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003692fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003693 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003694 return NULL;
3695}
3696
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003697void monitor_set_error(Monitor *mon, QError *qerror)
3698{
3699 /* report only the first error */
3700 if (!mon->error) {
3701 mon->error = qerror;
3702 } else {
3703 MON_DEBUG("Additional error report at %s:%d\n",
3704 qerror->file, qerror->linenr);
3705 QDECREF(qerror);
3706 }
3707}
3708
Adam Litke940cc302010-01-25 12:18:44 -06003709static int is_async_return(const QObject *data)
3710{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003711 if (data && qobject_type(data) == QTYPE_QDICT) {
3712 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3713 }
3714
3715 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003716}
3717
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003718static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3719{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003720 if (monitor_ctrl_mode(mon)) {
3721 if (ret && !monitor_has_error(mon)) {
3722 /*
3723 * If it returns failure, it must have passed on error.
3724 *
3725 * Action: Report an internal error to the client if in QMP.
3726 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003727 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003728 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3729 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003730 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003731
3732#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003733 if (!ret && monitor_has_error(mon)) {
3734 /*
3735 * If it returns success, it must not have passed an error.
3736 *
3737 * Action: Report the passed error to the client.
3738 */
3739 MON_DEBUG("command '%s' returned success but passed an error\n",
3740 cmd->name);
3741 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003742
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003743 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3744 /*
3745 * Handlers should not call Monitor print functions.
3746 *
3747 * Action: Ignore them in QMP.
3748 *
3749 * (XXX: we don't check any 'info' or 'query' command here
3750 * because the user print function _is_ called by do_info(), hence
3751 * we will trigger this check. This problem will go away when we
3752 * make 'query' commands real and kill do_info())
3753 */
3754 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3755 cmd->name, mon_print_count_get(mon));
3756 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003757#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003758 } else {
3759 assert(!monitor_has_error(mon));
3760 QDECREF(mon->error);
3761 mon->error = NULL;
3762 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003763}
3764
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003765static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3766 const QDict *params)
3767{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003768 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003769 QObject *data = NULL;
3770
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003771 mon_print_count_init(mon);
3772
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003773 ret = cmd->mhandler.cmd_new(mon, params, &data);
3774 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003775
Adam Litke940cc302010-01-25 12:18:44 -06003776 if (is_async_return(data)) {
3777 /*
3778 * Asynchronous commands have no initial return data but they can
3779 * generate errors. Data is returned via the async completion handler.
3780 */
3781 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3782 monitor_protocol_emitter(mon, NULL);
3783 }
3784 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003785 /* Monitor Protocol */
3786 monitor_protocol_emitter(mon, data);
3787 } else {
3788 /* User Protocol */
3789 if (data)
3790 cmd->user_print(mon, data);
3791 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003792
3793 qobject_decref(data);
3794}
3795
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003796static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003797{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003798 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003799 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003800
3801 qdict = qdict_new();
3802
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003803 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003804 if (!cmd)
3805 goto out;
3806
Adam Litke940cc302010-01-25 12:18:44 -06003807 if (monitor_handler_is_async(cmd)) {
3808 user_async_cmd_handler(mon, cmd, qdict);
3809 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003810 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003811 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003812 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003813 }
3814
Luiz Capitulino13917be2009-10-07 13:41:54 -03003815out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003816 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003817}
3818
bellard81d09122004-07-14 17:21:37 +00003819static void cmd_completion(const char *name, const char *list)
3820{
3821 const char *p, *pstart;
3822 char cmd[128];
3823 int len;
3824
3825 p = list;
3826 for(;;) {
3827 pstart = p;
3828 p = strchr(p, '|');
3829 if (!p)
3830 p = pstart + strlen(pstart);
3831 len = p - pstart;
3832 if (len > sizeof(cmd) - 2)
3833 len = sizeof(cmd) - 2;
3834 memcpy(cmd, pstart, len);
3835 cmd[len] = '\0';
3836 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003837 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003838 }
3839 if (*p == '\0')
3840 break;
3841 p++;
3842 }
3843}
3844
3845static void file_completion(const char *input)
3846{
3847 DIR *ffs;
3848 struct dirent *d;
3849 char path[1024];
3850 char file[1024], file_prefix[1024];
3851 int input_path_len;
3852 const char *p;
3853
ths5fafdf22007-09-16 21:08:06 +00003854 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003855 if (!p) {
3856 input_path_len = 0;
3857 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003858 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003859 } else {
3860 input_path_len = p - input + 1;
3861 memcpy(path, input, input_path_len);
3862 if (input_path_len > sizeof(path) - 1)
3863 input_path_len = sizeof(path) - 1;
3864 path[input_path_len] = '\0';
3865 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3866 }
3867#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003868 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3869 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003870#endif
3871 ffs = opendir(path);
3872 if (!ffs)
3873 return;
3874 for(;;) {
3875 struct stat sb;
3876 d = readdir(ffs);
3877 if (!d)
3878 break;
3879 if (strstart(d->d_name, file_prefix, NULL)) {
3880 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003881 if (input_path_len < sizeof(file))
3882 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3883 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003884 /* stat the file to find out if it's a directory.
3885 * In that case add a slash to speed up typing long paths
3886 */
3887 stat(file, &sb);
3888 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003889 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003890 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003891 }
3892 }
3893 closedir(ffs);
3894}
3895
aliguori51de9762009-03-05 23:00:43 +00003896static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003897{
aliguori51de9762009-03-05 23:00:43 +00003898 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003899 const char *input = opaque;
3900
3901 if (input[0] == '\0' ||
3902 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003903 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003904 }
3905}
3906
3907/* NOTE: this parser is an approximate form of the real command parser */
3908static void parse_cmdline(const char *cmdline,
3909 int *pnb_args, char **args)
3910{
3911 const char *p;
3912 int nb_args, ret;
3913 char buf[1024];
3914
3915 p = cmdline;
3916 nb_args = 0;
3917 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003918 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003919 p++;
3920 if (*p == '\0')
3921 break;
3922 if (nb_args >= MAX_ARGS)
3923 break;
3924 ret = get_str(buf, sizeof(buf), &p);
3925 args[nb_args] = qemu_strdup(buf);
3926 nb_args++;
3927 if (ret < 0)
3928 break;
3929 }
3930 *pnb_args = nb_args;
3931}
3932
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003933static const char *next_arg_type(const char *typestr)
3934{
3935 const char *p = strchr(typestr, ':');
3936 return (p != NULL ? ++p : typestr);
3937}
3938
aliguori4c36ba32009-03-05 23:01:37 +00003939static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003940{
3941 const char *cmdname;
3942 char *args[MAX_ARGS];
3943 int nb_args, i, len;
3944 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003945 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003946 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003947
3948 parse_cmdline(cmdline, &nb_args, args);
3949#ifdef DEBUG_COMPLETION
3950 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003951 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003952 }
3953#endif
3954
3955 /* if the line ends with a space, it means we want to complete the
3956 next arg */
3957 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003958 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003959 if (nb_args >= MAX_ARGS) {
3960 goto cleanup;
3961 }
bellard81d09122004-07-14 17:21:37 +00003962 args[nb_args++] = qemu_strdup("");
3963 }
3964 if (nb_args <= 1) {
3965 /* command completion */
3966 if (nb_args == 0)
3967 cmdname = "";
3968 else
3969 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003970 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003971 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003972 cmd_completion(cmdname, cmd->name);
3973 }
3974 } else {
3975 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02003976 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3977 if (compare_cmd(args[0], cmd->name)) {
3978 break;
3979 }
bellard81d09122004-07-14 17:21:37 +00003980 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003981 if (!cmd->name) {
3982 goto cleanup;
3983 }
3984
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003985 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003986 for(i = 0; i < nb_args - 2; i++) {
3987 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003988 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003989 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003990 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003991 }
3992 }
3993 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003994 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003995 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003996 }
bellard81d09122004-07-14 17:21:37 +00003997 switch(*ptype) {
3998 case 'F':
3999 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004000 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004001 file_completion(str);
4002 break;
4003 case 'B':
4004 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004005 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004006 bdrv_iterate(block_completion_it, (void *)str);
4007 break;
bellard7fe48482004-10-09 18:08:01 +00004008 case 's':
4009 /* XXX: more generic ? */
4010 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004011 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004012 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4013 cmd_completion(str, cmd->name);
4014 }
bellard64866c32006-05-07 18:03:31 +00004015 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004016 char *sep = strrchr(str, '-');
4017 if (sep)
4018 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004019 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004020 for(key = key_defs; key->name != NULL; key++) {
4021 cmd_completion(str, key->name);
4022 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004023 } else if (!strcmp(cmd->name, "help|?")) {
4024 readline_set_completion_index(cur_mon->rs, strlen(str));
4025 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4026 cmd_completion(str, cmd->name);
4027 }
bellard7fe48482004-10-09 18:08:01 +00004028 }
4029 break;
bellard81d09122004-07-14 17:21:37 +00004030 default:
4031 break;
4032 }
4033 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004034
4035cleanup:
4036 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00004037 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004038 }
bellard81d09122004-07-14 17:21:37 +00004039}
4040
aliguori731b0362009-03-05 23:01:42 +00004041static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004042{
aliguori731b0362009-03-05 23:01:42 +00004043 Monitor *mon = opaque;
4044
Jan Kiszkac62313b2009-12-04 14:05:29 +01004045 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004046}
4047
Luiz Capitulino09069b12010-02-04 18:10:06 -02004048static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4049{
4050 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4051 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4052}
4053
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004054/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004055 * Argument validation rules:
4056 *
4057 * 1. The argument must exist in cmd_args qdict
4058 * 2. The argument type must be the expected one
4059 *
4060 * Special case: If the argument doesn't exist in cmd_args and
4061 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4062 * checking is skipped for it.
4063 */
4064static int check_client_args_type(const QDict *client_args,
4065 const QDict *cmd_args, int flags)
4066{
4067 const QDictEntry *ent;
4068
4069 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4070 QObject *obj;
4071 QString *arg_type;
4072 const QObject *client_arg = qdict_entry_value(ent);
4073 const char *client_arg_name = qdict_entry_key(ent);
4074
4075 obj = qdict_get(cmd_args, client_arg_name);
4076 if (!obj) {
4077 if (flags & QMP_ACCEPT_UNKNOWNS) {
4078 /* handler accepts unknowns */
4079 continue;
4080 }
4081 /* client arg doesn't exist */
4082 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4083 return -1;
4084 }
4085
4086 arg_type = qobject_to_qstring(obj);
4087 assert(arg_type != NULL);
4088
4089 /* check if argument's type is correct */
4090 switch (qstring_get_str(arg_type)[0]) {
4091 case 'F':
4092 case 'B':
4093 case 's':
4094 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4095 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4096 "string");
4097 return -1;
4098 }
4099 break;
4100 case 'i':
4101 case 'l':
4102 case 'M':
4103 if (qobject_type(client_arg) != QTYPE_QINT) {
4104 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4105 "int");
4106 return -1;
4107 }
4108 break;
4109 case 'f':
4110 case 'T':
4111 if (qobject_type(client_arg) != QTYPE_QINT &&
4112 qobject_type(client_arg) != QTYPE_QFLOAT) {
4113 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4114 "number");
4115 return -1;
4116 }
4117 break;
4118 case 'b':
4119 case '-':
4120 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4121 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4122 "bool");
4123 return -1;
4124 }
4125 break;
4126 case 'O':
4127 assert(flags & QMP_ACCEPT_UNKNOWNS);
4128 break;
4129 case '/':
4130 case '.':
4131 /*
4132 * These types are not supported by QMP and thus are not
4133 * handled here. Fall through.
4134 */
4135 default:
4136 abort();
4137 }
4138 }
4139
4140 return 0;
4141}
4142
4143/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004144 * - Check if the client has passed all mandatory args
4145 * - Set special flags for argument validation
4146 */
4147static int check_mandatory_args(const QDict *cmd_args,
4148 const QDict *client_args, int *flags)
4149{
4150 const QDictEntry *ent;
4151
4152 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4153 const char *cmd_arg_name = qdict_entry_key(ent);
4154 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4155 assert(type != NULL);
4156
4157 if (qstring_get_str(type)[0] == 'O') {
4158 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4159 *flags |= QMP_ACCEPT_UNKNOWNS;
4160 } else if (qstring_get_str(type)[0] != '-' &&
4161 qstring_get_str(type)[1] != '?' &&
4162 !qdict_haskey(client_args, cmd_arg_name)) {
4163 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4164 return -1;
4165 }
4166 }
4167
4168 return 0;
4169}
4170
4171static QDict *qdict_from_args_type(const char *args_type)
4172{
4173 int i;
4174 QDict *qdict;
4175 QString *key, *type, *cur_qs;
4176
4177 assert(args_type != NULL);
4178
4179 qdict = qdict_new();
4180
4181 if (args_type == NULL || args_type[0] == '\0') {
4182 /* no args, empty qdict */
4183 goto out;
4184 }
4185
4186 key = qstring_new();
4187 type = qstring_new();
4188
4189 cur_qs = key;
4190
4191 for (i = 0;; i++) {
4192 switch (args_type[i]) {
4193 case ',':
4194 case '\0':
4195 qdict_put(qdict, qstring_get_str(key), type);
4196 QDECREF(key);
4197 if (args_type[i] == '\0') {
4198 goto out;
4199 }
4200 type = qstring_new(); /* qdict has ref */
4201 cur_qs = key = qstring_new();
4202 break;
4203 case ':':
4204 cur_qs = type;
4205 break;
4206 default:
4207 qstring_append_chr(cur_qs, args_type[i]);
4208 break;
4209 }
4210 }
4211
4212out:
4213 return qdict;
4214}
4215
4216/*
4217 * Client argument checking rules:
4218 *
4219 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004220 * 2. Each argument provided by the client must be expected
4221 * 3. Each argument provided by the client must have the type expected
4222 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004223 */
4224static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4225{
4226 int flags, err;
4227 QDict *cmd_args;
4228
4229 cmd_args = qdict_from_args_type(cmd->args_type);
4230
4231 flags = 0;
4232 err = check_mandatory_args(cmd_args, client_args, &flags);
4233 if (err) {
4234 goto out;
4235 }
4236
Luiz Capitulino4af91932010-06-22 11:44:05 -03004237 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004238
4239out:
4240 QDECREF(cmd_args);
4241 return err;
4242}
4243
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004244/*
4245 * Input object checking rules
4246 *
4247 * 1. Input object must be a dict
4248 * 2. The "execute" key must exist
4249 * 3. The "execute" key must be a string
4250 * 4. If the "arguments" key exists, it must be a dict
4251 * 5. If the "id" key exists, it can be anything (ie. json-value)
4252 * 6. Any argument not listed above is considered invalid
4253 */
4254static QDict *qmp_check_input_obj(QObject *input_obj)
4255{
4256 const QDictEntry *ent;
4257 int has_exec_key = 0;
4258 QDict *input_dict;
4259
4260 if (qobject_type(input_obj) != QTYPE_QDICT) {
4261 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4262 return NULL;
4263 }
4264
4265 input_dict = qobject_to_qdict(input_obj);
4266
4267 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4268 const char *arg_name = qdict_entry_key(ent);
4269 const QObject *arg_obj = qdict_entry_value(ent);
4270
4271 if (!strcmp(arg_name, "execute")) {
4272 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4273 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4274 "string");
4275 return NULL;
4276 }
4277 has_exec_key = 1;
4278 } else if (!strcmp(arg_name, "arguments")) {
4279 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4280 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4281 "object");
4282 return NULL;
4283 }
4284 } else if (!strcmp(arg_name, "id")) {
4285 /* FIXME: check duplicated IDs for async commands */
4286 } else {
4287 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4288 return NULL;
4289 }
4290 }
4291
4292 if (!has_exec_key) {
4293 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4294 return NULL;
4295 }
4296
4297 return input_dict;
4298}
4299
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004300static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4301{
4302 int err;
4303 QObject *obj;
4304 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004305 const mon_cmd_t *cmd;
4306 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004307 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004308
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004309 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004310
4311 obj = json_parser_parse(tokens, NULL);
4312 if (!obj) {
4313 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004314 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004315 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004316 }
4317
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004318 input = qmp_check_input_obj(obj);
4319 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004320 qobject_decref(obj);
4321 goto err_out;
4322 }
4323
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004324 mon->mc->id = qdict_get(input, "id");
4325 qobject_incref(mon->mc->id);
4326
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004327 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004328 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004329 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004330 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004331 }
4332
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004333 /*
4334 * XXX: We need this special case until we get info handlers
4335 * converted into 'query-' commands
4336 */
4337 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004338 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004339 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004340 } else if (strstart(cmd_name, "query-", &info_item)) {
4341 cmd = monitor_find_command("info");
4342 qdict_put_obj(input, "arguments",
4343 qobject_from_jsonf("{ 'item': %s }", info_item));
4344 } else {
4345 cmd = monitor_find_command(cmd_name);
Jan Kiszkaa6c4d362010-06-28 18:27:47 +02004346 if (!cmd || !monitor_handler_ported(cmd)
4347 || monitor_cmd_user_only(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004348 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004349 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004350 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004351 }
4352
4353 obj = qdict_get(input, "arguments");
4354 if (!obj) {
4355 args = qdict_new();
4356 } else {
4357 args = qobject_to_qdict(obj);
4358 QINCREF(args);
4359 }
4360
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004361 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004362 if (err < 0) {
4363 goto err_out;
4364 }
4365
Adam Litke940cc302010-01-25 12:18:44 -06004366 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004367 err = qmp_async_cmd_handler(mon, cmd, args);
4368 if (err) {
4369 /* emit the error response */
4370 goto err_out;
4371 }
Adam Litke940cc302010-01-25 12:18:44 -06004372 } else {
4373 monitor_call_handler(mon, cmd, args);
4374 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004375
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004376 goto out;
4377
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004378err_out:
4379 monitor_protocol_emitter(mon, NULL);
4380out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004381 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004382 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004383}
4384
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004385/**
4386 * monitor_control_read(): Read and handle QMP input
4387 */
4388static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4389{
4390 Monitor *old_mon = cur_mon;
4391
4392 cur_mon = opaque;
4393
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004394 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004395
4396 cur_mon = old_mon;
4397}
4398
aliguori731b0362009-03-05 23:01:42 +00004399static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004400{
aliguori731b0362009-03-05 23:01:42 +00004401 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004402 int i;
aliguori376253e2009-03-05 23:01:23 +00004403
aliguori731b0362009-03-05 23:01:42 +00004404 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004405
aliguoricde76ee2009-03-05 23:01:51 +00004406 if (cur_mon->rs) {
4407 for (i = 0; i < size; i++)
4408 readline_handle_byte(cur_mon->rs, buf[i]);
4409 } else {
4410 if (size == 0 || buf[size - 1] != 0)
4411 monitor_printf(cur_mon, "corrupted command\n");
4412 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004413 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004414 }
aliguori731b0362009-03-05 23:01:42 +00004415
4416 cur_mon = old_mon;
4417}
aliguorid8f44602008-10-06 13:52:44 +00004418
aliguori376253e2009-03-05 23:01:23 +00004419static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004420{
aliguori731b0362009-03-05 23:01:42 +00004421 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004422 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004423 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004424}
4425
aliguoricde76ee2009-03-05 23:01:51 +00004426int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004427{
aliguoricde76ee2009-03-05 23:01:51 +00004428 if (!mon->rs)
4429 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004430 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004431 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004432}
4433
aliguori376253e2009-03-05 23:01:23 +00004434void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004435{
aliguoricde76ee2009-03-05 23:01:51 +00004436 if (!mon->rs)
4437 return;
aliguori731b0362009-03-05 23:01:42 +00004438 if (--mon->suspend_cnt == 0)
4439 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004440}
4441
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004442static QObject *get_qmp_greeting(void)
4443{
4444 QObject *ver;
4445
4446 do_info_version(NULL, &ver);
4447 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4448}
4449
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004450/**
4451 * monitor_control_event(): Print QMP gretting
4452 */
4453static void monitor_control_event(void *opaque, int event)
4454{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004455 QObject *data;
4456 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004457
Luiz Capitulino47116d12010-02-08 17:01:30 -02004458 switch (event) {
4459 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004460 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004461 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004462 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004463 monitor_json_emitter(mon, data);
4464 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004465 break;
4466 case CHR_EVENT_CLOSED:
4467 json_message_parser_destroy(&mon->mc->parser);
4468 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004469 }
4470}
4471
aliguori731b0362009-03-05 23:01:42 +00004472static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004473{
aliguori376253e2009-03-05 23:01:23 +00004474 Monitor *mon = opaque;
4475
aliguori2724b182009-03-05 23:01:47 +00004476 switch (event) {
4477 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004478 mon->mux_out = 0;
4479 if (mon->reset_seen) {
4480 readline_restart(mon->rs);
4481 monitor_resume(mon);
4482 monitor_flush(mon);
4483 } else {
4484 mon->suspend_cnt = 0;
4485 }
aliguori2724b182009-03-05 23:01:47 +00004486 break;
ths86e94de2007-01-05 22:01:59 +00004487
aliguori2724b182009-03-05 23:01:47 +00004488 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004489 if (mon->reset_seen) {
4490 if (mon->suspend_cnt == 0) {
4491 monitor_printf(mon, "\n");
4492 }
4493 monitor_flush(mon);
4494 monitor_suspend(mon);
4495 } else {
4496 mon->suspend_cnt++;
4497 }
4498 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004499 break;
4500
Amit Shahb6b8df52009-10-07 18:31:16 +05304501 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004502 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4503 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004504 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004505 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004506 }
4507 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004508 break;
4509 }
ths86e94de2007-01-05 22:01:59 +00004510}
4511
aliguori76655d62009-03-06 20:27:37 +00004512
4513/*
4514 * Local variables:
4515 * c-indent-level: 4
4516 * c-basic-offset: 4
4517 * tab-width: 8
4518 * End:
4519 */
4520
aliguori731b0362009-03-05 23:01:42 +00004521void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004522{
aliguori731b0362009-03-05 23:01:42 +00004523 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004524 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004525
4526 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004527 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004528 is_first_init = 0;
4529 }
aliguori87127162009-03-05 23:01:29 +00004530
4531 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004532
aliguori87127162009-03-05 23:01:29 +00004533 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004534 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004535 if (flags & MONITOR_USE_READLINE) {
4536 mon->rs = readline_init(mon, monitor_find_completion);
4537 monitor_read_command(mon, 0);
4538 }
aliguori87127162009-03-05 23:01:29 +00004539
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004540 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004541 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004542 /* Control mode requires special handlers */
4543 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4544 monitor_control_event, mon);
4545 } else {
4546 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4547 monitor_event, mon);
4548 }
aliguori87127162009-03-05 23:01:29 +00004549
Blue Swirl72cf2d42009-09-12 07:36:22 +00004550 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004551 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4552 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004553}
4554
aliguori376253e2009-03-05 23:01:23 +00004555static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004556{
aliguoribb5fc202009-03-05 23:01:15 +00004557 BlockDriverState *bs = opaque;
4558 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004559
aliguoribb5fc202009-03-05 23:01:15 +00004560 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004561 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004562 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004563 }
aliguori731b0362009-03-05 23:01:42 +00004564 if (mon->password_completion_cb)
4565 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004566
aliguori731b0362009-03-05 23:01:42 +00004567 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004568}
aliguoric0f4ce72009-03-05 23:01:01 +00004569
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004570int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4571 BlockDriverCompletionFunc *completion_cb,
4572 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004573{
aliguoricde76ee2009-03-05 23:01:51 +00004574 int err;
4575
aliguoribb5fc202009-03-05 23:01:15 +00004576 if (!bdrv_key_required(bs)) {
4577 if (completion_cb)
4578 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004579 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004580 }
aliguoric0f4ce72009-03-05 23:01:01 +00004581
Luiz Capitulino94171e12009-12-07 21:37:00 +01004582 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004583 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004584 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004585 }
4586
aliguori376253e2009-03-05 23:01:23 +00004587 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4588 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004589
aliguori731b0362009-03-05 23:01:42 +00004590 mon->password_completion_cb = completion_cb;
4591 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004592
aliguoricde76ee2009-03-05 23:01:51 +00004593 err = monitor_read_password(mon, bdrv_password_cb, bs);
4594
4595 if (err && completion_cb)
4596 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004597
4598 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004599}