blob: 413edf4b3e9a7e93a37cd67b6e8cf76dc257e895 [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;
Corey Bryantadb696f2012-08-14 16:43:47 -0400157 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400158 QLIST_ENTRY(MonFdset) next;
159};
160
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200161typedef struct MonitorControl {
162 QObject *id;
163 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200164 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165} MonitorControl;
166
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100167/*
168 * To prevent flooding clients, events can be throttled. The
169 * throttling is calculated globally, rather than per-Monitor
170 * instance.
171 */
172typedef struct MonitorEventState {
173 MonitorEvent event; /* Event being tracked */
174 int64_t rate; /* Period over which to throttle. 0 to disable */
175 int64_t last; /* Time at which event was last emitted */
176 QEMUTimer *timer; /* Timer for handling delayed events */
177 QObject *data; /* Event pending delayed dispatch */
178} MonitorEventState;
179
aliguori87127162009-03-05 23:01:29 +0000180struct Monitor {
181 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200182 int mux_out;
183 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000184 int flags;
185 int suspend_cnt;
186 uint8_t outbuf[1024];
187 int outbuf_index;
188 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200189 MonitorControl *mc;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100190 CPUArchState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000191 BlockDriverCompletionFunc *password_completion_cb;
192 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200193 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500194 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000195 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000196};
197
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300198/* QMP checker flags */
199#define QMP_ACCEPT_UNKNOWNS 1
200
Blue Swirl72cf2d42009-09-12 07:36:22 +0000201static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400202static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400203static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000204
Wayne Xia816f8922011-10-12 11:32:41 +0800205static mon_cmd_t mon_cmds[];
206static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000207
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300208static const mon_cmd_t qmp_cmds[];
209
Markus Armbruster8631b602010-02-18 11:41:55 +0100210Monitor *cur_mon;
211Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000212
aliguori731b0362009-03-05 23:01:42 +0000213static void monitor_command_cb(Monitor *mon, const char *cmdline,
214 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000215
Luiz Capitulino09069b12010-02-04 18:10:06 -0200216static inline int qmp_cmd_mode(const Monitor *mon)
217{
218 return (mon->mc ? mon->mc->command_mode : 0);
219}
220
Luiz Capitulino418173c2009-11-26 22:58:51 -0200221/* Return true if in control mode, false otherwise */
222static inline int monitor_ctrl_mode(const Monitor *mon)
223{
224 return (mon->flags & MONITOR_USE_CONTROL);
225}
226
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100227/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228int monitor_cur_is_qmp(void)
229{
230 return cur_mon && monitor_ctrl_mode(cur_mon);
231}
232
Anthony Liguori7060b472011-09-02 12:34:50 -0500233void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000234{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200235 if (!mon->rs)
236 return;
237
aliguori731b0362009-03-05 23:01:42 +0000238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
239 if (show_prompt)
240 readline_show_prompt(mon->rs);
241}
bellard6a00d602005-11-21 23:25:50 +0000242
Anthony Liguori7060b472011-09-02 12:34:50 -0500243int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
244 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000245{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100246 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100247 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100248 return -EINVAL;
249 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
252 return 0;
253 } else {
254 monitor_printf(mon, "terminal does not support password prompting\n");
255 return -ENOTTY;
256 }
aliguoribb5fc202009-03-05 23:01:15 +0000257}
258
aliguori376253e2009-03-05 23:01:23 +0000259void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000260{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000263 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
267/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000268static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000269{
ths60fe76f2007-12-16 03:02:09 +0000270 char c;
aliguori731b0362009-03-05 23:01:42 +0000271
bellard7e2515e2004-08-01 21:52:19 +0000272 for(;;) {
273 c = *str++;
274 if (c == '\0')
275 break;
bellard7ba12602006-07-14 20:26:42 +0000276 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000277 mon->outbuf[mon->outbuf_index++] = '\r';
278 mon->outbuf[mon->outbuf_index++] = c;
279 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
280 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000282 }
283}
284
aliguori376253e2009-03-05 23:01:23 +0000285void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000286{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200287 char buf[4096];
288
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200289 if (!mon)
290 return;
291
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200292 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200293 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200294 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200295
296 vsnprintf(buf, sizeof(buf), fmt, ap);
297 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000298}
299
aliguori376253e2009-03-05 23:01:23 +0000300void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000301{
302 va_list ap;
303 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000304 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000305 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000306}
307
aliguori376253e2009-03-05 23:01:23 +0000308void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000309{
310 int i;
311
312 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000313 switch (filename[i]) {
314 case ' ':
315 case '"':
316 case '\\':
317 monitor_printf(mon, "\\%c", filename[i]);
318 break;
319 case '\t':
320 monitor_printf(mon, "\\t");
321 break;
322 case '\r':
323 monitor_printf(mon, "\\r");
324 break;
325 case '\n':
326 monitor_printf(mon, "\\n");
327 break;
328 default:
329 monitor_printf(mon, "%c", filename[i]);
330 break;
331 }
thsfef30742006-12-22 14:11:32 +0000332 }
333}
334
Stefan Weil8b7968f2010-09-23 21:28:05 +0200335static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
336 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000337{
338 va_list ap;
339 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000340 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000341 va_end(ap);
342 return 0;
343}
344
Luiz Capitulino13c74252009-10-07 13:41:55 -0300345static void monitor_user_noop(Monitor *mon, const QObject *data) { }
346
Luiz Capitulino9e807212010-09-16 10:58:59 -0300347static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300348{
349 return cmd->user_print != NULL;
350}
351
Luiz Capitulino4903de02010-09-16 11:01:32 -0300352static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600353{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200354 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600355}
356
Luiz Capitulino8204a912009-11-18 23:05:31 -0200357static inline int monitor_has_error(const Monitor *mon)
358{
359 return mon->error != NULL;
360}
361
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200362static void monitor_json_emitter(Monitor *mon, const QObject *data)
363{
364 QString *json;
365
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200366 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
367 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200368 assert(json != NULL);
369
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200370 qstring_append_chr(json, '\n');
371 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200372
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200373 QDECREF(json);
374}
375
Luiz Capitulinode253f12012-07-27 16:18:16 -0300376static QDict *build_qmp_error_dict(const QError *err)
377{
378 QObject *obj;
379
380 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
381 ErrorClass_lookup[err->err_class],
382 qerror_human(err));
383
384 return qobject_to_qdict(obj);
385}
386
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200387static void monitor_protocol_emitter(Monitor *mon, QObject *data)
388{
389 QDict *qmp;
390
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100391 trace_monitor_protocol_emitter(mon);
392
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200393 if (!monitor_has_error(mon)) {
394 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300395 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200396 if (data) {
397 qobject_incref(data);
398 qdict_put_obj(qmp, "return", data);
399 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200400 /* return an empty QDict by default */
401 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200402 }
403 } else {
404 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300405 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200406 QDECREF(mon->error);
407 mon->error = NULL;
408 }
409
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200410 if (mon->mc->id) {
411 qdict_put_obj(qmp, "id", mon->mc->id);
412 mon->mc->id = NULL;
413 }
414
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200415 monitor_json_emitter(mon, QOBJECT(qmp));
416 QDECREF(qmp);
417}
418
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200419static void timestamp_put(QDict *qdict)
420{
421 int err;
422 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000423 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200424
Blue Swirld08d6f02009-12-04 18:06:39 +0000425 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 if (err < 0)
427 return;
428
429 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
430 "'microseconds': %" PRId64 " }",
431 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200432 qdict_put_obj(qdict, "timestamp", obj);
433}
434
Daniel P. Berrange48608532012-05-21 17:59:51 +0100435
436static const char *monitor_event_names[] = {
437 [QEVENT_SHUTDOWN] = "SHUTDOWN",
438 [QEVENT_RESET] = "RESET",
439 [QEVENT_POWERDOWN] = "POWERDOWN",
440 [QEVENT_STOP] = "STOP",
441 [QEVENT_RESUME] = "RESUME",
442 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
443 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
444 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
445 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
446 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
447 [QEVENT_WATCHDOG] = "WATCHDOG",
448 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
449 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
450 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
451 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
452 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
453 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
454 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300455 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100456 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100457 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300458 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100459};
460QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
461
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100462MonitorEventState monitor_event_state[QEVENT_MAX];
463QemuMutex monitor_event_state_lock;
464
465/*
466 * Emits the event to every monitor instance
467 */
468static void
469monitor_protocol_event_emit(MonitorEvent event,
470 QObject *data)
471{
472 Monitor *mon;
473
474 trace_monitor_protocol_event_emit(event, data);
475 QLIST_FOREACH(mon, &mon_list, entry) {
476 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
477 monitor_json_emitter(mon, data);
478 }
479 }
480}
481
482
483/*
484 * Queue a new event for emission to Monitor instances,
485 * applying any rate limiting if required.
486 */
487static void
488monitor_protocol_event_queue(MonitorEvent event,
489 QObject *data)
490{
491 MonitorEventState *evstate;
492 int64_t now = qemu_get_clock_ns(rt_clock);
493 assert(event < QEVENT_MAX);
494
495 qemu_mutex_lock(&monitor_event_state_lock);
496 evstate = &(monitor_event_state[event]);
497 trace_monitor_protocol_event_queue(event,
498 data,
499 evstate->rate,
500 evstate->last,
501 now);
502
503 /* Rate limit of 0 indicates no throttling */
504 if (!evstate->rate) {
505 monitor_protocol_event_emit(event, data);
506 evstate->last = now;
507 } else {
508 int64_t delta = now - evstate->last;
509 if (evstate->data ||
510 delta < evstate->rate) {
511 /* If there's an existing event pending, replace
512 * it with the new event, otherwise schedule a
513 * timer for delayed emission
514 */
515 if (evstate->data) {
516 qobject_decref(evstate->data);
517 } else {
518 int64_t then = evstate->last + evstate->rate;
519 qemu_mod_timer_ns(evstate->timer, then);
520 }
521 evstate->data = data;
522 qobject_incref(evstate->data);
523 } else {
524 monitor_protocol_event_emit(event, data);
525 evstate->last = now;
526 }
527 }
528 qemu_mutex_unlock(&monitor_event_state_lock);
529}
530
531
532/*
533 * The callback invoked by QemuTimer when a delayed
534 * event is ready to be emitted
535 */
536static void monitor_protocol_event_handler(void *opaque)
537{
538 MonitorEventState *evstate = opaque;
539 int64_t now = qemu_get_clock_ns(rt_clock);
540
541 qemu_mutex_lock(&monitor_event_state_lock);
542
543 trace_monitor_protocol_event_handler(evstate->event,
544 evstate->data,
545 evstate->last,
546 now);
547 if (evstate->data) {
548 monitor_protocol_event_emit(evstate->event, evstate->data);
549 qobject_decref(evstate->data);
550 evstate->data = NULL;
551 }
552 evstate->last = now;
553 qemu_mutex_unlock(&monitor_event_state_lock);
554}
555
556
557/*
558 * @event: the event ID to be limited
559 * @rate: the rate limit in milliseconds
560 *
561 * Sets a rate limit on a particular event, so no
562 * more than 1 event will be emitted within @rate
563 * milliseconds
564 */
565static void
566monitor_protocol_event_throttle(MonitorEvent event,
567 int64_t rate)
568{
569 MonitorEventState *evstate;
570 assert(event < QEVENT_MAX);
571
572 evstate = &(monitor_event_state[event]);
573
574 trace_monitor_protocol_event_throttle(event, rate);
575 evstate->event = event;
576 evstate->rate = rate * SCALE_MS;
577 evstate->timer = qemu_new_timer(rt_clock,
578 SCALE_MS,
579 monitor_protocol_event_handler,
580 evstate);
581 evstate->last = 0;
582 evstate->data = NULL;
583}
584
585
586/* Global, one-time initializer to configure the rate limiting
587 * and initialize state */
588static void monitor_protocol_event_init(void)
589{
590 qemu_mutex_init(&monitor_event_state_lock);
591 /* Limit RTC & BALLOON events to 1 per second */
592 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
593 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
594 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
595}
596
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200597/**
598 * monitor_protocol_event(): Generate a Monitor event
599 *
600 * Event-specific data can be emitted through the (optional) 'data' parameter.
601 */
602void monitor_protocol_event(MonitorEvent event, QObject *data)
603{
604 QDict *qmp;
605 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200606
Blue Swirl242cd002009-12-04 18:05:45 +0000607 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200608
Daniel P. Berrange48608532012-05-21 17:59:51 +0100609 event_name = monitor_event_names[event];
610 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200611
612 qmp = qdict_new();
613 timestamp_put(qmp);
614 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200615 if (data) {
616 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200617 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200618 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200619
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100620 trace_monitor_protocol_event(event, event_name, qmp);
621 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200622 QDECREF(qmp);
623}
624
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200625static int do_qmp_capabilities(Monitor *mon, const QDict *params,
626 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200627{
628 /* Will setup QMP capabilities in the future */
629 if (monitor_ctrl_mode(mon)) {
630 mon->mc->command_mode = 1;
631 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200632
633 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200634}
635
Luiz Capitulino0268d972010-10-22 10:08:02 -0200636static void handle_user_command(Monitor *mon, const char *cmdline);
637
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200638char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
639 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200640{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200641 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200642 Monitor *old_mon, hmp;
643 CharDriverState mchar;
644
645 memset(&hmp, 0, sizeof(hmp));
646 qemu_chr_init_mem(&mchar);
647 hmp.chr = &mchar;
648
649 old_mon = cur_mon;
650 cur_mon = &hmp;
651
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200652 if (has_cpu_index) {
653 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200654 if (ret < 0) {
655 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200656 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
657 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200658 goto out;
659 }
660 }
661
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200662 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200663 cur_mon = old_mon;
664
665 if (qemu_chr_mem_osize(hmp.chr) > 0) {
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200666 QString *str = qemu_chr_mem_to_qs(hmp.chr);
667 output = g_strdup(qstring_get_str(str));
668 QDECREF(str);
669 } else {
670 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200671 }
672
673out:
674 qemu_chr_close_mem(hmp.chr);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200675 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200676}
677
bellard9dc39cb2004-03-14 21:38:27 +0000678static int compare_cmd(const char *name, const char *list)
679{
680 const char *p, *pstart;
681 int len;
682 len = strlen(name);
683 p = list;
684 for(;;) {
685 pstart = p;
686 p = strchr(p, '|');
687 if (!p)
688 p = pstart + strlen(pstart);
689 if ((p - pstart) == len && !memcmp(pstart, name, len))
690 return 1;
691 if (*p == '\0')
692 break;
693 p++;
694 }
695 return 0;
696}
697
Anthony Liguoric227f092009-10-01 16:12:16 -0500698static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000699 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000700{
Anthony Liguoric227f092009-10-01 16:12:16 -0500701 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000702
703 for(cmd = cmds; cmd->name != NULL; cmd++) {
704 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000705 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
706 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000707 }
708}
709
aliguori376253e2009-03-05 23:01:23 +0000710static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000711{
712 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000713 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000714 } else {
aliguori376253e2009-03-05 23:01:23 +0000715 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000716 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000717 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000718 monitor_printf(mon, "Log items (comma separated):\n");
719 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000720 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000721 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000722 }
723 }
bellard9dc39cb2004-03-14 21:38:27 +0000724 }
725}
726
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300727static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300728{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300729 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300730}
731
Lluísfc764102011-08-31 20:31:18 +0200732static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530733{
734 const char *tp_name = qdict_get_str(qdict, "name");
735 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200736 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000737
738 if (!ret) {
739 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
740 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530741}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100742
Michael Rothc45a8162011-10-02 08:44:37 -0500743#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100744static void do_trace_file(Monitor *mon, const QDict *qdict)
745{
746 const char *op = qdict_get_try_str(qdict, "op");
747 const char *arg = qdict_get_try_str(qdict, "arg");
748
749 if (!op) {
750 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
751 } else if (!strcmp(op, "on")) {
752 st_set_trace_file_enabled(true);
753 } else if (!strcmp(op, "off")) {
754 st_set_trace_file_enabled(false);
755 } else if (!strcmp(op, "flush")) {
756 st_flush_trace_buffer();
757 } else if (!strcmp(op, "set")) {
758 if (arg) {
759 st_set_trace_file(arg);
760 }
761 } else {
762 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
763 help_cmd(mon, "trace-file");
764 }
765}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530766#endif
767
Adam Litke940cc302010-01-25 12:18:44 -0600768static void user_monitor_complete(void *opaque, QObject *ret_data)
769{
770 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
771
772 if (ret_data) {
773 data->user_print(data->mon, ret_data);
774 }
775 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500776 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600777}
778
779static void qmp_monitor_complete(void *opaque, QObject *ret_data)
780{
781 monitor_protocol_emitter(opaque, ret_data);
782}
783
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300784static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
785 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600786{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300787 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600788}
789
Adam Litke940cc302010-01-25 12:18:44 -0600790static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
791 const QDict *params)
792{
793 int ret;
794
Anthony Liguori7267c092011-08-20 22:09:37 -0500795 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600796 cb_data->mon = mon;
797 cb_data->user_print = cmd->user_print;
798 monitor_suspend(mon);
799 ret = cmd->mhandler.cmd_async(mon, params,
800 user_monitor_complete, cb_data);
801 if (ret < 0) {
802 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500803 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600804 }
805}
806
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300807static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000808{
Anthony Liguoric227f092009-10-01 16:12:16 -0500809 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300810 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000811
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200812 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000813 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200814 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300815
816 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000817 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300818 break;
bellard9dc39cb2004-03-14 21:38:27 +0000819 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300820
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200821 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300822 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200823 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300824
Luiz Capitulinob5c30582011-10-21 16:24:28 -0200825 cmd->mhandler.info(mon);
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300826 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300827
828help:
829 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000830}
831
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300832CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000833{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300834 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200835 const mon_cmd_t *cmd;
836
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300837 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200838 info = g_malloc0(sizeof(*info));
839 info->value = g_malloc0(sizeof(*info->value));
840 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200841
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300842 info->next = cmd_list;
843 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200844 }
845
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300846 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000847}
848
Daniel P. Berrange48608532012-05-21 17:59:51 +0100849EventInfoList *qmp_query_events(Error **errp)
850{
851 EventInfoList *info, *ev_list = NULL;
852 MonitorEvent e;
853
854 for (e = 0 ; e < QEVENT_MAX ; e++) {
855 const char *event_name = monitor_event_names[e];
856 assert(event_name != NULL);
857 info = g_malloc0(sizeof(*info));
858 info->value = g_malloc0(sizeof(*info->value));
859 info->value->name = g_strdup(event_name);
860
861 info->next = ev_list;
862 ev_list = info;
863 }
864
865 return ev_list;
866}
867
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300868/* set the current CPU defined by the user */
869int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000870{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100871 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000872
873 for(env = first_cpu; env != NULL; env = env->next_cpu) {
874 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000875 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000876 return 0;
877 }
878 }
879 return -1;
880}
881
Andreas Färber9349b4f2012-03-14 01:38:32 +0100882static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000883{
aliguori731b0362009-03-05 23:01:42 +0000884 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300885 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000886 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300887 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000888 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000889}
890
Luiz Capitulino99b77962011-10-24 10:53:44 -0200891int monitor_get_cpu_index(void)
892{
893 return mon_get_cpu()->cpu_index;
894}
895
aliguori376253e2009-03-05 23:01:23 +0000896static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000897{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100898 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000899 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000900#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000901 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000902 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000903#else
aliguori376253e2009-03-05 23:01:23 +0000904 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000905 0);
bellard9307c4c2004-04-04 12:57:25 +0000906#endif
907}
908
aliguori376253e2009-03-05 23:01:23 +0000909static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000910{
aliguori376253e2009-03-05 23:01:23 +0000911 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000912}
913
aliguori376253e2009-03-05 23:01:23 +0000914static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000915{
916 int i;
bellard7e2515e2004-08-01 21:52:19 +0000917 const char *str;
ths3b46e622007-09-17 08:09:54 +0000918
aliguoricde76ee2009-03-05 23:01:51 +0000919 if (!mon->rs)
920 return;
bellard7e2515e2004-08-01 21:52:19 +0000921 i = 0;
922 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000923 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000924 if (!str)
925 break;
aliguori376253e2009-03-05 23:01:23 +0000926 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000927 i++;
bellardaa455482004-04-04 13:07:25 +0000928 }
929}
930
j_mayer76a66252007-03-07 08:32:30 +0000931#if defined(TARGET_PPC)
932/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000933static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000934{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100935 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000936
937 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000938 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000939}
940#endif
941
Lluísfc764102011-08-31 20:31:18 +0200942static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530943{
Lluísfc764102011-08-31 20:31:18 +0200944 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530945}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530946
Daniel P. Berrange13661082011-06-23 13:31:42 +0100947static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
948{
949 const char *protocol = qdict_get_str(qdict, "protocol");
950 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +0100951 CharDriverState *s;
952
953 if (strcmp(protocol, "spice") == 0) {
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000954 int fd = monitor_get_fd(mon, fdname);
955 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
956 int tls = qdict_get_try_bool(qdict, "tls", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100957 if (!using_spice) {
958 /* correct one? spice isn't a device ,,, */
959 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
960 return -1;
961 }
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000962 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
963 close(fd);
964 }
965 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800966#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +0100967 } else if (strcmp(protocol, "vnc") == 0) {
968 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +0100969 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100970 vnc_display_add_client(NULL, fd, skipauth);
971 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800972#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +0100973 } else if ((s = qemu_chr_find(protocol)) != NULL) {
974 int fd = monitor_get_fd(mon, fdname);
975 if (qemu_chr_add_client(s, fd) < 0) {
976 qerror_report(QERR_ADD_CLIENT_FAILED);
977 return -1;
978 }
979 return 0;
980 }
981
982 qerror_report(QERR_INVALID_PARAMETER, "protocol");
983 return -1;
984}
985
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200986static int client_migrate_info(Monitor *mon, const QDict *qdict,
987 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200988{
989 const char *protocol = qdict_get_str(qdict, "protocol");
990 const char *hostname = qdict_get_str(qdict, "hostname");
991 const char *subject = qdict_get_try_str(qdict, "cert-subject");
992 int port = qdict_get_try_int(qdict, "port", -1);
993 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
994 int ret;
995
996 if (strcmp(protocol, "spice") == 0) {
997 if (!using_spice) {
998 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
999 return -1;
1000 }
1001
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001002 if (port == -1 && tls_port == -1) {
1003 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1004 return -1;
1005 }
1006
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001007 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1008 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001009 if (ret != 0) {
1010 qerror_report(QERR_UNDEFINED_ERROR);
1011 return -1;
1012 }
1013 return 0;
1014 }
1015
1016 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1017 return -1;
1018}
1019
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001020static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001021{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001022 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001023 return 0;
bellard59a983b2004-03-17 23:17:16 +00001024}
1025
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001026static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001027{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001028 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001029}
1030
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001031static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001032{
1033 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001034 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001035
bellard9307c4c2004-04-04 12:57:25 +00001036 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001037 mask = 0;
1038 } else {
bellard9307c4c2004-04-04 12:57:25 +00001039 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001040 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001041 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001042 return;
1043 }
1044 }
1045 cpu_set_log(mask);
1046}
1047
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001048static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001049{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001050 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001051 if (!option || !strcmp(option, "on")) {
1052 singlestep = 1;
1053 } else if (!strcmp(option, "off")) {
1054 singlestep = 0;
1055 } else {
1056 monitor_printf(mon, "unexpected option %s\n", option);
1057 }
1058}
1059
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001060static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001061{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001062 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001063 if (!device)
1064 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1065 if (gdbserver_start(device) < 0) {
1066 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1067 device);
1068 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001069 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001070 } else {
aliguori59030a82009-04-05 18:43:41 +00001071 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1072 device);
bellard8a7ddc32004-03-31 19:00:16 +00001073 }
1074}
1075
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001076static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001077{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001078 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001079 if (select_watchdog_action(action) == -1) {
1080 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1081 }
1082}
1083
aliguori376253e2009-03-05 23:01:23 +00001084static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001085{
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001087 switch(c) {
1088 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001090 break;
1091 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001093 break;
1094 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001096 break;
1097 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001098 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001099 break;
1100 default:
1101 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001103 } else {
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001105 }
1106 break;
1107 }
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001109}
1110
aliguori376253e2009-03-05 23:01:23 +00001111static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001112 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001113{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001114 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001115 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001116 uint8_t buf[16];
1117 uint64_t v;
1118
1119 if (format == 'i') {
1120 int flags;
1121 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001122 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001123#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001124 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001125 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001126 } else if (wsize == 4) {
1127 flags = 0;
1128 } else {
bellard6a15fd12006-04-12 21:07:07 +00001129 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001130 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001131 if (env) {
1132#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001133 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001134 (env->segs[R_CS].flags & DESC_L_MASK))
1135 flags = 2;
1136 else
1137#endif
1138 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1139 flags = 1;
1140 }
bellard4c27ba22004-04-25 18:05:08 +00001141 }
1142#endif
aliguori376253e2009-03-05 23:01:23 +00001143 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001144 return;
1145 }
1146
1147 len = wsize * count;
1148 if (wsize == 1)
1149 line_size = 8;
1150 else
1151 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001152 max_digits = 0;
1153
1154 switch(format) {
1155 case 'o':
1156 max_digits = (wsize * 8 + 2) / 3;
1157 break;
1158 default:
1159 case 'x':
1160 max_digits = (wsize * 8) / 4;
1161 break;
1162 case 'u':
1163 case 'd':
1164 max_digits = (wsize * 8 * 10 + 32) / 33;
1165 break;
1166 case 'c':
1167 wsize = 1;
1168 break;
1169 }
1170
1171 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001172 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001173 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001174 else
aliguori376253e2009-03-05 23:01:23 +00001175 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001176 l = len;
1177 if (l > line_size)
1178 l = line_size;
1179 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001180 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001181 } else {
bellard6a00d602005-11-21 23:25:50 +00001182 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001183 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001184 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001185 break;
1186 }
bellard9307c4c2004-04-04 12:57:25 +00001187 }
ths5fafdf22007-09-16 21:08:06 +00001188 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001189 while (i < l) {
1190 switch(wsize) {
1191 default:
1192 case 1:
1193 v = ldub_raw(buf + i);
1194 break;
1195 case 2:
1196 v = lduw_raw(buf + i);
1197 break;
1198 case 4:
bellard92a31b12005-02-10 22:00:52 +00001199 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001200 break;
1201 case 8:
1202 v = ldq_raw(buf + i);
1203 break;
1204 }
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001206 switch(format) {
1207 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001209 break;
1210 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001212 break;
1213 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 }
1223 i += wsize;
1224 }
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001226 addr += l;
1227 len -= l;
1228 }
1229}
1230
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001231static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001232{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001233 int count = qdict_get_int(qdict, "count");
1234 int format = qdict_get_int(qdict, "format");
1235 int size = qdict_get_int(qdict, "size");
1236 target_long addr = qdict_get_int(qdict, "addr");
1237
aliguori376253e2009-03-05 23:01:23 +00001238 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001239}
1240
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001241static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001242{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001243 int count = qdict_get_int(qdict, "count");
1244 int format = qdict_get_int(qdict, "format");
1245 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001246 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001247
aliguori376253e2009-03-05 23:01:23 +00001248 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001249}
1250
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001251static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001252{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001253 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001254 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001255
bellard9307c4c2004-04-04 12:57:25 +00001256 switch(format) {
1257 case 'o':
Peter Maydell66f27e62012-06-25 16:52:24 +01001258 monitor_printf(mon, "%#" TARGET_PRIoPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001259 break;
1260 case 'x':
Peter Maydell66f27e62012-06-25 16:52:24 +01001261 monitor_printf(mon, "%#" TARGET_PRIxPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001262 break;
1263 case 'u':
Peter Maydell66f27e62012-06-25 16:52:24 +01001264 monitor_printf(mon, "%" TARGET_PRIuPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001265 break;
1266 default:
1267 case 'd':
Peter Maydell66f27e62012-06-25 16:52:24 +01001268 monitor_printf(mon, "%" TARGET_PRIdPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001269 break;
1270 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001272 break;
1273 }
aliguori376253e2009-03-05 23:01:23 +00001274 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001275}
1276
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001277static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001278{
1279 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001280 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001281 uint32_t start = qdict_get_int(qdict, "start");
1282 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001283
1284 sum = 0;
1285 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001286 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001287 /* BSD sum algorithm ('sum' Unix command) */
1288 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001289 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001290 }
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001292}
1293
bellarda3a91a32004-06-04 11:06:21 +00001294typedef struct {
1295 int keycode;
1296 const char *name;
1297} KeyDef;
1298
1299static const KeyDef key_defs[] = {
1300 { 0x2a, "shift" },
1301 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001302
bellarda3a91a32004-06-04 11:06:21 +00001303 { 0x38, "alt" },
1304 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001305 { 0x64, "altgr" },
1306 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001307 { 0x1d, "ctrl" },
1308 { 0x9d, "ctrl_r" },
1309
1310 { 0xdd, "menu" },
1311
1312 { 0x01, "esc" },
1313
1314 { 0x02, "1" },
1315 { 0x03, "2" },
1316 { 0x04, "3" },
1317 { 0x05, "4" },
1318 { 0x06, "5" },
1319 { 0x07, "6" },
1320 { 0x08, "7" },
1321 { 0x09, "8" },
1322 { 0x0a, "9" },
1323 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001324 { 0x0c, "minus" },
1325 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001326 { 0x0e, "backspace" },
1327
1328 { 0x0f, "tab" },
1329 { 0x10, "q" },
1330 { 0x11, "w" },
1331 { 0x12, "e" },
1332 { 0x13, "r" },
1333 { 0x14, "t" },
1334 { 0x15, "y" },
1335 { 0x16, "u" },
1336 { 0x17, "i" },
1337 { 0x18, "o" },
1338 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001339 { 0x1a, "bracket_left" },
1340 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001341 { 0x1c, "ret" },
1342
1343 { 0x1e, "a" },
1344 { 0x1f, "s" },
1345 { 0x20, "d" },
1346 { 0x21, "f" },
1347 { 0x22, "g" },
1348 { 0x23, "h" },
1349 { 0x24, "j" },
1350 { 0x25, "k" },
1351 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001352 { 0x27, "semicolon" },
1353 { 0x28, "apostrophe" },
1354 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001355
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001356 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001357 { 0x2c, "z" },
1358 { 0x2d, "x" },
1359 { 0x2e, "c" },
1360 { 0x2f, "v" },
1361 { 0x30, "b" },
1362 { 0x31, "n" },
1363 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001364 { 0x33, "comma" },
1365 { 0x34, "dot" },
1366 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001367
balrog4d3b6f62008-02-10 16:33:14 +00001368 { 0x37, "asterisk" },
1369
bellarda3a91a32004-06-04 11:06:21 +00001370 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001371 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001372 { 0x3b, "f1" },
1373 { 0x3c, "f2" },
1374 { 0x3d, "f3" },
1375 { 0x3e, "f4" },
1376 { 0x3f, "f5" },
1377 { 0x40, "f6" },
1378 { 0x41, "f7" },
1379 { 0x42, "f8" },
1380 { 0x43, "f9" },
1381 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001382 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001383 { 0x46, "scroll_lock" },
1384
bellard64866c32006-05-07 18:03:31 +00001385 { 0xb5, "kp_divide" },
1386 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001387 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001388 { 0x4e, "kp_add" },
1389 { 0x9c, "kp_enter" },
1390 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001391 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001392
1393 { 0x52, "kp_0" },
1394 { 0x4f, "kp_1" },
1395 { 0x50, "kp_2" },
1396 { 0x51, "kp_3" },
1397 { 0x4b, "kp_4" },
1398 { 0x4c, "kp_5" },
1399 { 0x4d, "kp_6" },
1400 { 0x47, "kp_7" },
1401 { 0x48, "kp_8" },
1402 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001403
bellarda3a91a32004-06-04 11:06:21 +00001404 { 0x56, "<" },
1405
1406 { 0x57, "f11" },
1407 { 0x58, "f12" },
1408
1409 { 0xb7, "print" },
1410
1411 { 0xc7, "home" },
1412 { 0xc9, "pgup" },
1413 { 0xd1, "pgdn" },
1414 { 0xcf, "end" },
1415
1416 { 0xcb, "left" },
1417 { 0xc8, "up" },
1418 { 0xd0, "down" },
1419 { 0xcd, "right" },
1420
1421 { 0xd2, "insert" },
1422 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001423#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1424 { 0xf0, "stop" },
1425 { 0xf1, "again" },
1426 { 0xf2, "props" },
1427 { 0xf3, "undo" },
1428 { 0xf4, "front" },
1429 { 0xf5, "copy" },
1430 { 0xf6, "open" },
1431 { 0xf7, "paste" },
1432 { 0xf8, "find" },
1433 { 0xf9, "cut" },
1434 { 0xfa, "lf" },
1435 { 0xfb, "help" },
1436 { 0xfc, "meta_l" },
1437 { 0xfd, "meta_r" },
1438 { 0xfe, "compose" },
1439#endif
bellarda3a91a32004-06-04 11:06:21 +00001440 { 0, NULL },
1441};
1442
1443static int get_keycode(const char *key)
1444{
1445 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001446 char *endp;
1447 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001448
1449 for(p = key_defs; p->name != NULL; p++) {
1450 if (!strcmp(key, p->name))
1451 return p->keycode;
1452 }
bellard64866c32006-05-07 18:03:31 +00001453 if (strstart(key, "0x", NULL)) {
1454 ret = strtoul(key, &endp, 0);
1455 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1456 return ret;
1457 }
bellarda3a91a32004-06-04 11:06:21 +00001458 return -1;
1459}
1460
balrogc8256f92008-06-08 22:45:01 +00001461#define MAX_KEYCODES 16
1462static uint8_t keycodes[MAX_KEYCODES];
1463static int nb_pending_keycodes;
1464static QEMUTimer *key_timer;
1465
1466static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001467{
balrogc8256f92008-06-08 22:45:01 +00001468 int keycode;
1469
1470 while (nb_pending_keycodes > 0) {
1471 nb_pending_keycodes--;
1472 keycode = keycodes[nb_pending_keycodes];
1473 if (keycode & 0x80)
1474 kbd_put_keycode(0xe0);
1475 kbd_put_keycode(keycode | 0x80);
1476 }
1477}
1478
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001479static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001480{
balrog3401c0d2008-06-04 10:05:59 +00001481 char keyname_buf[16];
1482 char *separator;
1483 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001484 const char *string = qdict_get_str(qdict, "string");
1485 int has_hold_time = qdict_haskey(qdict, "hold_time");
1486 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001487
balrogc8256f92008-06-08 22:45:01 +00001488 if (nb_pending_keycodes > 0) {
1489 qemu_del_timer(key_timer);
1490 release_keys(NULL);
1491 }
1492 if (!has_hold_time)
1493 hold_time = 100;
1494 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001495 while (1) {
1496 separator = strchr(string, '-');
1497 keyname_len = separator ? separator - string : strlen(string);
1498 if (keyname_len > 0) {
1499 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1500 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001502 return;
bellarda3a91a32004-06-04 11:06:21 +00001503 }
balrogc8256f92008-06-08 22:45:01 +00001504 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001506 return;
1507 }
1508 keyname_buf[keyname_len] = 0;
1509 keycode = get_keycode(keyname_buf);
1510 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001512 return;
1513 }
balrogc8256f92008-06-08 22:45:01 +00001514 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001515 }
balrog3401c0d2008-06-04 10:05:59 +00001516 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001517 break;
balrog3401c0d2008-06-04 10:05:59 +00001518 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001519 }
balrogc8256f92008-06-08 22:45:01 +00001520 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001521 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001522 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001523 keycode = keycodes[i];
1524 if (keycode & 0x80)
1525 kbd_put_keycode(0xe0);
1526 kbd_put_keycode(keycode & 0x7f);
1527 }
balrogc8256f92008-06-08 22:45:01 +00001528 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001529 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001530 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001531}
1532
bellard13224a82006-07-14 22:03:35 +00001533static int mouse_button_state;
1534
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001535static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001536{
1537 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001538 const char *dx_str = qdict_get_str(qdict, "dx_str");
1539 const char *dy_str = qdict_get_str(qdict, "dy_str");
1540 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001541 dx = strtol(dx_str, NULL, 0);
1542 dy = strtol(dy_str, NULL, 0);
1543 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001544 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001545 dz = strtol(dz_str, NULL, 0);
1546 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1547}
1548
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001549static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001550{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001551 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001552 mouse_button_state = button_state;
1553 kbd_mouse_event(0, 0, 0, mouse_button_state);
1554}
1555
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001556static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001557{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001558 int size = qdict_get_int(qdict, "size");
1559 int addr = qdict_get_int(qdict, "addr");
1560 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001561 uint32_t val;
1562 int suffix;
1563
1564 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001565 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001566 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001567 addr++;
1568 }
1569 addr &= 0xffff;
1570
1571 switch(size) {
1572 default:
1573 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001574 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001575 suffix = 'b';
1576 break;
1577 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001578 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001579 suffix = 'w';
1580 break;
1581 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001582 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001583 suffix = 'l';
1584 break;
1585 }
aliguori376253e2009-03-05 23:01:23 +00001586 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1587 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001588}
bellarda3a91a32004-06-04 11:06:21 +00001589
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001590static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001591{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001592 int size = qdict_get_int(qdict, "size");
1593 int addr = qdict_get_int(qdict, "addr");
1594 int val = qdict_get_int(qdict, "val");
1595
Jan Kiszkaf1147842009-07-14 10:20:11 +02001596 addr &= IOPORTS_MASK;
1597
1598 switch (size) {
1599 default:
1600 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001601 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001602 break;
1603 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001604 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001605 break;
1606 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001607 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001608 break;
1609 }
1610}
1611
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001612static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001613{
1614 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001615 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001616
Jan Kiszka76e30d02009-07-02 00:19:02 +02001617 res = qemu_boot_set(bootdevice);
1618 if (res == 0) {
1619 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1620 } else if (res > 0) {
1621 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001622 } else {
aliguori376253e2009-03-05 23:01:23 +00001623 monitor_printf(mon, "no function defined to set boot device list for "
1624 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001625 }
1626}
1627
bellardb86bda52004-09-18 19:32:46 +00001628#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001629static void print_pte(Monitor *mon, target_phys_addr_t addr,
1630 target_phys_addr_t pte,
1631 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001632{
Blue Swirld65aaf32010-12-11 18:56:24 +00001633#ifdef TARGET_X86_64
1634 if (addr & (1ULL << 47)) {
1635 addr |= -1LL << 48;
1636 }
1637#endif
1638 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1639 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001640 addr,
1641 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001642 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001643 pte & PG_GLOBAL_MASK ? 'G' : '-',
1644 pte & PG_PSE_MASK ? 'P' : '-',
1645 pte & PG_DIRTY_MASK ? 'D' : '-',
1646 pte & PG_ACCESSED_MASK ? 'A' : '-',
1647 pte & PG_PCD_MASK ? 'C' : '-',
1648 pte & PG_PWT_MASK ? 'T' : '-',
1649 pte & PG_USER_MASK ? 'U' : '-',
1650 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001651}
1652
Andreas Färber9349b4f2012-03-14 01:38:32 +01001653static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001654{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001655 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001656 uint32_t pgd, pde, pte;
1657
bellardb86bda52004-09-18 19:32:46 +00001658 pgd = env->cr[3] & ~0xfff;
1659 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001660 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001661 pde = le32_to_cpu(pde);
1662 if (pde & PG_PRESENT_MASK) {
1663 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001664 /* 4M pages */
1665 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001666 } else {
1667 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001668 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001669 pte = le32_to_cpu(pte);
1670 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001671 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001672 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001673 ~0xfff);
1674 }
1675 }
1676 }
1677 }
1678 }
1679}
1680
Andreas Färber9349b4f2012-03-14 01:38:32 +01001681static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001682{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001683 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001684 uint64_t pdpe, pde, pte;
1685 uint64_t pdp_addr, pd_addr, pt_addr;
1686
1687 pdp_addr = env->cr[3] & ~0x1f;
1688 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001689 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001690 pdpe = le64_to_cpu(pdpe);
1691 if (pdpe & PG_PRESENT_MASK) {
1692 pd_addr = pdpe & 0x3fffffffff000ULL;
1693 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001694 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001695 pde = le64_to_cpu(pde);
1696 if (pde & PG_PRESENT_MASK) {
1697 if (pde & PG_PSE_MASK) {
1698 /* 2M pages with PAE, CR4.PSE is ignored */
1699 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1700 ~((target_phys_addr_t)(1 << 20) - 1));
1701 } else {
1702 pt_addr = pde & 0x3fffffffff000ULL;
1703 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001704 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001705 pte = le64_to_cpu(pte);
1706 if (pte & PG_PRESENT_MASK) {
1707 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1708 + (l3 << 12),
1709 pte & ~PG_PSE_MASK,
1710 ~(target_phys_addr_t)0xfff);
1711 }
1712 }
1713 }
1714 }
1715 }
1716 }
1717 }
1718}
1719
1720#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001721static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001722{
1723 uint64_t l1, l2, l3, l4;
1724 uint64_t pml4e, pdpe, pde, pte;
1725 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1726
1727 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1728 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001729 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001730 pml4e = le64_to_cpu(pml4e);
1731 if (pml4e & PG_PRESENT_MASK) {
1732 pdp_addr = pml4e & 0x3fffffffff000ULL;
1733 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001734 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001735 pdpe = le64_to_cpu(pdpe);
1736 if (pdpe & PG_PRESENT_MASK) {
1737 if (pdpe & PG_PSE_MASK) {
1738 /* 1G pages, CR4.PSE is ignored */
1739 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1740 0x3ffffc0000000ULL);
1741 } else {
1742 pd_addr = pdpe & 0x3fffffffff000ULL;
1743 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001744 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001745 pde = le64_to_cpu(pde);
1746 if (pde & PG_PRESENT_MASK) {
1747 if (pde & PG_PSE_MASK) {
1748 /* 2M pages, CR4.PSE is ignored */
1749 print_pte(mon, (l1 << 39) + (l2 << 30) +
1750 (l3 << 21), pde,
1751 0x3ffffffe00000ULL);
1752 } else {
1753 pt_addr = pde & 0x3fffffffff000ULL;
1754 for (l4 = 0; l4 < 512; l4++) {
1755 cpu_physical_memory_read(pt_addr
1756 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001757 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001758 pte = le64_to_cpu(pte);
1759 if (pte & PG_PRESENT_MASK) {
1760 print_pte(mon, (l1 << 39) +
1761 (l2 << 30) +
1762 (l3 << 21) + (l4 << 12),
1763 pte & ~PG_PSE_MASK,
1764 0x3fffffffff000ULL);
1765 }
1766 }
1767 }
1768 }
1769 }
1770 }
1771 }
1772 }
1773 }
1774 }
1775}
1776#endif
1777
1778static void tlb_info(Monitor *mon)
1779{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001780 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001781
1782 env = mon_get_cpu();
1783
1784 if (!(env->cr[0] & CR0_PG_MASK)) {
1785 monitor_printf(mon, "PG disabled\n");
1786 return;
1787 }
1788 if (env->cr[4] & CR4_PAE_MASK) {
1789#ifdef TARGET_X86_64
1790 if (env->hflags & HF_LMA_MASK) {
1791 tlb_info_64(mon, env);
1792 } else
1793#endif
1794 {
1795 tlb_info_pae32(mon, env);
1796 }
1797 } else {
1798 tlb_info_32(mon, env);
1799 }
1800}
1801
Blue Swirl1b3cba62010-12-11 18:56:27 +00001802static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
1803 int *plast_prot,
1804 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001805{
bellard9746b152004-11-11 18:30:24 +00001806 int prot1;
1807 prot1 = *plast_prot;
1808 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001809 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001810 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1811 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001812 *pstart, end, end - *pstart,
1813 prot1 & PG_USER_MASK ? 'u' : '-',
1814 'r',
1815 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001816 }
1817 if (prot != 0)
1818 *pstart = end;
1819 else
1820 *pstart = -1;
1821 *plast_prot = prot;
1822 }
1823}
1824
Andreas Färber9349b4f2012-03-14 01:38:32 +01001825static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001826{
Austin Clementsb49ca722011-08-14 23:19:21 -04001827 unsigned int l1, l2;
1828 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001829 uint32_t pgd, pde, pte;
1830 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00001831
bellardb86bda52004-09-18 19:32:46 +00001832 pgd = env->cr[3] & ~0xfff;
1833 last_prot = 0;
1834 start = -1;
1835 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001836 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001837 pde = le32_to_cpu(pde);
1838 end = l1 << 22;
1839 if (pde & PG_PRESENT_MASK) {
1840 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1841 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001842 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001843 } else {
1844 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001845 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001846 pte = le32_to_cpu(pte);
1847 end = (l1 << 22) + (l2 << 12);
1848 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001849 prot = pte & pde &
1850 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001851 } else {
1852 prot = 0;
1853 }
aliguori376253e2009-03-05 23:01:23 +00001854 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001855 }
1856 }
1857 } else {
1858 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001859 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001860 }
1861 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001862 /* Flush last range */
1863 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001864}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001865
Andreas Färber9349b4f2012-03-14 01:38:32 +01001866static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001867{
Austin Clementsb49ca722011-08-14 23:19:21 -04001868 unsigned int l1, l2, l3;
1869 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001870 uint64_t pdpe, pde, pte;
1871 uint64_t pdp_addr, pd_addr, pt_addr;
1872 target_phys_addr_t start, end;
1873
1874 pdp_addr = env->cr[3] & ~0x1f;
1875 last_prot = 0;
1876 start = -1;
1877 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001878 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001879 pdpe = le64_to_cpu(pdpe);
1880 end = l1 << 30;
1881 if (pdpe & PG_PRESENT_MASK) {
1882 pd_addr = pdpe & 0x3fffffffff000ULL;
1883 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001884 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001885 pde = le64_to_cpu(pde);
1886 end = (l1 << 30) + (l2 << 21);
1887 if (pde & PG_PRESENT_MASK) {
1888 if (pde & PG_PSE_MASK) {
1889 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1890 PG_PRESENT_MASK);
1891 mem_print(mon, &start, &last_prot, end, prot);
1892 } else {
1893 pt_addr = pde & 0x3fffffffff000ULL;
1894 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001895 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001896 pte = le64_to_cpu(pte);
1897 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1898 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001899 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1900 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001901 } else {
1902 prot = 0;
1903 }
1904 mem_print(mon, &start, &last_prot, end, prot);
1905 }
1906 }
1907 } else {
1908 prot = 0;
1909 mem_print(mon, &start, &last_prot, end, prot);
1910 }
1911 }
1912 } else {
1913 prot = 0;
1914 mem_print(mon, &start, &last_prot, end, prot);
1915 }
1916 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001917 /* Flush last range */
1918 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001919}
1920
1921
1922#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001923static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001924{
1925 int prot, last_prot;
1926 uint64_t l1, l2, l3, l4;
1927 uint64_t pml4e, pdpe, pde, pte;
1928 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1929
1930 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1931 last_prot = 0;
1932 start = -1;
1933 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001934 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001935 pml4e = le64_to_cpu(pml4e);
1936 end = l1 << 39;
1937 if (pml4e & PG_PRESENT_MASK) {
1938 pdp_addr = pml4e & 0x3fffffffff000ULL;
1939 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001940 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001941 pdpe = le64_to_cpu(pdpe);
1942 end = (l1 << 39) + (l2 << 30);
1943 if (pdpe & PG_PRESENT_MASK) {
1944 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001945 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1946 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001947 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001948 mem_print(mon, &start, &last_prot, end, prot);
1949 } else {
1950 pd_addr = pdpe & 0x3fffffffff000ULL;
1951 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001952 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001953 pde = le64_to_cpu(pde);
1954 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1955 if (pde & PG_PRESENT_MASK) {
1956 if (pde & PG_PSE_MASK) {
1957 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1958 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001959 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001960 mem_print(mon, &start, &last_prot, end, prot);
1961 } else {
1962 pt_addr = pde & 0x3fffffffff000ULL;
1963 for (l4 = 0; l4 < 512; l4++) {
1964 cpu_physical_memory_read(pt_addr
1965 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001966 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001967 pte = le64_to_cpu(pte);
1968 end = (l1 << 39) + (l2 << 30) +
1969 (l3 << 21) + (l4 << 12);
1970 if (pte & PG_PRESENT_MASK) {
1971 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1972 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001973 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001974 } else {
1975 prot = 0;
1976 }
1977 mem_print(mon, &start, &last_prot, end, prot);
1978 }
1979 }
1980 } else {
1981 prot = 0;
1982 mem_print(mon, &start, &last_prot, end, prot);
1983 }
1984 }
1985 }
1986 } else {
1987 prot = 0;
1988 mem_print(mon, &start, &last_prot, end, prot);
1989 }
1990 }
1991 } else {
1992 prot = 0;
1993 mem_print(mon, &start, &last_prot, end, prot);
1994 }
1995 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001996 /* Flush last range */
1997 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001998}
1999#endif
2000
2001static void mem_info(Monitor *mon)
2002{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002003 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002004
2005 env = mon_get_cpu();
2006
2007 if (!(env->cr[0] & CR0_PG_MASK)) {
2008 monitor_printf(mon, "PG disabled\n");
2009 return;
2010 }
2011 if (env->cr[4] & CR4_PAE_MASK) {
2012#ifdef TARGET_X86_64
2013 if (env->hflags & HF_LMA_MASK) {
2014 mem_info_64(mon, env);
2015 } else
2016#endif
2017 {
2018 mem_info_pae32(mon, env);
2019 }
2020 } else {
2021 mem_info_32(mon, env);
2022 }
2023}
bellardb86bda52004-09-18 19:32:46 +00002024#endif
2025
aurel327c664e22009-03-03 06:12:22 +00002026#if defined(TARGET_SH4)
2027
aliguori376253e2009-03-05 23:01:23 +00002028static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002029{
aliguori376253e2009-03-05 23:01:23 +00002030 monitor_printf(mon, " tlb%i:\t"
2031 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2032 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2033 "dirty=%hhu writethrough=%hhu\n",
2034 idx,
2035 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2036 tlb->v, tlb->sh, tlb->c, tlb->pr,
2037 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002038}
2039
aliguori376253e2009-03-05 23:01:23 +00002040static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002041{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002042 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00002043 int i;
2044
aliguori376253e2009-03-05 23:01:23 +00002045 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002046 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002047 print_tlb (mon, i, &env->itlb[i]);
2048 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002049 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002050 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002051}
2052
2053#endif
2054
Max Filippov692f7372012-01-07 20:02:40 +04002055#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Blue Swirld41160a2010-12-19 13:42:56 +00002056static void tlb_info(Monitor *mon)
2057{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002058 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00002059
2060 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2061}
2062#endif
2063
Blue Swirl314e2982011-09-11 20:22:05 +00002064static void do_info_mtree(Monitor *mon)
2065{
2066 mtree_info((fprintf_function)monitor_printf, mon);
2067}
2068
aliguori030ea372009-04-21 22:30:47 +00002069static void do_info_numa(Monitor *mon)
2070{
aliguorib28b6232009-04-22 20:20:29 +00002071 int i;
Andreas Färber9349b4f2012-03-14 01:38:32 +01002072 CPUArchState *env;
aliguori030ea372009-04-21 22:30:47 +00002073
2074 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2075 for (i = 0; i < nb_numa_nodes; i++) {
2076 monitor_printf(mon, "node %d cpus:", i);
2077 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2078 if (env->numa_node == i) {
2079 monitor_printf(mon, " %d", env->cpu_index);
2080 }
2081 }
2082 monitor_printf(mon, "\n");
2083 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2084 node_mem[i] >> 20);
2085 }
2086}
2087
bellard5f1ce942006-02-08 22:40:15 +00002088#ifdef CONFIG_PROFILER
2089
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002090int64_t qemu_time;
2091int64_t dev_time;
2092
aliguori376253e2009-03-05 23:01:23 +00002093static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002094{
2095 int64_t total;
2096 total = qemu_time;
2097 if (total == 0)
2098 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002099 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002100 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002101 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002102 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002103 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002104 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002105}
2106#else
aliguori376253e2009-03-05 23:01:23 +00002107static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002108{
aliguori376253e2009-03-05 23:01:23 +00002109 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002110}
2111#endif
2112
bellardec36b692006-07-16 18:57:03 +00002113/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002114static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002115
aliguori376253e2009-03-05 23:01:23 +00002116static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002117{
2118 int i;
2119 CaptureState *s;
2120
2121 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002122 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002123 s->ops.info (s->opaque);
2124 }
2125}
2126
Blue Swirl23130862009-06-06 08:22:04 +00002127#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002128static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002129{
2130 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002131 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002132 CaptureState *s;
2133
2134 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2135 if (i == n) {
2136 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002137 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002138 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002139 return;
2140 }
2141 }
2142}
2143
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002144static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002145{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002146 const char *path = qdict_get_str(qdict, "path");
2147 int has_freq = qdict_haskey(qdict, "freq");
2148 int freq = qdict_get_try_int(qdict, "freq", -1);
2149 int has_bits = qdict_haskey(qdict, "bits");
2150 int bits = qdict_get_try_int(qdict, "bits", -1);
2151 int has_channels = qdict_haskey(qdict, "nchannels");
2152 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002153 CaptureState *s;
2154
Anthony Liguori7267c092011-08-20 22:09:37 -05002155 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002156
2157 freq = has_freq ? freq : 44100;
2158 bits = has_bits ? bits : 16;
2159 nchannels = has_channels ? nchannels : 2;
2160
2161 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002162 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002163 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002164 return;
bellardec36b692006-07-16 18:57:03 +00002165 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002166 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002167}
2168#endif
2169
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002171{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002172 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002173
aliguori76655d62009-03-06 20:27:37 +00002174 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002175 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002176 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002177 return acl;
2178}
aliguori76655d62009-03-06 20:27:37 +00002179
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002180static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002181{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002182 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183 qemu_acl *acl = find_acl(mon, aclname);
2184 qemu_acl_entry *entry;
2185 int i = 0;
2186
2187 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002188 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002189 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002190 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002191 i++;
2192 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002193 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002194 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002195 }
2196}
2197
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002198static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002199{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002200 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002201 qemu_acl *acl = find_acl(mon, aclname);
2202
2203 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002204 qemu_acl_reset(acl);
2205 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002206 }
2207}
aliguori76655d62009-03-06 20:27:37 +00002208
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002209static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002210{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002211 const char *aclname = qdict_get_str(qdict, "aclname");
2212 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002213 qemu_acl *acl = find_acl(mon, aclname);
2214
2215 if (acl) {
2216 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002217 acl->defaultDeny = 0;
2218 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002219 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002220 acl->defaultDeny = 1;
2221 monitor_printf(mon, "acl: policy set to 'deny'\n");
2222 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002223 monitor_printf(mon, "acl: unknown policy '%s', "
2224 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002225 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002226 }
2227}
aliguori76655d62009-03-06 20:27:37 +00002228
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002229static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002230{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002231 const char *aclname = qdict_get_str(qdict, "aclname");
2232 const char *match = qdict_get_str(qdict, "match");
2233 const char *policy = qdict_get_str(qdict, "policy");
2234 int has_index = qdict_haskey(qdict, "index");
2235 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002236 qemu_acl *acl = find_acl(mon, aclname);
2237 int deny, ret;
2238
2239 if (acl) {
2240 if (strcmp(policy, "allow") == 0) {
2241 deny = 0;
2242 } else if (strcmp(policy, "deny") == 0) {
2243 deny = 1;
2244 } else {
2245 monitor_printf(mon, "acl: unknown policy '%s', "
2246 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002247 return;
2248 }
aliguori28a76be2009-03-06 20:27:40 +00002249 if (has_index)
2250 ret = qemu_acl_insert(acl, deny, match, index);
2251 else
2252 ret = qemu_acl_append(acl, deny, match);
2253 if (ret < 0)
2254 monitor_printf(mon, "acl: unable to add acl entry\n");
2255 else
2256 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002257 }
2258}
aliguori76655d62009-03-06 20:27:37 +00002259
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002260static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002261{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002262 const char *aclname = qdict_get_str(qdict, "aclname");
2263 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002264 qemu_acl *acl = find_acl(mon, aclname);
2265 int ret;
aliguori76655d62009-03-06 20:27:37 +00002266
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002267 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002268 ret = qemu_acl_remove(acl, match);
2269 if (ret < 0)
2270 monitor_printf(mon, "acl: no matching acl entry\n");
2271 else
2272 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002273 }
2274}
2275
Huang Ying79c4f6b2009-06-23 10:05:14 +08002276#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002277static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002278{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002279 CPUArchState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002280 int cpu_index = qdict_get_int(qdict, "cpu_index");
2281 int bank = qdict_get_int(qdict, "bank");
2282 uint64_t status = qdict_get_int(qdict, "status");
2283 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2284 uint64_t addr = qdict_get_int(qdict, "addr");
2285 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002286 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002287
Jan Kiszka747461c2011-03-02 08:56:10 +01002288 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2289 flags |= MCE_INJECT_BROADCAST;
2290 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002291 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002292 if (cenv->cpu_index == cpu_index) {
2293 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002294 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002295 break;
2296 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002297 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002298}
2299#endif
2300
Corey Bryant208c9d12012-06-22 14:36:09 -04002301void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002302{
Anthony Liguoric227f092009-10-01 16:12:16 -05002303 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002304 int fd;
2305
Corey Bryant208c9d12012-06-22 14:36:09 -04002306 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002307 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002308 error_set(errp, QERR_FD_NOT_SUPPLIED);
2309 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002310 }
2311
2312 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002313 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2314 "a name not starting with a digit");
2315 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002316 }
2317
Corey Bryant208c9d12012-06-22 14:36:09 -04002318 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002319 if (strcmp(monfd->name, fdname) != 0) {
2320 continue;
2321 }
2322
2323 close(monfd->fd);
2324 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002325 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002326 }
2327
Anthony Liguori7267c092011-08-20 22:09:37 -05002328 monfd = g_malloc0(sizeof(mon_fd_t));
2329 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002330 monfd->fd = fd;
2331
Corey Bryant208c9d12012-06-22 14:36:09 -04002332 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002333}
2334
Corey Bryant208c9d12012-06-22 14:36:09 -04002335void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002336{
Anthony Liguoric227f092009-10-01 16:12:16 -05002337 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002338
Corey Bryant208c9d12012-06-22 14:36:09 -04002339 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002340 if (strcmp(monfd->name, fdname) != 0) {
2341 continue;
2342 }
2343
Blue Swirl72cf2d42009-09-12 07:36:22 +00002344 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002345 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002346 g_free(monfd->name);
2347 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002348 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002349 }
2350
Corey Bryant208c9d12012-06-22 14:36:09 -04002351 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002352}
2353
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002354static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002355{
Luiz Capitulino13548692011-07-29 15:36:43 -03002356 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002357 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002358
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002359 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002360
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002361 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002362 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002363 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002364}
2365
Mark McLoughlin7768e042009-07-22 09:11:41 +01002366int monitor_get_fd(Monitor *mon, const char *fdname)
2367{
Anthony Liguoric227f092009-10-01 16:12:16 -05002368 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002369
Blue Swirl72cf2d42009-09-12 07:36:22 +00002370 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002371 int fd;
2372
2373 if (strcmp(monfd->name, fdname) != 0) {
2374 continue;
2375 }
2376
2377 fd = monfd->fd;
2378
2379 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002380 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002381 g_free(monfd->name);
2382 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002383
2384 return fd;
2385 }
2386
2387 return -1;
2388}
2389
Corey Bryantba1c0482012-08-14 16:43:43 -04002390static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2391{
2392 MonFdsetFd *mon_fdset_fd;
2393 MonFdsetFd *mon_fdset_fd_next;
2394
2395 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantefb87c12012-08-14 16:43:48 -04002396 if (mon_fdset_fd->removed ||
2397 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002398 close(mon_fdset_fd->fd);
2399 g_free(mon_fdset_fd->opaque);
2400 QLIST_REMOVE(mon_fdset_fd, next);
2401 g_free(mon_fdset_fd);
2402 }
2403 }
2404
Corey Bryantadb696f2012-08-14 16:43:47 -04002405 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002406 QLIST_REMOVE(mon_fdset, next);
2407 g_free(mon_fdset);
2408 }
2409}
2410
Corey Bryantefb87c12012-08-14 16:43:48 -04002411static void monitor_fdsets_cleanup(void)
2412{
2413 MonFdset *mon_fdset;
2414 MonFdset *mon_fdset_next;
2415
2416 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2417 monitor_fdset_cleanup(mon_fdset);
2418 }
2419}
2420
Corey Bryantba1c0482012-08-14 16:43:43 -04002421AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2422 const char *opaque, Error **errp)
2423{
2424 int fd;
2425 Monitor *mon = cur_mon;
2426 MonFdset *mon_fdset;
2427 MonFdsetFd *mon_fdset_fd;
2428 AddfdInfo *fdinfo;
2429
2430 fd = qemu_chr_fe_get_msgfd(mon->chr);
2431 if (fd == -1) {
2432 error_set(errp, QERR_FD_NOT_SUPPLIED);
2433 goto error;
2434 }
2435
2436 if (has_fdset_id) {
2437 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2438 if (mon_fdset->id == fdset_id) {
2439 break;
2440 }
2441 }
2442 if (mon_fdset == NULL) {
2443 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2444 "an existing fdset-id");
2445 goto error;
2446 }
2447 } else {
2448 int64_t fdset_id_prev = -1;
2449 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2450
2451 /* Use first available fdset ID */
2452 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2453 mon_fdset_cur = mon_fdset;
2454 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2455 fdset_id_prev = mon_fdset_cur->id;
2456 continue;
2457 }
2458 break;
2459 }
2460
2461 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2462 mon_fdset->id = fdset_id_prev + 1;
2463
2464 /* The fdset list is ordered by fdset ID */
2465 if (mon_fdset->id == 0) {
2466 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2467 } else if (mon_fdset->id < mon_fdset_cur->id) {
2468 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2469 } else {
2470 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2471 }
2472 }
2473
2474 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2475 mon_fdset_fd->fd = fd;
2476 mon_fdset_fd->removed = false;
2477 if (has_opaque) {
2478 mon_fdset_fd->opaque = g_strdup(opaque);
2479 }
2480 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2481
2482 fdinfo = g_malloc0(sizeof(*fdinfo));
2483 fdinfo->fdset_id = mon_fdset->id;
2484 fdinfo->fd = mon_fdset_fd->fd;
2485
2486 return fdinfo;
2487
2488error:
2489 if (fd != -1) {
2490 close(fd);
2491 }
2492 return NULL;
2493}
2494
2495void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2496{
2497 MonFdset *mon_fdset;
2498 MonFdsetFd *mon_fdset_fd;
2499 char fd_str[60];
2500
2501 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2502 if (mon_fdset->id != fdset_id) {
2503 continue;
2504 }
2505 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2506 if (has_fd) {
2507 if (mon_fdset_fd->fd != fd) {
2508 continue;
2509 }
2510 mon_fdset_fd->removed = true;
2511 break;
2512 } else {
2513 mon_fdset_fd->removed = true;
2514 }
2515 }
2516 if (has_fd && !mon_fdset_fd) {
2517 goto error;
2518 }
2519 monitor_fdset_cleanup(mon_fdset);
2520 return;
2521 }
2522
2523error:
2524 if (has_fd) {
2525 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2526 fdset_id, fd);
2527 } else {
2528 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2529 }
2530 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2531}
2532
2533FdsetInfoList *qmp_query_fdsets(Error **errp)
2534{
2535 MonFdset *mon_fdset;
2536 MonFdsetFd *mon_fdset_fd;
2537 FdsetInfoList *fdset_list = NULL;
2538
2539 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2540 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2541 FdsetFdInfoList *fdsetfd_list = NULL;
2542
2543 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2544 fdset_info->value->fdset_id = mon_fdset->id;
2545
2546 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2547 FdsetFdInfoList *fdsetfd_info;
2548
2549 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2550 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2551 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2552 if (mon_fdset_fd->opaque) {
2553 fdsetfd_info->value->has_opaque = true;
2554 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2555 } else {
2556 fdsetfd_info->value->has_opaque = false;
2557 }
2558
2559 fdsetfd_info->next = fdsetfd_list;
2560 fdsetfd_list = fdsetfd_info;
2561 }
2562
2563 fdset_info->value->fds = fdsetfd_list;
2564
2565 fdset_info->next = fdset_list;
2566 fdset_list = fdset_info;
2567 }
2568
2569 return fdset_list;
2570}
2571
Corey Bryantadb696f2012-08-14 16:43:47 -04002572int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2573{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002574#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002575 MonFdset *mon_fdset;
2576 MonFdsetFd *mon_fdset_fd;
2577 int mon_fd_flags;
2578
Corey Bryantadb696f2012-08-14 16:43:47 -04002579 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2580 if (mon_fdset->id != fdset_id) {
2581 continue;
2582 }
2583 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2584 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2585 if (mon_fd_flags == -1) {
2586 return -1;
2587 }
2588
2589 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2590 return mon_fdset_fd->fd;
2591 }
2592 }
2593 errno = EACCES;
2594 return -1;
2595 }
2596#endif
2597
2598 errno = ENOENT;
2599 return -1;
2600}
2601
2602int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2603{
2604 MonFdset *mon_fdset;
2605 MonFdsetFd *mon_fdset_fd_dup;
2606
2607 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2608 if (mon_fdset->id != fdset_id) {
2609 continue;
2610 }
2611 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2612 if (mon_fdset_fd_dup->fd == dup_fd) {
2613 return -1;
2614 }
2615 }
2616 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2617 mon_fdset_fd_dup->fd = dup_fd;
2618 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2619 return 0;
2620 }
2621 return -1;
2622}
2623
2624static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2625{
2626 MonFdset *mon_fdset;
2627 MonFdsetFd *mon_fdset_fd_dup;
2628
2629 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2630 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2631 if (mon_fdset_fd_dup->fd == dup_fd) {
2632 if (remove) {
2633 QLIST_REMOVE(mon_fdset_fd_dup, next);
2634 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2635 monitor_fdset_cleanup(mon_fdset);
2636 }
2637 }
2638 return mon_fdset->id;
2639 }
2640 }
2641 }
2642 return -1;
2643}
2644
2645int monitor_fdset_dup_fd_find(int dup_fd)
2646{
2647 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2648}
2649
2650int monitor_fdset_dup_fd_remove(int dup_fd)
2651{
2652 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2653}
2654
Wayne Xia816f8922011-10-12 11:32:41 +08002655/* mon_cmds and info_cmds would be sorted at runtime */
2656static mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002657#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002658 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002659};
2660
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002661/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002662static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002663 {
2664 .name = "version",
2665 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .params = "",
2667 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002668 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "network",
2672 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 .params = "",
2674 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002675 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 },
2677 {
2678 .name = "chardev",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002682 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
2684 {
2685 .name = "block",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002689 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "blockstats",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002696 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002699 .name = "block-jobs",
2700 .args_type = "",
2701 .params = "",
2702 .help = "show progress of ongoing block device operations",
2703 .mhandler.info = hmp_info_block_jobs,
2704 },
2705 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 .name = "registers",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002710 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
2713 .name = "cpus",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002717 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 },
2719 {
2720 .name = "history",
2721 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 .params = "",
2723 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002724 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002726#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2727 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 {
2729 .name = "irq",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002733#ifdef TARGET_SPARC
2734 .mhandler.info = sun4m_irq_info,
2735#elif defined(TARGET_LM32)
2736 .mhandler.info = lm32_irq_info,
2737#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002738 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002739#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 },
2741 {
2742 .name = "pic",
2743 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 .params = "",
2745 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002746#ifdef TARGET_SPARC
2747 .mhandler.info = sun4m_pic_info,
2748#elif defined(TARGET_LM32)
2749 .mhandler.info = lm32_do_pic_info,
2750#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002751 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002752#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002754#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 {
2756 .name = "pci",
2757 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 .params = "",
2759 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002760 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002762#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002763 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 {
2765 .name = "tlb",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002769 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
aurel327c664e22009-03-03 06:12:22 +00002771#endif
2772#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 {
2774 .name = "mem",
2775 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 .params = "",
2777 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002778 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002779 },
bellardb86bda52004-09-18 19:32:46 +00002780#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 {
Blue Swirl314e2982011-09-11 20:22:05 +00002782 .name = "mtree",
2783 .args_type = "",
2784 .params = "",
2785 .help = "show memory tree",
2786 .mhandler.info = do_info_mtree,
2787 },
2788 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .name = "jit",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002793 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 },
2795 {
2796 .name = "kvm",
2797 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002798 .params = "",
2799 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002800 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 },
2802 {
2803 .name = "numa",
2804 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 .params = "",
2806 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002807 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 },
2809 {
2810 .name = "usb",
2811 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002812 .params = "",
2813 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002814 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002815 },
2816 {
2817 .name = "usbhost",
2818 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002819 .params = "",
2820 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002821 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 },
2823 {
2824 .name = "profile",
2825 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002826 .params = "",
2827 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002828 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 },
2830 {
2831 .name = "capture",
2832 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 .params = "",
2834 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002835 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002836 },
2837 {
2838 .name = "snapshots",
2839 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 .params = "",
2841 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002842 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 },
2844 {
2845 .name = "status",
2846 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002847 .params = "",
2848 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002849 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 },
2851 {
2852 .name = "pcmcia",
2853 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002854 .params = "",
2855 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002856 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002857 },
2858 {
2859 .name = "mice",
2860 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002861 .params = "",
2862 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002863 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 },
2865 {
2866 .name = "vnc",
2867 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002868 .params = "",
2869 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002870 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002871 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002872#if defined(CONFIG_SPICE)
2873 {
2874 .name = "spice",
2875 .args_type = "",
2876 .params = "",
2877 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002878 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002879 },
2880#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 {
2882 .name = "name",
2883 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002884 .params = "",
2885 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002886 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 },
2888 {
2889 .name = "uuid",
2890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 .params = "",
2892 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002893 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002894 },
j_mayer76a66252007-03-07 08:32:30 +00002895#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 {
2897 .name = "cpustats",
2898 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 .params = "",
2900 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002901 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002902 },
j_mayer76a66252007-03-07 08:32:30 +00002903#endif
blueswir131a60e22007-10-26 18:42:59 +00002904#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 {
2906 .name = "usernet",
2907 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002908 .params = "",
2909 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002910 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002911 },
blueswir131a60e22007-10-26 18:42:59 +00002912#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002913 {
2914 .name = "migrate",
2915 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002916 .params = "",
2917 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002918 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 },
2920 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002921 .name = "migrate_capabilities",
2922 .args_type = "",
2923 .params = "",
2924 .help = "show current migration capabilities",
2925 .mhandler.info = hmp_info_migrate_capabilities,
2926 },
2927 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002928 .name = "migrate_cache_size",
2929 .args_type = "",
2930 .params = "",
2931 .help = "show current migration xbzrle cache size",
2932 .mhandler.info = hmp_info_migrate_cache_size,
2933 },
2934 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002935 .name = "balloon",
2936 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002937 .params = "",
2938 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002939 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002940 },
2941 {
2942 .name = "qtree",
2943 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002944 .params = "",
2945 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002946 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002947 },
2948 {
2949 .name = "qdm",
2950 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002951 .params = "",
2952 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002953 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002954 },
2955 {
2956 .name = "roms",
2957 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002958 .params = "",
2959 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002960 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002961 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302962 {
2963 .name = "trace-events",
2964 .args_type = "",
2965 .params = "",
2966 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002967 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302968 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002969 {
2970 .name = NULL,
2971 },
bellard9dc39cb2004-03-14 21:38:27 +00002972};
2973
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002974static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002975#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002976 { /* NULL */ },
2977};
2978
bellard9307c4c2004-04-04 12:57:25 +00002979/*******************************************************************/
2980
2981static const char *pch;
2982static jmp_buf expr_env;
2983
bellard92a31b12005-02-10 22:00:52 +00002984#define MD_TLONG 0
2985#define MD_I32 1
2986
bellard9307c4c2004-04-04 12:57:25 +00002987typedef struct MonitorDef {
2988 const char *name;
2989 int offset;
blueswir18662d652008-10-02 18:32:44 +00002990 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002991 int type;
bellard9307c4c2004-04-04 12:57:25 +00002992} MonitorDef;
2993
bellard57206fd2004-04-25 18:54:52 +00002994#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002995static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002996{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002997 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002998 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002999}
3000#endif
3001
bellarda541f292004-04-12 20:39:29 +00003002#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003003static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003004{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003005 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003006 unsigned int u;
3007 int i;
3008
3009 u = 0;
3010 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003011 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003012
3013 return u;
3014}
3015
blueswir18662d652008-10-02 18:32:44 +00003016static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003017{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003018 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003019 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003020}
3021
blueswir18662d652008-10-02 18:32:44 +00003022static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003023{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003024 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003025 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003026}
bellard9fddaa02004-05-21 12:59:32 +00003027
blueswir18662d652008-10-02 18:32:44 +00003028static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003029{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003030 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003031 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003032}
3033
blueswir18662d652008-10-02 18:32:44 +00003034static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003035{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003036 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003037 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003038}
3039
blueswir18662d652008-10-02 18:32:44 +00003040static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003041{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003042 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003043 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003044}
bellarda541f292004-04-12 20:39:29 +00003045#endif
3046
bellarde95c8d52004-09-30 22:22:08 +00003047#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003048#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003049static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003050{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003051 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003052
3053 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003054}
bellard7b936c02005-10-30 17:05:13 +00003055#endif
bellarde95c8d52004-09-30 22:22:08 +00003056
blueswir18662d652008-10-02 18:32:44 +00003057static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003058{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003059 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003060 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003061}
3062#endif
3063
blueswir18662d652008-10-02 18:32:44 +00003064static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003065#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003066
3067#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01003068 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3069 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3070 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003071
Andreas Färbere59d1672012-02-16 00:40:47 +01003072 { "eax", offsetof(CPUX86State, regs[0]) },
3073 { "ecx", offsetof(CPUX86State, regs[1]) },
3074 { "edx", offsetof(CPUX86State, regs[2]) },
3075 { "ebx", offsetof(CPUX86State, regs[3]) },
3076 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3077 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3078 { "esi", offsetof(CPUX86State, regs[6]) },
3079 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003080#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003081 { "r8", offsetof(CPUX86State, regs[8]) },
3082 { "r9", offsetof(CPUX86State, regs[9]) },
3083 { "r10", offsetof(CPUX86State, regs[10]) },
3084 { "r11", offsetof(CPUX86State, regs[11]) },
3085 { "r12", offsetof(CPUX86State, regs[12]) },
3086 { "r13", offsetof(CPUX86State, regs[13]) },
3087 { "r14", offsetof(CPUX86State, regs[14]) },
3088 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003089#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003090 { "eflags", offsetof(CPUX86State, eflags) },
3091 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003092 SEG("cs", R_CS)
3093 SEG("ds", R_DS)
3094 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003095 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003096 SEG("fs", R_FS)
3097 SEG("gs", R_GS)
3098 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003099#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003100 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003101 { "r0", offsetof(CPUPPCState, gpr[0]) },
3102 { "r1", offsetof(CPUPPCState, gpr[1]) },
3103 { "r2", offsetof(CPUPPCState, gpr[2]) },
3104 { "r3", offsetof(CPUPPCState, gpr[3]) },
3105 { "r4", offsetof(CPUPPCState, gpr[4]) },
3106 { "r5", offsetof(CPUPPCState, gpr[5]) },
3107 { "r6", offsetof(CPUPPCState, gpr[6]) },
3108 { "r7", offsetof(CPUPPCState, gpr[7]) },
3109 { "r8", offsetof(CPUPPCState, gpr[8]) },
3110 { "r9", offsetof(CPUPPCState, gpr[9]) },
3111 { "r10", offsetof(CPUPPCState, gpr[10]) },
3112 { "r11", offsetof(CPUPPCState, gpr[11]) },
3113 { "r12", offsetof(CPUPPCState, gpr[12]) },
3114 { "r13", offsetof(CPUPPCState, gpr[13]) },
3115 { "r14", offsetof(CPUPPCState, gpr[14]) },
3116 { "r15", offsetof(CPUPPCState, gpr[15]) },
3117 { "r16", offsetof(CPUPPCState, gpr[16]) },
3118 { "r17", offsetof(CPUPPCState, gpr[17]) },
3119 { "r18", offsetof(CPUPPCState, gpr[18]) },
3120 { "r19", offsetof(CPUPPCState, gpr[19]) },
3121 { "r20", offsetof(CPUPPCState, gpr[20]) },
3122 { "r21", offsetof(CPUPPCState, gpr[21]) },
3123 { "r22", offsetof(CPUPPCState, gpr[22]) },
3124 { "r23", offsetof(CPUPPCState, gpr[23]) },
3125 { "r24", offsetof(CPUPPCState, gpr[24]) },
3126 { "r25", offsetof(CPUPPCState, gpr[25]) },
3127 { "r26", offsetof(CPUPPCState, gpr[26]) },
3128 { "r27", offsetof(CPUPPCState, gpr[27]) },
3129 { "r28", offsetof(CPUPPCState, gpr[28]) },
3130 { "r29", offsetof(CPUPPCState, gpr[29]) },
3131 { "r30", offsetof(CPUPPCState, gpr[30]) },
3132 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003133 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003134 { "f0", offsetof(CPUPPCState, fpr[0]) },
3135 { "f1", offsetof(CPUPPCState, fpr[1]) },
3136 { "f2", offsetof(CPUPPCState, fpr[2]) },
3137 { "f3", offsetof(CPUPPCState, fpr[3]) },
3138 { "f4", offsetof(CPUPPCState, fpr[4]) },
3139 { "f5", offsetof(CPUPPCState, fpr[5]) },
3140 { "f6", offsetof(CPUPPCState, fpr[6]) },
3141 { "f7", offsetof(CPUPPCState, fpr[7]) },
3142 { "f8", offsetof(CPUPPCState, fpr[8]) },
3143 { "f9", offsetof(CPUPPCState, fpr[9]) },
3144 { "f10", offsetof(CPUPPCState, fpr[10]) },
3145 { "f11", offsetof(CPUPPCState, fpr[11]) },
3146 { "f12", offsetof(CPUPPCState, fpr[12]) },
3147 { "f13", offsetof(CPUPPCState, fpr[13]) },
3148 { "f14", offsetof(CPUPPCState, fpr[14]) },
3149 { "f15", offsetof(CPUPPCState, fpr[15]) },
3150 { "f16", offsetof(CPUPPCState, fpr[16]) },
3151 { "f17", offsetof(CPUPPCState, fpr[17]) },
3152 { "f18", offsetof(CPUPPCState, fpr[18]) },
3153 { "f19", offsetof(CPUPPCState, fpr[19]) },
3154 { "f20", offsetof(CPUPPCState, fpr[20]) },
3155 { "f21", offsetof(CPUPPCState, fpr[21]) },
3156 { "f22", offsetof(CPUPPCState, fpr[22]) },
3157 { "f23", offsetof(CPUPPCState, fpr[23]) },
3158 { "f24", offsetof(CPUPPCState, fpr[24]) },
3159 { "f25", offsetof(CPUPPCState, fpr[25]) },
3160 { "f26", offsetof(CPUPPCState, fpr[26]) },
3161 { "f27", offsetof(CPUPPCState, fpr[27]) },
3162 { "f28", offsetof(CPUPPCState, fpr[28]) },
3163 { "f29", offsetof(CPUPPCState, fpr[29]) },
3164 { "f30", offsetof(CPUPPCState, fpr[30]) },
3165 { "f31", offsetof(CPUPPCState, fpr[31]) },
3166 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003167 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003168 { "nip|pc", offsetof(CPUPPCState, nip) },
3169 { "lr", offsetof(CPUPPCState, lr) },
3170 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003171 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003172 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003173 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003174 { "msr", 0, &monitor_get_msr, },
3175 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003176 { "tbu", 0, &monitor_get_tbu, },
3177 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003178#if defined(TARGET_PPC64)
3179 /* Address space register */
Andreas Färbere59d1672012-02-16 00:40:47 +01003180 { "asr", offsetof(CPUPPCState, asr) },
j_mayerff937db2007-09-19 05:49:13 +00003181#endif
3182 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003183 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3184 { "sr0", offsetof(CPUPPCState, sr[0]) },
3185 { "sr1", offsetof(CPUPPCState, sr[1]) },
3186 { "sr2", offsetof(CPUPPCState, sr[2]) },
3187 { "sr3", offsetof(CPUPPCState, sr[3]) },
3188 { "sr4", offsetof(CPUPPCState, sr[4]) },
3189 { "sr5", offsetof(CPUPPCState, sr[5]) },
3190 { "sr6", offsetof(CPUPPCState, sr[6]) },
3191 { "sr7", offsetof(CPUPPCState, sr[7]) },
3192 { "sr8", offsetof(CPUPPCState, sr[8]) },
3193 { "sr9", offsetof(CPUPPCState, sr[9]) },
3194 { "sr10", offsetof(CPUPPCState, sr[10]) },
3195 { "sr11", offsetof(CPUPPCState, sr[11]) },
3196 { "sr12", offsetof(CPUPPCState, sr[12]) },
3197 { "sr13", offsetof(CPUPPCState, sr[13]) },
3198 { "sr14", offsetof(CPUPPCState, sr[14]) },
3199 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003200 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003201 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003202
Andreas Färbere59d1672012-02-16 00:40:47 +01003203 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3204 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3205 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3206 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3207 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3208 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3209 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3210 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3211 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3212 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3213 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3214 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3215 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3216 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3217 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3218 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3219 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3220 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3221 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3222 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3223 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3224 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3225 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3226 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3227 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3228 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3229 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3230 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3231 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3232 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3233 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3234 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3235 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3236 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3237 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3238 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3239 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3240 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3241 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3242 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3243 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3244 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3245 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3246 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3247 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3248 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3249 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3250 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3251 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3252 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3253 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3254 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3255 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3256 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3257 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3258 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3259 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3260 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3261 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3262 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3263 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3264 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3265 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3266 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3267 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3268 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003269
bellarde95c8d52004-09-30 22:22:08 +00003270#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003271 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3272 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3273 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3274 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3275 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3276 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3277 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3278 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003279 { "o0", 0, monitor_get_reg },
3280 { "o1", 1, monitor_get_reg },
3281 { "o2", 2, monitor_get_reg },
3282 { "o3", 3, monitor_get_reg },
3283 { "o4", 4, monitor_get_reg },
3284 { "o5", 5, monitor_get_reg },
3285 { "o6", 6, monitor_get_reg },
3286 { "o7", 7, monitor_get_reg },
3287 { "l0", 8, monitor_get_reg },
3288 { "l1", 9, monitor_get_reg },
3289 { "l2", 10, monitor_get_reg },
3290 { "l3", 11, monitor_get_reg },
3291 { "l4", 12, monitor_get_reg },
3292 { "l5", 13, monitor_get_reg },
3293 { "l6", 14, monitor_get_reg },
3294 { "l7", 15, monitor_get_reg },
3295 { "i0", 16, monitor_get_reg },
3296 { "i1", 17, monitor_get_reg },
3297 { "i2", 18, monitor_get_reg },
3298 { "i3", 19, monitor_get_reg },
3299 { "i4", 20, monitor_get_reg },
3300 { "i5", 21, monitor_get_reg },
3301 { "i6", 22, monitor_get_reg },
3302 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003303 { "pc", offsetof(CPUSPARCState, pc) },
3304 { "npc", offsetof(CPUSPARCState, npc) },
3305 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003306#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003307 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003308 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003309#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003310 { "tbr", offsetof(CPUSPARCState, tbr) },
3311 { "fsr", offsetof(CPUSPARCState, fsr) },
3312 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3313 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3314 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3315 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3316 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3317 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3318 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3319 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3320 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3321 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3322 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3323 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3324 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3325 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3326 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3327 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3328 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3329 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3330 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3331 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3332 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3333 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3334 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3335 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3336 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3337 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3338 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3339 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3340 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3341 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3342 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3343 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003344#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003345 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3346 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3347 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3348 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3349 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3350 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3351 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3352 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3353 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3354 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3355 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3356 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3357 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3358 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3359 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3360 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3361 { "asi", offsetof(CPUSPARCState, asi) },
3362 { "pstate", offsetof(CPUSPARCState, pstate) },
3363 { "cansave", offsetof(CPUSPARCState, cansave) },
3364 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3365 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3366 { "wstate", offsetof(CPUSPARCState, wstate) },
3367 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3368 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003369#endif
bellard9307c4c2004-04-04 12:57:25 +00003370#endif
3371 { NULL },
3372};
3373
aliguori376253e2009-03-05 23:01:23 +00003374static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003375{
aliguori376253e2009-03-05 23:01:23 +00003376 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003377 longjmp(expr_env, 1);
3378}
3379
Markus Armbruster09b94182010-01-20 13:07:30 +01003380/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003381static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003382{
blueswir18662d652008-10-02 18:32:44 +00003383 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003384 void *ptr;
3385
bellard9307c4c2004-04-04 12:57:25 +00003386 for(md = monitor_defs; md->name != NULL; md++) {
3387 if (compare_cmd(name, md->name)) {
3388 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003389 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003390 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003391 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003392 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003393 switch(md->type) {
3394 case MD_I32:
3395 *pval = *(int32_t *)ptr;
3396 break;
3397 case MD_TLONG:
3398 *pval = *(target_long *)ptr;
3399 break;
3400 default:
3401 *pval = 0;
3402 break;
3403 }
bellard9307c4c2004-04-04 12:57:25 +00003404 }
3405 return 0;
3406 }
3407 }
3408 return -1;
3409}
3410
3411static void next(void)
3412{
Blue Swirl660f11b2009-07-31 21:16:51 +00003413 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003414 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003415 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003416 pch++;
3417 }
3418}
3419
aliguori376253e2009-03-05 23:01:23 +00003420static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003421
aliguori376253e2009-03-05 23:01:23 +00003422static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003423{
blueswir1c2efc952007-09-25 17:28:42 +00003424 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003425 char *p;
bellard6a00d602005-11-21 23:25:50 +00003426 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003427
3428 switch(*pch) {
3429 case '+':
3430 next();
aliguori376253e2009-03-05 23:01:23 +00003431 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003432 break;
3433 case '-':
3434 next();
aliguori376253e2009-03-05 23:01:23 +00003435 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003436 break;
3437 case '~':
3438 next();
aliguori376253e2009-03-05 23:01:23 +00003439 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003440 break;
3441 case '(':
3442 next();
aliguori376253e2009-03-05 23:01:23 +00003443 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003444 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003445 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003446 }
3447 next();
3448 break;
bellard81d09122004-07-14 17:21:37 +00003449 case '\'':
3450 pch++;
3451 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003452 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003453 n = *pch;
3454 pch++;
3455 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003456 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003457 next();
3458 break;
bellard9307c4c2004-04-04 12:57:25 +00003459 case '$':
3460 {
3461 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003462 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003463
bellard9307c4c2004-04-04 12:57:25 +00003464 pch++;
3465 q = buf;
3466 while ((*pch >= 'a' && *pch <= 'z') ||
3467 (*pch >= 'A' && *pch <= 'Z') ||
3468 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003469 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003470 if ((q - buf) < sizeof(buf) - 1)
3471 *q++ = *pch;
3472 pch++;
3473 }
blueswir1cd390082008-11-16 13:53:32 +00003474 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003475 pch++;
3476 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003477 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003478 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003479 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003480 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003481 }
3482 break;
3483 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003484 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003485 n = 0;
3486 break;
3487 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003488 errno = 0;
blueswir17743e582007-09-24 18:39:04 +00003489#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003490 n = strtoull(pch, &p, 0);
3491#else
bellard9307c4c2004-04-04 12:57:25 +00003492 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003493#endif
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003494 if (errno == ERANGE) {
3495 expr_error(mon, "number too large");
3496 }
bellard9307c4c2004-04-04 12:57:25 +00003497 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003498 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003499 }
3500 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003501 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003502 pch++;
3503 break;
3504 }
3505 return n;
3506}
3507
3508
aliguori376253e2009-03-05 23:01:23 +00003509static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003510{
blueswir1c2efc952007-09-25 17:28:42 +00003511 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003512 int op;
ths3b46e622007-09-17 08:09:54 +00003513
aliguori376253e2009-03-05 23:01:23 +00003514 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003515 for(;;) {
3516 op = *pch;
3517 if (op != '*' && op != '/' && op != '%')
3518 break;
3519 next();
aliguori376253e2009-03-05 23:01:23 +00003520 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003521 switch(op) {
3522 default:
3523 case '*':
3524 val *= val2;
3525 break;
3526 case '/':
3527 case '%':
ths5fafdf22007-09-16 21:08:06 +00003528 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003529 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003530 if (op == '/')
3531 val /= val2;
3532 else
3533 val %= val2;
3534 break;
3535 }
3536 }
3537 return val;
3538}
3539
aliguori376253e2009-03-05 23:01:23 +00003540static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003541{
blueswir1c2efc952007-09-25 17:28:42 +00003542 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003543 int op;
bellard9307c4c2004-04-04 12:57:25 +00003544
aliguori376253e2009-03-05 23:01:23 +00003545 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003546 for(;;) {
3547 op = *pch;
3548 if (op != '&' && op != '|' && op != '^')
3549 break;
3550 next();
aliguori376253e2009-03-05 23:01:23 +00003551 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003552 switch(op) {
3553 default:
3554 case '&':
3555 val &= val2;
3556 break;
3557 case '|':
3558 val |= val2;
3559 break;
3560 case '^':
3561 val ^= val2;
3562 break;
3563 }
3564 }
3565 return val;
3566}
3567
aliguori376253e2009-03-05 23:01:23 +00003568static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003569{
blueswir1c2efc952007-09-25 17:28:42 +00003570 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003571 int op;
bellard9307c4c2004-04-04 12:57:25 +00003572
aliguori376253e2009-03-05 23:01:23 +00003573 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003574 for(;;) {
3575 op = *pch;
3576 if (op != '+' && op != '-')
3577 break;
3578 next();
aliguori376253e2009-03-05 23:01:23 +00003579 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003580 if (op == '+')
3581 val += val2;
3582 else
3583 val -= val2;
3584 }
3585 return val;
3586}
3587
aliguori376253e2009-03-05 23:01:23 +00003588static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003589{
3590 pch = *pp;
3591 if (setjmp(expr_env)) {
3592 *pp = pch;
3593 return -1;
3594 }
blueswir1cd390082008-11-16 13:53:32 +00003595 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003596 pch++;
aliguori376253e2009-03-05 23:01:23 +00003597 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003598 *pp = pch;
3599 return 0;
3600}
3601
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003602static int get_double(Monitor *mon, double *pval, const char **pp)
3603{
3604 const char *p = *pp;
3605 char *tailp;
3606 double d;
3607
3608 d = strtod(p, &tailp);
3609 if (tailp == p) {
3610 monitor_printf(mon, "Number expected\n");
3611 return -1;
3612 }
3613 if (d != d || d - d != 0) {
3614 /* NaN or infinity */
3615 monitor_printf(mon, "Bad number\n");
3616 return -1;
3617 }
3618 *pval = d;
3619 *pp = tailp;
3620 return 0;
3621}
3622
bellard9307c4c2004-04-04 12:57:25 +00003623static int get_str(char *buf, int buf_size, const char **pp)
3624{
3625 const char *p;
3626 char *q;
3627 int c;
3628
bellard81d09122004-07-14 17:21:37 +00003629 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003630 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003631 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003632 p++;
3633 if (*p == '\0') {
3634 fail:
bellard81d09122004-07-14 17:21:37 +00003635 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003636 *pp = p;
3637 return -1;
3638 }
bellard9307c4c2004-04-04 12:57:25 +00003639 if (*p == '\"') {
3640 p++;
3641 while (*p != '\0' && *p != '\"') {
3642 if (*p == '\\') {
3643 p++;
3644 c = *p++;
3645 switch(c) {
3646 case 'n':
3647 c = '\n';
3648 break;
3649 case 'r':
3650 c = '\r';
3651 break;
3652 case '\\':
3653 case '\'':
3654 case '\"':
3655 break;
3656 default:
3657 qemu_printf("unsupported escape code: '\\%c'\n", c);
3658 goto fail;
3659 }
3660 if ((q - buf) < buf_size - 1) {
3661 *q++ = c;
3662 }
3663 } else {
3664 if ((q - buf) < buf_size - 1) {
3665 *q++ = *p;
3666 }
3667 p++;
3668 }
3669 }
3670 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003671 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003672 goto fail;
3673 }
3674 p++;
3675 } else {
blueswir1cd390082008-11-16 13:53:32 +00003676 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003677 if ((q - buf) < buf_size - 1) {
3678 *q++ = *p;
3679 }
3680 p++;
3681 }
bellard9307c4c2004-04-04 12:57:25 +00003682 }
bellard81d09122004-07-14 17:21:37 +00003683 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003684 *pp = p;
3685 return 0;
3686}
3687
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003688/*
3689 * Store the command-name in cmdname, and return a pointer to
3690 * the remaining of the command string.
3691 */
3692static const char *get_command_name(const char *cmdline,
3693 char *cmdname, size_t nlen)
3694{
3695 size_t len;
3696 const char *p, *pstart;
3697
3698 p = cmdline;
3699 while (qemu_isspace(*p))
3700 p++;
3701 if (*p == '\0')
3702 return NULL;
3703 pstart = p;
3704 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3705 p++;
3706 len = p - pstart;
3707 if (len > nlen - 1)
3708 len = nlen - 1;
3709 memcpy(cmdname, pstart, len);
3710 cmdname[len] = '\0';
3711 return p;
3712}
3713
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003714/**
3715 * Read key of 'type' into 'key' and return the current
3716 * 'type' pointer.
3717 */
3718static char *key_get_info(const char *type, char **key)
3719{
3720 size_t len;
3721 char *p, *str;
3722
3723 if (*type == ',')
3724 type++;
3725
3726 p = strchr(type, ':');
3727 if (!p) {
3728 *key = NULL;
3729 return NULL;
3730 }
3731 len = p - type;
3732
Anthony Liguori7267c092011-08-20 22:09:37 -05003733 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003734 memcpy(str, type, len);
3735 str[len] = '\0';
3736
3737 *key = str;
3738 return ++p;
3739}
3740
bellard9307c4c2004-04-04 12:57:25 +00003741static int default_fmt_format = 'x';
3742static int default_fmt_size = 4;
3743
3744#define MAX_ARGS 16
3745
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003746static int is_valid_option(const char *c, const char *typestr)
3747{
3748 char option[3];
3749
3750 option[0] = '-';
3751 option[1] = *c;
3752 option[2] = '\0';
3753
3754 typestr = strstr(typestr, option);
3755 return (typestr != NULL);
3756}
3757
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003758static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3759 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003760{
3761 const mon_cmd_t *cmd;
3762
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003763 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003764 if (compare_cmd(cmdname, cmd->name)) {
3765 return cmd;
3766 }
3767 }
3768
3769 return NULL;
3770}
3771
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003772static const mon_cmd_t *monitor_find_command(const char *cmdname)
3773{
3774 return search_dispatch_table(mon_cmds, cmdname);
3775}
3776
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003777static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3778{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003779 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003780}
3781
Anthony Liguoric227f092009-10-01 16:12:16 -05003782static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003783 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003784 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003785{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003786 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003787 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003788 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003789 char cmdname[256];
3790 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003791 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003792
3793#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003794 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003795#endif
ths3b46e622007-09-17 08:09:54 +00003796
bellard9307c4c2004-04-04 12:57:25 +00003797 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003798 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3799 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003800 return NULL;
ths3b46e622007-09-17 08:09:54 +00003801
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003802 cmd = monitor_find_command(cmdname);
3803 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003804 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003805 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003806 }
bellard9307c4c2004-04-04 12:57:25 +00003807
bellard9307c4c2004-04-04 12:57:25 +00003808 /* parse the parameters */
3809 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003810 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003811 typestr = key_get_info(typestr, &key);
3812 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003813 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003814 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003815 typestr++;
3816 switch(c) {
3817 case 'F':
bellard81d09122004-07-14 17:21:37 +00003818 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003819 case 's':
3820 {
3821 int ret;
ths3b46e622007-09-17 08:09:54 +00003822
blueswir1cd390082008-11-16 13:53:32 +00003823 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003824 p++;
3825 if (*typestr == '?') {
3826 typestr++;
3827 if (*p == '\0') {
3828 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003829 break;
bellard9307c4c2004-04-04 12:57:25 +00003830 }
3831 }
3832 ret = get_str(buf, sizeof(buf), &p);
3833 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003834 switch(c) {
3835 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003836 monitor_printf(mon, "%s: filename expected\n",
3837 cmdname);
bellard81d09122004-07-14 17:21:37 +00003838 break;
3839 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003840 monitor_printf(mon, "%s: block device name expected\n",
3841 cmdname);
bellard81d09122004-07-14 17:21:37 +00003842 break;
3843 default:
aliguori376253e2009-03-05 23:01:23 +00003844 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003845 break;
3846 }
bellard9307c4c2004-04-04 12:57:25 +00003847 goto fail;
3848 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003849 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003850 }
3851 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003852 case 'O':
3853 {
3854 QemuOptsList *opts_list;
3855 QemuOpts *opts;
3856
3857 opts_list = qemu_find_opts(key);
3858 if (!opts_list || opts_list->desc->name) {
3859 goto bad_type;
3860 }
3861 while (qemu_isspace(*p)) {
3862 p++;
3863 }
3864 if (!*p)
3865 break;
3866 if (get_str(buf, sizeof(buf), &p) < 0) {
3867 goto fail;
3868 }
3869 opts = qemu_opts_parse(opts_list, buf, 1);
3870 if (!opts) {
3871 goto fail;
3872 }
3873 qemu_opts_to_qdict(opts, qdict);
3874 qemu_opts_del(opts);
3875 }
3876 break;
bellard9307c4c2004-04-04 12:57:25 +00003877 case '/':
3878 {
3879 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003880
blueswir1cd390082008-11-16 13:53:32 +00003881 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003882 p++;
3883 if (*p == '/') {
3884 /* format found */
3885 p++;
3886 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003887 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003888 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003889 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003890 count = count * 10 + (*p - '0');
3891 p++;
3892 }
3893 }
3894 size = -1;
3895 format = -1;
3896 for(;;) {
3897 switch(*p) {
3898 case 'o':
3899 case 'd':
3900 case 'u':
3901 case 'x':
3902 case 'i':
3903 case 'c':
3904 format = *p++;
3905 break;
3906 case 'b':
3907 size = 1;
3908 p++;
3909 break;
3910 case 'h':
3911 size = 2;
3912 p++;
3913 break;
3914 case 'w':
3915 size = 4;
3916 p++;
3917 break;
3918 case 'g':
3919 case 'L':
3920 size = 8;
3921 p++;
3922 break;
3923 default:
3924 goto next;
3925 }
3926 }
3927 next:
blueswir1cd390082008-11-16 13:53:32 +00003928 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003929 monitor_printf(mon, "invalid char in format: '%c'\n",
3930 *p);
bellard9307c4c2004-04-04 12:57:25 +00003931 goto fail;
3932 }
bellard9307c4c2004-04-04 12:57:25 +00003933 if (format < 0)
3934 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003935 if (format != 'i') {
3936 /* for 'i', not specifying a size gives -1 as size */
3937 if (size < 0)
3938 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003939 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003940 }
bellard9307c4c2004-04-04 12:57:25 +00003941 default_fmt_format = format;
3942 } else {
3943 count = 1;
3944 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003945 if (format != 'i') {
3946 size = default_fmt_size;
3947 } else {
3948 size = -1;
3949 }
bellard9307c4c2004-04-04 12:57:25 +00003950 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003951 qdict_put(qdict, "count", qint_from_int(count));
3952 qdict_put(qdict, "format", qint_from_int(format));
3953 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003954 }
3955 break;
3956 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003957 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003958 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003959 {
blueswir1c2efc952007-09-25 17:28:42 +00003960 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003961
blueswir1cd390082008-11-16 13:53:32 +00003962 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003963 p++;
bellard34405572004-06-08 00:55:58 +00003964 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003965 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003966 if (*p == '\0') {
3967 typestr++;
3968 break;
3969 }
bellard34405572004-06-08 00:55:58 +00003970 } else {
3971 if (*p == '.') {
3972 p++;
blueswir1cd390082008-11-16 13:53:32 +00003973 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003974 p++;
bellard34405572004-06-08 00:55:58 +00003975 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003976 typestr++;
3977 break;
bellard34405572004-06-08 00:55:58 +00003978 }
3979 }
bellard13224a82006-07-14 22:03:35 +00003980 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003981 }
aliguori376253e2009-03-05 23:01:23 +00003982 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003983 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003984 /* Check if 'i' is greater than 32-bit */
3985 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3986 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3987 monitor_printf(mon, "integer is for 32-bit values\n");
3988 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003989 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003990 if (val < 0) {
3991 monitor_printf(mon, "enter a positive value\n");
3992 goto fail;
3993 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003994 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003995 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003996 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003997 }
3998 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003999 case 'o':
4000 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004001 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004002 char *end;
4003
4004 while (qemu_isspace(*p)) {
4005 p++;
4006 }
4007 if (*typestr == '?') {
4008 typestr++;
4009 if (*p == '\0') {
4010 break;
4011 }
4012 }
4013 val = strtosz(p, &end);
4014 if (val < 0) {
4015 monitor_printf(mon, "invalid size\n");
4016 goto fail;
4017 }
4018 qdict_put(qdict, key, qint_from_int(val));
4019 p = end;
4020 }
4021 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004022 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004023 {
4024 double val;
4025
4026 while (qemu_isspace(*p))
4027 p++;
4028 if (*typestr == '?') {
4029 typestr++;
4030 if (*p == '\0') {
4031 break;
4032 }
4033 }
4034 if (get_double(mon, &val, &p) < 0) {
4035 goto fail;
4036 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004037 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004038 switch (*p) {
4039 case 'm':
4040 val /= 1e3; p += 2; break;
4041 case 'u':
4042 val /= 1e6; p += 2; break;
4043 case 'n':
4044 val /= 1e9; p += 2; break;
4045 }
4046 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004047 if (*p && !qemu_isspace(*p)) {
4048 monitor_printf(mon, "Unknown unit suffix\n");
4049 goto fail;
4050 }
4051 qdict_put(qdict, key, qfloat_from_double(val));
4052 }
4053 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004054 case 'b':
4055 {
4056 const char *beg;
4057 int val;
4058
4059 while (qemu_isspace(*p)) {
4060 p++;
4061 }
4062 beg = p;
4063 while (qemu_isgraph(*p)) {
4064 p++;
4065 }
4066 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4067 val = 1;
4068 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4069 val = 0;
4070 } else {
4071 monitor_printf(mon, "Expected 'on' or 'off'\n");
4072 goto fail;
4073 }
4074 qdict_put(qdict, key, qbool_from_int(val));
4075 }
4076 break;
bellard9307c4c2004-04-04 12:57:25 +00004077 case '-':
4078 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004079 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004080 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004081 /* option */
ths3b46e622007-09-17 08:09:54 +00004082
bellard9307c4c2004-04-04 12:57:25 +00004083 c = *typestr++;
4084 if (c == '\0')
4085 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004086 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004087 p++;
bellard9307c4c2004-04-04 12:57:25 +00004088 if (*p == '-') {
4089 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004090 if(c != *p) {
4091 if(!is_valid_option(p, typestr)) {
4092
4093 monitor_printf(mon, "%s: unsupported option -%c\n",
4094 cmdname, *p);
4095 goto fail;
4096 } else {
4097 skip_key = 1;
4098 }
bellard9307c4c2004-04-04 12:57:25 +00004099 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004100 if(skip_key) {
4101 p = tmp;
4102 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004103 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004104 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004105 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004106 }
bellard9307c4c2004-04-04 12:57:25 +00004107 }
bellard9307c4c2004-04-04 12:57:25 +00004108 }
4109 break;
4110 default:
4111 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004112 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004113 goto fail;
4114 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004115 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004116 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004117 }
4118 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004119 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004120 p++;
4121 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004122 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4123 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004124 goto fail;
4125 }
4126
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004127 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004128
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004129fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004130 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004131 return NULL;
4132}
4133
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004134void monitor_set_error(Monitor *mon, QError *qerror)
4135{
4136 /* report only the first error */
4137 if (!mon->error) {
4138 mon->error = qerror;
4139 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004140 QDECREF(qerror);
4141 }
4142}
4143
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004144static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4145{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004146 if (ret && !monitor_has_error(mon)) {
4147 /*
4148 * If it returns failure, it must have passed on error.
4149 *
4150 * Action: Report an internal error to the client if in QMP.
4151 */
4152 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004153 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004154}
4155
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004156static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004157{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004158 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004159 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004160
4161 qdict = qdict_new();
4162
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004163 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004164 if (!cmd)
4165 goto out;
4166
Luiz Capitulino4903de02010-09-16 11:01:32 -03004167 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004168 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004169 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004170 QObject *data = NULL;
4171
4172 /* XXX: ignores the error code */
4173 cmd->mhandler.cmd_new(mon, qdict, &data);
4174 assert(!monitor_has_error(mon));
4175 if (data) {
4176 cmd->user_print(mon, data);
4177 qobject_decref(data);
4178 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004179 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004180 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004181 }
4182
Luiz Capitulino13917be2009-10-07 13:41:54 -03004183out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004184 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004185}
4186
bellard81d09122004-07-14 17:21:37 +00004187static void cmd_completion(const char *name, const char *list)
4188{
4189 const char *p, *pstart;
4190 char cmd[128];
4191 int len;
4192
4193 p = list;
4194 for(;;) {
4195 pstart = p;
4196 p = strchr(p, '|');
4197 if (!p)
4198 p = pstart + strlen(pstart);
4199 len = p - pstart;
4200 if (len > sizeof(cmd) - 2)
4201 len = sizeof(cmd) - 2;
4202 memcpy(cmd, pstart, len);
4203 cmd[len] = '\0';
4204 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004205 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004206 }
4207 if (*p == '\0')
4208 break;
4209 p++;
4210 }
4211}
4212
4213static void file_completion(const char *input)
4214{
4215 DIR *ffs;
4216 struct dirent *d;
4217 char path[1024];
4218 char file[1024], file_prefix[1024];
4219 int input_path_len;
4220 const char *p;
4221
ths5fafdf22007-09-16 21:08:06 +00004222 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004223 if (!p) {
4224 input_path_len = 0;
4225 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004226 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004227 } else {
4228 input_path_len = p - input + 1;
4229 memcpy(path, input, input_path_len);
4230 if (input_path_len > sizeof(path) - 1)
4231 input_path_len = sizeof(path) - 1;
4232 path[input_path_len] = '\0';
4233 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4234 }
4235#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004236 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4237 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004238#endif
4239 ffs = opendir(path);
4240 if (!ffs)
4241 return;
4242 for(;;) {
4243 struct stat sb;
4244 d = readdir(ffs);
4245 if (!d)
4246 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004247
4248 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4249 continue;
4250 }
4251
bellard81d09122004-07-14 17:21:37 +00004252 if (strstart(d->d_name, file_prefix, NULL)) {
4253 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004254 if (input_path_len < sizeof(file))
4255 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4256 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004257 /* stat the file to find out if it's a directory.
4258 * In that case add a slash to speed up typing long paths
4259 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004260 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004261 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004262 }
aliguori731b0362009-03-05 23:01:42 +00004263 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004264 }
4265 }
4266 closedir(ffs);
4267}
4268
aliguori51de9762009-03-05 23:00:43 +00004269static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004270{
aliguori51de9762009-03-05 23:00:43 +00004271 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004272 const char *input = opaque;
4273
4274 if (input[0] == '\0' ||
4275 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004276 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004277 }
4278}
4279
4280/* NOTE: this parser is an approximate form of the real command parser */
4281static void parse_cmdline(const char *cmdline,
4282 int *pnb_args, char **args)
4283{
4284 const char *p;
4285 int nb_args, ret;
4286 char buf[1024];
4287
4288 p = cmdline;
4289 nb_args = 0;
4290 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004291 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004292 p++;
4293 if (*p == '\0')
4294 break;
4295 if (nb_args >= MAX_ARGS)
4296 break;
4297 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004298 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004299 nb_args++;
4300 if (ret < 0)
4301 break;
4302 }
4303 *pnb_args = nb_args;
4304}
4305
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004306static const char *next_arg_type(const char *typestr)
4307{
4308 const char *p = strchr(typestr, ':');
4309 return (p != NULL ? ++p : typestr);
4310}
4311
aliguori4c36ba32009-03-05 23:01:37 +00004312static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004313{
4314 const char *cmdname;
4315 char *args[MAX_ARGS];
4316 int nb_args, i, len;
4317 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004318 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004319 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004320
4321 parse_cmdline(cmdline, &nb_args, args);
4322#ifdef DEBUG_COMPLETION
4323 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004324 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004325 }
4326#endif
4327
4328 /* if the line ends with a space, it means we want to complete the
4329 next arg */
4330 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004331 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004332 if (nb_args >= MAX_ARGS) {
4333 goto cleanup;
4334 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004335 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004336 }
4337 if (nb_args <= 1) {
4338 /* command completion */
4339 if (nb_args == 0)
4340 cmdname = "";
4341 else
4342 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004343 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004344 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004345 cmd_completion(cmdname, cmd->name);
4346 }
4347 } else {
4348 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004349 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4350 if (compare_cmd(args[0], cmd->name)) {
4351 break;
4352 }
bellard81d09122004-07-14 17:21:37 +00004353 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004354 if (!cmd->name) {
4355 goto cleanup;
4356 }
4357
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004358 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004359 for(i = 0; i < nb_args - 2; i++) {
4360 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004361 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004362 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004363 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004364 }
4365 }
4366 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004367 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004368 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004369 }
bellard81d09122004-07-14 17:21:37 +00004370 switch(*ptype) {
4371 case 'F':
4372 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004373 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004374 file_completion(str);
4375 break;
4376 case 'B':
4377 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004378 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004379 bdrv_iterate(block_completion_it, (void *)str);
4380 break;
bellard7fe48482004-10-09 18:08:01 +00004381 case 's':
4382 /* XXX: more generic ? */
4383 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004384 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004385 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4386 cmd_completion(str, cmd->name);
4387 }
bellard64866c32006-05-07 18:03:31 +00004388 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004389 char *sep = strrchr(str, '-');
4390 if (sep)
4391 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004392 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004393 for(key = key_defs; key->name != NULL; key++) {
4394 cmd_completion(str, key->name);
4395 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004396 } else if (!strcmp(cmd->name, "help|?")) {
4397 readline_set_completion_index(cur_mon->rs, strlen(str));
4398 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4399 cmd_completion(str, cmd->name);
4400 }
bellard7fe48482004-10-09 18:08:01 +00004401 }
4402 break;
bellard81d09122004-07-14 17:21:37 +00004403 default:
4404 break;
4405 }
4406 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004407
4408cleanup:
4409 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004410 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004411 }
bellard81d09122004-07-14 17:21:37 +00004412}
4413
aliguori731b0362009-03-05 23:01:42 +00004414static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004415{
aliguori731b0362009-03-05 23:01:42 +00004416 Monitor *mon = opaque;
4417
Jan Kiszkac62313b2009-12-04 14:05:29 +01004418 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004419}
4420
Luiz Capitulino09069b12010-02-04 18:10:06 -02004421static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4422{
4423 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4424 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4425}
4426
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004427/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004428 * Argument validation rules:
4429 *
4430 * 1. The argument must exist in cmd_args qdict
4431 * 2. The argument type must be the expected one
4432 *
4433 * Special case: If the argument doesn't exist in cmd_args and
4434 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4435 * checking is skipped for it.
4436 */
4437static int check_client_args_type(const QDict *client_args,
4438 const QDict *cmd_args, int flags)
4439{
4440 const QDictEntry *ent;
4441
4442 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4443 QObject *obj;
4444 QString *arg_type;
4445 const QObject *client_arg = qdict_entry_value(ent);
4446 const char *client_arg_name = qdict_entry_key(ent);
4447
4448 obj = qdict_get(cmd_args, client_arg_name);
4449 if (!obj) {
4450 if (flags & QMP_ACCEPT_UNKNOWNS) {
4451 /* handler accepts unknowns */
4452 continue;
4453 }
4454 /* client arg doesn't exist */
4455 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4456 return -1;
4457 }
4458
4459 arg_type = qobject_to_qstring(obj);
4460 assert(arg_type != NULL);
4461
4462 /* check if argument's type is correct */
4463 switch (qstring_get_str(arg_type)[0]) {
4464 case 'F':
4465 case 'B':
4466 case 's':
4467 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4468 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4469 "string");
4470 return -1;
4471 }
4472 break;
4473 case 'i':
4474 case 'l':
4475 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004476 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004477 if (qobject_type(client_arg) != QTYPE_QINT) {
4478 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4479 "int");
4480 return -1;
4481 }
4482 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004483 case 'T':
4484 if (qobject_type(client_arg) != QTYPE_QINT &&
4485 qobject_type(client_arg) != QTYPE_QFLOAT) {
4486 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4487 "number");
4488 return -1;
4489 }
4490 break;
4491 case 'b':
4492 case '-':
4493 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4494 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4495 "bool");
4496 return -1;
4497 }
4498 break;
4499 case 'O':
4500 assert(flags & QMP_ACCEPT_UNKNOWNS);
4501 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004502 case 'q':
4503 /* Any QObject can be passed. */
4504 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004505 case '/':
4506 case '.':
4507 /*
4508 * These types are not supported by QMP and thus are not
4509 * handled here. Fall through.
4510 */
4511 default:
4512 abort();
4513 }
4514 }
4515
4516 return 0;
4517}
4518
4519/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004520 * - Check if the client has passed all mandatory args
4521 * - Set special flags for argument validation
4522 */
4523static int check_mandatory_args(const QDict *cmd_args,
4524 const QDict *client_args, int *flags)
4525{
4526 const QDictEntry *ent;
4527
4528 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4529 const char *cmd_arg_name = qdict_entry_key(ent);
4530 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4531 assert(type != NULL);
4532
4533 if (qstring_get_str(type)[0] == 'O') {
4534 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4535 *flags |= QMP_ACCEPT_UNKNOWNS;
4536 } else if (qstring_get_str(type)[0] != '-' &&
4537 qstring_get_str(type)[1] != '?' &&
4538 !qdict_haskey(client_args, cmd_arg_name)) {
4539 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4540 return -1;
4541 }
4542 }
4543
4544 return 0;
4545}
4546
4547static QDict *qdict_from_args_type(const char *args_type)
4548{
4549 int i;
4550 QDict *qdict;
4551 QString *key, *type, *cur_qs;
4552
4553 assert(args_type != NULL);
4554
4555 qdict = qdict_new();
4556
4557 if (args_type == NULL || args_type[0] == '\0') {
4558 /* no args, empty qdict */
4559 goto out;
4560 }
4561
4562 key = qstring_new();
4563 type = qstring_new();
4564
4565 cur_qs = key;
4566
4567 for (i = 0;; i++) {
4568 switch (args_type[i]) {
4569 case ',':
4570 case '\0':
4571 qdict_put(qdict, qstring_get_str(key), type);
4572 QDECREF(key);
4573 if (args_type[i] == '\0') {
4574 goto out;
4575 }
4576 type = qstring_new(); /* qdict has ref */
4577 cur_qs = key = qstring_new();
4578 break;
4579 case ':':
4580 cur_qs = type;
4581 break;
4582 default:
4583 qstring_append_chr(cur_qs, args_type[i]);
4584 break;
4585 }
4586 }
4587
4588out:
4589 return qdict;
4590}
4591
4592/*
4593 * Client argument checking rules:
4594 *
4595 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004596 * 2. Each argument provided by the client must be expected
4597 * 3. Each argument provided by the client must have the type expected
4598 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004599 */
4600static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4601{
4602 int flags, err;
4603 QDict *cmd_args;
4604
4605 cmd_args = qdict_from_args_type(cmd->args_type);
4606
4607 flags = 0;
4608 err = check_mandatory_args(cmd_args, client_args, &flags);
4609 if (err) {
4610 goto out;
4611 }
4612
Luiz Capitulino4af91932010-06-22 11:44:05 -03004613 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004614
4615out:
4616 QDECREF(cmd_args);
4617 return err;
4618}
4619
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004620/*
4621 * Input object checking rules
4622 *
4623 * 1. Input object must be a dict
4624 * 2. The "execute" key must exist
4625 * 3. The "execute" key must be a string
4626 * 4. If the "arguments" key exists, it must be a dict
4627 * 5. If the "id" key exists, it can be anything (ie. json-value)
4628 * 6. Any argument not listed above is considered invalid
4629 */
4630static QDict *qmp_check_input_obj(QObject *input_obj)
4631{
4632 const QDictEntry *ent;
4633 int has_exec_key = 0;
4634 QDict *input_dict;
4635
4636 if (qobject_type(input_obj) != QTYPE_QDICT) {
4637 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4638 return NULL;
4639 }
4640
4641 input_dict = qobject_to_qdict(input_obj);
4642
4643 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4644 const char *arg_name = qdict_entry_key(ent);
4645 const QObject *arg_obj = qdict_entry_value(ent);
4646
4647 if (!strcmp(arg_name, "execute")) {
4648 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4649 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4650 "string");
4651 return NULL;
4652 }
4653 has_exec_key = 1;
4654 } else if (!strcmp(arg_name, "arguments")) {
4655 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4656 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4657 "object");
4658 return NULL;
4659 }
4660 } else if (!strcmp(arg_name, "id")) {
4661 /* FIXME: check duplicated IDs for async commands */
4662 } else {
4663 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4664 return NULL;
4665 }
4666 }
4667
4668 if (!has_exec_key) {
4669 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4670 return NULL;
4671 }
4672
4673 return input_dict;
4674}
4675
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004676static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4677 const QDict *params)
4678{
4679 int ret;
4680 QObject *data = NULL;
4681
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004682 ret = cmd->mhandler.cmd_new(mon, params, &data);
4683 handler_audit(mon, cmd, ret);
4684 monitor_protocol_emitter(mon, data);
4685 qobject_decref(data);
4686}
4687
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004688static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4689{
4690 int err;
4691 QObject *obj;
4692 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004693 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004694 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004695 Monitor *mon = cur_mon;
4696
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004697 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004698
4699 obj = json_parser_parse(tokens, NULL);
4700 if (!obj) {
4701 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004702 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004703 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004704 }
4705
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004706 input = qmp_check_input_obj(obj);
4707 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004708 qobject_decref(obj);
4709 goto err_out;
4710 }
4711
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004712 mon->mc->id = qdict_get(input, "id");
4713 qobject_incref(mon->mc->id);
4714
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004715 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004716 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004717 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004718 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004719 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004720 }
4721
Anthony Liguorie3193602011-09-02 12:34:47 -05004722 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004723 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004724 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004725 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004726 }
4727
4728 obj = qdict_get(input, "arguments");
4729 if (!obj) {
4730 args = qdict_new();
4731 } else {
4732 args = qobject_to_qdict(obj);
4733 QINCREF(args);
4734 }
4735
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004736 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004737 if (err < 0) {
4738 goto err_out;
4739 }
4740
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004741 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004742 err = qmp_async_cmd_handler(mon, cmd, args);
4743 if (err) {
4744 /* emit the error response */
4745 goto err_out;
4746 }
Adam Litke940cc302010-01-25 12:18:44 -06004747 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004748 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004749 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004750
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004751 goto out;
4752
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004753err_out:
4754 monitor_protocol_emitter(mon, NULL);
4755out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004756 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004757 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004758}
4759
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004760/**
4761 * monitor_control_read(): Read and handle QMP input
4762 */
4763static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4764{
4765 Monitor *old_mon = cur_mon;
4766
4767 cur_mon = opaque;
4768
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004769 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004770
4771 cur_mon = old_mon;
4772}
4773
aliguori731b0362009-03-05 23:01:42 +00004774static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004775{
aliguori731b0362009-03-05 23:01:42 +00004776 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004777 int i;
aliguori376253e2009-03-05 23:01:23 +00004778
aliguori731b0362009-03-05 23:01:42 +00004779 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004780
aliguoricde76ee2009-03-05 23:01:51 +00004781 if (cur_mon->rs) {
4782 for (i = 0; i < size; i++)
4783 readline_handle_byte(cur_mon->rs, buf[i]);
4784 } else {
4785 if (size == 0 || buf[size - 1] != 0)
4786 monitor_printf(cur_mon, "corrupted command\n");
4787 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004788 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004789 }
aliguori731b0362009-03-05 23:01:42 +00004790
4791 cur_mon = old_mon;
4792}
aliguorid8f44602008-10-06 13:52:44 +00004793
aliguori376253e2009-03-05 23:01:23 +00004794static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004795{
aliguori731b0362009-03-05 23:01:42 +00004796 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004797 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004798 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004799}
4800
aliguoricde76ee2009-03-05 23:01:51 +00004801int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004802{
aliguoricde76ee2009-03-05 23:01:51 +00004803 if (!mon->rs)
4804 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004805 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004806 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004807}
4808
aliguori376253e2009-03-05 23:01:23 +00004809void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004810{
aliguoricde76ee2009-03-05 23:01:51 +00004811 if (!mon->rs)
4812 return;
aliguori731b0362009-03-05 23:01:42 +00004813 if (--mon->suspend_cnt == 0)
4814 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004815}
4816
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004817static QObject *get_qmp_greeting(void)
4818{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004819 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004820
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004821 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004822 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4823}
4824
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004825/**
4826 * monitor_control_event(): Print QMP gretting
4827 */
4828static void monitor_control_event(void *opaque, int event)
4829{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004830 QObject *data;
4831 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004832
Luiz Capitulino47116d12010-02-08 17:01:30 -02004833 switch (event) {
4834 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004835 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004836 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004837 monitor_json_emitter(mon, data);
4838 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004839 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004840 break;
4841 case CHR_EVENT_CLOSED:
4842 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004843 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004844 mon_refcount--;
4845 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004846 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004847 }
4848}
4849
aliguori731b0362009-03-05 23:01:42 +00004850static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004851{
aliguori376253e2009-03-05 23:01:23 +00004852 Monitor *mon = opaque;
4853
aliguori2724b182009-03-05 23:01:47 +00004854 switch (event) {
4855 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004856 mon->mux_out = 0;
4857 if (mon->reset_seen) {
4858 readline_restart(mon->rs);
4859 monitor_resume(mon);
4860 monitor_flush(mon);
4861 } else {
4862 mon->suspend_cnt = 0;
4863 }
aliguori2724b182009-03-05 23:01:47 +00004864 break;
ths86e94de2007-01-05 22:01:59 +00004865
aliguori2724b182009-03-05 23:01:47 +00004866 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004867 if (mon->reset_seen) {
4868 if (mon->suspend_cnt == 0) {
4869 monitor_printf(mon, "\n");
4870 }
4871 monitor_flush(mon);
4872 monitor_suspend(mon);
4873 } else {
4874 mon->suspend_cnt++;
4875 }
4876 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004877 break;
4878
Amit Shahb6b8df52009-10-07 18:31:16 +05304879 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004880 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4881 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004882 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004883 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004884 }
4885 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004886 mon_refcount++;
4887 break;
4888
4889 case CHR_EVENT_CLOSED:
4890 mon_refcount--;
4891 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004892 break;
4893 }
ths86e94de2007-01-05 22:01:59 +00004894}
4895
Wayne Xia816f8922011-10-12 11:32:41 +08004896static int
4897compare_mon_cmd(const void *a, const void *b)
4898{
4899 return strcmp(((const mon_cmd_t *)a)->name,
4900 ((const mon_cmd_t *)b)->name);
4901}
4902
4903static void sortcmdlist(void)
4904{
4905 int array_num;
4906 int elem_size = sizeof(mon_cmd_t);
4907
4908 array_num = sizeof(mon_cmds)/elem_size-1;
4909 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4910
4911 array_num = sizeof(info_cmds)/elem_size-1;
4912 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4913}
4914
aliguori76655d62009-03-06 20:27:37 +00004915
4916/*
4917 * Local variables:
4918 * c-indent-level: 4
4919 * c-basic-offset: 4
4920 * tab-width: 8
4921 * End:
4922 */
4923
aliguori731b0362009-03-05 23:01:42 +00004924void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004925{
aliguori731b0362009-03-05 23:01:42 +00004926 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004927 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004928
4929 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004930 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004931 monitor_protocol_event_init();
ths20d8a3e2007-02-18 17:04:49 +00004932 is_first_init = 0;
4933 }
aliguori87127162009-03-05 23:01:29 +00004934
Anthony Liguori7267c092011-08-20 22:09:37 -05004935 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004936
aliguori87127162009-03-05 23:01:29 +00004937 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004938 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004939 if (flags & MONITOR_USE_READLINE) {
4940 mon->rs = readline_init(mon, monitor_find_completion);
4941 monitor_read_command(mon, 0);
4942 }
aliguori87127162009-03-05 23:01:29 +00004943
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004944 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004945 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004946 /* Control mode requires special handlers */
4947 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4948 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004949 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05004950
4951 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004952 } else {
4953 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4954 monitor_event, mon);
4955 }
aliguori87127162009-03-05 23:01:29 +00004956
Blue Swirl72cf2d42009-09-12 07:36:22 +00004957 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004958 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4959 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004960
4961 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004962}
4963
aliguori376253e2009-03-05 23:01:23 +00004964static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004965{
aliguoribb5fc202009-03-05 23:01:15 +00004966 BlockDriverState *bs = opaque;
4967 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004968
aliguoribb5fc202009-03-05 23:01:15 +00004969 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004970 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004971 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004972 }
aliguori731b0362009-03-05 23:01:42 +00004973 if (mon->password_completion_cb)
4974 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004975
aliguori731b0362009-03-05 23:01:42 +00004976 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004977}
aliguoric0f4ce72009-03-05 23:01:01 +00004978
Anthony Liguori7060b472011-09-02 12:34:50 -05004979ReadLineState *monitor_get_rs(Monitor *mon)
4980{
4981 return mon->rs;
4982}
4983
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004984int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4985 BlockDriverCompletionFunc *completion_cb,
4986 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004987{
aliguoricde76ee2009-03-05 23:01:51 +00004988 int err;
4989
aliguoribb5fc202009-03-05 23:01:15 +00004990 if (!bdrv_key_required(bs)) {
4991 if (completion_cb)
4992 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004993 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004994 }
aliguoric0f4ce72009-03-05 23:01:01 +00004995
Luiz Capitulino94171e12009-12-07 21:37:00 +01004996 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004997 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4998 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004999 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005000 }
5001
aliguori376253e2009-03-05 23:01:23 +00005002 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5003 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005004
aliguori731b0362009-03-05 23:01:42 +00005005 mon->password_completion_cb = completion_cb;
5006 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005007
aliguoricde76ee2009-03-05 23:01:51 +00005008 err = monitor_read_password(mon, bdrv_password_cb, bs);
5009
5010 if (err && completion_cb)
5011 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005012
5013 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005014}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005015
5016int monitor_read_block_device_key(Monitor *mon, const char *device,
5017 BlockDriverCompletionFunc *completion_cb,
5018 void *opaque)
5019{
5020 BlockDriverState *bs;
5021
5022 bs = bdrv_find(device);
5023 if (!bs) {
5024 monitor_printf(mon, "Device not found %s\n", device);
5025 return -1;
5026 }
5027
5028 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
5029}