blob: 8d813d512114a5451bb99d87bd2320de6c8173da [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"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000039#include "monitor.h"
40#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020042#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000044#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000045#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000046#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000047#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000048#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000049#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010051#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030052#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Blue Swirl314e2982011-09-11 20:22:05 +000066#include "memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +010069#include "qemu-thread.h"
ths6a5bd302007-12-03 17:05:38 +000070
Jan Kiszka661f1922011-10-16 11:53:13 +020071/* for pic/irq_info */
72#if defined(TARGET_SPARC)
73#include "hw/sun4m.h"
74#endif
75#include "hw/lm32_pic.h"
76
bellard9dc39cb2004-03-14 21:38:27 +000077//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000078//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000079
bellard9307c4c2004-04-04 12:57:25 +000080/*
81 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000082 *
bellard9307c4c2004-04-04 12:57:25 +000083 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000084 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000085 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010086 * 'O' option string of the form NAME=VALUE,...
87 * parsed according to QemuOptsList given by its name
88 * Example: 'device:O' uses qemu_device_opts.
89 * Restriction: only lists with empty desc are supported
90 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000091 * 'i' 32 bit integer
92 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030093 * 'M' Non-negative target long (32 or 64 bit), in user mode the
94 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020095 * 'o' octets (aka bytes)
96 * user mode accepts an optional T, t, G, g, M, m, K, k
97 * suffix, which multiplies the value by 2^40 for
98 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
99 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100100 * 'T' double
101 * user mode accepts an optional ms, us, ns suffix,
102 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000103 * '/' optional gdb-like print format (like "/10x")
104 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300105 * '?' optional type (for all types, except '/')
106 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100107 * 'b' boolean
108 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300109 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000110 *
111 */
112
Adam Litke940cc302010-01-25 12:18:44 -0600113typedef struct MonitorCompletionData MonitorCompletionData;
114struct MonitorCompletionData {
115 Monitor *mon;
116 void (*user_print)(Monitor *mon, const QObject *data);
117};
118
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000120 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000121 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *params;
123 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300124 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300125 union {
126 void (*info)(Monitor *mon);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600129 int (*cmd_async)(Monitor *mon, const QDict *params,
130 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200132 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Corey Bryantba1c0482012-08-14 16:43:43 -0400143/* file descriptor associated with a file descriptor set */
144typedef struct MonFdsetFd MonFdsetFd;
145struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
150};
151
152/* file descriptor set containing fds passed via SCM_RIGHTS */
153typedef struct MonFdset MonFdset;
154struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
157 QLIST_ENTRY(MonFdset) next;
158};
159
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200160typedef struct MonitorControl {
161 QObject *id;
162 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200163 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200164} MonitorControl;
165
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100166/*
167 * To prevent flooding clients, events can be throttled. The
168 * throttling is calculated globally, rather than per-Monitor
169 * instance.
170 */
171typedef struct MonitorEventState {
172 MonitorEvent event; /* Event being tracked */
173 int64_t rate; /* Period over which to throttle. 0 to disable */
174 int64_t last; /* Time at which event was last emitted */
175 QEMUTimer *timer; /* Timer for handling delayed events */
176 QObject *data; /* Event pending delayed dispatch */
177} MonitorEventState;
178
aliguori87127162009-03-05 23:01:29 +0000179struct Monitor {
180 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200181 int mux_out;
182 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000183 int flags;
184 int suspend_cnt;
185 uint8_t outbuf[1024];
186 int outbuf_index;
187 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200188 MonitorControl *mc;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100189 CPUArchState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000190 BlockDriverCompletionFunc *password_completion_cb;
191 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200192 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500193 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000194 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000195};
196
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300197/* QMP checker flags */
198#define QMP_ACCEPT_UNKNOWNS 1
199
Blue Swirl72cf2d42009-09-12 07:36:22 +0000200static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400201static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
bellard7e2515e2004-08-01 21:52:19 +0000202
Wayne Xia816f8922011-10-12 11:32:41 +0800203static mon_cmd_t mon_cmds[];
204static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000205
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300206static const mon_cmd_t qmp_cmds[];
207
Markus Armbruster8631b602010-02-18 11:41:55 +0100208Monitor *cur_mon;
209Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000210
aliguori731b0362009-03-05 23:01:42 +0000211static void monitor_command_cb(Monitor *mon, const char *cmdline,
212 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000213
Luiz Capitulino09069b12010-02-04 18:10:06 -0200214static inline int qmp_cmd_mode(const Monitor *mon)
215{
216 return (mon->mc ? mon->mc->command_mode : 0);
217}
218
Luiz Capitulino418173c2009-11-26 22:58:51 -0200219/* Return true if in control mode, false otherwise */
220static inline int monitor_ctrl_mode(const Monitor *mon)
221{
222 return (mon->flags & MONITOR_USE_CONTROL);
223}
224
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100225/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
226int monitor_cur_is_qmp(void)
227{
228 return cur_mon && monitor_ctrl_mode(cur_mon);
229}
230
Anthony Liguori7060b472011-09-02 12:34:50 -0500231void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000232{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200233 if (!mon->rs)
234 return;
235
aliguori731b0362009-03-05 23:01:42 +0000236 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
237 if (show_prompt)
238 readline_show_prompt(mon->rs);
239}
bellard6a00d602005-11-21 23:25:50 +0000240
Anthony Liguori7060b472011-09-02 12:34:50 -0500241int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
242 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000243{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100244 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100245 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100246 return -EINVAL;
247 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000248 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
249 /* prompt is printed on return from the command handler */
250 return 0;
251 } else {
252 monitor_printf(mon, "terminal does not support password prompting\n");
253 return -ENOTTY;
254 }
aliguoribb5fc202009-03-05 23:01:15 +0000255}
256
aliguori376253e2009-03-05 23:01:23 +0000257void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000258{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200259 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500260 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000261 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000262 }
263}
264
265/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000266static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000267{
ths60fe76f2007-12-16 03:02:09 +0000268 char c;
aliguori731b0362009-03-05 23:01:42 +0000269
bellard7e2515e2004-08-01 21:52:19 +0000270 for(;;) {
271 c = *str++;
272 if (c == '\0')
273 break;
bellard7ba12602006-07-14 20:26:42 +0000274 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000275 mon->outbuf[mon->outbuf_index++] = '\r';
276 mon->outbuf[mon->outbuf_index++] = c;
277 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
278 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000279 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000280 }
281}
282
aliguori376253e2009-03-05 23:01:23 +0000283void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000284{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200285 char buf[4096];
286
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200287 if (!mon)
288 return;
289
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200290 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200291 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200292 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200293
294 vsnprintf(buf, sizeof(buf), fmt, ap);
295 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000296}
297
aliguori376253e2009-03-05 23:01:23 +0000298void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000299{
300 va_list ap;
301 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000302 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000303 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000304}
305
aliguori376253e2009-03-05 23:01:23 +0000306void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000307{
308 int i;
309
310 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000311 switch (filename[i]) {
312 case ' ':
313 case '"':
314 case '\\':
315 monitor_printf(mon, "\\%c", filename[i]);
316 break;
317 case '\t':
318 monitor_printf(mon, "\\t");
319 break;
320 case '\r':
321 monitor_printf(mon, "\\r");
322 break;
323 case '\n':
324 monitor_printf(mon, "\\n");
325 break;
326 default:
327 monitor_printf(mon, "%c", filename[i]);
328 break;
329 }
thsfef30742006-12-22 14:11:32 +0000330 }
331}
332
Stefan Weil8b7968f2010-09-23 21:28:05 +0200333static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
334 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000335{
336 va_list ap;
337 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000338 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000339 va_end(ap);
340 return 0;
341}
342
Luiz Capitulino13c74252009-10-07 13:41:55 -0300343static void monitor_user_noop(Monitor *mon, const QObject *data) { }
344
Luiz Capitulino9e807212010-09-16 10:58:59 -0300345static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300346{
347 return cmd->user_print != NULL;
348}
349
Luiz Capitulino4903de02010-09-16 11:01:32 -0300350static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600351{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200352 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600353}
354
Luiz Capitulino8204a912009-11-18 23:05:31 -0200355static inline int monitor_has_error(const Monitor *mon)
356{
357 return mon->error != NULL;
358}
359
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200360static void monitor_json_emitter(Monitor *mon, const QObject *data)
361{
362 QString *json;
363
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200364 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
365 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200366 assert(json != NULL);
367
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200368 qstring_append_chr(json, '\n');
369 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200370
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200371 QDECREF(json);
372}
373
Luiz Capitulinode253f12012-07-27 16:18:16 -0300374static QDict *build_qmp_error_dict(const QError *err)
375{
376 QObject *obj;
377
378 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
379 ErrorClass_lookup[err->err_class],
380 qerror_human(err));
381
382 return qobject_to_qdict(obj);
383}
384
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200385static void monitor_protocol_emitter(Monitor *mon, QObject *data)
386{
387 QDict *qmp;
388
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100389 trace_monitor_protocol_emitter(mon);
390
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200391 if (!monitor_has_error(mon)) {
392 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300393 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200394 if (data) {
395 qobject_incref(data);
396 qdict_put_obj(qmp, "return", data);
397 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200398 /* return an empty QDict by default */
399 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200400 }
401 } else {
402 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300403 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200404 QDECREF(mon->error);
405 mon->error = NULL;
406 }
407
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200408 if (mon->mc->id) {
409 qdict_put_obj(qmp, "id", mon->mc->id);
410 mon->mc->id = NULL;
411 }
412
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200413 monitor_json_emitter(mon, QOBJECT(qmp));
414 QDECREF(qmp);
415}
416
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200417static void timestamp_put(QDict *qdict)
418{
419 int err;
420 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000421 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200422
Blue Swirld08d6f02009-12-04 18:06:39 +0000423 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200424 if (err < 0)
425 return;
426
427 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
428 "'microseconds': %" PRId64 " }",
429 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200430 qdict_put_obj(qdict, "timestamp", obj);
431}
432
Daniel P. Berrange48608532012-05-21 17:59:51 +0100433
434static const char *monitor_event_names[] = {
435 [QEVENT_SHUTDOWN] = "SHUTDOWN",
436 [QEVENT_RESET] = "RESET",
437 [QEVENT_POWERDOWN] = "POWERDOWN",
438 [QEVENT_STOP] = "STOP",
439 [QEVENT_RESUME] = "RESUME",
440 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
441 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
442 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
443 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
444 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
445 [QEVENT_WATCHDOG] = "WATCHDOG",
446 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
447 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
448 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
449 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
450 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
451 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
452 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300453 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100454 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100455 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100456};
457QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
458
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459MonitorEventState monitor_event_state[QEVENT_MAX];
460QemuMutex monitor_event_state_lock;
461
462/*
463 * Emits the event to every monitor instance
464 */
465static void
466monitor_protocol_event_emit(MonitorEvent event,
467 QObject *data)
468{
469 Monitor *mon;
470
471 trace_monitor_protocol_event_emit(event, data);
472 QLIST_FOREACH(mon, &mon_list, entry) {
473 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
474 monitor_json_emitter(mon, data);
475 }
476 }
477}
478
479
480/*
481 * Queue a new event for emission to Monitor instances,
482 * applying any rate limiting if required.
483 */
484static void
485monitor_protocol_event_queue(MonitorEvent event,
486 QObject *data)
487{
488 MonitorEventState *evstate;
489 int64_t now = qemu_get_clock_ns(rt_clock);
490 assert(event < QEVENT_MAX);
491
492 qemu_mutex_lock(&monitor_event_state_lock);
493 evstate = &(monitor_event_state[event]);
494 trace_monitor_protocol_event_queue(event,
495 data,
496 evstate->rate,
497 evstate->last,
498 now);
499
500 /* Rate limit of 0 indicates no throttling */
501 if (!evstate->rate) {
502 monitor_protocol_event_emit(event, data);
503 evstate->last = now;
504 } else {
505 int64_t delta = now - evstate->last;
506 if (evstate->data ||
507 delta < evstate->rate) {
508 /* If there's an existing event pending, replace
509 * it with the new event, otherwise schedule a
510 * timer for delayed emission
511 */
512 if (evstate->data) {
513 qobject_decref(evstate->data);
514 } else {
515 int64_t then = evstate->last + evstate->rate;
516 qemu_mod_timer_ns(evstate->timer, then);
517 }
518 evstate->data = data;
519 qobject_incref(evstate->data);
520 } else {
521 monitor_protocol_event_emit(event, data);
522 evstate->last = now;
523 }
524 }
525 qemu_mutex_unlock(&monitor_event_state_lock);
526}
527
528
529/*
530 * The callback invoked by QemuTimer when a delayed
531 * event is ready to be emitted
532 */
533static void monitor_protocol_event_handler(void *opaque)
534{
535 MonitorEventState *evstate = opaque;
536 int64_t now = qemu_get_clock_ns(rt_clock);
537
538 qemu_mutex_lock(&monitor_event_state_lock);
539
540 trace_monitor_protocol_event_handler(evstate->event,
541 evstate->data,
542 evstate->last,
543 now);
544 if (evstate->data) {
545 monitor_protocol_event_emit(evstate->event, evstate->data);
546 qobject_decref(evstate->data);
547 evstate->data = NULL;
548 }
549 evstate->last = now;
550 qemu_mutex_unlock(&monitor_event_state_lock);
551}
552
553
554/*
555 * @event: the event ID to be limited
556 * @rate: the rate limit in milliseconds
557 *
558 * Sets a rate limit on a particular event, so no
559 * more than 1 event will be emitted within @rate
560 * milliseconds
561 */
562static void
563monitor_protocol_event_throttle(MonitorEvent event,
564 int64_t rate)
565{
566 MonitorEventState *evstate;
567 assert(event < QEVENT_MAX);
568
569 evstate = &(monitor_event_state[event]);
570
571 trace_monitor_protocol_event_throttle(event, rate);
572 evstate->event = event;
573 evstate->rate = rate * SCALE_MS;
574 evstate->timer = qemu_new_timer(rt_clock,
575 SCALE_MS,
576 monitor_protocol_event_handler,
577 evstate);
578 evstate->last = 0;
579 evstate->data = NULL;
580}
581
582
583/* Global, one-time initializer to configure the rate limiting
584 * and initialize state */
585static void monitor_protocol_event_init(void)
586{
587 qemu_mutex_init(&monitor_event_state_lock);
588 /* Limit RTC & BALLOON events to 1 per second */
589 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
590 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
591 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
592}
593
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200594/**
595 * monitor_protocol_event(): Generate a Monitor event
596 *
597 * Event-specific data can be emitted through the (optional) 'data' parameter.
598 */
599void monitor_protocol_event(MonitorEvent event, QObject *data)
600{
601 QDict *qmp;
602 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200603
Blue Swirl242cd002009-12-04 18:05:45 +0000604 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200605
Daniel P. Berrange48608532012-05-21 17:59:51 +0100606 event_name = monitor_event_names[event];
607 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200608
609 qmp = qdict_new();
610 timestamp_put(qmp);
611 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200612 if (data) {
613 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200614 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200615 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200616
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100617 trace_monitor_protocol_event(event, event_name, qmp);
618 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200619 QDECREF(qmp);
620}
621
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200622static int do_qmp_capabilities(Monitor *mon, const QDict *params,
623 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200624{
625 /* Will setup QMP capabilities in the future */
626 if (monitor_ctrl_mode(mon)) {
627 mon->mc->command_mode = 1;
628 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200629
630 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200631}
632
Luiz Capitulino0268d972010-10-22 10:08:02 -0200633static void handle_user_command(Monitor *mon, const char *cmdline);
634
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200635char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
636 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200637{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200638 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200639 Monitor *old_mon, hmp;
640 CharDriverState mchar;
641
642 memset(&hmp, 0, sizeof(hmp));
643 qemu_chr_init_mem(&mchar);
644 hmp.chr = &mchar;
645
646 old_mon = cur_mon;
647 cur_mon = &hmp;
648
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200649 if (has_cpu_index) {
650 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200651 if (ret < 0) {
652 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200653 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
654 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200655 goto out;
656 }
657 }
658
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200659 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200660 cur_mon = old_mon;
661
662 if (qemu_chr_mem_osize(hmp.chr) > 0) {
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200663 QString *str = qemu_chr_mem_to_qs(hmp.chr);
664 output = g_strdup(qstring_get_str(str));
665 QDECREF(str);
666 } else {
667 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200668 }
669
670out:
671 qemu_chr_close_mem(hmp.chr);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200672 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200673}
674
bellard9dc39cb2004-03-14 21:38:27 +0000675static int compare_cmd(const char *name, const char *list)
676{
677 const char *p, *pstart;
678 int len;
679 len = strlen(name);
680 p = list;
681 for(;;) {
682 pstart = p;
683 p = strchr(p, '|');
684 if (!p)
685 p = pstart + strlen(pstart);
686 if ((p - pstart) == len && !memcmp(pstart, name, len))
687 return 1;
688 if (*p == '\0')
689 break;
690 p++;
691 }
692 return 0;
693}
694
Anthony Liguoric227f092009-10-01 16:12:16 -0500695static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000696 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000697{
Anthony Liguoric227f092009-10-01 16:12:16 -0500698 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000699
700 for(cmd = cmds; cmd->name != NULL; cmd++) {
701 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000702 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
703 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000704 }
705}
706
aliguori376253e2009-03-05 23:01:23 +0000707static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000708{
709 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000710 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000711 } else {
aliguori376253e2009-03-05 23:01:23 +0000712 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000713 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000714 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000715 monitor_printf(mon, "Log items (comma separated):\n");
716 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000717 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000718 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000719 }
720 }
bellard9dc39cb2004-03-14 21:38:27 +0000721 }
722}
723
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300724static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300725{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300726 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300727}
728
Lluísfc764102011-08-31 20:31:18 +0200729static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530730{
731 const char *tp_name = qdict_get_str(qdict, "name");
732 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200733 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000734
735 if (!ret) {
736 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
737 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530738}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100739
Michael Rothc45a8162011-10-02 08:44:37 -0500740#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100741static void do_trace_file(Monitor *mon, const QDict *qdict)
742{
743 const char *op = qdict_get_try_str(qdict, "op");
744 const char *arg = qdict_get_try_str(qdict, "arg");
745
746 if (!op) {
747 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
748 } else if (!strcmp(op, "on")) {
749 st_set_trace_file_enabled(true);
750 } else if (!strcmp(op, "off")) {
751 st_set_trace_file_enabled(false);
752 } else if (!strcmp(op, "flush")) {
753 st_flush_trace_buffer();
754 } else if (!strcmp(op, "set")) {
755 if (arg) {
756 st_set_trace_file(arg);
757 }
758 } else {
759 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
760 help_cmd(mon, "trace-file");
761 }
762}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530763#endif
764
Adam Litke940cc302010-01-25 12:18:44 -0600765static void user_monitor_complete(void *opaque, QObject *ret_data)
766{
767 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
768
769 if (ret_data) {
770 data->user_print(data->mon, ret_data);
771 }
772 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500773 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600774}
775
776static void qmp_monitor_complete(void *opaque, QObject *ret_data)
777{
778 monitor_protocol_emitter(opaque, ret_data);
779}
780
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300781static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
782 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600783{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300784 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600785}
786
Adam Litke940cc302010-01-25 12:18:44 -0600787static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
788 const QDict *params)
789{
790 int ret;
791
Anthony Liguori7267c092011-08-20 22:09:37 -0500792 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600793 cb_data->mon = mon;
794 cb_data->user_print = cmd->user_print;
795 monitor_suspend(mon);
796 ret = cmd->mhandler.cmd_async(mon, params,
797 user_monitor_complete, cb_data);
798 if (ret < 0) {
799 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500800 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600801 }
802}
803
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300804static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000805{
Anthony Liguoric227f092009-10-01 16:12:16 -0500806 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300807 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000808
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200809 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000810 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200811 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300812
813 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000814 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300815 break;
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300817
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200818 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300819 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200820 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300821
Luiz Capitulinob5c30582011-10-21 16:24:28 -0200822 cmd->mhandler.info(mon);
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300823 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300824
825help:
826 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000827}
828
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300829CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000830{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300831 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200832 const mon_cmd_t *cmd;
833
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300834 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200835 info = g_malloc0(sizeof(*info));
836 info->value = g_malloc0(sizeof(*info->value));
837 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200838
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300839 info->next = cmd_list;
840 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200841 }
842
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300843 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000844}
845
Daniel P. Berrange48608532012-05-21 17:59:51 +0100846EventInfoList *qmp_query_events(Error **errp)
847{
848 EventInfoList *info, *ev_list = NULL;
849 MonitorEvent e;
850
851 for (e = 0 ; e < QEVENT_MAX ; e++) {
852 const char *event_name = monitor_event_names[e];
853 assert(event_name != NULL);
854 info = g_malloc0(sizeof(*info));
855 info->value = g_malloc0(sizeof(*info->value));
856 info->value->name = g_strdup(event_name);
857
858 info->next = ev_list;
859 ev_list = info;
860 }
861
862 return ev_list;
863}
864
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300865/* set the current CPU defined by the user */
866int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000867{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100868 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000869
870 for(env = first_cpu; env != NULL; env = env->next_cpu) {
871 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000872 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000873 return 0;
874 }
875 }
876 return -1;
877}
878
Andreas Färber9349b4f2012-03-14 01:38:32 +0100879static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000880{
aliguori731b0362009-03-05 23:01:42 +0000881 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300882 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000883 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300884 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000885 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000886}
887
Luiz Capitulino99b77962011-10-24 10:53:44 -0200888int monitor_get_cpu_index(void)
889{
890 return mon_get_cpu()->cpu_index;
891}
892
aliguori376253e2009-03-05 23:01:23 +0000893static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000894{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100895 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000896 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000897#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000898 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000899 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000900#else
aliguori376253e2009-03-05 23:01:23 +0000901 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000902 0);
bellard9307c4c2004-04-04 12:57:25 +0000903#endif
904}
905
aliguori376253e2009-03-05 23:01:23 +0000906static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000907{
aliguori376253e2009-03-05 23:01:23 +0000908 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000909}
910
aliguori376253e2009-03-05 23:01:23 +0000911static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000912{
913 int i;
bellard7e2515e2004-08-01 21:52:19 +0000914 const char *str;
ths3b46e622007-09-17 08:09:54 +0000915
aliguoricde76ee2009-03-05 23:01:51 +0000916 if (!mon->rs)
917 return;
bellard7e2515e2004-08-01 21:52:19 +0000918 i = 0;
919 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000920 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000921 if (!str)
922 break;
aliguori376253e2009-03-05 23:01:23 +0000923 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000924 i++;
bellardaa455482004-04-04 13:07:25 +0000925 }
926}
927
j_mayer76a66252007-03-07 08:32:30 +0000928#if defined(TARGET_PPC)
929/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000930static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000931{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100932 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000933
934 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000935 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000936}
937#endif
938
Lluísfc764102011-08-31 20:31:18 +0200939static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530940{
Lluísfc764102011-08-31 20:31:18 +0200941 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530942}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530943
Daniel P. Berrange13661082011-06-23 13:31:42 +0100944static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
945{
946 const char *protocol = qdict_get_str(qdict, "protocol");
947 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +0100948 CharDriverState *s;
949
950 if (strcmp(protocol, "spice") == 0) {
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000951 int fd = monitor_get_fd(mon, fdname);
952 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
953 int tls = qdict_get_try_bool(qdict, "tls", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100954 if (!using_spice) {
955 /* correct one? spice isn't a device ,,, */
956 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
957 return -1;
958 }
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000959 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
960 close(fd);
961 }
962 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800963#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +0100964 } else if (strcmp(protocol, "vnc") == 0) {
965 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +0100966 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100967 vnc_display_add_client(NULL, fd, skipauth);
968 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800969#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +0100970 } else if ((s = qemu_chr_find(protocol)) != NULL) {
971 int fd = monitor_get_fd(mon, fdname);
972 if (qemu_chr_add_client(s, fd) < 0) {
973 qerror_report(QERR_ADD_CLIENT_FAILED);
974 return -1;
975 }
976 return 0;
977 }
978
979 qerror_report(QERR_INVALID_PARAMETER, "protocol");
980 return -1;
981}
982
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200983static int client_migrate_info(Monitor *mon, const QDict *qdict,
984 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200985{
986 const char *protocol = qdict_get_str(qdict, "protocol");
987 const char *hostname = qdict_get_str(qdict, "hostname");
988 const char *subject = qdict_get_try_str(qdict, "cert-subject");
989 int port = qdict_get_try_int(qdict, "port", -1);
990 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
991 int ret;
992
993 if (strcmp(protocol, "spice") == 0) {
994 if (!using_spice) {
995 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
996 return -1;
997 }
998
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200999 if (port == -1 && tls_port == -1) {
1000 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1001 return -1;
1002 }
1003
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001004 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1005 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001006 if (ret != 0) {
1007 qerror_report(QERR_UNDEFINED_ERROR);
1008 return -1;
1009 }
1010 return 0;
1011 }
1012
1013 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1014 return -1;
1015}
1016
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001017static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001018{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001019 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001020 return 0;
bellard59a983b2004-03-17 23:17:16 +00001021}
1022
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001023static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001024{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001025 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001026}
1027
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001028static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001029{
1030 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001031 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001032
bellard9307c4c2004-04-04 12:57:25 +00001033 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001034 mask = 0;
1035 } else {
bellard9307c4c2004-04-04 12:57:25 +00001036 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001037 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001038 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001039 return;
1040 }
1041 }
1042 cpu_set_log(mask);
1043}
1044
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001045static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001046{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001047 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001048 if (!option || !strcmp(option, "on")) {
1049 singlestep = 1;
1050 } else if (!strcmp(option, "off")) {
1051 singlestep = 0;
1052 } else {
1053 monitor_printf(mon, "unexpected option %s\n", option);
1054 }
1055}
1056
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001057static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001058{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001059 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001060 if (!device)
1061 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1062 if (gdbserver_start(device) < 0) {
1063 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1064 device);
1065 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001066 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001067 } else {
aliguori59030a82009-04-05 18:43:41 +00001068 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1069 device);
bellard8a7ddc32004-03-31 19:00:16 +00001070 }
1071}
1072
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001073static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001074{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001075 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001076 if (select_watchdog_action(action) == -1) {
1077 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1078 }
1079}
1080
aliguori376253e2009-03-05 23:01:23 +00001081static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001082{
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001084 switch(c) {
1085 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001087 break;
1088 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001090 break;
1091 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001093 break;
1094 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001096 break;
1097 default:
1098 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001099 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001100 } else {
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001102 }
1103 break;
1104 }
aliguori376253e2009-03-05 23:01:23 +00001105 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001106}
1107
aliguori376253e2009-03-05 23:01:23 +00001108static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001109 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001110{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001111 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001112 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001113 uint8_t buf[16];
1114 uint64_t v;
1115
1116 if (format == 'i') {
1117 int flags;
1118 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001119 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001120#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001121 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001122 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001123 } else if (wsize == 4) {
1124 flags = 0;
1125 } else {
bellard6a15fd12006-04-12 21:07:07 +00001126 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001127 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001128 if (env) {
1129#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001130 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001131 (env->segs[R_CS].flags & DESC_L_MASK))
1132 flags = 2;
1133 else
1134#endif
1135 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1136 flags = 1;
1137 }
bellard4c27ba22004-04-25 18:05:08 +00001138 }
1139#endif
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001141 return;
1142 }
1143
1144 len = wsize * count;
1145 if (wsize == 1)
1146 line_size = 8;
1147 else
1148 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001149 max_digits = 0;
1150
1151 switch(format) {
1152 case 'o':
1153 max_digits = (wsize * 8 + 2) / 3;
1154 break;
1155 default:
1156 case 'x':
1157 max_digits = (wsize * 8) / 4;
1158 break;
1159 case 'u':
1160 case 'd':
1161 max_digits = (wsize * 8 * 10 + 32) / 33;
1162 break;
1163 case 'c':
1164 wsize = 1;
1165 break;
1166 }
1167
1168 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001169 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001171 else
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001173 l = len;
1174 if (l > line_size)
1175 l = line_size;
1176 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001177 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001178 } else {
bellard6a00d602005-11-21 23:25:50 +00001179 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001180 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001181 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001182 break;
1183 }
bellard9307c4c2004-04-04 12:57:25 +00001184 }
ths5fafdf22007-09-16 21:08:06 +00001185 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001186 while (i < l) {
1187 switch(wsize) {
1188 default:
1189 case 1:
1190 v = ldub_raw(buf + i);
1191 break;
1192 case 2:
1193 v = lduw_raw(buf + i);
1194 break;
1195 case 4:
bellard92a31b12005-02-10 22:00:52 +00001196 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001197 break;
1198 case 8:
1199 v = ldq_raw(buf + i);
1200 break;
1201 }
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001203 switch(format) {
1204 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001206 break;
1207 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001209 break;
1210 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001212 break;
1213 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 }
1220 i += wsize;
1221 }
aliguori376253e2009-03-05 23:01:23 +00001222 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001223 addr += l;
1224 len -= l;
1225 }
1226}
1227
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001228static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001229{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001230 int count = qdict_get_int(qdict, "count");
1231 int format = qdict_get_int(qdict, "format");
1232 int size = qdict_get_int(qdict, "size");
1233 target_long addr = qdict_get_int(qdict, "addr");
1234
aliguori376253e2009-03-05 23:01:23 +00001235 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001236}
1237
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001238static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001239{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001240 int count = qdict_get_int(qdict, "count");
1241 int format = qdict_get_int(qdict, "format");
1242 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001243 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001244
aliguori376253e2009-03-05 23:01:23 +00001245 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001246}
1247
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001248static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001249{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001250 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001251 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001252
bellard9307c4c2004-04-04 12:57:25 +00001253 switch(format) {
1254 case 'o':
Peter Maydell66f27e62012-06-25 16:52:24 +01001255 monitor_printf(mon, "%#" TARGET_PRIoPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001256 break;
1257 case 'x':
Peter Maydell66f27e62012-06-25 16:52:24 +01001258 monitor_printf(mon, "%#" TARGET_PRIxPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001259 break;
1260 case 'u':
Peter Maydell66f27e62012-06-25 16:52:24 +01001261 monitor_printf(mon, "%" TARGET_PRIuPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001262 break;
1263 default:
1264 case 'd':
Peter Maydell66f27e62012-06-25 16:52:24 +01001265 monitor_printf(mon, "%" TARGET_PRIdPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001269 break;
1270 }
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001272}
1273
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001274static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001275{
1276 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001277 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001278 uint32_t start = qdict_get_int(qdict, "start");
1279 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001280
1281 sum = 0;
1282 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001283 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001284 /* BSD sum algorithm ('sum' Unix command) */
1285 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001286 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001287 }
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001289}
1290
bellarda3a91a32004-06-04 11:06:21 +00001291typedef struct {
1292 int keycode;
1293 const char *name;
1294} KeyDef;
1295
1296static const KeyDef key_defs[] = {
1297 { 0x2a, "shift" },
1298 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001299
bellarda3a91a32004-06-04 11:06:21 +00001300 { 0x38, "alt" },
1301 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001302 { 0x64, "altgr" },
1303 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001304 { 0x1d, "ctrl" },
1305 { 0x9d, "ctrl_r" },
1306
1307 { 0xdd, "menu" },
1308
1309 { 0x01, "esc" },
1310
1311 { 0x02, "1" },
1312 { 0x03, "2" },
1313 { 0x04, "3" },
1314 { 0x05, "4" },
1315 { 0x06, "5" },
1316 { 0x07, "6" },
1317 { 0x08, "7" },
1318 { 0x09, "8" },
1319 { 0x0a, "9" },
1320 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001321 { 0x0c, "minus" },
1322 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001323 { 0x0e, "backspace" },
1324
1325 { 0x0f, "tab" },
1326 { 0x10, "q" },
1327 { 0x11, "w" },
1328 { 0x12, "e" },
1329 { 0x13, "r" },
1330 { 0x14, "t" },
1331 { 0x15, "y" },
1332 { 0x16, "u" },
1333 { 0x17, "i" },
1334 { 0x18, "o" },
1335 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001336 { 0x1a, "bracket_left" },
1337 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001338 { 0x1c, "ret" },
1339
1340 { 0x1e, "a" },
1341 { 0x1f, "s" },
1342 { 0x20, "d" },
1343 { 0x21, "f" },
1344 { 0x22, "g" },
1345 { 0x23, "h" },
1346 { 0x24, "j" },
1347 { 0x25, "k" },
1348 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001349 { 0x27, "semicolon" },
1350 { 0x28, "apostrophe" },
1351 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001352
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001353 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001354 { 0x2c, "z" },
1355 { 0x2d, "x" },
1356 { 0x2e, "c" },
1357 { 0x2f, "v" },
1358 { 0x30, "b" },
1359 { 0x31, "n" },
1360 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001361 { 0x33, "comma" },
1362 { 0x34, "dot" },
1363 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001364
balrog4d3b6f62008-02-10 16:33:14 +00001365 { 0x37, "asterisk" },
1366
bellarda3a91a32004-06-04 11:06:21 +00001367 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001368 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001369 { 0x3b, "f1" },
1370 { 0x3c, "f2" },
1371 { 0x3d, "f3" },
1372 { 0x3e, "f4" },
1373 { 0x3f, "f5" },
1374 { 0x40, "f6" },
1375 { 0x41, "f7" },
1376 { 0x42, "f8" },
1377 { 0x43, "f9" },
1378 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001379 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001380 { 0x46, "scroll_lock" },
1381
bellard64866c32006-05-07 18:03:31 +00001382 { 0xb5, "kp_divide" },
1383 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001384 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001385 { 0x4e, "kp_add" },
1386 { 0x9c, "kp_enter" },
1387 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001388 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001389
1390 { 0x52, "kp_0" },
1391 { 0x4f, "kp_1" },
1392 { 0x50, "kp_2" },
1393 { 0x51, "kp_3" },
1394 { 0x4b, "kp_4" },
1395 { 0x4c, "kp_5" },
1396 { 0x4d, "kp_6" },
1397 { 0x47, "kp_7" },
1398 { 0x48, "kp_8" },
1399 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001400
bellarda3a91a32004-06-04 11:06:21 +00001401 { 0x56, "<" },
1402
1403 { 0x57, "f11" },
1404 { 0x58, "f12" },
1405
1406 { 0xb7, "print" },
1407
1408 { 0xc7, "home" },
1409 { 0xc9, "pgup" },
1410 { 0xd1, "pgdn" },
1411 { 0xcf, "end" },
1412
1413 { 0xcb, "left" },
1414 { 0xc8, "up" },
1415 { 0xd0, "down" },
1416 { 0xcd, "right" },
1417
1418 { 0xd2, "insert" },
1419 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001420#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1421 { 0xf0, "stop" },
1422 { 0xf1, "again" },
1423 { 0xf2, "props" },
1424 { 0xf3, "undo" },
1425 { 0xf4, "front" },
1426 { 0xf5, "copy" },
1427 { 0xf6, "open" },
1428 { 0xf7, "paste" },
1429 { 0xf8, "find" },
1430 { 0xf9, "cut" },
1431 { 0xfa, "lf" },
1432 { 0xfb, "help" },
1433 { 0xfc, "meta_l" },
1434 { 0xfd, "meta_r" },
1435 { 0xfe, "compose" },
1436#endif
bellarda3a91a32004-06-04 11:06:21 +00001437 { 0, NULL },
1438};
1439
1440static int get_keycode(const char *key)
1441{
1442 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001443 char *endp;
1444 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001445
1446 for(p = key_defs; p->name != NULL; p++) {
1447 if (!strcmp(key, p->name))
1448 return p->keycode;
1449 }
bellard64866c32006-05-07 18:03:31 +00001450 if (strstart(key, "0x", NULL)) {
1451 ret = strtoul(key, &endp, 0);
1452 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1453 return ret;
1454 }
bellarda3a91a32004-06-04 11:06:21 +00001455 return -1;
1456}
1457
balrogc8256f92008-06-08 22:45:01 +00001458#define MAX_KEYCODES 16
1459static uint8_t keycodes[MAX_KEYCODES];
1460static int nb_pending_keycodes;
1461static QEMUTimer *key_timer;
1462
1463static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001464{
balrogc8256f92008-06-08 22:45:01 +00001465 int keycode;
1466
1467 while (nb_pending_keycodes > 0) {
1468 nb_pending_keycodes--;
1469 keycode = keycodes[nb_pending_keycodes];
1470 if (keycode & 0x80)
1471 kbd_put_keycode(0xe0);
1472 kbd_put_keycode(keycode | 0x80);
1473 }
1474}
1475
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001476static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001477{
balrog3401c0d2008-06-04 10:05:59 +00001478 char keyname_buf[16];
1479 char *separator;
1480 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001481 const char *string = qdict_get_str(qdict, "string");
1482 int has_hold_time = qdict_haskey(qdict, "hold_time");
1483 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001484
balrogc8256f92008-06-08 22:45:01 +00001485 if (nb_pending_keycodes > 0) {
1486 qemu_del_timer(key_timer);
1487 release_keys(NULL);
1488 }
1489 if (!has_hold_time)
1490 hold_time = 100;
1491 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001492 while (1) {
1493 separator = strchr(string, '-');
1494 keyname_len = separator ? separator - string : strlen(string);
1495 if (keyname_len > 0) {
1496 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1497 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001498 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001499 return;
bellarda3a91a32004-06-04 11:06:21 +00001500 }
balrogc8256f92008-06-08 22:45:01 +00001501 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001503 return;
1504 }
1505 keyname_buf[keyname_len] = 0;
1506 keycode = get_keycode(keyname_buf);
1507 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001509 return;
1510 }
balrogc8256f92008-06-08 22:45:01 +00001511 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001512 }
balrog3401c0d2008-06-04 10:05:59 +00001513 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001514 break;
balrog3401c0d2008-06-04 10:05:59 +00001515 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001516 }
balrogc8256f92008-06-08 22:45:01 +00001517 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001518 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001519 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001520 keycode = keycodes[i];
1521 if (keycode & 0x80)
1522 kbd_put_keycode(0xe0);
1523 kbd_put_keycode(keycode & 0x7f);
1524 }
balrogc8256f92008-06-08 22:45:01 +00001525 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001526 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001527 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001528}
1529
bellard13224a82006-07-14 22:03:35 +00001530static int mouse_button_state;
1531
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001532static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001533{
1534 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001535 const char *dx_str = qdict_get_str(qdict, "dx_str");
1536 const char *dy_str = qdict_get_str(qdict, "dy_str");
1537 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001538 dx = strtol(dx_str, NULL, 0);
1539 dy = strtol(dy_str, NULL, 0);
1540 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001541 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001542 dz = strtol(dz_str, NULL, 0);
1543 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1544}
1545
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001546static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001547{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001548 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001549 mouse_button_state = button_state;
1550 kbd_mouse_event(0, 0, 0, mouse_button_state);
1551}
1552
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001553static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001554{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001555 int size = qdict_get_int(qdict, "size");
1556 int addr = qdict_get_int(qdict, "addr");
1557 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001558 uint32_t val;
1559 int suffix;
1560
1561 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001562 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001563 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001564 addr++;
1565 }
1566 addr &= 0xffff;
1567
1568 switch(size) {
1569 default:
1570 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001571 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001572 suffix = 'b';
1573 break;
1574 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001575 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001576 suffix = 'w';
1577 break;
1578 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001579 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001580 suffix = 'l';
1581 break;
1582 }
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1584 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001585}
bellarda3a91a32004-06-04 11:06:21 +00001586
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001587static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001588{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001589 int size = qdict_get_int(qdict, "size");
1590 int addr = qdict_get_int(qdict, "addr");
1591 int val = qdict_get_int(qdict, "val");
1592
Jan Kiszkaf1147842009-07-14 10:20:11 +02001593 addr &= IOPORTS_MASK;
1594
1595 switch (size) {
1596 default:
1597 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001598 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001599 break;
1600 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001601 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001602 break;
1603 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001604 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001605 break;
1606 }
1607}
1608
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001609static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001610{
1611 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001612 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001613
Jan Kiszka76e30d02009-07-02 00:19:02 +02001614 res = qemu_boot_set(bootdevice);
1615 if (res == 0) {
1616 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1617 } else if (res > 0) {
1618 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001619 } else {
aliguori376253e2009-03-05 23:01:23 +00001620 monitor_printf(mon, "no function defined to set boot device list for "
1621 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001622 }
1623}
1624
bellardb86bda52004-09-18 19:32:46 +00001625#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001626static void print_pte(Monitor *mon, target_phys_addr_t addr,
1627 target_phys_addr_t pte,
1628 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001629{
Blue Swirld65aaf32010-12-11 18:56:24 +00001630#ifdef TARGET_X86_64
1631 if (addr & (1ULL << 47)) {
1632 addr |= -1LL << 48;
1633 }
1634#endif
1635 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1636 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001637 addr,
1638 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001639 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001640 pte & PG_GLOBAL_MASK ? 'G' : '-',
1641 pte & PG_PSE_MASK ? 'P' : '-',
1642 pte & PG_DIRTY_MASK ? 'D' : '-',
1643 pte & PG_ACCESSED_MASK ? 'A' : '-',
1644 pte & PG_PCD_MASK ? 'C' : '-',
1645 pte & PG_PWT_MASK ? 'T' : '-',
1646 pte & PG_USER_MASK ? 'U' : '-',
1647 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001648}
1649
Andreas Färber9349b4f2012-03-14 01:38:32 +01001650static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001651{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001652 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001653 uint32_t pgd, pde, pte;
1654
bellardb86bda52004-09-18 19:32:46 +00001655 pgd = env->cr[3] & ~0xfff;
1656 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001657 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001658 pde = le32_to_cpu(pde);
1659 if (pde & PG_PRESENT_MASK) {
1660 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001661 /* 4M pages */
1662 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001663 } else {
1664 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001665 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001666 pte = le32_to_cpu(pte);
1667 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001668 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001669 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001670 ~0xfff);
1671 }
1672 }
1673 }
1674 }
1675 }
1676}
1677
Andreas Färber9349b4f2012-03-14 01:38:32 +01001678static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001679{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001680 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001681 uint64_t pdpe, pde, pte;
1682 uint64_t pdp_addr, pd_addr, pt_addr;
1683
1684 pdp_addr = env->cr[3] & ~0x1f;
1685 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001686 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001687 pdpe = le64_to_cpu(pdpe);
1688 if (pdpe & PG_PRESENT_MASK) {
1689 pd_addr = pdpe & 0x3fffffffff000ULL;
1690 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001691 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001692 pde = le64_to_cpu(pde);
1693 if (pde & PG_PRESENT_MASK) {
1694 if (pde & PG_PSE_MASK) {
1695 /* 2M pages with PAE, CR4.PSE is ignored */
1696 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1697 ~((target_phys_addr_t)(1 << 20) - 1));
1698 } else {
1699 pt_addr = pde & 0x3fffffffff000ULL;
1700 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001701 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001702 pte = le64_to_cpu(pte);
1703 if (pte & PG_PRESENT_MASK) {
1704 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1705 + (l3 << 12),
1706 pte & ~PG_PSE_MASK,
1707 ~(target_phys_addr_t)0xfff);
1708 }
1709 }
1710 }
1711 }
1712 }
1713 }
1714 }
1715}
1716
1717#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001718static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001719{
1720 uint64_t l1, l2, l3, l4;
1721 uint64_t pml4e, pdpe, pde, pte;
1722 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1723
1724 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1725 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001726 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001727 pml4e = le64_to_cpu(pml4e);
1728 if (pml4e & PG_PRESENT_MASK) {
1729 pdp_addr = pml4e & 0x3fffffffff000ULL;
1730 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001731 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001732 pdpe = le64_to_cpu(pdpe);
1733 if (pdpe & PG_PRESENT_MASK) {
1734 if (pdpe & PG_PSE_MASK) {
1735 /* 1G pages, CR4.PSE is ignored */
1736 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1737 0x3ffffc0000000ULL);
1738 } else {
1739 pd_addr = pdpe & 0x3fffffffff000ULL;
1740 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001741 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001742 pde = le64_to_cpu(pde);
1743 if (pde & PG_PRESENT_MASK) {
1744 if (pde & PG_PSE_MASK) {
1745 /* 2M pages, CR4.PSE is ignored */
1746 print_pte(mon, (l1 << 39) + (l2 << 30) +
1747 (l3 << 21), pde,
1748 0x3ffffffe00000ULL);
1749 } else {
1750 pt_addr = pde & 0x3fffffffff000ULL;
1751 for (l4 = 0; l4 < 512; l4++) {
1752 cpu_physical_memory_read(pt_addr
1753 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001754 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001755 pte = le64_to_cpu(pte);
1756 if (pte & PG_PRESENT_MASK) {
1757 print_pte(mon, (l1 << 39) +
1758 (l2 << 30) +
1759 (l3 << 21) + (l4 << 12),
1760 pte & ~PG_PSE_MASK,
1761 0x3fffffffff000ULL);
1762 }
1763 }
1764 }
1765 }
1766 }
1767 }
1768 }
1769 }
1770 }
1771 }
1772}
1773#endif
1774
1775static void tlb_info(Monitor *mon)
1776{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001777 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001778
1779 env = mon_get_cpu();
1780
1781 if (!(env->cr[0] & CR0_PG_MASK)) {
1782 monitor_printf(mon, "PG disabled\n");
1783 return;
1784 }
1785 if (env->cr[4] & CR4_PAE_MASK) {
1786#ifdef TARGET_X86_64
1787 if (env->hflags & HF_LMA_MASK) {
1788 tlb_info_64(mon, env);
1789 } else
1790#endif
1791 {
1792 tlb_info_pae32(mon, env);
1793 }
1794 } else {
1795 tlb_info_32(mon, env);
1796 }
1797}
1798
Blue Swirl1b3cba62010-12-11 18:56:27 +00001799static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
1800 int *plast_prot,
1801 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001802{
bellard9746b152004-11-11 18:30:24 +00001803 int prot1;
1804 prot1 = *plast_prot;
1805 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001806 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001807 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1808 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001809 *pstart, end, end - *pstart,
1810 prot1 & PG_USER_MASK ? 'u' : '-',
1811 'r',
1812 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001813 }
1814 if (prot != 0)
1815 *pstart = end;
1816 else
1817 *pstart = -1;
1818 *plast_prot = prot;
1819 }
1820}
1821
Andreas Färber9349b4f2012-03-14 01:38:32 +01001822static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001823{
Austin Clementsb49ca722011-08-14 23:19:21 -04001824 unsigned int l1, l2;
1825 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001826 uint32_t pgd, pde, pte;
1827 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00001828
bellardb86bda52004-09-18 19:32:46 +00001829 pgd = env->cr[3] & ~0xfff;
1830 last_prot = 0;
1831 start = -1;
1832 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001833 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001834 pde = le32_to_cpu(pde);
1835 end = l1 << 22;
1836 if (pde & PG_PRESENT_MASK) {
1837 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1838 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001839 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001840 } else {
1841 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001842 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001843 pte = le32_to_cpu(pte);
1844 end = (l1 << 22) + (l2 << 12);
1845 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001846 prot = pte & pde &
1847 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001848 } else {
1849 prot = 0;
1850 }
aliguori376253e2009-03-05 23:01:23 +00001851 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001852 }
1853 }
1854 } else {
1855 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001856 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001857 }
1858 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001859 /* Flush last range */
1860 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001861}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001862
Andreas Färber9349b4f2012-03-14 01:38:32 +01001863static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001864{
Austin Clementsb49ca722011-08-14 23:19:21 -04001865 unsigned int l1, l2, l3;
1866 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001867 uint64_t pdpe, pde, pte;
1868 uint64_t pdp_addr, pd_addr, pt_addr;
1869 target_phys_addr_t start, end;
1870
1871 pdp_addr = env->cr[3] & ~0x1f;
1872 last_prot = 0;
1873 start = -1;
1874 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001875 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001876 pdpe = le64_to_cpu(pdpe);
1877 end = l1 << 30;
1878 if (pdpe & PG_PRESENT_MASK) {
1879 pd_addr = pdpe & 0x3fffffffff000ULL;
1880 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001881 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001882 pde = le64_to_cpu(pde);
1883 end = (l1 << 30) + (l2 << 21);
1884 if (pde & PG_PRESENT_MASK) {
1885 if (pde & PG_PSE_MASK) {
1886 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1887 PG_PRESENT_MASK);
1888 mem_print(mon, &start, &last_prot, end, prot);
1889 } else {
1890 pt_addr = pde & 0x3fffffffff000ULL;
1891 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001892 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001893 pte = le64_to_cpu(pte);
1894 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1895 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001896 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1897 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001898 } else {
1899 prot = 0;
1900 }
1901 mem_print(mon, &start, &last_prot, end, prot);
1902 }
1903 }
1904 } else {
1905 prot = 0;
1906 mem_print(mon, &start, &last_prot, end, prot);
1907 }
1908 }
1909 } else {
1910 prot = 0;
1911 mem_print(mon, &start, &last_prot, end, prot);
1912 }
1913 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001914 /* Flush last range */
1915 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001916}
1917
1918
1919#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001920static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001921{
1922 int prot, last_prot;
1923 uint64_t l1, l2, l3, l4;
1924 uint64_t pml4e, pdpe, pde, pte;
1925 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1926
1927 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1928 last_prot = 0;
1929 start = -1;
1930 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001931 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001932 pml4e = le64_to_cpu(pml4e);
1933 end = l1 << 39;
1934 if (pml4e & PG_PRESENT_MASK) {
1935 pdp_addr = pml4e & 0x3fffffffff000ULL;
1936 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001937 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001938 pdpe = le64_to_cpu(pdpe);
1939 end = (l1 << 39) + (l2 << 30);
1940 if (pdpe & PG_PRESENT_MASK) {
1941 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001942 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1943 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001944 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001945 mem_print(mon, &start, &last_prot, end, prot);
1946 } else {
1947 pd_addr = pdpe & 0x3fffffffff000ULL;
1948 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001949 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001950 pde = le64_to_cpu(pde);
1951 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1952 if (pde & PG_PRESENT_MASK) {
1953 if (pde & PG_PSE_MASK) {
1954 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1955 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001956 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001957 mem_print(mon, &start, &last_prot, end, prot);
1958 } else {
1959 pt_addr = pde & 0x3fffffffff000ULL;
1960 for (l4 = 0; l4 < 512; l4++) {
1961 cpu_physical_memory_read(pt_addr
1962 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001963 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001964 pte = le64_to_cpu(pte);
1965 end = (l1 << 39) + (l2 << 30) +
1966 (l3 << 21) + (l4 << 12);
1967 if (pte & PG_PRESENT_MASK) {
1968 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1969 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001970 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001971 } else {
1972 prot = 0;
1973 }
1974 mem_print(mon, &start, &last_prot, end, prot);
1975 }
1976 }
1977 } else {
1978 prot = 0;
1979 mem_print(mon, &start, &last_prot, end, prot);
1980 }
1981 }
1982 }
1983 } else {
1984 prot = 0;
1985 mem_print(mon, &start, &last_prot, end, prot);
1986 }
1987 }
1988 } else {
1989 prot = 0;
1990 mem_print(mon, &start, &last_prot, end, prot);
1991 }
1992 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001993 /* Flush last range */
1994 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001995}
1996#endif
1997
1998static void mem_info(Monitor *mon)
1999{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002000 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002001
2002 env = mon_get_cpu();
2003
2004 if (!(env->cr[0] & CR0_PG_MASK)) {
2005 monitor_printf(mon, "PG disabled\n");
2006 return;
2007 }
2008 if (env->cr[4] & CR4_PAE_MASK) {
2009#ifdef TARGET_X86_64
2010 if (env->hflags & HF_LMA_MASK) {
2011 mem_info_64(mon, env);
2012 } else
2013#endif
2014 {
2015 mem_info_pae32(mon, env);
2016 }
2017 } else {
2018 mem_info_32(mon, env);
2019 }
2020}
bellardb86bda52004-09-18 19:32:46 +00002021#endif
2022
aurel327c664e22009-03-03 06:12:22 +00002023#if defined(TARGET_SH4)
2024
aliguori376253e2009-03-05 23:01:23 +00002025static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002026{
aliguori376253e2009-03-05 23:01:23 +00002027 monitor_printf(mon, " tlb%i:\t"
2028 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2029 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2030 "dirty=%hhu writethrough=%hhu\n",
2031 idx,
2032 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2033 tlb->v, tlb->sh, tlb->c, tlb->pr,
2034 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002035}
2036
aliguori376253e2009-03-05 23:01:23 +00002037static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002038{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002039 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00002040 int i;
2041
aliguori376253e2009-03-05 23:01:23 +00002042 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002043 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002044 print_tlb (mon, i, &env->itlb[i]);
2045 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002046 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002047 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002048}
2049
2050#endif
2051
Max Filippov692f7372012-01-07 20:02:40 +04002052#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Blue Swirld41160a2010-12-19 13:42:56 +00002053static void tlb_info(Monitor *mon)
2054{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002055 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00002056
2057 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2058}
2059#endif
2060
Blue Swirl314e2982011-09-11 20:22:05 +00002061static void do_info_mtree(Monitor *mon)
2062{
2063 mtree_info((fprintf_function)monitor_printf, mon);
2064}
2065
aliguori030ea372009-04-21 22:30:47 +00002066static void do_info_numa(Monitor *mon)
2067{
aliguorib28b6232009-04-22 20:20:29 +00002068 int i;
Andreas Färber9349b4f2012-03-14 01:38:32 +01002069 CPUArchState *env;
aliguori030ea372009-04-21 22:30:47 +00002070
2071 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2072 for (i = 0; i < nb_numa_nodes; i++) {
2073 monitor_printf(mon, "node %d cpus:", i);
2074 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2075 if (env->numa_node == i) {
2076 monitor_printf(mon, " %d", env->cpu_index);
2077 }
2078 }
2079 monitor_printf(mon, "\n");
2080 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2081 node_mem[i] >> 20);
2082 }
2083}
2084
bellard5f1ce942006-02-08 22:40:15 +00002085#ifdef CONFIG_PROFILER
2086
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002087int64_t qemu_time;
2088int64_t dev_time;
2089
aliguori376253e2009-03-05 23:01:23 +00002090static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002091{
2092 int64_t total;
2093 total = qemu_time;
2094 if (total == 0)
2095 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002096 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002097 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002098 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002099 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002100 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002101 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002102}
2103#else
aliguori376253e2009-03-05 23:01:23 +00002104static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002105{
aliguori376253e2009-03-05 23:01:23 +00002106 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002107}
2108#endif
2109
bellardec36b692006-07-16 18:57:03 +00002110/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002111static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002112
aliguori376253e2009-03-05 23:01:23 +00002113static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002114{
2115 int i;
2116 CaptureState *s;
2117
2118 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002119 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002120 s->ops.info (s->opaque);
2121 }
2122}
2123
Blue Swirl23130862009-06-06 08:22:04 +00002124#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002125static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002126{
2127 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002128 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002129 CaptureState *s;
2130
2131 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2132 if (i == n) {
2133 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002134 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002135 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002136 return;
2137 }
2138 }
2139}
2140
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002141static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002142{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002143 const char *path = qdict_get_str(qdict, "path");
2144 int has_freq = qdict_haskey(qdict, "freq");
2145 int freq = qdict_get_try_int(qdict, "freq", -1);
2146 int has_bits = qdict_haskey(qdict, "bits");
2147 int bits = qdict_get_try_int(qdict, "bits", -1);
2148 int has_channels = qdict_haskey(qdict, "nchannels");
2149 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002150 CaptureState *s;
2151
Anthony Liguori7267c092011-08-20 22:09:37 -05002152 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002153
2154 freq = has_freq ? freq : 44100;
2155 bits = has_bits ? bits : 16;
2156 nchannels = has_channels ? nchannels : 2;
2157
2158 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002159 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002160 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002161 return;
bellardec36b692006-07-16 18:57:03 +00002162 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002163 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002164}
2165#endif
2166
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002167static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002168{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002169 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002170
aliguori76655d62009-03-06 20:27:37 +00002171 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002172 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002173 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002174 return acl;
2175}
aliguori76655d62009-03-06 20:27:37 +00002176
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002177static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002178{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002179 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180 qemu_acl *acl = find_acl(mon, aclname);
2181 qemu_acl_entry *entry;
2182 int i = 0;
2183
2184 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002185 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002186 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002187 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002188 i++;
2189 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002190 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002191 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002192 }
2193}
2194
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002195static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002196{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002197 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002198 qemu_acl *acl = find_acl(mon, aclname);
2199
2200 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002201 qemu_acl_reset(acl);
2202 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002203 }
2204}
aliguori76655d62009-03-06 20:27:37 +00002205
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002206static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002207{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002208 const char *aclname = qdict_get_str(qdict, "aclname");
2209 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002210 qemu_acl *acl = find_acl(mon, aclname);
2211
2212 if (acl) {
2213 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002214 acl->defaultDeny = 0;
2215 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002216 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002217 acl->defaultDeny = 1;
2218 monitor_printf(mon, "acl: policy set to 'deny'\n");
2219 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002220 monitor_printf(mon, "acl: unknown policy '%s', "
2221 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002222 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002223 }
2224}
aliguori76655d62009-03-06 20:27:37 +00002225
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002226static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002227{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002228 const char *aclname = qdict_get_str(qdict, "aclname");
2229 const char *match = qdict_get_str(qdict, "match");
2230 const char *policy = qdict_get_str(qdict, "policy");
2231 int has_index = qdict_haskey(qdict, "index");
2232 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002233 qemu_acl *acl = find_acl(mon, aclname);
2234 int deny, ret;
2235
2236 if (acl) {
2237 if (strcmp(policy, "allow") == 0) {
2238 deny = 0;
2239 } else if (strcmp(policy, "deny") == 0) {
2240 deny = 1;
2241 } else {
2242 monitor_printf(mon, "acl: unknown policy '%s', "
2243 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002244 return;
2245 }
aliguori28a76be2009-03-06 20:27:40 +00002246 if (has_index)
2247 ret = qemu_acl_insert(acl, deny, match, index);
2248 else
2249 ret = qemu_acl_append(acl, deny, match);
2250 if (ret < 0)
2251 monitor_printf(mon, "acl: unable to add acl entry\n");
2252 else
2253 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002254 }
2255}
aliguori76655d62009-03-06 20:27:37 +00002256
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002257static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002258{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002259 const char *aclname = qdict_get_str(qdict, "aclname");
2260 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002261 qemu_acl *acl = find_acl(mon, aclname);
2262 int ret;
aliguori76655d62009-03-06 20:27:37 +00002263
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002264 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002265 ret = qemu_acl_remove(acl, match);
2266 if (ret < 0)
2267 monitor_printf(mon, "acl: no matching acl entry\n");
2268 else
2269 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002270 }
2271}
2272
Huang Ying79c4f6b2009-06-23 10:05:14 +08002273#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002274static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002275{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002276 CPUArchState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002277 int cpu_index = qdict_get_int(qdict, "cpu_index");
2278 int bank = qdict_get_int(qdict, "bank");
2279 uint64_t status = qdict_get_int(qdict, "status");
2280 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2281 uint64_t addr = qdict_get_int(qdict, "addr");
2282 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002283 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002284
Jan Kiszka747461c2011-03-02 08:56:10 +01002285 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2286 flags |= MCE_INJECT_BROADCAST;
2287 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002288 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002289 if (cenv->cpu_index == cpu_index) {
2290 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002291 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002292 break;
2293 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002294 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002295}
2296#endif
2297
Corey Bryant208c9d12012-06-22 14:36:09 -04002298void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002299{
Anthony Liguoric227f092009-10-01 16:12:16 -05002300 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002301 int fd;
2302
Corey Bryant208c9d12012-06-22 14:36:09 -04002303 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002304 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002305 error_set(errp, QERR_FD_NOT_SUPPLIED);
2306 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002307 }
2308
2309 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002310 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2311 "a name not starting with a digit");
2312 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002313 }
2314
Corey Bryant208c9d12012-06-22 14:36:09 -04002315 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002316 if (strcmp(monfd->name, fdname) != 0) {
2317 continue;
2318 }
2319
2320 close(monfd->fd);
2321 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002322 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002323 }
2324
Anthony Liguori7267c092011-08-20 22:09:37 -05002325 monfd = g_malloc0(sizeof(mon_fd_t));
2326 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002327 monfd->fd = fd;
2328
Corey Bryant208c9d12012-06-22 14:36:09 -04002329 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002330}
2331
Corey Bryant208c9d12012-06-22 14:36:09 -04002332void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002333{
Anthony Liguoric227f092009-10-01 16:12:16 -05002334 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002335
Corey Bryant208c9d12012-06-22 14:36:09 -04002336 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002337 if (strcmp(monfd->name, fdname) != 0) {
2338 continue;
2339 }
2340
Blue Swirl72cf2d42009-09-12 07:36:22 +00002341 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002342 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002343 g_free(monfd->name);
2344 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002345 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002346 }
2347
Corey Bryant208c9d12012-06-22 14:36:09 -04002348 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002349}
2350
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002351static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002352{
Luiz Capitulino13548692011-07-29 15:36:43 -03002353 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002354 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002355
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002356 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002357
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002358 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002359 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002360 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002361}
2362
Mark McLoughlin7768e042009-07-22 09:11:41 +01002363int monitor_get_fd(Monitor *mon, const char *fdname)
2364{
Anthony Liguoric227f092009-10-01 16:12:16 -05002365 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002366
Blue Swirl72cf2d42009-09-12 07:36:22 +00002367 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002368 int fd;
2369
2370 if (strcmp(monfd->name, fdname) != 0) {
2371 continue;
2372 }
2373
2374 fd = monfd->fd;
2375
2376 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002377 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002378 g_free(monfd->name);
2379 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002380
2381 return fd;
2382 }
2383
2384 return -1;
2385}
2386
Corey Bryantba1c0482012-08-14 16:43:43 -04002387static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2388{
2389 MonFdsetFd *mon_fdset_fd;
2390 MonFdsetFd *mon_fdset_fd_next;
2391
2392 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2393 if (mon_fdset_fd->removed) {
2394 close(mon_fdset_fd->fd);
2395 g_free(mon_fdset_fd->opaque);
2396 QLIST_REMOVE(mon_fdset_fd, next);
2397 g_free(mon_fdset_fd);
2398 }
2399 }
2400
2401 if (QLIST_EMPTY(&mon_fdset->fds)) {
2402 QLIST_REMOVE(mon_fdset, next);
2403 g_free(mon_fdset);
2404 }
2405}
2406
2407AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2408 const char *opaque, Error **errp)
2409{
2410 int fd;
2411 Monitor *mon = cur_mon;
2412 MonFdset *mon_fdset;
2413 MonFdsetFd *mon_fdset_fd;
2414 AddfdInfo *fdinfo;
2415
2416 fd = qemu_chr_fe_get_msgfd(mon->chr);
2417 if (fd == -1) {
2418 error_set(errp, QERR_FD_NOT_SUPPLIED);
2419 goto error;
2420 }
2421
2422 if (has_fdset_id) {
2423 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2424 if (mon_fdset->id == fdset_id) {
2425 break;
2426 }
2427 }
2428 if (mon_fdset == NULL) {
2429 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2430 "an existing fdset-id");
2431 goto error;
2432 }
2433 } else {
2434 int64_t fdset_id_prev = -1;
2435 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2436
2437 /* Use first available fdset ID */
2438 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2439 mon_fdset_cur = mon_fdset;
2440 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2441 fdset_id_prev = mon_fdset_cur->id;
2442 continue;
2443 }
2444 break;
2445 }
2446
2447 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2448 mon_fdset->id = fdset_id_prev + 1;
2449
2450 /* The fdset list is ordered by fdset ID */
2451 if (mon_fdset->id == 0) {
2452 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2453 } else if (mon_fdset->id < mon_fdset_cur->id) {
2454 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2455 } else {
2456 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2457 }
2458 }
2459
2460 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2461 mon_fdset_fd->fd = fd;
2462 mon_fdset_fd->removed = false;
2463 if (has_opaque) {
2464 mon_fdset_fd->opaque = g_strdup(opaque);
2465 }
2466 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2467
2468 fdinfo = g_malloc0(sizeof(*fdinfo));
2469 fdinfo->fdset_id = mon_fdset->id;
2470 fdinfo->fd = mon_fdset_fd->fd;
2471
2472 return fdinfo;
2473
2474error:
2475 if (fd != -1) {
2476 close(fd);
2477 }
2478 return NULL;
2479}
2480
2481void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2482{
2483 MonFdset *mon_fdset;
2484 MonFdsetFd *mon_fdset_fd;
2485 char fd_str[60];
2486
2487 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2488 if (mon_fdset->id != fdset_id) {
2489 continue;
2490 }
2491 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2492 if (has_fd) {
2493 if (mon_fdset_fd->fd != fd) {
2494 continue;
2495 }
2496 mon_fdset_fd->removed = true;
2497 break;
2498 } else {
2499 mon_fdset_fd->removed = true;
2500 }
2501 }
2502 if (has_fd && !mon_fdset_fd) {
2503 goto error;
2504 }
2505 monitor_fdset_cleanup(mon_fdset);
2506 return;
2507 }
2508
2509error:
2510 if (has_fd) {
2511 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2512 fdset_id, fd);
2513 } else {
2514 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2515 }
2516 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2517}
2518
2519FdsetInfoList *qmp_query_fdsets(Error **errp)
2520{
2521 MonFdset *mon_fdset;
2522 MonFdsetFd *mon_fdset_fd;
2523 FdsetInfoList *fdset_list = NULL;
2524
2525 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2526 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2527 FdsetFdInfoList *fdsetfd_list = NULL;
2528
2529 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2530 fdset_info->value->fdset_id = mon_fdset->id;
2531
2532 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2533 FdsetFdInfoList *fdsetfd_info;
2534
2535 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2536 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2537 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2538 if (mon_fdset_fd->opaque) {
2539 fdsetfd_info->value->has_opaque = true;
2540 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2541 } else {
2542 fdsetfd_info->value->has_opaque = false;
2543 }
2544
2545 fdsetfd_info->next = fdsetfd_list;
2546 fdsetfd_list = fdsetfd_info;
2547 }
2548
2549 fdset_info->value->fds = fdsetfd_list;
2550
2551 fdset_info->next = fdset_list;
2552 fdset_list = fdset_info;
2553 }
2554
2555 return fdset_list;
2556}
2557
Wayne Xia816f8922011-10-12 11:32:41 +08002558/* mon_cmds and info_cmds would be sorted at runtime */
2559static mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002560#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002561 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002562};
2563
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002564/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002565static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 {
2567 .name = "version",
2568 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 .params = "",
2570 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002571 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
2573 {
2574 .name = "network",
2575 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 .params = "",
2577 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002578 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 },
2580 {
2581 .name = "chardev",
2582 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 .params = "",
2584 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002585 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 },
2587 {
2588 .name = "block",
2589 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 .params = "",
2591 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002592 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 },
2594 {
2595 .name = "blockstats",
2596 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 .params = "",
2598 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002599 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 },
2601 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002602 .name = "block-jobs",
2603 .args_type = "",
2604 .params = "",
2605 .help = "show progress of ongoing block device operations",
2606 .mhandler.info = hmp_info_block_jobs,
2607 },
2608 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 .name = "registers",
2610 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 .params = "",
2612 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002613 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 },
2615 {
2616 .name = "cpus",
2617 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 .params = "",
2619 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002620 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 },
2622 {
2623 .name = "history",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002627 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002629#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2630 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 {
2632 .name = "irq",
2633 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002634 .params = "",
2635 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002636#ifdef TARGET_SPARC
2637 .mhandler.info = sun4m_irq_info,
2638#elif defined(TARGET_LM32)
2639 .mhandler.info = lm32_irq_info,
2640#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002641 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002642#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
2644 {
2645 .name = "pic",
2646 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 .params = "",
2648 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002649#ifdef TARGET_SPARC
2650 .mhandler.info = sun4m_pic_info,
2651#elif defined(TARGET_LM32)
2652 .mhandler.info = lm32_do_pic_info,
2653#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002654 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002655#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002657#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 {
2659 .name = "pci",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002663 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002665#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002666 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 {
2668 .name = "tlb",
2669 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 .params = "",
2671 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002672 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 },
aurel327c664e22009-03-03 06:12:22 +00002674#endif
2675#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 {
2677 .name = "mem",
2678 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 .params = "",
2680 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002681 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 },
bellardb86bda52004-09-18 19:32:46 +00002683#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 {
Blue Swirl314e2982011-09-11 20:22:05 +00002685 .name = "mtree",
2686 .args_type = "",
2687 .params = "",
2688 .help = "show memory tree",
2689 .mhandler.info = do_info_mtree,
2690 },
2691 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 .name = "jit",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002696 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
2699 .name = "kvm",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002703 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "numa",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002710 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
2713 .name = "usb",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002717 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 },
2719 {
2720 .name = "usbhost",
2721 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 .params = "",
2723 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002724 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 },
2726 {
2727 .name = "profile",
2728 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 .params = "",
2730 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002731 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 },
2733 {
2734 .name = "capture",
2735 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 .params = "",
2737 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002738 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 },
2740 {
2741 .name = "snapshots",
2742 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 .params = "",
2744 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002745 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 },
2747 {
2748 .name = "status",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002752 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
2754 {
2755 .name = "pcmcia",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002759 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
2761 {
2762 .name = "mice",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002766 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 },
2768 {
2769 .name = "vnc",
2770 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .params = "",
2772 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002773 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002775#if defined(CONFIG_SPICE)
2776 {
2777 .name = "spice",
2778 .args_type = "",
2779 .params = "",
2780 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002781 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002782 },
2783#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 {
2785 .name = "name",
2786 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 .params = "",
2788 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002789 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002790 },
2791 {
2792 .name = "uuid",
2793 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 .params = "",
2795 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002796 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 },
j_mayer76a66252007-03-07 08:32:30 +00002798#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 {
2800 .name = "cpustats",
2801 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 .params = "",
2803 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002804 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 },
j_mayer76a66252007-03-07 08:32:30 +00002806#endif
blueswir131a60e22007-10-26 18:42:59 +00002807#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 {
2809 .name = "usernet",
2810 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 .params = "",
2812 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002813 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 },
blueswir131a60e22007-10-26 18:42:59 +00002815#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 {
2817 .name = "migrate",
2818 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002819 .params = "",
2820 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002821 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 },
2823 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002824 .name = "migrate_capabilities",
2825 .args_type = "",
2826 .params = "",
2827 .help = "show current migration capabilities",
2828 .mhandler.info = hmp_info_migrate_capabilities,
2829 },
2830 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002831 .name = "migrate_cache_size",
2832 .args_type = "",
2833 .params = "",
2834 .help = "show current migration xbzrle cache size",
2835 .mhandler.info = hmp_info_migrate_cache_size,
2836 },
2837 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 .name = "balloon",
2839 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 .params = "",
2841 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002842 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 },
2844 {
2845 .name = "qtree",
2846 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002847 .params = "",
2848 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002849 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 },
2851 {
2852 .name = "qdm",
2853 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002854 .params = "",
2855 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002856 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002857 },
2858 {
2859 .name = "roms",
2860 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002861 .params = "",
2862 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002863 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302865 {
2866 .name = "trace-events",
2867 .args_type = "",
2868 .params = "",
2869 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002870 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302871 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 {
2873 .name = NULL,
2874 },
bellard9dc39cb2004-03-14 21:38:27 +00002875};
2876
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002877static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002878#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002879 { /* NULL */ },
2880};
2881
bellard9307c4c2004-04-04 12:57:25 +00002882/*******************************************************************/
2883
2884static const char *pch;
2885static jmp_buf expr_env;
2886
bellard92a31b12005-02-10 22:00:52 +00002887#define MD_TLONG 0
2888#define MD_I32 1
2889
bellard9307c4c2004-04-04 12:57:25 +00002890typedef struct MonitorDef {
2891 const char *name;
2892 int offset;
blueswir18662d652008-10-02 18:32:44 +00002893 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002894 int type;
bellard9307c4c2004-04-04 12:57:25 +00002895} MonitorDef;
2896
bellard57206fd2004-04-25 18:54:52 +00002897#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002898static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002899{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002900 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002901 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002902}
2903#endif
2904
bellarda541f292004-04-12 20:39:29 +00002905#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002906static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002907{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002908 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002909 unsigned int u;
2910 int i;
2911
2912 u = 0;
2913 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002914 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002915
2916 return u;
2917}
2918
blueswir18662d652008-10-02 18:32:44 +00002919static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002920{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002921 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002922 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002923}
2924
blueswir18662d652008-10-02 18:32:44 +00002925static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002926{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002927 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002928 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002929}
bellard9fddaa02004-05-21 12:59:32 +00002930
blueswir18662d652008-10-02 18:32:44 +00002931static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002932{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002933 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002934 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002935}
2936
blueswir18662d652008-10-02 18:32:44 +00002937static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002938{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002939 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002940 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002941}
2942
blueswir18662d652008-10-02 18:32:44 +00002943static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002944{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002945 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002946 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002947}
bellarda541f292004-04-12 20:39:29 +00002948#endif
2949
bellarde95c8d52004-09-30 22:22:08 +00002950#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002951#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002952static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002953{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002954 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002955
2956 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002957}
bellard7b936c02005-10-30 17:05:13 +00002958#endif
bellarde95c8d52004-09-30 22:22:08 +00002959
blueswir18662d652008-10-02 18:32:44 +00002960static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002961{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002962 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002963 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002964}
2965#endif
2966
blueswir18662d652008-10-02 18:32:44 +00002967static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002968#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002969
2970#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002971 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2972 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2973 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002974
Andreas Färbere59d1672012-02-16 00:40:47 +01002975 { "eax", offsetof(CPUX86State, regs[0]) },
2976 { "ecx", offsetof(CPUX86State, regs[1]) },
2977 { "edx", offsetof(CPUX86State, regs[2]) },
2978 { "ebx", offsetof(CPUX86State, regs[3]) },
2979 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2980 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2981 { "esi", offsetof(CPUX86State, regs[6]) },
2982 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002983#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002984 { "r8", offsetof(CPUX86State, regs[8]) },
2985 { "r9", offsetof(CPUX86State, regs[9]) },
2986 { "r10", offsetof(CPUX86State, regs[10]) },
2987 { "r11", offsetof(CPUX86State, regs[11]) },
2988 { "r12", offsetof(CPUX86State, regs[12]) },
2989 { "r13", offsetof(CPUX86State, regs[13]) },
2990 { "r14", offsetof(CPUX86State, regs[14]) },
2991 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002992#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002993 { "eflags", offsetof(CPUX86State, eflags) },
2994 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002995 SEG("cs", R_CS)
2996 SEG("ds", R_DS)
2997 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002998 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002999 SEG("fs", R_FS)
3000 SEG("gs", R_GS)
3001 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003002#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003003 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003004 { "r0", offsetof(CPUPPCState, gpr[0]) },
3005 { "r1", offsetof(CPUPPCState, gpr[1]) },
3006 { "r2", offsetof(CPUPPCState, gpr[2]) },
3007 { "r3", offsetof(CPUPPCState, gpr[3]) },
3008 { "r4", offsetof(CPUPPCState, gpr[4]) },
3009 { "r5", offsetof(CPUPPCState, gpr[5]) },
3010 { "r6", offsetof(CPUPPCState, gpr[6]) },
3011 { "r7", offsetof(CPUPPCState, gpr[7]) },
3012 { "r8", offsetof(CPUPPCState, gpr[8]) },
3013 { "r9", offsetof(CPUPPCState, gpr[9]) },
3014 { "r10", offsetof(CPUPPCState, gpr[10]) },
3015 { "r11", offsetof(CPUPPCState, gpr[11]) },
3016 { "r12", offsetof(CPUPPCState, gpr[12]) },
3017 { "r13", offsetof(CPUPPCState, gpr[13]) },
3018 { "r14", offsetof(CPUPPCState, gpr[14]) },
3019 { "r15", offsetof(CPUPPCState, gpr[15]) },
3020 { "r16", offsetof(CPUPPCState, gpr[16]) },
3021 { "r17", offsetof(CPUPPCState, gpr[17]) },
3022 { "r18", offsetof(CPUPPCState, gpr[18]) },
3023 { "r19", offsetof(CPUPPCState, gpr[19]) },
3024 { "r20", offsetof(CPUPPCState, gpr[20]) },
3025 { "r21", offsetof(CPUPPCState, gpr[21]) },
3026 { "r22", offsetof(CPUPPCState, gpr[22]) },
3027 { "r23", offsetof(CPUPPCState, gpr[23]) },
3028 { "r24", offsetof(CPUPPCState, gpr[24]) },
3029 { "r25", offsetof(CPUPPCState, gpr[25]) },
3030 { "r26", offsetof(CPUPPCState, gpr[26]) },
3031 { "r27", offsetof(CPUPPCState, gpr[27]) },
3032 { "r28", offsetof(CPUPPCState, gpr[28]) },
3033 { "r29", offsetof(CPUPPCState, gpr[29]) },
3034 { "r30", offsetof(CPUPPCState, gpr[30]) },
3035 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003036 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003037 { "f0", offsetof(CPUPPCState, fpr[0]) },
3038 { "f1", offsetof(CPUPPCState, fpr[1]) },
3039 { "f2", offsetof(CPUPPCState, fpr[2]) },
3040 { "f3", offsetof(CPUPPCState, fpr[3]) },
3041 { "f4", offsetof(CPUPPCState, fpr[4]) },
3042 { "f5", offsetof(CPUPPCState, fpr[5]) },
3043 { "f6", offsetof(CPUPPCState, fpr[6]) },
3044 { "f7", offsetof(CPUPPCState, fpr[7]) },
3045 { "f8", offsetof(CPUPPCState, fpr[8]) },
3046 { "f9", offsetof(CPUPPCState, fpr[9]) },
3047 { "f10", offsetof(CPUPPCState, fpr[10]) },
3048 { "f11", offsetof(CPUPPCState, fpr[11]) },
3049 { "f12", offsetof(CPUPPCState, fpr[12]) },
3050 { "f13", offsetof(CPUPPCState, fpr[13]) },
3051 { "f14", offsetof(CPUPPCState, fpr[14]) },
3052 { "f15", offsetof(CPUPPCState, fpr[15]) },
3053 { "f16", offsetof(CPUPPCState, fpr[16]) },
3054 { "f17", offsetof(CPUPPCState, fpr[17]) },
3055 { "f18", offsetof(CPUPPCState, fpr[18]) },
3056 { "f19", offsetof(CPUPPCState, fpr[19]) },
3057 { "f20", offsetof(CPUPPCState, fpr[20]) },
3058 { "f21", offsetof(CPUPPCState, fpr[21]) },
3059 { "f22", offsetof(CPUPPCState, fpr[22]) },
3060 { "f23", offsetof(CPUPPCState, fpr[23]) },
3061 { "f24", offsetof(CPUPPCState, fpr[24]) },
3062 { "f25", offsetof(CPUPPCState, fpr[25]) },
3063 { "f26", offsetof(CPUPPCState, fpr[26]) },
3064 { "f27", offsetof(CPUPPCState, fpr[27]) },
3065 { "f28", offsetof(CPUPPCState, fpr[28]) },
3066 { "f29", offsetof(CPUPPCState, fpr[29]) },
3067 { "f30", offsetof(CPUPPCState, fpr[30]) },
3068 { "f31", offsetof(CPUPPCState, fpr[31]) },
3069 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003070 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003071 { "nip|pc", offsetof(CPUPPCState, nip) },
3072 { "lr", offsetof(CPUPPCState, lr) },
3073 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003074 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003075 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003076 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003077 { "msr", 0, &monitor_get_msr, },
3078 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003079 { "tbu", 0, &monitor_get_tbu, },
3080 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003081#if defined(TARGET_PPC64)
3082 /* Address space register */
Andreas Färbere59d1672012-02-16 00:40:47 +01003083 { "asr", offsetof(CPUPPCState, asr) },
j_mayerff937db2007-09-19 05:49:13 +00003084#endif
3085 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003086 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3087 { "sr0", offsetof(CPUPPCState, sr[0]) },
3088 { "sr1", offsetof(CPUPPCState, sr[1]) },
3089 { "sr2", offsetof(CPUPPCState, sr[2]) },
3090 { "sr3", offsetof(CPUPPCState, sr[3]) },
3091 { "sr4", offsetof(CPUPPCState, sr[4]) },
3092 { "sr5", offsetof(CPUPPCState, sr[5]) },
3093 { "sr6", offsetof(CPUPPCState, sr[6]) },
3094 { "sr7", offsetof(CPUPPCState, sr[7]) },
3095 { "sr8", offsetof(CPUPPCState, sr[8]) },
3096 { "sr9", offsetof(CPUPPCState, sr[9]) },
3097 { "sr10", offsetof(CPUPPCState, sr[10]) },
3098 { "sr11", offsetof(CPUPPCState, sr[11]) },
3099 { "sr12", offsetof(CPUPPCState, sr[12]) },
3100 { "sr13", offsetof(CPUPPCState, sr[13]) },
3101 { "sr14", offsetof(CPUPPCState, sr[14]) },
3102 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003103 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003104 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003105
Andreas Färbere59d1672012-02-16 00:40:47 +01003106 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3107 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3108 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3109 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3110 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3111 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3112 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3113 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3114 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3115 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3116 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3117 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3118 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3119 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3120 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3121 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3122 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3123 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3124 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3125 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3126 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3127 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3128 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3129 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3130 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3131 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3132 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3133 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3134 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3135 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3136 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3137 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3138 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3139 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3140 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3141 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3142 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3143 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3144 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3145 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3146 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3147 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3148 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3149 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3150 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3151 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3152 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3153 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3154 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3155 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3156 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3157 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3158 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3159 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3160 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3161 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3162 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3163 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3164 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3165 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3166 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3167 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3168 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3169 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3170 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3171 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003172
bellarde95c8d52004-09-30 22:22:08 +00003173#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003174 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3175 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3176 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3177 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3178 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3179 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3180 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3181 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003182 { "o0", 0, monitor_get_reg },
3183 { "o1", 1, monitor_get_reg },
3184 { "o2", 2, monitor_get_reg },
3185 { "o3", 3, monitor_get_reg },
3186 { "o4", 4, monitor_get_reg },
3187 { "o5", 5, monitor_get_reg },
3188 { "o6", 6, monitor_get_reg },
3189 { "o7", 7, monitor_get_reg },
3190 { "l0", 8, monitor_get_reg },
3191 { "l1", 9, monitor_get_reg },
3192 { "l2", 10, monitor_get_reg },
3193 { "l3", 11, monitor_get_reg },
3194 { "l4", 12, monitor_get_reg },
3195 { "l5", 13, monitor_get_reg },
3196 { "l6", 14, monitor_get_reg },
3197 { "l7", 15, monitor_get_reg },
3198 { "i0", 16, monitor_get_reg },
3199 { "i1", 17, monitor_get_reg },
3200 { "i2", 18, monitor_get_reg },
3201 { "i3", 19, monitor_get_reg },
3202 { "i4", 20, monitor_get_reg },
3203 { "i5", 21, monitor_get_reg },
3204 { "i6", 22, monitor_get_reg },
3205 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003206 { "pc", offsetof(CPUSPARCState, pc) },
3207 { "npc", offsetof(CPUSPARCState, npc) },
3208 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003209#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003210 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003211 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003212#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003213 { "tbr", offsetof(CPUSPARCState, tbr) },
3214 { "fsr", offsetof(CPUSPARCState, fsr) },
3215 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3216 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3217 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3218 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3219 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3220 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3221 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3222 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3223 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3224 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3225 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3226 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3227 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3228 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3229 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3230 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3231 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3232 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3233 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3234 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3235 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3236 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3237 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3238 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3239 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3240 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3241 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3242 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3243 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3244 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3245 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3246 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003247#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003248 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3249 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3250 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3251 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3252 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3253 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3254 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3255 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3256 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3257 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3258 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3259 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3260 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3261 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3262 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3263 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3264 { "asi", offsetof(CPUSPARCState, asi) },
3265 { "pstate", offsetof(CPUSPARCState, pstate) },
3266 { "cansave", offsetof(CPUSPARCState, cansave) },
3267 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3268 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3269 { "wstate", offsetof(CPUSPARCState, wstate) },
3270 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3271 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003272#endif
bellard9307c4c2004-04-04 12:57:25 +00003273#endif
3274 { NULL },
3275};
3276
aliguori376253e2009-03-05 23:01:23 +00003277static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003278{
aliguori376253e2009-03-05 23:01:23 +00003279 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003280 longjmp(expr_env, 1);
3281}
3282
Markus Armbruster09b94182010-01-20 13:07:30 +01003283/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003284static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003285{
blueswir18662d652008-10-02 18:32:44 +00003286 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003287 void *ptr;
3288
bellard9307c4c2004-04-04 12:57:25 +00003289 for(md = monitor_defs; md->name != NULL; md++) {
3290 if (compare_cmd(name, md->name)) {
3291 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003292 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003293 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003294 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003295 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003296 switch(md->type) {
3297 case MD_I32:
3298 *pval = *(int32_t *)ptr;
3299 break;
3300 case MD_TLONG:
3301 *pval = *(target_long *)ptr;
3302 break;
3303 default:
3304 *pval = 0;
3305 break;
3306 }
bellard9307c4c2004-04-04 12:57:25 +00003307 }
3308 return 0;
3309 }
3310 }
3311 return -1;
3312}
3313
3314static void next(void)
3315{
Blue Swirl660f11b2009-07-31 21:16:51 +00003316 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003317 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003318 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003319 pch++;
3320 }
3321}
3322
aliguori376253e2009-03-05 23:01:23 +00003323static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003324
aliguori376253e2009-03-05 23:01:23 +00003325static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003326{
blueswir1c2efc952007-09-25 17:28:42 +00003327 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003328 char *p;
bellard6a00d602005-11-21 23:25:50 +00003329 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003330
3331 switch(*pch) {
3332 case '+':
3333 next();
aliguori376253e2009-03-05 23:01:23 +00003334 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003335 break;
3336 case '-':
3337 next();
aliguori376253e2009-03-05 23:01:23 +00003338 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003339 break;
3340 case '~':
3341 next();
aliguori376253e2009-03-05 23:01:23 +00003342 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003343 break;
3344 case '(':
3345 next();
aliguori376253e2009-03-05 23:01:23 +00003346 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003347 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003348 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003349 }
3350 next();
3351 break;
bellard81d09122004-07-14 17:21:37 +00003352 case '\'':
3353 pch++;
3354 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003355 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003356 n = *pch;
3357 pch++;
3358 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003359 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003360 next();
3361 break;
bellard9307c4c2004-04-04 12:57:25 +00003362 case '$':
3363 {
3364 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003365 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003366
bellard9307c4c2004-04-04 12:57:25 +00003367 pch++;
3368 q = buf;
3369 while ((*pch >= 'a' && *pch <= 'z') ||
3370 (*pch >= 'A' && *pch <= 'Z') ||
3371 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003372 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003373 if ((q - buf) < sizeof(buf) - 1)
3374 *q++ = *pch;
3375 pch++;
3376 }
blueswir1cd390082008-11-16 13:53:32 +00003377 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003378 pch++;
3379 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003380 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003381 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003382 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003383 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003384 }
3385 break;
3386 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003387 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003388 n = 0;
3389 break;
3390 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003391 errno = 0;
blueswir17743e582007-09-24 18:39:04 +00003392#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003393 n = strtoull(pch, &p, 0);
3394#else
bellard9307c4c2004-04-04 12:57:25 +00003395 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003396#endif
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003397 if (errno == ERANGE) {
3398 expr_error(mon, "number too large");
3399 }
bellard9307c4c2004-04-04 12:57:25 +00003400 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003401 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003402 }
3403 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003404 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003405 pch++;
3406 break;
3407 }
3408 return n;
3409}
3410
3411
aliguori376253e2009-03-05 23:01:23 +00003412static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003413{
blueswir1c2efc952007-09-25 17:28:42 +00003414 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003415 int op;
ths3b46e622007-09-17 08:09:54 +00003416
aliguori376253e2009-03-05 23:01:23 +00003417 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003418 for(;;) {
3419 op = *pch;
3420 if (op != '*' && op != '/' && op != '%')
3421 break;
3422 next();
aliguori376253e2009-03-05 23:01:23 +00003423 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003424 switch(op) {
3425 default:
3426 case '*':
3427 val *= val2;
3428 break;
3429 case '/':
3430 case '%':
ths5fafdf22007-09-16 21:08:06 +00003431 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003432 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003433 if (op == '/')
3434 val /= val2;
3435 else
3436 val %= val2;
3437 break;
3438 }
3439 }
3440 return val;
3441}
3442
aliguori376253e2009-03-05 23:01:23 +00003443static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003444{
blueswir1c2efc952007-09-25 17:28:42 +00003445 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003446 int op;
bellard9307c4c2004-04-04 12:57:25 +00003447
aliguori376253e2009-03-05 23:01:23 +00003448 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003449 for(;;) {
3450 op = *pch;
3451 if (op != '&' && op != '|' && op != '^')
3452 break;
3453 next();
aliguori376253e2009-03-05 23:01:23 +00003454 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003455 switch(op) {
3456 default:
3457 case '&':
3458 val &= val2;
3459 break;
3460 case '|':
3461 val |= val2;
3462 break;
3463 case '^':
3464 val ^= val2;
3465 break;
3466 }
3467 }
3468 return val;
3469}
3470
aliguori376253e2009-03-05 23:01:23 +00003471static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003472{
blueswir1c2efc952007-09-25 17:28:42 +00003473 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003474 int op;
bellard9307c4c2004-04-04 12:57:25 +00003475
aliguori376253e2009-03-05 23:01:23 +00003476 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003477 for(;;) {
3478 op = *pch;
3479 if (op != '+' && op != '-')
3480 break;
3481 next();
aliguori376253e2009-03-05 23:01:23 +00003482 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003483 if (op == '+')
3484 val += val2;
3485 else
3486 val -= val2;
3487 }
3488 return val;
3489}
3490
aliguori376253e2009-03-05 23:01:23 +00003491static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003492{
3493 pch = *pp;
3494 if (setjmp(expr_env)) {
3495 *pp = pch;
3496 return -1;
3497 }
blueswir1cd390082008-11-16 13:53:32 +00003498 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003499 pch++;
aliguori376253e2009-03-05 23:01:23 +00003500 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003501 *pp = pch;
3502 return 0;
3503}
3504
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003505static int get_double(Monitor *mon, double *pval, const char **pp)
3506{
3507 const char *p = *pp;
3508 char *tailp;
3509 double d;
3510
3511 d = strtod(p, &tailp);
3512 if (tailp == p) {
3513 monitor_printf(mon, "Number expected\n");
3514 return -1;
3515 }
3516 if (d != d || d - d != 0) {
3517 /* NaN or infinity */
3518 monitor_printf(mon, "Bad number\n");
3519 return -1;
3520 }
3521 *pval = d;
3522 *pp = tailp;
3523 return 0;
3524}
3525
bellard9307c4c2004-04-04 12:57:25 +00003526static int get_str(char *buf, int buf_size, const char **pp)
3527{
3528 const char *p;
3529 char *q;
3530 int c;
3531
bellard81d09122004-07-14 17:21:37 +00003532 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003533 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003534 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003535 p++;
3536 if (*p == '\0') {
3537 fail:
bellard81d09122004-07-14 17:21:37 +00003538 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003539 *pp = p;
3540 return -1;
3541 }
bellard9307c4c2004-04-04 12:57:25 +00003542 if (*p == '\"') {
3543 p++;
3544 while (*p != '\0' && *p != '\"') {
3545 if (*p == '\\') {
3546 p++;
3547 c = *p++;
3548 switch(c) {
3549 case 'n':
3550 c = '\n';
3551 break;
3552 case 'r':
3553 c = '\r';
3554 break;
3555 case '\\':
3556 case '\'':
3557 case '\"':
3558 break;
3559 default:
3560 qemu_printf("unsupported escape code: '\\%c'\n", c);
3561 goto fail;
3562 }
3563 if ((q - buf) < buf_size - 1) {
3564 *q++ = c;
3565 }
3566 } else {
3567 if ((q - buf) < buf_size - 1) {
3568 *q++ = *p;
3569 }
3570 p++;
3571 }
3572 }
3573 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003574 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003575 goto fail;
3576 }
3577 p++;
3578 } else {
blueswir1cd390082008-11-16 13:53:32 +00003579 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003580 if ((q - buf) < buf_size - 1) {
3581 *q++ = *p;
3582 }
3583 p++;
3584 }
bellard9307c4c2004-04-04 12:57:25 +00003585 }
bellard81d09122004-07-14 17:21:37 +00003586 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003587 *pp = p;
3588 return 0;
3589}
3590
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003591/*
3592 * Store the command-name in cmdname, and return a pointer to
3593 * the remaining of the command string.
3594 */
3595static const char *get_command_name(const char *cmdline,
3596 char *cmdname, size_t nlen)
3597{
3598 size_t len;
3599 const char *p, *pstart;
3600
3601 p = cmdline;
3602 while (qemu_isspace(*p))
3603 p++;
3604 if (*p == '\0')
3605 return NULL;
3606 pstart = p;
3607 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3608 p++;
3609 len = p - pstart;
3610 if (len > nlen - 1)
3611 len = nlen - 1;
3612 memcpy(cmdname, pstart, len);
3613 cmdname[len] = '\0';
3614 return p;
3615}
3616
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003617/**
3618 * Read key of 'type' into 'key' and return the current
3619 * 'type' pointer.
3620 */
3621static char *key_get_info(const char *type, char **key)
3622{
3623 size_t len;
3624 char *p, *str;
3625
3626 if (*type == ',')
3627 type++;
3628
3629 p = strchr(type, ':');
3630 if (!p) {
3631 *key = NULL;
3632 return NULL;
3633 }
3634 len = p - type;
3635
Anthony Liguori7267c092011-08-20 22:09:37 -05003636 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003637 memcpy(str, type, len);
3638 str[len] = '\0';
3639
3640 *key = str;
3641 return ++p;
3642}
3643
bellard9307c4c2004-04-04 12:57:25 +00003644static int default_fmt_format = 'x';
3645static int default_fmt_size = 4;
3646
3647#define MAX_ARGS 16
3648
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003649static int is_valid_option(const char *c, const char *typestr)
3650{
3651 char option[3];
3652
3653 option[0] = '-';
3654 option[1] = *c;
3655 option[2] = '\0';
3656
3657 typestr = strstr(typestr, option);
3658 return (typestr != NULL);
3659}
3660
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003661static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3662 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003663{
3664 const mon_cmd_t *cmd;
3665
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003666 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003667 if (compare_cmd(cmdname, cmd->name)) {
3668 return cmd;
3669 }
3670 }
3671
3672 return NULL;
3673}
3674
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003675static const mon_cmd_t *monitor_find_command(const char *cmdname)
3676{
3677 return search_dispatch_table(mon_cmds, cmdname);
3678}
3679
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003680static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3681{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003682 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003683}
3684
Anthony Liguoric227f092009-10-01 16:12:16 -05003685static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003686 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003687 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003688{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003689 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003690 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003691 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003692 char cmdname[256];
3693 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003694 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003695
3696#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003697 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003698#endif
ths3b46e622007-09-17 08:09:54 +00003699
bellard9307c4c2004-04-04 12:57:25 +00003700 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003701 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3702 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003703 return NULL;
ths3b46e622007-09-17 08:09:54 +00003704
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003705 cmd = monitor_find_command(cmdname);
3706 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003707 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003708 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003709 }
bellard9307c4c2004-04-04 12:57:25 +00003710
bellard9307c4c2004-04-04 12:57:25 +00003711 /* parse the parameters */
3712 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003713 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003714 typestr = key_get_info(typestr, &key);
3715 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003716 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003717 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003718 typestr++;
3719 switch(c) {
3720 case 'F':
bellard81d09122004-07-14 17:21:37 +00003721 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003722 case 's':
3723 {
3724 int ret;
ths3b46e622007-09-17 08:09:54 +00003725
blueswir1cd390082008-11-16 13:53:32 +00003726 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003727 p++;
3728 if (*typestr == '?') {
3729 typestr++;
3730 if (*p == '\0') {
3731 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003732 break;
bellard9307c4c2004-04-04 12:57:25 +00003733 }
3734 }
3735 ret = get_str(buf, sizeof(buf), &p);
3736 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003737 switch(c) {
3738 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003739 monitor_printf(mon, "%s: filename expected\n",
3740 cmdname);
bellard81d09122004-07-14 17:21:37 +00003741 break;
3742 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003743 monitor_printf(mon, "%s: block device name expected\n",
3744 cmdname);
bellard81d09122004-07-14 17:21:37 +00003745 break;
3746 default:
aliguori376253e2009-03-05 23:01:23 +00003747 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003748 break;
3749 }
bellard9307c4c2004-04-04 12:57:25 +00003750 goto fail;
3751 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003752 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003753 }
3754 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003755 case 'O':
3756 {
3757 QemuOptsList *opts_list;
3758 QemuOpts *opts;
3759
3760 opts_list = qemu_find_opts(key);
3761 if (!opts_list || opts_list->desc->name) {
3762 goto bad_type;
3763 }
3764 while (qemu_isspace(*p)) {
3765 p++;
3766 }
3767 if (!*p)
3768 break;
3769 if (get_str(buf, sizeof(buf), &p) < 0) {
3770 goto fail;
3771 }
3772 opts = qemu_opts_parse(opts_list, buf, 1);
3773 if (!opts) {
3774 goto fail;
3775 }
3776 qemu_opts_to_qdict(opts, qdict);
3777 qemu_opts_del(opts);
3778 }
3779 break;
bellard9307c4c2004-04-04 12:57:25 +00003780 case '/':
3781 {
3782 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003783
blueswir1cd390082008-11-16 13:53:32 +00003784 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003785 p++;
3786 if (*p == '/') {
3787 /* format found */
3788 p++;
3789 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003790 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003791 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003792 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003793 count = count * 10 + (*p - '0');
3794 p++;
3795 }
3796 }
3797 size = -1;
3798 format = -1;
3799 for(;;) {
3800 switch(*p) {
3801 case 'o':
3802 case 'd':
3803 case 'u':
3804 case 'x':
3805 case 'i':
3806 case 'c':
3807 format = *p++;
3808 break;
3809 case 'b':
3810 size = 1;
3811 p++;
3812 break;
3813 case 'h':
3814 size = 2;
3815 p++;
3816 break;
3817 case 'w':
3818 size = 4;
3819 p++;
3820 break;
3821 case 'g':
3822 case 'L':
3823 size = 8;
3824 p++;
3825 break;
3826 default:
3827 goto next;
3828 }
3829 }
3830 next:
blueswir1cd390082008-11-16 13:53:32 +00003831 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003832 monitor_printf(mon, "invalid char in format: '%c'\n",
3833 *p);
bellard9307c4c2004-04-04 12:57:25 +00003834 goto fail;
3835 }
bellard9307c4c2004-04-04 12:57:25 +00003836 if (format < 0)
3837 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003838 if (format != 'i') {
3839 /* for 'i', not specifying a size gives -1 as size */
3840 if (size < 0)
3841 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003842 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003843 }
bellard9307c4c2004-04-04 12:57:25 +00003844 default_fmt_format = format;
3845 } else {
3846 count = 1;
3847 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003848 if (format != 'i') {
3849 size = default_fmt_size;
3850 } else {
3851 size = -1;
3852 }
bellard9307c4c2004-04-04 12:57:25 +00003853 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003854 qdict_put(qdict, "count", qint_from_int(count));
3855 qdict_put(qdict, "format", qint_from_int(format));
3856 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003857 }
3858 break;
3859 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003860 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003861 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003862 {
blueswir1c2efc952007-09-25 17:28:42 +00003863 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003864
blueswir1cd390082008-11-16 13:53:32 +00003865 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003866 p++;
bellard34405572004-06-08 00:55:58 +00003867 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003868 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003869 if (*p == '\0') {
3870 typestr++;
3871 break;
3872 }
bellard34405572004-06-08 00:55:58 +00003873 } else {
3874 if (*p == '.') {
3875 p++;
blueswir1cd390082008-11-16 13:53:32 +00003876 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003877 p++;
bellard34405572004-06-08 00:55:58 +00003878 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003879 typestr++;
3880 break;
bellard34405572004-06-08 00:55:58 +00003881 }
3882 }
bellard13224a82006-07-14 22:03:35 +00003883 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003884 }
aliguori376253e2009-03-05 23:01:23 +00003885 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003886 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003887 /* Check if 'i' is greater than 32-bit */
3888 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3889 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3890 monitor_printf(mon, "integer is for 32-bit values\n");
3891 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003892 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003893 if (val < 0) {
3894 monitor_printf(mon, "enter a positive value\n");
3895 goto fail;
3896 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003897 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003898 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003899 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003900 }
3901 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003902 case 'o':
3903 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003904 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003905 char *end;
3906
3907 while (qemu_isspace(*p)) {
3908 p++;
3909 }
3910 if (*typestr == '?') {
3911 typestr++;
3912 if (*p == '\0') {
3913 break;
3914 }
3915 }
3916 val = strtosz(p, &end);
3917 if (val < 0) {
3918 monitor_printf(mon, "invalid size\n");
3919 goto fail;
3920 }
3921 qdict_put(qdict, key, qint_from_int(val));
3922 p = end;
3923 }
3924 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003925 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003926 {
3927 double val;
3928
3929 while (qemu_isspace(*p))
3930 p++;
3931 if (*typestr == '?') {
3932 typestr++;
3933 if (*p == '\0') {
3934 break;
3935 }
3936 }
3937 if (get_double(mon, &val, &p) < 0) {
3938 goto fail;
3939 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003940 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003941 switch (*p) {
3942 case 'm':
3943 val /= 1e3; p += 2; break;
3944 case 'u':
3945 val /= 1e6; p += 2; break;
3946 case 'n':
3947 val /= 1e9; p += 2; break;
3948 }
3949 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003950 if (*p && !qemu_isspace(*p)) {
3951 monitor_printf(mon, "Unknown unit suffix\n");
3952 goto fail;
3953 }
3954 qdict_put(qdict, key, qfloat_from_double(val));
3955 }
3956 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003957 case 'b':
3958 {
3959 const char *beg;
3960 int val;
3961
3962 while (qemu_isspace(*p)) {
3963 p++;
3964 }
3965 beg = p;
3966 while (qemu_isgraph(*p)) {
3967 p++;
3968 }
3969 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3970 val = 1;
3971 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3972 val = 0;
3973 } else {
3974 monitor_printf(mon, "Expected 'on' or 'off'\n");
3975 goto fail;
3976 }
3977 qdict_put(qdict, key, qbool_from_int(val));
3978 }
3979 break;
bellard9307c4c2004-04-04 12:57:25 +00003980 case '-':
3981 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003982 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003983 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003984 /* option */
ths3b46e622007-09-17 08:09:54 +00003985
bellard9307c4c2004-04-04 12:57:25 +00003986 c = *typestr++;
3987 if (c == '\0')
3988 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003989 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003990 p++;
bellard9307c4c2004-04-04 12:57:25 +00003991 if (*p == '-') {
3992 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003993 if(c != *p) {
3994 if(!is_valid_option(p, typestr)) {
3995
3996 monitor_printf(mon, "%s: unsupported option -%c\n",
3997 cmdname, *p);
3998 goto fail;
3999 } else {
4000 skip_key = 1;
4001 }
bellard9307c4c2004-04-04 12:57:25 +00004002 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004003 if(skip_key) {
4004 p = tmp;
4005 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004006 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004007 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004008 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004009 }
bellard9307c4c2004-04-04 12:57:25 +00004010 }
bellard9307c4c2004-04-04 12:57:25 +00004011 }
4012 break;
4013 default:
4014 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004015 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004016 goto fail;
4017 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004018 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004019 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004020 }
4021 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004022 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004023 p++;
4024 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004025 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4026 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004027 goto fail;
4028 }
4029
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004030 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004031
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004032fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004033 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004034 return NULL;
4035}
4036
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004037void monitor_set_error(Monitor *mon, QError *qerror)
4038{
4039 /* report only the first error */
4040 if (!mon->error) {
4041 mon->error = qerror;
4042 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004043 QDECREF(qerror);
4044 }
4045}
4046
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004047static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4048{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004049 if (ret && !monitor_has_error(mon)) {
4050 /*
4051 * If it returns failure, it must have passed on error.
4052 *
4053 * Action: Report an internal error to the client if in QMP.
4054 */
4055 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004056 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004057}
4058
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004059static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004060{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004061 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004062 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004063
4064 qdict = qdict_new();
4065
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004066 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004067 if (!cmd)
4068 goto out;
4069
Luiz Capitulino4903de02010-09-16 11:01:32 -03004070 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004071 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004072 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004073 QObject *data = NULL;
4074
4075 /* XXX: ignores the error code */
4076 cmd->mhandler.cmd_new(mon, qdict, &data);
4077 assert(!monitor_has_error(mon));
4078 if (data) {
4079 cmd->user_print(mon, data);
4080 qobject_decref(data);
4081 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004082 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004083 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004084 }
4085
Luiz Capitulino13917be2009-10-07 13:41:54 -03004086out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004087 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004088}
4089
bellard81d09122004-07-14 17:21:37 +00004090static void cmd_completion(const char *name, const char *list)
4091{
4092 const char *p, *pstart;
4093 char cmd[128];
4094 int len;
4095
4096 p = list;
4097 for(;;) {
4098 pstart = p;
4099 p = strchr(p, '|');
4100 if (!p)
4101 p = pstart + strlen(pstart);
4102 len = p - pstart;
4103 if (len > sizeof(cmd) - 2)
4104 len = sizeof(cmd) - 2;
4105 memcpy(cmd, pstart, len);
4106 cmd[len] = '\0';
4107 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004108 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004109 }
4110 if (*p == '\0')
4111 break;
4112 p++;
4113 }
4114}
4115
4116static void file_completion(const char *input)
4117{
4118 DIR *ffs;
4119 struct dirent *d;
4120 char path[1024];
4121 char file[1024], file_prefix[1024];
4122 int input_path_len;
4123 const char *p;
4124
ths5fafdf22007-09-16 21:08:06 +00004125 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004126 if (!p) {
4127 input_path_len = 0;
4128 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004129 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004130 } else {
4131 input_path_len = p - input + 1;
4132 memcpy(path, input, input_path_len);
4133 if (input_path_len > sizeof(path) - 1)
4134 input_path_len = sizeof(path) - 1;
4135 path[input_path_len] = '\0';
4136 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4137 }
4138#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004139 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4140 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004141#endif
4142 ffs = opendir(path);
4143 if (!ffs)
4144 return;
4145 for(;;) {
4146 struct stat sb;
4147 d = readdir(ffs);
4148 if (!d)
4149 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004150
4151 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4152 continue;
4153 }
4154
bellard81d09122004-07-14 17:21:37 +00004155 if (strstart(d->d_name, file_prefix, NULL)) {
4156 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004157 if (input_path_len < sizeof(file))
4158 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4159 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004160 /* stat the file to find out if it's a directory.
4161 * In that case add a slash to speed up typing long paths
4162 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004163 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004164 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004165 }
aliguori731b0362009-03-05 23:01:42 +00004166 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004167 }
4168 }
4169 closedir(ffs);
4170}
4171
aliguori51de9762009-03-05 23:00:43 +00004172static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004173{
aliguori51de9762009-03-05 23:00:43 +00004174 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004175 const char *input = opaque;
4176
4177 if (input[0] == '\0' ||
4178 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004179 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004180 }
4181}
4182
4183/* NOTE: this parser is an approximate form of the real command parser */
4184static void parse_cmdline(const char *cmdline,
4185 int *pnb_args, char **args)
4186{
4187 const char *p;
4188 int nb_args, ret;
4189 char buf[1024];
4190
4191 p = cmdline;
4192 nb_args = 0;
4193 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004194 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004195 p++;
4196 if (*p == '\0')
4197 break;
4198 if (nb_args >= MAX_ARGS)
4199 break;
4200 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004201 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004202 nb_args++;
4203 if (ret < 0)
4204 break;
4205 }
4206 *pnb_args = nb_args;
4207}
4208
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004209static const char *next_arg_type(const char *typestr)
4210{
4211 const char *p = strchr(typestr, ':');
4212 return (p != NULL ? ++p : typestr);
4213}
4214
aliguori4c36ba32009-03-05 23:01:37 +00004215static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004216{
4217 const char *cmdname;
4218 char *args[MAX_ARGS];
4219 int nb_args, i, len;
4220 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004221 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004222 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004223
4224 parse_cmdline(cmdline, &nb_args, args);
4225#ifdef DEBUG_COMPLETION
4226 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004227 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004228 }
4229#endif
4230
4231 /* if the line ends with a space, it means we want to complete the
4232 next arg */
4233 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004234 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004235 if (nb_args >= MAX_ARGS) {
4236 goto cleanup;
4237 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004238 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004239 }
4240 if (nb_args <= 1) {
4241 /* command completion */
4242 if (nb_args == 0)
4243 cmdname = "";
4244 else
4245 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004246 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004247 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004248 cmd_completion(cmdname, cmd->name);
4249 }
4250 } else {
4251 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004252 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4253 if (compare_cmd(args[0], cmd->name)) {
4254 break;
4255 }
bellard81d09122004-07-14 17:21:37 +00004256 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004257 if (!cmd->name) {
4258 goto cleanup;
4259 }
4260
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004261 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004262 for(i = 0; i < nb_args - 2; i++) {
4263 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004264 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004265 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004266 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004267 }
4268 }
4269 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004270 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004271 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004272 }
bellard81d09122004-07-14 17:21:37 +00004273 switch(*ptype) {
4274 case 'F':
4275 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004276 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004277 file_completion(str);
4278 break;
4279 case 'B':
4280 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004281 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004282 bdrv_iterate(block_completion_it, (void *)str);
4283 break;
bellard7fe48482004-10-09 18:08:01 +00004284 case 's':
4285 /* XXX: more generic ? */
4286 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004287 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004288 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4289 cmd_completion(str, cmd->name);
4290 }
bellard64866c32006-05-07 18:03:31 +00004291 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004292 char *sep = strrchr(str, '-');
4293 if (sep)
4294 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004295 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004296 for(key = key_defs; key->name != NULL; key++) {
4297 cmd_completion(str, key->name);
4298 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004299 } else if (!strcmp(cmd->name, "help|?")) {
4300 readline_set_completion_index(cur_mon->rs, strlen(str));
4301 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4302 cmd_completion(str, cmd->name);
4303 }
bellard7fe48482004-10-09 18:08:01 +00004304 }
4305 break;
bellard81d09122004-07-14 17:21:37 +00004306 default:
4307 break;
4308 }
4309 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004310
4311cleanup:
4312 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004313 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004314 }
bellard81d09122004-07-14 17:21:37 +00004315}
4316
aliguori731b0362009-03-05 23:01:42 +00004317static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004318{
aliguori731b0362009-03-05 23:01:42 +00004319 Monitor *mon = opaque;
4320
Jan Kiszkac62313b2009-12-04 14:05:29 +01004321 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004322}
4323
Luiz Capitulino09069b12010-02-04 18:10:06 -02004324static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4325{
4326 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4327 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4328}
4329
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004330/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004331 * Argument validation rules:
4332 *
4333 * 1. The argument must exist in cmd_args qdict
4334 * 2. The argument type must be the expected one
4335 *
4336 * Special case: If the argument doesn't exist in cmd_args and
4337 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4338 * checking is skipped for it.
4339 */
4340static int check_client_args_type(const QDict *client_args,
4341 const QDict *cmd_args, int flags)
4342{
4343 const QDictEntry *ent;
4344
4345 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4346 QObject *obj;
4347 QString *arg_type;
4348 const QObject *client_arg = qdict_entry_value(ent);
4349 const char *client_arg_name = qdict_entry_key(ent);
4350
4351 obj = qdict_get(cmd_args, client_arg_name);
4352 if (!obj) {
4353 if (flags & QMP_ACCEPT_UNKNOWNS) {
4354 /* handler accepts unknowns */
4355 continue;
4356 }
4357 /* client arg doesn't exist */
4358 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4359 return -1;
4360 }
4361
4362 arg_type = qobject_to_qstring(obj);
4363 assert(arg_type != NULL);
4364
4365 /* check if argument's type is correct */
4366 switch (qstring_get_str(arg_type)[0]) {
4367 case 'F':
4368 case 'B':
4369 case 's':
4370 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4371 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4372 "string");
4373 return -1;
4374 }
4375 break;
4376 case 'i':
4377 case 'l':
4378 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004379 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004380 if (qobject_type(client_arg) != QTYPE_QINT) {
4381 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4382 "int");
4383 return -1;
4384 }
4385 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004386 case 'T':
4387 if (qobject_type(client_arg) != QTYPE_QINT &&
4388 qobject_type(client_arg) != QTYPE_QFLOAT) {
4389 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4390 "number");
4391 return -1;
4392 }
4393 break;
4394 case 'b':
4395 case '-':
4396 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4397 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4398 "bool");
4399 return -1;
4400 }
4401 break;
4402 case 'O':
4403 assert(flags & QMP_ACCEPT_UNKNOWNS);
4404 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004405 case 'q':
4406 /* Any QObject can be passed. */
4407 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004408 case '/':
4409 case '.':
4410 /*
4411 * These types are not supported by QMP and thus are not
4412 * handled here. Fall through.
4413 */
4414 default:
4415 abort();
4416 }
4417 }
4418
4419 return 0;
4420}
4421
4422/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004423 * - Check if the client has passed all mandatory args
4424 * - Set special flags for argument validation
4425 */
4426static int check_mandatory_args(const QDict *cmd_args,
4427 const QDict *client_args, int *flags)
4428{
4429 const QDictEntry *ent;
4430
4431 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4432 const char *cmd_arg_name = qdict_entry_key(ent);
4433 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4434 assert(type != NULL);
4435
4436 if (qstring_get_str(type)[0] == 'O') {
4437 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4438 *flags |= QMP_ACCEPT_UNKNOWNS;
4439 } else if (qstring_get_str(type)[0] != '-' &&
4440 qstring_get_str(type)[1] != '?' &&
4441 !qdict_haskey(client_args, cmd_arg_name)) {
4442 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4443 return -1;
4444 }
4445 }
4446
4447 return 0;
4448}
4449
4450static QDict *qdict_from_args_type(const char *args_type)
4451{
4452 int i;
4453 QDict *qdict;
4454 QString *key, *type, *cur_qs;
4455
4456 assert(args_type != NULL);
4457
4458 qdict = qdict_new();
4459
4460 if (args_type == NULL || args_type[0] == '\0') {
4461 /* no args, empty qdict */
4462 goto out;
4463 }
4464
4465 key = qstring_new();
4466 type = qstring_new();
4467
4468 cur_qs = key;
4469
4470 for (i = 0;; i++) {
4471 switch (args_type[i]) {
4472 case ',':
4473 case '\0':
4474 qdict_put(qdict, qstring_get_str(key), type);
4475 QDECREF(key);
4476 if (args_type[i] == '\0') {
4477 goto out;
4478 }
4479 type = qstring_new(); /* qdict has ref */
4480 cur_qs = key = qstring_new();
4481 break;
4482 case ':':
4483 cur_qs = type;
4484 break;
4485 default:
4486 qstring_append_chr(cur_qs, args_type[i]);
4487 break;
4488 }
4489 }
4490
4491out:
4492 return qdict;
4493}
4494
4495/*
4496 * Client argument checking rules:
4497 *
4498 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004499 * 2. Each argument provided by the client must be expected
4500 * 3. Each argument provided by the client must have the type expected
4501 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004502 */
4503static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4504{
4505 int flags, err;
4506 QDict *cmd_args;
4507
4508 cmd_args = qdict_from_args_type(cmd->args_type);
4509
4510 flags = 0;
4511 err = check_mandatory_args(cmd_args, client_args, &flags);
4512 if (err) {
4513 goto out;
4514 }
4515
Luiz Capitulino4af91932010-06-22 11:44:05 -03004516 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004517
4518out:
4519 QDECREF(cmd_args);
4520 return err;
4521}
4522
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004523/*
4524 * Input object checking rules
4525 *
4526 * 1. Input object must be a dict
4527 * 2. The "execute" key must exist
4528 * 3. The "execute" key must be a string
4529 * 4. If the "arguments" key exists, it must be a dict
4530 * 5. If the "id" key exists, it can be anything (ie. json-value)
4531 * 6. Any argument not listed above is considered invalid
4532 */
4533static QDict *qmp_check_input_obj(QObject *input_obj)
4534{
4535 const QDictEntry *ent;
4536 int has_exec_key = 0;
4537 QDict *input_dict;
4538
4539 if (qobject_type(input_obj) != QTYPE_QDICT) {
4540 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4541 return NULL;
4542 }
4543
4544 input_dict = qobject_to_qdict(input_obj);
4545
4546 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4547 const char *arg_name = qdict_entry_key(ent);
4548 const QObject *arg_obj = qdict_entry_value(ent);
4549
4550 if (!strcmp(arg_name, "execute")) {
4551 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4552 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4553 "string");
4554 return NULL;
4555 }
4556 has_exec_key = 1;
4557 } else if (!strcmp(arg_name, "arguments")) {
4558 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4559 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4560 "object");
4561 return NULL;
4562 }
4563 } else if (!strcmp(arg_name, "id")) {
4564 /* FIXME: check duplicated IDs for async commands */
4565 } else {
4566 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4567 return NULL;
4568 }
4569 }
4570
4571 if (!has_exec_key) {
4572 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4573 return NULL;
4574 }
4575
4576 return input_dict;
4577}
4578
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004579static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4580 const QDict *params)
4581{
4582 int ret;
4583 QObject *data = NULL;
4584
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004585 ret = cmd->mhandler.cmd_new(mon, params, &data);
4586 handler_audit(mon, cmd, ret);
4587 monitor_protocol_emitter(mon, data);
4588 qobject_decref(data);
4589}
4590
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004591static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4592{
4593 int err;
4594 QObject *obj;
4595 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004596 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004597 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004598 Monitor *mon = cur_mon;
4599
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004600 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004601
4602 obj = json_parser_parse(tokens, NULL);
4603 if (!obj) {
4604 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004605 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004606 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004607 }
4608
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004609 input = qmp_check_input_obj(obj);
4610 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004611 qobject_decref(obj);
4612 goto err_out;
4613 }
4614
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004615 mon->mc->id = qdict_get(input, "id");
4616 qobject_incref(mon->mc->id);
4617
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004618 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004619 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004620 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004621 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004622 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004623 }
4624
Anthony Liguorie3193602011-09-02 12:34:47 -05004625 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004626 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004627 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004628 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004629 }
4630
4631 obj = qdict_get(input, "arguments");
4632 if (!obj) {
4633 args = qdict_new();
4634 } else {
4635 args = qobject_to_qdict(obj);
4636 QINCREF(args);
4637 }
4638
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004639 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004640 if (err < 0) {
4641 goto err_out;
4642 }
4643
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004644 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004645 err = qmp_async_cmd_handler(mon, cmd, args);
4646 if (err) {
4647 /* emit the error response */
4648 goto err_out;
4649 }
Adam Litke940cc302010-01-25 12:18:44 -06004650 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004651 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004652 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004653
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004654 goto out;
4655
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004656err_out:
4657 monitor_protocol_emitter(mon, NULL);
4658out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004659 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004660 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004661}
4662
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004663/**
4664 * monitor_control_read(): Read and handle QMP input
4665 */
4666static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4667{
4668 Monitor *old_mon = cur_mon;
4669
4670 cur_mon = opaque;
4671
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004672 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004673
4674 cur_mon = old_mon;
4675}
4676
aliguori731b0362009-03-05 23:01:42 +00004677static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004678{
aliguori731b0362009-03-05 23:01:42 +00004679 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004680 int i;
aliguori376253e2009-03-05 23:01:23 +00004681
aliguori731b0362009-03-05 23:01:42 +00004682 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004683
aliguoricde76ee2009-03-05 23:01:51 +00004684 if (cur_mon->rs) {
4685 for (i = 0; i < size; i++)
4686 readline_handle_byte(cur_mon->rs, buf[i]);
4687 } else {
4688 if (size == 0 || buf[size - 1] != 0)
4689 monitor_printf(cur_mon, "corrupted command\n");
4690 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004691 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004692 }
aliguori731b0362009-03-05 23:01:42 +00004693
4694 cur_mon = old_mon;
4695}
aliguorid8f44602008-10-06 13:52:44 +00004696
aliguori376253e2009-03-05 23:01:23 +00004697static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004698{
aliguori731b0362009-03-05 23:01:42 +00004699 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004700 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004701 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004702}
4703
aliguoricde76ee2009-03-05 23:01:51 +00004704int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004705{
aliguoricde76ee2009-03-05 23:01:51 +00004706 if (!mon->rs)
4707 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004708 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004709 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004710}
4711
aliguori376253e2009-03-05 23:01:23 +00004712void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004713{
aliguoricde76ee2009-03-05 23:01:51 +00004714 if (!mon->rs)
4715 return;
aliguori731b0362009-03-05 23:01:42 +00004716 if (--mon->suspend_cnt == 0)
4717 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004718}
4719
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004720static QObject *get_qmp_greeting(void)
4721{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004722 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004723
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004724 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004725 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4726}
4727
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004728/**
4729 * monitor_control_event(): Print QMP gretting
4730 */
4731static void monitor_control_event(void *opaque, int event)
4732{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004733 QObject *data;
4734 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004735
Luiz Capitulino47116d12010-02-08 17:01:30 -02004736 switch (event) {
4737 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004738 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004739 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004740 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004741 monitor_json_emitter(mon, data);
4742 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004743 break;
4744 case CHR_EVENT_CLOSED:
4745 json_message_parser_destroy(&mon->mc->parser);
4746 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004747 }
4748}
4749
aliguori731b0362009-03-05 23:01:42 +00004750static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004751{
aliguori376253e2009-03-05 23:01:23 +00004752 Monitor *mon = opaque;
4753
aliguori2724b182009-03-05 23:01:47 +00004754 switch (event) {
4755 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004756 mon->mux_out = 0;
4757 if (mon->reset_seen) {
4758 readline_restart(mon->rs);
4759 monitor_resume(mon);
4760 monitor_flush(mon);
4761 } else {
4762 mon->suspend_cnt = 0;
4763 }
aliguori2724b182009-03-05 23:01:47 +00004764 break;
ths86e94de2007-01-05 22:01:59 +00004765
aliguori2724b182009-03-05 23:01:47 +00004766 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004767 if (mon->reset_seen) {
4768 if (mon->suspend_cnt == 0) {
4769 monitor_printf(mon, "\n");
4770 }
4771 monitor_flush(mon);
4772 monitor_suspend(mon);
4773 } else {
4774 mon->suspend_cnt++;
4775 }
4776 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004777 break;
4778
Amit Shahb6b8df52009-10-07 18:31:16 +05304779 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004780 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4781 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004782 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004783 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004784 }
4785 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004786 break;
4787 }
ths86e94de2007-01-05 22:01:59 +00004788}
4789
Wayne Xia816f8922011-10-12 11:32:41 +08004790static int
4791compare_mon_cmd(const void *a, const void *b)
4792{
4793 return strcmp(((const mon_cmd_t *)a)->name,
4794 ((const mon_cmd_t *)b)->name);
4795}
4796
4797static void sortcmdlist(void)
4798{
4799 int array_num;
4800 int elem_size = sizeof(mon_cmd_t);
4801
4802 array_num = sizeof(mon_cmds)/elem_size-1;
4803 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4804
4805 array_num = sizeof(info_cmds)/elem_size-1;
4806 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4807}
4808
aliguori76655d62009-03-06 20:27:37 +00004809
4810/*
4811 * Local variables:
4812 * c-indent-level: 4
4813 * c-basic-offset: 4
4814 * tab-width: 8
4815 * End:
4816 */
4817
aliguori731b0362009-03-05 23:01:42 +00004818void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004819{
aliguori731b0362009-03-05 23:01:42 +00004820 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004821 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004822
4823 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004824 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004825 monitor_protocol_event_init();
ths20d8a3e2007-02-18 17:04:49 +00004826 is_first_init = 0;
4827 }
aliguori87127162009-03-05 23:01:29 +00004828
Anthony Liguori7267c092011-08-20 22:09:37 -05004829 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004830
aliguori87127162009-03-05 23:01:29 +00004831 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004832 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004833 if (flags & MONITOR_USE_READLINE) {
4834 mon->rs = readline_init(mon, monitor_find_completion);
4835 monitor_read_command(mon, 0);
4836 }
aliguori87127162009-03-05 23:01:29 +00004837
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004838 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004839 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004840 /* Control mode requires special handlers */
4841 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4842 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004843 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004844 } else {
4845 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4846 monitor_event, mon);
4847 }
aliguori87127162009-03-05 23:01:29 +00004848
Blue Swirl72cf2d42009-09-12 07:36:22 +00004849 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004850 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4851 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004852
4853 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004854}
4855
aliguori376253e2009-03-05 23:01:23 +00004856static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004857{
aliguoribb5fc202009-03-05 23:01:15 +00004858 BlockDriverState *bs = opaque;
4859 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004860
aliguoribb5fc202009-03-05 23:01:15 +00004861 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004862 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004863 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004864 }
aliguori731b0362009-03-05 23:01:42 +00004865 if (mon->password_completion_cb)
4866 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004867
aliguori731b0362009-03-05 23:01:42 +00004868 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004869}
aliguoric0f4ce72009-03-05 23:01:01 +00004870
Anthony Liguori7060b472011-09-02 12:34:50 -05004871ReadLineState *monitor_get_rs(Monitor *mon)
4872{
4873 return mon->rs;
4874}
4875
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004876int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4877 BlockDriverCompletionFunc *completion_cb,
4878 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004879{
aliguoricde76ee2009-03-05 23:01:51 +00004880 int err;
4881
aliguoribb5fc202009-03-05 23:01:15 +00004882 if (!bdrv_key_required(bs)) {
4883 if (completion_cb)
4884 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004885 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004886 }
aliguoric0f4ce72009-03-05 23:01:01 +00004887
Luiz Capitulino94171e12009-12-07 21:37:00 +01004888 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004889 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4890 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004891 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004892 }
4893
aliguori376253e2009-03-05 23:01:23 +00004894 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4895 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004896
aliguori731b0362009-03-05 23:01:42 +00004897 mon->password_completion_cb = completion_cb;
4898 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004899
aliguoricde76ee2009-03-05 23:01:51 +00004900 err = monitor_read_password(mon, bdrv_password_cb, bs);
4901
4902 if (err && completion_cb)
4903 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004904
4905 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004906}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004907
4908int monitor_read_block_device_key(Monitor *mon, const char *device,
4909 BlockDriverCompletionFunc *completion_cb,
4910 void *opaque)
4911{
4912 BlockDriverState *bs;
4913
4914 bs = bdrv_find(device);
4915 if (!bs) {
4916 monitor_printf(mon, "Device not found %s\n", device);
4917 return -1;
4918 }
4919
4920 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4921}