blob: 13b0fa57382c35bd82aaa36dacbd24cc8ca7feb3 [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 int mon_set_cpu(int cpu_index);
518static void handle_user_command(Monitor *mon, const char *cmdline);
519
520static int do_hmp_passthrough(Monitor *mon, const QDict *params,
521 QObject **ret_data)
522{
523 int ret = 0;
524 Monitor *old_mon, hmp;
525 CharDriverState mchar;
526
527 memset(&hmp, 0, sizeof(hmp));
528 qemu_chr_init_mem(&mchar);
529 hmp.chr = &mchar;
530
531 old_mon = cur_mon;
532 cur_mon = &hmp;
533
534 if (qdict_haskey(params, "cpu-index")) {
535 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
536 if (ret < 0) {
537 cur_mon = old_mon;
538 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
539 goto out;
540 }
541 }
542
543 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
544 cur_mon = old_mon;
545
546 if (qemu_chr_mem_osize(hmp.chr) > 0) {
547 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
548 }
549
550out:
551 qemu_chr_close_mem(hmp.chr);
552 return ret;
553}
554
bellard9dc39cb2004-03-14 21:38:27 +0000555static int compare_cmd(const char *name, const char *list)
556{
557 const char *p, *pstart;
558 int len;
559 len = strlen(name);
560 p = list;
561 for(;;) {
562 pstart = p;
563 p = strchr(p, '|');
564 if (!p)
565 p = pstart + strlen(pstart);
566 if ((p - pstart) == len && !memcmp(pstart, name, len))
567 return 1;
568 if (*p == '\0')
569 break;
570 p++;
571 }
572 return 0;
573}
574
Anthony Liguoric227f092009-10-01 16:12:16 -0500575static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000576 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000577{
Anthony Liguoric227f092009-10-01 16:12:16 -0500578 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000579
580 for(cmd = cmds; cmd->name != NULL; cmd++) {
581 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000582 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
583 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000584 }
585}
586
aliguori376253e2009-03-05 23:01:23 +0000587static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000588{
589 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000590 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000591 } else {
aliguori376253e2009-03-05 23:01:23 +0000592 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000593 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000594 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000595 monitor_printf(mon, "Log items (comma separated):\n");
596 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000597 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000598 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000599 }
600 }
bellard9dc39cb2004-03-14 21:38:27 +0000601 }
602}
603
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300604static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300605{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300606 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300607}
608
Lluísfc764102011-08-31 20:31:18 +0200609static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530610{
611 const char *tp_name = qdict_get_str(qdict, "name");
612 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200613 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000614
615 if (!ret) {
616 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
617 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530618}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100619
Michael Rothc45a8162011-10-02 08:44:37 -0500620#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100621static void do_trace_file(Monitor *mon, const QDict *qdict)
622{
623 const char *op = qdict_get_try_str(qdict, "op");
624 const char *arg = qdict_get_try_str(qdict, "arg");
625
626 if (!op) {
627 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
628 } else if (!strcmp(op, "on")) {
629 st_set_trace_file_enabled(true);
630 } else if (!strcmp(op, "off")) {
631 st_set_trace_file_enabled(false);
632 } else if (!strcmp(op, "flush")) {
633 st_flush_trace_buffer();
634 } else if (!strcmp(op, "set")) {
635 if (arg) {
636 st_set_trace_file(arg);
637 }
638 } else {
639 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
640 help_cmd(mon, "trace-file");
641 }
642}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530643#endif
644
Adam Litke940cc302010-01-25 12:18:44 -0600645static void user_monitor_complete(void *opaque, QObject *ret_data)
646{
647 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
648
649 if (ret_data) {
650 data->user_print(data->mon, ret_data);
651 }
652 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500653 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600654}
655
656static void qmp_monitor_complete(void *opaque, QObject *ret_data)
657{
658 monitor_protocol_emitter(opaque, ret_data);
659}
660
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300661static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
662 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600663{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300664 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600665}
666
667static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
668{
669 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
670}
671
672static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
673 const QDict *params)
674{
675 int ret;
676
Anthony Liguori7267c092011-08-20 22:09:37 -0500677 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600678 cb_data->mon = mon;
679 cb_data->user_print = cmd->user_print;
680 monitor_suspend(mon);
681 ret = cmd->mhandler.cmd_async(mon, params,
682 user_monitor_complete, cb_data);
683 if (ret < 0) {
684 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500685 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600686 }
687}
688
689static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
690{
691 int ret;
692
Anthony Liguori7267c092011-08-20 22:09:37 -0500693 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600694 cb_data->mon = mon;
695 cb_data->user_print = cmd->user_print;
696 monitor_suspend(mon);
697 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
698 if (ret < 0) {
699 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500700 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600701 }
702}
703
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300704static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000705{
Anthony Liguoric227f092009-10-01 16:12:16 -0500706 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300707 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000708
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200709 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000710 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200711 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300712
713 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000714 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300715 break;
bellard9dc39cb2004-03-14 21:38:27 +0000716 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300717
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200718 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300719 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200720 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300721
Luiz Capitulino4903de02010-09-16 11:01:32 -0300722 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300723 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300724 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300725 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200726
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300727 cmd->mhandler.info_new(mon, &info_data);
728 if (info_data) {
729 cmd->user_print(mon, info_data);
730 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200731 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300732 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300733 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300734 }
735
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300736 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300737
738help:
739 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000740}
741
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300742static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200743{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300744 CommandInfoList *info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200745
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300746 info = g_malloc0(sizeof(*info));
747 info->value = g_malloc0(sizeof(*info->value));
748 info->value->name = g_strdup(cmd_name);
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200749
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300750 return info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200751}
752
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300753CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000754{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300755 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200756 const mon_cmd_t *cmd;
757
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300758 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300759 info = alloc_cmd_entry(cmd->name);
760 info->next = cmd_list;
761 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200762 }
763
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300764 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300765 char buf[128];
766 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300767 info = alloc_cmd_entry(buf);
768 info->next = cmd_list;
769 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200770 }
771
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300772 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000773}
774
bellard6a00d602005-11-21 23:25:50 +0000775/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000776static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000777{
778 CPUState *env;
779
780 for(env = first_cpu; env != NULL; env = env->next_cpu) {
781 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000782 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000783 return 0;
784 }
785 }
786 return -1;
787}
788
pbrook9596ebb2007-11-18 01:44:38 +0000789static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000790{
aliguori731b0362009-03-05 23:01:42 +0000791 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000792 mon_set_cpu(0);
793 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300794 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000795 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000796}
797
aliguori376253e2009-03-05 23:01:23 +0000798static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000799{
bellard6a00d602005-11-21 23:25:50 +0000800 CPUState *env;
801 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000802#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000803 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000804 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000805#else
aliguori376253e2009-03-05 23:01:23 +0000806 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000807 0);
bellard9307c4c2004-04-04 12:57:25 +0000808#endif
809}
810
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300811static void print_cpu_iter(QObject *obj, void *opaque)
812{
813 QDict *cpu;
814 int active = ' ';
815 Monitor *mon = opaque;
816
817 assert(qobject_type(obj) == QTYPE_QDICT);
818 cpu = qobject_to_qdict(obj);
819
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200820 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300821 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200822 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300823
824 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
825
826#if defined(TARGET_I386)
827 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
828 (target_ulong) qdict_get_int(cpu, "pc"));
829#elif defined(TARGET_PPC)
830 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
831 (target_long) qdict_get_int(cpu, "nip"));
832#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500833 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300834 (target_long) qdict_get_int(cpu, "pc"));
835 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
836 (target_long) qdict_get_int(cpu, "npc"));
837#elif defined(TARGET_MIPS)
838 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "PC"));
840#endif
841
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200842 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300843 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200844 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300845
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100846 monitor_printf(mon, " thread_id=%" PRId64 " ",
847 qdict_get_int(cpu, "thread_id"));
848
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300849 monitor_printf(mon, "\n");
850}
851
852static void monitor_print_cpus(Monitor *mon, const QObject *data)
853{
854 QList *cpu_list;
855
856 assert(qobject_type(data) == QTYPE_QLIST);
857 cpu_list = qobject_to_qlist(data);
858 qlist_iter(cpu_list, print_cpu_iter, mon);
859}
860
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300861static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000862{
863 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300864 QList *cpu_list;
865
866 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000867
868 /* just to set the default cpu if not already done */
869 mon_get_cpu();
870
871 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200872 QDict *cpu;
873 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300874
Avi Kivity4c0960c2009-08-17 23:19:53 +0300875 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300876
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
878 env->cpu_index, env == mon->mon_cpu,
879 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200880
881 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300882
bellard6a00d602005-11-21 23:25:50 +0000883#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300884 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000885#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300886 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000887#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888 qdict_put(cpu, "pc", qint_from_int(env->pc));
889 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000890#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300891 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000892#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100893 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300894
895 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000896 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300897
898 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000899}
900
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200901static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000902{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300903 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200904 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100905 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
906 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200907 return -1;
908 }
909 return 0;
bellard6a00d602005-11-21 23:25:50 +0000910}
911
aliguori376253e2009-03-05 23:01:23 +0000912static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000913{
aliguori376253e2009-03-05 23:01:23 +0000914 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000915}
916
aliguori376253e2009-03-05 23:01:23 +0000917static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000918{
919 int i;
bellard7e2515e2004-08-01 21:52:19 +0000920 const char *str;
ths3b46e622007-09-17 08:09:54 +0000921
aliguoricde76ee2009-03-05 23:01:51 +0000922 if (!mon->rs)
923 return;
bellard7e2515e2004-08-01 21:52:19 +0000924 i = 0;
925 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000926 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000927 if (!str)
928 break;
aliguori376253e2009-03-05 23:01:23 +0000929 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000930 i++;
bellardaa455482004-04-04 13:07:25 +0000931 }
932}
933
j_mayer76a66252007-03-07 08:32:30 +0000934#if defined(TARGET_PPC)
935/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000936static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000937{
938 CPUState *env;
939
940 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000941 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000942}
943#endif
944
Lluís6d8a7642011-08-31 20:30:43 +0200945#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530946static void do_info_trace(Monitor *mon)
947{
948 st_print_trace((FILE *)mon, &monitor_fprintf);
949}
Lluís31965ae2011-08-31 20:31:24 +0200950#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530951
Lluísfc764102011-08-31 20:31:18 +0200952static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530953{
Lluísfc764102011-08-31 20:31:18 +0200954 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530955}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530956
Jes Sorensen821601e2011-03-16 13:33:36 +0100957#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200958static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000959{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600960 if (!password || !password[0]) {
961 if (vnc_display_disable_login(NULL)) {
962 qerror_report(QERR_SET_PASSWD_FAILED);
963 return -1;
964 }
965 return 0;
966 }
967
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200968 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100969 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200970 return -1;
971 }
aliguoribb5fc202009-03-05 23:01:15 +0000972
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200973 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100974}
975
976static void change_vnc_password_cb(Monitor *mon, const char *password,
977 void *opaque)
978{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100979 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000980 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000981}
982
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200983static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000984{
ths70848512007-08-25 01:37:05 +0000985 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000986 strcmp(target, "password") == 0) {
987 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000988 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000989 strncpy(password, arg, sizeof(password));
990 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200991 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000992 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200993 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000994 }
ths70848512007-08-25 01:37:05 +0000995 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200996 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100997 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200998 return -1;
999 }
ths70848512007-08-25 01:37:05 +00001000 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001001
1002 return 0;
thse25a5822007-08-25 01:36:20 +00001003}
Jes Sorensen821601e2011-03-16 13:33:36 +01001004#else
1005static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1006{
1007 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1008 return -ENODEV;
1009}
1010#endif
thse25a5822007-08-25 01:36:20 +00001011
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001012/**
1013 * do_change(): Change a removable medium, or VNC configuration
1014 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001015static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001016{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001017 const char *device = qdict_get_str(qdict, "device");
1018 const char *target = qdict_get_str(qdict, "target");
1019 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001020 int ret;
1021
thse25a5822007-08-25 01:36:20 +00001022 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001023 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001024 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001025 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001026 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027
1028 return ret;
thse25a5822007-08-25 01:36:20 +00001029}
1030
Gerd Hoffmann75721502010-10-07 12:22:54 +02001031static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1032{
1033 const char *protocol = qdict_get_str(qdict, "protocol");
1034 const char *password = qdict_get_str(qdict, "password");
1035 const char *connected = qdict_get_try_str(qdict, "connected");
1036 int disconnect_if_connected = 0;
1037 int fail_if_connected = 0;
1038 int rc;
1039
1040 if (connected) {
1041 if (strcmp(connected, "fail") == 0) {
1042 fail_if_connected = 1;
1043 } else if (strcmp(connected, "disconnect") == 0) {
1044 disconnect_if_connected = 1;
1045 } else if (strcmp(connected, "keep") == 0) {
1046 /* nothing */
1047 } else {
1048 qerror_report(QERR_INVALID_PARAMETER, "connected");
1049 return -1;
1050 }
1051 }
1052
1053 if (strcmp(protocol, "spice") == 0) {
1054 if (!using_spice) {
1055 /* correct one? spice isn't a device ,,, */
1056 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1057 return -1;
1058 }
1059 rc = qemu_spice_set_passwd(password, fail_if_connected,
1060 disconnect_if_connected);
1061 if (rc != 0) {
1062 qerror_report(QERR_SET_PASSWD_FAILED);
1063 return -1;
1064 }
1065 return 0;
1066 }
1067
1068 if (strcmp(protocol, "vnc") == 0) {
1069 if (fail_if_connected || disconnect_if_connected) {
1070 /* vnc supports "connected=keep" only */
1071 qerror_report(QERR_INVALID_PARAMETER, "connected");
1072 return -1;
1073 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001074 /* Note that setting an empty password will not disable login through
1075 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001076 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001077 }
1078
1079 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1080 return -1;
1081}
1082
1083static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1084{
1085 const char *protocol = qdict_get_str(qdict, "protocol");
1086 const char *whenstr = qdict_get_str(qdict, "time");
1087 time_t when;
1088 int rc;
1089
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001090 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001091 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001092 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001093 when = TIME_MAX;
1094 } else if (whenstr[0] == '+') {
1095 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1096 } else {
1097 when = strtoull(whenstr, NULL, 10);
1098 }
1099
1100 if (strcmp(protocol, "spice") == 0) {
1101 if (!using_spice) {
1102 /* correct one? spice isn't a device ,,, */
1103 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1104 return -1;
1105 }
1106 rc = qemu_spice_set_pw_expire(when);
1107 if (rc != 0) {
1108 qerror_report(QERR_SET_PASSWD_FAILED);
1109 return -1;
1110 }
1111 return 0;
1112 }
1113
1114 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001115 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001116 }
1117
1118 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1119 return -1;
1120}
1121
Daniel P. Berrange13661082011-06-23 13:31:42 +01001122static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1123{
1124 const char *protocol = qdict_get_str(qdict, "protocol");
1125 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001126 CharDriverState *s;
1127
1128 if (strcmp(protocol, "spice") == 0) {
1129 if (!using_spice) {
1130 /* correct one? spice isn't a device ,,, */
1131 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1132 return -1;
1133 }
1134 qerror_report(QERR_ADD_CLIENT_FAILED);
1135 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001136#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001137 } else if (strcmp(protocol, "vnc") == 0) {
1138 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001139 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001140 vnc_display_add_client(NULL, fd, skipauth);
1141 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001142#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001143 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1144 int fd = monitor_get_fd(mon, fdname);
1145 if (qemu_chr_add_client(s, fd) < 0) {
1146 qerror_report(QERR_ADD_CLIENT_FAILED);
1147 return -1;
1148 }
1149 return 0;
1150 }
1151
1152 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1153 return -1;
1154}
1155
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001156static int client_migrate_info(Monitor *mon, const QDict *qdict,
1157 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001158{
1159 const char *protocol = qdict_get_str(qdict, "protocol");
1160 const char *hostname = qdict_get_str(qdict, "hostname");
1161 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1162 int port = qdict_get_try_int(qdict, "port", -1);
1163 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1164 int ret;
1165
1166 if (strcmp(protocol, "spice") == 0) {
1167 if (!using_spice) {
1168 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1169 return -1;
1170 }
1171
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001172 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1173 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001174 if (ret != 0) {
1175 qerror_report(QERR_UNDEFINED_ERROR);
1176 return -1;
1177 }
1178 return 0;
1179 }
1180
1181 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1182 return -1;
1183}
1184
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001185static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001186{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001188 return 0;
bellard59a983b2004-03-17 23:17:16 +00001189}
1190
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001191static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001192{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001194}
1195
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001196static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001197{
1198 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001199 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001200
bellard9307c4c2004-04-04 12:57:25 +00001201 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001202 mask = 0;
1203 } else {
bellard9307c4c2004-04-04 12:57:25 +00001204 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001205 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001206 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001207 return;
1208 }
1209 }
1210 cpu_set_log(mask);
1211}
1212
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001213static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001214{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001215 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001216 if (!option || !strcmp(option, "on")) {
1217 singlestep = 1;
1218 } else if (!strcmp(option, "off")) {
1219 singlestep = 0;
1220 } else {
1221 monitor_printf(mon, "unexpected option %s\n", option);
1222 }
1223}
1224
aliguoribb5fc202009-03-05 23:01:15 +00001225static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001226
aliguori376253e2009-03-05 23:01:23 +00001227struct bdrv_iterate_context {
1228 Monitor *mon;
1229 int err;
1230};
aliguoric0f4ce72009-03-05 23:01:01 +00001231
Luiz Capitulino28a72822011-09-26 17:43:50 -03001232static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1233{
1234 bdrv_iostatus_reset(bs);
1235}
1236
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001237/**
1238 * do_cont(): Resume emulation.
1239 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001240static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001241{
1242 struct bdrv_iterate_context context = { mon, 0 };
1243
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001244 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301245 qerror_report(QERR_MIGRATION_EXPECTED);
1246 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001247 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1248 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001249 qerror_report(QERR_RESET_REQUIRED);
1250 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301251 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001252
Luiz Capitulino28a72822011-09-26 17:43:50 -03001253 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001254 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001255 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001256 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001257 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001258 return 0;
1259 } else {
1260 return -1;
1261 }
bellard8a7ddc32004-03-31 19:00:16 +00001262}
1263
aliguoribb5fc202009-03-05 23:01:15 +00001264static void bdrv_key_cb(void *opaque, int err)
1265{
aliguori376253e2009-03-05 23:01:23 +00001266 Monitor *mon = opaque;
1267
aliguoribb5fc202009-03-05 23:01:15 +00001268 /* another key was set successfully, retry to continue */
1269 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001270 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001271}
1272
1273static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1274{
aliguori376253e2009-03-05 23:01:23 +00001275 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001276
aliguori376253e2009-03-05 23:01:23 +00001277 if (!context->err && bdrv_key_required(bs)) {
1278 context->err = -EBUSY;
1279 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1280 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001281 }
1282}
1283
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001284static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001285{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001286 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001287 if (!device)
1288 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1289 if (gdbserver_start(device) < 0) {
1290 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1291 device);
1292 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001293 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001294 } else {
aliguori59030a82009-04-05 18:43:41 +00001295 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1296 device);
bellard8a7ddc32004-03-31 19:00:16 +00001297 }
1298}
1299
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001300static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001301{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001302 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001303 if (select_watchdog_action(action) == -1) {
1304 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1305 }
1306}
1307
aliguori376253e2009-03-05 23:01:23 +00001308static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001309{
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001311 switch(c) {
1312 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001317 break;
1318 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001320 break;
1321 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001322 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001323 break;
1324 default:
1325 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001327 } else {
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001329 }
1330 break;
1331 }
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001333}
1334
aliguori376253e2009-03-05 23:01:23 +00001335static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001336 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001337{
bellard6a00d602005-11-21 23:25:50 +00001338 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001339 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001340 uint8_t buf[16];
1341 uint64_t v;
1342
1343 if (format == 'i') {
1344 int flags;
1345 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001346 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001347#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001348 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001349 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001350 } else if (wsize == 4) {
1351 flags = 0;
1352 } else {
bellard6a15fd12006-04-12 21:07:07 +00001353 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001354 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001355 if (env) {
1356#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001357 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001358 (env->segs[R_CS].flags & DESC_L_MASK))
1359 flags = 2;
1360 else
1361#endif
1362 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1363 flags = 1;
1364 }
bellard4c27ba22004-04-25 18:05:08 +00001365 }
1366#endif
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001368 return;
1369 }
1370
1371 len = wsize * count;
1372 if (wsize == 1)
1373 line_size = 8;
1374 else
1375 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001376 max_digits = 0;
1377
1378 switch(format) {
1379 case 'o':
1380 max_digits = (wsize * 8 + 2) / 3;
1381 break;
1382 default:
1383 case 'x':
1384 max_digits = (wsize * 8) / 4;
1385 break;
1386 case 'u':
1387 case 'd':
1388 max_digits = (wsize * 8 * 10 + 32) / 33;
1389 break;
1390 case 'c':
1391 wsize = 1;
1392 break;
1393 }
1394
1395 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001396 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001398 else
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001400 l = len;
1401 if (l > line_size)
1402 l = line_size;
1403 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001404 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001405 } else {
bellard6a00d602005-11-21 23:25:50 +00001406 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001407 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001409 break;
1410 }
bellard9307c4c2004-04-04 12:57:25 +00001411 }
ths5fafdf22007-09-16 21:08:06 +00001412 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001413 while (i < l) {
1414 switch(wsize) {
1415 default:
1416 case 1:
1417 v = ldub_raw(buf + i);
1418 break;
1419 case 2:
1420 v = lduw_raw(buf + i);
1421 break;
1422 case 4:
bellard92a31b12005-02-10 22:00:52 +00001423 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001424 break;
1425 case 8:
1426 v = ldq_raw(buf + i);
1427 break;
1428 }
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001430 switch(format) {
1431 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001432 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001433 break;
1434 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001436 break;
1437 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001439 break;
1440 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001441 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001442 break;
1443 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001444 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001445 break;
1446 }
1447 i += wsize;
1448 }
aliguori376253e2009-03-05 23:01:23 +00001449 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001450 addr += l;
1451 len -= l;
1452 }
1453}
1454
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001455static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001456{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001457 int count = qdict_get_int(qdict, "count");
1458 int format = qdict_get_int(qdict, "format");
1459 int size = qdict_get_int(qdict, "size");
1460 target_long addr = qdict_get_int(qdict, "addr");
1461
aliguori376253e2009-03-05 23:01:23 +00001462 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001463}
1464
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001465static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001466{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001467 int count = qdict_get_int(qdict, "count");
1468 int format = qdict_get_int(qdict, "format");
1469 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001470 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001471
aliguori376253e2009-03-05 23:01:23 +00001472 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001473}
1474
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001475static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001476{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001477 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001478 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001479
blueswir17743e582007-09-24 18:39:04 +00001480#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001481 switch(format) {
1482 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001483 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001484 break;
1485 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001486 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001487 break;
1488 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001489 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001490 break;
1491 default:
1492 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001493 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001494 break;
1495 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001497 break;
1498 }
bellard92a31b12005-02-10 22:00:52 +00001499#else
1500 switch(format) {
1501 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001503 break;
1504 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001506 break;
1507 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001509 break;
1510 default:
1511 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001513 break;
1514 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001515 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001516 break;
1517 }
1518#endif
aliguori376253e2009-03-05 23:01:23 +00001519 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001520}
1521
Luiz Capitulino98696222010-02-10 23:49:58 -02001522static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001523{
1524 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001525 uint32_t size = qdict_get_int(qdict, "size");
1526 const char *filename = qdict_get_str(qdict, "filename");
1527 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001528 uint32_t l;
1529 CPUState *env;
1530 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001531 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001532
1533 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001534
1535 f = fopen(filename, "wb");
1536 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001537 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001538 return -1;
bellardb371dc52007-01-03 15:20:39 +00001539 }
1540 while (size != 0) {
1541 l = sizeof(buf);
1542 if (l > size)
1543 l = size;
1544 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001545 if (fwrite(buf, 1, l, f) != l) {
1546 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1547 goto exit;
1548 }
bellardb371dc52007-01-03 15:20:39 +00001549 addr += l;
1550 size -= l;
1551 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001552
1553 ret = 0;
1554
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001555exit:
bellardb371dc52007-01-03 15:20:39 +00001556 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001557 return ret;
bellardb371dc52007-01-03 15:20:39 +00001558}
1559
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001560static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001561 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001562{
1563 FILE *f;
1564 uint32_t l;
1565 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001566 uint32_t size = qdict_get_int(qdict, "size");
1567 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001568 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001569 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001570
1571 f = fopen(filename, "wb");
1572 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001573 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001574 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001575 }
1576 while (size != 0) {
1577 l = sizeof(buf);
1578 if (l > size)
1579 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001580 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001581 if (fwrite(buf, 1, l, f) != l) {
1582 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1583 goto exit;
1584 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001585 fflush(f);
1586 addr += l;
1587 size -= l;
1588 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001589
1590 ret = 0;
1591
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001592exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001593 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001594 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001595}
1596
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001597static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001598{
1599 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001600 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001601 uint32_t start = qdict_get_int(qdict, "start");
1602 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001603
1604 sum = 0;
1605 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001606 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001607 /* BSD sum algorithm ('sum' Unix command) */
1608 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001609 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001610 }
aliguori376253e2009-03-05 23:01:23 +00001611 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001612}
1613
bellarda3a91a32004-06-04 11:06:21 +00001614typedef struct {
1615 int keycode;
1616 const char *name;
1617} KeyDef;
1618
1619static const KeyDef key_defs[] = {
1620 { 0x2a, "shift" },
1621 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001622
bellarda3a91a32004-06-04 11:06:21 +00001623 { 0x38, "alt" },
1624 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001625 { 0x64, "altgr" },
1626 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001627 { 0x1d, "ctrl" },
1628 { 0x9d, "ctrl_r" },
1629
1630 { 0xdd, "menu" },
1631
1632 { 0x01, "esc" },
1633
1634 { 0x02, "1" },
1635 { 0x03, "2" },
1636 { 0x04, "3" },
1637 { 0x05, "4" },
1638 { 0x06, "5" },
1639 { 0x07, "6" },
1640 { 0x08, "7" },
1641 { 0x09, "8" },
1642 { 0x0a, "9" },
1643 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001644 { 0x0c, "minus" },
1645 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001646 { 0x0e, "backspace" },
1647
1648 { 0x0f, "tab" },
1649 { 0x10, "q" },
1650 { 0x11, "w" },
1651 { 0x12, "e" },
1652 { 0x13, "r" },
1653 { 0x14, "t" },
1654 { 0x15, "y" },
1655 { 0x16, "u" },
1656 { 0x17, "i" },
1657 { 0x18, "o" },
1658 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001659 { 0x1a, "bracket_left" },
1660 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001661 { 0x1c, "ret" },
1662
1663 { 0x1e, "a" },
1664 { 0x1f, "s" },
1665 { 0x20, "d" },
1666 { 0x21, "f" },
1667 { 0x22, "g" },
1668 { 0x23, "h" },
1669 { 0x24, "j" },
1670 { 0x25, "k" },
1671 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001672 { 0x27, "semicolon" },
1673 { 0x28, "apostrophe" },
1674 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001675
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001676 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001677 { 0x2c, "z" },
1678 { 0x2d, "x" },
1679 { 0x2e, "c" },
1680 { 0x2f, "v" },
1681 { 0x30, "b" },
1682 { 0x31, "n" },
1683 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001684 { 0x33, "comma" },
1685 { 0x34, "dot" },
1686 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001687
balrog4d3b6f62008-02-10 16:33:14 +00001688 { 0x37, "asterisk" },
1689
bellarda3a91a32004-06-04 11:06:21 +00001690 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001691 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001692 { 0x3b, "f1" },
1693 { 0x3c, "f2" },
1694 { 0x3d, "f3" },
1695 { 0x3e, "f4" },
1696 { 0x3f, "f5" },
1697 { 0x40, "f6" },
1698 { 0x41, "f7" },
1699 { 0x42, "f8" },
1700 { 0x43, "f9" },
1701 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001702 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001703 { 0x46, "scroll_lock" },
1704
bellard64866c32006-05-07 18:03:31 +00001705 { 0xb5, "kp_divide" },
1706 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001707 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001708 { 0x4e, "kp_add" },
1709 { 0x9c, "kp_enter" },
1710 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001711 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001712
1713 { 0x52, "kp_0" },
1714 { 0x4f, "kp_1" },
1715 { 0x50, "kp_2" },
1716 { 0x51, "kp_3" },
1717 { 0x4b, "kp_4" },
1718 { 0x4c, "kp_5" },
1719 { 0x4d, "kp_6" },
1720 { 0x47, "kp_7" },
1721 { 0x48, "kp_8" },
1722 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001723
bellarda3a91a32004-06-04 11:06:21 +00001724 { 0x56, "<" },
1725
1726 { 0x57, "f11" },
1727 { 0x58, "f12" },
1728
1729 { 0xb7, "print" },
1730
1731 { 0xc7, "home" },
1732 { 0xc9, "pgup" },
1733 { 0xd1, "pgdn" },
1734 { 0xcf, "end" },
1735
1736 { 0xcb, "left" },
1737 { 0xc8, "up" },
1738 { 0xd0, "down" },
1739 { 0xcd, "right" },
1740
1741 { 0xd2, "insert" },
1742 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001743#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1744 { 0xf0, "stop" },
1745 { 0xf1, "again" },
1746 { 0xf2, "props" },
1747 { 0xf3, "undo" },
1748 { 0xf4, "front" },
1749 { 0xf5, "copy" },
1750 { 0xf6, "open" },
1751 { 0xf7, "paste" },
1752 { 0xf8, "find" },
1753 { 0xf9, "cut" },
1754 { 0xfa, "lf" },
1755 { 0xfb, "help" },
1756 { 0xfc, "meta_l" },
1757 { 0xfd, "meta_r" },
1758 { 0xfe, "compose" },
1759#endif
bellarda3a91a32004-06-04 11:06:21 +00001760 { 0, NULL },
1761};
1762
1763static int get_keycode(const char *key)
1764{
1765 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001766 char *endp;
1767 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001768
1769 for(p = key_defs; p->name != NULL; p++) {
1770 if (!strcmp(key, p->name))
1771 return p->keycode;
1772 }
bellard64866c32006-05-07 18:03:31 +00001773 if (strstart(key, "0x", NULL)) {
1774 ret = strtoul(key, &endp, 0);
1775 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1776 return ret;
1777 }
bellarda3a91a32004-06-04 11:06:21 +00001778 return -1;
1779}
1780
balrogc8256f92008-06-08 22:45:01 +00001781#define MAX_KEYCODES 16
1782static uint8_t keycodes[MAX_KEYCODES];
1783static int nb_pending_keycodes;
1784static QEMUTimer *key_timer;
1785
1786static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001787{
balrogc8256f92008-06-08 22:45:01 +00001788 int keycode;
1789
1790 while (nb_pending_keycodes > 0) {
1791 nb_pending_keycodes--;
1792 keycode = keycodes[nb_pending_keycodes];
1793 if (keycode & 0x80)
1794 kbd_put_keycode(0xe0);
1795 kbd_put_keycode(keycode | 0x80);
1796 }
1797}
1798
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001799static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001800{
balrog3401c0d2008-06-04 10:05:59 +00001801 char keyname_buf[16];
1802 char *separator;
1803 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001804 const char *string = qdict_get_str(qdict, "string");
1805 int has_hold_time = qdict_haskey(qdict, "hold_time");
1806 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001807
balrogc8256f92008-06-08 22:45:01 +00001808 if (nb_pending_keycodes > 0) {
1809 qemu_del_timer(key_timer);
1810 release_keys(NULL);
1811 }
1812 if (!has_hold_time)
1813 hold_time = 100;
1814 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001815 while (1) {
1816 separator = strchr(string, '-');
1817 keyname_len = separator ? separator - string : strlen(string);
1818 if (keyname_len > 0) {
1819 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1820 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001821 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001822 return;
bellarda3a91a32004-06-04 11:06:21 +00001823 }
balrogc8256f92008-06-08 22:45:01 +00001824 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001825 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001826 return;
1827 }
1828 keyname_buf[keyname_len] = 0;
1829 keycode = get_keycode(keyname_buf);
1830 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001831 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001832 return;
1833 }
balrogc8256f92008-06-08 22:45:01 +00001834 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001835 }
balrog3401c0d2008-06-04 10:05:59 +00001836 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001837 break;
balrog3401c0d2008-06-04 10:05:59 +00001838 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001839 }
balrogc8256f92008-06-08 22:45:01 +00001840 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001841 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001842 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001843 keycode = keycodes[i];
1844 if (keycode & 0x80)
1845 kbd_put_keycode(0xe0);
1846 kbd_put_keycode(keycode & 0x7f);
1847 }
balrogc8256f92008-06-08 22:45:01 +00001848 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001849 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001850 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001851}
1852
bellard13224a82006-07-14 22:03:35 +00001853static int mouse_button_state;
1854
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001855static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001856{
1857 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001858 const char *dx_str = qdict_get_str(qdict, "dx_str");
1859 const char *dy_str = qdict_get_str(qdict, "dy_str");
1860 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001861 dx = strtol(dx_str, NULL, 0);
1862 dy = strtol(dy_str, NULL, 0);
1863 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001864 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001865 dz = strtol(dz_str, NULL, 0);
1866 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1867}
1868
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001869static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001870{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001871 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001872 mouse_button_state = button_state;
1873 kbd_mouse_event(0, 0, 0, mouse_button_state);
1874}
1875
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001876static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001877{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001878 int size = qdict_get_int(qdict, "size");
1879 int addr = qdict_get_int(qdict, "addr");
1880 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001881 uint32_t val;
1882 int suffix;
1883
1884 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001885 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001886 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001887 addr++;
1888 }
1889 addr &= 0xffff;
1890
1891 switch(size) {
1892 default:
1893 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001894 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001895 suffix = 'b';
1896 break;
1897 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001898 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001899 suffix = 'w';
1900 break;
1901 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001902 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001903 suffix = 'l';
1904 break;
1905 }
aliguori376253e2009-03-05 23:01:23 +00001906 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1907 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001908}
bellarda3a91a32004-06-04 11:06:21 +00001909
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001910static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001911{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001912 int size = qdict_get_int(qdict, "size");
1913 int addr = qdict_get_int(qdict, "addr");
1914 int val = qdict_get_int(qdict, "val");
1915
Jan Kiszkaf1147842009-07-14 10:20:11 +02001916 addr &= IOPORTS_MASK;
1917
1918 switch (size) {
1919 default:
1920 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001921 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001922 break;
1923 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001924 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001925 break;
1926 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001927 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001928 break;
1929 }
1930}
1931
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001932static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001933{
1934 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001935 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001936
Jan Kiszka76e30d02009-07-02 00:19:02 +02001937 res = qemu_boot_set(bootdevice);
1938 if (res == 0) {
1939 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1940 } else if (res > 0) {
1941 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001942 } else {
aliguori376253e2009-03-05 23:01:23 +00001943 monitor_printf(mon, "no function defined to set boot device list for "
1944 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001945 }
1946}
1947
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001948/**
Luiz Capitulino43076662009-10-07 13:41:59 -03001949 * do_system_powerdown(): Issue a machine powerdown
1950 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001951static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1952 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001953{
1954 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001955 return 0;
bellard34751872005-07-02 14:31:34 +00001956}
1957
bellardb86bda52004-09-18 19:32:46 +00001958#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001959static void print_pte(Monitor *mon, target_phys_addr_t addr,
1960 target_phys_addr_t pte,
1961 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001962{
Blue Swirld65aaf32010-12-11 18:56:24 +00001963#ifdef TARGET_X86_64
1964 if (addr & (1ULL << 47)) {
1965 addr |= -1LL << 48;
1966 }
1967#endif
1968 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1969 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001970 addr,
1971 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001972 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001973 pte & PG_GLOBAL_MASK ? 'G' : '-',
1974 pte & PG_PSE_MASK ? 'P' : '-',
1975 pte & PG_DIRTY_MASK ? 'D' : '-',
1976 pte & PG_ACCESSED_MASK ? 'A' : '-',
1977 pte & PG_PCD_MASK ? 'C' : '-',
1978 pte & PG_PWT_MASK ? 'T' : '-',
1979 pte & PG_USER_MASK ? 'U' : '-',
1980 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001981}
1982
Blue Swirld65aaf32010-12-11 18:56:24 +00001983static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001984{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001985 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001986 uint32_t pgd, pde, pte;
1987
bellardb86bda52004-09-18 19:32:46 +00001988 pgd = env->cr[3] & ~0xfff;
1989 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001990 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001991 pde = le32_to_cpu(pde);
1992 if (pde & PG_PRESENT_MASK) {
1993 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001994 /* 4M pages */
1995 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001996 } else {
1997 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001998 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001999 pte = le32_to_cpu(pte);
2000 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002001 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002002 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002003 ~0xfff);
2004 }
2005 }
2006 }
2007 }
2008 }
2009}
2010
Blue Swirld65aaf32010-12-11 18:56:24 +00002011static void tlb_info_pae32(Monitor *mon, CPUState *env)
2012{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002013 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002014 uint64_t pdpe, pde, pte;
2015 uint64_t pdp_addr, pd_addr, pt_addr;
2016
2017 pdp_addr = env->cr[3] & ~0x1f;
2018 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002019 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002020 pdpe = le64_to_cpu(pdpe);
2021 if (pdpe & PG_PRESENT_MASK) {
2022 pd_addr = pdpe & 0x3fffffffff000ULL;
2023 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002024 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002025 pde = le64_to_cpu(pde);
2026 if (pde & PG_PRESENT_MASK) {
2027 if (pde & PG_PSE_MASK) {
2028 /* 2M pages with PAE, CR4.PSE is ignored */
2029 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2030 ~((target_phys_addr_t)(1 << 20) - 1));
2031 } else {
2032 pt_addr = pde & 0x3fffffffff000ULL;
2033 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002034 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002035 pte = le64_to_cpu(pte);
2036 if (pte & PG_PRESENT_MASK) {
2037 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2038 + (l3 << 12),
2039 pte & ~PG_PSE_MASK,
2040 ~(target_phys_addr_t)0xfff);
2041 }
2042 }
2043 }
2044 }
2045 }
2046 }
2047 }
2048}
2049
2050#ifdef TARGET_X86_64
2051static void tlb_info_64(Monitor *mon, CPUState *env)
2052{
2053 uint64_t l1, l2, l3, l4;
2054 uint64_t pml4e, pdpe, pde, pte;
2055 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2056
2057 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2058 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002059 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002060 pml4e = le64_to_cpu(pml4e);
2061 if (pml4e & PG_PRESENT_MASK) {
2062 pdp_addr = pml4e & 0x3fffffffff000ULL;
2063 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002064 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002065 pdpe = le64_to_cpu(pdpe);
2066 if (pdpe & PG_PRESENT_MASK) {
2067 if (pdpe & PG_PSE_MASK) {
2068 /* 1G pages, CR4.PSE is ignored */
2069 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2070 0x3ffffc0000000ULL);
2071 } else {
2072 pd_addr = pdpe & 0x3fffffffff000ULL;
2073 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002074 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002075 pde = le64_to_cpu(pde);
2076 if (pde & PG_PRESENT_MASK) {
2077 if (pde & PG_PSE_MASK) {
2078 /* 2M pages, CR4.PSE is ignored */
2079 print_pte(mon, (l1 << 39) + (l2 << 30) +
2080 (l3 << 21), pde,
2081 0x3ffffffe00000ULL);
2082 } else {
2083 pt_addr = pde & 0x3fffffffff000ULL;
2084 for (l4 = 0; l4 < 512; l4++) {
2085 cpu_physical_memory_read(pt_addr
2086 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002087 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002088 pte = le64_to_cpu(pte);
2089 if (pte & PG_PRESENT_MASK) {
2090 print_pte(mon, (l1 << 39) +
2091 (l2 << 30) +
2092 (l3 << 21) + (l4 << 12),
2093 pte & ~PG_PSE_MASK,
2094 0x3fffffffff000ULL);
2095 }
2096 }
2097 }
2098 }
2099 }
2100 }
2101 }
2102 }
2103 }
2104 }
2105}
2106#endif
2107
2108static void tlb_info(Monitor *mon)
2109{
2110 CPUState *env;
2111
2112 env = mon_get_cpu();
2113
2114 if (!(env->cr[0] & CR0_PG_MASK)) {
2115 monitor_printf(mon, "PG disabled\n");
2116 return;
2117 }
2118 if (env->cr[4] & CR4_PAE_MASK) {
2119#ifdef TARGET_X86_64
2120 if (env->hflags & HF_LMA_MASK) {
2121 tlb_info_64(mon, env);
2122 } else
2123#endif
2124 {
2125 tlb_info_pae32(mon, env);
2126 }
2127 } else {
2128 tlb_info_32(mon, env);
2129 }
2130}
2131
Blue Swirl1b3cba62010-12-11 18:56:27 +00002132static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2133 int *plast_prot,
2134 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002135{
bellard9746b152004-11-11 18:30:24 +00002136 int prot1;
2137 prot1 = *plast_prot;
2138 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002139 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002140 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2141 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002142 *pstart, end, end - *pstart,
2143 prot1 & PG_USER_MASK ? 'u' : '-',
2144 'r',
2145 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002146 }
2147 if (prot != 0)
2148 *pstart = end;
2149 else
2150 *pstart = -1;
2151 *plast_prot = prot;
2152 }
2153}
2154
Blue Swirl1b3cba62010-12-11 18:56:27 +00002155static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002156{
Austin Clementsb49ca722011-08-14 23:19:21 -04002157 unsigned int l1, l2;
2158 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002159 uint32_t pgd, pde, pte;
2160 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002161
bellardb86bda52004-09-18 19:32:46 +00002162 pgd = env->cr[3] & ~0xfff;
2163 last_prot = 0;
2164 start = -1;
2165 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002166 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002167 pde = le32_to_cpu(pde);
2168 end = l1 << 22;
2169 if (pde & PG_PRESENT_MASK) {
2170 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2171 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002172 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002173 } else {
2174 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002175 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002176 pte = le32_to_cpu(pte);
2177 end = (l1 << 22) + (l2 << 12);
2178 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002179 prot = pte & pde &
2180 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002181 } else {
2182 prot = 0;
2183 }
aliguori376253e2009-03-05 23:01:23 +00002184 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002185 }
2186 }
2187 } else {
2188 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002189 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002190 }
2191 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002192 /* Flush last range */
2193 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002194}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002195
2196static void mem_info_pae32(Monitor *mon, CPUState *env)
2197{
Austin Clementsb49ca722011-08-14 23:19:21 -04002198 unsigned int l1, l2, l3;
2199 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002200 uint64_t pdpe, pde, pte;
2201 uint64_t pdp_addr, pd_addr, pt_addr;
2202 target_phys_addr_t start, end;
2203
2204 pdp_addr = env->cr[3] & ~0x1f;
2205 last_prot = 0;
2206 start = -1;
2207 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002208 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002209 pdpe = le64_to_cpu(pdpe);
2210 end = l1 << 30;
2211 if (pdpe & PG_PRESENT_MASK) {
2212 pd_addr = pdpe & 0x3fffffffff000ULL;
2213 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002214 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002215 pde = le64_to_cpu(pde);
2216 end = (l1 << 30) + (l2 << 21);
2217 if (pde & PG_PRESENT_MASK) {
2218 if (pde & PG_PSE_MASK) {
2219 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2220 PG_PRESENT_MASK);
2221 mem_print(mon, &start, &last_prot, end, prot);
2222 } else {
2223 pt_addr = pde & 0x3fffffffff000ULL;
2224 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002225 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002226 pte = le64_to_cpu(pte);
2227 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2228 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002229 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2230 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002231 } else {
2232 prot = 0;
2233 }
2234 mem_print(mon, &start, &last_prot, end, prot);
2235 }
2236 }
2237 } else {
2238 prot = 0;
2239 mem_print(mon, &start, &last_prot, end, prot);
2240 }
2241 }
2242 } else {
2243 prot = 0;
2244 mem_print(mon, &start, &last_prot, end, prot);
2245 }
2246 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002247 /* Flush last range */
2248 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002249}
2250
2251
2252#ifdef TARGET_X86_64
2253static void mem_info_64(Monitor *mon, CPUState *env)
2254{
2255 int prot, last_prot;
2256 uint64_t l1, l2, l3, l4;
2257 uint64_t pml4e, pdpe, pde, pte;
2258 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2259
2260 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2261 last_prot = 0;
2262 start = -1;
2263 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002264 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002265 pml4e = le64_to_cpu(pml4e);
2266 end = l1 << 39;
2267 if (pml4e & PG_PRESENT_MASK) {
2268 pdp_addr = pml4e & 0x3fffffffff000ULL;
2269 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002270 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002271 pdpe = le64_to_cpu(pdpe);
2272 end = (l1 << 39) + (l2 << 30);
2273 if (pdpe & PG_PRESENT_MASK) {
2274 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002275 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2276 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002277 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002278 mem_print(mon, &start, &last_prot, end, prot);
2279 } else {
2280 pd_addr = pdpe & 0x3fffffffff000ULL;
2281 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002282 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002283 pde = le64_to_cpu(pde);
2284 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2285 if (pde & PG_PRESENT_MASK) {
2286 if (pde & PG_PSE_MASK) {
2287 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2288 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002289 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002290 mem_print(mon, &start, &last_prot, end, prot);
2291 } else {
2292 pt_addr = pde & 0x3fffffffff000ULL;
2293 for (l4 = 0; l4 < 512; l4++) {
2294 cpu_physical_memory_read(pt_addr
2295 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002296 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002297 pte = le64_to_cpu(pte);
2298 end = (l1 << 39) + (l2 << 30) +
2299 (l3 << 21) + (l4 << 12);
2300 if (pte & PG_PRESENT_MASK) {
2301 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2302 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002303 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002304 } else {
2305 prot = 0;
2306 }
2307 mem_print(mon, &start, &last_prot, end, prot);
2308 }
2309 }
2310 } else {
2311 prot = 0;
2312 mem_print(mon, &start, &last_prot, end, prot);
2313 }
2314 }
2315 }
2316 } else {
2317 prot = 0;
2318 mem_print(mon, &start, &last_prot, end, prot);
2319 }
2320 }
2321 } else {
2322 prot = 0;
2323 mem_print(mon, &start, &last_prot, end, prot);
2324 }
2325 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002326 /* Flush last range */
2327 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002328}
2329#endif
2330
2331static void mem_info(Monitor *mon)
2332{
2333 CPUState *env;
2334
2335 env = mon_get_cpu();
2336
2337 if (!(env->cr[0] & CR0_PG_MASK)) {
2338 monitor_printf(mon, "PG disabled\n");
2339 return;
2340 }
2341 if (env->cr[4] & CR4_PAE_MASK) {
2342#ifdef TARGET_X86_64
2343 if (env->hflags & HF_LMA_MASK) {
2344 mem_info_64(mon, env);
2345 } else
2346#endif
2347 {
2348 mem_info_pae32(mon, env);
2349 }
2350 } else {
2351 mem_info_32(mon, env);
2352 }
2353}
bellardb86bda52004-09-18 19:32:46 +00002354#endif
2355
aurel327c664e22009-03-03 06:12:22 +00002356#if defined(TARGET_SH4)
2357
aliguori376253e2009-03-05 23:01:23 +00002358static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002359{
aliguori376253e2009-03-05 23:01:23 +00002360 monitor_printf(mon, " tlb%i:\t"
2361 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2362 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2363 "dirty=%hhu writethrough=%hhu\n",
2364 idx,
2365 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2366 tlb->v, tlb->sh, tlb->c, tlb->pr,
2367 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002368}
2369
aliguori376253e2009-03-05 23:01:23 +00002370static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002371{
2372 CPUState *env = mon_get_cpu();
2373 int i;
2374
aliguori376253e2009-03-05 23:01:23 +00002375 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002376 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002377 print_tlb (mon, i, &env->itlb[i]);
2378 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002379 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002380 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002381}
2382
2383#endif
2384
Scott Woodbebabbc2011-08-18 10:38:42 +00002385#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002386static void tlb_info(Monitor *mon)
2387{
2388 CPUState *env1 = mon_get_cpu();
2389
2390 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2391}
2392#endif
2393
Blue Swirl314e2982011-09-11 20:22:05 +00002394static void do_info_mtree(Monitor *mon)
2395{
2396 mtree_info((fprintf_function)monitor_printf, mon);
2397}
2398
aliguori030ea372009-04-21 22:30:47 +00002399static void do_info_numa(Monitor *mon)
2400{
aliguorib28b6232009-04-22 20:20:29 +00002401 int i;
aliguori030ea372009-04-21 22:30:47 +00002402 CPUState *env;
2403
2404 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2405 for (i = 0; i < nb_numa_nodes; i++) {
2406 monitor_printf(mon, "node %d cpus:", i);
2407 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2408 if (env->numa_node == i) {
2409 monitor_printf(mon, " %d", env->cpu_index);
2410 }
2411 }
2412 monitor_printf(mon, "\n");
2413 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2414 node_mem[i] >> 20);
2415 }
2416}
2417
bellard5f1ce942006-02-08 22:40:15 +00002418#ifdef CONFIG_PROFILER
2419
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002420int64_t qemu_time;
2421int64_t dev_time;
2422
aliguori376253e2009-03-05 23:01:23 +00002423static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002424{
2425 int64_t total;
2426 total = qemu_time;
2427 if (total == 0)
2428 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002429 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002430 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002431 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002432 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002433 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002434 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002435}
2436#else
aliguori376253e2009-03-05 23:01:23 +00002437static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002438{
aliguori376253e2009-03-05 23:01:23 +00002439 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002440}
2441#endif
2442
bellardec36b692006-07-16 18:57:03 +00002443/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002444static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002445
aliguori376253e2009-03-05 23:01:23 +00002446static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002447{
2448 int i;
2449 CaptureState *s;
2450
2451 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002452 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002453 s->ops.info (s->opaque);
2454 }
2455}
2456
Blue Swirl23130862009-06-06 08:22:04 +00002457#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002458static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002459{
2460 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002461 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002462 CaptureState *s;
2463
2464 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2465 if (i == n) {
2466 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002467 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002468 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002469 return;
2470 }
2471 }
2472}
2473
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002474static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002475{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002476 const char *path = qdict_get_str(qdict, "path");
2477 int has_freq = qdict_haskey(qdict, "freq");
2478 int freq = qdict_get_try_int(qdict, "freq", -1);
2479 int has_bits = qdict_haskey(qdict, "bits");
2480 int bits = qdict_get_try_int(qdict, "bits", -1);
2481 int has_channels = qdict_haskey(qdict, "nchannels");
2482 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002483 CaptureState *s;
2484
Anthony Liguori7267c092011-08-20 22:09:37 -05002485 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002486
2487 freq = has_freq ? freq : 44100;
2488 bits = has_bits ? bits : 16;
2489 nchannels = has_channels ? nchannels : 2;
2490
2491 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002492 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002493 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002494 return;
bellardec36b692006-07-16 18:57:03 +00002495 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002496 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002497}
2498#endif
2499
aurel32dc1c0b72008-04-27 23:52:12 +00002500#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002501static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002502{
2503 CPUState *env;
2504
2505 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2506 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2507 }
2508
2509 return 0;
2510}
2511#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002512static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002513{
2514 qerror_report(QERR_UNSUPPORTED);
2515 return -1;
2516}
aurel32dc1c0b72008-04-27 23:52:12 +00002517#endif
2518
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002519static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002520{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002521 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002522
aliguori76655d62009-03-06 20:27:37 +00002523 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002524 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002525 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002526 return acl;
2527}
aliguori76655d62009-03-06 20:27:37 +00002528
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002529static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002530{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002531 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002532 qemu_acl *acl = find_acl(mon, aclname);
2533 qemu_acl_entry *entry;
2534 int i = 0;
2535
2536 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002537 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002538 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002539 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002540 i++;
2541 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002542 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002543 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002544 }
2545}
2546
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002547static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002548{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002549 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002550 qemu_acl *acl = find_acl(mon, aclname);
2551
2552 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002553 qemu_acl_reset(acl);
2554 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002555 }
2556}
aliguori76655d62009-03-06 20:27:37 +00002557
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002558static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002559{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002560 const char *aclname = qdict_get_str(qdict, "aclname");
2561 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002562 qemu_acl *acl = find_acl(mon, aclname);
2563
2564 if (acl) {
2565 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002566 acl->defaultDeny = 0;
2567 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002568 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002569 acl->defaultDeny = 1;
2570 monitor_printf(mon, "acl: policy set to 'deny'\n");
2571 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002572 monitor_printf(mon, "acl: unknown policy '%s', "
2573 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002574 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002575 }
2576}
aliguori76655d62009-03-06 20:27:37 +00002577
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002578static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002579{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002580 const char *aclname = qdict_get_str(qdict, "aclname");
2581 const char *match = qdict_get_str(qdict, "match");
2582 const char *policy = qdict_get_str(qdict, "policy");
2583 int has_index = qdict_haskey(qdict, "index");
2584 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002585 qemu_acl *acl = find_acl(mon, aclname);
2586 int deny, ret;
2587
2588 if (acl) {
2589 if (strcmp(policy, "allow") == 0) {
2590 deny = 0;
2591 } else if (strcmp(policy, "deny") == 0) {
2592 deny = 1;
2593 } else {
2594 monitor_printf(mon, "acl: unknown policy '%s', "
2595 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002596 return;
2597 }
aliguori28a76be2009-03-06 20:27:40 +00002598 if (has_index)
2599 ret = qemu_acl_insert(acl, deny, match, index);
2600 else
2601 ret = qemu_acl_append(acl, deny, match);
2602 if (ret < 0)
2603 monitor_printf(mon, "acl: unable to add acl entry\n");
2604 else
2605 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002606 }
2607}
aliguori76655d62009-03-06 20:27:37 +00002608
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002609static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002610{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002611 const char *aclname = qdict_get_str(qdict, "aclname");
2612 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002613 qemu_acl *acl = find_acl(mon, aclname);
2614 int ret;
aliguori76655d62009-03-06 20:27:37 +00002615
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002616 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002617 ret = qemu_acl_remove(acl, match);
2618 if (ret < 0)
2619 monitor_printf(mon, "acl: no matching acl entry\n");
2620 else
2621 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002622 }
2623}
2624
Huang Ying79c4f6b2009-06-23 10:05:14 +08002625#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002626static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002627{
2628 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002629 int cpu_index = qdict_get_int(qdict, "cpu_index");
2630 int bank = qdict_get_int(qdict, "bank");
2631 uint64_t status = qdict_get_int(qdict, "status");
2632 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2633 uint64_t addr = qdict_get_int(qdict, "addr");
2634 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002635 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002636
Jan Kiszka747461c2011-03-02 08:56:10 +01002637 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2638 flags |= MCE_INJECT_BROADCAST;
2639 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002640 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002641 if (cenv->cpu_index == cpu_index) {
2642 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002643 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002644 break;
2645 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002646 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002647}
2648#endif
2649
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002650static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002651{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002652 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002653 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002654 int fd;
2655
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002656 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002657 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002658 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002659 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002660 }
2661
2662 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002663 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2664 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002665 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002666 }
2667
Blue Swirl72cf2d42009-09-12 07:36:22 +00002668 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002669 if (strcmp(monfd->name, fdname) != 0) {
2670 continue;
2671 }
2672
2673 close(monfd->fd);
2674 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002675 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002676 }
2677
Anthony Liguori7267c092011-08-20 22:09:37 -05002678 monfd = g_malloc0(sizeof(mon_fd_t));
2679 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002680 monfd->fd = fd;
2681
Blue Swirl72cf2d42009-09-12 07:36:22 +00002682 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002683 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002684}
2685
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002686static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002687{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002688 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002689 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002690
Blue Swirl72cf2d42009-09-12 07:36:22 +00002691 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002692 if (strcmp(monfd->name, fdname) != 0) {
2693 continue;
2694 }
2695
Blue Swirl72cf2d42009-09-12 07:36:22 +00002696 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002697 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002698 g_free(monfd->name);
2699 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002700 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002701 }
2702
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002703 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002704 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002705}
2706
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002707static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002708{
Luiz Capitulino13548692011-07-29 15:36:43 -03002709 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002710 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002711
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002712 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002713
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002714 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002715 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002716 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002717}
2718
Mark McLoughlin7768e042009-07-22 09:11:41 +01002719int monitor_get_fd(Monitor *mon, const char *fdname)
2720{
Anthony Liguoric227f092009-10-01 16:12:16 -05002721 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002722
Blue Swirl72cf2d42009-09-12 07:36:22 +00002723 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002724 int fd;
2725
2726 if (strcmp(monfd->name, fdname) != 0) {
2727 continue;
2728 }
2729
2730 fd = monfd->fd;
2731
2732 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002733 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002734 g_free(monfd->name);
2735 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002736
2737 return fd;
2738 }
2739
2740 return -1;
2741}
2742
Anthony Liguoric227f092009-10-01 16:12:16 -05002743static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002744#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002745 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002746};
2747
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002748/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002749static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 {
2751 .name = "version",
2752 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 .params = "",
2754 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002755 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
2757 {
2758 .name = "network",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002762 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
2764 {
2765 .name = "chardev",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002769 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
2771 {
2772 .name = "block",
2773 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 .params = "",
2775 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002776 .user_print = bdrv_info_print,
2777 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
2779 {
2780 .name = "blockstats",
2781 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002782 .params = "",
2783 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002784 .user_print = bdrv_stats_print,
2785 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002786 },
2787 {
2788 .name = "registers",
2789 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002790 .params = "",
2791 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002792 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002793 },
2794 {
2795 .name = "cpus",
2796 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 .params = "",
2798 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002799 .user_print = monitor_print_cpus,
2800 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 },
2802 {
2803 .name = "history",
2804 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 .params = "",
2806 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002807 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002809#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2810 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 {
2812 .name = "irq",
2813 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 .params = "",
2815 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002816#ifdef TARGET_SPARC
2817 .mhandler.info = sun4m_irq_info,
2818#elif defined(TARGET_LM32)
2819 .mhandler.info = lm32_irq_info,
2820#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002821 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002822#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
2824 {
2825 .name = "pic",
2826 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .params = "",
2828 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002829#ifdef TARGET_SPARC
2830 .mhandler.info = sun4m_pic_info,
2831#elif defined(TARGET_LM32)
2832 .mhandler.info = lm32_do_pic_info,
2833#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002834 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002835#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002836 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002837#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 {
2839 .name = "pci",
2840 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 .params = "",
2842 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002843 .user_print = do_pci_info_print,
2844 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002846#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2847 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 {
2849 .name = "tlb",
2850 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002851 .params = "",
2852 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002853 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002854 },
aurel327c664e22009-03-03 06:12:22 +00002855#endif
2856#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002857 {
2858 .name = "mem",
2859 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002860 .params = "",
2861 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002862 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002863 },
bellardb86bda52004-09-18 19:32:46 +00002864#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002865 {
Blue Swirl314e2982011-09-11 20:22:05 +00002866 .name = "mtree",
2867 .args_type = "",
2868 .params = "",
2869 .help = "show memory tree",
2870 .mhandler.info = do_info_mtree,
2871 },
2872 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002873 .name = "jit",
2874 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 .params = "",
2876 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002877 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 },
2879 {
2880 .name = "kvm",
2881 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002882 .params = "",
2883 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002884 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 },
2886 {
2887 .name = "numa",
2888 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 .params = "",
2890 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002891 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 },
2893 {
2894 .name = "usb",
2895 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 .params = "",
2897 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002898 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 },
2900 {
2901 .name = "usbhost",
2902 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 .params = "",
2904 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002905 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002906 },
2907 {
2908 .name = "profile",
2909 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002910 .params = "",
2911 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002912 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002913 },
2914 {
2915 .name = "capture",
2916 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002917 .params = "",
2918 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002919 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002920 },
2921 {
2922 .name = "snapshots",
2923 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002924 .params = "",
2925 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002926 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002927 },
2928 {
2929 .name = "status",
2930 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002931 .params = "",
2932 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002933 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002934 },
2935 {
2936 .name = "pcmcia",
2937 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002938 .params = "",
2939 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002940 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002941 },
2942 {
2943 .name = "mice",
2944 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002945 .params = "",
2946 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002947 .user_print = do_info_mice_print,
2948 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002949 },
2950 {
2951 .name = "vnc",
2952 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002953 .params = "",
2954 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002955 .user_print = do_info_vnc_print,
2956 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002957 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002958#if defined(CONFIG_SPICE)
2959 {
2960 .name = "spice",
2961 .args_type = "",
2962 .params = "",
2963 .help = "show the spice server status",
2964 .user_print = do_info_spice_print,
2965 .mhandler.info_new = do_info_spice,
2966 },
2967#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002968 {
2969 .name = "name",
2970 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002971 .params = "",
2972 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002973 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002974 },
2975 {
2976 .name = "uuid",
2977 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002978 .params = "",
2979 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002980 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002981 },
j_mayer76a66252007-03-07 08:32:30 +00002982#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002983 {
2984 .name = "cpustats",
2985 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002986 .params = "",
2987 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002988 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002989 },
j_mayer76a66252007-03-07 08:32:30 +00002990#endif
blueswir131a60e22007-10-26 18:42:59 +00002991#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002992 {
2993 .name = "usernet",
2994 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002995 .params = "",
2996 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002997 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002998 },
blueswir131a60e22007-10-26 18:42:59 +00002999#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003000 {
3001 .name = "migrate",
3002 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003003 .params = "",
3004 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003005 .user_print = do_info_migrate_print,
3006 .mhandler.info_new = do_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 = "mice",
3099 .args_type = "",
3100 .params = "",
3101 .help = "show which guest mouse is receiving events",
3102 .user_print = do_info_mice_print,
3103 .mhandler.info_new = do_info_mice,
3104 },
3105 {
3106 .name = "vnc",
3107 .args_type = "",
3108 .params = "",
3109 .help = "show the vnc server status",
3110 .user_print = do_info_vnc_print,
3111 .mhandler.info_new = do_info_vnc,
3112 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003113#if defined(CONFIG_SPICE)
3114 {
3115 .name = "spice",
3116 .args_type = "",
3117 .params = "",
3118 .help = "show the spice server status",
3119 .user_print = do_info_spice_print,
3120 .mhandler.info_new = do_info_spice,
3121 },
3122#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003123 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003124 .name = "migrate",
3125 .args_type = "",
3126 .params = "",
3127 .help = "show migration status",
3128 .user_print = do_info_migrate_print,
3129 .mhandler.info_new = do_info_migrate,
3130 },
3131 {
3132 .name = "balloon",
3133 .args_type = "",
3134 .params = "",
3135 .help = "show balloon information",
3136 .user_print = monitor_print_balloon,
3137 .mhandler.info_async = do_info_balloon,
3138 .flags = MONITOR_CMD_ASYNC,
3139 },
3140 { /* NULL */ },
3141};
3142
bellard9307c4c2004-04-04 12:57:25 +00003143/*******************************************************************/
3144
3145static const char *pch;
3146static jmp_buf expr_env;
3147
bellard92a31b12005-02-10 22:00:52 +00003148#define MD_TLONG 0
3149#define MD_I32 1
3150
bellard9307c4c2004-04-04 12:57:25 +00003151typedef struct MonitorDef {
3152 const char *name;
3153 int offset;
blueswir18662d652008-10-02 18:32:44 +00003154 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003155 int type;
bellard9307c4c2004-04-04 12:57:25 +00003156} MonitorDef;
3157
bellard57206fd2004-04-25 18:54:52 +00003158#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003159static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003160{
bellard6a00d602005-11-21 23:25:50 +00003161 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003162 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003163}
3164#endif
3165
bellarda541f292004-04-12 20:39:29 +00003166#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003167static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003168{
bellard6a00d602005-11-21 23:25:50 +00003169 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003170 unsigned int u;
3171 int i;
3172
3173 u = 0;
3174 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003175 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003176
3177 return u;
3178}
3179
blueswir18662d652008-10-02 18:32:44 +00003180static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003181{
bellard6a00d602005-11-21 23:25:50 +00003182 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003183 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003184}
3185
blueswir18662d652008-10-02 18:32:44 +00003186static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003187{
bellard6a00d602005-11-21 23:25:50 +00003188 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003189 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003190}
bellard9fddaa02004-05-21 12:59:32 +00003191
blueswir18662d652008-10-02 18:32:44 +00003192static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003193{
bellard6a00d602005-11-21 23:25:50 +00003194 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003195 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003196}
3197
blueswir18662d652008-10-02 18:32:44 +00003198static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003199{
bellard6a00d602005-11-21 23:25:50 +00003200 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003201 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003202}
3203
blueswir18662d652008-10-02 18:32:44 +00003204static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003205{
bellard6a00d602005-11-21 23:25:50 +00003206 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003207 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003208}
bellarda541f292004-04-12 20:39:29 +00003209#endif
3210
bellarde95c8d52004-09-30 22:22:08 +00003211#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003212#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003213static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003214{
bellard6a00d602005-11-21 23:25:50 +00003215 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003216
3217 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003218}
bellard7b936c02005-10-30 17:05:13 +00003219#endif
bellarde95c8d52004-09-30 22:22:08 +00003220
blueswir18662d652008-10-02 18:32:44 +00003221static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003222{
bellard6a00d602005-11-21 23:25:50 +00003223 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003224 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003225}
3226#endif
3227
blueswir18662d652008-10-02 18:32:44 +00003228static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003229#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003230
3231#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003232 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003233 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003234 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003235
bellard9307c4c2004-04-04 12:57:25 +00003236 { "eax", offsetof(CPUState, regs[0]) },
3237 { "ecx", offsetof(CPUState, regs[1]) },
3238 { "edx", offsetof(CPUState, regs[2]) },
3239 { "ebx", offsetof(CPUState, regs[3]) },
3240 { "esp|sp", offsetof(CPUState, regs[4]) },
3241 { "ebp|fp", offsetof(CPUState, regs[5]) },
3242 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003243 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003244#ifdef TARGET_X86_64
3245 { "r8", offsetof(CPUState, regs[8]) },
3246 { "r9", offsetof(CPUState, regs[9]) },
3247 { "r10", offsetof(CPUState, regs[10]) },
3248 { "r11", offsetof(CPUState, regs[11]) },
3249 { "r12", offsetof(CPUState, regs[12]) },
3250 { "r13", offsetof(CPUState, regs[13]) },
3251 { "r14", offsetof(CPUState, regs[14]) },
3252 { "r15", offsetof(CPUState, regs[15]) },
3253#endif
bellard9307c4c2004-04-04 12:57:25 +00003254 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003255 { "eip", offsetof(CPUState, eip) },
3256 SEG("cs", R_CS)
3257 SEG("ds", R_DS)
3258 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003259 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003260 SEG("fs", R_FS)
3261 SEG("gs", R_GS)
3262 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003263#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003264 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003265 { "r0", offsetof(CPUState, gpr[0]) },
3266 { "r1", offsetof(CPUState, gpr[1]) },
3267 { "r2", offsetof(CPUState, gpr[2]) },
3268 { "r3", offsetof(CPUState, gpr[3]) },
3269 { "r4", offsetof(CPUState, gpr[4]) },
3270 { "r5", offsetof(CPUState, gpr[5]) },
3271 { "r6", offsetof(CPUState, gpr[6]) },
3272 { "r7", offsetof(CPUState, gpr[7]) },
3273 { "r8", offsetof(CPUState, gpr[8]) },
3274 { "r9", offsetof(CPUState, gpr[9]) },
3275 { "r10", offsetof(CPUState, gpr[10]) },
3276 { "r11", offsetof(CPUState, gpr[11]) },
3277 { "r12", offsetof(CPUState, gpr[12]) },
3278 { "r13", offsetof(CPUState, gpr[13]) },
3279 { "r14", offsetof(CPUState, gpr[14]) },
3280 { "r15", offsetof(CPUState, gpr[15]) },
3281 { "r16", offsetof(CPUState, gpr[16]) },
3282 { "r17", offsetof(CPUState, gpr[17]) },
3283 { "r18", offsetof(CPUState, gpr[18]) },
3284 { "r19", offsetof(CPUState, gpr[19]) },
3285 { "r20", offsetof(CPUState, gpr[20]) },
3286 { "r21", offsetof(CPUState, gpr[21]) },
3287 { "r22", offsetof(CPUState, gpr[22]) },
3288 { "r23", offsetof(CPUState, gpr[23]) },
3289 { "r24", offsetof(CPUState, gpr[24]) },
3290 { "r25", offsetof(CPUState, gpr[25]) },
3291 { "r26", offsetof(CPUState, gpr[26]) },
3292 { "r27", offsetof(CPUState, gpr[27]) },
3293 { "r28", offsetof(CPUState, gpr[28]) },
3294 { "r29", offsetof(CPUState, gpr[29]) },
3295 { "r30", offsetof(CPUState, gpr[30]) },
3296 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003297 /* Floating point registers */
3298 { "f0", offsetof(CPUState, fpr[0]) },
3299 { "f1", offsetof(CPUState, fpr[1]) },
3300 { "f2", offsetof(CPUState, fpr[2]) },
3301 { "f3", offsetof(CPUState, fpr[3]) },
3302 { "f4", offsetof(CPUState, fpr[4]) },
3303 { "f5", offsetof(CPUState, fpr[5]) },
3304 { "f6", offsetof(CPUState, fpr[6]) },
3305 { "f7", offsetof(CPUState, fpr[7]) },
3306 { "f8", offsetof(CPUState, fpr[8]) },
3307 { "f9", offsetof(CPUState, fpr[9]) },
3308 { "f10", offsetof(CPUState, fpr[10]) },
3309 { "f11", offsetof(CPUState, fpr[11]) },
3310 { "f12", offsetof(CPUState, fpr[12]) },
3311 { "f13", offsetof(CPUState, fpr[13]) },
3312 { "f14", offsetof(CPUState, fpr[14]) },
3313 { "f15", offsetof(CPUState, fpr[15]) },
3314 { "f16", offsetof(CPUState, fpr[16]) },
3315 { "f17", offsetof(CPUState, fpr[17]) },
3316 { "f18", offsetof(CPUState, fpr[18]) },
3317 { "f19", offsetof(CPUState, fpr[19]) },
3318 { "f20", offsetof(CPUState, fpr[20]) },
3319 { "f21", offsetof(CPUState, fpr[21]) },
3320 { "f22", offsetof(CPUState, fpr[22]) },
3321 { "f23", offsetof(CPUState, fpr[23]) },
3322 { "f24", offsetof(CPUState, fpr[24]) },
3323 { "f25", offsetof(CPUState, fpr[25]) },
3324 { "f26", offsetof(CPUState, fpr[26]) },
3325 { "f27", offsetof(CPUState, fpr[27]) },
3326 { "f28", offsetof(CPUState, fpr[28]) },
3327 { "f29", offsetof(CPUState, fpr[29]) },
3328 { "f30", offsetof(CPUState, fpr[30]) },
3329 { "f31", offsetof(CPUState, fpr[31]) },
3330 { "fpscr", offsetof(CPUState, fpscr) },
3331 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003332 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003333 { "lr", offsetof(CPUState, lr) },
3334 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003335 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003336 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003337 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003338 { "msr", 0, &monitor_get_msr, },
3339 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003340 { "tbu", 0, &monitor_get_tbu, },
3341 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003342#if defined(TARGET_PPC64)
3343 /* Address space register */
3344 { "asr", offsetof(CPUState, asr) },
3345#endif
3346 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003347 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003348 { "sr0", offsetof(CPUState, sr[0]) },
3349 { "sr1", offsetof(CPUState, sr[1]) },
3350 { "sr2", offsetof(CPUState, sr[2]) },
3351 { "sr3", offsetof(CPUState, sr[3]) },
3352 { "sr4", offsetof(CPUState, sr[4]) },
3353 { "sr5", offsetof(CPUState, sr[5]) },
3354 { "sr6", offsetof(CPUState, sr[6]) },
3355 { "sr7", offsetof(CPUState, sr[7]) },
3356 { "sr8", offsetof(CPUState, sr[8]) },
3357 { "sr9", offsetof(CPUState, sr[9]) },
3358 { "sr10", offsetof(CPUState, sr[10]) },
3359 { "sr11", offsetof(CPUState, sr[11]) },
3360 { "sr12", offsetof(CPUState, sr[12]) },
3361 { "sr13", offsetof(CPUState, sr[13]) },
3362 { "sr14", offsetof(CPUState, sr[14]) },
3363 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003364 /* Too lazy to put BATs... */
3365 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3366
3367 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3368 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3369 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3370 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3371 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3372 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3373 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3374 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3375 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3376 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3377 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3378 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3379 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3380 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3381 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3382 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3383 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3384 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3385 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3386 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3387 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3388 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3389 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3390 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3391 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3392 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3393 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3394 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3395 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3396 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3397 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3398 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3399 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3400 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3401 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3402 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3403 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3404 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3405 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3406 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3407 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3408 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3409 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3410 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3411 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3412 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3413 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3414 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3415 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3416 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3417 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3418 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3419 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3420 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3421 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3422 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3423 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3424 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3425 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3426 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3427 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3428 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3429 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3430 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3431 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3432 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3433
bellarde95c8d52004-09-30 22:22:08 +00003434#elif defined(TARGET_SPARC)
3435 { "g0", offsetof(CPUState, gregs[0]) },
3436 { "g1", offsetof(CPUState, gregs[1]) },
3437 { "g2", offsetof(CPUState, gregs[2]) },
3438 { "g3", offsetof(CPUState, gregs[3]) },
3439 { "g4", offsetof(CPUState, gregs[4]) },
3440 { "g5", offsetof(CPUState, gregs[5]) },
3441 { "g6", offsetof(CPUState, gregs[6]) },
3442 { "g7", offsetof(CPUState, gregs[7]) },
3443 { "o0", 0, monitor_get_reg },
3444 { "o1", 1, monitor_get_reg },
3445 { "o2", 2, monitor_get_reg },
3446 { "o3", 3, monitor_get_reg },
3447 { "o4", 4, monitor_get_reg },
3448 { "o5", 5, monitor_get_reg },
3449 { "o6", 6, monitor_get_reg },
3450 { "o7", 7, monitor_get_reg },
3451 { "l0", 8, monitor_get_reg },
3452 { "l1", 9, monitor_get_reg },
3453 { "l2", 10, monitor_get_reg },
3454 { "l3", 11, monitor_get_reg },
3455 { "l4", 12, monitor_get_reg },
3456 { "l5", 13, monitor_get_reg },
3457 { "l6", 14, monitor_get_reg },
3458 { "l7", 15, monitor_get_reg },
3459 { "i0", 16, monitor_get_reg },
3460 { "i1", 17, monitor_get_reg },
3461 { "i2", 18, monitor_get_reg },
3462 { "i3", 19, monitor_get_reg },
3463 { "i4", 20, monitor_get_reg },
3464 { "i5", 21, monitor_get_reg },
3465 { "i6", 22, monitor_get_reg },
3466 { "i7", 23, monitor_get_reg },
3467 { "pc", offsetof(CPUState, pc) },
3468 { "npc", offsetof(CPUState, npc) },
3469 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003470#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003471 { "psr", 0, &monitor_get_psr, },
3472 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003473#endif
bellarde95c8d52004-09-30 22:22:08 +00003474 { "tbr", offsetof(CPUState, tbr) },
3475 { "fsr", offsetof(CPUState, fsr) },
3476 { "f0", offsetof(CPUState, fpr[0]) },
3477 { "f1", offsetof(CPUState, fpr[1]) },
3478 { "f2", offsetof(CPUState, fpr[2]) },
3479 { "f3", offsetof(CPUState, fpr[3]) },
3480 { "f4", offsetof(CPUState, fpr[4]) },
3481 { "f5", offsetof(CPUState, fpr[5]) },
3482 { "f6", offsetof(CPUState, fpr[6]) },
3483 { "f7", offsetof(CPUState, fpr[7]) },
3484 { "f8", offsetof(CPUState, fpr[8]) },
3485 { "f9", offsetof(CPUState, fpr[9]) },
3486 { "f10", offsetof(CPUState, fpr[10]) },
3487 { "f11", offsetof(CPUState, fpr[11]) },
3488 { "f12", offsetof(CPUState, fpr[12]) },
3489 { "f13", offsetof(CPUState, fpr[13]) },
3490 { "f14", offsetof(CPUState, fpr[14]) },
3491 { "f15", offsetof(CPUState, fpr[15]) },
3492 { "f16", offsetof(CPUState, fpr[16]) },
3493 { "f17", offsetof(CPUState, fpr[17]) },
3494 { "f18", offsetof(CPUState, fpr[18]) },
3495 { "f19", offsetof(CPUState, fpr[19]) },
3496 { "f20", offsetof(CPUState, fpr[20]) },
3497 { "f21", offsetof(CPUState, fpr[21]) },
3498 { "f22", offsetof(CPUState, fpr[22]) },
3499 { "f23", offsetof(CPUState, fpr[23]) },
3500 { "f24", offsetof(CPUState, fpr[24]) },
3501 { "f25", offsetof(CPUState, fpr[25]) },
3502 { "f26", offsetof(CPUState, fpr[26]) },
3503 { "f27", offsetof(CPUState, fpr[27]) },
3504 { "f28", offsetof(CPUState, fpr[28]) },
3505 { "f29", offsetof(CPUState, fpr[29]) },
3506 { "f30", offsetof(CPUState, fpr[30]) },
3507 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003508#ifdef TARGET_SPARC64
3509 { "f32", offsetof(CPUState, fpr[32]) },
3510 { "f34", offsetof(CPUState, fpr[34]) },
3511 { "f36", offsetof(CPUState, fpr[36]) },
3512 { "f38", offsetof(CPUState, fpr[38]) },
3513 { "f40", offsetof(CPUState, fpr[40]) },
3514 { "f42", offsetof(CPUState, fpr[42]) },
3515 { "f44", offsetof(CPUState, fpr[44]) },
3516 { "f46", offsetof(CPUState, fpr[46]) },
3517 { "f48", offsetof(CPUState, fpr[48]) },
3518 { "f50", offsetof(CPUState, fpr[50]) },
3519 { "f52", offsetof(CPUState, fpr[52]) },
3520 { "f54", offsetof(CPUState, fpr[54]) },
3521 { "f56", offsetof(CPUState, fpr[56]) },
3522 { "f58", offsetof(CPUState, fpr[58]) },
3523 { "f60", offsetof(CPUState, fpr[60]) },
3524 { "f62", offsetof(CPUState, fpr[62]) },
3525 { "asi", offsetof(CPUState, asi) },
3526 { "pstate", offsetof(CPUState, pstate) },
3527 { "cansave", offsetof(CPUState, cansave) },
3528 { "canrestore", offsetof(CPUState, canrestore) },
3529 { "otherwin", offsetof(CPUState, otherwin) },
3530 { "wstate", offsetof(CPUState, wstate) },
3531 { "cleanwin", offsetof(CPUState, cleanwin) },
3532 { "fprs", offsetof(CPUState, fprs) },
3533#endif
bellard9307c4c2004-04-04 12:57:25 +00003534#endif
3535 { NULL },
3536};
3537
aliguori376253e2009-03-05 23:01:23 +00003538static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003539{
aliguori376253e2009-03-05 23:01:23 +00003540 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003541 longjmp(expr_env, 1);
3542}
3543
Markus Armbruster09b94182010-01-20 13:07:30 +01003544/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003545static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003546{
blueswir18662d652008-10-02 18:32:44 +00003547 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003548 void *ptr;
3549
bellard9307c4c2004-04-04 12:57:25 +00003550 for(md = monitor_defs; md->name != NULL; md++) {
3551 if (compare_cmd(name, md->name)) {
3552 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003553 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003554 } else {
bellard6a00d602005-11-21 23:25:50 +00003555 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003556 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003557 switch(md->type) {
3558 case MD_I32:
3559 *pval = *(int32_t *)ptr;
3560 break;
3561 case MD_TLONG:
3562 *pval = *(target_long *)ptr;
3563 break;
3564 default:
3565 *pval = 0;
3566 break;
3567 }
bellard9307c4c2004-04-04 12:57:25 +00003568 }
3569 return 0;
3570 }
3571 }
3572 return -1;
3573}
3574
3575static void next(void)
3576{
Blue Swirl660f11b2009-07-31 21:16:51 +00003577 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003578 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003579 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003580 pch++;
3581 }
3582}
3583
aliguori376253e2009-03-05 23:01:23 +00003584static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003585
aliguori376253e2009-03-05 23:01:23 +00003586static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003587{
blueswir1c2efc952007-09-25 17:28:42 +00003588 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003589 char *p;
bellard6a00d602005-11-21 23:25:50 +00003590 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003591
3592 switch(*pch) {
3593 case '+':
3594 next();
aliguori376253e2009-03-05 23:01:23 +00003595 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003596 break;
3597 case '-':
3598 next();
aliguori376253e2009-03-05 23:01:23 +00003599 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003600 break;
3601 case '~':
3602 next();
aliguori376253e2009-03-05 23:01:23 +00003603 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003604 break;
3605 case '(':
3606 next();
aliguori376253e2009-03-05 23:01:23 +00003607 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003608 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003609 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003610 }
3611 next();
3612 break;
bellard81d09122004-07-14 17:21:37 +00003613 case '\'':
3614 pch++;
3615 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003616 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003617 n = *pch;
3618 pch++;
3619 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003620 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003621 next();
3622 break;
bellard9307c4c2004-04-04 12:57:25 +00003623 case '$':
3624 {
3625 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003626 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003627
bellard9307c4c2004-04-04 12:57:25 +00003628 pch++;
3629 q = buf;
3630 while ((*pch >= 'a' && *pch <= 'z') ||
3631 (*pch >= 'A' && *pch <= 'Z') ||
3632 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003633 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003634 if ((q - buf) < sizeof(buf) - 1)
3635 *q++ = *pch;
3636 pch++;
3637 }
blueswir1cd390082008-11-16 13:53:32 +00003638 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003639 pch++;
3640 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003641 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003642 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003643 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003644 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003645 }
3646 break;
3647 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003648 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003649 n = 0;
3650 break;
3651 default:
blueswir17743e582007-09-24 18:39:04 +00003652#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003653 n = strtoull(pch, &p, 0);
3654#else
bellard9307c4c2004-04-04 12:57:25 +00003655 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003656#endif
bellard9307c4c2004-04-04 12:57:25 +00003657 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003658 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003659 }
3660 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003661 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003662 pch++;
3663 break;
3664 }
3665 return n;
3666}
3667
3668
aliguori376253e2009-03-05 23:01:23 +00003669static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003670{
blueswir1c2efc952007-09-25 17:28:42 +00003671 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003672 int op;
ths3b46e622007-09-17 08:09:54 +00003673
aliguori376253e2009-03-05 23:01:23 +00003674 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003675 for(;;) {
3676 op = *pch;
3677 if (op != '*' && op != '/' && op != '%')
3678 break;
3679 next();
aliguori376253e2009-03-05 23:01:23 +00003680 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003681 switch(op) {
3682 default:
3683 case '*':
3684 val *= val2;
3685 break;
3686 case '/':
3687 case '%':
ths5fafdf22007-09-16 21:08:06 +00003688 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003689 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003690 if (op == '/')
3691 val /= val2;
3692 else
3693 val %= val2;
3694 break;
3695 }
3696 }
3697 return val;
3698}
3699
aliguori376253e2009-03-05 23:01:23 +00003700static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003701{
blueswir1c2efc952007-09-25 17:28:42 +00003702 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003703 int op;
bellard9307c4c2004-04-04 12:57:25 +00003704
aliguori376253e2009-03-05 23:01:23 +00003705 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003706 for(;;) {
3707 op = *pch;
3708 if (op != '&' && op != '|' && op != '^')
3709 break;
3710 next();
aliguori376253e2009-03-05 23:01:23 +00003711 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003712 switch(op) {
3713 default:
3714 case '&':
3715 val &= val2;
3716 break;
3717 case '|':
3718 val |= val2;
3719 break;
3720 case '^':
3721 val ^= val2;
3722 break;
3723 }
3724 }
3725 return val;
3726}
3727
aliguori376253e2009-03-05 23:01:23 +00003728static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003729{
blueswir1c2efc952007-09-25 17:28:42 +00003730 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003731 int op;
bellard9307c4c2004-04-04 12:57:25 +00003732
aliguori376253e2009-03-05 23:01:23 +00003733 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003734 for(;;) {
3735 op = *pch;
3736 if (op != '+' && op != '-')
3737 break;
3738 next();
aliguori376253e2009-03-05 23:01:23 +00003739 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003740 if (op == '+')
3741 val += val2;
3742 else
3743 val -= val2;
3744 }
3745 return val;
3746}
3747
aliguori376253e2009-03-05 23:01:23 +00003748static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003749{
3750 pch = *pp;
3751 if (setjmp(expr_env)) {
3752 *pp = pch;
3753 return -1;
3754 }
blueswir1cd390082008-11-16 13:53:32 +00003755 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003756 pch++;
aliguori376253e2009-03-05 23:01:23 +00003757 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003758 *pp = pch;
3759 return 0;
3760}
3761
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003762static int get_double(Monitor *mon, double *pval, const char **pp)
3763{
3764 const char *p = *pp;
3765 char *tailp;
3766 double d;
3767
3768 d = strtod(p, &tailp);
3769 if (tailp == p) {
3770 monitor_printf(mon, "Number expected\n");
3771 return -1;
3772 }
3773 if (d != d || d - d != 0) {
3774 /* NaN or infinity */
3775 monitor_printf(mon, "Bad number\n");
3776 return -1;
3777 }
3778 *pval = d;
3779 *pp = tailp;
3780 return 0;
3781}
3782
bellard9307c4c2004-04-04 12:57:25 +00003783static int get_str(char *buf, int buf_size, const char **pp)
3784{
3785 const char *p;
3786 char *q;
3787 int c;
3788
bellard81d09122004-07-14 17:21:37 +00003789 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003790 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003791 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003792 p++;
3793 if (*p == '\0') {
3794 fail:
bellard81d09122004-07-14 17:21:37 +00003795 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003796 *pp = p;
3797 return -1;
3798 }
bellard9307c4c2004-04-04 12:57:25 +00003799 if (*p == '\"') {
3800 p++;
3801 while (*p != '\0' && *p != '\"') {
3802 if (*p == '\\') {
3803 p++;
3804 c = *p++;
3805 switch(c) {
3806 case 'n':
3807 c = '\n';
3808 break;
3809 case 'r':
3810 c = '\r';
3811 break;
3812 case '\\':
3813 case '\'':
3814 case '\"':
3815 break;
3816 default:
3817 qemu_printf("unsupported escape code: '\\%c'\n", c);
3818 goto fail;
3819 }
3820 if ((q - buf) < buf_size - 1) {
3821 *q++ = c;
3822 }
3823 } else {
3824 if ((q - buf) < buf_size - 1) {
3825 *q++ = *p;
3826 }
3827 p++;
3828 }
3829 }
3830 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003831 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003832 goto fail;
3833 }
3834 p++;
3835 } else {
blueswir1cd390082008-11-16 13:53:32 +00003836 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003837 if ((q - buf) < buf_size - 1) {
3838 *q++ = *p;
3839 }
3840 p++;
3841 }
bellard9307c4c2004-04-04 12:57:25 +00003842 }
bellard81d09122004-07-14 17:21:37 +00003843 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003844 *pp = p;
3845 return 0;
3846}
3847
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003848/*
3849 * Store the command-name in cmdname, and return a pointer to
3850 * the remaining of the command string.
3851 */
3852static const char *get_command_name(const char *cmdline,
3853 char *cmdname, size_t nlen)
3854{
3855 size_t len;
3856 const char *p, *pstart;
3857
3858 p = cmdline;
3859 while (qemu_isspace(*p))
3860 p++;
3861 if (*p == '\0')
3862 return NULL;
3863 pstart = p;
3864 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3865 p++;
3866 len = p - pstart;
3867 if (len > nlen - 1)
3868 len = nlen - 1;
3869 memcpy(cmdname, pstart, len);
3870 cmdname[len] = '\0';
3871 return p;
3872}
3873
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003874/**
3875 * Read key of 'type' into 'key' and return the current
3876 * 'type' pointer.
3877 */
3878static char *key_get_info(const char *type, char **key)
3879{
3880 size_t len;
3881 char *p, *str;
3882
3883 if (*type == ',')
3884 type++;
3885
3886 p = strchr(type, ':');
3887 if (!p) {
3888 *key = NULL;
3889 return NULL;
3890 }
3891 len = p - type;
3892
Anthony Liguori7267c092011-08-20 22:09:37 -05003893 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003894 memcpy(str, type, len);
3895 str[len] = '\0';
3896
3897 *key = str;
3898 return ++p;
3899}
3900
bellard9307c4c2004-04-04 12:57:25 +00003901static int default_fmt_format = 'x';
3902static int default_fmt_size = 4;
3903
3904#define MAX_ARGS 16
3905
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003906static int is_valid_option(const char *c, const char *typestr)
3907{
3908 char option[3];
3909
3910 option[0] = '-';
3911 option[1] = *c;
3912 option[2] = '\0';
3913
3914 typestr = strstr(typestr, option);
3915 return (typestr != NULL);
3916}
3917
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003918static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3919 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003920{
3921 const mon_cmd_t *cmd;
3922
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003923 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003924 if (compare_cmd(cmdname, cmd->name)) {
3925 return cmd;
3926 }
3927 }
3928
3929 return NULL;
3930}
3931
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003932static const mon_cmd_t *monitor_find_command(const char *cmdname)
3933{
3934 return search_dispatch_table(mon_cmds, cmdname);
3935}
3936
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003937static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3938{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003939 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003940}
3941
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003942static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3943{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003944 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003945}
3946
Anthony Liguoric227f092009-10-01 16:12:16 -05003947static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003948 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003949 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003950{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003951 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003952 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003953 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003954 char cmdname[256];
3955 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003956 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003957
3958#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003959 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003960#endif
ths3b46e622007-09-17 08:09:54 +00003961
bellard9307c4c2004-04-04 12:57:25 +00003962 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003963 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3964 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003965 return NULL;
ths3b46e622007-09-17 08:09:54 +00003966
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003967 cmd = monitor_find_command(cmdname);
3968 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003969 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003970 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003971 }
bellard9307c4c2004-04-04 12:57:25 +00003972
bellard9307c4c2004-04-04 12:57:25 +00003973 /* parse the parameters */
3974 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003975 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003976 typestr = key_get_info(typestr, &key);
3977 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003978 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003979 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003980 typestr++;
3981 switch(c) {
3982 case 'F':
bellard81d09122004-07-14 17:21:37 +00003983 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003984 case 's':
3985 {
3986 int ret;
ths3b46e622007-09-17 08:09:54 +00003987
blueswir1cd390082008-11-16 13:53:32 +00003988 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003989 p++;
3990 if (*typestr == '?') {
3991 typestr++;
3992 if (*p == '\0') {
3993 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003994 break;
bellard9307c4c2004-04-04 12:57:25 +00003995 }
3996 }
3997 ret = get_str(buf, sizeof(buf), &p);
3998 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003999 switch(c) {
4000 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004001 monitor_printf(mon, "%s: filename expected\n",
4002 cmdname);
bellard81d09122004-07-14 17:21:37 +00004003 break;
4004 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004005 monitor_printf(mon, "%s: block device name expected\n",
4006 cmdname);
bellard81d09122004-07-14 17:21:37 +00004007 break;
4008 default:
aliguori376253e2009-03-05 23:01:23 +00004009 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004010 break;
4011 }
bellard9307c4c2004-04-04 12:57:25 +00004012 goto fail;
4013 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004014 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004015 }
4016 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004017 case 'O':
4018 {
4019 QemuOptsList *opts_list;
4020 QemuOpts *opts;
4021
4022 opts_list = qemu_find_opts(key);
4023 if (!opts_list || opts_list->desc->name) {
4024 goto bad_type;
4025 }
4026 while (qemu_isspace(*p)) {
4027 p++;
4028 }
4029 if (!*p)
4030 break;
4031 if (get_str(buf, sizeof(buf), &p) < 0) {
4032 goto fail;
4033 }
4034 opts = qemu_opts_parse(opts_list, buf, 1);
4035 if (!opts) {
4036 goto fail;
4037 }
4038 qemu_opts_to_qdict(opts, qdict);
4039 qemu_opts_del(opts);
4040 }
4041 break;
bellard9307c4c2004-04-04 12:57:25 +00004042 case '/':
4043 {
4044 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004045
blueswir1cd390082008-11-16 13:53:32 +00004046 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004047 p++;
4048 if (*p == '/') {
4049 /* format found */
4050 p++;
4051 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004052 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004053 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004054 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004055 count = count * 10 + (*p - '0');
4056 p++;
4057 }
4058 }
4059 size = -1;
4060 format = -1;
4061 for(;;) {
4062 switch(*p) {
4063 case 'o':
4064 case 'd':
4065 case 'u':
4066 case 'x':
4067 case 'i':
4068 case 'c':
4069 format = *p++;
4070 break;
4071 case 'b':
4072 size = 1;
4073 p++;
4074 break;
4075 case 'h':
4076 size = 2;
4077 p++;
4078 break;
4079 case 'w':
4080 size = 4;
4081 p++;
4082 break;
4083 case 'g':
4084 case 'L':
4085 size = 8;
4086 p++;
4087 break;
4088 default:
4089 goto next;
4090 }
4091 }
4092 next:
blueswir1cd390082008-11-16 13:53:32 +00004093 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004094 monitor_printf(mon, "invalid char in format: '%c'\n",
4095 *p);
bellard9307c4c2004-04-04 12:57:25 +00004096 goto fail;
4097 }
bellard9307c4c2004-04-04 12:57:25 +00004098 if (format < 0)
4099 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004100 if (format != 'i') {
4101 /* for 'i', not specifying a size gives -1 as size */
4102 if (size < 0)
4103 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004104 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004105 }
bellard9307c4c2004-04-04 12:57:25 +00004106 default_fmt_format = format;
4107 } else {
4108 count = 1;
4109 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004110 if (format != 'i') {
4111 size = default_fmt_size;
4112 } else {
4113 size = -1;
4114 }
bellard9307c4c2004-04-04 12:57:25 +00004115 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004116 qdict_put(qdict, "count", qint_from_int(count));
4117 qdict_put(qdict, "format", qint_from_int(format));
4118 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004119 }
4120 break;
4121 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004122 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004123 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004124 {
blueswir1c2efc952007-09-25 17:28:42 +00004125 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004126
blueswir1cd390082008-11-16 13:53:32 +00004127 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004128 p++;
bellard34405572004-06-08 00:55:58 +00004129 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004130 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004131 if (*p == '\0') {
4132 typestr++;
4133 break;
4134 }
bellard34405572004-06-08 00:55:58 +00004135 } else {
4136 if (*p == '.') {
4137 p++;
blueswir1cd390082008-11-16 13:53:32 +00004138 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004139 p++;
bellard34405572004-06-08 00:55:58 +00004140 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004141 typestr++;
4142 break;
bellard34405572004-06-08 00:55:58 +00004143 }
4144 }
bellard13224a82006-07-14 22:03:35 +00004145 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004146 }
aliguori376253e2009-03-05 23:01:23 +00004147 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004148 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004149 /* Check if 'i' is greater than 32-bit */
4150 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4151 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4152 monitor_printf(mon, "integer is for 32-bit values\n");
4153 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004154 } else if (c == 'M') {
4155 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004156 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004157 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004158 }
4159 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004160 case 'o':
4161 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004162 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004163 char *end;
4164
4165 while (qemu_isspace(*p)) {
4166 p++;
4167 }
4168 if (*typestr == '?') {
4169 typestr++;
4170 if (*p == '\0') {
4171 break;
4172 }
4173 }
4174 val = strtosz(p, &end);
4175 if (val < 0) {
4176 monitor_printf(mon, "invalid size\n");
4177 goto fail;
4178 }
4179 qdict_put(qdict, key, qint_from_int(val));
4180 p = end;
4181 }
4182 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004183 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004184 {
4185 double val;
4186
4187 while (qemu_isspace(*p))
4188 p++;
4189 if (*typestr == '?') {
4190 typestr++;
4191 if (*p == '\0') {
4192 break;
4193 }
4194 }
4195 if (get_double(mon, &val, &p) < 0) {
4196 goto fail;
4197 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004198 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004199 switch (*p) {
4200 case 'm':
4201 val /= 1e3; p += 2; break;
4202 case 'u':
4203 val /= 1e6; p += 2; break;
4204 case 'n':
4205 val /= 1e9; p += 2; break;
4206 }
4207 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004208 if (*p && !qemu_isspace(*p)) {
4209 monitor_printf(mon, "Unknown unit suffix\n");
4210 goto fail;
4211 }
4212 qdict_put(qdict, key, qfloat_from_double(val));
4213 }
4214 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004215 case 'b':
4216 {
4217 const char *beg;
4218 int val;
4219
4220 while (qemu_isspace(*p)) {
4221 p++;
4222 }
4223 beg = p;
4224 while (qemu_isgraph(*p)) {
4225 p++;
4226 }
4227 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4228 val = 1;
4229 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4230 val = 0;
4231 } else {
4232 monitor_printf(mon, "Expected 'on' or 'off'\n");
4233 goto fail;
4234 }
4235 qdict_put(qdict, key, qbool_from_int(val));
4236 }
4237 break;
bellard9307c4c2004-04-04 12:57:25 +00004238 case '-':
4239 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004240 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004241 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004242 /* option */
ths3b46e622007-09-17 08:09:54 +00004243
bellard9307c4c2004-04-04 12:57:25 +00004244 c = *typestr++;
4245 if (c == '\0')
4246 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004247 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004248 p++;
bellard9307c4c2004-04-04 12:57:25 +00004249 if (*p == '-') {
4250 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004251 if(c != *p) {
4252 if(!is_valid_option(p, typestr)) {
4253
4254 monitor_printf(mon, "%s: unsupported option -%c\n",
4255 cmdname, *p);
4256 goto fail;
4257 } else {
4258 skip_key = 1;
4259 }
bellard9307c4c2004-04-04 12:57:25 +00004260 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004261 if(skip_key) {
4262 p = tmp;
4263 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004264 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004265 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004266 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004267 }
bellard9307c4c2004-04-04 12:57:25 +00004268 }
bellard9307c4c2004-04-04 12:57:25 +00004269 }
4270 break;
4271 default:
4272 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004273 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004274 goto fail;
4275 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004276 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004277 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004278 }
4279 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004280 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004281 p++;
4282 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004283 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4284 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004285 goto fail;
4286 }
4287
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004288 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004289
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004290fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004291 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004292 return NULL;
4293}
4294
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004295void monitor_set_error(Monitor *mon, QError *qerror)
4296{
4297 /* report only the first error */
4298 if (!mon->error) {
4299 mon->error = qerror;
4300 } else {
4301 MON_DEBUG("Additional error report at %s:%d\n",
4302 qerror->file, qerror->linenr);
4303 QDECREF(qerror);
4304 }
4305}
4306
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004307static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4308{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004309 if (ret && !monitor_has_error(mon)) {
4310 /*
4311 * If it returns failure, it must have passed on error.
4312 *
4313 * Action: Report an internal error to the client if in QMP.
4314 */
4315 qerror_report(QERR_UNDEFINED_ERROR);
4316 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4317 cmd->name);
4318 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004319
4320#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004321 if (!ret && monitor_has_error(mon)) {
4322 /*
4323 * If it returns success, it must not have passed an error.
4324 *
4325 * Action: Report the passed error to the client.
4326 */
4327 MON_DEBUG("command '%s' returned success but passed an error\n",
4328 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004329 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004330
4331 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4332 /*
4333 * Handlers should not call Monitor print functions.
4334 *
4335 * Action: Ignore them in QMP.
4336 *
4337 * (XXX: we don't check any 'info' or 'query' command here
4338 * because the user print function _is_ called by do_info(), hence
4339 * we will trigger this check. This problem will go away when we
4340 * make 'query' commands real and kill do_info())
4341 */
4342 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4343 cmd->name, mon_print_count_get(mon));
4344 }
4345#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004346}
4347
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004348static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004349{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004350 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004351 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004352
4353 qdict = qdict_new();
4354
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004355 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004356 if (!cmd)
4357 goto out;
4358
Luiz Capitulino4903de02010-09-16 11:01:32 -03004359 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004360 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004361 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004362 QObject *data = NULL;
4363
4364 /* XXX: ignores the error code */
4365 cmd->mhandler.cmd_new(mon, qdict, &data);
4366 assert(!monitor_has_error(mon));
4367 if (data) {
4368 cmd->user_print(mon, data);
4369 qobject_decref(data);
4370 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004371 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004372 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004373 }
4374
Luiz Capitulino13917be2009-10-07 13:41:54 -03004375out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004376 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004377}
4378
bellard81d09122004-07-14 17:21:37 +00004379static void cmd_completion(const char *name, const char *list)
4380{
4381 const char *p, *pstart;
4382 char cmd[128];
4383 int len;
4384
4385 p = list;
4386 for(;;) {
4387 pstart = p;
4388 p = strchr(p, '|');
4389 if (!p)
4390 p = pstart + strlen(pstart);
4391 len = p - pstart;
4392 if (len > sizeof(cmd) - 2)
4393 len = sizeof(cmd) - 2;
4394 memcpy(cmd, pstart, len);
4395 cmd[len] = '\0';
4396 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004397 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004398 }
4399 if (*p == '\0')
4400 break;
4401 p++;
4402 }
4403}
4404
4405static void file_completion(const char *input)
4406{
4407 DIR *ffs;
4408 struct dirent *d;
4409 char path[1024];
4410 char file[1024], file_prefix[1024];
4411 int input_path_len;
4412 const char *p;
4413
ths5fafdf22007-09-16 21:08:06 +00004414 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004415 if (!p) {
4416 input_path_len = 0;
4417 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004418 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004419 } else {
4420 input_path_len = p - input + 1;
4421 memcpy(path, input, input_path_len);
4422 if (input_path_len > sizeof(path) - 1)
4423 input_path_len = sizeof(path) - 1;
4424 path[input_path_len] = '\0';
4425 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4426 }
4427#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004428 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4429 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004430#endif
4431 ffs = opendir(path);
4432 if (!ffs)
4433 return;
4434 for(;;) {
4435 struct stat sb;
4436 d = readdir(ffs);
4437 if (!d)
4438 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004439
4440 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4441 continue;
4442 }
4443
bellard81d09122004-07-14 17:21:37 +00004444 if (strstart(d->d_name, file_prefix, NULL)) {
4445 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004446 if (input_path_len < sizeof(file))
4447 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4448 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004449 /* stat the file to find out if it's a directory.
4450 * In that case add a slash to speed up typing long paths
4451 */
4452 stat(file, &sb);
4453 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004454 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004455 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004456 }
4457 }
4458 closedir(ffs);
4459}
4460
aliguori51de9762009-03-05 23:00:43 +00004461static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004462{
aliguori51de9762009-03-05 23:00:43 +00004463 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004464 const char *input = opaque;
4465
4466 if (input[0] == '\0' ||
4467 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004468 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004469 }
4470}
4471
4472/* NOTE: this parser is an approximate form of the real command parser */
4473static void parse_cmdline(const char *cmdline,
4474 int *pnb_args, char **args)
4475{
4476 const char *p;
4477 int nb_args, ret;
4478 char buf[1024];
4479
4480 p = cmdline;
4481 nb_args = 0;
4482 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004483 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004484 p++;
4485 if (*p == '\0')
4486 break;
4487 if (nb_args >= MAX_ARGS)
4488 break;
4489 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004490 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004491 nb_args++;
4492 if (ret < 0)
4493 break;
4494 }
4495 *pnb_args = nb_args;
4496}
4497
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004498static const char *next_arg_type(const char *typestr)
4499{
4500 const char *p = strchr(typestr, ':');
4501 return (p != NULL ? ++p : typestr);
4502}
4503
aliguori4c36ba32009-03-05 23:01:37 +00004504static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004505{
4506 const char *cmdname;
4507 char *args[MAX_ARGS];
4508 int nb_args, i, len;
4509 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004510 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004511 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004512
4513 parse_cmdline(cmdline, &nb_args, args);
4514#ifdef DEBUG_COMPLETION
4515 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004516 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004517 }
4518#endif
4519
4520 /* if the line ends with a space, it means we want to complete the
4521 next arg */
4522 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004523 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004524 if (nb_args >= MAX_ARGS) {
4525 goto cleanup;
4526 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004527 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004528 }
4529 if (nb_args <= 1) {
4530 /* command completion */
4531 if (nb_args == 0)
4532 cmdname = "";
4533 else
4534 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004535 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004536 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004537 cmd_completion(cmdname, cmd->name);
4538 }
4539 } else {
4540 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004541 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4542 if (compare_cmd(args[0], cmd->name)) {
4543 break;
4544 }
bellard81d09122004-07-14 17:21:37 +00004545 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004546 if (!cmd->name) {
4547 goto cleanup;
4548 }
4549
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004550 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004551 for(i = 0; i < nb_args - 2; i++) {
4552 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004553 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004554 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004555 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004556 }
4557 }
4558 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004559 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004560 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004561 }
bellard81d09122004-07-14 17:21:37 +00004562 switch(*ptype) {
4563 case 'F':
4564 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004565 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004566 file_completion(str);
4567 break;
4568 case 'B':
4569 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004570 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004571 bdrv_iterate(block_completion_it, (void *)str);
4572 break;
bellard7fe48482004-10-09 18:08:01 +00004573 case 's':
4574 /* XXX: more generic ? */
4575 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004576 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004577 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4578 cmd_completion(str, cmd->name);
4579 }
bellard64866c32006-05-07 18:03:31 +00004580 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004581 char *sep = strrchr(str, '-');
4582 if (sep)
4583 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004584 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004585 for(key = key_defs; key->name != NULL; key++) {
4586 cmd_completion(str, key->name);
4587 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004588 } else if (!strcmp(cmd->name, "help|?")) {
4589 readline_set_completion_index(cur_mon->rs, strlen(str));
4590 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4591 cmd_completion(str, cmd->name);
4592 }
bellard7fe48482004-10-09 18:08:01 +00004593 }
4594 break;
bellard81d09122004-07-14 17:21:37 +00004595 default:
4596 break;
4597 }
4598 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004599
4600cleanup:
4601 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004602 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004603 }
bellard81d09122004-07-14 17:21:37 +00004604}
4605
aliguori731b0362009-03-05 23:01:42 +00004606static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004607{
aliguori731b0362009-03-05 23:01:42 +00004608 Monitor *mon = opaque;
4609
Jan Kiszkac62313b2009-12-04 14:05:29 +01004610 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004611}
4612
Luiz Capitulino09069b12010-02-04 18:10:06 -02004613static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4614{
4615 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4616 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4617}
4618
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004619/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004620 * Argument validation rules:
4621 *
4622 * 1. The argument must exist in cmd_args qdict
4623 * 2. The argument type must be the expected one
4624 *
4625 * Special case: If the argument doesn't exist in cmd_args and
4626 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4627 * checking is skipped for it.
4628 */
4629static int check_client_args_type(const QDict *client_args,
4630 const QDict *cmd_args, int flags)
4631{
4632 const QDictEntry *ent;
4633
4634 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4635 QObject *obj;
4636 QString *arg_type;
4637 const QObject *client_arg = qdict_entry_value(ent);
4638 const char *client_arg_name = qdict_entry_key(ent);
4639
4640 obj = qdict_get(cmd_args, client_arg_name);
4641 if (!obj) {
4642 if (flags & QMP_ACCEPT_UNKNOWNS) {
4643 /* handler accepts unknowns */
4644 continue;
4645 }
4646 /* client arg doesn't exist */
4647 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4648 return -1;
4649 }
4650
4651 arg_type = qobject_to_qstring(obj);
4652 assert(arg_type != NULL);
4653
4654 /* check if argument's type is correct */
4655 switch (qstring_get_str(arg_type)[0]) {
4656 case 'F':
4657 case 'B':
4658 case 's':
4659 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4660 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4661 "string");
4662 return -1;
4663 }
4664 break;
4665 case 'i':
4666 case 'l':
4667 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004668 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004669 if (qobject_type(client_arg) != QTYPE_QINT) {
4670 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4671 "int");
4672 return -1;
4673 }
4674 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004675 case 'T':
4676 if (qobject_type(client_arg) != QTYPE_QINT &&
4677 qobject_type(client_arg) != QTYPE_QFLOAT) {
4678 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4679 "number");
4680 return -1;
4681 }
4682 break;
4683 case 'b':
4684 case '-':
4685 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4686 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4687 "bool");
4688 return -1;
4689 }
4690 break;
4691 case 'O':
4692 assert(flags & QMP_ACCEPT_UNKNOWNS);
4693 break;
4694 case '/':
4695 case '.':
4696 /*
4697 * These types are not supported by QMP and thus are not
4698 * handled here. Fall through.
4699 */
4700 default:
4701 abort();
4702 }
4703 }
4704
4705 return 0;
4706}
4707
4708/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004709 * - Check if the client has passed all mandatory args
4710 * - Set special flags for argument validation
4711 */
4712static int check_mandatory_args(const QDict *cmd_args,
4713 const QDict *client_args, int *flags)
4714{
4715 const QDictEntry *ent;
4716
4717 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4718 const char *cmd_arg_name = qdict_entry_key(ent);
4719 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4720 assert(type != NULL);
4721
4722 if (qstring_get_str(type)[0] == 'O') {
4723 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4724 *flags |= QMP_ACCEPT_UNKNOWNS;
4725 } else if (qstring_get_str(type)[0] != '-' &&
4726 qstring_get_str(type)[1] != '?' &&
4727 !qdict_haskey(client_args, cmd_arg_name)) {
4728 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4729 return -1;
4730 }
4731 }
4732
4733 return 0;
4734}
4735
4736static QDict *qdict_from_args_type(const char *args_type)
4737{
4738 int i;
4739 QDict *qdict;
4740 QString *key, *type, *cur_qs;
4741
4742 assert(args_type != NULL);
4743
4744 qdict = qdict_new();
4745
4746 if (args_type == NULL || args_type[0] == '\0') {
4747 /* no args, empty qdict */
4748 goto out;
4749 }
4750
4751 key = qstring_new();
4752 type = qstring_new();
4753
4754 cur_qs = key;
4755
4756 for (i = 0;; i++) {
4757 switch (args_type[i]) {
4758 case ',':
4759 case '\0':
4760 qdict_put(qdict, qstring_get_str(key), type);
4761 QDECREF(key);
4762 if (args_type[i] == '\0') {
4763 goto out;
4764 }
4765 type = qstring_new(); /* qdict has ref */
4766 cur_qs = key = qstring_new();
4767 break;
4768 case ':':
4769 cur_qs = type;
4770 break;
4771 default:
4772 qstring_append_chr(cur_qs, args_type[i]);
4773 break;
4774 }
4775 }
4776
4777out:
4778 return qdict;
4779}
4780
4781/*
4782 * Client argument checking rules:
4783 *
4784 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004785 * 2. Each argument provided by the client must be expected
4786 * 3. Each argument provided by the client must have the type expected
4787 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004788 */
4789static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4790{
4791 int flags, err;
4792 QDict *cmd_args;
4793
4794 cmd_args = qdict_from_args_type(cmd->args_type);
4795
4796 flags = 0;
4797 err = check_mandatory_args(cmd_args, client_args, &flags);
4798 if (err) {
4799 goto out;
4800 }
4801
Luiz Capitulino4af91932010-06-22 11:44:05 -03004802 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004803
4804out:
4805 QDECREF(cmd_args);
4806 return err;
4807}
4808
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004809/*
4810 * Input object checking rules
4811 *
4812 * 1. Input object must be a dict
4813 * 2. The "execute" key must exist
4814 * 3. The "execute" key must be a string
4815 * 4. If the "arguments" key exists, it must be a dict
4816 * 5. If the "id" key exists, it can be anything (ie. json-value)
4817 * 6. Any argument not listed above is considered invalid
4818 */
4819static QDict *qmp_check_input_obj(QObject *input_obj)
4820{
4821 const QDictEntry *ent;
4822 int has_exec_key = 0;
4823 QDict *input_dict;
4824
4825 if (qobject_type(input_obj) != QTYPE_QDICT) {
4826 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4827 return NULL;
4828 }
4829
4830 input_dict = qobject_to_qdict(input_obj);
4831
4832 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4833 const char *arg_name = qdict_entry_key(ent);
4834 const QObject *arg_obj = qdict_entry_value(ent);
4835
4836 if (!strcmp(arg_name, "execute")) {
4837 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4838 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4839 "string");
4840 return NULL;
4841 }
4842 has_exec_key = 1;
4843 } else if (!strcmp(arg_name, "arguments")) {
4844 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4845 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4846 "object");
4847 return NULL;
4848 }
4849 } else if (!strcmp(arg_name, "id")) {
4850 /* FIXME: check duplicated IDs for async commands */
4851 } else {
4852 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4853 return NULL;
4854 }
4855 }
4856
4857 if (!has_exec_key) {
4858 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4859 return NULL;
4860 }
4861
4862 return input_dict;
4863}
4864
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004865static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4866{
4867 QObject *ret_data = NULL;
4868
Luiz Capitulino4903de02010-09-16 11:01:32 -03004869 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004870 qmp_async_info_handler(mon, cmd);
4871 if (monitor_has_error(mon)) {
4872 monitor_protocol_emitter(mon, NULL);
4873 }
4874 } else {
4875 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004876 monitor_protocol_emitter(mon, ret_data);
4877 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004878 }
4879}
4880
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004881static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4882 const QDict *params)
4883{
4884 int ret;
4885 QObject *data = NULL;
4886
4887 mon_print_count_init(mon);
4888
4889 ret = cmd->mhandler.cmd_new(mon, params, &data);
4890 handler_audit(mon, cmd, ret);
4891 monitor_protocol_emitter(mon, data);
4892 qobject_decref(data);
4893}
4894
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004895static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4896{
4897 int err;
4898 QObject *obj;
4899 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004900 const mon_cmd_t *cmd;
4901 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004902 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004903
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004904 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004905 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004906
4907 obj = json_parser_parse(tokens, NULL);
4908 if (!obj) {
4909 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004910 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004911 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004912 }
4913
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004914 input = qmp_check_input_obj(obj);
4915 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004916 qobject_decref(obj);
4917 goto err_out;
4918 }
4919
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004920 mon->mc->id = qdict_get(input, "id");
4921 qobject_incref(mon->mc->id);
4922
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004923 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004924 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004925 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004926 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004927 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004928 }
4929
Anthony Liguorie3193602011-09-02 12:34:47 -05004930 cmd = qmp_find_cmd(cmd_name);
4931 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004932 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004933 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004934 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004935 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004936 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004937 }
4938
4939 obj = qdict_get(input, "arguments");
4940 if (!obj) {
4941 args = qdict_new();
4942 } else {
4943 args = qobject_to_qdict(obj);
4944 QINCREF(args);
4945 }
4946
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004947 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004948 if (err < 0) {
4949 goto err_out;
4950 }
4951
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004952 if (query_cmd) {
4953 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004954 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004955 err = qmp_async_cmd_handler(mon, cmd, args);
4956 if (err) {
4957 /* emit the error response */
4958 goto err_out;
4959 }
Adam Litke940cc302010-01-25 12:18:44 -06004960 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004961 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004962 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004963
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004964 goto out;
4965
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004966err_out:
4967 monitor_protocol_emitter(mon, NULL);
4968out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004969 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004970 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004971}
4972
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004973/**
4974 * monitor_control_read(): Read and handle QMP input
4975 */
4976static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4977{
4978 Monitor *old_mon = cur_mon;
4979
4980 cur_mon = opaque;
4981
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004982 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004983
4984 cur_mon = old_mon;
4985}
4986
aliguori731b0362009-03-05 23:01:42 +00004987static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004988{
aliguori731b0362009-03-05 23:01:42 +00004989 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004990 int i;
aliguori376253e2009-03-05 23:01:23 +00004991
aliguori731b0362009-03-05 23:01:42 +00004992 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004993
aliguoricde76ee2009-03-05 23:01:51 +00004994 if (cur_mon->rs) {
4995 for (i = 0; i < size; i++)
4996 readline_handle_byte(cur_mon->rs, buf[i]);
4997 } else {
4998 if (size == 0 || buf[size - 1] != 0)
4999 monitor_printf(cur_mon, "corrupted command\n");
5000 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005001 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005002 }
aliguori731b0362009-03-05 23:01:42 +00005003
5004 cur_mon = old_mon;
5005}
aliguorid8f44602008-10-06 13:52:44 +00005006
aliguori376253e2009-03-05 23:01:23 +00005007static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005008{
aliguori731b0362009-03-05 23:01:42 +00005009 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005010 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005011 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005012}
5013
aliguoricde76ee2009-03-05 23:01:51 +00005014int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005015{
aliguoricde76ee2009-03-05 23:01:51 +00005016 if (!mon->rs)
5017 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005018 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005019 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005020}
5021
aliguori376253e2009-03-05 23:01:23 +00005022void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005023{
aliguoricde76ee2009-03-05 23:01:51 +00005024 if (!mon->rs)
5025 return;
aliguori731b0362009-03-05 23:01:42 +00005026 if (--mon->suspend_cnt == 0)
5027 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005028}
5029
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005030static QObject *get_qmp_greeting(void)
5031{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005032 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005033
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005034 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005035 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5036}
5037
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005038/**
5039 * monitor_control_event(): Print QMP gretting
5040 */
5041static void monitor_control_event(void *opaque, int event)
5042{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005043 QObject *data;
5044 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005045
Luiz Capitulino47116d12010-02-08 17:01:30 -02005046 switch (event) {
5047 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005048 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005049 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005050 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005051 monitor_json_emitter(mon, data);
5052 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005053 break;
5054 case CHR_EVENT_CLOSED:
5055 json_message_parser_destroy(&mon->mc->parser);
5056 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005057 }
5058}
5059
aliguori731b0362009-03-05 23:01:42 +00005060static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005061{
aliguori376253e2009-03-05 23:01:23 +00005062 Monitor *mon = opaque;
5063
aliguori2724b182009-03-05 23:01:47 +00005064 switch (event) {
5065 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005066 mon->mux_out = 0;
5067 if (mon->reset_seen) {
5068 readline_restart(mon->rs);
5069 monitor_resume(mon);
5070 monitor_flush(mon);
5071 } else {
5072 mon->suspend_cnt = 0;
5073 }
aliguori2724b182009-03-05 23:01:47 +00005074 break;
ths86e94de2007-01-05 22:01:59 +00005075
aliguori2724b182009-03-05 23:01:47 +00005076 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005077 if (mon->reset_seen) {
5078 if (mon->suspend_cnt == 0) {
5079 monitor_printf(mon, "\n");
5080 }
5081 monitor_flush(mon);
5082 monitor_suspend(mon);
5083 } else {
5084 mon->suspend_cnt++;
5085 }
5086 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005087 break;
5088
Amit Shahb6b8df52009-10-07 18:31:16 +05305089 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005090 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5091 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005092 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005093 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005094 }
5095 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005096 break;
5097 }
ths86e94de2007-01-05 22:01:59 +00005098}
5099
aliguori76655d62009-03-06 20:27:37 +00005100
5101/*
5102 * Local variables:
5103 * c-indent-level: 4
5104 * c-basic-offset: 4
5105 * tab-width: 8
5106 * End:
5107 */
5108
aliguori731b0362009-03-05 23:01:42 +00005109void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005110{
aliguori731b0362009-03-05 23:01:42 +00005111 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005112 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005113
5114 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005115 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005116 is_first_init = 0;
5117 }
aliguori87127162009-03-05 23:01:29 +00005118
Anthony Liguori7267c092011-08-20 22:09:37 -05005119 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005120
aliguori87127162009-03-05 23:01:29 +00005121 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005122 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005123 if (flags & MONITOR_USE_READLINE) {
5124 mon->rs = readline_init(mon, monitor_find_completion);
5125 monitor_read_command(mon, 0);
5126 }
aliguori87127162009-03-05 23:01:29 +00005127
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005128 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005129 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005130 /* Control mode requires special handlers */
5131 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5132 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005133 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005134 } else {
5135 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5136 monitor_event, mon);
5137 }
aliguori87127162009-03-05 23:01:29 +00005138
Blue Swirl72cf2d42009-09-12 07:36:22 +00005139 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005140 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5141 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005142}
5143
aliguori376253e2009-03-05 23:01:23 +00005144static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005145{
aliguoribb5fc202009-03-05 23:01:15 +00005146 BlockDriverState *bs = opaque;
5147 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005148
aliguoribb5fc202009-03-05 23:01:15 +00005149 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005150 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005151 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005152 }
aliguori731b0362009-03-05 23:01:42 +00005153 if (mon->password_completion_cb)
5154 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005155
aliguori731b0362009-03-05 23:01:42 +00005156 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005157}
aliguoric0f4ce72009-03-05 23:01:01 +00005158
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005159int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5160 BlockDriverCompletionFunc *completion_cb,
5161 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005162{
aliguoricde76ee2009-03-05 23:01:51 +00005163 int err;
5164
aliguoribb5fc202009-03-05 23:01:15 +00005165 if (!bdrv_key_required(bs)) {
5166 if (completion_cb)
5167 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005168 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005169 }
aliguoric0f4ce72009-03-05 23:01:01 +00005170
Luiz Capitulino94171e12009-12-07 21:37:00 +01005171 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005172 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005173 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005174 }
5175
aliguori376253e2009-03-05 23:01:23 +00005176 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5177 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005178
aliguori731b0362009-03-05 23:01:42 +00005179 mon->password_completion_cb = completion_cb;
5180 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005181
aliguoricde76ee2009-03-05 23:01:51 +00005182 err = monitor_read_password(mon, bdrv_password_cb, bs);
5183
5184 if (err && completion_cb)
5185 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005186
5187 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005188}