blob: 379b7d4a58c787cd1d04ff9ae9ecfc91a280ece8 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000039#include "monitor.h"
40#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020042#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000044#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000045#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000046#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000047#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000048#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000049#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010051#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030052#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Blue Swirl314e2982011-09-11 20:22:05 +000066#include "memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
ths6a5bd302007-12-03 17:05:38 +000069
Jan Kiszka661f1922011-10-16 11:53:13 +020070/* for pic/irq_info */
71#if defined(TARGET_SPARC)
72#include "hw/sun4m.h"
73#endif
74#include "hw/lm32_pic.h"
75
bellard9dc39cb2004-03-14 21:38:27 +000076//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000077//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000078
bellard9307c4c2004-04-04 12:57:25 +000079/*
80 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000081 *
bellard9307c4c2004-04-04 12:57:25 +000082 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000083 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000084 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010085 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000090 * 'i' 32 bit integer
91 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010092 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020094 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010099 * 'T' double
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000102 * '/' optional gdb-like print format (like "/10x")
103 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100106 * 'b' boolean
107 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300108 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000109 *
110 */
111
Adam Litke940cc302010-01-25 12:18:44 -0600112typedef struct MonitorCompletionData MonitorCompletionData;
113struct MonitorCompletionData {
114 Monitor *mon;
115 void (*user_print)(Monitor *mon, const QObject *data);
116};
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300123 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 union {
125 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300126 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500133 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200134 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500135} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000136
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500138typedef struct mon_fd_t mon_fd_t;
139struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140 char *name;
141 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143};
144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145typedef struct MonitorControl {
146 QObject *id;
147 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200148 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200149} MonitorControl;
150
aliguori87127162009-03-05 23:01:29 +0000151struct Monitor {
152 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200153 int mux_out;
154 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000155 int flags;
156 int suspend_cnt;
157 uint8_t outbuf[1024];
158 int outbuf_index;
159 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200160 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000161 CPUState *mon_cpu;
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200164#ifdef CONFIG_DEBUG_MONITOR
165 int print_calls_nr;
166#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200167 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500168 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000170};
171
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200172#ifdef CONFIG_DEBUG_MONITOR
173#define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200176
177static inline void mon_print_count_inc(Monitor *mon)
178{
179 mon->print_calls_nr++;
180}
181
182static inline void mon_print_count_init(Monitor *mon)
183{
184 mon->print_calls_nr = 0;
185}
186
187static inline int mon_print_count_get(const Monitor *mon)
188{
189 return mon->print_calls_nr;
190}
191
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200192#else /* !CONFIG_DEBUG_MONITOR */
193#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200194static inline void mon_print_count_inc(Monitor *mon) { }
195static inline void mon_print_count_init(Monitor *mon) { }
196static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200197#endif /* CONFIG_DEBUG_MONITOR */
198
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300199/* QMP checker flags */
200#define QMP_ACCEPT_UNKNOWNS 1
201
Blue Swirl72cf2d42009-09-12 07:36:22 +0000202static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000203
Anthony Liguoric227f092009-10-01 16:12:16 -0500204static const mon_cmd_t mon_cmds[];
205static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000206
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300207static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300208static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300209
Markus Armbruster8631b602010-02-18 11:41:55 +0100210Monitor *cur_mon;
211Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000212
aliguori731b0362009-03-05 23:01:42 +0000213static void monitor_command_cb(Monitor *mon, const char *cmdline,
214 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000215
Luiz Capitulino09069b12010-02-04 18:10:06 -0200216static inline int qmp_cmd_mode(const Monitor *mon)
217{
218 return (mon->mc ? mon->mc->command_mode : 0);
219}
220
Luiz Capitulino418173c2009-11-26 22:58:51 -0200221/* Return true if in control mode, false otherwise */
222static inline int monitor_ctrl_mode(const Monitor *mon)
223{
224 return (mon->flags & MONITOR_USE_CONTROL);
225}
226
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100227/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228int monitor_cur_is_qmp(void)
229{
230 return cur_mon && monitor_ctrl_mode(cur_mon);
231}
232
aliguori731b0362009-03-05 23:01:42 +0000233static void monitor_read_command(Monitor *mon, int show_prompt)
234{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200235 if (!mon->rs)
236 return;
237
aliguori731b0362009-03-05 23:01:42 +0000238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
239 if (show_prompt)
240 readline_show_prompt(mon->rs);
241}
bellard6a00d602005-11-21 23:25:50 +0000242
aliguoricde76ee2009-03-05 23:01:51 +0000243static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
244 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000245{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100246 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100247 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100248 return -EINVAL;
249 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
252 return 0;
253 } else {
254 monitor_printf(mon, "terminal does not support password prompting\n");
255 return -ENOTTY;
256 }
aliguoribb5fc202009-03-05 23:01:15 +0000257}
258
aliguori376253e2009-03-05 23:01:23 +0000259void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000260{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000263 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
267/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000268static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000269{
ths60fe76f2007-12-16 03:02:09 +0000270 char c;
aliguori731b0362009-03-05 23:01:42 +0000271
bellard7e2515e2004-08-01 21:52:19 +0000272 for(;;) {
273 c = *str++;
274 if (c == '\0')
275 break;
bellard7ba12602006-07-14 20:26:42 +0000276 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000277 mon->outbuf[mon->outbuf_index++] = '\r';
278 mon->outbuf[mon->outbuf_index++] = c;
279 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
280 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000282 }
283}
284
aliguori376253e2009-03-05 23:01:23 +0000285void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000286{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200287 char buf[4096];
288
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200289 if (!mon)
290 return;
291
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200292 mon_print_count_inc(mon);
293
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200295 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200296 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200297
298 vsnprintf(buf, sizeof(buf), fmt, ap);
299 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000303{
304 va_list ap;
305 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000306 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000307 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000308}
309
aliguori376253e2009-03-05 23:01:23 +0000310void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000311{
312 int i;
313
314 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000315 switch (filename[i]) {
316 case ' ':
317 case '"':
318 case '\\':
319 monitor_printf(mon, "\\%c", filename[i]);
320 break;
321 case '\t':
322 monitor_printf(mon, "\\t");
323 break;
324 case '\r':
325 monitor_printf(mon, "\\r");
326 break;
327 case '\n':
328 monitor_printf(mon, "\\n");
329 break;
330 default:
331 monitor_printf(mon, "%c", filename[i]);
332 break;
333 }
thsfef30742006-12-22 14:11:32 +0000334 }
335}
336
Stefan Weil8b7968f2010-09-23 21:28:05 +0200337static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
338 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000339{
340 va_list ap;
341 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000342 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000343 va_end(ap);
344 return 0;
345}
346
Luiz Capitulino13c74252009-10-07 13:41:55 -0300347static void monitor_user_noop(Monitor *mon, const QObject *data) { }
348
Luiz Capitulino9e807212010-09-16 10:58:59 -0300349static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300350{
351 return cmd->user_print != NULL;
352}
353
Luiz Capitulino4903de02010-09-16 11:01:32 -0300354static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600355{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200356 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600357}
358
Luiz Capitulino8204a912009-11-18 23:05:31 -0200359static inline int monitor_has_error(const Monitor *mon)
360{
361 return mon->error != NULL;
362}
363
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200364static void monitor_json_emitter(Monitor *mon, const QObject *data)
365{
366 QString *json;
367
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200368 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
369 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200370 assert(json != NULL);
371
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200372 qstring_append_chr(json, '\n');
373 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200374
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200375 QDECREF(json);
376}
377
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378static void monitor_protocol_emitter(Monitor *mon, QObject *data)
379{
380 QDict *qmp;
381
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100382 trace_monitor_protocol_emitter(mon);
383
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 qmp = qdict_new();
385
386 if (!monitor_has_error(mon)) {
387 /* success response */
388 if (data) {
389 qobject_incref(data);
390 qdict_put_obj(qmp, "return", data);
391 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200392 /* return an empty QDict by default */
393 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200394 }
395 } else {
396 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100397 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200398 qdict_put(qmp, "error", mon->error->error);
399 QINCREF(mon->error->error);
400 QDECREF(mon->error);
401 mon->error = NULL;
402 }
403
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200404 if (mon->mc->id) {
405 qdict_put_obj(qmp, "id", mon->mc->id);
406 mon->mc->id = NULL;
407 }
408
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200409 monitor_json_emitter(mon, QOBJECT(qmp));
410 QDECREF(qmp);
411}
412
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200413static void timestamp_put(QDict *qdict)
414{
415 int err;
416 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000417 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418
Blue Swirld08d6f02009-12-04 18:06:39 +0000419 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200420 if (err < 0)
421 return;
422
423 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
424 "'microseconds': %" PRId64 " }",
425 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 qdict_put_obj(qdict, "timestamp", obj);
427}
428
429/**
430 * monitor_protocol_event(): Generate a Monitor event
431 *
432 * Event-specific data can be emitted through the (optional) 'data' parameter.
433 */
434void monitor_protocol_event(MonitorEvent event, QObject *data)
435{
436 QDict *qmp;
437 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600438 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439
Blue Swirl242cd002009-12-04 18:05:45 +0000440 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200442 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000443 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200444 event_name = "SHUTDOWN";
445 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000446 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200447 event_name = "RESET";
448 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000449 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200450 event_name = "POWERDOWN";
451 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000452 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200453 event_name = "STOP";
454 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300455 case QEVENT_RESUME:
456 event_name = "RESUME";
457 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200458 case QEVENT_VNC_CONNECTED:
459 event_name = "VNC_CONNECTED";
460 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200461 case QEVENT_VNC_INITIALIZED:
462 event_name = "VNC_INITIALIZED";
463 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200464 case QEVENT_VNC_DISCONNECTED:
465 event_name = "VNC_DISCONNECTED";
466 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200467 case QEVENT_BLOCK_IO_ERROR:
468 event_name = "BLOCK_IO_ERROR";
469 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300470 case QEVENT_RTC_CHANGE:
471 event_name = "RTC_CHANGE";
472 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300473 case QEVENT_WATCHDOG:
474 event_name = "WATCHDOG";
475 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200476 case QEVENT_SPICE_CONNECTED:
477 event_name = "SPICE_CONNECTED";
478 break;
479 case QEVENT_SPICE_INITIALIZED:
480 event_name = "SPICE_INITIALIZED";
481 break;
482 case QEVENT_SPICE_DISCONNECTED:
483 event_name = "SPICE_DISCONNECTED";
484 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200485 default:
486 abort();
487 break;
488 }
489
490 qmp = qdict_new();
491 timestamp_put(qmp);
492 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200493 if (data) {
494 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200495 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200496 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200497
Adam Litkef039a562010-01-15 08:34:02 -0600498 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200499 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200500 monitor_json_emitter(mon, QOBJECT(qmp));
501 }
Adam Litkef039a562010-01-15 08:34:02 -0600502 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200503 QDECREF(qmp);
504}
505
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200506static int do_qmp_capabilities(Monitor *mon, const QDict *params,
507 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200508{
509 /* Will setup QMP capabilities in the future */
510 if (monitor_ctrl_mode(mon)) {
511 mon->mc->command_mode = 1;
512 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200513
514 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200515}
516
Luiz Capitulino0268d972010-10-22 10:08:02 -0200517static void handle_user_command(Monitor *mon, const char *cmdline);
518
519static int do_hmp_passthrough(Monitor *mon, const QDict *params,
520 QObject **ret_data)
521{
522 int ret = 0;
523 Monitor *old_mon, hmp;
524 CharDriverState mchar;
525
526 memset(&hmp, 0, sizeof(hmp));
527 qemu_chr_init_mem(&mchar);
528 hmp.chr = &mchar;
529
530 old_mon = cur_mon;
531 cur_mon = &hmp;
532
533 if (qdict_haskey(params, "cpu-index")) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300534 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
Luiz Capitulino0268d972010-10-22 10:08:02 -0200535 if (ret < 0) {
536 cur_mon = old_mon;
537 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
538 goto out;
539 }
540 }
541
542 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
543 cur_mon = old_mon;
544
545 if (qemu_chr_mem_osize(hmp.chr) > 0) {
546 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
547 }
548
549out:
550 qemu_chr_close_mem(hmp.chr);
551 return ret;
552}
553
bellard9dc39cb2004-03-14 21:38:27 +0000554static int compare_cmd(const char *name, const char *list)
555{
556 const char *p, *pstart;
557 int len;
558 len = strlen(name);
559 p = list;
560 for(;;) {
561 pstart = p;
562 p = strchr(p, '|');
563 if (!p)
564 p = pstart + strlen(pstart);
565 if ((p - pstart) == len && !memcmp(pstart, name, len))
566 return 1;
567 if (*p == '\0')
568 break;
569 p++;
570 }
571 return 0;
572}
573
Anthony Liguoric227f092009-10-01 16:12:16 -0500574static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000575 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000576{
Anthony Liguoric227f092009-10-01 16:12:16 -0500577 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000578
579 for(cmd = cmds; cmd->name != NULL; cmd++) {
580 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000581 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
582 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000583 }
584}
585
aliguori376253e2009-03-05 23:01:23 +0000586static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000587{
588 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000589 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000590 } else {
aliguori376253e2009-03-05 23:01:23 +0000591 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000592 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000593 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "Log items (comma separated):\n");
595 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000596 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000597 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000598 }
599 }
bellard9dc39cb2004-03-14 21:38:27 +0000600 }
601}
602
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300603static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300604{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300605 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300606}
607
Lluísfc764102011-08-31 20:31:18 +0200608static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530609{
610 const char *tp_name = qdict_get_str(qdict, "name");
611 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200612 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000613
614 if (!ret) {
615 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
616 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530617}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100618
Michael Rothc45a8162011-10-02 08:44:37 -0500619#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100620static void do_trace_file(Monitor *mon, const QDict *qdict)
621{
622 const char *op = qdict_get_try_str(qdict, "op");
623 const char *arg = qdict_get_try_str(qdict, "arg");
624
625 if (!op) {
626 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
627 } else if (!strcmp(op, "on")) {
628 st_set_trace_file_enabled(true);
629 } else if (!strcmp(op, "off")) {
630 st_set_trace_file_enabled(false);
631 } else if (!strcmp(op, "flush")) {
632 st_flush_trace_buffer();
633 } else if (!strcmp(op, "set")) {
634 if (arg) {
635 st_set_trace_file(arg);
636 }
637 } else {
638 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
639 help_cmd(mon, "trace-file");
640 }
641}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530642#endif
643
Adam Litke940cc302010-01-25 12:18:44 -0600644static void user_monitor_complete(void *opaque, QObject *ret_data)
645{
646 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
647
648 if (ret_data) {
649 data->user_print(data->mon, ret_data);
650 }
651 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500652 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600653}
654
655static void qmp_monitor_complete(void *opaque, QObject *ret_data)
656{
657 monitor_protocol_emitter(opaque, ret_data);
658}
659
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300660static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
661 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600662{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300663 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600664}
665
666static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
667{
668 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
669}
670
671static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
672 const QDict *params)
673{
674 int ret;
675
Anthony Liguori7267c092011-08-20 22:09:37 -0500676 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600677 cb_data->mon = mon;
678 cb_data->user_print = cmd->user_print;
679 monitor_suspend(mon);
680 ret = cmd->mhandler.cmd_async(mon, params,
681 user_monitor_complete, cb_data);
682 if (ret < 0) {
683 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500684 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600685 }
686}
687
688static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
689{
690 int ret;
691
Anthony Liguori7267c092011-08-20 22:09:37 -0500692 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600693 cb_data->mon = mon;
694 cb_data->user_print = cmd->user_print;
695 monitor_suspend(mon);
696 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
697 if (ret < 0) {
698 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500699 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600700 }
701}
702
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300703static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000704{
Anthony Liguoric227f092009-10-01 16:12:16 -0500705 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300706 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000707
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200708 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000709 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200710 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711
712 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000713 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300714 break;
bellard9dc39cb2004-03-14 21:38:27 +0000715 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300716
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200717 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300718 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200719 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300720
Luiz Capitulino4903de02010-09-16 11:01:32 -0300721 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300722 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300723 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300724 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200725
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300726 cmd->mhandler.info_new(mon, &info_data);
727 if (info_data) {
728 cmd->user_print(mon, info_data);
729 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200730 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300731 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300732 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300733 }
734
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300735 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300736
737help:
738 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000739}
740
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300741static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200742{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300743 CommandInfoList *info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200744
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300745 info = g_malloc0(sizeof(*info));
746 info->value = g_malloc0(sizeof(*info->value));
747 info->value->name = g_strdup(cmd_name);
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200748
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300749 return info;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200750}
751
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300752CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000753{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300754 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200755 const mon_cmd_t *cmd;
756
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300757 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300758 info = alloc_cmd_entry(cmd->name);
759 info->next = cmd_list;
760 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200761 }
762
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300763 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300764 char buf[128];
765 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300766 info = alloc_cmd_entry(buf);
767 info->next = cmd_list;
768 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200769 }
770
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300771 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000772}
773
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300774/* set the current CPU defined by the user */
775int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000776{
777 CPUState *env;
778
779 for(env = first_cpu; env != NULL; env = env->next_cpu) {
780 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000781 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000782 return 0;
783 }
784 }
785 return -1;
786}
787
pbrook9596ebb2007-11-18 01:44:38 +0000788static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000789{
aliguori731b0362009-03-05 23:01:42 +0000790 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300791 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000792 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300793 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000794 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000795}
796
Luiz Capitulino99b77962011-10-24 10:53:44 -0200797int monitor_get_cpu_index(void)
798{
799 return mon_get_cpu()->cpu_index;
800}
801
aliguori376253e2009-03-05 23:01:23 +0000802static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000803{
bellard6a00d602005-11-21 23:25:50 +0000804 CPUState *env;
805 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000806#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000807 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000808 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000809#else
aliguori376253e2009-03-05 23:01:23 +0000810 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000811 0);
bellard9307c4c2004-04-04 12:57:25 +0000812#endif
813}
814
aliguori376253e2009-03-05 23:01:23 +0000815static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000816{
aliguori376253e2009-03-05 23:01:23 +0000817 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000818}
819
aliguori376253e2009-03-05 23:01:23 +0000820static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000821{
822 int i;
bellard7e2515e2004-08-01 21:52:19 +0000823 const char *str;
ths3b46e622007-09-17 08:09:54 +0000824
aliguoricde76ee2009-03-05 23:01:51 +0000825 if (!mon->rs)
826 return;
bellard7e2515e2004-08-01 21:52:19 +0000827 i = 0;
828 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000829 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000830 if (!str)
831 break;
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000833 i++;
bellardaa455482004-04-04 13:07:25 +0000834 }
835}
836
j_mayer76a66252007-03-07 08:32:30 +0000837#if defined(TARGET_PPC)
838/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000839static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000840{
841 CPUState *env;
842
843 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000844 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000845}
846#endif
847
Lluís6d8a7642011-08-31 20:30:43 +0200848#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530849static void do_info_trace(Monitor *mon)
850{
851 st_print_trace((FILE *)mon, &monitor_fprintf);
852}
Lluís31965ae2011-08-31 20:31:24 +0200853#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854
Lluísfc764102011-08-31 20:31:18 +0200855static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530856{
Lluísfc764102011-08-31 20:31:18 +0200857 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530858}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530859
Jes Sorensen821601e2011-03-16 13:33:36 +0100860#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200861static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000862{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600863 if (!password || !password[0]) {
864 if (vnc_display_disable_login(NULL)) {
865 qerror_report(QERR_SET_PASSWD_FAILED);
866 return -1;
867 }
868 return 0;
869 }
870
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200871 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100872 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200873 return -1;
874 }
aliguoribb5fc202009-03-05 23:01:15 +0000875
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200876 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100877}
878
879static void change_vnc_password_cb(Monitor *mon, const char *password,
880 void *opaque)
881{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100882 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000883 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000884}
885
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200886static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000887{
ths70848512007-08-25 01:37:05 +0000888 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000889 strcmp(target, "password") == 0) {
890 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000891 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000892 strncpy(password, arg, sizeof(password));
893 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200894 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000895 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200896 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000897 }
ths70848512007-08-25 01:37:05 +0000898 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200899 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100900 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200901 return -1;
902 }
ths70848512007-08-25 01:37:05 +0000903 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200904
905 return 0;
thse25a5822007-08-25 01:36:20 +0000906}
Jes Sorensen821601e2011-03-16 13:33:36 +0100907#else
908static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
909{
910 qerror_report(QERR_FEATURE_DISABLED, "vnc");
911 return -ENODEV;
912}
913#endif
thse25a5822007-08-25 01:36:20 +0000914
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100915/**
916 * do_change(): Change a removable medium, or VNC configuration
917 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200918static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000919{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300920 const char *device = qdict_get_str(qdict, "device");
921 const char *target = qdict_get_str(qdict, "target");
922 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200923 int ret;
924
thse25a5822007-08-25 01:36:20 +0000925 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200926 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000927 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200928 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000929 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200930
931 return ret;
thse25a5822007-08-25 01:36:20 +0000932}
933
Gerd Hoffmann75721502010-10-07 12:22:54 +0200934static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
935{
936 const char *protocol = qdict_get_str(qdict, "protocol");
937 const char *password = qdict_get_str(qdict, "password");
938 const char *connected = qdict_get_try_str(qdict, "connected");
939 int disconnect_if_connected = 0;
940 int fail_if_connected = 0;
941 int rc;
942
943 if (connected) {
944 if (strcmp(connected, "fail") == 0) {
945 fail_if_connected = 1;
946 } else if (strcmp(connected, "disconnect") == 0) {
947 disconnect_if_connected = 1;
948 } else if (strcmp(connected, "keep") == 0) {
949 /* nothing */
950 } else {
951 qerror_report(QERR_INVALID_PARAMETER, "connected");
952 return -1;
953 }
954 }
955
956 if (strcmp(protocol, "spice") == 0) {
957 if (!using_spice) {
958 /* correct one? spice isn't a device ,,, */
959 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
960 return -1;
961 }
962 rc = qemu_spice_set_passwd(password, fail_if_connected,
963 disconnect_if_connected);
964 if (rc != 0) {
965 qerror_report(QERR_SET_PASSWD_FAILED);
966 return -1;
967 }
968 return 0;
969 }
970
971 if (strcmp(protocol, "vnc") == 0) {
972 if (fail_if_connected || disconnect_if_connected) {
973 /* vnc supports "connected=keep" only */
974 qerror_report(QERR_INVALID_PARAMETER, "connected");
975 return -1;
976 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600977 /* Note that setting an empty password will not disable login through
978 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +0100979 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200980 }
981
982 qerror_report(QERR_INVALID_PARAMETER, "protocol");
983 return -1;
984}
985
986static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
987{
988 const char *protocol = qdict_get_str(qdict, "protocol");
989 const char *whenstr = qdict_get_str(qdict, "time");
990 time_t when;
991 int rc;
992
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100993 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200994 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100995 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200996 when = TIME_MAX;
997 } else if (whenstr[0] == '+') {
998 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
999 } else {
1000 when = strtoull(whenstr, NULL, 10);
1001 }
1002
1003 if (strcmp(protocol, "spice") == 0) {
1004 if (!using_spice) {
1005 /* correct one? spice isn't a device ,,, */
1006 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1007 return -1;
1008 }
1009 rc = qemu_spice_set_pw_expire(when);
1010 if (rc != 0) {
1011 qerror_report(QERR_SET_PASSWD_FAILED);
1012 return -1;
1013 }
1014 return 0;
1015 }
1016
1017 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001018 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001019 }
1020
1021 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1022 return -1;
1023}
1024
Daniel P. Berrange13661082011-06-23 13:31:42 +01001025static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1026{
1027 const char *protocol = qdict_get_str(qdict, "protocol");
1028 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001029 CharDriverState *s;
1030
1031 if (strcmp(protocol, "spice") == 0) {
1032 if (!using_spice) {
1033 /* correct one? spice isn't a device ,,, */
1034 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1035 return -1;
1036 }
1037 qerror_report(QERR_ADD_CLIENT_FAILED);
1038 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001039#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001040 } else if (strcmp(protocol, "vnc") == 0) {
1041 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001042 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001043 vnc_display_add_client(NULL, fd, skipauth);
1044 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001045#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001046 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1047 int fd = monitor_get_fd(mon, fdname);
1048 if (qemu_chr_add_client(s, fd) < 0) {
1049 qerror_report(QERR_ADD_CLIENT_FAILED);
1050 return -1;
1051 }
1052 return 0;
1053 }
1054
1055 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1056 return -1;
1057}
1058
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001059static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1060{
1061 const char *protocol = qdict_get_str(qdict, "protocol");
1062 const char *hostname = qdict_get_str(qdict, "hostname");
1063 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1064 int port = qdict_get_try_int(qdict, "port", -1);
1065 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1066 int ret;
1067
1068 if (strcmp(protocol, "spice") == 0) {
1069 if (!using_spice) {
1070 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1071 return -1;
1072 }
1073
1074 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1075 if (ret != 0) {
1076 qerror_report(QERR_UNDEFINED_ERROR);
1077 return -1;
1078 }
1079 return 0;
1080 }
1081
1082 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1083 return -1;
1084}
1085
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001086static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001087{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001088 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001089 return 0;
bellard59a983b2004-03-17 23:17:16 +00001090}
1091
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001092static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001093{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001095}
1096
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001097static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001098{
1099 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001100 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001101
bellard9307c4c2004-04-04 12:57:25 +00001102 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001103 mask = 0;
1104 } else {
bellard9307c4c2004-04-04 12:57:25 +00001105 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001106 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001107 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001108 return;
1109 }
1110 }
1111 cpu_set_log(mask);
1112}
1113
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001114static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001115{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001116 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001117 if (!option || !strcmp(option, "on")) {
1118 singlestep = 1;
1119 } else if (!strcmp(option, "off")) {
1120 singlestep = 0;
1121 } else {
1122 monitor_printf(mon, "unexpected option %s\n", option);
1123 }
1124}
1125
aliguoribb5fc202009-03-05 23:01:15 +00001126static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001127
aliguori376253e2009-03-05 23:01:23 +00001128struct bdrv_iterate_context {
1129 Monitor *mon;
1130 int err;
1131};
aliguoric0f4ce72009-03-05 23:01:01 +00001132
Luiz Capitulino28a72822011-09-26 17:43:50 -03001133static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1134{
1135 bdrv_iostatus_reset(bs);
1136}
1137
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001138/**
1139 * do_cont(): Resume emulation.
1140 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001141static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001142{
1143 struct bdrv_iterate_context context = { mon, 0 };
1144
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001145 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301146 qerror_report(QERR_MIGRATION_EXPECTED);
1147 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001148 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1149 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001150 qerror_report(QERR_RESET_REQUIRED);
1151 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301152 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001153
Luiz Capitulino28a72822011-09-26 17:43:50 -03001154 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001155 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001156 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001157 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001158 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001159 return 0;
1160 } else {
1161 return -1;
1162 }
bellard8a7ddc32004-03-31 19:00:16 +00001163}
1164
aliguoribb5fc202009-03-05 23:01:15 +00001165static void bdrv_key_cb(void *opaque, int err)
1166{
aliguori376253e2009-03-05 23:01:23 +00001167 Monitor *mon = opaque;
1168
aliguoribb5fc202009-03-05 23:01:15 +00001169 /* another key was set successfully, retry to continue */
1170 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001171 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001172}
1173
1174static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1175{
aliguori376253e2009-03-05 23:01:23 +00001176 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001177
aliguori376253e2009-03-05 23:01:23 +00001178 if (!context->err && bdrv_key_required(bs)) {
1179 context->err = -EBUSY;
1180 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1181 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001182 }
1183}
1184
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001185static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001186{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001188 if (!device)
1189 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1190 if (gdbserver_start(device) < 0) {
1191 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1192 device);
1193 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001194 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001195 } else {
aliguori59030a82009-04-05 18:43:41 +00001196 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1197 device);
bellard8a7ddc32004-03-31 19:00:16 +00001198 }
1199}
1200
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001202{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001203 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001204 if (select_watchdog_action(action) == -1) {
1205 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1206 }
1207}
1208
aliguori376253e2009-03-05 23:01:23 +00001209static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001210{
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001212 switch(c) {
1213 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 default:
1226 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001228 } else {
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001230 }
1231 break;
1232 }
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001234}
1235
aliguori376253e2009-03-05 23:01:23 +00001236static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001237 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001238{
bellard6a00d602005-11-21 23:25:50 +00001239 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001240 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001241 uint8_t buf[16];
1242 uint64_t v;
1243
1244 if (format == 'i') {
1245 int flags;
1246 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001247 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001248#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001249 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001250 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001251 } else if (wsize == 4) {
1252 flags = 0;
1253 } else {
bellard6a15fd12006-04-12 21:07:07 +00001254 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001255 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001256 if (env) {
1257#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001258 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001259 (env->segs[R_CS].flags & DESC_L_MASK))
1260 flags = 2;
1261 else
1262#endif
1263 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1264 flags = 1;
1265 }
bellard4c27ba22004-04-25 18:05:08 +00001266 }
1267#endif
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001269 return;
1270 }
1271
1272 len = wsize * count;
1273 if (wsize == 1)
1274 line_size = 8;
1275 else
1276 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001277 max_digits = 0;
1278
1279 switch(format) {
1280 case 'o':
1281 max_digits = (wsize * 8 + 2) / 3;
1282 break;
1283 default:
1284 case 'x':
1285 max_digits = (wsize * 8) / 4;
1286 break;
1287 case 'u':
1288 case 'd':
1289 max_digits = (wsize * 8 * 10 + 32) / 33;
1290 break;
1291 case 'c':
1292 wsize = 1;
1293 break;
1294 }
1295
1296 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001297 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001299 else
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001301 l = len;
1302 if (l > line_size)
1303 l = line_size;
1304 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001305 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001306 } else {
bellard6a00d602005-11-21 23:25:50 +00001307 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001308 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001310 break;
1311 }
bellard9307c4c2004-04-04 12:57:25 +00001312 }
ths5fafdf22007-09-16 21:08:06 +00001313 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001314 while (i < l) {
1315 switch(wsize) {
1316 default:
1317 case 1:
1318 v = ldub_raw(buf + i);
1319 break;
1320 case 2:
1321 v = lduw_raw(buf + i);
1322 break;
1323 case 4:
bellard92a31b12005-02-10 22:00:52 +00001324 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001325 break;
1326 case 8:
1327 v = ldq_raw(buf + i);
1328 break;
1329 }
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001331 switch(format) {
1332 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001334 break;
1335 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001336 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001337 break;
1338 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001339 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001340 break;
1341 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001343 break;
1344 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001345 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001346 break;
1347 }
1348 i += wsize;
1349 }
aliguori376253e2009-03-05 23:01:23 +00001350 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001351 addr += l;
1352 len -= l;
1353 }
1354}
1355
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001357{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001358 int count = qdict_get_int(qdict, "count");
1359 int format = qdict_get_int(qdict, "format");
1360 int size = qdict_get_int(qdict, "size");
1361 target_long addr = qdict_get_int(qdict, "addr");
1362
aliguori376253e2009-03-05 23:01:23 +00001363 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001364}
1365
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001366static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001367{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001368 int count = qdict_get_int(qdict, "count");
1369 int format = qdict_get_int(qdict, "format");
1370 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001371 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001372
aliguori376253e2009-03-05 23:01:23 +00001373 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001374}
1375
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001376static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001377{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001378 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001379 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001380
blueswir17743e582007-09-24 18:39:04 +00001381#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001382 switch(format) {
1383 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001385 break;
1386 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001391 break;
1392 default:
1393 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001398 break;
1399 }
bellard92a31b12005-02-10 22:00:52 +00001400#else
1401 switch(format) {
1402 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001404 break;
1405 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001407 break;
1408 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001409 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001410 break;
1411 default:
1412 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001414 break;
1415 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001417 break;
1418 }
1419#endif
aliguori376253e2009-03-05 23:01:23 +00001420 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001421}
1422
Luiz Capitulino98696222010-02-10 23:49:58 -02001423static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001424{
1425 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001426 uint32_t size = qdict_get_int(qdict, "size");
1427 const char *filename = qdict_get_str(qdict, "filename");
1428 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001429 uint32_t l;
1430 CPUState *env;
1431 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001432 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001433
1434 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001435
1436 f = fopen(filename, "wb");
1437 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001438 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001439 return -1;
bellardb371dc52007-01-03 15:20:39 +00001440 }
1441 while (size != 0) {
1442 l = sizeof(buf);
1443 if (l > size)
1444 l = size;
1445 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001446 if (fwrite(buf, 1, l, f) != l) {
1447 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1448 goto exit;
1449 }
bellardb371dc52007-01-03 15:20:39 +00001450 addr += l;
1451 size -= l;
1452 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001453
1454 ret = 0;
1455
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001456exit:
bellardb371dc52007-01-03 15:20:39 +00001457 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001458 return ret;
bellardb371dc52007-01-03 15:20:39 +00001459}
1460
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001461static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001462 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001463{
1464 FILE *f;
1465 uint32_t l;
1466 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001467 uint32_t size = qdict_get_int(qdict, "size");
1468 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001469 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001470 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001471
1472 f = fopen(filename, "wb");
1473 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001474 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001475 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001476 }
1477 while (size != 0) {
1478 l = sizeof(buf);
1479 if (l > size)
1480 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001481 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001482 if (fwrite(buf, 1, l, f) != l) {
1483 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1484 goto exit;
1485 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001486 fflush(f);
1487 addr += l;
1488 size -= l;
1489 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001490
1491 ret = 0;
1492
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001493exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001494 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001495 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001496}
1497
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001498static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001499{
1500 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001501 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001502 uint32_t start = qdict_get_int(qdict, "start");
1503 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001504
1505 sum = 0;
1506 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001507 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001508 /* BSD sum algorithm ('sum' Unix command) */
1509 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001510 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001511 }
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001513}
1514
bellarda3a91a32004-06-04 11:06:21 +00001515typedef struct {
1516 int keycode;
1517 const char *name;
1518} KeyDef;
1519
1520static const KeyDef key_defs[] = {
1521 { 0x2a, "shift" },
1522 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001523
bellarda3a91a32004-06-04 11:06:21 +00001524 { 0x38, "alt" },
1525 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001526 { 0x64, "altgr" },
1527 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001528 { 0x1d, "ctrl" },
1529 { 0x9d, "ctrl_r" },
1530
1531 { 0xdd, "menu" },
1532
1533 { 0x01, "esc" },
1534
1535 { 0x02, "1" },
1536 { 0x03, "2" },
1537 { 0x04, "3" },
1538 { 0x05, "4" },
1539 { 0x06, "5" },
1540 { 0x07, "6" },
1541 { 0x08, "7" },
1542 { 0x09, "8" },
1543 { 0x0a, "9" },
1544 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001545 { 0x0c, "minus" },
1546 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001547 { 0x0e, "backspace" },
1548
1549 { 0x0f, "tab" },
1550 { 0x10, "q" },
1551 { 0x11, "w" },
1552 { 0x12, "e" },
1553 { 0x13, "r" },
1554 { 0x14, "t" },
1555 { 0x15, "y" },
1556 { 0x16, "u" },
1557 { 0x17, "i" },
1558 { 0x18, "o" },
1559 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001560 { 0x1a, "bracket_left" },
1561 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001562 { 0x1c, "ret" },
1563
1564 { 0x1e, "a" },
1565 { 0x1f, "s" },
1566 { 0x20, "d" },
1567 { 0x21, "f" },
1568 { 0x22, "g" },
1569 { 0x23, "h" },
1570 { 0x24, "j" },
1571 { 0x25, "k" },
1572 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001573 { 0x27, "semicolon" },
1574 { 0x28, "apostrophe" },
1575 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001576
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001577 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001578 { 0x2c, "z" },
1579 { 0x2d, "x" },
1580 { 0x2e, "c" },
1581 { 0x2f, "v" },
1582 { 0x30, "b" },
1583 { 0x31, "n" },
1584 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001585 { 0x33, "comma" },
1586 { 0x34, "dot" },
1587 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001588
balrog4d3b6f62008-02-10 16:33:14 +00001589 { 0x37, "asterisk" },
1590
bellarda3a91a32004-06-04 11:06:21 +00001591 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001592 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001593 { 0x3b, "f1" },
1594 { 0x3c, "f2" },
1595 { 0x3d, "f3" },
1596 { 0x3e, "f4" },
1597 { 0x3f, "f5" },
1598 { 0x40, "f6" },
1599 { 0x41, "f7" },
1600 { 0x42, "f8" },
1601 { 0x43, "f9" },
1602 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001603 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001604 { 0x46, "scroll_lock" },
1605
bellard64866c32006-05-07 18:03:31 +00001606 { 0xb5, "kp_divide" },
1607 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001608 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001609 { 0x4e, "kp_add" },
1610 { 0x9c, "kp_enter" },
1611 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001612 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001613
1614 { 0x52, "kp_0" },
1615 { 0x4f, "kp_1" },
1616 { 0x50, "kp_2" },
1617 { 0x51, "kp_3" },
1618 { 0x4b, "kp_4" },
1619 { 0x4c, "kp_5" },
1620 { 0x4d, "kp_6" },
1621 { 0x47, "kp_7" },
1622 { 0x48, "kp_8" },
1623 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001624
bellarda3a91a32004-06-04 11:06:21 +00001625 { 0x56, "<" },
1626
1627 { 0x57, "f11" },
1628 { 0x58, "f12" },
1629
1630 { 0xb7, "print" },
1631
1632 { 0xc7, "home" },
1633 { 0xc9, "pgup" },
1634 { 0xd1, "pgdn" },
1635 { 0xcf, "end" },
1636
1637 { 0xcb, "left" },
1638 { 0xc8, "up" },
1639 { 0xd0, "down" },
1640 { 0xcd, "right" },
1641
1642 { 0xd2, "insert" },
1643 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001644#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1645 { 0xf0, "stop" },
1646 { 0xf1, "again" },
1647 { 0xf2, "props" },
1648 { 0xf3, "undo" },
1649 { 0xf4, "front" },
1650 { 0xf5, "copy" },
1651 { 0xf6, "open" },
1652 { 0xf7, "paste" },
1653 { 0xf8, "find" },
1654 { 0xf9, "cut" },
1655 { 0xfa, "lf" },
1656 { 0xfb, "help" },
1657 { 0xfc, "meta_l" },
1658 { 0xfd, "meta_r" },
1659 { 0xfe, "compose" },
1660#endif
bellarda3a91a32004-06-04 11:06:21 +00001661 { 0, NULL },
1662};
1663
1664static int get_keycode(const char *key)
1665{
1666 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001667 char *endp;
1668 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001669
1670 for(p = key_defs; p->name != NULL; p++) {
1671 if (!strcmp(key, p->name))
1672 return p->keycode;
1673 }
bellard64866c32006-05-07 18:03:31 +00001674 if (strstart(key, "0x", NULL)) {
1675 ret = strtoul(key, &endp, 0);
1676 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1677 return ret;
1678 }
bellarda3a91a32004-06-04 11:06:21 +00001679 return -1;
1680}
1681
balrogc8256f92008-06-08 22:45:01 +00001682#define MAX_KEYCODES 16
1683static uint8_t keycodes[MAX_KEYCODES];
1684static int nb_pending_keycodes;
1685static QEMUTimer *key_timer;
1686
1687static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001688{
balrogc8256f92008-06-08 22:45:01 +00001689 int keycode;
1690
1691 while (nb_pending_keycodes > 0) {
1692 nb_pending_keycodes--;
1693 keycode = keycodes[nb_pending_keycodes];
1694 if (keycode & 0x80)
1695 kbd_put_keycode(0xe0);
1696 kbd_put_keycode(keycode | 0x80);
1697 }
1698}
1699
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001700static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001701{
balrog3401c0d2008-06-04 10:05:59 +00001702 char keyname_buf[16];
1703 char *separator;
1704 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001705 const char *string = qdict_get_str(qdict, "string");
1706 int has_hold_time = qdict_haskey(qdict, "hold_time");
1707 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001708
balrogc8256f92008-06-08 22:45:01 +00001709 if (nb_pending_keycodes > 0) {
1710 qemu_del_timer(key_timer);
1711 release_keys(NULL);
1712 }
1713 if (!has_hold_time)
1714 hold_time = 100;
1715 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001716 while (1) {
1717 separator = strchr(string, '-');
1718 keyname_len = separator ? separator - string : strlen(string);
1719 if (keyname_len > 0) {
1720 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1721 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001722 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001723 return;
bellarda3a91a32004-06-04 11:06:21 +00001724 }
balrogc8256f92008-06-08 22:45:01 +00001725 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001726 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001727 return;
1728 }
1729 keyname_buf[keyname_len] = 0;
1730 keycode = get_keycode(keyname_buf);
1731 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001732 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001733 return;
1734 }
balrogc8256f92008-06-08 22:45:01 +00001735 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001736 }
balrog3401c0d2008-06-04 10:05:59 +00001737 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001738 break;
balrog3401c0d2008-06-04 10:05:59 +00001739 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001740 }
balrogc8256f92008-06-08 22:45:01 +00001741 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001742 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001743 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001744 keycode = keycodes[i];
1745 if (keycode & 0x80)
1746 kbd_put_keycode(0xe0);
1747 kbd_put_keycode(keycode & 0x7f);
1748 }
balrogc8256f92008-06-08 22:45:01 +00001749 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001750 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001751 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001752}
1753
bellard13224a82006-07-14 22:03:35 +00001754static int mouse_button_state;
1755
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001756static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001757{
1758 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001759 const char *dx_str = qdict_get_str(qdict, "dx_str");
1760 const char *dy_str = qdict_get_str(qdict, "dy_str");
1761 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001762 dx = strtol(dx_str, NULL, 0);
1763 dy = strtol(dy_str, NULL, 0);
1764 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001765 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001766 dz = strtol(dz_str, NULL, 0);
1767 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1768}
1769
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001770static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001771{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001772 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001773 mouse_button_state = button_state;
1774 kbd_mouse_event(0, 0, 0, mouse_button_state);
1775}
1776
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001777static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001778{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001779 int size = qdict_get_int(qdict, "size");
1780 int addr = qdict_get_int(qdict, "addr");
1781 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001782 uint32_t val;
1783 int suffix;
1784
1785 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001786 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001787 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001788 addr++;
1789 }
1790 addr &= 0xffff;
1791
1792 switch(size) {
1793 default:
1794 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001795 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001796 suffix = 'b';
1797 break;
1798 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001799 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001800 suffix = 'w';
1801 break;
1802 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001803 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001804 suffix = 'l';
1805 break;
1806 }
aliguori376253e2009-03-05 23:01:23 +00001807 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1808 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001809}
bellarda3a91a32004-06-04 11:06:21 +00001810
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001811static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001812{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001813 int size = qdict_get_int(qdict, "size");
1814 int addr = qdict_get_int(qdict, "addr");
1815 int val = qdict_get_int(qdict, "val");
1816
Jan Kiszkaf1147842009-07-14 10:20:11 +02001817 addr &= IOPORTS_MASK;
1818
1819 switch (size) {
1820 default:
1821 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001822 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001823 break;
1824 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001825 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001826 break;
1827 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001828 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001829 break;
1830 }
1831}
1832
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001833static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001834{
1835 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001836 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001837
Jan Kiszka76e30d02009-07-02 00:19:02 +02001838 res = qemu_boot_set(bootdevice);
1839 if (res == 0) {
1840 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1841 } else if (res > 0) {
1842 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001843 } else {
aliguori376253e2009-03-05 23:01:23 +00001844 monitor_printf(mon, "no function defined to set boot device list for "
1845 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001846 }
1847}
1848
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001849/**
Luiz Capitulino43076662009-10-07 13:41:59 -03001850 * do_system_powerdown(): Issue a machine powerdown
1851 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001852static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1853 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001854{
1855 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001856 return 0;
bellard34751872005-07-02 14:31:34 +00001857}
1858
bellardb86bda52004-09-18 19:32:46 +00001859#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001860static void print_pte(Monitor *mon, target_phys_addr_t addr,
1861 target_phys_addr_t pte,
1862 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001863{
Blue Swirld65aaf32010-12-11 18:56:24 +00001864#ifdef TARGET_X86_64
1865 if (addr & (1ULL << 47)) {
1866 addr |= -1LL << 48;
1867 }
1868#endif
1869 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1870 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001871 addr,
1872 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001873 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001874 pte & PG_GLOBAL_MASK ? 'G' : '-',
1875 pte & PG_PSE_MASK ? 'P' : '-',
1876 pte & PG_DIRTY_MASK ? 'D' : '-',
1877 pte & PG_ACCESSED_MASK ? 'A' : '-',
1878 pte & PG_PCD_MASK ? 'C' : '-',
1879 pte & PG_PWT_MASK ? 'T' : '-',
1880 pte & PG_USER_MASK ? 'U' : '-',
1881 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001882}
1883
Blue Swirld65aaf32010-12-11 18:56:24 +00001884static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001885{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001886 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001887 uint32_t pgd, pde, pte;
1888
bellardb86bda52004-09-18 19:32:46 +00001889 pgd = env->cr[3] & ~0xfff;
1890 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001891 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001892 pde = le32_to_cpu(pde);
1893 if (pde & PG_PRESENT_MASK) {
1894 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001895 /* 4M pages */
1896 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001897 } else {
1898 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001899 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001900 pte = le32_to_cpu(pte);
1901 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001902 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001903 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001904 ~0xfff);
1905 }
1906 }
1907 }
1908 }
1909 }
1910}
1911
Blue Swirld65aaf32010-12-11 18:56:24 +00001912static void tlb_info_pae32(Monitor *mon, CPUState *env)
1913{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001914 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001915 uint64_t pdpe, pde, pte;
1916 uint64_t pdp_addr, pd_addr, pt_addr;
1917
1918 pdp_addr = env->cr[3] & ~0x1f;
1919 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001920 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001921 pdpe = le64_to_cpu(pdpe);
1922 if (pdpe & PG_PRESENT_MASK) {
1923 pd_addr = pdpe & 0x3fffffffff000ULL;
1924 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001925 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001926 pde = le64_to_cpu(pde);
1927 if (pde & PG_PRESENT_MASK) {
1928 if (pde & PG_PSE_MASK) {
1929 /* 2M pages with PAE, CR4.PSE is ignored */
1930 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1931 ~((target_phys_addr_t)(1 << 20) - 1));
1932 } else {
1933 pt_addr = pde & 0x3fffffffff000ULL;
1934 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001935 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001936 pte = le64_to_cpu(pte);
1937 if (pte & PG_PRESENT_MASK) {
1938 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1939 + (l3 << 12),
1940 pte & ~PG_PSE_MASK,
1941 ~(target_phys_addr_t)0xfff);
1942 }
1943 }
1944 }
1945 }
1946 }
1947 }
1948 }
1949}
1950
1951#ifdef TARGET_X86_64
1952static void tlb_info_64(Monitor *mon, CPUState *env)
1953{
1954 uint64_t l1, l2, l3, l4;
1955 uint64_t pml4e, pdpe, pde, pte;
1956 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1957
1958 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1959 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001960 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001961 pml4e = le64_to_cpu(pml4e);
1962 if (pml4e & PG_PRESENT_MASK) {
1963 pdp_addr = pml4e & 0x3fffffffff000ULL;
1964 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001965 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001966 pdpe = le64_to_cpu(pdpe);
1967 if (pdpe & PG_PRESENT_MASK) {
1968 if (pdpe & PG_PSE_MASK) {
1969 /* 1G pages, CR4.PSE is ignored */
1970 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1971 0x3ffffc0000000ULL);
1972 } else {
1973 pd_addr = pdpe & 0x3fffffffff000ULL;
1974 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001975 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001976 pde = le64_to_cpu(pde);
1977 if (pde & PG_PRESENT_MASK) {
1978 if (pde & PG_PSE_MASK) {
1979 /* 2M pages, CR4.PSE is ignored */
1980 print_pte(mon, (l1 << 39) + (l2 << 30) +
1981 (l3 << 21), pde,
1982 0x3ffffffe00000ULL);
1983 } else {
1984 pt_addr = pde & 0x3fffffffff000ULL;
1985 for (l4 = 0; l4 < 512; l4++) {
1986 cpu_physical_memory_read(pt_addr
1987 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001988 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001989 pte = le64_to_cpu(pte);
1990 if (pte & PG_PRESENT_MASK) {
1991 print_pte(mon, (l1 << 39) +
1992 (l2 << 30) +
1993 (l3 << 21) + (l4 << 12),
1994 pte & ~PG_PSE_MASK,
1995 0x3fffffffff000ULL);
1996 }
1997 }
1998 }
1999 }
2000 }
2001 }
2002 }
2003 }
2004 }
2005 }
2006}
2007#endif
2008
2009static void tlb_info(Monitor *mon)
2010{
2011 CPUState *env;
2012
2013 env = mon_get_cpu();
2014
2015 if (!(env->cr[0] & CR0_PG_MASK)) {
2016 monitor_printf(mon, "PG disabled\n");
2017 return;
2018 }
2019 if (env->cr[4] & CR4_PAE_MASK) {
2020#ifdef TARGET_X86_64
2021 if (env->hflags & HF_LMA_MASK) {
2022 tlb_info_64(mon, env);
2023 } else
2024#endif
2025 {
2026 tlb_info_pae32(mon, env);
2027 }
2028 } else {
2029 tlb_info_32(mon, env);
2030 }
2031}
2032
Blue Swirl1b3cba62010-12-11 18:56:27 +00002033static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2034 int *plast_prot,
2035 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002036{
bellard9746b152004-11-11 18:30:24 +00002037 int prot1;
2038 prot1 = *plast_prot;
2039 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002040 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002041 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2042 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002043 *pstart, end, end - *pstart,
2044 prot1 & PG_USER_MASK ? 'u' : '-',
2045 'r',
2046 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002047 }
2048 if (prot != 0)
2049 *pstart = end;
2050 else
2051 *pstart = -1;
2052 *plast_prot = prot;
2053 }
2054}
2055
Blue Swirl1b3cba62010-12-11 18:56:27 +00002056static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002057{
Austin Clementsb49ca722011-08-14 23:19:21 -04002058 unsigned int l1, l2;
2059 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002060 uint32_t pgd, pde, pte;
2061 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002062
bellardb86bda52004-09-18 19:32:46 +00002063 pgd = env->cr[3] & ~0xfff;
2064 last_prot = 0;
2065 start = -1;
2066 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002067 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002068 pde = le32_to_cpu(pde);
2069 end = l1 << 22;
2070 if (pde & PG_PRESENT_MASK) {
2071 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2072 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002073 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002074 } else {
2075 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002076 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002077 pte = le32_to_cpu(pte);
2078 end = (l1 << 22) + (l2 << 12);
2079 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002080 prot = pte & pde &
2081 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002082 } else {
2083 prot = 0;
2084 }
aliguori376253e2009-03-05 23:01:23 +00002085 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002086 }
2087 }
2088 } else {
2089 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002090 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002091 }
2092 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002093 /* Flush last range */
2094 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002095}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002096
2097static void mem_info_pae32(Monitor *mon, CPUState *env)
2098{
Austin Clementsb49ca722011-08-14 23:19:21 -04002099 unsigned int l1, l2, l3;
2100 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002101 uint64_t pdpe, pde, pte;
2102 uint64_t pdp_addr, pd_addr, pt_addr;
2103 target_phys_addr_t start, end;
2104
2105 pdp_addr = env->cr[3] & ~0x1f;
2106 last_prot = 0;
2107 start = -1;
2108 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002109 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002110 pdpe = le64_to_cpu(pdpe);
2111 end = l1 << 30;
2112 if (pdpe & PG_PRESENT_MASK) {
2113 pd_addr = pdpe & 0x3fffffffff000ULL;
2114 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002115 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002116 pde = le64_to_cpu(pde);
2117 end = (l1 << 30) + (l2 << 21);
2118 if (pde & PG_PRESENT_MASK) {
2119 if (pde & PG_PSE_MASK) {
2120 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2121 PG_PRESENT_MASK);
2122 mem_print(mon, &start, &last_prot, end, prot);
2123 } else {
2124 pt_addr = pde & 0x3fffffffff000ULL;
2125 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002126 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002127 pte = le64_to_cpu(pte);
2128 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2129 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002130 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2131 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002132 } else {
2133 prot = 0;
2134 }
2135 mem_print(mon, &start, &last_prot, end, prot);
2136 }
2137 }
2138 } else {
2139 prot = 0;
2140 mem_print(mon, &start, &last_prot, end, prot);
2141 }
2142 }
2143 } else {
2144 prot = 0;
2145 mem_print(mon, &start, &last_prot, end, prot);
2146 }
2147 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002148 /* Flush last range */
2149 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002150}
2151
2152
2153#ifdef TARGET_X86_64
2154static void mem_info_64(Monitor *mon, CPUState *env)
2155{
2156 int prot, last_prot;
2157 uint64_t l1, l2, l3, l4;
2158 uint64_t pml4e, pdpe, pde, pte;
2159 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2160
2161 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2162 last_prot = 0;
2163 start = -1;
2164 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002165 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002166 pml4e = le64_to_cpu(pml4e);
2167 end = l1 << 39;
2168 if (pml4e & PG_PRESENT_MASK) {
2169 pdp_addr = pml4e & 0x3fffffffff000ULL;
2170 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002171 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002172 pdpe = le64_to_cpu(pdpe);
2173 end = (l1 << 39) + (l2 << 30);
2174 if (pdpe & PG_PRESENT_MASK) {
2175 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002176 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2177 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002178 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002179 mem_print(mon, &start, &last_prot, end, prot);
2180 } else {
2181 pd_addr = pdpe & 0x3fffffffff000ULL;
2182 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002183 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002184 pde = le64_to_cpu(pde);
2185 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2186 if (pde & PG_PRESENT_MASK) {
2187 if (pde & PG_PSE_MASK) {
2188 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2189 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002190 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002191 mem_print(mon, &start, &last_prot, end, prot);
2192 } else {
2193 pt_addr = pde & 0x3fffffffff000ULL;
2194 for (l4 = 0; l4 < 512; l4++) {
2195 cpu_physical_memory_read(pt_addr
2196 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002197 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002198 pte = le64_to_cpu(pte);
2199 end = (l1 << 39) + (l2 << 30) +
2200 (l3 << 21) + (l4 << 12);
2201 if (pte & PG_PRESENT_MASK) {
2202 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2203 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002204 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002205 } else {
2206 prot = 0;
2207 }
2208 mem_print(mon, &start, &last_prot, end, prot);
2209 }
2210 }
2211 } else {
2212 prot = 0;
2213 mem_print(mon, &start, &last_prot, end, prot);
2214 }
2215 }
2216 }
2217 } else {
2218 prot = 0;
2219 mem_print(mon, &start, &last_prot, end, prot);
2220 }
2221 }
2222 } else {
2223 prot = 0;
2224 mem_print(mon, &start, &last_prot, end, prot);
2225 }
2226 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002227 /* Flush last range */
2228 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002229}
2230#endif
2231
2232static void mem_info(Monitor *mon)
2233{
2234 CPUState *env;
2235
2236 env = mon_get_cpu();
2237
2238 if (!(env->cr[0] & CR0_PG_MASK)) {
2239 monitor_printf(mon, "PG disabled\n");
2240 return;
2241 }
2242 if (env->cr[4] & CR4_PAE_MASK) {
2243#ifdef TARGET_X86_64
2244 if (env->hflags & HF_LMA_MASK) {
2245 mem_info_64(mon, env);
2246 } else
2247#endif
2248 {
2249 mem_info_pae32(mon, env);
2250 }
2251 } else {
2252 mem_info_32(mon, env);
2253 }
2254}
bellardb86bda52004-09-18 19:32:46 +00002255#endif
2256
aurel327c664e22009-03-03 06:12:22 +00002257#if defined(TARGET_SH4)
2258
aliguori376253e2009-03-05 23:01:23 +00002259static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002260{
aliguori376253e2009-03-05 23:01:23 +00002261 monitor_printf(mon, " tlb%i:\t"
2262 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2263 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2264 "dirty=%hhu writethrough=%hhu\n",
2265 idx,
2266 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2267 tlb->v, tlb->sh, tlb->c, tlb->pr,
2268 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002269}
2270
aliguori376253e2009-03-05 23:01:23 +00002271static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002272{
2273 CPUState *env = mon_get_cpu();
2274 int i;
2275
aliguori376253e2009-03-05 23:01:23 +00002276 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002277 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002278 print_tlb (mon, i, &env->itlb[i]);
2279 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002280 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002281 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002282}
2283
2284#endif
2285
Scott Woodbebabbc2011-08-18 10:38:42 +00002286#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002287static void tlb_info(Monitor *mon)
2288{
2289 CPUState *env1 = mon_get_cpu();
2290
2291 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2292}
2293#endif
2294
Blue Swirl314e2982011-09-11 20:22:05 +00002295static void do_info_mtree(Monitor *mon)
2296{
2297 mtree_info((fprintf_function)monitor_printf, mon);
2298}
2299
aliguori030ea372009-04-21 22:30:47 +00002300static void do_info_numa(Monitor *mon)
2301{
aliguorib28b6232009-04-22 20:20:29 +00002302 int i;
aliguori030ea372009-04-21 22:30:47 +00002303 CPUState *env;
2304
2305 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2306 for (i = 0; i < nb_numa_nodes; i++) {
2307 monitor_printf(mon, "node %d cpus:", i);
2308 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2309 if (env->numa_node == i) {
2310 monitor_printf(mon, " %d", env->cpu_index);
2311 }
2312 }
2313 monitor_printf(mon, "\n");
2314 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2315 node_mem[i] >> 20);
2316 }
2317}
2318
bellard5f1ce942006-02-08 22:40:15 +00002319#ifdef CONFIG_PROFILER
2320
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002321int64_t qemu_time;
2322int64_t dev_time;
2323
aliguori376253e2009-03-05 23:01:23 +00002324static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002325{
2326 int64_t total;
2327 total = qemu_time;
2328 if (total == 0)
2329 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002330 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002331 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002332 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002333 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002334 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002335 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002336}
2337#else
aliguori376253e2009-03-05 23:01:23 +00002338static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002339{
aliguori376253e2009-03-05 23:01:23 +00002340 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002341}
2342#endif
2343
bellardec36b692006-07-16 18:57:03 +00002344/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002345static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002346
aliguori376253e2009-03-05 23:01:23 +00002347static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002348{
2349 int i;
2350 CaptureState *s;
2351
2352 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002353 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002354 s->ops.info (s->opaque);
2355 }
2356}
2357
Blue Swirl23130862009-06-06 08:22:04 +00002358#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002359static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002360{
2361 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002362 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002363 CaptureState *s;
2364
2365 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2366 if (i == n) {
2367 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002368 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002369 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002370 return;
2371 }
2372 }
2373}
2374
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002375static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002376{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002377 const char *path = qdict_get_str(qdict, "path");
2378 int has_freq = qdict_haskey(qdict, "freq");
2379 int freq = qdict_get_try_int(qdict, "freq", -1);
2380 int has_bits = qdict_haskey(qdict, "bits");
2381 int bits = qdict_get_try_int(qdict, "bits", -1);
2382 int has_channels = qdict_haskey(qdict, "nchannels");
2383 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002384 CaptureState *s;
2385
Anthony Liguori7267c092011-08-20 22:09:37 -05002386 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002387
2388 freq = has_freq ? freq : 44100;
2389 bits = has_bits ? bits : 16;
2390 nchannels = has_channels ? nchannels : 2;
2391
2392 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002393 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002394 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002395 return;
bellardec36b692006-07-16 18:57:03 +00002396 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002397 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002398}
2399#endif
2400
aurel32dc1c0b72008-04-27 23:52:12 +00002401#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002402static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002403{
2404 CPUState *env;
2405
2406 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2407 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2408 }
2409
2410 return 0;
2411}
2412#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002413static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002414{
2415 qerror_report(QERR_UNSUPPORTED);
2416 return -1;
2417}
aurel32dc1c0b72008-04-27 23:52:12 +00002418#endif
2419
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002420static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002421{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002422 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002423
aliguori76655d62009-03-06 20:27:37 +00002424 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002425 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002426 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002427 return acl;
2428}
aliguori76655d62009-03-06 20:27:37 +00002429
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002430static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002431{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002432 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002433 qemu_acl *acl = find_acl(mon, aclname);
2434 qemu_acl_entry *entry;
2435 int i = 0;
2436
2437 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002438 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002439 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002440 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002441 i++;
2442 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002443 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002444 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002445 }
2446}
2447
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002448static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002449{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002450 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002451 qemu_acl *acl = find_acl(mon, aclname);
2452
2453 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002454 qemu_acl_reset(acl);
2455 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002456 }
2457}
aliguori76655d62009-03-06 20:27:37 +00002458
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002459static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002460{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002461 const char *aclname = qdict_get_str(qdict, "aclname");
2462 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002463 qemu_acl *acl = find_acl(mon, aclname);
2464
2465 if (acl) {
2466 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002467 acl->defaultDeny = 0;
2468 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002469 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002470 acl->defaultDeny = 1;
2471 monitor_printf(mon, "acl: policy set to 'deny'\n");
2472 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002473 monitor_printf(mon, "acl: unknown policy '%s', "
2474 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002475 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002476 }
2477}
aliguori76655d62009-03-06 20:27:37 +00002478
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002479static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002480{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002481 const char *aclname = qdict_get_str(qdict, "aclname");
2482 const char *match = qdict_get_str(qdict, "match");
2483 const char *policy = qdict_get_str(qdict, "policy");
2484 int has_index = qdict_haskey(qdict, "index");
2485 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002486 qemu_acl *acl = find_acl(mon, aclname);
2487 int deny, ret;
2488
2489 if (acl) {
2490 if (strcmp(policy, "allow") == 0) {
2491 deny = 0;
2492 } else if (strcmp(policy, "deny") == 0) {
2493 deny = 1;
2494 } else {
2495 monitor_printf(mon, "acl: unknown policy '%s', "
2496 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002497 return;
2498 }
aliguori28a76be2009-03-06 20:27:40 +00002499 if (has_index)
2500 ret = qemu_acl_insert(acl, deny, match, index);
2501 else
2502 ret = qemu_acl_append(acl, deny, match);
2503 if (ret < 0)
2504 monitor_printf(mon, "acl: unable to add acl entry\n");
2505 else
2506 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002507 }
2508}
aliguori76655d62009-03-06 20:27:37 +00002509
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002510static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002511{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002512 const char *aclname = qdict_get_str(qdict, "aclname");
2513 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002514 qemu_acl *acl = find_acl(mon, aclname);
2515 int ret;
aliguori76655d62009-03-06 20:27:37 +00002516
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002517 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002518 ret = qemu_acl_remove(acl, match);
2519 if (ret < 0)
2520 monitor_printf(mon, "acl: no matching acl entry\n");
2521 else
2522 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002523 }
2524}
2525
Huang Ying79c4f6b2009-06-23 10:05:14 +08002526#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002527static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002528{
2529 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002530 int cpu_index = qdict_get_int(qdict, "cpu_index");
2531 int bank = qdict_get_int(qdict, "bank");
2532 uint64_t status = qdict_get_int(qdict, "status");
2533 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2534 uint64_t addr = qdict_get_int(qdict, "addr");
2535 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002536 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002537
Jan Kiszka747461c2011-03-02 08:56:10 +01002538 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2539 flags |= MCE_INJECT_BROADCAST;
2540 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002541 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002542 if (cenv->cpu_index == cpu_index) {
2543 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002544 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002545 break;
2546 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002547 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002548}
2549#endif
2550
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002551static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002552{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002553 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002554 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002555 int fd;
2556
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002557 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002558 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002559 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002560 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002561 }
2562
2563 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002564 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2565 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002566 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002567 }
2568
Blue Swirl72cf2d42009-09-12 07:36:22 +00002569 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002570 if (strcmp(monfd->name, fdname) != 0) {
2571 continue;
2572 }
2573
2574 close(monfd->fd);
2575 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002576 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002577 }
2578
Anthony Liguori7267c092011-08-20 22:09:37 -05002579 monfd = g_malloc0(sizeof(mon_fd_t));
2580 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002581 monfd->fd = fd;
2582
Blue Swirl72cf2d42009-09-12 07:36:22 +00002583 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002584 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002585}
2586
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002587static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002588{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002589 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002590 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002591
Blue Swirl72cf2d42009-09-12 07:36:22 +00002592 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002593 if (strcmp(monfd->name, fdname) != 0) {
2594 continue;
2595 }
2596
Blue Swirl72cf2d42009-09-12 07:36:22 +00002597 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002598 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002599 g_free(monfd->name);
2600 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002601 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002602 }
2603
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002604 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002605 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002606}
2607
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002608static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002609{
Luiz Capitulino13548692011-07-29 15:36:43 -03002610 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002611 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002612
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002613 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002614
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002615 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002616 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002617 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002618}
2619
Mark McLoughlin7768e042009-07-22 09:11:41 +01002620int monitor_get_fd(Monitor *mon, const char *fdname)
2621{
Anthony Liguoric227f092009-10-01 16:12:16 -05002622 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002623
Blue Swirl72cf2d42009-09-12 07:36:22 +00002624 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002625 int fd;
2626
2627 if (strcmp(monfd->name, fdname) != 0) {
2628 continue;
2629 }
2630
2631 fd = monfd->fd;
2632
2633 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002634 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002635 g_free(monfd->name);
2636 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002637
2638 return fd;
2639 }
2640
2641 return -1;
2642}
2643
Anthony Liguoric227f092009-10-01 16:12:16 -05002644static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002645#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002646 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002647};
2648
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002649/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002650static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 {
2652 .name = "version",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002656 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
2659 .name = "network",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002663 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 },
2665 {
2666 .name = "chardev",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002670 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
2673 .name = "block",
2674 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 .params = "",
2676 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002677 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 },
2679 {
2680 .name = "blockstats",
2681 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .params = "",
2683 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002684 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 },
2686 {
2687 .name = "registers",
2688 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 .params = "",
2690 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002691 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 },
2693 {
2694 .name = "cpus",
2695 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .params = "",
2697 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002698 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
2700 {
2701 .name = "history",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002705 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002707#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2708 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 {
2710 .name = "irq",
2711 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 .params = "",
2713 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002714#ifdef TARGET_SPARC
2715 .mhandler.info = sun4m_irq_info,
2716#elif defined(TARGET_LM32)
2717 .mhandler.info = lm32_irq_info,
2718#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002719 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002720#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
2722 {
2723 .name = "pic",
2724 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 .params = "",
2726 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002727#ifdef TARGET_SPARC
2728 .mhandler.info = sun4m_pic_info,
2729#elif defined(TARGET_LM32)
2730 .mhandler.info = lm32_do_pic_info,
2731#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002732 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002733#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002735#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 {
2737 .name = "pci",
2738 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 .params = "",
2740 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002741 .user_print = do_pci_info_print,
2742 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002744#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2745 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 {
2747 .name = "tlb",
2748 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 .params = "",
2750 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002751 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 },
aurel327c664e22009-03-03 06:12:22 +00002753#endif
2754#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 {
2756 .name = "mem",
2757 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 .params = "",
2759 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002760 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 },
bellardb86bda52004-09-18 19:32:46 +00002762#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 {
Blue Swirl314e2982011-09-11 20:22:05 +00002764 .name = "mtree",
2765 .args_type = "",
2766 .params = "",
2767 .help = "show memory tree",
2768 .mhandler.info = do_info_mtree,
2769 },
2770 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .name = "jit",
2772 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 .params = "",
2774 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002775 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 },
2777 {
2778 .name = "kvm",
2779 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 .params = "",
2781 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002782 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 },
2784 {
2785 .name = "numa",
2786 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 .params = "",
2788 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002789 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002790 },
2791 {
2792 .name = "usb",
2793 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 .params = "",
2795 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002796 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 },
2798 {
2799 .name = "usbhost",
2800 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 .params = "",
2802 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002803 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002804 },
2805 {
2806 .name = "profile",
2807 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 .params = "",
2809 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002810 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 },
2812 {
2813 .name = "capture",
2814 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002815 .params = "",
2816 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002817 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 },
2819 {
2820 .name = "snapshots",
2821 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 .params = "",
2823 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002824 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 },
2826 {
2827 .name = "status",
2828 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 .params = "",
2830 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002831 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002832 },
2833 {
2834 .name = "pcmcia",
2835 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002836 .params = "",
2837 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002838 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002839 },
2840 {
2841 .name = "mice",
2842 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 .params = "",
2844 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002845 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002846 },
2847 {
2848 .name = "vnc",
2849 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 .params = "",
2851 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002852 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002853 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002854#if defined(CONFIG_SPICE)
2855 {
2856 .name = "spice",
2857 .args_type = "",
2858 .params = "",
2859 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002860 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002861 },
2862#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002863 {
2864 .name = "name",
2865 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002866 .params = "",
2867 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002868 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002869 },
2870 {
2871 .name = "uuid",
2872 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002873 .params = "",
2874 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002875 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002876 },
j_mayer76a66252007-03-07 08:32:30 +00002877#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 {
2879 .name = "cpustats",
2880 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 .params = "",
2882 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002883 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002884 },
j_mayer76a66252007-03-07 08:32:30 +00002885#endif
blueswir131a60e22007-10-26 18:42:59 +00002886#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 {
2888 .name = "usernet",
2889 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002890 .params = "",
2891 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002892 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002893 },
blueswir131a60e22007-10-26 18:42:59 +00002894#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 {
2896 .name = "migrate",
2897 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002898 .params = "",
2899 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002900 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002901 },
2902 {
2903 .name = "balloon",
2904 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 .params = "",
2906 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002907 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002908 },
2909 {
2910 .name = "qtree",
2911 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 .params = "",
2913 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002914 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002915 },
2916 {
2917 .name = "qdm",
2918 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 .params = "",
2920 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002921 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 },
2923 {
2924 .name = "roms",
2925 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 .params = "",
2927 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002928 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 },
Lluís6d8a7642011-08-31 20:30:43 +02002930#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05302931 {
2932 .name = "trace",
2933 .args_type = "",
2934 .params = "",
2935 .help = "show current contents of trace buffer",
2936 .mhandler.info = do_info_trace,
2937 },
Lluís31965ae2011-08-31 20:31:24 +02002938#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05302939 {
2940 .name = "trace-events",
2941 .args_type = "",
2942 .params = "",
2943 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002944 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302945 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002946 {
2947 .name = NULL,
2948 },
bellard9dc39cb2004-03-14 21:38:27 +00002949};
2950
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002951static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002952#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002953 { /* NULL */ },
2954};
2955
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002956static const mon_cmd_t qmp_query_cmds[] = {
2957 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002958 .name = "pci",
2959 .args_type = "",
2960 .params = "",
2961 .help = "show PCI info",
2962 .user_print = do_pci_info_print,
2963 .mhandler.info_new = do_pci_info,
2964 },
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002965 { /* NULL */ },
2966};
2967
bellard9307c4c2004-04-04 12:57:25 +00002968/*******************************************************************/
2969
2970static const char *pch;
2971static jmp_buf expr_env;
2972
bellard92a31b12005-02-10 22:00:52 +00002973#define MD_TLONG 0
2974#define MD_I32 1
2975
bellard9307c4c2004-04-04 12:57:25 +00002976typedef struct MonitorDef {
2977 const char *name;
2978 int offset;
blueswir18662d652008-10-02 18:32:44 +00002979 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002980 int type;
bellard9307c4c2004-04-04 12:57:25 +00002981} MonitorDef;
2982
bellard57206fd2004-04-25 18:54:52 +00002983#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002984static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002985{
bellard6a00d602005-11-21 23:25:50 +00002986 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002987 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002988}
2989#endif
2990
bellarda541f292004-04-12 20:39:29 +00002991#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002992static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002993{
bellard6a00d602005-11-21 23:25:50 +00002994 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002995 unsigned int u;
2996 int i;
2997
2998 u = 0;
2999 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003000 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003001
3002 return u;
3003}
3004
blueswir18662d652008-10-02 18:32:44 +00003005static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003006{
bellard6a00d602005-11-21 23:25:50 +00003007 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003008 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003009}
3010
blueswir18662d652008-10-02 18:32:44 +00003011static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003012{
bellard6a00d602005-11-21 23:25:50 +00003013 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003014 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003015}
bellard9fddaa02004-05-21 12:59:32 +00003016
blueswir18662d652008-10-02 18:32:44 +00003017static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003018{
bellard6a00d602005-11-21 23:25:50 +00003019 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003020 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003021}
3022
blueswir18662d652008-10-02 18:32:44 +00003023static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003024{
bellard6a00d602005-11-21 23:25:50 +00003025 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003026 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003027}
3028
blueswir18662d652008-10-02 18:32:44 +00003029static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003030{
bellard6a00d602005-11-21 23:25:50 +00003031 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003032 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003033}
bellarda541f292004-04-12 20:39:29 +00003034#endif
3035
bellarde95c8d52004-09-30 22:22:08 +00003036#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003037#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003038static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003039{
bellard6a00d602005-11-21 23:25:50 +00003040 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003041
3042 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003043}
bellard7b936c02005-10-30 17:05:13 +00003044#endif
bellarde95c8d52004-09-30 22:22:08 +00003045
blueswir18662d652008-10-02 18:32:44 +00003046static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003047{
bellard6a00d602005-11-21 23:25:50 +00003048 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003049 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003050}
3051#endif
3052
blueswir18662d652008-10-02 18:32:44 +00003053static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003054#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003055
3056#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003057 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003058 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003059 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003060
bellard9307c4c2004-04-04 12:57:25 +00003061 { "eax", offsetof(CPUState, regs[0]) },
3062 { "ecx", offsetof(CPUState, regs[1]) },
3063 { "edx", offsetof(CPUState, regs[2]) },
3064 { "ebx", offsetof(CPUState, regs[3]) },
3065 { "esp|sp", offsetof(CPUState, regs[4]) },
3066 { "ebp|fp", offsetof(CPUState, regs[5]) },
3067 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003068 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003069#ifdef TARGET_X86_64
3070 { "r8", offsetof(CPUState, regs[8]) },
3071 { "r9", offsetof(CPUState, regs[9]) },
3072 { "r10", offsetof(CPUState, regs[10]) },
3073 { "r11", offsetof(CPUState, regs[11]) },
3074 { "r12", offsetof(CPUState, regs[12]) },
3075 { "r13", offsetof(CPUState, regs[13]) },
3076 { "r14", offsetof(CPUState, regs[14]) },
3077 { "r15", offsetof(CPUState, regs[15]) },
3078#endif
bellard9307c4c2004-04-04 12:57:25 +00003079 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003080 { "eip", offsetof(CPUState, eip) },
3081 SEG("cs", R_CS)
3082 SEG("ds", R_DS)
3083 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003084 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003085 SEG("fs", R_FS)
3086 SEG("gs", R_GS)
3087 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003088#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003089 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003090 { "r0", offsetof(CPUState, gpr[0]) },
3091 { "r1", offsetof(CPUState, gpr[1]) },
3092 { "r2", offsetof(CPUState, gpr[2]) },
3093 { "r3", offsetof(CPUState, gpr[3]) },
3094 { "r4", offsetof(CPUState, gpr[4]) },
3095 { "r5", offsetof(CPUState, gpr[5]) },
3096 { "r6", offsetof(CPUState, gpr[6]) },
3097 { "r7", offsetof(CPUState, gpr[7]) },
3098 { "r8", offsetof(CPUState, gpr[8]) },
3099 { "r9", offsetof(CPUState, gpr[9]) },
3100 { "r10", offsetof(CPUState, gpr[10]) },
3101 { "r11", offsetof(CPUState, gpr[11]) },
3102 { "r12", offsetof(CPUState, gpr[12]) },
3103 { "r13", offsetof(CPUState, gpr[13]) },
3104 { "r14", offsetof(CPUState, gpr[14]) },
3105 { "r15", offsetof(CPUState, gpr[15]) },
3106 { "r16", offsetof(CPUState, gpr[16]) },
3107 { "r17", offsetof(CPUState, gpr[17]) },
3108 { "r18", offsetof(CPUState, gpr[18]) },
3109 { "r19", offsetof(CPUState, gpr[19]) },
3110 { "r20", offsetof(CPUState, gpr[20]) },
3111 { "r21", offsetof(CPUState, gpr[21]) },
3112 { "r22", offsetof(CPUState, gpr[22]) },
3113 { "r23", offsetof(CPUState, gpr[23]) },
3114 { "r24", offsetof(CPUState, gpr[24]) },
3115 { "r25", offsetof(CPUState, gpr[25]) },
3116 { "r26", offsetof(CPUState, gpr[26]) },
3117 { "r27", offsetof(CPUState, gpr[27]) },
3118 { "r28", offsetof(CPUState, gpr[28]) },
3119 { "r29", offsetof(CPUState, gpr[29]) },
3120 { "r30", offsetof(CPUState, gpr[30]) },
3121 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003122 /* Floating point registers */
3123 { "f0", offsetof(CPUState, fpr[0]) },
3124 { "f1", offsetof(CPUState, fpr[1]) },
3125 { "f2", offsetof(CPUState, fpr[2]) },
3126 { "f3", offsetof(CPUState, fpr[3]) },
3127 { "f4", offsetof(CPUState, fpr[4]) },
3128 { "f5", offsetof(CPUState, fpr[5]) },
3129 { "f6", offsetof(CPUState, fpr[6]) },
3130 { "f7", offsetof(CPUState, fpr[7]) },
3131 { "f8", offsetof(CPUState, fpr[8]) },
3132 { "f9", offsetof(CPUState, fpr[9]) },
3133 { "f10", offsetof(CPUState, fpr[10]) },
3134 { "f11", offsetof(CPUState, fpr[11]) },
3135 { "f12", offsetof(CPUState, fpr[12]) },
3136 { "f13", offsetof(CPUState, fpr[13]) },
3137 { "f14", offsetof(CPUState, fpr[14]) },
3138 { "f15", offsetof(CPUState, fpr[15]) },
3139 { "f16", offsetof(CPUState, fpr[16]) },
3140 { "f17", offsetof(CPUState, fpr[17]) },
3141 { "f18", offsetof(CPUState, fpr[18]) },
3142 { "f19", offsetof(CPUState, fpr[19]) },
3143 { "f20", offsetof(CPUState, fpr[20]) },
3144 { "f21", offsetof(CPUState, fpr[21]) },
3145 { "f22", offsetof(CPUState, fpr[22]) },
3146 { "f23", offsetof(CPUState, fpr[23]) },
3147 { "f24", offsetof(CPUState, fpr[24]) },
3148 { "f25", offsetof(CPUState, fpr[25]) },
3149 { "f26", offsetof(CPUState, fpr[26]) },
3150 { "f27", offsetof(CPUState, fpr[27]) },
3151 { "f28", offsetof(CPUState, fpr[28]) },
3152 { "f29", offsetof(CPUState, fpr[29]) },
3153 { "f30", offsetof(CPUState, fpr[30]) },
3154 { "f31", offsetof(CPUState, fpr[31]) },
3155 { "fpscr", offsetof(CPUState, fpscr) },
3156 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003157 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003158 { "lr", offsetof(CPUState, lr) },
3159 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003160 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003161 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003162 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003163 { "msr", 0, &monitor_get_msr, },
3164 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003165 { "tbu", 0, &monitor_get_tbu, },
3166 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003167#if defined(TARGET_PPC64)
3168 /* Address space register */
3169 { "asr", offsetof(CPUState, asr) },
3170#endif
3171 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003172 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003173 { "sr0", offsetof(CPUState, sr[0]) },
3174 { "sr1", offsetof(CPUState, sr[1]) },
3175 { "sr2", offsetof(CPUState, sr[2]) },
3176 { "sr3", offsetof(CPUState, sr[3]) },
3177 { "sr4", offsetof(CPUState, sr[4]) },
3178 { "sr5", offsetof(CPUState, sr[5]) },
3179 { "sr6", offsetof(CPUState, sr[6]) },
3180 { "sr7", offsetof(CPUState, sr[7]) },
3181 { "sr8", offsetof(CPUState, sr[8]) },
3182 { "sr9", offsetof(CPUState, sr[9]) },
3183 { "sr10", offsetof(CPUState, sr[10]) },
3184 { "sr11", offsetof(CPUState, sr[11]) },
3185 { "sr12", offsetof(CPUState, sr[12]) },
3186 { "sr13", offsetof(CPUState, sr[13]) },
3187 { "sr14", offsetof(CPUState, sr[14]) },
3188 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003189 /* Too lazy to put BATs... */
3190 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3191
3192 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3193 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3194 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3195 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3196 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3197 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3198 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3199 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3200 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3201 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3202 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3203 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3204 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3205 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3206 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3207 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3208 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3209 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3210 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3211 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3212 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3213 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3214 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3215 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3216 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3217 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3218 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3219 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3220 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3221 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3222 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3223 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3224 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3225 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3226 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3227 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3228 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3229 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3230 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3231 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3232 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3233 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3234 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3235 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3236 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3237 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3238 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3239 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3240 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3241 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3242 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3243 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3244 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3245 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3246 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3247 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3248 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3249 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3250 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3251 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3252 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3253 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3254 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3255 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3256 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3257 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3258
bellarde95c8d52004-09-30 22:22:08 +00003259#elif defined(TARGET_SPARC)
3260 { "g0", offsetof(CPUState, gregs[0]) },
3261 { "g1", offsetof(CPUState, gregs[1]) },
3262 { "g2", offsetof(CPUState, gregs[2]) },
3263 { "g3", offsetof(CPUState, gregs[3]) },
3264 { "g4", offsetof(CPUState, gregs[4]) },
3265 { "g5", offsetof(CPUState, gregs[5]) },
3266 { "g6", offsetof(CPUState, gregs[6]) },
3267 { "g7", offsetof(CPUState, gregs[7]) },
3268 { "o0", 0, monitor_get_reg },
3269 { "o1", 1, monitor_get_reg },
3270 { "o2", 2, monitor_get_reg },
3271 { "o3", 3, monitor_get_reg },
3272 { "o4", 4, monitor_get_reg },
3273 { "o5", 5, monitor_get_reg },
3274 { "o6", 6, monitor_get_reg },
3275 { "o7", 7, monitor_get_reg },
3276 { "l0", 8, monitor_get_reg },
3277 { "l1", 9, monitor_get_reg },
3278 { "l2", 10, monitor_get_reg },
3279 { "l3", 11, monitor_get_reg },
3280 { "l4", 12, monitor_get_reg },
3281 { "l5", 13, monitor_get_reg },
3282 { "l6", 14, monitor_get_reg },
3283 { "l7", 15, monitor_get_reg },
3284 { "i0", 16, monitor_get_reg },
3285 { "i1", 17, monitor_get_reg },
3286 { "i2", 18, monitor_get_reg },
3287 { "i3", 19, monitor_get_reg },
3288 { "i4", 20, monitor_get_reg },
3289 { "i5", 21, monitor_get_reg },
3290 { "i6", 22, monitor_get_reg },
3291 { "i7", 23, monitor_get_reg },
3292 { "pc", offsetof(CPUState, pc) },
3293 { "npc", offsetof(CPUState, npc) },
3294 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003295#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003296 { "psr", 0, &monitor_get_psr, },
3297 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003298#endif
bellarde95c8d52004-09-30 22:22:08 +00003299 { "tbr", offsetof(CPUState, tbr) },
3300 { "fsr", offsetof(CPUState, fsr) },
3301 { "f0", offsetof(CPUState, fpr[0]) },
3302 { "f1", offsetof(CPUState, fpr[1]) },
3303 { "f2", offsetof(CPUState, fpr[2]) },
3304 { "f3", offsetof(CPUState, fpr[3]) },
3305 { "f4", offsetof(CPUState, fpr[4]) },
3306 { "f5", offsetof(CPUState, fpr[5]) },
3307 { "f6", offsetof(CPUState, fpr[6]) },
3308 { "f7", offsetof(CPUState, fpr[7]) },
3309 { "f8", offsetof(CPUState, fpr[8]) },
3310 { "f9", offsetof(CPUState, fpr[9]) },
3311 { "f10", offsetof(CPUState, fpr[10]) },
3312 { "f11", offsetof(CPUState, fpr[11]) },
3313 { "f12", offsetof(CPUState, fpr[12]) },
3314 { "f13", offsetof(CPUState, fpr[13]) },
3315 { "f14", offsetof(CPUState, fpr[14]) },
3316 { "f15", offsetof(CPUState, fpr[15]) },
3317 { "f16", offsetof(CPUState, fpr[16]) },
3318 { "f17", offsetof(CPUState, fpr[17]) },
3319 { "f18", offsetof(CPUState, fpr[18]) },
3320 { "f19", offsetof(CPUState, fpr[19]) },
3321 { "f20", offsetof(CPUState, fpr[20]) },
3322 { "f21", offsetof(CPUState, fpr[21]) },
3323 { "f22", offsetof(CPUState, fpr[22]) },
3324 { "f23", offsetof(CPUState, fpr[23]) },
3325 { "f24", offsetof(CPUState, fpr[24]) },
3326 { "f25", offsetof(CPUState, fpr[25]) },
3327 { "f26", offsetof(CPUState, fpr[26]) },
3328 { "f27", offsetof(CPUState, fpr[27]) },
3329 { "f28", offsetof(CPUState, fpr[28]) },
3330 { "f29", offsetof(CPUState, fpr[29]) },
3331 { "f30", offsetof(CPUState, fpr[30]) },
3332 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003333#ifdef TARGET_SPARC64
3334 { "f32", offsetof(CPUState, fpr[32]) },
3335 { "f34", offsetof(CPUState, fpr[34]) },
3336 { "f36", offsetof(CPUState, fpr[36]) },
3337 { "f38", offsetof(CPUState, fpr[38]) },
3338 { "f40", offsetof(CPUState, fpr[40]) },
3339 { "f42", offsetof(CPUState, fpr[42]) },
3340 { "f44", offsetof(CPUState, fpr[44]) },
3341 { "f46", offsetof(CPUState, fpr[46]) },
3342 { "f48", offsetof(CPUState, fpr[48]) },
3343 { "f50", offsetof(CPUState, fpr[50]) },
3344 { "f52", offsetof(CPUState, fpr[52]) },
3345 { "f54", offsetof(CPUState, fpr[54]) },
3346 { "f56", offsetof(CPUState, fpr[56]) },
3347 { "f58", offsetof(CPUState, fpr[58]) },
3348 { "f60", offsetof(CPUState, fpr[60]) },
3349 { "f62", offsetof(CPUState, fpr[62]) },
3350 { "asi", offsetof(CPUState, asi) },
3351 { "pstate", offsetof(CPUState, pstate) },
3352 { "cansave", offsetof(CPUState, cansave) },
3353 { "canrestore", offsetof(CPUState, canrestore) },
3354 { "otherwin", offsetof(CPUState, otherwin) },
3355 { "wstate", offsetof(CPUState, wstate) },
3356 { "cleanwin", offsetof(CPUState, cleanwin) },
3357 { "fprs", offsetof(CPUState, fprs) },
3358#endif
bellard9307c4c2004-04-04 12:57:25 +00003359#endif
3360 { NULL },
3361};
3362
aliguori376253e2009-03-05 23:01:23 +00003363static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003364{
aliguori376253e2009-03-05 23:01:23 +00003365 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003366 longjmp(expr_env, 1);
3367}
3368
Markus Armbruster09b94182010-01-20 13:07:30 +01003369/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003370static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003371{
blueswir18662d652008-10-02 18:32:44 +00003372 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003373 void *ptr;
3374
bellard9307c4c2004-04-04 12:57:25 +00003375 for(md = monitor_defs; md->name != NULL; md++) {
3376 if (compare_cmd(name, md->name)) {
3377 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003378 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003379 } else {
bellard6a00d602005-11-21 23:25:50 +00003380 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003381 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003382 switch(md->type) {
3383 case MD_I32:
3384 *pval = *(int32_t *)ptr;
3385 break;
3386 case MD_TLONG:
3387 *pval = *(target_long *)ptr;
3388 break;
3389 default:
3390 *pval = 0;
3391 break;
3392 }
bellard9307c4c2004-04-04 12:57:25 +00003393 }
3394 return 0;
3395 }
3396 }
3397 return -1;
3398}
3399
3400static void next(void)
3401{
Blue Swirl660f11b2009-07-31 21:16:51 +00003402 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003403 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003404 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003405 pch++;
3406 }
3407}
3408
aliguori376253e2009-03-05 23:01:23 +00003409static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003410
aliguori376253e2009-03-05 23:01:23 +00003411static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003412{
blueswir1c2efc952007-09-25 17:28:42 +00003413 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003414 char *p;
bellard6a00d602005-11-21 23:25:50 +00003415 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003416
3417 switch(*pch) {
3418 case '+':
3419 next();
aliguori376253e2009-03-05 23:01:23 +00003420 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003421 break;
3422 case '-':
3423 next();
aliguori376253e2009-03-05 23:01:23 +00003424 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003425 break;
3426 case '~':
3427 next();
aliguori376253e2009-03-05 23:01:23 +00003428 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003429 break;
3430 case '(':
3431 next();
aliguori376253e2009-03-05 23:01:23 +00003432 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003433 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003434 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003435 }
3436 next();
3437 break;
bellard81d09122004-07-14 17:21:37 +00003438 case '\'':
3439 pch++;
3440 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003441 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003442 n = *pch;
3443 pch++;
3444 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003445 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003446 next();
3447 break;
bellard9307c4c2004-04-04 12:57:25 +00003448 case '$':
3449 {
3450 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003451 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003452
bellard9307c4c2004-04-04 12:57:25 +00003453 pch++;
3454 q = buf;
3455 while ((*pch >= 'a' && *pch <= 'z') ||
3456 (*pch >= 'A' && *pch <= 'Z') ||
3457 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003458 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003459 if ((q - buf) < sizeof(buf) - 1)
3460 *q++ = *pch;
3461 pch++;
3462 }
blueswir1cd390082008-11-16 13:53:32 +00003463 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003464 pch++;
3465 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003466 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003467 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003468 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003469 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003470 }
3471 break;
3472 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003473 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003474 n = 0;
3475 break;
3476 default:
blueswir17743e582007-09-24 18:39:04 +00003477#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003478 n = strtoull(pch, &p, 0);
3479#else
bellard9307c4c2004-04-04 12:57:25 +00003480 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003481#endif
bellard9307c4c2004-04-04 12:57:25 +00003482 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003483 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003484 }
3485 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003486 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003487 pch++;
3488 break;
3489 }
3490 return n;
3491}
3492
3493
aliguori376253e2009-03-05 23:01:23 +00003494static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003495{
blueswir1c2efc952007-09-25 17:28:42 +00003496 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003497 int op;
ths3b46e622007-09-17 08:09:54 +00003498
aliguori376253e2009-03-05 23:01:23 +00003499 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003500 for(;;) {
3501 op = *pch;
3502 if (op != '*' && op != '/' && op != '%')
3503 break;
3504 next();
aliguori376253e2009-03-05 23:01:23 +00003505 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003506 switch(op) {
3507 default:
3508 case '*':
3509 val *= val2;
3510 break;
3511 case '/':
3512 case '%':
ths5fafdf22007-09-16 21:08:06 +00003513 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003514 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003515 if (op == '/')
3516 val /= val2;
3517 else
3518 val %= val2;
3519 break;
3520 }
3521 }
3522 return val;
3523}
3524
aliguori376253e2009-03-05 23:01:23 +00003525static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003526{
blueswir1c2efc952007-09-25 17:28:42 +00003527 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003528 int op;
bellard9307c4c2004-04-04 12:57:25 +00003529
aliguori376253e2009-03-05 23:01:23 +00003530 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003531 for(;;) {
3532 op = *pch;
3533 if (op != '&' && op != '|' && op != '^')
3534 break;
3535 next();
aliguori376253e2009-03-05 23:01:23 +00003536 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003537 switch(op) {
3538 default:
3539 case '&':
3540 val &= val2;
3541 break;
3542 case '|':
3543 val |= val2;
3544 break;
3545 case '^':
3546 val ^= val2;
3547 break;
3548 }
3549 }
3550 return val;
3551}
3552
aliguori376253e2009-03-05 23:01:23 +00003553static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003554{
blueswir1c2efc952007-09-25 17:28:42 +00003555 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003556 int op;
bellard9307c4c2004-04-04 12:57:25 +00003557
aliguori376253e2009-03-05 23:01:23 +00003558 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003559 for(;;) {
3560 op = *pch;
3561 if (op != '+' && op != '-')
3562 break;
3563 next();
aliguori376253e2009-03-05 23:01:23 +00003564 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003565 if (op == '+')
3566 val += val2;
3567 else
3568 val -= val2;
3569 }
3570 return val;
3571}
3572
aliguori376253e2009-03-05 23:01:23 +00003573static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003574{
3575 pch = *pp;
3576 if (setjmp(expr_env)) {
3577 *pp = pch;
3578 return -1;
3579 }
blueswir1cd390082008-11-16 13:53:32 +00003580 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003581 pch++;
aliguori376253e2009-03-05 23:01:23 +00003582 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003583 *pp = pch;
3584 return 0;
3585}
3586
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003587static int get_double(Monitor *mon, double *pval, const char **pp)
3588{
3589 const char *p = *pp;
3590 char *tailp;
3591 double d;
3592
3593 d = strtod(p, &tailp);
3594 if (tailp == p) {
3595 monitor_printf(mon, "Number expected\n");
3596 return -1;
3597 }
3598 if (d != d || d - d != 0) {
3599 /* NaN or infinity */
3600 monitor_printf(mon, "Bad number\n");
3601 return -1;
3602 }
3603 *pval = d;
3604 *pp = tailp;
3605 return 0;
3606}
3607
bellard9307c4c2004-04-04 12:57:25 +00003608static int get_str(char *buf, int buf_size, const char **pp)
3609{
3610 const char *p;
3611 char *q;
3612 int c;
3613
bellard81d09122004-07-14 17:21:37 +00003614 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003615 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003616 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003617 p++;
3618 if (*p == '\0') {
3619 fail:
bellard81d09122004-07-14 17:21:37 +00003620 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003621 *pp = p;
3622 return -1;
3623 }
bellard9307c4c2004-04-04 12:57:25 +00003624 if (*p == '\"') {
3625 p++;
3626 while (*p != '\0' && *p != '\"') {
3627 if (*p == '\\') {
3628 p++;
3629 c = *p++;
3630 switch(c) {
3631 case 'n':
3632 c = '\n';
3633 break;
3634 case 'r':
3635 c = '\r';
3636 break;
3637 case '\\':
3638 case '\'':
3639 case '\"':
3640 break;
3641 default:
3642 qemu_printf("unsupported escape code: '\\%c'\n", c);
3643 goto fail;
3644 }
3645 if ((q - buf) < buf_size - 1) {
3646 *q++ = c;
3647 }
3648 } else {
3649 if ((q - buf) < buf_size - 1) {
3650 *q++ = *p;
3651 }
3652 p++;
3653 }
3654 }
3655 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003656 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003657 goto fail;
3658 }
3659 p++;
3660 } else {
blueswir1cd390082008-11-16 13:53:32 +00003661 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003662 if ((q - buf) < buf_size - 1) {
3663 *q++ = *p;
3664 }
3665 p++;
3666 }
bellard9307c4c2004-04-04 12:57:25 +00003667 }
bellard81d09122004-07-14 17:21:37 +00003668 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003669 *pp = p;
3670 return 0;
3671}
3672
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003673/*
3674 * Store the command-name in cmdname, and return a pointer to
3675 * the remaining of the command string.
3676 */
3677static const char *get_command_name(const char *cmdline,
3678 char *cmdname, size_t nlen)
3679{
3680 size_t len;
3681 const char *p, *pstart;
3682
3683 p = cmdline;
3684 while (qemu_isspace(*p))
3685 p++;
3686 if (*p == '\0')
3687 return NULL;
3688 pstart = p;
3689 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3690 p++;
3691 len = p - pstart;
3692 if (len > nlen - 1)
3693 len = nlen - 1;
3694 memcpy(cmdname, pstart, len);
3695 cmdname[len] = '\0';
3696 return p;
3697}
3698
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003699/**
3700 * Read key of 'type' into 'key' and return the current
3701 * 'type' pointer.
3702 */
3703static char *key_get_info(const char *type, char **key)
3704{
3705 size_t len;
3706 char *p, *str;
3707
3708 if (*type == ',')
3709 type++;
3710
3711 p = strchr(type, ':');
3712 if (!p) {
3713 *key = NULL;
3714 return NULL;
3715 }
3716 len = p - type;
3717
Anthony Liguori7267c092011-08-20 22:09:37 -05003718 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003719 memcpy(str, type, len);
3720 str[len] = '\0';
3721
3722 *key = str;
3723 return ++p;
3724}
3725
bellard9307c4c2004-04-04 12:57:25 +00003726static int default_fmt_format = 'x';
3727static int default_fmt_size = 4;
3728
3729#define MAX_ARGS 16
3730
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003731static int is_valid_option(const char *c, const char *typestr)
3732{
3733 char option[3];
3734
3735 option[0] = '-';
3736 option[1] = *c;
3737 option[2] = '\0';
3738
3739 typestr = strstr(typestr, option);
3740 return (typestr != NULL);
3741}
3742
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003743static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3744 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003745{
3746 const mon_cmd_t *cmd;
3747
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003748 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003749 if (compare_cmd(cmdname, cmd->name)) {
3750 return cmd;
3751 }
3752 }
3753
3754 return NULL;
3755}
3756
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003757static const mon_cmd_t *monitor_find_command(const char *cmdname)
3758{
3759 return search_dispatch_table(mon_cmds, cmdname);
3760}
3761
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003762static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3763{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003764 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003765}
3766
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003767static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3768{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003769 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003770}
3771
Anthony Liguoric227f092009-10-01 16:12:16 -05003772static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003773 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003774 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003775{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003776 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003777 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003778 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003779 char cmdname[256];
3780 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003781 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003782
3783#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003784 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003785#endif
ths3b46e622007-09-17 08:09:54 +00003786
bellard9307c4c2004-04-04 12:57:25 +00003787 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003788 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3789 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003790 return NULL;
ths3b46e622007-09-17 08:09:54 +00003791
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003792 cmd = monitor_find_command(cmdname);
3793 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003794 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003795 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003796 }
bellard9307c4c2004-04-04 12:57:25 +00003797
bellard9307c4c2004-04-04 12:57:25 +00003798 /* parse the parameters */
3799 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003800 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003801 typestr = key_get_info(typestr, &key);
3802 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003803 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003804 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003805 typestr++;
3806 switch(c) {
3807 case 'F':
bellard81d09122004-07-14 17:21:37 +00003808 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003809 case 's':
3810 {
3811 int ret;
ths3b46e622007-09-17 08:09:54 +00003812
blueswir1cd390082008-11-16 13:53:32 +00003813 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003814 p++;
3815 if (*typestr == '?') {
3816 typestr++;
3817 if (*p == '\0') {
3818 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003819 break;
bellard9307c4c2004-04-04 12:57:25 +00003820 }
3821 }
3822 ret = get_str(buf, sizeof(buf), &p);
3823 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003824 switch(c) {
3825 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003826 monitor_printf(mon, "%s: filename expected\n",
3827 cmdname);
bellard81d09122004-07-14 17:21:37 +00003828 break;
3829 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003830 monitor_printf(mon, "%s: block device name expected\n",
3831 cmdname);
bellard81d09122004-07-14 17:21:37 +00003832 break;
3833 default:
aliguori376253e2009-03-05 23:01:23 +00003834 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003835 break;
3836 }
bellard9307c4c2004-04-04 12:57:25 +00003837 goto fail;
3838 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003839 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003840 }
3841 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003842 case 'O':
3843 {
3844 QemuOptsList *opts_list;
3845 QemuOpts *opts;
3846
3847 opts_list = qemu_find_opts(key);
3848 if (!opts_list || opts_list->desc->name) {
3849 goto bad_type;
3850 }
3851 while (qemu_isspace(*p)) {
3852 p++;
3853 }
3854 if (!*p)
3855 break;
3856 if (get_str(buf, sizeof(buf), &p) < 0) {
3857 goto fail;
3858 }
3859 opts = qemu_opts_parse(opts_list, buf, 1);
3860 if (!opts) {
3861 goto fail;
3862 }
3863 qemu_opts_to_qdict(opts, qdict);
3864 qemu_opts_del(opts);
3865 }
3866 break;
bellard9307c4c2004-04-04 12:57:25 +00003867 case '/':
3868 {
3869 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003870
blueswir1cd390082008-11-16 13:53:32 +00003871 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003872 p++;
3873 if (*p == '/') {
3874 /* format found */
3875 p++;
3876 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003877 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003878 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003879 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003880 count = count * 10 + (*p - '0');
3881 p++;
3882 }
3883 }
3884 size = -1;
3885 format = -1;
3886 for(;;) {
3887 switch(*p) {
3888 case 'o':
3889 case 'd':
3890 case 'u':
3891 case 'x':
3892 case 'i':
3893 case 'c':
3894 format = *p++;
3895 break;
3896 case 'b':
3897 size = 1;
3898 p++;
3899 break;
3900 case 'h':
3901 size = 2;
3902 p++;
3903 break;
3904 case 'w':
3905 size = 4;
3906 p++;
3907 break;
3908 case 'g':
3909 case 'L':
3910 size = 8;
3911 p++;
3912 break;
3913 default:
3914 goto next;
3915 }
3916 }
3917 next:
blueswir1cd390082008-11-16 13:53:32 +00003918 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003919 monitor_printf(mon, "invalid char in format: '%c'\n",
3920 *p);
bellard9307c4c2004-04-04 12:57:25 +00003921 goto fail;
3922 }
bellard9307c4c2004-04-04 12:57:25 +00003923 if (format < 0)
3924 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003925 if (format != 'i') {
3926 /* for 'i', not specifying a size gives -1 as size */
3927 if (size < 0)
3928 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003929 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003930 }
bellard9307c4c2004-04-04 12:57:25 +00003931 default_fmt_format = format;
3932 } else {
3933 count = 1;
3934 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003935 if (format != 'i') {
3936 size = default_fmt_size;
3937 } else {
3938 size = -1;
3939 }
bellard9307c4c2004-04-04 12:57:25 +00003940 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003941 qdict_put(qdict, "count", qint_from_int(count));
3942 qdict_put(qdict, "format", qint_from_int(format));
3943 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003944 }
3945 break;
3946 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003947 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003948 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003949 {
blueswir1c2efc952007-09-25 17:28:42 +00003950 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003951
blueswir1cd390082008-11-16 13:53:32 +00003952 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003953 p++;
bellard34405572004-06-08 00:55:58 +00003954 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003955 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003956 if (*p == '\0') {
3957 typestr++;
3958 break;
3959 }
bellard34405572004-06-08 00:55:58 +00003960 } else {
3961 if (*p == '.') {
3962 p++;
blueswir1cd390082008-11-16 13:53:32 +00003963 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003964 p++;
bellard34405572004-06-08 00:55:58 +00003965 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003966 typestr++;
3967 break;
bellard34405572004-06-08 00:55:58 +00003968 }
3969 }
bellard13224a82006-07-14 22:03:35 +00003970 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003971 }
aliguori376253e2009-03-05 23:01:23 +00003972 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003973 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003974 /* Check if 'i' is greater than 32-bit */
3975 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3976 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3977 monitor_printf(mon, "integer is for 32-bit values\n");
3978 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003979 } else if (c == 'M') {
3980 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003981 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003982 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003983 }
3984 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003985 case 'o':
3986 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003987 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003988 char *end;
3989
3990 while (qemu_isspace(*p)) {
3991 p++;
3992 }
3993 if (*typestr == '?') {
3994 typestr++;
3995 if (*p == '\0') {
3996 break;
3997 }
3998 }
3999 val = strtosz(p, &end);
4000 if (val < 0) {
4001 monitor_printf(mon, "invalid size\n");
4002 goto fail;
4003 }
4004 qdict_put(qdict, key, qint_from_int(val));
4005 p = end;
4006 }
4007 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004008 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004009 {
4010 double val;
4011
4012 while (qemu_isspace(*p))
4013 p++;
4014 if (*typestr == '?') {
4015 typestr++;
4016 if (*p == '\0') {
4017 break;
4018 }
4019 }
4020 if (get_double(mon, &val, &p) < 0) {
4021 goto fail;
4022 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004023 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004024 switch (*p) {
4025 case 'm':
4026 val /= 1e3; p += 2; break;
4027 case 'u':
4028 val /= 1e6; p += 2; break;
4029 case 'n':
4030 val /= 1e9; p += 2; break;
4031 }
4032 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004033 if (*p && !qemu_isspace(*p)) {
4034 monitor_printf(mon, "Unknown unit suffix\n");
4035 goto fail;
4036 }
4037 qdict_put(qdict, key, qfloat_from_double(val));
4038 }
4039 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004040 case 'b':
4041 {
4042 const char *beg;
4043 int val;
4044
4045 while (qemu_isspace(*p)) {
4046 p++;
4047 }
4048 beg = p;
4049 while (qemu_isgraph(*p)) {
4050 p++;
4051 }
4052 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4053 val = 1;
4054 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4055 val = 0;
4056 } else {
4057 monitor_printf(mon, "Expected 'on' or 'off'\n");
4058 goto fail;
4059 }
4060 qdict_put(qdict, key, qbool_from_int(val));
4061 }
4062 break;
bellard9307c4c2004-04-04 12:57:25 +00004063 case '-':
4064 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004065 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004066 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004067 /* option */
ths3b46e622007-09-17 08:09:54 +00004068
bellard9307c4c2004-04-04 12:57:25 +00004069 c = *typestr++;
4070 if (c == '\0')
4071 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004072 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004073 p++;
bellard9307c4c2004-04-04 12:57:25 +00004074 if (*p == '-') {
4075 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004076 if(c != *p) {
4077 if(!is_valid_option(p, typestr)) {
4078
4079 monitor_printf(mon, "%s: unsupported option -%c\n",
4080 cmdname, *p);
4081 goto fail;
4082 } else {
4083 skip_key = 1;
4084 }
bellard9307c4c2004-04-04 12:57:25 +00004085 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004086 if(skip_key) {
4087 p = tmp;
4088 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004089 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004090 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004091 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004092 }
bellard9307c4c2004-04-04 12:57:25 +00004093 }
bellard9307c4c2004-04-04 12:57:25 +00004094 }
4095 break;
4096 default:
4097 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004098 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004099 goto fail;
4100 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004101 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004102 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004103 }
4104 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004105 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004106 p++;
4107 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004108 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4109 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004110 goto fail;
4111 }
4112
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004113 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004114
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004115fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004116 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004117 return NULL;
4118}
4119
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004120void monitor_set_error(Monitor *mon, QError *qerror)
4121{
4122 /* report only the first error */
4123 if (!mon->error) {
4124 mon->error = qerror;
4125 } else {
4126 MON_DEBUG("Additional error report at %s:%d\n",
4127 qerror->file, qerror->linenr);
4128 QDECREF(qerror);
4129 }
4130}
4131
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004132static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4133{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004134 if (ret && !monitor_has_error(mon)) {
4135 /*
4136 * If it returns failure, it must have passed on error.
4137 *
4138 * Action: Report an internal error to the client if in QMP.
4139 */
4140 qerror_report(QERR_UNDEFINED_ERROR);
4141 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4142 cmd->name);
4143 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004144
4145#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004146 if (!ret && monitor_has_error(mon)) {
4147 /*
4148 * If it returns success, it must not have passed an error.
4149 *
4150 * Action: Report the passed error to the client.
4151 */
4152 MON_DEBUG("command '%s' returned success but passed an error\n",
4153 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004154 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004155
4156 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4157 /*
4158 * Handlers should not call Monitor print functions.
4159 *
4160 * Action: Ignore them in QMP.
4161 *
4162 * (XXX: we don't check any 'info' or 'query' command here
4163 * because the user print function _is_ called by do_info(), hence
4164 * we will trigger this check. This problem will go away when we
4165 * make 'query' commands real and kill do_info())
4166 */
4167 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4168 cmd->name, mon_print_count_get(mon));
4169 }
4170#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004171}
4172
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004173static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004174{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004175 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004176 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004177
4178 qdict = qdict_new();
4179
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004180 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004181 if (!cmd)
4182 goto out;
4183
Luiz Capitulino4903de02010-09-16 11:01:32 -03004184 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004185 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004186 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004187 QObject *data = NULL;
4188
4189 /* XXX: ignores the error code */
4190 cmd->mhandler.cmd_new(mon, qdict, &data);
4191 assert(!monitor_has_error(mon));
4192 if (data) {
4193 cmd->user_print(mon, data);
4194 qobject_decref(data);
4195 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004196 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004197 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004198 }
4199
Luiz Capitulino13917be2009-10-07 13:41:54 -03004200out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004201 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004202}
4203
bellard81d09122004-07-14 17:21:37 +00004204static void cmd_completion(const char *name, const char *list)
4205{
4206 const char *p, *pstart;
4207 char cmd[128];
4208 int len;
4209
4210 p = list;
4211 for(;;) {
4212 pstart = p;
4213 p = strchr(p, '|');
4214 if (!p)
4215 p = pstart + strlen(pstart);
4216 len = p - pstart;
4217 if (len > sizeof(cmd) - 2)
4218 len = sizeof(cmd) - 2;
4219 memcpy(cmd, pstart, len);
4220 cmd[len] = '\0';
4221 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004222 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004223 }
4224 if (*p == '\0')
4225 break;
4226 p++;
4227 }
4228}
4229
4230static void file_completion(const char *input)
4231{
4232 DIR *ffs;
4233 struct dirent *d;
4234 char path[1024];
4235 char file[1024], file_prefix[1024];
4236 int input_path_len;
4237 const char *p;
4238
ths5fafdf22007-09-16 21:08:06 +00004239 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004240 if (!p) {
4241 input_path_len = 0;
4242 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004243 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004244 } else {
4245 input_path_len = p - input + 1;
4246 memcpy(path, input, input_path_len);
4247 if (input_path_len > sizeof(path) - 1)
4248 input_path_len = sizeof(path) - 1;
4249 path[input_path_len] = '\0';
4250 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4251 }
4252#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004253 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4254 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004255#endif
4256 ffs = opendir(path);
4257 if (!ffs)
4258 return;
4259 for(;;) {
4260 struct stat sb;
4261 d = readdir(ffs);
4262 if (!d)
4263 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004264
4265 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4266 continue;
4267 }
4268
bellard81d09122004-07-14 17:21:37 +00004269 if (strstart(d->d_name, file_prefix, NULL)) {
4270 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004271 if (input_path_len < sizeof(file))
4272 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4273 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004274 /* stat the file to find out if it's a directory.
4275 * In that case add a slash to speed up typing long paths
4276 */
4277 stat(file, &sb);
4278 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004279 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004280 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004281 }
4282 }
4283 closedir(ffs);
4284}
4285
aliguori51de9762009-03-05 23:00:43 +00004286static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004287{
aliguori51de9762009-03-05 23:00:43 +00004288 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004289 const char *input = opaque;
4290
4291 if (input[0] == '\0' ||
4292 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004293 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004294 }
4295}
4296
4297/* NOTE: this parser is an approximate form of the real command parser */
4298static void parse_cmdline(const char *cmdline,
4299 int *pnb_args, char **args)
4300{
4301 const char *p;
4302 int nb_args, ret;
4303 char buf[1024];
4304
4305 p = cmdline;
4306 nb_args = 0;
4307 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004308 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004309 p++;
4310 if (*p == '\0')
4311 break;
4312 if (nb_args >= MAX_ARGS)
4313 break;
4314 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004315 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004316 nb_args++;
4317 if (ret < 0)
4318 break;
4319 }
4320 *pnb_args = nb_args;
4321}
4322
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004323static const char *next_arg_type(const char *typestr)
4324{
4325 const char *p = strchr(typestr, ':');
4326 return (p != NULL ? ++p : typestr);
4327}
4328
aliguori4c36ba32009-03-05 23:01:37 +00004329static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004330{
4331 const char *cmdname;
4332 char *args[MAX_ARGS];
4333 int nb_args, i, len;
4334 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004335 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004336 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004337
4338 parse_cmdline(cmdline, &nb_args, args);
4339#ifdef DEBUG_COMPLETION
4340 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004341 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004342 }
4343#endif
4344
4345 /* if the line ends with a space, it means we want to complete the
4346 next arg */
4347 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004348 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004349 if (nb_args >= MAX_ARGS) {
4350 goto cleanup;
4351 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004352 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004353 }
4354 if (nb_args <= 1) {
4355 /* command completion */
4356 if (nb_args == 0)
4357 cmdname = "";
4358 else
4359 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004360 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004361 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004362 cmd_completion(cmdname, cmd->name);
4363 }
4364 } else {
4365 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004366 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4367 if (compare_cmd(args[0], cmd->name)) {
4368 break;
4369 }
bellard81d09122004-07-14 17:21:37 +00004370 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004371 if (!cmd->name) {
4372 goto cleanup;
4373 }
4374
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004375 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004376 for(i = 0; i < nb_args - 2; i++) {
4377 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004378 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004379 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004380 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004381 }
4382 }
4383 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004384 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004385 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004386 }
bellard81d09122004-07-14 17:21:37 +00004387 switch(*ptype) {
4388 case 'F':
4389 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004390 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004391 file_completion(str);
4392 break;
4393 case 'B':
4394 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004395 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004396 bdrv_iterate(block_completion_it, (void *)str);
4397 break;
bellard7fe48482004-10-09 18:08:01 +00004398 case 's':
4399 /* XXX: more generic ? */
4400 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004401 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004402 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4403 cmd_completion(str, cmd->name);
4404 }
bellard64866c32006-05-07 18:03:31 +00004405 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004406 char *sep = strrchr(str, '-');
4407 if (sep)
4408 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004409 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004410 for(key = key_defs; key->name != NULL; key++) {
4411 cmd_completion(str, key->name);
4412 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004413 } else if (!strcmp(cmd->name, "help|?")) {
4414 readline_set_completion_index(cur_mon->rs, strlen(str));
4415 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4416 cmd_completion(str, cmd->name);
4417 }
bellard7fe48482004-10-09 18:08:01 +00004418 }
4419 break;
bellard81d09122004-07-14 17:21:37 +00004420 default:
4421 break;
4422 }
4423 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004424
4425cleanup:
4426 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004427 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004428 }
bellard81d09122004-07-14 17:21:37 +00004429}
4430
aliguori731b0362009-03-05 23:01:42 +00004431static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004432{
aliguori731b0362009-03-05 23:01:42 +00004433 Monitor *mon = opaque;
4434
Jan Kiszkac62313b2009-12-04 14:05:29 +01004435 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004436}
4437
Luiz Capitulino09069b12010-02-04 18:10:06 -02004438static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4439{
4440 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4441 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4442}
4443
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004444/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004445 * Argument validation rules:
4446 *
4447 * 1. The argument must exist in cmd_args qdict
4448 * 2. The argument type must be the expected one
4449 *
4450 * Special case: If the argument doesn't exist in cmd_args and
4451 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4452 * checking is skipped for it.
4453 */
4454static int check_client_args_type(const QDict *client_args,
4455 const QDict *cmd_args, int flags)
4456{
4457 const QDictEntry *ent;
4458
4459 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4460 QObject *obj;
4461 QString *arg_type;
4462 const QObject *client_arg = qdict_entry_value(ent);
4463 const char *client_arg_name = qdict_entry_key(ent);
4464
4465 obj = qdict_get(cmd_args, client_arg_name);
4466 if (!obj) {
4467 if (flags & QMP_ACCEPT_UNKNOWNS) {
4468 /* handler accepts unknowns */
4469 continue;
4470 }
4471 /* client arg doesn't exist */
4472 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4473 return -1;
4474 }
4475
4476 arg_type = qobject_to_qstring(obj);
4477 assert(arg_type != NULL);
4478
4479 /* check if argument's type is correct */
4480 switch (qstring_get_str(arg_type)[0]) {
4481 case 'F':
4482 case 'B':
4483 case 's':
4484 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4485 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4486 "string");
4487 return -1;
4488 }
4489 break;
4490 case 'i':
4491 case 'l':
4492 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004493 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004494 if (qobject_type(client_arg) != QTYPE_QINT) {
4495 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4496 "int");
4497 return -1;
4498 }
4499 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004500 case 'T':
4501 if (qobject_type(client_arg) != QTYPE_QINT &&
4502 qobject_type(client_arg) != QTYPE_QFLOAT) {
4503 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4504 "number");
4505 return -1;
4506 }
4507 break;
4508 case 'b':
4509 case '-':
4510 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4511 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4512 "bool");
4513 return -1;
4514 }
4515 break;
4516 case 'O':
4517 assert(flags & QMP_ACCEPT_UNKNOWNS);
4518 break;
4519 case '/':
4520 case '.':
4521 /*
4522 * These types are not supported by QMP and thus are not
4523 * handled here. Fall through.
4524 */
4525 default:
4526 abort();
4527 }
4528 }
4529
4530 return 0;
4531}
4532
4533/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004534 * - Check if the client has passed all mandatory args
4535 * - Set special flags for argument validation
4536 */
4537static int check_mandatory_args(const QDict *cmd_args,
4538 const QDict *client_args, int *flags)
4539{
4540 const QDictEntry *ent;
4541
4542 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4543 const char *cmd_arg_name = qdict_entry_key(ent);
4544 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4545 assert(type != NULL);
4546
4547 if (qstring_get_str(type)[0] == 'O') {
4548 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4549 *flags |= QMP_ACCEPT_UNKNOWNS;
4550 } else if (qstring_get_str(type)[0] != '-' &&
4551 qstring_get_str(type)[1] != '?' &&
4552 !qdict_haskey(client_args, cmd_arg_name)) {
4553 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4554 return -1;
4555 }
4556 }
4557
4558 return 0;
4559}
4560
4561static QDict *qdict_from_args_type(const char *args_type)
4562{
4563 int i;
4564 QDict *qdict;
4565 QString *key, *type, *cur_qs;
4566
4567 assert(args_type != NULL);
4568
4569 qdict = qdict_new();
4570
4571 if (args_type == NULL || args_type[0] == '\0') {
4572 /* no args, empty qdict */
4573 goto out;
4574 }
4575
4576 key = qstring_new();
4577 type = qstring_new();
4578
4579 cur_qs = key;
4580
4581 for (i = 0;; i++) {
4582 switch (args_type[i]) {
4583 case ',':
4584 case '\0':
4585 qdict_put(qdict, qstring_get_str(key), type);
4586 QDECREF(key);
4587 if (args_type[i] == '\0') {
4588 goto out;
4589 }
4590 type = qstring_new(); /* qdict has ref */
4591 cur_qs = key = qstring_new();
4592 break;
4593 case ':':
4594 cur_qs = type;
4595 break;
4596 default:
4597 qstring_append_chr(cur_qs, args_type[i]);
4598 break;
4599 }
4600 }
4601
4602out:
4603 return qdict;
4604}
4605
4606/*
4607 * Client argument checking rules:
4608 *
4609 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004610 * 2. Each argument provided by the client must be expected
4611 * 3. Each argument provided by the client must have the type expected
4612 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004613 */
4614static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4615{
4616 int flags, err;
4617 QDict *cmd_args;
4618
4619 cmd_args = qdict_from_args_type(cmd->args_type);
4620
4621 flags = 0;
4622 err = check_mandatory_args(cmd_args, client_args, &flags);
4623 if (err) {
4624 goto out;
4625 }
4626
Luiz Capitulino4af91932010-06-22 11:44:05 -03004627 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004628
4629out:
4630 QDECREF(cmd_args);
4631 return err;
4632}
4633
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004634/*
4635 * Input object checking rules
4636 *
4637 * 1. Input object must be a dict
4638 * 2. The "execute" key must exist
4639 * 3. The "execute" key must be a string
4640 * 4. If the "arguments" key exists, it must be a dict
4641 * 5. If the "id" key exists, it can be anything (ie. json-value)
4642 * 6. Any argument not listed above is considered invalid
4643 */
4644static QDict *qmp_check_input_obj(QObject *input_obj)
4645{
4646 const QDictEntry *ent;
4647 int has_exec_key = 0;
4648 QDict *input_dict;
4649
4650 if (qobject_type(input_obj) != QTYPE_QDICT) {
4651 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4652 return NULL;
4653 }
4654
4655 input_dict = qobject_to_qdict(input_obj);
4656
4657 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4658 const char *arg_name = qdict_entry_key(ent);
4659 const QObject *arg_obj = qdict_entry_value(ent);
4660
4661 if (!strcmp(arg_name, "execute")) {
4662 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4663 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4664 "string");
4665 return NULL;
4666 }
4667 has_exec_key = 1;
4668 } else if (!strcmp(arg_name, "arguments")) {
4669 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4670 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4671 "object");
4672 return NULL;
4673 }
4674 } else if (!strcmp(arg_name, "id")) {
4675 /* FIXME: check duplicated IDs for async commands */
4676 } else {
4677 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4678 return NULL;
4679 }
4680 }
4681
4682 if (!has_exec_key) {
4683 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4684 return NULL;
4685 }
4686
4687 return input_dict;
4688}
4689
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004690static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4691{
4692 QObject *ret_data = NULL;
4693
Luiz Capitulino4903de02010-09-16 11:01:32 -03004694 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004695 qmp_async_info_handler(mon, cmd);
4696 if (monitor_has_error(mon)) {
4697 monitor_protocol_emitter(mon, NULL);
4698 }
4699 } else {
4700 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004701 monitor_protocol_emitter(mon, ret_data);
4702 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004703 }
4704}
4705
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004706static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4707 const QDict *params)
4708{
4709 int ret;
4710 QObject *data = NULL;
4711
4712 mon_print_count_init(mon);
4713
4714 ret = cmd->mhandler.cmd_new(mon, params, &data);
4715 handler_audit(mon, cmd, ret);
4716 monitor_protocol_emitter(mon, data);
4717 qobject_decref(data);
4718}
4719
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004720static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4721{
4722 int err;
4723 QObject *obj;
4724 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004725 const mon_cmd_t *cmd;
4726 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004727 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004728
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004729 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004730 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004731
4732 obj = json_parser_parse(tokens, NULL);
4733 if (!obj) {
4734 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004735 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004736 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004737 }
4738
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004739 input = qmp_check_input_obj(obj);
4740 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004741 qobject_decref(obj);
4742 goto err_out;
4743 }
4744
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004745 mon->mc->id = qdict_get(input, "id");
4746 qobject_incref(mon->mc->id);
4747
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004748 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004749 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004750 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004751 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004752 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004753 }
4754
Anthony Liguorie3193602011-09-02 12:34:47 -05004755 cmd = qmp_find_cmd(cmd_name);
4756 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004757 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004758 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004759 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004760 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004761 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004762 }
4763
4764 obj = qdict_get(input, "arguments");
4765 if (!obj) {
4766 args = qdict_new();
4767 } else {
4768 args = qobject_to_qdict(obj);
4769 QINCREF(args);
4770 }
4771
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004772 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004773 if (err < 0) {
4774 goto err_out;
4775 }
4776
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004777 if (query_cmd) {
4778 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004779 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004780 err = qmp_async_cmd_handler(mon, cmd, args);
4781 if (err) {
4782 /* emit the error response */
4783 goto err_out;
4784 }
Adam Litke940cc302010-01-25 12:18:44 -06004785 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004786 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004787 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004788
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004789 goto out;
4790
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004791err_out:
4792 monitor_protocol_emitter(mon, NULL);
4793out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004794 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004795 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004796}
4797
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004798/**
4799 * monitor_control_read(): Read and handle QMP input
4800 */
4801static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4802{
4803 Monitor *old_mon = cur_mon;
4804
4805 cur_mon = opaque;
4806
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004807 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004808
4809 cur_mon = old_mon;
4810}
4811
aliguori731b0362009-03-05 23:01:42 +00004812static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004813{
aliguori731b0362009-03-05 23:01:42 +00004814 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004815 int i;
aliguori376253e2009-03-05 23:01:23 +00004816
aliguori731b0362009-03-05 23:01:42 +00004817 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004818
aliguoricde76ee2009-03-05 23:01:51 +00004819 if (cur_mon->rs) {
4820 for (i = 0; i < size; i++)
4821 readline_handle_byte(cur_mon->rs, buf[i]);
4822 } else {
4823 if (size == 0 || buf[size - 1] != 0)
4824 monitor_printf(cur_mon, "corrupted command\n");
4825 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004826 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004827 }
aliguori731b0362009-03-05 23:01:42 +00004828
4829 cur_mon = old_mon;
4830}
aliguorid8f44602008-10-06 13:52:44 +00004831
aliguori376253e2009-03-05 23:01:23 +00004832static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004833{
aliguori731b0362009-03-05 23:01:42 +00004834 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004835 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004836 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004837}
4838
aliguoricde76ee2009-03-05 23:01:51 +00004839int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004840{
aliguoricde76ee2009-03-05 23:01:51 +00004841 if (!mon->rs)
4842 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004843 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004844 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004845}
4846
aliguori376253e2009-03-05 23:01:23 +00004847void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004848{
aliguoricde76ee2009-03-05 23:01:51 +00004849 if (!mon->rs)
4850 return;
aliguori731b0362009-03-05 23:01:42 +00004851 if (--mon->suspend_cnt == 0)
4852 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004853}
4854
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004855static QObject *get_qmp_greeting(void)
4856{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004857 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004858
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004859 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004860 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4861}
4862
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004863/**
4864 * monitor_control_event(): Print QMP gretting
4865 */
4866static void monitor_control_event(void *opaque, int event)
4867{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004868 QObject *data;
4869 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004870
Luiz Capitulino47116d12010-02-08 17:01:30 -02004871 switch (event) {
4872 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004873 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004874 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004875 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004876 monitor_json_emitter(mon, data);
4877 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004878 break;
4879 case CHR_EVENT_CLOSED:
4880 json_message_parser_destroy(&mon->mc->parser);
4881 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004882 }
4883}
4884
aliguori731b0362009-03-05 23:01:42 +00004885static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004886{
aliguori376253e2009-03-05 23:01:23 +00004887 Monitor *mon = opaque;
4888
aliguori2724b182009-03-05 23:01:47 +00004889 switch (event) {
4890 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004891 mon->mux_out = 0;
4892 if (mon->reset_seen) {
4893 readline_restart(mon->rs);
4894 monitor_resume(mon);
4895 monitor_flush(mon);
4896 } else {
4897 mon->suspend_cnt = 0;
4898 }
aliguori2724b182009-03-05 23:01:47 +00004899 break;
ths86e94de2007-01-05 22:01:59 +00004900
aliguori2724b182009-03-05 23:01:47 +00004901 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004902 if (mon->reset_seen) {
4903 if (mon->suspend_cnt == 0) {
4904 monitor_printf(mon, "\n");
4905 }
4906 monitor_flush(mon);
4907 monitor_suspend(mon);
4908 } else {
4909 mon->suspend_cnt++;
4910 }
4911 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004912 break;
4913
Amit Shahb6b8df52009-10-07 18:31:16 +05304914 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004915 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4916 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004917 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004918 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004919 }
4920 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004921 break;
4922 }
ths86e94de2007-01-05 22:01:59 +00004923}
4924
aliguori76655d62009-03-06 20:27:37 +00004925
4926/*
4927 * Local variables:
4928 * c-indent-level: 4
4929 * c-basic-offset: 4
4930 * tab-width: 8
4931 * End:
4932 */
4933
aliguori731b0362009-03-05 23:01:42 +00004934void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004935{
aliguori731b0362009-03-05 23:01:42 +00004936 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004937 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004938
4939 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004940 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004941 is_first_init = 0;
4942 }
aliguori87127162009-03-05 23:01:29 +00004943
Anthony Liguori7267c092011-08-20 22:09:37 -05004944 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004945
aliguori87127162009-03-05 23:01:29 +00004946 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004947 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004948 if (flags & MONITOR_USE_READLINE) {
4949 mon->rs = readline_init(mon, monitor_find_completion);
4950 monitor_read_command(mon, 0);
4951 }
aliguori87127162009-03-05 23:01:29 +00004952
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004953 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004954 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004955 /* Control mode requires special handlers */
4956 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4957 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004958 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004959 } else {
4960 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4961 monitor_event, mon);
4962 }
aliguori87127162009-03-05 23:01:29 +00004963
Blue Swirl72cf2d42009-09-12 07:36:22 +00004964 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004965 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4966 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004967}
4968
aliguori376253e2009-03-05 23:01:23 +00004969static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004970{
aliguoribb5fc202009-03-05 23:01:15 +00004971 BlockDriverState *bs = opaque;
4972 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004973
aliguoribb5fc202009-03-05 23:01:15 +00004974 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004975 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004976 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004977 }
aliguori731b0362009-03-05 23:01:42 +00004978 if (mon->password_completion_cb)
4979 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004980
aliguori731b0362009-03-05 23:01:42 +00004981 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004982}
aliguoric0f4ce72009-03-05 23:01:01 +00004983
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004984int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4985 BlockDriverCompletionFunc *completion_cb,
4986 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004987{
aliguoricde76ee2009-03-05 23:01:51 +00004988 int err;
4989
aliguoribb5fc202009-03-05 23:01:15 +00004990 if (!bdrv_key_required(bs)) {
4991 if (completion_cb)
4992 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004993 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004994 }
aliguoric0f4ce72009-03-05 23:01:01 +00004995
Luiz Capitulino94171e12009-12-07 21:37:00 +01004996 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004997 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004998 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004999 }
5000
aliguori376253e2009-03-05 23:01:23 +00005001 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5002 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005003
aliguori731b0362009-03-05 23:01:42 +00005004 mon->password_completion_cb = completion_cb;
5005 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005006
aliguoricde76ee2009-03-05 23:01:51 +00005007 err = monitor_read_password(mon, bdrv_password_cb, bs);
5008
5009 if (err && completion_cb)
5010 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005011
5012 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005013}