blob: a1b46b3c62026ae4e3077bb833ac1f8cb1f83cfa [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 Capitulinoaf4ce882009-10-07 13:41:52 -0300126 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200127 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600128 int (*cmd_async)(Monitor *mon, const QDict *params,
129 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300130 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500131 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200132 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200143typedef struct MonitorControl {
144 QObject *id;
145 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200146 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200147} MonitorControl;
148
aliguori87127162009-03-05 23:01:29 +0000149struct Monitor {
150 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200151 int mux_out;
152 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000153 int flags;
154 int suspend_cnt;
155 uint8_t outbuf[1024];
156 int outbuf_index;
157 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200158 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000159 CPUState *mon_cpu;
160 BlockDriverCompletionFunc *password_completion_cb;
161 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200162#ifdef CONFIG_DEBUG_MONITOR
163 int print_calls_nr;
164#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200165 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500166 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000167 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000168};
169
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200170#ifdef CONFIG_DEBUG_MONITOR
171#define MON_DEBUG(fmt, ...) do { \
172 fprintf(stderr, "Monitor: "); \
173 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200174
175static inline void mon_print_count_inc(Monitor *mon)
176{
177 mon->print_calls_nr++;
178}
179
180static inline void mon_print_count_init(Monitor *mon)
181{
182 mon->print_calls_nr = 0;
183}
184
185static inline int mon_print_count_get(const Monitor *mon)
186{
187 return mon->print_calls_nr;
188}
189
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200190#else /* !CONFIG_DEBUG_MONITOR */
191#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200192static inline void mon_print_count_inc(Monitor *mon) { }
193static inline void mon_print_count_init(Monitor *mon) { }
194static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200195#endif /* CONFIG_DEBUG_MONITOR */
196
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300197/* QMP checker flags */
198#define QMP_ACCEPT_UNKNOWNS 1
199
Blue Swirl72cf2d42009-09-12 07:36:22 +0000200static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000201
Wayne Xia816f8922011-10-12 11:32:41 +0800202static mon_cmd_t mon_cmds[];
203static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000204
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300205static const mon_cmd_t qmp_cmds[];
206
Markus Armbruster8631b602010-02-18 11:41:55 +0100207Monitor *cur_mon;
208Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000209
aliguori731b0362009-03-05 23:01:42 +0000210static void monitor_command_cb(Monitor *mon, const char *cmdline,
211 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000212
Luiz Capitulino09069b12010-02-04 18:10:06 -0200213static inline int qmp_cmd_mode(const Monitor *mon)
214{
215 return (mon->mc ? mon->mc->command_mode : 0);
216}
217
Luiz Capitulino418173c2009-11-26 22:58:51 -0200218/* Return true if in control mode, false otherwise */
219static inline int monitor_ctrl_mode(const Monitor *mon)
220{
221 return (mon->flags & MONITOR_USE_CONTROL);
222}
223
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100224/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
225int monitor_cur_is_qmp(void)
226{
227 return cur_mon && monitor_ctrl_mode(cur_mon);
228}
229
aliguori731b0362009-03-05 23:01:42 +0000230static void monitor_read_command(Monitor *mon, int show_prompt)
231{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200232 if (!mon->rs)
233 return;
234
aliguori731b0362009-03-05 23:01:42 +0000235 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
236 if (show_prompt)
237 readline_show_prompt(mon->rs);
238}
bellard6a00d602005-11-21 23:25:50 +0000239
aliguoricde76ee2009-03-05 23:01:51 +0000240static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
241 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000242{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100243 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100244 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100245 return -EINVAL;
246 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000247 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
248 /* prompt is printed on return from the command handler */
249 return 0;
250 } else {
251 monitor_printf(mon, "terminal does not support password prompting\n");
252 return -ENOTTY;
253 }
aliguoribb5fc202009-03-05 23:01:15 +0000254}
255
aliguori376253e2009-03-05 23:01:23 +0000256void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000257{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200258 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500259 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000260 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000261 }
262}
263
264/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000265static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000266{
ths60fe76f2007-12-16 03:02:09 +0000267 char c;
aliguori731b0362009-03-05 23:01:42 +0000268
bellard7e2515e2004-08-01 21:52:19 +0000269 for(;;) {
270 c = *str++;
271 if (c == '\0')
272 break;
bellard7ba12602006-07-14 20:26:42 +0000273 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000274 mon->outbuf[mon->outbuf_index++] = '\r';
275 mon->outbuf[mon->outbuf_index++] = c;
276 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
277 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000278 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000279 }
280}
281
aliguori376253e2009-03-05 23:01:23 +0000282void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000283{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200284 char buf[4096];
285
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200286 if (!mon)
287 return;
288
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200289 mon_print_count_inc(mon);
290
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200291 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200292 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200293 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294
295 vsnprintf(buf, sizeof(buf), fmt, ap);
296 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000297}
298
aliguori376253e2009-03-05 23:01:23 +0000299void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000300{
301 va_list ap;
302 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000303 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000304 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000305}
306
aliguori376253e2009-03-05 23:01:23 +0000307void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000308{
309 int i;
310
311 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000312 switch (filename[i]) {
313 case ' ':
314 case '"':
315 case '\\':
316 monitor_printf(mon, "\\%c", filename[i]);
317 break;
318 case '\t':
319 monitor_printf(mon, "\\t");
320 break;
321 case '\r':
322 monitor_printf(mon, "\\r");
323 break;
324 case '\n':
325 monitor_printf(mon, "\\n");
326 break;
327 default:
328 monitor_printf(mon, "%c", filename[i]);
329 break;
330 }
thsfef30742006-12-22 14:11:32 +0000331 }
332}
333
Stefan Weil8b7968f2010-09-23 21:28:05 +0200334static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
335 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000336{
337 va_list ap;
338 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000339 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000340 va_end(ap);
341 return 0;
342}
343
Luiz Capitulino13c74252009-10-07 13:41:55 -0300344static void monitor_user_noop(Monitor *mon, const QObject *data) { }
345
Luiz Capitulino9e807212010-09-16 10:58:59 -0300346static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300347{
348 return cmd->user_print != NULL;
349}
350
Luiz Capitulino4903de02010-09-16 11:01:32 -0300351static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600352{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200353 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600354}
355
Luiz Capitulino8204a912009-11-18 23:05:31 -0200356static inline int monitor_has_error(const Monitor *mon)
357{
358 return mon->error != NULL;
359}
360
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200361static void monitor_json_emitter(Monitor *mon, const QObject *data)
362{
363 QString *json;
364
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200365 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
366 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200367 assert(json != NULL);
368
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200369 qstring_append_chr(json, '\n');
370 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200371
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200372 QDECREF(json);
373}
374
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200375static void monitor_protocol_emitter(Monitor *mon, QObject *data)
376{
377 QDict *qmp;
378
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100379 trace_monitor_protocol_emitter(mon);
380
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200381 qmp = qdict_new();
382
383 if (!monitor_has_error(mon)) {
384 /* success response */
385 if (data) {
386 qobject_incref(data);
387 qdict_put_obj(qmp, "return", data);
388 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200389 /* return an empty QDict by default */
390 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200391 }
392 } else {
393 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100394 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200395 qdict_put(qmp, "error", mon->error->error);
396 QINCREF(mon->error->error);
397 QDECREF(mon->error);
398 mon->error = NULL;
399 }
400
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200401 if (mon->mc->id) {
402 qdict_put_obj(qmp, "id", mon->mc->id);
403 mon->mc->id = NULL;
404 }
405
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200406 monitor_json_emitter(mon, QOBJECT(qmp));
407 QDECREF(qmp);
408}
409
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410static void timestamp_put(QDict *qdict)
411{
412 int err;
413 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000414 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200415
Blue Swirld08d6f02009-12-04 18:06:39 +0000416 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200417 if (err < 0)
418 return;
419
420 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
421 "'microseconds': %" PRId64 " }",
422 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200423 qdict_put_obj(qdict, "timestamp", obj);
424}
425
426/**
427 * monitor_protocol_event(): Generate a Monitor event
428 *
429 * Event-specific data can be emitted through the (optional) 'data' parameter.
430 */
431void monitor_protocol_event(MonitorEvent event, QObject *data)
432{
433 QDict *qmp;
434 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600435 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200436
Blue Swirl242cd002009-12-04 18:05:45 +0000437 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200438
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000440 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200441 event_name = "SHUTDOWN";
442 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000443 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200444 event_name = "RESET";
445 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000446 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200447 event_name = "POWERDOWN";
448 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000449 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200450 event_name = "STOP";
451 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300452 case QEVENT_RESUME:
453 event_name = "RESUME";
454 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200455 case QEVENT_VNC_CONNECTED:
456 event_name = "VNC_CONNECTED";
457 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200458 case QEVENT_VNC_INITIALIZED:
459 event_name = "VNC_INITIALIZED";
460 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200461 case QEVENT_VNC_DISCONNECTED:
462 event_name = "VNC_DISCONNECTED";
463 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200464 case QEVENT_BLOCK_IO_ERROR:
465 event_name = "BLOCK_IO_ERROR";
466 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300467 case QEVENT_RTC_CHANGE:
468 event_name = "RTC_CHANGE";
469 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300470 case QEVENT_WATCHDOG:
471 event_name = "WATCHDOG";
472 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200473 case QEVENT_SPICE_CONNECTED:
474 event_name = "SPICE_CONNECTED";
475 break;
476 case QEVENT_SPICE_INITIALIZED:
477 event_name = "SPICE_INITIALIZED";
478 break;
479 case QEVENT_SPICE_DISCONNECTED:
480 event_name = "SPICE_DISCONNECTED";
481 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200482 default:
483 abort();
484 break;
485 }
486
487 qmp = qdict_new();
488 timestamp_put(qmp);
489 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200490 if (data) {
491 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200492 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200493 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200494
Adam Litkef039a562010-01-15 08:34:02 -0600495 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200496 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200497 monitor_json_emitter(mon, QOBJECT(qmp));
498 }
Adam Litkef039a562010-01-15 08:34:02 -0600499 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200500 QDECREF(qmp);
501}
502
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200503static int do_qmp_capabilities(Monitor *mon, const QDict *params,
504 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200505{
506 /* Will setup QMP capabilities in the future */
507 if (monitor_ctrl_mode(mon)) {
508 mon->mc->command_mode = 1;
509 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200510
511 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200512}
513
Luiz Capitulino0268d972010-10-22 10:08:02 -0200514static void handle_user_command(Monitor *mon, const char *cmdline);
515
516static int do_hmp_passthrough(Monitor *mon, const QDict *params,
517 QObject **ret_data)
518{
519 int ret = 0;
520 Monitor *old_mon, hmp;
521 CharDriverState mchar;
522
523 memset(&hmp, 0, sizeof(hmp));
524 qemu_chr_init_mem(&mchar);
525 hmp.chr = &mchar;
526
527 old_mon = cur_mon;
528 cur_mon = &hmp;
529
530 if (qdict_haskey(params, "cpu-index")) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300531 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
Luiz Capitulino0268d972010-10-22 10:08:02 -0200532 if (ret < 0) {
533 cur_mon = old_mon;
534 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
535 goto out;
536 }
537 }
538
539 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
540 cur_mon = old_mon;
541
542 if (qemu_chr_mem_osize(hmp.chr) > 0) {
543 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
544 }
545
546out:
547 qemu_chr_close_mem(hmp.chr);
548 return ret;
549}
550
bellard9dc39cb2004-03-14 21:38:27 +0000551static int compare_cmd(const char *name, const char *list)
552{
553 const char *p, *pstart;
554 int len;
555 len = strlen(name);
556 p = list;
557 for(;;) {
558 pstart = p;
559 p = strchr(p, '|');
560 if (!p)
561 p = pstart + strlen(pstart);
562 if ((p - pstart) == len && !memcmp(pstart, name, len))
563 return 1;
564 if (*p == '\0')
565 break;
566 p++;
567 }
568 return 0;
569}
570
Anthony Liguoric227f092009-10-01 16:12:16 -0500571static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000572 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000573{
Anthony Liguoric227f092009-10-01 16:12:16 -0500574 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000575
576 for(cmd = cmds; cmd->name != NULL; cmd++) {
577 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000578 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
579 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000580 }
581}
582
aliguori376253e2009-03-05 23:01:23 +0000583static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000584{
585 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000586 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000587 } else {
aliguori376253e2009-03-05 23:01:23 +0000588 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000589 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000590 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000591 monitor_printf(mon, "Log items (comma separated):\n");
592 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000593 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000594 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000595 }
596 }
bellard9dc39cb2004-03-14 21:38:27 +0000597 }
598}
599
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300600static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300601{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300602 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300603}
604
Lluísfc764102011-08-31 20:31:18 +0200605static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530606{
607 const char *tp_name = qdict_get_str(qdict, "name");
608 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200609 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000610
611 if (!ret) {
612 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
613 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530614}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100615
Michael Rothc45a8162011-10-02 08:44:37 -0500616#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100617static void do_trace_file(Monitor *mon, const QDict *qdict)
618{
619 const char *op = qdict_get_try_str(qdict, "op");
620 const char *arg = qdict_get_try_str(qdict, "arg");
621
622 if (!op) {
623 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
624 } else if (!strcmp(op, "on")) {
625 st_set_trace_file_enabled(true);
626 } else if (!strcmp(op, "off")) {
627 st_set_trace_file_enabled(false);
628 } else if (!strcmp(op, "flush")) {
629 st_flush_trace_buffer();
630 } else if (!strcmp(op, "set")) {
631 if (arg) {
632 st_set_trace_file(arg);
633 }
634 } else {
635 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
636 help_cmd(mon, "trace-file");
637 }
638}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530639#endif
640
Adam Litke940cc302010-01-25 12:18:44 -0600641static void user_monitor_complete(void *opaque, QObject *ret_data)
642{
643 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
644
645 if (ret_data) {
646 data->user_print(data->mon, ret_data);
647 }
648 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500649 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600650}
651
652static void qmp_monitor_complete(void *opaque, QObject *ret_data)
653{
654 monitor_protocol_emitter(opaque, ret_data);
655}
656
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300657static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
658 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600659{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300660 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600661}
662
Adam Litke940cc302010-01-25 12:18:44 -0600663static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
664 const QDict *params)
665{
666 int ret;
667
Anthony Liguori7267c092011-08-20 22:09:37 -0500668 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600669 cb_data->mon = mon;
670 cb_data->user_print = cmd->user_print;
671 monitor_suspend(mon);
672 ret = cmd->mhandler.cmd_async(mon, params,
673 user_monitor_complete, cb_data);
674 if (ret < 0) {
675 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500676 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600677 }
678}
679
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300680static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000681{
Anthony Liguoric227f092009-10-01 16:12:16 -0500682 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300683 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000684
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200685 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000686 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200687 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300688
689 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000690 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300691 break;
bellard9dc39cb2004-03-14 21:38:27 +0000692 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300693
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200694 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300695 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200696 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300697
Luiz Capitulinob5c30582011-10-21 16:24:28 -0200698 cmd->mhandler.info(mon);
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300699 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300700
701help:
702 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000703}
704
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300705CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000706{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300707 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200708 const mon_cmd_t *cmd;
709
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300710 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200711 info = g_malloc0(sizeof(*info));
712 info->value = g_malloc0(sizeof(*info->value));
713 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200714
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300715 info->next = cmd_list;
716 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200717 }
718
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300719 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000720}
721
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300722/* set the current CPU defined by the user */
723int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000724{
725 CPUState *env;
726
727 for(env = first_cpu; env != NULL; env = env->next_cpu) {
728 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000729 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000730 return 0;
731 }
732 }
733 return -1;
734}
735
pbrook9596ebb2007-11-18 01:44:38 +0000736static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000737{
aliguori731b0362009-03-05 23:01:42 +0000738 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300739 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000740 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300741 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000742 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000743}
744
Luiz Capitulino99b77962011-10-24 10:53:44 -0200745int monitor_get_cpu_index(void)
746{
747 return mon_get_cpu()->cpu_index;
748}
749
aliguori376253e2009-03-05 23:01:23 +0000750static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000751{
bellard6a00d602005-11-21 23:25:50 +0000752 CPUState *env;
753 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000754#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000755 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000756 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000757#else
aliguori376253e2009-03-05 23:01:23 +0000758 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000759 0);
bellard9307c4c2004-04-04 12:57:25 +0000760#endif
761}
762
aliguori376253e2009-03-05 23:01:23 +0000763static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000764{
aliguori376253e2009-03-05 23:01:23 +0000765 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000766}
767
aliguori376253e2009-03-05 23:01:23 +0000768static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000769{
770 int i;
bellard7e2515e2004-08-01 21:52:19 +0000771 const char *str;
ths3b46e622007-09-17 08:09:54 +0000772
aliguoricde76ee2009-03-05 23:01:51 +0000773 if (!mon->rs)
774 return;
bellard7e2515e2004-08-01 21:52:19 +0000775 i = 0;
776 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000777 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000778 if (!str)
779 break;
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000781 i++;
bellardaa455482004-04-04 13:07:25 +0000782 }
783}
784
j_mayer76a66252007-03-07 08:32:30 +0000785#if defined(TARGET_PPC)
786/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000787static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000788{
789 CPUState *env;
790
791 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000792 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000793}
794#endif
795
Lluís6d8a7642011-08-31 20:30:43 +0200796#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530797static void do_info_trace(Monitor *mon)
798{
799 st_print_trace((FILE *)mon, &monitor_fprintf);
800}
Lluís31965ae2011-08-31 20:31:24 +0200801#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530802
Lluísfc764102011-08-31 20:31:18 +0200803static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530804{
Lluísfc764102011-08-31 20:31:18 +0200805 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530806}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530807
Jes Sorensen821601e2011-03-16 13:33:36 +0100808#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200809static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000810{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600811 if (!password || !password[0]) {
812 if (vnc_display_disable_login(NULL)) {
813 qerror_report(QERR_SET_PASSWD_FAILED);
814 return -1;
815 }
816 return 0;
817 }
818
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200819 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100820 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200821 return -1;
822 }
aliguoribb5fc202009-03-05 23:01:15 +0000823
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200824 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100825}
826
827static void change_vnc_password_cb(Monitor *mon, const char *password,
828 void *opaque)
829{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100830 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000831 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000832}
833
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200834static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000835{
ths70848512007-08-25 01:37:05 +0000836 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000837 strcmp(target, "password") == 0) {
838 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000839 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000840 strncpy(password, arg, sizeof(password));
841 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200842 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000843 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200844 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000845 }
ths70848512007-08-25 01:37:05 +0000846 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200847 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100848 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200849 return -1;
850 }
ths70848512007-08-25 01:37:05 +0000851 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200852
853 return 0;
thse25a5822007-08-25 01:36:20 +0000854}
Jes Sorensen821601e2011-03-16 13:33:36 +0100855#else
856static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
857{
858 qerror_report(QERR_FEATURE_DISABLED, "vnc");
859 return -ENODEV;
860}
861#endif
thse25a5822007-08-25 01:36:20 +0000862
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100863/**
864 * do_change(): Change a removable medium, or VNC configuration
865 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200866static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000867{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300868 const char *device = qdict_get_str(qdict, "device");
869 const char *target = qdict_get_str(qdict, "target");
870 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200871 int ret;
872
thse25a5822007-08-25 01:36:20 +0000873 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200874 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000875 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200876 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000877 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200878
879 return ret;
thse25a5822007-08-25 01:36:20 +0000880}
881
Gerd Hoffmann75721502010-10-07 12:22:54 +0200882static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
883{
884 const char *protocol = qdict_get_str(qdict, "protocol");
885 const char *password = qdict_get_str(qdict, "password");
886 const char *connected = qdict_get_try_str(qdict, "connected");
887 int disconnect_if_connected = 0;
888 int fail_if_connected = 0;
889 int rc;
890
891 if (connected) {
892 if (strcmp(connected, "fail") == 0) {
893 fail_if_connected = 1;
894 } else if (strcmp(connected, "disconnect") == 0) {
895 disconnect_if_connected = 1;
896 } else if (strcmp(connected, "keep") == 0) {
897 /* nothing */
898 } else {
899 qerror_report(QERR_INVALID_PARAMETER, "connected");
900 return -1;
901 }
902 }
903
904 if (strcmp(protocol, "spice") == 0) {
905 if (!using_spice) {
906 /* correct one? spice isn't a device ,,, */
907 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
908 return -1;
909 }
910 rc = qemu_spice_set_passwd(password, fail_if_connected,
911 disconnect_if_connected);
912 if (rc != 0) {
913 qerror_report(QERR_SET_PASSWD_FAILED);
914 return -1;
915 }
916 return 0;
917 }
918
919 if (strcmp(protocol, "vnc") == 0) {
920 if (fail_if_connected || disconnect_if_connected) {
921 /* vnc supports "connected=keep" only */
922 qerror_report(QERR_INVALID_PARAMETER, "connected");
923 return -1;
924 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600925 /* Note that setting an empty password will not disable login through
926 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +0100927 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200928 }
929
930 qerror_report(QERR_INVALID_PARAMETER, "protocol");
931 return -1;
932}
933
934static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
935{
936 const char *protocol = qdict_get_str(qdict, "protocol");
937 const char *whenstr = qdict_get_str(qdict, "time");
938 time_t when;
939 int rc;
940
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100941 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200942 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100943 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200944 when = TIME_MAX;
945 } else if (whenstr[0] == '+') {
946 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
947 } else {
948 when = strtoull(whenstr, NULL, 10);
949 }
950
951 if (strcmp(protocol, "spice") == 0) {
952 if (!using_spice) {
953 /* correct one? spice isn't a device ,,, */
954 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
955 return -1;
956 }
957 rc = qemu_spice_set_pw_expire(when);
958 if (rc != 0) {
959 qerror_report(QERR_SET_PASSWD_FAILED);
960 return -1;
961 }
962 return 0;
963 }
964
965 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +0100966 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200967 }
968
969 qerror_report(QERR_INVALID_PARAMETER, "protocol");
970 return -1;
971}
972
Daniel P. Berrange13661082011-06-23 13:31:42 +0100973static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
974{
975 const char *protocol = qdict_get_str(qdict, "protocol");
976 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +0100977 CharDriverState *s;
978
979 if (strcmp(protocol, "spice") == 0) {
980 if (!using_spice) {
981 /* correct one? spice isn't a device ,,, */
982 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
983 return -1;
984 }
985 qerror_report(QERR_ADD_CLIENT_FAILED);
986 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +0800987#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +0100988 } else if (strcmp(protocol, "vnc") == 0) {
989 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +0100990 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100991 vnc_display_add_client(NULL, fd, skipauth);
992 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800993#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +0100994 } else if ((s = qemu_chr_find(protocol)) != NULL) {
995 int fd = monitor_get_fd(mon, fdname);
996 if (qemu_chr_add_client(s, fd) < 0) {
997 qerror_report(QERR_ADD_CLIENT_FAILED);
998 return -1;
999 }
1000 return 0;
1001 }
1002
1003 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1004 return -1;
1005}
1006
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001007static int client_migrate_info(Monitor *mon, const QDict *qdict,
1008 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001009{
1010 const char *protocol = qdict_get_str(qdict, "protocol");
1011 const char *hostname = qdict_get_str(qdict, "hostname");
1012 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1013 int port = qdict_get_try_int(qdict, "port", -1);
1014 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1015 int ret;
1016
1017 if (strcmp(protocol, "spice") == 0) {
1018 if (!using_spice) {
1019 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1020 return -1;
1021 }
1022
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001023 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1024 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001025 if (ret != 0) {
1026 qerror_report(QERR_UNDEFINED_ERROR);
1027 return -1;
1028 }
1029 return 0;
1030 }
1031
1032 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1033 return -1;
1034}
1035
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001036static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001037{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001038 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001039 return 0;
bellard59a983b2004-03-17 23:17:16 +00001040}
1041
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001042static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001043{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001044 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001045}
1046
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001047static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001048{
1049 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001050 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001051
bellard9307c4c2004-04-04 12:57:25 +00001052 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001053 mask = 0;
1054 } else {
bellard9307c4c2004-04-04 12:57:25 +00001055 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001056 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001057 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001058 return;
1059 }
1060 }
1061 cpu_set_log(mask);
1062}
1063
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001064static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001065{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001066 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001067 if (!option || !strcmp(option, "on")) {
1068 singlestep = 1;
1069 } else if (!strcmp(option, "off")) {
1070 singlestep = 0;
1071 } else {
1072 monitor_printf(mon, "unexpected option %s\n", option);
1073 }
1074}
1075
aliguoribb5fc202009-03-05 23:01:15 +00001076static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001077
aliguori376253e2009-03-05 23:01:23 +00001078struct bdrv_iterate_context {
1079 Monitor *mon;
1080 int err;
1081};
aliguoric0f4ce72009-03-05 23:01:01 +00001082
Luiz Capitulino28a72822011-09-26 17:43:50 -03001083static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1084{
1085 bdrv_iostatus_reset(bs);
1086}
1087
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001088/**
1089 * do_cont(): Resume emulation.
1090 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001091static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001092{
1093 struct bdrv_iterate_context context = { mon, 0 };
1094
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001095 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301096 qerror_report(QERR_MIGRATION_EXPECTED);
1097 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001098 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1099 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001100 qerror_report(QERR_RESET_REQUIRED);
1101 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301102 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001103
Luiz Capitulino28a72822011-09-26 17:43:50 -03001104 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001105 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001106 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001107 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001108 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001109 return 0;
1110 } else {
1111 return -1;
1112 }
bellard8a7ddc32004-03-31 19:00:16 +00001113}
1114
aliguoribb5fc202009-03-05 23:01:15 +00001115static void bdrv_key_cb(void *opaque, int err)
1116{
aliguori376253e2009-03-05 23:01:23 +00001117 Monitor *mon = opaque;
1118
aliguoribb5fc202009-03-05 23:01:15 +00001119 /* another key was set successfully, retry to continue */
1120 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001121 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001122}
1123
1124static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1125{
aliguori376253e2009-03-05 23:01:23 +00001126 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001127
aliguori376253e2009-03-05 23:01:23 +00001128 if (!context->err && bdrv_key_required(bs)) {
1129 context->err = -EBUSY;
1130 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1131 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001132 }
1133}
1134
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001135static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001136{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001137 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001138 if (!device)
1139 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1140 if (gdbserver_start(device) < 0) {
1141 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1142 device);
1143 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001144 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001145 } else {
aliguori59030a82009-04-05 18:43:41 +00001146 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1147 device);
bellard8a7ddc32004-03-31 19:00:16 +00001148 }
1149}
1150
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001151static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001152{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001153 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001154 if (select_watchdog_action(action) == -1) {
1155 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1156 }
1157}
1158
aliguori376253e2009-03-05 23:01:23 +00001159static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001160{
aliguori376253e2009-03-05 23:01:23 +00001161 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001162 switch(c) {
1163 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001164 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001165 break;
1166 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001167 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001168 break;
1169 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001171 break;
1172 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001173 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001174 break;
1175 default:
1176 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001178 } else {
aliguori376253e2009-03-05 23:01:23 +00001179 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001180 }
1181 break;
1182 }
aliguori376253e2009-03-05 23:01:23 +00001183 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001184}
1185
aliguori376253e2009-03-05 23:01:23 +00001186static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001187 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001188{
bellard6a00d602005-11-21 23:25:50 +00001189 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001190 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001191 uint8_t buf[16];
1192 uint64_t v;
1193
1194 if (format == 'i') {
1195 int flags;
1196 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001197 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001198#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001199 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001200 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001201 } else if (wsize == 4) {
1202 flags = 0;
1203 } else {
bellard6a15fd12006-04-12 21:07:07 +00001204 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001205 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001206 if (env) {
1207#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001208 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001209 (env->segs[R_CS].flags & DESC_L_MASK))
1210 flags = 2;
1211 else
1212#endif
1213 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1214 flags = 1;
1215 }
bellard4c27ba22004-04-25 18:05:08 +00001216 }
1217#endif
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001219 return;
1220 }
1221
1222 len = wsize * count;
1223 if (wsize == 1)
1224 line_size = 8;
1225 else
1226 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001227 max_digits = 0;
1228
1229 switch(format) {
1230 case 'o':
1231 max_digits = (wsize * 8 + 2) / 3;
1232 break;
1233 default:
1234 case 'x':
1235 max_digits = (wsize * 8) / 4;
1236 break;
1237 case 'u':
1238 case 'd':
1239 max_digits = (wsize * 8 * 10 + 32) / 33;
1240 break;
1241 case 'c':
1242 wsize = 1;
1243 break;
1244 }
1245
1246 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001247 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001249 else
aliguori376253e2009-03-05 23:01:23 +00001250 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001251 l = len;
1252 if (l > line_size)
1253 l = line_size;
1254 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001255 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001256 } else {
bellard6a00d602005-11-21 23:25:50 +00001257 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001258 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001260 break;
1261 }
bellard9307c4c2004-04-04 12:57:25 +00001262 }
ths5fafdf22007-09-16 21:08:06 +00001263 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001264 while (i < l) {
1265 switch(wsize) {
1266 default:
1267 case 1:
1268 v = ldub_raw(buf + i);
1269 break;
1270 case 2:
1271 v = lduw_raw(buf + i);
1272 break;
1273 case 4:
bellard92a31b12005-02-10 22:00:52 +00001274 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001275 break;
1276 case 8:
1277 v = ldq_raw(buf + i);
1278 break;
1279 }
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001281 switch(format) {
1282 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001283 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001286 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001290 break;
1291 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001292 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 }
1298 i += wsize;
1299 }
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001301 addr += l;
1302 len -= l;
1303 }
1304}
1305
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001306static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001307{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001308 int count = qdict_get_int(qdict, "count");
1309 int format = qdict_get_int(qdict, "format");
1310 int size = qdict_get_int(qdict, "size");
1311 target_long addr = qdict_get_int(qdict, "addr");
1312
aliguori376253e2009-03-05 23:01:23 +00001313 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001314}
1315
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001316static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001317{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001318 int count = qdict_get_int(qdict, "count");
1319 int format = qdict_get_int(qdict, "format");
1320 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001321 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001322
aliguori376253e2009-03-05 23:01:23 +00001323 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001324}
1325
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001326static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001327{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001328 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001329 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001330
blueswir17743e582007-09-24 18:39:04 +00001331#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001332 switch(format) {
1333 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001335 break;
1336 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001341 break;
1342 default:
1343 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001347 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 }
bellard92a31b12005-02-10 22:00:52 +00001350#else
1351 switch(format) {
1352 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001354 break;
1355 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001357 break;
1358 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001360 break;
1361 default:
1362 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001363 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001364 break;
1365 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001366 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001367 break;
1368 }
1369#endif
aliguori376253e2009-03-05 23:01:23 +00001370 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001371}
1372
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001373static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001374{
1375 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001376 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001377 uint32_t start = qdict_get_int(qdict, "start");
1378 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001379
1380 sum = 0;
1381 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001382 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001383 /* BSD sum algorithm ('sum' Unix command) */
1384 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001385 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001386 }
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001388}
1389
bellarda3a91a32004-06-04 11:06:21 +00001390typedef struct {
1391 int keycode;
1392 const char *name;
1393} KeyDef;
1394
1395static const KeyDef key_defs[] = {
1396 { 0x2a, "shift" },
1397 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001398
bellarda3a91a32004-06-04 11:06:21 +00001399 { 0x38, "alt" },
1400 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001401 { 0x64, "altgr" },
1402 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001403 { 0x1d, "ctrl" },
1404 { 0x9d, "ctrl_r" },
1405
1406 { 0xdd, "menu" },
1407
1408 { 0x01, "esc" },
1409
1410 { 0x02, "1" },
1411 { 0x03, "2" },
1412 { 0x04, "3" },
1413 { 0x05, "4" },
1414 { 0x06, "5" },
1415 { 0x07, "6" },
1416 { 0x08, "7" },
1417 { 0x09, "8" },
1418 { 0x0a, "9" },
1419 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001420 { 0x0c, "minus" },
1421 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001422 { 0x0e, "backspace" },
1423
1424 { 0x0f, "tab" },
1425 { 0x10, "q" },
1426 { 0x11, "w" },
1427 { 0x12, "e" },
1428 { 0x13, "r" },
1429 { 0x14, "t" },
1430 { 0x15, "y" },
1431 { 0x16, "u" },
1432 { 0x17, "i" },
1433 { 0x18, "o" },
1434 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001435 { 0x1a, "bracket_left" },
1436 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001437 { 0x1c, "ret" },
1438
1439 { 0x1e, "a" },
1440 { 0x1f, "s" },
1441 { 0x20, "d" },
1442 { 0x21, "f" },
1443 { 0x22, "g" },
1444 { 0x23, "h" },
1445 { 0x24, "j" },
1446 { 0x25, "k" },
1447 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001448 { 0x27, "semicolon" },
1449 { 0x28, "apostrophe" },
1450 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001451
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001452 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001453 { 0x2c, "z" },
1454 { 0x2d, "x" },
1455 { 0x2e, "c" },
1456 { 0x2f, "v" },
1457 { 0x30, "b" },
1458 { 0x31, "n" },
1459 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001460 { 0x33, "comma" },
1461 { 0x34, "dot" },
1462 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001463
balrog4d3b6f62008-02-10 16:33:14 +00001464 { 0x37, "asterisk" },
1465
bellarda3a91a32004-06-04 11:06:21 +00001466 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001467 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001468 { 0x3b, "f1" },
1469 { 0x3c, "f2" },
1470 { 0x3d, "f3" },
1471 { 0x3e, "f4" },
1472 { 0x3f, "f5" },
1473 { 0x40, "f6" },
1474 { 0x41, "f7" },
1475 { 0x42, "f8" },
1476 { 0x43, "f9" },
1477 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001478 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001479 { 0x46, "scroll_lock" },
1480
bellard64866c32006-05-07 18:03:31 +00001481 { 0xb5, "kp_divide" },
1482 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001483 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001484 { 0x4e, "kp_add" },
1485 { 0x9c, "kp_enter" },
1486 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001487 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001488
1489 { 0x52, "kp_0" },
1490 { 0x4f, "kp_1" },
1491 { 0x50, "kp_2" },
1492 { 0x51, "kp_3" },
1493 { 0x4b, "kp_4" },
1494 { 0x4c, "kp_5" },
1495 { 0x4d, "kp_6" },
1496 { 0x47, "kp_7" },
1497 { 0x48, "kp_8" },
1498 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001499
bellarda3a91a32004-06-04 11:06:21 +00001500 { 0x56, "<" },
1501
1502 { 0x57, "f11" },
1503 { 0x58, "f12" },
1504
1505 { 0xb7, "print" },
1506
1507 { 0xc7, "home" },
1508 { 0xc9, "pgup" },
1509 { 0xd1, "pgdn" },
1510 { 0xcf, "end" },
1511
1512 { 0xcb, "left" },
1513 { 0xc8, "up" },
1514 { 0xd0, "down" },
1515 { 0xcd, "right" },
1516
1517 { 0xd2, "insert" },
1518 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001519#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1520 { 0xf0, "stop" },
1521 { 0xf1, "again" },
1522 { 0xf2, "props" },
1523 { 0xf3, "undo" },
1524 { 0xf4, "front" },
1525 { 0xf5, "copy" },
1526 { 0xf6, "open" },
1527 { 0xf7, "paste" },
1528 { 0xf8, "find" },
1529 { 0xf9, "cut" },
1530 { 0xfa, "lf" },
1531 { 0xfb, "help" },
1532 { 0xfc, "meta_l" },
1533 { 0xfd, "meta_r" },
1534 { 0xfe, "compose" },
1535#endif
bellarda3a91a32004-06-04 11:06:21 +00001536 { 0, NULL },
1537};
1538
1539static int get_keycode(const char *key)
1540{
1541 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001542 char *endp;
1543 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001544
1545 for(p = key_defs; p->name != NULL; p++) {
1546 if (!strcmp(key, p->name))
1547 return p->keycode;
1548 }
bellard64866c32006-05-07 18:03:31 +00001549 if (strstart(key, "0x", NULL)) {
1550 ret = strtoul(key, &endp, 0);
1551 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1552 return ret;
1553 }
bellarda3a91a32004-06-04 11:06:21 +00001554 return -1;
1555}
1556
balrogc8256f92008-06-08 22:45:01 +00001557#define MAX_KEYCODES 16
1558static uint8_t keycodes[MAX_KEYCODES];
1559static int nb_pending_keycodes;
1560static QEMUTimer *key_timer;
1561
1562static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001563{
balrogc8256f92008-06-08 22:45:01 +00001564 int keycode;
1565
1566 while (nb_pending_keycodes > 0) {
1567 nb_pending_keycodes--;
1568 keycode = keycodes[nb_pending_keycodes];
1569 if (keycode & 0x80)
1570 kbd_put_keycode(0xe0);
1571 kbd_put_keycode(keycode | 0x80);
1572 }
1573}
1574
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001575static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001576{
balrog3401c0d2008-06-04 10:05:59 +00001577 char keyname_buf[16];
1578 char *separator;
1579 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001580 const char *string = qdict_get_str(qdict, "string");
1581 int has_hold_time = qdict_haskey(qdict, "hold_time");
1582 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001583
balrogc8256f92008-06-08 22:45:01 +00001584 if (nb_pending_keycodes > 0) {
1585 qemu_del_timer(key_timer);
1586 release_keys(NULL);
1587 }
1588 if (!has_hold_time)
1589 hold_time = 100;
1590 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001591 while (1) {
1592 separator = strchr(string, '-');
1593 keyname_len = separator ? separator - string : strlen(string);
1594 if (keyname_len > 0) {
1595 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1596 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001597 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001598 return;
bellarda3a91a32004-06-04 11:06:21 +00001599 }
balrogc8256f92008-06-08 22:45:01 +00001600 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001601 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001602 return;
1603 }
1604 keyname_buf[keyname_len] = 0;
1605 keycode = get_keycode(keyname_buf);
1606 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001607 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001608 return;
1609 }
balrogc8256f92008-06-08 22:45:01 +00001610 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001611 }
balrog3401c0d2008-06-04 10:05:59 +00001612 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001613 break;
balrog3401c0d2008-06-04 10:05:59 +00001614 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001615 }
balrogc8256f92008-06-08 22:45:01 +00001616 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001617 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001618 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001619 keycode = keycodes[i];
1620 if (keycode & 0x80)
1621 kbd_put_keycode(0xe0);
1622 kbd_put_keycode(keycode & 0x7f);
1623 }
balrogc8256f92008-06-08 22:45:01 +00001624 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001625 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001626 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001627}
1628
bellard13224a82006-07-14 22:03:35 +00001629static int mouse_button_state;
1630
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001631static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001632{
1633 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001634 const char *dx_str = qdict_get_str(qdict, "dx_str");
1635 const char *dy_str = qdict_get_str(qdict, "dy_str");
1636 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001637 dx = strtol(dx_str, NULL, 0);
1638 dy = strtol(dy_str, NULL, 0);
1639 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001640 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001641 dz = strtol(dz_str, NULL, 0);
1642 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1643}
1644
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001645static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001646{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001647 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001648 mouse_button_state = button_state;
1649 kbd_mouse_event(0, 0, 0, mouse_button_state);
1650}
1651
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001652static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001653{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001654 int size = qdict_get_int(qdict, "size");
1655 int addr = qdict_get_int(qdict, "addr");
1656 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001657 uint32_t val;
1658 int suffix;
1659
1660 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001661 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001662 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001663 addr++;
1664 }
1665 addr &= 0xffff;
1666
1667 switch(size) {
1668 default:
1669 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001670 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001671 suffix = 'b';
1672 break;
1673 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001674 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001675 suffix = 'w';
1676 break;
1677 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001678 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001679 suffix = 'l';
1680 break;
1681 }
aliguori376253e2009-03-05 23:01:23 +00001682 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1683 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001684}
bellarda3a91a32004-06-04 11:06:21 +00001685
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001686static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001687{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001688 int size = qdict_get_int(qdict, "size");
1689 int addr = qdict_get_int(qdict, "addr");
1690 int val = qdict_get_int(qdict, "val");
1691
Jan Kiszkaf1147842009-07-14 10:20:11 +02001692 addr &= IOPORTS_MASK;
1693
1694 switch (size) {
1695 default:
1696 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001697 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001698 break;
1699 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001700 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001701 break;
1702 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001703 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001704 break;
1705 }
1706}
1707
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001708static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001709{
1710 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001711 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001712
Jan Kiszka76e30d02009-07-02 00:19:02 +02001713 res = qemu_boot_set(bootdevice);
1714 if (res == 0) {
1715 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1716 } else if (res > 0) {
1717 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001718 } else {
aliguori376253e2009-03-05 23:01:23 +00001719 monitor_printf(mon, "no function defined to set boot device list for "
1720 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001721 }
1722}
1723
bellardb86bda52004-09-18 19:32:46 +00001724#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001725static void print_pte(Monitor *mon, target_phys_addr_t addr,
1726 target_phys_addr_t pte,
1727 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001728{
Blue Swirld65aaf32010-12-11 18:56:24 +00001729#ifdef TARGET_X86_64
1730 if (addr & (1ULL << 47)) {
1731 addr |= -1LL << 48;
1732 }
1733#endif
1734 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1735 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001736 addr,
1737 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001738 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001739 pte & PG_GLOBAL_MASK ? 'G' : '-',
1740 pte & PG_PSE_MASK ? 'P' : '-',
1741 pte & PG_DIRTY_MASK ? 'D' : '-',
1742 pte & PG_ACCESSED_MASK ? 'A' : '-',
1743 pte & PG_PCD_MASK ? 'C' : '-',
1744 pte & PG_PWT_MASK ? 'T' : '-',
1745 pte & PG_USER_MASK ? 'U' : '-',
1746 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001747}
1748
Blue Swirld65aaf32010-12-11 18:56:24 +00001749static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001750{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001751 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001752 uint32_t pgd, pde, pte;
1753
bellardb86bda52004-09-18 19:32:46 +00001754 pgd = env->cr[3] & ~0xfff;
1755 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001756 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001757 pde = le32_to_cpu(pde);
1758 if (pde & PG_PRESENT_MASK) {
1759 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001760 /* 4M pages */
1761 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001762 } else {
1763 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001764 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001765 pte = le32_to_cpu(pte);
1766 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001767 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001768 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001769 ~0xfff);
1770 }
1771 }
1772 }
1773 }
1774 }
1775}
1776
Blue Swirld65aaf32010-12-11 18:56:24 +00001777static void tlb_info_pae32(Monitor *mon, CPUState *env)
1778{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001779 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001780 uint64_t pdpe, pde, pte;
1781 uint64_t pdp_addr, pd_addr, pt_addr;
1782
1783 pdp_addr = env->cr[3] & ~0x1f;
1784 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001785 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001786 pdpe = le64_to_cpu(pdpe);
1787 if (pdpe & PG_PRESENT_MASK) {
1788 pd_addr = pdpe & 0x3fffffffff000ULL;
1789 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001790 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001791 pde = le64_to_cpu(pde);
1792 if (pde & PG_PRESENT_MASK) {
1793 if (pde & PG_PSE_MASK) {
1794 /* 2M pages with PAE, CR4.PSE is ignored */
1795 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1796 ~((target_phys_addr_t)(1 << 20) - 1));
1797 } else {
1798 pt_addr = pde & 0x3fffffffff000ULL;
1799 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001800 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001801 pte = le64_to_cpu(pte);
1802 if (pte & PG_PRESENT_MASK) {
1803 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1804 + (l3 << 12),
1805 pte & ~PG_PSE_MASK,
1806 ~(target_phys_addr_t)0xfff);
1807 }
1808 }
1809 }
1810 }
1811 }
1812 }
1813 }
1814}
1815
1816#ifdef TARGET_X86_64
1817static void tlb_info_64(Monitor *mon, CPUState *env)
1818{
1819 uint64_t l1, l2, l3, l4;
1820 uint64_t pml4e, pdpe, pde, pte;
1821 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1822
1823 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1824 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001825 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001826 pml4e = le64_to_cpu(pml4e);
1827 if (pml4e & PG_PRESENT_MASK) {
1828 pdp_addr = pml4e & 0x3fffffffff000ULL;
1829 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001830 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001831 pdpe = le64_to_cpu(pdpe);
1832 if (pdpe & PG_PRESENT_MASK) {
1833 if (pdpe & PG_PSE_MASK) {
1834 /* 1G pages, CR4.PSE is ignored */
1835 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1836 0x3ffffc0000000ULL);
1837 } else {
1838 pd_addr = pdpe & 0x3fffffffff000ULL;
1839 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001840 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001841 pde = le64_to_cpu(pde);
1842 if (pde & PG_PRESENT_MASK) {
1843 if (pde & PG_PSE_MASK) {
1844 /* 2M pages, CR4.PSE is ignored */
1845 print_pte(mon, (l1 << 39) + (l2 << 30) +
1846 (l3 << 21), pde,
1847 0x3ffffffe00000ULL);
1848 } else {
1849 pt_addr = pde & 0x3fffffffff000ULL;
1850 for (l4 = 0; l4 < 512; l4++) {
1851 cpu_physical_memory_read(pt_addr
1852 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001853 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001854 pte = le64_to_cpu(pte);
1855 if (pte & PG_PRESENT_MASK) {
1856 print_pte(mon, (l1 << 39) +
1857 (l2 << 30) +
1858 (l3 << 21) + (l4 << 12),
1859 pte & ~PG_PSE_MASK,
1860 0x3fffffffff000ULL);
1861 }
1862 }
1863 }
1864 }
1865 }
1866 }
1867 }
1868 }
1869 }
1870 }
1871}
1872#endif
1873
1874static void tlb_info(Monitor *mon)
1875{
1876 CPUState *env;
1877
1878 env = mon_get_cpu();
1879
1880 if (!(env->cr[0] & CR0_PG_MASK)) {
1881 monitor_printf(mon, "PG disabled\n");
1882 return;
1883 }
1884 if (env->cr[4] & CR4_PAE_MASK) {
1885#ifdef TARGET_X86_64
1886 if (env->hflags & HF_LMA_MASK) {
1887 tlb_info_64(mon, env);
1888 } else
1889#endif
1890 {
1891 tlb_info_pae32(mon, env);
1892 }
1893 } else {
1894 tlb_info_32(mon, env);
1895 }
1896}
1897
Blue Swirl1b3cba62010-12-11 18:56:27 +00001898static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
1899 int *plast_prot,
1900 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001901{
bellard9746b152004-11-11 18:30:24 +00001902 int prot1;
1903 prot1 = *plast_prot;
1904 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001905 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001906 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1907 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001908 *pstart, end, end - *pstart,
1909 prot1 & PG_USER_MASK ? 'u' : '-',
1910 'r',
1911 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001912 }
1913 if (prot != 0)
1914 *pstart = end;
1915 else
1916 *pstart = -1;
1917 *plast_prot = prot;
1918 }
1919}
1920
Blue Swirl1b3cba62010-12-11 18:56:27 +00001921static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001922{
Austin Clementsb49ca722011-08-14 23:19:21 -04001923 unsigned int l1, l2;
1924 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001925 uint32_t pgd, pde, pte;
1926 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00001927
bellardb86bda52004-09-18 19:32:46 +00001928 pgd = env->cr[3] & ~0xfff;
1929 last_prot = 0;
1930 start = -1;
1931 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001932 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001933 pde = le32_to_cpu(pde);
1934 end = l1 << 22;
1935 if (pde & PG_PRESENT_MASK) {
1936 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1937 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001938 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001939 } else {
1940 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001941 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001942 pte = le32_to_cpu(pte);
1943 end = (l1 << 22) + (l2 << 12);
1944 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001945 prot = pte & pde &
1946 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001947 } else {
1948 prot = 0;
1949 }
aliguori376253e2009-03-05 23:01:23 +00001950 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001951 }
1952 }
1953 } else {
1954 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001955 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001956 }
1957 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001958 /* Flush last range */
1959 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001960}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001961
1962static void mem_info_pae32(Monitor *mon, CPUState *env)
1963{
Austin Clementsb49ca722011-08-14 23:19:21 -04001964 unsigned int l1, l2, l3;
1965 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001966 uint64_t pdpe, pde, pte;
1967 uint64_t pdp_addr, pd_addr, pt_addr;
1968 target_phys_addr_t start, end;
1969
1970 pdp_addr = env->cr[3] & ~0x1f;
1971 last_prot = 0;
1972 start = -1;
1973 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001974 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001975 pdpe = le64_to_cpu(pdpe);
1976 end = l1 << 30;
1977 if (pdpe & PG_PRESENT_MASK) {
1978 pd_addr = pdpe & 0x3fffffffff000ULL;
1979 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001980 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001981 pde = le64_to_cpu(pde);
1982 end = (l1 << 30) + (l2 << 21);
1983 if (pde & PG_PRESENT_MASK) {
1984 if (pde & PG_PSE_MASK) {
1985 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1986 PG_PRESENT_MASK);
1987 mem_print(mon, &start, &last_prot, end, prot);
1988 } else {
1989 pt_addr = pde & 0x3fffffffff000ULL;
1990 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001991 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001992 pte = le64_to_cpu(pte);
1993 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1994 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001995 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1996 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001997 } else {
1998 prot = 0;
1999 }
2000 mem_print(mon, &start, &last_prot, end, prot);
2001 }
2002 }
2003 } else {
2004 prot = 0;
2005 mem_print(mon, &start, &last_prot, end, prot);
2006 }
2007 }
2008 } else {
2009 prot = 0;
2010 mem_print(mon, &start, &last_prot, end, prot);
2011 }
2012 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002013 /* Flush last range */
2014 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002015}
2016
2017
2018#ifdef TARGET_X86_64
2019static void mem_info_64(Monitor *mon, CPUState *env)
2020{
2021 int prot, last_prot;
2022 uint64_t l1, l2, l3, l4;
2023 uint64_t pml4e, pdpe, pde, pte;
2024 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2025
2026 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2027 last_prot = 0;
2028 start = -1;
2029 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002030 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002031 pml4e = le64_to_cpu(pml4e);
2032 end = l1 << 39;
2033 if (pml4e & PG_PRESENT_MASK) {
2034 pdp_addr = pml4e & 0x3fffffffff000ULL;
2035 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002036 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002037 pdpe = le64_to_cpu(pdpe);
2038 end = (l1 << 39) + (l2 << 30);
2039 if (pdpe & PG_PRESENT_MASK) {
2040 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002041 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2042 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002043 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002044 mem_print(mon, &start, &last_prot, end, prot);
2045 } else {
2046 pd_addr = pdpe & 0x3fffffffff000ULL;
2047 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002048 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002049 pde = le64_to_cpu(pde);
2050 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2051 if (pde & PG_PRESENT_MASK) {
2052 if (pde & PG_PSE_MASK) {
2053 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2054 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002055 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002056 mem_print(mon, &start, &last_prot, end, prot);
2057 } else {
2058 pt_addr = pde & 0x3fffffffff000ULL;
2059 for (l4 = 0; l4 < 512; l4++) {
2060 cpu_physical_memory_read(pt_addr
2061 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002062 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002063 pte = le64_to_cpu(pte);
2064 end = (l1 << 39) + (l2 << 30) +
2065 (l3 << 21) + (l4 << 12);
2066 if (pte & PG_PRESENT_MASK) {
2067 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2068 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002069 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002070 } else {
2071 prot = 0;
2072 }
2073 mem_print(mon, &start, &last_prot, end, prot);
2074 }
2075 }
2076 } else {
2077 prot = 0;
2078 mem_print(mon, &start, &last_prot, end, prot);
2079 }
2080 }
2081 }
2082 } else {
2083 prot = 0;
2084 mem_print(mon, &start, &last_prot, end, prot);
2085 }
2086 }
2087 } else {
2088 prot = 0;
2089 mem_print(mon, &start, &last_prot, end, prot);
2090 }
2091 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002092 /* Flush last range */
2093 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002094}
2095#endif
2096
2097static void mem_info(Monitor *mon)
2098{
2099 CPUState *env;
2100
2101 env = mon_get_cpu();
2102
2103 if (!(env->cr[0] & CR0_PG_MASK)) {
2104 monitor_printf(mon, "PG disabled\n");
2105 return;
2106 }
2107 if (env->cr[4] & CR4_PAE_MASK) {
2108#ifdef TARGET_X86_64
2109 if (env->hflags & HF_LMA_MASK) {
2110 mem_info_64(mon, env);
2111 } else
2112#endif
2113 {
2114 mem_info_pae32(mon, env);
2115 }
2116 } else {
2117 mem_info_32(mon, env);
2118 }
2119}
bellardb86bda52004-09-18 19:32:46 +00002120#endif
2121
aurel327c664e22009-03-03 06:12:22 +00002122#if defined(TARGET_SH4)
2123
aliguori376253e2009-03-05 23:01:23 +00002124static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002125{
aliguori376253e2009-03-05 23:01:23 +00002126 monitor_printf(mon, " tlb%i:\t"
2127 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2128 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2129 "dirty=%hhu writethrough=%hhu\n",
2130 idx,
2131 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2132 tlb->v, tlb->sh, tlb->c, tlb->pr,
2133 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002134}
2135
aliguori376253e2009-03-05 23:01:23 +00002136static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002137{
2138 CPUState *env = mon_get_cpu();
2139 int i;
2140
aliguori376253e2009-03-05 23:01:23 +00002141 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002142 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002143 print_tlb (mon, i, &env->itlb[i]);
2144 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002145 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002146 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002147}
2148
2149#endif
2150
Scott Woodbebabbc2011-08-18 10:38:42 +00002151#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002152static void tlb_info(Monitor *mon)
2153{
2154 CPUState *env1 = mon_get_cpu();
2155
2156 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2157}
2158#endif
2159
Blue Swirl314e2982011-09-11 20:22:05 +00002160static void do_info_mtree(Monitor *mon)
2161{
2162 mtree_info((fprintf_function)monitor_printf, mon);
2163}
2164
aliguori030ea372009-04-21 22:30:47 +00002165static void do_info_numa(Monitor *mon)
2166{
aliguorib28b6232009-04-22 20:20:29 +00002167 int i;
aliguori030ea372009-04-21 22:30:47 +00002168 CPUState *env;
2169
2170 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2171 for (i = 0; i < nb_numa_nodes; i++) {
2172 monitor_printf(mon, "node %d cpus:", i);
2173 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2174 if (env->numa_node == i) {
2175 monitor_printf(mon, " %d", env->cpu_index);
2176 }
2177 }
2178 monitor_printf(mon, "\n");
2179 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2180 node_mem[i] >> 20);
2181 }
2182}
2183
bellard5f1ce942006-02-08 22:40:15 +00002184#ifdef CONFIG_PROFILER
2185
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002186int64_t qemu_time;
2187int64_t dev_time;
2188
aliguori376253e2009-03-05 23:01:23 +00002189static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002190{
2191 int64_t total;
2192 total = qemu_time;
2193 if (total == 0)
2194 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002195 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002196 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002197 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002198 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002199 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002200 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002201}
2202#else
aliguori376253e2009-03-05 23:01:23 +00002203static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002204{
aliguori376253e2009-03-05 23:01:23 +00002205 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002206}
2207#endif
2208
bellardec36b692006-07-16 18:57:03 +00002209/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002210static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002211
aliguori376253e2009-03-05 23:01:23 +00002212static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002213{
2214 int i;
2215 CaptureState *s;
2216
2217 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002218 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002219 s->ops.info (s->opaque);
2220 }
2221}
2222
Blue Swirl23130862009-06-06 08:22:04 +00002223#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002224static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002225{
2226 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002227 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002228 CaptureState *s;
2229
2230 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2231 if (i == n) {
2232 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002233 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002234 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002235 return;
2236 }
2237 }
2238}
2239
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002240static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002241{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002242 const char *path = qdict_get_str(qdict, "path");
2243 int has_freq = qdict_haskey(qdict, "freq");
2244 int freq = qdict_get_try_int(qdict, "freq", -1);
2245 int has_bits = qdict_haskey(qdict, "bits");
2246 int bits = qdict_get_try_int(qdict, "bits", -1);
2247 int has_channels = qdict_haskey(qdict, "nchannels");
2248 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002249 CaptureState *s;
2250
Anthony Liguori7267c092011-08-20 22:09:37 -05002251 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002252
2253 freq = has_freq ? freq : 44100;
2254 bits = has_bits ? bits : 16;
2255 nchannels = has_channels ? nchannels : 2;
2256
2257 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002258 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002259 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002260 return;
bellardec36b692006-07-16 18:57:03 +00002261 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002262 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002263}
2264#endif
2265
aurel32dc1c0b72008-04-27 23:52:12 +00002266#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002267static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002268{
2269 CPUState *env;
2270
2271 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2272 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2273 }
2274
2275 return 0;
2276}
2277#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002278static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002279{
2280 qerror_report(QERR_UNSUPPORTED);
2281 return -1;
2282}
aurel32dc1c0b72008-04-27 23:52:12 +00002283#endif
2284
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002285static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002286{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002287 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002288
aliguori76655d62009-03-06 20:27:37 +00002289 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002290 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002291 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002292 return acl;
2293}
aliguori76655d62009-03-06 20:27:37 +00002294
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002295static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002296{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002297 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002298 qemu_acl *acl = find_acl(mon, aclname);
2299 qemu_acl_entry *entry;
2300 int i = 0;
2301
2302 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002303 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002304 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002305 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002306 i++;
2307 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002308 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002309 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002310 }
2311}
2312
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002313static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002314{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002315 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002316 qemu_acl *acl = find_acl(mon, aclname);
2317
2318 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002319 qemu_acl_reset(acl);
2320 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002321 }
2322}
aliguori76655d62009-03-06 20:27:37 +00002323
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002324static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002325{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002326 const char *aclname = qdict_get_str(qdict, "aclname");
2327 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002328 qemu_acl *acl = find_acl(mon, aclname);
2329
2330 if (acl) {
2331 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002332 acl->defaultDeny = 0;
2333 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002334 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002335 acl->defaultDeny = 1;
2336 monitor_printf(mon, "acl: policy set to 'deny'\n");
2337 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002338 monitor_printf(mon, "acl: unknown policy '%s', "
2339 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002340 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002341 }
2342}
aliguori76655d62009-03-06 20:27:37 +00002343
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002344static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002345{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002346 const char *aclname = qdict_get_str(qdict, "aclname");
2347 const char *match = qdict_get_str(qdict, "match");
2348 const char *policy = qdict_get_str(qdict, "policy");
2349 int has_index = qdict_haskey(qdict, "index");
2350 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002351 qemu_acl *acl = find_acl(mon, aclname);
2352 int deny, ret;
2353
2354 if (acl) {
2355 if (strcmp(policy, "allow") == 0) {
2356 deny = 0;
2357 } else if (strcmp(policy, "deny") == 0) {
2358 deny = 1;
2359 } else {
2360 monitor_printf(mon, "acl: unknown policy '%s', "
2361 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002362 return;
2363 }
aliguori28a76be2009-03-06 20:27:40 +00002364 if (has_index)
2365 ret = qemu_acl_insert(acl, deny, match, index);
2366 else
2367 ret = qemu_acl_append(acl, deny, match);
2368 if (ret < 0)
2369 monitor_printf(mon, "acl: unable to add acl entry\n");
2370 else
2371 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002372 }
2373}
aliguori76655d62009-03-06 20:27:37 +00002374
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002375static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002376{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002377 const char *aclname = qdict_get_str(qdict, "aclname");
2378 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002379 qemu_acl *acl = find_acl(mon, aclname);
2380 int ret;
aliguori76655d62009-03-06 20:27:37 +00002381
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002382 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002383 ret = qemu_acl_remove(acl, match);
2384 if (ret < 0)
2385 monitor_printf(mon, "acl: no matching acl entry\n");
2386 else
2387 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002388 }
2389}
2390
Huang Ying79c4f6b2009-06-23 10:05:14 +08002391#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002392static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002393{
2394 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002395 int cpu_index = qdict_get_int(qdict, "cpu_index");
2396 int bank = qdict_get_int(qdict, "bank");
2397 uint64_t status = qdict_get_int(qdict, "status");
2398 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2399 uint64_t addr = qdict_get_int(qdict, "addr");
2400 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002401 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002402
Jan Kiszka747461c2011-03-02 08:56:10 +01002403 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2404 flags |= MCE_INJECT_BROADCAST;
2405 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002406 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002407 if (cenv->cpu_index == cpu_index) {
2408 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002409 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002410 break;
2411 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002412 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002413}
2414#endif
2415
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002416static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002417{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002418 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002419 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002420 int fd;
2421
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002422 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002423 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002424 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002425 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002426 }
2427
2428 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002429 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2430 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002431 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002432 }
2433
Blue Swirl72cf2d42009-09-12 07:36:22 +00002434 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002435 if (strcmp(monfd->name, fdname) != 0) {
2436 continue;
2437 }
2438
2439 close(monfd->fd);
2440 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002441 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002442 }
2443
Anthony Liguori7267c092011-08-20 22:09:37 -05002444 monfd = g_malloc0(sizeof(mon_fd_t));
2445 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002446 monfd->fd = fd;
2447
Blue Swirl72cf2d42009-09-12 07:36:22 +00002448 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002449 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002450}
2451
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002452static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002453{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002454 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002455 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002456
Blue Swirl72cf2d42009-09-12 07:36:22 +00002457 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002458 if (strcmp(monfd->name, fdname) != 0) {
2459 continue;
2460 }
2461
Blue Swirl72cf2d42009-09-12 07:36:22 +00002462 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002463 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002464 g_free(monfd->name);
2465 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002466 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002467 }
2468
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002469 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002470 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002471}
2472
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002473static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002474{
Luiz Capitulino13548692011-07-29 15:36:43 -03002475 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002476 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002477
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002478 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002479
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002480 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002481 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002482 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002483}
2484
Mark McLoughlin7768e042009-07-22 09:11:41 +01002485int monitor_get_fd(Monitor *mon, const char *fdname)
2486{
Anthony Liguoric227f092009-10-01 16:12:16 -05002487 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002488
Blue Swirl72cf2d42009-09-12 07:36:22 +00002489 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002490 int fd;
2491
2492 if (strcmp(monfd->name, fdname) != 0) {
2493 continue;
2494 }
2495
2496 fd = monfd->fd;
2497
2498 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002499 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002500 g_free(monfd->name);
2501 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002502
2503 return fd;
2504 }
2505
2506 return -1;
2507}
2508
Wayne Xia816f8922011-10-12 11:32:41 +08002509/* mon_cmds and info_cmds would be sorted at runtime */
2510static mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002511#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002512 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002513};
2514
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002515/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002516static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 {
2518 .name = "version",
2519 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 .params = "",
2521 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002522 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 },
2524 {
2525 .name = "network",
2526 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 .params = "",
2528 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002529 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 },
2531 {
2532 .name = "chardev",
2533 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 .params = "",
2535 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002536 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 },
2538 {
2539 .name = "block",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002543 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "blockstats",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002550 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
2553 .name = "registers",
2554 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .params = "",
2556 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002557 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 },
2559 {
2560 .name = "cpus",
2561 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 .params = "",
2563 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002564 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 },
2566 {
2567 .name = "history",
2568 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 .params = "",
2570 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002571 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002573#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2574 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 {
2576 .name = "irq",
2577 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 .params = "",
2579 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002580#ifdef TARGET_SPARC
2581 .mhandler.info = sun4m_irq_info,
2582#elif defined(TARGET_LM32)
2583 .mhandler.info = lm32_irq_info,
2584#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002585 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002586#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
2588 {
2589 .name = "pic",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002593#ifdef TARGET_SPARC
2594 .mhandler.info = sun4m_pic_info,
2595#elif defined(TARGET_LM32)
2596 .mhandler.info = lm32_do_pic_info,
2597#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002598 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002599#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002601#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 {
2603 .name = "pci",
2604 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 .params = "",
2606 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002607 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002609#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2610 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 {
2612 .name = "tlb",
2613 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 .params = "",
2615 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002616 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 },
aurel327c664e22009-03-03 06:12:22 +00002618#endif
2619#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 {
2621 .name = "mem",
2622 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 .params = "",
2624 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002625 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 },
bellardb86bda52004-09-18 19:32:46 +00002627#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 {
Blue Swirl314e2982011-09-11 20:22:05 +00002629 .name = "mtree",
2630 .args_type = "",
2631 .params = "",
2632 .help = "show memory tree",
2633 .mhandler.info = do_info_mtree,
2634 },
2635 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 .name = "jit",
2637 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 .params = "",
2639 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002640 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 },
2642 {
2643 .name = "kvm",
2644 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 .params = "",
2646 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002647 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 },
2649 {
2650 .name = "numa",
2651 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 .params = "",
2653 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002654 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 },
2656 {
2657 .name = "usb",
2658 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 .params = "",
2660 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002661 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 },
2663 {
2664 .name = "usbhost",
2665 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .params = "",
2667 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002668 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "profile",
2672 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 .params = "",
2674 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002675 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 },
2677 {
2678 .name = "capture",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002682 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
2684 {
2685 .name = "snapshots",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002689 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "status",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002696 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
2699 .name = "pcmcia",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002703 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "mice",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002710 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
2713 .name = "vnc",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002717 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002719#if defined(CONFIG_SPICE)
2720 {
2721 .name = "spice",
2722 .args_type = "",
2723 .params = "",
2724 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002725 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002726 },
2727#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 {
2729 .name = "name",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002733 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
2735 {
2736 .name = "uuid",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002740 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 },
j_mayer76a66252007-03-07 08:32:30 +00002742#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 {
2744 .name = "cpustats",
2745 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 .params = "",
2747 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002748 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 },
j_mayer76a66252007-03-07 08:32:30 +00002750#endif
blueswir131a60e22007-10-26 18:42:59 +00002751#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 {
2753 .name = "usernet",
2754 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 .params = "",
2756 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002757 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 },
blueswir131a60e22007-10-26 18:42:59 +00002759#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 {
2761 .name = "migrate",
2762 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 .params = "",
2764 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002765 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 },
2767 {
2768 .name = "balloon",
2769 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 .params = "",
2771 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002772 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 },
2774 {
2775 .name = "qtree",
2776 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 .params = "",
2778 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002779 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 },
2781 {
2782 .name = "qdm",
2783 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 .params = "",
2785 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002786 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 },
2788 {
2789 .name = "roms",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002793 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 },
Lluís6d8a7642011-08-31 20:30:43 +02002795#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05302796 {
2797 .name = "trace",
2798 .args_type = "",
2799 .params = "",
2800 .help = "show current contents of trace buffer",
2801 .mhandler.info = do_info_trace,
2802 },
Lluís31965ae2011-08-31 20:31:24 +02002803#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05302804 {
2805 .name = "trace-events",
2806 .args_type = "",
2807 .params = "",
2808 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002809 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302810 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 {
2812 .name = NULL,
2813 },
bellard9dc39cb2004-03-14 21:38:27 +00002814};
2815
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002816static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002817#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002818 { /* NULL */ },
2819};
2820
bellard9307c4c2004-04-04 12:57:25 +00002821/*******************************************************************/
2822
2823static const char *pch;
2824static jmp_buf expr_env;
2825
bellard92a31b12005-02-10 22:00:52 +00002826#define MD_TLONG 0
2827#define MD_I32 1
2828
bellard9307c4c2004-04-04 12:57:25 +00002829typedef struct MonitorDef {
2830 const char *name;
2831 int offset;
blueswir18662d652008-10-02 18:32:44 +00002832 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002833 int type;
bellard9307c4c2004-04-04 12:57:25 +00002834} MonitorDef;
2835
bellard57206fd2004-04-25 18:54:52 +00002836#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002837static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002838{
bellard6a00d602005-11-21 23:25:50 +00002839 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002840 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002841}
2842#endif
2843
bellarda541f292004-04-12 20:39:29 +00002844#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002845static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002846{
bellard6a00d602005-11-21 23:25:50 +00002847 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002848 unsigned int u;
2849 int i;
2850
2851 u = 0;
2852 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002853 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002854
2855 return u;
2856}
2857
blueswir18662d652008-10-02 18:32:44 +00002858static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002859{
bellard6a00d602005-11-21 23:25:50 +00002860 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002861 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002862}
2863
blueswir18662d652008-10-02 18:32:44 +00002864static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002865{
bellard6a00d602005-11-21 23:25:50 +00002866 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002867 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002868}
bellard9fddaa02004-05-21 12:59:32 +00002869
blueswir18662d652008-10-02 18:32:44 +00002870static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002871{
bellard6a00d602005-11-21 23:25:50 +00002872 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002873 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002874}
2875
blueswir18662d652008-10-02 18:32:44 +00002876static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002877{
bellard6a00d602005-11-21 23:25:50 +00002878 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002879 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002880}
2881
blueswir18662d652008-10-02 18:32:44 +00002882static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002883{
bellard6a00d602005-11-21 23:25:50 +00002884 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002885 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002886}
bellarda541f292004-04-12 20:39:29 +00002887#endif
2888
bellarde95c8d52004-09-30 22:22:08 +00002889#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002890#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002891static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002892{
bellard6a00d602005-11-21 23:25:50 +00002893 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002894
2895 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002896}
bellard7b936c02005-10-30 17:05:13 +00002897#endif
bellarde95c8d52004-09-30 22:22:08 +00002898
blueswir18662d652008-10-02 18:32:44 +00002899static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002900{
bellard6a00d602005-11-21 23:25:50 +00002901 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002902 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002903}
2904#endif
2905
blueswir18662d652008-10-02 18:32:44 +00002906static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002907#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002908
2909#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002910 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002911 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002912 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002913
bellard9307c4c2004-04-04 12:57:25 +00002914 { "eax", offsetof(CPUState, regs[0]) },
2915 { "ecx", offsetof(CPUState, regs[1]) },
2916 { "edx", offsetof(CPUState, regs[2]) },
2917 { "ebx", offsetof(CPUState, regs[3]) },
2918 { "esp|sp", offsetof(CPUState, regs[4]) },
2919 { "ebp|fp", offsetof(CPUState, regs[5]) },
2920 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002921 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002922#ifdef TARGET_X86_64
2923 { "r8", offsetof(CPUState, regs[8]) },
2924 { "r9", offsetof(CPUState, regs[9]) },
2925 { "r10", offsetof(CPUState, regs[10]) },
2926 { "r11", offsetof(CPUState, regs[11]) },
2927 { "r12", offsetof(CPUState, regs[12]) },
2928 { "r13", offsetof(CPUState, regs[13]) },
2929 { "r14", offsetof(CPUState, regs[14]) },
2930 { "r15", offsetof(CPUState, regs[15]) },
2931#endif
bellard9307c4c2004-04-04 12:57:25 +00002932 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002933 { "eip", offsetof(CPUState, eip) },
2934 SEG("cs", R_CS)
2935 SEG("ds", R_DS)
2936 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002937 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002938 SEG("fs", R_FS)
2939 SEG("gs", R_GS)
2940 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002941#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002942 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002943 { "r0", offsetof(CPUState, gpr[0]) },
2944 { "r1", offsetof(CPUState, gpr[1]) },
2945 { "r2", offsetof(CPUState, gpr[2]) },
2946 { "r3", offsetof(CPUState, gpr[3]) },
2947 { "r4", offsetof(CPUState, gpr[4]) },
2948 { "r5", offsetof(CPUState, gpr[5]) },
2949 { "r6", offsetof(CPUState, gpr[6]) },
2950 { "r7", offsetof(CPUState, gpr[7]) },
2951 { "r8", offsetof(CPUState, gpr[8]) },
2952 { "r9", offsetof(CPUState, gpr[9]) },
2953 { "r10", offsetof(CPUState, gpr[10]) },
2954 { "r11", offsetof(CPUState, gpr[11]) },
2955 { "r12", offsetof(CPUState, gpr[12]) },
2956 { "r13", offsetof(CPUState, gpr[13]) },
2957 { "r14", offsetof(CPUState, gpr[14]) },
2958 { "r15", offsetof(CPUState, gpr[15]) },
2959 { "r16", offsetof(CPUState, gpr[16]) },
2960 { "r17", offsetof(CPUState, gpr[17]) },
2961 { "r18", offsetof(CPUState, gpr[18]) },
2962 { "r19", offsetof(CPUState, gpr[19]) },
2963 { "r20", offsetof(CPUState, gpr[20]) },
2964 { "r21", offsetof(CPUState, gpr[21]) },
2965 { "r22", offsetof(CPUState, gpr[22]) },
2966 { "r23", offsetof(CPUState, gpr[23]) },
2967 { "r24", offsetof(CPUState, gpr[24]) },
2968 { "r25", offsetof(CPUState, gpr[25]) },
2969 { "r26", offsetof(CPUState, gpr[26]) },
2970 { "r27", offsetof(CPUState, gpr[27]) },
2971 { "r28", offsetof(CPUState, gpr[28]) },
2972 { "r29", offsetof(CPUState, gpr[29]) },
2973 { "r30", offsetof(CPUState, gpr[30]) },
2974 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002975 /* Floating point registers */
2976 { "f0", offsetof(CPUState, fpr[0]) },
2977 { "f1", offsetof(CPUState, fpr[1]) },
2978 { "f2", offsetof(CPUState, fpr[2]) },
2979 { "f3", offsetof(CPUState, fpr[3]) },
2980 { "f4", offsetof(CPUState, fpr[4]) },
2981 { "f5", offsetof(CPUState, fpr[5]) },
2982 { "f6", offsetof(CPUState, fpr[6]) },
2983 { "f7", offsetof(CPUState, fpr[7]) },
2984 { "f8", offsetof(CPUState, fpr[8]) },
2985 { "f9", offsetof(CPUState, fpr[9]) },
2986 { "f10", offsetof(CPUState, fpr[10]) },
2987 { "f11", offsetof(CPUState, fpr[11]) },
2988 { "f12", offsetof(CPUState, fpr[12]) },
2989 { "f13", offsetof(CPUState, fpr[13]) },
2990 { "f14", offsetof(CPUState, fpr[14]) },
2991 { "f15", offsetof(CPUState, fpr[15]) },
2992 { "f16", offsetof(CPUState, fpr[16]) },
2993 { "f17", offsetof(CPUState, fpr[17]) },
2994 { "f18", offsetof(CPUState, fpr[18]) },
2995 { "f19", offsetof(CPUState, fpr[19]) },
2996 { "f20", offsetof(CPUState, fpr[20]) },
2997 { "f21", offsetof(CPUState, fpr[21]) },
2998 { "f22", offsetof(CPUState, fpr[22]) },
2999 { "f23", offsetof(CPUState, fpr[23]) },
3000 { "f24", offsetof(CPUState, fpr[24]) },
3001 { "f25", offsetof(CPUState, fpr[25]) },
3002 { "f26", offsetof(CPUState, fpr[26]) },
3003 { "f27", offsetof(CPUState, fpr[27]) },
3004 { "f28", offsetof(CPUState, fpr[28]) },
3005 { "f29", offsetof(CPUState, fpr[29]) },
3006 { "f30", offsetof(CPUState, fpr[30]) },
3007 { "f31", offsetof(CPUState, fpr[31]) },
3008 { "fpscr", offsetof(CPUState, fpscr) },
3009 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003010 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003011 { "lr", offsetof(CPUState, lr) },
3012 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003013 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003014 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003015 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003016 { "msr", 0, &monitor_get_msr, },
3017 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003018 { "tbu", 0, &monitor_get_tbu, },
3019 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003020#if defined(TARGET_PPC64)
3021 /* Address space register */
3022 { "asr", offsetof(CPUState, asr) },
3023#endif
3024 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003025 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003026 { "sr0", offsetof(CPUState, sr[0]) },
3027 { "sr1", offsetof(CPUState, sr[1]) },
3028 { "sr2", offsetof(CPUState, sr[2]) },
3029 { "sr3", offsetof(CPUState, sr[3]) },
3030 { "sr4", offsetof(CPUState, sr[4]) },
3031 { "sr5", offsetof(CPUState, sr[5]) },
3032 { "sr6", offsetof(CPUState, sr[6]) },
3033 { "sr7", offsetof(CPUState, sr[7]) },
3034 { "sr8", offsetof(CPUState, sr[8]) },
3035 { "sr9", offsetof(CPUState, sr[9]) },
3036 { "sr10", offsetof(CPUState, sr[10]) },
3037 { "sr11", offsetof(CPUState, sr[11]) },
3038 { "sr12", offsetof(CPUState, sr[12]) },
3039 { "sr13", offsetof(CPUState, sr[13]) },
3040 { "sr14", offsetof(CPUState, sr[14]) },
3041 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003042 /* Too lazy to put BATs... */
3043 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3044
3045 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3046 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3047 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3048 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3049 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3050 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3051 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3052 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3053 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3054 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3055 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3056 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3057 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3058 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3059 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3060 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3061 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3062 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3063 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3064 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3065 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3066 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3067 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3068 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3069 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3070 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3071 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3072 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3073 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3074 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3075 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3076 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3077 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3078 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3079 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3080 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3081 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3082 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3083 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3084 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3085 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3086 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3087 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3088 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3089 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3090 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3091 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3092 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3093 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3094 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3095 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3096 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3097 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3098 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3099 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3100 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3101 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3102 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3103 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3104 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3105 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3106 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3107 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3108 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3109 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3110 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3111
bellarde95c8d52004-09-30 22:22:08 +00003112#elif defined(TARGET_SPARC)
3113 { "g0", offsetof(CPUState, gregs[0]) },
3114 { "g1", offsetof(CPUState, gregs[1]) },
3115 { "g2", offsetof(CPUState, gregs[2]) },
3116 { "g3", offsetof(CPUState, gregs[3]) },
3117 { "g4", offsetof(CPUState, gregs[4]) },
3118 { "g5", offsetof(CPUState, gregs[5]) },
3119 { "g6", offsetof(CPUState, gregs[6]) },
3120 { "g7", offsetof(CPUState, gregs[7]) },
3121 { "o0", 0, monitor_get_reg },
3122 { "o1", 1, monitor_get_reg },
3123 { "o2", 2, monitor_get_reg },
3124 { "o3", 3, monitor_get_reg },
3125 { "o4", 4, monitor_get_reg },
3126 { "o5", 5, monitor_get_reg },
3127 { "o6", 6, monitor_get_reg },
3128 { "o7", 7, monitor_get_reg },
3129 { "l0", 8, monitor_get_reg },
3130 { "l1", 9, monitor_get_reg },
3131 { "l2", 10, monitor_get_reg },
3132 { "l3", 11, monitor_get_reg },
3133 { "l4", 12, monitor_get_reg },
3134 { "l5", 13, monitor_get_reg },
3135 { "l6", 14, monitor_get_reg },
3136 { "l7", 15, monitor_get_reg },
3137 { "i0", 16, monitor_get_reg },
3138 { "i1", 17, monitor_get_reg },
3139 { "i2", 18, monitor_get_reg },
3140 { "i3", 19, monitor_get_reg },
3141 { "i4", 20, monitor_get_reg },
3142 { "i5", 21, monitor_get_reg },
3143 { "i6", 22, monitor_get_reg },
3144 { "i7", 23, monitor_get_reg },
3145 { "pc", offsetof(CPUState, pc) },
3146 { "npc", offsetof(CPUState, npc) },
3147 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003148#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003149 { "psr", 0, &monitor_get_psr, },
3150 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003151#endif
bellarde95c8d52004-09-30 22:22:08 +00003152 { "tbr", offsetof(CPUState, tbr) },
3153 { "fsr", offsetof(CPUState, fsr) },
Richard Henderson30038fd2011-10-17 10:42:49 -07003154 { "f0", offsetof(CPUState, fpr[0].l.upper) },
3155 { "f1", offsetof(CPUState, fpr[0].l.lower) },
3156 { "f2", offsetof(CPUState, fpr[1].l.upper) },
3157 { "f3", offsetof(CPUState, fpr[1].l.lower) },
3158 { "f4", offsetof(CPUState, fpr[2].l.upper) },
3159 { "f5", offsetof(CPUState, fpr[2].l.lower) },
3160 { "f6", offsetof(CPUState, fpr[3].l.upper) },
3161 { "f7", offsetof(CPUState, fpr[3].l.lower) },
3162 { "f8", offsetof(CPUState, fpr[4].l.upper) },
3163 { "f9", offsetof(CPUState, fpr[4].l.lower) },
3164 { "f10", offsetof(CPUState, fpr[5].l.upper) },
3165 { "f11", offsetof(CPUState, fpr[5].l.lower) },
3166 { "f12", offsetof(CPUState, fpr[6].l.upper) },
3167 { "f13", offsetof(CPUState, fpr[6].l.lower) },
3168 { "f14", offsetof(CPUState, fpr[7].l.upper) },
3169 { "f15", offsetof(CPUState, fpr[7].l.lower) },
3170 { "f16", offsetof(CPUState, fpr[8].l.upper) },
3171 { "f17", offsetof(CPUState, fpr[8].l.lower) },
3172 { "f18", offsetof(CPUState, fpr[9].l.upper) },
3173 { "f19", offsetof(CPUState, fpr[9].l.lower) },
3174 { "f20", offsetof(CPUState, fpr[10].l.upper) },
3175 { "f21", offsetof(CPUState, fpr[10].l.lower) },
3176 { "f22", offsetof(CPUState, fpr[11].l.upper) },
3177 { "f23", offsetof(CPUState, fpr[11].l.lower) },
3178 { "f24", offsetof(CPUState, fpr[12].l.upper) },
3179 { "f25", offsetof(CPUState, fpr[12].l.lower) },
3180 { "f26", offsetof(CPUState, fpr[13].l.upper) },
3181 { "f27", offsetof(CPUState, fpr[13].l.lower) },
3182 { "f28", offsetof(CPUState, fpr[14].l.upper) },
3183 { "f29", offsetof(CPUState, fpr[14].l.lower) },
3184 { "f30", offsetof(CPUState, fpr[15].l.upper) },
3185 { "f31", offsetof(CPUState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003186#ifdef TARGET_SPARC64
Richard Henderson30038fd2011-10-17 10:42:49 -07003187 { "f32", offsetof(CPUState, fpr[16]) },
3188 { "f34", offsetof(CPUState, fpr[17]) },
3189 { "f36", offsetof(CPUState, fpr[18]) },
3190 { "f38", offsetof(CPUState, fpr[19]) },
3191 { "f40", offsetof(CPUState, fpr[20]) },
3192 { "f42", offsetof(CPUState, fpr[21]) },
3193 { "f44", offsetof(CPUState, fpr[22]) },
3194 { "f46", offsetof(CPUState, fpr[23]) },
3195 { "f48", offsetof(CPUState, fpr[24]) },
3196 { "f50", offsetof(CPUState, fpr[25]) },
3197 { "f52", offsetof(CPUState, fpr[26]) },
3198 { "f54", offsetof(CPUState, fpr[27]) },
3199 { "f56", offsetof(CPUState, fpr[28]) },
3200 { "f58", offsetof(CPUState, fpr[29]) },
3201 { "f60", offsetof(CPUState, fpr[30]) },
3202 { "f62", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003203 { "asi", offsetof(CPUState, asi) },
3204 { "pstate", offsetof(CPUState, pstate) },
3205 { "cansave", offsetof(CPUState, cansave) },
3206 { "canrestore", offsetof(CPUState, canrestore) },
3207 { "otherwin", offsetof(CPUState, otherwin) },
3208 { "wstate", offsetof(CPUState, wstate) },
3209 { "cleanwin", offsetof(CPUState, cleanwin) },
3210 { "fprs", offsetof(CPUState, fprs) },
3211#endif
bellard9307c4c2004-04-04 12:57:25 +00003212#endif
3213 { NULL },
3214};
3215
aliguori376253e2009-03-05 23:01:23 +00003216static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003217{
aliguori376253e2009-03-05 23:01:23 +00003218 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003219 longjmp(expr_env, 1);
3220}
3221
Markus Armbruster09b94182010-01-20 13:07:30 +01003222/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003223static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003224{
blueswir18662d652008-10-02 18:32:44 +00003225 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003226 void *ptr;
3227
bellard9307c4c2004-04-04 12:57:25 +00003228 for(md = monitor_defs; md->name != NULL; md++) {
3229 if (compare_cmd(name, md->name)) {
3230 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003231 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003232 } else {
bellard6a00d602005-11-21 23:25:50 +00003233 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003234 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003235 switch(md->type) {
3236 case MD_I32:
3237 *pval = *(int32_t *)ptr;
3238 break;
3239 case MD_TLONG:
3240 *pval = *(target_long *)ptr;
3241 break;
3242 default:
3243 *pval = 0;
3244 break;
3245 }
bellard9307c4c2004-04-04 12:57:25 +00003246 }
3247 return 0;
3248 }
3249 }
3250 return -1;
3251}
3252
3253static void next(void)
3254{
Blue Swirl660f11b2009-07-31 21:16:51 +00003255 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003256 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003257 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003258 pch++;
3259 }
3260}
3261
aliguori376253e2009-03-05 23:01:23 +00003262static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003263
aliguori376253e2009-03-05 23:01:23 +00003264static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003265{
blueswir1c2efc952007-09-25 17:28:42 +00003266 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003267 char *p;
bellard6a00d602005-11-21 23:25:50 +00003268 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003269
3270 switch(*pch) {
3271 case '+':
3272 next();
aliguori376253e2009-03-05 23:01:23 +00003273 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003274 break;
3275 case '-':
3276 next();
aliguori376253e2009-03-05 23:01:23 +00003277 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003278 break;
3279 case '~':
3280 next();
aliguori376253e2009-03-05 23:01:23 +00003281 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003282 break;
3283 case '(':
3284 next();
aliguori376253e2009-03-05 23:01:23 +00003285 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003286 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003287 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003288 }
3289 next();
3290 break;
bellard81d09122004-07-14 17:21:37 +00003291 case '\'':
3292 pch++;
3293 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003294 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003295 n = *pch;
3296 pch++;
3297 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003298 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003299 next();
3300 break;
bellard9307c4c2004-04-04 12:57:25 +00003301 case '$':
3302 {
3303 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003304 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003305
bellard9307c4c2004-04-04 12:57:25 +00003306 pch++;
3307 q = buf;
3308 while ((*pch >= 'a' && *pch <= 'z') ||
3309 (*pch >= 'A' && *pch <= 'Z') ||
3310 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003311 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003312 if ((q - buf) < sizeof(buf) - 1)
3313 *q++ = *pch;
3314 pch++;
3315 }
blueswir1cd390082008-11-16 13:53:32 +00003316 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003317 pch++;
3318 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003319 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003320 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003321 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003322 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003323 }
3324 break;
3325 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003326 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003327 n = 0;
3328 break;
3329 default:
blueswir17743e582007-09-24 18:39:04 +00003330#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003331 n = strtoull(pch, &p, 0);
3332#else
bellard9307c4c2004-04-04 12:57:25 +00003333 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003334#endif
bellard9307c4c2004-04-04 12:57:25 +00003335 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003336 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003337 }
3338 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003339 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003340 pch++;
3341 break;
3342 }
3343 return n;
3344}
3345
3346
aliguori376253e2009-03-05 23:01:23 +00003347static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003348{
blueswir1c2efc952007-09-25 17:28:42 +00003349 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003350 int op;
ths3b46e622007-09-17 08:09:54 +00003351
aliguori376253e2009-03-05 23:01:23 +00003352 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003353 for(;;) {
3354 op = *pch;
3355 if (op != '*' && op != '/' && op != '%')
3356 break;
3357 next();
aliguori376253e2009-03-05 23:01:23 +00003358 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003359 switch(op) {
3360 default:
3361 case '*':
3362 val *= val2;
3363 break;
3364 case '/':
3365 case '%':
ths5fafdf22007-09-16 21:08:06 +00003366 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003367 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003368 if (op == '/')
3369 val /= val2;
3370 else
3371 val %= val2;
3372 break;
3373 }
3374 }
3375 return val;
3376}
3377
aliguori376253e2009-03-05 23:01:23 +00003378static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003379{
blueswir1c2efc952007-09-25 17:28:42 +00003380 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003381 int op;
bellard9307c4c2004-04-04 12:57:25 +00003382
aliguori376253e2009-03-05 23:01:23 +00003383 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003384 for(;;) {
3385 op = *pch;
3386 if (op != '&' && op != '|' && op != '^')
3387 break;
3388 next();
aliguori376253e2009-03-05 23:01:23 +00003389 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003390 switch(op) {
3391 default:
3392 case '&':
3393 val &= val2;
3394 break;
3395 case '|':
3396 val |= val2;
3397 break;
3398 case '^':
3399 val ^= val2;
3400 break;
3401 }
3402 }
3403 return val;
3404}
3405
aliguori376253e2009-03-05 23:01:23 +00003406static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003407{
blueswir1c2efc952007-09-25 17:28:42 +00003408 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003409 int op;
bellard9307c4c2004-04-04 12:57:25 +00003410
aliguori376253e2009-03-05 23:01:23 +00003411 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003412 for(;;) {
3413 op = *pch;
3414 if (op != '+' && op != '-')
3415 break;
3416 next();
aliguori376253e2009-03-05 23:01:23 +00003417 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003418 if (op == '+')
3419 val += val2;
3420 else
3421 val -= val2;
3422 }
3423 return val;
3424}
3425
aliguori376253e2009-03-05 23:01:23 +00003426static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003427{
3428 pch = *pp;
3429 if (setjmp(expr_env)) {
3430 *pp = pch;
3431 return -1;
3432 }
blueswir1cd390082008-11-16 13:53:32 +00003433 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003434 pch++;
aliguori376253e2009-03-05 23:01:23 +00003435 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003436 *pp = pch;
3437 return 0;
3438}
3439
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003440static int get_double(Monitor *mon, double *pval, const char **pp)
3441{
3442 const char *p = *pp;
3443 char *tailp;
3444 double d;
3445
3446 d = strtod(p, &tailp);
3447 if (tailp == p) {
3448 monitor_printf(mon, "Number expected\n");
3449 return -1;
3450 }
3451 if (d != d || d - d != 0) {
3452 /* NaN or infinity */
3453 monitor_printf(mon, "Bad number\n");
3454 return -1;
3455 }
3456 *pval = d;
3457 *pp = tailp;
3458 return 0;
3459}
3460
bellard9307c4c2004-04-04 12:57:25 +00003461static int get_str(char *buf, int buf_size, const char **pp)
3462{
3463 const char *p;
3464 char *q;
3465 int c;
3466
bellard81d09122004-07-14 17:21:37 +00003467 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003468 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003469 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003470 p++;
3471 if (*p == '\0') {
3472 fail:
bellard81d09122004-07-14 17:21:37 +00003473 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003474 *pp = p;
3475 return -1;
3476 }
bellard9307c4c2004-04-04 12:57:25 +00003477 if (*p == '\"') {
3478 p++;
3479 while (*p != '\0' && *p != '\"') {
3480 if (*p == '\\') {
3481 p++;
3482 c = *p++;
3483 switch(c) {
3484 case 'n':
3485 c = '\n';
3486 break;
3487 case 'r':
3488 c = '\r';
3489 break;
3490 case '\\':
3491 case '\'':
3492 case '\"':
3493 break;
3494 default:
3495 qemu_printf("unsupported escape code: '\\%c'\n", c);
3496 goto fail;
3497 }
3498 if ((q - buf) < buf_size - 1) {
3499 *q++ = c;
3500 }
3501 } else {
3502 if ((q - buf) < buf_size - 1) {
3503 *q++ = *p;
3504 }
3505 p++;
3506 }
3507 }
3508 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003509 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003510 goto fail;
3511 }
3512 p++;
3513 } else {
blueswir1cd390082008-11-16 13:53:32 +00003514 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003515 if ((q - buf) < buf_size - 1) {
3516 *q++ = *p;
3517 }
3518 p++;
3519 }
bellard9307c4c2004-04-04 12:57:25 +00003520 }
bellard81d09122004-07-14 17:21:37 +00003521 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003522 *pp = p;
3523 return 0;
3524}
3525
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003526/*
3527 * Store the command-name in cmdname, and return a pointer to
3528 * the remaining of the command string.
3529 */
3530static const char *get_command_name(const char *cmdline,
3531 char *cmdname, size_t nlen)
3532{
3533 size_t len;
3534 const char *p, *pstart;
3535
3536 p = cmdline;
3537 while (qemu_isspace(*p))
3538 p++;
3539 if (*p == '\0')
3540 return NULL;
3541 pstart = p;
3542 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3543 p++;
3544 len = p - pstart;
3545 if (len > nlen - 1)
3546 len = nlen - 1;
3547 memcpy(cmdname, pstart, len);
3548 cmdname[len] = '\0';
3549 return p;
3550}
3551
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003552/**
3553 * Read key of 'type' into 'key' and return the current
3554 * 'type' pointer.
3555 */
3556static char *key_get_info(const char *type, char **key)
3557{
3558 size_t len;
3559 char *p, *str;
3560
3561 if (*type == ',')
3562 type++;
3563
3564 p = strchr(type, ':');
3565 if (!p) {
3566 *key = NULL;
3567 return NULL;
3568 }
3569 len = p - type;
3570
Anthony Liguori7267c092011-08-20 22:09:37 -05003571 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003572 memcpy(str, type, len);
3573 str[len] = '\0';
3574
3575 *key = str;
3576 return ++p;
3577}
3578
bellard9307c4c2004-04-04 12:57:25 +00003579static int default_fmt_format = 'x';
3580static int default_fmt_size = 4;
3581
3582#define MAX_ARGS 16
3583
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003584static int is_valid_option(const char *c, const char *typestr)
3585{
3586 char option[3];
3587
3588 option[0] = '-';
3589 option[1] = *c;
3590 option[2] = '\0';
3591
3592 typestr = strstr(typestr, option);
3593 return (typestr != NULL);
3594}
3595
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003596static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3597 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003598{
3599 const mon_cmd_t *cmd;
3600
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003601 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003602 if (compare_cmd(cmdname, cmd->name)) {
3603 return cmd;
3604 }
3605 }
3606
3607 return NULL;
3608}
3609
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003610static const mon_cmd_t *monitor_find_command(const char *cmdname)
3611{
3612 return search_dispatch_table(mon_cmds, cmdname);
3613}
3614
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003615static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3616{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003617 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003618}
3619
Anthony Liguoric227f092009-10-01 16:12:16 -05003620static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003621 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003622 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003623{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003624 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003625 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003626 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003627 char cmdname[256];
3628 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003629 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003630
3631#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003632 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003633#endif
ths3b46e622007-09-17 08:09:54 +00003634
bellard9307c4c2004-04-04 12:57:25 +00003635 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003636 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3637 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003638 return NULL;
ths3b46e622007-09-17 08:09:54 +00003639
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003640 cmd = monitor_find_command(cmdname);
3641 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003642 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003643 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003644 }
bellard9307c4c2004-04-04 12:57:25 +00003645
bellard9307c4c2004-04-04 12:57:25 +00003646 /* parse the parameters */
3647 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003648 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003649 typestr = key_get_info(typestr, &key);
3650 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003651 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003652 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003653 typestr++;
3654 switch(c) {
3655 case 'F':
bellard81d09122004-07-14 17:21:37 +00003656 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003657 case 's':
3658 {
3659 int ret;
ths3b46e622007-09-17 08:09:54 +00003660
blueswir1cd390082008-11-16 13:53:32 +00003661 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003662 p++;
3663 if (*typestr == '?') {
3664 typestr++;
3665 if (*p == '\0') {
3666 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003667 break;
bellard9307c4c2004-04-04 12:57:25 +00003668 }
3669 }
3670 ret = get_str(buf, sizeof(buf), &p);
3671 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003672 switch(c) {
3673 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003674 monitor_printf(mon, "%s: filename expected\n",
3675 cmdname);
bellard81d09122004-07-14 17:21:37 +00003676 break;
3677 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003678 monitor_printf(mon, "%s: block device name expected\n",
3679 cmdname);
bellard81d09122004-07-14 17:21:37 +00003680 break;
3681 default:
aliguori376253e2009-03-05 23:01:23 +00003682 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003683 break;
3684 }
bellard9307c4c2004-04-04 12:57:25 +00003685 goto fail;
3686 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003687 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003688 }
3689 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003690 case 'O':
3691 {
3692 QemuOptsList *opts_list;
3693 QemuOpts *opts;
3694
3695 opts_list = qemu_find_opts(key);
3696 if (!opts_list || opts_list->desc->name) {
3697 goto bad_type;
3698 }
3699 while (qemu_isspace(*p)) {
3700 p++;
3701 }
3702 if (!*p)
3703 break;
3704 if (get_str(buf, sizeof(buf), &p) < 0) {
3705 goto fail;
3706 }
3707 opts = qemu_opts_parse(opts_list, buf, 1);
3708 if (!opts) {
3709 goto fail;
3710 }
3711 qemu_opts_to_qdict(opts, qdict);
3712 qemu_opts_del(opts);
3713 }
3714 break;
bellard9307c4c2004-04-04 12:57:25 +00003715 case '/':
3716 {
3717 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003718
blueswir1cd390082008-11-16 13:53:32 +00003719 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003720 p++;
3721 if (*p == '/') {
3722 /* format found */
3723 p++;
3724 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003725 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003726 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003727 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003728 count = count * 10 + (*p - '0');
3729 p++;
3730 }
3731 }
3732 size = -1;
3733 format = -1;
3734 for(;;) {
3735 switch(*p) {
3736 case 'o':
3737 case 'd':
3738 case 'u':
3739 case 'x':
3740 case 'i':
3741 case 'c':
3742 format = *p++;
3743 break;
3744 case 'b':
3745 size = 1;
3746 p++;
3747 break;
3748 case 'h':
3749 size = 2;
3750 p++;
3751 break;
3752 case 'w':
3753 size = 4;
3754 p++;
3755 break;
3756 case 'g':
3757 case 'L':
3758 size = 8;
3759 p++;
3760 break;
3761 default:
3762 goto next;
3763 }
3764 }
3765 next:
blueswir1cd390082008-11-16 13:53:32 +00003766 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003767 monitor_printf(mon, "invalid char in format: '%c'\n",
3768 *p);
bellard9307c4c2004-04-04 12:57:25 +00003769 goto fail;
3770 }
bellard9307c4c2004-04-04 12:57:25 +00003771 if (format < 0)
3772 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003773 if (format != 'i') {
3774 /* for 'i', not specifying a size gives -1 as size */
3775 if (size < 0)
3776 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003777 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003778 }
bellard9307c4c2004-04-04 12:57:25 +00003779 default_fmt_format = format;
3780 } else {
3781 count = 1;
3782 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003783 if (format != 'i') {
3784 size = default_fmt_size;
3785 } else {
3786 size = -1;
3787 }
bellard9307c4c2004-04-04 12:57:25 +00003788 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003789 qdict_put(qdict, "count", qint_from_int(count));
3790 qdict_put(qdict, "format", qint_from_int(format));
3791 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003792 }
3793 break;
3794 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003795 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003796 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003797 {
blueswir1c2efc952007-09-25 17:28:42 +00003798 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003799
blueswir1cd390082008-11-16 13:53:32 +00003800 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003801 p++;
bellard34405572004-06-08 00:55:58 +00003802 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003803 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003804 if (*p == '\0') {
3805 typestr++;
3806 break;
3807 }
bellard34405572004-06-08 00:55:58 +00003808 } else {
3809 if (*p == '.') {
3810 p++;
blueswir1cd390082008-11-16 13:53:32 +00003811 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003812 p++;
bellard34405572004-06-08 00:55:58 +00003813 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003814 typestr++;
3815 break;
bellard34405572004-06-08 00:55:58 +00003816 }
3817 }
bellard13224a82006-07-14 22:03:35 +00003818 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003819 }
aliguori376253e2009-03-05 23:01:23 +00003820 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003821 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003822 /* Check if 'i' is greater than 32-bit */
3823 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3824 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3825 monitor_printf(mon, "integer is for 32-bit values\n");
3826 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003827 } else if (c == 'M') {
3828 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003829 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003830 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003831 }
3832 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003833 case 'o':
3834 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003835 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003836 char *end;
3837
3838 while (qemu_isspace(*p)) {
3839 p++;
3840 }
3841 if (*typestr == '?') {
3842 typestr++;
3843 if (*p == '\0') {
3844 break;
3845 }
3846 }
3847 val = strtosz(p, &end);
3848 if (val < 0) {
3849 monitor_printf(mon, "invalid size\n");
3850 goto fail;
3851 }
3852 qdict_put(qdict, key, qint_from_int(val));
3853 p = end;
3854 }
3855 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003856 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003857 {
3858 double val;
3859
3860 while (qemu_isspace(*p))
3861 p++;
3862 if (*typestr == '?') {
3863 typestr++;
3864 if (*p == '\0') {
3865 break;
3866 }
3867 }
3868 if (get_double(mon, &val, &p) < 0) {
3869 goto fail;
3870 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003871 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003872 switch (*p) {
3873 case 'm':
3874 val /= 1e3; p += 2; break;
3875 case 'u':
3876 val /= 1e6; p += 2; break;
3877 case 'n':
3878 val /= 1e9; p += 2; break;
3879 }
3880 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003881 if (*p && !qemu_isspace(*p)) {
3882 monitor_printf(mon, "Unknown unit suffix\n");
3883 goto fail;
3884 }
3885 qdict_put(qdict, key, qfloat_from_double(val));
3886 }
3887 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003888 case 'b':
3889 {
3890 const char *beg;
3891 int val;
3892
3893 while (qemu_isspace(*p)) {
3894 p++;
3895 }
3896 beg = p;
3897 while (qemu_isgraph(*p)) {
3898 p++;
3899 }
3900 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3901 val = 1;
3902 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3903 val = 0;
3904 } else {
3905 monitor_printf(mon, "Expected 'on' or 'off'\n");
3906 goto fail;
3907 }
3908 qdict_put(qdict, key, qbool_from_int(val));
3909 }
3910 break;
bellard9307c4c2004-04-04 12:57:25 +00003911 case '-':
3912 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003913 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003914 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003915 /* option */
ths3b46e622007-09-17 08:09:54 +00003916
bellard9307c4c2004-04-04 12:57:25 +00003917 c = *typestr++;
3918 if (c == '\0')
3919 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003920 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003921 p++;
bellard9307c4c2004-04-04 12:57:25 +00003922 if (*p == '-') {
3923 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003924 if(c != *p) {
3925 if(!is_valid_option(p, typestr)) {
3926
3927 monitor_printf(mon, "%s: unsupported option -%c\n",
3928 cmdname, *p);
3929 goto fail;
3930 } else {
3931 skip_key = 1;
3932 }
bellard9307c4c2004-04-04 12:57:25 +00003933 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003934 if(skip_key) {
3935 p = tmp;
3936 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003937 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003938 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003939 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003940 }
bellard9307c4c2004-04-04 12:57:25 +00003941 }
bellard9307c4c2004-04-04 12:57:25 +00003942 }
3943 break;
3944 default:
3945 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003946 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003947 goto fail;
3948 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003949 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003950 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003951 }
3952 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003953 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003954 p++;
3955 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003956 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3957 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003958 goto fail;
3959 }
3960
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003961 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003962
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003963fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05003964 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003965 return NULL;
3966}
3967
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003968void monitor_set_error(Monitor *mon, QError *qerror)
3969{
3970 /* report only the first error */
3971 if (!mon->error) {
3972 mon->error = qerror;
3973 } else {
3974 MON_DEBUG("Additional error report at %s:%d\n",
3975 qerror->file, qerror->linenr);
3976 QDECREF(qerror);
3977 }
3978}
3979
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003980static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3981{
Luiz Capitulino6d441432010-11-22 16:35:09 -02003982 if (ret && !monitor_has_error(mon)) {
3983 /*
3984 * If it returns failure, it must have passed on error.
3985 *
3986 * Action: Report an internal error to the client if in QMP.
3987 */
3988 qerror_report(QERR_UNDEFINED_ERROR);
3989 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3990 cmd->name);
3991 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003992
3993#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02003994 if (!ret && monitor_has_error(mon)) {
3995 /*
3996 * If it returns success, it must not have passed an error.
3997 *
3998 * Action: Report the passed error to the client.
3999 */
4000 MON_DEBUG("command '%s' returned success but passed an error\n",
4001 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004002 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004003
4004 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4005 /*
4006 * Handlers should not call Monitor print functions.
4007 *
4008 * Action: Ignore them in QMP.
4009 *
4010 * (XXX: we don't check any 'info' or 'query' command here
4011 * because the user print function _is_ called by do_info(), hence
4012 * we will trigger this check. This problem will go away when we
4013 * make 'query' commands real and kill do_info())
4014 */
4015 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4016 cmd->name, mon_print_count_get(mon));
4017 }
4018#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004019}
4020
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004021static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004022{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004023 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004024 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004025
4026 qdict = qdict_new();
4027
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004028 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004029 if (!cmd)
4030 goto out;
4031
Luiz Capitulino4903de02010-09-16 11:01:32 -03004032 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004033 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004034 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004035 QObject *data = NULL;
4036
4037 /* XXX: ignores the error code */
4038 cmd->mhandler.cmd_new(mon, qdict, &data);
4039 assert(!monitor_has_error(mon));
4040 if (data) {
4041 cmd->user_print(mon, data);
4042 qobject_decref(data);
4043 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004044 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004045 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004046 }
4047
Luiz Capitulino13917be2009-10-07 13:41:54 -03004048out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004049 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004050}
4051
bellard81d09122004-07-14 17:21:37 +00004052static void cmd_completion(const char *name, const char *list)
4053{
4054 const char *p, *pstart;
4055 char cmd[128];
4056 int len;
4057
4058 p = list;
4059 for(;;) {
4060 pstart = p;
4061 p = strchr(p, '|');
4062 if (!p)
4063 p = pstart + strlen(pstart);
4064 len = p - pstart;
4065 if (len > sizeof(cmd) - 2)
4066 len = sizeof(cmd) - 2;
4067 memcpy(cmd, pstart, len);
4068 cmd[len] = '\0';
4069 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004070 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004071 }
4072 if (*p == '\0')
4073 break;
4074 p++;
4075 }
4076}
4077
4078static void file_completion(const char *input)
4079{
4080 DIR *ffs;
4081 struct dirent *d;
4082 char path[1024];
4083 char file[1024], file_prefix[1024];
4084 int input_path_len;
4085 const char *p;
4086
ths5fafdf22007-09-16 21:08:06 +00004087 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004088 if (!p) {
4089 input_path_len = 0;
4090 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004091 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004092 } else {
4093 input_path_len = p - input + 1;
4094 memcpy(path, input, input_path_len);
4095 if (input_path_len > sizeof(path) - 1)
4096 input_path_len = sizeof(path) - 1;
4097 path[input_path_len] = '\0';
4098 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4099 }
4100#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004101 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4102 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004103#endif
4104 ffs = opendir(path);
4105 if (!ffs)
4106 return;
4107 for(;;) {
4108 struct stat sb;
4109 d = readdir(ffs);
4110 if (!d)
4111 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004112
4113 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4114 continue;
4115 }
4116
bellard81d09122004-07-14 17:21:37 +00004117 if (strstart(d->d_name, file_prefix, NULL)) {
4118 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004119 if (input_path_len < sizeof(file))
4120 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4121 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004122 /* stat the file to find out if it's a directory.
4123 * In that case add a slash to speed up typing long paths
4124 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004125 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004126 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004127 }
aliguori731b0362009-03-05 23:01:42 +00004128 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004129 }
4130 }
4131 closedir(ffs);
4132}
4133
aliguori51de9762009-03-05 23:00:43 +00004134static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004135{
aliguori51de9762009-03-05 23:00:43 +00004136 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004137 const char *input = opaque;
4138
4139 if (input[0] == '\0' ||
4140 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004141 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004142 }
4143}
4144
4145/* NOTE: this parser is an approximate form of the real command parser */
4146static void parse_cmdline(const char *cmdline,
4147 int *pnb_args, char **args)
4148{
4149 const char *p;
4150 int nb_args, ret;
4151 char buf[1024];
4152
4153 p = cmdline;
4154 nb_args = 0;
4155 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004156 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004157 p++;
4158 if (*p == '\0')
4159 break;
4160 if (nb_args >= MAX_ARGS)
4161 break;
4162 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004163 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004164 nb_args++;
4165 if (ret < 0)
4166 break;
4167 }
4168 *pnb_args = nb_args;
4169}
4170
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004171static const char *next_arg_type(const char *typestr)
4172{
4173 const char *p = strchr(typestr, ':');
4174 return (p != NULL ? ++p : typestr);
4175}
4176
aliguori4c36ba32009-03-05 23:01:37 +00004177static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004178{
4179 const char *cmdname;
4180 char *args[MAX_ARGS];
4181 int nb_args, i, len;
4182 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004183 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004184 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004185
4186 parse_cmdline(cmdline, &nb_args, args);
4187#ifdef DEBUG_COMPLETION
4188 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004189 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004190 }
4191#endif
4192
4193 /* if the line ends with a space, it means we want to complete the
4194 next arg */
4195 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004196 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004197 if (nb_args >= MAX_ARGS) {
4198 goto cleanup;
4199 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004200 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004201 }
4202 if (nb_args <= 1) {
4203 /* command completion */
4204 if (nb_args == 0)
4205 cmdname = "";
4206 else
4207 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004208 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004209 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004210 cmd_completion(cmdname, cmd->name);
4211 }
4212 } else {
4213 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004214 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4215 if (compare_cmd(args[0], cmd->name)) {
4216 break;
4217 }
bellard81d09122004-07-14 17:21:37 +00004218 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004219 if (!cmd->name) {
4220 goto cleanup;
4221 }
4222
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004223 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004224 for(i = 0; i < nb_args - 2; i++) {
4225 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004226 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004227 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004228 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004229 }
4230 }
4231 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004232 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004233 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004234 }
bellard81d09122004-07-14 17:21:37 +00004235 switch(*ptype) {
4236 case 'F':
4237 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004238 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004239 file_completion(str);
4240 break;
4241 case 'B':
4242 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004243 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004244 bdrv_iterate(block_completion_it, (void *)str);
4245 break;
bellard7fe48482004-10-09 18:08:01 +00004246 case 's':
4247 /* XXX: more generic ? */
4248 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004249 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004250 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4251 cmd_completion(str, cmd->name);
4252 }
bellard64866c32006-05-07 18:03:31 +00004253 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004254 char *sep = strrchr(str, '-');
4255 if (sep)
4256 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004257 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004258 for(key = key_defs; key->name != NULL; key++) {
4259 cmd_completion(str, key->name);
4260 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004261 } else if (!strcmp(cmd->name, "help|?")) {
4262 readline_set_completion_index(cur_mon->rs, strlen(str));
4263 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4264 cmd_completion(str, cmd->name);
4265 }
bellard7fe48482004-10-09 18:08:01 +00004266 }
4267 break;
bellard81d09122004-07-14 17:21:37 +00004268 default:
4269 break;
4270 }
4271 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004272
4273cleanup:
4274 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004275 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004276 }
bellard81d09122004-07-14 17:21:37 +00004277}
4278
aliguori731b0362009-03-05 23:01:42 +00004279static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004280{
aliguori731b0362009-03-05 23:01:42 +00004281 Monitor *mon = opaque;
4282
Jan Kiszkac62313b2009-12-04 14:05:29 +01004283 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004284}
4285
Luiz Capitulino09069b12010-02-04 18:10:06 -02004286static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4287{
4288 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4289 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4290}
4291
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004292/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004293 * Argument validation rules:
4294 *
4295 * 1. The argument must exist in cmd_args qdict
4296 * 2. The argument type must be the expected one
4297 *
4298 * Special case: If the argument doesn't exist in cmd_args and
4299 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4300 * checking is skipped for it.
4301 */
4302static int check_client_args_type(const QDict *client_args,
4303 const QDict *cmd_args, int flags)
4304{
4305 const QDictEntry *ent;
4306
4307 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4308 QObject *obj;
4309 QString *arg_type;
4310 const QObject *client_arg = qdict_entry_value(ent);
4311 const char *client_arg_name = qdict_entry_key(ent);
4312
4313 obj = qdict_get(cmd_args, client_arg_name);
4314 if (!obj) {
4315 if (flags & QMP_ACCEPT_UNKNOWNS) {
4316 /* handler accepts unknowns */
4317 continue;
4318 }
4319 /* client arg doesn't exist */
4320 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4321 return -1;
4322 }
4323
4324 arg_type = qobject_to_qstring(obj);
4325 assert(arg_type != NULL);
4326
4327 /* check if argument's type is correct */
4328 switch (qstring_get_str(arg_type)[0]) {
4329 case 'F':
4330 case 'B':
4331 case 's':
4332 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4333 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4334 "string");
4335 return -1;
4336 }
4337 break;
4338 case 'i':
4339 case 'l':
4340 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004341 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004342 if (qobject_type(client_arg) != QTYPE_QINT) {
4343 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4344 "int");
4345 return -1;
4346 }
4347 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004348 case 'T':
4349 if (qobject_type(client_arg) != QTYPE_QINT &&
4350 qobject_type(client_arg) != QTYPE_QFLOAT) {
4351 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4352 "number");
4353 return -1;
4354 }
4355 break;
4356 case 'b':
4357 case '-':
4358 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4359 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4360 "bool");
4361 return -1;
4362 }
4363 break;
4364 case 'O':
4365 assert(flags & QMP_ACCEPT_UNKNOWNS);
4366 break;
4367 case '/':
4368 case '.':
4369 /*
4370 * These types are not supported by QMP and thus are not
4371 * handled here. Fall through.
4372 */
4373 default:
4374 abort();
4375 }
4376 }
4377
4378 return 0;
4379}
4380
4381/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004382 * - Check if the client has passed all mandatory args
4383 * - Set special flags for argument validation
4384 */
4385static int check_mandatory_args(const QDict *cmd_args,
4386 const QDict *client_args, int *flags)
4387{
4388 const QDictEntry *ent;
4389
4390 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4391 const char *cmd_arg_name = qdict_entry_key(ent);
4392 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4393 assert(type != NULL);
4394
4395 if (qstring_get_str(type)[0] == 'O') {
4396 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4397 *flags |= QMP_ACCEPT_UNKNOWNS;
4398 } else if (qstring_get_str(type)[0] != '-' &&
4399 qstring_get_str(type)[1] != '?' &&
4400 !qdict_haskey(client_args, cmd_arg_name)) {
4401 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4402 return -1;
4403 }
4404 }
4405
4406 return 0;
4407}
4408
4409static QDict *qdict_from_args_type(const char *args_type)
4410{
4411 int i;
4412 QDict *qdict;
4413 QString *key, *type, *cur_qs;
4414
4415 assert(args_type != NULL);
4416
4417 qdict = qdict_new();
4418
4419 if (args_type == NULL || args_type[0] == '\0') {
4420 /* no args, empty qdict */
4421 goto out;
4422 }
4423
4424 key = qstring_new();
4425 type = qstring_new();
4426
4427 cur_qs = key;
4428
4429 for (i = 0;; i++) {
4430 switch (args_type[i]) {
4431 case ',':
4432 case '\0':
4433 qdict_put(qdict, qstring_get_str(key), type);
4434 QDECREF(key);
4435 if (args_type[i] == '\0') {
4436 goto out;
4437 }
4438 type = qstring_new(); /* qdict has ref */
4439 cur_qs = key = qstring_new();
4440 break;
4441 case ':':
4442 cur_qs = type;
4443 break;
4444 default:
4445 qstring_append_chr(cur_qs, args_type[i]);
4446 break;
4447 }
4448 }
4449
4450out:
4451 return qdict;
4452}
4453
4454/*
4455 * Client argument checking rules:
4456 *
4457 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004458 * 2. Each argument provided by the client must be expected
4459 * 3. Each argument provided by the client must have the type expected
4460 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004461 */
4462static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4463{
4464 int flags, err;
4465 QDict *cmd_args;
4466
4467 cmd_args = qdict_from_args_type(cmd->args_type);
4468
4469 flags = 0;
4470 err = check_mandatory_args(cmd_args, client_args, &flags);
4471 if (err) {
4472 goto out;
4473 }
4474
Luiz Capitulino4af91932010-06-22 11:44:05 -03004475 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004476
4477out:
4478 QDECREF(cmd_args);
4479 return err;
4480}
4481
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004482/*
4483 * Input object checking rules
4484 *
4485 * 1. Input object must be a dict
4486 * 2. The "execute" key must exist
4487 * 3. The "execute" key must be a string
4488 * 4. If the "arguments" key exists, it must be a dict
4489 * 5. If the "id" key exists, it can be anything (ie. json-value)
4490 * 6. Any argument not listed above is considered invalid
4491 */
4492static QDict *qmp_check_input_obj(QObject *input_obj)
4493{
4494 const QDictEntry *ent;
4495 int has_exec_key = 0;
4496 QDict *input_dict;
4497
4498 if (qobject_type(input_obj) != QTYPE_QDICT) {
4499 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4500 return NULL;
4501 }
4502
4503 input_dict = qobject_to_qdict(input_obj);
4504
4505 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4506 const char *arg_name = qdict_entry_key(ent);
4507 const QObject *arg_obj = qdict_entry_value(ent);
4508
4509 if (!strcmp(arg_name, "execute")) {
4510 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4511 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4512 "string");
4513 return NULL;
4514 }
4515 has_exec_key = 1;
4516 } else if (!strcmp(arg_name, "arguments")) {
4517 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4518 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4519 "object");
4520 return NULL;
4521 }
4522 } else if (!strcmp(arg_name, "id")) {
4523 /* FIXME: check duplicated IDs for async commands */
4524 } else {
4525 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4526 return NULL;
4527 }
4528 }
4529
4530 if (!has_exec_key) {
4531 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4532 return NULL;
4533 }
4534
4535 return input_dict;
4536}
4537
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004538static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4539 const QDict *params)
4540{
4541 int ret;
4542 QObject *data = NULL;
4543
4544 mon_print_count_init(mon);
4545
4546 ret = cmd->mhandler.cmd_new(mon, params, &data);
4547 handler_audit(mon, cmd, ret);
4548 monitor_protocol_emitter(mon, data);
4549 qobject_decref(data);
4550}
4551
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004552static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4553{
4554 int err;
4555 QObject *obj;
4556 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004557 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004558 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004559 Monitor *mon = cur_mon;
4560
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004561 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004562
4563 obj = json_parser_parse(tokens, NULL);
4564 if (!obj) {
4565 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004566 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004567 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004568 }
4569
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004570 input = qmp_check_input_obj(obj);
4571 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004572 qobject_decref(obj);
4573 goto err_out;
4574 }
4575
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004576 mon->mc->id = qdict_get(input, "id");
4577 qobject_incref(mon->mc->id);
4578
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004579 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004580 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004581 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004582 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004583 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004584 }
4585
Anthony Liguorie3193602011-09-02 12:34:47 -05004586 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004587 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004588 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004589 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004590 }
4591
4592 obj = qdict_get(input, "arguments");
4593 if (!obj) {
4594 args = qdict_new();
4595 } else {
4596 args = qobject_to_qdict(obj);
4597 QINCREF(args);
4598 }
4599
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004600 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004601 if (err < 0) {
4602 goto err_out;
4603 }
4604
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004605 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004606 err = qmp_async_cmd_handler(mon, cmd, args);
4607 if (err) {
4608 /* emit the error response */
4609 goto err_out;
4610 }
Adam Litke940cc302010-01-25 12:18:44 -06004611 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004612 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004613 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004614
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004615 goto out;
4616
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004617err_out:
4618 monitor_protocol_emitter(mon, NULL);
4619out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004620 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004621 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004622}
4623
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004624/**
4625 * monitor_control_read(): Read and handle QMP input
4626 */
4627static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4628{
4629 Monitor *old_mon = cur_mon;
4630
4631 cur_mon = opaque;
4632
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004633 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004634
4635 cur_mon = old_mon;
4636}
4637
aliguori731b0362009-03-05 23:01:42 +00004638static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004639{
aliguori731b0362009-03-05 23:01:42 +00004640 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004641 int i;
aliguori376253e2009-03-05 23:01:23 +00004642
aliguori731b0362009-03-05 23:01:42 +00004643 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004644
aliguoricde76ee2009-03-05 23:01:51 +00004645 if (cur_mon->rs) {
4646 for (i = 0; i < size; i++)
4647 readline_handle_byte(cur_mon->rs, buf[i]);
4648 } else {
4649 if (size == 0 || buf[size - 1] != 0)
4650 monitor_printf(cur_mon, "corrupted command\n");
4651 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004652 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004653 }
aliguori731b0362009-03-05 23:01:42 +00004654
4655 cur_mon = old_mon;
4656}
aliguorid8f44602008-10-06 13:52:44 +00004657
aliguori376253e2009-03-05 23:01:23 +00004658static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004659{
aliguori731b0362009-03-05 23:01:42 +00004660 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004661 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004662 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004663}
4664
aliguoricde76ee2009-03-05 23:01:51 +00004665int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004666{
aliguoricde76ee2009-03-05 23:01:51 +00004667 if (!mon->rs)
4668 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004669 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004670 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004671}
4672
aliguori376253e2009-03-05 23:01:23 +00004673void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004674{
aliguoricde76ee2009-03-05 23:01:51 +00004675 if (!mon->rs)
4676 return;
aliguori731b0362009-03-05 23:01:42 +00004677 if (--mon->suspend_cnt == 0)
4678 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004679}
4680
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004681static QObject *get_qmp_greeting(void)
4682{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004683 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004684
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004685 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004686 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4687}
4688
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004689/**
4690 * monitor_control_event(): Print QMP gretting
4691 */
4692static void monitor_control_event(void *opaque, int event)
4693{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004694 QObject *data;
4695 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004696
Luiz Capitulino47116d12010-02-08 17:01:30 -02004697 switch (event) {
4698 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004699 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004700 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004701 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004702 monitor_json_emitter(mon, data);
4703 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004704 break;
4705 case CHR_EVENT_CLOSED:
4706 json_message_parser_destroy(&mon->mc->parser);
4707 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004708 }
4709}
4710
aliguori731b0362009-03-05 23:01:42 +00004711static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004712{
aliguori376253e2009-03-05 23:01:23 +00004713 Monitor *mon = opaque;
4714
aliguori2724b182009-03-05 23:01:47 +00004715 switch (event) {
4716 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004717 mon->mux_out = 0;
4718 if (mon->reset_seen) {
4719 readline_restart(mon->rs);
4720 monitor_resume(mon);
4721 monitor_flush(mon);
4722 } else {
4723 mon->suspend_cnt = 0;
4724 }
aliguori2724b182009-03-05 23:01:47 +00004725 break;
ths86e94de2007-01-05 22:01:59 +00004726
aliguori2724b182009-03-05 23:01:47 +00004727 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004728 if (mon->reset_seen) {
4729 if (mon->suspend_cnt == 0) {
4730 monitor_printf(mon, "\n");
4731 }
4732 monitor_flush(mon);
4733 monitor_suspend(mon);
4734 } else {
4735 mon->suspend_cnt++;
4736 }
4737 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004738 break;
4739
Amit Shahb6b8df52009-10-07 18:31:16 +05304740 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004741 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4742 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004743 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004744 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004745 }
4746 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004747 break;
4748 }
ths86e94de2007-01-05 22:01:59 +00004749}
4750
Wayne Xia816f8922011-10-12 11:32:41 +08004751static int
4752compare_mon_cmd(const void *a, const void *b)
4753{
4754 return strcmp(((const mon_cmd_t *)a)->name,
4755 ((const mon_cmd_t *)b)->name);
4756}
4757
4758static void sortcmdlist(void)
4759{
4760 int array_num;
4761 int elem_size = sizeof(mon_cmd_t);
4762
4763 array_num = sizeof(mon_cmds)/elem_size-1;
4764 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4765
4766 array_num = sizeof(info_cmds)/elem_size-1;
4767 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4768}
4769
aliguori76655d62009-03-06 20:27:37 +00004770
4771/*
4772 * Local variables:
4773 * c-indent-level: 4
4774 * c-basic-offset: 4
4775 * tab-width: 8
4776 * End:
4777 */
4778
aliguori731b0362009-03-05 23:01:42 +00004779void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004780{
aliguori731b0362009-03-05 23:01:42 +00004781 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004782 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004783
4784 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004785 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004786 is_first_init = 0;
4787 }
aliguori87127162009-03-05 23:01:29 +00004788
Anthony Liguori7267c092011-08-20 22:09:37 -05004789 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004790
aliguori87127162009-03-05 23:01:29 +00004791 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004792 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004793 if (flags & MONITOR_USE_READLINE) {
4794 mon->rs = readline_init(mon, monitor_find_completion);
4795 monitor_read_command(mon, 0);
4796 }
aliguori87127162009-03-05 23:01:29 +00004797
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004798 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004799 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004800 /* Control mode requires special handlers */
4801 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4802 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004803 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004804 } else {
4805 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4806 monitor_event, mon);
4807 }
aliguori87127162009-03-05 23:01:29 +00004808
Blue Swirl72cf2d42009-09-12 07:36:22 +00004809 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004810 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4811 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004812
4813 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004814}
4815
aliguori376253e2009-03-05 23:01:23 +00004816static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004817{
aliguoribb5fc202009-03-05 23:01:15 +00004818 BlockDriverState *bs = opaque;
4819 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004820
aliguoribb5fc202009-03-05 23:01:15 +00004821 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004822 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004823 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004824 }
aliguori731b0362009-03-05 23:01:42 +00004825 if (mon->password_completion_cb)
4826 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004827
aliguori731b0362009-03-05 23:01:42 +00004828 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004829}
aliguoric0f4ce72009-03-05 23:01:01 +00004830
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004831int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4832 BlockDriverCompletionFunc *completion_cb,
4833 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004834{
aliguoricde76ee2009-03-05 23:01:51 +00004835 int err;
4836
aliguoribb5fc202009-03-05 23:01:15 +00004837 if (!bdrv_key_required(bs)) {
4838 if (completion_cb)
4839 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004840 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004841 }
aliguoric0f4ce72009-03-05 23:01:01 +00004842
Luiz Capitulino94171e12009-12-07 21:37:00 +01004843 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004844 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004845 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004846 }
4847
aliguori376253e2009-03-05 23:01:23 +00004848 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4849 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004850
aliguori731b0362009-03-05 23:01:42 +00004851 mon->password_completion_cb = completion_cb;
4852 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004853
aliguoricde76ee2009-03-05 23:01:51 +00004854 err = monitor_read_password(mon, bdrv_password_cb, bs);
4855
4856 if (err && completion_cb)
4857 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004858
4859 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004860}