blob: 3f99ea0b908842a571ae013a24632b95d3a7f538 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000039#include "monitor.h"
40#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020042#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000044#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000045#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000046#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000047#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000048#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000049#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010051#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030052#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Blue Swirl314e2982011-09-11 20:22:05 +000066#include "memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
ths6a5bd302007-12-03 17:05:38 +000069
Jan Kiszka661f1922011-10-16 11:53:13 +020070/* for pic/irq_info */
71#if defined(TARGET_SPARC)
72#include "hw/sun4m.h"
73#endif
74#include "hw/lm32_pic.h"
75
bellard9dc39cb2004-03-14 21:38:27 +000076//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000077//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000078
bellard9307c4c2004-04-04 12:57:25 +000079/*
80 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000081 *
bellard9307c4c2004-04-04 12:57:25 +000082 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000083 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000084 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010085 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000090 * 'i' 32 bit integer
91 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010092 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020094 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010099 * 'T' double
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000102 * '/' optional gdb-like print format (like "/10x")
103 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100106 * 'b' boolean
107 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300108 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000109 *
110 */
111
Adam Litke940cc302010-01-25 12:18:44 -0600112typedef struct MonitorCompletionData MonitorCompletionData;
113struct MonitorCompletionData {
114 Monitor *mon;
115 void (*user_print)(Monitor *mon, const QObject *data);
116};
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300123 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 union {
125 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300126 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500133 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200134 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500135} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000136
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500138typedef struct mon_fd_t mon_fd_t;
139struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140 char *name;
141 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143};
144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145typedef struct MonitorControl {
146 QObject *id;
147 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200148 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200149} MonitorControl;
150
aliguori87127162009-03-05 23:01:29 +0000151struct Monitor {
152 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200153 int mux_out;
154 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000155 int flags;
156 int suspend_cnt;
157 uint8_t outbuf[1024];
158 int outbuf_index;
159 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200160 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000161 CPUState *mon_cpu;
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200164#ifdef CONFIG_DEBUG_MONITOR
165 int print_calls_nr;
166#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200167 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500168 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000170};
171
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200172#ifdef CONFIG_DEBUG_MONITOR
173#define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200176
177static inline void mon_print_count_inc(Monitor *mon)
178{
179 mon->print_calls_nr++;
180}
181
182static inline void mon_print_count_init(Monitor *mon)
183{
184 mon->print_calls_nr = 0;
185}
186
187static inline int mon_print_count_get(const Monitor *mon)
188{
189 return mon->print_calls_nr;
190}
191
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200192#else /* !CONFIG_DEBUG_MONITOR */
193#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200194static inline void mon_print_count_inc(Monitor *mon) { }
195static inline void mon_print_count_init(Monitor *mon) { }
196static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200197#endif /* CONFIG_DEBUG_MONITOR */
198
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300199/* QMP checker flags */
200#define QMP_ACCEPT_UNKNOWNS 1
201
Blue Swirl72cf2d42009-09-12 07:36:22 +0000202static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000203
Anthony Liguoric227f092009-10-01 16:12:16 -0500204static const mon_cmd_t mon_cmds[];
205static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000206
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300207static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300208static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300209
Markus Armbruster8631b602010-02-18 11:41:55 +0100210Monitor *cur_mon;
211Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000212
aliguori731b0362009-03-05 23:01:42 +0000213static void monitor_command_cb(Monitor *mon, const char *cmdline,
214 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000215
Luiz Capitulino09069b12010-02-04 18:10:06 -0200216static inline int qmp_cmd_mode(const Monitor *mon)
217{
218 return (mon->mc ? mon->mc->command_mode : 0);
219}
220
Luiz Capitulino418173c2009-11-26 22:58:51 -0200221/* Return true if in control mode, false otherwise */
222static inline int monitor_ctrl_mode(const Monitor *mon)
223{
224 return (mon->flags & MONITOR_USE_CONTROL);
225}
226
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100227/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228int monitor_cur_is_qmp(void)
229{
230 return cur_mon && monitor_ctrl_mode(cur_mon);
231}
232
aliguori731b0362009-03-05 23:01:42 +0000233static void monitor_read_command(Monitor *mon, int show_prompt)
234{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200235 if (!mon->rs)
236 return;
237
aliguori731b0362009-03-05 23:01:42 +0000238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
239 if (show_prompt)
240 readline_show_prompt(mon->rs);
241}
bellard6a00d602005-11-21 23:25:50 +0000242
aliguoricde76ee2009-03-05 23:01:51 +0000243static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
244 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000245{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100246 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100247 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100248 return -EINVAL;
249 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
252 return 0;
253 } else {
254 monitor_printf(mon, "terminal does not support password prompting\n");
255 return -ENOTTY;
256 }
aliguoribb5fc202009-03-05 23:01:15 +0000257}
258
aliguori376253e2009-03-05 23:01:23 +0000259void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000260{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000263 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
267/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000268static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000269{
ths60fe76f2007-12-16 03:02:09 +0000270 char c;
aliguori731b0362009-03-05 23:01:42 +0000271
bellard7e2515e2004-08-01 21:52:19 +0000272 for(;;) {
273 c = *str++;
274 if (c == '\0')
275 break;
bellard7ba12602006-07-14 20:26:42 +0000276 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000277 mon->outbuf[mon->outbuf_index++] = '\r';
278 mon->outbuf[mon->outbuf_index++] = c;
279 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
280 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000282 }
283}
284
aliguori376253e2009-03-05 23:01:23 +0000285void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000286{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200287 char buf[4096];
288
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200289 if (!mon)
290 return;
291
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200292 mon_print_count_inc(mon);
293
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200295 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200296 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200297
298 vsnprintf(buf, sizeof(buf), fmt, ap);
299 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000303{
304 va_list ap;
305 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000306 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000307 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000308}
309
aliguori376253e2009-03-05 23:01:23 +0000310void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000311{
312 int i;
313
314 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000315 switch (filename[i]) {
316 case ' ':
317 case '"':
318 case '\\':
319 monitor_printf(mon, "\\%c", filename[i]);
320 break;
321 case '\t':
322 monitor_printf(mon, "\\t");
323 break;
324 case '\r':
325 monitor_printf(mon, "\\r");
326 break;
327 case '\n':
328 monitor_printf(mon, "\\n");
329 break;
330 default:
331 monitor_printf(mon, "%c", filename[i]);
332 break;
333 }
thsfef30742006-12-22 14:11:32 +0000334 }
335}
336
Stefan Weil8b7968f2010-09-23 21:28:05 +0200337static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
338 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000339{
340 va_list ap;
341 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000342 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000343 va_end(ap);
344 return 0;
345}
346
Luiz Capitulino13c74252009-10-07 13:41:55 -0300347static void monitor_user_noop(Monitor *mon, const QObject *data) { }
348
Luiz Capitulino9e807212010-09-16 10:58:59 -0300349static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300350{
351 return cmd->user_print != NULL;
352}
353
Luiz Capitulino4903de02010-09-16 11:01:32 -0300354static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600355{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200356 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600357}
358
Luiz Capitulino8204a912009-11-18 23:05:31 -0200359static inline int monitor_has_error(const Monitor *mon)
360{
361 return mon->error != NULL;
362}
363
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200364static void monitor_json_emitter(Monitor *mon, const QObject *data)
365{
366 QString *json;
367
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200368 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
369 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200370 assert(json != NULL);
371
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200372 qstring_append_chr(json, '\n');
373 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200374
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200375 QDECREF(json);
376}
377
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378static void monitor_protocol_emitter(Monitor *mon, QObject *data)
379{
380 QDict *qmp;
381
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100382 trace_monitor_protocol_emitter(mon);
383
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 qmp = qdict_new();
385
386 if (!monitor_has_error(mon)) {
387 /* success response */
388 if (data) {
389 qobject_incref(data);
390 qdict_put_obj(qmp, "return", data);
391 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200392 /* return an empty QDict by default */
393 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200394 }
395 } else {
396 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100397 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200398 qdict_put(qmp, "error", mon->error->error);
399 QINCREF(mon->error->error);
400 QDECREF(mon->error);
401 mon->error = NULL;
402 }
403
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200404 if (mon->mc->id) {
405 qdict_put_obj(qmp, "id", mon->mc->id);
406 mon->mc->id = NULL;
407 }
408
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200409 monitor_json_emitter(mon, QOBJECT(qmp));
410 QDECREF(qmp);
411}
412
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200413static void timestamp_put(QDict *qdict)
414{
415 int err;
416 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000417 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418
Blue Swirld08d6f02009-12-04 18:06:39 +0000419 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200420 if (err < 0)
421 return;
422
423 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
424 "'microseconds': %" PRId64 " }",
425 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 qdict_put_obj(qdict, "timestamp", obj);
427}
428
429/**
430 * monitor_protocol_event(): Generate a Monitor event
431 *
432 * Event-specific data can be emitted through the (optional) 'data' parameter.
433 */
434void monitor_protocol_event(MonitorEvent event, QObject *data)
435{
436 QDict *qmp;
437 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600438 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439
Blue Swirl242cd002009-12-04 18:05:45 +0000440 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200442 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000443 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200444 event_name = "SHUTDOWN";
445 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000446 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200447 event_name = "RESET";
448 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000449 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200450 event_name = "POWERDOWN";
451 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000452 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200453 event_name = "STOP";
454 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300455 case QEVENT_RESUME:
456 event_name = "RESUME";
457 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200458 case QEVENT_VNC_CONNECTED:
459 event_name = "VNC_CONNECTED";
460 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200461 case QEVENT_VNC_INITIALIZED:
462 event_name = "VNC_INITIALIZED";
463 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200464 case QEVENT_VNC_DISCONNECTED:
465 event_name = "VNC_DISCONNECTED";
466 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200467 case QEVENT_BLOCK_IO_ERROR:
468 event_name = "BLOCK_IO_ERROR";
469 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300470 case QEVENT_RTC_CHANGE:
471 event_name = "RTC_CHANGE";
472 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300473 case QEVENT_WATCHDOG:
474 event_name = "WATCHDOG";
475 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200476 case QEVENT_SPICE_CONNECTED:
477 event_name = "SPICE_CONNECTED";
478 break;
479 case QEVENT_SPICE_INITIALIZED:
480 event_name = "SPICE_INITIALIZED";
481 break;
482 case QEVENT_SPICE_DISCONNECTED:
483 event_name = "SPICE_DISCONNECTED";
484 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200485 default:
486 abort();
487 break;
488 }
489
490 qmp = qdict_new();
491 timestamp_put(qmp);
492 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200493 if (data) {
494 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200495 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200496 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200497
Adam Litkef039a562010-01-15 08:34:02 -0600498 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200499 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200500 monitor_json_emitter(mon, QOBJECT(qmp));
501 }
Adam Litkef039a562010-01-15 08:34:02 -0600502 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200503 QDECREF(qmp);
504}
505
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200506static int do_qmp_capabilities(Monitor *mon, const QDict *params,
507 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200508{
509 /* Will setup QMP capabilities in the future */
510 if (monitor_ctrl_mode(mon)) {
511 mon->mc->command_mode = 1;
512 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200513
514 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200515}
516
Luiz Capitulino0268d972010-10-22 10:08:02 -0200517static void handle_user_command(Monitor *mon, const char *cmdline);
518
519static int do_hmp_passthrough(Monitor *mon, const QDict *params,
520 QObject **ret_data)
521{
522 int ret = 0;
523 Monitor *old_mon, hmp;
524 CharDriverState mchar;
525
526 memset(&hmp, 0, sizeof(hmp));
527 qemu_chr_init_mem(&mchar);
528 hmp.chr = &mchar;
529
530 old_mon = cur_mon;
531 cur_mon = &hmp;
532
533 if (qdict_haskey(params, "cpu-index")) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300534 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
Luiz Capitulino0268d972010-10-22 10:08:02 -0200535 if (ret < 0) {
536 cur_mon = old_mon;
537 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
538 goto out;
539 }
540 }
541
542 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
543 cur_mon = old_mon;
544
545 if (qemu_chr_mem_osize(hmp.chr) > 0) {
546 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
547 }
548
549out:
550 qemu_chr_close_mem(hmp.chr);
551 return ret;
552}
553
bellard9dc39cb2004-03-14 21:38:27 +0000554static int compare_cmd(const char *name, const char *list)
555{
556 const char *p, *pstart;
557 int len;
558 len = strlen(name);
559 p = list;
560 for(;;) {
561 pstart = p;
562 p = strchr(p, '|');
563 if (!p)
564 p = pstart + strlen(pstart);
565 if ((p - pstart) == len && !memcmp(pstart, name, len))
566 return 1;
567 if (*p == '\0')
568 break;
569 p++;
570 }
571 return 0;
572}
573
Anthony Liguoric227f092009-10-01 16:12:16 -0500574static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000575 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000576{
Anthony Liguoric227f092009-10-01 16:12:16 -0500577 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000578
579 for(cmd = cmds; cmd->name != NULL; cmd++) {
580 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
582 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000583 }
584}
585
aliguori376253e2009-03-05 23:01:23 +0000586static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000587{
588 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000589 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000590 } else {
aliguori376253e2009-03-05 23:01:23 +0000591 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000592 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000593 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "Log items (comma separated):\n");
595 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000596 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000597 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000598 }
599 }
bellard9dc39cb2004-03-14 21:38:27 +0000600 }
601}
602
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300603static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300604{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300605 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300606}
607
Lluísfc764102011-08-31 20:31:18 +0200608static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530609{
610 const char *tp_name = qdict_get_str(qdict, "name");
611 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200612 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000613
614 if (!ret) {
615 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
616 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530617}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100618
Michael Rothc45a8162011-10-02 08:44:37 -0500619#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100620static void do_trace_file(Monitor *mon, const QDict *qdict)
621{
622 const char *op = qdict_get_try_str(qdict, "op");
623 const char *arg = qdict_get_try_str(qdict, "arg");
624
625 if (!op) {
626 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
627 } else if (!strcmp(op, "on")) {
628 st_set_trace_file_enabled(true);
629 } else if (!strcmp(op, "off")) {
630 st_set_trace_file_enabled(false);
631 } else if (!strcmp(op, "flush")) {
632 st_flush_trace_buffer();
633 } else if (!strcmp(op, "set")) {
634 if (arg) {
635 st_set_trace_file(arg);
636 }
637 } else {
638 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
639 help_cmd(mon, "trace-file");
640 }
641}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530642#endif
643
Adam Litke940cc302010-01-25 12:18:44 -0600644static void user_monitor_complete(void *opaque, QObject *ret_data)
645{
646 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
647
648 if (ret_data) {
649 data->user_print(data->mon, ret_data);
650 }
651 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500652 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600653}
654
655static void qmp_monitor_complete(void *opaque, QObject *ret_data)
656{
657 monitor_protocol_emitter(opaque, ret_data);
658}
659
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300660static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
661 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600662{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300663 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600664}
665
666static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
667{
668 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
669}
670
671static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
672 const QDict *params)
673{
674 int ret;
675
Anthony Liguori7267c092011-08-20 22:09:37 -0500676 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600677 cb_data->mon = mon;
678 cb_data->user_print = cmd->user_print;
679 monitor_suspend(mon);
680 ret = cmd->mhandler.cmd_async(mon, params,
681 user_monitor_complete, cb_data);
682 if (ret < 0) {
683 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500684 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600685 }
686}
687
688static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
689{
690 int ret;
691
Anthony Liguori7267c092011-08-20 22:09:37 -0500692 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600693 cb_data->mon = mon;
694 cb_data->user_print = cmd->user_print;
695 monitor_suspend(mon);
696 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
697 if (ret < 0) {
698 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500699 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600700 }
701}
702
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300703static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000704{
Anthony Liguoric227f092009-10-01 16:12:16 -0500705 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300706 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000707
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200708 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000709 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200710 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711
712 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000713 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300714 break;
bellard9dc39cb2004-03-14 21:38:27 +0000715 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300716
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200717 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300718 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200719 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300720
Luiz Capitulino4903de02010-09-16 11:01:32 -0300721 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300722 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300723 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300724 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200725
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300726 cmd->mhandler.info_new(mon, &info_data);
727 if (info_data) {
728 cmd->user_print(mon, info_data);
729 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200730 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300731 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300732 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300733 }
734
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300735 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300736
737help:
738 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000739}
740
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300741static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200742{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300743 CommandInfoList *info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200744
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300745 info = g_malloc0(sizeof(*info));
746 info->value = g_malloc0(sizeof(*info->value));
747 info->value->name = g_strdup(cmd_name);
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200748
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300749 return info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200750}
751
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300752CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000753{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300754 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200755 const mon_cmd_t *cmd;
756
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300757 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300758 info = alloc_cmd_entry(cmd->name);
759 info->next = cmd_list;
760 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200761 }
762
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300763 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300764 char buf[128];
765 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300766 info = alloc_cmd_entry(buf);
767 info->next = cmd_list;
768 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200769 }
770
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300771 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000772}
773
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300774/* set the current CPU defined by the user */
775int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000776{
777 CPUState *env;
778
779 for(env = first_cpu; env != NULL; env = env->next_cpu) {
780 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000781 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000782 return 0;
783 }
784 }
785 return -1;
786}
787
pbrook9596ebb2007-11-18 01:44:38 +0000788static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000789{
aliguori731b0362009-03-05 23:01:42 +0000790 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300791 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000792 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300793 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000794 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000795}
796
aliguori376253e2009-03-05 23:01:23 +0000797static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000798{
bellard6a00d602005-11-21 23:25:50 +0000799 CPUState *env;
800 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000801#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000802 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000803 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000804#else
aliguori376253e2009-03-05 23:01:23 +0000805 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000806 0);
bellard9307c4c2004-04-04 12:57:25 +0000807#endif
808}
809
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300810static void print_cpu_iter(QObject *obj, void *opaque)
811{
812 QDict *cpu;
813 int active = ' ';
814 Monitor *mon = opaque;
815
816 assert(qobject_type(obj) == QTYPE_QDICT);
817 cpu = qobject_to_qdict(obj);
818
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200819 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300820 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200821 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300822
823 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
824
825#if defined(TARGET_I386)
826 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
827 (target_ulong) qdict_get_int(cpu, "pc"));
828#elif defined(TARGET_PPC)
829 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
830 (target_long) qdict_get_int(cpu, "nip"));
831#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500832 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300833 (target_long) qdict_get_int(cpu, "pc"));
834 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
835 (target_long) qdict_get_int(cpu, "npc"));
836#elif defined(TARGET_MIPS)
837 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
838 (target_long) qdict_get_int(cpu, "PC"));
839#endif
840
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200841 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300842 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200843 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300844
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100845 monitor_printf(mon, " thread_id=%" PRId64 " ",
846 qdict_get_int(cpu, "thread_id"));
847
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300848 monitor_printf(mon, "\n");
849}
850
851static void monitor_print_cpus(Monitor *mon, const QObject *data)
852{
853 QList *cpu_list;
854
855 assert(qobject_type(data) == QTYPE_QLIST);
856 cpu_list = qobject_to_qlist(data);
857 qlist_iter(cpu_list, print_cpu_iter, mon);
858}
859
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300860static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000861{
862 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300863 QList *cpu_list;
864
865 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000866
867 /* just to set the default cpu if not already done */
868 mon_get_cpu();
869
870 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200871 QDict *cpu;
872 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300873
Avi Kivity4c0960c2009-08-17 23:19:53 +0300874 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300875
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200876 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
877 env->cpu_index, env == mon->mon_cpu,
878 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200879
880 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881
bellard6a00d602005-11-21 23:25:50 +0000882#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300883 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000884#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300885 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000886#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300887 qdict_put(cpu, "pc", qint_from_int(env->pc));
888 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000889#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000891#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100892 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300893
894 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000895 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300896
897 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000898}
899
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200900static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000901{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300902 int index = qdict_get_int(qdict, "index");
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300903 if (monitor_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100904 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
905 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200906 return -1;
907 }
908 return 0;
bellard6a00d602005-11-21 23:25:50 +0000909}
910
aliguori376253e2009-03-05 23:01:23 +0000911static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000912{
aliguori376253e2009-03-05 23:01:23 +0000913 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000914}
915
aliguori376253e2009-03-05 23:01:23 +0000916static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000917{
918 int i;
bellard7e2515e2004-08-01 21:52:19 +0000919 const char *str;
ths3b46e622007-09-17 08:09:54 +0000920
aliguoricde76ee2009-03-05 23:01:51 +0000921 if (!mon->rs)
922 return;
bellard7e2515e2004-08-01 21:52:19 +0000923 i = 0;
924 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000925 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000926 if (!str)
927 break;
aliguori376253e2009-03-05 23:01:23 +0000928 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000929 i++;
bellardaa455482004-04-04 13:07:25 +0000930 }
931}
932
j_mayer76a66252007-03-07 08:32:30 +0000933#if defined(TARGET_PPC)
934/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000935static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000936{
937 CPUState *env;
938
939 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000940 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000941}
942#endif
943
Lluís6d8a7642011-08-31 20:30:43 +0200944#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530945static void do_info_trace(Monitor *mon)
946{
947 st_print_trace((FILE *)mon, &monitor_fprintf);
948}
Lluís31965ae2011-08-31 20:31:24 +0200949#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530950
Lluísfc764102011-08-31 20:31:18 +0200951static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530952{
Lluísfc764102011-08-31 20:31:18 +0200953 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530954}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530955
Jes Sorensen821601e2011-03-16 13:33:36 +0100956#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200957static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000958{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600959 if (!password || !password[0]) {
960 if (vnc_display_disable_login(NULL)) {
961 qerror_report(QERR_SET_PASSWD_FAILED);
962 return -1;
963 }
964 return 0;
965 }
966
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200967 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100968 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200969 return -1;
970 }
aliguoribb5fc202009-03-05 23:01:15 +0000971
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200972 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100973}
974
975static void change_vnc_password_cb(Monitor *mon, const char *password,
976 void *opaque)
977{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100978 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000979 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000980}
981
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200982static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000983{
ths70848512007-08-25 01:37:05 +0000984 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000985 strcmp(target, "password") == 0) {
986 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000987 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000988 strncpy(password, arg, sizeof(password));
989 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200990 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000991 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200992 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000993 }
ths70848512007-08-25 01:37:05 +0000994 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200995 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100996 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200997 return -1;
998 }
ths70848512007-08-25 01:37:05 +0000999 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001000
1001 return 0;
thse25a5822007-08-25 01:36:20 +00001002}
Jes Sorensen821601e2011-03-16 13:33:36 +01001003#else
1004static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1005{
1006 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1007 return -ENODEV;
1008}
1009#endif
thse25a5822007-08-25 01:36:20 +00001010
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001011/**
1012 * do_change(): Change a removable medium, or VNC configuration
1013 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001014static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001015{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001016 const char *device = qdict_get_str(qdict, "device");
1017 const char *target = qdict_get_str(qdict, "target");
1018 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001019 int ret;
1020
thse25a5822007-08-25 01:36:20 +00001021 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001022 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001023 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001024 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001025 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001026
1027 return ret;
thse25a5822007-08-25 01:36:20 +00001028}
1029
Gerd Hoffmann75721502010-10-07 12:22:54 +02001030static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1031{
1032 const char *protocol = qdict_get_str(qdict, "protocol");
1033 const char *password = qdict_get_str(qdict, "password");
1034 const char *connected = qdict_get_try_str(qdict, "connected");
1035 int disconnect_if_connected = 0;
1036 int fail_if_connected = 0;
1037 int rc;
1038
1039 if (connected) {
1040 if (strcmp(connected, "fail") == 0) {
1041 fail_if_connected = 1;
1042 } else if (strcmp(connected, "disconnect") == 0) {
1043 disconnect_if_connected = 1;
1044 } else if (strcmp(connected, "keep") == 0) {
1045 /* nothing */
1046 } else {
1047 qerror_report(QERR_INVALID_PARAMETER, "connected");
1048 return -1;
1049 }
1050 }
1051
1052 if (strcmp(protocol, "spice") == 0) {
1053 if (!using_spice) {
1054 /* correct one? spice isn't a device ,,, */
1055 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1056 return -1;
1057 }
1058 rc = qemu_spice_set_passwd(password, fail_if_connected,
1059 disconnect_if_connected);
1060 if (rc != 0) {
1061 qerror_report(QERR_SET_PASSWD_FAILED);
1062 return -1;
1063 }
1064 return 0;
1065 }
1066
1067 if (strcmp(protocol, "vnc") == 0) {
1068 if (fail_if_connected || disconnect_if_connected) {
1069 /* vnc supports "connected=keep" only */
1070 qerror_report(QERR_INVALID_PARAMETER, "connected");
1071 return -1;
1072 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001073 /* Note that setting an empty password will not disable login through
1074 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001075 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001076 }
1077
1078 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1079 return -1;
1080}
1081
1082static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1083{
1084 const char *protocol = qdict_get_str(qdict, "protocol");
1085 const char *whenstr = qdict_get_str(qdict, "time");
1086 time_t when;
1087 int rc;
1088
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001089 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001090 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001091 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001092 when = TIME_MAX;
1093 } else if (whenstr[0] == '+') {
1094 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1095 } else {
1096 when = strtoull(whenstr, NULL, 10);
1097 }
1098
1099 if (strcmp(protocol, "spice") == 0) {
1100 if (!using_spice) {
1101 /* correct one? spice isn't a device ,,, */
1102 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1103 return -1;
1104 }
1105 rc = qemu_spice_set_pw_expire(when);
1106 if (rc != 0) {
1107 qerror_report(QERR_SET_PASSWD_FAILED);
1108 return -1;
1109 }
1110 return 0;
1111 }
1112
1113 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001114 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001115 }
1116
1117 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1118 return -1;
1119}
1120
Daniel P. Berrange13661082011-06-23 13:31:42 +01001121static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1122{
1123 const char *protocol = qdict_get_str(qdict, "protocol");
1124 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001125 CharDriverState *s;
1126
1127 if (strcmp(protocol, "spice") == 0) {
1128 if (!using_spice) {
1129 /* correct one? spice isn't a device ,,, */
1130 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1131 return -1;
1132 }
1133 qerror_report(QERR_ADD_CLIENT_FAILED);
1134 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001135#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001136 } else if (strcmp(protocol, "vnc") == 0) {
1137 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001138 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001139 vnc_display_add_client(NULL, fd, skipauth);
1140 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001141#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001142 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1143 int fd = monitor_get_fd(mon, fdname);
1144 if (qemu_chr_add_client(s, fd) < 0) {
1145 qerror_report(QERR_ADD_CLIENT_FAILED);
1146 return -1;
1147 }
1148 return 0;
1149 }
1150
1151 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1152 return -1;
1153}
1154
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001155static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1156{
1157 const char *protocol = qdict_get_str(qdict, "protocol");
1158 const char *hostname = qdict_get_str(qdict, "hostname");
1159 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1160 int port = qdict_get_try_int(qdict, "port", -1);
1161 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1162 int ret;
1163
1164 if (strcmp(protocol, "spice") == 0) {
1165 if (!using_spice) {
1166 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1167 return -1;
1168 }
1169
1170 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1171 if (ret != 0) {
1172 qerror_report(QERR_UNDEFINED_ERROR);
1173 return -1;
1174 }
1175 return 0;
1176 }
1177
1178 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1179 return -1;
1180}
1181
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001182static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001183{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001184 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001185 return 0;
bellard59a983b2004-03-17 23:17:16 +00001186}
1187
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001188static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001189{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001190 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001191}
1192
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001194{
1195 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001196 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001197
bellard9307c4c2004-04-04 12:57:25 +00001198 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001199 mask = 0;
1200 } else {
bellard9307c4c2004-04-04 12:57:25 +00001201 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001202 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001203 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001204 return;
1205 }
1206 }
1207 cpu_set_log(mask);
1208}
1209
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001210static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001211{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001212 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001213 if (!option || !strcmp(option, "on")) {
1214 singlestep = 1;
1215 } else if (!strcmp(option, "off")) {
1216 singlestep = 0;
1217 } else {
1218 monitor_printf(mon, "unexpected option %s\n", option);
1219 }
1220}
1221
aliguoribb5fc202009-03-05 23:01:15 +00001222static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001223
aliguori376253e2009-03-05 23:01:23 +00001224struct bdrv_iterate_context {
1225 Monitor *mon;
1226 int err;
1227};
aliguoric0f4ce72009-03-05 23:01:01 +00001228
Luiz Capitulino28a72822011-09-26 17:43:50 -03001229static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1230{
1231 bdrv_iostatus_reset(bs);
1232}
1233
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001234/**
1235 * do_cont(): Resume emulation.
1236 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001237static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001238{
1239 struct bdrv_iterate_context context = { mon, 0 };
1240
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001241 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301242 qerror_report(QERR_MIGRATION_EXPECTED);
1243 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001244 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1245 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001246 qerror_report(QERR_RESET_REQUIRED);
1247 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301248 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001249
Luiz Capitulino28a72822011-09-26 17:43:50 -03001250 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001251 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001252 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001253 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001254 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001255 return 0;
1256 } else {
1257 return -1;
1258 }
bellard8a7ddc32004-03-31 19:00:16 +00001259}
1260
aliguoribb5fc202009-03-05 23:01:15 +00001261static void bdrv_key_cb(void *opaque, int err)
1262{
aliguori376253e2009-03-05 23:01:23 +00001263 Monitor *mon = opaque;
1264
aliguoribb5fc202009-03-05 23:01:15 +00001265 /* another key was set successfully, retry to continue */
1266 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001267 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001268}
1269
1270static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1271{
aliguori376253e2009-03-05 23:01:23 +00001272 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001273
aliguori376253e2009-03-05 23:01:23 +00001274 if (!context->err && bdrv_key_required(bs)) {
1275 context->err = -EBUSY;
1276 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1277 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001278 }
1279}
1280
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001281static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001282{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001283 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001284 if (!device)
1285 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1286 if (gdbserver_start(device) < 0) {
1287 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1288 device);
1289 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001290 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001291 } else {
aliguori59030a82009-04-05 18:43:41 +00001292 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1293 device);
bellard8a7ddc32004-03-31 19:00:16 +00001294 }
1295}
1296
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001297static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001298{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001299 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001300 if (select_watchdog_action(action) == -1) {
1301 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1302 }
1303}
1304
aliguori376253e2009-03-05 23:01:23 +00001305static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001306{
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001308 switch(c) {
1309 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001311 break;
1312 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001317 break;
1318 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001320 break;
1321 default:
1322 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001324 } else {
aliguori376253e2009-03-05 23:01:23 +00001325 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001326 }
1327 break;
1328 }
aliguori376253e2009-03-05 23:01:23 +00001329 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001330}
1331
aliguori376253e2009-03-05 23:01:23 +00001332static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001333 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001334{
bellard6a00d602005-11-21 23:25:50 +00001335 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001336 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001337 uint8_t buf[16];
1338 uint64_t v;
1339
1340 if (format == 'i') {
1341 int flags;
1342 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001343 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001344#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001345 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001346 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001347 } else if (wsize == 4) {
1348 flags = 0;
1349 } else {
bellard6a15fd12006-04-12 21:07:07 +00001350 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001351 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001352 if (env) {
1353#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001354 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001355 (env->segs[R_CS].flags & DESC_L_MASK))
1356 flags = 2;
1357 else
1358#endif
1359 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1360 flags = 1;
1361 }
bellard4c27ba22004-04-25 18:05:08 +00001362 }
1363#endif
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001365 return;
1366 }
1367
1368 len = wsize * count;
1369 if (wsize == 1)
1370 line_size = 8;
1371 else
1372 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001373 max_digits = 0;
1374
1375 switch(format) {
1376 case 'o':
1377 max_digits = (wsize * 8 + 2) / 3;
1378 break;
1379 default:
1380 case 'x':
1381 max_digits = (wsize * 8) / 4;
1382 break;
1383 case 'u':
1384 case 'd':
1385 max_digits = (wsize * 8 * 10 + 32) / 33;
1386 break;
1387 case 'c':
1388 wsize = 1;
1389 break;
1390 }
1391
1392 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001393 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001395 else
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001397 l = len;
1398 if (l > line_size)
1399 l = line_size;
1400 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001401 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001402 } else {
bellard6a00d602005-11-21 23:25:50 +00001403 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001404 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001405 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001406 break;
1407 }
bellard9307c4c2004-04-04 12:57:25 +00001408 }
ths5fafdf22007-09-16 21:08:06 +00001409 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001410 while (i < l) {
1411 switch(wsize) {
1412 default:
1413 case 1:
1414 v = ldub_raw(buf + i);
1415 break;
1416 case 2:
1417 v = lduw_raw(buf + i);
1418 break;
1419 case 4:
bellard92a31b12005-02-10 22:00:52 +00001420 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001421 break;
1422 case 8:
1423 v = ldq_raw(buf + i);
1424 break;
1425 }
aliguori376253e2009-03-05 23:01:23 +00001426 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001427 switch(format) {
1428 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001430 break;
1431 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001432 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001433 break;
1434 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001436 break;
1437 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001439 break;
1440 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001441 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001442 break;
1443 }
1444 i += wsize;
1445 }
aliguori376253e2009-03-05 23:01:23 +00001446 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001447 addr += l;
1448 len -= l;
1449 }
1450}
1451
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001452static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001453{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001454 int count = qdict_get_int(qdict, "count");
1455 int format = qdict_get_int(qdict, "format");
1456 int size = qdict_get_int(qdict, "size");
1457 target_long addr = qdict_get_int(qdict, "addr");
1458
aliguori376253e2009-03-05 23:01:23 +00001459 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001460}
1461
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001462static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001463{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001464 int count = qdict_get_int(qdict, "count");
1465 int format = qdict_get_int(qdict, "format");
1466 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001467 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001468
aliguori376253e2009-03-05 23:01:23 +00001469 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001470}
1471
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001472static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001473{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001474 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001475 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001476
blueswir17743e582007-09-24 18:39:04 +00001477#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001478 switch(format) {
1479 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001480 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001481 break;
1482 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001483 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001484 break;
1485 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001486 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001487 break;
1488 default:
1489 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001490 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001491 break;
1492 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001493 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001494 break;
1495 }
bellard92a31b12005-02-10 22:00:52 +00001496#else
1497 switch(format) {
1498 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001499 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001500 break;
1501 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001503 break;
1504 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001506 break;
1507 default:
1508 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001510 break;
1511 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001513 break;
1514 }
1515#endif
aliguori376253e2009-03-05 23:01:23 +00001516 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001517}
1518
Luiz Capitulino98696222010-02-10 23:49:58 -02001519static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001520{
1521 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001522 uint32_t size = qdict_get_int(qdict, "size");
1523 const char *filename = qdict_get_str(qdict, "filename");
1524 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001525 uint32_t l;
1526 CPUState *env;
1527 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001528 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001529
1530 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001531
1532 f = fopen(filename, "wb");
1533 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001534 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001535 return -1;
bellardb371dc52007-01-03 15:20:39 +00001536 }
1537 while (size != 0) {
1538 l = sizeof(buf);
1539 if (l > size)
1540 l = size;
1541 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001542 if (fwrite(buf, 1, l, f) != l) {
1543 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1544 goto exit;
1545 }
bellardb371dc52007-01-03 15:20:39 +00001546 addr += l;
1547 size -= l;
1548 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001549
1550 ret = 0;
1551
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001552exit:
bellardb371dc52007-01-03 15:20:39 +00001553 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001554 return ret;
bellardb371dc52007-01-03 15:20:39 +00001555}
1556
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001557static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001558 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001559{
1560 FILE *f;
1561 uint32_t l;
1562 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001563 uint32_t size = qdict_get_int(qdict, "size");
1564 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001565 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001566 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001567
1568 f = fopen(filename, "wb");
1569 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001570 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001571 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001572 }
1573 while (size != 0) {
1574 l = sizeof(buf);
1575 if (l > size)
1576 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001577 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001578 if (fwrite(buf, 1, l, f) != l) {
1579 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1580 goto exit;
1581 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001582 fflush(f);
1583 addr += l;
1584 size -= l;
1585 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001586
1587 ret = 0;
1588
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001589exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001590 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001591 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001592}
1593
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001594static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001595{
1596 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001597 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001598 uint32_t start = qdict_get_int(qdict, "start");
1599 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001600
1601 sum = 0;
1602 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001603 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001604 /* BSD sum algorithm ('sum' Unix command) */
1605 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001606 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001607 }
aliguori376253e2009-03-05 23:01:23 +00001608 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001609}
1610
bellarda3a91a32004-06-04 11:06:21 +00001611typedef struct {
1612 int keycode;
1613 const char *name;
1614} KeyDef;
1615
1616static const KeyDef key_defs[] = {
1617 { 0x2a, "shift" },
1618 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001619
bellarda3a91a32004-06-04 11:06:21 +00001620 { 0x38, "alt" },
1621 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001622 { 0x64, "altgr" },
1623 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001624 { 0x1d, "ctrl" },
1625 { 0x9d, "ctrl_r" },
1626
1627 { 0xdd, "menu" },
1628
1629 { 0x01, "esc" },
1630
1631 { 0x02, "1" },
1632 { 0x03, "2" },
1633 { 0x04, "3" },
1634 { 0x05, "4" },
1635 { 0x06, "5" },
1636 { 0x07, "6" },
1637 { 0x08, "7" },
1638 { 0x09, "8" },
1639 { 0x0a, "9" },
1640 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001641 { 0x0c, "minus" },
1642 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001643 { 0x0e, "backspace" },
1644
1645 { 0x0f, "tab" },
1646 { 0x10, "q" },
1647 { 0x11, "w" },
1648 { 0x12, "e" },
1649 { 0x13, "r" },
1650 { 0x14, "t" },
1651 { 0x15, "y" },
1652 { 0x16, "u" },
1653 { 0x17, "i" },
1654 { 0x18, "o" },
1655 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001656 { 0x1a, "bracket_left" },
1657 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001658 { 0x1c, "ret" },
1659
1660 { 0x1e, "a" },
1661 { 0x1f, "s" },
1662 { 0x20, "d" },
1663 { 0x21, "f" },
1664 { 0x22, "g" },
1665 { 0x23, "h" },
1666 { 0x24, "j" },
1667 { 0x25, "k" },
1668 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001669 { 0x27, "semicolon" },
1670 { 0x28, "apostrophe" },
1671 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001672
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001673 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001674 { 0x2c, "z" },
1675 { 0x2d, "x" },
1676 { 0x2e, "c" },
1677 { 0x2f, "v" },
1678 { 0x30, "b" },
1679 { 0x31, "n" },
1680 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001681 { 0x33, "comma" },
1682 { 0x34, "dot" },
1683 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001684
balrog4d3b6f62008-02-10 16:33:14 +00001685 { 0x37, "asterisk" },
1686
bellarda3a91a32004-06-04 11:06:21 +00001687 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001688 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001689 { 0x3b, "f1" },
1690 { 0x3c, "f2" },
1691 { 0x3d, "f3" },
1692 { 0x3e, "f4" },
1693 { 0x3f, "f5" },
1694 { 0x40, "f6" },
1695 { 0x41, "f7" },
1696 { 0x42, "f8" },
1697 { 0x43, "f9" },
1698 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001699 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001700 { 0x46, "scroll_lock" },
1701
bellard64866c32006-05-07 18:03:31 +00001702 { 0xb5, "kp_divide" },
1703 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001704 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001705 { 0x4e, "kp_add" },
1706 { 0x9c, "kp_enter" },
1707 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001708 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001709
1710 { 0x52, "kp_0" },
1711 { 0x4f, "kp_1" },
1712 { 0x50, "kp_2" },
1713 { 0x51, "kp_3" },
1714 { 0x4b, "kp_4" },
1715 { 0x4c, "kp_5" },
1716 { 0x4d, "kp_6" },
1717 { 0x47, "kp_7" },
1718 { 0x48, "kp_8" },
1719 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001720
bellarda3a91a32004-06-04 11:06:21 +00001721 { 0x56, "<" },
1722
1723 { 0x57, "f11" },
1724 { 0x58, "f12" },
1725
1726 { 0xb7, "print" },
1727
1728 { 0xc7, "home" },
1729 { 0xc9, "pgup" },
1730 { 0xd1, "pgdn" },
1731 { 0xcf, "end" },
1732
1733 { 0xcb, "left" },
1734 { 0xc8, "up" },
1735 { 0xd0, "down" },
1736 { 0xcd, "right" },
1737
1738 { 0xd2, "insert" },
1739 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001740#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1741 { 0xf0, "stop" },
1742 { 0xf1, "again" },
1743 { 0xf2, "props" },
1744 { 0xf3, "undo" },
1745 { 0xf4, "front" },
1746 { 0xf5, "copy" },
1747 { 0xf6, "open" },
1748 { 0xf7, "paste" },
1749 { 0xf8, "find" },
1750 { 0xf9, "cut" },
1751 { 0xfa, "lf" },
1752 { 0xfb, "help" },
1753 { 0xfc, "meta_l" },
1754 { 0xfd, "meta_r" },
1755 { 0xfe, "compose" },
1756#endif
bellarda3a91a32004-06-04 11:06:21 +00001757 { 0, NULL },
1758};
1759
1760static int get_keycode(const char *key)
1761{
1762 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001763 char *endp;
1764 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001765
1766 for(p = key_defs; p->name != NULL; p++) {
1767 if (!strcmp(key, p->name))
1768 return p->keycode;
1769 }
bellard64866c32006-05-07 18:03:31 +00001770 if (strstart(key, "0x", NULL)) {
1771 ret = strtoul(key, &endp, 0);
1772 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1773 return ret;
1774 }
bellarda3a91a32004-06-04 11:06:21 +00001775 return -1;
1776}
1777
balrogc8256f92008-06-08 22:45:01 +00001778#define MAX_KEYCODES 16
1779static uint8_t keycodes[MAX_KEYCODES];
1780static int nb_pending_keycodes;
1781static QEMUTimer *key_timer;
1782
1783static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001784{
balrogc8256f92008-06-08 22:45:01 +00001785 int keycode;
1786
1787 while (nb_pending_keycodes > 0) {
1788 nb_pending_keycodes--;
1789 keycode = keycodes[nb_pending_keycodes];
1790 if (keycode & 0x80)
1791 kbd_put_keycode(0xe0);
1792 kbd_put_keycode(keycode | 0x80);
1793 }
1794}
1795
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001796static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001797{
balrog3401c0d2008-06-04 10:05:59 +00001798 char keyname_buf[16];
1799 char *separator;
1800 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001801 const char *string = qdict_get_str(qdict, "string");
1802 int has_hold_time = qdict_haskey(qdict, "hold_time");
1803 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001804
balrogc8256f92008-06-08 22:45:01 +00001805 if (nb_pending_keycodes > 0) {
1806 qemu_del_timer(key_timer);
1807 release_keys(NULL);
1808 }
1809 if (!has_hold_time)
1810 hold_time = 100;
1811 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001812 while (1) {
1813 separator = strchr(string, '-');
1814 keyname_len = separator ? separator - string : strlen(string);
1815 if (keyname_len > 0) {
1816 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1817 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001818 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001819 return;
bellarda3a91a32004-06-04 11:06:21 +00001820 }
balrogc8256f92008-06-08 22:45:01 +00001821 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001822 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001823 return;
1824 }
1825 keyname_buf[keyname_len] = 0;
1826 keycode = get_keycode(keyname_buf);
1827 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001828 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001829 return;
1830 }
balrogc8256f92008-06-08 22:45:01 +00001831 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001832 }
balrog3401c0d2008-06-04 10:05:59 +00001833 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001834 break;
balrog3401c0d2008-06-04 10:05:59 +00001835 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001836 }
balrogc8256f92008-06-08 22:45:01 +00001837 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001838 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001839 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001840 keycode = keycodes[i];
1841 if (keycode & 0x80)
1842 kbd_put_keycode(0xe0);
1843 kbd_put_keycode(keycode & 0x7f);
1844 }
balrogc8256f92008-06-08 22:45:01 +00001845 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001846 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001847 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001848}
1849
bellard13224a82006-07-14 22:03:35 +00001850static int mouse_button_state;
1851
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001852static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001853{
1854 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001855 const char *dx_str = qdict_get_str(qdict, "dx_str");
1856 const char *dy_str = qdict_get_str(qdict, "dy_str");
1857 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001858 dx = strtol(dx_str, NULL, 0);
1859 dy = strtol(dy_str, NULL, 0);
1860 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001861 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001862 dz = strtol(dz_str, NULL, 0);
1863 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1864}
1865
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001866static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001867{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001868 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001869 mouse_button_state = button_state;
1870 kbd_mouse_event(0, 0, 0, mouse_button_state);
1871}
1872
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001873static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001874{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001875 int size = qdict_get_int(qdict, "size");
1876 int addr = qdict_get_int(qdict, "addr");
1877 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001878 uint32_t val;
1879 int suffix;
1880
1881 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001882 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001883 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001884 addr++;
1885 }
1886 addr &= 0xffff;
1887
1888 switch(size) {
1889 default:
1890 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001891 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001892 suffix = 'b';
1893 break;
1894 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001895 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001896 suffix = 'w';
1897 break;
1898 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001899 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001900 suffix = 'l';
1901 break;
1902 }
aliguori376253e2009-03-05 23:01:23 +00001903 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1904 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001905}
bellarda3a91a32004-06-04 11:06:21 +00001906
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001907static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001908{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001909 int size = qdict_get_int(qdict, "size");
1910 int addr = qdict_get_int(qdict, "addr");
1911 int val = qdict_get_int(qdict, "val");
1912
Jan Kiszkaf1147842009-07-14 10:20:11 +02001913 addr &= IOPORTS_MASK;
1914
1915 switch (size) {
1916 default:
1917 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001918 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001919 break;
1920 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001921 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001922 break;
1923 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001924 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001925 break;
1926 }
1927}
1928
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001929static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001930{
1931 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001932 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001933
Jan Kiszka76e30d02009-07-02 00:19:02 +02001934 res = qemu_boot_set(bootdevice);
1935 if (res == 0) {
1936 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1937 } else if (res > 0) {
1938 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001939 } else {
aliguori376253e2009-03-05 23:01:23 +00001940 monitor_printf(mon, "no function defined to set boot device list for "
1941 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001942 }
1943}
1944
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001945/**
Luiz Capitulino43076662009-10-07 13:41:59 -03001946 * do_system_powerdown(): Issue a machine powerdown
1947 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001948static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1949 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001950{
1951 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001952 return 0;
bellard34751872005-07-02 14:31:34 +00001953}
1954
bellardb86bda52004-09-18 19:32:46 +00001955#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001956static void print_pte(Monitor *mon, target_phys_addr_t addr,
1957 target_phys_addr_t pte,
1958 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001959{
Blue Swirld65aaf32010-12-11 18:56:24 +00001960#ifdef TARGET_X86_64
1961 if (addr & (1ULL << 47)) {
1962 addr |= -1LL << 48;
1963 }
1964#endif
1965 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1966 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001967 addr,
1968 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001969 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001970 pte & PG_GLOBAL_MASK ? 'G' : '-',
1971 pte & PG_PSE_MASK ? 'P' : '-',
1972 pte & PG_DIRTY_MASK ? 'D' : '-',
1973 pte & PG_ACCESSED_MASK ? 'A' : '-',
1974 pte & PG_PCD_MASK ? 'C' : '-',
1975 pte & PG_PWT_MASK ? 'T' : '-',
1976 pte & PG_USER_MASK ? 'U' : '-',
1977 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001978}
1979
Blue Swirld65aaf32010-12-11 18:56:24 +00001980static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001981{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001982 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001983 uint32_t pgd, pde, pte;
1984
bellardb86bda52004-09-18 19:32:46 +00001985 pgd = env->cr[3] & ~0xfff;
1986 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001987 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001988 pde = le32_to_cpu(pde);
1989 if (pde & PG_PRESENT_MASK) {
1990 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001991 /* 4M pages */
1992 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001993 } else {
1994 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001995 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001996 pte = le32_to_cpu(pte);
1997 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001998 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001999 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002000 ~0xfff);
2001 }
2002 }
2003 }
2004 }
2005 }
2006}
2007
Blue Swirld65aaf32010-12-11 18:56:24 +00002008static void tlb_info_pae32(Monitor *mon, CPUState *env)
2009{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002010 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002011 uint64_t pdpe, pde, pte;
2012 uint64_t pdp_addr, pd_addr, pt_addr;
2013
2014 pdp_addr = env->cr[3] & ~0x1f;
2015 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002016 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002017 pdpe = le64_to_cpu(pdpe);
2018 if (pdpe & PG_PRESENT_MASK) {
2019 pd_addr = pdpe & 0x3fffffffff000ULL;
2020 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002021 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002022 pde = le64_to_cpu(pde);
2023 if (pde & PG_PRESENT_MASK) {
2024 if (pde & PG_PSE_MASK) {
2025 /* 2M pages with PAE, CR4.PSE is ignored */
2026 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2027 ~((target_phys_addr_t)(1 << 20) - 1));
2028 } else {
2029 pt_addr = pde & 0x3fffffffff000ULL;
2030 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002031 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002032 pte = le64_to_cpu(pte);
2033 if (pte & PG_PRESENT_MASK) {
2034 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2035 + (l3 << 12),
2036 pte & ~PG_PSE_MASK,
2037 ~(target_phys_addr_t)0xfff);
2038 }
2039 }
2040 }
2041 }
2042 }
2043 }
2044 }
2045}
2046
2047#ifdef TARGET_X86_64
2048static void tlb_info_64(Monitor *mon, CPUState *env)
2049{
2050 uint64_t l1, l2, l3, l4;
2051 uint64_t pml4e, pdpe, pde, pte;
2052 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2053
2054 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2055 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002056 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002057 pml4e = le64_to_cpu(pml4e);
2058 if (pml4e & PG_PRESENT_MASK) {
2059 pdp_addr = pml4e & 0x3fffffffff000ULL;
2060 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002061 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002062 pdpe = le64_to_cpu(pdpe);
2063 if (pdpe & PG_PRESENT_MASK) {
2064 if (pdpe & PG_PSE_MASK) {
2065 /* 1G pages, CR4.PSE is ignored */
2066 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2067 0x3ffffc0000000ULL);
2068 } else {
2069 pd_addr = pdpe & 0x3fffffffff000ULL;
2070 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002071 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002072 pde = le64_to_cpu(pde);
2073 if (pde & PG_PRESENT_MASK) {
2074 if (pde & PG_PSE_MASK) {
2075 /* 2M pages, CR4.PSE is ignored */
2076 print_pte(mon, (l1 << 39) + (l2 << 30) +
2077 (l3 << 21), pde,
2078 0x3ffffffe00000ULL);
2079 } else {
2080 pt_addr = pde & 0x3fffffffff000ULL;
2081 for (l4 = 0; l4 < 512; l4++) {
2082 cpu_physical_memory_read(pt_addr
2083 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002084 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002085 pte = le64_to_cpu(pte);
2086 if (pte & PG_PRESENT_MASK) {
2087 print_pte(mon, (l1 << 39) +
2088 (l2 << 30) +
2089 (l3 << 21) + (l4 << 12),
2090 pte & ~PG_PSE_MASK,
2091 0x3fffffffff000ULL);
2092 }
2093 }
2094 }
2095 }
2096 }
2097 }
2098 }
2099 }
2100 }
2101 }
2102}
2103#endif
2104
2105static void tlb_info(Monitor *mon)
2106{
2107 CPUState *env;
2108
2109 env = mon_get_cpu();
2110
2111 if (!(env->cr[0] & CR0_PG_MASK)) {
2112 monitor_printf(mon, "PG disabled\n");
2113 return;
2114 }
2115 if (env->cr[4] & CR4_PAE_MASK) {
2116#ifdef TARGET_X86_64
2117 if (env->hflags & HF_LMA_MASK) {
2118 tlb_info_64(mon, env);
2119 } else
2120#endif
2121 {
2122 tlb_info_pae32(mon, env);
2123 }
2124 } else {
2125 tlb_info_32(mon, env);
2126 }
2127}
2128
Blue Swirl1b3cba62010-12-11 18:56:27 +00002129static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2130 int *plast_prot,
2131 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002132{
bellard9746b152004-11-11 18:30:24 +00002133 int prot1;
2134 prot1 = *plast_prot;
2135 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002136 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002137 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2138 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002139 *pstart, end, end - *pstart,
2140 prot1 & PG_USER_MASK ? 'u' : '-',
2141 'r',
2142 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002143 }
2144 if (prot != 0)
2145 *pstart = end;
2146 else
2147 *pstart = -1;
2148 *plast_prot = prot;
2149 }
2150}
2151
Blue Swirl1b3cba62010-12-11 18:56:27 +00002152static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002153{
Austin Clementsb49ca722011-08-14 23:19:21 -04002154 unsigned int l1, l2;
2155 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002156 uint32_t pgd, pde, pte;
2157 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002158
bellardb86bda52004-09-18 19:32:46 +00002159 pgd = env->cr[3] & ~0xfff;
2160 last_prot = 0;
2161 start = -1;
2162 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002163 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002164 pde = le32_to_cpu(pde);
2165 end = l1 << 22;
2166 if (pde & PG_PRESENT_MASK) {
2167 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2168 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002169 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002170 } else {
2171 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002172 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002173 pte = le32_to_cpu(pte);
2174 end = (l1 << 22) + (l2 << 12);
2175 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002176 prot = pte & pde &
2177 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002178 } else {
2179 prot = 0;
2180 }
aliguori376253e2009-03-05 23:01:23 +00002181 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002182 }
2183 }
2184 } else {
2185 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002186 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002187 }
2188 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002189 /* Flush last range */
2190 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002191}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002192
2193static void mem_info_pae32(Monitor *mon, CPUState *env)
2194{
Austin Clementsb49ca722011-08-14 23:19:21 -04002195 unsigned int l1, l2, l3;
2196 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002197 uint64_t pdpe, pde, pte;
2198 uint64_t pdp_addr, pd_addr, pt_addr;
2199 target_phys_addr_t start, end;
2200
2201 pdp_addr = env->cr[3] & ~0x1f;
2202 last_prot = 0;
2203 start = -1;
2204 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002205 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002206 pdpe = le64_to_cpu(pdpe);
2207 end = l1 << 30;
2208 if (pdpe & PG_PRESENT_MASK) {
2209 pd_addr = pdpe & 0x3fffffffff000ULL;
2210 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002211 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002212 pde = le64_to_cpu(pde);
2213 end = (l1 << 30) + (l2 << 21);
2214 if (pde & PG_PRESENT_MASK) {
2215 if (pde & PG_PSE_MASK) {
2216 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2217 PG_PRESENT_MASK);
2218 mem_print(mon, &start, &last_prot, end, prot);
2219 } else {
2220 pt_addr = pde & 0x3fffffffff000ULL;
2221 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002222 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002223 pte = le64_to_cpu(pte);
2224 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2225 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002226 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2227 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002228 } else {
2229 prot = 0;
2230 }
2231 mem_print(mon, &start, &last_prot, end, prot);
2232 }
2233 }
2234 } else {
2235 prot = 0;
2236 mem_print(mon, &start, &last_prot, end, prot);
2237 }
2238 }
2239 } else {
2240 prot = 0;
2241 mem_print(mon, &start, &last_prot, end, prot);
2242 }
2243 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002244 /* Flush last range */
2245 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002246}
2247
2248
2249#ifdef TARGET_X86_64
2250static void mem_info_64(Monitor *mon, CPUState *env)
2251{
2252 int prot, last_prot;
2253 uint64_t l1, l2, l3, l4;
2254 uint64_t pml4e, pdpe, pde, pte;
2255 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2256
2257 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2258 last_prot = 0;
2259 start = -1;
2260 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002261 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002262 pml4e = le64_to_cpu(pml4e);
2263 end = l1 << 39;
2264 if (pml4e & PG_PRESENT_MASK) {
2265 pdp_addr = pml4e & 0x3fffffffff000ULL;
2266 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002267 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002268 pdpe = le64_to_cpu(pdpe);
2269 end = (l1 << 39) + (l2 << 30);
2270 if (pdpe & PG_PRESENT_MASK) {
2271 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002272 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2273 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002274 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002275 mem_print(mon, &start, &last_prot, end, prot);
2276 } else {
2277 pd_addr = pdpe & 0x3fffffffff000ULL;
2278 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002279 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002280 pde = le64_to_cpu(pde);
2281 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2282 if (pde & PG_PRESENT_MASK) {
2283 if (pde & PG_PSE_MASK) {
2284 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2285 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002286 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002287 mem_print(mon, &start, &last_prot, end, prot);
2288 } else {
2289 pt_addr = pde & 0x3fffffffff000ULL;
2290 for (l4 = 0; l4 < 512; l4++) {
2291 cpu_physical_memory_read(pt_addr
2292 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002293 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002294 pte = le64_to_cpu(pte);
2295 end = (l1 << 39) + (l2 << 30) +
2296 (l3 << 21) + (l4 << 12);
2297 if (pte & PG_PRESENT_MASK) {
2298 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2299 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002300 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002301 } else {
2302 prot = 0;
2303 }
2304 mem_print(mon, &start, &last_prot, end, prot);
2305 }
2306 }
2307 } else {
2308 prot = 0;
2309 mem_print(mon, &start, &last_prot, end, prot);
2310 }
2311 }
2312 }
2313 } else {
2314 prot = 0;
2315 mem_print(mon, &start, &last_prot, end, prot);
2316 }
2317 }
2318 } else {
2319 prot = 0;
2320 mem_print(mon, &start, &last_prot, end, prot);
2321 }
2322 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002323 /* Flush last range */
2324 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002325}
2326#endif
2327
2328static void mem_info(Monitor *mon)
2329{
2330 CPUState *env;
2331
2332 env = mon_get_cpu();
2333
2334 if (!(env->cr[0] & CR0_PG_MASK)) {
2335 monitor_printf(mon, "PG disabled\n");
2336 return;
2337 }
2338 if (env->cr[4] & CR4_PAE_MASK) {
2339#ifdef TARGET_X86_64
2340 if (env->hflags & HF_LMA_MASK) {
2341 mem_info_64(mon, env);
2342 } else
2343#endif
2344 {
2345 mem_info_pae32(mon, env);
2346 }
2347 } else {
2348 mem_info_32(mon, env);
2349 }
2350}
bellardb86bda52004-09-18 19:32:46 +00002351#endif
2352
aurel327c664e22009-03-03 06:12:22 +00002353#if defined(TARGET_SH4)
2354
aliguori376253e2009-03-05 23:01:23 +00002355static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002356{
aliguori376253e2009-03-05 23:01:23 +00002357 monitor_printf(mon, " tlb%i:\t"
2358 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2359 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2360 "dirty=%hhu writethrough=%hhu\n",
2361 idx,
2362 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2363 tlb->v, tlb->sh, tlb->c, tlb->pr,
2364 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002365}
2366
aliguori376253e2009-03-05 23:01:23 +00002367static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002368{
2369 CPUState *env = mon_get_cpu();
2370 int i;
2371
aliguori376253e2009-03-05 23:01:23 +00002372 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002373 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002374 print_tlb (mon, i, &env->itlb[i]);
2375 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002376 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002377 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002378}
2379
2380#endif
2381
Scott Woodbebabbc2011-08-18 10:38:42 +00002382#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002383static void tlb_info(Monitor *mon)
2384{
2385 CPUState *env1 = mon_get_cpu();
2386
2387 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2388}
2389#endif
2390
Blue Swirl314e2982011-09-11 20:22:05 +00002391static void do_info_mtree(Monitor *mon)
2392{
2393 mtree_info((fprintf_function)monitor_printf, mon);
2394}
2395
aliguori030ea372009-04-21 22:30:47 +00002396static void do_info_numa(Monitor *mon)
2397{
aliguorib28b6232009-04-22 20:20:29 +00002398 int i;
aliguori030ea372009-04-21 22:30:47 +00002399 CPUState *env;
2400
2401 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2402 for (i = 0; i < nb_numa_nodes; i++) {
2403 monitor_printf(mon, "node %d cpus:", i);
2404 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2405 if (env->numa_node == i) {
2406 monitor_printf(mon, " %d", env->cpu_index);
2407 }
2408 }
2409 monitor_printf(mon, "\n");
2410 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2411 node_mem[i] >> 20);
2412 }
2413}
2414
bellard5f1ce942006-02-08 22:40:15 +00002415#ifdef CONFIG_PROFILER
2416
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002417int64_t qemu_time;
2418int64_t dev_time;
2419
aliguori376253e2009-03-05 23:01:23 +00002420static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002421{
2422 int64_t total;
2423 total = qemu_time;
2424 if (total == 0)
2425 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002426 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002427 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002428 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002429 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002430 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002431 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002432}
2433#else
aliguori376253e2009-03-05 23:01:23 +00002434static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002435{
aliguori376253e2009-03-05 23:01:23 +00002436 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002437}
2438#endif
2439
bellardec36b692006-07-16 18:57:03 +00002440/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002441static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002442
aliguori376253e2009-03-05 23:01:23 +00002443static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002444{
2445 int i;
2446 CaptureState *s;
2447
2448 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002449 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002450 s->ops.info (s->opaque);
2451 }
2452}
2453
Blue Swirl23130862009-06-06 08:22:04 +00002454#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002455static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002456{
2457 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002458 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002459 CaptureState *s;
2460
2461 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2462 if (i == n) {
2463 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002464 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002465 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002466 return;
2467 }
2468 }
2469}
2470
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002471static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002472{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002473 const char *path = qdict_get_str(qdict, "path");
2474 int has_freq = qdict_haskey(qdict, "freq");
2475 int freq = qdict_get_try_int(qdict, "freq", -1);
2476 int has_bits = qdict_haskey(qdict, "bits");
2477 int bits = qdict_get_try_int(qdict, "bits", -1);
2478 int has_channels = qdict_haskey(qdict, "nchannels");
2479 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002480 CaptureState *s;
2481
Anthony Liguori7267c092011-08-20 22:09:37 -05002482 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002483
2484 freq = has_freq ? freq : 44100;
2485 bits = has_bits ? bits : 16;
2486 nchannels = has_channels ? nchannels : 2;
2487
2488 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002489 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002490 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002491 return;
bellardec36b692006-07-16 18:57:03 +00002492 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002493 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002494}
2495#endif
2496
aurel32dc1c0b72008-04-27 23:52:12 +00002497#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002498static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002499{
2500 CPUState *env;
2501
2502 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2503 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2504 }
2505
2506 return 0;
2507}
2508#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002509static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002510{
2511 qerror_report(QERR_UNSUPPORTED);
2512 return -1;
2513}
aurel32dc1c0b72008-04-27 23:52:12 +00002514#endif
2515
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002516static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002517{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002518 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002519
aliguori76655d62009-03-06 20:27:37 +00002520 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002521 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002522 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002523 return acl;
2524}
aliguori76655d62009-03-06 20:27:37 +00002525
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002526static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002527{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002528 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002529 qemu_acl *acl = find_acl(mon, aclname);
2530 qemu_acl_entry *entry;
2531 int i = 0;
2532
2533 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002534 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002535 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002536 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002537 i++;
2538 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002539 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002540 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002541 }
2542}
2543
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002544static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002545{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002546 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002547 qemu_acl *acl = find_acl(mon, aclname);
2548
2549 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002550 qemu_acl_reset(acl);
2551 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002552 }
2553}
aliguori76655d62009-03-06 20:27:37 +00002554
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002555static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002556{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002557 const char *aclname = qdict_get_str(qdict, "aclname");
2558 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002559 qemu_acl *acl = find_acl(mon, aclname);
2560
2561 if (acl) {
2562 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002563 acl->defaultDeny = 0;
2564 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002565 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002566 acl->defaultDeny = 1;
2567 monitor_printf(mon, "acl: policy set to 'deny'\n");
2568 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002569 monitor_printf(mon, "acl: unknown policy '%s', "
2570 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002571 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002572 }
2573}
aliguori76655d62009-03-06 20:27:37 +00002574
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002575static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002576{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002577 const char *aclname = qdict_get_str(qdict, "aclname");
2578 const char *match = qdict_get_str(qdict, "match");
2579 const char *policy = qdict_get_str(qdict, "policy");
2580 int has_index = qdict_haskey(qdict, "index");
2581 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002582 qemu_acl *acl = find_acl(mon, aclname);
2583 int deny, ret;
2584
2585 if (acl) {
2586 if (strcmp(policy, "allow") == 0) {
2587 deny = 0;
2588 } else if (strcmp(policy, "deny") == 0) {
2589 deny = 1;
2590 } else {
2591 monitor_printf(mon, "acl: unknown policy '%s', "
2592 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002593 return;
2594 }
aliguori28a76be2009-03-06 20:27:40 +00002595 if (has_index)
2596 ret = qemu_acl_insert(acl, deny, match, index);
2597 else
2598 ret = qemu_acl_append(acl, deny, match);
2599 if (ret < 0)
2600 monitor_printf(mon, "acl: unable to add acl entry\n");
2601 else
2602 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002603 }
2604}
aliguori76655d62009-03-06 20:27:37 +00002605
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002606static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002607{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002608 const char *aclname = qdict_get_str(qdict, "aclname");
2609 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002610 qemu_acl *acl = find_acl(mon, aclname);
2611 int ret;
aliguori76655d62009-03-06 20:27:37 +00002612
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002613 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002614 ret = qemu_acl_remove(acl, match);
2615 if (ret < 0)
2616 monitor_printf(mon, "acl: no matching acl entry\n");
2617 else
2618 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002619 }
2620}
2621
Huang Ying79c4f6b2009-06-23 10:05:14 +08002622#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002623static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002624{
2625 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002626 int cpu_index = qdict_get_int(qdict, "cpu_index");
2627 int bank = qdict_get_int(qdict, "bank");
2628 uint64_t status = qdict_get_int(qdict, "status");
2629 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2630 uint64_t addr = qdict_get_int(qdict, "addr");
2631 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002632 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002633
Jan Kiszka747461c2011-03-02 08:56:10 +01002634 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2635 flags |= MCE_INJECT_BROADCAST;
2636 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002637 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002638 if (cenv->cpu_index == cpu_index) {
2639 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002640 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002641 break;
2642 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002643 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002644}
2645#endif
2646
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002647static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002648{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002649 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002650 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002651 int fd;
2652
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002653 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002654 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002655 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002656 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002657 }
2658
2659 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002660 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2661 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002662 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002663 }
2664
Blue Swirl72cf2d42009-09-12 07:36:22 +00002665 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002666 if (strcmp(monfd->name, fdname) != 0) {
2667 continue;
2668 }
2669
2670 close(monfd->fd);
2671 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002672 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002673 }
2674
Anthony Liguori7267c092011-08-20 22:09:37 -05002675 monfd = g_malloc0(sizeof(mon_fd_t));
2676 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002677 monfd->fd = fd;
2678
Blue Swirl72cf2d42009-09-12 07:36:22 +00002679 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002680 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002681}
2682
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002683static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002684{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002685 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002686 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002687
Blue Swirl72cf2d42009-09-12 07:36:22 +00002688 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002689 if (strcmp(monfd->name, fdname) != 0) {
2690 continue;
2691 }
2692
Blue Swirl72cf2d42009-09-12 07:36:22 +00002693 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002694 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002695 g_free(monfd->name);
2696 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002697 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002698 }
2699
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002700 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002701 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002702}
2703
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002704static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002705{
Luiz Capitulino13548692011-07-29 15:36:43 -03002706 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002707 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002708
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002709 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002710
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002711 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002712 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002713 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002714}
2715
Mark McLoughlin7768e042009-07-22 09:11:41 +01002716int monitor_get_fd(Monitor *mon, const char *fdname)
2717{
Anthony Liguoric227f092009-10-01 16:12:16 -05002718 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002719
Blue Swirl72cf2d42009-09-12 07:36:22 +00002720 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002721 int fd;
2722
2723 if (strcmp(monfd->name, fdname) != 0) {
2724 continue;
2725 }
2726
2727 fd = monfd->fd;
2728
2729 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002730 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002731 g_free(monfd->name);
2732 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002733
2734 return fd;
2735 }
2736
2737 return -1;
2738}
2739
Anthony Liguoric227f092009-10-01 16:12:16 -05002740static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002741#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002742 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002743};
2744
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002745/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002746static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 {
2748 .name = "version",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002752 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
2754 {
2755 .name = "network",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002759 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
2761 {
2762 .name = "chardev",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002766 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 },
2768 {
2769 .name = "block",
2770 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .params = "",
2772 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002773 .user_print = bdrv_info_print,
2774 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 },
2776 {
2777 .name = "blockstats",
2778 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002779 .params = "",
2780 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002781 .user_print = bdrv_stats_print,
2782 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 },
2784 {
2785 .name = "registers",
2786 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 .params = "",
2788 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002789 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002790 },
2791 {
2792 .name = "cpus",
2793 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 .params = "",
2795 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002796 .user_print = monitor_print_cpus,
2797 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002798 },
2799 {
2800 .name = "history",
2801 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 .params = "",
2803 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002804 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002806#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2807 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 {
2809 .name = "irq",
2810 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 .params = "",
2812 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002813#ifdef TARGET_SPARC
2814 .mhandler.info = sun4m_irq_info,
2815#elif defined(TARGET_LM32)
2816 .mhandler.info = lm32_irq_info,
2817#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002818 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002819#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 },
2821 {
2822 .name = "pic",
2823 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002824 .params = "",
2825 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002826#ifdef TARGET_SPARC
2827 .mhandler.info = sun4m_pic_info,
2828#elif defined(TARGET_LM32)
2829 .mhandler.info = lm32_do_pic_info,
2830#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002831 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002832#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002834#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002835 {
2836 .name = "pci",
2837 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 .params = "",
2839 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002840 .user_print = do_pci_info_print,
2841 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002842 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002843#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2844 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 {
2846 .name = "tlb",
2847 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 .params = "",
2849 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002850 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002851 },
aurel327c664e22009-03-03 06:12:22 +00002852#endif
2853#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002854 {
2855 .name = "mem",
2856 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002857 .params = "",
2858 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002859 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002860 },
bellardb86bda52004-09-18 19:32:46 +00002861#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002862 {
Blue Swirl314e2982011-09-11 20:22:05 +00002863 .name = "mtree",
2864 .args_type = "",
2865 .params = "",
2866 .help = "show memory tree",
2867 .mhandler.info = do_info_mtree,
2868 },
2869 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002870 .name = "jit",
2871 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 .params = "",
2873 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002874 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 },
2876 {
2877 .name = "kvm",
2878 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002879 .params = "",
2880 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002881 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002882 },
2883 {
2884 .name = "numa",
2885 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002886 .params = "",
2887 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002888 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 },
2890 {
2891 .name = "usb",
2892 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002893 .params = "",
2894 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002895 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 },
2897 {
2898 .name = "usbhost",
2899 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002900 .params = "",
2901 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002902 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 },
2904 {
2905 .name = "profile",
2906 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002907 .params = "",
2908 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002909 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002910 },
2911 {
2912 .name = "capture",
2913 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002914 .params = "",
2915 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002916 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002917 },
2918 {
2919 .name = "snapshots",
2920 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002921 .params = "",
2922 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002923 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002924 },
2925 {
2926 .name = "status",
2927 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002928 .params = "",
2929 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002930 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002931 },
2932 {
2933 .name = "pcmcia",
2934 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002935 .params = "",
2936 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002937 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002938 },
2939 {
2940 .name = "mice",
2941 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002942 .params = "",
2943 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002944 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002945 },
2946 {
2947 .name = "vnc",
2948 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002949 .params = "",
2950 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002951 .user_print = do_info_vnc_print,
2952 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002953 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002954#if defined(CONFIG_SPICE)
2955 {
2956 .name = "spice",
2957 .args_type = "",
2958 .params = "",
2959 .help = "show the spice server status",
2960 .user_print = do_info_spice_print,
2961 .mhandler.info_new = do_info_spice,
2962 },
2963#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002964 {
2965 .name = "name",
2966 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002967 .params = "",
2968 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002969 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002970 },
2971 {
2972 .name = "uuid",
2973 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002974 .params = "",
2975 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002976 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002977 },
j_mayer76a66252007-03-07 08:32:30 +00002978#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002979 {
2980 .name = "cpustats",
2981 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002982 .params = "",
2983 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002984 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002985 },
j_mayer76a66252007-03-07 08:32:30 +00002986#endif
blueswir131a60e22007-10-26 18:42:59 +00002987#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002988 {
2989 .name = "usernet",
2990 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002991 .params = "",
2992 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002993 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002994 },
blueswir131a60e22007-10-26 18:42:59 +00002995#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002996 {
2997 .name = "migrate",
2998 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002999 .params = "",
3000 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03003001 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003002 },
3003 {
3004 .name = "balloon",
3005 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003006 .params = "",
3007 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003008 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003009 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003010 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003011 },
3012 {
3013 .name = "qtree",
3014 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003015 .params = "",
3016 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003017 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003018 },
3019 {
3020 .name = "qdm",
3021 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003022 .params = "",
3023 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003024 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003025 },
3026 {
3027 .name = "roms",
3028 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003029 .params = "",
3030 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003031 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003032 },
Lluís6d8a7642011-08-31 20:30:43 +02003033#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303034 {
3035 .name = "trace",
3036 .args_type = "",
3037 .params = "",
3038 .help = "show current contents of trace buffer",
3039 .mhandler.info = do_info_trace,
3040 },
Lluís31965ae2011-08-31 20:31:24 +02003041#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303042 {
3043 .name = "trace-events",
3044 .args_type = "",
3045 .params = "",
3046 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003047 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303048 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003049 {
3050 .name = NULL,
3051 },
bellard9dc39cb2004-03-14 21:38:27 +00003052};
3053
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003054static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05003055#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003056 { /* NULL */ },
3057};
3058
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003059static const mon_cmd_t qmp_query_cmds[] = {
3060 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003061 .name = "block",
3062 .args_type = "",
3063 .params = "",
3064 .help = "show the block devices",
3065 .user_print = bdrv_info_print,
3066 .mhandler.info_new = bdrv_info,
3067 },
3068 {
3069 .name = "blockstats",
3070 .args_type = "",
3071 .params = "",
3072 .help = "show block device statistics",
3073 .user_print = bdrv_stats_print,
3074 .mhandler.info_new = bdrv_info_stats,
3075 },
3076 {
3077 .name = "cpus",
3078 .args_type = "",
3079 .params = "",
3080 .help = "show infos for each CPU",
3081 .user_print = monitor_print_cpus,
3082 .mhandler.info_new = do_info_cpus,
3083 },
3084 {
3085 .name = "pci",
3086 .args_type = "",
3087 .params = "",
3088 .help = "show PCI info",
3089 .user_print = do_pci_info_print,
3090 .mhandler.info_new = do_pci_info,
3091 },
3092 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003093 .name = "vnc",
3094 .args_type = "",
3095 .params = "",
3096 .help = "show the vnc server status",
3097 .user_print = do_info_vnc_print,
3098 .mhandler.info_new = do_info_vnc,
3099 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003100#if defined(CONFIG_SPICE)
3101 {
3102 .name = "spice",
3103 .args_type = "",
3104 .params = "",
3105 .help = "show the spice server status",
3106 .user_print = do_info_spice_print,
3107 .mhandler.info_new = do_info_spice,
3108 },
3109#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003110 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003111 .name = "balloon",
3112 .args_type = "",
3113 .params = "",
3114 .help = "show balloon information",
3115 .user_print = monitor_print_balloon,
3116 .mhandler.info_async = do_info_balloon,
3117 .flags = MONITOR_CMD_ASYNC,
3118 },
3119 { /* NULL */ },
3120};
3121
bellard9307c4c2004-04-04 12:57:25 +00003122/*******************************************************************/
3123
3124static const char *pch;
3125static jmp_buf expr_env;
3126
bellard92a31b12005-02-10 22:00:52 +00003127#define MD_TLONG 0
3128#define MD_I32 1
3129
bellard9307c4c2004-04-04 12:57:25 +00003130typedef struct MonitorDef {
3131 const char *name;
3132 int offset;
blueswir18662d652008-10-02 18:32:44 +00003133 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003134 int type;
bellard9307c4c2004-04-04 12:57:25 +00003135} MonitorDef;
3136
bellard57206fd2004-04-25 18:54:52 +00003137#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003138static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003139{
bellard6a00d602005-11-21 23:25:50 +00003140 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003141 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003142}
3143#endif
3144
bellarda541f292004-04-12 20:39:29 +00003145#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003146static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003147{
bellard6a00d602005-11-21 23:25:50 +00003148 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003149 unsigned int u;
3150 int i;
3151
3152 u = 0;
3153 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003154 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003155
3156 return u;
3157}
3158
blueswir18662d652008-10-02 18:32:44 +00003159static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003160{
bellard6a00d602005-11-21 23:25:50 +00003161 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003162 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003163}
3164
blueswir18662d652008-10-02 18:32:44 +00003165static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003166{
bellard6a00d602005-11-21 23:25:50 +00003167 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003168 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003169}
bellard9fddaa02004-05-21 12:59:32 +00003170
blueswir18662d652008-10-02 18:32:44 +00003171static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003172{
bellard6a00d602005-11-21 23:25:50 +00003173 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003174 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003175}
3176
blueswir18662d652008-10-02 18:32:44 +00003177static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003178{
bellard6a00d602005-11-21 23:25:50 +00003179 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003180 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003181}
3182
blueswir18662d652008-10-02 18:32:44 +00003183static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003184{
bellard6a00d602005-11-21 23:25:50 +00003185 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003186 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003187}
bellarda541f292004-04-12 20:39:29 +00003188#endif
3189
bellarde95c8d52004-09-30 22:22:08 +00003190#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003191#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003192static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003193{
bellard6a00d602005-11-21 23:25:50 +00003194 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003195
3196 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003197}
bellard7b936c02005-10-30 17:05:13 +00003198#endif
bellarde95c8d52004-09-30 22:22:08 +00003199
blueswir18662d652008-10-02 18:32:44 +00003200static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003201{
bellard6a00d602005-11-21 23:25:50 +00003202 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003203 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003204}
3205#endif
3206
blueswir18662d652008-10-02 18:32:44 +00003207static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003208#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003209
3210#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003211 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003212 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003213 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003214
bellard9307c4c2004-04-04 12:57:25 +00003215 { "eax", offsetof(CPUState, regs[0]) },
3216 { "ecx", offsetof(CPUState, regs[1]) },
3217 { "edx", offsetof(CPUState, regs[2]) },
3218 { "ebx", offsetof(CPUState, regs[3]) },
3219 { "esp|sp", offsetof(CPUState, regs[4]) },
3220 { "ebp|fp", offsetof(CPUState, regs[5]) },
3221 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003222 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003223#ifdef TARGET_X86_64
3224 { "r8", offsetof(CPUState, regs[8]) },
3225 { "r9", offsetof(CPUState, regs[9]) },
3226 { "r10", offsetof(CPUState, regs[10]) },
3227 { "r11", offsetof(CPUState, regs[11]) },
3228 { "r12", offsetof(CPUState, regs[12]) },
3229 { "r13", offsetof(CPUState, regs[13]) },
3230 { "r14", offsetof(CPUState, regs[14]) },
3231 { "r15", offsetof(CPUState, regs[15]) },
3232#endif
bellard9307c4c2004-04-04 12:57:25 +00003233 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003234 { "eip", offsetof(CPUState, eip) },
3235 SEG("cs", R_CS)
3236 SEG("ds", R_DS)
3237 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003238 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003239 SEG("fs", R_FS)
3240 SEG("gs", R_GS)
3241 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003242#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003243 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003244 { "r0", offsetof(CPUState, gpr[0]) },
3245 { "r1", offsetof(CPUState, gpr[1]) },
3246 { "r2", offsetof(CPUState, gpr[2]) },
3247 { "r3", offsetof(CPUState, gpr[3]) },
3248 { "r4", offsetof(CPUState, gpr[4]) },
3249 { "r5", offsetof(CPUState, gpr[5]) },
3250 { "r6", offsetof(CPUState, gpr[6]) },
3251 { "r7", offsetof(CPUState, gpr[7]) },
3252 { "r8", offsetof(CPUState, gpr[8]) },
3253 { "r9", offsetof(CPUState, gpr[9]) },
3254 { "r10", offsetof(CPUState, gpr[10]) },
3255 { "r11", offsetof(CPUState, gpr[11]) },
3256 { "r12", offsetof(CPUState, gpr[12]) },
3257 { "r13", offsetof(CPUState, gpr[13]) },
3258 { "r14", offsetof(CPUState, gpr[14]) },
3259 { "r15", offsetof(CPUState, gpr[15]) },
3260 { "r16", offsetof(CPUState, gpr[16]) },
3261 { "r17", offsetof(CPUState, gpr[17]) },
3262 { "r18", offsetof(CPUState, gpr[18]) },
3263 { "r19", offsetof(CPUState, gpr[19]) },
3264 { "r20", offsetof(CPUState, gpr[20]) },
3265 { "r21", offsetof(CPUState, gpr[21]) },
3266 { "r22", offsetof(CPUState, gpr[22]) },
3267 { "r23", offsetof(CPUState, gpr[23]) },
3268 { "r24", offsetof(CPUState, gpr[24]) },
3269 { "r25", offsetof(CPUState, gpr[25]) },
3270 { "r26", offsetof(CPUState, gpr[26]) },
3271 { "r27", offsetof(CPUState, gpr[27]) },
3272 { "r28", offsetof(CPUState, gpr[28]) },
3273 { "r29", offsetof(CPUState, gpr[29]) },
3274 { "r30", offsetof(CPUState, gpr[30]) },
3275 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003276 /* Floating point registers */
3277 { "f0", offsetof(CPUState, fpr[0]) },
3278 { "f1", offsetof(CPUState, fpr[1]) },
3279 { "f2", offsetof(CPUState, fpr[2]) },
3280 { "f3", offsetof(CPUState, fpr[3]) },
3281 { "f4", offsetof(CPUState, fpr[4]) },
3282 { "f5", offsetof(CPUState, fpr[5]) },
3283 { "f6", offsetof(CPUState, fpr[6]) },
3284 { "f7", offsetof(CPUState, fpr[7]) },
3285 { "f8", offsetof(CPUState, fpr[8]) },
3286 { "f9", offsetof(CPUState, fpr[9]) },
3287 { "f10", offsetof(CPUState, fpr[10]) },
3288 { "f11", offsetof(CPUState, fpr[11]) },
3289 { "f12", offsetof(CPUState, fpr[12]) },
3290 { "f13", offsetof(CPUState, fpr[13]) },
3291 { "f14", offsetof(CPUState, fpr[14]) },
3292 { "f15", offsetof(CPUState, fpr[15]) },
3293 { "f16", offsetof(CPUState, fpr[16]) },
3294 { "f17", offsetof(CPUState, fpr[17]) },
3295 { "f18", offsetof(CPUState, fpr[18]) },
3296 { "f19", offsetof(CPUState, fpr[19]) },
3297 { "f20", offsetof(CPUState, fpr[20]) },
3298 { "f21", offsetof(CPUState, fpr[21]) },
3299 { "f22", offsetof(CPUState, fpr[22]) },
3300 { "f23", offsetof(CPUState, fpr[23]) },
3301 { "f24", offsetof(CPUState, fpr[24]) },
3302 { "f25", offsetof(CPUState, fpr[25]) },
3303 { "f26", offsetof(CPUState, fpr[26]) },
3304 { "f27", offsetof(CPUState, fpr[27]) },
3305 { "f28", offsetof(CPUState, fpr[28]) },
3306 { "f29", offsetof(CPUState, fpr[29]) },
3307 { "f30", offsetof(CPUState, fpr[30]) },
3308 { "f31", offsetof(CPUState, fpr[31]) },
3309 { "fpscr", offsetof(CPUState, fpscr) },
3310 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003311 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003312 { "lr", offsetof(CPUState, lr) },
3313 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003314 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003315 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003316 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003317 { "msr", 0, &monitor_get_msr, },
3318 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003319 { "tbu", 0, &monitor_get_tbu, },
3320 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003321#if defined(TARGET_PPC64)
3322 /* Address space register */
3323 { "asr", offsetof(CPUState, asr) },
3324#endif
3325 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003326 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003327 { "sr0", offsetof(CPUState, sr[0]) },
3328 { "sr1", offsetof(CPUState, sr[1]) },
3329 { "sr2", offsetof(CPUState, sr[2]) },
3330 { "sr3", offsetof(CPUState, sr[3]) },
3331 { "sr4", offsetof(CPUState, sr[4]) },
3332 { "sr5", offsetof(CPUState, sr[5]) },
3333 { "sr6", offsetof(CPUState, sr[6]) },
3334 { "sr7", offsetof(CPUState, sr[7]) },
3335 { "sr8", offsetof(CPUState, sr[8]) },
3336 { "sr9", offsetof(CPUState, sr[9]) },
3337 { "sr10", offsetof(CPUState, sr[10]) },
3338 { "sr11", offsetof(CPUState, sr[11]) },
3339 { "sr12", offsetof(CPUState, sr[12]) },
3340 { "sr13", offsetof(CPUState, sr[13]) },
3341 { "sr14", offsetof(CPUState, sr[14]) },
3342 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003343 /* Too lazy to put BATs... */
3344 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3345
3346 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3347 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3348 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3349 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3350 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3351 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3352 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3353 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3354 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3355 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3356 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3357 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3358 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3359 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3360 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3361 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3362 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3363 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3364 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3365 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3366 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3367 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3368 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3369 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3370 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3371 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3372 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3373 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3374 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3375 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3376 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3377 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3378 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3379 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3380 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3381 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3382 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3383 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3384 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3385 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3386 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3387 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3388 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3389 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3390 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3391 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3392 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3393 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3394 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3395 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3396 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3397 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3398 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3399 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3400 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3401 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3402 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3403 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3404 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3405 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3406 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3407 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3408 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3409 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3410 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3411 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3412
bellarde95c8d52004-09-30 22:22:08 +00003413#elif defined(TARGET_SPARC)
3414 { "g0", offsetof(CPUState, gregs[0]) },
3415 { "g1", offsetof(CPUState, gregs[1]) },
3416 { "g2", offsetof(CPUState, gregs[2]) },
3417 { "g3", offsetof(CPUState, gregs[3]) },
3418 { "g4", offsetof(CPUState, gregs[4]) },
3419 { "g5", offsetof(CPUState, gregs[5]) },
3420 { "g6", offsetof(CPUState, gregs[6]) },
3421 { "g7", offsetof(CPUState, gregs[7]) },
3422 { "o0", 0, monitor_get_reg },
3423 { "o1", 1, monitor_get_reg },
3424 { "o2", 2, monitor_get_reg },
3425 { "o3", 3, monitor_get_reg },
3426 { "o4", 4, monitor_get_reg },
3427 { "o5", 5, monitor_get_reg },
3428 { "o6", 6, monitor_get_reg },
3429 { "o7", 7, monitor_get_reg },
3430 { "l0", 8, monitor_get_reg },
3431 { "l1", 9, monitor_get_reg },
3432 { "l2", 10, monitor_get_reg },
3433 { "l3", 11, monitor_get_reg },
3434 { "l4", 12, monitor_get_reg },
3435 { "l5", 13, monitor_get_reg },
3436 { "l6", 14, monitor_get_reg },
3437 { "l7", 15, monitor_get_reg },
3438 { "i0", 16, monitor_get_reg },
3439 { "i1", 17, monitor_get_reg },
3440 { "i2", 18, monitor_get_reg },
3441 { "i3", 19, monitor_get_reg },
3442 { "i4", 20, monitor_get_reg },
3443 { "i5", 21, monitor_get_reg },
3444 { "i6", 22, monitor_get_reg },
3445 { "i7", 23, monitor_get_reg },
3446 { "pc", offsetof(CPUState, pc) },
3447 { "npc", offsetof(CPUState, npc) },
3448 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003449#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003450 { "psr", 0, &monitor_get_psr, },
3451 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003452#endif
bellarde95c8d52004-09-30 22:22:08 +00003453 { "tbr", offsetof(CPUState, tbr) },
3454 { "fsr", offsetof(CPUState, fsr) },
3455 { "f0", offsetof(CPUState, fpr[0]) },
3456 { "f1", offsetof(CPUState, fpr[1]) },
3457 { "f2", offsetof(CPUState, fpr[2]) },
3458 { "f3", offsetof(CPUState, fpr[3]) },
3459 { "f4", offsetof(CPUState, fpr[4]) },
3460 { "f5", offsetof(CPUState, fpr[5]) },
3461 { "f6", offsetof(CPUState, fpr[6]) },
3462 { "f7", offsetof(CPUState, fpr[7]) },
3463 { "f8", offsetof(CPUState, fpr[8]) },
3464 { "f9", offsetof(CPUState, fpr[9]) },
3465 { "f10", offsetof(CPUState, fpr[10]) },
3466 { "f11", offsetof(CPUState, fpr[11]) },
3467 { "f12", offsetof(CPUState, fpr[12]) },
3468 { "f13", offsetof(CPUState, fpr[13]) },
3469 { "f14", offsetof(CPUState, fpr[14]) },
3470 { "f15", offsetof(CPUState, fpr[15]) },
3471 { "f16", offsetof(CPUState, fpr[16]) },
3472 { "f17", offsetof(CPUState, fpr[17]) },
3473 { "f18", offsetof(CPUState, fpr[18]) },
3474 { "f19", offsetof(CPUState, fpr[19]) },
3475 { "f20", offsetof(CPUState, fpr[20]) },
3476 { "f21", offsetof(CPUState, fpr[21]) },
3477 { "f22", offsetof(CPUState, fpr[22]) },
3478 { "f23", offsetof(CPUState, fpr[23]) },
3479 { "f24", offsetof(CPUState, fpr[24]) },
3480 { "f25", offsetof(CPUState, fpr[25]) },
3481 { "f26", offsetof(CPUState, fpr[26]) },
3482 { "f27", offsetof(CPUState, fpr[27]) },
3483 { "f28", offsetof(CPUState, fpr[28]) },
3484 { "f29", offsetof(CPUState, fpr[29]) },
3485 { "f30", offsetof(CPUState, fpr[30]) },
3486 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003487#ifdef TARGET_SPARC64
3488 { "f32", offsetof(CPUState, fpr[32]) },
3489 { "f34", offsetof(CPUState, fpr[34]) },
3490 { "f36", offsetof(CPUState, fpr[36]) },
3491 { "f38", offsetof(CPUState, fpr[38]) },
3492 { "f40", offsetof(CPUState, fpr[40]) },
3493 { "f42", offsetof(CPUState, fpr[42]) },
3494 { "f44", offsetof(CPUState, fpr[44]) },
3495 { "f46", offsetof(CPUState, fpr[46]) },
3496 { "f48", offsetof(CPUState, fpr[48]) },
3497 { "f50", offsetof(CPUState, fpr[50]) },
3498 { "f52", offsetof(CPUState, fpr[52]) },
3499 { "f54", offsetof(CPUState, fpr[54]) },
3500 { "f56", offsetof(CPUState, fpr[56]) },
3501 { "f58", offsetof(CPUState, fpr[58]) },
3502 { "f60", offsetof(CPUState, fpr[60]) },
3503 { "f62", offsetof(CPUState, fpr[62]) },
3504 { "asi", offsetof(CPUState, asi) },
3505 { "pstate", offsetof(CPUState, pstate) },
3506 { "cansave", offsetof(CPUState, cansave) },
3507 { "canrestore", offsetof(CPUState, canrestore) },
3508 { "otherwin", offsetof(CPUState, otherwin) },
3509 { "wstate", offsetof(CPUState, wstate) },
3510 { "cleanwin", offsetof(CPUState, cleanwin) },
3511 { "fprs", offsetof(CPUState, fprs) },
3512#endif
bellard9307c4c2004-04-04 12:57:25 +00003513#endif
3514 { NULL },
3515};
3516
aliguori376253e2009-03-05 23:01:23 +00003517static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003518{
aliguori376253e2009-03-05 23:01:23 +00003519 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003520 longjmp(expr_env, 1);
3521}
3522
Markus Armbruster09b94182010-01-20 13:07:30 +01003523/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003524static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003525{
blueswir18662d652008-10-02 18:32:44 +00003526 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003527 void *ptr;
3528
bellard9307c4c2004-04-04 12:57:25 +00003529 for(md = monitor_defs; md->name != NULL; md++) {
3530 if (compare_cmd(name, md->name)) {
3531 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003532 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003533 } else {
bellard6a00d602005-11-21 23:25:50 +00003534 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003535 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003536 switch(md->type) {
3537 case MD_I32:
3538 *pval = *(int32_t *)ptr;
3539 break;
3540 case MD_TLONG:
3541 *pval = *(target_long *)ptr;
3542 break;
3543 default:
3544 *pval = 0;
3545 break;
3546 }
bellard9307c4c2004-04-04 12:57:25 +00003547 }
3548 return 0;
3549 }
3550 }
3551 return -1;
3552}
3553
3554static void next(void)
3555{
Blue Swirl660f11b2009-07-31 21:16:51 +00003556 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003557 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003558 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003559 pch++;
3560 }
3561}
3562
aliguori376253e2009-03-05 23:01:23 +00003563static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003564
aliguori376253e2009-03-05 23:01:23 +00003565static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003566{
blueswir1c2efc952007-09-25 17:28:42 +00003567 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003568 char *p;
bellard6a00d602005-11-21 23:25:50 +00003569 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003570
3571 switch(*pch) {
3572 case '+':
3573 next();
aliguori376253e2009-03-05 23:01:23 +00003574 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003575 break;
3576 case '-':
3577 next();
aliguori376253e2009-03-05 23:01:23 +00003578 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003579 break;
3580 case '~':
3581 next();
aliguori376253e2009-03-05 23:01:23 +00003582 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003583 break;
3584 case '(':
3585 next();
aliguori376253e2009-03-05 23:01:23 +00003586 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003587 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003588 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003589 }
3590 next();
3591 break;
bellard81d09122004-07-14 17:21:37 +00003592 case '\'':
3593 pch++;
3594 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003595 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003596 n = *pch;
3597 pch++;
3598 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003599 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003600 next();
3601 break;
bellard9307c4c2004-04-04 12:57:25 +00003602 case '$':
3603 {
3604 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003605 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003606
bellard9307c4c2004-04-04 12:57:25 +00003607 pch++;
3608 q = buf;
3609 while ((*pch >= 'a' && *pch <= 'z') ||
3610 (*pch >= 'A' && *pch <= 'Z') ||
3611 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003612 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003613 if ((q - buf) < sizeof(buf) - 1)
3614 *q++ = *pch;
3615 pch++;
3616 }
blueswir1cd390082008-11-16 13:53:32 +00003617 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003618 pch++;
3619 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003620 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003621 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003622 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003623 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003624 }
3625 break;
3626 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003627 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003628 n = 0;
3629 break;
3630 default:
blueswir17743e582007-09-24 18:39:04 +00003631#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003632 n = strtoull(pch, &p, 0);
3633#else
bellard9307c4c2004-04-04 12:57:25 +00003634 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003635#endif
bellard9307c4c2004-04-04 12:57:25 +00003636 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003637 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003638 }
3639 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003640 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003641 pch++;
3642 break;
3643 }
3644 return n;
3645}
3646
3647
aliguori376253e2009-03-05 23:01:23 +00003648static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003649{
blueswir1c2efc952007-09-25 17:28:42 +00003650 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003651 int op;
ths3b46e622007-09-17 08:09:54 +00003652
aliguori376253e2009-03-05 23:01:23 +00003653 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003654 for(;;) {
3655 op = *pch;
3656 if (op != '*' && op != '/' && op != '%')
3657 break;
3658 next();
aliguori376253e2009-03-05 23:01:23 +00003659 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003660 switch(op) {
3661 default:
3662 case '*':
3663 val *= val2;
3664 break;
3665 case '/':
3666 case '%':
ths5fafdf22007-09-16 21:08:06 +00003667 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003668 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003669 if (op == '/')
3670 val /= val2;
3671 else
3672 val %= val2;
3673 break;
3674 }
3675 }
3676 return val;
3677}
3678
aliguori376253e2009-03-05 23:01:23 +00003679static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003680{
blueswir1c2efc952007-09-25 17:28:42 +00003681 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003682 int op;
bellard9307c4c2004-04-04 12:57:25 +00003683
aliguori376253e2009-03-05 23:01:23 +00003684 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003685 for(;;) {
3686 op = *pch;
3687 if (op != '&' && op != '|' && op != '^')
3688 break;
3689 next();
aliguori376253e2009-03-05 23:01:23 +00003690 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003691 switch(op) {
3692 default:
3693 case '&':
3694 val &= val2;
3695 break;
3696 case '|':
3697 val |= val2;
3698 break;
3699 case '^':
3700 val ^= val2;
3701 break;
3702 }
3703 }
3704 return val;
3705}
3706
aliguori376253e2009-03-05 23:01:23 +00003707static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003708{
blueswir1c2efc952007-09-25 17:28:42 +00003709 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003710 int op;
bellard9307c4c2004-04-04 12:57:25 +00003711
aliguori376253e2009-03-05 23:01:23 +00003712 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003713 for(;;) {
3714 op = *pch;
3715 if (op != '+' && op != '-')
3716 break;
3717 next();
aliguori376253e2009-03-05 23:01:23 +00003718 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003719 if (op == '+')
3720 val += val2;
3721 else
3722 val -= val2;
3723 }
3724 return val;
3725}
3726
aliguori376253e2009-03-05 23:01:23 +00003727static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003728{
3729 pch = *pp;
3730 if (setjmp(expr_env)) {
3731 *pp = pch;
3732 return -1;
3733 }
blueswir1cd390082008-11-16 13:53:32 +00003734 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003735 pch++;
aliguori376253e2009-03-05 23:01:23 +00003736 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003737 *pp = pch;
3738 return 0;
3739}
3740
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003741static int get_double(Monitor *mon, double *pval, const char **pp)
3742{
3743 const char *p = *pp;
3744 char *tailp;
3745 double d;
3746
3747 d = strtod(p, &tailp);
3748 if (tailp == p) {
3749 monitor_printf(mon, "Number expected\n");
3750 return -1;
3751 }
3752 if (d != d || d - d != 0) {
3753 /* NaN or infinity */
3754 monitor_printf(mon, "Bad number\n");
3755 return -1;
3756 }
3757 *pval = d;
3758 *pp = tailp;
3759 return 0;
3760}
3761
bellard9307c4c2004-04-04 12:57:25 +00003762static int get_str(char *buf, int buf_size, const char **pp)
3763{
3764 const char *p;
3765 char *q;
3766 int c;
3767
bellard81d09122004-07-14 17:21:37 +00003768 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003769 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003770 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003771 p++;
3772 if (*p == '\0') {
3773 fail:
bellard81d09122004-07-14 17:21:37 +00003774 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003775 *pp = p;
3776 return -1;
3777 }
bellard9307c4c2004-04-04 12:57:25 +00003778 if (*p == '\"') {
3779 p++;
3780 while (*p != '\0' && *p != '\"') {
3781 if (*p == '\\') {
3782 p++;
3783 c = *p++;
3784 switch(c) {
3785 case 'n':
3786 c = '\n';
3787 break;
3788 case 'r':
3789 c = '\r';
3790 break;
3791 case '\\':
3792 case '\'':
3793 case '\"':
3794 break;
3795 default:
3796 qemu_printf("unsupported escape code: '\\%c'\n", c);
3797 goto fail;
3798 }
3799 if ((q - buf) < buf_size - 1) {
3800 *q++ = c;
3801 }
3802 } else {
3803 if ((q - buf) < buf_size - 1) {
3804 *q++ = *p;
3805 }
3806 p++;
3807 }
3808 }
3809 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003810 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003811 goto fail;
3812 }
3813 p++;
3814 } else {
blueswir1cd390082008-11-16 13:53:32 +00003815 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003816 if ((q - buf) < buf_size - 1) {
3817 *q++ = *p;
3818 }
3819 p++;
3820 }
bellard9307c4c2004-04-04 12:57:25 +00003821 }
bellard81d09122004-07-14 17:21:37 +00003822 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003823 *pp = p;
3824 return 0;
3825}
3826
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003827/*
3828 * Store the command-name in cmdname, and return a pointer to
3829 * the remaining of the command string.
3830 */
3831static const char *get_command_name(const char *cmdline,
3832 char *cmdname, size_t nlen)
3833{
3834 size_t len;
3835 const char *p, *pstart;
3836
3837 p = cmdline;
3838 while (qemu_isspace(*p))
3839 p++;
3840 if (*p == '\0')
3841 return NULL;
3842 pstart = p;
3843 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3844 p++;
3845 len = p - pstart;
3846 if (len > nlen - 1)
3847 len = nlen - 1;
3848 memcpy(cmdname, pstart, len);
3849 cmdname[len] = '\0';
3850 return p;
3851}
3852
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003853/**
3854 * Read key of 'type' into 'key' and return the current
3855 * 'type' pointer.
3856 */
3857static char *key_get_info(const char *type, char **key)
3858{
3859 size_t len;
3860 char *p, *str;
3861
3862 if (*type == ',')
3863 type++;
3864
3865 p = strchr(type, ':');
3866 if (!p) {
3867 *key = NULL;
3868 return NULL;
3869 }
3870 len = p - type;
3871
Anthony Liguori7267c092011-08-20 22:09:37 -05003872 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003873 memcpy(str, type, len);
3874 str[len] = '\0';
3875
3876 *key = str;
3877 return ++p;
3878}
3879
bellard9307c4c2004-04-04 12:57:25 +00003880static int default_fmt_format = 'x';
3881static int default_fmt_size = 4;
3882
3883#define MAX_ARGS 16
3884
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003885static int is_valid_option(const char *c, const char *typestr)
3886{
3887 char option[3];
3888
3889 option[0] = '-';
3890 option[1] = *c;
3891 option[2] = '\0';
3892
3893 typestr = strstr(typestr, option);
3894 return (typestr != NULL);
3895}
3896
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003897static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3898 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003899{
3900 const mon_cmd_t *cmd;
3901
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003902 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003903 if (compare_cmd(cmdname, cmd->name)) {
3904 return cmd;
3905 }
3906 }
3907
3908 return NULL;
3909}
3910
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003911static const mon_cmd_t *monitor_find_command(const char *cmdname)
3912{
3913 return search_dispatch_table(mon_cmds, cmdname);
3914}
3915
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003916static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3917{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003918 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003919}
3920
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003921static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3922{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003923 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003924}
3925
Anthony Liguoric227f092009-10-01 16:12:16 -05003926static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003927 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003928 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003929{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003930 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003931 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003932 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003933 char cmdname[256];
3934 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003935 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003936
3937#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003938 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003939#endif
ths3b46e622007-09-17 08:09:54 +00003940
bellard9307c4c2004-04-04 12:57:25 +00003941 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003942 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3943 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003944 return NULL;
ths3b46e622007-09-17 08:09:54 +00003945
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003946 cmd = monitor_find_command(cmdname);
3947 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003948 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003949 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003950 }
bellard9307c4c2004-04-04 12:57:25 +00003951
bellard9307c4c2004-04-04 12:57:25 +00003952 /* parse the parameters */
3953 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003954 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003955 typestr = key_get_info(typestr, &key);
3956 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003957 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003958 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003959 typestr++;
3960 switch(c) {
3961 case 'F':
bellard81d09122004-07-14 17:21:37 +00003962 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003963 case 's':
3964 {
3965 int ret;
ths3b46e622007-09-17 08:09:54 +00003966
blueswir1cd390082008-11-16 13:53:32 +00003967 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003968 p++;
3969 if (*typestr == '?') {
3970 typestr++;
3971 if (*p == '\0') {
3972 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003973 break;
bellard9307c4c2004-04-04 12:57:25 +00003974 }
3975 }
3976 ret = get_str(buf, sizeof(buf), &p);
3977 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003978 switch(c) {
3979 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003980 monitor_printf(mon, "%s: filename expected\n",
3981 cmdname);
bellard81d09122004-07-14 17:21:37 +00003982 break;
3983 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003984 monitor_printf(mon, "%s: block device name expected\n",
3985 cmdname);
bellard81d09122004-07-14 17:21:37 +00003986 break;
3987 default:
aliguori376253e2009-03-05 23:01:23 +00003988 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003989 break;
3990 }
bellard9307c4c2004-04-04 12:57:25 +00003991 goto fail;
3992 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003993 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003994 }
3995 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003996 case 'O':
3997 {
3998 QemuOptsList *opts_list;
3999 QemuOpts *opts;
4000
4001 opts_list = qemu_find_opts(key);
4002 if (!opts_list || opts_list->desc->name) {
4003 goto bad_type;
4004 }
4005 while (qemu_isspace(*p)) {
4006 p++;
4007 }
4008 if (!*p)
4009 break;
4010 if (get_str(buf, sizeof(buf), &p) < 0) {
4011 goto fail;
4012 }
4013 opts = qemu_opts_parse(opts_list, buf, 1);
4014 if (!opts) {
4015 goto fail;
4016 }
4017 qemu_opts_to_qdict(opts, qdict);
4018 qemu_opts_del(opts);
4019 }
4020 break;
bellard9307c4c2004-04-04 12:57:25 +00004021 case '/':
4022 {
4023 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004024
blueswir1cd390082008-11-16 13:53:32 +00004025 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004026 p++;
4027 if (*p == '/') {
4028 /* format found */
4029 p++;
4030 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004031 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004032 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004033 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004034 count = count * 10 + (*p - '0');
4035 p++;
4036 }
4037 }
4038 size = -1;
4039 format = -1;
4040 for(;;) {
4041 switch(*p) {
4042 case 'o':
4043 case 'd':
4044 case 'u':
4045 case 'x':
4046 case 'i':
4047 case 'c':
4048 format = *p++;
4049 break;
4050 case 'b':
4051 size = 1;
4052 p++;
4053 break;
4054 case 'h':
4055 size = 2;
4056 p++;
4057 break;
4058 case 'w':
4059 size = 4;
4060 p++;
4061 break;
4062 case 'g':
4063 case 'L':
4064 size = 8;
4065 p++;
4066 break;
4067 default:
4068 goto next;
4069 }
4070 }
4071 next:
blueswir1cd390082008-11-16 13:53:32 +00004072 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004073 monitor_printf(mon, "invalid char in format: '%c'\n",
4074 *p);
bellard9307c4c2004-04-04 12:57:25 +00004075 goto fail;
4076 }
bellard9307c4c2004-04-04 12:57:25 +00004077 if (format < 0)
4078 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004079 if (format != 'i') {
4080 /* for 'i', not specifying a size gives -1 as size */
4081 if (size < 0)
4082 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004083 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004084 }
bellard9307c4c2004-04-04 12:57:25 +00004085 default_fmt_format = format;
4086 } else {
4087 count = 1;
4088 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004089 if (format != 'i') {
4090 size = default_fmt_size;
4091 } else {
4092 size = -1;
4093 }
bellard9307c4c2004-04-04 12:57:25 +00004094 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004095 qdict_put(qdict, "count", qint_from_int(count));
4096 qdict_put(qdict, "format", qint_from_int(format));
4097 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004098 }
4099 break;
4100 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004101 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004102 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004103 {
blueswir1c2efc952007-09-25 17:28:42 +00004104 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004105
blueswir1cd390082008-11-16 13:53:32 +00004106 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004107 p++;
bellard34405572004-06-08 00:55:58 +00004108 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004109 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004110 if (*p == '\0') {
4111 typestr++;
4112 break;
4113 }
bellard34405572004-06-08 00:55:58 +00004114 } else {
4115 if (*p == '.') {
4116 p++;
blueswir1cd390082008-11-16 13:53:32 +00004117 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004118 p++;
bellard34405572004-06-08 00:55:58 +00004119 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004120 typestr++;
4121 break;
bellard34405572004-06-08 00:55:58 +00004122 }
4123 }
bellard13224a82006-07-14 22:03:35 +00004124 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004125 }
aliguori376253e2009-03-05 23:01:23 +00004126 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004127 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004128 /* Check if 'i' is greater than 32-bit */
4129 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4130 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4131 monitor_printf(mon, "integer is for 32-bit values\n");
4132 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004133 } else if (c == 'M') {
4134 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004135 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004136 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004137 }
4138 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004139 case 'o':
4140 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004141 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004142 char *end;
4143
4144 while (qemu_isspace(*p)) {
4145 p++;
4146 }
4147 if (*typestr == '?') {
4148 typestr++;
4149 if (*p == '\0') {
4150 break;
4151 }
4152 }
4153 val = strtosz(p, &end);
4154 if (val < 0) {
4155 monitor_printf(mon, "invalid size\n");
4156 goto fail;
4157 }
4158 qdict_put(qdict, key, qint_from_int(val));
4159 p = end;
4160 }
4161 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004162 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004163 {
4164 double val;
4165
4166 while (qemu_isspace(*p))
4167 p++;
4168 if (*typestr == '?') {
4169 typestr++;
4170 if (*p == '\0') {
4171 break;
4172 }
4173 }
4174 if (get_double(mon, &val, &p) < 0) {
4175 goto fail;
4176 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004177 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004178 switch (*p) {
4179 case 'm':
4180 val /= 1e3; p += 2; break;
4181 case 'u':
4182 val /= 1e6; p += 2; break;
4183 case 'n':
4184 val /= 1e9; p += 2; break;
4185 }
4186 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004187 if (*p && !qemu_isspace(*p)) {
4188 monitor_printf(mon, "Unknown unit suffix\n");
4189 goto fail;
4190 }
4191 qdict_put(qdict, key, qfloat_from_double(val));
4192 }
4193 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004194 case 'b':
4195 {
4196 const char *beg;
4197 int val;
4198
4199 while (qemu_isspace(*p)) {
4200 p++;
4201 }
4202 beg = p;
4203 while (qemu_isgraph(*p)) {
4204 p++;
4205 }
4206 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4207 val = 1;
4208 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4209 val = 0;
4210 } else {
4211 monitor_printf(mon, "Expected 'on' or 'off'\n");
4212 goto fail;
4213 }
4214 qdict_put(qdict, key, qbool_from_int(val));
4215 }
4216 break;
bellard9307c4c2004-04-04 12:57:25 +00004217 case '-':
4218 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004219 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004220 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004221 /* option */
ths3b46e622007-09-17 08:09:54 +00004222
bellard9307c4c2004-04-04 12:57:25 +00004223 c = *typestr++;
4224 if (c == '\0')
4225 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004226 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004227 p++;
bellard9307c4c2004-04-04 12:57:25 +00004228 if (*p == '-') {
4229 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004230 if(c != *p) {
4231 if(!is_valid_option(p, typestr)) {
4232
4233 monitor_printf(mon, "%s: unsupported option -%c\n",
4234 cmdname, *p);
4235 goto fail;
4236 } else {
4237 skip_key = 1;
4238 }
bellard9307c4c2004-04-04 12:57:25 +00004239 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004240 if(skip_key) {
4241 p = tmp;
4242 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004243 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004244 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004245 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004246 }
bellard9307c4c2004-04-04 12:57:25 +00004247 }
bellard9307c4c2004-04-04 12:57:25 +00004248 }
4249 break;
4250 default:
4251 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004252 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004253 goto fail;
4254 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004255 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004256 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004257 }
4258 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004259 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004260 p++;
4261 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004262 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4263 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004264 goto fail;
4265 }
4266
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004267 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004268
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004269fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004270 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004271 return NULL;
4272}
4273
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004274void monitor_set_error(Monitor *mon, QError *qerror)
4275{
4276 /* report only the first error */
4277 if (!mon->error) {
4278 mon->error = qerror;
4279 } else {
4280 MON_DEBUG("Additional error report at %s:%d\n",
4281 qerror->file, qerror->linenr);
4282 QDECREF(qerror);
4283 }
4284}
4285
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004286static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4287{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004288 if (ret && !monitor_has_error(mon)) {
4289 /*
4290 * If it returns failure, it must have passed on error.
4291 *
4292 * Action: Report an internal error to the client if in QMP.
4293 */
4294 qerror_report(QERR_UNDEFINED_ERROR);
4295 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4296 cmd->name);
4297 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004298
4299#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004300 if (!ret && monitor_has_error(mon)) {
4301 /*
4302 * If it returns success, it must not have passed an error.
4303 *
4304 * Action: Report the passed error to the client.
4305 */
4306 MON_DEBUG("command '%s' returned success but passed an error\n",
4307 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004308 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004309
4310 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4311 /*
4312 * Handlers should not call Monitor print functions.
4313 *
4314 * Action: Ignore them in QMP.
4315 *
4316 * (XXX: we don't check any 'info' or 'query' command here
4317 * because the user print function _is_ called by do_info(), hence
4318 * we will trigger this check. This problem will go away when we
4319 * make 'query' commands real and kill do_info())
4320 */
4321 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4322 cmd->name, mon_print_count_get(mon));
4323 }
4324#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004325}
4326
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004327static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004328{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004329 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004330 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004331
4332 qdict = qdict_new();
4333
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004334 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004335 if (!cmd)
4336 goto out;
4337
Luiz Capitulino4903de02010-09-16 11:01:32 -03004338 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004339 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004340 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004341 QObject *data = NULL;
4342
4343 /* XXX: ignores the error code */
4344 cmd->mhandler.cmd_new(mon, qdict, &data);
4345 assert(!monitor_has_error(mon));
4346 if (data) {
4347 cmd->user_print(mon, data);
4348 qobject_decref(data);
4349 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004350 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004351 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004352 }
4353
Luiz Capitulino13917be2009-10-07 13:41:54 -03004354out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004355 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004356}
4357
bellard81d09122004-07-14 17:21:37 +00004358static void cmd_completion(const char *name, const char *list)
4359{
4360 const char *p, *pstart;
4361 char cmd[128];
4362 int len;
4363
4364 p = list;
4365 for(;;) {
4366 pstart = p;
4367 p = strchr(p, '|');
4368 if (!p)
4369 p = pstart + strlen(pstart);
4370 len = p - pstart;
4371 if (len > sizeof(cmd) - 2)
4372 len = sizeof(cmd) - 2;
4373 memcpy(cmd, pstart, len);
4374 cmd[len] = '\0';
4375 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004376 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004377 }
4378 if (*p == '\0')
4379 break;
4380 p++;
4381 }
4382}
4383
4384static void file_completion(const char *input)
4385{
4386 DIR *ffs;
4387 struct dirent *d;
4388 char path[1024];
4389 char file[1024], file_prefix[1024];
4390 int input_path_len;
4391 const char *p;
4392
ths5fafdf22007-09-16 21:08:06 +00004393 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004394 if (!p) {
4395 input_path_len = 0;
4396 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004397 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004398 } else {
4399 input_path_len = p - input + 1;
4400 memcpy(path, input, input_path_len);
4401 if (input_path_len > sizeof(path) - 1)
4402 input_path_len = sizeof(path) - 1;
4403 path[input_path_len] = '\0';
4404 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4405 }
4406#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004407 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4408 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004409#endif
4410 ffs = opendir(path);
4411 if (!ffs)
4412 return;
4413 for(;;) {
4414 struct stat sb;
4415 d = readdir(ffs);
4416 if (!d)
4417 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004418
4419 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4420 continue;
4421 }
4422
bellard81d09122004-07-14 17:21:37 +00004423 if (strstart(d->d_name, file_prefix, NULL)) {
4424 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004425 if (input_path_len < sizeof(file))
4426 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4427 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004428 /* stat the file to find out if it's a directory.
4429 * In that case add a slash to speed up typing long paths
4430 */
4431 stat(file, &sb);
4432 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004433 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004434 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004435 }
4436 }
4437 closedir(ffs);
4438}
4439
aliguori51de9762009-03-05 23:00:43 +00004440static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004441{
aliguori51de9762009-03-05 23:00:43 +00004442 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004443 const char *input = opaque;
4444
4445 if (input[0] == '\0' ||
4446 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004447 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004448 }
4449}
4450
4451/* NOTE: this parser is an approximate form of the real command parser */
4452static void parse_cmdline(const char *cmdline,
4453 int *pnb_args, char **args)
4454{
4455 const char *p;
4456 int nb_args, ret;
4457 char buf[1024];
4458
4459 p = cmdline;
4460 nb_args = 0;
4461 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004462 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004463 p++;
4464 if (*p == '\0')
4465 break;
4466 if (nb_args >= MAX_ARGS)
4467 break;
4468 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004469 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004470 nb_args++;
4471 if (ret < 0)
4472 break;
4473 }
4474 *pnb_args = nb_args;
4475}
4476
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004477static const char *next_arg_type(const char *typestr)
4478{
4479 const char *p = strchr(typestr, ':');
4480 return (p != NULL ? ++p : typestr);
4481}
4482
aliguori4c36ba32009-03-05 23:01:37 +00004483static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004484{
4485 const char *cmdname;
4486 char *args[MAX_ARGS];
4487 int nb_args, i, len;
4488 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004489 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004490 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004491
4492 parse_cmdline(cmdline, &nb_args, args);
4493#ifdef DEBUG_COMPLETION
4494 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004495 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004496 }
4497#endif
4498
4499 /* if the line ends with a space, it means we want to complete the
4500 next arg */
4501 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004502 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004503 if (nb_args >= MAX_ARGS) {
4504 goto cleanup;
4505 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004506 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004507 }
4508 if (nb_args <= 1) {
4509 /* command completion */
4510 if (nb_args == 0)
4511 cmdname = "";
4512 else
4513 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004514 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004515 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004516 cmd_completion(cmdname, cmd->name);
4517 }
4518 } else {
4519 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004520 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4521 if (compare_cmd(args[0], cmd->name)) {
4522 break;
4523 }
bellard81d09122004-07-14 17:21:37 +00004524 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004525 if (!cmd->name) {
4526 goto cleanup;
4527 }
4528
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004529 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004530 for(i = 0; i < nb_args - 2; i++) {
4531 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004532 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004533 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004534 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004535 }
4536 }
4537 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004538 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004539 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004540 }
bellard81d09122004-07-14 17:21:37 +00004541 switch(*ptype) {
4542 case 'F':
4543 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004544 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004545 file_completion(str);
4546 break;
4547 case 'B':
4548 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004549 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004550 bdrv_iterate(block_completion_it, (void *)str);
4551 break;
bellard7fe48482004-10-09 18:08:01 +00004552 case 's':
4553 /* XXX: more generic ? */
4554 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004555 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004556 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4557 cmd_completion(str, cmd->name);
4558 }
bellard64866c32006-05-07 18:03:31 +00004559 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004560 char *sep = strrchr(str, '-');
4561 if (sep)
4562 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004563 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004564 for(key = key_defs; key->name != NULL; key++) {
4565 cmd_completion(str, key->name);
4566 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004567 } else if (!strcmp(cmd->name, "help|?")) {
4568 readline_set_completion_index(cur_mon->rs, strlen(str));
4569 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4570 cmd_completion(str, cmd->name);
4571 }
bellard7fe48482004-10-09 18:08:01 +00004572 }
4573 break;
bellard81d09122004-07-14 17:21:37 +00004574 default:
4575 break;
4576 }
4577 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004578
4579cleanup:
4580 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004581 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004582 }
bellard81d09122004-07-14 17:21:37 +00004583}
4584
aliguori731b0362009-03-05 23:01:42 +00004585static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004586{
aliguori731b0362009-03-05 23:01:42 +00004587 Monitor *mon = opaque;
4588
Jan Kiszkac62313b2009-12-04 14:05:29 +01004589 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004590}
4591
Luiz Capitulino09069b12010-02-04 18:10:06 -02004592static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4593{
4594 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4595 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4596}
4597
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004598/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004599 * Argument validation rules:
4600 *
4601 * 1. The argument must exist in cmd_args qdict
4602 * 2. The argument type must be the expected one
4603 *
4604 * Special case: If the argument doesn't exist in cmd_args and
4605 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4606 * checking is skipped for it.
4607 */
4608static int check_client_args_type(const QDict *client_args,
4609 const QDict *cmd_args, int flags)
4610{
4611 const QDictEntry *ent;
4612
4613 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4614 QObject *obj;
4615 QString *arg_type;
4616 const QObject *client_arg = qdict_entry_value(ent);
4617 const char *client_arg_name = qdict_entry_key(ent);
4618
4619 obj = qdict_get(cmd_args, client_arg_name);
4620 if (!obj) {
4621 if (flags & QMP_ACCEPT_UNKNOWNS) {
4622 /* handler accepts unknowns */
4623 continue;
4624 }
4625 /* client arg doesn't exist */
4626 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4627 return -1;
4628 }
4629
4630 arg_type = qobject_to_qstring(obj);
4631 assert(arg_type != NULL);
4632
4633 /* check if argument's type is correct */
4634 switch (qstring_get_str(arg_type)[0]) {
4635 case 'F':
4636 case 'B':
4637 case 's':
4638 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4639 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4640 "string");
4641 return -1;
4642 }
4643 break;
4644 case 'i':
4645 case 'l':
4646 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004647 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004648 if (qobject_type(client_arg) != QTYPE_QINT) {
4649 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4650 "int");
4651 return -1;
4652 }
4653 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004654 case 'T':
4655 if (qobject_type(client_arg) != QTYPE_QINT &&
4656 qobject_type(client_arg) != QTYPE_QFLOAT) {
4657 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4658 "number");
4659 return -1;
4660 }
4661 break;
4662 case 'b':
4663 case '-':
4664 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4665 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4666 "bool");
4667 return -1;
4668 }
4669 break;
4670 case 'O':
4671 assert(flags & QMP_ACCEPT_UNKNOWNS);
4672 break;
4673 case '/':
4674 case '.':
4675 /*
4676 * These types are not supported by QMP and thus are not
4677 * handled here. Fall through.
4678 */
4679 default:
4680 abort();
4681 }
4682 }
4683
4684 return 0;
4685}
4686
4687/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004688 * - Check if the client has passed all mandatory args
4689 * - Set special flags for argument validation
4690 */
4691static int check_mandatory_args(const QDict *cmd_args,
4692 const QDict *client_args, int *flags)
4693{
4694 const QDictEntry *ent;
4695
4696 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4697 const char *cmd_arg_name = qdict_entry_key(ent);
4698 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4699 assert(type != NULL);
4700
4701 if (qstring_get_str(type)[0] == 'O') {
4702 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4703 *flags |= QMP_ACCEPT_UNKNOWNS;
4704 } else if (qstring_get_str(type)[0] != '-' &&
4705 qstring_get_str(type)[1] != '?' &&
4706 !qdict_haskey(client_args, cmd_arg_name)) {
4707 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4708 return -1;
4709 }
4710 }
4711
4712 return 0;
4713}
4714
4715static QDict *qdict_from_args_type(const char *args_type)
4716{
4717 int i;
4718 QDict *qdict;
4719 QString *key, *type, *cur_qs;
4720
4721 assert(args_type != NULL);
4722
4723 qdict = qdict_new();
4724
4725 if (args_type == NULL || args_type[0] == '\0') {
4726 /* no args, empty qdict */
4727 goto out;
4728 }
4729
4730 key = qstring_new();
4731 type = qstring_new();
4732
4733 cur_qs = key;
4734
4735 for (i = 0;; i++) {
4736 switch (args_type[i]) {
4737 case ',':
4738 case '\0':
4739 qdict_put(qdict, qstring_get_str(key), type);
4740 QDECREF(key);
4741 if (args_type[i] == '\0') {
4742 goto out;
4743 }
4744 type = qstring_new(); /* qdict has ref */
4745 cur_qs = key = qstring_new();
4746 break;
4747 case ':':
4748 cur_qs = type;
4749 break;
4750 default:
4751 qstring_append_chr(cur_qs, args_type[i]);
4752 break;
4753 }
4754 }
4755
4756out:
4757 return qdict;
4758}
4759
4760/*
4761 * Client argument checking rules:
4762 *
4763 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004764 * 2. Each argument provided by the client must be expected
4765 * 3. Each argument provided by the client must have the type expected
4766 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004767 */
4768static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4769{
4770 int flags, err;
4771 QDict *cmd_args;
4772
4773 cmd_args = qdict_from_args_type(cmd->args_type);
4774
4775 flags = 0;
4776 err = check_mandatory_args(cmd_args, client_args, &flags);
4777 if (err) {
4778 goto out;
4779 }
4780
Luiz Capitulino4af91932010-06-22 11:44:05 -03004781 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004782
4783out:
4784 QDECREF(cmd_args);
4785 return err;
4786}
4787
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004788/*
4789 * Input object checking rules
4790 *
4791 * 1. Input object must be a dict
4792 * 2. The "execute" key must exist
4793 * 3. The "execute" key must be a string
4794 * 4. If the "arguments" key exists, it must be a dict
4795 * 5. If the "id" key exists, it can be anything (ie. json-value)
4796 * 6. Any argument not listed above is considered invalid
4797 */
4798static QDict *qmp_check_input_obj(QObject *input_obj)
4799{
4800 const QDictEntry *ent;
4801 int has_exec_key = 0;
4802 QDict *input_dict;
4803
4804 if (qobject_type(input_obj) != QTYPE_QDICT) {
4805 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4806 return NULL;
4807 }
4808
4809 input_dict = qobject_to_qdict(input_obj);
4810
4811 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4812 const char *arg_name = qdict_entry_key(ent);
4813 const QObject *arg_obj = qdict_entry_value(ent);
4814
4815 if (!strcmp(arg_name, "execute")) {
4816 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4817 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4818 "string");
4819 return NULL;
4820 }
4821 has_exec_key = 1;
4822 } else if (!strcmp(arg_name, "arguments")) {
4823 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4824 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4825 "object");
4826 return NULL;
4827 }
4828 } else if (!strcmp(arg_name, "id")) {
4829 /* FIXME: check duplicated IDs for async commands */
4830 } else {
4831 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4832 return NULL;
4833 }
4834 }
4835
4836 if (!has_exec_key) {
4837 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4838 return NULL;
4839 }
4840
4841 return input_dict;
4842}
4843
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004844static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4845{
4846 QObject *ret_data = NULL;
4847
Luiz Capitulino4903de02010-09-16 11:01:32 -03004848 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004849 qmp_async_info_handler(mon, cmd);
4850 if (monitor_has_error(mon)) {
4851 monitor_protocol_emitter(mon, NULL);
4852 }
4853 } else {
4854 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004855 monitor_protocol_emitter(mon, ret_data);
4856 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004857 }
4858}
4859
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004860static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4861 const QDict *params)
4862{
4863 int ret;
4864 QObject *data = NULL;
4865
4866 mon_print_count_init(mon);
4867
4868 ret = cmd->mhandler.cmd_new(mon, params, &data);
4869 handler_audit(mon, cmd, ret);
4870 monitor_protocol_emitter(mon, data);
4871 qobject_decref(data);
4872}
4873
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004874static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4875{
4876 int err;
4877 QObject *obj;
4878 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004879 const mon_cmd_t *cmd;
4880 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004881 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004882
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004883 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004884 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004885
4886 obj = json_parser_parse(tokens, NULL);
4887 if (!obj) {
4888 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004889 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004890 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004891 }
4892
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004893 input = qmp_check_input_obj(obj);
4894 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004895 qobject_decref(obj);
4896 goto err_out;
4897 }
4898
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004899 mon->mc->id = qdict_get(input, "id");
4900 qobject_incref(mon->mc->id);
4901
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004902 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004903 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004904 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004905 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004906 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004907 }
4908
Anthony Liguorie3193602011-09-02 12:34:47 -05004909 cmd = qmp_find_cmd(cmd_name);
4910 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004911 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004912 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004913 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004914 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004915 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004916 }
4917
4918 obj = qdict_get(input, "arguments");
4919 if (!obj) {
4920 args = qdict_new();
4921 } else {
4922 args = qobject_to_qdict(obj);
4923 QINCREF(args);
4924 }
4925
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004926 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004927 if (err < 0) {
4928 goto err_out;
4929 }
4930
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004931 if (query_cmd) {
4932 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004933 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004934 err = qmp_async_cmd_handler(mon, cmd, args);
4935 if (err) {
4936 /* emit the error response */
4937 goto err_out;
4938 }
Adam Litke940cc302010-01-25 12:18:44 -06004939 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004940 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004941 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004942
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004943 goto out;
4944
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004945err_out:
4946 monitor_protocol_emitter(mon, NULL);
4947out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004948 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004949 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004950}
4951
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004952/**
4953 * monitor_control_read(): Read and handle QMP input
4954 */
4955static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4956{
4957 Monitor *old_mon = cur_mon;
4958
4959 cur_mon = opaque;
4960
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004961 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004962
4963 cur_mon = old_mon;
4964}
4965
aliguori731b0362009-03-05 23:01:42 +00004966static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004967{
aliguori731b0362009-03-05 23:01:42 +00004968 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004969 int i;
aliguori376253e2009-03-05 23:01:23 +00004970
aliguori731b0362009-03-05 23:01:42 +00004971 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004972
aliguoricde76ee2009-03-05 23:01:51 +00004973 if (cur_mon->rs) {
4974 for (i = 0; i < size; i++)
4975 readline_handle_byte(cur_mon->rs, buf[i]);
4976 } else {
4977 if (size == 0 || buf[size - 1] != 0)
4978 monitor_printf(cur_mon, "corrupted command\n");
4979 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004980 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004981 }
aliguori731b0362009-03-05 23:01:42 +00004982
4983 cur_mon = old_mon;
4984}
aliguorid8f44602008-10-06 13:52:44 +00004985
aliguori376253e2009-03-05 23:01:23 +00004986static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004987{
aliguori731b0362009-03-05 23:01:42 +00004988 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004989 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004990 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004991}
4992
aliguoricde76ee2009-03-05 23:01:51 +00004993int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004994{
aliguoricde76ee2009-03-05 23:01:51 +00004995 if (!mon->rs)
4996 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004997 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004998 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004999}
5000
aliguori376253e2009-03-05 23:01:23 +00005001void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005002{
aliguoricde76ee2009-03-05 23:01:51 +00005003 if (!mon->rs)
5004 return;
aliguori731b0362009-03-05 23:01:42 +00005005 if (--mon->suspend_cnt == 0)
5006 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005007}
5008
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005009static QObject *get_qmp_greeting(void)
5010{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005011 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005012
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005013 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005014 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5015}
5016
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005017/**
5018 * monitor_control_event(): Print QMP gretting
5019 */
5020static void monitor_control_event(void *opaque, int event)
5021{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005022 QObject *data;
5023 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005024
Luiz Capitulino47116d12010-02-08 17:01:30 -02005025 switch (event) {
5026 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005027 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005028 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005029 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005030 monitor_json_emitter(mon, data);
5031 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005032 break;
5033 case CHR_EVENT_CLOSED:
5034 json_message_parser_destroy(&mon->mc->parser);
5035 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005036 }
5037}
5038
aliguori731b0362009-03-05 23:01:42 +00005039static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005040{
aliguori376253e2009-03-05 23:01:23 +00005041 Monitor *mon = opaque;
5042
aliguori2724b182009-03-05 23:01:47 +00005043 switch (event) {
5044 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005045 mon->mux_out = 0;
5046 if (mon->reset_seen) {
5047 readline_restart(mon->rs);
5048 monitor_resume(mon);
5049 monitor_flush(mon);
5050 } else {
5051 mon->suspend_cnt = 0;
5052 }
aliguori2724b182009-03-05 23:01:47 +00005053 break;
ths86e94de2007-01-05 22:01:59 +00005054
aliguori2724b182009-03-05 23:01:47 +00005055 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005056 if (mon->reset_seen) {
5057 if (mon->suspend_cnt == 0) {
5058 monitor_printf(mon, "\n");
5059 }
5060 monitor_flush(mon);
5061 monitor_suspend(mon);
5062 } else {
5063 mon->suspend_cnt++;
5064 }
5065 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005066 break;
5067
Amit Shahb6b8df52009-10-07 18:31:16 +05305068 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005069 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5070 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005071 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005072 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005073 }
5074 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005075 break;
5076 }
ths86e94de2007-01-05 22:01:59 +00005077}
5078
aliguori76655d62009-03-06 20:27:37 +00005079
5080/*
5081 * Local variables:
5082 * c-indent-level: 4
5083 * c-basic-offset: 4
5084 * tab-width: 8
5085 * End:
5086 */
5087
aliguori731b0362009-03-05 23:01:42 +00005088void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005089{
aliguori731b0362009-03-05 23:01:42 +00005090 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005091 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005092
5093 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005094 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005095 is_first_init = 0;
5096 }
aliguori87127162009-03-05 23:01:29 +00005097
Anthony Liguori7267c092011-08-20 22:09:37 -05005098 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005099
aliguori87127162009-03-05 23:01:29 +00005100 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005101 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005102 if (flags & MONITOR_USE_READLINE) {
5103 mon->rs = readline_init(mon, monitor_find_completion);
5104 monitor_read_command(mon, 0);
5105 }
aliguori87127162009-03-05 23:01:29 +00005106
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005107 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005108 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005109 /* Control mode requires special handlers */
5110 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5111 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005112 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005113 } else {
5114 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5115 monitor_event, mon);
5116 }
aliguori87127162009-03-05 23:01:29 +00005117
Blue Swirl72cf2d42009-09-12 07:36:22 +00005118 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005119 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5120 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005121}
5122
aliguori376253e2009-03-05 23:01:23 +00005123static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005124{
aliguoribb5fc202009-03-05 23:01:15 +00005125 BlockDriverState *bs = opaque;
5126 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005127
aliguoribb5fc202009-03-05 23:01:15 +00005128 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005129 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005130 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005131 }
aliguori731b0362009-03-05 23:01:42 +00005132 if (mon->password_completion_cb)
5133 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005134
aliguori731b0362009-03-05 23:01:42 +00005135 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005136}
aliguoric0f4ce72009-03-05 23:01:01 +00005137
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005138int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5139 BlockDriverCompletionFunc *completion_cb,
5140 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005141{
aliguoricde76ee2009-03-05 23:01:51 +00005142 int err;
5143
aliguoribb5fc202009-03-05 23:01:15 +00005144 if (!bdrv_key_required(bs)) {
5145 if (completion_cb)
5146 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005147 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005148 }
aliguoric0f4ce72009-03-05 23:01:01 +00005149
Luiz Capitulino94171e12009-12-07 21:37:00 +01005150 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005151 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005152 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005153 }
5154
aliguori376253e2009-03-05 23:01:23 +00005155 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5156 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005157
aliguori731b0362009-03-05 23:01:42 +00005158 mon->password_completion_cb = completion_cb;
5159 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005160
aliguoricde76ee2009-03-05 23:01:51 +00005161 err = monitor_read_password(mon, bdrv_password_cb, bs);
5162
5163 if (err && completion_cb)
5164 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005165
5166 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005167}