blob: 3e1cd3394289848faa8b9dc6172fd567a7f2b05e [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"
ths6a5bd302007-12-03 17:05:38 +000069
Jan Kiszka661f1922011-10-16 11:53:13 +020070/* for pic/irq_info */
71#if defined(TARGET_SPARC)
72#include "hw/sun4m.h"
73#endif
74#include "hw/lm32_pic.h"
75
bellard9dc39cb2004-03-14 21:38:27 +000076//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000077//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000078
bellard9307c4c2004-04-04 12:57:25 +000079/*
80 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000081 *
bellard9307c4c2004-04-04 12:57:25 +000082 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000083 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000084 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010085 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000090 * 'i' 32 bit integer
91 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010092 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020094 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010099 * 'T' double
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000102 * '/' optional gdb-like print format (like "/10x")
103 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100106 * 'b' boolean
107 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300108 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000109 *
110 */
111
Adam Litke940cc302010-01-25 12:18:44 -0600112typedef struct MonitorCompletionData MonitorCompletionData;
113struct MonitorCompletionData {
114 Monitor *mon;
115 void (*user_print)(Monitor *mon, const QObject *data);
116};
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300123 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 union {
125 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300126 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500133 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200134 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500135} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000136
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500138typedef struct mon_fd_t mon_fd_t;
139struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140 char *name;
141 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143};
144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145typedef struct MonitorControl {
146 QObject *id;
147 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200148 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200149} MonitorControl;
150
aliguori87127162009-03-05 23:01:29 +0000151struct Monitor {
152 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200153 int mux_out;
154 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000155 int flags;
156 int suspend_cnt;
157 uint8_t outbuf[1024];
158 int outbuf_index;
159 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200160 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000161 CPUState *mon_cpu;
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200164#ifdef CONFIG_DEBUG_MONITOR
165 int print_calls_nr;
166#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200167 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500168 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000170};
171
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200172#ifdef CONFIG_DEBUG_MONITOR
173#define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200176
177static inline void mon_print_count_inc(Monitor *mon)
178{
179 mon->print_calls_nr++;
180}
181
182static inline void mon_print_count_init(Monitor *mon)
183{
184 mon->print_calls_nr = 0;
185}
186
187static inline int mon_print_count_get(const Monitor *mon)
188{
189 return mon->print_calls_nr;
190}
191
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200192#else /* !CONFIG_DEBUG_MONITOR */
193#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200194static inline void mon_print_count_inc(Monitor *mon) { }
195static inline void mon_print_count_init(Monitor *mon) { }
196static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200197#endif /* CONFIG_DEBUG_MONITOR */
198
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300199/* QMP checker flags */
200#define QMP_ACCEPT_UNKNOWNS 1
201
Blue Swirl72cf2d42009-09-12 07:36:22 +0000202static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000203
Anthony Liguoric227f092009-10-01 16:12:16 -0500204static const mon_cmd_t mon_cmds[];
205static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000206
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300207static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300208static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300209
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
aliguori731b0362009-03-05 23:01:42 +0000233static void monitor_read_command(Monitor *mon, int show_prompt)
234{
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
aliguoricde76ee2009-03-05 23:01:51 +0000243static int 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 Capitulino10e4f602010-02-10 23:50:06 -0200292 mon_print_count_inc(mon);
293
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200295 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200296 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200297
298 vsnprintf(buf, sizeof(buf), fmt, ap);
299 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000303{
304 va_list ap;
305 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000306 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000307 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000308}
309
aliguori376253e2009-03-05 23:01:23 +0000310void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000311{
312 int i;
313
314 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000315 switch (filename[i]) {
316 case ' ':
317 case '"':
318 case '\\':
319 monitor_printf(mon, "\\%c", filename[i]);
320 break;
321 case '\t':
322 monitor_printf(mon, "\\t");
323 break;
324 case '\r':
325 monitor_printf(mon, "\\r");
326 break;
327 case '\n':
328 monitor_printf(mon, "\\n");
329 break;
330 default:
331 monitor_printf(mon, "%c", filename[i]);
332 break;
333 }
thsfef30742006-12-22 14:11:32 +0000334 }
335}
336
Stefan Weil8b7968f2010-09-23 21:28:05 +0200337static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
338 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000339{
340 va_list ap;
341 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000342 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000343 va_end(ap);
344 return 0;
345}
346
Luiz Capitulino13c74252009-10-07 13:41:55 -0300347static void monitor_user_noop(Monitor *mon, const QObject *data) { }
348
Luiz Capitulino9e807212010-09-16 10:58:59 -0300349static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300350{
351 return cmd->user_print != NULL;
352}
353
Luiz Capitulino4903de02010-09-16 11:01:32 -0300354static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600355{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200356 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600357}
358
Luiz Capitulino8204a912009-11-18 23:05:31 -0200359static inline int monitor_has_error(const Monitor *mon)
360{
361 return mon->error != NULL;
362}
363
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200364static void monitor_json_emitter(Monitor *mon, const QObject *data)
365{
366 QString *json;
367
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200368 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
369 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200370 assert(json != NULL);
371
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200372 qstring_append_chr(json, '\n');
373 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200374
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200375 QDECREF(json);
376}
377
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378static void monitor_protocol_emitter(Monitor *mon, QObject *data)
379{
380 QDict *qmp;
381
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100382 trace_monitor_protocol_emitter(mon);
383
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 qmp = qdict_new();
385
386 if (!monitor_has_error(mon)) {
387 /* success response */
388 if (data) {
389 qobject_incref(data);
390 qdict_put_obj(qmp, "return", data);
391 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200392 /* return an empty QDict by default */
393 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200394 }
395 } else {
396 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100397 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200398 qdict_put(qmp, "error", mon->error->error);
399 QINCREF(mon->error->error);
400 QDECREF(mon->error);
401 mon->error = NULL;
402 }
403
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200404 if (mon->mc->id) {
405 qdict_put_obj(qmp, "id", mon->mc->id);
406 mon->mc->id = NULL;
407 }
408
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200409 monitor_json_emitter(mon, QOBJECT(qmp));
410 QDECREF(qmp);
411}
412
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200413static void timestamp_put(QDict *qdict)
414{
415 int err;
416 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000417 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418
Blue Swirld08d6f02009-12-04 18:06:39 +0000419 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200420 if (err < 0)
421 return;
422
423 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
424 "'microseconds': %" PRId64 " }",
425 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 qdict_put_obj(qdict, "timestamp", obj);
427}
428
429/**
430 * monitor_protocol_event(): Generate a Monitor event
431 *
432 * Event-specific data can be emitted through the (optional) 'data' parameter.
433 */
434void monitor_protocol_event(MonitorEvent event, QObject *data)
435{
436 QDict *qmp;
437 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600438 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439
Blue Swirl242cd002009-12-04 18:05:45 +0000440 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200442 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000443 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200444 event_name = "SHUTDOWN";
445 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000446 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200447 event_name = "RESET";
448 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000449 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200450 event_name = "POWERDOWN";
451 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000452 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200453 event_name = "STOP";
454 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300455 case QEVENT_RESUME:
456 event_name = "RESUME";
457 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200458 case QEVENT_VNC_CONNECTED:
459 event_name = "VNC_CONNECTED";
460 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200461 case QEVENT_VNC_INITIALIZED:
462 event_name = "VNC_INITIALIZED";
463 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200464 case QEVENT_VNC_DISCONNECTED:
465 event_name = "VNC_DISCONNECTED";
466 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200467 case QEVENT_BLOCK_IO_ERROR:
468 event_name = "BLOCK_IO_ERROR";
469 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300470 case QEVENT_RTC_CHANGE:
471 event_name = "RTC_CHANGE";
472 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300473 case QEVENT_WATCHDOG:
474 event_name = "WATCHDOG";
475 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200476 case QEVENT_SPICE_CONNECTED:
477 event_name = "SPICE_CONNECTED";
478 break;
479 case QEVENT_SPICE_INITIALIZED:
480 event_name = "SPICE_INITIALIZED";
481 break;
482 case QEVENT_SPICE_DISCONNECTED:
483 event_name = "SPICE_DISCONNECTED";
484 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200485 default:
486 abort();
487 break;
488 }
489
490 qmp = qdict_new();
491 timestamp_put(qmp);
492 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200493 if (data) {
494 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200495 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200496 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200497
Adam Litkef039a562010-01-15 08:34:02 -0600498 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200499 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200500 monitor_json_emitter(mon, QOBJECT(qmp));
501 }
Adam Litkef039a562010-01-15 08:34:02 -0600502 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200503 QDECREF(qmp);
504}
505
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200506static int do_qmp_capabilities(Monitor *mon, const QDict *params,
507 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200508{
509 /* Will setup QMP capabilities in the future */
510 if (monitor_ctrl_mode(mon)) {
511 mon->mc->command_mode = 1;
512 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200513
514 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200515}
516
Luiz Capitulino0268d972010-10-22 10:08:02 -0200517static void handle_user_command(Monitor *mon, const char *cmdline);
518
519static int do_hmp_passthrough(Monitor *mon, const QDict *params,
520 QObject **ret_data)
521{
522 int ret = 0;
523 Monitor *old_mon, hmp;
524 CharDriverState mchar;
525
526 memset(&hmp, 0, sizeof(hmp));
527 qemu_chr_init_mem(&mchar);
528 hmp.chr = &mchar;
529
530 old_mon = cur_mon;
531 cur_mon = &hmp;
532
533 if (qdict_haskey(params, "cpu-index")) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300534 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
Luiz Capitulino0268d972010-10-22 10:08:02 -0200535 if (ret < 0) {
536 cur_mon = old_mon;
537 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
538 goto out;
539 }
540 }
541
542 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
543 cur_mon = old_mon;
544
545 if (qemu_chr_mem_osize(hmp.chr) > 0) {
546 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
547 }
548
549out:
550 qemu_chr_close_mem(hmp.chr);
551 return ret;
552}
553
bellard9dc39cb2004-03-14 21:38:27 +0000554static int compare_cmd(const char *name, const char *list)
555{
556 const char *p, *pstart;
557 int len;
558 len = strlen(name);
559 p = list;
560 for(;;) {
561 pstart = p;
562 p = strchr(p, '|');
563 if (!p)
564 p = pstart + strlen(pstart);
565 if ((p - pstart) == len && !memcmp(pstart, name, len))
566 return 1;
567 if (*p == '\0')
568 break;
569 p++;
570 }
571 return 0;
572}
573
Anthony Liguoric227f092009-10-01 16:12:16 -0500574static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000575 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000576{
Anthony Liguoric227f092009-10-01 16:12:16 -0500577 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000578
579 for(cmd = cmds; cmd->name != NULL; cmd++) {
580 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
582 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000583 }
584}
585
aliguori376253e2009-03-05 23:01:23 +0000586static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000587{
588 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000589 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000590 } else {
aliguori376253e2009-03-05 23:01:23 +0000591 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000592 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000593 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "Log items (comma separated):\n");
595 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000596 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000597 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000598 }
599 }
bellard9dc39cb2004-03-14 21:38:27 +0000600 }
601}
602
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300603static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300604{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300605 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300606}
607
Lluísfc764102011-08-31 20:31:18 +0200608static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530609{
610 const char *tp_name = qdict_get_str(qdict, "name");
611 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200612 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000613
614 if (!ret) {
615 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
616 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530617}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100618
Michael Rothc45a8162011-10-02 08:44:37 -0500619#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100620static void do_trace_file(Monitor *mon, const QDict *qdict)
621{
622 const char *op = qdict_get_try_str(qdict, "op");
623 const char *arg = qdict_get_try_str(qdict, "arg");
624
625 if (!op) {
626 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
627 } else if (!strcmp(op, "on")) {
628 st_set_trace_file_enabled(true);
629 } else if (!strcmp(op, "off")) {
630 st_set_trace_file_enabled(false);
631 } else if (!strcmp(op, "flush")) {
632 st_flush_trace_buffer();
633 } else if (!strcmp(op, "set")) {
634 if (arg) {
635 st_set_trace_file(arg);
636 }
637 } else {
638 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
639 help_cmd(mon, "trace-file");
640 }
641}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530642#endif
643
Adam Litke940cc302010-01-25 12:18:44 -0600644static void user_monitor_complete(void *opaque, QObject *ret_data)
645{
646 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
647
648 if (ret_data) {
649 data->user_print(data->mon, ret_data);
650 }
651 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500652 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600653}
654
655static void qmp_monitor_complete(void *opaque, QObject *ret_data)
656{
657 monitor_protocol_emitter(opaque, ret_data);
658}
659
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300660static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
661 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600662{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300663 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600664}
665
666static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
667{
668 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
669}
670
671static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
672 const QDict *params)
673{
674 int ret;
675
Anthony Liguori7267c092011-08-20 22:09:37 -0500676 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600677 cb_data->mon = mon;
678 cb_data->user_print = cmd->user_print;
679 monitor_suspend(mon);
680 ret = cmd->mhandler.cmd_async(mon, params,
681 user_monitor_complete, cb_data);
682 if (ret < 0) {
683 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500684 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600685 }
686}
687
688static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
689{
690 int ret;
691
Anthony Liguori7267c092011-08-20 22:09:37 -0500692 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600693 cb_data->mon = mon;
694 cb_data->user_print = cmd->user_print;
695 monitor_suspend(mon);
696 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
697 if (ret < 0) {
698 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500699 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600700 }
701}
702
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300703static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000704{
Anthony Liguoric227f092009-10-01 16:12:16 -0500705 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300706 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000707
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200708 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000709 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200710 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711
712 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000713 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300714 break;
bellard9dc39cb2004-03-14 21:38:27 +0000715 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300716
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200717 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300718 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200719 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300720
Luiz Capitulino4903de02010-09-16 11:01:32 -0300721 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300722 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300723 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300724 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200725
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300726 cmd->mhandler.info_new(mon, &info_data);
727 if (info_data) {
728 cmd->user_print(mon, info_data);
729 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200730 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300731 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300732 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300733 }
734
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300735 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300736
737help:
738 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000739}
740
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300741static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200742{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300743 CommandInfoList *info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200744
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300745 info = g_malloc0(sizeof(*info));
746 info->value = g_malloc0(sizeof(*info->value));
747 info->value->name = g_strdup(cmd_name);
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200748
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300749 return info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200750}
751
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300752CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000753{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300754 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200755 const mon_cmd_t *cmd;
756
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300757 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300758 info = alloc_cmd_entry(cmd->name);
759 info->next = cmd_list;
760 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200761 }
762
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300763 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300764 char buf[128];
765 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300766 info = alloc_cmd_entry(buf);
767 info->next = cmd_list;
768 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200769 }
770
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300771 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000772}
773
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300774/* set the current CPU defined by the user */
775int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000776{
777 CPUState *env;
778
779 for(env = first_cpu; env != NULL; env = env->next_cpu) {
780 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000781 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000782 return 0;
783 }
784 }
785 return -1;
786}
787
pbrook9596ebb2007-11-18 01:44:38 +0000788static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000789{
aliguori731b0362009-03-05 23:01:42 +0000790 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300791 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000792 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300793 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000794 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000795}
796
Luiz Capitulino99b77962011-10-24 10:53:44 -0200797int monitor_get_cpu_index(void)
798{
799 return mon_get_cpu()->cpu_index;
800}
801
aliguori376253e2009-03-05 23:01:23 +0000802static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000803{
bellard6a00d602005-11-21 23:25:50 +0000804 CPUState *env;
805 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000806#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000807 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000808 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000809#else
aliguori376253e2009-03-05 23:01:23 +0000810 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000811 0);
bellard9307c4c2004-04-04 12:57:25 +0000812#endif
813}
814
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300815static void print_cpu_iter(QObject *obj, void *opaque)
816{
817 QDict *cpu;
818 int active = ' ';
819 Monitor *mon = opaque;
820
821 assert(qobject_type(obj) == QTYPE_QDICT);
822 cpu = qobject_to_qdict(obj);
823
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200824 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300825 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200826 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300827
828 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
829
830#if defined(TARGET_I386)
831 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
832 (target_ulong) qdict_get_int(cpu, "pc"));
833#elif defined(TARGET_PPC)
834 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
835 (target_long) qdict_get_int(cpu, "nip"));
836#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500837 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300838 (target_long) qdict_get_int(cpu, "pc"));
839 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
840 (target_long) qdict_get_int(cpu, "npc"));
841#elif defined(TARGET_MIPS)
842 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
843 (target_long) qdict_get_int(cpu, "PC"));
844#endif
845
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200846 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300847 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200848 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300849
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100850 monitor_printf(mon, " thread_id=%" PRId64 " ",
851 qdict_get_int(cpu, "thread_id"));
852
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300853 monitor_printf(mon, "\n");
854}
855
856static void monitor_print_cpus(Monitor *mon, const QObject *data)
857{
858 QList *cpu_list;
859
860 assert(qobject_type(data) == QTYPE_QLIST);
861 cpu_list = qobject_to_qlist(data);
862 qlist_iter(cpu_list, print_cpu_iter, mon);
863}
864
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300865static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000866{
867 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300868 QList *cpu_list;
869
870 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000871
872 /* just to set the default cpu if not already done */
873 mon_get_cpu();
874
875 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200876 QDict *cpu;
877 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878
Avi Kivity4c0960c2009-08-17 23:19:53 +0300879 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200881 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
882 env->cpu_index, env == mon->mon_cpu,
883 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200884
885 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300886
bellard6a00d602005-11-21 23:25:50 +0000887#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000889#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000891#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300892 qdict_put(cpu, "pc", qint_from_int(env->pc));
893 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000894#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300895 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000896#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100897 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300898
899 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000900 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300901
902 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000903}
904
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200905static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000906{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300907 int index = qdict_get_int(qdict, "index");
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300908 if (monitor_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100909 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
910 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200911 return -1;
912 }
913 return 0;
bellard6a00d602005-11-21 23:25:50 +0000914}
915
aliguori376253e2009-03-05 23:01:23 +0000916static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000917{
aliguori376253e2009-03-05 23:01:23 +0000918 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000919}
920
aliguori376253e2009-03-05 23:01:23 +0000921static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000922{
923 int i;
bellard7e2515e2004-08-01 21:52:19 +0000924 const char *str;
ths3b46e622007-09-17 08:09:54 +0000925
aliguoricde76ee2009-03-05 23:01:51 +0000926 if (!mon->rs)
927 return;
bellard7e2515e2004-08-01 21:52:19 +0000928 i = 0;
929 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000930 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000931 if (!str)
932 break;
aliguori376253e2009-03-05 23:01:23 +0000933 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000934 i++;
bellardaa455482004-04-04 13:07:25 +0000935 }
936}
937
j_mayer76a66252007-03-07 08:32:30 +0000938#if defined(TARGET_PPC)
939/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000940static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000941{
942 CPUState *env;
943
944 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000945 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000946}
947#endif
948
Lluís6d8a7642011-08-31 20:30:43 +0200949#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530950static void do_info_trace(Monitor *mon)
951{
952 st_print_trace((FILE *)mon, &monitor_fprintf);
953}
Lluís31965ae2011-08-31 20:31:24 +0200954#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530955
Lluísfc764102011-08-31 20:31:18 +0200956static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530957{
Lluísfc764102011-08-31 20:31:18 +0200958 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530959}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530960
Jes Sorensen821601e2011-03-16 13:33:36 +0100961#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200962static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000963{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600964 if (!password || !password[0]) {
965 if (vnc_display_disable_login(NULL)) {
966 qerror_report(QERR_SET_PASSWD_FAILED);
967 return -1;
968 }
969 return 0;
970 }
971
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200972 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100973 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200974 return -1;
975 }
aliguoribb5fc202009-03-05 23:01:15 +0000976
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200977 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100978}
979
980static void change_vnc_password_cb(Monitor *mon, const char *password,
981 void *opaque)
982{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100983 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000984 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000985}
986
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200987static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000988{
ths70848512007-08-25 01:37:05 +0000989 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000990 strcmp(target, "password") == 0) {
991 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000992 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000993 strncpy(password, arg, sizeof(password));
994 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200995 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000996 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200997 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000998 }
ths70848512007-08-25 01:37:05 +0000999 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001000 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001001 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001002 return -1;
1003 }
ths70848512007-08-25 01:37:05 +00001004 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001005
1006 return 0;
thse25a5822007-08-25 01:36:20 +00001007}
Jes Sorensen821601e2011-03-16 13:33:36 +01001008#else
1009static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1010{
1011 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1012 return -ENODEV;
1013}
1014#endif
thse25a5822007-08-25 01:36:20 +00001015
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001016/**
1017 * do_change(): Change a removable medium, or VNC configuration
1018 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001019static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001020{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001021 const char *device = qdict_get_str(qdict, "device");
1022 const char *target = qdict_get_str(qdict, "target");
1023 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001024 int ret;
1025
thse25a5822007-08-25 01:36:20 +00001026 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001028 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001029 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001030 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001031
1032 return ret;
thse25a5822007-08-25 01:36:20 +00001033}
1034
Gerd Hoffmann75721502010-10-07 12:22:54 +02001035static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1036{
1037 const char *protocol = qdict_get_str(qdict, "protocol");
1038 const char *password = qdict_get_str(qdict, "password");
1039 const char *connected = qdict_get_try_str(qdict, "connected");
1040 int disconnect_if_connected = 0;
1041 int fail_if_connected = 0;
1042 int rc;
1043
1044 if (connected) {
1045 if (strcmp(connected, "fail") == 0) {
1046 fail_if_connected = 1;
1047 } else if (strcmp(connected, "disconnect") == 0) {
1048 disconnect_if_connected = 1;
1049 } else if (strcmp(connected, "keep") == 0) {
1050 /* nothing */
1051 } else {
1052 qerror_report(QERR_INVALID_PARAMETER, "connected");
1053 return -1;
1054 }
1055 }
1056
1057 if (strcmp(protocol, "spice") == 0) {
1058 if (!using_spice) {
1059 /* correct one? spice isn't a device ,,, */
1060 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1061 return -1;
1062 }
1063 rc = qemu_spice_set_passwd(password, fail_if_connected,
1064 disconnect_if_connected);
1065 if (rc != 0) {
1066 qerror_report(QERR_SET_PASSWD_FAILED);
1067 return -1;
1068 }
1069 return 0;
1070 }
1071
1072 if (strcmp(protocol, "vnc") == 0) {
1073 if (fail_if_connected || disconnect_if_connected) {
1074 /* vnc supports "connected=keep" only */
1075 qerror_report(QERR_INVALID_PARAMETER, "connected");
1076 return -1;
1077 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001078 /* Note that setting an empty password will not disable login through
1079 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001080 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001081 }
1082
1083 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1084 return -1;
1085}
1086
1087static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1088{
1089 const char *protocol = qdict_get_str(qdict, "protocol");
1090 const char *whenstr = qdict_get_str(qdict, "time");
1091 time_t when;
1092 int rc;
1093
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001094 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001095 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001096 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001097 when = TIME_MAX;
1098 } else if (whenstr[0] == '+') {
1099 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1100 } else {
1101 when = strtoull(whenstr, NULL, 10);
1102 }
1103
1104 if (strcmp(protocol, "spice") == 0) {
1105 if (!using_spice) {
1106 /* correct one? spice isn't a device ,,, */
1107 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1108 return -1;
1109 }
1110 rc = qemu_spice_set_pw_expire(when);
1111 if (rc != 0) {
1112 qerror_report(QERR_SET_PASSWD_FAILED);
1113 return -1;
1114 }
1115 return 0;
1116 }
1117
1118 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001119 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001120 }
1121
1122 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1123 return -1;
1124}
1125
Daniel P. Berrange13661082011-06-23 13:31:42 +01001126static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1127{
1128 const char *protocol = qdict_get_str(qdict, "protocol");
1129 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001130 CharDriverState *s;
1131
1132 if (strcmp(protocol, "spice") == 0) {
1133 if (!using_spice) {
1134 /* correct one? spice isn't a device ,,, */
1135 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1136 return -1;
1137 }
1138 qerror_report(QERR_ADD_CLIENT_FAILED);
1139 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001140#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001141 } else if (strcmp(protocol, "vnc") == 0) {
1142 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001143 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001144 vnc_display_add_client(NULL, fd, skipauth);
1145 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001146#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001147 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1148 int fd = monitor_get_fd(mon, fdname);
1149 if (qemu_chr_add_client(s, fd) < 0) {
1150 qerror_report(QERR_ADD_CLIENT_FAILED);
1151 return -1;
1152 }
1153 return 0;
1154 }
1155
1156 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1157 return -1;
1158}
1159
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001160static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1161{
1162 const char *protocol = qdict_get_str(qdict, "protocol");
1163 const char *hostname = qdict_get_str(qdict, "hostname");
1164 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1165 int port = qdict_get_try_int(qdict, "port", -1);
1166 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1167 int ret;
1168
1169 if (strcmp(protocol, "spice") == 0) {
1170 if (!using_spice) {
1171 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1172 return -1;
1173 }
1174
1175 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1176 if (ret != 0) {
1177 qerror_report(QERR_UNDEFINED_ERROR);
1178 return -1;
1179 }
1180 return 0;
1181 }
1182
1183 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1184 return -1;
1185}
1186
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001187static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001188{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001189 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001190 return 0;
bellard59a983b2004-03-17 23:17:16 +00001191}
1192
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001194{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001195 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001196}
1197
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001198static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001199{
1200 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001202
bellard9307c4c2004-04-04 12:57:25 +00001203 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001204 mask = 0;
1205 } else {
bellard9307c4c2004-04-04 12:57:25 +00001206 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001207 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001208 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001209 return;
1210 }
1211 }
1212 cpu_set_log(mask);
1213}
1214
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001215static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001216{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001217 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001218 if (!option || !strcmp(option, "on")) {
1219 singlestep = 1;
1220 } else if (!strcmp(option, "off")) {
1221 singlestep = 0;
1222 } else {
1223 monitor_printf(mon, "unexpected option %s\n", option);
1224 }
1225}
1226
aliguoribb5fc202009-03-05 23:01:15 +00001227static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001228
aliguori376253e2009-03-05 23:01:23 +00001229struct bdrv_iterate_context {
1230 Monitor *mon;
1231 int err;
1232};
aliguoric0f4ce72009-03-05 23:01:01 +00001233
Luiz Capitulino28a72822011-09-26 17:43:50 -03001234static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1235{
1236 bdrv_iostatus_reset(bs);
1237}
1238
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001239/**
1240 * do_cont(): Resume emulation.
1241 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001242static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001243{
1244 struct bdrv_iterate_context context = { mon, 0 };
1245
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001246 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301247 qerror_report(QERR_MIGRATION_EXPECTED);
1248 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001249 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1250 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001251 qerror_report(QERR_RESET_REQUIRED);
1252 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301253 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001254
Luiz Capitulino28a72822011-09-26 17:43:50 -03001255 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001256 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001257 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001258 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001259 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001260 return 0;
1261 } else {
1262 return -1;
1263 }
bellard8a7ddc32004-03-31 19:00:16 +00001264}
1265
aliguoribb5fc202009-03-05 23:01:15 +00001266static void bdrv_key_cb(void *opaque, int err)
1267{
aliguori376253e2009-03-05 23:01:23 +00001268 Monitor *mon = opaque;
1269
aliguoribb5fc202009-03-05 23:01:15 +00001270 /* another key was set successfully, retry to continue */
1271 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001272 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001273}
1274
1275static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1276{
aliguori376253e2009-03-05 23:01:23 +00001277 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001278
aliguori376253e2009-03-05 23:01:23 +00001279 if (!context->err && bdrv_key_required(bs)) {
1280 context->err = -EBUSY;
1281 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1282 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001283 }
1284}
1285
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001286static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001287{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001288 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001289 if (!device)
1290 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1291 if (gdbserver_start(device) < 0) {
1292 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1293 device);
1294 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001295 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001296 } else {
aliguori59030a82009-04-05 18:43:41 +00001297 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1298 device);
bellard8a7ddc32004-03-31 19:00:16 +00001299 }
1300}
1301
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001302static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001303{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001304 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001305 if (select_watchdog_action(action) == -1) {
1306 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1307 }
1308}
1309
aliguori376253e2009-03-05 23:01:23 +00001310static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001311{
aliguori376253e2009-03-05 23:01:23 +00001312 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001313 switch(c) {
1314 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001315 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001316 break;
1317 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001318 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001319 break;
1320 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001321 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001322 break;
1323 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001325 break;
1326 default:
1327 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001329 } else {
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001331 }
1332 break;
1333 }
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001335}
1336
aliguori376253e2009-03-05 23:01:23 +00001337static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001338 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001339{
bellard6a00d602005-11-21 23:25:50 +00001340 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001341 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001342 uint8_t buf[16];
1343 uint64_t v;
1344
1345 if (format == 'i') {
1346 int flags;
1347 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001348 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001349#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001350 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001351 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001352 } else if (wsize == 4) {
1353 flags = 0;
1354 } else {
bellard6a15fd12006-04-12 21:07:07 +00001355 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001356 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001357 if (env) {
1358#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001359 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001360 (env->segs[R_CS].flags & DESC_L_MASK))
1361 flags = 2;
1362 else
1363#endif
1364 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1365 flags = 1;
1366 }
bellard4c27ba22004-04-25 18:05:08 +00001367 }
1368#endif
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001370 return;
1371 }
1372
1373 len = wsize * count;
1374 if (wsize == 1)
1375 line_size = 8;
1376 else
1377 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001378 max_digits = 0;
1379
1380 switch(format) {
1381 case 'o':
1382 max_digits = (wsize * 8 + 2) / 3;
1383 break;
1384 default:
1385 case 'x':
1386 max_digits = (wsize * 8) / 4;
1387 break;
1388 case 'u':
1389 case 'd':
1390 max_digits = (wsize * 8 * 10 + 32) / 33;
1391 break;
1392 case 'c':
1393 wsize = 1;
1394 break;
1395 }
1396
1397 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001398 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001400 else
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001402 l = len;
1403 if (l > line_size)
1404 l = line_size;
1405 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001406 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001407 } else {
bellard6a00d602005-11-21 23:25:50 +00001408 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001409 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001411 break;
1412 }
bellard9307c4c2004-04-04 12:57:25 +00001413 }
ths5fafdf22007-09-16 21:08:06 +00001414 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001415 while (i < l) {
1416 switch(wsize) {
1417 default:
1418 case 1:
1419 v = ldub_raw(buf + i);
1420 break;
1421 case 2:
1422 v = lduw_raw(buf + i);
1423 break;
1424 case 4:
bellard92a31b12005-02-10 22:00:52 +00001425 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001426 break;
1427 case 8:
1428 v = ldq_raw(buf + i);
1429 break;
1430 }
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001432 switch(format) {
1433 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001434 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001435 break;
1436 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001437 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001438 break;
1439 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001440 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001441 break;
1442 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001444 break;
1445 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001446 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001447 break;
1448 }
1449 i += wsize;
1450 }
aliguori376253e2009-03-05 23:01:23 +00001451 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001452 addr += l;
1453 len -= l;
1454 }
1455}
1456
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001457static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001458{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001459 int count = qdict_get_int(qdict, "count");
1460 int format = qdict_get_int(qdict, "format");
1461 int size = qdict_get_int(qdict, "size");
1462 target_long addr = qdict_get_int(qdict, "addr");
1463
aliguori376253e2009-03-05 23:01:23 +00001464 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001465}
1466
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001467static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001468{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001469 int count = qdict_get_int(qdict, "count");
1470 int format = qdict_get_int(qdict, "format");
1471 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001472 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001473
aliguori376253e2009-03-05 23:01:23 +00001474 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001475}
1476
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001477static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001478{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001479 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001480 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001481
blueswir17743e582007-09-24 18:39:04 +00001482#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001483 switch(format) {
1484 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001485 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001486 break;
1487 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001488 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001489 break;
1490 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001491 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001492 break;
1493 default:
1494 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001495 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001496 break;
1497 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001498 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001499 break;
1500 }
bellard92a31b12005-02-10 22:00:52 +00001501#else
1502 switch(format) {
1503 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001504 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001505 break;
1506 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001507 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001508 break;
1509 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001511 break;
1512 default:
1513 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001514 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001515 break;
1516 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001517 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001518 break;
1519 }
1520#endif
aliguori376253e2009-03-05 23:01:23 +00001521 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001522}
1523
Luiz Capitulino98696222010-02-10 23:49:58 -02001524static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001525{
1526 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001527 uint32_t size = qdict_get_int(qdict, "size");
1528 const char *filename = qdict_get_str(qdict, "filename");
1529 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001530 uint32_t l;
1531 CPUState *env;
1532 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001533 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001534
1535 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001536
1537 f = fopen(filename, "wb");
1538 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001539 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001540 return -1;
bellardb371dc52007-01-03 15:20:39 +00001541 }
1542 while (size != 0) {
1543 l = sizeof(buf);
1544 if (l > size)
1545 l = size;
1546 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001547 if (fwrite(buf, 1, l, f) != l) {
1548 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1549 goto exit;
1550 }
bellardb371dc52007-01-03 15:20:39 +00001551 addr += l;
1552 size -= l;
1553 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001554
1555 ret = 0;
1556
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001557exit:
bellardb371dc52007-01-03 15:20:39 +00001558 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001559 return ret;
bellardb371dc52007-01-03 15:20:39 +00001560}
1561
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001562static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001563 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001564{
1565 FILE *f;
1566 uint32_t l;
1567 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001568 uint32_t size = qdict_get_int(qdict, "size");
1569 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001570 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001571 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001572
1573 f = fopen(filename, "wb");
1574 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001575 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001576 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001577 }
1578 while (size != 0) {
1579 l = sizeof(buf);
1580 if (l > size)
1581 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001582 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001583 if (fwrite(buf, 1, l, f) != l) {
1584 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1585 goto exit;
1586 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001587 fflush(f);
1588 addr += l;
1589 size -= l;
1590 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001591
1592 ret = 0;
1593
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001594exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001595 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001596 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001597}
1598
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001599static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001600{
1601 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001602 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001603 uint32_t start = qdict_get_int(qdict, "start");
1604 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001605
1606 sum = 0;
1607 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001608 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001609 /* BSD sum algorithm ('sum' Unix command) */
1610 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001611 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001612 }
aliguori376253e2009-03-05 23:01:23 +00001613 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001614}
1615
bellarda3a91a32004-06-04 11:06:21 +00001616typedef struct {
1617 int keycode;
1618 const char *name;
1619} KeyDef;
1620
1621static const KeyDef key_defs[] = {
1622 { 0x2a, "shift" },
1623 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001624
bellarda3a91a32004-06-04 11:06:21 +00001625 { 0x38, "alt" },
1626 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001627 { 0x64, "altgr" },
1628 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001629 { 0x1d, "ctrl" },
1630 { 0x9d, "ctrl_r" },
1631
1632 { 0xdd, "menu" },
1633
1634 { 0x01, "esc" },
1635
1636 { 0x02, "1" },
1637 { 0x03, "2" },
1638 { 0x04, "3" },
1639 { 0x05, "4" },
1640 { 0x06, "5" },
1641 { 0x07, "6" },
1642 { 0x08, "7" },
1643 { 0x09, "8" },
1644 { 0x0a, "9" },
1645 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001646 { 0x0c, "minus" },
1647 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001648 { 0x0e, "backspace" },
1649
1650 { 0x0f, "tab" },
1651 { 0x10, "q" },
1652 { 0x11, "w" },
1653 { 0x12, "e" },
1654 { 0x13, "r" },
1655 { 0x14, "t" },
1656 { 0x15, "y" },
1657 { 0x16, "u" },
1658 { 0x17, "i" },
1659 { 0x18, "o" },
1660 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001661 { 0x1a, "bracket_left" },
1662 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001663 { 0x1c, "ret" },
1664
1665 { 0x1e, "a" },
1666 { 0x1f, "s" },
1667 { 0x20, "d" },
1668 { 0x21, "f" },
1669 { 0x22, "g" },
1670 { 0x23, "h" },
1671 { 0x24, "j" },
1672 { 0x25, "k" },
1673 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001674 { 0x27, "semicolon" },
1675 { 0x28, "apostrophe" },
1676 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001677
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001678 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001679 { 0x2c, "z" },
1680 { 0x2d, "x" },
1681 { 0x2e, "c" },
1682 { 0x2f, "v" },
1683 { 0x30, "b" },
1684 { 0x31, "n" },
1685 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001686 { 0x33, "comma" },
1687 { 0x34, "dot" },
1688 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001689
balrog4d3b6f62008-02-10 16:33:14 +00001690 { 0x37, "asterisk" },
1691
bellarda3a91a32004-06-04 11:06:21 +00001692 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001693 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001694 { 0x3b, "f1" },
1695 { 0x3c, "f2" },
1696 { 0x3d, "f3" },
1697 { 0x3e, "f4" },
1698 { 0x3f, "f5" },
1699 { 0x40, "f6" },
1700 { 0x41, "f7" },
1701 { 0x42, "f8" },
1702 { 0x43, "f9" },
1703 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001704 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001705 { 0x46, "scroll_lock" },
1706
bellard64866c32006-05-07 18:03:31 +00001707 { 0xb5, "kp_divide" },
1708 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001709 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001710 { 0x4e, "kp_add" },
1711 { 0x9c, "kp_enter" },
1712 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001713 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001714
1715 { 0x52, "kp_0" },
1716 { 0x4f, "kp_1" },
1717 { 0x50, "kp_2" },
1718 { 0x51, "kp_3" },
1719 { 0x4b, "kp_4" },
1720 { 0x4c, "kp_5" },
1721 { 0x4d, "kp_6" },
1722 { 0x47, "kp_7" },
1723 { 0x48, "kp_8" },
1724 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001725
bellarda3a91a32004-06-04 11:06:21 +00001726 { 0x56, "<" },
1727
1728 { 0x57, "f11" },
1729 { 0x58, "f12" },
1730
1731 { 0xb7, "print" },
1732
1733 { 0xc7, "home" },
1734 { 0xc9, "pgup" },
1735 { 0xd1, "pgdn" },
1736 { 0xcf, "end" },
1737
1738 { 0xcb, "left" },
1739 { 0xc8, "up" },
1740 { 0xd0, "down" },
1741 { 0xcd, "right" },
1742
1743 { 0xd2, "insert" },
1744 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001745#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1746 { 0xf0, "stop" },
1747 { 0xf1, "again" },
1748 { 0xf2, "props" },
1749 { 0xf3, "undo" },
1750 { 0xf4, "front" },
1751 { 0xf5, "copy" },
1752 { 0xf6, "open" },
1753 { 0xf7, "paste" },
1754 { 0xf8, "find" },
1755 { 0xf9, "cut" },
1756 { 0xfa, "lf" },
1757 { 0xfb, "help" },
1758 { 0xfc, "meta_l" },
1759 { 0xfd, "meta_r" },
1760 { 0xfe, "compose" },
1761#endif
bellarda3a91a32004-06-04 11:06:21 +00001762 { 0, NULL },
1763};
1764
1765static int get_keycode(const char *key)
1766{
1767 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001768 char *endp;
1769 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001770
1771 for(p = key_defs; p->name != NULL; p++) {
1772 if (!strcmp(key, p->name))
1773 return p->keycode;
1774 }
bellard64866c32006-05-07 18:03:31 +00001775 if (strstart(key, "0x", NULL)) {
1776 ret = strtoul(key, &endp, 0);
1777 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1778 return ret;
1779 }
bellarda3a91a32004-06-04 11:06:21 +00001780 return -1;
1781}
1782
balrogc8256f92008-06-08 22:45:01 +00001783#define MAX_KEYCODES 16
1784static uint8_t keycodes[MAX_KEYCODES];
1785static int nb_pending_keycodes;
1786static QEMUTimer *key_timer;
1787
1788static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001789{
balrogc8256f92008-06-08 22:45:01 +00001790 int keycode;
1791
1792 while (nb_pending_keycodes > 0) {
1793 nb_pending_keycodes--;
1794 keycode = keycodes[nb_pending_keycodes];
1795 if (keycode & 0x80)
1796 kbd_put_keycode(0xe0);
1797 kbd_put_keycode(keycode | 0x80);
1798 }
1799}
1800
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001801static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001802{
balrog3401c0d2008-06-04 10:05:59 +00001803 char keyname_buf[16];
1804 char *separator;
1805 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001806 const char *string = qdict_get_str(qdict, "string");
1807 int has_hold_time = qdict_haskey(qdict, "hold_time");
1808 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001809
balrogc8256f92008-06-08 22:45:01 +00001810 if (nb_pending_keycodes > 0) {
1811 qemu_del_timer(key_timer);
1812 release_keys(NULL);
1813 }
1814 if (!has_hold_time)
1815 hold_time = 100;
1816 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001817 while (1) {
1818 separator = strchr(string, '-');
1819 keyname_len = separator ? separator - string : strlen(string);
1820 if (keyname_len > 0) {
1821 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1822 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001823 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001824 return;
bellarda3a91a32004-06-04 11:06:21 +00001825 }
balrogc8256f92008-06-08 22:45:01 +00001826 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001827 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001828 return;
1829 }
1830 keyname_buf[keyname_len] = 0;
1831 keycode = get_keycode(keyname_buf);
1832 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001833 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001834 return;
1835 }
balrogc8256f92008-06-08 22:45:01 +00001836 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001837 }
balrog3401c0d2008-06-04 10:05:59 +00001838 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001839 break;
balrog3401c0d2008-06-04 10:05:59 +00001840 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001841 }
balrogc8256f92008-06-08 22:45:01 +00001842 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001843 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001844 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001845 keycode = keycodes[i];
1846 if (keycode & 0x80)
1847 kbd_put_keycode(0xe0);
1848 kbd_put_keycode(keycode & 0x7f);
1849 }
balrogc8256f92008-06-08 22:45:01 +00001850 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001851 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001852 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001853}
1854
bellard13224a82006-07-14 22:03:35 +00001855static int mouse_button_state;
1856
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001857static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001858{
1859 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001860 const char *dx_str = qdict_get_str(qdict, "dx_str");
1861 const char *dy_str = qdict_get_str(qdict, "dy_str");
1862 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001863 dx = strtol(dx_str, NULL, 0);
1864 dy = strtol(dy_str, NULL, 0);
1865 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001866 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001867 dz = strtol(dz_str, NULL, 0);
1868 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1869}
1870
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001871static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001872{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001873 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001874 mouse_button_state = button_state;
1875 kbd_mouse_event(0, 0, 0, mouse_button_state);
1876}
1877
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001878static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001879{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001880 int size = qdict_get_int(qdict, "size");
1881 int addr = qdict_get_int(qdict, "addr");
1882 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001883 uint32_t val;
1884 int suffix;
1885
1886 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001887 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001888 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001889 addr++;
1890 }
1891 addr &= 0xffff;
1892
1893 switch(size) {
1894 default:
1895 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001896 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001897 suffix = 'b';
1898 break;
1899 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001900 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001901 suffix = 'w';
1902 break;
1903 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001904 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001905 suffix = 'l';
1906 break;
1907 }
aliguori376253e2009-03-05 23:01:23 +00001908 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1909 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001910}
bellarda3a91a32004-06-04 11:06:21 +00001911
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001912static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001913{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001914 int size = qdict_get_int(qdict, "size");
1915 int addr = qdict_get_int(qdict, "addr");
1916 int val = qdict_get_int(qdict, "val");
1917
Jan Kiszkaf1147842009-07-14 10:20:11 +02001918 addr &= IOPORTS_MASK;
1919
1920 switch (size) {
1921 default:
1922 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001923 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001924 break;
1925 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001926 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001927 break;
1928 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001929 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001930 break;
1931 }
1932}
1933
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001934static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001935{
1936 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001937 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001938
Jan Kiszka76e30d02009-07-02 00:19:02 +02001939 res = qemu_boot_set(bootdevice);
1940 if (res == 0) {
1941 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1942 } else if (res > 0) {
1943 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001944 } else {
aliguori376253e2009-03-05 23:01:23 +00001945 monitor_printf(mon, "no function defined to set boot device list for "
1946 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001947 }
1948}
1949
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001950/**
Luiz Capitulino43076662009-10-07 13:41:59 -03001951 * do_system_powerdown(): Issue a machine powerdown
1952 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001953static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1954 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001955{
1956 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001957 return 0;
bellard34751872005-07-02 14:31:34 +00001958}
1959
bellardb86bda52004-09-18 19:32:46 +00001960#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001961static void print_pte(Monitor *mon, target_phys_addr_t addr,
1962 target_phys_addr_t pte,
1963 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001964{
Blue Swirld65aaf32010-12-11 18:56:24 +00001965#ifdef TARGET_X86_64
1966 if (addr & (1ULL << 47)) {
1967 addr |= -1LL << 48;
1968 }
1969#endif
1970 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1971 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001972 addr,
1973 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001974 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001975 pte & PG_GLOBAL_MASK ? 'G' : '-',
1976 pte & PG_PSE_MASK ? 'P' : '-',
1977 pte & PG_DIRTY_MASK ? 'D' : '-',
1978 pte & PG_ACCESSED_MASK ? 'A' : '-',
1979 pte & PG_PCD_MASK ? 'C' : '-',
1980 pte & PG_PWT_MASK ? 'T' : '-',
1981 pte & PG_USER_MASK ? 'U' : '-',
1982 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001983}
1984
Blue Swirld65aaf32010-12-11 18:56:24 +00001985static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001986{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001987 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001988 uint32_t pgd, pde, pte;
1989
bellardb86bda52004-09-18 19:32:46 +00001990 pgd = env->cr[3] & ~0xfff;
1991 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001992 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001993 pde = le32_to_cpu(pde);
1994 if (pde & PG_PRESENT_MASK) {
1995 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001996 /* 4M pages */
1997 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001998 } else {
1999 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002000 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002001 pte = le32_to_cpu(pte);
2002 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002003 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002004 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002005 ~0xfff);
2006 }
2007 }
2008 }
2009 }
2010 }
2011}
2012
Blue Swirld65aaf32010-12-11 18:56:24 +00002013static void tlb_info_pae32(Monitor *mon, CPUState *env)
2014{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002015 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002016 uint64_t pdpe, pde, pte;
2017 uint64_t pdp_addr, pd_addr, pt_addr;
2018
2019 pdp_addr = env->cr[3] & ~0x1f;
2020 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002021 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002022 pdpe = le64_to_cpu(pdpe);
2023 if (pdpe & PG_PRESENT_MASK) {
2024 pd_addr = pdpe & 0x3fffffffff000ULL;
2025 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002026 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002027 pde = le64_to_cpu(pde);
2028 if (pde & PG_PRESENT_MASK) {
2029 if (pde & PG_PSE_MASK) {
2030 /* 2M pages with PAE, CR4.PSE is ignored */
2031 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2032 ~((target_phys_addr_t)(1 << 20) - 1));
2033 } else {
2034 pt_addr = pde & 0x3fffffffff000ULL;
2035 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002036 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002037 pte = le64_to_cpu(pte);
2038 if (pte & PG_PRESENT_MASK) {
2039 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2040 + (l3 << 12),
2041 pte & ~PG_PSE_MASK,
2042 ~(target_phys_addr_t)0xfff);
2043 }
2044 }
2045 }
2046 }
2047 }
2048 }
2049 }
2050}
2051
2052#ifdef TARGET_X86_64
2053static void tlb_info_64(Monitor *mon, CPUState *env)
2054{
2055 uint64_t l1, l2, l3, l4;
2056 uint64_t pml4e, pdpe, pde, pte;
2057 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2058
2059 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2060 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002061 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002062 pml4e = le64_to_cpu(pml4e);
2063 if (pml4e & PG_PRESENT_MASK) {
2064 pdp_addr = pml4e & 0x3fffffffff000ULL;
2065 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002066 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002067 pdpe = le64_to_cpu(pdpe);
2068 if (pdpe & PG_PRESENT_MASK) {
2069 if (pdpe & PG_PSE_MASK) {
2070 /* 1G pages, CR4.PSE is ignored */
2071 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2072 0x3ffffc0000000ULL);
2073 } else {
2074 pd_addr = pdpe & 0x3fffffffff000ULL;
2075 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002076 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002077 pde = le64_to_cpu(pde);
2078 if (pde & PG_PRESENT_MASK) {
2079 if (pde & PG_PSE_MASK) {
2080 /* 2M pages, CR4.PSE is ignored */
2081 print_pte(mon, (l1 << 39) + (l2 << 30) +
2082 (l3 << 21), pde,
2083 0x3ffffffe00000ULL);
2084 } else {
2085 pt_addr = pde & 0x3fffffffff000ULL;
2086 for (l4 = 0; l4 < 512; l4++) {
2087 cpu_physical_memory_read(pt_addr
2088 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002089 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002090 pte = le64_to_cpu(pte);
2091 if (pte & PG_PRESENT_MASK) {
2092 print_pte(mon, (l1 << 39) +
2093 (l2 << 30) +
2094 (l3 << 21) + (l4 << 12),
2095 pte & ~PG_PSE_MASK,
2096 0x3fffffffff000ULL);
2097 }
2098 }
2099 }
2100 }
2101 }
2102 }
2103 }
2104 }
2105 }
2106 }
2107}
2108#endif
2109
2110static void tlb_info(Monitor *mon)
2111{
2112 CPUState *env;
2113
2114 env = mon_get_cpu();
2115
2116 if (!(env->cr[0] & CR0_PG_MASK)) {
2117 monitor_printf(mon, "PG disabled\n");
2118 return;
2119 }
2120 if (env->cr[4] & CR4_PAE_MASK) {
2121#ifdef TARGET_X86_64
2122 if (env->hflags & HF_LMA_MASK) {
2123 tlb_info_64(mon, env);
2124 } else
2125#endif
2126 {
2127 tlb_info_pae32(mon, env);
2128 }
2129 } else {
2130 tlb_info_32(mon, env);
2131 }
2132}
2133
Blue Swirl1b3cba62010-12-11 18:56:27 +00002134static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2135 int *plast_prot,
2136 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002137{
bellard9746b152004-11-11 18:30:24 +00002138 int prot1;
2139 prot1 = *plast_prot;
2140 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002141 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002142 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2143 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002144 *pstart, end, end - *pstart,
2145 prot1 & PG_USER_MASK ? 'u' : '-',
2146 'r',
2147 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002148 }
2149 if (prot != 0)
2150 *pstart = end;
2151 else
2152 *pstart = -1;
2153 *plast_prot = prot;
2154 }
2155}
2156
Blue Swirl1b3cba62010-12-11 18:56:27 +00002157static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002158{
Austin Clementsb49ca722011-08-14 23:19:21 -04002159 unsigned int l1, l2;
2160 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002161 uint32_t pgd, pde, pte;
2162 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002163
bellardb86bda52004-09-18 19:32:46 +00002164 pgd = env->cr[3] & ~0xfff;
2165 last_prot = 0;
2166 start = -1;
2167 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002168 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002169 pde = le32_to_cpu(pde);
2170 end = l1 << 22;
2171 if (pde & PG_PRESENT_MASK) {
2172 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2173 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002174 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002175 } else {
2176 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002177 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002178 pte = le32_to_cpu(pte);
2179 end = (l1 << 22) + (l2 << 12);
2180 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002181 prot = pte & pde &
2182 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002183 } else {
2184 prot = 0;
2185 }
aliguori376253e2009-03-05 23:01:23 +00002186 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002187 }
2188 }
2189 } else {
2190 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002191 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002192 }
2193 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002194 /* Flush last range */
2195 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002196}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002197
2198static void mem_info_pae32(Monitor *mon, CPUState *env)
2199{
Austin Clementsb49ca722011-08-14 23:19:21 -04002200 unsigned int l1, l2, l3;
2201 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002202 uint64_t pdpe, pde, pte;
2203 uint64_t pdp_addr, pd_addr, pt_addr;
2204 target_phys_addr_t start, end;
2205
2206 pdp_addr = env->cr[3] & ~0x1f;
2207 last_prot = 0;
2208 start = -1;
2209 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002210 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002211 pdpe = le64_to_cpu(pdpe);
2212 end = l1 << 30;
2213 if (pdpe & PG_PRESENT_MASK) {
2214 pd_addr = pdpe & 0x3fffffffff000ULL;
2215 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002216 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002217 pde = le64_to_cpu(pde);
2218 end = (l1 << 30) + (l2 << 21);
2219 if (pde & PG_PRESENT_MASK) {
2220 if (pde & PG_PSE_MASK) {
2221 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2222 PG_PRESENT_MASK);
2223 mem_print(mon, &start, &last_prot, end, prot);
2224 } else {
2225 pt_addr = pde & 0x3fffffffff000ULL;
2226 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002227 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002228 pte = le64_to_cpu(pte);
2229 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2230 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002231 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2232 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002233 } else {
2234 prot = 0;
2235 }
2236 mem_print(mon, &start, &last_prot, end, prot);
2237 }
2238 }
2239 } else {
2240 prot = 0;
2241 mem_print(mon, &start, &last_prot, end, prot);
2242 }
2243 }
2244 } else {
2245 prot = 0;
2246 mem_print(mon, &start, &last_prot, end, prot);
2247 }
2248 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002249 /* Flush last range */
2250 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002251}
2252
2253
2254#ifdef TARGET_X86_64
2255static void mem_info_64(Monitor *mon, CPUState *env)
2256{
2257 int prot, last_prot;
2258 uint64_t l1, l2, l3, l4;
2259 uint64_t pml4e, pdpe, pde, pte;
2260 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2261
2262 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2263 last_prot = 0;
2264 start = -1;
2265 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002266 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002267 pml4e = le64_to_cpu(pml4e);
2268 end = l1 << 39;
2269 if (pml4e & PG_PRESENT_MASK) {
2270 pdp_addr = pml4e & 0x3fffffffff000ULL;
2271 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002272 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002273 pdpe = le64_to_cpu(pdpe);
2274 end = (l1 << 39) + (l2 << 30);
2275 if (pdpe & PG_PRESENT_MASK) {
2276 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002277 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2278 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002279 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002280 mem_print(mon, &start, &last_prot, end, prot);
2281 } else {
2282 pd_addr = pdpe & 0x3fffffffff000ULL;
2283 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002284 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002285 pde = le64_to_cpu(pde);
2286 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2287 if (pde & PG_PRESENT_MASK) {
2288 if (pde & PG_PSE_MASK) {
2289 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2290 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002291 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002292 mem_print(mon, &start, &last_prot, end, prot);
2293 } else {
2294 pt_addr = pde & 0x3fffffffff000ULL;
2295 for (l4 = 0; l4 < 512; l4++) {
2296 cpu_physical_memory_read(pt_addr
2297 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002298 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002299 pte = le64_to_cpu(pte);
2300 end = (l1 << 39) + (l2 << 30) +
2301 (l3 << 21) + (l4 << 12);
2302 if (pte & PG_PRESENT_MASK) {
2303 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2304 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002305 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002306 } else {
2307 prot = 0;
2308 }
2309 mem_print(mon, &start, &last_prot, end, prot);
2310 }
2311 }
2312 } else {
2313 prot = 0;
2314 mem_print(mon, &start, &last_prot, end, prot);
2315 }
2316 }
2317 }
2318 } else {
2319 prot = 0;
2320 mem_print(mon, &start, &last_prot, end, prot);
2321 }
2322 }
2323 } else {
2324 prot = 0;
2325 mem_print(mon, &start, &last_prot, end, prot);
2326 }
2327 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002328 /* Flush last range */
2329 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002330}
2331#endif
2332
2333static void mem_info(Monitor *mon)
2334{
2335 CPUState *env;
2336
2337 env = mon_get_cpu();
2338
2339 if (!(env->cr[0] & CR0_PG_MASK)) {
2340 monitor_printf(mon, "PG disabled\n");
2341 return;
2342 }
2343 if (env->cr[4] & CR4_PAE_MASK) {
2344#ifdef TARGET_X86_64
2345 if (env->hflags & HF_LMA_MASK) {
2346 mem_info_64(mon, env);
2347 } else
2348#endif
2349 {
2350 mem_info_pae32(mon, env);
2351 }
2352 } else {
2353 mem_info_32(mon, env);
2354 }
2355}
bellardb86bda52004-09-18 19:32:46 +00002356#endif
2357
aurel327c664e22009-03-03 06:12:22 +00002358#if defined(TARGET_SH4)
2359
aliguori376253e2009-03-05 23:01:23 +00002360static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002361{
aliguori376253e2009-03-05 23:01:23 +00002362 monitor_printf(mon, " tlb%i:\t"
2363 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2364 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2365 "dirty=%hhu writethrough=%hhu\n",
2366 idx,
2367 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2368 tlb->v, tlb->sh, tlb->c, tlb->pr,
2369 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002370}
2371
aliguori376253e2009-03-05 23:01:23 +00002372static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002373{
2374 CPUState *env = mon_get_cpu();
2375 int i;
2376
aliguori376253e2009-03-05 23:01:23 +00002377 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002378 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002379 print_tlb (mon, i, &env->itlb[i]);
2380 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002381 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002382 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002383}
2384
2385#endif
2386
Scott Woodbebabbc2011-08-18 10:38:42 +00002387#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002388static void tlb_info(Monitor *mon)
2389{
2390 CPUState *env1 = mon_get_cpu();
2391
2392 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2393}
2394#endif
2395
Blue Swirl314e2982011-09-11 20:22:05 +00002396static void do_info_mtree(Monitor *mon)
2397{
2398 mtree_info((fprintf_function)monitor_printf, mon);
2399}
2400
aliguori030ea372009-04-21 22:30:47 +00002401static void do_info_numa(Monitor *mon)
2402{
aliguorib28b6232009-04-22 20:20:29 +00002403 int i;
aliguori030ea372009-04-21 22:30:47 +00002404 CPUState *env;
2405
2406 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2407 for (i = 0; i < nb_numa_nodes; i++) {
2408 monitor_printf(mon, "node %d cpus:", i);
2409 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2410 if (env->numa_node == i) {
2411 monitor_printf(mon, " %d", env->cpu_index);
2412 }
2413 }
2414 monitor_printf(mon, "\n");
2415 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2416 node_mem[i] >> 20);
2417 }
2418}
2419
bellard5f1ce942006-02-08 22:40:15 +00002420#ifdef CONFIG_PROFILER
2421
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002422int64_t qemu_time;
2423int64_t dev_time;
2424
aliguori376253e2009-03-05 23:01:23 +00002425static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002426{
2427 int64_t total;
2428 total = qemu_time;
2429 if (total == 0)
2430 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002431 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002432 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002433 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002434 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002435 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002436 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002437}
2438#else
aliguori376253e2009-03-05 23:01:23 +00002439static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002440{
aliguori376253e2009-03-05 23:01:23 +00002441 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002442}
2443#endif
2444
bellardec36b692006-07-16 18:57:03 +00002445/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002446static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002447
aliguori376253e2009-03-05 23:01:23 +00002448static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002449{
2450 int i;
2451 CaptureState *s;
2452
2453 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002454 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002455 s->ops.info (s->opaque);
2456 }
2457}
2458
Blue Swirl23130862009-06-06 08:22:04 +00002459#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002460static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002461{
2462 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002463 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002464 CaptureState *s;
2465
2466 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2467 if (i == n) {
2468 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002469 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002470 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002471 return;
2472 }
2473 }
2474}
2475
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002476static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002477{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002478 const char *path = qdict_get_str(qdict, "path");
2479 int has_freq = qdict_haskey(qdict, "freq");
2480 int freq = qdict_get_try_int(qdict, "freq", -1);
2481 int has_bits = qdict_haskey(qdict, "bits");
2482 int bits = qdict_get_try_int(qdict, "bits", -1);
2483 int has_channels = qdict_haskey(qdict, "nchannels");
2484 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002485 CaptureState *s;
2486
Anthony Liguori7267c092011-08-20 22:09:37 -05002487 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002488
2489 freq = has_freq ? freq : 44100;
2490 bits = has_bits ? bits : 16;
2491 nchannels = has_channels ? nchannels : 2;
2492
2493 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002494 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002495 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002496 return;
bellardec36b692006-07-16 18:57:03 +00002497 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002498 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002499}
2500#endif
2501
aurel32dc1c0b72008-04-27 23:52:12 +00002502#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002503static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002504{
2505 CPUState *env;
2506
2507 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2508 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2509 }
2510
2511 return 0;
2512}
2513#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002514static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002515{
2516 qerror_report(QERR_UNSUPPORTED);
2517 return -1;
2518}
aurel32dc1c0b72008-04-27 23:52:12 +00002519#endif
2520
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002521static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002522{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002523 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002524
aliguori76655d62009-03-06 20:27:37 +00002525 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002526 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002527 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002528 return acl;
2529}
aliguori76655d62009-03-06 20:27:37 +00002530
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002531static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002532{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002533 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002534 qemu_acl *acl = find_acl(mon, aclname);
2535 qemu_acl_entry *entry;
2536 int i = 0;
2537
2538 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002539 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002540 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002541 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002542 i++;
2543 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002544 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002545 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002546 }
2547}
2548
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002549static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002550{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002551 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002552 qemu_acl *acl = find_acl(mon, aclname);
2553
2554 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002555 qemu_acl_reset(acl);
2556 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002557 }
2558}
aliguori76655d62009-03-06 20:27:37 +00002559
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002560static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002561{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002562 const char *aclname = qdict_get_str(qdict, "aclname");
2563 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002564 qemu_acl *acl = find_acl(mon, aclname);
2565
2566 if (acl) {
2567 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002568 acl->defaultDeny = 0;
2569 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002570 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002571 acl->defaultDeny = 1;
2572 monitor_printf(mon, "acl: policy set to 'deny'\n");
2573 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002574 monitor_printf(mon, "acl: unknown policy '%s', "
2575 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002576 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002577 }
2578}
aliguori76655d62009-03-06 20:27:37 +00002579
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002580static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002581{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002582 const char *aclname = qdict_get_str(qdict, "aclname");
2583 const char *match = qdict_get_str(qdict, "match");
2584 const char *policy = qdict_get_str(qdict, "policy");
2585 int has_index = qdict_haskey(qdict, "index");
2586 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002587 qemu_acl *acl = find_acl(mon, aclname);
2588 int deny, ret;
2589
2590 if (acl) {
2591 if (strcmp(policy, "allow") == 0) {
2592 deny = 0;
2593 } else if (strcmp(policy, "deny") == 0) {
2594 deny = 1;
2595 } else {
2596 monitor_printf(mon, "acl: unknown policy '%s', "
2597 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002598 return;
2599 }
aliguori28a76be2009-03-06 20:27:40 +00002600 if (has_index)
2601 ret = qemu_acl_insert(acl, deny, match, index);
2602 else
2603 ret = qemu_acl_append(acl, deny, match);
2604 if (ret < 0)
2605 monitor_printf(mon, "acl: unable to add acl entry\n");
2606 else
2607 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002608 }
2609}
aliguori76655d62009-03-06 20:27:37 +00002610
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002611static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002612{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002613 const char *aclname = qdict_get_str(qdict, "aclname");
2614 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002615 qemu_acl *acl = find_acl(mon, aclname);
2616 int ret;
aliguori76655d62009-03-06 20:27:37 +00002617
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002618 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002619 ret = qemu_acl_remove(acl, match);
2620 if (ret < 0)
2621 monitor_printf(mon, "acl: no matching acl entry\n");
2622 else
2623 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002624 }
2625}
2626
Huang Ying79c4f6b2009-06-23 10:05:14 +08002627#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002628static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002629{
2630 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002631 int cpu_index = qdict_get_int(qdict, "cpu_index");
2632 int bank = qdict_get_int(qdict, "bank");
2633 uint64_t status = qdict_get_int(qdict, "status");
2634 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2635 uint64_t addr = qdict_get_int(qdict, "addr");
2636 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002637 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002638
Jan Kiszka747461c2011-03-02 08:56:10 +01002639 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2640 flags |= MCE_INJECT_BROADCAST;
2641 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002642 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002643 if (cenv->cpu_index == cpu_index) {
2644 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002645 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002646 break;
2647 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002648 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002649}
2650#endif
2651
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002652static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002653{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002654 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002655 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002656 int fd;
2657
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002658 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002659 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002660 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002661 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002662 }
2663
2664 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002665 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2666 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002667 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002668 }
2669
Blue Swirl72cf2d42009-09-12 07:36:22 +00002670 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002671 if (strcmp(monfd->name, fdname) != 0) {
2672 continue;
2673 }
2674
2675 close(monfd->fd);
2676 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002677 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002678 }
2679
Anthony Liguori7267c092011-08-20 22:09:37 -05002680 monfd = g_malloc0(sizeof(mon_fd_t));
2681 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002682 monfd->fd = fd;
2683
Blue Swirl72cf2d42009-09-12 07:36:22 +00002684 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002685 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002686}
2687
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002688static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002689{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002690 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002691 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002692
Blue Swirl72cf2d42009-09-12 07:36:22 +00002693 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002694 if (strcmp(monfd->name, fdname) != 0) {
2695 continue;
2696 }
2697
Blue Swirl72cf2d42009-09-12 07:36:22 +00002698 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002699 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002700 g_free(monfd->name);
2701 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002702 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002703 }
2704
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002705 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002706 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002707}
2708
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002709static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002710{
Luiz Capitulino13548692011-07-29 15:36:43 -03002711 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002712 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002713
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002714 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002715
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002716 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002717 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002718 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002719}
2720
Mark McLoughlin7768e042009-07-22 09:11:41 +01002721int monitor_get_fd(Monitor *mon, const char *fdname)
2722{
Anthony Liguoric227f092009-10-01 16:12:16 -05002723 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002724
Blue Swirl72cf2d42009-09-12 07:36:22 +00002725 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002726 int fd;
2727
2728 if (strcmp(monfd->name, fdname) != 0) {
2729 continue;
2730 }
2731
2732 fd = monfd->fd;
2733
2734 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002735 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002736 g_free(monfd->name);
2737 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002738
2739 return fd;
2740 }
2741
2742 return -1;
2743}
2744
Anthony Liguoric227f092009-10-01 16:12:16 -05002745static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002746#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002747 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002748};
2749
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002750/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002751static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 {
2753 .name = "version",
2754 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 .params = "",
2756 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002757 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 },
2759 {
2760 .name = "network",
2761 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002762 .params = "",
2763 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002764 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 },
2766 {
2767 .name = "chardev",
2768 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002769 .params = "",
2770 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002771 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 },
2773 {
2774 .name = "block",
2775 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 .params = "",
2777 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002778 .user_print = bdrv_info_print,
2779 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 },
2781 {
2782 .name = "blockstats",
2783 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 .params = "",
2785 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002786 .user_print = bdrv_stats_print,
2787 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 },
2789 {
2790 .name = "registers",
2791 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .params = "",
2793 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002794 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "cpus",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002801 .user_print = monitor_print_cpus,
2802 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 },
2804 {
2805 .name = "history",
2806 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002807 .params = "",
2808 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002809 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002810 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002811#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2812 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 {
2814 .name = "irq",
2815 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 .params = "",
2817 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002818#ifdef TARGET_SPARC
2819 .mhandler.info = sun4m_irq_info,
2820#elif defined(TARGET_LM32)
2821 .mhandler.info = lm32_irq_info,
2822#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002823 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002824#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 },
2826 {
2827 .name = "pic",
2828 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 .params = "",
2830 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002831#ifdef TARGET_SPARC
2832 .mhandler.info = sun4m_pic_info,
2833#elif defined(TARGET_LM32)
2834 .mhandler.info = lm32_do_pic_info,
2835#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002836 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002837#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002839#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 {
2841 .name = "pci",
2842 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 .params = "",
2844 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002845 .user_print = do_pci_info_print,
2846 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002847 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002848#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2849 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 {
2851 .name = "tlb",
2852 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002853 .params = "",
2854 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002855 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002856 },
aurel327c664e22009-03-03 06:12:22 +00002857#endif
2858#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002859 {
2860 .name = "mem",
2861 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002862 .params = "",
2863 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002864 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002865 },
bellardb86bda52004-09-18 19:32:46 +00002866#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002867 {
Blue Swirl314e2982011-09-11 20:22:05 +00002868 .name = "mtree",
2869 .args_type = "",
2870 .params = "",
2871 .help = "show memory tree",
2872 .mhandler.info = do_info_mtree,
2873 },
2874 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 .name = "jit",
2876 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002877 .params = "",
2878 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002879 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 },
2881 {
2882 .name = "kvm",
2883 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002884 .params = "",
2885 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002886 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 },
2888 {
2889 .name = "numa",
2890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 .params = "",
2892 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002893 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002894 },
2895 {
2896 .name = "usb",
2897 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002898 .params = "",
2899 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002900 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002901 },
2902 {
2903 .name = "usbhost",
2904 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 .params = "",
2906 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002907 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002908 },
2909 {
2910 .name = "profile",
2911 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 .params = "",
2913 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002914 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002915 },
2916 {
2917 .name = "capture",
2918 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 .params = "",
2920 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002921 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 },
2923 {
2924 .name = "snapshots",
2925 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 .params = "",
2927 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002928 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 },
2930 {
2931 .name = "status",
2932 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002933 .params = "",
2934 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002935 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002936 },
2937 {
2938 .name = "pcmcia",
2939 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002940 .params = "",
2941 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002942 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002943 },
2944 {
2945 .name = "mice",
2946 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002947 .params = "",
2948 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002949 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002950 },
2951 {
2952 .name = "vnc",
2953 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002954 .params = "",
2955 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002956 .user_print = do_info_vnc_print,
2957 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002958 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002959#if defined(CONFIG_SPICE)
2960 {
2961 .name = "spice",
2962 .args_type = "",
2963 .params = "",
2964 .help = "show the spice server status",
2965 .user_print = do_info_spice_print,
2966 .mhandler.info_new = do_info_spice,
2967 },
2968#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002969 {
2970 .name = "name",
2971 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002972 .params = "",
2973 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002974 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002975 },
2976 {
2977 .name = "uuid",
2978 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002979 .params = "",
2980 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002981 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002982 },
j_mayer76a66252007-03-07 08:32:30 +00002983#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002984 {
2985 .name = "cpustats",
2986 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002987 .params = "",
2988 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002989 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002990 },
j_mayer76a66252007-03-07 08:32:30 +00002991#endif
blueswir131a60e22007-10-26 18:42:59 +00002992#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002993 {
2994 .name = "usernet",
2995 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002996 .params = "",
2997 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002998 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002999 },
blueswir131a60e22007-10-26 18:42:59 +00003000#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003001 {
3002 .name = "migrate",
3003 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003004 .params = "",
3005 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03003006 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003007 },
3008 {
3009 .name = "balloon",
3010 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003011 .params = "",
3012 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003013 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003014 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003015 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003016 },
3017 {
3018 .name = "qtree",
3019 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003020 .params = "",
3021 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003022 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003023 },
3024 {
3025 .name = "qdm",
3026 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003027 .params = "",
3028 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003029 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003030 },
3031 {
3032 .name = "roms",
3033 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003034 .params = "",
3035 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003036 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003037 },
Lluís6d8a7642011-08-31 20:30:43 +02003038#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303039 {
3040 .name = "trace",
3041 .args_type = "",
3042 .params = "",
3043 .help = "show current contents of trace buffer",
3044 .mhandler.info = do_info_trace,
3045 },
Lluís31965ae2011-08-31 20:31:24 +02003046#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303047 {
3048 .name = "trace-events",
3049 .args_type = "",
3050 .params = "",
3051 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003052 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303053 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003054 {
3055 .name = NULL,
3056 },
bellard9dc39cb2004-03-14 21:38:27 +00003057};
3058
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003059static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05003060#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003061 { /* NULL */ },
3062};
3063
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003064static const mon_cmd_t qmp_query_cmds[] = {
3065 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003066 .name = "block",
3067 .args_type = "",
3068 .params = "",
3069 .help = "show the block devices",
3070 .user_print = bdrv_info_print,
3071 .mhandler.info_new = bdrv_info,
3072 },
3073 {
3074 .name = "blockstats",
3075 .args_type = "",
3076 .params = "",
3077 .help = "show block device statistics",
3078 .user_print = bdrv_stats_print,
3079 .mhandler.info_new = bdrv_info_stats,
3080 },
3081 {
3082 .name = "cpus",
3083 .args_type = "",
3084 .params = "",
3085 .help = "show infos for each CPU",
3086 .user_print = monitor_print_cpus,
3087 .mhandler.info_new = do_info_cpus,
3088 },
3089 {
3090 .name = "pci",
3091 .args_type = "",
3092 .params = "",
3093 .help = "show PCI info",
3094 .user_print = do_pci_info_print,
3095 .mhandler.info_new = do_pci_info,
3096 },
3097 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003098 .name = "vnc",
3099 .args_type = "",
3100 .params = "",
3101 .help = "show the vnc server status",
3102 .user_print = do_info_vnc_print,
3103 .mhandler.info_new = do_info_vnc,
3104 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003105#if defined(CONFIG_SPICE)
3106 {
3107 .name = "spice",
3108 .args_type = "",
3109 .params = "",
3110 .help = "show the spice server status",
3111 .user_print = do_info_spice_print,
3112 .mhandler.info_new = do_info_spice,
3113 },
3114#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003115 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003116 .name = "balloon",
3117 .args_type = "",
3118 .params = "",
3119 .help = "show balloon information",
3120 .user_print = monitor_print_balloon,
3121 .mhandler.info_async = do_info_balloon,
3122 .flags = MONITOR_CMD_ASYNC,
3123 },
3124 { /* NULL */ },
3125};
3126
bellard9307c4c2004-04-04 12:57:25 +00003127/*******************************************************************/
3128
3129static const char *pch;
3130static jmp_buf expr_env;
3131
bellard92a31b12005-02-10 22:00:52 +00003132#define MD_TLONG 0
3133#define MD_I32 1
3134
bellard9307c4c2004-04-04 12:57:25 +00003135typedef struct MonitorDef {
3136 const char *name;
3137 int offset;
blueswir18662d652008-10-02 18:32:44 +00003138 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003139 int type;
bellard9307c4c2004-04-04 12:57:25 +00003140} MonitorDef;
3141
bellard57206fd2004-04-25 18:54:52 +00003142#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003143static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003144{
bellard6a00d602005-11-21 23:25:50 +00003145 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003146 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003147}
3148#endif
3149
bellarda541f292004-04-12 20:39:29 +00003150#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003151static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003152{
bellard6a00d602005-11-21 23:25:50 +00003153 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003154 unsigned int u;
3155 int i;
3156
3157 u = 0;
3158 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003159 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003160
3161 return u;
3162}
3163
blueswir18662d652008-10-02 18:32:44 +00003164static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003165{
bellard6a00d602005-11-21 23:25:50 +00003166 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003167 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003168}
3169
blueswir18662d652008-10-02 18:32:44 +00003170static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003171{
bellard6a00d602005-11-21 23:25:50 +00003172 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003173 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003174}
bellard9fddaa02004-05-21 12:59:32 +00003175
blueswir18662d652008-10-02 18:32:44 +00003176static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003177{
bellard6a00d602005-11-21 23:25:50 +00003178 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003179 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003180}
3181
blueswir18662d652008-10-02 18:32:44 +00003182static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003183{
bellard6a00d602005-11-21 23:25:50 +00003184 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003185 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003186}
3187
blueswir18662d652008-10-02 18:32:44 +00003188static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003189{
bellard6a00d602005-11-21 23:25:50 +00003190 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003191 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003192}
bellarda541f292004-04-12 20:39:29 +00003193#endif
3194
bellarde95c8d52004-09-30 22:22:08 +00003195#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003196#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003197static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003198{
bellard6a00d602005-11-21 23:25:50 +00003199 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003200
3201 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003202}
bellard7b936c02005-10-30 17:05:13 +00003203#endif
bellarde95c8d52004-09-30 22:22:08 +00003204
blueswir18662d652008-10-02 18:32:44 +00003205static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003206{
bellard6a00d602005-11-21 23:25:50 +00003207 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003208 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003209}
3210#endif
3211
blueswir18662d652008-10-02 18:32:44 +00003212static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003213#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003214
3215#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003216 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003217 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003218 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003219
bellard9307c4c2004-04-04 12:57:25 +00003220 { "eax", offsetof(CPUState, regs[0]) },
3221 { "ecx", offsetof(CPUState, regs[1]) },
3222 { "edx", offsetof(CPUState, regs[2]) },
3223 { "ebx", offsetof(CPUState, regs[3]) },
3224 { "esp|sp", offsetof(CPUState, regs[4]) },
3225 { "ebp|fp", offsetof(CPUState, regs[5]) },
3226 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003227 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003228#ifdef TARGET_X86_64
3229 { "r8", offsetof(CPUState, regs[8]) },
3230 { "r9", offsetof(CPUState, regs[9]) },
3231 { "r10", offsetof(CPUState, regs[10]) },
3232 { "r11", offsetof(CPUState, regs[11]) },
3233 { "r12", offsetof(CPUState, regs[12]) },
3234 { "r13", offsetof(CPUState, regs[13]) },
3235 { "r14", offsetof(CPUState, regs[14]) },
3236 { "r15", offsetof(CPUState, regs[15]) },
3237#endif
bellard9307c4c2004-04-04 12:57:25 +00003238 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003239 { "eip", offsetof(CPUState, eip) },
3240 SEG("cs", R_CS)
3241 SEG("ds", R_DS)
3242 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003243 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003244 SEG("fs", R_FS)
3245 SEG("gs", R_GS)
3246 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003247#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003248 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003249 { "r0", offsetof(CPUState, gpr[0]) },
3250 { "r1", offsetof(CPUState, gpr[1]) },
3251 { "r2", offsetof(CPUState, gpr[2]) },
3252 { "r3", offsetof(CPUState, gpr[3]) },
3253 { "r4", offsetof(CPUState, gpr[4]) },
3254 { "r5", offsetof(CPUState, gpr[5]) },
3255 { "r6", offsetof(CPUState, gpr[6]) },
3256 { "r7", offsetof(CPUState, gpr[7]) },
3257 { "r8", offsetof(CPUState, gpr[8]) },
3258 { "r9", offsetof(CPUState, gpr[9]) },
3259 { "r10", offsetof(CPUState, gpr[10]) },
3260 { "r11", offsetof(CPUState, gpr[11]) },
3261 { "r12", offsetof(CPUState, gpr[12]) },
3262 { "r13", offsetof(CPUState, gpr[13]) },
3263 { "r14", offsetof(CPUState, gpr[14]) },
3264 { "r15", offsetof(CPUState, gpr[15]) },
3265 { "r16", offsetof(CPUState, gpr[16]) },
3266 { "r17", offsetof(CPUState, gpr[17]) },
3267 { "r18", offsetof(CPUState, gpr[18]) },
3268 { "r19", offsetof(CPUState, gpr[19]) },
3269 { "r20", offsetof(CPUState, gpr[20]) },
3270 { "r21", offsetof(CPUState, gpr[21]) },
3271 { "r22", offsetof(CPUState, gpr[22]) },
3272 { "r23", offsetof(CPUState, gpr[23]) },
3273 { "r24", offsetof(CPUState, gpr[24]) },
3274 { "r25", offsetof(CPUState, gpr[25]) },
3275 { "r26", offsetof(CPUState, gpr[26]) },
3276 { "r27", offsetof(CPUState, gpr[27]) },
3277 { "r28", offsetof(CPUState, gpr[28]) },
3278 { "r29", offsetof(CPUState, gpr[29]) },
3279 { "r30", offsetof(CPUState, gpr[30]) },
3280 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003281 /* Floating point registers */
3282 { "f0", offsetof(CPUState, fpr[0]) },
3283 { "f1", offsetof(CPUState, fpr[1]) },
3284 { "f2", offsetof(CPUState, fpr[2]) },
3285 { "f3", offsetof(CPUState, fpr[3]) },
3286 { "f4", offsetof(CPUState, fpr[4]) },
3287 { "f5", offsetof(CPUState, fpr[5]) },
3288 { "f6", offsetof(CPUState, fpr[6]) },
3289 { "f7", offsetof(CPUState, fpr[7]) },
3290 { "f8", offsetof(CPUState, fpr[8]) },
3291 { "f9", offsetof(CPUState, fpr[9]) },
3292 { "f10", offsetof(CPUState, fpr[10]) },
3293 { "f11", offsetof(CPUState, fpr[11]) },
3294 { "f12", offsetof(CPUState, fpr[12]) },
3295 { "f13", offsetof(CPUState, fpr[13]) },
3296 { "f14", offsetof(CPUState, fpr[14]) },
3297 { "f15", offsetof(CPUState, fpr[15]) },
3298 { "f16", offsetof(CPUState, fpr[16]) },
3299 { "f17", offsetof(CPUState, fpr[17]) },
3300 { "f18", offsetof(CPUState, fpr[18]) },
3301 { "f19", offsetof(CPUState, fpr[19]) },
3302 { "f20", offsetof(CPUState, fpr[20]) },
3303 { "f21", offsetof(CPUState, fpr[21]) },
3304 { "f22", offsetof(CPUState, fpr[22]) },
3305 { "f23", offsetof(CPUState, fpr[23]) },
3306 { "f24", offsetof(CPUState, fpr[24]) },
3307 { "f25", offsetof(CPUState, fpr[25]) },
3308 { "f26", offsetof(CPUState, fpr[26]) },
3309 { "f27", offsetof(CPUState, fpr[27]) },
3310 { "f28", offsetof(CPUState, fpr[28]) },
3311 { "f29", offsetof(CPUState, fpr[29]) },
3312 { "f30", offsetof(CPUState, fpr[30]) },
3313 { "f31", offsetof(CPUState, fpr[31]) },
3314 { "fpscr", offsetof(CPUState, fpscr) },
3315 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003316 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003317 { "lr", offsetof(CPUState, lr) },
3318 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003319 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003320 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003321 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003322 { "msr", 0, &monitor_get_msr, },
3323 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003324 { "tbu", 0, &monitor_get_tbu, },
3325 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003326#if defined(TARGET_PPC64)
3327 /* Address space register */
3328 { "asr", offsetof(CPUState, asr) },
3329#endif
3330 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003331 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003332 { "sr0", offsetof(CPUState, sr[0]) },
3333 { "sr1", offsetof(CPUState, sr[1]) },
3334 { "sr2", offsetof(CPUState, sr[2]) },
3335 { "sr3", offsetof(CPUState, sr[3]) },
3336 { "sr4", offsetof(CPUState, sr[4]) },
3337 { "sr5", offsetof(CPUState, sr[5]) },
3338 { "sr6", offsetof(CPUState, sr[6]) },
3339 { "sr7", offsetof(CPUState, sr[7]) },
3340 { "sr8", offsetof(CPUState, sr[8]) },
3341 { "sr9", offsetof(CPUState, sr[9]) },
3342 { "sr10", offsetof(CPUState, sr[10]) },
3343 { "sr11", offsetof(CPUState, sr[11]) },
3344 { "sr12", offsetof(CPUState, sr[12]) },
3345 { "sr13", offsetof(CPUState, sr[13]) },
3346 { "sr14", offsetof(CPUState, sr[14]) },
3347 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003348 /* Too lazy to put BATs... */
3349 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3350
3351 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3352 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3353 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3354 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3355 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3356 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3357 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3358 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3359 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3360 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3361 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3362 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3363 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3364 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3365 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3366 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3367 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3368 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3369 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3370 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3371 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3372 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3373 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3374 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3375 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3376 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3377 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3378 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3379 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3380 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3381 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3382 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3383 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3384 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3385 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3386 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3387 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3388 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3389 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3390 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3391 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3392 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3393 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3394 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3395 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3396 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3397 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3398 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3399 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3400 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3401 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3402 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3403 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3404 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3405 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3406 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3407 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3408 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3409 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3410 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3411 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3412 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3413 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3414 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3415 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3416 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3417
bellarde95c8d52004-09-30 22:22:08 +00003418#elif defined(TARGET_SPARC)
3419 { "g0", offsetof(CPUState, gregs[0]) },
3420 { "g1", offsetof(CPUState, gregs[1]) },
3421 { "g2", offsetof(CPUState, gregs[2]) },
3422 { "g3", offsetof(CPUState, gregs[3]) },
3423 { "g4", offsetof(CPUState, gregs[4]) },
3424 { "g5", offsetof(CPUState, gregs[5]) },
3425 { "g6", offsetof(CPUState, gregs[6]) },
3426 { "g7", offsetof(CPUState, gregs[7]) },
3427 { "o0", 0, monitor_get_reg },
3428 { "o1", 1, monitor_get_reg },
3429 { "o2", 2, monitor_get_reg },
3430 { "o3", 3, monitor_get_reg },
3431 { "o4", 4, monitor_get_reg },
3432 { "o5", 5, monitor_get_reg },
3433 { "o6", 6, monitor_get_reg },
3434 { "o7", 7, monitor_get_reg },
3435 { "l0", 8, monitor_get_reg },
3436 { "l1", 9, monitor_get_reg },
3437 { "l2", 10, monitor_get_reg },
3438 { "l3", 11, monitor_get_reg },
3439 { "l4", 12, monitor_get_reg },
3440 { "l5", 13, monitor_get_reg },
3441 { "l6", 14, monitor_get_reg },
3442 { "l7", 15, monitor_get_reg },
3443 { "i0", 16, monitor_get_reg },
3444 { "i1", 17, monitor_get_reg },
3445 { "i2", 18, monitor_get_reg },
3446 { "i3", 19, monitor_get_reg },
3447 { "i4", 20, monitor_get_reg },
3448 { "i5", 21, monitor_get_reg },
3449 { "i6", 22, monitor_get_reg },
3450 { "i7", 23, monitor_get_reg },
3451 { "pc", offsetof(CPUState, pc) },
3452 { "npc", offsetof(CPUState, npc) },
3453 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003454#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003455 { "psr", 0, &monitor_get_psr, },
3456 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003457#endif
bellarde95c8d52004-09-30 22:22:08 +00003458 { "tbr", offsetof(CPUState, tbr) },
3459 { "fsr", offsetof(CPUState, fsr) },
3460 { "f0", offsetof(CPUState, fpr[0]) },
3461 { "f1", offsetof(CPUState, fpr[1]) },
3462 { "f2", offsetof(CPUState, fpr[2]) },
3463 { "f3", offsetof(CPUState, fpr[3]) },
3464 { "f4", offsetof(CPUState, fpr[4]) },
3465 { "f5", offsetof(CPUState, fpr[5]) },
3466 { "f6", offsetof(CPUState, fpr[6]) },
3467 { "f7", offsetof(CPUState, fpr[7]) },
3468 { "f8", offsetof(CPUState, fpr[8]) },
3469 { "f9", offsetof(CPUState, fpr[9]) },
3470 { "f10", offsetof(CPUState, fpr[10]) },
3471 { "f11", offsetof(CPUState, fpr[11]) },
3472 { "f12", offsetof(CPUState, fpr[12]) },
3473 { "f13", offsetof(CPUState, fpr[13]) },
3474 { "f14", offsetof(CPUState, fpr[14]) },
3475 { "f15", offsetof(CPUState, fpr[15]) },
3476 { "f16", offsetof(CPUState, fpr[16]) },
3477 { "f17", offsetof(CPUState, fpr[17]) },
3478 { "f18", offsetof(CPUState, fpr[18]) },
3479 { "f19", offsetof(CPUState, fpr[19]) },
3480 { "f20", offsetof(CPUState, fpr[20]) },
3481 { "f21", offsetof(CPUState, fpr[21]) },
3482 { "f22", offsetof(CPUState, fpr[22]) },
3483 { "f23", offsetof(CPUState, fpr[23]) },
3484 { "f24", offsetof(CPUState, fpr[24]) },
3485 { "f25", offsetof(CPUState, fpr[25]) },
3486 { "f26", offsetof(CPUState, fpr[26]) },
3487 { "f27", offsetof(CPUState, fpr[27]) },
3488 { "f28", offsetof(CPUState, fpr[28]) },
3489 { "f29", offsetof(CPUState, fpr[29]) },
3490 { "f30", offsetof(CPUState, fpr[30]) },
3491 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003492#ifdef TARGET_SPARC64
3493 { "f32", offsetof(CPUState, fpr[32]) },
3494 { "f34", offsetof(CPUState, fpr[34]) },
3495 { "f36", offsetof(CPUState, fpr[36]) },
3496 { "f38", offsetof(CPUState, fpr[38]) },
3497 { "f40", offsetof(CPUState, fpr[40]) },
3498 { "f42", offsetof(CPUState, fpr[42]) },
3499 { "f44", offsetof(CPUState, fpr[44]) },
3500 { "f46", offsetof(CPUState, fpr[46]) },
3501 { "f48", offsetof(CPUState, fpr[48]) },
3502 { "f50", offsetof(CPUState, fpr[50]) },
3503 { "f52", offsetof(CPUState, fpr[52]) },
3504 { "f54", offsetof(CPUState, fpr[54]) },
3505 { "f56", offsetof(CPUState, fpr[56]) },
3506 { "f58", offsetof(CPUState, fpr[58]) },
3507 { "f60", offsetof(CPUState, fpr[60]) },
3508 { "f62", offsetof(CPUState, fpr[62]) },
3509 { "asi", offsetof(CPUState, asi) },
3510 { "pstate", offsetof(CPUState, pstate) },
3511 { "cansave", offsetof(CPUState, cansave) },
3512 { "canrestore", offsetof(CPUState, canrestore) },
3513 { "otherwin", offsetof(CPUState, otherwin) },
3514 { "wstate", offsetof(CPUState, wstate) },
3515 { "cleanwin", offsetof(CPUState, cleanwin) },
3516 { "fprs", offsetof(CPUState, fprs) },
3517#endif
bellard9307c4c2004-04-04 12:57:25 +00003518#endif
3519 { NULL },
3520};
3521
aliguori376253e2009-03-05 23:01:23 +00003522static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003523{
aliguori376253e2009-03-05 23:01:23 +00003524 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003525 longjmp(expr_env, 1);
3526}
3527
Markus Armbruster09b94182010-01-20 13:07:30 +01003528/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003529static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003530{
blueswir18662d652008-10-02 18:32:44 +00003531 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003532 void *ptr;
3533
bellard9307c4c2004-04-04 12:57:25 +00003534 for(md = monitor_defs; md->name != NULL; md++) {
3535 if (compare_cmd(name, md->name)) {
3536 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003537 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003538 } else {
bellard6a00d602005-11-21 23:25:50 +00003539 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003540 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003541 switch(md->type) {
3542 case MD_I32:
3543 *pval = *(int32_t *)ptr;
3544 break;
3545 case MD_TLONG:
3546 *pval = *(target_long *)ptr;
3547 break;
3548 default:
3549 *pval = 0;
3550 break;
3551 }
bellard9307c4c2004-04-04 12:57:25 +00003552 }
3553 return 0;
3554 }
3555 }
3556 return -1;
3557}
3558
3559static void next(void)
3560{
Blue Swirl660f11b2009-07-31 21:16:51 +00003561 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003562 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003563 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003564 pch++;
3565 }
3566}
3567
aliguori376253e2009-03-05 23:01:23 +00003568static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003569
aliguori376253e2009-03-05 23:01:23 +00003570static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003571{
blueswir1c2efc952007-09-25 17:28:42 +00003572 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003573 char *p;
bellard6a00d602005-11-21 23:25:50 +00003574 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003575
3576 switch(*pch) {
3577 case '+':
3578 next();
aliguori376253e2009-03-05 23:01:23 +00003579 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003580 break;
3581 case '-':
3582 next();
aliguori376253e2009-03-05 23:01:23 +00003583 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003584 break;
3585 case '~':
3586 next();
aliguori376253e2009-03-05 23:01:23 +00003587 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003588 break;
3589 case '(':
3590 next();
aliguori376253e2009-03-05 23:01:23 +00003591 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003592 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003593 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003594 }
3595 next();
3596 break;
bellard81d09122004-07-14 17:21:37 +00003597 case '\'':
3598 pch++;
3599 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003600 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003601 n = *pch;
3602 pch++;
3603 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003604 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003605 next();
3606 break;
bellard9307c4c2004-04-04 12:57:25 +00003607 case '$':
3608 {
3609 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003610 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003611
bellard9307c4c2004-04-04 12:57:25 +00003612 pch++;
3613 q = buf;
3614 while ((*pch >= 'a' && *pch <= 'z') ||
3615 (*pch >= 'A' && *pch <= 'Z') ||
3616 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003617 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003618 if ((q - buf) < sizeof(buf) - 1)
3619 *q++ = *pch;
3620 pch++;
3621 }
blueswir1cd390082008-11-16 13:53:32 +00003622 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003623 pch++;
3624 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003625 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003626 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003627 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003628 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003629 }
3630 break;
3631 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003632 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003633 n = 0;
3634 break;
3635 default:
blueswir17743e582007-09-24 18:39:04 +00003636#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003637 n = strtoull(pch, &p, 0);
3638#else
bellard9307c4c2004-04-04 12:57:25 +00003639 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003640#endif
bellard9307c4c2004-04-04 12:57:25 +00003641 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003642 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003643 }
3644 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003645 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003646 pch++;
3647 break;
3648 }
3649 return n;
3650}
3651
3652
aliguori376253e2009-03-05 23:01:23 +00003653static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003654{
blueswir1c2efc952007-09-25 17:28:42 +00003655 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003656 int op;
ths3b46e622007-09-17 08:09:54 +00003657
aliguori376253e2009-03-05 23:01:23 +00003658 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003659 for(;;) {
3660 op = *pch;
3661 if (op != '*' && op != '/' && op != '%')
3662 break;
3663 next();
aliguori376253e2009-03-05 23:01:23 +00003664 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003665 switch(op) {
3666 default:
3667 case '*':
3668 val *= val2;
3669 break;
3670 case '/':
3671 case '%':
ths5fafdf22007-09-16 21:08:06 +00003672 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003673 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003674 if (op == '/')
3675 val /= val2;
3676 else
3677 val %= val2;
3678 break;
3679 }
3680 }
3681 return val;
3682}
3683
aliguori376253e2009-03-05 23:01:23 +00003684static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003685{
blueswir1c2efc952007-09-25 17:28:42 +00003686 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003687 int op;
bellard9307c4c2004-04-04 12:57:25 +00003688
aliguori376253e2009-03-05 23:01:23 +00003689 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003690 for(;;) {
3691 op = *pch;
3692 if (op != '&' && op != '|' && op != '^')
3693 break;
3694 next();
aliguori376253e2009-03-05 23:01:23 +00003695 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003696 switch(op) {
3697 default:
3698 case '&':
3699 val &= val2;
3700 break;
3701 case '|':
3702 val |= val2;
3703 break;
3704 case '^':
3705 val ^= val2;
3706 break;
3707 }
3708 }
3709 return val;
3710}
3711
aliguori376253e2009-03-05 23:01:23 +00003712static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003713{
blueswir1c2efc952007-09-25 17:28:42 +00003714 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003715 int op;
bellard9307c4c2004-04-04 12:57:25 +00003716
aliguori376253e2009-03-05 23:01:23 +00003717 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003718 for(;;) {
3719 op = *pch;
3720 if (op != '+' && op != '-')
3721 break;
3722 next();
aliguori376253e2009-03-05 23:01:23 +00003723 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003724 if (op == '+')
3725 val += val2;
3726 else
3727 val -= val2;
3728 }
3729 return val;
3730}
3731
aliguori376253e2009-03-05 23:01:23 +00003732static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003733{
3734 pch = *pp;
3735 if (setjmp(expr_env)) {
3736 *pp = pch;
3737 return -1;
3738 }
blueswir1cd390082008-11-16 13:53:32 +00003739 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003740 pch++;
aliguori376253e2009-03-05 23:01:23 +00003741 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003742 *pp = pch;
3743 return 0;
3744}
3745
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003746static int get_double(Monitor *mon, double *pval, const char **pp)
3747{
3748 const char *p = *pp;
3749 char *tailp;
3750 double d;
3751
3752 d = strtod(p, &tailp);
3753 if (tailp == p) {
3754 monitor_printf(mon, "Number expected\n");
3755 return -1;
3756 }
3757 if (d != d || d - d != 0) {
3758 /* NaN or infinity */
3759 monitor_printf(mon, "Bad number\n");
3760 return -1;
3761 }
3762 *pval = d;
3763 *pp = tailp;
3764 return 0;
3765}
3766
bellard9307c4c2004-04-04 12:57:25 +00003767static int get_str(char *buf, int buf_size, const char **pp)
3768{
3769 const char *p;
3770 char *q;
3771 int c;
3772
bellard81d09122004-07-14 17:21:37 +00003773 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003774 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003775 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003776 p++;
3777 if (*p == '\0') {
3778 fail:
bellard81d09122004-07-14 17:21:37 +00003779 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003780 *pp = p;
3781 return -1;
3782 }
bellard9307c4c2004-04-04 12:57:25 +00003783 if (*p == '\"') {
3784 p++;
3785 while (*p != '\0' && *p != '\"') {
3786 if (*p == '\\') {
3787 p++;
3788 c = *p++;
3789 switch(c) {
3790 case 'n':
3791 c = '\n';
3792 break;
3793 case 'r':
3794 c = '\r';
3795 break;
3796 case '\\':
3797 case '\'':
3798 case '\"':
3799 break;
3800 default:
3801 qemu_printf("unsupported escape code: '\\%c'\n", c);
3802 goto fail;
3803 }
3804 if ((q - buf) < buf_size - 1) {
3805 *q++ = c;
3806 }
3807 } else {
3808 if ((q - buf) < buf_size - 1) {
3809 *q++ = *p;
3810 }
3811 p++;
3812 }
3813 }
3814 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003815 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003816 goto fail;
3817 }
3818 p++;
3819 } else {
blueswir1cd390082008-11-16 13:53:32 +00003820 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003821 if ((q - buf) < buf_size - 1) {
3822 *q++ = *p;
3823 }
3824 p++;
3825 }
bellard9307c4c2004-04-04 12:57:25 +00003826 }
bellard81d09122004-07-14 17:21:37 +00003827 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003828 *pp = p;
3829 return 0;
3830}
3831
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003832/*
3833 * Store the command-name in cmdname, and return a pointer to
3834 * the remaining of the command string.
3835 */
3836static const char *get_command_name(const char *cmdline,
3837 char *cmdname, size_t nlen)
3838{
3839 size_t len;
3840 const char *p, *pstart;
3841
3842 p = cmdline;
3843 while (qemu_isspace(*p))
3844 p++;
3845 if (*p == '\0')
3846 return NULL;
3847 pstart = p;
3848 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3849 p++;
3850 len = p - pstart;
3851 if (len > nlen - 1)
3852 len = nlen - 1;
3853 memcpy(cmdname, pstart, len);
3854 cmdname[len] = '\0';
3855 return p;
3856}
3857
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003858/**
3859 * Read key of 'type' into 'key' and return the current
3860 * 'type' pointer.
3861 */
3862static char *key_get_info(const char *type, char **key)
3863{
3864 size_t len;
3865 char *p, *str;
3866
3867 if (*type == ',')
3868 type++;
3869
3870 p = strchr(type, ':');
3871 if (!p) {
3872 *key = NULL;
3873 return NULL;
3874 }
3875 len = p - type;
3876
Anthony Liguori7267c092011-08-20 22:09:37 -05003877 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003878 memcpy(str, type, len);
3879 str[len] = '\0';
3880
3881 *key = str;
3882 return ++p;
3883}
3884
bellard9307c4c2004-04-04 12:57:25 +00003885static int default_fmt_format = 'x';
3886static int default_fmt_size = 4;
3887
3888#define MAX_ARGS 16
3889
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003890static int is_valid_option(const char *c, const char *typestr)
3891{
3892 char option[3];
3893
3894 option[0] = '-';
3895 option[1] = *c;
3896 option[2] = '\0';
3897
3898 typestr = strstr(typestr, option);
3899 return (typestr != NULL);
3900}
3901
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003902static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3903 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003904{
3905 const mon_cmd_t *cmd;
3906
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003907 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003908 if (compare_cmd(cmdname, cmd->name)) {
3909 return cmd;
3910 }
3911 }
3912
3913 return NULL;
3914}
3915
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003916static const mon_cmd_t *monitor_find_command(const char *cmdname)
3917{
3918 return search_dispatch_table(mon_cmds, cmdname);
3919}
3920
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003921static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3922{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003923 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003924}
3925
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003926static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3927{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003928 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003929}
3930
Anthony Liguoric227f092009-10-01 16:12:16 -05003931static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003932 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003933 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003934{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003935 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003936 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003937 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003938 char cmdname[256];
3939 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003940 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003941
3942#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003943 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003944#endif
ths3b46e622007-09-17 08:09:54 +00003945
bellard9307c4c2004-04-04 12:57:25 +00003946 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003947 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3948 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003949 return NULL;
ths3b46e622007-09-17 08:09:54 +00003950
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003951 cmd = monitor_find_command(cmdname);
3952 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003953 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003954 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003955 }
bellard9307c4c2004-04-04 12:57:25 +00003956
bellard9307c4c2004-04-04 12:57:25 +00003957 /* parse the parameters */
3958 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003959 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003960 typestr = key_get_info(typestr, &key);
3961 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003962 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003963 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003964 typestr++;
3965 switch(c) {
3966 case 'F':
bellard81d09122004-07-14 17:21:37 +00003967 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003968 case 's':
3969 {
3970 int ret;
ths3b46e622007-09-17 08:09:54 +00003971
blueswir1cd390082008-11-16 13:53:32 +00003972 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003973 p++;
3974 if (*typestr == '?') {
3975 typestr++;
3976 if (*p == '\0') {
3977 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003978 break;
bellard9307c4c2004-04-04 12:57:25 +00003979 }
3980 }
3981 ret = get_str(buf, sizeof(buf), &p);
3982 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003983 switch(c) {
3984 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003985 monitor_printf(mon, "%s: filename expected\n",
3986 cmdname);
bellard81d09122004-07-14 17:21:37 +00003987 break;
3988 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003989 monitor_printf(mon, "%s: block device name expected\n",
3990 cmdname);
bellard81d09122004-07-14 17:21:37 +00003991 break;
3992 default:
aliguori376253e2009-03-05 23:01:23 +00003993 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003994 break;
3995 }
bellard9307c4c2004-04-04 12:57:25 +00003996 goto fail;
3997 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003998 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003999 }
4000 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004001 case 'O':
4002 {
4003 QemuOptsList *opts_list;
4004 QemuOpts *opts;
4005
4006 opts_list = qemu_find_opts(key);
4007 if (!opts_list || opts_list->desc->name) {
4008 goto bad_type;
4009 }
4010 while (qemu_isspace(*p)) {
4011 p++;
4012 }
4013 if (!*p)
4014 break;
4015 if (get_str(buf, sizeof(buf), &p) < 0) {
4016 goto fail;
4017 }
4018 opts = qemu_opts_parse(opts_list, buf, 1);
4019 if (!opts) {
4020 goto fail;
4021 }
4022 qemu_opts_to_qdict(opts, qdict);
4023 qemu_opts_del(opts);
4024 }
4025 break;
bellard9307c4c2004-04-04 12:57:25 +00004026 case '/':
4027 {
4028 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004029
blueswir1cd390082008-11-16 13:53:32 +00004030 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004031 p++;
4032 if (*p == '/') {
4033 /* format found */
4034 p++;
4035 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004036 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004037 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004038 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004039 count = count * 10 + (*p - '0');
4040 p++;
4041 }
4042 }
4043 size = -1;
4044 format = -1;
4045 for(;;) {
4046 switch(*p) {
4047 case 'o':
4048 case 'd':
4049 case 'u':
4050 case 'x':
4051 case 'i':
4052 case 'c':
4053 format = *p++;
4054 break;
4055 case 'b':
4056 size = 1;
4057 p++;
4058 break;
4059 case 'h':
4060 size = 2;
4061 p++;
4062 break;
4063 case 'w':
4064 size = 4;
4065 p++;
4066 break;
4067 case 'g':
4068 case 'L':
4069 size = 8;
4070 p++;
4071 break;
4072 default:
4073 goto next;
4074 }
4075 }
4076 next:
blueswir1cd390082008-11-16 13:53:32 +00004077 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004078 monitor_printf(mon, "invalid char in format: '%c'\n",
4079 *p);
bellard9307c4c2004-04-04 12:57:25 +00004080 goto fail;
4081 }
bellard9307c4c2004-04-04 12:57:25 +00004082 if (format < 0)
4083 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004084 if (format != 'i') {
4085 /* for 'i', not specifying a size gives -1 as size */
4086 if (size < 0)
4087 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004088 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004089 }
bellard9307c4c2004-04-04 12:57:25 +00004090 default_fmt_format = format;
4091 } else {
4092 count = 1;
4093 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004094 if (format != 'i') {
4095 size = default_fmt_size;
4096 } else {
4097 size = -1;
4098 }
bellard9307c4c2004-04-04 12:57:25 +00004099 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004100 qdict_put(qdict, "count", qint_from_int(count));
4101 qdict_put(qdict, "format", qint_from_int(format));
4102 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004103 }
4104 break;
4105 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004106 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004107 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004108 {
blueswir1c2efc952007-09-25 17:28:42 +00004109 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004110
blueswir1cd390082008-11-16 13:53:32 +00004111 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004112 p++;
bellard34405572004-06-08 00:55:58 +00004113 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004114 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004115 if (*p == '\0') {
4116 typestr++;
4117 break;
4118 }
bellard34405572004-06-08 00:55:58 +00004119 } else {
4120 if (*p == '.') {
4121 p++;
blueswir1cd390082008-11-16 13:53:32 +00004122 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004123 p++;
bellard34405572004-06-08 00:55:58 +00004124 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004125 typestr++;
4126 break;
bellard34405572004-06-08 00:55:58 +00004127 }
4128 }
bellard13224a82006-07-14 22:03:35 +00004129 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004130 }
aliguori376253e2009-03-05 23:01:23 +00004131 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004132 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004133 /* Check if 'i' is greater than 32-bit */
4134 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4135 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4136 monitor_printf(mon, "integer is for 32-bit values\n");
4137 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004138 } else if (c == 'M') {
4139 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004140 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004141 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004142 }
4143 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004144 case 'o':
4145 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004146 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004147 char *end;
4148
4149 while (qemu_isspace(*p)) {
4150 p++;
4151 }
4152 if (*typestr == '?') {
4153 typestr++;
4154 if (*p == '\0') {
4155 break;
4156 }
4157 }
4158 val = strtosz(p, &end);
4159 if (val < 0) {
4160 monitor_printf(mon, "invalid size\n");
4161 goto fail;
4162 }
4163 qdict_put(qdict, key, qint_from_int(val));
4164 p = end;
4165 }
4166 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004167 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004168 {
4169 double val;
4170
4171 while (qemu_isspace(*p))
4172 p++;
4173 if (*typestr == '?') {
4174 typestr++;
4175 if (*p == '\0') {
4176 break;
4177 }
4178 }
4179 if (get_double(mon, &val, &p) < 0) {
4180 goto fail;
4181 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004182 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004183 switch (*p) {
4184 case 'm':
4185 val /= 1e3; p += 2; break;
4186 case 'u':
4187 val /= 1e6; p += 2; break;
4188 case 'n':
4189 val /= 1e9; p += 2; break;
4190 }
4191 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004192 if (*p && !qemu_isspace(*p)) {
4193 monitor_printf(mon, "Unknown unit suffix\n");
4194 goto fail;
4195 }
4196 qdict_put(qdict, key, qfloat_from_double(val));
4197 }
4198 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004199 case 'b':
4200 {
4201 const char *beg;
4202 int val;
4203
4204 while (qemu_isspace(*p)) {
4205 p++;
4206 }
4207 beg = p;
4208 while (qemu_isgraph(*p)) {
4209 p++;
4210 }
4211 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4212 val = 1;
4213 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4214 val = 0;
4215 } else {
4216 monitor_printf(mon, "Expected 'on' or 'off'\n");
4217 goto fail;
4218 }
4219 qdict_put(qdict, key, qbool_from_int(val));
4220 }
4221 break;
bellard9307c4c2004-04-04 12:57:25 +00004222 case '-':
4223 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004224 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004225 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004226 /* option */
ths3b46e622007-09-17 08:09:54 +00004227
bellard9307c4c2004-04-04 12:57:25 +00004228 c = *typestr++;
4229 if (c == '\0')
4230 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004231 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004232 p++;
bellard9307c4c2004-04-04 12:57:25 +00004233 if (*p == '-') {
4234 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004235 if(c != *p) {
4236 if(!is_valid_option(p, typestr)) {
4237
4238 monitor_printf(mon, "%s: unsupported option -%c\n",
4239 cmdname, *p);
4240 goto fail;
4241 } else {
4242 skip_key = 1;
4243 }
bellard9307c4c2004-04-04 12:57:25 +00004244 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004245 if(skip_key) {
4246 p = tmp;
4247 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004248 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004249 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004250 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004251 }
bellard9307c4c2004-04-04 12:57:25 +00004252 }
bellard9307c4c2004-04-04 12:57:25 +00004253 }
4254 break;
4255 default:
4256 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004257 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004258 goto fail;
4259 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004260 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004261 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004262 }
4263 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004264 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004265 p++;
4266 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004267 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4268 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004269 goto fail;
4270 }
4271
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004272 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004273
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004274fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004275 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004276 return NULL;
4277}
4278
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004279void monitor_set_error(Monitor *mon, QError *qerror)
4280{
4281 /* report only the first error */
4282 if (!mon->error) {
4283 mon->error = qerror;
4284 } else {
4285 MON_DEBUG("Additional error report at %s:%d\n",
4286 qerror->file, qerror->linenr);
4287 QDECREF(qerror);
4288 }
4289}
4290
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004291static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4292{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004293 if (ret && !monitor_has_error(mon)) {
4294 /*
4295 * If it returns failure, it must have passed on error.
4296 *
4297 * Action: Report an internal error to the client if in QMP.
4298 */
4299 qerror_report(QERR_UNDEFINED_ERROR);
4300 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4301 cmd->name);
4302 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004303
4304#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004305 if (!ret && monitor_has_error(mon)) {
4306 /*
4307 * If it returns success, it must not have passed an error.
4308 *
4309 * Action: Report the passed error to the client.
4310 */
4311 MON_DEBUG("command '%s' returned success but passed an error\n",
4312 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004313 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004314
4315 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4316 /*
4317 * Handlers should not call Monitor print functions.
4318 *
4319 * Action: Ignore them in QMP.
4320 *
4321 * (XXX: we don't check any 'info' or 'query' command here
4322 * because the user print function _is_ called by do_info(), hence
4323 * we will trigger this check. This problem will go away when we
4324 * make 'query' commands real and kill do_info())
4325 */
4326 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4327 cmd->name, mon_print_count_get(mon));
4328 }
4329#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004330}
4331
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004332static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004333{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004334 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004335 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004336
4337 qdict = qdict_new();
4338
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004339 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004340 if (!cmd)
4341 goto out;
4342
Luiz Capitulino4903de02010-09-16 11:01:32 -03004343 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004344 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004345 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004346 QObject *data = NULL;
4347
4348 /* XXX: ignores the error code */
4349 cmd->mhandler.cmd_new(mon, qdict, &data);
4350 assert(!monitor_has_error(mon));
4351 if (data) {
4352 cmd->user_print(mon, data);
4353 qobject_decref(data);
4354 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004355 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004356 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004357 }
4358
Luiz Capitulino13917be2009-10-07 13:41:54 -03004359out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004360 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004361}
4362
bellard81d09122004-07-14 17:21:37 +00004363static void cmd_completion(const char *name, const char *list)
4364{
4365 const char *p, *pstart;
4366 char cmd[128];
4367 int len;
4368
4369 p = list;
4370 for(;;) {
4371 pstart = p;
4372 p = strchr(p, '|');
4373 if (!p)
4374 p = pstart + strlen(pstart);
4375 len = p - pstart;
4376 if (len > sizeof(cmd) - 2)
4377 len = sizeof(cmd) - 2;
4378 memcpy(cmd, pstart, len);
4379 cmd[len] = '\0';
4380 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004381 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004382 }
4383 if (*p == '\0')
4384 break;
4385 p++;
4386 }
4387}
4388
4389static void file_completion(const char *input)
4390{
4391 DIR *ffs;
4392 struct dirent *d;
4393 char path[1024];
4394 char file[1024], file_prefix[1024];
4395 int input_path_len;
4396 const char *p;
4397
ths5fafdf22007-09-16 21:08:06 +00004398 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004399 if (!p) {
4400 input_path_len = 0;
4401 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004402 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004403 } else {
4404 input_path_len = p - input + 1;
4405 memcpy(path, input, input_path_len);
4406 if (input_path_len > sizeof(path) - 1)
4407 input_path_len = sizeof(path) - 1;
4408 path[input_path_len] = '\0';
4409 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4410 }
4411#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004412 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4413 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004414#endif
4415 ffs = opendir(path);
4416 if (!ffs)
4417 return;
4418 for(;;) {
4419 struct stat sb;
4420 d = readdir(ffs);
4421 if (!d)
4422 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004423
4424 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4425 continue;
4426 }
4427
bellard81d09122004-07-14 17:21:37 +00004428 if (strstart(d->d_name, file_prefix, NULL)) {
4429 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004430 if (input_path_len < sizeof(file))
4431 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4432 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004433 /* stat the file to find out if it's a directory.
4434 * In that case add a slash to speed up typing long paths
4435 */
4436 stat(file, &sb);
4437 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004438 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004439 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004440 }
4441 }
4442 closedir(ffs);
4443}
4444
aliguori51de9762009-03-05 23:00:43 +00004445static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004446{
aliguori51de9762009-03-05 23:00:43 +00004447 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004448 const char *input = opaque;
4449
4450 if (input[0] == '\0' ||
4451 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004452 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004453 }
4454}
4455
4456/* NOTE: this parser is an approximate form of the real command parser */
4457static void parse_cmdline(const char *cmdline,
4458 int *pnb_args, char **args)
4459{
4460 const char *p;
4461 int nb_args, ret;
4462 char buf[1024];
4463
4464 p = cmdline;
4465 nb_args = 0;
4466 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004467 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004468 p++;
4469 if (*p == '\0')
4470 break;
4471 if (nb_args >= MAX_ARGS)
4472 break;
4473 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004474 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004475 nb_args++;
4476 if (ret < 0)
4477 break;
4478 }
4479 *pnb_args = nb_args;
4480}
4481
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004482static const char *next_arg_type(const char *typestr)
4483{
4484 const char *p = strchr(typestr, ':');
4485 return (p != NULL ? ++p : typestr);
4486}
4487
aliguori4c36ba32009-03-05 23:01:37 +00004488static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004489{
4490 const char *cmdname;
4491 char *args[MAX_ARGS];
4492 int nb_args, i, len;
4493 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004494 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004495 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004496
4497 parse_cmdline(cmdline, &nb_args, args);
4498#ifdef DEBUG_COMPLETION
4499 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004500 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004501 }
4502#endif
4503
4504 /* if the line ends with a space, it means we want to complete the
4505 next arg */
4506 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004507 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004508 if (nb_args >= MAX_ARGS) {
4509 goto cleanup;
4510 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004511 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004512 }
4513 if (nb_args <= 1) {
4514 /* command completion */
4515 if (nb_args == 0)
4516 cmdname = "";
4517 else
4518 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004519 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004520 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004521 cmd_completion(cmdname, cmd->name);
4522 }
4523 } else {
4524 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004525 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4526 if (compare_cmd(args[0], cmd->name)) {
4527 break;
4528 }
bellard81d09122004-07-14 17:21:37 +00004529 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004530 if (!cmd->name) {
4531 goto cleanup;
4532 }
4533
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004534 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004535 for(i = 0; i < nb_args - 2; i++) {
4536 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004537 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004538 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004539 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004540 }
4541 }
4542 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004543 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004544 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004545 }
bellard81d09122004-07-14 17:21:37 +00004546 switch(*ptype) {
4547 case 'F':
4548 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004549 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004550 file_completion(str);
4551 break;
4552 case 'B':
4553 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004554 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004555 bdrv_iterate(block_completion_it, (void *)str);
4556 break;
bellard7fe48482004-10-09 18:08:01 +00004557 case 's':
4558 /* XXX: more generic ? */
4559 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004560 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004561 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4562 cmd_completion(str, cmd->name);
4563 }
bellard64866c32006-05-07 18:03:31 +00004564 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004565 char *sep = strrchr(str, '-');
4566 if (sep)
4567 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004568 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004569 for(key = key_defs; key->name != NULL; key++) {
4570 cmd_completion(str, key->name);
4571 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004572 } else if (!strcmp(cmd->name, "help|?")) {
4573 readline_set_completion_index(cur_mon->rs, strlen(str));
4574 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4575 cmd_completion(str, cmd->name);
4576 }
bellard7fe48482004-10-09 18:08:01 +00004577 }
4578 break;
bellard81d09122004-07-14 17:21:37 +00004579 default:
4580 break;
4581 }
4582 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004583
4584cleanup:
4585 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004586 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004587 }
bellard81d09122004-07-14 17:21:37 +00004588}
4589
aliguori731b0362009-03-05 23:01:42 +00004590static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004591{
aliguori731b0362009-03-05 23:01:42 +00004592 Monitor *mon = opaque;
4593
Jan Kiszkac62313b2009-12-04 14:05:29 +01004594 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004595}
4596
Luiz Capitulino09069b12010-02-04 18:10:06 -02004597static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4598{
4599 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4600 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4601}
4602
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004603/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004604 * Argument validation rules:
4605 *
4606 * 1. The argument must exist in cmd_args qdict
4607 * 2. The argument type must be the expected one
4608 *
4609 * Special case: If the argument doesn't exist in cmd_args and
4610 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4611 * checking is skipped for it.
4612 */
4613static int check_client_args_type(const QDict *client_args,
4614 const QDict *cmd_args, int flags)
4615{
4616 const QDictEntry *ent;
4617
4618 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4619 QObject *obj;
4620 QString *arg_type;
4621 const QObject *client_arg = qdict_entry_value(ent);
4622 const char *client_arg_name = qdict_entry_key(ent);
4623
4624 obj = qdict_get(cmd_args, client_arg_name);
4625 if (!obj) {
4626 if (flags & QMP_ACCEPT_UNKNOWNS) {
4627 /* handler accepts unknowns */
4628 continue;
4629 }
4630 /* client arg doesn't exist */
4631 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4632 return -1;
4633 }
4634
4635 arg_type = qobject_to_qstring(obj);
4636 assert(arg_type != NULL);
4637
4638 /* check if argument's type is correct */
4639 switch (qstring_get_str(arg_type)[0]) {
4640 case 'F':
4641 case 'B':
4642 case 's':
4643 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4644 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4645 "string");
4646 return -1;
4647 }
4648 break;
4649 case 'i':
4650 case 'l':
4651 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004652 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004653 if (qobject_type(client_arg) != QTYPE_QINT) {
4654 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4655 "int");
4656 return -1;
4657 }
4658 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004659 case 'T':
4660 if (qobject_type(client_arg) != QTYPE_QINT &&
4661 qobject_type(client_arg) != QTYPE_QFLOAT) {
4662 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4663 "number");
4664 return -1;
4665 }
4666 break;
4667 case 'b':
4668 case '-':
4669 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4670 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4671 "bool");
4672 return -1;
4673 }
4674 break;
4675 case 'O':
4676 assert(flags & QMP_ACCEPT_UNKNOWNS);
4677 break;
4678 case '/':
4679 case '.':
4680 /*
4681 * These types are not supported by QMP and thus are not
4682 * handled here. Fall through.
4683 */
4684 default:
4685 abort();
4686 }
4687 }
4688
4689 return 0;
4690}
4691
4692/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004693 * - Check if the client has passed all mandatory args
4694 * - Set special flags for argument validation
4695 */
4696static int check_mandatory_args(const QDict *cmd_args,
4697 const QDict *client_args, int *flags)
4698{
4699 const QDictEntry *ent;
4700
4701 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4702 const char *cmd_arg_name = qdict_entry_key(ent);
4703 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4704 assert(type != NULL);
4705
4706 if (qstring_get_str(type)[0] == 'O') {
4707 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4708 *flags |= QMP_ACCEPT_UNKNOWNS;
4709 } else if (qstring_get_str(type)[0] != '-' &&
4710 qstring_get_str(type)[1] != '?' &&
4711 !qdict_haskey(client_args, cmd_arg_name)) {
4712 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4713 return -1;
4714 }
4715 }
4716
4717 return 0;
4718}
4719
4720static QDict *qdict_from_args_type(const char *args_type)
4721{
4722 int i;
4723 QDict *qdict;
4724 QString *key, *type, *cur_qs;
4725
4726 assert(args_type != NULL);
4727
4728 qdict = qdict_new();
4729
4730 if (args_type == NULL || args_type[0] == '\0') {
4731 /* no args, empty qdict */
4732 goto out;
4733 }
4734
4735 key = qstring_new();
4736 type = qstring_new();
4737
4738 cur_qs = key;
4739
4740 for (i = 0;; i++) {
4741 switch (args_type[i]) {
4742 case ',':
4743 case '\0':
4744 qdict_put(qdict, qstring_get_str(key), type);
4745 QDECREF(key);
4746 if (args_type[i] == '\0') {
4747 goto out;
4748 }
4749 type = qstring_new(); /* qdict has ref */
4750 cur_qs = key = qstring_new();
4751 break;
4752 case ':':
4753 cur_qs = type;
4754 break;
4755 default:
4756 qstring_append_chr(cur_qs, args_type[i]);
4757 break;
4758 }
4759 }
4760
4761out:
4762 return qdict;
4763}
4764
4765/*
4766 * Client argument checking rules:
4767 *
4768 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004769 * 2. Each argument provided by the client must be expected
4770 * 3. Each argument provided by the client must have the type expected
4771 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004772 */
4773static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4774{
4775 int flags, err;
4776 QDict *cmd_args;
4777
4778 cmd_args = qdict_from_args_type(cmd->args_type);
4779
4780 flags = 0;
4781 err = check_mandatory_args(cmd_args, client_args, &flags);
4782 if (err) {
4783 goto out;
4784 }
4785
Luiz Capitulino4af91932010-06-22 11:44:05 -03004786 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004787
4788out:
4789 QDECREF(cmd_args);
4790 return err;
4791}
4792
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004793/*
4794 * Input object checking rules
4795 *
4796 * 1. Input object must be a dict
4797 * 2. The "execute" key must exist
4798 * 3. The "execute" key must be a string
4799 * 4. If the "arguments" key exists, it must be a dict
4800 * 5. If the "id" key exists, it can be anything (ie. json-value)
4801 * 6. Any argument not listed above is considered invalid
4802 */
4803static QDict *qmp_check_input_obj(QObject *input_obj)
4804{
4805 const QDictEntry *ent;
4806 int has_exec_key = 0;
4807 QDict *input_dict;
4808
4809 if (qobject_type(input_obj) != QTYPE_QDICT) {
4810 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4811 return NULL;
4812 }
4813
4814 input_dict = qobject_to_qdict(input_obj);
4815
4816 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4817 const char *arg_name = qdict_entry_key(ent);
4818 const QObject *arg_obj = qdict_entry_value(ent);
4819
4820 if (!strcmp(arg_name, "execute")) {
4821 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4822 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4823 "string");
4824 return NULL;
4825 }
4826 has_exec_key = 1;
4827 } else if (!strcmp(arg_name, "arguments")) {
4828 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4829 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4830 "object");
4831 return NULL;
4832 }
4833 } else if (!strcmp(arg_name, "id")) {
4834 /* FIXME: check duplicated IDs for async commands */
4835 } else {
4836 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4837 return NULL;
4838 }
4839 }
4840
4841 if (!has_exec_key) {
4842 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4843 return NULL;
4844 }
4845
4846 return input_dict;
4847}
4848
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004849static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4850{
4851 QObject *ret_data = NULL;
4852
Luiz Capitulino4903de02010-09-16 11:01:32 -03004853 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004854 qmp_async_info_handler(mon, cmd);
4855 if (monitor_has_error(mon)) {
4856 monitor_protocol_emitter(mon, NULL);
4857 }
4858 } else {
4859 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004860 monitor_protocol_emitter(mon, ret_data);
4861 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004862 }
4863}
4864
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004865static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4866 const QDict *params)
4867{
4868 int ret;
4869 QObject *data = NULL;
4870
4871 mon_print_count_init(mon);
4872
4873 ret = cmd->mhandler.cmd_new(mon, params, &data);
4874 handler_audit(mon, cmd, ret);
4875 monitor_protocol_emitter(mon, data);
4876 qobject_decref(data);
4877}
4878
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004879static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4880{
4881 int err;
4882 QObject *obj;
4883 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004884 const mon_cmd_t *cmd;
4885 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004886 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004887
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004888 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004889 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004890
4891 obj = json_parser_parse(tokens, NULL);
4892 if (!obj) {
4893 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004894 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004895 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004896 }
4897
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004898 input = qmp_check_input_obj(obj);
4899 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004900 qobject_decref(obj);
4901 goto err_out;
4902 }
4903
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004904 mon->mc->id = qdict_get(input, "id");
4905 qobject_incref(mon->mc->id);
4906
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004907 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004908 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004909 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004910 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004911 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004912 }
4913
Anthony Liguorie3193602011-09-02 12:34:47 -05004914 cmd = qmp_find_cmd(cmd_name);
4915 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004916 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004917 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004918 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004919 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004920 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004921 }
4922
4923 obj = qdict_get(input, "arguments");
4924 if (!obj) {
4925 args = qdict_new();
4926 } else {
4927 args = qobject_to_qdict(obj);
4928 QINCREF(args);
4929 }
4930
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004931 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004932 if (err < 0) {
4933 goto err_out;
4934 }
4935
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004936 if (query_cmd) {
4937 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004938 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004939 err = qmp_async_cmd_handler(mon, cmd, args);
4940 if (err) {
4941 /* emit the error response */
4942 goto err_out;
4943 }
Adam Litke940cc302010-01-25 12:18:44 -06004944 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004945 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004946 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004947
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004948 goto out;
4949
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004950err_out:
4951 monitor_protocol_emitter(mon, NULL);
4952out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004953 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004954 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004955}
4956
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004957/**
4958 * monitor_control_read(): Read and handle QMP input
4959 */
4960static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4961{
4962 Monitor *old_mon = cur_mon;
4963
4964 cur_mon = opaque;
4965
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004966 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004967
4968 cur_mon = old_mon;
4969}
4970
aliguori731b0362009-03-05 23:01:42 +00004971static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004972{
aliguori731b0362009-03-05 23:01:42 +00004973 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004974 int i;
aliguori376253e2009-03-05 23:01:23 +00004975
aliguori731b0362009-03-05 23:01:42 +00004976 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004977
aliguoricde76ee2009-03-05 23:01:51 +00004978 if (cur_mon->rs) {
4979 for (i = 0; i < size; i++)
4980 readline_handle_byte(cur_mon->rs, buf[i]);
4981 } else {
4982 if (size == 0 || buf[size - 1] != 0)
4983 monitor_printf(cur_mon, "corrupted command\n");
4984 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004985 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004986 }
aliguori731b0362009-03-05 23:01:42 +00004987
4988 cur_mon = old_mon;
4989}
aliguorid8f44602008-10-06 13:52:44 +00004990
aliguori376253e2009-03-05 23:01:23 +00004991static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004992{
aliguori731b0362009-03-05 23:01:42 +00004993 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004994 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004995 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004996}
4997
aliguoricde76ee2009-03-05 23:01:51 +00004998int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004999{
aliguoricde76ee2009-03-05 23:01:51 +00005000 if (!mon->rs)
5001 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005002 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005003 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005004}
5005
aliguori376253e2009-03-05 23:01:23 +00005006void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005007{
aliguoricde76ee2009-03-05 23:01:51 +00005008 if (!mon->rs)
5009 return;
aliguori731b0362009-03-05 23:01:42 +00005010 if (--mon->suspend_cnt == 0)
5011 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005012}
5013
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005014static QObject *get_qmp_greeting(void)
5015{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005016 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005017
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005018 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005019 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5020}
5021
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005022/**
5023 * monitor_control_event(): Print QMP gretting
5024 */
5025static void monitor_control_event(void *opaque, int event)
5026{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005027 QObject *data;
5028 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005029
Luiz Capitulino47116d12010-02-08 17:01:30 -02005030 switch (event) {
5031 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005032 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005033 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005034 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005035 monitor_json_emitter(mon, data);
5036 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005037 break;
5038 case CHR_EVENT_CLOSED:
5039 json_message_parser_destroy(&mon->mc->parser);
5040 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005041 }
5042}
5043
aliguori731b0362009-03-05 23:01:42 +00005044static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005045{
aliguori376253e2009-03-05 23:01:23 +00005046 Monitor *mon = opaque;
5047
aliguori2724b182009-03-05 23:01:47 +00005048 switch (event) {
5049 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005050 mon->mux_out = 0;
5051 if (mon->reset_seen) {
5052 readline_restart(mon->rs);
5053 monitor_resume(mon);
5054 monitor_flush(mon);
5055 } else {
5056 mon->suspend_cnt = 0;
5057 }
aliguori2724b182009-03-05 23:01:47 +00005058 break;
ths86e94de2007-01-05 22:01:59 +00005059
aliguori2724b182009-03-05 23:01:47 +00005060 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005061 if (mon->reset_seen) {
5062 if (mon->suspend_cnt == 0) {
5063 monitor_printf(mon, "\n");
5064 }
5065 monitor_flush(mon);
5066 monitor_suspend(mon);
5067 } else {
5068 mon->suspend_cnt++;
5069 }
5070 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005071 break;
5072
Amit Shahb6b8df52009-10-07 18:31:16 +05305073 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005074 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5075 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005076 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005077 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005078 }
5079 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005080 break;
5081 }
ths86e94de2007-01-05 22:01:59 +00005082}
5083
aliguori76655d62009-03-06 20:27:37 +00005084
5085/*
5086 * Local variables:
5087 * c-indent-level: 4
5088 * c-basic-offset: 4
5089 * tab-width: 8
5090 * End:
5091 */
5092
aliguori731b0362009-03-05 23:01:42 +00005093void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005094{
aliguori731b0362009-03-05 23:01:42 +00005095 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005096 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005097
5098 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005099 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005100 is_first_init = 0;
5101 }
aliguori87127162009-03-05 23:01:29 +00005102
Anthony Liguori7267c092011-08-20 22:09:37 -05005103 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005104
aliguori87127162009-03-05 23:01:29 +00005105 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005106 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005107 if (flags & MONITOR_USE_READLINE) {
5108 mon->rs = readline_init(mon, monitor_find_completion);
5109 monitor_read_command(mon, 0);
5110 }
aliguori87127162009-03-05 23:01:29 +00005111
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005112 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005113 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005114 /* Control mode requires special handlers */
5115 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5116 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005117 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005118 } else {
5119 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5120 monitor_event, mon);
5121 }
aliguori87127162009-03-05 23:01:29 +00005122
Blue Swirl72cf2d42009-09-12 07:36:22 +00005123 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005124 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5125 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005126}
5127
aliguori376253e2009-03-05 23:01:23 +00005128static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005129{
aliguoribb5fc202009-03-05 23:01:15 +00005130 BlockDriverState *bs = opaque;
5131 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005132
aliguoribb5fc202009-03-05 23:01:15 +00005133 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005134 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005135 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005136 }
aliguori731b0362009-03-05 23:01:42 +00005137 if (mon->password_completion_cb)
5138 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005139
aliguori731b0362009-03-05 23:01:42 +00005140 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005141}
aliguoric0f4ce72009-03-05 23:01:01 +00005142
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005143int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5144 BlockDriverCompletionFunc *completion_cb,
5145 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005146{
aliguoricde76ee2009-03-05 23:01:51 +00005147 int err;
5148
aliguoribb5fc202009-03-05 23:01:15 +00005149 if (!bdrv_key_required(bs)) {
5150 if (completion_cb)
5151 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005152 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005153 }
aliguoric0f4ce72009-03-05 23:01:01 +00005154
Luiz Capitulino94171e12009-12-07 21:37:00 +01005155 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005156 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005157 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005158 }
5159
aliguori376253e2009-03-05 23:01:23 +00005160 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5161 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005162
aliguori731b0362009-03-05 23:01:42 +00005163 mon->password_completion_cb = completion_cb;
5164 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005165
aliguoricde76ee2009-03-05 23:01:51 +00005166 err = monitor_read_password(mon, bdrv_password_cb, bs);
5167
5168 if (err && completion_cb)
5169 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005170
5171 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005172}