blob: f85a9d2499f6311942425bd1ba4847a31a3cb9af [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
Anthony Liguori7060b472011-09-02 12:34:50 -0500230void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000231{
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
Anthony Liguori7060b472011-09-02 12:34:50 -0500240int 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
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200516char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
517 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200518{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200519 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200520 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
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200530 if (has_cpu_index) {
531 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200532 if (ret < 0) {
533 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200534 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
535 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200536 goto out;
537 }
538 }
539
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200540 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200541 cur_mon = old_mon;
542
543 if (qemu_chr_mem_osize(hmp.chr) > 0) {
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200544 QString *str = qemu_chr_mem_to_qs(hmp.chr);
545 output = g_strdup(qstring_get_str(str));
546 QDECREF(str);
547 } else {
548 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200549 }
550
551out:
552 qemu_chr_close_mem(hmp.chr);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200553 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200554}
555
bellard9dc39cb2004-03-14 21:38:27 +0000556static int compare_cmd(const char *name, const char *list)
557{
558 const char *p, *pstart;
559 int len;
560 len = strlen(name);
561 p = list;
562 for(;;) {
563 pstart = p;
564 p = strchr(p, '|');
565 if (!p)
566 p = pstart + strlen(pstart);
567 if ((p - pstart) == len && !memcmp(pstart, name, len))
568 return 1;
569 if (*p == '\0')
570 break;
571 p++;
572 }
573 return 0;
574}
575
Anthony Liguoric227f092009-10-01 16:12:16 -0500576static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000577 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000578{
Anthony Liguoric227f092009-10-01 16:12:16 -0500579 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000580
581 for(cmd = cmds; cmd->name != NULL; cmd++) {
582 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000583 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
584 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000585 }
586}
587
aliguori376253e2009-03-05 23:01:23 +0000588static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000589{
590 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000591 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000592 } else {
aliguori376253e2009-03-05 23:01:23 +0000593 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000594 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000595 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000596 monitor_printf(mon, "Log items (comma separated):\n");
597 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000598 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000599 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000600 }
601 }
bellard9dc39cb2004-03-14 21:38:27 +0000602 }
603}
604
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300605static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300606{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300607 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300608}
609
Lluísfc764102011-08-31 20:31:18 +0200610static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530611{
612 const char *tp_name = qdict_get_str(qdict, "name");
613 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200614 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000615
616 if (!ret) {
617 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
618 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530619}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100620
Michael Rothc45a8162011-10-02 08:44:37 -0500621#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100622static void do_trace_file(Monitor *mon, const QDict *qdict)
623{
624 const char *op = qdict_get_try_str(qdict, "op");
625 const char *arg = qdict_get_try_str(qdict, "arg");
626
627 if (!op) {
628 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
629 } else if (!strcmp(op, "on")) {
630 st_set_trace_file_enabled(true);
631 } else if (!strcmp(op, "off")) {
632 st_set_trace_file_enabled(false);
633 } else if (!strcmp(op, "flush")) {
634 st_flush_trace_buffer();
635 } else if (!strcmp(op, "set")) {
636 if (arg) {
637 st_set_trace_file(arg);
638 }
639 } else {
640 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
641 help_cmd(mon, "trace-file");
642 }
643}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530644#endif
645
Adam Litke940cc302010-01-25 12:18:44 -0600646static void user_monitor_complete(void *opaque, QObject *ret_data)
647{
648 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
649
650 if (ret_data) {
651 data->user_print(data->mon, ret_data);
652 }
653 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500654 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600655}
656
657static void qmp_monitor_complete(void *opaque, QObject *ret_data)
658{
659 monitor_protocol_emitter(opaque, ret_data);
660}
661
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300662static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
663 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600664{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300665 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600666}
667
Adam Litke940cc302010-01-25 12:18:44 -0600668static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
669 const QDict *params)
670{
671 int ret;
672
Anthony Liguori7267c092011-08-20 22:09:37 -0500673 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600674 cb_data->mon = mon;
675 cb_data->user_print = cmd->user_print;
676 monitor_suspend(mon);
677 ret = cmd->mhandler.cmd_async(mon, params,
678 user_monitor_complete, cb_data);
679 if (ret < 0) {
680 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500681 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600682 }
683}
684
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300685static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000686{
Anthony Liguoric227f092009-10-01 16:12:16 -0500687 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300688 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000689
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200690 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000691 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200692 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300693
694 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000695 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300696 break;
bellard9dc39cb2004-03-14 21:38:27 +0000697 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300698
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200699 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300700 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200701 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300702
Luiz Capitulinob5c30582011-10-21 16:24:28 -0200703 cmd->mhandler.info(mon);
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300704 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300705
706help:
707 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000708}
709
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300710CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000711{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300712 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200713 const mon_cmd_t *cmd;
714
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300715 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200716 info = g_malloc0(sizeof(*info));
717 info->value = g_malloc0(sizeof(*info->value));
718 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200719
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300720 info->next = cmd_list;
721 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200722 }
723
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300724 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000725}
726
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300727/* set the current CPU defined by the user */
728int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000729{
730 CPUState *env;
731
732 for(env = first_cpu; env != NULL; env = env->next_cpu) {
733 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000734 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000735 return 0;
736 }
737 }
738 return -1;
739}
740
pbrook9596ebb2007-11-18 01:44:38 +0000741static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000742{
aliguori731b0362009-03-05 23:01:42 +0000743 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300744 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000745 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300746 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000747 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000748}
749
Luiz Capitulino99b77962011-10-24 10:53:44 -0200750int monitor_get_cpu_index(void)
751{
752 return mon_get_cpu()->cpu_index;
753}
754
aliguori376253e2009-03-05 23:01:23 +0000755static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000756{
bellard6a00d602005-11-21 23:25:50 +0000757 CPUState *env;
758 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000759#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000760 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000761 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000762#else
aliguori376253e2009-03-05 23:01:23 +0000763 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000764 0);
bellard9307c4c2004-04-04 12:57:25 +0000765#endif
766}
767
aliguori376253e2009-03-05 23:01:23 +0000768static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000769{
aliguori376253e2009-03-05 23:01:23 +0000770 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000771}
772
aliguori376253e2009-03-05 23:01:23 +0000773static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000774{
775 int i;
bellard7e2515e2004-08-01 21:52:19 +0000776 const char *str;
ths3b46e622007-09-17 08:09:54 +0000777
aliguoricde76ee2009-03-05 23:01:51 +0000778 if (!mon->rs)
779 return;
bellard7e2515e2004-08-01 21:52:19 +0000780 i = 0;
781 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000782 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000783 if (!str)
784 break;
aliguori376253e2009-03-05 23:01:23 +0000785 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000786 i++;
bellardaa455482004-04-04 13:07:25 +0000787 }
788}
789
j_mayer76a66252007-03-07 08:32:30 +0000790#if defined(TARGET_PPC)
791/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000792static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000793{
794 CPUState *env;
795
796 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000797 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000798}
799#endif
800
Lluís6d8a7642011-08-31 20:30:43 +0200801#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530802static void do_info_trace(Monitor *mon)
803{
804 st_print_trace((FILE *)mon, &monitor_fprintf);
805}
Lluís31965ae2011-08-31 20:31:24 +0200806#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530807
Lluísfc764102011-08-31 20:31:18 +0200808static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530809{
Lluísfc764102011-08-31 20:31:18 +0200810 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530811}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530812
Jes Sorensen821601e2011-03-16 13:33:36 +0100813#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200814static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000815{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600816 if (!password || !password[0]) {
817 if (vnc_display_disable_login(NULL)) {
818 qerror_report(QERR_SET_PASSWD_FAILED);
819 return -1;
820 }
821 return 0;
822 }
823
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200824 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100825 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200826 return -1;
827 }
aliguoribb5fc202009-03-05 23:01:15 +0000828
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200829 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100830}
831
832static void change_vnc_password_cb(Monitor *mon, const char *password,
833 void *opaque)
834{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100835 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000836 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000837}
838
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200839static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000840{
ths70848512007-08-25 01:37:05 +0000841 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000842 strcmp(target, "password") == 0) {
843 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000844 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000845 strncpy(password, arg, sizeof(password));
846 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200847 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000848 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200849 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000850 }
ths70848512007-08-25 01:37:05 +0000851 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200852 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100853 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200854 return -1;
855 }
ths70848512007-08-25 01:37:05 +0000856 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200857
858 return 0;
thse25a5822007-08-25 01:36:20 +0000859}
Jes Sorensen821601e2011-03-16 13:33:36 +0100860#else
861static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
862{
863 qerror_report(QERR_FEATURE_DISABLED, "vnc");
864 return -ENODEV;
865}
866#endif
thse25a5822007-08-25 01:36:20 +0000867
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100868/**
869 * do_change(): Change a removable medium, or VNC configuration
870 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200871static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000872{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300873 const char *device = qdict_get_str(qdict, "device");
874 const char *target = qdict_get_str(qdict, "target");
875 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200876 int ret;
877
thse25a5822007-08-25 01:36:20 +0000878 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200879 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000880 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200881 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000882 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200883
884 return ret;
thse25a5822007-08-25 01:36:20 +0000885}
886
Daniel P. Berrange13661082011-06-23 13:31:42 +0100887static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
888{
889 const char *protocol = qdict_get_str(qdict, "protocol");
890 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +0100891 CharDriverState *s;
892
893 if (strcmp(protocol, "spice") == 0) {
894 if (!using_spice) {
895 /* correct one? spice isn't a device ,,, */
896 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
897 return -1;
898 }
899 qerror_report(QERR_ADD_CLIENT_FAILED);
900 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +0800901#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +0100902 } else if (strcmp(protocol, "vnc") == 0) {
903 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +0100904 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100905 vnc_display_add_client(NULL, fd, skipauth);
906 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800907#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +0100908 } else if ((s = qemu_chr_find(protocol)) != NULL) {
909 int fd = monitor_get_fd(mon, fdname);
910 if (qemu_chr_add_client(s, fd) < 0) {
911 qerror_report(QERR_ADD_CLIENT_FAILED);
912 return -1;
913 }
914 return 0;
915 }
916
917 qerror_report(QERR_INVALID_PARAMETER, "protocol");
918 return -1;
919}
920
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200921static int client_migrate_info(Monitor *mon, const QDict *qdict,
922 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200923{
924 const char *protocol = qdict_get_str(qdict, "protocol");
925 const char *hostname = qdict_get_str(qdict, "hostname");
926 const char *subject = qdict_get_try_str(qdict, "cert-subject");
927 int port = qdict_get_try_int(qdict, "port", -1);
928 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
929 int ret;
930
931 if (strcmp(protocol, "spice") == 0) {
932 if (!using_spice) {
933 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
934 return -1;
935 }
936
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200937 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
938 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200939 if (ret != 0) {
940 qerror_report(QERR_UNDEFINED_ERROR);
941 return -1;
942 }
943 return 0;
944 }
945
946 qerror_report(QERR_INVALID_PARAMETER, "protocol");
947 return -1;
948}
949
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300950static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +0000951{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300952 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300953 return 0;
bellard59a983b2004-03-17 23:17:16 +0000954}
955
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300956static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000957{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300958 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000959}
960
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300961static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000962{
963 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300964 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000965
bellard9307c4c2004-04-04 12:57:25 +0000966 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000967 mask = 0;
968 } else {
bellard9307c4c2004-04-04 12:57:25 +0000969 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000970 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000971 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000972 return;
973 }
974 }
975 cpu_set_log(mask);
976}
977
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300978static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000979{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300980 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000981 if (!option || !strcmp(option, "on")) {
982 singlestep = 1;
983 } else if (!strcmp(option, "off")) {
984 singlestep = 0;
985 } else {
986 monitor_printf(mon, "unexpected option %s\n", option);
987 }
988}
989
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300990static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000991{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300992 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000993 if (!device)
994 device = "tcp::" DEFAULT_GDBSTUB_PORT;
995 if (gdbserver_start(device) < 0) {
996 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
997 device);
998 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000999 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001000 } else {
aliguori59030a82009-04-05 18:43:41 +00001001 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1002 device);
bellard8a7ddc32004-03-31 19:00:16 +00001003 }
1004}
1005
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001006static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001007{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001008 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001009 if (select_watchdog_action(action) == -1) {
1010 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1011 }
1012}
1013
aliguori376253e2009-03-05 23:01:23 +00001014static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001015{
aliguori376253e2009-03-05 23:01:23 +00001016 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001017 switch(c) {
1018 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001019 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001020 break;
1021 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001022 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001023 break;
1024 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001025 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001026 break;
1027 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001028 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001029 break;
1030 default:
1031 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001032 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001033 } else {
aliguori376253e2009-03-05 23:01:23 +00001034 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001035 }
1036 break;
1037 }
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001039}
1040
aliguori376253e2009-03-05 23:01:23 +00001041static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001042 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001043{
bellard6a00d602005-11-21 23:25:50 +00001044 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001045 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001046 uint8_t buf[16];
1047 uint64_t v;
1048
1049 if (format == 'i') {
1050 int flags;
1051 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001052 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001053#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001054 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001055 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001056 } else if (wsize == 4) {
1057 flags = 0;
1058 } else {
bellard6a15fd12006-04-12 21:07:07 +00001059 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001060 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001061 if (env) {
1062#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001063 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001064 (env->segs[R_CS].flags & DESC_L_MASK))
1065 flags = 2;
1066 else
1067#endif
1068 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1069 flags = 1;
1070 }
bellard4c27ba22004-04-25 18:05:08 +00001071 }
1072#endif
aliguori376253e2009-03-05 23:01:23 +00001073 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001074 return;
1075 }
1076
1077 len = wsize * count;
1078 if (wsize == 1)
1079 line_size = 8;
1080 else
1081 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001082 max_digits = 0;
1083
1084 switch(format) {
1085 case 'o':
1086 max_digits = (wsize * 8 + 2) / 3;
1087 break;
1088 default:
1089 case 'x':
1090 max_digits = (wsize * 8) / 4;
1091 break;
1092 case 'u':
1093 case 'd':
1094 max_digits = (wsize * 8 * 10 + 32) / 33;
1095 break;
1096 case 'c':
1097 wsize = 1;
1098 break;
1099 }
1100
1101 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001102 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001103 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001104 else
aliguori376253e2009-03-05 23:01:23 +00001105 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001106 l = len;
1107 if (l > line_size)
1108 l = line_size;
1109 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001110 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001111 } else {
bellard6a00d602005-11-21 23:25:50 +00001112 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001113 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001115 break;
1116 }
bellard9307c4c2004-04-04 12:57:25 +00001117 }
ths5fafdf22007-09-16 21:08:06 +00001118 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001119 while (i < l) {
1120 switch(wsize) {
1121 default:
1122 case 1:
1123 v = ldub_raw(buf + i);
1124 break;
1125 case 2:
1126 v = lduw_raw(buf + i);
1127 break;
1128 case 4:
bellard92a31b12005-02-10 22:00:52 +00001129 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001130 break;
1131 case 8:
1132 v = ldq_raw(buf + i);
1133 break;
1134 }
aliguori376253e2009-03-05 23:01:23 +00001135 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001136 switch(format) {
1137 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001138 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001139 break;
1140 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001142 break;
1143 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001144 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001145 break;
1146 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001147 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001148 break;
1149 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001150 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001151 break;
1152 }
1153 i += wsize;
1154 }
aliguori376253e2009-03-05 23:01:23 +00001155 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001156 addr += l;
1157 len -= l;
1158 }
1159}
1160
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001161static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001162{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001163 int count = qdict_get_int(qdict, "count");
1164 int format = qdict_get_int(qdict, "format");
1165 int size = qdict_get_int(qdict, "size");
1166 target_long addr = qdict_get_int(qdict, "addr");
1167
aliguori376253e2009-03-05 23:01:23 +00001168 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001169}
1170
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001171static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001172{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001173 int count = qdict_get_int(qdict, "count");
1174 int format = qdict_get_int(qdict, "format");
1175 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001176 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001177
aliguori376253e2009-03-05 23:01:23 +00001178 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001179}
1180
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001181static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001182{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001183 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001184 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001185
blueswir17743e582007-09-24 18:39:04 +00001186#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001187 switch(format) {
1188 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001190 break;
1191 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001192 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001193 break;
1194 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001196 break;
1197 default:
1198 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001200 break;
1201 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001203 break;
1204 }
bellard92a31b12005-02-10 22:00:52 +00001205#else
1206 switch(format) {
1207 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001209 break;
1210 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001212 break;
1213 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001214 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001215 break;
1216 default:
1217 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001219 break;
1220 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001222 break;
1223 }
1224#endif
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001226}
1227
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001228static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001229{
1230 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001231 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001232 uint32_t start = qdict_get_int(qdict, "start");
1233 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001234
1235 sum = 0;
1236 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001237 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001238 /* BSD sum algorithm ('sum' Unix command) */
1239 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001240 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001241 }
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001243}
1244
bellarda3a91a32004-06-04 11:06:21 +00001245typedef struct {
1246 int keycode;
1247 const char *name;
1248} KeyDef;
1249
1250static const KeyDef key_defs[] = {
1251 { 0x2a, "shift" },
1252 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001253
bellarda3a91a32004-06-04 11:06:21 +00001254 { 0x38, "alt" },
1255 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001256 { 0x64, "altgr" },
1257 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001258 { 0x1d, "ctrl" },
1259 { 0x9d, "ctrl_r" },
1260
1261 { 0xdd, "menu" },
1262
1263 { 0x01, "esc" },
1264
1265 { 0x02, "1" },
1266 { 0x03, "2" },
1267 { 0x04, "3" },
1268 { 0x05, "4" },
1269 { 0x06, "5" },
1270 { 0x07, "6" },
1271 { 0x08, "7" },
1272 { 0x09, "8" },
1273 { 0x0a, "9" },
1274 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001275 { 0x0c, "minus" },
1276 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001277 { 0x0e, "backspace" },
1278
1279 { 0x0f, "tab" },
1280 { 0x10, "q" },
1281 { 0x11, "w" },
1282 { 0x12, "e" },
1283 { 0x13, "r" },
1284 { 0x14, "t" },
1285 { 0x15, "y" },
1286 { 0x16, "u" },
1287 { 0x17, "i" },
1288 { 0x18, "o" },
1289 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001290 { 0x1a, "bracket_left" },
1291 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001292 { 0x1c, "ret" },
1293
1294 { 0x1e, "a" },
1295 { 0x1f, "s" },
1296 { 0x20, "d" },
1297 { 0x21, "f" },
1298 { 0x22, "g" },
1299 { 0x23, "h" },
1300 { 0x24, "j" },
1301 { 0x25, "k" },
1302 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001303 { 0x27, "semicolon" },
1304 { 0x28, "apostrophe" },
1305 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001306
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001307 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001308 { 0x2c, "z" },
1309 { 0x2d, "x" },
1310 { 0x2e, "c" },
1311 { 0x2f, "v" },
1312 { 0x30, "b" },
1313 { 0x31, "n" },
1314 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001315 { 0x33, "comma" },
1316 { 0x34, "dot" },
1317 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001318
balrog4d3b6f62008-02-10 16:33:14 +00001319 { 0x37, "asterisk" },
1320
bellarda3a91a32004-06-04 11:06:21 +00001321 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001322 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001323 { 0x3b, "f1" },
1324 { 0x3c, "f2" },
1325 { 0x3d, "f3" },
1326 { 0x3e, "f4" },
1327 { 0x3f, "f5" },
1328 { 0x40, "f6" },
1329 { 0x41, "f7" },
1330 { 0x42, "f8" },
1331 { 0x43, "f9" },
1332 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001333 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001334 { 0x46, "scroll_lock" },
1335
bellard64866c32006-05-07 18:03:31 +00001336 { 0xb5, "kp_divide" },
1337 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001338 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001339 { 0x4e, "kp_add" },
1340 { 0x9c, "kp_enter" },
1341 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001342 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001343
1344 { 0x52, "kp_0" },
1345 { 0x4f, "kp_1" },
1346 { 0x50, "kp_2" },
1347 { 0x51, "kp_3" },
1348 { 0x4b, "kp_4" },
1349 { 0x4c, "kp_5" },
1350 { 0x4d, "kp_6" },
1351 { 0x47, "kp_7" },
1352 { 0x48, "kp_8" },
1353 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001354
bellarda3a91a32004-06-04 11:06:21 +00001355 { 0x56, "<" },
1356
1357 { 0x57, "f11" },
1358 { 0x58, "f12" },
1359
1360 { 0xb7, "print" },
1361
1362 { 0xc7, "home" },
1363 { 0xc9, "pgup" },
1364 { 0xd1, "pgdn" },
1365 { 0xcf, "end" },
1366
1367 { 0xcb, "left" },
1368 { 0xc8, "up" },
1369 { 0xd0, "down" },
1370 { 0xcd, "right" },
1371
1372 { 0xd2, "insert" },
1373 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001374#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1375 { 0xf0, "stop" },
1376 { 0xf1, "again" },
1377 { 0xf2, "props" },
1378 { 0xf3, "undo" },
1379 { 0xf4, "front" },
1380 { 0xf5, "copy" },
1381 { 0xf6, "open" },
1382 { 0xf7, "paste" },
1383 { 0xf8, "find" },
1384 { 0xf9, "cut" },
1385 { 0xfa, "lf" },
1386 { 0xfb, "help" },
1387 { 0xfc, "meta_l" },
1388 { 0xfd, "meta_r" },
1389 { 0xfe, "compose" },
1390#endif
bellarda3a91a32004-06-04 11:06:21 +00001391 { 0, NULL },
1392};
1393
1394static int get_keycode(const char *key)
1395{
1396 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001397 char *endp;
1398 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001399
1400 for(p = key_defs; p->name != NULL; p++) {
1401 if (!strcmp(key, p->name))
1402 return p->keycode;
1403 }
bellard64866c32006-05-07 18:03:31 +00001404 if (strstart(key, "0x", NULL)) {
1405 ret = strtoul(key, &endp, 0);
1406 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1407 return ret;
1408 }
bellarda3a91a32004-06-04 11:06:21 +00001409 return -1;
1410}
1411
balrogc8256f92008-06-08 22:45:01 +00001412#define MAX_KEYCODES 16
1413static uint8_t keycodes[MAX_KEYCODES];
1414static int nb_pending_keycodes;
1415static QEMUTimer *key_timer;
1416
1417static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001418{
balrogc8256f92008-06-08 22:45:01 +00001419 int keycode;
1420
1421 while (nb_pending_keycodes > 0) {
1422 nb_pending_keycodes--;
1423 keycode = keycodes[nb_pending_keycodes];
1424 if (keycode & 0x80)
1425 kbd_put_keycode(0xe0);
1426 kbd_put_keycode(keycode | 0x80);
1427 }
1428}
1429
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001430static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001431{
balrog3401c0d2008-06-04 10:05:59 +00001432 char keyname_buf[16];
1433 char *separator;
1434 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001435 const char *string = qdict_get_str(qdict, "string");
1436 int has_hold_time = qdict_haskey(qdict, "hold_time");
1437 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001438
balrogc8256f92008-06-08 22:45:01 +00001439 if (nb_pending_keycodes > 0) {
1440 qemu_del_timer(key_timer);
1441 release_keys(NULL);
1442 }
1443 if (!has_hold_time)
1444 hold_time = 100;
1445 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001446 while (1) {
1447 separator = strchr(string, '-');
1448 keyname_len = separator ? separator - string : strlen(string);
1449 if (keyname_len > 0) {
1450 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1451 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001452 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001453 return;
bellarda3a91a32004-06-04 11:06:21 +00001454 }
balrogc8256f92008-06-08 22:45:01 +00001455 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001457 return;
1458 }
1459 keyname_buf[keyname_len] = 0;
1460 keycode = get_keycode(keyname_buf);
1461 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001462 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001463 return;
1464 }
balrogc8256f92008-06-08 22:45:01 +00001465 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001466 }
balrog3401c0d2008-06-04 10:05:59 +00001467 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001468 break;
balrog3401c0d2008-06-04 10:05:59 +00001469 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001470 }
balrogc8256f92008-06-08 22:45:01 +00001471 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001472 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001473 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001474 keycode = keycodes[i];
1475 if (keycode & 0x80)
1476 kbd_put_keycode(0xe0);
1477 kbd_put_keycode(keycode & 0x7f);
1478 }
balrogc8256f92008-06-08 22:45:01 +00001479 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001480 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001481 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001482}
1483
bellard13224a82006-07-14 22:03:35 +00001484static int mouse_button_state;
1485
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001486static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001487{
1488 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001489 const char *dx_str = qdict_get_str(qdict, "dx_str");
1490 const char *dy_str = qdict_get_str(qdict, "dy_str");
1491 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001492 dx = strtol(dx_str, NULL, 0);
1493 dy = strtol(dy_str, NULL, 0);
1494 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001495 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001496 dz = strtol(dz_str, NULL, 0);
1497 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1498}
1499
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001500static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001501{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001502 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001503 mouse_button_state = button_state;
1504 kbd_mouse_event(0, 0, 0, mouse_button_state);
1505}
1506
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001507static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001508{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001509 int size = qdict_get_int(qdict, "size");
1510 int addr = qdict_get_int(qdict, "addr");
1511 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001512 uint32_t val;
1513 int suffix;
1514
1515 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001516 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001517 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001518 addr++;
1519 }
1520 addr &= 0xffff;
1521
1522 switch(size) {
1523 default:
1524 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001525 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001526 suffix = 'b';
1527 break;
1528 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001529 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001530 suffix = 'w';
1531 break;
1532 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001533 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001534 suffix = 'l';
1535 break;
1536 }
aliguori376253e2009-03-05 23:01:23 +00001537 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1538 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001539}
bellarda3a91a32004-06-04 11:06:21 +00001540
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001541static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001542{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001543 int size = qdict_get_int(qdict, "size");
1544 int addr = qdict_get_int(qdict, "addr");
1545 int val = qdict_get_int(qdict, "val");
1546
Jan Kiszkaf1147842009-07-14 10:20:11 +02001547 addr &= IOPORTS_MASK;
1548
1549 switch (size) {
1550 default:
1551 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001552 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001553 break;
1554 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001555 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001556 break;
1557 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001558 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001559 break;
1560 }
1561}
1562
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001563static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001564{
1565 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001566 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001567
Jan Kiszka76e30d02009-07-02 00:19:02 +02001568 res = qemu_boot_set(bootdevice);
1569 if (res == 0) {
1570 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1571 } else if (res > 0) {
1572 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001573 } else {
aliguori376253e2009-03-05 23:01:23 +00001574 monitor_printf(mon, "no function defined to set boot device list for "
1575 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001576 }
1577}
1578
bellardb86bda52004-09-18 19:32:46 +00001579#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001580static void print_pte(Monitor *mon, target_phys_addr_t addr,
1581 target_phys_addr_t pte,
1582 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001583{
Blue Swirld65aaf32010-12-11 18:56:24 +00001584#ifdef TARGET_X86_64
1585 if (addr & (1ULL << 47)) {
1586 addr |= -1LL << 48;
1587 }
1588#endif
1589 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1590 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001591 addr,
1592 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001593 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001594 pte & PG_GLOBAL_MASK ? 'G' : '-',
1595 pte & PG_PSE_MASK ? 'P' : '-',
1596 pte & PG_DIRTY_MASK ? 'D' : '-',
1597 pte & PG_ACCESSED_MASK ? 'A' : '-',
1598 pte & PG_PCD_MASK ? 'C' : '-',
1599 pte & PG_PWT_MASK ? 'T' : '-',
1600 pte & PG_USER_MASK ? 'U' : '-',
1601 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001602}
1603
Blue Swirld65aaf32010-12-11 18:56:24 +00001604static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001605{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001606 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001607 uint32_t pgd, pde, pte;
1608
bellardb86bda52004-09-18 19:32:46 +00001609 pgd = env->cr[3] & ~0xfff;
1610 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001611 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001612 pde = le32_to_cpu(pde);
1613 if (pde & PG_PRESENT_MASK) {
1614 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001615 /* 4M pages */
1616 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001617 } else {
1618 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001619 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001620 pte = le32_to_cpu(pte);
1621 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001622 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001623 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001624 ~0xfff);
1625 }
1626 }
1627 }
1628 }
1629 }
1630}
1631
Blue Swirld65aaf32010-12-11 18:56:24 +00001632static void tlb_info_pae32(Monitor *mon, CPUState *env)
1633{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001634 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001635 uint64_t pdpe, pde, pte;
1636 uint64_t pdp_addr, pd_addr, pt_addr;
1637
1638 pdp_addr = env->cr[3] & ~0x1f;
1639 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001640 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001641 pdpe = le64_to_cpu(pdpe);
1642 if (pdpe & PG_PRESENT_MASK) {
1643 pd_addr = pdpe & 0x3fffffffff000ULL;
1644 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001645 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001646 pde = le64_to_cpu(pde);
1647 if (pde & PG_PRESENT_MASK) {
1648 if (pde & PG_PSE_MASK) {
1649 /* 2M pages with PAE, CR4.PSE is ignored */
1650 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1651 ~((target_phys_addr_t)(1 << 20) - 1));
1652 } else {
1653 pt_addr = pde & 0x3fffffffff000ULL;
1654 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001655 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001656 pte = le64_to_cpu(pte);
1657 if (pte & PG_PRESENT_MASK) {
1658 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1659 + (l3 << 12),
1660 pte & ~PG_PSE_MASK,
1661 ~(target_phys_addr_t)0xfff);
1662 }
1663 }
1664 }
1665 }
1666 }
1667 }
1668 }
1669}
1670
1671#ifdef TARGET_X86_64
1672static void tlb_info_64(Monitor *mon, CPUState *env)
1673{
1674 uint64_t l1, l2, l3, l4;
1675 uint64_t pml4e, pdpe, pde, pte;
1676 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1677
1678 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1679 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001680 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001681 pml4e = le64_to_cpu(pml4e);
1682 if (pml4e & PG_PRESENT_MASK) {
1683 pdp_addr = pml4e & 0x3fffffffff000ULL;
1684 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001685 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001686 pdpe = le64_to_cpu(pdpe);
1687 if (pdpe & PG_PRESENT_MASK) {
1688 if (pdpe & PG_PSE_MASK) {
1689 /* 1G pages, CR4.PSE is ignored */
1690 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1691 0x3ffffc0000000ULL);
1692 } else {
1693 pd_addr = pdpe & 0x3fffffffff000ULL;
1694 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001695 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001696 pde = le64_to_cpu(pde);
1697 if (pde & PG_PRESENT_MASK) {
1698 if (pde & PG_PSE_MASK) {
1699 /* 2M pages, CR4.PSE is ignored */
1700 print_pte(mon, (l1 << 39) + (l2 << 30) +
1701 (l3 << 21), pde,
1702 0x3ffffffe00000ULL);
1703 } else {
1704 pt_addr = pde & 0x3fffffffff000ULL;
1705 for (l4 = 0; l4 < 512; l4++) {
1706 cpu_physical_memory_read(pt_addr
1707 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001708 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001709 pte = le64_to_cpu(pte);
1710 if (pte & PG_PRESENT_MASK) {
1711 print_pte(mon, (l1 << 39) +
1712 (l2 << 30) +
1713 (l3 << 21) + (l4 << 12),
1714 pte & ~PG_PSE_MASK,
1715 0x3fffffffff000ULL);
1716 }
1717 }
1718 }
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726}
1727#endif
1728
1729static void tlb_info(Monitor *mon)
1730{
1731 CPUState *env;
1732
1733 env = mon_get_cpu();
1734
1735 if (!(env->cr[0] & CR0_PG_MASK)) {
1736 monitor_printf(mon, "PG disabled\n");
1737 return;
1738 }
1739 if (env->cr[4] & CR4_PAE_MASK) {
1740#ifdef TARGET_X86_64
1741 if (env->hflags & HF_LMA_MASK) {
1742 tlb_info_64(mon, env);
1743 } else
1744#endif
1745 {
1746 tlb_info_pae32(mon, env);
1747 }
1748 } else {
1749 tlb_info_32(mon, env);
1750 }
1751}
1752
Blue Swirl1b3cba62010-12-11 18:56:27 +00001753static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
1754 int *plast_prot,
1755 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001756{
bellard9746b152004-11-11 18:30:24 +00001757 int prot1;
1758 prot1 = *plast_prot;
1759 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001760 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001761 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1762 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001763 *pstart, end, end - *pstart,
1764 prot1 & PG_USER_MASK ? 'u' : '-',
1765 'r',
1766 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001767 }
1768 if (prot != 0)
1769 *pstart = end;
1770 else
1771 *pstart = -1;
1772 *plast_prot = prot;
1773 }
1774}
1775
Blue Swirl1b3cba62010-12-11 18:56:27 +00001776static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001777{
Austin Clementsb49ca722011-08-14 23:19:21 -04001778 unsigned int l1, l2;
1779 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001780 uint32_t pgd, pde, pte;
1781 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00001782
bellardb86bda52004-09-18 19:32:46 +00001783 pgd = env->cr[3] & ~0xfff;
1784 last_prot = 0;
1785 start = -1;
1786 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001787 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001788 pde = le32_to_cpu(pde);
1789 end = l1 << 22;
1790 if (pde & PG_PRESENT_MASK) {
1791 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1792 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001793 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001794 } else {
1795 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001796 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001797 pte = le32_to_cpu(pte);
1798 end = (l1 << 22) + (l2 << 12);
1799 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001800 prot = pte & pde &
1801 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001802 } else {
1803 prot = 0;
1804 }
aliguori376253e2009-03-05 23:01:23 +00001805 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001806 }
1807 }
1808 } else {
1809 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001810 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001811 }
1812 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001813 /* Flush last range */
1814 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001815}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001816
1817static void mem_info_pae32(Monitor *mon, CPUState *env)
1818{
Austin Clementsb49ca722011-08-14 23:19:21 -04001819 unsigned int l1, l2, l3;
1820 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001821 uint64_t pdpe, pde, pte;
1822 uint64_t pdp_addr, pd_addr, pt_addr;
1823 target_phys_addr_t start, end;
1824
1825 pdp_addr = env->cr[3] & ~0x1f;
1826 last_prot = 0;
1827 start = -1;
1828 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001829 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001830 pdpe = le64_to_cpu(pdpe);
1831 end = l1 << 30;
1832 if (pdpe & PG_PRESENT_MASK) {
1833 pd_addr = pdpe & 0x3fffffffff000ULL;
1834 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001835 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001836 pde = le64_to_cpu(pde);
1837 end = (l1 << 30) + (l2 << 21);
1838 if (pde & PG_PRESENT_MASK) {
1839 if (pde & PG_PSE_MASK) {
1840 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1841 PG_PRESENT_MASK);
1842 mem_print(mon, &start, &last_prot, end, prot);
1843 } else {
1844 pt_addr = pde & 0x3fffffffff000ULL;
1845 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001846 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001847 pte = le64_to_cpu(pte);
1848 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1849 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001850 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1851 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001852 } else {
1853 prot = 0;
1854 }
1855 mem_print(mon, &start, &last_prot, end, prot);
1856 }
1857 }
1858 } else {
1859 prot = 0;
1860 mem_print(mon, &start, &last_prot, end, prot);
1861 }
1862 }
1863 } else {
1864 prot = 0;
1865 mem_print(mon, &start, &last_prot, end, prot);
1866 }
1867 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001868 /* Flush last range */
1869 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001870}
1871
1872
1873#ifdef TARGET_X86_64
1874static void mem_info_64(Monitor *mon, CPUState *env)
1875{
1876 int prot, last_prot;
1877 uint64_t l1, l2, l3, l4;
1878 uint64_t pml4e, pdpe, pde, pte;
1879 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1880
1881 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1882 last_prot = 0;
1883 start = -1;
1884 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001885 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001886 pml4e = le64_to_cpu(pml4e);
1887 end = l1 << 39;
1888 if (pml4e & PG_PRESENT_MASK) {
1889 pdp_addr = pml4e & 0x3fffffffff000ULL;
1890 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001891 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001892 pdpe = le64_to_cpu(pdpe);
1893 end = (l1 << 39) + (l2 << 30);
1894 if (pdpe & PG_PRESENT_MASK) {
1895 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001896 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1897 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001898 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001899 mem_print(mon, &start, &last_prot, end, prot);
1900 } else {
1901 pd_addr = pdpe & 0x3fffffffff000ULL;
1902 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001903 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001904 pde = le64_to_cpu(pde);
1905 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1906 if (pde & PG_PRESENT_MASK) {
1907 if (pde & PG_PSE_MASK) {
1908 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1909 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001910 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001911 mem_print(mon, &start, &last_prot, end, prot);
1912 } else {
1913 pt_addr = pde & 0x3fffffffff000ULL;
1914 for (l4 = 0; l4 < 512; l4++) {
1915 cpu_physical_memory_read(pt_addr
1916 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001917 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001918 pte = le64_to_cpu(pte);
1919 end = (l1 << 39) + (l2 << 30) +
1920 (l3 << 21) + (l4 << 12);
1921 if (pte & PG_PRESENT_MASK) {
1922 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1923 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001924 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001925 } else {
1926 prot = 0;
1927 }
1928 mem_print(mon, &start, &last_prot, end, prot);
1929 }
1930 }
1931 } else {
1932 prot = 0;
1933 mem_print(mon, &start, &last_prot, end, prot);
1934 }
1935 }
1936 }
1937 } else {
1938 prot = 0;
1939 mem_print(mon, &start, &last_prot, end, prot);
1940 }
1941 }
1942 } else {
1943 prot = 0;
1944 mem_print(mon, &start, &last_prot, end, prot);
1945 }
1946 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001947 /* Flush last range */
1948 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001949}
1950#endif
1951
1952static void mem_info(Monitor *mon)
1953{
1954 CPUState *env;
1955
1956 env = mon_get_cpu();
1957
1958 if (!(env->cr[0] & CR0_PG_MASK)) {
1959 monitor_printf(mon, "PG disabled\n");
1960 return;
1961 }
1962 if (env->cr[4] & CR4_PAE_MASK) {
1963#ifdef TARGET_X86_64
1964 if (env->hflags & HF_LMA_MASK) {
1965 mem_info_64(mon, env);
1966 } else
1967#endif
1968 {
1969 mem_info_pae32(mon, env);
1970 }
1971 } else {
1972 mem_info_32(mon, env);
1973 }
1974}
bellardb86bda52004-09-18 19:32:46 +00001975#endif
1976
aurel327c664e22009-03-03 06:12:22 +00001977#if defined(TARGET_SH4)
1978
aliguori376253e2009-03-05 23:01:23 +00001979static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001980{
aliguori376253e2009-03-05 23:01:23 +00001981 monitor_printf(mon, " tlb%i:\t"
1982 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1983 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1984 "dirty=%hhu writethrough=%hhu\n",
1985 idx,
1986 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1987 tlb->v, tlb->sh, tlb->c, tlb->pr,
1988 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001989}
1990
aliguori376253e2009-03-05 23:01:23 +00001991static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001992{
1993 CPUState *env = mon_get_cpu();
1994 int i;
1995
aliguori376253e2009-03-05 23:01:23 +00001996 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001997 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001998 print_tlb (mon, i, &env->itlb[i]);
1999 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002000 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002001 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002002}
2003
2004#endif
2005
Scott Woodbebabbc2011-08-18 10:38:42 +00002006#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002007static void tlb_info(Monitor *mon)
2008{
2009 CPUState *env1 = mon_get_cpu();
2010
2011 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2012}
2013#endif
2014
Blue Swirl314e2982011-09-11 20:22:05 +00002015static void do_info_mtree(Monitor *mon)
2016{
2017 mtree_info((fprintf_function)monitor_printf, mon);
2018}
2019
aliguori030ea372009-04-21 22:30:47 +00002020static void do_info_numa(Monitor *mon)
2021{
aliguorib28b6232009-04-22 20:20:29 +00002022 int i;
aliguori030ea372009-04-21 22:30:47 +00002023 CPUState *env;
2024
2025 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2026 for (i = 0; i < nb_numa_nodes; i++) {
2027 monitor_printf(mon, "node %d cpus:", i);
2028 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2029 if (env->numa_node == i) {
2030 monitor_printf(mon, " %d", env->cpu_index);
2031 }
2032 }
2033 monitor_printf(mon, "\n");
2034 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2035 node_mem[i] >> 20);
2036 }
2037}
2038
bellard5f1ce942006-02-08 22:40:15 +00002039#ifdef CONFIG_PROFILER
2040
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002041int64_t qemu_time;
2042int64_t dev_time;
2043
aliguori376253e2009-03-05 23:01:23 +00002044static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002045{
2046 int64_t total;
2047 total = qemu_time;
2048 if (total == 0)
2049 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002050 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002051 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002052 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002053 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002054 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002055 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002056}
2057#else
aliguori376253e2009-03-05 23:01:23 +00002058static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002059{
aliguori376253e2009-03-05 23:01:23 +00002060 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002061}
2062#endif
2063
bellardec36b692006-07-16 18:57:03 +00002064/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002065static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002066
aliguori376253e2009-03-05 23:01:23 +00002067static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002068{
2069 int i;
2070 CaptureState *s;
2071
2072 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002073 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002074 s->ops.info (s->opaque);
2075 }
2076}
2077
Blue Swirl23130862009-06-06 08:22:04 +00002078#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002079static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002080{
2081 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002082 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002083 CaptureState *s;
2084
2085 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2086 if (i == n) {
2087 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002088 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002089 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002090 return;
2091 }
2092 }
2093}
2094
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002095static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002096{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002097 const char *path = qdict_get_str(qdict, "path");
2098 int has_freq = qdict_haskey(qdict, "freq");
2099 int freq = qdict_get_try_int(qdict, "freq", -1);
2100 int has_bits = qdict_haskey(qdict, "bits");
2101 int bits = qdict_get_try_int(qdict, "bits", -1);
2102 int has_channels = qdict_haskey(qdict, "nchannels");
2103 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002104 CaptureState *s;
2105
Anthony Liguori7267c092011-08-20 22:09:37 -05002106 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002107
2108 freq = has_freq ? freq : 44100;
2109 bits = has_bits ? bits : 16;
2110 nchannels = has_channels ? nchannels : 2;
2111
2112 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002113 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002114 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002115 return;
bellardec36b692006-07-16 18:57:03 +00002116 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002117 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002118}
2119#endif
2120
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002122{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002123 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002124
aliguori76655d62009-03-06 20:27:37 +00002125 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002127 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128 return acl;
2129}
aliguori76655d62009-03-06 20:27:37 +00002130
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002131static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002132{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002133 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002134 qemu_acl *acl = find_acl(mon, aclname);
2135 qemu_acl_entry *entry;
2136 int i = 0;
2137
2138 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002139 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002140 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002141 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002142 i++;
2143 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002144 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002145 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002146 }
2147}
2148
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002149static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002150{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002151 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 qemu_acl *acl = find_acl(mon, aclname);
2153
2154 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002155 qemu_acl_reset(acl);
2156 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157 }
2158}
aliguori76655d62009-03-06 20:27:37 +00002159
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002160static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002161{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002162 const char *aclname = qdict_get_str(qdict, "aclname");
2163 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002164 qemu_acl *acl = find_acl(mon, aclname);
2165
2166 if (acl) {
2167 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002168 acl->defaultDeny = 0;
2169 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002171 acl->defaultDeny = 1;
2172 monitor_printf(mon, "acl: policy set to 'deny'\n");
2173 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002174 monitor_printf(mon, "acl: unknown policy '%s', "
2175 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002176 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002177 }
2178}
aliguori76655d62009-03-06 20:27:37 +00002179
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002180static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002181{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002182 const char *aclname = qdict_get_str(qdict, "aclname");
2183 const char *match = qdict_get_str(qdict, "match");
2184 const char *policy = qdict_get_str(qdict, "policy");
2185 int has_index = qdict_haskey(qdict, "index");
2186 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002187 qemu_acl *acl = find_acl(mon, aclname);
2188 int deny, ret;
2189
2190 if (acl) {
2191 if (strcmp(policy, "allow") == 0) {
2192 deny = 0;
2193 } else if (strcmp(policy, "deny") == 0) {
2194 deny = 1;
2195 } else {
2196 monitor_printf(mon, "acl: unknown policy '%s', "
2197 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002198 return;
2199 }
aliguori28a76be2009-03-06 20:27:40 +00002200 if (has_index)
2201 ret = qemu_acl_insert(acl, deny, match, index);
2202 else
2203 ret = qemu_acl_append(acl, deny, match);
2204 if (ret < 0)
2205 monitor_printf(mon, "acl: unable to add acl entry\n");
2206 else
2207 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002208 }
2209}
aliguori76655d62009-03-06 20:27:37 +00002210
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002211static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002212{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002213 const char *aclname = qdict_get_str(qdict, "aclname");
2214 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002215 qemu_acl *acl = find_acl(mon, aclname);
2216 int ret;
aliguori76655d62009-03-06 20:27:37 +00002217
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002218 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002219 ret = qemu_acl_remove(acl, match);
2220 if (ret < 0)
2221 monitor_printf(mon, "acl: no matching acl entry\n");
2222 else
2223 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002224 }
2225}
2226
Huang Ying79c4f6b2009-06-23 10:05:14 +08002227#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002228static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002229{
2230 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002231 int cpu_index = qdict_get_int(qdict, "cpu_index");
2232 int bank = qdict_get_int(qdict, "bank");
2233 uint64_t status = qdict_get_int(qdict, "status");
2234 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2235 uint64_t addr = qdict_get_int(qdict, "addr");
2236 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002237 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002238
Jan Kiszka747461c2011-03-02 08:56:10 +01002239 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2240 flags |= MCE_INJECT_BROADCAST;
2241 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002242 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002243 if (cenv->cpu_index == cpu_index) {
2244 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002245 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002246 break;
2247 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002248 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002249}
2250#endif
2251
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002252static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002254 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002255 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002256 int fd;
2257
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002258 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002259 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002260 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002261 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002262 }
2263
2264 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002265 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2266 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002267 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002268 }
2269
Blue Swirl72cf2d42009-09-12 07:36:22 +00002270 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002271 if (strcmp(monfd->name, fdname) != 0) {
2272 continue;
2273 }
2274
2275 close(monfd->fd);
2276 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002277 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002278 }
2279
Anthony Liguori7267c092011-08-20 22:09:37 -05002280 monfd = g_malloc0(sizeof(mon_fd_t));
2281 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002282 monfd->fd = fd;
2283
Blue Swirl72cf2d42009-09-12 07:36:22 +00002284 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002285 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002286}
2287
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002288static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002289{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002290 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002291 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002292
Blue Swirl72cf2d42009-09-12 07:36:22 +00002293 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002294 if (strcmp(monfd->name, fdname) != 0) {
2295 continue;
2296 }
2297
Blue Swirl72cf2d42009-09-12 07:36:22 +00002298 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002299 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002300 g_free(monfd->name);
2301 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002302 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002303 }
2304
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002305 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002306 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002307}
2308
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002309static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002310{
Luiz Capitulino13548692011-07-29 15:36:43 -03002311 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002312 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002313
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002314 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002315
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002316 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002317 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002318 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002319}
2320
Mark McLoughlin7768e042009-07-22 09:11:41 +01002321int monitor_get_fd(Monitor *mon, const char *fdname)
2322{
Anthony Liguoric227f092009-10-01 16:12:16 -05002323 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002324
Blue Swirl72cf2d42009-09-12 07:36:22 +00002325 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002326 int fd;
2327
2328 if (strcmp(monfd->name, fdname) != 0) {
2329 continue;
2330 }
2331
2332 fd = monfd->fd;
2333
2334 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002335 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002336 g_free(monfd->name);
2337 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002338
2339 return fd;
2340 }
2341
2342 return -1;
2343}
2344
Wayne Xia816f8922011-10-12 11:32:41 +08002345/* mon_cmds and info_cmds would be sorted at runtime */
2346static mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002347#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002348 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002349};
2350
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002351/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002352static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002353 {
2354 .name = "version",
2355 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002356 .params = "",
2357 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002358 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002359 },
2360 {
2361 .name = "network",
2362 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002363 .params = "",
2364 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002365 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002366 },
2367 {
2368 .name = "chardev",
2369 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002370 .params = "",
2371 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002372 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002373 },
2374 {
2375 .name = "block",
2376 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002377 .params = "",
2378 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002379 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002380 },
2381 {
2382 .name = "blockstats",
2383 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002384 .params = "",
2385 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002386 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002387 },
2388 {
2389 .name = "registers",
2390 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002391 .params = "",
2392 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002393 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002394 },
2395 {
2396 .name = "cpus",
2397 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 .params = "",
2399 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002400 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002401 },
2402 {
2403 .name = "history",
2404 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 .params = "",
2406 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002407 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002408 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002409#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2410 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002411 {
2412 .name = "irq",
2413 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002414 .params = "",
2415 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002416#ifdef TARGET_SPARC
2417 .mhandler.info = sun4m_irq_info,
2418#elif defined(TARGET_LM32)
2419 .mhandler.info = lm32_irq_info,
2420#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002421 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002422#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002423 },
2424 {
2425 .name = "pic",
2426 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002427 .params = "",
2428 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002429#ifdef TARGET_SPARC
2430 .mhandler.info = sun4m_pic_info,
2431#elif defined(TARGET_LM32)
2432 .mhandler.info = lm32_do_pic_info,
2433#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002434 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002435#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002436 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002437#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002438 {
2439 .name = "pci",
2440 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002441 .params = "",
2442 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002443 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002444 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002445#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2446 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002447 {
2448 .name = "tlb",
2449 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002450 .params = "",
2451 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002452 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002453 },
aurel327c664e22009-03-03 06:12:22 +00002454#endif
2455#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002456 {
2457 .name = "mem",
2458 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002459 .params = "",
2460 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002461 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002462 },
bellardb86bda52004-09-18 19:32:46 +00002463#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002464 {
Blue Swirl314e2982011-09-11 20:22:05 +00002465 .name = "mtree",
2466 .args_type = "",
2467 .params = "",
2468 .help = "show memory tree",
2469 .mhandler.info = do_info_mtree,
2470 },
2471 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002472 .name = "jit",
2473 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 .params = "",
2475 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002476 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002477 },
2478 {
2479 .name = "kvm",
2480 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002481 .params = "",
2482 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002483 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 },
2485 {
2486 .name = "numa",
2487 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002488 .params = "",
2489 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002490 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002491 },
2492 {
2493 .name = "usb",
2494 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002495 .params = "",
2496 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002497 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002498 },
2499 {
2500 .name = "usbhost",
2501 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002502 .params = "",
2503 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002504 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002505 },
2506 {
2507 .name = "profile",
2508 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 .params = "",
2510 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002511 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 },
2513 {
2514 .name = "capture",
2515 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 .params = "",
2517 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002518 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002519 },
2520 {
2521 .name = "snapshots",
2522 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 .params = "",
2524 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002525 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 },
2527 {
2528 .name = "status",
2529 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 .params = "",
2531 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002532 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 },
2534 {
2535 .name = "pcmcia",
2536 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 .params = "",
2538 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002539 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "mice",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002546 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
2549 .name = "vnc",
2550 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 .params = "",
2552 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002553 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002555#if defined(CONFIG_SPICE)
2556 {
2557 .name = "spice",
2558 .args_type = "",
2559 .params = "",
2560 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002561 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002562 },
2563#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 {
2565 .name = "name",
2566 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .params = "",
2568 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002569 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 },
2571 {
2572 .name = "uuid",
2573 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 .params = "",
2575 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002576 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 },
j_mayer76a66252007-03-07 08:32:30 +00002578#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 {
2580 .name = "cpustats",
2581 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 .params = "",
2583 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002584 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 },
j_mayer76a66252007-03-07 08:32:30 +00002586#endif
blueswir131a60e22007-10-26 18:42:59 +00002587#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 {
2589 .name = "usernet",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002593 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
blueswir131a60e22007-10-26 18:42:59 +00002595#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 {
2597 .name = "migrate",
2598 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 .params = "",
2600 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002601 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
2603 {
2604 .name = "balloon",
2605 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 .params = "",
2607 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002608 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 },
2610 {
2611 .name = "qtree",
2612 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 .params = "",
2614 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002615 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 },
2617 {
2618 .name = "qdm",
2619 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 .params = "",
2621 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002622 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 },
2624 {
2625 .name = "roms",
2626 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 .params = "",
2628 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002629 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 },
Lluís6d8a7642011-08-31 20:30:43 +02002631#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05302632 {
2633 .name = "trace",
2634 .args_type = "",
2635 .params = "",
2636 .help = "show current contents of trace buffer",
2637 .mhandler.info = do_info_trace,
2638 },
Lluís31965ae2011-08-31 20:31:24 +02002639#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05302640 {
2641 .name = "trace-events",
2642 .args_type = "",
2643 .params = "",
2644 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002645 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302646 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 {
2648 .name = NULL,
2649 },
bellard9dc39cb2004-03-14 21:38:27 +00002650};
2651
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002652static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002653#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002654 { /* NULL */ },
2655};
2656
bellard9307c4c2004-04-04 12:57:25 +00002657/*******************************************************************/
2658
2659static const char *pch;
2660static jmp_buf expr_env;
2661
bellard92a31b12005-02-10 22:00:52 +00002662#define MD_TLONG 0
2663#define MD_I32 1
2664
bellard9307c4c2004-04-04 12:57:25 +00002665typedef struct MonitorDef {
2666 const char *name;
2667 int offset;
blueswir18662d652008-10-02 18:32:44 +00002668 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002669 int type;
bellard9307c4c2004-04-04 12:57:25 +00002670} MonitorDef;
2671
bellard57206fd2004-04-25 18:54:52 +00002672#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002673static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002674{
bellard6a00d602005-11-21 23:25:50 +00002675 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002676 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002677}
2678#endif
2679
bellarda541f292004-04-12 20:39:29 +00002680#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002681static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002682{
bellard6a00d602005-11-21 23:25:50 +00002683 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002684 unsigned int u;
2685 int i;
2686
2687 u = 0;
2688 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002689 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002690
2691 return u;
2692}
2693
blueswir18662d652008-10-02 18:32:44 +00002694static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002695{
bellard6a00d602005-11-21 23:25:50 +00002696 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002697 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002698}
2699
blueswir18662d652008-10-02 18:32:44 +00002700static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002701{
bellard6a00d602005-11-21 23:25:50 +00002702 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002703 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002704}
bellard9fddaa02004-05-21 12:59:32 +00002705
blueswir18662d652008-10-02 18:32:44 +00002706static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002707{
bellard6a00d602005-11-21 23:25:50 +00002708 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002709 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002710}
2711
blueswir18662d652008-10-02 18:32:44 +00002712static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002713{
bellard6a00d602005-11-21 23:25:50 +00002714 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002715 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002716}
2717
blueswir18662d652008-10-02 18:32:44 +00002718static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002719{
bellard6a00d602005-11-21 23:25:50 +00002720 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002721 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002722}
bellarda541f292004-04-12 20:39:29 +00002723#endif
2724
bellarde95c8d52004-09-30 22:22:08 +00002725#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002726#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002727static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002728{
bellard6a00d602005-11-21 23:25:50 +00002729 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002730
2731 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002732}
bellard7b936c02005-10-30 17:05:13 +00002733#endif
bellarde95c8d52004-09-30 22:22:08 +00002734
blueswir18662d652008-10-02 18:32:44 +00002735static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002736{
bellard6a00d602005-11-21 23:25:50 +00002737 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002738 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002739}
2740#endif
2741
blueswir18662d652008-10-02 18:32:44 +00002742static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002743#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002744
2745#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002746 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002747 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002748 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002749
bellard9307c4c2004-04-04 12:57:25 +00002750 { "eax", offsetof(CPUState, regs[0]) },
2751 { "ecx", offsetof(CPUState, regs[1]) },
2752 { "edx", offsetof(CPUState, regs[2]) },
2753 { "ebx", offsetof(CPUState, regs[3]) },
2754 { "esp|sp", offsetof(CPUState, regs[4]) },
2755 { "ebp|fp", offsetof(CPUState, regs[5]) },
2756 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002757 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002758#ifdef TARGET_X86_64
2759 { "r8", offsetof(CPUState, regs[8]) },
2760 { "r9", offsetof(CPUState, regs[9]) },
2761 { "r10", offsetof(CPUState, regs[10]) },
2762 { "r11", offsetof(CPUState, regs[11]) },
2763 { "r12", offsetof(CPUState, regs[12]) },
2764 { "r13", offsetof(CPUState, regs[13]) },
2765 { "r14", offsetof(CPUState, regs[14]) },
2766 { "r15", offsetof(CPUState, regs[15]) },
2767#endif
bellard9307c4c2004-04-04 12:57:25 +00002768 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002769 { "eip", offsetof(CPUState, eip) },
2770 SEG("cs", R_CS)
2771 SEG("ds", R_DS)
2772 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002773 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002774 SEG("fs", R_FS)
2775 SEG("gs", R_GS)
2776 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002777#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002778 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002779 { "r0", offsetof(CPUState, gpr[0]) },
2780 { "r1", offsetof(CPUState, gpr[1]) },
2781 { "r2", offsetof(CPUState, gpr[2]) },
2782 { "r3", offsetof(CPUState, gpr[3]) },
2783 { "r4", offsetof(CPUState, gpr[4]) },
2784 { "r5", offsetof(CPUState, gpr[5]) },
2785 { "r6", offsetof(CPUState, gpr[6]) },
2786 { "r7", offsetof(CPUState, gpr[7]) },
2787 { "r8", offsetof(CPUState, gpr[8]) },
2788 { "r9", offsetof(CPUState, gpr[9]) },
2789 { "r10", offsetof(CPUState, gpr[10]) },
2790 { "r11", offsetof(CPUState, gpr[11]) },
2791 { "r12", offsetof(CPUState, gpr[12]) },
2792 { "r13", offsetof(CPUState, gpr[13]) },
2793 { "r14", offsetof(CPUState, gpr[14]) },
2794 { "r15", offsetof(CPUState, gpr[15]) },
2795 { "r16", offsetof(CPUState, gpr[16]) },
2796 { "r17", offsetof(CPUState, gpr[17]) },
2797 { "r18", offsetof(CPUState, gpr[18]) },
2798 { "r19", offsetof(CPUState, gpr[19]) },
2799 { "r20", offsetof(CPUState, gpr[20]) },
2800 { "r21", offsetof(CPUState, gpr[21]) },
2801 { "r22", offsetof(CPUState, gpr[22]) },
2802 { "r23", offsetof(CPUState, gpr[23]) },
2803 { "r24", offsetof(CPUState, gpr[24]) },
2804 { "r25", offsetof(CPUState, gpr[25]) },
2805 { "r26", offsetof(CPUState, gpr[26]) },
2806 { "r27", offsetof(CPUState, gpr[27]) },
2807 { "r28", offsetof(CPUState, gpr[28]) },
2808 { "r29", offsetof(CPUState, gpr[29]) },
2809 { "r30", offsetof(CPUState, gpr[30]) },
2810 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002811 /* Floating point registers */
2812 { "f0", offsetof(CPUState, fpr[0]) },
2813 { "f1", offsetof(CPUState, fpr[1]) },
2814 { "f2", offsetof(CPUState, fpr[2]) },
2815 { "f3", offsetof(CPUState, fpr[3]) },
2816 { "f4", offsetof(CPUState, fpr[4]) },
2817 { "f5", offsetof(CPUState, fpr[5]) },
2818 { "f6", offsetof(CPUState, fpr[6]) },
2819 { "f7", offsetof(CPUState, fpr[7]) },
2820 { "f8", offsetof(CPUState, fpr[8]) },
2821 { "f9", offsetof(CPUState, fpr[9]) },
2822 { "f10", offsetof(CPUState, fpr[10]) },
2823 { "f11", offsetof(CPUState, fpr[11]) },
2824 { "f12", offsetof(CPUState, fpr[12]) },
2825 { "f13", offsetof(CPUState, fpr[13]) },
2826 { "f14", offsetof(CPUState, fpr[14]) },
2827 { "f15", offsetof(CPUState, fpr[15]) },
2828 { "f16", offsetof(CPUState, fpr[16]) },
2829 { "f17", offsetof(CPUState, fpr[17]) },
2830 { "f18", offsetof(CPUState, fpr[18]) },
2831 { "f19", offsetof(CPUState, fpr[19]) },
2832 { "f20", offsetof(CPUState, fpr[20]) },
2833 { "f21", offsetof(CPUState, fpr[21]) },
2834 { "f22", offsetof(CPUState, fpr[22]) },
2835 { "f23", offsetof(CPUState, fpr[23]) },
2836 { "f24", offsetof(CPUState, fpr[24]) },
2837 { "f25", offsetof(CPUState, fpr[25]) },
2838 { "f26", offsetof(CPUState, fpr[26]) },
2839 { "f27", offsetof(CPUState, fpr[27]) },
2840 { "f28", offsetof(CPUState, fpr[28]) },
2841 { "f29", offsetof(CPUState, fpr[29]) },
2842 { "f30", offsetof(CPUState, fpr[30]) },
2843 { "f31", offsetof(CPUState, fpr[31]) },
2844 { "fpscr", offsetof(CPUState, fpscr) },
2845 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002846 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002847 { "lr", offsetof(CPUState, lr) },
2848 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002849 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002850 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002851 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002852 { "msr", 0, &monitor_get_msr, },
2853 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002854 { "tbu", 0, &monitor_get_tbu, },
2855 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002856#if defined(TARGET_PPC64)
2857 /* Address space register */
2858 { "asr", offsetof(CPUState, asr) },
2859#endif
2860 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11002861 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00002862 { "sr0", offsetof(CPUState, sr[0]) },
2863 { "sr1", offsetof(CPUState, sr[1]) },
2864 { "sr2", offsetof(CPUState, sr[2]) },
2865 { "sr3", offsetof(CPUState, sr[3]) },
2866 { "sr4", offsetof(CPUState, sr[4]) },
2867 { "sr5", offsetof(CPUState, sr[5]) },
2868 { "sr6", offsetof(CPUState, sr[6]) },
2869 { "sr7", offsetof(CPUState, sr[7]) },
2870 { "sr8", offsetof(CPUState, sr[8]) },
2871 { "sr9", offsetof(CPUState, sr[9]) },
2872 { "sr10", offsetof(CPUState, sr[10]) },
2873 { "sr11", offsetof(CPUState, sr[11]) },
2874 { "sr12", offsetof(CPUState, sr[12]) },
2875 { "sr13", offsetof(CPUState, sr[13]) },
2876 { "sr14", offsetof(CPUState, sr[14]) },
2877 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05002878 /* Too lazy to put BATs... */
2879 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
2880
2881 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
2882 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
2883 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
2884 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
2885 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
2886 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
2887 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
2888 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
2889 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
2890 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
2891 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
2892 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
2893 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
2894 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
2895 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
2896 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
2897 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
2898 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
2899 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
2900 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
2901 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
2902 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
2903 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
2904 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
2905 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
2906 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
2907 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
2908 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
2909 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
2910 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
2911 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
2912 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
2913 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
2914 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
2915 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
2916 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
2917 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
2918 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
2919 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
2920 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
2921 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
2922 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
2923 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
2924 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
2925 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
2926 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
2927 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
2928 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
2929 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
2930 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
2931 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
2932 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
2933 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
2934 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
2935 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
2936 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
2937 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
2938 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
2939 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
2940 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
2941 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
2942 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
2943 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
2944 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
2945 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
2946 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
2947
bellarde95c8d52004-09-30 22:22:08 +00002948#elif defined(TARGET_SPARC)
2949 { "g0", offsetof(CPUState, gregs[0]) },
2950 { "g1", offsetof(CPUState, gregs[1]) },
2951 { "g2", offsetof(CPUState, gregs[2]) },
2952 { "g3", offsetof(CPUState, gregs[3]) },
2953 { "g4", offsetof(CPUState, gregs[4]) },
2954 { "g5", offsetof(CPUState, gregs[5]) },
2955 { "g6", offsetof(CPUState, gregs[6]) },
2956 { "g7", offsetof(CPUState, gregs[7]) },
2957 { "o0", 0, monitor_get_reg },
2958 { "o1", 1, monitor_get_reg },
2959 { "o2", 2, monitor_get_reg },
2960 { "o3", 3, monitor_get_reg },
2961 { "o4", 4, monitor_get_reg },
2962 { "o5", 5, monitor_get_reg },
2963 { "o6", 6, monitor_get_reg },
2964 { "o7", 7, monitor_get_reg },
2965 { "l0", 8, monitor_get_reg },
2966 { "l1", 9, monitor_get_reg },
2967 { "l2", 10, monitor_get_reg },
2968 { "l3", 11, monitor_get_reg },
2969 { "l4", 12, monitor_get_reg },
2970 { "l5", 13, monitor_get_reg },
2971 { "l6", 14, monitor_get_reg },
2972 { "l7", 15, monitor_get_reg },
2973 { "i0", 16, monitor_get_reg },
2974 { "i1", 17, monitor_get_reg },
2975 { "i2", 18, monitor_get_reg },
2976 { "i3", 19, monitor_get_reg },
2977 { "i4", 20, monitor_get_reg },
2978 { "i5", 21, monitor_get_reg },
2979 { "i6", 22, monitor_get_reg },
2980 { "i7", 23, monitor_get_reg },
2981 { "pc", offsetof(CPUState, pc) },
2982 { "npc", offsetof(CPUState, npc) },
2983 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002984#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002985 { "psr", 0, &monitor_get_psr, },
2986 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002987#endif
bellarde95c8d52004-09-30 22:22:08 +00002988 { "tbr", offsetof(CPUState, tbr) },
2989 { "fsr", offsetof(CPUState, fsr) },
Richard Henderson30038fd2011-10-17 10:42:49 -07002990 { "f0", offsetof(CPUState, fpr[0].l.upper) },
2991 { "f1", offsetof(CPUState, fpr[0].l.lower) },
2992 { "f2", offsetof(CPUState, fpr[1].l.upper) },
2993 { "f3", offsetof(CPUState, fpr[1].l.lower) },
2994 { "f4", offsetof(CPUState, fpr[2].l.upper) },
2995 { "f5", offsetof(CPUState, fpr[2].l.lower) },
2996 { "f6", offsetof(CPUState, fpr[3].l.upper) },
2997 { "f7", offsetof(CPUState, fpr[3].l.lower) },
2998 { "f8", offsetof(CPUState, fpr[4].l.upper) },
2999 { "f9", offsetof(CPUState, fpr[4].l.lower) },
3000 { "f10", offsetof(CPUState, fpr[5].l.upper) },
3001 { "f11", offsetof(CPUState, fpr[5].l.lower) },
3002 { "f12", offsetof(CPUState, fpr[6].l.upper) },
3003 { "f13", offsetof(CPUState, fpr[6].l.lower) },
3004 { "f14", offsetof(CPUState, fpr[7].l.upper) },
3005 { "f15", offsetof(CPUState, fpr[7].l.lower) },
3006 { "f16", offsetof(CPUState, fpr[8].l.upper) },
3007 { "f17", offsetof(CPUState, fpr[8].l.lower) },
3008 { "f18", offsetof(CPUState, fpr[9].l.upper) },
3009 { "f19", offsetof(CPUState, fpr[9].l.lower) },
3010 { "f20", offsetof(CPUState, fpr[10].l.upper) },
3011 { "f21", offsetof(CPUState, fpr[10].l.lower) },
3012 { "f22", offsetof(CPUState, fpr[11].l.upper) },
3013 { "f23", offsetof(CPUState, fpr[11].l.lower) },
3014 { "f24", offsetof(CPUState, fpr[12].l.upper) },
3015 { "f25", offsetof(CPUState, fpr[12].l.lower) },
3016 { "f26", offsetof(CPUState, fpr[13].l.upper) },
3017 { "f27", offsetof(CPUState, fpr[13].l.lower) },
3018 { "f28", offsetof(CPUState, fpr[14].l.upper) },
3019 { "f29", offsetof(CPUState, fpr[14].l.lower) },
3020 { "f30", offsetof(CPUState, fpr[15].l.upper) },
3021 { "f31", offsetof(CPUState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003022#ifdef TARGET_SPARC64
Richard Henderson30038fd2011-10-17 10:42:49 -07003023 { "f32", offsetof(CPUState, fpr[16]) },
3024 { "f34", offsetof(CPUState, fpr[17]) },
3025 { "f36", offsetof(CPUState, fpr[18]) },
3026 { "f38", offsetof(CPUState, fpr[19]) },
3027 { "f40", offsetof(CPUState, fpr[20]) },
3028 { "f42", offsetof(CPUState, fpr[21]) },
3029 { "f44", offsetof(CPUState, fpr[22]) },
3030 { "f46", offsetof(CPUState, fpr[23]) },
3031 { "f48", offsetof(CPUState, fpr[24]) },
3032 { "f50", offsetof(CPUState, fpr[25]) },
3033 { "f52", offsetof(CPUState, fpr[26]) },
3034 { "f54", offsetof(CPUState, fpr[27]) },
3035 { "f56", offsetof(CPUState, fpr[28]) },
3036 { "f58", offsetof(CPUState, fpr[29]) },
3037 { "f60", offsetof(CPUState, fpr[30]) },
3038 { "f62", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003039 { "asi", offsetof(CPUState, asi) },
3040 { "pstate", offsetof(CPUState, pstate) },
3041 { "cansave", offsetof(CPUState, cansave) },
3042 { "canrestore", offsetof(CPUState, canrestore) },
3043 { "otherwin", offsetof(CPUState, otherwin) },
3044 { "wstate", offsetof(CPUState, wstate) },
3045 { "cleanwin", offsetof(CPUState, cleanwin) },
3046 { "fprs", offsetof(CPUState, fprs) },
3047#endif
bellard9307c4c2004-04-04 12:57:25 +00003048#endif
3049 { NULL },
3050};
3051
aliguori376253e2009-03-05 23:01:23 +00003052static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003053{
aliguori376253e2009-03-05 23:01:23 +00003054 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003055 longjmp(expr_env, 1);
3056}
3057
Markus Armbruster09b94182010-01-20 13:07:30 +01003058/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003059static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003060{
blueswir18662d652008-10-02 18:32:44 +00003061 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003062 void *ptr;
3063
bellard9307c4c2004-04-04 12:57:25 +00003064 for(md = monitor_defs; md->name != NULL; md++) {
3065 if (compare_cmd(name, md->name)) {
3066 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003067 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003068 } else {
bellard6a00d602005-11-21 23:25:50 +00003069 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003070 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003071 switch(md->type) {
3072 case MD_I32:
3073 *pval = *(int32_t *)ptr;
3074 break;
3075 case MD_TLONG:
3076 *pval = *(target_long *)ptr;
3077 break;
3078 default:
3079 *pval = 0;
3080 break;
3081 }
bellard9307c4c2004-04-04 12:57:25 +00003082 }
3083 return 0;
3084 }
3085 }
3086 return -1;
3087}
3088
3089static void next(void)
3090{
Blue Swirl660f11b2009-07-31 21:16:51 +00003091 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003092 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003093 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003094 pch++;
3095 }
3096}
3097
aliguori376253e2009-03-05 23:01:23 +00003098static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003099
aliguori376253e2009-03-05 23:01:23 +00003100static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003101{
blueswir1c2efc952007-09-25 17:28:42 +00003102 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003103 char *p;
bellard6a00d602005-11-21 23:25:50 +00003104 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003105
3106 switch(*pch) {
3107 case '+':
3108 next();
aliguori376253e2009-03-05 23:01:23 +00003109 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003110 break;
3111 case '-':
3112 next();
aliguori376253e2009-03-05 23:01:23 +00003113 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003114 break;
3115 case '~':
3116 next();
aliguori376253e2009-03-05 23:01:23 +00003117 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003118 break;
3119 case '(':
3120 next();
aliguori376253e2009-03-05 23:01:23 +00003121 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003122 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003123 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003124 }
3125 next();
3126 break;
bellard81d09122004-07-14 17:21:37 +00003127 case '\'':
3128 pch++;
3129 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003130 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003131 n = *pch;
3132 pch++;
3133 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003134 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003135 next();
3136 break;
bellard9307c4c2004-04-04 12:57:25 +00003137 case '$':
3138 {
3139 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003140 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003141
bellard9307c4c2004-04-04 12:57:25 +00003142 pch++;
3143 q = buf;
3144 while ((*pch >= 'a' && *pch <= 'z') ||
3145 (*pch >= 'A' && *pch <= 'Z') ||
3146 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003147 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003148 if ((q - buf) < sizeof(buf) - 1)
3149 *q++ = *pch;
3150 pch++;
3151 }
blueswir1cd390082008-11-16 13:53:32 +00003152 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003153 pch++;
3154 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003155 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003156 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003157 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003158 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003159 }
3160 break;
3161 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003162 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003163 n = 0;
3164 break;
3165 default:
blueswir17743e582007-09-24 18:39:04 +00003166#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003167 n = strtoull(pch, &p, 0);
3168#else
bellard9307c4c2004-04-04 12:57:25 +00003169 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003170#endif
bellard9307c4c2004-04-04 12:57:25 +00003171 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003172 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003173 }
3174 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003175 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003176 pch++;
3177 break;
3178 }
3179 return n;
3180}
3181
3182
aliguori376253e2009-03-05 23:01:23 +00003183static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003184{
blueswir1c2efc952007-09-25 17:28:42 +00003185 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003186 int op;
ths3b46e622007-09-17 08:09:54 +00003187
aliguori376253e2009-03-05 23:01:23 +00003188 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003189 for(;;) {
3190 op = *pch;
3191 if (op != '*' && op != '/' && op != '%')
3192 break;
3193 next();
aliguori376253e2009-03-05 23:01:23 +00003194 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003195 switch(op) {
3196 default:
3197 case '*':
3198 val *= val2;
3199 break;
3200 case '/':
3201 case '%':
ths5fafdf22007-09-16 21:08:06 +00003202 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003203 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003204 if (op == '/')
3205 val /= val2;
3206 else
3207 val %= val2;
3208 break;
3209 }
3210 }
3211 return val;
3212}
3213
aliguori376253e2009-03-05 23:01:23 +00003214static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003215{
blueswir1c2efc952007-09-25 17:28:42 +00003216 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003217 int op;
bellard9307c4c2004-04-04 12:57:25 +00003218
aliguori376253e2009-03-05 23:01:23 +00003219 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003220 for(;;) {
3221 op = *pch;
3222 if (op != '&' && op != '|' && op != '^')
3223 break;
3224 next();
aliguori376253e2009-03-05 23:01:23 +00003225 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003226 switch(op) {
3227 default:
3228 case '&':
3229 val &= val2;
3230 break;
3231 case '|':
3232 val |= val2;
3233 break;
3234 case '^':
3235 val ^= val2;
3236 break;
3237 }
3238 }
3239 return val;
3240}
3241
aliguori376253e2009-03-05 23:01:23 +00003242static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003243{
blueswir1c2efc952007-09-25 17:28:42 +00003244 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003245 int op;
bellard9307c4c2004-04-04 12:57:25 +00003246
aliguori376253e2009-03-05 23:01:23 +00003247 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003248 for(;;) {
3249 op = *pch;
3250 if (op != '+' && op != '-')
3251 break;
3252 next();
aliguori376253e2009-03-05 23:01:23 +00003253 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003254 if (op == '+')
3255 val += val2;
3256 else
3257 val -= val2;
3258 }
3259 return val;
3260}
3261
aliguori376253e2009-03-05 23:01:23 +00003262static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003263{
3264 pch = *pp;
3265 if (setjmp(expr_env)) {
3266 *pp = pch;
3267 return -1;
3268 }
blueswir1cd390082008-11-16 13:53:32 +00003269 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003270 pch++;
aliguori376253e2009-03-05 23:01:23 +00003271 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003272 *pp = pch;
3273 return 0;
3274}
3275
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003276static int get_double(Monitor *mon, double *pval, const char **pp)
3277{
3278 const char *p = *pp;
3279 char *tailp;
3280 double d;
3281
3282 d = strtod(p, &tailp);
3283 if (tailp == p) {
3284 monitor_printf(mon, "Number expected\n");
3285 return -1;
3286 }
3287 if (d != d || d - d != 0) {
3288 /* NaN or infinity */
3289 monitor_printf(mon, "Bad number\n");
3290 return -1;
3291 }
3292 *pval = d;
3293 *pp = tailp;
3294 return 0;
3295}
3296
bellard9307c4c2004-04-04 12:57:25 +00003297static int get_str(char *buf, int buf_size, const char **pp)
3298{
3299 const char *p;
3300 char *q;
3301 int c;
3302
bellard81d09122004-07-14 17:21:37 +00003303 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003304 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003305 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003306 p++;
3307 if (*p == '\0') {
3308 fail:
bellard81d09122004-07-14 17:21:37 +00003309 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003310 *pp = p;
3311 return -1;
3312 }
bellard9307c4c2004-04-04 12:57:25 +00003313 if (*p == '\"') {
3314 p++;
3315 while (*p != '\0' && *p != '\"') {
3316 if (*p == '\\') {
3317 p++;
3318 c = *p++;
3319 switch(c) {
3320 case 'n':
3321 c = '\n';
3322 break;
3323 case 'r':
3324 c = '\r';
3325 break;
3326 case '\\':
3327 case '\'':
3328 case '\"':
3329 break;
3330 default:
3331 qemu_printf("unsupported escape code: '\\%c'\n", c);
3332 goto fail;
3333 }
3334 if ((q - buf) < buf_size - 1) {
3335 *q++ = c;
3336 }
3337 } else {
3338 if ((q - buf) < buf_size - 1) {
3339 *q++ = *p;
3340 }
3341 p++;
3342 }
3343 }
3344 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003345 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003346 goto fail;
3347 }
3348 p++;
3349 } else {
blueswir1cd390082008-11-16 13:53:32 +00003350 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003351 if ((q - buf) < buf_size - 1) {
3352 *q++ = *p;
3353 }
3354 p++;
3355 }
bellard9307c4c2004-04-04 12:57:25 +00003356 }
bellard81d09122004-07-14 17:21:37 +00003357 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003358 *pp = p;
3359 return 0;
3360}
3361
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003362/*
3363 * Store the command-name in cmdname, and return a pointer to
3364 * the remaining of the command string.
3365 */
3366static const char *get_command_name(const char *cmdline,
3367 char *cmdname, size_t nlen)
3368{
3369 size_t len;
3370 const char *p, *pstart;
3371
3372 p = cmdline;
3373 while (qemu_isspace(*p))
3374 p++;
3375 if (*p == '\0')
3376 return NULL;
3377 pstart = p;
3378 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3379 p++;
3380 len = p - pstart;
3381 if (len > nlen - 1)
3382 len = nlen - 1;
3383 memcpy(cmdname, pstart, len);
3384 cmdname[len] = '\0';
3385 return p;
3386}
3387
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003388/**
3389 * Read key of 'type' into 'key' and return the current
3390 * 'type' pointer.
3391 */
3392static char *key_get_info(const char *type, char **key)
3393{
3394 size_t len;
3395 char *p, *str;
3396
3397 if (*type == ',')
3398 type++;
3399
3400 p = strchr(type, ':');
3401 if (!p) {
3402 *key = NULL;
3403 return NULL;
3404 }
3405 len = p - type;
3406
Anthony Liguori7267c092011-08-20 22:09:37 -05003407 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003408 memcpy(str, type, len);
3409 str[len] = '\0';
3410
3411 *key = str;
3412 return ++p;
3413}
3414
bellard9307c4c2004-04-04 12:57:25 +00003415static int default_fmt_format = 'x';
3416static int default_fmt_size = 4;
3417
3418#define MAX_ARGS 16
3419
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003420static int is_valid_option(const char *c, const char *typestr)
3421{
3422 char option[3];
3423
3424 option[0] = '-';
3425 option[1] = *c;
3426 option[2] = '\0';
3427
3428 typestr = strstr(typestr, option);
3429 return (typestr != NULL);
3430}
3431
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003432static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3433 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003434{
3435 const mon_cmd_t *cmd;
3436
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003437 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003438 if (compare_cmd(cmdname, cmd->name)) {
3439 return cmd;
3440 }
3441 }
3442
3443 return NULL;
3444}
3445
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003446static const mon_cmd_t *monitor_find_command(const char *cmdname)
3447{
3448 return search_dispatch_table(mon_cmds, cmdname);
3449}
3450
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003451static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3452{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003453 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003454}
3455
Anthony Liguoric227f092009-10-01 16:12:16 -05003456static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003457 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003458 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003459{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003460 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003461 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003462 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003463 char cmdname[256];
3464 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003465 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003466
3467#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003468 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003469#endif
ths3b46e622007-09-17 08:09:54 +00003470
bellard9307c4c2004-04-04 12:57:25 +00003471 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003472 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3473 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003474 return NULL;
ths3b46e622007-09-17 08:09:54 +00003475
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003476 cmd = monitor_find_command(cmdname);
3477 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003478 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003479 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003480 }
bellard9307c4c2004-04-04 12:57:25 +00003481
bellard9307c4c2004-04-04 12:57:25 +00003482 /* parse the parameters */
3483 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003484 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003485 typestr = key_get_info(typestr, &key);
3486 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003487 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003488 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003489 typestr++;
3490 switch(c) {
3491 case 'F':
bellard81d09122004-07-14 17:21:37 +00003492 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003493 case 's':
3494 {
3495 int ret;
ths3b46e622007-09-17 08:09:54 +00003496
blueswir1cd390082008-11-16 13:53:32 +00003497 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003498 p++;
3499 if (*typestr == '?') {
3500 typestr++;
3501 if (*p == '\0') {
3502 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003503 break;
bellard9307c4c2004-04-04 12:57:25 +00003504 }
3505 }
3506 ret = get_str(buf, sizeof(buf), &p);
3507 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003508 switch(c) {
3509 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003510 monitor_printf(mon, "%s: filename expected\n",
3511 cmdname);
bellard81d09122004-07-14 17:21:37 +00003512 break;
3513 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003514 monitor_printf(mon, "%s: block device name expected\n",
3515 cmdname);
bellard81d09122004-07-14 17:21:37 +00003516 break;
3517 default:
aliguori376253e2009-03-05 23:01:23 +00003518 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003519 break;
3520 }
bellard9307c4c2004-04-04 12:57:25 +00003521 goto fail;
3522 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003523 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003524 }
3525 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003526 case 'O':
3527 {
3528 QemuOptsList *opts_list;
3529 QemuOpts *opts;
3530
3531 opts_list = qemu_find_opts(key);
3532 if (!opts_list || opts_list->desc->name) {
3533 goto bad_type;
3534 }
3535 while (qemu_isspace(*p)) {
3536 p++;
3537 }
3538 if (!*p)
3539 break;
3540 if (get_str(buf, sizeof(buf), &p) < 0) {
3541 goto fail;
3542 }
3543 opts = qemu_opts_parse(opts_list, buf, 1);
3544 if (!opts) {
3545 goto fail;
3546 }
3547 qemu_opts_to_qdict(opts, qdict);
3548 qemu_opts_del(opts);
3549 }
3550 break;
bellard9307c4c2004-04-04 12:57:25 +00003551 case '/':
3552 {
3553 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003554
blueswir1cd390082008-11-16 13:53:32 +00003555 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003556 p++;
3557 if (*p == '/') {
3558 /* format found */
3559 p++;
3560 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003561 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003562 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003563 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003564 count = count * 10 + (*p - '0');
3565 p++;
3566 }
3567 }
3568 size = -1;
3569 format = -1;
3570 for(;;) {
3571 switch(*p) {
3572 case 'o':
3573 case 'd':
3574 case 'u':
3575 case 'x':
3576 case 'i':
3577 case 'c':
3578 format = *p++;
3579 break;
3580 case 'b':
3581 size = 1;
3582 p++;
3583 break;
3584 case 'h':
3585 size = 2;
3586 p++;
3587 break;
3588 case 'w':
3589 size = 4;
3590 p++;
3591 break;
3592 case 'g':
3593 case 'L':
3594 size = 8;
3595 p++;
3596 break;
3597 default:
3598 goto next;
3599 }
3600 }
3601 next:
blueswir1cd390082008-11-16 13:53:32 +00003602 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003603 monitor_printf(mon, "invalid char in format: '%c'\n",
3604 *p);
bellard9307c4c2004-04-04 12:57:25 +00003605 goto fail;
3606 }
bellard9307c4c2004-04-04 12:57:25 +00003607 if (format < 0)
3608 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003609 if (format != 'i') {
3610 /* for 'i', not specifying a size gives -1 as size */
3611 if (size < 0)
3612 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003613 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003614 }
bellard9307c4c2004-04-04 12:57:25 +00003615 default_fmt_format = format;
3616 } else {
3617 count = 1;
3618 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003619 if (format != 'i') {
3620 size = default_fmt_size;
3621 } else {
3622 size = -1;
3623 }
bellard9307c4c2004-04-04 12:57:25 +00003624 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003625 qdict_put(qdict, "count", qint_from_int(count));
3626 qdict_put(qdict, "format", qint_from_int(format));
3627 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003628 }
3629 break;
3630 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003631 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003632 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003633 {
blueswir1c2efc952007-09-25 17:28:42 +00003634 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003635
blueswir1cd390082008-11-16 13:53:32 +00003636 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003637 p++;
bellard34405572004-06-08 00:55:58 +00003638 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003639 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003640 if (*p == '\0') {
3641 typestr++;
3642 break;
3643 }
bellard34405572004-06-08 00:55:58 +00003644 } else {
3645 if (*p == '.') {
3646 p++;
blueswir1cd390082008-11-16 13:53:32 +00003647 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003648 p++;
bellard34405572004-06-08 00:55:58 +00003649 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003650 typestr++;
3651 break;
bellard34405572004-06-08 00:55:58 +00003652 }
3653 }
bellard13224a82006-07-14 22:03:35 +00003654 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003655 }
aliguori376253e2009-03-05 23:01:23 +00003656 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003657 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003658 /* Check if 'i' is greater than 32-bit */
3659 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3660 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3661 monitor_printf(mon, "integer is for 32-bit values\n");
3662 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003663 } else if (c == 'M') {
3664 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003665 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003666 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003667 }
3668 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003669 case 'o':
3670 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003671 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003672 char *end;
3673
3674 while (qemu_isspace(*p)) {
3675 p++;
3676 }
3677 if (*typestr == '?') {
3678 typestr++;
3679 if (*p == '\0') {
3680 break;
3681 }
3682 }
3683 val = strtosz(p, &end);
3684 if (val < 0) {
3685 monitor_printf(mon, "invalid size\n");
3686 goto fail;
3687 }
3688 qdict_put(qdict, key, qint_from_int(val));
3689 p = end;
3690 }
3691 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003692 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003693 {
3694 double val;
3695
3696 while (qemu_isspace(*p))
3697 p++;
3698 if (*typestr == '?') {
3699 typestr++;
3700 if (*p == '\0') {
3701 break;
3702 }
3703 }
3704 if (get_double(mon, &val, &p) < 0) {
3705 goto fail;
3706 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003707 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003708 switch (*p) {
3709 case 'm':
3710 val /= 1e3; p += 2; break;
3711 case 'u':
3712 val /= 1e6; p += 2; break;
3713 case 'n':
3714 val /= 1e9; p += 2; break;
3715 }
3716 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003717 if (*p && !qemu_isspace(*p)) {
3718 monitor_printf(mon, "Unknown unit suffix\n");
3719 goto fail;
3720 }
3721 qdict_put(qdict, key, qfloat_from_double(val));
3722 }
3723 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003724 case 'b':
3725 {
3726 const char *beg;
3727 int val;
3728
3729 while (qemu_isspace(*p)) {
3730 p++;
3731 }
3732 beg = p;
3733 while (qemu_isgraph(*p)) {
3734 p++;
3735 }
3736 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3737 val = 1;
3738 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3739 val = 0;
3740 } else {
3741 monitor_printf(mon, "Expected 'on' or 'off'\n");
3742 goto fail;
3743 }
3744 qdict_put(qdict, key, qbool_from_int(val));
3745 }
3746 break;
bellard9307c4c2004-04-04 12:57:25 +00003747 case '-':
3748 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003749 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003750 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003751 /* option */
ths3b46e622007-09-17 08:09:54 +00003752
bellard9307c4c2004-04-04 12:57:25 +00003753 c = *typestr++;
3754 if (c == '\0')
3755 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003756 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003757 p++;
bellard9307c4c2004-04-04 12:57:25 +00003758 if (*p == '-') {
3759 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003760 if(c != *p) {
3761 if(!is_valid_option(p, typestr)) {
3762
3763 monitor_printf(mon, "%s: unsupported option -%c\n",
3764 cmdname, *p);
3765 goto fail;
3766 } else {
3767 skip_key = 1;
3768 }
bellard9307c4c2004-04-04 12:57:25 +00003769 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003770 if(skip_key) {
3771 p = tmp;
3772 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003773 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003774 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003775 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003776 }
bellard9307c4c2004-04-04 12:57:25 +00003777 }
bellard9307c4c2004-04-04 12:57:25 +00003778 }
3779 break;
3780 default:
3781 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003782 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003783 goto fail;
3784 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003785 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003786 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003787 }
3788 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003789 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003790 p++;
3791 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003792 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3793 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003794 goto fail;
3795 }
3796
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003797 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003798
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003799fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05003800 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003801 return NULL;
3802}
3803
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003804void monitor_set_error(Monitor *mon, QError *qerror)
3805{
3806 /* report only the first error */
3807 if (!mon->error) {
3808 mon->error = qerror;
3809 } else {
3810 MON_DEBUG("Additional error report at %s:%d\n",
3811 qerror->file, qerror->linenr);
3812 QDECREF(qerror);
3813 }
3814}
3815
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003816static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3817{
Luiz Capitulino6d441432010-11-22 16:35:09 -02003818 if (ret && !monitor_has_error(mon)) {
3819 /*
3820 * If it returns failure, it must have passed on error.
3821 *
3822 * Action: Report an internal error to the client if in QMP.
3823 */
3824 qerror_report(QERR_UNDEFINED_ERROR);
3825 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3826 cmd->name);
3827 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003828
3829#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02003830 if (!ret && monitor_has_error(mon)) {
3831 /*
3832 * If it returns success, it must not have passed an error.
3833 *
3834 * Action: Report the passed error to the client.
3835 */
3836 MON_DEBUG("command '%s' returned success but passed an error\n",
3837 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003838 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02003839
3840 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3841 /*
3842 * Handlers should not call Monitor print functions.
3843 *
3844 * Action: Ignore them in QMP.
3845 *
3846 * (XXX: we don't check any 'info' or 'query' command here
3847 * because the user print function _is_ called by do_info(), hence
3848 * we will trigger this check. This problem will go away when we
3849 * make 'query' commands real and kill do_info())
3850 */
3851 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3852 cmd->name, mon_print_count_get(mon));
3853 }
3854#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003855}
3856
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003857static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003858{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003859 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003860 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003861
3862 qdict = qdict_new();
3863
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003864 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003865 if (!cmd)
3866 goto out;
3867
Luiz Capitulino4903de02010-09-16 11:01:32 -03003868 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06003869 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03003870 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03003871 QObject *data = NULL;
3872
3873 /* XXX: ignores the error code */
3874 cmd->mhandler.cmd_new(mon, qdict, &data);
3875 assert(!monitor_has_error(mon));
3876 if (data) {
3877 cmd->user_print(mon, data);
3878 qobject_decref(data);
3879 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03003880 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003881 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003882 }
3883
Luiz Capitulino13917be2009-10-07 13:41:54 -03003884out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003885 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003886}
3887
bellard81d09122004-07-14 17:21:37 +00003888static void cmd_completion(const char *name, const char *list)
3889{
3890 const char *p, *pstart;
3891 char cmd[128];
3892 int len;
3893
3894 p = list;
3895 for(;;) {
3896 pstart = p;
3897 p = strchr(p, '|');
3898 if (!p)
3899 p = pstart + strlen(pstart);
3900 len = p - pstart;
3901 if (len > sizeof(cmd) - 2)
3902 len = sizeof(cmd) - 2;
3903 memcpy(cmd, pstart, len);
3904 cmd[len] = '\0';
3905 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003906 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003907 }
3908 if (*p == '\0')
3909 break;
3910 p++;
3911 }
3912}
3913
3914static void file_completion(const char *input)
3915{
3916 DIR *ffs;
3917 struct dirent *d;
3918 char path[1024];
3919 char file[1024], file_prefix[1024];
3920 int input_path_len;
3921 const char *p;
3922
ths5fafdf22007-09-16 21:08:06 +00003923 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003924 if (!p) {
3925 input_path_len = 0;
3926 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003927 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003928 } else {
3929 input_path_len = p - input + 1;
3930 memcpy(path, input, input_path_len);
3931 if (input_path_len > sizeof(path) - 1)
3932 input_path_len = sizeof(path) - 1;
3933 path[input_path_len] = '\0';
3934 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3935 }
3936#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003937 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3938 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003939#endif
3940 ffs = opendir(path);
3941 if (!ffs)
3942 return;
3943 for(;;) {
3944 struct stat sb;
3945 d = readdir(ffs);
3946 if (!d)
3947 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003948
3949 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3950 continue;
3951 }
3952
bellard81d09122004-07-14 17:21:37 +00003953 if (strstart(d->d_name, file_prefix, NULL)) {
3954 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003955 if (input_path_len < sizeof(file))
3956 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3957 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003958 /* stat the file to find out if it's a directory.
3959 * In that case add a slash to speed up typing long paths
3960 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003961 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003962 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003963 }
aliguori731b0362009-03-05 23:01:42 +00003964 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003965 }
3966 }
3967 closedir(ffs);
3968}
3969
aliguori51de9762009-03-05 23:00:43 +00003970static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003971{
aliguori51de9762009-03-05 23:00:43 +00003972 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003973 const char *input = opaque;
3974
3975 if (input[0] == '\0' ||
3976 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003977 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003978 }
3979}
3980
3981/* NOTE: this parser is an approximate form of the real command parser */
3982static void parse_cmdline(const char *cmdline,
3983 int *pnb_args, char **args)
3984{
3985 const char *p;
3986 int nb_args, ret;
3987 char buf[1024];
3988
3989 p = cmdline;
3990 nb_args = 0;
3991 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003992 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003993 p++;
3994 if (*p == '\0')
3995 break;
3996 if (nb_args >= MAX_ARGS)
3997 break;
3998 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05003999 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004000 nb_args++;
4001 if (ret < 0)
4002 break;
4003 }
4004 *pnb_args = nb_args;
4005}
4006
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004007static const char *next_arg_type(const char *typestr)
4008{
4009 const char *p = strchr(typestr, ':');
4010 return (p != NULL ? ++p : typestr);
4011}
4012
aliguori4c36ba32009-03-05 23:01:37 +00004013static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004014{
4015 const char *cmdname;
4016 char *args[MAX_ARGS];
4017 int nb_args, i, len;
4018 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004019 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004020 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004021
4022 parse_cmdline(cmdline, &nb_args, args);
4023#ifdef DEBUG_COMPLETION
4024 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004025 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004026 }
4027#endif
4028
4029 /* if the line ends with a space, it means we want to complete the
4030 next arg */
4031 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004032 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004033 if (nb_args >= MAX_ARGS) {
4034 goto cleanup;
4035 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004036 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004037 }
4038 if (nb_args <= 1) {
4039 /* command completion */
4040 if (nb_args == 0)
4041 cmdname = "";
4042 else
4043 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004044 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004045 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004046 cmd_completion(cmdname, cmd->name);
4047 }
4048 } else {
4049 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004050 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4051 if (compare_cmd(args[0], cmd->name)) {
4052 break;
4053 }
bellard81d09122004-07-14 17:21:37 +00004054 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004055 if (!cmd->name) {
4056 goto cleanup;
4057 }
4058
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004059 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004060 for(i = 0; i < nb_args - 2; i++) {
4061 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004062 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004063 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004064 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004065 }
4066 }
4067 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004068 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004069 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004070 }
bellard81d09122004-07-14 17:21:37 +00004071 switch(*ptype) {
4072 case 'F':
4073 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004074 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004075 file_completion(str);
4076 break;
4077 case 'B':
4078 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004079 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004080 bdrv_iterate(block_completion_it, (void *)str);
4081 break;
bellard7fe48482004-10-09 18:08:01 +00004082 case 's':
4083 /* XXX: more generic ? */
4084 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004085 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004086 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4087 cmd_completion(str, cmd->name);
4088 }
bellard64866c32006-05-07 18:03:31 +00004089 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004090 char *sep = strrchr(str, '-');
4091 if (sep)
4092 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004093 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004094 for(key = key_defs; key->name != NULL; key++) {
4095 cmd_completion(str, key->name);
4096 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004097 } else if (!strcmp(cmd->name, "help|?")) {
4098 readline_set_completion_index(cur_mon->rs, strlen(str));
4099 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4100 cmd_completion(str, cmd->name);
4101 }
bellard7fe48482004-10-09 18:08:01 +00004102 }
4103 break;
bellard81d09122004-07-14 17:21:37 +00004104 default:
4105 break;
4106 }
4107 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004108
4109cleanup:
4110 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004111 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004112 }
bellard81d09122004-07-14 17:21:37 +00004113}
4114
aliguori731b0362009-03-05 23:01:42 +00004115static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004116{
aliguori731b0362009-03-05 23:01:42 +00004117 Monitor *mon = opaque;
4118
Jan Kiszkac62313b2009-12-04 14:05:29 +01004119 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004120}
4121
Luiz Capitulino09069b12010-02-04 18:10:06 -02004122static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4123{
4124 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4125 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4126}
4127
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004128/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004129 * Argument validation rules:
4130 *
4131 * 1. The argument must exist in cmd_args qdict
4132 * 2. The argument type must be the expected one
4133 *
4134 * Special case: If the argument doesn't exist in cmd_args and
4135 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4136 * checking is skipped for it.
4137 */
4138static int check_client_args_type(const QDict *client_args,
4139 const QDict *cmd_args, int flags)
4140{
4141 const QDictEntry *ent;
4142
4143 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4144 QObject *obj;
4145 QString *arg_type;
4146 const QObject *client_arg = qdict_entry_value(ent);
4147 const char *client_arg_name = qdict_entry_key(ent);
4148
4149 obj = qdict_get(cmd_args, client_arg_name);
4150 if (!obj) {
4151 if (flags & QMP_ACCEPT_UNKNOWNS) {
4152 /* handler accepts unknowns */
4153 continue;
4154 }
4155 /* client arg doesn't exist */
4156 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4157 return -1;
4158 }
4159
4160 arg_type = qobject_to_qstring(obj);
4161 assert(arg_type != NULL);
4162
4163 /* check if argument's type is correct */
4164 switch (qstring_get_str(arg_type)[0]) {
4165 case 'F':
4166 case 'B':
4167 case 's':
4168 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4169 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4170 "string");
4171 return -1;
4172 }
4173 break;
4174 case 'i':
4175 case 'l':
4176 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004177 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004178 if (qobject_type(client_arg) != QTYPE_QINT) {
4179 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4180 "int");
4181 return -1;
4182 }
4183 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004184 case 'T':
4185 if (qobject_type(client_arg) != QTYPE_QINT &&
4186 qobject_type(client_arg) != QTYPE_QFLOAT) {
4187 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4188 "number");
4189 return -1;
4190 }
4191 break;
4192 case 'b':
4193 case '-':
4194 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4195 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4196 "bool");
4197 return -1;
4198 }
4199 break;
4200 case 'O':
4201 assert(flags & QMP_ACCEPT_UNKNOWNS);
4202 break;
4203 case '/':
4204 case '.':
4205 /*
4206 * These types are not supported by QMP and thus are not
4207 * handled here. Fall through.
4208 */
4209 default:
4210 abort();
4211 }
4212 }
4213
4214 return 0;
4215}
4216
4217/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004218 * - Check if the client has passed all mandatory args
4219 * - Set special flags for argument validation
4220 */
4221static int check_mandatory_args(const QDict *cmd_args,
4222 const QDict *client_args, int *flags)
4223{
4224 const QDictEntry *ent;
4225
4226 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4227 const char *cmd_arg_name = qdict_entry_key(ent);
4228 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4229 assert(type != NULL);
4230
4231 if (qstring_get_str(type)[0] == 'O') {
4232 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4233 *flags |= QMP_ACCEPT_UNKNOWNS;
4234 } else if (qstring_get_str(type)[0] != '-' &&
4235 qstring_get_str(type)[1] != '?' &&
4236 !qdict_haskey(client_args, cmd_arg_name)) {
4237 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4238 return -1;
4239 }
4240 }
4241
4242 return 0;
4243}
4244
4245static QDict *qdict_from_args_type(const char *args_type)
4246{
4247 int i;
4248 QDict *qdict;
4249 QString *key, *type, *cur_qs;
4250
4251 assert(args_type != NULL);
4252
4253 qdict = qdict_new();
4254
4255 if (args_type == NULL || args_type[0] == '\0') {
4256 /* no args, empty qdict */
4257 goto out;
4258 }
4259
4260 key = qstring_new();
4261 type = qstring_new();
4262
4263 cur_qs = key;
4264
4265 for (i = 0;; i++) {
4266 switch (args_type[i]) {
4267 case ',':
4268 case '\0':
4269 qdict_put(qdict, qstring_get_str(key), type);
4270 QDECREF(key);
4271 if (args_type[i] == '\0') {
4272 goto out;
4273 }
4274 type = qstring_new(); /* qdict has ref */
4275 cur_qs = key = qstring_new();
4276 break;
4277 case ':':
4278 cur_qs = type;
4279 break;
4280 default:
4281 qstring_append_chr(cur_qs, args_type[i]);
4282 break;
4283 }
4284 }
4285
4286out:
4287 return qdict;
4288}
4289
4290/*
4291 * Client argument checking rules:
4292 *
4293 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004294 * 2. Each argument provided by the client must be expected
4295 * 3. Each argument provided by the client must have the type expected
4296 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004297 */
4298static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4299{
4300 int flags, err;
4301 QDict *cmd_args;
4302
4303 cmd_args = qdict_from_args_type(cmd->args_type);
4304
4305 flags = 0;
4306 err = check_mandatory_args(cmd_args, client_args, &flags);
4307 if (err) {
4308 goto out;
4309 }
4310
Luiz Capitulino4af91932010-06-22 11:44:05 -03004311 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004312
4313out:
4314 QDECREF(cmd_args);
4315 return err;
4316}
4317
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004318/*
4319 * Input object checking rules
4320 *
4321 * 1. Input object must be a dict
4322 * 2. The "execute" key must exist
4323 * 3. The "execute" key must be a string
4324 * 4. If the "arguments" key exists, it must be a dict
4325 * 5. If the "id" key exists, it can be anything (ie. json-value)
4326 * 6. Any argument not listed above is considered invalid
4327 */
4328static QDict *qmp_check_input_obj(QObject *input_obj)
4329{
4330 const QDictEntry *ent;
4331 int has_exec_key = 0;
4332 QDict *input_dict;
4333
4334 if (qobject_type(input_obj) != QTYPE_QDICT) {
4335 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4336 return NULL;
4337 }
4338
4339 input_dict = qobject_to_qdict(input_obj);
4340
4341 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4342 const char *arg_name = qdict_entry_key(ent);
4343 const QObject *arg_obj = qdict_entry_value(ent);
4344
4345 if (!strcmp(arg_name, "execute")) {
4346 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4347 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4348 "string");
4349 return NULL;
4350 }
4351 has_exec_key = 1;
4352 } else if (!strcmp(arg_name, "arguments")) {
4353 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4354 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4355 "object");
4356 return NULL;
4357 }
4358 } else if (!strcmp(arg_name, "id")) {
4359 /* FIXME: check duplicated IDs for async commands */
4360 } else {
4361 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4362 return NULL;
4363 }
4364 }
4365
4366 if (!has_exec_key) {
4367 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4368 return NULL;
4369 }
4370
4371 return input_dict;
4372}
4373
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004374static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4375 const QDict *params)
4376{
4377 int ret;
4378 QObject *data = NULL;
4379
4380 mon_print_count_init(mon);
4381
4382 ret = cmd->mhandler.cmd_new(mon, params, &data);
4383 handler_audit(mon, cmd, ret);
4384 monitor_protocol_emitter(mon, data);
4385 qobject_decref(data);
4386}
4387
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004388static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4389{
4390 int err;
4391 QObject *obj;
4392 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004393 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004394 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004395 Monitor *mon = cur_mon;
4396
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004397 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004398
4399 obj = json_parser_parse(tokens, NULL);
4400 if (!obj) {
4401 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004402 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004403 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004404 }
4405
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004406 input = qmp_check_input_obj(obj);
4407 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004408 qobject_decref(obj);
4409 goto err_out;
4410 }
4411
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004412 mon->mc->id = qdict_get(input, "id");
4413 qobject_incref(mon->mc->id);
4414
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004415 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004416 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004417 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004418 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004419 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004420 }
4421
Anthony Liguorie3193602011-09-02 12:34:47 -05004422 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004423 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004424 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004425 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004426 }
4427
4428 obj = qdict_get(input, "arguments");
4429 if (!obj) {
4430 args = qdict_new();
4431 } else {
4432 args = qobject_to_qdict(obj);
4433 QINCREF(args);
4434 }
4435
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004436 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004437 if (err < 0) {
4438 goto err_out;
4439 }
4440
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004441 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004442 err = qmp_async_cmd_handler(mon, cmd, args);
4443 if (err) {
4444 /* emit the error response */
4445 goto err_out;
4446 }
Adam Litke940cc302010-01-25 12:18:44 -06004447 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004448 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004449 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004450
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004451 goto out;
4452
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004453err_out:
4454 monitor_protocol_emitter(mon, NULL);
4455out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004456 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004457 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004458}
4459
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004460/**
4461 * monitor_control_read(): Read and handle QMP input
4462 */
4463static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4464{
4465 Monitor *old_mon = cur_mon;
4466
4467 cur_mon = opaque;
4468
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004469 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004470
4471 cur_mon = old_mon;
4472}
4473
aliguori731b0362009-03-05 23:01:42 +00004474static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004475{
aliguori731b0362009-03-05 23:01:42 +00004476 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004477 int i;
aliguori376253e2009-03-05 23:01:23 +00004478
aliguori731b0362009-03-05 23:01:42 +00004479 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004480
aliguoricde76ee2009-03-05 23:01:51 +00004481 if (cur_mon->rs) {
4482 for (i = 0; i < size; i++)
4483 readline_handle_byte(cur_mon->rs, buf[i]);
4484 } else {
4485 if (size == 0 || buf[size - 1] != 0)
4486 monitor_printf(cur_mon, "corrupted command\n");
4487 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004488 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004489 }
aliguori731b0362009-03-05 23:01:42 +00004490
4491 cur_mon = old_mon;
4492}
aliguorid8f44602008-10-06 13:52:44 +00004493
aliguori376253e2009-03-05 23:01:23 +00004494static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004495{
aliguori731b0362009-03-05 23:01:42 +00004496 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004497 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004498 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004499}
4500
aliguoricde76ee2009-03-05 23:01:51 +00004501int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004502{
aliguoricde76ee2009-03-05 23:01:51 +00004503 if (!mon->rs)
4504 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004505 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004506 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004507}
4508
aliguori376253e2009-03-05 23:01:23 +00004509void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004510{
aliguoricde76ee2009-03-05 23:01:51 +00004511 if (!mon->rs)
4512 return;
aliguori731b0362009-03-05 23:01:42 +00004513 if (--mon->suspend_cnt == 0)
4514 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004515}
4516
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004517static QObject *get_qmp_greeting(void)
4518{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004519 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004520
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004521 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004522 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4523}
4524
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004525/**
4526 * monitor_control_event(): Print QMP gretting
4527 */
4528static void monitor_control_event(void *opaque, int event)
4529{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004530 QObject *data;
4531 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004532
Luiz Capitulino47116d12010-02-08 17:01:30 -02004533 switch (event) {
4534 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004535 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004536 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004537 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004538 monitor_json_emitter(mon, data);
4539 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004540 break;
4541 case CHR_EVENT_CLOSED:
4542 json_message_parser_destroy(&mon->mc->parser);
4543 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004544 }
4545}
4546
aliguori731b0362009-03-05 23:01:42 +00004547static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004548{
aliguori376253e2009-03-05 23:01:23 +00004549 Monitor *mon = opaque;
4550
aliguori2724b182009-03-05 23:01:47 +00004551 switch (event) {
4552 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004553 mon->mux_out = 0;
4554 if (mon->reset_seen) {
4555 readline_restart(mon->rs);
4556 monitor_resume(mon);
4557 monitor_flush(mon);
4558 } else {
4559 mon->suspend_cnt = 0;
4560 }
aliguori2724b182009-03-05 23:01:47 +00004561 break;
ths86e94de2007-01-05 22:01:59 +00004562
aliguori2724b182009-03-05 23:01:47 +00004563 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004564 if (mon->reset_seen) {
4565 if (mon->suspend_cnt == 0) {
4566 monitor_printf(mon, "\n");
4567 }
4568 monitor_flush(mon);
4569 monitor_suspend(mon);
4570 } else {
4571 mon->suspend_cnt++;
4572 }
4573 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004574 break;
4575
Amit Shahb6b8df52009-10-07 18:31:16 +05304576 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004577 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4578 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004579 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004580 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004581 }
4582 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004583 break;
4584 }
ths86e94de2007-01-05 22:01:59 +00004585}
4586
Wayne Xia816f8922011-10-12 11:32:41 +08004587static int
4588compare_mon_cmd(const void *a, const void *b)
4589{
4590 return strcmp(((const mon_cmd_t *)a)->name,
4591 ((const mon_cmd_t *)b)->name);
4592}
4593
4594static void sortcmdlist(void)
4595{
4596 int array_num;
4597 int elem_size = sizeof(mon_cmd_t);
4598
4599 array_num = sizeof(mon_cmds)/elem_size-1;
4600 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4601
4602 array_num = sizeof(info_cmds)/elem_size-1;
4603 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4604}
4605
aliguori76655d62009-03-06 20:27:37 +00004606
4607/*
4608 * Local variables:
4609 * c-indent-level: 4
4610 * c-basic-offset: 4
4611 * tab-width: 8
4612 * End:
4613 */
4614
aliguori731b0362009-03-05 23:01:42 +00004615void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004616{
aliguori731b0362009-03-05 23:01:42 +00004617 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004618 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004619
4620 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004621 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004622 is_first_init = 0;
4623 }
aliguori87127162009-03-05 23:01:29 +00004624
Anthony Liguori7267c092011-08-20 22:09:37 -05004625 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004626
aliguori87127162009-03-05 23:01:29 +00004627 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004628 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004629 if (flags & MONITOR_USE_READLINE) {
4630 mon->rs = readline_init(mon, monitor_find_completion);
4631 monitor_read_command(mon, 0);
4632 }
aliguori87127162009-03-05 23:01:29 +00004633
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004634 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004635 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004636 /* Control mode requires special handlers */
4637 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4638 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004639 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004640 } else {
4641 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4642 monitor_event, mon);
4643 }
aliguori87127162009-03-05 23:01:29 +00004644
Blue Swirl72cf2d42009-09-12 07:36:22 +00004645 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004646 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4647 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004648
4649 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004650}
4651
aliguori376253e2009-03-05 23:01:23 +00004652static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004653{
aliguoribb5fc202009-03-05 23:01:15 +00004654 BlockDriverState *bs = opaque;
4655 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004656
aliguoribb5fc202009-03-05 23:01:15 +00004657 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004658 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004659 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004660 }
aliguori731b0362009-03-05 23:01:42 +00004661 if (mon->password_completion_cb)
4662 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004663
aliguori731b0362009-03-05 23:01:42 +00004664 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004665}
aliguoric0f4ce72009-03-05 23:01:01 +00004666
Anthony Liguori7060b472011-09-02 12:34:50 -05004667ReadLineState *monitor_get_rs(Monitor *mon)
4668{
4669 return mon->rs;
4670}
4671
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004672int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4673 BlockDriverCompletionFunc *completion_cb,
4674 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004675{
aliguoricde76ee2009-03-05 23:01:51 +00004676 int err;
4677
aliguoribb5fc202009-03-05 23:01:15 +00004678 if (!bdrv_key_required(bs)) {
4679 if (completion_cb)
4680 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004681 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004682 }
aliguoric0f4ce72009-03-05 23:01:01 +00004683
Luiz Capitulino94171e12009-12-07 21:37:00 +01004684 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004685 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4686 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004687 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004688 }
4689
aliguori376253e2009-03-05 23:01:23 +00004690 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4691 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004692
aliguori731b0362009-03-05 23:01:42 +00004693 mon->password_completion_cb = completion_cb;
4694 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004695
aliguoricde76ee2009-03-05 23:01:51 +00004696 err = monitor_read_password(mon, bdrv_password_cb, bs);
4697
4698 if (err && completion_cb)
4699 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004700
4701 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004702}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004703
4704int monitor_read_block_device_key(Monitor *mon, const char *device,
4705 BlockDriverCompletionFunc *completion_cb,
4706 void *opaque)
4707{
4708 BlockDriverState *bs;
4709
4710 bs = bdrv_find(device);
4711 if (!bs) {
4712 monitor_printf(mon, "Device not found %s\n", device);
4713 return -1;
4714 }
4715
4716 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4717}