blob: a4ab163e5e5857f0d850cfa8a81bb6db02151800 [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)
Jes Sorensendbc0c672010-10-21 17:15:47 +020081 * 'o' octets (aka bytes)
82 * user mode accepts an optional T, t, G, g, M, m, K, k
83 * suffix, which multiplies the value by 2^40 for
84 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
85 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010086 * 'T' double
87 * user mode accepts an optional ms, us, ns suffix,
88 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000089 * '/' optional gdb-like print format (like "/10x")
90 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030091 * '?' optional type (for all types, except '/')
92 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010093 * 'b' boolean
94 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -030095 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000096 *
97 */
98
Adam Litke940cc302010-01-25 12:18:44 -060099typedef struct MonitorCompletionData MonitorCompletionData;
100struct MonitorCompletionData {
101 Monitor *mon;
102 void (*user_print)(Monitor *mon, const QObject *data);
103};
104
Anthony Liguoric227f092009-10-01 16:12:16 -0500105typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000106 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000107 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000108 const char *params;
109 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300110 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300111 union {
112 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300113 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600114 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300115 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200116 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600117 int (*cmd_async)(Monitor *mon, const QDict *params,
118 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300119 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200120 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500121} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000122
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100123/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500124typedef struct mon_fd_t mon_fd_t;
125struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100126 char *name;
127 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500128 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100129};
130
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200131typedef struct MonitorControl {
132 QObject *id;
133 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200134 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200135} MonitorControl;
136
aliguori87127162009-03-05 23:01:29 +0000137struct Monitor {
138 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200139 int mux_out;
140 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000141 int flags;
142 int suspend_cnt;
143 uint8_t outbuf[1024];
144 int outbuf_index;
145 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200146 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000147 CPUState *mon_cpu;
148 BlockDriverCompletionFunc *password_completion_cb;
149 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200150#ifdef CONFIG_DEBUG_MONITOR
151 int print_calls_nr;
152#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200153 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500154 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000155 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000156};
157
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200158#ifdef CONFIG_DEBUG_MONITOR
159#define MON_DEBUG(fmt, ...) do { \
160 fprintf(stderr, "Monitor: "); \
161 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200162
163static inline void mon_print_count_inc(Monitor *mon)
164{
165 mon->print_calls_nr++;
166}
167
168static inline void mon_print_count_init(Monitor *mon)
169{
170 mon->print_calls_nr = 0;
171}
172
173static inline int mon_print_count_get(const Monitor *mon)
174{
175 return mon->print_calls_nr;
176}
177
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200178#else /* !CONFIG_DEBUG_MONITOR */
179#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200180static inline void mon_print_count_inc(Monitor *mon) { }
181static inline void mon_print_count_init(Monitor *mon) { }
182static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200183#endif /* CONFIG_DEBUG_MONITOR */
184
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300185/* QMP checker flags */
186#define QMP_ACCEPT_UNKNOWNS 1
187
Blue Swirl72cf2d42009-09-12 07:36:22 +0000188static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000189
Anthony Liguoric227f092009-10-01 16:12:16 -0500190static const mon_cmd_t mon_cmds[];
191static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000192
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300193static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300194static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300195
Markus Armbruster8631b602010-02-18 11:41:55 +0100196Monitor *cur_mon;
197Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000198
aliguori731b0362009-03-05 23:01:42 +0000199static void monitor_command_cb(Monitor *mon, const char *cmdline,
200 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000201
Luiz Capitulino09069b12010-02-04 18:10:06 -0200202static inline int qmp_cmd_mode(const Monitor *mon)
203{
204 return (mon->mc ? mon->mc->command_mode : 0);
205}
206
Luiz Capitulino418173c2009-11-26 22:58:51 -0200207/* Return true if in control mode, false otherwise */
208static inline int monitor_ctrl_mode(const Monitor *mon)
209{
210 return (mon->flags & MONITOR_USE_CONTROL);
211}
212
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100213/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
214int monitor_cur_is_qmp(void)
215{
216 return cur_mon && monitor_ctrl_mode(cur_mon);
217}
218
aliguori731b0362009-03-05 23:01:42 +0000219static void monitor_read_command(Monitor *mon, int show_prompt)
220{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200221 if (!mon->rs)
222 return;
223
aliguori731b0362009-03-05 23:01:42 +0000224 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
225 if (show_prompt)
226 readline_show_prompt(mon->rs);
227}
bellard6a00d602005-11-21 23:25:50 +0000228
aliguoricde76ee2009-03-05 23:01:51 +0000229static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
230 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000231{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100232 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100233 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100234 return -EINVAL;
235 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000236 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
237 /* prompt is printed on return from the command handler */
238 return 0;
239 } else {
240 monitor_printf(mon, "terminal does not support password prompting\n");
241 return -ENOTTY;
242 }
aliguoribb5fc202009-03-05 23:01:15 +0000243}
244
aliguori376253e2009-03-05 23:01:23 +0000245void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000246{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200247 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000248 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
249 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000250 }
251}
252
253/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000254static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000255{
ths60fe76f2007-12-16 03:02:09 +0000256 char c;
aliguori731b0362009-03-05 23:01:42 +0000257
bellard7e2515e2004-08-01 21:52:19 +0000258 for(;;) {
259 c = *str++;
260 if (c == '\0')
261 break;
bellard7ba12602006-07-14 20:26:42 +0000262 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000263 mon->outbuf[mon->outbuf_index++] = '\r';
264 mon->outbuf[mon->outbuf_index++] = c;
265 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
266 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000267 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000268 }
269}
270
aliguori376253e2009-03-05 23:01:23 +0000271void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000272{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200273 char buf[4096];
274
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200275 if (!mon)
276 return;
277
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200278 mon_print_count_inc(mon);
279
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200280 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200281 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200282 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200283
284 vsnprintf(buf, sizeof(buf), fmt, ap);
285 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000286}
287
aliguori376253e2009-03-05 23:01:23 +0000288void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000289{
290 va_list ap;
291 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000292 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000293 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000294}
295
aliguori376253e2009-03-05 23:01:23 +0000296void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000297{
298 int i;
299
300 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000301 switch (filename[i]) {
302 case ' ':
303 case '"':
304 case '\\':
305 monitor_printf(mon, "\\%c", filename[i]);
306 break;
307 case '\t':
308 monitor_printf(mon, "\\t");
309 break;
310 case '\r':
311 monitor_printf(mon, "\\r");
312 break;
313 case '\n':
314 monitor_printf(mon, "\\n");
315 break;
316 default:
317 monitor_printf(mon, "%c", filename[i]);
318 break;
319 }
thsfef30742006-12-22 14:11:32 +0000320 }
321}
322
Stefan Weil8b7968f2010-09-23 21:28:05 +0200323static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
324 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000325{
326 va_list ap;
327 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000328 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000329 va_end(ap);
330 return 0;
331}
332
Luiz Capitulino13c74252009-10-07 13:41:55 -0300333static void monitor_user_noop(Monitor *mon, const QObject *data) { }
334
Luiz Capitulino9e807212010-09-16 10:58:59 -0300335static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300336{
337 return cmd->user_print != NULL;
338}
339
Luiz Capitulino4903de02010-09-16 11:01:32 -0300340static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600341{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200342 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600343}
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
Daniel P. Berrange39eaab92010-06-07 15:42:31 +0100354 if (mon->flags & MONITOR_USE_PRETTY)
355 json = qobject_to_json_pretty(data);
356 else
357 json = qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200358 assert(json != NULL);
359
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360 qstring_append_chr(json, '\n');
361 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200362
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200363 QDECREF(json);
364}
365
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200366static void monitor_protocol_emitter(Monitor *mon, QObject *data)
367{
368 QDict *qmp;
369
370 qmp = qdict_new();
371
372 if (!monitor_has_error(mon)) {
373 /* success response */
374 if (data) {
375 qobject_incref(data);
376 qdict_put_obj(qmp, "return", data);
377 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200378 /* return an empty QDict by default */
379 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200380 }
381 } else {
382 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100383 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 qdict_put(qmp, "error", mon->error->error);
385 QINCREF(mon->error->error);
386 QDECREF(mon->error);
387 mon->error = NULL;
388 }
389
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200390 if (mon->mc->id) {
391 qdict_put_obj(qmp, "id", mon->mc->id);
392 mon->mc->id = NULL;
393 }
394
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200395 monitor_json_emitter(mon, QOBJECT(qmp));
396 QDECREF(qmp);
397}
398
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399static void timestamp_put(QDict *qdict)
400{
401 int err;
402 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000403 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200404
Blue Swirld08d6f02009-12-04 18:06:39 +0000405 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200406 if (err < 0)
407 return;
408
409 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
410 "'microseconds': %" PRId64 " }",
411 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200412 qdict_put_obj(qdict, "timestamp", obj);
413}
414
415/**
416 * monitor_protocol_event(): Generate a Monitor event
417 *
418 * Event-specific data can be emitted through the (optional) 'data' parameter.
419 */
420void monitor_protocol_event(MonitorEvent event, QObject *data)
421{
422 QDict *qmp;
423 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600424 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425
Blue Swirl242cd002009-12-04 18:05:45 +0000426 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200427
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200428 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000429 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200430 event_name = "SHUTDOWN";
431 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000432 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200433 event_name = "RESET";
434 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000435 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200436 event_name = "POWERDOWN";
437 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000438 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200439 event_name = "STOP";
440 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300441 case QEVENT_RESUME:
442 event_name = "RESUME";
443 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200444 case QEVENT_VNC_CONNECTED:
445 event_name = "VNC_CONNECTED";
446 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200447 case QEVENT_VNC_INITIALIZED:
448 event_name = "VNC_INITIALIZED";
449 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200450 case QEVENT_VNC_DISCONNECTED:
451 event_name = "VNC_DISCONNECTED";
452 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200453 case QEVENT_BLOCK_IO_ERROR:
454 event_name = "BLOCK_IO_ERROR";
455 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300456 case QEVENT_RTC_CHANGE:
457 event_name = "RTC_CHANGE";
458 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300459 case QEVENT_WATCHDOG:
460 event_name = "WATCHDOG";
461 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200462 default:
463 abort();
464 break;
465 }
466
467 qmp = qdict_new();
468 timestamp_put(qmp);
469 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200470 if (data) {
471 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200472 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200473 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200474
Adam Litkef039a562010-01-15 08:34:02 -0600475 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200476 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200477 monitor_json_emitter(mon, QOBJECT(qmp));
478 }
Adam Litkef039a562010-01-15 08:34:02 -0600479 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200480 QDECREF(qmp);
481}
482
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200483static int do_qmp_capabilities(Monitor *mon, const QDict *params,
484 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200485{
486 /* Will setup QMP capabilities in the future */
487 if (monitor_ctrl_mode(mon)) {
488 mon->mc->command_mode = 1;
489 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200490
491 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200492}
493
Luiz Capitulino0268d972010-10-22 10:08:02 -0200494static int mon_set_cpu(int cpu_index);
495static void handle_user_command(Monitor *mon, const char *cmdline);
496
497static int do_hmp_passthrough(Monitor *mon, const QDict *params,
498 QObject **ret_data)
499{
500 int ret = 0;
501 Monitor *old_mon, hmp;
502 CharDriverState mchar;
503
504 memset(&hmp, 0, sizeof(hmp));
505 qemu_chr_init_mem(&mchar);
506 hmp.chr = &mchar;
507
508 old_mon = cur_mon;
509 cur_mon = &hmp;
510
511 if (qdict_haskey(params, "cpu-index")) {
512 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
513 if (ret < 0) {
514 cur_mon = old_mon;
515 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
516 goto out;
517 }
518 }
519
520 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
521 cur_mon = old_mon;
522
523 if (qemu_chr_mem_osize(hmp.chr) > 0) {
524 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
525 }
526
527out:
528 qemu_chr_close_mem(hmp.chr);
529 return ret;
530}
531
bellard9dc39cb2004-03-14 21:38:27 +0000532static int compare_cmd(const char *name, const char *list)
533{
534 const char *p, *pstart;
535 int len;
536 len = strlen(name);
537 p = list;
538 for(;;) {
539 pstart = p;
540 p = strchr(p, '|');
541 if (!p)
542 p = pstart + strlen(pstart);
543 if ((p - pstart) == len && !memcmp(pstart, name, len))
544 return 1;
545 if (*p == '\0')
546 break;
547 p++;
548 }
549 return 0;
550}
551
Anthony Liguoric227f092009-10-01 16:12:16 -0500552static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000553 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000554{
Anthony Liguoric227f092009-10-01 16:12:16 -0500555 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000556
557 for(cmd = cmds; cmd->name != NULL; cmd++) {
558 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000559 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
560 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000561 }
562}
563
aliguori376253e2009-03-05 23:01:23 +0000564static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000565{
566 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000567 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000568 } else {
aliguori376253e2009-03-05 23:01:23 +0000569 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000570 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000571 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000572 monitor_printf(mon, "Log items (comma separated):\n");
573 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000574 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000575 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000576 }
577 }
bellard9dc39cb2004-03-14 21:38:27 +0000578 }
579}
580
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300581static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300582{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300583 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300584}
585
Prerna Saxena22890ab2010-06-24 17:04:53 +0530586#ifdef CONFIG_SIMPLE_TRACE
587static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
588{
589 const char *tp_name = qdict_get_str(qdict, "name");
590 bool new_state = qdict_get_bool(qdict, "option");
Blue Swirlf871d682010-10-13 19:14:29 +0000591 int ret = st_change_trace_event_state(tp_name, new_state);
592
593 if (!ret) {
594 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
595 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530596}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100597
598static void do_trace_file(Monitor *mon, const QDict *qdict)
599{
600 const char *op = qdict_get_try_str(qdict, "op");
601 const char *arg = qdict_get_try_str(qdict, "arg");
602
603 if (!op) {
604 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
605 } else if (!strcmp(op, "on")) {
606 st_set_trace_file_enabled(true);
607 } else if (!strcmp(op, "off")) {
608 st_set_trace_file_enabled(false);
609 } else if (!strcmp(op, "flush")) {
610 st_flush_trace_buffer();
611 } else if (!strcmp(op, "set")) {
612 if (arg) {
613 st_set_trace_file(arg);
614 }
615 } else {
616 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
617 help_cmd(mon, "trace-file");
618 }
619}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530620#endif
621
Adam Litke940cc302010-01-25 12:18:44 -0600622static void user_monitor_complete(void *opaque, QObject *ret_data)
623{
624 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
625
626 if (ret_data) {
627 data->user_print(data->mon, ret_data);
628 }
629 monitor_resume(data->mon);
630 qemu_free(data);
631}
632
633static void qmp_monitor_complete(void *opaque, QObject *ret_data)
634{
635 monitor_protocol_emitter(opaque, ret_data);
636}
637
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300638static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
639 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600640{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300641 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600642}
643
644static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
645{
646 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
647}
648
649static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
650 const QDict *params)
651{
652 int ret;
653
654 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
655 cb_data->mon = mon;
656 cb_data->user_print = cmd->user_print;
657 monitor_suspend(mon);
658 ret = cmd->mhandler.cmd_async(mon, params,
659 user_monitor_complete, cb_data);
660 if (ret < 0) {
661 monitor_resume(mon);
662 qemu_free(cb_data);
663 }
664}
665
666static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
667{
668 int ret;
669
670 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
671 cb_data->mon = mon;
672 cb_data->user_print = cmd->user_print;
673 monitor_suspend(mon);
674 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
675 if (ret < 0) {
676 monitor_resume(mon);
677 qemu_free(cb_data);
678 }
679}
680
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300681static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000682{
Anthony Liguoric227f092009-10-01 16:12:16 -0500683 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300684 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000685
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200686 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000687 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200688 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300689
690 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000691 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300692 break;
bellard9dc39cb2004-03-14 21:38:27 +0000693 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300694
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200695 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300696 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200697 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300698
Luiz Capitulino4903de02010-09-16 11:01:32 -0300699 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300700 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300701 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300702 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200703
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300704 cmd->mhandler.info_new(mon, &info_data);
705 if (info_data) {
706 cmd->user_print(mon, info_data);
707 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200708 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300709 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300710 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711 }
712
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300713 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300714
715help:
716 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000717}
718
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200719static void do_info_version_print(Monitor *mon, const QObject *data)
720{
721 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300722 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200723
724 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300725 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200726
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300727 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
728 qdict_get_int(qemu, "major"),
729 qdict_get_int(qemu, "minor"),
730 qdict_get_int(qemu, "micro"),
731 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200732}
733
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300734static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000735{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300736 const char *version = QEMU_VERSION;
737 int major = 0, minor = 0, micro = 0;
738 char *tmp;
739
740 major = strtol(version, &tmp, 10);
741 tmp++;
742 minor = strtol(tmp, &tmp, 10);
743 tmp++;
744 micro = strtol(tmp, &tmp, 10);
745
746 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
747 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000748}
749
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200750static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000751{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200752 QDict *qdict;
753
754 qdict = qobject_to_qdict(data);
755 if (qdict_size(qdict) == 0) {
756 return;
757 }
758
759 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
760}
761
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200762static void do_info_name(Monitor *mon, QObject **ret_data)
763{
764 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
765 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000766}
767
Luiz Capitulino1a728672009-12-10 17:15:56 -0200768static QObject *get_cmd_dict(const char *name)
769{
770 const char *p;
771
772 /* Remove '|' from some commands */
773 p = strchr(name, '|');
774 if (p) {
775 p++;
776 } else {
777 p = name;
778 }
779
780 return qobject_from_jsonf("{ 'name': %s }", p);
781}
782
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200783static void do_info_commands(Monitor *mon, QObject **ret_data)
784{
785 QList *cmd_list;
786 const mon_cmd_t *cmd;
787
788 cmd_list = qlist_new();
789
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300790 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300791 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200792 }
793
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300794 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300795 char buf[128];
796 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
797 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200798 }
799
800 *ret_data = QOBJECT(cmd_list);
801}
802
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200803static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000804{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200805 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
806}
807
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200808static void do_info_uuid(Monitor *mon, QObject **ret_data)
809{
810 char uuid[64];
811
812 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000813 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
814 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
815 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
816 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200817 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000818}
819
bellard6a00d602005-11-21 23:25:50 +0000820/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000821static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000822{
823 CPUState *env;
824
825 for(env = first_cpu; env != NULL; env = env->next_cpu) {
826 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000827 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000828 return 0;
829 }
830 }
831 return -1;
832}
833
pbrook9596ebb2007-11-18 01:44:38 +0000834static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000835{
aliguori731b0362009-03-05 23:01:42 +0000836 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000837 mon_set_cpu(0);
838 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300839 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000840 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000841}
842
aliguori376253e2009-03-05 23:01:23 +0000843static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000844{
bellard6a00d602005-11-21 23:25:50 +0000845 CPUState *env;
846 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000847#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000848 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000849 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000850#else
aliguori376253e2009-03-05 23:01:23 +0000851 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000852 0);
bellard9307c4c2004-04-04 12:57:25 +0000853#endif
854}
855
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300856static void print_cpu_iter(QObject *obj, void *opaque)
857{
858 QDict *cpu;
859 int active = ' ';
860 Monitor *mon = opaque;
861
862 assert(qobject_type(obj) == QTYPE_QDICT);
863 cpu = qobject_to_qdict(obj);
864
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200865 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300866 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200867 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300868
869 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
870
871#if defined(TARGET_I386)
872 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
873 (target_ulong) qdict_get_int(cpu, "pc"));
874#elif defined(TARGET_PPC)
875 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
876 (target_long) qdict_get_int(cpu, "nip"));
877#elif defined(TARGET_SPARC)
878 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
879 (target_long) qdict_get_int(cpu, "pc"));
880 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
881 (target_long) qdict_get_int(cpu, "npc"));
882#elif defined(TARGET_MIPS)
883 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
884 (target_long) qdict_get_int(cpu, "PC"));
885#endif
886
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200887 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200889 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890
891 monitor_printf(mon, "\n");
892}
893
894static void monitor_print_cpus(Monitor *mon, const QObject *data)
895{
896 QList *cpu_list;
897
898 assert(qobject_type(data) == QTYPE_QLIST);
899 cpu_list = qobject_to_qlist(data);
900 qlist_iter(cpu_list, print_cpu_iter, mon);
901}
902
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300903static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000904{
905 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300906 QList *cpu_list;
907
908 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000909
910 /* just to set the default cpu if not already done */
911 mon_get_cpu();
912
913 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200914 QDict *cpu;
915 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300916
Avi Kivity4c0960c2009-08-17 23:19:53 +0300917 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300918
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200919 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
920 env->cpu_index, env == mon->mon_cpu,
921 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200922
923 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300924
bellard6a00d602005-11-21 23:25:50 +0000925#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300926 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000927#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300928 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000929#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300930 qdict_put(cpu, "pc", qint_from_int(env->pc));
931 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000932#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300933 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000934#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300935
936 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000937 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300938
939 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000940}
941
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200942static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000943{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300944 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200945 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100946 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
947 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200948 return -1;
949 }
950 return 0;
bellard6a00d602005-11-21 23:25:50 +0000951}
952
aliguori376253e2009-03-05 23:01:23 +0000953static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000954{
aliguori376253e2009-03-05 23:01:23 +0000955 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000956}
957
aliguori376253e2009-03-05 23:01:23 +0000958static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000959{
960 int i;
bellard7e2515e2004-08-01 21:52:19 +0000961 const char *str;
ths3b46e622007-09-17 08:09:54 +0000962
aliguoricde76ee2009-03-05 23:01:51 +0000963 if (!mon->rs)
964 return;
bellard7e2515e2004-08-01 21:52:19 +0000965 i = 0;
966 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000967 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000968 if (!str)
969 break;
aliguori376253e2009-03-05 23:01:23 +0000970 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000971 i++;
bellardaa455482004-04-04 13:07:25 +0000972 }
973}
974
j_mayer76a66252007-03-07 08:32:30 +0000975#if defined(TARGET_PPC)
976/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000977static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000978{
979 CPUState *env;
980
981 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000982 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000983}
984#endif
985
Prerna Saxena22890ab2010-06-24 17:04:53 +0530986#if defined(CONFIG_SIMPLE_TRACE)
987static void do_info_trace(Monitor *mon)
988{
989 st_print_trace((FILE *)mon, &monitor_fprintf);
990}
991
992static void do_info_trace_events(Monitor *mon)
993{
994 st_print_trace_events((FILE *)mon, &monitor_fprintf);
995}
996#endif
997
Luiz Capitulinob223f352009-10-07 13:41:56 -0300998/**
999 * do_quit(): Quit QEMU execution
1000 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001001static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001002{
Luiz Capitulino39b59d22010-05-11 18:08:20 -03001003 monitor_suspend(mon);
1004 no_shutdown = 0;
1005 qemu_system_shutdown_request();
1006
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001007 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001008}
1009
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001010static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001011{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001012 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001013 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001014 return -1;
1015 }
aliguoribb5fc202009-03-05 23:01:15 +00001016
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001017 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001018}
1019
1020static void change_vnc_password_cb(Monitor *mon, const char *password,
1021 void *opaque)
1022{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001023 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001024 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001025}
1026
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001028{
ths70848512007-08-25 01:37:05 +00001029 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001030 strcmp(target, "password") == 0) {
1031 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001032 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001033 strncpy(password, arg, sizeof(password));
1034 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001035 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001036 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001037 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001038 }
ths70848512007-08-25 01:37:05 +00001039 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001040 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001041 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001042 return -1;
1043 }
ths70848512007-08-25 01:37:05 +00001044 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001045
1046 return 0;
thse25a5822007-08-25 01:36:20 +00001047}
1048
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001049/**
1050 * do_change(): Change a removable medium, or VNC configuration
1051 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001052static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001053{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001054 const char *device = qdict_get_str(qdict, "device");
1055 const char *target = qdict_get_str(qdict, "target");
1056 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001057 int ret;
1058
thse25a5822007-08-25 01:36:20 +00001059 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001060 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001061 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001062 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001063 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001064
1065 return ret;
thse25a5822007-08-25 01:36:20 +00001066}
1067
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001068static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001069{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001070 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001071 return 0;
bellard59a983b2004-03-17 23:17:16 +00001072}
1073
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001075{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001076 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001077}
1078
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001079static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001080{
1081 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001082 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001083
bellard9307c4c2004-04-04 12:57:25 +00001084 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001085 mask = 0;
1086 } else {
bellard9307c4c2004-04-04 12:57:25 +00001087 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001088 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001089 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001090 return;
1091 }
1092 }
1093 cpu_set_log(mask);
1094}
1095
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001096static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001097{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001098 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001099 if (!option || !strcmp(option, "on")) {
1100 singlestep = 1;
1101 } else if (!strcmp(option, "off")) {
1102 singlestep = 0;
1103 } else {
1104 monitor_printf(mon, "unexpected option %s\n", option);
1105 }
1106}
1107
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001108/**
1109 * do_stop(): Stop VM execution
1110 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001111static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001112{
1113 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001114 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001115}
1116
aliguoribb5fc202009-03-05 23:01:15 +00001117static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001118
aliguori376253e2009-03-05 23:01:23 +00001119struct bdrv_iterate_context {
1120 Monitor *mon;
1121 int err;
1122};
aliguoric0f4ce72009-03-05 23:01:01 +00001123
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001124/**
1125 * do_cont(): Resume emulation.
1126 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001127static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001128{
1129 struct bdrv_iterate_context context = { mon, 0 };
1130
Amit Shah8e848652010-07-27 15:49:19 +05301131 if (incoming_expected) {
1132 qerror_report(QERR_MIGRATION_EXPECTED);
1133 return -1;
1134 }
aliguori376253e2009-03-05 23:01:23 +00001135 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001136 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001137 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001138 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001139 return 0;
1140 } else {
1141 return -1;
1142 }
bellard8a7ddc32004-03-31 19:00:16 +00001143}
1144
aliguoribb5fc202009-03-05 23:01:15 +00001145static void bdrv_key_cb(void *opaque, int err)
1146{
aliguori376253e2009-03-05 23:01:23 +00001147 Monitor *mon = opaque;
1148
aliguoribb5fc202009-03-05 23:01:15 +00001149 /* another key was set successfully, retry to continue */
1150 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001151 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001152}
1153
1154static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1155{
aliguori376253e2009-03-05 23:01:23 +00001156 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001157
aliguori376253e2009-03-05 23:01:23 +00001158 if (!context->err && bdrv_key_required(bs)) {
1159 context->err = -EBUSY;
1160 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1161 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001162 }
1163}
1164
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001165static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001166{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001167 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001168 if (!device)
1169 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1170 if (gdbserver_start(device) < 0) {
1171 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1172 device);
1173 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001174 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001175 } else {
aliguori59030a82009-04-05 18:43:41 +00001176 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1177 device);
bellard8a7ddc32004-03-31 19:00:16 +00001178 }
1179}
1180
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001181static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001182{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001183 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001184 if (select_watchdog_action(action) == -1) {
1185 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1186 }
1187}
1188
aliguori376253e2009-03-05 23:01:23 +00001189static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001190{
aliguori376253e2009-03-05 23:01:23 +00001191 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001192 switch(c) {
1193 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001194 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001195 break;
1196 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001197 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001198 break;
1199 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001200 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001201 break;
1202 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001204 break;
1205 default:
1206 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001208 } else {
aliguori376253e2009-03-05 23:01:23 +00001209 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001210 }
1211 break;
1212 }
aliguori376253e2009-03-05 23:01:23 +00001213 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001214}
1215
aliguori376253e2009-03-05 23:01:23 +00001216static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001217 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001218{
bellard6a00d602005-11-21 23:25:50 +00001219 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001220 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001221 uint8_t buf[16];
1222 uint64_t v;
1223
1224 if (format == 'i') {
1225 int flags;
1226 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001227 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001228#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001229 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001230 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001231 } else if (wsize == 4) {
1232 flags = 0;
1233 } else {
bellard6a15fd12006-04-12 21:07:07 +00001234 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001235 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001236 if (env) {
1237#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001238 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001239 (env->segs[R_CS].flags & DESC_L_MASK))
1240 flags = 2;
1241 else
1242#endif
1243 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1244 flags = 1;
1245 }
bellard4c27ba22004-04-25 18:05:08 +00001246 }
1247#endif
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001249 return;
1250 }
1251
1252 len = wsize * count;
1253 if (wsize == 1)
1254 line_size = 8;
1255 else
1256 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001257 max_digits = 0;
1258
1259 switch(format) {
1260 case 'o':
1261 max_digits = (wsize * 8 + 2) / 3;
1262 break;
1263 default:
1264 case 'x':
1265 max_digits = (wsize * 8) / 4;
1266 break;
1267 case 'u':
1268 case 'd':
1269 max_digits = (wsize * 8 * 10 + 32) / 33;
1270 break;
1271 case 'c':
1272 wsize = 1;
1273 break;
1274 }
1275
1276 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001277 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001279 else
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001281 l = len;
1282 if (l > line_size)
1283 l = line_size;
1284 if (is_physical) {
1285 cpu_physical_memory_rw(addr, buf, l, 0);
1286 } else {
bellard6a00d602005-11-21 23:25:50 +00001287 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001288 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001290 break;
1291 }
bellard9307c4c2004-04-04 12:57:25 +00001292 }
ths5fafdf22007-09-16 21:08:06 +00001293 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001294 while (i < l) {
1295 switch(wsize) {
1296 default:
1297 case 1:
1298 v = ldub_raw(buf + i);
1299 break;
1300 case 2:
1301 v = lduw_raw(buf + i);
1302 break;
1303 case 4:
bellard92a31b12005-02-10 22:00:52 +00001304 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001305 break;
1306 case 8:
1307 v = ldq_raw(buf + i);
1308 break;
1309 }
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001311 switch(format) {
1312 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001317 break;
1318 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001320 break;
1321 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001322 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001323 break;
1324 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001325 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001326 break;
1327 }
1328 i += wsize;
1329 }
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001331 addr += l;
1332 len -= l;
1333 }
1334}
1335
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001337{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001338 int count = qdict_get_int(qdict, "count");
1339 int format = qdict_get_int(qdict, "format");
1340 int size = qdict_get_int(qdict, "size");
1341 target_long addr = qdict_get_int(qdict, "addr");
1342
aliguori376253e2009-03-05 23:01:23 +00001343 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001344}
1345
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001346static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001347{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001348 int count = qdict_get_int(qdict, "count");
1349 int format = qdict_get_int(qdict, "format");
1350 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001351 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001352
aliguori376253e2009-03-05 23:01:23 +00001353 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001354}
1355
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001357{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001358 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001359 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001360
blueswir17743e582007-09-24 18:39:04 +00001361#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001362 switch(format) {
1363 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001365 break;
1366 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001368 break;
1369 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001370 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001371 break;
1372 default:
1373 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001375 break;
1376 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001378 break;
1379 }
bellard92a31b12005-02-10 22:00:52 +00001380#else
1381 switch(format) {
1382 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001384 break;
1385 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001387 break;
1388 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001390 break;
1391 default:
1392 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001393 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001394 break;
1395 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001397 break;
1398 }
1399#endif
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001401}
1402
Luiz Capitulino98696222010-02-10 23:49:58 -02001403static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001404{
1405 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001406 uint32_t size = qdict_get_int(qdict, "size");
1407 const char *filename = qdict_get_str(qdict, "filename");
1408 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001409 uint32_t l;
1410 CPUState *env;
1411 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001412 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001413
1414 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001415
1416 f = fopen(filename, "wb");
1417 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001418 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001419 return -1;
bellardb371dc52007-01-03 15:20:39 +00001420 }
1421 while (size != 0) {
1422 l = sizeof(buf);
1423 if (l > size)
1424 l = size;
1425 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001426 if (fwrite(buf, 1, l, f) != l) {
1427 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1428 goto exit;
1429 }
bellardb371dc52007-01-03 15:20:39 +00001430 addr += l;
1431 size -= l;
1432 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001433
1434 ret = 0;
1435
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001436exit:
bellardb371dc52007-01-03 15:20:39 +00001437 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001438 return ret;
bellardb371dc52007-01-03 15:20:39 +00001439}
1440
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001441static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001442 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001443{
1444 FILE *f;
1445 uint32_t l;
1446 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001447 uint32_t size = qdict_get_int(qdict, "size");
1448 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001449 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001450 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001451
1452 f = fopen(filename, "wb");
1453 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001454 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001455 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001456 }
1457 while (size != 0) {
1458 l = sizeof(buf);
1459 if (l > size)
1460 l = size;
1461 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001462 if (fwrite(buf, 1, l, f) != l) {
1463 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1464 goto exit;
1465 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001466 fflush(f);
1467 addr += l;
1468 size -= l;
1469 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001470
1471 ret = 0;
1472
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001473exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001474 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001475 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001476}
1477
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001478static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001479{
1480 uint32_t addr;
1481 uint8_t buf[1];
1482 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001483 uint32_t start = qdict_get_int(qdict, "start");
1484 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001485
1486 sum = 0;
1487 for(addr = start; addr < (start + size); addr++) {
1488 cpu_physical_memory_rw(addr, buf, 1, 0);
1489 /* BSD sum algorithm ('sum' Unix command) */
1490 sum = (sum >> 1) | (sum << 15);
1491 sum += buf[0];
1492 }
aliguori376253e2009-03-05 23:01:23 +00001493 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001494}
1495
bellarda3a91a32004-06-04 11:06:21 +00001496typedef struct {
1497 int keycode;
1498 const char *name;
1499} KeyDef;
1500
1501static const KeyDef key_defs[] = {
1502 { 0x2a, "shift" },
1503 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001504
bellarda3a91a32004-06-04 11:06:21 +00001505 { 0x38, "alt" },
1506 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001507 { 0x64, "altgr" },
1508 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001509 { 0x1d, "ctrl" },
1510 { 0x9d, "ctrl_r" },
1511
1512 { 0xdd, "menu" },
1513
1514 { 0x01, "esc" },
1515
1516 { 0x02, "1" },
1517 { 0x03, "2" },
1518 { 0x04, "3" },
1519 { 0x05, "4" },
1520 { 0x06, "5" },
1521 { 0x07, "6" },
1522 { 0x08, "7" },
1523 { 0x09, "8" },
1524 { 0x0a, "9" },
1525 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001526 { 0x0c, "minus" },
1527 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001528 { 0x0e, "backspace" },
1529
1530 { 0x0f, "tab" },
1531 { 0x10, "q" },
1532 { 0x11, "w" },
1533 { 0x12, "e" },
1534 { 0x13, "r" },
1535 { 0x14, "t" },
1536 { 0x15, "y" },
1537 { 0x16, "u" },
1538 { 0x17, "i" },
1539 { 0x18, "o" },
1540 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001541 { 0x1a, "bracket_left" },
1542 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001543 { 0x1c, "ret" },
1544
1545 { 0x1e, "a" },
1546 { 0x1f, "s" },
1547 { 0x20, "d" },
1548 { 0x21, "f" },
1549 { 0x22, "g" },
1550 { 0x23, "h" },
1551 { 0x24, "j" },
1552 { 0x25, "k" },
1553 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001554 { 0x27, "semicolon" },
1555 { 0x28, "apostrophe" },
1556 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001557
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001558 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001559 { 0x2c, "z" },
1560 { 0x2d, "x" },
1561 { 0x2e, "c" },
1562 { 0x2f, "v" },
1563 { 0x30, "b" },
1564 { 0x31, "n" },
1565 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001566 { 0x33, "comma" },
1567 { 0x34, "dot" },
1568 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001569
balrog4d3b6f62008-02-10 16:33:14 +00001570 { 0x37, "asterisk" },
1571
bellarda3a91a32004-06-04 11:06:21 +00001572 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001573 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001574 { 0x3b, "f1" },
1575 { 0x3c, "f2" },
1576 { 0x3d, "f3" },
1577 { 0x3e, "f4" },
1578 { 0x3f, "f5" },
1579 { 0x40, "f6" },
1580 { 0x41, "f7" },
1581 { 0x42, "f8" },
1582 { 0x43, "f9" },
1583 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001584 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001585 { 0x46, "scroll_lock" },
1586
bellard64866c32006-05-07 18:03:31 +00001587 { 0xb5, "kp_divide" },
1588 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001589 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001590 { 0x4e, "kp_add" },
1591 { 0x9c, "kp_enter" },
1592 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001593 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001594
1595 { 0x52, "kp_0" },
1596 { 0x4f, "kp_1" },
1597 { 0x50, "kp_2" },
1598 { 0x51, "kp_3" },
1599 { 0x4b, "kp_4" },
1600 { 0x4c, "kp_5" },
1601 { 0x4d, "kp_6" },
1602 { 0x47, "kp_7" },
1603 { 0x48, "kp_8" },
1604 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001605
bellarda3a91a32004-06-04 11:06:21 +00001606 { 0x56, "<" },
1607
1608 { 0x57, "f11" },
1609 { 0x58, "f12" },
1610
1611 { 0xb7, "print" },
1612
1613 { 0xc7, "home" },
1614 { 0xc9, "pgup" },
1615 { 0xd1, "pgdn" },
1616 { 0xcf, "end" },
1617
1618 { 0xcb, "left" },
1619 { 0xc8, "up" },
1620 { 0xd0, "down" },
1621 { 0xcd, "right" },
1622
1623 { 0xd2, "insert" },
1624 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001625#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1626 { 0xf0, "stop" },
1627 { 0xf1, "again" },
1628 { 0xf2, "props" },
1629 { 0xf3, "undo" },
1630 { 0xf4, "front" },
1631 { 0xf5, "copy" },
1632 { 0xf6, "open" },
1633 { 0xf7, "paste" },
1634 { 0xf8, "find" },
1635 { 0xf9, "cut" },
1636 { 0xfa, "lf" },
1637 { 0xfb, "help" },
1638 { 0xfc, "meta_l" },
1639 { 0xfd, "meta_r" },
1640 { 0xfe, "compose" },
1641#endif
bellarda3a91a32004-06-04 11:06:21 +00001642 { 0, NULL },
1643};
1644
1645static int get_keycode(const char *key)
1646{
1647 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001648 char *endp;
1649 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001650
1651 for(p = key_defs; p->name != NULL; p++) {
1652 if (!strcmp(key, p->name))
1653 return p->keycode;
1654 }
bellard64866c32006-05-07 18:03:31 +00001655 if (strstart(key, "0x", NULL)) {
1656 ret = strtoul(key, &endp, 0);
1657 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1658 return ret;
1659 }
bellarda3a91a32004-06-04 11:06:21 +00001660 return -1;
1661}
1662
balrogc8256f92008-06-08 22:45:01 +00001663#define MAX_KEYCODES 16
1664static uint8_t keycodes[MAX_KEYCODES];
1665static int nb_pending_keycodes;
1666static QEMUTimer *key_timer;
1667
1668static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001669{
balrogc8256f92008-06-08 22:45:01 +00001670 int keycode;
1671
1672 while (nb_pending_keycodes > 0) {
1673 nb_pending_keycodes--;
1674 keycode = keycodes[nb_pending_keycodes];
1675 if (keycode & 0x80)
1676 kbd_put_keycode(0xe0);
1677 kbd_put_keycode(keycode | 0x80);
1678 }
1679}
1680
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001681static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001682{
balrog3401c0d2008-06-04 10:05:59 +00001683 char keyname_buf[16];
1684 char *separator;
1685 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001686 const char *string = qdict_get_str(qdict, "string");
1687 int has_hold_time = qdict_haskey(qdict, "hold_time");
1688 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001689
balrogc8256f92008-06-08 22:45:01 +00001690 if (nb_pending_keycodes > 0) {
1691 qemu_del_timer(key_timer);
1692 release_keys(NULL);
1693 }
1694 if (!has_hold_time)
1695 hold_time = 100;
1696 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001697 while (1) {
1698 separator = strchr(string, '-');
1699 keyname_len = separator ? separator - string : strlen(string);
1700 if (keyname_len > 0) {
1701 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1702 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001703 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001704 return;
bellarda3a91a32004-06-04 11:06:21 +00001705 }
balrogc8256f92008-06-08 22:45:01 +00001706 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001707 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001708 return;
1709 }
1710 keyname_buf[keyname_len] = 0;
1711 keycode = get_keycode(keyname_buf);
1712 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001713 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001714 return;
1715 }
balrogc8256f92008-06-08 22:45:01 +00001716 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001717 }
balrog3401c0d2008-06-04 10:05:59 +00001718 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001719 break;
balrog3401c0d2008-06-04 10:05:59 +00001720 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001721 }
balrogc8256f92008-06-08 22:45:01 +00001722 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001723 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001724 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001725 keycode = keycodes[i];
1726 if (keycode & 0x80)
1727 kbd_put_keycode(0xe0);
1728 kbd_put_keycode(keycode & 0x7f);
1729 }
balrogc8256f92008-06-08 22:45:01 +00001730 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001731 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001732 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001733}
1734
bellard13224a82006-07-14 22:03:35 +00001735static int mouse_button_state;
1736
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001737static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001738{
1739 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001740 const char *dx_str = qdict_get_str(qdict, "dx_str");
1741 const char *dy_str = qdict_get_str(qdict, "dy_str");
1742 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001743 dx = strtol(dx_str, NULL, 0);
1744 dy = strtol(dy_str, NULL, 0);
1745 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001746 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001747 dz = strtol(dz_str, NULL, 0);
1748 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1749}
1750
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001751static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001752{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001753 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001754 mouse_button_state = button_state;
1755 kbd_mouse_event(0, 0, 0, mouse_button_state);
1756}
1757
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001758static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001759{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001760 int size = qdict_get_int(qdict, "size");
1761 int addr = qdict_get_int(qdict, "addr");
1762 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001763 uint32_t val;
1764 int suffix;
1765
1766 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001767 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001768 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001769 addr++;
1770 }
1771 addr &= 0xffff;
1772
1773 switch(size) {
1774 default:
1775 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001776 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001777 suffix = 'b';
1778 break;
1779 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001780 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001781 suffix = 'w';
1782 break;
1783 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001784 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001785 suffix = 'l';
1786 break;
1787 }
aliguori376253e2009-03-05 23:01:23 +00001788 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1789 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001790}
bellarda3a91a32004-06-04 11:06:21 +00001791
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001792static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001793{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001794 int size = qdict_get_int(qdict, "size");
1795 int addr = qdict_get_int(qdict, "addr");
1796 int val = qdict_get_int(qdict, "val");
1797
Jan Kiszkaf1147842009-07-14 10:20:11 +02001798 addr &= IOPORTS_MASK;
1799
1800 switch (size) {
1801 default:
1802 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001803 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001804 break;
1805 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001806 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001807 break;
1808 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001809 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001810 break;
1811 }
1812}
1813
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001814static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001815{
1816 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001817 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001818
Jan Kiszka76e30d02009-07-02 00:19:02 +02001819 res = qemu_boot_set(bootdevice);
1820 if (res == 0) {
1821 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1822 } else if (res > 0) {
1823 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001824 } else {
aliguori376253e2009-03-05 23:01:23 +00001825 monitor_printf(mon, "no function defined to set boot device list for "
1826 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001827 }
1828}
1829
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001830/**
1831 * do_system_reset(): Issue a machine reset
1832 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001833static int do_system_reset(Monitor *mon, const QDict *qdict,
1834 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001835{
1836 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001837 return 0;
bellarde4f90822004-06-20 12:35:44 +00001838}
1839
Luiz Capitulino43076662009-10-07 13:41:59 -03001840/**
1841 * do_system_powerdown(): Issue a machine powerdown
1842 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001843static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1844 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001845{
1846 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001847 return 0;
bellard34751872005-07-02 14:31:34 +00001848}
1849
bellardb86bda52004-09-18 19:32:46 +00001850#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001851static void print_pte(Monitor *mon, target_phys_addr_t addr,
1852 target_phys_addr_t pte,
1853 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001854{
Blue Swirld65aaf32010-12-11 18:56:24 +00001855#ifdef TARGET_X86_64
1856 if (addr & (1ULL << 47)) {
1857 addr |= -1LL << 48;
1858 }
1859#endif
1860 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1861 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001862 addr,
1863 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001864 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001865 pte & PG_GLOBAL_MASK ? 'G' : '-',
1866 pte & PG_PSE_MASK ? 'P' : '-',
1867 pte & PG_DIRTY_MASK ? 'D' : '-',
1868 pte & PG_ACCESSED_MASK ? 'A' : '-',
1869 pte & PG_PCD_MASK ? 'C' : '-',
1870 pte & PG_PWT_MASK ? 'T' : '-',
1871 pte & PG_USER_MASK ? 'U' : '-',
1872 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001873}
1874
Blue Swirld65aaf32010-12-11 18:56:24 +00001875static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001876{
bellardb86bda52004-09-18 19:32:46 +00001877 int l1, l2;
1878 uint32_t pgd, pde, pte;
1879
bellardb86bda52004-09-18 19:32:46 +00001880 pgd = env->cr[3] & ~0xfff;
1881 for(l1 = 0; l1 < 1024; l1++) {
1882 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1883 pde = le32_to_cpu(pde);
1884 if (pde & PG_PRESENT_MASK) {
1885 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001886 /* 4M pages */
1887 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001888 } else {
1889 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001890 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001891 (uint8_t *)&pte, 4);
1892 pte = le32_to_cpu(pte);
1893 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001894 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001895 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001896 ~0xfff);
1897 }
1898 }
1899 }
1900 }
1901 }
1902}
1903
Blue Swirld65aaf32010-12-11 18:56:24 +00001904static void tlb_info_pae32(Monitor *mon, CPUState *env)
1905{
1906 int l1, l2, l3;
1907 uint64_t pdpe, pde, pte;
1908 uint64_t pdp_addr, pd_addr, pt_addr;
1909
1910 pdp_addr = env->cr[3] & ~0x1f;
1911 for (l1 = 0; l1 < 4; l1++) {
1912 cpu_physical_memory_read(pdp_addr + l1 * 8, (uint8_t *)&pdpe, 8);
1913 pdpe = le64_to_cpu(pdpe);
1914 if (pdpe & PG_PRESENT_MASK) {
1915 pd_addr = pdpe & 0x3fffffffff000ULL;
1916 for (l2 = 0; l2 < 512; l2++) {
1917 cpu_physical_memory_read(pd_addr + l2 * 8,
1918 (uint8_t *)&pde, 8);
1919 pde = le64_to_cpu(pde);
1920 if (pde & PG_PRESENT_MASK) {
1921 if (pde & PG_PSE_MASK) {
1922 /* 2M pages with PAE, CR4.PSE is ignored */
1923 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1924 ~((target_phys_addr_t)(1 << 20) - 1));
1925 } else {
1926 pt_addr = pde & 0x3fffffffff000ULL;
1927 for (l3 = 0; l3 < 512; l3++) {
1928 cpu_physical_memory_read(pt_addr + l3 * 8,
1929 (uint8_t *)&pte, 8);
1930 pte = le64_to_cpu(pte);
1931 if (pte & PG_PRESENT_MASK) {
1932 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1933 + (l3 << 12),
1934 pte & ~PG_PSE_MASK,
1935 ~(target_phys_addr_t)0xfff);
1936 }
1937 }
1938 }
1939 }
1940 }
1941 }
1942 }
1943}
1944
1945#ifdef TARGET_X86_64
1946static void tlb_info_64(Monitor *mon, CPUState *env)
1947{
1948 uint64_t l1, l2, l3, l4;
1949 uint64_t pml4e, pdpe, pde, pte;
1950 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1951
1952 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1953 for (l1 = 0; l1 < 512; l1++) {
1954 cpu_physical_memory_read(pml4_addr + l1 * 8, (uint8_t *)&pml4e, 8);
1955 pml4e = le64_to_cpu(pml4e);
1956 if (pml4e & PG_PRESENT_MASK) {
1957 pdp_addr = pml4e & 0x3fffffffff000ULL;
1958 for (l2 = 0; l2 < 512; l2++) {
1959 cpu_physical_memory_read(pdp_addr + l2 * 8, (uint8_t *)&pdpe,
1960 8);
1961 pdpe = le64_to_cpu(pdpe);
1962 if (pdpe & PG_PRESENT_MASK) {
1963 if (pdpe & PG_PSE_MASK) {
1964 /* 1G pages, CR4.PSE is ignored */
1965 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1966 0x3ffffc0000000ULL);
1967 } else {
1968 pd_addr = pdpe & 0x3fffffffff000ULL;
1969 for (l3 = 0; l3 < 512; l3++) {
1970 cpu_physical_memory_read(pd_addr + l3 * 8,
1971 (uint8_t *)&pde, 8);
1972 pde = le64_to_cpu(pde);
1973 if (pde & PG_PRESENT_MASK) {
1974 if (pde & PG_PSE_MASK) {
1975 /* 2M pages, CR4.PSE is ignored */
1976 print_pte(mon, (l1 << 39) + (l2 << 30) +
1977 (l3 << 21), pde,
1978 0x3ffffffe00000ULL);
1979 } else {
1980 pt_addr = pde & 0x3fffffffff000ULL;
1981 for (l4 = 0; l4 < 512; l4++) {
1982 cpu_physical_memory_read(pt_addr
1983 + l4 * 8,
1984 (uint8_t *)&pte,
1985 8);
1986 pte = le64_to_cpu(pte);
1987 if (pte & PG_PRESENT_MASK) {
1988 print_pte(mon, (l1 << 39) +
1989 (l2 << 30) +
1990 (l3 << 21) + (l4 << 12),
1991 pte & ~PG_PSE_MASK,
1992 0x3fffffffff000ULL);
1993 }
1994 }
1995 }
1996 }
1997 }
1998 }
1999 }
2000 }
2001 }
2002 }
2003}
2004#endif
2005
2006static void tlb_info(Monitor *mon)
2007{
2008 CPUState *env;
2009
2010 env = mon_get_cpu();
2011
2012 if (!(env->cr[0] & CR0_PG_MASK)) {
2013 monitor_printf(mon, "PG disabled\n");
2014 return;
2015 }
2016 if (env->cr[4] & CR4_PAE_MASK) {
2017#ifdef TARGET_X86_64
2018 if (env->hflags & HF_LMA_MASK) {
2019 tlb_info_64(mon, env);
2020 } else
2021#endif
2022 {
2023 tlb_info_pae32(mon, env);
2024 }
2025 } else {
2026 tlb_info_32(mon, env);
2027 }
2028}
2029
aliguori376253e2009-03-05 23:01:23 +00002030static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00002031 uint32_t end, int prot)
2032{
bellard9746b152004-11-11 18:30:24 +00002033 int prot1;
2034 prot1 = *plast_prot;
2035 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002036 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00002037 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
2038 *pstart, end, end - *pstart,
2039 prot1 & PG_USER_MASK ? 'u' : '-',
2040 'r',
2041 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002042 }
2043 if (prot != 0)
2044 *pstart = end;
2045 else
2046 *pstart = -1;
2047 *plast_prot = prot;
2048 }
2049}
2050
aliguori376253e2009-03-05 23:01:23 +00002051static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00002052{
bellard6a00d602005-11-21 23:25:50 +00002053 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00002054 int l1, l2, prot, last_prot;
2055 uint32_t pgd, pde, pte, start, end;
2056
bellard6a00d602005-11-21 23:25:50 +00002057 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002058
bellardb86bda52004-09-18 19:32:46 +00002059 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00002060 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00002061 return;
2062 }
2063 pgd = env->cr[3] & ~0xfff;
2064 last_prot = 0;
2065 start = -1;
2066 for(l1 = 0; l1 < 1024; l1++) {
2067 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
2068 pde = le32_to_cpu(pde);
2069 end = l1 << 22;
2070 if (pde & PG_PRESENT_MASK) {
2071 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2072 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002073 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002074 } else {
2075 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00002076 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00002077 (uint8_t *)&pte, 4);
2078 pte = le32_to_cpu(pte);
2079 end = (l1 << 22) + (l2 << 12);
2080 if (pte & PG_PRESENT_MASK) {
2081 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2082 } else {
2083 prot = 0;
2084 }
aliguori376253e2009-03-05 23:01:23 +00002085 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002086 }
2087 }
2088 } else {
2089 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002090 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002091 }
2092 }
2093}
2094#endif
2095
aurel327c664e22009-03-03 06:12:22 +00002096#if defined(TARGET_SH4)
2097
aliguori376253e2009-03-05 23:01:23 +00002098static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002099{
aliguori376253e2009-03-05 23:01:23 +00002100 monitor_printf(mon, " tlb%i:\t"
2101 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2102 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2103 "dirty=%hhu writethrough=%hhu\n",
2104 idx,
2105 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2106 tlb->v, tlb->sh, tlb->c, tlb->pr,
2107 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002108}
2109
aliguori376253e2009-03-05 23:01:23 +00002110static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002111{
2112 CPUState *env = mon_get_cpu();
2113 int i;
2114
aliguori376253e2009-03-05 23:01:23 +00002115 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002116 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002117 print_tlb (mon, i, &env->itlb[i]);
2118 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002119 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002120 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002121}
2122
2123#endif
2124
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002125static void do_info_kvm_print(Monitor *mon, const QObject *data)
2126{
2127 QDict *qdict;
2128
2129 qdict = qobject_to_qdict(data);
2130
2131 monitor_printf(mon, "kvm support: ");
2132 if (qdict_get_bool(qdict, "present")) {
2133 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2134 "enabled" : "disabled");
2135 } else {
2136 monitor_printf(mon, "not compiled\n");
2137 }
2138}
2139
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002140static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002141{
2142#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002143 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2144 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002145#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002146 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002147#endif
2148}
2149
aliguori030ea372009-04-21 22:30:47 +00002150static void do_info_numa(Monitor *mon)
2151{
aliguorib28b6232009-04-22 20:20:29 +00002152 int i;
aliguori030ea372009-04-21 22:30:47 +00002153 CPUState *env;
2154
2155 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2156 for (i = 0; i < nb_numa_nodes; i++) {
2157 monitor_printf(mon, "node %d cpus:", i);
2158 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2159 if (env->numa_node == i) {
2160 monitor_printf(mon, " %d", env->cpu_index);
2161 }
2162 }
2163 monitor_printf(mon, "\n");
2164 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2165 node_mem[i] >> 20);
2166 }
2167}
2168
bellard5f1ce942006-02-08 22:40:15 +00002169#ifdef CONFIG_PROFILER
2170
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002171int64_t qemu_time;
2172int64_t dev_time;
2173
aliguori376253e2009-03-05 23:01:23 +00002174static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002175{
2176 int64_t total;
2177 total = qemu_time;
2178 if (total == 0)
2179 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002180 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002181 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002182 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002183 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002184 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002185 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002186}
2187#else
aliguori376253e2009-03-05 23:01:23 +00002188static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002189{
aliguori376253e2009-03-05 23:01:23 +00002190 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002191}
2192#endif
2193
bellardec36b692006-07-16 18:57:03 +00002194/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002195static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002196
aliguori376253e2009-03-05 23:01:23 +00002197static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002198{
2199 int i;
2200 CaptureState *s;
2201
2202 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002203 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002204 s->ops.info (s->opaque);
2205 }
2206}
2207
Blue Swirl23130862009-06-06 08:22:04 +00002208#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002209static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002210{
2211 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002212 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002213 CaptureState *s;
2214
2215 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2216 if (i == n) {
2217 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002218 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002219 qemu_free (s);
2220 return;
2221 }
2222 }
2223}
2224
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002225static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002226{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002227 const char *path = qdict_get_str(qdict, "path");
2228 int has_freq = qdict_haskey(qdict, "freq");
2229 int freq = qdict_get_try_int(qdict, "freq", -1);
2230 int has_bits = qdict_haskey(qdict, "bits");
2231 int bits = qdict_get_try_int(qdict, "bits", -1);
2232 int has_channels = qdict_haskey(qdict, "nchannels");
2233 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002234 CaptureState *s;
2235
2236 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002237
2238 freq = has_freq ? freq : 44100;
2239 bits = has_bits ? bits : 16;
2240 nchannels = has_channels ? nchannels : 2;
2241
2242 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002243 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002244 qemu_free (s);
2245 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002246 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002247}
2248#endif
2249
aurel32dc1c0b72008-04-27 23:52:12 +00002250#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002251static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002252{
2253 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002254 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002255
2256 for (env = first_cpu; env != NULL; env = env->next_cpu)
2257 if (env->cpu_index == cpu_index) {
2258 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2259 break;
2260 }
2261}
2262#endif
2263
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002264static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002265{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002266 QDict *qdict;
2267
2268 qdict = qobject_to_qdict(data);
2269
2270 monitor_printf(mon, "VM status: ");
2271 if (qdict_get_bool(qdict, "running")) {
2272 monitor_printf(mon, "running");
2273 if (qdict_get_bool(qdict, "singlestep")) {
2274 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002275 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002276 } else {
2277 monitor_printf(mon, "paused");
2278 }
2279
2280 monitor_printf(mon, "\n");
2281}
2282
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002283static void do_info_status(Monitor *mon, QObject **ret_data)
2284{
2285 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2286 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002287}
2288
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002289static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002290{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002291 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002292
aliguori76655d62009-03-06 20:27:37 +00002293 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002294 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002295 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002296 return acl;
2297}
aliguori76655d62009-03-06 20:27:37 +00002298
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002299static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002300{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002301 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002302 qemu_acl *acl = find_acl(mon, aclname);
2303 qemu_acl_entry *entry;
2304 int i = 0;
2305
2306 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002307 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002308 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002309 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002310 i++;
2311 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002312 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002313 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002314 }
2315}
2316
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002317static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002318{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002319 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002320 qemu_acl *acl = find_acl(mon, aclname);
2321
2322 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002323 qemu_acl_reset(acl);
2324 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002325 }
2326}
aliguori76655d62009-03-06 20:27:37 +00002327
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002328static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002329{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002330 const char *aclname = qdict_get_str(qdict, "aclname");
2331 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002332 qemu_acl *acl = find_acl(mon, aclname);
2333
2334 if (acl) {
2335 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002336 acl->defaultDeny = 0;
2337 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002338 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002339 acl->defaultDeny = 1;
2340 monitor_printf(mon, "acl: policy set to 'deny'\n");
2341 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002342 monitor_printf(mon, "acl: unknown policy '%s', "
2343 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002344 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002345 }
2346}
aliguori76655d62009-03-06 20:27:37 +00002347
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002348static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002349{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002350 const char *aclname = qdict_get_str(qdict, "aclname");
2351 const char *match = qdict_get_str(qdict, "match");
2352 const char *policy = qdict_get_str(qdict, "policy");
2353 int has_index = qdict_haskey(qdict, "index");
2354 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002355 qemu_acl *acl = find_acl(mon, aclname);
2356 int deny, ret;
2357
2358 if (acl) {
2359 if (strcmp(policy, "allow") == 0) {
2360 deny = 0;
2361 } else if (strcmp(policy, "deny") == 0) {
2362 deny = 1;
2363 } else {
2364 monitor_printf(mon, "acl: unknown policy '%s', "
2365 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002366 return;
2367 }
aliguori28a76be2009-03-06 20:27:40 +00002368 if (has_index)
2369 ret = qemu_acl_insert(acl, deny, match, index);
2370 else
2371 ret = qemu_acl_append(acl, deny, match);
2372 if (ret < 0)
2373 monitor_printf(mon, "acl: unable to add acl entry\n");
2374 else
2375 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002376 }
2377}
aliguori76655d62009-03-06 20:27:37 +00002378
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002379static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002380{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002381 const char *aclname = qdict_get_str(qdict, "aclname");
2382 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002383 qemu_acl *acl = find_acl(mon, aclname);
2384 int ret;
aliguori76655d62009-03-06 20:27:37 +00002385
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002386 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002387 ret = qemu_acl_remove(acl, match);
2388 if (ret < 0)
2389 monitor_printf(mon, "acl: no matching acl entry\n");
2390 else
2391 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002392 }
2393}
2394
Huang Ying79c4f6b2009-06-23 10:05:14 +08002395#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002396static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002397{
2398 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002399 int cpu_index = qdict_get_int(qdict, "cpu_index");
2400 int bank = qdict_get_int(qdict, "bank");
2401 uint64_t status = qdict_get_int(qdict, "status");
2402 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2403 uint64_t addr = qdict_get_int(qdict, "addr");
2404 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002405
2406 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2407 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2408 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2409 break;
2410 }
2411}
2412#endif
2413
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002414static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002415{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002416 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002417 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002418 int fd;
2419
2420 fd = qemu_chr_get_msgfd(mon->chr);
2421 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002422 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002423 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002424 }
2425
2426 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002427 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2428 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002429 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002430 }
2431
Blue Swirl72cf2d42009-09-12 07:36:22 +00002432 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002433 if (strcmp(monfd->name, fdname) != 0) {
2434 continue;
2435 }
2436
2437 close(monfd->fd);
2438 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002439 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002440 }
2441
Anthony Liguoric227f092009-10-01 16:12:16 -05002442 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002443 monfd->name = qemu_strdup(fdname);
2444 monfd->fd = fd;
2445
Blue Swirl72cf2d42009-09-12 07:36:22 +00002446 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002447 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002448}
2449
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002450static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002451{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002452 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002453 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002454
Blue Swirl72cf2d42009-09-12 07:36:22 +00002455 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002456 if (strcmp(monfd->name, fdname) != 0) {
2457 continue;
2458 }
2459
Blue Swirl72cf2d42009-09-12 07:36:22 +00002460 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002461 close(monfd->fd);
2462 qemu_free(monfd->name);
2463 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002464 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002465 }
2466
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002467 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002468 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002469}
2470
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002471static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002472{
2473 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002474 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002475
2476 vm_stop(0);
2477
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002478 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002479 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002480 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002481}
2482
Mark McLoughlin7768e042009-07-22 09:11:41 +01002483int monitor_get_fd(Monitor *mon, const char *fdname)
2484{
Anthony Liguoric227f092009-10-01 16:12:16 -05002485 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002486
Blue Swirl72cf2d42009-09-12 07:36:22 +00002487 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002488 int fd;
2489
2490 if (strcmp(monfd->name, fdname) != 0) {
2491 continue;
2492 }
2493
2494 fd = monfd->fd;
2495
2496 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002497 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002498 qemu_free(monfd->name);
2499 qemu_free(monfd);
2500
2501 return fd;
2502 }
2503
2504 return -1;
2505}
2506
Anthony Liguoric227f092009-10-01 16:12:16 -05002507static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002508#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002509 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002510};
2511
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002512/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002513static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 {
2515 .name = "version",
2516 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 .params = "",
2518 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002519 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002520 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 },
2522 {
2523 .name = "network",
2524 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 .params = "",
2526 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002527 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 },
2529 {
2530 .name = "chardev",
2531 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 .params = "",
2533 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002534 .user_print = qemu_chr_info_print,
2535 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 },
2537 {
2538 .name = "block",
2539 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 .params = "",
2541 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002542 .user_print = bdrv_info_print,
2543 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "blockstats",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002550 .user_print = bdrv_stats_print,
2551 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 },
2553 {
2554 .name = "registers",
2555 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 .params = "",
2557 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002558 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 },
2560 {
2561 .name = "cpus",
2562 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 .params = "",
2564 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002565 .user_print = monitor_print_cpus,
2566 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 },
2568 {
2569 .name = "history",
2570 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 .params = "",
2572 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002573 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 },
2575 {
2576 .name = "irq",
2577 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 .params = "",
2579 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002580 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002581 },
2582 {
2583 .name = "pic",
2584 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 .params = "",
2586 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002587 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 },
2589 {
2590 .name = "pci",
2591 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 .params = "",
2593 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002594 .user_print = do_pci_info_print,
2595 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 },
aurel327c664e22009-03-03 06:12:22 +00002597#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 {
2599 .name = "tlb",
2600 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 .params = "",
2602 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002603 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 },
aurel327c664e22009-03-03 06:12:22 +00002605#endif
2606#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 {
2608 .name = "mem",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002612 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
bellardb86bda52004-09-18 19:32:46 +00002614#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 {
2616 .name = "jit",
2617 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 .params = "",
2619 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002620 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 },
2622 {
2623 .name = "kvm",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002627 .user_print = do_info_kvm_print,
2628 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 },
2630 {
2631 .name = "numa",
2632 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 .params = "",
2634 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002635 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
2637 {
2638 .name = "usb",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002642 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
2644 {
2645 .name = "usbhost",
2646 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 .params = "",
2648 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002649 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 },
2651 {
2652 .name = "profile",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002656 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
2659 .name = "capture",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002663 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 },
2665 {
2666 .name = "snapshots",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002670 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
2673 .name = "status",
2674 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 .params = "",
2676 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002677 .user_print = do_info_status_print,
2678 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 },
2680 {
2681 .name = "pcmcia",
2682 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 .params = "",
2684 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002685 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 },
2687 {
2688 .name = "mice",
2689 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 .params = "",
2691 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002692 .user_print = do_info_mice_print,
2693 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 },
2695 {
2696 .name = "vnc",
2697 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 .params = "",
2699 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002700 .user_print = do_info_vnc_print,
2701 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 },
2703 {
2704 .name = "name",
2705 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 .params = "",
2707 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002708 .user_print = do_info_name_print,
2709 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 },
2711 {
2712 .name = "uuid",
2713 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 .params = "",
2715 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002716 .user_print = do_info_uuid_print,
2717 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 },
j_mayer76a66252007-03-07 08:32:30 +00002719#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 {
2721 .name = "cpustats",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002725 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 },
j_mayer76a66252007-03-07 08:32:30 +00002727#endif
blueswir131a60e22007-10-26 18:42:59 +00002728#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 {
2730 .name = "usernet",
2731 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 .params = "",
2733 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002734 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 },
blueswir131a60e22007-10-26 18:42:59 +00002736#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002737 {
2738 .name = "migrate",
2739 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 .params = "",
2741 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002742 .user_print = do_info_migrate_print,
2743 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 },
2745 {
2746 .name = "balloon",
2747 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 .params = "",
2749 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002750 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002751 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002752 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
2754 {
2755 .name = "qtree",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002759 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
2761 {
2762 .name = "qdm",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002766 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 },
2768 {
2769 .name = "roms",
2770 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .params = "",
2772 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002773 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302775#if defined(CONFIG_SIMPLE_TRACE)
2776 {
2777 .name = "trace",
2778 .args_type = "",
2779 .params = "",
2780 .help = "show current contents of trace buffer",
2781 .mhandler.info = do_info_trace,
2782 },
2783 {
2784 .name = "trace-events",
2785 .args_type = "",
2786 .params = "",
2787 .help = "show available trace-events & their state",
2788 .mhandler.info = do_info_trace_events,
2789 },
2790#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 {
2792 .name = NULL,
2793 },
bellard9dc39cb2004-03-14 21:38:27 +00002794};
2795
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002796static const mon_cmd_t qmp_cmds[] = {
2797#include "qmp-commands.h"
2798 { /* NULL */ },
2799};
2800
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002801static const mon_cmd_t qmp_query_cmds[] = {
2802 {
2803 .name = "version",
2804 .args_type = "",
2805 .params = "",
2806 .help = "show the version of QEMU",
2807 .user_print = do_info_version_print,
2808 .mhandler.info_new = do_info_version,
2809 },
2810 {
2811 .name = "commands",
2812 .args_type = "",
2813 .params = "",
2814 .help = "list QMP available commands",
2815 .user_print = monitor_user_noop,
2816 .mhandler.info_new = do_info_commands,
2817 },
2818 {
2819 .name = "chardev",
2820 .args_type = "",
2821 .params = "",
2822 .help = "show the character devices",
2823 .user_print = qemu_chr_info_print,
2824 .mhandler.info_new = qemu_chr_info,
2825 },
2826 {
2827 .name = "block",
2828 .args_type = "",
2829 .params = "",
2830 .help = "show the block devices",
2831 .user_print = bdrv_info_print,
2832 .mhandler.info_new = bdrv_info,
2833 },
2834 {
2835 .name = "blockstats",
2836 .args_type = "",
2837 .params = "",
2838 .help = "show block device statistics",
2839 .user_print = bdrv_stats_print,
2840 .mhandler.info_new = bdrv_info_stats,
2841 },
2842 {
2843 .name = "cpus",
2844 .args_type = "",
2845 .params = "",
2846 .help = "show infos for each CPU",
2847 .user_print = monitor_print_cpus,
2848 .mhandler.info_new = do_info_cpus,
2849 },
2850 {
2851 .name = "pci",
2852 .args_type = "",
2853 .params = "",
2854 .help = "show PCI info",
2855 .user_print = do_pci_info_print,
2856 .mhandler.info_new = do_pci_info,
2857 },
2858 {
2859 .name = "kvm",
2860 .args_type = "",
2861 .params = "",
2862 .help = "show KVM information",
2863 .user_print = do_info_kvm_print,
2864 .mhandler.info_new = do_info_kvm,
2865 },
2866 {
2867 .name = "status",
2868 .args_type = "",
2869 .params = "",
2870 .help = "show the current VM status (running|paused)",
2871 .user_print = do_info_status_print,
2872 .mhandler.info_new = do_info_status,
2873 },
2874 {
2875 .name = "mice",
2876 .args_type = "",
2877 .params = "",
2878 .help = "show which guest mouse is receiving events",
2879 .user_print = do_info_mice_print,
2880 .mhandler.info_new = do_info_mice,
2881 },
2882 {
2883 .name = "vnc",
2884 .args_type = "",
2885 .params = "",
2886 .help = "show the vnc server status",
2887 .user_print = do_info_vnc_print,
2888 .mhandler.info_new = do_info_vnc,
2889 },
2890 {
2891 .name = "name",
2892 .args_type = "",
2893 .params = "",
2894 .help = "show the current VM name",
2895 .user_print = do_info_name_print,
2896 .mhandler.info_new = do_info_name,
2897 },
2898 {
2899 .name = "uuid",
2900 .args_type = "",
2901 .params = "",
2902 .help = "show the current VM UUID",
2903 .user_print = do_info_uuid_print,
2904 .mhandler.info_new = do_info_uuid,
2905 },
2906 {
2907 .name = "migrate",
2908 .args_type = "",
2909 .params = "",
2910 .help = "show migration status",
2911 .user_print = do_info_migrate_print,
2912 .mhandler.info_new = do_info_migrate,
2913 },
2914 {
2915 .name = "balloon",
2916 .args_type = "",
2917 .params = "",
2918 .help = "show balloon information",
2919 .user_print = monitor_print_balloon,
2920 .mhandler.info_async = do_info_balloon,
2921 .flags = MONITOR_CMD_ASYNC,
2922 },
2923 { /* NULL */ },
2924};
2925
bellard9307c4c2004-04-04 12:57:25 +00002926/*******************************************************************/
2927
2928static const char *pch;
2929static jmp_buf expr_env;
2930
bellard92a31b12005-02-10 22:00:52 +00002931#define MD_TLONG 0
2932#define MD_I32 1
2933
bellard9307c4c2004-04-04 12:57:25 +00002934typedef struct MonitorDef {
2935 const char *name;
2936 int offset;
blueswir18662d652008-10-02 18:32:44 +00002937 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002938 int type;
bellard9307c4c2004-04-04 12:57:25 +00002939} MonitorDef;
2940
bellard57206fd2004-04-25 18:54:52 +00002941#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002942static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002943{
bellard6a00d602005-11-21 23:25:50 +00002944 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002945 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002946}
2947#endif
2948
bellarda541f292004-04-12 20:39:29 +00002949#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002950static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002951{
bellard6a00d602005-11-21 23:25:50 +00002952 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002953 unsigned int u;
2954 int i;
2955
2956 u = 0;
2957 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002958 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002959
2960 return u;
2961}
2962
blueswir18662d652008-10-02 18:32:44 +00002963static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002964{
bellard6a00d602005-11-21 23:25:50 +00002965 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002966 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002967}
2968
blueswir18662d652008-10-02 18:32:44 +00002969static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002970{
bellard6a00d602005-11-21 23:25:50 +00002971 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002972 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002973}
bellard9fddaa02004-05-21 12:59:32 +00002974
blueswir18662d652008-10-02 18:32:44 +00002975static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002976{
bellard6a00d602005-11-21 23:25:50 +00002977 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002978 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002979}
2980
blueswir18662d652008-10-02 18:32:44 +00002981static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002982{
bellard6a00d602005-11-21 23:25:50 +00002983 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002984 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002985}
2986
blueswir18662d652008-10-02 18:32:44 +00002987static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002988{
bellard6a00d602005-11-21 23:25:50 +00002989 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002990 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002991}
bellarda541f292004-04-12 20:39:29 +00002992#endif
2993
bellarde95c8d52004-09-30 22:22:08 +00002994#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002995#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002996static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002997{
bellard6a00d602005-11-21 23:25:50 +00002998 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002999
3000 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003001}
bellard7b936c02005-10-30 17:05:13 +00003002#endif
bellarde95c8d52004-09-30 22:22:08 +00003003
blueswir18662d652008-10-02 18:32:44 +00003004static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003005{
bellard6a00d602005-11-21 23:25:50 +00003006 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003007 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003008}
3009#endif
3010
blueswir18662d652008-10-02 18:32:44 +00003011static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003012#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003013
3014#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003015 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003016 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003017 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003018
bellard9307c4c2004-04-04 12:57:25 +00003019 { "eax", offsetof(CPUState, regs[0]) },
3020 { "ecx", offsetof(CPUState, regs[1]) },
3021 { "edx", offsetof(CPUState, regs[2]) },
3022 { "ebx", offsetof(CPUState, regs[3]) },
3023 { "esp|sp", offsetof(CPUState, regs[4]) },
3024 { "ebp|fp", offsetof(CPUState, regs[5]) },
3025 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003026 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003027#ifdef TARGET_X86_64
3028 { "r8", offsetof(CPUState, regs[8]) },
3029 { "r9", offsetof(CPUState, regs[9]) },
3030 { "r10", offsetof(CPUState, regs[10]) },
3031 { "r11", offsetof(CPUState, regs[11]) },
3032 { "r12", offsetof(CPUState, regs[12]) },
3033 { "r13", offsetof(CPUState, regs[13]) },
3034 { "r14", offsetof(CPUState, regs[14]) },
3035 { "r15", offsetof(CPUState, regs[15]) },
3036#endif
bellard9307c4c2004-04-04 12:57:25 +00003037 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003038 { "eip", offsetof(CPUState, eip) },
3039 SEG("cs", R_CS)
3040 SEG("ds", R_DS)
3041 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003042 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003043 SEG("fs", R_FS)
3044 SEG("gs", R_GS)
3045 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003046#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003047 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003048 { "r0", offsetof(CPUState, gpr[0]) },
3049 { "r1", offsetof(CPUState, gpr[1]) },
3050 { "r2", offsetof(CPUState, gpr[2]) },
3051 { "r3", offsetof(CPUState, gpr[3]) },
3052 { "r4", offsetof(CPUState, gpr[4]) },
3053 { "r5", offsetof(CPUState, gpr[5]) },
3054 { "r6", offsetof(CPUState, gpr[6]) },
3055 { "r7", offsetof(CPUState, gpr[7]) },
3056 { "r8", offsetof(CPUState, gpr[8]) },
3057 { "r9", offsetof(CPUState, gpr[9]) },
3058 { "r10", offsetof(CPUState, gpr[10]) },
3059 { "r11", offsetof(CPUState, gpr[11]) },
3060 { "r12", offsetof(CPUState, gpr[12]) },
3061 { "r13", offsetof(CPUState, gpr[13]) },
3062 { "r14", offsetof(CPUState, gpr[14]) },
3063 { "r15", offsetof(CPUState, gpr[15]) },
3064 { "r16", offsetof(CPUState, gpr[16]) },
3065 { "r17", offsetof(CPUState, gpr[17]) },
3066 { "r18", offsetof(CPUState, gpr[18]) },
3067 { "r19", offsetof(CPUState, gpr[19]) },
3068 { "r20", offsetof(CPUState, gpr[20]) },
3069 { "r21", offsetof(CPUState, gpr[21]) },
3070 { "r22", offsetof(CPUState, gpr[22]) },
3071 { "r23", offsetof(CPUState, gpr[23]) },
3072 { "r24", offsetof(CPUState, gpr[24]) },
3073 { "r25", offsetof(CPUState, gpr[25]) },
3074 { "r26", offsetof(CPUState, gpr[26]) },
3075 { "r27", offsetof(CPUState, gpr[27]) },
3076 { "r28", offsetof(CPUState, gpr[28]) },
3077 { "r29", offsetof(CPUState, gpr[29]) },
3078 { "r30", offsetof(CPUState, gpr[30]) },
3079 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003080 /* Floating point registers */
3081 { "f0", offsetof(CPUState, fpr[0]) },
3082 { "f1", offsetof(CPUState, fpr[1]) },
3083 { "f2", offsetof(CPUState, fpr[2]) },
3084 { "f3", offsetof(CPUState, fpr[3]) },
3085 { "f4", offsetof(CPUState, fpr[4]) },
3086 { "f5", offsetof(CPUState, fpr[5]) },
3087 { "f6", offsetof(CPUState, fpr[6]) },
3088 { "f7", offsetof(CPUState, fpr[7]) },
3089 { "f8", offsetof(CPUState, fpr[8]) },
3090 { "f9", offsetof(CPUState, fpr[9]) },
3091 { "f10", offsetof(CPUState, fpr[10]) },
3092 { "f11", offsetof(CPUState, fpr[11]) },
3093 { "f12", offsetof(CPUState, fpr[12]) },
3094 { "f13", offsetof(CPUState, fpr[13]) },
3095 { "f14", offsetof(CPUState, fpr[14]) },
3096 { "f15", offsetof(CPUState, fpr[15]) },
3097 { "f16", offsetof(CPUState, fpr[16]) },
3098 { "f17", offsetof(CPUState, fpr[17]) },
3099 { "f18", offsetof(CPUState, fpr[18]) },
3100 { "f19", offsetof(CPUState, fpr[19]) },
3101 { "f20", offsetof(CPUState, fpr[20]) },
3102 { "f21", offsetof(CPUState, fpr[21]) },
3103 { "f22", offsetof(CPUState, fpr[22]) },
3104 { "f23", offsetof(CPUState, fpr[23]) },
3105 { "f24", offsetof(CPUState, fpr[24]) },
3106 { "f25", offsetof(CPUState, fpr[25]) },
3107 { "f26", offsetof(CPUState, fpr[26]) },
3108 { "f27", offsetof(CPUState, fpr[27]) },
3109 { "f28", offsetof(CPUState, fpr[28]) },
3110 { "f29", offsetof(CPUState, fpr[29]) },
3111 { "f30", offsetof(CPUState, fpr[30]) },
3112 { "f31", offsetof(CPUState, fpr[31]) },
3113 { "fpscr", offsetof(CPUState, fpscr) },
3114 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003115 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003116 { "lr", offsetof(CPUState, lr) },
3117 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003118 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003119 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003120 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003121 { "msr", 0, &monitor_get_msr, },
3122 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003123 { "tbu", 0, &monitor_get_tbu, },
3124 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003125#if defined(TARGET_PPC64)
3126 /* Address space register */
3127 { "asr", offsetof(CPUState, asr) },
3128#endif
3129 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00003130 { "sdr1", offsetof(CPUState, sdr1) },
3131 { "sr0", offsetof(CPUState, sr[0]) },
3132 { "sr1", offsetof(CPUState, sr[1]) },
3133 { "sr2", offsetof(CPUState, sr[2]) },
3134 { "sr3", offsetof(CPUState, sr[3]) },
3135 { "sr4", offsetof(CPUState, sr[4]) },
3136 { "sr5", offsetof(CPUState, sr[5]) },
3137 { "sr6", offsetof(CPUState, sr[6]) },
3138 { "sr7", offsetof(CPUState, sr[7]) },
3139 { "sr8", offsetof(CPUState, sr[8]) },
3140 { "sr9", offsetof(CPUState, sr[9]) },
3141 { "sr10", offsetof(CPUState, sr[10]) },
3142 { "sr11", offsetof(CPUState, sr[11]) },
3143 { "sr12", offsetof(CPUState, sr[12]) },
3144 { "sr13", offsetof(CPUState, sr[13]) },
3145 { "sr14", offsetof(CPUState, sr[14]) },
3146 { "sr15", offsetof(CPUState, sr[15]) },
3147 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003148#elif defined(TARGET_SPARC)
3149 { "g0", offsetof(CPUState, gregs[0]) },
3150 { "g1", offsetof(CPUState, gregs[1]) },
3151 { "g2", offsetof(CPUState, gregs[2]) },
3152 { "g3", offsetof(CPUState, gregs[3]) },
3153 { "g4", offsetof(CPUState, gregs[4]) },
3154 { "g5", offsetof(CPUState, gregs[5]) },
3155 { "g6", offsetof(CPUState, gregs[6]) },
3156 { "g7", offsetof(CPUState, gregs[7]) },
3157 { "o0", 0, monitor_get_reg },
3158 { "o1", 1, monitor_get_reg },
3159 { "o2", 2, monitor_get_reg },
3160 { "o3", 3, monitor_get_reg },
3161 { "o4", 4, monitor_get_reg },
3162 { "o5", 5, monitor_get_reg },
3163 { "o6", 6, monitor_get_reg },
3164 { "o7", 7, monitor_get_reg },
3165 { "l0", 8, monitor_get_reg },
3166 { "l1", 9, monitor_get_reg },
3167 { "l2", 10, monitor_get_reg },
3168 { "l3", 11, monitor_get_reg },
3169 { "l4", 12, monitor_get_reg },
3170 { "l5", 13, monitor_get_reg },
3171 { "l6", 14, monitor_get_reg },
3172 { "l7", 15, monitor_get_reg },
3173 { "i0", 16, monitor_get_reg },
3174 { "i1", 17, monitor_get_reg },
3175 { "i2", 18, monitor_get_reg },
3176 { "i3", 19, monitor_get_reg },
3177 { "i4", 20, monitor_get_reg },
3178 { "i5", 21, monitor_get_reg },
3179 { "i6", 22, monitor_get_reg },
3180 { "i7", 23, monitor_get_reg },
3181 { "pc", offsetof(CPUState, pc) },
3182 { "npc", offsetof(CPUState, npc) },
3183 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003184#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003185 { "psr", 0, &monitor_get_psr, },
3186 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003187#endif
bellarde95c8d52004-09-30 22:22:08 +00003188 { "tbr", offsetof(CPUState, tbr) },
3189 { "fsr", offsetof(CPUState, fsr) },
3190 { "f0", offsetof(CPUState, fpr[0]) },
3191 { "f1", offsetof(CPUState, fpr[1]) },
3192 { "f2", offsetof(CPUState, fpr[2]) },
3193 { "f3", offsetof(CPUState, fpr[3]) },
3194 { "f4", offsetof(CPUState, fpr[4]) },
3195 { "f5", offsetof(CPUState, fpr[5]) },
3196 { "f6", offsetof(CPUState, fpr[6]) },
3197 { "f7", offsetof(CPUState, fpr[7]) },
3198 { "f8", offsetof(CPUState, fpr[8]) },
3199 { "f9", offsetof(CPUState, fpr[9]) },
3200 { "f10", offsetof(CPUState, fpr[10]) },
3201 { "f11", offsetof(CPUState, fpr[11]) },
3202 { "f12", offsetof(CPUState, fpr[12]) },
3203 { "f13", offsetof(CPUState, fpr[13]) },
3204 { "f14", offsetof(CPUState, fpr[14]) },
3205 { "f15", offsetof(CPUState, fpr[15]) },
3206 { "f16", offsetof(CPUState, fpr[16]) },
3207 { "f17", offsetof(CPUState, fpr[17]) },
3208 { "f18", offsetof(CPUState, fpr[18]) },
3209 { "f19", offsetof(CPUState, fpr[19]) },
3210 { "f20", offsetof(CPUState, fpr[20]) },
3211 { "f21", offsetof(CPUState, fpr[21]) },
3212 { "f22", offsetof(CPUState, fpr[22]) },
3213 { "f23", offsetof(CPUState, fpr[23]) },
3214 { "f24", offsetof(CPUState, fpr[24]) },
3215 { "f25", offsetof(CPUState, fpr[25]) },
3216 { "f26", offsetof(CPUState, fpr[26]) },
3217 { "f27", offsetof(CPUState, fpr[27]) },
3218 { "f28", offsetof(CPUState, fpr[28]) },
3219 { "f29", offsetof(CPUState, fpr[29]) },
3220 { "f30", offsetof(CPUState, fpr[30]) },
3221 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003222#ifdef TARGET_SPARC64
3223 { "f32", offsetof(CPUState, fpr[32]) },
3224 { "f34", offsetof(CPUState, fpr[34]) },
3225 { "f36", offsetof(CPUState, fpr[36]) },
3226 { "f38", offsetof(CPUState, fpr[38]) },
3227 { "f40", offsetof(CPUState, fpr[40]) },
3228 { "f42", offsetof(CPUState, fpr[42]) },
3229 { "f44", offsetof(CPUState, fpr[44]) },
3230 { "f46", offsetof(CPUState, fpr[46]) },
3231 { "f48", offsetof(CPUState, fpr[48]) },
3232 { "f50", offsetof(CPUState, fpr[50]) },
3233 { "f52", offsetof(CPUState, fpr[52]) },
3234 { "f54", offsetof(CPUState, fpr[54]) },
3235 { "f56", offsetof(CPUState, fpr[56]) },
3236 { "f58", offsetof(CPUState, fpr[58]) },
3237 { "f60", offsetof(CPUState, fpr[60]) },
3238 { "f62", offsetof(CPUState, fpr[62]) },
3239 { "asi", offsetof(CPUState, asi) },
3240 { "pstate", offsetof(CPUState, pstate) },
3241 { "cansave", offsetof(CPUState, cansave) },
3242 { "canrestore", offsetof(CPUState, canrestore) },
3243 { "otherwin", offsetof(CPUState, otherwin) },
3244 { "wstate", offsetof(CPUState, wstate) },
3245 { "cleanwin", offsetof(CPUState, cleanwin) },
3246 { "fprs", offsetof(CPUState, fprs) },
3247#endif
bellard9307c4c2004-04-04 12:57:25 +00003248#endif
3249 { NULL },
3250};
3251
aliguori376253e2009-03-05 23:01:23 +00003252static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003253{
aliguori376253e2009-03-05 23:01:23 +00003254 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003255 longjmp(expr_env, 1);
3256}
3257
Markus Armbruster09b94182010-01-20 13:07:30 +01003258/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003259static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003260{
blueswir18662d652008-10-02 18:32:44 +00003261 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003262 void *ptr;
3263
bellard9307c4c2004-04-04 12:57:25 +00003264 for(md = monitor_defs; md->name != NULL; md++) {
3265 if (compare_cmd(name, md->name)) {
3266 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003267 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003268 } else {
bellard6a00d602005-11-21 23:25:50 +00003269 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003270 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003271 switch(md->type) {
3272 case MD_I32:
3273 *pval = *(int32_t *)ptr;
3274 break;
3275 case MD_TLONG:
3276 *pval = *(target_long *)ptr;
3277 break;
3278 default:
3279 *pval = 0;
3280 break;
3281 }
bellard9307c4c2004-04-04 12:57:25 +00003282 }
3283 return 0;
3284 }
3285 }
3286 return -1;
3287}
3288
3289static void next(void)
3290{
Blue Swirl660f11b2009-07-31 21:16:51 +00003291 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003292 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003293 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003294 pch++;
3295 }
3296}
3297
aliguori376253e2009-03-05 23:01:23 +00003298static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003299
aliguori376253e2009-03-05 23:01:23 +00003300static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003301{
blueswir1c2efc952007-09-25 17:28:42 +00003302 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003303 char *p;
bellard6a00d602005-11-21 23:25:50 +00003304 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003305
3306 switch(*pch) {
3307 case '+':
3308 next();
aliguori376253e2009-03-05 23:01:23 +00003309 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003310 break;
3311 case '-':
3312 next();
aliguori376253e2009-03-05 23:01:23 +00003313 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003314 break;
3315 case '~':
3316 next();
aliguori376253e2009-03-05 23:01:23 +00003317 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003318 break;
3319 case '(':
3320 next();
aliguori376253e2009-03-05 23:01:23 +00003321 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003322 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003323 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003324 }
3325 next();
3326 break;
bellard81d09122004-07-14 17:21:37 +00003327 case '\'':
3328 pch++;
3329 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003330 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003331 n = *pch;
3332 pch++;
3333 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003334 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003335 next();
3336 break;
bellard9307c4c2004-04-04 12:57:25 +00003337 case '$':
3338 {
3339 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003340 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003341
bellard9307c4c2004-04-04 12:57:25 +00003342 pch++;
3343 q = buf;
3344 while ((*pch >= 'a' && *pch <= 'z') ||
3345 (*pch >= 'A' && *pch <= 'Z') ||
3346 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003347 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003348 if ((q - buf) < sizeof(buf) - 1)
3349 *q++ = *pch;
3350 pch++;
3351 }
blueswir1cd390082008-11-16 13:53:32 +00003352 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003353 pch++;
3354 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003355 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003356 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003357 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003358 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003359 }
3360 break;
3361 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003362 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003363 n = 0;
3364 break;
3365 default:
blueswir17743e582007-09-24 18:39:04 +00003366#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003367 n = strtoull(pch, &p, 0);
3368#else
bellard9307c4c2004-04-04 12:57:25 +00003369 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003370#endif
bellard9307c4c2004-04-04 12:57:25 +00003371 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003372 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003373 }
3374 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003375 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003376 pch++;
3377 break;
3378 }
3379 return n;
3380}
3381
3382
aliguori376253e2009-03-05 23:01:23 +00003383static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003384{
blueswir1c2efc952007-09-25 17:28:42 +00003385 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003386 int op;
ths3b46e622007-09-17 08:09:54 +00003387
aliguori376253e2009-03-05 23:01:23 +00003388 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003389 for(;;) {
3390 op = *pch;
3391 if (op != '*' && op != '/' && op != '%')
3392 break;
3393 next();
aliguori376253e2009-03-05 23:01:23 +00003394 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003395 switch(op) {
3396 default:
3397 case '*':
3398 val *= val2;
3399 break;
3400 case '/':
3401 case '%':
ths5fafdf22007-09-16 21:08:06 +00003402 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003403 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003404 if (op == '/')
3405 val /= val2;
3406 else
3407 val %= val2;
3408 break;
3409 }
3410 }
3411 return val;
3412}
3413
aliguori376253e2009-03-05 23:01:23 +00003414static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003415{
blueswir1c2efc952007-09-25 17:28:42 +00003416 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003417 int op;
bellard9307c4c2004-04-04 12:57:25 +00003418
aliguori376253e2009-03-05 23:01:23 +00003419 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003420 for(;;) {
3421 op = *pch;
3422 if (op != '&' && op != '|' && op != '^')
3423 break;
3424 next();
aliguori376253e2009-03-05 23:01:23 +00003425 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003426 switch(op) {
3427 default:
3428 case '&':
3429 val &= val2;
3430 break;
3431 case '|':
3432 val |= val2;
3433 break;
3434 case '^':
3435 val ^= val2;
3436 break;
3437 }
3438 }
3439 return val;
3440}
3441
aliguori376253e2009-03-05 23:01:23 +00003442static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003443{
blueswir1c2efc952007-09-25 17:28:42 +00003444 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003445 int op;
bellard9307c4c2004-04-04 12:57:25 +00003446
aliguori376253e2009-03-05 23:01:23 +00003447 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003448 for(;;) {
3449 op = *pch;
3450 if (op != '+' && op != '-')
3451 break;
3452 next();
aliguori376253e2009-03-05 23:01:23 +00003453 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003454 if (op == '+')
3455 val += val2;
3456 else
3457 val -= val2;
3458 }
3459 return val;
3460}
3461
aliguori376253e2009-03-05 23:01:23 +00003462static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003463{
3464 pch = *pp;
3465 if (setjmp(expr_env)) {
3466 *pp = pch;
3467 return -1;
3468 }
blueswir1cd390082008-11-16 13:53:32 +00003469 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003470 pch++;
aliguori376253e2009-03-05 23:01:23 +00003471 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003472 *pp = pch;
3473 return 0;
3474}
3475
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003476static int get_double(Monitor *mon, double *pval, const char **pp)
3477{
3478 const char *p = *pp;
3479 char *tailp;
3480 double d;
3481
3482 d = strtod(p, &tailp);
3483 if (tailp == p) {
3484 monitor_printf(mon, "Number expected\n");
3485 return -1;
3486 }
3487 if (d != d || d - d != 0) {
3488 /* NaN or infinity */
3489 monitor_printf(mon, "Bad number\n");
3490 return -1;
3491 }
3492 *pval = d;
3493 *pp = tailp;
3494 return 0;
3495}
3496
bellard9307c4c2004-04-04 12:57:25 +00003497static int get_str(char *buf, int buf_size, const char **pp)
3498{
3499 const char *p;
3500 char *q;
3501 int c;
3502
bellard81d09122004-07-14 17:21:37 +00003503 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003504 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003505 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003506 p++;
3507 if (*p == '\0') {
3508 fail:
bellard81d09122004-07-14 17:21:37 +00003509 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003510 *pp = p;
3511 return -1;
3512 }
bellard9307c4c2004-04-04 12:57:25 +00003513 if (*p == '\"') {
3514 p++;
3515 while (*p != '\0' && *p != '\"') {
3516 if (*p == '\\') {
3517 p++;
3518 c = *p++;
3519 switch(c) {
3520 case 'n':
3521 c = '\n';
3522 break;
3523 case 'r':
3524 c = '\r';
3525 break;
3526 case '\\':
3527 case '\'':
3528 case '\"':
3529 break;
3530 default:
3531 qemu_printf("unsupported escape code: '\\%c'\n", c);
3532 goto fail;
3533 }
3534 if ((q - buf) < buf_size - 1) {
3535 *q++ = c;
3536 }
3537 } else {
3538 if ((q - buf) < buf_size - 1) {
3539 *q++ = *p;
3540 }
3541 p++;
3542 }
3543 }
3544 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003545 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003546 goto fail;
3547 }
3548 p++;
3549 } else {
blueswir1cd390082008-11-16 13:53:32 +00003550 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003551 if ((q - buf) < buf_size - 1) {
3552 *q++ = *p;
3553 }
3554 p++;
3555 }
bellard9307c4c2004-04-04 12:57:25 +00003556 }
bellard81d09122004-07-14 17:21:37 +00003557 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003558 *pp = p;
3559 return 0;
3560}
3561
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003562/*
3563 * Store the command-name in cmdname, and return a pointer to
3564 * the remaining of the command string.
3565 */
3566static const char *get_command_name(const char *cmdline,
3567 char *cmdname, size_t nlen)
3568{
3569 size_t len;
3570 const char *p, *pstart;
3571
3572 p = cmdline;
3573 while (qemu_isspace(*p))
3574 p++;
3575 if (*p == '\0')
3576 return NULL;
3577 pstart = p;
3578 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3579 p++;
3580 len = p - pstart;
3581 if (len > nlen - 1)
3582 len = nlen - 1;
3583 memcpy(cmdname, pstart, len);
3584 cmdname[len] = '\0';
3585 return p;
3586}
3587
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003588/**
3589 * Read key of 'type' into 'key' and return the current
3590 * 'type' pointer.
3591 */
3592static char *key_get_info(const char *type, char **key)
3593{
3594 size_t len;
3595 char *p, *str;
3596
3597 if (*type == ',')
3598 type++;
3599
3600 p = strchr(type, ':');
3601 if (!p) {
3602 *key = NULL;
3603 return NULL;
3604 }
3605 len = p - type;
3606
3607 str = qemu_malloc(len + 1);
3608 memcpy(str, type, len);
3609 str[len] = '\0';
3610
3611 *key = str;
3612 return ++p;
3613}
3614
bellard9307c4c2004-04-04 12:57:25 +00003615static int default_fmt_format = 'x';
3616static int default_fmt_size = 4;
3617
3618#define MAX_ARGS 16
3619
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003620static int is_valid_option(const char *c, const char *typestr)
3621{
3622 char option[3];
3623
3624 option[0] = '-';
3625 option[1] = *c;
3626 option[2] = '\0';
3627
3628 typestr = strstr(typestr, option);
3629 return (typestr != NULL);
3630}
3631
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003632static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3633 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003634{
3635 const mon_cmd_t *cmd;
3636
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003637 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003638 if (compare_cmd(cmdname, cmd->name)) {
3639 return cmd;
3640 }
3641 }
3642
3643 return NULL;
3644}
3645
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003646static const mon_cmd_t *monitor_find_command(const char *cmdname)
3647{
3648 return search_dispatch_table(mon_cmds, cmdname);
3649}
3650
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003651static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3652{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003653 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003654}
3655
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003656static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3657{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003658 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003659}
3660
Anthony Liguoric227f092009-10-01 16:12:16 -05003661static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003662 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003663 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003664{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003665 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003666 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003667 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003668 char cmdname[256];
3669 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003670 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003671
3672#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003673 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003674#endif
ths3b46e622007-09-17 08:09:54 +00003675
bellard9307c4c2004-04-04 12:57:25 +00003676 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003677 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3678 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003679 return NULL;
ths3b46e622007-09-17 08:09:54 +00003680
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003681 cmd = monitor_find_command(cmdname);
3682 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003683 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003684 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003685 }
bellard9307c4c2004-04-04 12:57:25 +00003686
bellard9307c4c2004-04-04 12:57:25 +00003687 /* parse the parameters */
3688 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003689 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003690 typestr = key_get_info(typestr, &key);
3691 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003692 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003693 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003694 typestr++;
3695 switch(c) {
3696 case 'F':
bellard81d09122004-07-14 17:21:37 +00003697 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003698 case 's':
3699 {
3700 int ret;
ths3b46e622007-09-17 08:09:54 +00003701
blueswir1cd390082008-11-16 13:53:32 +00003702 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003703 p++;
3704 if (*typestr == '?') {
3705 typestr++;
3706 if (*p == '\0') {
3707 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003708 break;
bellard9307c4c2004-04-04 12:57:25 +00003709 }
3710 }
3711 ret = get_str(buf, sizeof(buf), &p);
3712 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003713 switch(c) {
3714 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003715 monitor_printf(mon, "%s: filename expected\n",
3716 cmdname);
bellard81d09122004-07-14 17:21:37 +00003717 break;
3718 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003719 monitor_printf(mon, "%s: block device name expected\n",
3720 cmdname);
bellard81d09122004-07-14 17:21:37 +00003721 break;
3722 default:
aliguori376253e2009-03-05 23:01:23 +00003723 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003724 break;
3725 }
bellard9307c4c2004-04-04 12:57:25 +00003726 goto fail;
3727 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003728 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003729 }
3730 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003731 case 'O':
3732 {
3733 QemuOptsList *opts_list;
3734 QemuOpts *opts;
3735
3736 opts_list = qemu_find_opts(key);
3737 if (!opts_list || opts_list->desc->name) {
3738 goto bad_type;
3739 }
3740 while (qemu_isspace(*p)) {
3741 p++;
3742 }
3743 if (!*p)
3744 break;
3745 if (get_str(buf, sizeof(buf), &p) < 0) {
3746 goto fail;
3747 }
3748 opts = qemu_opts_parse(opts_list, buf, 1);
3749 if (!opts) {
3750 goto fail;
3751 }
3752 qemu_opts_to_qdict(opts, qdict);
3753 qemu_opts_del(opts);
3754 }
3755 break;
bellard9307c4c2004-04-04 12:57:25 +00003756 case '/':
3757 {
3758 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003759
blueswir1cd390082008-11-16 13:53:32 +00003760 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003761 p++;
3762 if (*p == '/') {
3763 /* format found */
3764 p++;
3765 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003766 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003767 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003768 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003769 count = count * 10 + (*p - '0');
3770 p++;
3771 }
3772 }
3773 size = -1;
3774 format = -1;
3775 for(;;) {
3776 switch(*p) {
3777 case 'o':
3778 case 'd':
3779 case 'u':
3780 case 'x':
3781 case 'i':
3782 case 'c':
3783 format = *p++;
3784 break;
3785 case 'b':
3786 size = 1;
3787 p++;
3788 break;
3789 case 'h':
3790 size = 2;
3791 p++;
3792 break;
3793 case 'w':
3794 size = 4;
3795 p++;
3796 break;
3797 case 'g':
3798 case 'L':
3799 size = 8;
3800 p++;
3801 break;
3802 default:
3803 goto next;
3804 }
3805 }
3806 next:
blueswir1cd390082008-11-16 13:53:32 +00003807 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003808 monitor_printf(mon, "invalid char in format: '%c'\n",
3809 *p);
bellard9307c4c2004-04-04 12:57:25 +00003810 goto fail;
3811 }
bellard9307c4c2004-04-04 12:57:25 +00003812 if (format < 0)
3813 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003814 if (format != 'i') {
3815 /* for 'i', not specifying a size gives -1 as size */
3816 if (size < 0)
3817 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003818 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003819 }
bellard9307c4c2004-04-04 12:57:25 +00003820 default_fmt_format = format;
3821 } else {
3822 count = 1;
3823 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003824 if (format != 'i') {
3825 size = default_fmt_size;
3826 } else {
3827 size = -1;
3828 }
bellard9307c4c2004-04-04 12:57:25 +00003829 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003830 qdict_put(qdict, "count", qint_from_int(count));
3831 qdict_put(qdict, "format", qint_from_int(format));
3832 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003833 }
3834 break;
3835 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003836 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003837 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003838 {
blueswir1c2efc952007-09-25 17:28:42 +00003839 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003840
blueswir1cd390082008-11-16 13:53:32 +00003841 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003842 p++;
bellard34405572004-06-08 00:55:58 +00003843 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003844 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003845 if (*p == '\0') {
3846 typestr++;
3847 break;
3848 }
bellard34405572004-06-08 00:55:58 +00003849 } else {
3850 if (*p == '.') {
3851 p++;
blueswir1cd390082008-11-16 13:53:32 +00003852 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003853 p++;
bellard34405572004-06-08 00:55:58 +00003854 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003855 typestr++;
3856 break;
bellard34405572004-06-08 00:55:58 +00003857 }
3858 }
bellard13224a82006-07-14 22:03:35 +00003859 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003860 }
aliguori376253e2009-03-05 23:01:23 +00003861 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003862 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003863 /* Check if 'i' is greater than 32-bit */
3864 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3865 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3866 monitor_printf(mon, "integer is for 32-bit values\n");
3867 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003868 } else if (c == 'M') {
3869 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003870 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003871 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003872 }
3873 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003874 case 'o':
3875 {
3876 ssize_t val;
3877 char *end;
3878
3879 while (qemu_isspace(*p)) {
3880 p++;
3881 }
3882 if (*typestr == '?') {
3883 typestr++;
3884 if (*p == '\0') {
3885 break;
3886 }
3887 }
3888 val = strtosz(p, &end);
3889 if (val < 0) {
3890 monitor_printf(mon, "invalid size\n");
3891 goto fail;
3892 }
3893 qdict_put(qdict, key, qint_from_int(val));
3894 p = end;
3895 }
3896 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003897 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003898 {
3899 double val;
3900
3901 while (qemu_isspace(*p))
3902 p++;
3903 if (*typestr == '?') {
3904 typestr++;
3905 if (*p == '\0') {
3906 break;
3907 }
3908 }
3909 if (get_double(mon, &val, &p) < 0) {
3910 goto fail;
3911 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003912 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003913 switch (*p) {
3914 case 'm':
3915 val /= 1e3; p += 2; break;
3916 case 'u':
3917 val /= 1e6; p += 2; break;
3918 case 'n':
3919 val /= 1e9; p += 2; break;
3920 }
3921 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003922 if (*p && !qemu_isspace(*p)) {
3923 monitor_printf(mon, "Unknown unit suffix\n");
3924 goto fail;
3925 }
3926 qdict_put(qdict, key, qfloat_from_double(val));
3927 }
3928 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003929 case 'b':
3930 {
3931 const char *beg;
3932 int val;
3933
3934 while (qemu_isspace(*p)) {
3935 p++;
3936 }
3937 beg = p;
3938 while (qemu_isgraph(*p)) {
3939 p++;
3940 }
3941 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3942 val = 1;
3943 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3944 val = 0;
3945 } else {
3946 monitor_printf(mon, "Expected 'on' or 'off'\n");
3947 goto fail;
3948 }
3949 qdict_put(qdict, key, qbool_from_int(val));
3950 }
3951 break;
bellard9307c4c2004-04-04 12:57:25 +00003952 case '-':
3953 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003954 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003955 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003956 /* option */
ths3b46e622007-09-17 08:09:54 +00003957
bellard9307c4c2004-04-04 12:57:25 +00003958 c = *typestr++;
3959 if (c == '\0')
3960 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003961 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003962 p++;
bellard9307c4c2004-04-04 12:57:25 +00003963 if (*p == '-') {
3964 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003965 if(c != *p) {
3966 if(!is_valid_option(p, typestr)) {
3967
3968 monitor_printf(mon, "%s: unsupported option -%c\n",
3969 cmdname, *p);
3970 goto fail;
3971 } else {
3972 skip_key = 1;
3973 }
bellard9307c4c2004-04-04 12:57:25 +00003974 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003975 if(skip_key) {
3976 p = tmp;
3977 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003978 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003979 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003980 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003981 }
bellard9307c4c2004-04-04 12:57:25 +00003982 }
bellard9307c4c2004-04-04 12:57:25 +00003983 }
3984 break;
3985 default:
3986 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003987 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003988 goto fail;
3989 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003990 qemu_free(key);
3991 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003992 }
3993 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003994 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003995 p++;
3996 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003997 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3998 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003999 goto fail;
4000 }
4001
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004002 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004003
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004004fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004005 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004006 return NULL;
4007}
4008
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004009void monitor_set_error(Monitor *mon, QError *qerror)
4010{
4011 /* report only the first error */
4012 if (!mon->error) {
4013 mon->error = qerror;
4014 } else {
4015 MON_DEBUG("Additional error report at %s:%d\n",
4016 qerror->file, qerror->linenr);
4017 QDECREF(qerror);
4018 }
4019}
4020
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004021static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4022{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004023 if (monitor_ctrl_mode(mon)) {
4024 if (ret && !monitor_has_error(mon)) {
4025 /*
4026 * If it returns failure, it must have passed on error.
4027 *
4028 * Action: Report an internal error to the client if in QMP.
4029 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004030 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004031 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4032 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004033 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004034
4035#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004036 if (!ret && monitor_has_error(mon)) {
4037 /*
4038 * If it returns success, it must not have passed an error.
4039 *
4040 * Action: Report the passed error to the client.
4041 */
4042 MON_DEBUG("command '%s' returned success but passed an error\n",
4043 cmd->name);
4044 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02004045
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004046 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4047 /*
4048 * Handlers should not call Monitor print functions.
4049 *
4050 * Action: Ignore them in QMP.
4051 *
4052 * (XXX: we don't check any 'info' or 'query' command here
4053 * because the user print function _is_ called by do_info(), hence
4054 * we will trigger this check. This problem will go away when we
4055 * make 'query' commands real and kill do_info())
4056 */
4057 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4058 cmd->name, mon_print_count_get(mon));
4059 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004060#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004061 } else {
4062 assert(!monitor_has_error(mon));
4063 QDECREF(mon->error);
4064 mon->error = NULL;
4065 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004066}
4067
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004068static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004069{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004070 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004071 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004072
4073 qdict = qdict_new();
4074
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004075 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004076 if (!cmd)
4077 goto out;
4078
Luiz Capitulino4903de02010-09-16 11:01:32 -03004079 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004080 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004081 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004082 QObject *data = NULL;
4083
4084 /* XXX: ignores the error code */
4085 cmd->mhandler.cmd_new(mon, qdict, &data);
4086 assert(!monitor_has_error(mon));
4087 if (data) {
4088 cmd->user_print(mon, data);
4089 qobject_decref(data);
4090 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004091 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004092 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004093 }
4094
Luiz Capitulino13917be2009-10-07 13:41:54 -03004095out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004096 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004097}
4098
bellard81d09122004-07-14 17:21:37 +00004099static void cmd_completion(const char *name, const char *list)
4100{
4101 const char *p, *pstart;
4102 char cmd[128];
4103 int len;
4104
4105 p = list;
4106 for(;;) {
4107 pstart = p;
4108 p = strchr(p, '|');
4109 if (!p)
4110 p = pstart + strlen(pstart);
4111 len = p - pstart;
4112 if (len > sizeof(cmd) - 2)
4113 len = sizeof(cmd) - 2;
4114 memcpy(cmd, pstart, len);
4115 cmd[len] = '\0';
4116 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004117 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004118 }
4119 if (*p == '\0')
4120 break;
4121 p++;
4122 }
4123}
4124
4125static void file_completion(const char *input)
4126{
4127 DIR *ffs;
4128 struct dirent *d;
4129 char path[1024];
4130 char file[1024], file_prefix[1024];
4131 int input_path_len;
4132 const char *p;
4133
ths5fafdf22007-09-16 21:08:06 +00004134 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004135 if (!p) {
4136 input_path_len = 0;
4137 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004138 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004139 } else {
4140 input_path_len = p - input + 1;
4141 memcpy(path, input, input_path_len);
4142 if (input_path_len > sizeof(path) - 1)
4143 input_path_len = sizeof(path) - 1;
4144 path[input_path_len] = '\0';
4145 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4146 }
4147#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004148 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4149 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004150#endif
4151 ffs = opendir(path);
4152 if (!ffs)
4153 return;
4154 for(;;) {
4155 struct stat sb;
4156 d = readdir(ffs);
4157 if (!d)
4158 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004159
4160 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4161 continue;
4162 }
4163
bellard81d09122004-07-14 17:21:37 +00004164 if (strstart(d->d_name, file_prefix, NULL)) {
4165 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004166 if (input_path_len < sizeof(file))
4167 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4168 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004169 /* stat the file to find out if it's a directory.
4170 * In that case add a slash to speed up typing long paths
4171 */
4172 stat(file, &sb);
4173 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004174 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004175 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004176 }
4177 }
4178 closedir(ffs);
4179}
4180
aliguori51de9762009-03-05 23:00:43 +00004181static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004182{
aliguori51de9762009-03-05 23:00:43 +00004183 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004184 const char *input = opaque;
4185
4186 if (input[0] == '\0' ||
4187 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004188 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004189 }
4190}
4191
4192/* NOTE: this parser is an approximate form of the real command parser */
4193static void parse_cmdline(const char *cmdline,
4194 int *pnb_args, char **args)
4195{
4196 const char *p;
4197 int nb_args, ret;
4198 char buf[1024];
4199
4200 p = cmdline;
4201 nb_args = 0;
4202 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004203 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004204 p++;
4205 if (*p == '\0')
4206 break;
4207 if (nb_args >= MAX_ARGS)
4208 break;
4209 ret = get_str(buf, sizeof(buf), &p);
4210 args[nb_args] = qemu_strdup(buf);
4211 nb_args++;
4212 if (ret < 0)
4213 break;
4214 }
4215 *pnb_args = nb_args;
4216}
4217
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004218static const char *next_arg_type(const char *typestr)
4219{
4220 const char *p = strchr(typestr, ':');
4221 return (p != NULL ? ++p : typestr);
4222}
4223
aliguori4c36ba32009-03-05 23:01:37 +00004224static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004225{
4226 const char *cmdname;
4227 char *args[MAX_ARGS];
4228 int nb_args, i, len;
4229 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004230 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004231 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004232
4233 parse_cmdline(cmdline, &nb_args, args);
4234#ifdef DEBUG_COMPLETION
4235 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004236 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004237 }
4238#endif
4239
4240 /* if the line ends with a space, it means we want to complete the
4241 next arg */
4242 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004243 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004244 if (nb_args >= MAX_ARGS) {
4245 goto cleanup;
4246 }
bellard81d09122004-07-14 17:21:37 +00004247 args[nb_args++] = qemu_strdup("");
4248 }
4249 if (nb_args <= 1) {
4250 /* command completion */
4251 if (nb_args == 0)
4252 cmdname = "";
4253 else
4254 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004255 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004256 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004257 cmd_completion(cmdname, cmd->name);
4258 }
4259 } else {
4260 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004261 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4262 if (compare_cmd(args[0], cmd->name)) {
4263 break;
4264 }
bellard81d09122004-07-14 17:21:37 +00004265 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004266 if (!cmd->name) {
4267 goto cleanup;
4268 }
4269
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004270 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004271 for(i = 0; i < nb_args - 2; i++) {
4272 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004273 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004274 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004275 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004276 }
4277 }
4278 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004279 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004280 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004281 }
bellard81d09122004-07-14 17:21:37 +00004282 switch(*ptype) {
4283 case 'F':
4284 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004285 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004286 file_completion(str);
4287 break;
4288 case 'B':
4289 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004290 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004291 bdrv_iterate(block_completion_it, (void *)str);
4292 break;
bellard7fe48482004-10-09 18:08:01 +00004293 case 's':
4294 /* XXX: more generic ? */
4295 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004296 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004297 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4298 cmd_completion(str, cmd->name);
4299 }
bellard64866c32006-05-07 18:03:31 +00004300 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004301 char *sep = strrchr(str, '-');
4302 if (sep)
4303 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004304 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004305 for(key = key_defs; key->name != NULL; key++) {
4306 cmd_completion(str, key->name);
4307 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004308 } else if (!strcmp(cmd->name, "help|?")) {
4309 readline_set_completion_index(cur_mon->rs, strlen(str));
4310 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4311 cmd_completion(str, cmd->name);
4312 }
bellard7fe48482004-10-09 18:08:01 +00004313 }
4314 break;
bellard81d09122004-07-14 17:21:37 +00004315 default:
4316 break;
4317 }
4318 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004319
4320cleanup:
4321 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00004322 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004323 }
bellard81d09122004-07-14 17:21:37 +00004324}
4325
aliguori731b0362009-03-05 23:01:42 +00004326static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004327{
aliguori731b0362009-03-05 23:01:42 +00004328 Monitor *mon = opaque;
4329
Jan Kiszkac62313b2009-12-04 14:05:29 +01004330 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004331}
4332
Luiz Capitulino09069b12010-02-04 18:10:06 -02004333static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4334{
4335 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4336 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4337}
4338
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004339/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004340 * Argument validation rules:
4341 *
4342 * 1. The argument must exist in cmd_args qdict
4343 * 2. The argument type must be the expected one
4344 *
4345 * Special case: If the argument doesn't exist in cmd_args and
4346 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4347 * checking is skipped for it.
4348 */
4349static int check_client_args_type(const QDict *client_args,
4350 const QDict *cmd_args, int flags)
4351{
4352 const QDictEntry *ent;
4353
4354 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4355 QObject *obj;
4356 QString *arg_type;
4357 const QObject *client_arg = qdict_entry_value(ent);
4358 const char *client_arg_name = qdict_entry_key(ent);
4359
4360 obj = qdict_get(cmd_args, client_arg_name);
4361 if (!obj) {
4362 if (flags & QMP_ACCEPT_UNKNOWNS) {
4363 /* handler accepts unknowns */
4364 continue;
4365 }
4366 /* client arg doesn't exist */
4367 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4368 return -1;
4369 }
4370
4371 arg_type = qobject_to_qstring(obj);
4372 assert(arg_type != NULL);
4373
4374 /* check if argument's type is correct */
4375 switch (qstring_get_str(arg_type)[0]) {
4376 case 'F':
4377 case 'B':
4378 case 's':
4379 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4380 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4381 "string");
4382 return -1;
4383 }
4384 break;
4385 case 'i':
4386 case 'l':
4387 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004388 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004389 if (qobject_type(client_arg) != QTYPE_QINT) {
4390 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4391 "int");
4392 return -1;
4393 }
4394 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004395 case 'T':
4396 if (qobject_type(client_arg) != QTYPE_QINT &&
4397 qobject_type(client_arg) != QTYPE_QFLOAT) {
4398 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4399 "number");
4400 return -1;
4401 }
4402 break;
4403 case 'b':
4404 case '-':
4405 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4406 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4407 "bool");
4408 return -1;
4409 }
4410 break;
4411 case 'O':
4412 assert(flags & QMP_ACCEPT_UNKNOWNS);
4413 break;
4414 case '/':
4415 case '.':
4416 /*
4417 * These types are not supported by QMP and thus are not
4418 * handled here. Fall through.
4419 */
4420 default:
4421 abort();
4422 }
4423 }
4424
4425 return 0;
4426}
4427
4428/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004429 * - Check if the client has passed all mandatory args
4430 * - Set special flags for argument validation
4431 */
4432static int check_mandatory_args(const QDict *cmd_args,
4433 const QDict *client_args, int *flags)
4434{
4435 const QDictEntry *ent;
4436
4437 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4438 const char *cmd_arg_name = qdict_entry_key(ent);
4439 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4440 assert(type != NULL);
4441
4442 if (qstring_get_str(type)[0] == 'O') {
4443 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4444 *flags |= QMP_ACCEPT_UNKNOWNS;
4445 } else if (qstring_get_str(type)[0] != '-' &&
4446 qstring_get_str(type)[1] != '?' &&
4447 !qdict_haskey(client_args, cmd_arg_name)) {
4448 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4449 return -1;
4450 }
4451 }
4452
4453 return 0;
4454}
4455
4456static QDict *qdict_from_args_type(const char *args_type)
4457{
4458 int i;
4459 QDict *qdict;
4460 QString *key, *type, *cur_qs;
4461
4462 assert(args_type != NULL);
4463
4464 qdict = qdict_new();
4465
4466 if (args_type == NULL || args_type[0] == '\0') {
4467 /* no args, empty qdict */
4468 goto out;
4469 }
4470
4471 key = qstring_new();
4472 type = qstring_new();
4473
4474 cur_qs = key;
4475
4476 for (i = 0;; i++) {
4477 switch (args_type[i]) {
4478 case ',':
4479 case '\0':
4480 qdict_put(qdict, qstring_get_str(key), type);
4481 QDECREF(key);
4482 if (args_type[i] == '\0') {
4483 goto out;
4484 }
4485 type = qstring_new(); /* qdict has ref */
4486 cur_qs = key = qstring_new();
4487 break;
4488 case ':':
4489 cur_qs = type;
4490 break;
4491 default:
4492 qstring_append_chr(cur_qs, args_type[i]);
4493 break;
4494 }
4495 }
4496
4497out:
4498 return qdict;
4499}
4500
4501/*
4502 * Client argument checking rules:
4503 *
4504 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004505 * 2. Each argument provided by the client must be expected
4506 * 3. Each argument provided by the client must have the type expected
4507 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004508 */
4509static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4510{
4511 int flags, err;
4512 QDict *cmd_args;
4513
4514 cmd_args = qdict_from_args_type(cmd->args_type);
4515
4516 flags = 0;
4517 err = check_mandatory_args(cmd_args, client_args, &flags);
4518 if (err) {
4519 goto out;
4520 }
4521
Luiz Capitulino4af91932010-06-22 11:44:05 -03004522 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004523
4524out:
4525 QDECREF(cmd_args);
4526 return err;
4527}
4528
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004529/*
4530 * Input object checking rules
4531 *
4532 * 1. Input object must be a dict
4533 * 2. The "execute" key must exist
4534 * 3. The "execute" key must be a string
4535 * 4. If the "arguments" key exists, it must be a dict
4536 * 5. If the "id" key exists, it can be anything (ie. json-value)
4537 * 6. Any argument not listed above is considered invalid
4538 */
4539static QDict *qmp_check_input_obj(QObject *input_obj)
4540{
4541 const QDictEntry *ent;
4542 int has_exec_key = 0;
4543 QDict *input_dict;
4544
4545 if (qobject_type(input_obj) != QTYPE_QDICT) {
4546 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4547 return NULL;
4548 }
4549
4550 input_dict = qobject_to_qdict(input_obj);
4551
4552 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4553 const char *arg_name = qdict_entry_key(ent);
4554 const QObject *arg_obj = qdict_entry_value(ent);
4555
4556 if (!strcmp(arg_name, "execute")) {
4557 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4558 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4559 "string");
4560 return NULL;
4561 }
4562 has_exec_key = 1;
4563 } else if (!strcmp(arg_name, "arguments")) {
4564 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4565 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4566 "object");
4567 return NULL;
4568 }
4569 } else if (!strcmp(arg_name, "id")) {
4570 /* FIXME: check duplicated IDs for async commands */
4571 } else {
4572 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4573 return NULL;
4574 }
4575 }
4576
4577 if (!has_exec_key) {
4578 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4579 return NULL;
4580 }
4581
4582 return input_dict;
4583}
4584
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004585static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4586{
4587 QObject *ret_data = NULL;
4588
Luiz Capitulino4903de02010-09-16 11:01:32 -03004589 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004590 qmp_async_info_handler(mon, cmd);
4591 if (monitor_has_error(mon)) {
4592 monitor_protocol_emitter(mon, NULL);
4593 }
4594 } else {
4595 cmd->mhandler.info_new(mon, &ret_data);
4596 if (ret_data) {
4597 monitor_protocol_emitter(mon, ret_data);
4598 qobject_decref(ret_data);
4599 }
4600 }
4601}
4602
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004603static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4604 const QDict *params)
4605{
4606 int ret;
4607 QObject *data = NULL;
4608
4609 mon_print_count_init(mon);
4610
4611 ret = cmd->mhandler.cmd_new(mon, params, &data);
4612 handler_audit(mon, cmd, ret);
4613 monitor_protocol_emitter(mon, data);
4614 qobject_decref(data);
4615}
4616
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004617static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4618{
4619 int err;
4620 QObject *obj;
4621 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004622 const mon_cmd_t *cmd;
4623 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004624 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004625
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004626 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004627 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004628
4629 obj = json_parser_parse(tokens, NULL);
4630 if (!obj) {
4631 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004632 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004633 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004634 }
4635
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004636 input = qmp_check_input_obj(obj);
4637 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004638 qobject_decref(obj);
4639 goto err_out;
4640 }
4641
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004642 mon->mc->id = qdict_get(input, "id");
4643 qobject_incref(mon->mc->id);
4644
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004645 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004646 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004647 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004648 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004649 }
4650
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004651 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004652 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004653 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004654 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004655 }
4656
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004657 if (!cmd) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004658 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4659 goto err_out;
4660 }
4661
4662 obj = qdict_get(input, "arguments");
4663 if (!obj) {
4664 args = qdict_new();
4665 } else {
4666 args = qobject_to_qdict(obj);
4667 QINCREF(args);
4668 }
4669
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004670 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004671 if (err < 0) {
4672 goto err_out;
4673 }
4674
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004675 if (query_cmd) {
4676 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004677 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004678 err = qmp_async_cmd_handler(mon, cmd, args);
4679 if (err) {
4680 /* emit the error response */
4681 goto err_out;
4682 }
Adam Litke940cc302010-01-25 12:18:44 -06004683 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004684 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004685 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004686
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004687 goto out;
4688
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004689err_out:
4690 monitor_protocol_emitter(mon, NULL);
4691out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004692 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004693 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004694}
4695
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004696/**
4697 * monitor_control_read(): Read and handle QMP input
4698 */
4699static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4700{
4701 Monitor *old_mon = cur_mon;
4702
4703 cur_mon = opaque;
4704
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004705 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004706
4707 cur_mon = old_mon;
4708}
4709
aliguori731b0362009-03-05 23:01:42 +00004710static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004711{
aliguori731b0362009-03-05 23:01:42 +00004712 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004713 int i;
aliguori376253e2009-03-05 23:01:23 +00004714
aliguori731b0362009-03-05 23:01:42 +00004715 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004716
aliguoricde76ee2009-03-05 23:01:51 +00004717 if (cur_mon->rs) {
4718 for (i = 0; i < size; i++)
4719 readline_handle_byte(cur_mon->rs, buf[i]);
4720 } else {
4721 if (size == 0 || buf[size - 1] != 0)
4722 monitor_printf(cur_mon, "corrupted command\n");
4723 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004724 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004725 }
aliguori731b0362009-03-05 23:01:42 +00004726
4727 cur_mon = old_mon;
4728}
aliguorid8f44602008-10-06 13:52:44 +00004729
aliguori376253e2009-03-05 23:01:23 +00004730static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004731{
aliguori731b0362009-03-05 23:01:42 +00004732 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004733 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004734 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004735}
4736
aliguoricde76ee2009-03-05 23:01:51 +00004737int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004738{
aliguoricde76ee2009-03-05 23:01:51 +00004739 if (!mon->rs)
4740 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004741 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004742 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004743}
4744
aliguori376253e2009-03-05 23:01:23 +00004745void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004746{
aliguoricde76ee2009-03-05 23:01:51 +00004747 if (!mon->rs)
4748 return;
aliguori731b0362009-03-05 23:01:42 +00004749 if (--mon->suspend_cnt == 0)
4750 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004751}
4752
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004753static QObject *get_qmp_greeting(void)
4754{
4755 QObject *ver;
4756
4757 do_info_version(NULL, &ver);
4758 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4759}
4760
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004761/**
4762 * monitor_control_event(): Print QMP gretting
4763 */
4764static void monitor_control_event(void *opaque, int event)
4765{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004766 QObject *data;
4767 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004768
Luiz Capitulino47116d12010-02-08 17:01:30 -02004769 switch (event) {
4770 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004771 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004772 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004773 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004774 monitor_json_emitter(mon, data);
4775 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004776 break;
4777 case CHR_EVENT_CLOSED:
4778 json_message_parser_destroy(&mon->mc->parser);
4779 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004780 }
4781}
4782
aliguori731b0362009-03-05 23:01:42 +00004783static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004784{
aliguori376253e2009-03-05 23:01:23 +00004785 Monitor *mon = opaque;
4786
aliguori2724b182009-03-05 23:01:47 +00004787 switch (event) {
4788 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004789 mon->mux_out = 0;
4790 if (mon->reset_seen) {
4791 readline_restart(mon->rs);
4792 monitor_resume(mon);
4793 monitor_flush(mon);
4794 } else {
4795 mon->suspend_cnt = 0;
4796 }
aliguori2724b182009-03-05 23:01:47 +00004797 break;
ths86e94de2007-01-05 22:01:59 +00004798
aliguori2724b182009-03-05 23:01:47 +00004799 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004800 if (mon->reset_seen) {
4801 if (mon->suspend_cnt == 0) {
4802 monitor_printf(mon, "\n");
4803 }
4804 monitor_flush(mon);
4805 monitor_suspend(mon);
4806 } else {
4807 mon->suspend_cnt++;
4808 }
4809 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004810 break;
4811
Amit Shahb6b8df52009-10-07 18:31:16 +05304812 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004813 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4814 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004815 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004816 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004817 }
4818 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004819 break;
4820 }
ths86e94de2007-01-05 22:01:59 +00004821}
4822
aliguori76655d62009-03-06 20:27:37 +00004823
4824/*
4825 * Local variables:
4826 * c-indent-level: 4
4827 * c-basic-offset: 4
4828 * tab-width: 8
4829 * End:
4830 */
4831
aliguori731b0362009-03-05 23:01:42 +00004832void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004833{
aliguori731b0362009-03-05 23:01:42 +00004834 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004835 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004836
4837 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004838 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004839 is_first_init = 0;
4840 }
aliguori87127162009-03-05 23:01:29 +00004841
4842 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004843
aliguori87127162009-03-05 23:01:29 +00004844 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004845 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004846 if (flags & MONITOR_USE_READLINE) {
4847 mon->rs = readline_init(mon, monitor_find_completion);
4848 monitor_read_command(mon, 0);
4849 }
aliguori87127162009-03-05 23:01:29 +00004850
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004851 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004852 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004853 /* Control mode requires special handlers */
4854 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4855 monitor_control_event, mon);
4856 } else {
4857 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4858 monitor_event, mon);
4859 }
aliguori87127162009-03-05 23:01:29 +00004860
Blue Swirl72cf2d42009-09-12 07:36:22 +00004861 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004862 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4863 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004864}
4865
aliguori376253e2009-03-05 23:01:23 +00004866static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004867{
aliguoribb5fc202009-03-05 23:01:15 +00004868 BlockDriverState *bs = opaque;
4869 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004870
aliguoribb5fc202009-03-05 23:01:15 +00004871 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004872 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004873 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004874 }
aliguori731b0362009-03-05 23:01:42 +00004875 if (mon->password_completion_cb)
4876 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004877
aliguori731b0362009-03-05 23:01:42 +00004878 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004879}
aliguoric0f4ce72009-03-05 23:01:01 +00004880
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004881int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4882 BlockDriverCompletionFunc *completion_cb,
4883 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004884{
aliguoricde76ee2009-03-05 23:01:51 +00004885 int err;
4886
aliguoribb5fc202009-03-05 23:01:15 +00004887 if (!bdrv_key_required(bs)) {
4888 if (completion_cb)
4889 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004890 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004891 }
aliguoric0f4ce72009-03-05 23:01:01 +00004892
Luiz Capitulino94171e12009-12-07 21:37:00 +01004893 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004894 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004895 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004896 }
4897
aliguori376253e2009-03-05 23:01:23 +00004898 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4899 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004900
aliguori731b0362009-03-05 23:01:42 +00004901 mon->password_completion_cb = completion_cb;
4902 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004903
aliguoricde76ee2009-03-05 23:01:51 +00004904 err = monitor_read_password(mon, bdrv_password_cb, bs);
4905
4906 if (err && completion_cb)
4907 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004908
4909 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004910}