blob: 70e54602a993c23937e5f65d6098ec37a334ef4c [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000039#include "monitor.h"
40#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020042#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000044#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000045#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000046#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000047#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000048#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000049#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010051#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030052#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Blue Swirl314e2982011-09-11 20:22:05 +000066#include "memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
ths6a5bd302007-12-03 17:05:38 +000069
Jan Kiszka661f1922011-10-16 11:53:13 +020070/* for pic/irq_info */
71#if defined(TARGET_SPARC)
72#include "hw/sun4m.h"
73#endif
74#include "hw/lm32_pic.h"
75
bellard9dc39cb2004-03-14 21:38:27 +000076//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000077//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000078
bellard9307c4c2004-04-04 12:57:25 +000079/*
80 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000081 *
bellard9307c4c2004-04-04 12:57:25 +000082 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000083 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000084 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010085 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000090 * 'i' 32 bit integer
91 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010092 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020094 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010099 * 'T' double
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000102 * '/' optional gdb-like print format (like "/10x")
103 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100106 * 'b' boolean
107 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300108 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000109 *
110 */
111
Adam Litke940cc302010-01-25 12:18:44 -0600112typedef struct MonitorCompletionData MonitorCompletionData;
113struct MonitorCompletionData {
114 Monitor *mon;
115 void (*user_print)(Monitor *mon, const QObject *data);
116};
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300123 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 union {
125 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300126 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500133 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200134 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500135} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000136
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500138typedef struct mon_fd_t mon_fd_t;
139struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140 char *name;
141 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143};
144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145typedef struct MonitorControl {
146 QObject *id;
147 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200148 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200149} MonitorControl;
150
aliguori87127162009-03-05 23:01:29 +0000151struct Monitor {
152 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200153 int mux_out;
154 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000155 int flags;
156 int suspend_cnt;
157 uint8_t outbuf[1024];
158 int outbuf_index;
159 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200160 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000161 CPUState *mon_cpu;
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200164#ifdef CONFIG_DEBUG_MONITOR
165 int print_calls_nr;
166#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200167 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500168 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000170};
171
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200172#ifdef CONFIG_DEBUG_MONITOR
173#define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200176
177static inline void mon_print_count_inc(Monitor *mon)
178{
179 mon->print_calls_nr++;
180}
181
182static inline void mon_print_count_init(Monitor *mon)
183{
184 mon->print_calls_nr = 0;
185}
186
187static inline int mon_print_count_get(const Monitor *mon)
188{
189 return mon->print_calls_nr;
190}
191
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200192#else /* !CONFIG_DEBUG_MONITOR */
193#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200194static inline void mon_print_count_inc(Monitor *mon) { }
195static inline void mon_print_count_init(Monitor *mon) { }
196static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200197#endif /* CONFIG_DEBUG_MONITOR */
198
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300199/* QMP checker flags */
200#define QMP_ACCEPT_UNKNOWNS 1
201
Blue Swirl72cf2d42009-09-12 07:36:22 +0000202static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000203
Anthony Liguoric227f092009-10-01 16:12:16 -0500204static const mon_cmd_t mon_cmds[];
205static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000206
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300207static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300208static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300209
Markus Armbruster8631b602010-02-18 11:41:55 +0100210Monitor *cur_mon;
211Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000212
aliguori731b0362009-03-05 23:01:42 +0000213static void monitor_command_cb(Monitor *mon, const char *cmdline,
214 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000215
Luiz Capitulino09069b12010-02-04 18:10:06 -0200216static inline int qmp_cmd_mode(const Monitor *mon)
217{
218 return (mon->mc ? mon->mc->command_mode : 0);
219}
220
Luiz Capitulino418173c2009-11-26 22:58:51 -0200221/* Return true if in control mode, false otherwise */
222static inline int monitor_ctrl_mode(const Monitor *mon)
223{
224 return (mon->flags & MONITOR_USE_CONTROL);
225}
226
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100227/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228int monitor_cur_is_qmp(void)
229{
230 return cur_mon && monitor_ctrl_mode(cur_mon);
231}
232
aliguori731b0362009-03-05 23:01:42 +0000233static void monitor_read_command(Monitor *mon, int show_prompt)
234{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200235 if (!mon->rs)
236 return;
237
aliguori731b0362009-03-05 23:01:42 +0000238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
239 if (show_prompt)
240 readline_show_prompt(mon->rs);
241}
bellard6a00d602005-11-21 23:25:50 +0000242
aliguoricde76ee2009-03-05 23:01:51 +0000243static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
244 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000245{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100246 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100247 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100248 return -EINVAL;
249 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
252 return 0;
253 } else {
254 monitor_printf(mon, "terminal does not support password prompting\n");
255 return -ENOTTY;
256 }
aliguoribb5fc202009-03-05 23:01:15 +0000257}
258
aliguori376253e2009-03-05 23:01:23 +0000259void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000260{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000263 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
267/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000268static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000269{
ths60fe76f2007-12-16 03:02:09 +0000270 char c;
aliguori731b0362009-03-05 23:01:42 +0000271
bellard7e2515e2004-08-01 21:52:19 +0000272 for(;;) {
273 c = *str++;
274 if (c == '\0')
275 break;
bellard7ba12602006-07-14 20:26:42 +0000276 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000277 mon->outbuf[mon->outbuf_index++] = '\r';
278 mon->outbuf[mon->outbuf_index++] = c;
279 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
280 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000282 }
283}
284
aliguori376253e2009-03-05 23:01:23 +0000285void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000286{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200287 char buf[4096];
288
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200289 if (!mon)
290 return;
291
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200292 mon_print_count_inc(mon);
293
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200295 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200296 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200297
298 vsnprintf(buf, sizeof(buf), fmt, ap);
299 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000303{
304 va_list ap;
305 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000306 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000307 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000308}
309
aliguori376253e2009-03-05 23:01:23 +0000310void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000311{
312 int i;
313
314 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000315 switch (filename[i]) {
316 case ' ':
317 case '"':
318 case '\\':
319 monitor_printf(mon, "\\%c", filename[i]);
320 break;
321 case '\t':
322 monitor_printf(mon, "\\t");
323 break;
324 case '\r':
325 monitor_printf(mon, "\\r");
326 break;
327 case '\n':
328 monitor_printf(mon, "\\n");
329 break;
330 default:
331 monitor_printf(mon, "%c", filename[i]);
332 break;
333 }
thsfef30742006-12-22 14:11:32 +0000334 }
335}
336
Stefan Weil8b7968f2010-09-23 21:28:05 +0200337static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
338 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000339{
340 va_list ap;
341 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000342 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000343 va_end(ap);
344 return 0;
345}
346
Luiz Capitulino13c74252009-10-07 13:41:55 -0300347static void monitor_user_noop(Monitor *mon, const QObject *data) { }
348
Luiz Capitulino9e807212010-09-16 10:58:59 -0300349static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300350{
351 return cmd->user_print != NULL;
352}
353
Luiz Capitulino4903de02010-09-16 11:01:32 -0300354static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600355{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200356 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600357}
358
Luiz Capitulino8204a912009-11-18 23:05:31 -0200359static inline int monitor_has_error(const Monitor *mon)
360{
361 return mon->error != NULL;
362}
363
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200364static void monitor_json_emitter(Monitor *mon, const QObject *data)
365{
366 QString *json;
367
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200368 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
369 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200370 assert(json != NULL);
371
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200372 qstring_append_chr(json, '\n');
373 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200374
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200375 QDECREF(json);
376}
377
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378static void monitor_protocol_emitter(Monitor *mon, QObject *data)
379{
380 QDict *qmp;
381
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100382 trace_monitor_protocol_emitter(mon);
383
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 qmp = qdict_new();
385
386 if (!monitor_has_error(mon)) {
387 /* success response */
388 if (data) {
389 qobject_incref(data);
390 qdict_put_obj(qmp, "return", data);
391 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200392 /* return an empty QDict by default */
393 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200394 }
395 } else {
396 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100397 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200398 qdict_put(qmp, "error", mon->error->error);
399 QINCREF(mon->error->error);
400 QDECREF(mon->error);
401 mon->error = NULL;
402 }
403
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200404 if (mon->mc->id) {
405 qdict_put_obj(qmp, "id", mon->mc->id);
406 mon->mc->id = NULL;
407 }
408
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200409 monitor_json_emitter(mon, QOBJECT(qmp));
410 QDECREF(qmp);
411}
412
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200413static void timestamp_put(QDict *qdict)
414{
415 int err;
416 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000417 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418
Blue Swirld08d6f02009-12-04 18:06:39 +0000419 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200420 if (err < 0)
421 return;
422
423 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
424 "'microseconds': %" PRId64 " }",
425 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 qdict_put_obj(qdict, "timestamp", obj);
427}
428
429/**
430 * monitor_protocol_event(): Generate a Monitor event
431 *
432 * Event-specific data can be emitted through the (optional) 'data' parameter.
433 */
434void monitor_protocol_event(MonitorEvent event, QObject *data)
435{
436 QDict *qmp;
437 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600438 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439
Blue Swirl242cd002009-12-04 18:05:45 +0000440 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200442 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000443 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200444 event_name = "SHUTDOWN";
445 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000446 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200447 event_name = "RESET";
448 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000449 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200450 event_name = "POWERDOWN";
451 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000452 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200453 event_name = "STOP";
454 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300455 case QEVENT_RESUME:
456 event_name = "RESUME";
457 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200458 case QEVENT_VNC_CONNECTED:
459 event_name = "VNC_CONNECTED";
460 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200461 case QEVENT_VNC_INITIALIZED:
462 event_name = "VNC_INITIALIZED";
463 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200464 case QEVENT_VNC_DISCONNECTED:
465 event_name = "VNC_DISCONNECTED";
466 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200467 case QEVENT_BLOCK_IO_ERROR:
468 event_name = "BLOCK_IO_ERROR";
469 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300470 case QEVENT_RTC_CHANGE:
471 event_name = "RTC_CHANGE";
472 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300473 case QEVENT_WATCHDOG:
474 event_name = "WATCHDOG";
475 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200476 case QEVENT_SPICE_CONNECTED:
477 event_name = "SPICE_CONNECTED";
478 break;
479 case QEVENT_SPICE_INITIALIZED:
480 event_name = "SPICE_INITIALIZED";
481 break;
482 case QEVENT_SPICE_DISCONNECTED:
483 event_name = "SPICE_DISCONNECTED";
484 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200485 default:
486 abort();
487 break;
488 }
489
490 qmp = qdict_new();
491 timestamp_put(qmp);
492 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200493 if (data) {
494 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200495 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200496 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200497
Adam Litkef039a562010-01-15 08:34:02 -0600498 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200499 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200500 monitor_json_emitter(mon, QOBJECT(qmp));
501 }
Adam Litkef039a562010-01-15 08:34:02 -0600502 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200503 QDECREF(qmp);
504}
505
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200506static int do_qmp_capabilities(Monitor *mon, const QDict *params,
507 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200508{
509 /* Will setup QMP capabilities in the future */
510 if (monitor_ctrl_mode(mon)) {
511 mon->mc->command_mode = 1;
512 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200513
514 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200515}
516
Luiz Capitulino0268d972010-10-22 10:08:02 -0200517static void handle_user_command(Monitor *mon, const char *cmdline);
518
519static int do_hmp_passthrough(Monitor *mon, const QDict *params,
520 QObject **ret_data)
521{
522 int ret = 0;
523 Monitor *old_mon, hmp;
524 CharDriverState mchar;
525
526 memset(&hmp, 0, sizeof(hmp));
527 qemu_chr_init_mem(&mchar);
528 hmp.chr = &mchar;
529
530 old_mon = cur_mon;
531 cur_mon = &hmp;
532
533 if (qdict_haskey(params, "cpu-index")) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300534 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
Luiz Capitulino0268d972010-10-22 10:08:02 -0200535 if (ret < 0) {
536 cur_mon = old_mon;
537 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
538 goto out;
539 }
540 }
541
542 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
543 cur_mon = old_mon;
544
545 if (qemu_chr_mem_osize(hmp.chr) > 0) {
546 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
547 }
548
549out:
550 qemu_chr_close_mem(hmp.chr);
551 return ret;
552}
553
bellard9dc39cb2004-03-14 21:38:27 +0000554static int compare_cmd(const char *name, const char *list)
555{
556 const char *p, *pstart;
557 int len;
558 len = strlen(name);
559 p = list;
560 for(;;) {
561 pstart = p;
562 p = strchr(p, '|');
563 if (!p)
564 p = pstart + strlen(pstart);
565 if ((p - pstart) == len && !memcmp(pstart, name, len))
566 return 1;
567 if (*p == '\0')
568 break;
569 p++;
570 }
571 return 0;
572}
573
Anthony Liguoric227f092009-10-01 16:12:16 -0500574static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000575 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000576{
Anthony Liguoric227f092009-10-01 16:12:16 -0500577 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000578
579 for(cmd = cmds; cmd->name != NULL; cmd++) {
580 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
582 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000583 }
584}
585
aliguori376253e2009-03-05 23:01:23 +0000586static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000587{
588 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000589 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000590 } else {
aliguori376253e2009-03-05 23:01:23 +0000591 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000592 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000593 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "Log items (comma separated):\n");
595 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000596 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000597 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000598 }
599 }
bellard9dc39cb2004-03-14 21:38:27 +0000600 }
601}
602
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300603static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300604{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300605 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300606}
607
Lluísfc764102011-08-31 20:31:18 +0200608static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530609{
610 const char *tp_name = qdict_get_str(qdict, "name");
611 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200612 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000613
614 if (!ret) {
615 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
616 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530617}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100618
Michael Rothc45a8162011-10-02 08:44:37 -0500619#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100620static void do_trace_file(Monitor *mon, const QDict *qdict)
621{
622 const char *op = qdict_get_try_str(qdict, "op");
623 const char *arg = qdict_get_try_str(qdict, "arg");
624
625 if (!op) {
626 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
627 } else if (!strcmp(op, "on")) {
628 st_set_trace_file_enabled(true);
629 } else if (!strcmp(op, "off")) {
630 st_set_trace_file_enabled(false);
631 } else if (!strcmp(op, "flush")) {
632 st_flush_trace_buffer();
633 } else if (!strcmp(op, "set")) {
634 if (arg) {
635 st_set_trace_file(arg);
636 }
637 } else {
638 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
639 help_cmd(mon, "trace-file");
640 }
641}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530642#endif
643
Adam Litke940cc302010-01-25 12:18:44 -0600644static void user_monitor_complete(void *opaque, QObject *ret_data)
645{
646 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
647
648 if (ret_data) {
649 data->user_print(data->mon, ret_data);
650 }
651 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500652 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600653}
654
655static void qmp_monitor_complete(void *opaque, QObject *ret_data)
656{
657 monitor_protocol_emitter(opaque, ret_data);
658}
659
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300660static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
661 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600662{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300663 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600664}
665
666static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
667{
668 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
669}
670
671static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
672 const QDict *params)
673{
674 int ret;
675
Anthony Liguori7267c092011-08-20 22:09:37 -0500676 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600677 cb_data->mon = mon;
678 cb_data->user_print = cmd->user_print;
679 monitor_suspend(mon);
680 ret = cmd->mhandler.cmd_async(mon, params,
681 user_monitor_complete, cb_data);
682 if (ret < 0) {
683 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500684 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600685 }
686}
687
688static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
689{
690 int ret;
691
Anthony Liguori7267c092011-08-20 22:09:37 -0500692 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600693 cb_data->mon = mon;
694 cb_data->user_print = cmd->user_print;
695 monitor_suspend(mon);
696 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
697 if (ret < 0) {
698 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500699 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600700 }
701}
702
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300703static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000704{
Anthony Liguoric227f092009-10-01 16:12:16 -0500705 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300706 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000707
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200708 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000709 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200710 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711
712 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000713 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300714 break;
bellard9dc39cb2004-03-14 21:38:27 +0000715 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300716
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200717 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300718 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200719 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300720
Luiz Capitulino4903de02010-09-16 11:01:32 -0300721 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300722 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300723 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300724 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200725
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300726 cmd->mhandler.info_new(mon, &info_data);
727 if (info_data) {
728 cmd->user_print(mon, info_data);
729 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200730 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300731 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300732 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300733 }
734
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300735 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300736
737help:
738 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000739}
740
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300741static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200742{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300743 CommandInfoList *info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200744
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300745 info = g_malloc0(sizeof(*info));
746 info->value = g_malloc0(sizeof(*info->value));
747 info->value->name = g_strdup(cmd_name);
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200748
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300749 return info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200750}
751
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300752CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000753{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300754 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200755 const mon_cmd_t *cmd;
756
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300757 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300758 info = alloc_cmd_entry(cmd->name);
759 info->next = cmd_list;
760 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200761 }
762
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300763 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300764 char buf[128];
765 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300766 info = alloc_cmd_entry(buf);
767 info->next = cmd_list;
768 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200769 }
770
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300771 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000772}
773
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300774/* set the current CPU defined by the user */
775int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000776{
777 CPUState *env;
778
779 for(env = first_cpu; env != NULL; env = env->next_cpu) {
780 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000781 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000782 return 0;
783 }
784 }
785 return -1;
786}
787
pbrook9596ebb2007-11-18 01:44:38 +0000788static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000789{
aliguori731b0362009-03-05 23:01:42 +0000790 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300791 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000792 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300793 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000794 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000795}
796
Luiz Capitulino99b77962011-10-24 10:53:44 -0200797int monitor_get_cpu_index(void)
798{
799 return mon_get_cpu()->cpu_index;
800}
801
aliguori376253e2009-03-05 23:01:23 +0000802static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000803{
bellard6a00d602005-11-21 23:25:50 +0000804 CPUState *env;
805 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000806#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000807 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000808 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000809#else
aliguori376253e2009-03-05 23:01:23 +0000810 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000811 0);
bellard9307c4c2004-04-04 12:57:25 +0000812#endif
813}
814
aliguori376253e2009-03-05 23:01:23 +0000815static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000816{
aliguori376253e2009-03-05 23:01:23 +0000817 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000818}
819
aliguori376253e2009-03-05 23:01:23 +0000820static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000821{
822 int i;
bellard7e2515e2004-08-01 21:52:19 +0000823 const char *str;
ths3b46e622007-09-17 08:09:54 +0000824
aliguoricde76ee2009-03-05 23:01:51 +0000825 if (!mon->rs)
826 return;
bellard7e2515e2004-08-01 21:52:19 +0000827 i = 0;
828 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000829 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000830 if (!str)
831 break;
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000833 i++;
bellardaa455482004-04-04 13:07:25 +0000834 }
835}
836
j_mayer76a66252007-03-07 08:32:30 +0000837#if defined(TARGET_PPC)
838/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000839static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000840{
841 CPUState *env;
842
843 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000844 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000845}
846#endif
847
Lluís6d8a7642011-08-31 20:30:43 +0200848#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530849static void do_info_trace(Monitor *mon)
850{
851 st_print_trace((FILE *)mon, &monitor_fprintf);
852}
Lluís31965ae2011-08-31 20:31:24 +0200853#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854
Lluísfc764102011-08-31 20:31:18 +0200855static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530856{
Lluísfc764102011-08-31 20:31:18 +0200857 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530858}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530859
Jes Sorensen821601e2011-03-16 13:33:36 +0100860#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200861static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000862{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600863 if (!password || !password[0]) {
864 if (vnc_display_disable_login(NULL)) {
865 qerror_report(QERR_SET_PASSWD_FAILED);
866 return -1;
867 }
868 return 0;
869 }
870
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200871 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100872 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200873 return -1;
874 }
aliguoribb5fc202009-03-05 23:01:15 +0000875
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200876 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100877}
878
879static void change_vnc_password_cb(Monitor *mon, const char *password,
880 void *opaque)
881{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100882 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000883 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000884}
885
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200886static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000887{
ths70848512007-08-25 01:37:05 +0000888 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000889 strcmp(target, "password") == 0) {
890 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000891 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000892 strncpy(password, arg, sizeof(password));
893 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200894 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000895 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200896 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000897 }
ths70848512007-08-25 01:37:05 +0000898 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200899 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100900 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200901 return -1;
902 }
ths70848512007-08-25 01:37:05 +0000903 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200904
905 return 0;
thse25a5822007-08-25 01:36:20 +0000906}
Jes Sorensen821601e2011-03-16 13:33:36 +0100907#else
908static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
909{
910 qerror_report(QERR_FEATURE_DISABLED, "vnc");
911 return -ENODEV;
912}
913#endif
thse25a5822007-08-25 01:36:20 +0000914
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100915/**
916 * do_change(): Change a removable medium, or VNC configuration
917 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200918static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000919{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300920 const char *device = qdict_get_str(qdict, "device");
921 const char *target = qdict_get_str(qdict, "target");
922 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200923 int ret;
924
thse25a5822007-08-25 01:36:20 +0000925 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200926 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000927 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200928 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000929 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200930
931 return ret;
thse25a5822007-08-25 01:36:20 +0000932}
933
Gerd Hoffmann75721502010-10-07 12:22:54 +0200934static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
935{
936 const char *protocol = qdict_get_str(qdict, "protocol");
937 const char *password = qdict_get_str(qdict, "password");
938 const char *connected = qdict_get_try_str(qdict, "connected");
939 int disconnect_if_connected = 0;
940 int fail_if_connected = 0;
941 int rc;
942
943 if (connected) {
944 if (strcmp(connected, "fail") == 0) {
945 fail_if_connected = 1;
946 } else if (strcmp(connected, "disconnect") == 0) {
947 disconnect_if_connected = 1;
948 } else if (strcmp(connected, "keep") == 0) {
949 /* nothing */
950 } else {
951 qerror_report(QERR_INVALID_PARAMETER, "connected");
952 return -1;
953 }
954 }
955
956 if (strcmp(protocol, "spice") == 0) {
957 if (!using_spice) {
958 /* correct one? spice isn't a device ,,, */
959 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
960 return -1;
961 }
962 rc = qemu_spice_set_passwd(password, fail_if_connected,
963 disconnect_if_connected);
964 if (rc != 0) {
965 qerror_report(QERR_SET_PASSWD_FAILED);
966 return -1;
967 }
968 return 0;
969 }
970
971 if (strcmp(protocol, "vnc") == 0) {
972 if (fail_if_connected || disconnect_if_connected) {
973 /* vnc supports "connected=keep" only */
974 qerror_report(QERR_INVALID_PARAMETER, "connected");
975 return -1;
976 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600977 /* Note that setting an empty password will not disable login through
978 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +0100979 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200980 }
981
982 qerror_report(QERR_INVALID_PARAMETER, "protocol");
983 return -1;
984}
985
986static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
987{
988 const char *protocol = qdict_get_str(qdict, "protocol");
989 const char *whenstr = qdict_get_str(qdict, "time");
990 time_t when;
991 int rc;
992
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100993 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200994 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100995 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200996 when = TIME_MAX;
997 } else if (whenstr[0] == '+') {
998 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
999 } else {
1000 when = strtoull(whenstr, NULL, 10);
1001 }
1002
1003 if (strcmp(protocol, "spice") == 0) {
1004 if (!using_spice) {
1005 /* correct one? spice isn't a device ,,, */
1006 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1007 return -1;
1008 }
1009 rc = qemu_spice_set_pw_expire(when);
1010 if (rc != 0) {
1011 qerror_report(QERR_SET_PASSWD_FAILED);
1012 return -1;
1013 }
1014 return 0;
1015 }
1016
1017 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001018 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001019 }
1020
1021 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1022 return -1;
1023}
1024
Daniel P. Berrange13661082011-06-23 13:31:42 +01001025static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1026{
1027 const char *protocol = qdict_get_str(qdict, "protocol");
1028 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001029 CharDriverState *s;
1030
1031 if (strcmp(protocol, "spice") == 0) {
1032 if (!using_spice) {
1033 /* correct one? spice isn't a device ,,, */
1034 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1035 return -1;
1036 }
1037 qerror_report(QERR_ADD_CLIENT_FAILED);
1038 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001039#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001040 } else if (strcmp(protocol, "vnc") == 0) {
1041 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001042 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001043 vnc_display_add_client(NULL, fd, skipauth);
1044 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001045#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001046 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1047 int fd = monitor_get_fd(mon, fdname);
1048 if (qemu_chr_add_client(s, fd) < 0) {
1049 qerror_report(QERR_ADD_CLIENT_FAILED);
1050 return -1;
1051 }
1052 return 0;
1053 }
1054
1055 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1056 return -1;
1057}
1058
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001059static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1060{
1061 const char *protocol = qdict_get_str(qdict, "protocol");
1062 const char *hostname = qdict_get_str(qdict, "hostname");
1063 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1064 int port = qdict_get_try_int(qdict, "port", -1);
1065 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1066 int ret;
1067
1068 if (strcmp(protocol, "spice") == 0) {
1069 if (!using_spice) {
1070 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1071 return -1;
1072 }
1073
1074 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1075 if (ret != 0) {
1076 qerror_report(QERR_UNDEFINED_ERROR);
1077 return -1;
1078 }
1079 return 0;
1080 }
1081
1082 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1083 return -1;
1084}
1085
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001086static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001087{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001088 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001089 return 0;
bellard59a983b2004-03-17 23:17:16 +00001090}
1091
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001092static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001093{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001095}
1096
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001097static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001098{
1099 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001100 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001101
bellard9307c4c2004-04-04 12:57:25 +00001102 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001103 mask = 0;
1104 } else {
bellard9307c4c2004-04-04 12:57:25 +00001105 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001106 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001107 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001108 return;
1109 }
1110 }
1111 cpu_set_log(mask);
1112}
1113
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001114static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001115{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001116 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001117 if (!option || !strcmp(option, "on")) {
1118 singlestep = 1;
1119 } else if (!strcmp(option, "off")) {
1120 singlestep = 0;
1121 } else {
1122 monitor_printf(mon, "unexpected option %s\n", option);
1123 }
1124}
1125
aliguoribb5fc202009-03-05 23:01:15 +00001126static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001127
aliguori376253e2009-03-05 23:01:23 +00001128struct bdrv_iterate_context {
1129 Monitor *mon;
1130 int err;
1131};
aliguoric0f4ce72009-03-05 23:01:01 +00001132
Luiz Capitulino28a72822011-09-26 17:43:50 -03001133static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1134{
1135 bdrv_iostatus_reset(bs);
1136}
1137
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001138/**
1139 * do_cont(): Resume emulation.
1140 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001141static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001142{
1143 struct bdrv_iterate_context context = { mon, 0 };
1144
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001145 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301146 qerror_report(QERR_MIGRATION_EXPECTED);
1147 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001148 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1149 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001150 qerror_report(QERR_RESET_REQUIRED);
1151 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301152 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001153
Luiz Capitulino28a72822011-09-26 17:43:50 -03001154 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001155 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001156 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001157 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001158 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001159 return 0;
1160 } else {
1161 return -1;
1162 }
bellard8a7ddc32004-03-31 19:00:16 +00001163}
1164
aliguoribb5fc202009-03-05 23:01:15 +00001165static void bdrv_key_cb(void *opaque, int err)
1166{
aliguori376253e2009-03-05 23:01:23 +00001167 Monitor *mon = opaque;
1168
aliguoribb5fc202009-03-05 23:01:15 +00001169 /* another key was set successfully, retry to continue */
1170 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001171 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001172}
1173
1174static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1175{
aliguori376253e2009-03-05 23:01:23 +00001176 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001177
aliguori376253e2009-03-05 23:01:23 +00001178 if (!context->err && bdrv_key_required(bs)) {
1179 context->err = -EBUSY;
1180 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1181 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001182 }
1183}
1184
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001185static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001186{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001188 if (!device)
1189 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1190 if (gdbserver_start(device) < 0) {
1191 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1192 device);
1193 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001194 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001195 } else {
aliguori59030a82009-04-05 18:43:41 +00001196 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1197 device);
bellard8a7ddc32004-03-31 19:00:16 +00001198 }
1199}
1200
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001202{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001203 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001204 if (select_watchdog_action(action) == -1) {
1205 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1206 }
1207}
1208
aliguori376253e2009-03-05 23:01:23 +00001209static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001210{
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001212 switch(c) {
1213 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 default:
1226 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001228 } else {
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001230 }
1231 break;
1232 }
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001234}
1235
aliguori376253e2009-03-05 23:01:23 +00001236static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001237 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001238{
bellard6a00d602005-11-21 23:25:50 +00001239 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001240 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001241 uint8_t buf[16];
1242 uint64_t v;
1243
1244 if (format == 'i') {
1245 int flags;
1246 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001247 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001248#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001249 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001250 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001251 } else if (wsize == 4) {
1252 flags = 0;
1253 } else {
bellard6a15fd12006-04-12 21:07:07 +00001254 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001255 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001256 if (env) {
1257#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001258 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001259 (env->segs[R_CS].flags & DESC_L_MASK))
1260 flags = 2;
1261 else
1262#endif
1263 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1264 flags = 1;
1265 }
bellard4c27ba22004-04-25 18:05:08 +00001266 }
1267#endif
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001269 return;
1270 }
1271
1272 len = wsize * count;
1273 if (wsize == 1)
1274 line_size = 8;
1275 else
1276 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001277 max_digits = 0;
1278
1279 switch(format) {
1280 case 'o':
1281 max_digits = (wsize * 8 + 2) / 3;
1282 break;
1283 default:
1284 case 'x':
1285 max_digits = (wsize * 8) / 4;
1286 break;
1287 case 'u':
1288 case 'd':
1289 max_digits = (wsize * 8 * 10 + 32) / 33;
1290 break;
1291 case 'c':
1292 wsize = 1;
1293 break;
1294 }
1295
1296 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001297 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001299 else
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001301 l = len;
1302 if (l > line_size)
1303 l = line_size;
1304 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001305 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001306 } else {
bellard6a00d602005-11-21 23:25:50 +00001307 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001308 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001310 break;
1311 }
bellard9307c4c2004-04-04 12:57:25 +00001312 }
ths5fafdf22007-09-16 21:08:06 +00001313 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001314 while (i < l) {
1315 switch(wsize) {
1316 default:
1317 case 1:
1318 v = ldub_raw(buf + i);
1319 break;
1320 case 2:
1321 v = lduw_raw(buf + i);
1322 break;
1323 case 4:
bellard92a31b12005-02-10 22:00:52 +00001324 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001325 break;
1326 case 8:
1327 v = ldq_raw(buf + i);
1328 break;
1329 }
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001331 switch(format) {
1332 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001334 break;
1335 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001337 break;
1338 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001339 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001340 break;
1341 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001343 break;
1344 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001345 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001346 break;
1347 }
1348 i += wsize;
1349 }
aliguori376253e2009-03-05 23:01:23 +00001350 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001351 addr += l;
1352 len -= l;
1353 }
1354}
1355
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001357{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001358 int count = qdict_get_int(qdict, "count");
1359 int format = qdict_get_int(qdict, "format");
1360 int size = qdict_get_int(qdict, "size");
1361 target_long addr = qdict_get_int(qdict, "addr");
1362
aliguori376253e2009-03-05 23:01:23 +00001363 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001364}
1365
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001366static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001367{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001368 int count = qdict_get_int(qdict, "count");
1369 int format = qdict_get_int(qdict, "format");
1370 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001371 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001372
aliguori376253e2009-03-05 23:01:23 +00001373 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001374}
1375
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001376static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001377{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001378 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001379 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001380
blueswir17743e582007-09-24 18:39:04 +00001381#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001382 switch(format) {
1383 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001385 break;
1386 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001391 break;
1392 default:
1393 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001398 break;
1399 }
bellard92a31b12005-02-10 22:00:52 +00001400#else
1401 switch(format) {
1402 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001404 break;
1405 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001407 break;
1408 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001409 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001410 break;
1411 default:
1412 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001414 break;
1415 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001417 break;
1418 }
1419#endif
aliguori376253e2009-03-05 23:01:23 +00001420 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001421}
1422
Luiz Capitulino98696222010-02-10 23:49:58 -02001423static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001424{
1425 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001426 uint32_t size = qdict_get_int(qdict, "size");
1427 const char *filename = qdict_get_str(qdict, "filename");
1428 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001429 uint32_t l;
1430 CPUState *env;
1431 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001432 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001433
1434 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001435
1436 f = fopen(filename, "wb");
1437 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001438 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001439 return -1;
bellardb371dc52007-01-03 15:20:39 +00001440 }
1441 while (size != 0) {
1442 l = sizeof(buf);
1443 if (l > size)
1444 l = size;
1445 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001446 if (fwrite(buf, 1, l, f) != l) {
1447 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1448 goto exit;
1449 }
bellardb371dc52007-01-03 15:20:39 +00001450 addr += l;
1451 size -= l;
1452 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001453
1454 ret = 0;
1455
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001456exit:
bellardb371dc52007-01-03 15:20:39 +00001457 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001458 return ret;
bellardb371dc52007-01-03 15:20:39 +00001459}
1460
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001461static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001462 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001463{
1464 FILE *f;
1465 uint32_t l;
1466 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001467 uint32_t size = qdict_get_int(qdict, "size");
1468 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001469 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001470 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001471
1472 f = fopen(filename, "wb");
1473 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001474 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001475 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001476 }
1477 while (size != 0) {
1478 l = sizeof(buf);
1479 if (l > size)
1480 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001481 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001482 if (fwrite(buf, 1, l, f) != l) {
1483 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1484 goto exit;
1485 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001486 fflush(f);
1487 addr += l;
1488 size -= l;
1489 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001490
1491 ret = 0;
1492
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001493exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001494 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001495 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001496}
1497
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001498static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001499{
1500 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001501 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001502 uint32_t start = qdict_get_int(qdict, "start");
1503 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001504
1505 sum = 0;
1506 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001507 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001508 /* BSD sum algorithm ('sum' Unix command) */
1509 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001510 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001511 }
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001513}
1514
bellarda3a91a32004-06-04 11:06:21 +00001515typedef struct {
1516 int keycode;
1517 const char *name;
1518} KeyDef;
1519
1520static const KeyDef key_defs[] = {
1521 { 0x2a, "shift" },
1522 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001523
bellarda3a91a32004-06-04 11:06:21 +00001524 { 0x38, "alt" },
1525 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001526 { 0x64, "altgr" },
1527 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001528 { 0x1d, "ctrl" },
1529 { 0x9d, "ctrl_r" },
1530
1531 { 0xdd, "menu" },
1532
1533 { 0x01, "esc" },
1534
1535 { 0x02, "1" },
1536 { 0x03, "2" },
1537 { 0x04, "3" },
1538 { 0x05, "4" },
1539 { 0x06, "5" },
1540 { 0x07, "6" },
1541 { 0x08, "7" },
1542 { 0x09, "8" },
1543 { 0x0a, "9" },
1544 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001545 { 0x0c, "minus" },
1546 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001547 { 0x0e, "backspace" },
1548
1549 { 0x0f, "tab" },
1550 { 0x10, "q" },
1551 { 0x11, "w" },
1552 { 0x12, "e" },
1553 { 0x13, "r" },
1554 { 0x14, "t" },
1555 { 0x15, "y" },
1556 { 0x16, "u" },
1557 { 0x17, "i" },
1558 { 0x18, "o" },
1559 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001560 { 0x1a, "bracket_left" },
1561 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001562 { 0x1c, "ret" },
1563
1564 { 0x1e, "a" },
1565 { 0x1f, "s" },
1566 { 0x20, "d" },
1567 { 0x21, "f" },
1568 { 0x22, "g" },
1569 { 0x23, "h" },
1570 { 0x24, "j" },
1571 { 0x25, "k" },
1572 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001573 { 0x27, "semicolon" },
1574 { 0x28, "apostrophe" },
1575 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001576
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001577 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001578 { 0x2c, "z" },
1579 { 0x2d, "x" },
1580 { 0x2e, "c" },
1581 { 0x2f, "v" },
1582 { 0x30, "b" },
1583 { 0x31, "n" },
1584 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001585 { 0x33, "comma" },
1586 { 0x34, "dot" },
1587 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001588
balrog4d3b6f62008-02-10 16:33:14 +00001589 { 0x37, "asterisk" },
1590
bellarda3a91a32004-06-04 11:06:21 +00001591 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001592 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001593 { 0x3b, "f1" },
1594 { 0x3c, "f2" },
1595 { 0x3d, "f3" },
1596 { 0x3e, "f4" },
1597 { 0x3f, "f5" },
1598 { 0x40, "f6" },
1599 { 0x41, "f7" },
1600 { 0x42, "f8" },
1601 { 0x43, "f9" },
1602 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001603 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001604 { 0x46, "scroll_lock" },
1605
bellard64866c32006-05-07 18:03:31 +00001606 { 0xb5, "kp_divide" },
1607 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001608 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001609 { 0x4e, "kp_add" },
1610 { 0x9c, "kp_enter" },
1611 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001612 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001613
1614 { 0x52, "kp_0" },
1615 { 0x4f, "kp_1" },
1616 { 0x50, "kp_2" },
1617 { 0x51, "kp_3" },
1618 { 0x4b, "kp_4" },
1619 { 0x4c, "kp_5" },
1620 { 0x4d, "kp_6" },
1621 { 0x47, "kp_7" },
1622 { 0x48, "kp_8" },
1623 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001624
bellarda3a91a32004-06-04 11:06:21 +00001625 { 0x56, "<" },
1626
1627 { 0x57, "f11" },
1628 { 0x58, "f12" },
1629
1630 { 0xb7, "print" },
1631
1632 { 0xc7, "home" },
1633 { 0xc9, "pgup" },
1634 { 0xd1, "pgdn" },
1635 { 0xcf, "end" },
1636
1637 { 0xcb, "left" },
1638 { 0xc8, "up" },
1639 { 0xd0, "down" },
1640 { 0xcd, "right" },
1641
1642 { 0xd2, "insert" },
1643 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001644#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1645 { 0xf0, "stop" },
1646 { 0xf1, "again" },
1647 { 0xf2, "props" },
1648 { 0xf3, "undo" },
1649 { 0xf4, "front" },
1650 { 0xf5, "copy" },
1651 { 0xf6, "open" },
1652 { 0xf7, "paste" },
1653 { 0xf8, "find" },
1654 { 0xf9, "cut" },
1655 { 0xfa, "lf" },
1656 { 0xfb, "help" },
1657 { 0xfc, "meta_l" },
1658 { 0xfd, "meta_r" },
1659 { 0xfe, "compose" },
1660#endif
bellarda3a91a32004-06-04 11:06:21 +00001661 { 0, NULL },
1662};
1663
1664static int get_keycode(const char *key)
1665{
1666 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001667 char *endp;
1668 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001669
1670 for(p = key_defs; p->name != NULL; p++) {
1671 if (!strcmp(key, p->name))
1672 return p->keycode;
1673 }
bellard64866c32006-05-07 18:03:31 +00001674 if (strstart(key, "0x", NULL)) {
1675 ret = strtoul(key, &endp, 0);
1676 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1677 return ret;
1678 }
bellarda3a91a32004-06-04 11:06:21 +00001679 return -1;
1680}
1681
balrogc8256f92008-06-08 22:45:01 +00001682#define MAX_KEYCODES 16
1683static uint8_t keycodes[MAX_KEYCODES];
1684static int nb_pending_keycodes;
1685static QEMUTimer *key_timer;
1686
1687static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001688{
balrogc8256f92008-06-08 22:45:01 +00001689 int keycode;
1690
1691 while (nb_pending_keycodes > 0) {
1692 nb_pending_keycodes--;
1693 keycode = keycodes[nb_pending_keycodes];
1694 if (keycode & 0x80)
1695 kbd_put_keycode(0xe0);
1696 kbd_put_keycode(keycode | 0x80);
1697 }
1698}
1699
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001700static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001701{
balrog3401c0d2008-06-04 10:05:59 +00001702 char keyname_buf[16];
1703 char *separator;
1704 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001705 const char *string = qdict_get_str(qdict, "string");
1706 int has_hold_time = qdict_haskey(qdict, "hold_time");
1707 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001708
balrogc8256f92008-06-08 22:45:01 +00001709 if (nb_pending_keycodes > 0) {
1710 qemu_del_timer(key_timer);
1711 release_keys(NULL);
1712 }
1713 if (!has_hold_time)
1714 hold_time = 100;
1715 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001716 while (1) {
1717 separator = strchr(string, '-');
1718 keyname_len = separator ? separator - string : strlen(string);
1719 if (keyname_len > 0) {
1720 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1721 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001722 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001723 return;
bellarda3a91a32004-06-04 11:06:21 +00001724 }
balrogc8256f92008-06-08 22:45:01 +00001725 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001726 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001727 return;
1728 }
1729 keyname_buf[keyname_len] = 0;
1730 keycode = get_keycode(keyname_buf);
1731 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001732 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001733 return;
1734 }
balrogc8256f92008-06-08 22:45:01 +00001735 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001736 }
balrog3401c0d2008-06-04 10:05:59 +00001737 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001738 break;
balrog3401c0d2008-06-04 10:05:59 +00001739 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001740 }
balrogc8256f92008-06-08 22:45:01 +00001741 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001742 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001743 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001744 keycode = keycodes[i];
1745 if (keycode & 0x80)
1746 kbd_put_keycode(0xe0);
1747 kbd_put_keycode(keycode & 0x7f);
1748 }
balrogc8256f92008-06-08 22:45:01 +00001749 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001750 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001751 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001752}
1753
bellard13224a82006-07-14 22:03:35 +00001754static int mouse_button_state;
1755
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001756static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001757{
1758 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001759 const char *dx_str = qdict_get_str(qdict, "dx_str");
1760 const char *dy_str = qdict_get_str(qdict, "dy_str");
1761 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001762 dx = strtol(dx_str, NULL, 0);
1763 dy = strtol(dy_str, NULL, 0);
1764 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001765 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001766 dz = strtol(dz_str, NULL, 0);
1767 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1768}
1769
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001770static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001771{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001772 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001773 mouse_button_state = button_state;
1774 kbd_mouse_event(0, 0, 0, mouse_button_state);
1775}
1776
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001777static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001778{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001779 int size = qdict_get_int(qdict, "size");
1780 int addr = qdict_get_int(qdict, "addr");
1781 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001782 uint32_t val;
1783 int suffix;
1784
1785 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001786 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001787 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001788 addr++;
1789 }
1790 addr &= 0xffff;
1791
1792 switch(size) {
1793 default:
1794 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001795 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001796 suffix = 'b';
1797 break;
1798 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001799 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001800 suffix = 'w';
1801 break;
1802 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001803 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001804 suffix = 'l';
1805 break;
1806 }
aliguori376253e2009-03-05 23:01:23 +00001807 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1808 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001809}
bellarda3a91a32004-06-04 11:06:21 +00001810
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001811static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001812{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001813 int size = qdict_get_int(qdict, "size");
1814 int addr = qdict_get_int(qdict, "addr");
1815 int val = qdict_get_int(qdict, "val");
1816
Jan Kiszkaf1147842009-07-14 10:20:11 +02001817 addr &= IOPORTS_MASK;
1818
1819 switch (size) {
1820 default:
1821 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001822 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001823 break;
1824 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001825 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001826 break;
1827 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001828 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001829 break;
1830 }
1831}
1832
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001833static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001834{
1835 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001836 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001837
Jan Kiszka76e30d02009-07-02 00:19:02 +02001838 res = qemu_boot_set(bootdevice);
1839 if (res == 0) {
1840 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1841 } else if (res > 0) {
1842 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001843 } else {
aliguori376253e2009-03-05 23:01:23 +00001844 monitor_printf(mon, "no function defined to set boot device list for "
1845 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001846 }
1847}
1848
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001849/**
Luiz Capitulino43076662009-10-07 13:41:59 -03001850 * do_system_powerdown(): Issue a machine powerdown
1851 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001852static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1853 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001854{
1855 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001856 return 0;
bellard34751872005-07-02 14:31:34 +00001857}
1858
bellardb86bda52004-09-18 19:32:46 +00001859#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001860static void print_pte(Monitor *mon, target_phys_addr_t addr,
1861 target_phys_addr_t pte,
1862 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001863{
Blue Swirld65aaf32010-12-11 18:56:24 +00001864#ifdef TARGET_X86_64
1865 if (addr & (1ULL << 47)) {
1866 addr |= -1LL << 48;
1867 }
1868#endif
1869 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1870 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001871 addr,
1872 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001873 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001874 pte & PG_GLOBAL_MASK ? 'G' : '-',
1875 pte & PG_PSE_MASK ? 'P' : '-',
1876 pte & PG_DIRTY_MASK ? 'D' : '-',
1877 pte & PG_ACCESSED_MASK ? 'A' : '-',
1878 pte & PG_PCD_MASK ? 'C' : '-',
1879 pte & PG_PWT_MASK ? 'T' : '-',
1880 pte & PG_USER_MASK ? 'U' : '-',
1881 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001882}
1883
Blue Swirld65aaf32010-12-11 18:56:24 +00001884static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001885{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001886 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001887 uint32_t pgd, pde, pte;
1888
bellardb86bda52004-09-18 19:32:46 +00001889 pgd = env->cr[3] & ~0xfff;
1890 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001891 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001892 pde = le32_to_cpu(pde);
1893 if (pde & PG_PRESENT_MASK) {
1894 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001895 /* 4M pages */
1896 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001897 } else {
1898 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001899 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001900 pte = le32_to_cpu(pte);
1901 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001902 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001903 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001904 ~0xfff);
1905 }
1906 }
1907 }
1908 }
1909 }
1910}
1911
Blue Swirld65aaf32010-12-11 18:56:24 +00001912static void tlb_info_pae32(Monitor *mon, CPUState *env)
1913{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001914 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001915 uint64_t pdpe, pde, pte;
1916 uint64_t pdp_addr, pd_addr, pt_addr;
1917
1918 pdp_addr = env->cr[3] & ~0x1f;
1919 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001920 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001921 pdpe = le64_to_cpu(pdpe);
1922 if (pdpe & PG_PRESENT_MASK) {
1923 pd_addr = pdpe & 0x3fffffffff000ULL;
1924 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001925 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001926 pde = le64_to_cpu(pde);
1927 if (pde & PG_PRESENT_MASK) {
1928 if (pde & PG_PSE_MASK) {
1929 /* 2M pages with PAE, CR4.PSE is ignored */
1930 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1931 ~((target_phys_addr_t)(1 << 20) - 1));
1932 } else {
1933 pt_addr = pde & 0x3fffffffff000ULL;
1934 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001935 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001936 pte = le64_to_cpu(pte);
1937 if (pte & PG_PRESENT_MASK) {
1938 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1939 + (l3 << 12),
1940 pte & ~PG_PSE_MASK,
1941 ~(target_phys_addr_t)0xfff);
1942 }
1943 }
1944 }
1945 }
1946 }
1947 }
1948 }
1949}
1950
1951#ifdef TARGET_X86_64
1952static void tlb_info_64(Monitor *mon, CPUState *env)
1953{
1954 uint64_t l1, l2, l3, l4;
1955 uint64_t pml4e, pdpe, pde, pte;
1956 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1957
1958 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1959 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001960 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001961 pml4e = le64_to_cpu(pml4e);
1962 if (pml4e & PG_PRESENT_MASK) {
1963 pdp_addr = pml4e & 0x3fffffffff000ULL;
1964 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001965 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001966 pdpe = le64_to_cpu(pdpe);
1967 if (pdpe & PG_PRESENT_MASK) {
1968 if (pdpe & PG_PSE_MASK) {
1969 /* 1G pages, CR4.PSE is ignored */
1970 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1971 0x3ffffc0000000ULL);
1972 } else {
1973 pd_addr = pdpe & 0x3fffffffff000ULL;
1974 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001975 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001976 pde = le64_to_cpu(pde);
1977 if (pde & PG_PRESENT_MASK) {
1978 if (pde & PG_PSE_MASK) {
1979 /* 2M pages, CR4.PSE is ignored */
1980 print_pte(mon, (l1 << 39) + (l2 << 30) +
1981 (l3 << 21), pde,
1982 0x3ffffffe00000ULL);
1983 } else {
1984 pt_addr = pde & 0x3fffffffff000ULL;
1985 for (l4 = 0; l4 < 512; l4++) {
1986 cpu_physical_memory_read(pt_addr
1987 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001988 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001989 pte = le64_to_cpu(pte);
1990 if (pte & PG_PRESENT_MASK) {
1991 print_pte(mon, (l1 << 39) +
1992 (l2 << 30) +
1993 (l3 << 21) + (l4 << 12),
1994 pte & ~PG_PSE_MASK,
1995 0x3fffffffff000ULL);
1996 }
1997 }
1998 }
1999 }
2000 }
2001 }
2002 }
2003 }
2004 }
2005 }
2006}
2007#endif
2008
2009static void tlb_info(Monitor *mon)
2010{
2011 CPUState *env;
2012
2013 env = mon_get_cpu();
2014
2015 if (!(env->cr[0] & CR0_PG_MASK)) {
2016 monitor_printf(mon, "PG disabled\n");
2017 return;
2018 }
2019 if (env->cr[4] & CR4_PAE_MASK) {
2020#ifdef TARGET_X86_64
2021 if (env->hflags & HF_LMA_MASK) {
2022 tlb_info_64(mon, env);
2023 } else
2024#endif
2025 {
2026 tlb_info_pae32(mon, env);
2027 }
2028 } else {
2029 tlb_info_32(mon, env);
2030 }
2031}
2032
Blue Swirl1b3cba62010-12-11 18:56:27 +00002033static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2034 int *plast_prot,
2035 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002036{
bellard9746b152004-11-11 18:30:24 +00002037 int prot1;
2038 prot1 = *plast_prot;
2039 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002040 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002041 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2042 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002043 *pstart, end, end - *pstart,
2044 prot1 & PG_USER_MASK ? 'u' : '-',
2045 'r',
2046 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002047 }
2048 if (prot != 0)
2049 *pstart = end;
2050 else
2051 *pstart = -1;
2052 *plast_prot = prot;
2053 }
2054}
2055
Blue Swirl1b3cba62010-12-11 18:56:27 +00002056static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002057{
Austin Clementsb49ca722011-08-14 23:19:21 -04002058 unsigned int l1, l2;
2059 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002060 uint32_t pgd, pde, pte;
2061 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002062
bellardb86bda52004-09-18 19:32:46 +00002063 pgd = env->cr[3] & ~0xfff;
2064 last_prot = 0;
2065 start = -1;
2066 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002067 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002068 pde = le32_to_cpu(pde);
2069 end = l1 << 22;
2070 if (pde & PG_PRESENT_MASK) {
2071 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2072 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002073 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002074 } else {
2075 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002076 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002077 pte = le32_to_cpu(pte);
2078 end = (l1 << 22) + (l2 << 12);
2079 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002080 prot = pte & pde &
2081 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002082 } else {
2083 prot = 0;
2084 }
aliguori376253e2009-03-05 23:01:23 +00002085 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002086 }
2087 }
2088 } else {
2089 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002090 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002091 }
2092 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002093 /* Flush last range */
2094 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002095}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002096
2097static void mem_info_pae32(Monitor *mon, CPUState *env)
2098{
Austin Clementsb49ca722011-08-14 23:19:21 -04002099 unsigned int l1, l2, l3;
2100 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002101 uint64_t pdpe, pde, pte;
2102 uint64_t pdp_addr, pd_addr, pt_addr;
2103 target_phys_addr_t start, end;
2104
2105 pdp_addr = env->cr[3] & ~0x1f;
2106 last_prot = 0;
2107 start = -1;
2108 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002109 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002110 pdpe = le64_to_cpu(pdpe);
2111 end = l1 << 30;
2112 if (pdpe & PG_PRESENT_MASK) {
2113 pd_addr = pdpe & 0x3fffffffff000ULL;
2114 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002115 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002116 pde = le64_to_cpu(pde);
2117 end = (l1 << 30) + (l2 << 21);
2118 if (pde & PG_PRESENT_MASK) {
2119 if (pde & PG_PSE_MASK) {
2120 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2121 PG_PRESENT_MASK);
2122 mem_print(mon, &start, &last_prot, end, prot);
2123 } else {
2124 pt_addr = pde & 0x3fffffffff000ULL;
2125 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002126 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002127 pte = le64_to_cpu(pte);
2128 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2129 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002130 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2131 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002132 } else {
2133 prot = 0;
2134 }
2135 mem_print(mon, &start, &last_prot, end, prot);
2136 }
2137 }
2138 } else {
2139 prot = 0;
2140 mem_print(mon, &start, &last_prot, end, prot);
2141 }
2142 }
2143 } else {
2144 prot = 0;
2145 mem_print(mon, &start, &last_prot, end, prot);
2146 }
2147 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002148 /* Flush last range */
2149 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002150}
2151
2152
2153#ifdef TARGET_X86_64
2154static void mem_info_64(Monitor *mon, CPUState *env)
2155{
2156 int prot, last_prot;
2157 uint64_t l1, l2, l3, l4;
2158 uint64_t pml4e, pdpe, pde, pte;
2159 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2160
2161 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2162 last_prot = 0;
2163 start = -1;
2164 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002165 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002166 pml4e = le64_to_cpu(pml4e);
2167 end = l1 << 39;
2168 if (pml4e & PG_PRESENT_MASK) {
2169 pdp_addr = pml4e & 0x3fffffffff000ULL;
2170 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002171 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002172 pdpe = le64_to_cpu(pdpe);
2173 end = (l1 << 39) + (l2 << 30);
2174 if (pdpe & PG_PRESENT_MASK) {
2175 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002176 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2177 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002178 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002179 mem_print(mon, &start, &last_prot, end, prot);
2180 } else {
2181 pd_addr = pdpe & 0x3fffffffff000ULL;
2182 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002183 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002184 pde = le64_to_cpu(pde);
2185 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2186 if (pde & PG_PRESENT_MASK) {
2187 if (pde & PG_PSE_MASK) {
2188 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2189 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002190 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002191 mem_print(mon, &start, &last_prot, end, prot);
2192 } else {
2193 pt_addr = pde & 0x3fffffffff000ULL;
2194 for (l4 = 0; l4 < 512; l4++) {
2195 cpu_physical_memory_read(pt_addr
2196 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002197 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002198 pte = le64_to_cpu(pte);
2199 end = (l1 << 39) + (l2 << 30) +
2200 (l3 << 21) + (l4 << 12);
2201 if (pte & PG_PRESENT_MASK) {
2202 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2203 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002204 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002205 } else {
2206 prot = 0;
2207 }
2208 mem_print(mon, &start, &last_prot, end, prot);
2209 }
2210 }
2211 } else {
2212 prot = 0;
2213 mem_print(mon, &start, &last_prot, end, prot);
2214 }
2215 }
2216 }
2217 } else {
2218 prot = 0;
2219 mem_print(mon, &start, &last_prot, end, prot);
2220 }
2221 }
2222 } else {
2223 prot = 0;
2224 mem_print(mon, &start, &last_prot, end, prot);
2225 }
2226 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002227 /* Flush last range */
2228 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002229}
2230#endif
2231
2232static void mem_info(Monitor *mon)
2233{
2234 CPUState *env;
2235
2236 env = mon_get_cpu();
2237
2238 if (!(env->cr[0] & CR0_PG_MASK)) {
2239 monitor_printf(mon, "PG disabled\n");
2240 return;
2241 }
2242 if (env->cr[4] & CR4_PAE_MASK) {
2243#ifdef TARGET_X86_64
2244 if (env->hflags & HF_LMA_MASK) {
2245 mem_info_64(mon, env);
2246 } else
2247#endif
2248 {
2249 mem_info_pae32(mon, env);
2250 }
2251 } else {
2252 mem_info_32(mon, env);
2253 }
2254}
bellardb86bda52004-09-18 19:32:46 +00002255#endif
2256
aurel327c664e22009-03-03 06:12:22 +00002257#if defined(TARGET_SH4)
2258
aliguori376253e2009-03-05 23:01:23 +00002259static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002260{
aliguori376253e2009-03-05 23:01:23 +00002261 monitor_printf(mon, " tlb%i:\t"
2262 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2263 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2264 "dirty=%hhu writethrough=%hhu\n",
2265 idx,
2266 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2267 tlb->v, tlb->sh, tlb->c, tlb->pr,
2268 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002269}
2270
aliguori376253e2009-03-05 23:01:23 +00002271static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002272{
2273 CPUState *env = mon_get_cpu();
2274 int i;
2275
aliguori376253e2009-03-05 23:01:23 +00002276 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002277 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002278 print_tlb (mon, i, &env->itlb[i]);
2279 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002280 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002281 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002282}
2283
2284#endif
2285
Scott Woodbebabbc2011-08-18 10:38:42 +00002286#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002287static void tlb_info(Monitor *mon)
2288{
2289 CPUState *env1 = mon_get_cpu();
2290
2291 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2292}
2293#endif
2294
Blue Swirl314e2982011-09-11 20:22:05 +00002295static void do_info_mtree(Monitor *mon)
2296{
2297 mtree_info((fprintf_function)monitor_printf, mon);
2298}
2299
aliguori030ea372009-04-21 22:30:47 +00002300static void do_info_numa(Monitor *mon)
2301{
aliguorib28b6232009-04-22 20:20:29 +00002302 int i;
aliguori030ea372009-04-21 22:30:47 +00002303 CPUState *env;
2304
2305 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2306 for (i = 0; i < nb_numa_nodes; i++) {
2307 monitor_printf(mon, "node %d cpus:", i);
2308 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2309 if (env->numa_node == i) {
2310 monitor_printf(mon, " %d", env->cpu_index);
2311 }
2312 }
2313 monitor_printf(mon, "\n");
2314 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2315 node_mem[i] >> 20);
2316 }
2317}
2318
bellard5f1ce942006-02-08 22:40:15 +00002319#ifdef CONFIG_PROFILER
2320
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002321int64_t qemu_time;
2322int64_t dev_time;
2323
aliguori376253e2009-03-05 23:01:23 +00002324static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002325{
2326 int64_t total;
2327 total = qemu_time;
2328 if (total == 0)
2329 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002330 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002331 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002332 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002333 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002334 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002335 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002336}
2337#else
aliguori376253e2009-03-05 23:01:23 +00002338static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002339{
aliguori376253e2009-03-05 23:01:23 +00002340 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002341}
2342#endif
2343
bellardec36b692006-07-16 18:57:03 +00002344/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002345static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002346
aliguori376253e2009-03-05 23:01:23 +00002347static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002348{
2349 int i;
2350 CaptureState *s;
2351
2352 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002353 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002354 s->ops.info (s->opaque);
2355 }
2356}
2357
Blue Swirl23130862009-06-06 08:22:04 +00002358#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002359static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002360{
2361 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002362 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002363 CaptureState *s;
2364
2365 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2366 if (i == n) {
2367 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002368 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002369 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002370 return;
2371 }
2372 }
2373}
2374
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002375static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002376{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002377 const char *path = qdict_get_str(qdict, "path");
2378 int has_freq = qdict_haskey(qdict, "freq");
2379 int freq = qdict_get_try_int(qdict, "freq", -1);
2380 int has_bits = qdict_haskey(qdict, "bits");
2381 int bits = qdict_get_try_int(qdict, "bits", -1);
2382 int has_channels = qdict_haskey(qdict, "nchannels");
2383 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002384 CaptureState *s;
2385
Anthony Liguori7267c092011-08-20 22:09:37 -05002386 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002387
2388 freq = has_freq ? freq : 44100;
2389 bits = has_bits ? bits : 16;
2390 nchannels = has_channels ? nchannels : 2;
2391
2392 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002393 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002394 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002395 return;
bellardec36b692006-07-16 18:57:03 +00002396 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002397 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002398}
2399#endif
2400
aurel32dc1c0b72008-04-27 23:52:12 +00002401#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002402static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002403{
2404 CPUState *env;
2405
2406 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2407 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2408 }
2409
2410 return 0;
2411}
2412#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002413static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002414{
2415 qerror_report(QERR_UNSUPPORTED);
2416 return -1;
2417}
aurel32dc1c0b72008-04-27 23:52:12 +00002418#endif
2419
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002420static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002421{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002422 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002423
aliguori76655d62009-03-06 20:27:37 +00002424 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002425 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002426 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002427 return acl;
2428}
aliguori76655d62009-03-06 20:27:37 +00002429
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002430static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002431{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002432 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002433 qemu_acl *acl = find_acl(mon, aclname);
2434 qemu_acl_entry *entry;
2435 int i = 0;
2436
2437 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002438 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002439 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002440 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002441 i++;
2442 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002443 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002444 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002445 }
2446}
2447
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002448static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002449{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002450 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002451 qemu_acl *acl = find_acl(mon, aclname);
2452
2453 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002454 qemu_acl_reset(acl);
2455 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002456 }
2457}
aliguori76655d62009-03-06 20:27:37 +00002458
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002459static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002460{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002461 const char *aclname = qdict_get_str(qdict, "aclname");
2462 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002463 qemu_acl *acl = find_acl(mon, aclname);
2464
2465 if (acl) {
2466 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002467 acl->defaultDeny = 0;
2468 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002469 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002470 acl->defaultDeny = 1;
2471 monitor_printf(mon, "acl: policy set to 'deny'\n");
2472 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002473 monitor_printf(mon, "acl: unknown policy '%s', "
2474 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002475 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002476 }
2477}
aliguori76655d62009-03-06 20:27:37 +00002478
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002479static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002480{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002481 const char *aclname = qdict_get_str(qdict, "aclname");
2482 const char *match = qdict_get_str(qdict, "match");
2483 const char *policy = qdict_get_str(qdict, "policy");
2484 int has_index = qdict_haskey(qdict, "index");
2485 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002486 qemu_acl *acl = find_acl(mon, aclname);
2487 int deny, ret;
2488
2489 if (acl) {
2490 if (strcmp(policy, "allow") == 0) {
2491 deny = 0;
2492 } else if (strcmp(policy, "deny") == 0) {
2493 deny = 1;
2494 } else {
2495 monitor_printf(mon, "acl: unknown policy '%s', "
2496 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002497 return;
2498 }
aliguori28a76be2009-03-06 20:27:40 +00002499 if (has_index)
2500 ret = qemu_acl_insert(acl, deny, match, index);
2501 else
2502 ret = qemu_acl_append(acl, deny, match);
2503 if (ret < 0)
2504 monitor_printf(mon, "acl: unable to add acl entry\n");
2505 else
2506 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002507 }
2508}
aliguori76655d62009-03-06 20:27:37 +00002509
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002510static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002511{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002512 const char *aclname = qdict_get_str(qdict, "aclname");
2513 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002514 qemu_acl *acl = find_acl(mon, aclname);
2515 int ret;
aliguori76655d62009-03-06 20:27:37 +00002516
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002517 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002518 ret = qemu_acl_remove(acl, match);
2519 if (ret < 0)
2520 monitor_printf(mon, "acl: no matching acl entry\n");
2521 else
2522 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002523 }
2524}
2525
Huang Ying79c4f6b2009-06-23 10:05:14 +08002526#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002527static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002528{
2529 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002530 int cpu_index = qdict_get_int(qdict, "cpu_index");
2531 int bank = qdict_get_int(qdict, "bank");
2532 uint64_t status = qdict_get_int(qdict, "status");
2533 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2534 uint64_t addr = qdict_get_int(qdict, "addr");
2535 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002536 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002537
Jan Kiszka747461c2011-03-02 08:56:10 +01002538 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2539 flags |= MCE_INJECT_BROADCAST;
2540 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002541 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002542 if (cenv->cpu_index == cpu_index) {
2543 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002544 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002545 break;
2546 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002547 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002548}
2549#endif
2550
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002551static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002552{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002553 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002554 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002555 int fd;
2556
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002557 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002558 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002559 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002560 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002561 }
2562
2563 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002564 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2565 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002566 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002567 }
2568
Blue Swirl72cf2d42009-09-12 07:36:22 +00002569 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002570 if (strcmp(monfd->name, fdname) != 0) {
2571 continue;
2572 }
2573
2574 close(monfd->fd);
2575 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002576 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002577 }
2578
Anthony Liguori7267c092011-08-20 22:09:37 -05002579 monfd = g_malloc0(sizeof(mon_fd_t));
2580 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002581 monfd->fd = fd;
2582
Blue Swirl72cf2d42009-09-12 07:36:22 +00002583 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002584 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002585}
2586
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002587static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002588{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002589 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002590 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002591
Blue Swirl72cf2d42009-09-12 07:36:22 +00002592 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002593 if (strcmp(monfd->name, fdname) != 0) {
2594 continue;
2595 }
2596
Blue Swirl72cf2d42009-09-12 07:36:22 +00002597 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002598 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002599 g_free(monfd->name);
2600 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002601 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002602 }
2603
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002604 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002605 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002606}
2607
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002608static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002609{
Luiz Capitulino13548692011-07-29 15:36:43 -03002610 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002611 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002612
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002613 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002614
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002615 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002616 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002617 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002618}
2619
Mark McLoughlin7768e042009-07-22 09:11:41 +01002620int monitor_get_fd(Monitor *mon, const char *fdname)
2621{
Anthony Liguoric227f092009-10-01 16:12:16 -05002622 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002623
Blue Swirl72cf2d42009-09-12 07:36:22 +00002624 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002625 int fd;
2626
2627 if (strcmp(monfd->name, fdname) != 0) {
2628 continue;
2629 }
2630
2631 fd = monfd->fd;
2632
2633 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002634 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002635 g_free(monfd->name);
2636 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002637
2638 return fd;
2639 }
2640
2641 return -1;
2642}
2643
Anthony Liguoric227f092009-10-01 16:12:16 -05002644static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002645#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002646 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002647};
2648
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002649/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002650static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 {
2652 .name = "version",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002656 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
2659 .name = "network",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002663 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 },
2665 {
2666 .name = "chardev",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002670 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
2673 .name = "block",
2674 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 .params = "",
2676 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002677 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 },
2679 {
2680 .name = "blockstats",
2681 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .params = "",
2683 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002684 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 },
2686 {
2687 .name = "registers",
2688 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 .params = "",
2690 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002691 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 },
2693 {
2694 .name = "cpus",
2695 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .params = "",
2697 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002698 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
2700 {
2701 .name = "history",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002705 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002707#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2708 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 {
2710 .name = "irq",
2711 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 .params = "",
2713 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002714#ifdef TARGET_SPARC
2715 .mhandler.info = sun4m_irq_info,
2716#elif defined(TARGET_LM32)
2717 .mhandler.info = lm32_irq_info,
2718#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002719 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002720#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
2722 {
2723 .name = "pic",
2724 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 .params = "",
2726 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002727#ifdef TARGET_SPARC
2728 .mhandler.info = sun4m_pic_info,
2729#elif defined(TARGET_LM32)
2730 .mhandler.info = lm32_do_pic_info,
2731#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002732 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002733#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002735#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 {
2737 .name = "pci",
2738 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 .params = "",
2740 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002741 .user_print = do_pci_info_print,
2742 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002744#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2745 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 {
2747 .name = "tlb",
2748 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 .params = "",
2750 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002751 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 },
aurel327c664e22009-03-03 06:12:22 +00002753#endif
2754#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 {
2756 .name = "mem",
2757 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 .params = "",
2759 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002760 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 },
bellardb86bda52004-09-18 19:32:46 +00002762#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 {
Blue Swirl314e2982011-09-11 20:22:05 +00002764 .name = "mtree",
2765 .args_type = "",
2766 .params = "",
2767 .help = "show memory tree",
2768 .mhandler.info = do_info_mtree,
2769 },
2770 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .name = "jit",
2772 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 .params = "",
2774 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002775 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 },
2777 {
2778 .name = "kvm",
2779 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 .params = "",
2781 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002782 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 },
2784 {
2785 .name = "numa",
2786 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 .params = "",
2788 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002789 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002790 },
2791 {
2792 .name = "usb",
2793 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 .params = "",
2795 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002796 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 },
2798 {
2799 .name = "usbhost",
2800 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 .params = "",
2802 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002803 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002804 },
2805 {
2806 .name = "profile",
2807 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 .params = "",
2809 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002810 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 },
2812 {
2813 .name = "capture",
2814 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002815 .params = "",
2816 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002817 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 },
2819 {
2820 .name = "snapshots",
2821 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 .params = "",
2823 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002824 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 },
2826 {
2827 .name = "status",
2828 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 .params = "",
2830 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002831 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002832 },
2833 {
2834 .name = "pcmcia",
2835 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002836 .params = "",
2837 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002838 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002839 },
2840 {
2841 .name = "mice",
2842 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 .params = "",
2844 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002845 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002846 },
2847 {
2848 .name = "vnc",
2849 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 .params = "",
2851 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002852 .user_print = do_info_vnc_print,
2853 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002854 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002855#if defined(CONFIG_SPICE)
2856 {
2857 .name = "spice",
2858 .args_type = "",
2859 .params = "",
2860 .help = "show the spice server status",
2861 .user_print = do_info_spice_print,
2862 .mhandler.info_new = do_info_spice,
2863 },
2864#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002865 {
2866 .name = "name",
2867 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002868 .params = "",
2869 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002870 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002871 },
2872 {
2873 .name = "uuid",
2874 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 .params = "",
2876 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002877 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 },
j_mayer76a66252007-03-07 08:32:30 +00002879#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 {
2881 .name = "cpustats",
2882 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002883 .params = "",
2884 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002885 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002886 },
j_mayer76a66252007-03-07 08:32:30 +00002887#endif
blueswir131a60e22007-10-26 18:42:59 +00002888#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 {
2890 .name = "usernet",
2891 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 .params = "",
2893 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002894 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 },
blueswir131a60e22007-10-26 18:42:59 +00002896#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002897 {
2898 .name = "migrate",
2899 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002900 .params = "",
2901 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002902 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 },
2904 {
2905 .name = "balloon",
2906 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002907 .params = "",
2908 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002909 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002910 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002911 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 },
2913 {
2914 .name = "qtree",
2915 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002916 .params = "",
2917 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002918 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 },
2920 {
2921 .name = "qdm",
2922 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002923 .params = "",
2924 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002925 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 },
2927 {
2928 .name = "roms",
2929 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002930 .params = "",
2931 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002932 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002933 },
Lluís6d8a7642011-08-31 20:30:43 +02002934#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05302935 {
2936 .name = "trace",
2937 .args_type = "",
2938 .params = "",
2939 .help = "show current contents of trace buffer",
2940 .mhandler.info = do_info_trace,
2941 },
Lluís31965ae2011-08-31 20:31:24 +02002942#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05302943 {
2944 .name = "trace-events",
2945 .args_type = "",
2946 .params = "",
2947 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002948 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302949 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002950 {
2951 .name = NULL,
2952 },
bellard9dc39cb2004-03-14 21:38:27 +00002953};
2954
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002955static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002956#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002957 { /* NULL */ },
2958};
2959
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002960static const mon_cmd_t qmp_query_cmds[] = {
2961 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002962 .name = "pci",
2963 .args_type = "",
2964 .params = "",
2965 .help = "show PCI info",
2966 .user_print = do_pci_info_print,
2967 .mhandler.info_new = do_pci_info,
2968 },
2969 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002970 .name = "vnc",
2971 .args_type = "",
2972 .params = "",
2973 .help = "show the vnc server status",
2974 .user_print = do_info_vnc_print,
2975 .mhandler.info_new = do_info_vnc,
2976 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002977#if defined(CONFIG_SPICE)
2978 {
2979 .name = "spice",
2980 .args_type = "",
2981 .params = "",
2982 .help = "show the spice server status",
2983 .user_print = do_info_spice_print,
2984 .mhandler.info_new = do_info_spice,
2985 },
2986#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002987 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002988 .name = "balloon",
2989 .args_type = "",
2990 .params = "",
2991 .help = "show balloon information",
2992 .user_print = monitor_print_balloon,
2993 .mhandler.info_async = do_info_balloon,
2994 .flags = MONITOR_CMD_ASYNC,
2995 },
2996 { /* NULL */ },
2997};
2998
bellard9307c4c2004-04-04 12:57:25 +00002999/*******************************************************************/
3000
3001static const char *pch;
3002static jmp_buf expr_env;
3003
bellard92a31b12005-02-10 22:00:52 +00003004#define MD_TLONG 0
3005#define MD_I32 1
3006
bellard9307c4c2004-04-04 12:57:25 +00003007typedef struct MonitorDef {
3008 const char *name;
3009 int offset;
blueswir18662d652008-10-02 18:32:44 +00003010 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003011 int type;
bellard9307c4c2004-04-04 12:57:25 +00003012} MonitorDef;
3013
bellard57206fd2004-04-25 18:54:52 +00003014#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003015static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003016{
bellard6a00d602005-11-21 23:25:50 +00003017 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003018 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003019}
3020#endif
3021
bellarda541f292004-04-12 20:39:29 +00003022#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003023static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003024{
bellard6a00d602005-11-21 23:25:50 +00003025 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003026 unsigned int u;
3027 int i;
3028
3029 u = 0;
3030 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003031 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003032
3033 return u;
3034}
3035
blueswir18662d652008-10-02 18:32:44 +00003036static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003037{
bellard6a00d602005-11-21 23:25:50 +00003038 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003039 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003040}
3041
blueswir18662d652008-10-02 18:32:44 +00003042static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003043{
bellard6a00d602005-11-21 23:25:50 +00003044 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003045 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003046}
bellard9fddaa02004-05-21 12:59:32 +00003047
blueswir18662d652008-10-02 18:32:44 +00003048static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003049{
bellard6a00d602005-11-21 23:25:50 +00003050 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003051 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003052}
3053
blueswir18662d652008-10-02 18:32:44 +00003054static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003055{
bellard6a00d602005-11-21 23:25:50 +00003056 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003057 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003058}
3059
blueswir18662d652008-10-02 18:32:44 +00003060static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003061{
bellard6a00d602005-11-21 23:25:50 +00003062 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003063 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003064}
bellarda541f292004-04-12 20:39:29 +00003065#endif
3066
bellarde95c8d52004-09-30 22:22:08 +00003067#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003068#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003069static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003070{
bellard6a00d602005-11-21 23:25:50 +00003071 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003072
3073 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003074}
bellard7b936c02005-10-30 17:05:13 +00003075#endif
bellarde95c8d52004-09-30 22:22:08 +00003076
blueswir18662d652008-10-02 18:32:44 +00003077static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003078{
bellard6a00d602005-11-21 23:25:50 +00003079 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003080 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003081}
3082#endif
3083
blueswir18662d652008-10-02 18:32:44 +00003084static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003085#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003086
3087#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003088 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003089 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003090 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003091
bellard9307c4c2004-04-04 12:57:25 +00003092 { "eax", offsetof(CPUState, regs[0]) },
3093 { "ecx", offsetof(CPUState, regs[1]) },
3094 { "edx", offsetof(CPUState, regs[2]) },
3095 { "ebx", offsetof(CPUState, regs[3]) },
3096 { "esp|sp", offsetof(CPUState, regs[4]) },
3097 { "ebp|fp", offsetof(CPUState, regs[5]) },
3098 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003099 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003100#ifdef TARGET_X86_64
3101 { "r8", offsetof(CPUState, regs[8]) },
3102 { "r9", offsetof(CPUState, regs[9]) },
3103 { "r10", offsetof(CPUState, regs[10]) },
3104 { "r11", offsetof(CPUState, regs[11]) },
3105 { "r12", offsetof(CPUState, regs[12]) },
3106 { "r13", offsetof(CPUState, regs[13]) },
3107 { "r14", offsetof(CPUState, regs[14]) },
3108 { "r15", offsetof(CPUState, regs[15]) },
3109#endif
bellard9307c4c2004-04-04 12:57:25 +00003110 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003111 { "eip", offsetof(CPUState, eip) },
3112 SEG("cs", R_CS)
3113 SEG("ds", R_DS)
3114 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003115 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003116 SEG("fs", R_FS)
3117 SEG("gs", R_GS)
3118 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003119#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003120 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003121 { "r0", offsetof(CPUState, gpr[0]) },
3122 { "r1", offsetof(CPUState, gpr[1]) },
3123 { "r2", offsetof(CPUState, gpr[2]) },
3124 { "r3", offsetof(CPUState, gpr[3]) },
3125 { "r4", offsetof(CPUState, gpr[4]) },
3126 { "r5", offsetof(CPUState, gpr[5]) },
3127 { "r6", offsetof(CPUState, gpr[6]) },
3128 { "r7", offsetof(CPUState, gpr[7]) },
3129 { "r8", offsetof(CPUState, gpr[8]) },
3130 { "r9", offsetof(CPUState, gpr[9]) },
3131 { "r10", offsetof(CPUState, gpr[10]) },
3132 { "r11", offsetof(CPUState, gpr[11]) },
3133 { "r12", offsetof(CPUState, gpr[12]) },
3134 { "r13", offsetof(CPUState, gpr[13]) },
3135 { "r14", offsetof(CPUState, gpr[14]) },
3136 { "r15", offsetof(CPUState, gpr[15]) },
3137 { "r16", offsetof(CPUState, gpr[16]) },
3138 { "r17", offsetof(CPUState, gpr[17]) },
3139 { "r18", offsetof(CPUState, gpr[18]) },
3140 { "r19", offsetof(CPUState, gpr[19]) },
3141 { "r20", offsetof(CPUState, gpr[20]) },
3142 { "r21", offsetof(CPUState, gpr[21]) },
3143 { "r22", offsetof(CPUState, gpr[22]) },
3144 { "r23", offsetof(CPUState, gpr[23]) },
3145 { "r24", offsetof(CPUState, gpr[24]) },
3146 { "r25", offsetof(CPUState, gpr[25]) },
3147 { "r26", offsetof(CPUState, gpr[26]) },
3148 { "r27", offsetof(CPUState, gpr[27]) },
3149 { "r28", offsetof(CPUState, gpr[28]) },
3150 { "r29", offsetof(CPUState, gpr[29]) },
3151 { "r30", offsetof(CPUState, gpr[30]) },
3152 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003153 /* Floating point registers */
3154 { "f0", offsetof(CPUState, fpr[0]) },
3155 { "f1", offsetof(CPUState, fpr[1]) },
3156 { "f2", offsetof(CPUState, fpr[2]) },
3157 { "f3", offsetof(CPUState, fpr[3]) },
3158 { "f4", offsetof(CPUState, fpr[4]) },
3159 { "f5", offsetof(CPUState, fpr[5]) },
3160 { "f6", offsetof(CPUState, fpr[6]) },
3161 { "f7", offsetof(CPUState, fpr[7]) },
3162 { "f8", offsetof(CPUState, fpr[8]) },
3163 { "f9", offsetof(CPUState, fpr[9]) },
3164 { "f10", offsetof(CPUState, fpr[10]) },
3165 { "f11", offsetof(CPUState, fpr[11]) },
3166 { "f12", offsetof(CPUState, fpr[12]) },
3167 { "f13", offsetof(CPUState, fpr[13]) },
3168 { "f14", offsetof(CPUState, fpr[14]) },
3169 { "f15", offsetof(CPUState, fpr[15]) },
3170 { "f16", offsetof(CPUState, fpr[16]) },
3171 { "f17", offsetof(CPUState, fpr[17]) },
3172 { "f18", offsetof(CPUState, fpr[18]) },
3173 { "f19", offsetof(CPUState, fpr[19]) },
3174 { "f20", offsetof(CPUState, fpr[20]) },
3175 { "f21", offsetof(CPUState, fpr[21]) },
3176 { "f22", offsetof(CPUState, fpr[22]) },
3177 { "f23", offsetof(CPUState, fpr[23]) },
3178 { "f24", offsetof(CPUState, fpr[24]) },
3179 { "f25", offsetof(CPUState, fpr[25]) },
3180 { "f26", offsetof(CPUState, fpr[26]) },
3181 { "f27", offsetof(CPUState, fpr[27]) },
3182 { "f28", offsetof(CPUState, fpr[28]) },
3183 { "f29", offsetof(CPUState, fpr[29]) },
3184 { "f30", offsetof(CPUState, fpr[30]) },
3185 { "f31", offsetof(CPUState, fpr[31]) },
3186 { "fpscr", offsetof(CPUState, fpscr) },
3187 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003188 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003189 { "lr", offsetof(CPUState, lr) },
3190 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003191 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003192 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003193 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003194 { "msr", 0, &monitor_get_msr, },
3195 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003196 { "tbu", 0, &monitor_get_tbu, },
3197 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003198#if defined(TARGET_PPC64)
3199 /* Address space register */
3200 { "asr", offsetof(CPUState, asr) },
3201#endif
3202 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003203 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003204 { "sr0", offsetof(CPUState, sr[0]) },
3205 { "sr1", offsetof(CPUState, sr[1]) },
3206 { "sr2", offsetof(CPUState, sr[2]) },
3207 { "sr3", offsetof(CPUState, sr[3]) },
3208 { "sr4", offsetof(CPUState, sr[4]) },
3209 { "sr5", offsetof(CPUState, sr[5]) },
3210 { "sr6", offsetof(CPUState, sr[6]) },
3211 { "sr7", offsetof(CPUState, sr[7]) },
3212 { "sr8", offsetof(CPUState, sr[8]) },
3213 { "sr9", offsetof(CPUState, sr[9]) },
3214 { "sr10", offsetof(CPUState, sr[10]) },
3215 { "sr11", offsetof(CPUState, sr[11]) },
3216 { "sr12", offsetof(CPUState, sr[12]) },
3217 { "sr13", offsetof(CPUState, sr[13]) },
3218 { "sr14", offsetof(CPUState, sr[14]) },
3219 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003220 /* Too lazy to put BATs... */
3221 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3222
3223 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3224 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3225 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3226 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3227 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3228 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3229 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3230 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3231 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3232 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3233 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3234 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3235 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3236 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3237 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3238 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3239 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3240 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3241 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3242 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3243 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3244 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3245 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3246 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3247 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3248 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3249 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3250 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3251 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3252 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3253 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3254 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3255 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3256 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3257 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3258 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3259 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3260 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3261 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3262 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3263 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3264 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3265 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3266 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3267 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3268 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3269 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3270 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3271 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3272 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3273 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3274 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3275 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3276 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3277 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3278 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3279 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3280 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3281 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3282 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3283 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3284 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3285 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3286 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3287 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3288 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3289
bellarde95c8d52004-09-30 22:22:08 +00003290#elif defined(TARGET_SPARC)
3291 { "g0", offsetof(CPUState, gregs[0]) },
3292 { "g1", offsetof(CPUState, gregs[1]) },
3293 { "g2", offsetof(CPUState, gregs[2]) },
3294 { "g3", offsetof(CPUState, gregs[3]) },
3295 { "g4", offsetof(CPUState, gregs[4]) },
3296 { "g5", offsetof(CPUState, gregs[5]) },
3297 { "g6", offsetof(CPUState, gregs[6]) },
3298 { "g7", offsetof(CPUState, gregs[7]) },
3299 { "o0", 0, monitor_get_reg },
3300 { "o1", 1, monitor_get_reg },
3301 { "o2", 2, monitor_get_reg },
3302 { "o3", 3, monitor_get_reg },
3303 { "o4", 4, monitor_get_reg },
3304 { "o5", 5, monitor_get_reg },
3305 { "o6", 6, monitor_get_reg },
3306 { "o7", 7, monitor_get_reg },
3307 { "l0", 8, monitor_get_reg },
3308 { "l1", 9, monitor_get_reg },
3309 { "l2", 10, monitor_get_reg },
3310 { "l3", 11, monitor_get_reg },
3311 { "l4", 12, monitor_get_reg },
3312 { "l5", 13, monitor_get_reg },
3313 { "l6", 14, monitor_get_reg },
3314 { "l7", 15, monitor_get_reg },
3315 { "i0", 16, monitor_get_reg },
3316 { "i1", 17, monitor_get_reg },
3317 { "i2", 18, monitor_get_reg },
3318 { "i3", 19, monitor_get_reg },
3319 { "i4", 20, monitor_get_reg },
3320 { "i5", 21, monitor_get_reg },
3321 { "i6", 22, monitor_get_reg },
3322 { "i7", 23, monitor_get_reg },
3323 { "pc", offsetof(CPUState, pc) },
3324 { "npc", offsetof(CPUState, npc) },
3325 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003326#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003327 { "psr", 0, &monitor_get_psr, },
3328 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003329#endif
bellarde95c8d52004-09-30 22:22:08 +00003330 { "tbr", offsetof(CPUState, tbr) },
3331 { "fsr", offsetof(CPUState, fsr) },
3332 { "f0", offsetof(CPUState, fpr[0]) },
3333 { "f1", offsetof(CPUState, fpr[1]) },
3334 { "f2", offsetof(CPUState, fpr[2]) },
3335 { "f3", offsetof(CPUState, fpr[3]) },
3336 { "f4", offsetof(CPUState, fpr[4]) },
3337 { "f5", offsetof(CPUState, fpr[5]) },
3338 { "f6", offsetof(CPUState, fpr[6]) },
3339 { "f7", offsetof(CPUState, fpr[7]) },
3340 { "f8", offsetof(CPUState, fpr[8]) },
3341 { "f9", offsetof(CPUState, fpr[9]) },
3342 { "f10", offsetof(CPUState, fpr[10]) },
3343 { "f11", offsetof(CPUState, fpr[11]) },
3344 { "f12", offsetof(CPUState, fpr[12]) },
3345 { "f13", offsetof(CPUState, fpr[13]) },
3346 { "f14", offsetof(CPUState, fpr[14]) },
3347 { "f15", offsetof(CPUState, fpr[15]) },
3348 { "f16", offsetof(CPUState, fpr[16]) },
3349 { "f17", offsetof(CPUState, fpr[17]) },
3350 { "f18", offsetof(CPUState, fpr[18]) },
3351 { "f19", offsetof(CPUState, fpr[19]) },
3352 { "f20", offsetof(CPUState, fpr[20]) },
3353 { "f21", offsetof(CPUState, fpr[21]) },
3354 { "f22", offsetof(CPUState, fpr[22]) },
3355 { "f23", offsetof(CPUState, fpr[23]) },
3356 { "f24", offsetof(CPUState, fpr[24]) },
3357 { "f25", offsetof(CPUState, fpr[25]) },
3358 { "f26", offsetof(CPUState, fpr[26]) },
3359 { "f27", offsetof(CPUState, fpr[27]) },
3360 { "f28", offsetof(CPUState, fpr[28]) },
3361 { "f29", offsetof(CPUState, fpr[29]) },
3362 { "f30", offsetof(CPUState, fpr[30]) },
3363 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003364#ifdef TARGET_SPARC64
3365 { "f32", offsetof(CPUState, fpr[32]) },
3366 { "f34", offsetof(CPUState, fpr[34]) },
3367 { "f36", offsetof(CPUState, fpr[36]) },
3368 { "f38", offsetof(CPUState, fpr[38]) },
3369 { "f40", offsetof(CPUState, fpr[40]) },
3370 { "f42", offsetof(CPUState, fpr[42]) },
3371 { "f44", offsetof(CPUState, fpr[44]) },
3372 { "f46", offsetof(CPUState, fpr[46]) },
3373 { "f48", offsetof(CPUState, fpr[48]) },
3374 { "f50", offsetof(CPUState, fpr[50]) },
3375 { "f52", offsetof(CPUState, fpr[52]) },
3376 { "f54", offsetof(CPUState, fpr[54]) },
3377 { "f56", offsetof(CPUState, fpr[56]) },
3378 { "f58", offsetof(CPUState, fpr[58]) },
3379 { "f60", offsetof(CPUState, fpr[60]) },
3380 { "f62", offsetof(CPUState, fpr[62]) },
3381 { "asi", offsetof(CPUState, asi) },
3382 { "pstate", offsetof(CPUState, pstate) },
3383 { "cansave", offsetof(CPUState, cansave) },
3384 { "canrestore", offsetof(CPUState, canrestore) },
3385 { "otherwin", offsetof(CPUState, otherwin) },
3386 { "wstate", offsetof(CPUState, wstate) },
3387 { "cleanwin", offsetof(CPUState, cleanwin) },
3388 { "fprs", offsetof(CPUState, fprs) },
3389#endif
bellard9307c4c2004-04-04 12:57:25 +00003390#endif
3391 { NULL },
3392};
3393
aliguori376253e2009-03-05 23:01:23 +00003394static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003395{
aliguori376253e2009-03-05 23:01:23 +00003396 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003397 longjmp(expr_env, 1);
3398}
3399
Markus Armbruster09b94182010-01-20 13:07:30 +01003400/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003401static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003402{
blueswir18662d652008-10-02 18:32:44 +00003403 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003404 void *ptr;
3405
bellard9307c4c2004-04-04 12:57:25 +00003406 for(md = monitor_defs; md->name != NULL; md++) {
3407 if (compare_cmd(name, md->name)) {
3408 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003409 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003410 } else {
bellard6a00d602005-11-21 23:25:50 +00003411 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003412 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003413 switch(md->type) {
3414 case MD_I32:
3415 *pval = *(int32_t *)ptr;
3416 break;
3417 case MD_TLONG:
3418 *pval = *(target_long *)ptr;
3419 break;
3420 default:
3421 *pval = 0;
3422 break;
3423 }
bellard9307c4c2004-04-04 12:57:25 +00003424 }
3425 return 0;
3426 }
3427 }
3428 return -1;
3429}
3430
3431static void next(void)
3432{
Blue Swirl660f11b2009-07-31 21:16:51 +00003433 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003434 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003435 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003436 pch++;
3437 }
3438}
3439
aliguori376253e2009-03-05 23:01:23 +00003440static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003441
aliguori376253e2009-03-05 23:01:23 +00003442static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003443{
blueswir1c2efc952007-09-25 17:28:42 +00003444 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003445 char *p;
bellard6a00d602005-11-21 23:25:50 +00003446 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003447
3448 switch(*pch) {
3449 case '+':
3450 next();
aliguori376253e2009-03-05 23:01:23 +00003451 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003452 break;
3453 case '-':
3454 next();
aliguori376253e2009-03-05 23:01:23 +00003455 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003456 break;
3457 case '~':
3458 next();
aliguori376253e2009-03-05 23:01:23 +00003459 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003460 break;
3461 case '(':
3462 next();
aliguori376253e2009-03-05 23:01:23 +00003463 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003464 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003465 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003466 }
3467 next();
3468 break;
bellard81d09122004-07-14 17:21:37 +00003469 case '\'':
3470 pch++;
3471 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003472 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003473 n = *pch;
3474 pch++;
3475 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003476 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003477 next();
3478 break;
bellard9307c4c2004-04-04 12:57:25 +00003479 case '$':
3480 {
3481 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003482 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003483
bellard9307c4c2004-04-04 12:57:25 +00003484 pch++;
3485 q = buf;
3486 while ((*pch >= 'a' && *pch <= 'z') ||
3487 (*pch >= 'A' && *pch <= 'Z') ||
3488 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003489 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003490 if ((q - buf) < sizeof(buf) - 1)
3491 *q++ = *pch;
3492 pch++;
3493 }
blueswir1cd390082008-11-16 13:53:32 +00003494 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003495 pch++;
3496 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003497 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003498 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003499 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003500 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003501 }
3502 break;
3503 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003504 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003505 n = 0;
3506 break;
3507 default:
blueswir17743e582007-09-24 18:39:04 +00003508#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003509 n = strtoull(pch, &p, 0);
3510#else
bellard9307c4c2004-04-04 12:57:25 +00003511 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003512#endif
bellard9307c4c2004-04-04 12:57:25 +00003513 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003514 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003515 }
3516 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003517 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003518 pch++;
3519 break;
3520 }
3521 return n;
3522}
3523
3524
aliguori376253e2009-03-05 23:01:23 +00003525static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003526{
blueswir1c2efc952007-09-25 17:28:42 +00003527 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003528 int op;
ths3b46e622007-09-17 08:09:54 +00003529
aliguori376253e2009-03-05 23:01:23 +00003530 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003531 for(;;) {
3532 op = *pch;
3533 if (op != '*' && op != '/' && op != '%')
3534 break;
3535 next();
aliguori376253e2009-03-05 23:01:23 +00003536 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003537 switch(op) {
3538 default:
3539 case '*':
3540 val *= val2;
3541 break;
3542 case '/':
3543 case '%':
ths5fafdf22007-09-16 21:08:06 +00003544 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003545 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003546 if (op == '/')
3547 val /= val2;
3548 else
3549 val %= val2;
3550 break;
3551 }
3552 }
3553 return val;
3554}
3555
aliguori376253e2009-03-05 23:01:23 +00003556static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003557{
blueswir1c2efc952007-09-25 17:28:42 +00003558 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003559 int op;
bellard9307c4c2004-04-04 12:57:25 +00003560
aliguori376253e2009-03-05 23:01:23 +00003561 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003562 for(;;) {
3563 op = *pch;
3564 if (op != '&' && op != '|' && op != '^')
3565 break;
3566 next();
aliguori376253e2009-03-05 23:01:23 +00003567 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003568 switch(op) {
3569 default:
3570 case '&':
3571 val &= val2;
3572 break;
3573 case '|':
3574 val |= val2;
3575 break;
3576 case '^':
3577 val ^= val2;
3578 break;
3579 }
3580 }
3581 return val;
3582}
3583
aliguori376253e2009-03-05 23:01:23 +00003584static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003585{
blueswir1c2efc952007-09-25 17:28:42 +00003586 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003587 int op;
bellard9307c4c2004-04-04 12:57:25 +00003588
aliguori376253e2009-03-05 23:01:23 +00003589 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003590 for(;;) {
3591 op = *pch;
3592 if (op != '+' && op != '-')
3593 break;
3594 next();
aliguori376253e2009-03-05 23:01:23 +00003595 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003596 if (op == '+')
3597 val += val2;
3598 else
3599 val -= val2;
3600 }
3601 return val;
3602}
3603
aliguori376253e2009-03-05 23:01:23 +00003604static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003605{
3606 pch = *pp;
3607 if (setjmp(expr_env)) {
3608 *pp = pch;
3609 return -1;
3610 }
blueswir1cd390082008-11-16 13:53:32 +00003611 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003612 pch++;
aliguori376253e2009-03-05 23:01:23 +00003613 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003614 *pp = pch;
3615 return 0;
3616}
3617
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003618static int get_double(Monitor *mon, double *pval, const char **pp)
3619{
3620 const char *p = *pp;
3621 char *tailp;
3622 double d;
3623
3624 d = strtod(p, &tailp);
3625 if (tailp == p) {
3626 monitor_printf(mon, "Number expected\n");
3627 return -1;
3628 }
3629 if (d != d || d - d != 0) {
3630 /* NaN or infinity */
3631 monitor_printf(mon, "Bad number\n");
3632 return -1;
3633 }
3634 *pval = d;
3635 *pp = tailp;
3636 return 0;
3637}
3638
bellard9307c4c2004-04-04 12:57:25 +00003639static int get_str(char *buf, int buf_size, const char **pp)
3640{
3641 const char *p;
3642 char *q;
3643 int c;
3644
bellard81d09122004-07-14 17:21:37 +00003645 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003646 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003647 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003648 p++;
3649 if (*p == '\0') {
3650 fail:
bellard81d09122004-07-14 17:21:37 +00003651 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003652 *pp = p;
3653 return -1;
3654 }
bellard9307c4c2004-04-04 12:57:25 +00003655 if (*p == '\"') {
3656 p++;
3657 while (*p != '\0' && *p != '\"') {
3658 if (*p == '\\') {
3659 p++;
3660 c = *p++;
3661 switch(c) {
3662 case 'n':
3663 c = '\n';
3664 break;
3665 case 'r':
3666 c = '\r';
3667 break;
3668 case '\\':
3669 case '\'':
3670 case '\"':
3671 break;
3672 default:
3673 qemu_printf("unsupported escape code: '\\%c'\n", c);
3674 goto fail;
3675 }
3676 if ((q - buf) < buf_size - 1) {
3677 *q++ = c;
3678 }
3679 } else {
3680 if ((q - buf) < buf_size - 1) {
3681 *q++ = *p;
3682 }
3683 p++;
3684 }
3685 }
3686 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003687 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003688 goto fail;
3689 }
3690 p++;
3691 } else {
blueswir1cd390082008-11-16 13:53:32 +00003692 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003693 if ((q - buf) < buf_size - 1) {
3694 *q++ = *p;
3695 }
3696 p++;
3697 }
bellard9307c4c2004-04-04 12:57:25 +00003698 }
bellard81d09122004-07-14 17:21:37 +00003699 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003700 *pp = p;
3701 return 0;
3702}
3703
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003704/*
3705 * Store the command-name in cmdname, and return a pointer to
3706 * the remaining of the command string.
3707 */
3708static const char *get_command_name(const char *cmdline,
3709 char *cmdname, size_t nlen)
3710{
3711 size_t len;
3712 const char *p, *pstart;
3713
3714 p = cmdline;
3715 while (qemu_isspace(*p))
3716 p++;
3717 if (*p == '\0')
3718 return NULL;
3719 pstart = p;
3720 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3721 p++;
3722 len = p - pstart;
3723 if (len > nlen - 1)
3724 len = nlen - 1;
3725 memcpy(cmdname, pstart, len);
3726 cmdname[len] = '\0';
3727 return p;
3728}
3729
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003730/**
3731 * Read key of 'type' into 'key' and return the current
3732 * 'type' pointer.
3733 */
3734static char *key_get_info(const char *type, char **key)
3735{
3736 size_t len;
3737 char *p, *str;
3738
3739 if (*type == ',')
3740 type++;
3741
3742 p = strchr(type, ':');
3743 if (!p) {
3744 *key = NULL;
3745 return NULL;
3746 }
3747 len = p - type;
3748
Anthony Liguori7267c092011-08-20 22:09:37 -05003749 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003750 memcpy(str, type, len);
3751 str[len] = '\0';
3752
3753 *key = str;
3754 return ++p;
3755}
3756
bellard9307c4c2004-04-04 12:57:25 +00003757static int default_fmt_format = 'x';
3758static int default_fmt_size = 4;
3759
3760#define MAX_ARGS 16
3761
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003762static int is_valid_option(const char *c, const char *typestr)
3763{
3764 char option[3];
3765
3766 option[0] = '-';
3767 option[1] = *c;
3768 option[2] = '\0';
3769
3770 typestr = strstr(typestr, option);
3771 return (typestr != NULL);
3772}
3773
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003774static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3775 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003776{
3777 const mon_cmd_t *cmd;
3778
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003779 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003780 if (compare_cmd(cmdname, cmd->name)) {
3781 return cmd;
3782 }
3783 }
3784
3785 return NULL;
3786}
3787
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003788static const mon_cmd_t *monitor_find_command(const char *cmdname)
3789{
3790 return search_dispatch_table(mon_cmds, cmdname);
3791}
3792
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003793static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3794{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003795 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003796}
3797
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003798static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3799{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003800 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003801}
3802
Anthony Liguoric227f092009-10-01 16:12:16 -05003803static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003804 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003805 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003806{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003807 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003808 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003809 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003810 char cmdname[256];
3811 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003812 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003813
3814#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003815 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003816#endif
ths3b46e622007-09-17 08:09:54 +00003817
bellard9307c4c2004-04-04 12:57:25 +00003818 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003819 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3820 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003821 return NULL;
ths3b46e622007-09-17 08:09:54 +00003822
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003823 cmd = monitor_find_command(cmdname);
3824 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003825 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003826 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003827 }
bellard9307c4c2004-04-04 12:57:25 +00003828
bellard9307c4c2004-04-04 12:57:25 +00003829 /* parse the parameters */
3830 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003831 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003832 typestr = key_get_info(typestr, &key);
3833 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003834 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003835 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003836 typestr++;
3837 switch(c) {
3838 case 'F':
bellard81d09122004-07-14 17:21:37 +00003839 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003840 case 's':
3841 {
3842 int ret;
ths3b46e622007-09-17 08:09:54 +00003843
blueswir1cd390082008-11-16 13:53:32 +00003844 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003845 p++;
3846 if (*typestr == '?') {
3847 typestr++;
3848 if (*p == '\0') {
3849 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003850 break;
bellard9307c4c2004-04-04 12:57:25 +00003851 }
3852 }
3853 ret = get_str(buf, sizeof(buf), &p);
3854 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003855 switch(c) {
3856 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003857 monitor_printf(mon, "%s: filename expected\n",
3858 cmdname);
bellard81d09122004-07-14 17:21:37 +00003859 break;
3860 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003861 monitor_printf(mon, "%s: block device name expected\n",
3862 cmdname);
bellard81d09122004-07-14 17:21:37 +00003863 break;
3864 default:
aliguori376253e2009-03-05 23:01:23 +00003865 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003866 break;
3867 }
bellard9307c4c2004-04-04 12:57:25 +00003868 goto fail;
3869 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003870 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003871 }
3872 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003873 case 'O':
3874 {
3875 QemuOptsList *opts_list;
3876 QemuOpts *opts;
3877
3878 opts_list = qemu_find_opts(key);
3879 if (!opts_list || opts_list->desc->name) {
3880 goto bad_type;
3881 }
3882 while (qemu_isspace(*p)) {
3883 p++;
3884 }
3885 if (!*p)
3886 break;
3887 if (get_str(buf, sizeof(buf), &p) < 0) {
3888 goto fail;
3889 }
3890 opts = qemu_opts_parse(opts_list, buf, 1);
3891 if (!opts) {
3892 goto fail;
3893 }
3894 qemu_opts_to_qdict(opts, qdict);
3895 qemu_opts_del(opts);
3896 }
3897 break;
bellard9307c4c2004-04-04 12:57:25 +00003898 case '/':
3899 {
3900 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003901
blueswir1cd390082008-11-16 13:53:32 +00003902 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003903 p++;
3904 if (*p == '/') {
3905 /* format found */
3906 p++;
3907 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003908 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003909 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003910 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003911 count = count * 10 + (*p - '0');
3912 p++;
3913 }
3914 }
3915 size = -1;
3916 format = -1;
3917 for(;;) {
3918 switch(*p) {
3919 case 'o':
3920 case 'd':
3921 case 'u':
3922 case 'x':
3923 case 'i':
3924 case 'c':
3925 format = *p++;
3926 break;
3927 case 'b':
3928 size = 1;
3929 p++;
3930 break;
3931 case 'h':
3932 size = 2;
3933 p++;
3934 break;
3935 case 'w':
3936 size = 4;
3937 p++;
3938 break;
3939 case 'g':
3940 case 'L':
3941 size = 8;
3942 p++;
3943 break;
3944 default:
3945 goto next;
3946 }
3947 }
3948 next:
blueswir1cd390082008-11-16 13:53:32 +00003949 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003950 monitor_printf(mon, "invalid char in format: '%c'\n",
3951 *p);
bellard9307c4c2004-04-04 12:57:25 +00003952 goto fail;
3953 }
bellard9307c4c2004-04-04 12:57:25 +00003954 if (format < 0)
3955 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003956 if (format != 'i') {
3957 /* for 'i', not specifying a size gives -1 as size */
3958 if (size < 0)
3959 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003960 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003961 }
bellard9307c4c2004-04-04 12:57:25 +00003962 default_fmt_format = format;
3963 } else {
3964 count = 1;
3965 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003966 if (format != 'i') {
3967 size = default_fmt_size;
3968 } else {
3969 size = -1;
3970 }
bellard9307c4c2004-04-04 12:57:25 +00003971 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003972 qdict_put(qdict, "count", qint_from_int(count));
3973 qdict_put(qdict, "format", qint_from_int(format));
3974 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003975 }
3976 break;
3977 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003978 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003979 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003980 {
blueswir1c2efc952007-09-25 17:28:42 +00003981 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003982
blueswir1cd390082008-11-16 13:53:32 +00003983 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003984 p++;
bellard34405572004-06-08 00:55:58 +00003985 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003986 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003987 if (*p == '\0') {
3988 typestr++;
3989 break;
3990 }
bellard34405572004-06-08 00:55:58 +00003991 } else {
3992 if (*p == '.') {
3993 p++;
blueswir1cd390082008-11-16 13:53:32 +00003994 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003995 p++;
bellard34405572004-06-08 00:55:58 +00003996 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003997 typestr++;
3998 break;
bellard34405572004-06-08 00:55:58 +00003999 }
4000 }
bellard13224a82006-07-14 22:03:35 +00004001 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004002 }
aliguori376253e2009-03-05 23:01:23 +00004003 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004004 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004005 /* Check if 'i' is greater than 32-bit */
4006 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4007 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4008 monitor_printf(mon, "integer is for 32-bit values\n");
4009 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004010 } else if (c == 'M') {
4011 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004012 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004013 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004014 }
4015 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004016 case 'o':
4017 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004018 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004019 char *end;
4020
4021 while (qemu_isspace(*p)) {
4022 p++;
4023 }
4024 if (*typestr == '?') {
4025 typestr++;
4026 if (*p == '\0') {
4027 break;
4028 }
4029 }
4030 val = strtosz(p, &end);
4031 if (val < 0) {
4032 monitor_printf(mon, "invalid size\n");
4033 goto fail;
4034 }
4035 qdict_put(qdict, key, qint_from_int(val));
4036 p = end;
4037 }
4038 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004039 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004040 {
4041 double val;
4042
4043 while (qemu_isspace(*p))
4044 p++;
4045 if (*typestr == '?') {
4046 typestr++;
4047 if (*p == '\0') {
4048 break;
4049 }
4050 }
4051 if (get_double(mon, &val, &p) < 0) {
4052 goto fail;
4053 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004054 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004055 switch (*p) {
4056 case 'm':
4057 val /= 1e3; p += 2; break;
4058 case 'u':
4059 val /= 1e6; p += 2; break;
4060 case 'n':
4061 val /= 1e9; p += 2; break;
4062 }
4063 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004064 if (*p && !qemu_isspace(*p)) {
4065 monitor_printf(mon, "Unknown unit suffix\n");
4066 goto fail;
4067 }
4068 qdict_put(qdict, key, qfloat_from_double(val));
4069 }
4070 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004071 case 'b':
4072 {
4073 const char *beg;
4074 int val;
4075
4076 while (qemu_isspace(*p)) {
4077 p++;
4078 }
4079 beg = p;
4080 while (qemu_isgraph(*p)) {
4081 p++;
4082 }
4083 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4084 val = 1;
4085 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4086 val = 0;
4087 } else {
4088 monitor_printf(mon, "Expected 'on' or 'off'\n");
4089 goto fail;
4090 }
4091 qdict_put(qdict, key, qbool_from_int(val));
4092 }
4093 break;
bellard9307c4c2004-04-04 12:57:25 +00004094 case '-':
4095 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004096 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004097 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004098 /* option */
ths3b46e622007-09-17 08:09:54 +00004099
bellard9307c4c2004-04-04 12:57:25 +00004100 c = *typestr++;
4101 if (c == '\0')
4102 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004103 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004104 p++;
bellard9307c4c2004-04-04 12:57:25 +00004105 if (*p == '-') {
4106 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004107 if(c != *p) {
4108 if(!is_valid_option(p, typestr)) {
4109
4110 monitor_printf(mon, "%s: unsupported option -%c\n",
4111 cmdname, *p);
4112 goto fail;
4113 } else {
4114 skip_key = 1;
4115 }
bellard9307c4c2004-04-04 12:57:25 +00004116 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004117 if(skip_key) {
4118 p = tmp;
4119 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004120 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004121 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004122 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004123 }
bellard9307c4c2004-04-04 12:57:25 +00004124 }
bellard9307c4c2004-04-04 12:57:25 +00004125 }
4126 break;
4127 default:
4128 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004129 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004130 goto fail;
4131 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004132 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004133 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004134 }
4135 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004136 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004137 p++;
4138 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004139 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4140 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004141 goto fail;
4142 }
4143
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004144 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004145
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004146fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004147 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004148 return NULL;
4149}
4150
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004151void monitor_set_error(Monitor *mon, QError *qerror)
4152{
4153 /* report only the first error */
4154 if (!mon->error) {
4155 mon->error = qerror;
4156 } else {
4157 MON_DEBUG("Additional error report at %s:%d\n",
4158 qerror->file, qerror->linenr);
4159 QDECREF(qerror);
4160 }
4161}
4162
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004163static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4164{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004165 if (ret && !monitor_has_error(mon)) {
4166 /*
4167 * If it returns failure, it must have passed on error.
4168 *
4169 * Action: Report an internal error to the client if in QMP.
4170 */
4171 qerror_report(QERR_UNDEFINED_ERROR);
4172 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4173 cmd->name);
4174 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004175
4176#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004177 if (!ret && monitor_has_error(mon)) {
4178 /*
4179 * If it returns success, it must not have passed an error.
4180 *
4181 * Action: Report the passed error to the client.
4182 */
4183 MON_DEBUG("command '%s' returned success but passed an error\n",
4184 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004185 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004186
4187 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4188 /*
4189 * Handlers should not call Monitor print functions.
4190 *
4191 * Action: Ignore them in QMP.
4192 *
4193 * (XXX: we don't check any 'info' or 'query' command here
4194 * because the user print function _is_ called by do_info(), hence
4195 * we will trigger this check. This problem will go away when we
4196 * make 'query' commands real and kill do_info())
4197 */
4198 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4199 cmd->name, mon_print_count_get(mon));
4200 }
4201#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004202}
4203
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004204static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004205{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004206 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004207 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004208
4209 qdict = qdict_new();
4210
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004211 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004212 if (!cmd)
4213 goto out;
4214
Luiz Capitulino4903de02010-09-16 11:01:32 -03004215 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004216 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004217 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004218 QObject *data = NULL;
4219
4220 /* XXX: ignores the error code */
4221 cmd->mhandler.cmd_new(mon, qdict, &data);
4222 assert(!monitor_has_error(mon));
4223 if (data) {
4224 cmd->user_print(mon, data);
4225 qobject_decref(data);
4226 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004227 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004228 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004229 }
4230
Luiz Capitulino13917be2009-10-07 13:41:54 -03004231out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004232 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004233}
4234
bellard81d09122004-07-14 17:21:37 +00004235static void cmd_completion(const char *name, const char *list)
4236{
4237 const char *p, *pstart;
4238 char cmd[128];
4239 int len;
4240
4241 p = list;
4242 for(;;) {
4243 pstart = p;
4244 p = strchr(p, '|');
4245 if (!p)
4246 p = pstart + strlen(pstart);
4247 len = p - pstart;
4248 if (len > sizeof(cmd) - 2)
4249 len = sizeof(cmd) - 2;
4250 memcpy(cmd, pstart, len);
4251 cmd[len] = '\0';
4252 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004253 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004254 }
4255 if (*p == '\0')
4256 break;
4257 p++;
4258 }
4259}
4260
4261static void file_completion(const char *input)
4262{
4263 DIR *ffs;
4264 struct dirent *d;
4265 char path[1024];
4266 char file[1024], file_prefix[1024];
4267 int input_path_len;
4268 const char *p;
4269
ths5fafdf22007-09-16 21:08:06 +00004270 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004271 if (!p) {
4272 input_path_len = 0;
4273 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004274 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004275 } else {
4276 input_path_len = p - input + 1;
4277 memcpy(path, input, input_path_len);
4278 if (input_path_len > sizeof(path) - 1)
4279 input_path_len = sizeof(path) - 1;
4280 path[input_path_len] = '\0';
4281 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4282 }
4283#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004284 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4285 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004286#endif
4287 ffs = opendir(path);
4288 if (!ffs)
4289 return;
4290 for(;;) {
4291 struct stat sb;
4292 d = readdir(ffs);
4293 if (!d)
4294 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004295
4296 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4297 continue;
4298 }
4299
bellard81d09122004-07-14 17:21:37 +00004300 if (strstart(d->d_name, file_prefix, NULL)) {
4301 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004302 if (input_path_len < sizeof(file))
4303 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4304 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004305 /* stat the file to find out if it's a directory.
4306 * In that case add a slash to speed up typing long paths
4307 */
4308 stat(file, &sb);
4309 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004310 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004311 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004312 }
4313 }
4314 closedir(ffs);
4315}
4316
aliguori51de9762009-03-05 23:00:43 +00004317static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004318{
aliguori51de9762009-03-05 23:00:43 +00004319 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004320 const char *input = opaque;
4321
4322 if (input[0] == '\0' ||
4323 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004324 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004325 }
4326}
4327
4328/* NOTE: this parser is an approximate form of the real command parser */
4329static void parse_cmdline(const char *cmdline,
4330 int *pnb_args, char **args)
4331{
4332 const char *p;
4333 int nb_args, ret;
4334 char buf[1024];
4335
4336 p = cmdline;
4337 nb_args = 0;
4338 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004339 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004340 p++;
4341 if (*p == '\0')
4342 break;
4343 if (nb_args >= MAX_ARGS)
4344 break;
4345 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004346 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004347 nb_args++;
4348 if (ret < 0)
4349 break;
4350 }
4351 *pnb_args = nb_args;
4352}
4353
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004354static const char *next_arg_type(const char *typestr)
4355{
4356 const char *p = strchr(typestr, ':');
4357 return (p != NULL ? ++p : typestr);
4358}
4359
aliguori4c36ba32009-03-05 23:01:37 +00004360static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004361{
4362 const char *cmdname;
4363 char *args[MAX_ARGS];
4364 int nb_args, i, len;
4365 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004366 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004367 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004368
4369 parse_cmdline(cmdline, &nb_args, args);
4370#ifdef DEBUG_COMPLETION
4371 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004372 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004373 }
4374#endif
4375
4376 /* if the line ends with a space, it means we want to complete the
4377 next arg */
4378 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004379 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004380 if (nb_args >= MAX_ARGS) {
4381 goto cleanup;
4382 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004383 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004384 }
4385 if (nb_args <= 1) {
4386 /* command completion */
4387 if (nb_args == 0)
4388 cmdname = "";
4389 else
4390 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004391 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004392 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004393 cmd_completion(cmdname, cmd->name);
4394 }
4395 } else {
4396 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004397 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4398 if (compare_cmd(args[0], cmd->name)) {
4399 break;
4400 }
bellard81d09122004-07-14 17:21:37 +00004401 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004402 if (!cmd->name) {
4403 goto cleanup;
4404 }
4405
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004406 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004407 for(i = 0; i < nb_args - 2; i++) {
4408 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004409 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004410 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004411 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004412 }
4413 }
4414 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004415 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004416 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004417 }
bellard81d09122004-07-14 17:21:37 +00004418 switch(*ptype) {
4419 case 'F':
4420 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004421 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004422 file_completion(str);
4423 break;
4424 case 'B':
4425 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004426 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004427 bdrv_iterate(block_completion_it, (void *)str);
4428 break;
bellard7fe48482004-10-09 18:08:01 +00004429 case 's':
4430 /* XXX: more generic ? */
4431 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004432 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004433 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4434 cmd_completion(str, cmd->name);
4435 }
bellard64866c32006-05-07 18:03:31 +00004436 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004437 char *sep = strrchr(str, '-');
4438 if (sep)
4439 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004440 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004441 for(key = key_defs; key->name != NULL; key++) {
4442 cmd_completion(str, key->name);
4443 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004444 } else if (!strcmp(cmd->name, "help|?")) {
4445 readline_set_completion_index(cur_mon->rs, strlen(str));
4446 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4447 cmd_completion(str, cmd->name);
4448 }
bellard7fe48482004-10-09 18:08:01 +00004449 }
4450 break;
bellard81d09122004-07-14 17:21:37 +00004451 default:
4452 break;
4453 }
4454 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004455
4456cleanup:
4457 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004458 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004459 }
bellard81d09122004-07-14 17:21:37 +00004460}
4461
aliguori731b0362009-03-05 23:01:42 +00004462static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004463{
aliguori731b0362009-03-05 23:01:42 +00004464 Monitor *mon = opaque;
4465
Jan Kiszkac62313b2009-12-04 14:05:29 +01004466 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004467}
4468
Luiz Capitulino09069b12010-02-04 18:10:06 -02004469static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4470{
4471 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4472 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4473}
4474
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004475/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004476 * Argument validation rules:
4477 *
4478 * 1. The argument must exist in cmd_args qdict
4479 * 2. The argument type must be the expected one
4480 *
4481 * Special case: If the argument doesn't exist in cmd_args and
4482 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4483 * checking is skipped for it.
4484 */
4485static int check_client_args_type(const QDict *client_args,
4486 const QDict *cmd_args, int flags)
4487{
4488 const QDictEntry *ent;
4489
4490 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4491 QObject *obj;
4492 QString *arg_type;
4493 const QObject *client_arg = qdict_entry_value(ent);
4494 const char *client_arg_name = qdict_entry_key(ent);
4495
4496 obj = qdict_get(cmd_args, client_arg_name);
4497 if (!obj) {
4498 if (flags & QMP_ACCEPT_UNKNOWNS) {
4499 /* handler accepts unknowns */
4500 continue;
4501 }
4502 /* client arg doesn't exist */
4503 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4504 return -1;
4505 }
4506
4507 arg_type = qobject_to_qstring(obj);
4508 assert(arg_type != NULL);
4509
4510 /* check if argument's type is correct */
4511 switch (qstring_get_str(arg_type)[0]) {
4512 case 'F':
4513 case 'B':
4514 case 's':
4515 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4516 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4517 "string");
4518 return -1;
4519 }
4520 break;
4521 case 'i':
4522 case 'l':
4523 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004524 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004525 if (qobject_type(client_arg) != QTYPE_QINT) {
4526 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4527 "int");
4528 return -1;
4529 }
4530 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004531 case 'T':
4532 if (qobject_type(client_arg) != QTYPE_QINT &&
4533 qobject_type(client_arg) != QTYPE_QFLOAT) {
4534 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4535 "number");
4536 return -1;
4537 }
4538 break;
4539 case 'b':
4540 case '-':
4541 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4542 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4543 "bool");
4544 return -1;
4545 }
4546 break;
4547 case 'O':
4548 assert(flags & QMP_ACCEPT_UNKNOWNS);
4549 break;
4550 case '/':
4551 case '.':
4552 /*
4553 * These types are not supported by QMP and thus are not
4554 * handled here. Fall through.
4555 */
4556 default:
4557 abort();
4558 }
4559 }
4560
4561 return 0;
4562}
4563
4564/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004565 * - Check if the client has passed all mandatory args
4566 * - Set special flags for argument validation
4567 */
4568static int check_mandatory_args(const QDict *cmd_args,
4569 const QDict *client_args, int *flags)
4570{
4571 const QDictEntry *ent;
4572
4573 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4574 const char *cmd_arg_name = qdict_entry_key(ent);
4575 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4576 assert(type != NULL);
4577
4578 if (qstring_get_str(type)[0] == 'O') {
4579 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4580 *flags |= QMP_ACCEPT_UNKNOWNS;
4581 } else if (qstring_get_str(type)[0] != '-' &&
4582 qstring_get_str(type)[1] != '?' &&
4583 !qdict_haskey(client_args, cmd_arg_name)) {
4584 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4585 return -1;
4586 }
4587 }
4588
4589 return 0;
4590}
4591
4592static QDict *qdict_from_args_type(const char *args_type)
4593{
4594 int i;
4595 QDict *qdict;
4596 QString *key, *type, *cur_qs;
4597
4598 assert(args_type != NULL);
4599
4600 qdict = qdict_new();
4601
4602 if (args_type == NULL || args_type[0] == '\0') {
4603 /* no args, empty qdict */
4604 goto out;
4605 }
4606
4607 key = qstring_new();
4608 type = qstring_new();
4609
4610 cur_qs = key;
4611
4612 for (i = 0;; i++) {
4613 switch (args_type[i]) {
4614 case ',':
4615 case '\0':
4616 qdict_put(qdict, qstring_get_str(key), type);
4617 QDECREF(key);
4618 if (args_type[i] == '\0') {
4619 goto out;
4620 }
4621 type = qstring_new(); /* qdict has ref */
4622 cur_qs = key = qstring_new();
4623 break;
4624 case ':':
4625 cur_qs = type;
4626 break;
4627 default:
4628 qstring_append_chr(cur_qs, args_type[i]);
4629 break;
4630 }
4631 }
4632
4633out:
4634 return qdict;
4635}
4636
4637/*
4638 * Client argument checking rules:
4639 *
4640 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004641 * 2. Each argument provided by the client must be expected
4642 * 3. Each argument provided by the client must have the type expected
4643 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004644 */
4645static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4646{
4647 int flags, err;
4648 QDict *cmd_args;
4649
4650 cmd_args = qdict_from_args_type(cmd->args_type);
4651
4652 flags = 0;
4653 err = check_mandatory_args(cmd_args, client_args, &flags);
4654 if (err) {
4655 goto out;
4656 }
4657
Luiz Capitulino4af91932010-06-22 11:44:05 -03004658 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004659
4660out:
4661 QDECREF(cmd_args);
4662 return err;
4663}
4664
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004665/*
4666 * Input object checking rules
4667 *
4668 * 1. Input object must be a dict
4669 * 2. The "execute" key must exist
4670 * 3. The "execute" key must be a string
4671 * 4. If the "arguments" key exists, it must be a dict
4672 * 5. If the "id" key exists, it can be anything (ie. json-value)
4673 * 6. Any argument not listed above is considered invalid
4674 */
4675static QDict *qmp_check_input_obj(QObject *input_obj)
4676{
4677 const QDictEntry *ent;
4678 int has_exec_key = 0;
4679 QDict *input_dict;
4680
4681 if (qobject_type(input_obj) != QTYPE_QDICT) {
4682 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4683 return NULL;
4684 }
4685
4686 input_dict = qobject_to_qdict(input_obj);
4687
4688 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4689 const char *arg_name = qdict_entry_key(ent);
4690 const QObject *arg_obj = qdict_entry_value(ent);
4691
4692 if (!strcmp(arg_name, "execute")) {
4693 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4694 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4695 "string");
4696 return NULL;
4697 }
4698 has_exec_key = 1;
4699 } else if (!strcmp(arg_name, "arguments")) {
4700 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4701 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4702 "object");
4703 return NULL;
4704 }
4705 } else if (!strcmp(arg_name, "id")) {
4706 /* FIXME: check duplicated IDs for async commands */
4707 } else {
4708 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4709 return NULL;
4710 }
4711 }
4712
4713 if (!has_exec_key) {
4714 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4715 return NULL;
4716 }
4717
4718 return input_dict;
4719}
4720
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004721static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4722{
4723 QObject *ret_data = NULL;
4724
Luiz Capitulino4903de02010-09-16 11:01:32 -03004725 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004726 qmp_async_info_handler(mon, cmd);
4727 if (monitor_has_error(mon)) {
4728 monitor_protocol_emitter(mon, NULL);
4729 }
4730 } else {
4731 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004732 monitor_protocol_emitter(mon, ret_data);
4733 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004734 }
4735}
4736
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004737static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4738 const QDict *params)
4739{
4740 int ret;
4741 QObject *data = NULL;
4742
4743 mon_print_count_init(mon);
4744
4745 ret = cmd->mhandler.cmd_new(mon, params, &data);
4746 handler_audit(mon, cmd, ret);
4747 monitor_protocol_emitter(mon, data);
4748 qobject_decref(data);
4749}
4750
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004751static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4752{
4753 int err;
4754 QObject *obj;
4755 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004756 const mon_cmd_t *cmd;
4757 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004758 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004759
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004760 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004761 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004762
4763 obj = json_parser_parse(tokens, NULL);
4764 if (!obj) {
4765 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004766 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004767 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004768 }
4769
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004770 input = qmp_check_input_obj(obj);
4771 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004772 qobject_decref(obj);
4773 goto err_out;
4774 }
4775
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004776 mon->mc->id = qdict_get(input, "id");
4777 qobject_incref(mon->mc->id);
4778
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004779 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004780 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004781 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004782 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004783 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004784 }
4785
Anthony Liguorie3193602011-09-02 12:34:47 -05004786 cmd = qmp_find_cmd(cmd_name);
4787 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004788 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004789 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004790 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004791 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004792 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004793 }
4794
4795 obj = qdict_get(input, "arguments");
4796 if (!obj) {
4797 args = qdict_new();
4798 } else {
4799 args = qobject_to_qdict(obj);
4800 QINCREF(args);
4801 }
4802
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004803 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004804 if (err < 0) {
4805 goto err_out;
4806 }
4807
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004808 if (query_cmd) {
4809 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004810 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004811 err = qmp_async_cmd_handler(mon, cmd, args);
4812 if (err) {
4813 /* emit the error response */
4814 goto err_out;
4815 }
Adam Litke940cc302010-01-25 12:18:44 -06004816 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004817 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004818 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004819
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004820 goto out;
4821
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004822err_out:
4823 monitor_protocol_emitter(mon, NULL);
4824out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004825 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004826 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004827}
4828
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004829/**
4830 * monitor_control_read(): Read and handle QMP input
4831 */
4832static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4833{
4834 Monitor *old_mon = cur_mon;
4835
4836 cur_mon = opaque;
4837
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004838 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004839
4840 cur_mon = old_mon;
4841}
4842
aliguori731b0362009-03-05 23:01:42 +00004843static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004844{
aliguori731b0362009-03-05 23:01:42 +00004845 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004846 int i;
aliguori376253e2009-03-05 23:01:23 +00004847
aliguori731b0362009-03-05 23:01:42 +00004848 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004849
aliguoricde76ee2009-03-05 23:01:51 +00004850 if (cur_mon->rs) {
4851 for (i = 0; i < size; i++)
4852 readline_handle_byte(cur_mon->rs, buf[i]);
4853 } else {
4854 if (size == 0 || buf[size - 1] != 0)
4855 monitor_printf(cur_mon, "corrupted command\n");
4856 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004857 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004858 }
aliguori731b0362009-03-05 23:01:42 +00004859
4860 cur_mon = old_mon;
4861}
aliguorid8f44602008-10-06 13:52:44 +00004862
aliguori376253e2009-03-05 23:01:23 +00004863static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004864{
aliguori731b0362009-03-05 23:01:42 +00004865 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004866 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004867 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004868}
4869
aliguoricde76ee2009-03-05 23:01:51 +00004870int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004871{
aliguoricde76ee2009-03-05 23:01:51 +00004872 if (!mon->rs)
4873 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004874 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004875 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004876}
4877
aliguori376253e2009-03-05 23:01:23 +00004878void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004879{
aliguoricde76ee2009-03-05 23:01:51 +00004880 if (!mon->rs)
4881 return;
aliguori731b0362009-03-05 23:01:42 +00004882 if (--mon->suspend_cnt == 0)
4883 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004884}
4885
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004886static QObject *get_qmp_greeting(void)
4887{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004888 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004889
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004890 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004891 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4892}
4893
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004894/**
4895 * monitor_control_event(): Print QMP gretting
4896 */
4897static void monitor_control_event(void *opaque, int event)
4898{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004899 QObject *data;
4900 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004901
Luiz Capitulino47116d12010-02-08 17:01:30 -02004902 switch (event) {
4903 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004904 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004905 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004906 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004907 monitor_json_emitter(mon, data);
4908 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004909 break;
4910 case CHR_EVENT_CLOSED:
4911 json_message_parser_destroy(&mon->mc->parser);
4912 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004913 }
4914}
4915
aliguori731b0362009-03-05 23:01:42 +00004916static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004917{
aliguori376253e2009-03-05 23:01:23 +00004918 Monitor *mon = opaque;
4919
aliguori2724b182009-03-05 23:01:47 +00004920 switch (event) {
4921 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004922 mon->mux_out = 0;
4923 if (mon->reset_seen) {
4924 readline_restart(mon->rs);
4925 monitor_resume(mon);
4926 monitor_flush(mon);
4927 } else {
4928 mon->suspend_cnt = 0;
4929 }
aliguori2724b182009-03-05 23:01:47 +00004930 break;
ths86e94de2007-01-05 22:01:59 +00004931
aliguori2724b182009-03-05 23:01:47 +00004932 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004933 if (mon->reset_seen) {
4934 if (mon->suspend_cnt == 0) {
4935 monitor_printf(mon, "\n");
4936 }
4937 monitor_flush(mon);
4938 monitor_suspend(mon);
4939 } else {
4940 mon->suspend_cnt++;
4941 }
4942 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004943 break;
4944
Amit Shahb6b8df52009-10-07 18:31:16 +05304945 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004946 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4947 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004948 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004949 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004950 }
4951 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004952 break;
4953 }
ths86e94de2007-01-05 22:01:59 +00004954}
4955
aliguori76655d62009-03-06 20:27:37 +00004956
4957/*
4958 * Local variables:
4959 * c-indent-level: 4
4960 * c-basic-offset: 4
4961 * tab-width: 8
4962 * End:
4963 */
4964
aliguori731b0362009-03-05 23:01:42 +00004965void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004966{
aliguori731b0362009-03-05 23:01:42 +00004967 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004968 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004969
4970 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004971 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004972 is_first_init = 0;
4973 }
aliguori87127162009-03-05 23:01:29 +00004974
Anthony Liguori7267c092011-08-20 22:09:37 -05004975 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004976
aliguori87127162009-03-05 23:01:29 +00004977 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004978 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004979 if (flags & MONITOR_USE_READLINE) {
4980 mon->rs = readline_init(mon, monitor_find_completion);
4981 monitor_read_command(mon, 0);
4982 }
aliguori87127162009-03-05 23:01:29 +00004983
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004984 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004985 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004986 /* Control mode requires special handlers */
4987 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4988 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004989 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004990 } else {
4991 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4992 monitor_event, mon);
4993 }
aliguori87127162009-03-05 23:01:29 +00004994
Blue Swirl72cf2d42009-09-12 07:36:22 +00004995 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004996 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4997 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004998}
4999
aliguori376253e2009-03-05 23:01:23 +00005000static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005001{
aliguoribb5fc202009-03-05 23:01:15 +00005002 BlockDriverState *bs = opaque;
5003 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005004
aliguoribb5fc202009-03-05 23:01:15 +00005005 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005006 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005007 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005008 }
aliguori731b0362009-03-05 23:01:42 +00005009 if (mon->password_completion_cb)
5010 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005011
aliguori731b0362009-03-05 23:01:42 +00005012 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005013}
aliguoric0f4ce72009-03-05 23:01:01 +00005014
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005015int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5016 BlockDriverCompletionFunc *completion_cb,
5017 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005018{
aliguoricde76ee2009-03-05 23:01:51 +00005019 int err;
5020
aliguoribb5fc202009-03-05 23:01:15 +00005021 if (!bdrv_key_required(bs)) {
5022 if (completion_cb)
5023 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005024 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005025 }
aliguoric0f4ce72009-03-05 23:01:01 +00005026
Luiz Capitulino94171e12009-12-07 21:37:00 +01005027 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005028 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005029 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005030 }
5031
aliguori376253e2009-03-05 23:01:23 +00005032 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5033 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005034
aliguori731b0362009-03-05 23:01:42 +00005035 mon->password_completion_cb = completion_cb;
5036 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005037
aliguoricde76ee2009-03-05 23:01:51 +00005038 err = monitor_read_password(mon, bdrv_password_cb, bs);
5039
5040 if (err && completion_cb)
5041 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005042
5043 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005044}