blob: 2f49c74793493db8c456d637e22c512283197a71 [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 Capitulinod15e5462009-12-10 17:16:06 -02002677 .user_print = bdrv_info_print,
2678 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 },
2680 {
2681 .name = "blockstats",
2682 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 .params = "",
2684 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002685 .user_print = bdrv_stats_print,
2686 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 },
2688 {
2689 .name = "registers",
2690 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 .params = "",
2692 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002693 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 },
2695 {
2696 .name = "cpus",
2697 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 .params = "",
2699 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002700 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 },
2702 {
2703 .name = "history",
2704 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 .params = "",
2706 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002707 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002709#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2710 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 {
2712 .name = "irq",
2713 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 .params = "",
2715 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002716#ifdef TARGET_SPARC
2717 .mhandler.info = sun4m_irq_info,
2718#elif defined(TARGET_LM32)
2719 .mhandler.info = lm32_irq_info,
2720#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002721 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002722#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 },
2724 {
2725 .name = "pic",
2726 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 .params = "",
2728 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002729#ifdef TARGET_SPARC
2730 .mhandler.info = sun4m_pic_info,
2731#elif defined(TARGET_LM32)
2732 .mhandler.info = lm32_do_pic_info,
2733#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002734 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002735#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002737#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 {
2739 .name = "pci",
2740 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 .params = "",
2742 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002743 .user_print = do_pci_info_print,
2744 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002746#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2747 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 {
2749 .name = "tlb",
2750 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 .params = "",
2752 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002753 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 },
aurel327c664e22009-03-03 06:12:22 +00002755#endif
2756#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 {
2758 .name = "mem",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002762 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
bellardb86bda52004-09-18 19:32:46 +00002764#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 {
Blue Swirl314e2982011-09-11 20:22:05 +00002766 .name = "mtree",
2767 .args_type = "",
2768 .params = "",
2769 .help = "show memory tree",
2770 .mhandler.info = do_info_mtree,
2771 },
2772 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 .name = "jit",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002777 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
2779 {
2780 .name = "kvm",
2781 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002782 .params = "",
2783 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002784 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 },
2786 {
2787 .name = "numa",
2788 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .params = "",
2790 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002791 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 },
2793 {
2794 .name = "usb",
2795 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002796 .params = "",
2797 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002798 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 },
2800 {
2801 .name = "usbhost",
2802 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 .params = "",
2804 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002805 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 },
2807 {
2808 .name = "profile",
2809 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002810 .params = "",
2811 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002812 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 },
2814 {
2815 .name = "capture",
2816 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002817 .params = "",
2818 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002819 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 },
2821 {
2822 .name = "snapshots",
2823 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002824 .params = "",
2825 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002826 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 },
2828 {
2829 .name = "status",
2830 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002831 .params = "",
2832 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002833 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 },
2835 {
2836 .name = "pcmcia",
2837 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 .params = "",
2839 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002840 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 },
2842 {
2843 .name = "mice",
2844 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 .params = "",
2846 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002847 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 },
2849 {
2850 .name = "vnc",
2851 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002852 .params = "",
2853 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002854 .user_print = do_info_vnc_print,
2855 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002856 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002857#if defined(CONFIG_SPICE)
2858 {
2859 .name = "spice",
2860 .args_type = "",
2861 .params = "",
2862 .help = "show the spice server status",
2863 .user_print = do_info_spice_print,
2864 .mhandler.info_new = do_info_spice,
2865 },
2866#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002867 {
2868 .name = "name",
2869 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002870 .params = "",
2871 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002872 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002873 },
2874 {
2875 .name = "uuid",
2876 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002877 .params = "",
2878 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002879 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 },
j_mayer76a66252007-03-07 08:32:30 +00002881#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002882 {
2883 .name = "cpustats",
2884 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 .params = "",
2886 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002887 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 },
j_mayer76a66252007-03-07 08:32:30 +00002889#endif
blueswir131a60e22007-10-26 18:42:59 +00002890#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 {
2892 .name = "usernet",
2893 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002894 .params = "",
2895 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002896 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002897 },
blueswir131a60e22007-10-26 18:42:59 +00002898#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 {
2900 .name = "migrate",
2901 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002902 .params = "",
2903 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002904 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 },
2906 {
2907 .name = "balloon",
2908 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002909 .params = "",
2910 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002911 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002912 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002913 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002914 },
2915 {
2916 .name = "qtree",
2917 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002918 .params = "",
2919 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002920 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002921 },
2922 {
2923 .name = "qdm",
2924 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002925 .params = "",
2926 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002927 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002928 },
2929 {
2930 .name = "roms",
2931 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002932 .params = "",
2933 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002934 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002935 },
Lluís6d8a7642011-08-31 20:30:43 +02002936#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05302937 {
2938 .name = "trace",
2939 .args_type = "",
2940 .params = "",
2941 .help = "show current contents of trace buffer",
2942 .mhandler.info = do_info_trace,
2943 },
Lluís31965ae2011-08-31 20:31:24 +02002944#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05302945 {
2946 .name = "trace-events",
2947 .args_type = "",
2948 .params = "",
2949 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002950 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302951 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002952 {
2953 .name = NULL,
2954 },
bellard9dc39cb2004-03-14 21:38:27 +00002955};
2956
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002957static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002958#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002959 { /* NULL */ },
2960};
2961
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002962static const mon_cmd_t qmp_query_cmds[] = {
2963 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002964 .name = "block",
2965 .args_type = "",
2966 .params = "",
2967 .help = "show the block devices",
2968 .user_print = bdrv_info_print,
2969 .mhandler.info_new = bdrv_info,
2970 },
2971 {
2972 .name = "blockstats",
2973 .args_type = "",
2974 .params = "",
2975 .help = "show block device statistics",
2976 .user_print = bdrv_stats_print,
2977 .mhandler.info_new = bdrv_info_stats,
2978 },
2979 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002980 .name = "pci",
2981 .args_type = "",
2982 .params = "",
2983 .help = "show PCI info",
2984 .user_print = do_pci_info_print,
2985 .mhandler.info_new = do_pci_info,
2986 },
2987 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002988 .name = "vnc",
2989 .args_type = "",
2990 .params = "",
2991 .help = "show the vnc server status",
2992 .user_print = do_info_vnc_print,
2993 .mhandler.info_new = do_info_vnc,
2994 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002995#if defined(CONFIG_SPICE)
2996 {
2997 .name = "spice",
2998 .args_type = "",
2999 .params = "",
3000 .help = "show the spice server status",
3001 .user_print = do_info_spice_print,
3002 .mhandler.info_new = do_info_spice,
3003 },
3004#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003005 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003006 .name = "balloon",
3007 .args_type = "",
3008 .params = "",
3009 .help = "show balloon information",
3010 .user_print = monitor_print_balloon,
3011 .mhandler.info_async = do_info_balloon,
3012 .flags = MONITOR_CMD_ASYNC,
3013 },
3014 { /* NULL */ },
3015};
3016
bellard9307c4c2004-04-04 12:57:25 +00003017/*******************************************************************/
3018
3019static const char *pch;
3020static jmp_buf expr_env;
3021
bellard92a31b12005-02-10 22:00:52 +00003022#define MD_TLONG 0
3023#define MD_I32 1
3024
bellard9307c4c2004-04-04 12:57:25 +00003025typedef struct MonitorDef {
3026 const char *name;
3027 int offset;
blueswir18662d652008-10-02 18:32:44 +00003028 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003029 int type;
bellard9307c4c2004-04-04 12:57:25 +00003030} MonitorDef;
3031
bellard57206fd2004-04-25 18:54:52 +00003032#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003033static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003034{
bellard6a00d602005-11-21 23:25:50 +00003035 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003036 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003037}
3038#endif
3039
bellarda541f292004-04-12 20:39:29 +00003040#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003041static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003042{
bellard6a00d602005-11-21 23:25:50 +00003043 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003044 unsigned int u;
3045 int i;
3046
3047 u = 0;
3048 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003049 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003050
3051 return u;
3052}
3053
blueswir18662d652008-10-02 18:32:44 +00003054static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003055{
bellard6a00d602005-11-21 23:25:50 +00003056 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003057 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003058}
3059
blueswir18662d652008-10-02 18:32:44 +00003060static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003061{
bellard6a00d602005-11-21 23:25:50 +00003062 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003063 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003064}
bellard9fddaa02004-05-21 12:59:32 +00003065
blueswir18662d652008-10-02 18:32:44 +00003066static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003067{
bellard6a00d602005-11-21 23:25:50 +00003068 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003069 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003070}
3071
blueswir18662d652008-10-02 18:32:44 +00003072static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003073{
bellard6a00d602005-11-21 23:25:50 +00003074 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003075 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003076}
3077
blueswir18662d652008-10-02 18:32:44 +00003078static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003079{
bellard6a00d602005-11-21 23:25:50 +00003080 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003081 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003082}
bellarda541f292004-04-12 20:39:29 +00003083#endif
3084
bellarde95c8d52004-09-30 22:22:08 +00003085#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003086#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003087static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003088{
bellard6a00d602005-11-21 23:25:50 +00003089 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003090
3091 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003092}
bellard7b936c02005-10-30 17:05:13 +00003093#endif
bellarde95c8d52004-09-30 22:22:08 +00003094
blueswir18662d652008-10-02 18:32:44 +00003095static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003096{
bellard6a00d602005-11-21 23:25:50 +00003097 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003098 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003099}
3100#endif
3101
blueswir18662d652008-10-02 18:32:44 +00003102static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003103#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003104
3105#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003106 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003107 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003108 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003109
bellard9307c4c2004-04-04 12:57:25 +00003110 { "eax", offsetof(CPUState, regs[0]) },
3111 { "ecx", offsetof(CPUState, regs[1]) },
3112 { "edx", offsetof(CPUState, regs[2]) },
3113 { "ebx", offsetof(CPUState, regs[3]) },
3114 { "esp|sp", offsetof(CPUState, regs[4]) },
3115 { "ebp|fp", offsetof(CPUState, regs[5]) },
3116 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003117 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003118#ifdef TARGET_X86_64
3119 { "r8", offsetof(CPUState, regs[8]) },
3120 { "r9", offsetof(CPUState, regs[9]) },
3121 { "r10", offsetof(CPUState, regs[10]) },
3122 { "r11", offsetof(CPUState, regs[11]) },
3123 { "r12", offsetof(CPUState, regs[12]) },
3124 { "r13", offsetof(CPUState, regs[13]) },
3125 { "r14", offsetof(CPUState, regs[14]) },
3126 { "r15", offsetof(CPUState, regs[15]) },
3127#endif
bellard9307c4c2004-04-04 12:57:25 +00003128 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003129 { "eip", offsetof(CPUState, eip) },
3130 SEG("cs", R_CS)
3131 SEG("ds", R_DS)
3132 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003133 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003134 SEG("fs", R_FS)
3135 SEG("gs", R_GS)
3136 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003137#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003138 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003139 { "r0", offsetof(CPUState, gpr[0]) },
3140 { "r1", offsetof(CPUState, gpr[1]) },
3141 { "r2", offsetof(CPUState, gpr[2]) },
3142 { "r3", offsetof(CPUState, gpr[3]) },
3143 { "r4", offsetof(CPUState, gpr[4]) },
3144 { "r5", offsetof(CPUState, gpr[5]) },
3145 { "r6", offsetof(CPUState, gpr[6]) },
3146 { "r7", offsetof(CPUState, gpr[7]) },
3147 { "r8", offsetof(CPUState, gpr[8]) },
3148 { "r9", offsetof(CPUState, gpr[9]) },
3149 { "r10", offsetof(CPUState, gpr[10]) },
3150 { "r11", offsetof(CPUState, gpr[11]) },
3151 { "r12", offsetof(CPUState, gpr[12]) },
3152 { "r13", offsetof(CPUState, gpr[13]) },
3153 { "r14", offsetof(CPUState, gpr[14]) },
3154 { "r15", offsetof(CPUState, gpr[15]) },
3155 { "r16", offsetof(CPUState, gpr[16]) },
3156 { "r17", offsetof(CPUState, gpr[17]) },
3157 { "r18", offsetof(CPUState, gpr[18]) },
3158 { "r19", offsetof(CPUState, gpr[19]) },
3159 { "r20", offsetof(CPUState, gpr[20]) },
3160 { "r21", offsetof(CPUState, gpr[21]) },
3161 { "r22", offsetof(CPUState, gpr[22]) },
3162 { "r23", offsetof(CPUState, gpr[23]) },
3163 { "r24", offsetof(CPUState, gpr[24]) },
3164 { "r25", offsetof(CPUState, gpr[25]) },
3165 { "r26", offsetof(CPUState, gpr[26]) },
3166 { "r27", offsetof(CPUState, gpr[27]) },
3167 { "r28", offsetof(CPUState, gpr[28]) },
3168 { "r29", offsetof(CPUState, gpr[29]) },
3169 { "r30", offsetof(CPUState, gpr[30]) },
3170 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003171 /* Floating point registers */
3172 { "f0", offsetof(CPUState, fpr[0]) },
3173 { "f1", offsetof(CPUState, fpr[1]) },
3174 { "f2", offsetof(CPUState, fpr[2]) },
3175 { "f3", offsetof(CPUState, fpr[3]) },
3176 { "f4", offsetof(CPUState, fpr[4]) },
3177 { "f5", offsetof(CPUState, fpr[5]) },
3178 { "f6", offsetof(CPUState, fpr[6]) },
3179 { "f7", offsetof(CPUState, fpr[7]) },
3180 { "f8", offsetof(CPUState, fpr[8]) },
3181 { "f9", offsetof(CPUState, fpr[9]) },
3182 { "f10", offsetof(CPUState, fpr[10]) },
3183 { "f11", offsetof(CPUState, fpr[11]) },
3184 { "f12", offsetof(CPUState, fpr[12]) },
3185 { "f13", offsetof(CPUState, fpr[13]) },
3186 { "f14", offsetof(CPUState, fpr[14]) },
3187 { "f15", offsetof(CPUState, fpr[15]) },
3188 { "f16", offsetof(CPUState, fpr[16]) },
3189 { "f17", offsetof(CPUState, fpr[17]) },
3190 { "f18", offsetof(CPUState, fpr[18]) },
3191 { "f19", offsetof(CPUState, fpr[19]) },
3192 { "f20", offsetof(CPUState, fpr[20]) },
3193 { "f21", offsetof(CPUState, fpr[21]) },
3194 { "f22", offsetof(CPUState, fpr[22]) },
3195 { "f23", offsetof(CPUState, fpr[23]) },
3196 { "f24", offsetof(CPUState, fpr[24]) },
3197 { "f25", offsetof(CPUState, fpr[25]) },
3198 { "f26", offsetof(CPUState, fpr[26]) },
3199 { "f27", offsetof(CPUState, fpr[27]) },
3200 { "f28", offsetof(CPUState, fpr[28]) },
3201 { "f29", offsetof(CPUState, fpr[29]) },
3202 { "f30", offsetof(CPUState, fpr[30]) },
3203 { "f31", offsetof(CPUState, fpr[31]) },
3204 { "fpscr", offsetof(CPUState, fpscr) },
3205 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003206 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003207 { "lr", offsetof(CPUState, lr) },
3208 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003209 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003210 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003211 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003212 { "msr", 0, &monitor_get_msr, },
3213 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003214 { "tbu", 0, &monitor_get_tbu, },
3215 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003216#if defined(TARGET_PPC64)
3217 /* Address space register */
3218 { "asr", offsetof(CPUState, asr) },
3219#endif
3220 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003221 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003222 { "sr0", offsetof(CPUState, sr[0]) },
3223 { "sr1", offsetof(CPUState, sr[1]) },
3224 { "sr2", offsetof(CPUState, sr[2]) },
3225 { "sr3", offsetof(CPUState, sr[3]) },
3226 { "sr4", offsetof(CPUState, sr[4]) },
3227 { "sr5", offsetof(CPUState, sr[5]) },
3228 { "sr6", offsetof(CPUState, sr[6]) },
3229 { "sr7", offsetof(CPUState, sr[7]) },
3230 { "sr8", offsetof(CPUState, sr[8]) },
3231 { "sr9", offsetof(CPUState, sr[9]) },
3232 { "sr10", offsetof(CPUState, sr[10]) },
3233 { "sr11", offsetof(CPUState, sr[11]) },
3234 { "sr12", offsetof(CPUState, sr[12]) },
3235 { "sr13", offsetof(CPUState, sr[13]) },
3236 { "sr14", offsetof(CPUState, sr[14]) },
3237 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003238 /* Too lazy to put BATs... */
3239 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3240
3241 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3242 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3243 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3244 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3245 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3246 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3247 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3248 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3249 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3250 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3251 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3252 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3253 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3254 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3255 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3256 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3257 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3258 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3259 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3260 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3261 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3262 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3263 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3264 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3265 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3266 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3267 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3268 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3269 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3270 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3271 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3272 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3273 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3274 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3275 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3276 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3277 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3278 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3279 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3280 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3281 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3282 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3283 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3284 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3285 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3286 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3287 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3288 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3289 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3290 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3291 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3292 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3293 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3294 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3295 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3296 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3297 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3298 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3299 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3300 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3301 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3302 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3303 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3304 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3305 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3306 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3307
bellarde95c8d52004-09-30 22:22:08 +00003308#elif defined(TARGET_SPARC)
3309 { "g0", offsetof(CPUState, gregs[0]) },
3310 { "g1", offsetof(CPUState, gregs[1]) },
3311 { "g2", offsetof(CPUState, gregs[2]) },
3312 { "g3", offsetof(CPUState, gregs[3]) },
3313 { "g4", offsetof(CPUState, gregs[4]) },
3314 { "g5", offsetof(CPUState, gregs[5]) },
3315 { "g6", offsetof(CPUState, gregs[6]) },
3316 { "g7", offsetof(CPUState, gregs[7]) },
3317 { "o0", 0, monitor_get_reg },
3318 { "o1", 1, monitor_get_reg },
3319 { "o2", 2, monitor_get_reg },
3320 { "o3", 3, monitor_get_reg },
3321 { "o4", 4, monitor_get_reg },
3322 { "o5", 5, monitor_get_reg },
3323 { "o6", 6, monitor_get_reg },
3324 { "o7", 7, monitor_get_reg },
3325 { "l0", 8, monitor_get_reg },
3326 { "l1", 9, monitor_get_reg },
3327 { "l2", 10, monitor_get_reg },
3328 { "l3", 11, monitor_get_reg },
3329 { "l4", 12, monitor_get_reg },
3330 { "l5", 13, monitor_get_reg },
3331 { "l6", 14, monitor_get_reg },
3332 { "l7", 15, monitor_get_reg },
3333 { "i0", 16, monitor_get_reg },
3334 { "i1", 17, monitor_get_reg },
3335 { "i2", 18, monitor_get_reg },
3336 { "i3", 19, monitor_get_reg },
3337 { "i4", 20, monitor_get_reg },
3338 { "i5", 21, monitor_get_reg },
3339 { "i6", 22, monitor_get_reg },
3340 { "i7", 23, monitor_get_reg },
3341 { "pc", offsetof(CPUState, pc) },
3342 { "npc", offsetof(CPUState, npc) },
3343 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003344#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003345 { "psr", 0, &monitor_get_psr, },
3346 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003347#endif
bellarde95c8d52004-09-30 22:22:08 +00003348 { "tbr", offsetof(CPUState, tbr) },
3349 { "fsr", offsetof(CPUState, fsr) },
3350 { "f0", offsetof(CPUState, fpr[0]) },
3351 { "f1", offsetof(CPUState, fpr[1]) },
3352 { "f2", offsetof(CPUState, fpr[2]) },
3353 { "f3", offsetof(CPUState, fpr[3]) },
3354 { "f4", offsetof(CPUState, fpr[4]) },
3355 { "f5", offsetof(CPUState, fpr[5]) },
3356 { "f6", offsetof(CPUState, fpr[6]) },
3357 { "f7", offsetof(CPUState, fpr[7]) },
3358 { "f8", offsetof(CPUState, fpr[8]) },
3359 { "f9", offsetof(CPUState, fpr[9]) },
3360 { "f10", offsetof(CPUState, fpr[10]) },
3361 { "f11", offsetof(CPUState, fpr[11]) },
3362 { "f12", offsetof(CPUState, fpr[12]) },
3363 { "f13", offsetof(CPUState, fpr[13]) },
3364 { "f14", offsetof(CPUState, fpr[14]) },
3365 { "f15", offsetof(CPUState, fpr[15]) },
3366 { "f16", offsetof(CPUState, fpr[16]) },
3367 { "f17", offsetof(CPUState, fpr[17]) },
3368 { "f18", offsetof(CPUState, fpr[18]) },
3369 { "f19", offsetof(CPUState, fpr[19]) },
3370 { "f20", offsetof(CPUState, fpr[20]) },
3371 { "f21", offsetof(CPUState, fpr[21]) },
3372 { "f22", offsetof(CPUState, fpr[22]) },
3373 { "f23", offsetof(CPUState, fpr[23]) },
3374 { "f24", offsetof(CPUState, fpr[24]) },
3375 { "f25", offsetof(CPUState, fpr[25]) },
3376 { "f26", offsetof(CPUState, fpr[26]) },
3377 { "f27", offsetof(CPUState, fpr[27]) },
3378 { "f28", offsetof(CPUState, fpr[28]) },
3379 { "f29", offsetof(CPUState, fpr[29]) },
3380 { "f30", offsetof(CPUState, fpr[30]) },
3381 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003382#ifdef TARGET_SPARC64
3383 { "f32", offsetof(CPUState, fpr[32]) },
3384 { "f34", offsetof(CPUState, fpr[34]) },
3385 { "f36", offsetof(CPUState, fpr[36]) },
3386 { "f38", offsetof(CPUState, fpr[38]) },
3387 { "f40", offsetof(CPUState, fpr[40]) },
3388 { "f42", offsetof(CPUState, fpr[42]) },
3389 { "f44", offsetof(CPUState, fpr[44]) },
3390 { "f46", offsetof(CPUState, fpr[46]) },
3391 { "f48", offsetof(CPUState, fpr[48]) },
3392 { "f50", offsetof(CPUState, fpr[50]) },
3393 { "f52", offsetof(CPUState, fpr[52]) },
3394 { "f54", offsetof(CPUState, fpr[54]) },
3395 { "f56", offsetof(CPUState, fpr[56]) },
3396 { "f58", offsetof(CPUState, fpr[58]) },
3397 { "f60", offsetof(CPUState, fpr[60]) },
3398 { "f62", offsetof(CPUState, fpr[62]) },
3399 { "asi", offsetof(CPUState, asi) },
3400 { "pstate", offsetof(CPUState, pstate) },
3401 { "cansave", offsetof(CPUState, cansave) },
3402 { "canrestore", offsetof(CPUState, canrestore) },
3403 { "otherwin", offsetof(CPUState, otherwin) },
3404 { "wstate", offsetof(CPUState, wstate) },
3405 { "cleanwin", offsetof(CPUState, cleanwin) },
3406 { "fprs", offsetof(CPUState, fprs) },
3407#endif
bellard9307c4c2004-04-04 12:57:25 +00003408#endif
3409 { NULL },
3410};
3411
aliguori376253e2009-03-05 23:01:23 +00003412static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003413{
aliguori376253e2009-03-05 23:01:23 +00003414 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003415 longjmp(expr_env, 1);
3416}
3417
Markus Armbruster09b94182010-01-20 13:07:30 +01003418/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003419static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003420{
blueswir18662d652008-10-02 18:32:44 +00003421 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003422 void *ptr;
3423
bellard9307c4c2004-04-04 12:57:25 +00003424 for(md = monitor_defs; md->name != NULL; md++) {
3425 if (compare_cmd(name, md->name)) {
3426 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003427 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003428 } else {
bellard6a00d602005-11-21 23:25:50 +00003429 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003430 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003431 switch(md->type) {
3432 case MD_I32:
3433 *pval = *(int32_t *)ptr;
3434 break;
3435 case MD_TLONG:
3436 *pval = *(target_long *)ptr;
3437 break;
3438 default:
3439 *pval = 0;
3440 break;
3441 }
bellard9307c4c2004-04-04 12:57:25 +00003442 }
3443 return 0;
3444 }
3445 }
3446 return -1;
3447}
3448
3449static void next(void)
3450{
Blue Swirl660f11b2009-07-31 21:16:51 +00003451 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003452 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003453 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003454 pch++;
3455 }
3456}
3457
aliguori376253e2009-03-05 23:01:23 +00003458static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003459
aliguori376253e2009-03-05 23:01:23 +00003460static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003461{
blueswir1c2efc952007-09-25 17:28:42 +00003462 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003463 char *p;
bellard6a00d602005-11-21 23:25:50 +00003464 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003465
3466 switch(*pch) {
3467 case '+':
3468 next();
aliguori376253e2009-03-05 23:01:23 +00003469 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003470 break;
3471 case '-':
3472 next();
aliguori376253e2009-03-05 23:01:23 +00003473 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003474 break;
3475 case '~':
3476 next();
aliguori376253e2009-03-05 23:01:23 +00003477 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003478 break;
3479 case '(':
3480 next();
aliguori376253e2009-03-05 23:01:23 +00003481 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003482 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003483 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003484 }
3485 next();
3486 break;
bellard81d09122004-07-14 17:21:37 +00003487 case '\'':
3488 pch++;
3489 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003490 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003491 n = *pch;
3492 pch++;
3493 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003494 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003495 next();
3496 break;
bellard9307c4c2004-04-04 12:57:25 +00003497 case '$':
3498 {
3499 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003500 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003501
bellard9307c4c2004-04-04 12:57:25 +00003502 pch++;
3503 q = buf;
3504 while ((*pch >= 'a' && *pch <= 'z') ||
3505 (*pch >= 'A' && *pch <= 'Z') ||
3506 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003507 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003508 if ((q - buf) < sizeof(buf) - 1)
3509 *q++ = *pch;
3510 pch++;
3511 }
blueswir1cd390082008-11-16 13:53:32 +00003512 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003513 pch++;
3514 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003515 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003516 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003517 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003518 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003519 }
3520 break;
3521 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003522 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003523 n = 0;
3524 break;
3525 default:
blueswir17743e582007-09-24 18:39:04 +00003526#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003527 n = strtoull(pch, &p, 0);
3528#else
bellard9307c4c2004-04-04 12:57:25 +00003529 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003530#endif
bellard9307c4c2004-04-04 12:57:25 +00003531 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003532 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003533 }
3534 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003535 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003536 pch++;
3537 break;
3538 }
3539 return n;
3540}
3541
3542
aliguori376253e2009-03-05 23:01:23 +00003543static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003544{
blueswir1c2efc952007-09-25 17:28:42 +00003545 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003546 int op;
ths3b46e622007-09-17 08:09:54 +00003547
aliguori376253e2009-03-05 23:01:23 +00003548 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003549 for(;;) {
3550 op = *pch;
3551 if (op != '*' && op != '/' && op != '%')
3552 break;
3553 next();
aliguori376253e2009-03-05 23:01:23 +00003554 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003555 switch(op) {
3556 default:
3557 case '*':
3558 val *= val2;
3559 break;
3560 case '/':
3561 case '%':
ths5fafdf22007-09-16 21:08:06 +00003562 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003563 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003564 if (op == '/')
3565 val /= val2;
3566 else
3567 val %= val2;
3568 break;
3569 }
3570 }
3571 return val;
3572}
3573
aliguori376253e2009-03-05 23:01:23 +00003574static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003575{
blueswir1c2efc952007-09-25 17:28:42 +00003576 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003577 int op;
bellard9307c4c2004-04-04 12:57:25 +00003578
aliguori376253e2009-03-05 23:01:23 +00003579 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003580 for(;;) {
3581 op = *pch;
3582 if (op != '&' && op != '|' && op != '^')
3583 break;
3584 next();
aliguori376253e2009-03-05 23:01:23 +00003585 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003586 switch(op) {
3587 default:
3588 case '&':
3589 val &= val2;
3590 break;
3591 case '|':
3592 val |= val2;
3593 break;
3594 case '^':
3595 val ^= val2;
3596 break;
3597 }
3598 }
3599 return val;
3600}
3601
aliguori376253e2009-03-05 23:01:23 +00003602static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003603{
blueswir1c2efc952007-09-25 17:28:42 +00003604 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003605 int op;
bellard9307c4c2004-04-04 12:57:25 +00003606
aliguori376253e2009-03-05 23:01:23 +00003607 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003608 for(;;) {
3609 op = *pch;
3610 if (op != '+' && op != '-')
3611 break;
3612 next();
aliguori376253e2009-03-05 23:01:23 +00003613 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003614 if (op == '+')
3615 val += val2;
3616 else
3617 val -= val2;
3618 }
3619 return val;
3620}
3621
aliguori376253e2009-03-05 23:01:23 +00003622static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003623{
3624 pch = *pp;
3625 if (setjmp(expr_env)) {
3626 *pp = pch;
3627 return -1;
3628 }
blueswir1cd390082008-11-16 13:53:32 +00003629 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003630 pch++;
aliguori376253e2009-03-05 23:01:23 +00003631 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003632 *pp = pch;
3633 return 0;
3634}
3635
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003636static int get_double(Monitor *mon, double *pval, const char **pp)
3637{
3638 const char *p = *pp;
3639 char *tailp;
3640 double d;
3641
3642 d = strtod(p, &tailp);
3643 if (tailp == p) {
3644 monitor_printf(mon, "Number expected\n");
3645 return -1;
3646 }
3647 if (d != d || d - d != 0) {
3648 /* NaN or infinity */
3649 monitor_printf(mon, "Bad number\n");
3650 return -1;
3651 }
3652 *pval = d;
3653 *pp = tailp;
3654 return 0;
3655}
3656
bellard9307c4c2004-04-04 12:57:25 +00003657static int get_str(char *buf, int buf_size, const char **pp)
3658{
3659 const char *p;
3660 char *q;
3661 int c;
3662
bellard81d09122004-07-14 17:21:37 +00003663 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003664 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003665 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003666 p++;
3667 if (*p == '\0') {
3668 fail:
bellard81d09122004-07-14 17:21:37 +00003669 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003670 *pp = p;
3671 return -1;
3672 }
bellard9307c4c2004-04-04 12:57:25 +00003673 if (*p == '\"') {
3674 p++;
3675 while (*p != '\0' && *p != '\"') {
3676 if (*p == '\\') {
3677 p++;
3678 c = *p++;
3679 switch(c) {
3680 case 'n':
3681 c = '\n';
3682 break;
3683 case 'r':
3684 c = '\r';
3685 break;
3686 case '\\':
3687 case '\'':
3688 case '\"':
3689 break;
3690 default:
3691 qemu_printf("unsupported escape code: '\\%c'\n", c);
3692 goto fail;
3693 }
3694 if ((q - buf) < buf_size - 1) {
3695 *q++ = c;
3696 }
3697 } else {
3698 if ((q - buf) < buf_size - 1) {
3699 *q++ = *p;
3700 }
3701 p++;
3702 }
3703 }
3704 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003705 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003706 goto fail;
3707 }
3708 p++;
3709 } else {
blueswir1cd390082008-11-16 13:53:32 +00003710 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003711 if ((q - buf) < buf_size - 1) {
3712 *q++ = *p;
3713 }
3714 p++;
3715 }
bellard9307c4c2004-04-04 12:57:25 +00003716 }
bellard81d09122004-07-14 17:21:37 +00003717 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003718 *pp = p;
3719 return 0;
3720}
3721
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003722/*
3723 * Store the command-name in cmdname, and return a pointer to
3724 * the remaining of the command string.
3725 */
3726static const char *get_command_name(const char *cmdline,
3727 char *cmdname, size_t nlen)
3728{
3729 size_t len;
3730 const char *p, *pstart;
3731
3732 p = cmdline;
3733 while (qemu_isspace(*p))
3734 p++;
3735 if (*p == '\0')
3736 return NULL;
3737 pstart = p;
3738 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3739 p++;
3740 len = p - pstart;
3741 if (len > nlen - 1)
3742 len = nlen - 1;
3743 memcpy(cmdname, pstart, len);
3744 cmdname[len] = '\0';
3745 return p;
3746}
3747
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003748/**
3749 * Read key of 'type' into 'key' and return the current
3750 * 'type' pointer.
3751 */
3752static char *key_get_info(const char *type, char **key)
3753{
3754 size_t len;
3755 char *p, *str;
3756
3757 if (*type == ',')
3758 type++;
3759
3760 p = strchr(type, ':');
3761 if (!p) {
3762 *key = NULL;
3763 return NULL;
3764 }
3765 len = p - type;
3766
Anthony Liguori7267c092011-08-20 22:09:37 -05003767 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003768 memcpy(str, type, len);
3769 str[len] = '\0';
3770
3771 *key = str;
3772 return ++p;
3773}
3774
bellard9307c4c2004-04-04 12:57:25 +00003775static int default_fmt_format = 'x';
3776static int default_fmt_size = 4;
3777
3778#define MAX_ARGS 16
3779
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003780static int is_valid_option(const char *c, const char *typestr)
3781{
3782 char option[3];
3783
3784 option[0] = '-';
3785 option[1] = *c;
3786 option[2] = '\0';
3787
3788 typestr = strstr(typestr, option);
3789 return (typestr != NULL);
3790}
3791
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003792static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3793 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003794{
3795 const mon_cmd_t *cmd;
3796
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003797 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003798 if (compare_cmd(cmdname, cmd->name)) {
3799 return cmd;
3800 }
3801 }
3802
3803 return NULL;
3804}
3805
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003806static const mon_cmd_t *monitor_find_command(const char *cmdname)
3807{
3808 return search_dispatch_table(mon_cmds, cmdname);
3809}
3810
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003811static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3812{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003813 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003814}
3815
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003816static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3817{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003818 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003819}
3820
Anthony Liguoric227f092009-10-01 16:12:16 -05003821static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003822 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003823 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003824{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003825 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003826 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003827 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003828 char cmdname[256];
3829 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003830 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003831
3832#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003833 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003834#endif
ths3b46e622007-09-17 08:09:54 +00003835
bellard9307c4c2004-04-04 12:57:25 +00003836 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003837 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3838 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003839 return NULL;
ths3b46e622007-09-17 08:09:54 +00003840
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003841 cmd = monitor_find_command(cmdname);
3842 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003843 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003844 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003845 }
bellard9307c4c2004-04-04 12:57:25 +00003846
bellard9307c4c2004-04-04 12:57:25 +00003847 /* parse the parameters */
3848 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003849 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003850 typestr = key_get_info(typestr, &key);
3851 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003852 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003853 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003854 typestr++;
3855 switch(c) {
3856 case 'F':
bellard81d09122004-07-14 17:21:37 +00003857 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003858 case 's':
3859 {
3860 int ret;
ths3b46e622007-09-17 08:09:54 +00003861
blueswir1cd390082008-11-16 13:53:32 +00003862 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003863 p++;
3864 if (*typestr == '?') {
3865 typestr++;
3866 if (*p == '\0') {
3867 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003868 break;
bellard9307c4c2004-04-04 12:57:25 +00003869 }
3870 }
3871 ret = get_str(buf, sizeof(buf), &p);
3872 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003873 switch(c) {
3874 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003875 monitor_printf(mon, "%s: filename expected\n",
3876 cmdname);
bellard81d09122004-07-14 17:21:37 +00003877 break;
3878 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003879 monitor_printf(mon, "%s: block device name expected\n",
3880 cmdname);
bellard81d09122004-07-14 17:21:37 +00003881 break;
3882 default:
aliguori376253e2009-03-05 23:01:23 +00003883 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003884 break;
3885 }
bellard9307c4c2004-04-04 12:57:25 +00003886 goto fail;
3887 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003888 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003889 }
3890 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003891 case 'O':
3892 {
3893 QemuOptsList *opts_list;
3894 QemuOpts *opts;
3895
3896 opts_list = qemu_find_opts(key);
3897 if (!opts_list || opts_list->desc->name) {
3898 goto bad_type;
3899 }
3900 while (qemu_isspace(*p)) {
3901 p++;
3902 }
3903 if (!*p)
3904 break;
3905 if (get_str(buf, sizeof(buf), &p) < 0) {
3906 goto fail;
3907 }
3908 opts = qemu_opts_parse(opts_list, buf, 1);
3909 if (!opts) {
3910 goto fail;
3911 }
3912 qemu_opts_to_qdict(opts, qdict);
3913 qemu_opts_del(opts);
3914 }
3915 break;
bellard9307c4c2004-04-04 12:57:25 +00003916 case '/':
3917 {
3918 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003919
blueswir1cd390082008-11-16 13:53:32 +00003920 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003921 p++;
3922 if (*p == '/') {
3923 /* format found */
3924 p++;
3925 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003926 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003927 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003928 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003929 count = count * 10 + (*p - '0');
3930 p++;
3931 }
3932 }
3933 size = -1;
3934 format = -1;
3935 for(;;) {
3936 switch(*p) {
3937 case 'o':
3938 case 'd':
3939 case 'u':
3940 case 'x':
3941 case 'i':
3942 case 'c':
3943 format = *p++;
3944 break;
3945 case 'b':
3946 size = 1;
3947 p++;
3948 break;
3949 case 'h':
3950 size = 2;
3951 p++;
3952 break;
3953 case 'w':
3954 size = 4;
3955 p++;
3956 break;
3957 case 'g':
3958 case 'L':
3959 size = 8;
3960 p++;
3961 break;
3962 default:
3963 goto next;
3964 }
3965 }
3966 next:
blueswir1cd390082008-11-16 13:53:32 +00003967 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003968 monitor_printf(mon, "invalid char in format: '%c'\n",
3969 *p);
bellard9307c4c2004-04-04 12:57:25 +00003970 goto fail;
3971 }
bellard9307c4c2004-04-04 12:57:25 +00003972 if (format < 0)
3973 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003974 if (format != 'i') {
3975 /* for 'i', not specifying a size gives -1 as size */
3976 if (size < 0)
3977 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003978 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003979 }
bellard9307c4c2004-04-04 12:57:25 +00003980 default_fmt_format = format;
3981 } else {
3982 count = 1;
3983 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003984 if (format != 'i') {
3985 size = default_fmt_size;
3986 } else {
3987 size = -1;
3988 }
bellard9307c4c2004-04-04 12:57:25 +00003989 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003990 qdict_put(qdict, "count", qint_from_int(count));
3991 qdict_put(qdict, "format", qint_from_int(format));
3992 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003993 }
3994 break;
3995 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003996 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003997 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003998 {
blueswir1c2efc952007-09-25 17:28:42 +00003999 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004000
blueswir1cd390082008-11-16 13:53:32 +00004001 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004002 p++;
bellard34405572004-06-08 00:55:58 +00004003 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004004 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004005 if (*p == '\0') {
4006 typestr++;
4007 break;
4008 }
bellard34405572004-06-08 00:55:58 +00004009 } else {
4010 if (*p == '.') {
4011 p++;
blueswir1cd390082008-11-16 13:53:32 +00004012 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004013 p++;
bellard34405572004-06-08 00:55:58 +00004014 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004015 typestr++;
4016 break;
bellard34405572004-06-08 00:55:58 +00004017 }
4018 }
bellard13224a82006-07-14 22:03:35 +00004019 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004020 }
aliguori376253e2009-03-05 23:01:23 +00004021 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004022 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004023 /* Check if 'i' is greater than 32-bit */
4024 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4025 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4026 monitor_printf(mon, "integer is for 32-bit values\n");
4027 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004028 } else if (c == 'M') {
4029 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004030 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004031 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004032 }
4033 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004034 case 'o':
4035 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004036 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004037 char *end;
4038
4039 while (qemu_isspace(*p)) {
4040 p++;
4041 }
4042 if (*typestr == '?') {
4043 typestr++;
4044 if (*p == '\0') {
4045 break;
4046 }
4047 }
4048 val = strtosz(p, &end);
4049 if (val < 0) {
4050 monitor_printf(mon, "invalid size\n");
4051 goto fail;
4052 }
4053 qdict_put(qdict, key, qint_from_int(val));
4054 p = end;
4055 }
4056 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004057 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004058 {
4059 double val;
4060
4061 while (qemu_isspace(*p))
4062 p++;
4063 if (*typestr == '?') {
4064 typestr++;
4065 if (*p == '\0') {
4066 break;
4067 }
4068 }
4069 if (get_double(mon, &val, &p) < 0) {
4070 goto fail;
4071 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004072 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004073 switch (*p) {
4074 case 'm':
4075 val /= 1e3; p += 2; break;
4076 case 'u':
4077 val /= 1e6; p += 2; break;
4078 case 'n':
4079 val /= 1e9; p += 2; break;
4080 }
4081 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004082 if (*p && !qemu_isspace(*p)) {
4083 monitor_printf(mon, "Unknown unit suffix\n");
4084 goto fail;
4085 }
4086 qdict_put(qdict, key, qfloat_from_double(val));
4087 }
4088 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004089 case 'b':
4090 {
4091 const char *beg;
4092 int val;
4093
4094 while (qemu_isspace(*p)) {
4095 p++;
4096 }
4097 beg = p;
4098 while (qemu_isgraph(*p)) {
4099 p++;
4100 }
4101 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4102 val = 1;
4103 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4104 val = 0;
4105 } else {
4106 monitor_printf(mon, "Expected 'on' or 'off'\n");
4107 goto fail;
4108 }
4109 qdict_put(qdict, key, qbool_from_int(val));
4110 }
4111 break;
bellard9307c4c2004-04-04 12:57:25 +00004112 case '-':
4113 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004114 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004115 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004116 /* option */
ths3b46e622007-09-17 08:09:54 +00004117
bellard9307c4c2004-04-04 12:57:25 +00004118 c = *typestr++;
4119 if (c == '\0')
4120 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004121 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004122 p++;
bellard9307c4c2004-04-04 12:57:25 +00004123 if (*p == '-') {
4124 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004125 if(c != *p) {
4126 if(!is_valid_option(p, typestr)) {
4127
4128 monitor_printf(mon, "%s: unsupported option -%c\n",
4129 cmdname, *p);
4130 goto fail;
4131 } else {
4132 skip_key = 1;
4133 }
bellard9307c4c2004-04-04 12:57:25 +00004134 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004135 if(skip_key) {
4136 p = tmp;
4137 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004138 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004139 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004140 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004141 }
bellard9307c4c2004-04-04 12:57:25 +00004142 }
bellard9307c4c2004-04-04 12:57:25 +00004143 }
4144 break;
4145 default:
4146 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004147 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004148 goto fail;
4149 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004150 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004151 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004152 }
4153 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004154 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004155 p++;
4156 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004157 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4158 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004159 goto fail;
4160 }
4161
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004162 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004163
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004164fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004165 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004166 return NULL;
4167}
4168
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004169void monitor_set_error(Monitor *mon, QError *qerror)
4170{
4171 /* report only the first error */
4172 if (!mon->error) {
4173 mon->error = qerror;
4174 } else {
4175 MON_DEBUG("Additional error report at %s:%d\n",
4176 qerror->file, qerror->linenr);
4177 QDECREF(qerror);
4178 }
4179}
4180
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004181static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4182{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004183 if (ret && !monitor_has_error(mon)) {
4184 /*
4185 * If it returns failure, it must have passed on error.
4186 *
4187 * Action: Report an internal error to the client if in QMP.
4188 */
4189 qerror_report(QERR_UNDEFINED_ERROR);
4190 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4191 cmd->name);
4192 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004193
4194#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004195 if (!ret && monitor_has_error(mon)) {
4196 /*
4197 * If it returns success, it must not have passed an error.
4198 *
4199 * Action: Report the passed error to the client.
4200 */
4201 MON_DEBUG("command '%s' returned success but passed an error\n",
4202 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004203 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004204
4205 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4206 /*
4207 * Handlers should not call Monitor print functions.
4208 *
4209 * Action: Ignore them in QMP.
4210 *
4211 * (XXX: we don't check any 'info' or 'query' command here
4212 * because the user print function _is_ called by do_info(), hence
4213 * we will trigger this check. This problem will go away when we
4214 * make 'query' commands real and kill do_info())
4215 */
4216 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4217 cmd->name, mon_print_count_get(mon));
4218 }
4219#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004220}
4221
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004222static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004223{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004224 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004225 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004226
4227 qdict = qdict_new();
4228
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004229 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004230 if (!cmd)
4231 goto out;
4232
Luiz Capitulino4903de02010-09-16 11:01:32 -03004233 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004234 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004235 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004236 QObject *data = NULL;
4237
4238 /* XXX: ignores the error code */
4239 cmd->mhandler.cmd_new(mon, qdict, &data);
4240 assert(!monitor_has_error(mon));
4241 if (data) {
4242 cmd->user_print(mon, data);
4243 qobject_decref(data);
4244 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004245 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004246 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004247 }
4248
Luiz Capitulino13917be2009-10-07 13:41:54 -03004249out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004250 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004251}
4252
bellard81d09122004-07-14 17:21:37 +00004253static void cmd_completion(const char *name, const char *list)
4254{
4255 const char *p, *pstart;
4256 char cmd[128];
4257 int len;
4258
4259 p = list;
4260 for(;;) {
4261 pstart = p;
4262 p = strchr(p, '|');
4263 if (!p)
4264 p = pstart + strlen(pstart);
4265 len = p - pstart;
4266 if (len > sizeof(cmd) - 2)
4267 len = sizeof(cmd) - 2;
4268 memcpy(cmd, pstart, len);
4269 cmd[len] = '\0';
4270 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004271 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004272 }
4273 if (*p == '\0')
4274 break;
4275 p++;
4276 }
4277}
4278
4279static void file_completion(const char *input)
4280{
4281 DIR *ffs;
4282 struct dirent *d;
4283 char path[1024];
4284 char file[1024], file_prefix[1024];
4285 int input_path_len;
4286 const char *p;
4287
ths5fafdf22007-09-16 21:08:06 +00004288 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004289 if (!p) {
4290 input_path_len = 0;
4291 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004292 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004293 } else {
4294 input_path_len = p - input + 1;
4295 memcpy(path, input, input_path_len);
4296 if (input_path_len > sizeof(path) - 1)
4297 input_path_len = sizeof(path) - 1;
4298 path[input_path_len] = '\0';
4299 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4300 }
4301#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004302 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4303 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004304#endif
4305 ffs = opendir(path);
4306 if (!ffs)
4307 return;
4308 for(;;) {
4309 struct stat sb;
4310 d = readdir(ffs);
4311 if (!d)
4312 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004313
4314 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4315 continue;
4316 }
4317
bellard81d09122004-07-14 17:21:37 +00004318 if (strstart(d->d_name, file_prefix, NULL)) {
4319 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004320 if (input_path_len < sizeof(file))
4321 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4322 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004323 /* stat the file to find out if it's a directory.
4324 * In that case add a slash to speed up typing long paths
4325 */
4326 stat(file, &sb);
4327 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004328 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004329 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004330 }
4331 }
4332 closedir(ffs);
4333}
4334
aliguori51de9762009-03-05 23:00:43 +00004335static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004336{
aliguori51de9762009-03-05 23:00:43 +00004337 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004338 const char *input = opaque;
4339
4340 if (input[0] == '\0' ||
4341 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004342 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004343 }
4344}
4345
4346/* NOTE: this parser is an approximate form of the real command parser */
4347static void parse_cmdline(const char *cmdline,
4348 int *pnb_args, char **args)
4349{
4350 const char *p;
4351 int nb_args, ret;
4352 char buf[1024];
4353
4354 p = cmdline;
4355 nb_args = 0;
4356 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004357 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004358 p++;
4359 if (*p == '\0')
4360 break;
4361 if (nb_args >= MAX_ARGS)
4362 break;
4363 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004364 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004365 nb_args++;
4366 if (ret < 0)
4367 break;
4368 }
4369 *pnb_args = nb_args;
4370}
4371
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004372static const char *next_arg_type(const char *typestr)
4373{
4374 const char *p = strchr(typestr, ':');
4375 return (p != NULL ? ++p : typestr);
4376}
4377
aliguori4c36ba32009-03-05 23:01:37 +00004378static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004379{
4380 const char *cmdname;
4381 char *args[MAX_ARGS];
4382 int nb_args, i, len;
4383 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004384 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004385 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004386
4387 parse_cmdline(cmdline, &nb_args, args);
4388#ifdef DEBUG_COMPLETION
4389 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004390 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004391 }
4392#endif
4393
4394 /* if the line ends with a space, it means we want to complete the
4395 next arg */
4396 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004397 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004398 if (nb_args >= MAX_ARGS) {
4399 goto cleanup;
4400 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004401 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004402 }
4403 if (nb_args <= 1) {
4404 /* command completion */
4405 if (nb_args == 0)
4406 cmdname = "";
4407 else
4408 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004409 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004410 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004411 cmd_completion(cmdname, cmd->name);
4412 }
4413 } else {
4414 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004415 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4416 if (compare_cmd(args[0], cmd->name)) {
4417 break;
4418 }
bellard81d09122004-07-14 17:21:37 +00004419 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004420 if (!cmd->name) {
4421 goto cleanup;
4422 }
4423
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004424 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004425 for(i = 0; i < nb_args - 2; i++) {
4426 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004427 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004428 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004429 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004430 }
4431 }
4432 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004433 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004434 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004435 }
bellard81d09122004-07-14 17:21:37 +00004436 switch(*ptype) {
4437 case 'F':
4438 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004439 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004440 file_completion(str);
4441 break;
4442 case 'B':
4443 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004444 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004445 bdrv_iterate(block_completion_it, (void *)str);
4446 break;
bellard7fe48482004-10-09 18:08:01 +00004447 case 's':
4448 /* XXX: more generic ? */
4449 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004450 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004451 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4452 cmd_completion(str, cmd->name);
4453 }
bellard64866c32006-05-07 18:03:31 +00004454 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004455 char *sep = strrchr(str, '-');
4456 if (sep)
4457 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004458 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004459 for(key = key_defs; key->name != NULL; key++) {
4460 cmd_completion(str, key->name);
4461 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004462 } else if (!strcmp(cmd->name, "help|?")) {
4463 readline_set_completion_index(cur_mon->rs, strlen(str));
4464 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4465 cmd_completion(str, cmd->name);
4466 }
bellard7fe48482004-10-09 18:08:01 +00004467 }
4468 break;
bellard81d09122004-07-14 17:21:37 +00004469 default:
4470 break;
4471 }
4472 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004473
4474cleanup:
4475 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004476 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004477 }
bellard81d09122004-07-14 17:21:37 +00004478}
4479
aliguori731b0362009-03-05 23:01:42 +00004480static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004481{
aliguori731b0362009-03-05 23:01:42 +00004482 Monitor *mon = opaque;
4483
Jan Kiszkac62313b2009-12-04 14:05:29 +01004484 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004485}
4486
Luiz Capitulino09069b12010-02-04 18:10:06 -02004487static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4488{
4489 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4490 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4491}
4492
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004493/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004494 * Argument validation rules:
4495 *
4496 * 1. The argument must exist in cmd_args qdict
4497 * 2. The argument type must be the expected one
4498 *
4499 * Special case: If the argument doesn't exist in cmd_args and
4500 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4501 * checking is skipped for it.
4502 */
4503static int check_client_args_type(const QDict *client_args,
4504 const QDict *cmd_args, int flags)
4505{
4506 const QDictEntry *ent;
4507
4508 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4509 QObject *obj;
4510 QString *arg_type;
4511 const QObject *client_arg = qdict_entry_value(ent);
4512 const char *client_arg_name = qdict_entry_key(ent);
4513
4514 obj = qdict_get(cmd_args, client_arg_name);
4515 if (!obj) {
4516 if (flags & QMP_ACCEPT_UNKNOWNS) {
4517 /* handler accepts unknowns */
4518 continue;
4519 }
4520 /* client arg doesn't exist */
4521 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4522 return -1;
4523 }
4524
4525 arg_type = qobject_to_qstring(obj);
4526 assert(arg_type != NULL);
4527
4528 /* check if argument's type is correct */
4529 switch (qstring_get_str(arg_type)[0]) {
4530 case 'F':
4531 case 'B':
4532 case 's':
4533 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4534 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4535 "string");
4536 return -1;
4537 }
4538 break;
4539 case 'i':
4540 case 'l':
4541 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004542 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004543 if (qobject_type(client_arg) != QTYPE_QINT) {
4544 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4545 "int");
4546 return -1;
4547 }
4548 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004549 case 'T':
4550 if (qobject_type(client_arg) != QTYPE_QINT &&
4551 qobject_type(client_arg) != QTYPE_QFLOAT) {
4552 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4553 "number");
4554 return -1;
4555 }
4556 break;
4557 case 'b':
4558 case '-':
4559 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4560 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4561 "bool");
4562 return -1;
4563 }
4564 break;
4565 case 'O':
4566 assert(flags & QMP_ACCEPT_UNKNOWNS);
4567 break;
4568 case '/':
4569 case '.':
4570 /*
4571 * These types are not supported by QMP and thus are not
4572 * handled here. Fall through.
4573 */
4574 default:
4575 abort();
4576 }
4577 }
4578
4579 return 0;
4580}
4581
4582/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004583 * - Check if the client has passed all mandatory args
4584 * - Set special flags for argument validation
4585 */
4586static int check_mandatory_args(const QDict *cmd_args,
4587 const QDict *client_args, int *flags)
4588{
4589 const QDictEntry *ent;
4590
4591 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4592 const char *cmd_arg_name = qdict_entry_key(ent);
4593 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4594 assert(type != NULL);
4595
4596 if (qstring_get_str(type)[0] == 'O') {
4597 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4598 *flags |= QMP_ACCEPT_UNKNOWNS;
4599 } else if (qstring_get_str(type)[0] != '-' &&
4600 qstring_get_str(type)[1] != '?' &&
4601 !qdict_haskey(client_args, cmd_arg_name)) {
4602 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4603 return -1;
4604 }
4605 }
4606
4607 return 0;
4608}
4609
4610static QDict *qdict_from_args_type(const char *args_type)
4611{
4612 int i;
4613 QDict *qdict;
4614 QString *key, *type, *cur_qs;
4615
4616 assert(args_type != NULL);
4617
4618 qdict = qdict_new();
4619
4620 if (args_type == NULL || args_type[0] == '\0') {
4621 /* no args, empty qdict */
4622 goto out;
4623 }
4624
4625 key = qstring_new();
4626 type = qstring_new();
4627
4628 cur_qs = key;
4629
4630 for (i = 0;; i++) {
4631 switch (args_type[i]) {
4632 case ',':
4633 case '\0':
4634 qdict_put(qdict, qstring_get_str(key), type);
4635 QDECREF(key);
4636 if (args_type[i] == '\0') {
4637 goto out;
4638 }
4639 type = qstring_new(); /* qdict has ref */
4640 cur_qs = key = qstring_new();
4641 break;
4642 case ':':
4643 cur_qs = type;
4644 break;
4645 default:
4646 qstring_append_chr(cur_qs, args_type[i]);
4647 break;
4648 }
4649 }
4650
4651out:
4652 return qdict;
4653}
4654
4655/*
4656 * Client argument checking rules:
4657 *
4658 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004659 * 2. Each argument provided by the client must be expected
4660 * 3. Each argument provided by the client must have the type expected
4661 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004662 */
4663static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4664{
4665 int flags, err;
4666 QDict *cmd_args;
4667
4668 cmd_args = qdict_from_args_type(cmd->args_type);
4669
4670 flags = 0;
4671 err = check_mandatory_args(cmd_args, client_args, &flags);
4672 if (err) {
4673 goto out;
4674 }
4675
Luiz Capitulino4af91932010-06-22 11:44:05 -03004676 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004677
4678out:
4679 QDECREF(cmd_args);
4680 return err;
4681}
4682
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004683/*
4684 * Input object checking rules
4685 *
4686 * 1. Input object must be a dict
4687 * 2. The "execute" key must exist
4688 * 3. The "execute" key must be a string
4689 * 4. If the "arguments" key exists, it must be a dict
4690 * 5. If the "id" key exists, it can be anything (ie. json-value)
4691 * 6. Any argument not listed above is considered invalid
4692 */
4693static QDict *qmp_check_input_obj(QObject *input_obj)
4694{
4695 const QDictEntry *ent;
4696 int has_exec_key = 0;
4697 QDict *input_dict;
4698
4699 if (qobject_type(input_obj) != QTYPE_QDICT) {
4700 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4701 return NULL;
4702 }
4703
4704 input_dict = qobject_to_qdict(input_obj);
4705
4706 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4707 const char *arg_name = qdict_entry_key(ent);
4708 const QObject *arg_obj = qdict_entry_value(ent);
4709
4710 if (!strcmp(arg_name, "execute")) {
4711 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4712 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4713 "string");
4714 return NULL;
4715 }
4716 has_exec_key = 1;
4717 } else if (!strcmp(arg_name, "arguments")) {
4718 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4719 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4720 "object");
4721 return NULL;
4722 }
4723 } else if (!strcmp(arg_name, "id")) {
4724 /* FIXME: check duplicated IDs for async commands */
4725 } else {
4726 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4727 return NULL;
4728 }
4729 }
4730
4731 if (!has_exec_key) {
4732 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4733 return NULL;
4734 }
4735
4736 return input_dict;
4737}
4738
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004739static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4740{
4741 QObject *ret_data = NULL;
4742
Luiz Capitulino4903de02010-09-16 11:01:32 -03004743 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004744 qmp_async_info_handler(mon, cmd);
4745 if (monitor_has_error(mon)) {
4746 monitor_protocol_emitter(mon, NULL);
4747 }
4748 } else {
4749 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004750 monitor_protocol_emitter(mon, ret_data);
4751 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004752 }
4753}
4754
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004755static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4756 const QDict *params)
4757{
4758 int ret;
4759 QObject *data = NULL;
4760
4761 mon_print_count_init(mon);
4762
4763 ret = cmd->mhandler.cmd_new(mon, params, &data);
4764 handler_audit(mon, cmd, ret);
4765 monitor_protocol_emitter(mon, data);
4766 qobject_decref(data);
4767}
4768
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004769static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4770{
4771 int err;
4772 QObject *obj;
4773 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004774 const mon_cmd_t *cmd;
4775 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004776 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004777
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004778 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004779 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004780
4781 obj = json_parser_parse(tokens, NULL);
4782 if (!obj) {
4783 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004784 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004785 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004786 }
4787
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004788 input = qmp_check_input_obj(obj);
4789 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004790 qobject_decref(obj);
4791 goto err_out;
4792 }
4793
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004794 mon->mc->id = qdict_get(input, "id");
4795 qobject_incref(mon->mc->id);
4796
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004797 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004798 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004799 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004800 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004801 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004802 }
4803
Anthony Liguorie3193602011-09-02 12:34:47 -05004804 cmd = qmp_find_cmd(cmd_name);
4805 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004806 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004807 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004808 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004809 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004810 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004811 }
4812
4813 obj = qdict_get(input, "arguments");
4814 if (!obj) {
4815 args = qdict_new();
4816 } else {
4817 args = qobject_to_qdict(obj);
4818 QINCREF(args);
4819 }
4820
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004821 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004822 if (err < 0) {
4823 goto err_out;
4824 }
4825
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004826 if (query_cmd) {
4827 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004828 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004829 err = qmp_async_cmd_handler(mon, cmd, args);
4830 if (err) {
4831 /* emit the error response */
4832 goto err_out;
4833 }
Adam Litke940cc302010-01-25 12:18:44 -06004834 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004835 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004836 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004837
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004838 goto out;
4839
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004840err_out:
4841 monitor_protocol_emitter(mon, NULL);
4842out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004843 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004844 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004845}
4846
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004847/**
4848 * monitor_control_read(): Read and handle QMP input
4849 */
4850static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4851{
4852 Monitor *old_mon = cur_mon;
4853
4854 cur_mon = opaque;
4855
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004856 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004857
4858 cur_mon = old_mon;
4859}
4860
aliguori731b0362009-03-05 23:01:42 +00004861static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004862{
aliguori731b0362009-03-05 23:01:42 +00004863 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004864 int i;
aliguori376253e2009-03-05 23:01:23 +00004865
aliguori731b0362009-03-05 23:01:42 +00004866 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004867
aliguoricde76ee2009-03-05 23:01:51 +00004868 if (cur_mon->rs) {
4869 for (i = 0; i < size; i++)
4870 readline_handle_byte(cur_mon->rs, buf[i]);
4871 } else {
4872 if (size == 0 || buf[size - 1] != 0)
4873 monitor_printf(cur_mon, "corrupted command\n");
4874 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004875 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004876 }
aliguori731b0362009-03-05 23:01:42 +00004877
4878 cur_mon = old_mon;
4879}
aliguorid8f44602008-10-06 13:52:44 +00004880
aliguori376253e2009-03-05 23:01:23 +00004881static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004882{
aliguori731b0362009-03-05 23:01:42 +00004883 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004884 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004885 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004886}
4887
aliguoricde76ee2009-03-05 23:01:51 +00004888int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004889{
aliguoricde76ee2009-03-05 23:01:51 +00004890 if (!mon->rs)
4891 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004892 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004893 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004894}
4895
aliguori376253e2009-03-05 23:01:23 +00004896void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004897{
aliguoricde76ee2009-03-05 23:01:51 +00004898 if (!mon->rs)
4899 return;
aliguori731b0362009-03-05 23:01:42 +00004900 if (--mon->suspend_cnt == 0)
4901 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004902}
4903
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004904static QObject *get_qmp_greeting(void)
4905{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004906 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004907
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004908 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004909 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4910}
4911
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004912/**
4913 * monitor_control_event(): Print QMP gretting
4914 */
4915static void monitor_control_event(void *opaque, int event)
4916{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004917 QObject *data;
4918 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004919
Luiz Capitulino47116d12010-02-08 17:01:30 -02004920 switch (event) {
4921 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004922 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004923 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004924 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004925 monitor_json_emitter(mon, data);
4926 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004927 break;
4928 case CHR_EVENT_CLOSED:
4929 json_message_parser_destroy(&mon->mc->parser);
4930 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004931 }
4932}
4933
aliguori731b0362009-03-05 23:01:42 +00004934static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004935{
aliguori376253e2009-03-05 23:01:23 +00004936 Monitor *mon = opaque;
4937
aliguori2724b182009-03-05 23:01:47 +00004938 switch (event) {
4939 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004940 mon->mux_out = 0;
4941 if (mon->reset_seen) {
4942 readline_restart(mon->rs);
4943 monitor_resume(mon);
4944 monitor_flush(mon);
4945 } else {
4946 mon->suspend_cnt = 0;
4947 }
aliguori2724b182009-03-05 23:01:47 +00004948 break;
ths86e94de2007-01-05 22:01:59 +00004949
aliguori2724b182009-03-05 23:01:47 +00004950 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004951 if (mon->reset_seen) {
4952 if (mon->suspend_cnt == 0) {
4953 monitor_printf(mon, "\n");
4954 }
4955 monitor_flush(mon);
4956 monitor_suspend(mon);
4957 } else {
4958 mon->suspend_cnt++;
4959 }
4960 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004961 break;
4962
Amit Shahb6b8df52009-10-07 18:31:16 +05304963 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004964 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4965 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004966 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004967 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004968 }
4969 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004970 break;
4971 }
ths86e94de2007-01-05 22:01:59 +00004972}
4973
aliguori76655d62009-03-06 20:27:37 +00004974
4975/*
4976 * Local variables:
4977 * c-indent-level: 4
4978 * c-basic-offset: 4
4979 * tab-width: 8
4980 * End:
4981 */
4982
aliguori731b0362009-03-05 23:01:42 +00004983void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004984{
aliguori731b0362009-03-05 23:01:42 +00004985 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004986 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004987
4988 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004989 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004990 is_first_init = 0;
4991 }
aliguori87127162009-03-05 23:01:29 +00004992
Anthony Liguori7267c092011-08-20 22:09:37 -05004993 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004994
aliguori87127162009-03-05 23:01:29 +00004995 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004996 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004997 if (flags & MONITOR_USE_READLINE) {
4998 mon->rs = readline_init(mon, monitor_find_completion);
4999 monitor_read_command(mon, 0);
5000 }
aliguori87127162009-03-05 23:01:29 +00005001
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005002 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005003 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005004 /* Control mode requires special handlers */
5005 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5006 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005007 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005008 } else {
5009 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5010 monitor_event, mon);
5011 }
aliguori87127162009-03-05 23:01:29 +00005012
Blue Swirl72cf2d42009-09-12 07:36:22 +00005013 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005014 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5015 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005016}
5017
aliguori376253e2009-03-05 23:01:23 +00005018static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005019{
aliguoribb5fc202009-03-05 23:01:15 +00005020 BlockDriverState *bs = opaque;
5021 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005022
aliguoribb5fc202009-03-05 23:01:15 +00005023 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005024 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005025 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005026 }
aliguori731b0362009-03-05 23:01:42 +00005027 if (mon->password_completion_cb)
5028 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005029
aliguori731b0362009-03-05 23:01:42 +00005030 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005031}
aliguoric0f4ce72009-03-05 23:01:01 +00005032
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005033int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5034 BlockDriverCompletionFunc *completion_cb,
5035 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005036{
aliguoricde76ee2009-03-05 23:01:51 +00005037 int err;
5038
aliguoribb5fc202009-03-05 23:01:15 +00005039 if (!bdrv_key_required(bs)) {
5040 if (completion_cb)
5041 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005042 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005043 }
aliguoric0f4ce72009-03-05 23:01:01 +00005044
Luiz Capitulino94171e12009-12-07 21:37:00 +01005045 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005046 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005047 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005048 }
5049
aliguori376253e2009-03-05 23:01:23 +00005050 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5051 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005052
aliguori731b0362009-03-05 23:01:42 +00005053 mon->password_completion_cb = completion_cb;
5054 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005055
aliguoricde76ee2009-03-05 23:01:51 +00005056 err = monitor_read_password(mon, bdrv_password_cb, bs);
5057
5058 if (err && completion_cb)
5059 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005060
5061 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005062}