blob: 0c90506cdd37413bdeb9cdeb77a312d3b2a35853 [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"
Lluís31965ae2011-08-31 20:31:24 +020060#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020061#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020062#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053063#endif
Lluísfc764102011-08-31 20:31:18 +020064#include "trace/control.h"
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
bellard9dc39cb2004-03-14 21:38:27 +000070//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000071//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000072
bellard9307c4c2004-04-04 12:57:25 +000073/*
74 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000075 *
bellard9307c4c2004-04-04 12:57:25 +000076 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000077 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000078 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010079 * 'O' option string of the form NAME=VALUE,...
80 * parsed according to QemuOptsList given by its name
81 * Example: 'device:O' uses qemu_device_opts.
82 * Restriction: only lists with empty desc are supported
83 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000084 * 'i' 32 bit integer
85 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010086 * 'M' just like 'l', except in user mode the value is
87 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020088 * 'o' octets (aka bytes)
89 * user mode accepts an optional T, t, G, g, M, m, K, k
90 * suffix, which multiplies the value by 2^40 for
91 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
92 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010093 * 'T' double
94 * user mode accepts an optional ms, us, ns suffix,
95 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000096 * '/' optional gdb-like print format (like "/10x")
97 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030098 * '?' optional type (for all types, except '/')
99 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100100 * 'b' boolean
101 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300102 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000103 *
104 */
105
Adam Litke940cc302010-01-25 12:18:44 -0600106typedef struct MonitorCompletionData MonitorCompletionData;
107struct MonitorCompletionData {
108 Monitor *mon;
109 void (*user_print)(Monitor *mon, const QObject *data);
110};
111
Anthony Liguoric227f092009-10-01 16:12:16 -0500112typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000113 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000114 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000115 const char *params;
116 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300117 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300118 union {
119 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300120 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600121 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300122 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200123 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600124 int (*cmd_async)(Monitor *mon, const QDict *params,
125 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500127 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200128 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500129} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000130
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100131/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500132typedef struct mon_fd_t mon_fd_t;
133struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100134 char *name;
135 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500136 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137};
138
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200139typedef struct MonitorControl {
140 QObject *id;
141 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200142 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200143} MonitorControl;
144
aliguori87127162009-03-05 23:01:29 +0000145struct Monitor {
146 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200147 int mux_out;
148 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000149 int flags;
150 int suspend_cnt;
151 uint8_t outbuf[1024];
152 int outbuf_index;
153 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200154 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000155 CPUState *mon_cpu;
156 BlockDriverCompletionFunc *password_completion_cb;
157 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200158#ifdef CONFIG_DEBUG_MONITOR
159 int print_calls_nr;
160#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200161 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500162 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000164};
165
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200166#ifdef CONFIG_DEBUG_MONITOR
167#define MON_DEBUG(fmt, ...) do { \
168 fprintf(stderr, "Monitor: "); \
169 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200170
171static inline void mon_print_count_inc(Monitor *mon)
172{
173 mon->print_calls_nr++;
174}
175
176static inline void mon_print_count_init(Monitor *mon)
177{
178 mon->print_calls_nr = 0;
179}
180
181static inline int mon_print_count_get(const Monitor *mon)
182{
183 return mon->print_calls_nr;
184}
185
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200186#else /* !CONFIG_DEBUG_MONITOR */
187#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200188static inline void mon_print_count_inc(Monitor *mon) { }
189static inline void mon_print_count_init(Monitor *mon) { }
190static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200191#endif /* CONFIG_DEBUG_MONITOR */
192
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300193/* QMP checker flags */
194#define QMP_ACCEPT_UNKNOWNS 1
195
Blue Swirl72cf2d42009-09-12 07:36:22 +0000196static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000197
Anthony Liguoric227f092009-10-01 16:12:16 -0500198static const mon_cmd_t mon_cmds[];
199static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000200
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300201static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300202static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300203
Markus Armbruster8631b602010-02-18 11:41:55 +0100204Monitor *cur_mon;
205Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000206
aliguori731b0362009-03-05 23:01:42 +0000207static void monitor_command_cb(Monitor *mon, const char *cmdline,
208 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000209
Luiz Capitulino09069b12010-02-04 18:10:06 -0200210static inline int qmp_cmd_mode(const Monitor *mon)
211{
212 return (mon->mc ? mon->mc->command_mode : 0);
213}
214
Luiz Capitulino418173c2009-11-26 22:58:51 -0200215/* Return true if in control mode, false otherwise */
216static inline int monitor_ctrl_mode(const Monitor *mon)
217{
218 return (mon->flags & MONITOR_USE_CONTROL);
219}
220
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100221/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
222int monitor_cur_is_qmp(void)
223{
224 return cur_mon && monitor_ctrl_mode(cur_mon);
225}
226
aliguori731b0362009-03-05 23:01:42 +0000227static void monitor_read_command(Monitor *mon, int show_prompt)
228{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200229 if (!mon->rs)
230 return;
231
aliguori731b0362009-03-05 23:01:42 +0000232 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
233 if (show_prompt)
234 readline_show_prompt(mon->rs);
235}
bellard6a00d602005-11-21 23:25:50 +0000236
aliguoricde76ee2009-03-05 23:01:51 +0000237static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
238 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000239{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100240 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100241 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100242 return -EINVAL;
243 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000244 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
245 /* prompt is printed on return from the command handler */
246 return 0;
247 } else {
248 monitor_printf(mon, "terminal does not support password prompting\n");
249 return -ENOTTY;
250 }
aliguoribb5fc202009-03-05 23:01:15 +0000251}
252
aliguori376253e2009-03-05 23:01:23 +0000253void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000254{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200255 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500256 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000257 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000258 }
259}
260
261/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000262static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000263{
ths60fe76f2007-12-16 03:02:09 +0000264 char c;
aliguori731b0362009-03-05 23:01:42 +0000265
bellard7e2515e2004-08-01 21:52:19 +0000266 for(;;) {
267 c = *str++;
268 if (c == '\0')
269 break;
bellard7ba12602006-07-14 20:26:42 +0000270 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000271 mon->outbuf[mon->outbuf_index++] = '\r';
272 mon->outbuf[mon->outbuf_index++] = c;
273 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
274 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000275 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000276 }
277}
278
aliguori376253e2009-03-05 23:01:23 +0000279void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000280{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200281 char buf[4096];
282
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200283 if (!mon)
284 return;
285
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200286 mon_print_count_inc(mon);
287
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200288 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200289 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200290 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200291
292 vsnprintf(buf, sizeof(buf), fmt, ap);
293 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000294}
295
aliguori376253e2009-03-05 23:01:23 +0000296void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000297{
298 va_list ap;
299 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000300 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000301 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000302}
303
aliguori376253e2009-03-05 23:01:23 +0000304void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000305{
306 int i;
307
308 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000309 switch (filename[i]) {
310 case ' ':
311 case '"':
312 case '\\':
313 monitor_printf(mon, "\\%c", filename[i]);
314 break;
315 case '\t':
316 monitor_printf(mon, "\\t");
317 break;
318 case '\r':
319 monitor_printf(mon, "\\r");
320 break;
321 case '\n':
322 monitor_printf(mon, "\\n");
323 break;
324 default:
325 monitor_printf(mon, "%c", filename[i]);
326 break;
327 }
thsfef30742006-12-22 14:11:32 +0000328 }
329}
330
Stefan Weil8b7968f2010-09-23 21:28:05 +0200331static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
332 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000333{
334 va_list ap;
335 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000336 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000337 va_end(ap);
338 return 0;
339}
340
Luiz Capitulino13c74252009-10-07 13:41:55 -0300341static void monitor_user_noop(Monitor *mon, const QObject *data) { }
342
Luiz Capitulino9e807212010-09-16 10:58:59 -0300343static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300344{
345 return cmd->user_print != NULL;
346}
347
Luiz Capitulino4903de02010-09-16 11:01:32 -0300348static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600349{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200350 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600351}
352
Luiz Capitulino8204a912009-11-18 23:05:31 -0200353static inline int monitor_has_error(const Monitor *mon)
354{
355 return mon->error != NULL;
356}
357
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200358static void monitor_json_emitter(Monitor *mon, const QObject *data)
359{
360 QString *json;
361
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200362 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
363 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200364 assert(json != NULL);
365
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200366 qstring_append_chr(json, '\n');
367 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200368
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200369 QDECREF(json);
370}
371
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200372static void monitor_protocol_emitter(Monitor *mon, QObject *data)
373{
374 QDict *qmp;
375
376 qmp = qdict_new();
377
378 if (!monitor_has_error(mon)) {
379 /* success response */
380 if (data) {
381 qobject_incref(data);
382 qdict_put_obj(qmp, "return", data);
383 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200384 /* return an empty QDict by default */
385 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200386 }
387 } else {
388 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100389 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200390 qdict_put(qmp, "error", mon->error->error);
391 QINCREF(mon->error->error);
392 QDECREF(mon->error);
393 mon->error = NULL;
394 }
395
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200396 if (mon->mc->id) {
397 qdict_put_obj(qmp, "id", mon->mc->id);
398 mon->mc->id = NULL;
399 }
400
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200401 monitor_json_emitter(mon, QOBJECT(qmp));
402 QDECREF(qmp);
403}
404
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200405static void timestamp_put(QDict *qdict)
406{
407 int err;
408 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000409 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410
Blue Swirld08d6f02009-12-04 18:06:39 +0000411 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200412 if (err < 0)
413 return;
414
415 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
416 "'microseconds': %" PRId64 " }",
417 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418 qdict_put_obj(qdict, "timestamp", obj);
419}
420
421/**
422 * monitor_protocol_event(): Generate a Monitor event
423 *
424 * Event-specific data can be emitted through the (optional) 'data' parameter.
425 */
426void monitor_protocol_event(MonitorEvent event, QObject *data)
427{
428 QDict *qmp;
429 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600430 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200431
Blue Swirl242cd002009-12-04 18:05:45 +0000432 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200433
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200434 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000435 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200436 event_name = "SHUTDOWN";
437 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000438 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200439 event_name = "RESET";
440 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000441 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200442 event_name = "POWERDOWN";
443 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000444 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200445 event_name = "STOP";
446 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300447 case QEVENT_RESUME:
448 event_name = "RESUME";
449 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200450 case QEVENT_VNC_CONNECTED:
451 event_name = "VNC_CONNECTED";
452 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200453 case QEVENT_VNC_INITIALIZED:
454 event_name = "VNC_INITIALIZED";
455 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200456 case QEVENT_VNC_DISCONNECTED:
457 event_name = "VNC_DISCONNECTED";
458 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200459 case QEVENT_BLOCK_IO_ERROR:
460 event_name = "BLOCK_IO_ERROR";
461 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300462 case QEVENT_RTC_CHANGE:
463 event_name = "RTC_CHANGE";
464 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300465 case QEVENT_WATCHDOG:
466 event_name = "WATCHDOG";
467 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200468 case QEVENT_SPICE_CONNECTED:
469 event_name = "SPICE_CONNECTED";
470 break;
471 case QEVENT_SPICE_INITIALIZED:
472 event_name = "SPICE_INITIALIZED";
473 break;
474 case QEVENT_SPICE_DISCONNECTED:
475 event_name = "SPICE_DISCONNECTED";
476 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200477 default:
478 abort();
479 break;
480 }
481
482 qmp = qdict_new();
483 timestamp_put(qmp);
484 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200485 if (data) {
486 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200487 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200488 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200489
Adam Litkef039a562010-01-15 08:34:02 -0600490 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200491 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200492 monitor_json_emitter(mon, QOBJECT(qmp));
493 }
Adam Litkef039a562010-01-15 08:34:02 -0600494 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200495 QDECREF(qmp);
496}
497
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200498static int do_qmp_capabilities(Monitor *mon, const QDict *params,
499 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200500{
501 /* Will setup QMP capabilities in the future */
502 if (monitor_ctrl_mode(mon)) {
503 mon->mc->command_mode = 1;
504 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200505
506 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200507}
508
Luiz Capitulino0268d972010-10-22 10:08:02 -0200509static int mon_set_cpu(int cpu_index);
510static void handle_user_command(Monitor *mon, const char *cmdline);
511
512static int do_hmp_passthrough(Monitor *mon, const QDict *params,
513 QObject **ret_data)
514{
515 int ret = 0;
516 Monitor *old_mon, hmp;
517 CharDriverState mchar;
518
519 memset(&hmp, 0, sizeof(hmp));
520 qemu_chr_init_mem(&mchar);
521 hmp.chr = &mchar;
522
523 old_mon = cur_mon;
524 cur_mon = &hmp;
525
526 if (qdict_haskey(params, "cpu-index")) {
527 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
528 if (ret < 0) {
529 cur_mon = old_mon;
530 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
531 goto out;
532 }
533 }
534
535 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
536 cur_mon = old_mon;
537
538 if (qemu_chr_mem_osize(hmp.chr) > 0) {
539 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
540 }
541
542out:
543 qemu_chr_close_mem(hmp.chr);
544 return ret;
545}
546
bellard9dc39cb2004-03-14 21:38:27 +0000547static int compare_cmd(const char *name, const char *list)
548{
549 const char *p, *pstart;
550 int len;
551 len = strlen(name);
552 p = list;
553 for(;;) {
554 pstart = p;
555 p = strchr(p, '|');
556 if (!p)
557 p = pstart + strlen(pstart);
558 if ((p - pstart) == len && !memcmp(pstart, name, len))
559 return 1;
560 if (*p == '\0')
561 break;
562 p++;
563 }
564 return 0;
565}
566
Anthony Liguoric227f092009-10-01 16:12:16 -0500567static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000568 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000569{
Anthony Liguoric227f092009-10-01 16:12:16 -0500570 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000571
572 for(cmd = cmds; cmd->name != NULL; cmd++) {
573 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000574 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
575 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000576 }
577}
578
aliguori376253e2009-03-05 23:01:23 +0000579static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000580{
581 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000582 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000583 } else {
aliguori376253e2009-03-05 23:01:23 +0000584 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000585 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000586 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000587 monitor_printf(mon, "Log items (comma separated):\n");
588 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000589 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000590 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000591 }
592 }
bellard9dc39cb2004-03-14 21:38:27 +0000593 }
594}
595
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300596static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300597{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300598 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300599}
600
Lluísfc764102011-08-31 20:31:18 +0200601static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530602{
603 const char *tp_name = qdict_get_str(qdict, "name");
604 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200605 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000606
607 if (!ret) {
608 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
609 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530610}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100611
Lluís31965ae2011-08-31 20:31:24 +0200612#ifdef CONFIG_SIMPLE_TRACE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100613static void do_trace_file(Monitor *mon, const QDict *qdict)
614{
615 const char *op = qdict_get_try_str(qdict, "op");
616 const char *arg = qdict_get_try_str(qdict, "arg");
617
618 if (!op) {
619 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
620 } else if (!strcmp(op, "on")) {
621 st_set_trace_file_enabled(true);
622 } else if (!strcmp(op, "off")) {
623 st_set_trace_file_enabled(false);
624 } else if (!strcmp(op, "flush")) {
625 st_flush_trace_buffer();
626 } else if (!strcmp(op, "set")) {
627 if (arg) {
628 st_set_trace_file(arg);
629 }
630 } else {
631 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
632 help_cmd(mon, "trace-file");
633 }
634}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530635#endif
636
Adam Litke940cc302010-01-25 12:18:44 -0600637static void user_monitor_complete(void *opaque, QObject *ret_data)
638{
639 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
640
641 if (ret_data) {
642 data->user_print(data->mon, ret_data);
643 }
644 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500645 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600646}
647
648static void qmp_monitor_complete(void *opaque, QObject *ret_data)
649{
650 monitor_protocol_emitter(opaque, ret_data);
651}
652
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300653static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
654 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600655{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300656 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600657}
658
659static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
660{
661 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
662}
663
664static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
665 const QDict *params)
666{
667 int ret;
668
Anthony Liguori7267c092011-08-20 22:09:37 -0500669 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600670 cb_data->mon = mon;
671 cb_data->user_print = cmd->user_print;
672 monitor_suspend(mon);
673 ret = cmd->mhandler.cmd_async(mon, params,
674 user_monitor_complete, cb_data);
675 if (ret < 0) {
676 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500677 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600678 }
679}
680
681static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
682{
683 int ret;
684
Anthony Liguori7267c092011-08-20 22:09:37 -0500685 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600686 cb_data->mon = mon;
687 cb_data->user_print = cmd->user_print;
688 monitor_suspend(mon);
689 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
690 if (ret < 0) {
691 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500692 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600693 }
694}
695
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300696static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000697{
Anthony Liguoric227f092009-10-01 16:12:16 -0500698 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300699 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000700
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200701 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000702 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200703 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300704
705 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000706 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300707 break;
bellard9dc39cb2004-03-14 21:38:27 +0000708 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300709
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200710 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200712 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300713
Luiz Capitulino4903de02010-09-16 11:01:32 -0300714 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300715 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300716 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300717 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200718
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300719 cmd->mhandler.info_new(mon, &info_data);
720 if (info_data) {
721 cmd->user_print(mon, info_data);
722 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200723 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300724 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300725 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300726 }
727
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300728 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300729
730help:
731 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000732}
733
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300734static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
Luiz Capitulino1a728672009-12-10 17:15:56 -0200735{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300736 CommandInfoList *info;
Luiz Capitulino1a728672009-12-10 17:15:56 -0200737
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300738 info = g_malloc0(sizeof(*info));
739 info->value = g_malloc0(sizeof(*info->value));
740 info->value->name = g_strdup(cmd_name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200741
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300742 return info;
Luiz Capitulino1a728672009-12-10 17:15:56 -0200743}
744
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300745CommandInfoList *qmp_query_commands(Error **errp)
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200746{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300747 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200748 const mon_cmd_t *cmd;
749
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300750 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300751 info = alloc_cmd_entry(cmd->name);
752 info->next = cmd_list;
753 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200754 }
755
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300756 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300757 char buf[128];
758 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300759 info = alloc_cmd_entry(buf);
760 info->next = cmd_list;
761 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200762 }
763
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300764 return cmd_list;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200765}
766
bellard6a00d602005-11-21 23:25:50 +0000767/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000768static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000769{
770 CPUState *env;
771
772 for(env = first_cpu; env != NULL; env = env->next_cpu) {
773 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000774 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000775 return 0;
776 }
777 }
778 return -1;
779}
780
pbrook9596ebb2007-11-18 01:44:38 +0000781static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000782{
aliguori731b0362009-03-05 23:01:42 +0000783 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000784 mon_set_cpu(0);
785 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300786 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000787 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000788}
789
aliguori376253e2009-03-05 23:01:23 +0000790static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000791{
bellard6a00d602005-11-21 23:25:50 +0000792 CPUState *env;
793 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000794#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000795 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000796 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000797#else
aliguori376253e2009-03-05 23:01:23 +0000798 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000799 0);
bellard9307c4c2004-04-04 12:57:25 +0000800#endif
801}
802
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300803static void print_cpu_iter(QObject *obj, void *opaque)
804{
805 QDict *cpu;
806 int active = ' ';
807 Monitor *mon = opaque;
808
809 assert(qobject_type(obj) == QTYPE_QDICT);
810 cpu = qobject_to_qdict(obj);
811
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200812 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300813 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200814 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300815
816 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
817
818#if defined(TARGET_I386)
819 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
820 (target_ulong) qdict_get_int(cpu, "pc"));
821#elif defined(TARGET_PPC)
822 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
823 (target_long) qdict_get_int(cpu, "nip"));
824#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500825 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300826 (target_long) qdict_get_int(cpu, "pc"));
827 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
828 (target_long) qdict_get_int(cpu, "npc"));
829#elif defined(TARGET_MIPS)
830 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
831 (target_long) qdict_get_int(cpu, "PC"));
832#endif
833
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200834 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300835 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200836 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300837
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100838 monitor_printf(mon, " thread_id=%" PRId64 " ",
839 qdict_get_int(cpu, "thread_id"));
840
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300841 monitor_printf(mon, "\n");
842}
843
844static void monitor_print_cpus(Monitor *mon, const QObject *data)
845{
846 QList *cpu_list;
847
848 assert(qobject_type(data) == QTYPE_QLIST);
849 cpu_list = qobject_to_qlist(data);
850 qlist_iter(cpu_list, print_cpu_iter, mon);
851}
852
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300853static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000854{
855 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300856 QList *cpu_list;
857
858 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000859
860 /* just to set the default cpu if not already done */
861 mon_get_cpu();
862
863 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200864 QDict *cpu;
865 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300866
Avi Kivity4c0960c2009-08-17 23:19:53 +0300867 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300868
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200869 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
870 env->cpu_index, env == mon->mon_cpu,
871 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200872
873 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300874
bellard6a00d602005-11-21 23:25:50 +0000875#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300876 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000877#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000879#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880 qdict_put(cpu, "pc", qint_from_int(env->pc));
881 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000882#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300883 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000884#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100885 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300886
887 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000888 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300889
890 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000891}
892
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200893static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000894{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300895 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200896 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100897 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
898 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200899 return -1;
900 }
901 return 0;
bellard6a00d602005-11-21 23:25:50 +0000902}
903
aliguori376253e2009-03-05 23:01:23 +0000904static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000905{
aliguori376253e2009-03-05 23:01:23 +0000906 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000907}
908
aliguori376253e2009-03-05 23:01:23 +0000909static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000910{
911 int i;
bellard7e2515e2004-08-01 21:52:19 +0000912 const char *str;
ths3b46e622007-09-17 08:09:54 +0000913
aliguoricde76ee2009-03-05 23:01:51 +0000914 if (!mon->rs)
915 return;
bellard7e2515e2004-08-01 21:52:19 +0000916 i = 0;
917 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000918 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000919 if (!str)
920 break;
aliguori376253e2009-03-05 23:01:23 +0000921 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000922 i++;
bellardaa455482004-04-04 13:07:25 +0000923 }
924}
925
j_mayer76a66252007-03-07 08:32:30 +0000926#if defined(TARGET_PPC)
927/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000928static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000929{
930 CPUState *env;
931
932 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000933 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000934}
935#endif
936
Lluís6d8a7642011-08-31 20:30:43 +0200937#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530938static void do_info_trace(Monitor *mon)
939{
940 st_print_trace((FILE *)mon, &monitor_fprintf);
941}
Lluís31965ae2011-08-31 20:31:24 +0200942#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530943
Lluísfc764102011-08-31 20:31:18 +0200944static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530945{
Lluísfc764102011-08-31 20:31:18 +0200946 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530947}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530948
Luiz Capitulinob223f352009-10-07 13:41:56 -0300949/**
950 * do_quit(): Quit QEMU execution
951 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200952static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000953{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300954 monitor_suspend(mon);
955 no_shutdown = 0;
956 qemu_system_shutdown_request();
957
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200958 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000959}
960
Jes Sorensen821601e2011-03-16 13:33:36 +0100961#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200962static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000963{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600964 if (!password || !password[0]) {
965 if (vnc_display_disable_login(NULL)) {
966 qerror_report(QERR_SET_PASSWD_FAILED);
967 return -1;
968 }
969 return 0;
970 }
971
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200972 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100973 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200974 return -1;
975 }
aliguoribb5fc202009-03-05 23:01:15 +0000976
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200977 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100978}
979
980static void change_vnc_password_cb(Monitor *mon, const char *password,
981 void *opaque)
982{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100983 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000984 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000985}
986
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200987static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000988{
ths70848512007-08-25 01:37:05 +0000989 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000990 strcmp(target, "password") == 0) {
991 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000992 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000993 strncpy(password, arg, sizeof(password));
994 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200995 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000996 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200997 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000998 }
ths70848512007-08-25 01:37:05 +0000999 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001000 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001001 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001002 return -1;
1003 }
ths70848512007-08-25 01:37:05 +00001004 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001005
1006 return 0;
thse25a5822007-08-25 01:36:20 +00001007}
Jes Sorensen821601e2011-03-16 13:33:36 +01001008#else
1009static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1010{
1011 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1012 return -ENODEV;
1013}
1014#endif
thse25a5822007-08-25 01:36:20 +00001015
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001016/**
1017 * do_change(): Change a removable medium, or VNC configuration
1018 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001019static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001020{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001021 const char *device = qdict_get_str(qdict, "device");
1022 const char *target = qdict_get_str(qdict, "target");
1023 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001024 int ret;
1025
thse25a5822007-08-25 01:36:20 +00001026 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001028 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001029 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001030 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001031
1032 return ret;
thse25a5822007-08-25 01:36:20 +00001033}
1034
Gerd Hoffmann75721502010-10-07 12:22:54 +02001035static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1036{
1037 const char *protocol = qdict_get_str(qdict, "protocol");
1038 const char *password = qdict_get_str(qdict, "password");
1039 const char *connected = qdict_get_try_str(qdict, "connected");
1040 int disconnect_if_connected = 0;
1041 int fail_if_connected = 0;
1042 int rc;
1043
1044 if (connected) {
1045 if (strcmp(connected, "fail") == 0) {
1046 fail_if_connected = 1;
1047 } else if (strcmp(connected, "disconnect") == 0) {
1048 disconnect_if_connected = 1;
1049 } else if (strcmp(connected, "keep") == 0) {
1050 /* nothing */
1051 } else {
1052 qerror_report(QERR_INVALID_PARAMETER, "connected");
1053 return -1;
1054 }
1055 }
1056
1057 if (strcmp(protocol, "spice") == 0) {
1058 if (!using_spice) {
1059 /* correct one? spice isn't a device ,,, */
1060 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1061 return -1;
1062 }
1063 rc = qemu_spice_set_passwd(password, fail_if_connected,
1064 disconnect_if_connected);
1065 if (rc != 0) {
1066 qerror_report(QERR_SET_PASSWD_FAILED);
1067 return -1;
1068 }
1069 return 0;
1070 }
1071
1072 if (strcmp(protocol, "vnc") == 0) {
1073 if (fail_if_connected || disconnect_if_connected) {
1074 /* vnc supports "connected=keep" only */
1075 qerror_report(QERR_INVALID_PARAMETER, "connected");
1076 return -1;
1077 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001078 /* Note that setting an empty password will not disable login through
1079 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001080 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001081 }
1082
1083 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1084 return -1;
1085}
1086
1087static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1088{
1089 const char *protocol = qdict_get_str(qdict, "protocol");
1090 const char *whenstr = qdict_get_str(qdict, "time");
1091 time_t when;
1092 int rc;
1093
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001094 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001095 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001096 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001097 when = TIME_MAX;
1098 } else if (whenstr[0] == '+') {
1099 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1100 } else {
1101 when = strtoull(whenstr, NULL, 10);
1102 }
1103
1104 if (strcmp(protocol, "spice") == 0) {
1105 if (!using_spice) {
1106 /* correct one? spice isn't a device ,,, */
1107 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1108 return -1;
1109 }
1110 rc = qemu_spice_set_pw_expire(when);
1111 if (rc != 0) {
1112 qerror_report(QERR_SET_PASSWD_FAILED);
1113 return -1;
1114 }
1115 return 0;
1116 }
1117
1118 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001119 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001120 }
1121
1122 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1123 return -1;
1124}
1125
Daniel P. Berrange13661082011-06-23 13:31:42 +01001126static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1127{
1128 const char *protocol = qdict_get_str(qdict, "protocol");
1129 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001130 CharDriverState *s;
1131
1132 if (strcmp(protocol, "spice") == 0) {
1133 if (!using_spice) {
1134 /* correct one? spice isn't a device ,,, */
1135 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1136 return -1;
1137 }
1138 qerror_report(QERR_ADD_CLIENT_FAILED);
1139 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001140#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001141 } else if (strcmp(protocol, "vnc") == 0) {
1142 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001143 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001144 vnc_display_add_client(NULL, fd, skipauth);
1145 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001146#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001147 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1148 int fd = monitor_get_fd(mon, fdname);
1149 if (qemu_chr_add_client(s, fd) < 0) {
1150 qerror_report(QERR_ADD_CLIENT_FAILED);
1151 return -1;
1152 }
1153 return 0;
1154 }
1155
1156 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1157 return -1;
1158}
1159
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001160static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1161{
1162 const char *protocol = qdict_get_str(qdict, "protocol");
1163 const char *hostname = qdict_get_str(qdict, "hostname");
1164 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1165 int port = qdict_get_try_int(qdict, "port", -1);
1166 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1167 int ret;
1168
1169 if (strcmp(protocol, "spice") == 0) {
1170 if (!using_spice) {
1171 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1172 return -1;
1173 }
1174
1175 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1176 if (ret != 0) {
1177 qerror_report(QERR_UNDEFINED_ERROR);
1178 return -1;
1179 }
1180 return 0;
1181 }
1182
1183 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1184 return -1;
1185}
1186
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001187static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001188{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001189 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001190 return 0;
bellard59a983b2004-03-17 23:17:16 +00001191}
1192
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001193static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001194{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001195 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001196}
1197
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001198static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001199{
1200 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001202
bellard9307c4c2004-04-04 12:57:25 +00001203 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001204 mask = 0;
1205 } else {
bellard9307c4c2004-04-04 12:57:25 +00001206 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001207 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001208 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001209 return;
1210 }
1211 }
1212 cpu_set_log(mask);
1213}
1214
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001215static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001216{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001217 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001218 if (!option || !strcmp(option, "on")) {
1219 singlestep = 1;
1220 } else if (!strcmp(option, "off")) {
1221 singlestep = 0;
1222 } else {
1223 monitor_printf(mon, "unexpected option %s\n", option);
1224 }
1225}
1226
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001227/**
1228 * do_stop(): Stop VM execution
1229 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001230static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001231{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001232 vm_stop(RUN_STATE_PAUSED);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001233 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001234}
1235
aliguoribb5fc202009-03-05 23:01:15 +00001236static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001237
aliguori376253e2009-03-05 23:01:23 +00001238struct bdrv_iterate_context {
1239 Monitor *mon;
1240 int err;
1241};
aliguoric0f4ce72009-03-05 23:01:01 +00001242
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001243/**
1244 * do_cont(): Resume emulation.
1245 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001246static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001247{
1248 struct bdrv_iterate_context context = { mon, 0 };
1249
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001250 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301251 qerror_report(QERR_MIGRATION_EXPECTED);
1252 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001253 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1254 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001255 qerror_report(QERR_RESET_REQUIRED);
1256 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301257 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001258
aliguori376253e2009-03-05 23:01:23 +00001259 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001260 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001261 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001262 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001263 return 0;
1264 } else {
1265 return -1;
1266 }
bellard8a7ddc32004-03-31 19:00:16 +00001267}
1268
aliguoribb5fc202009-03-05 23:01:15 +00001269static void bdrv_key_cb(void *opaque, int err)
1270{
aliguori376253e2009-03-05 23:01:23 +00001271 Monitor *mon = opaque;
1272
aliguoribb5fc202009-03-05 23:01:15 +00001273 /* another key was set successfully, retry to continue */
1274 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001275 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001276}
1277
1278static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1279{
aliguori376253e2009-03-05 23:01:23 +00001280 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001281
aliguori376253e2009-03-05 23:01:23 +00001282 if (!context->err && bdrv_key_required(bs)) {
1283 context->err = -EBUSY;
1284 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1285 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001286 }
1287}
1288
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001289static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001290{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001291 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001292 if (!device)
1293 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1294 if (gdbserver_start(device) < 0) {
1295 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1296 device);
1297 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001298 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001299 } else {
aliguori59030a82009-04-05 18:43:41 +00001300 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1301 device);
bellard8a7ddc32004-03-31 19:00:16 +00001302 }
1303}
1304
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001305static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001306{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001307 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001308 if (select_watchdog_action(action) == -1) {
1309 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1310 }
1311}
1312
aliguori376253e2009-03-05 23:01:23 +00001313static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001314{
aliguori376253e2009-03-05 23:01:23 +00001315 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001316 switch(c) {
1317 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001318 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001319 break;
1320 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001321 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001322 break;
1323 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001325 break;
1326 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001327 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001328 break;
1329 default:
1330 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001331 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001332 } else {
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001334 }
1335 break;
1336 }
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001338}
1339
aliguori376253e2009-03-05 23:01:23 +00001340static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001341 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001342{
bellard6a00d602005-11-21 23:25:50 +00001343 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001344 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001345 uint8_t buf[16];
1346 uint64_t v;
1347
1348 if (format == 'i') {
1349 int flags;
1350 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001351 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001352#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001353 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001354 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001355 } else if (wsize == 4) {
1356 flags = 0;
1357 } else {
bellard6a15fd12006-04-12 21:07:07 +00001358 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001359 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001360 if (env) {
1361#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001362 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001363 (env->segs[R_CS].flags & DESC_L_MASK))
1364 flags = 2;
1365 else
1366#endif
1367 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1368 flags = 1;
1369 }
bellard4c27ba22004-04-25 18:05:08 +00001370 }
1371#endif
aliguori376253e2009-03-05 23:01:23 +00001372 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001373 return;
1374 }
1375
1376 len = wsize * count;
1377 if (wsize == 1)
1378 line_size = 8;
1379 else
1380 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001381 max_digits = 0;
1382
1383 switch(format) {
1384 case 'o':
1385 max_digits = (wsize * 8 + 2) / 3;
1386 break;
1387 default:
1388 case 'x':
1389 max_digits = (wsize * 8) / 4;
1390 break;
1391 case 'u':
1392 case 'd':
1393 max_digits = (wsize * 8 * 10 + 32) / 33;
1394 break;
1395 case 'c':
1396 wsize = 1;
1397 break;
1398 }
1399
1400 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001401 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001403 else
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001405 l = len;
1406 if (l > line_size)
1407 l = line_size;
1408 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001409 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001410 } else {
bellard6a00d602005-11-21 23:25:50 +00001411 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001412 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001413 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001414 break;
1415 }
bellard9307c4c2004-04-04 12:57:25 +00001416 }
ths5fafdf22007-09-16 21:08:06 +00001417 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001418 while (i < l) {
1419 switch(wsize) {
1420 default:
1421 case 1:
1422 v = ldub_raw(buf + i);
1423 break;
1424 case 2:
1425 v = lduw_raw(buf + i);
1426 break;
1427 case 4:
bellard92a31b12005-02-10 22:00:52 +00001428 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001429 break;
1430 case 8:
1431 v = ldq_raw(buf + i);
1432 break;
1433 }
aliguori376253e2009-03-05 23:01:23 +00001434 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001435 switch(format) {
1436 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001437 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001438 break;
1439 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001440 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001441 break;
1442 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001444 break;
1445 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001446 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001447 break;
1448 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001449 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001450 break;
1451 }
1452 i += wsize;
1453 }
aliguori376253e2009-03-05 23:01:23 +00001454 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001455 addr += l;
1456 len -= l;
1457 }
1458}
1459
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001460static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001461{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001462 int count = qdict_get_int(qdict, "count");
1463 int format = qdict_get_int(qdict, "format");
1464 int size = qdict_get_int(qdict, "size");
1465 target_long addr = qdict_get_int(qdict, "addr");
1466
aliguori376253e2009-03-05 23:01:23 +00001467 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001468}
1469
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001470static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001471{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001472 int count = qdict_get_int(qdict, "count");
1473 int format = qdict_get_int(qdict, "format");
1474 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001475 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001476
aliguori376253e2009-03-05 23:01:23 +00001477 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001478}
1479
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001480static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001481{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001482 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001483 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001484
blueswir17743e582007-09-24 18:39:04 +00001485#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001486 switch(format) {
1487 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001488 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001489 break;
1490 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001491 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001492 break;
1493 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001494 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001495 break;
1496 default:
1497 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001498 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001499 break;
1500 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001502 break;
1503 }
bellard92a31b12005-02-10 22:00:52 +00001504#else
1505 switch(format) {
1506 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001507 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001508 break;
1509 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001511 break;
1512 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001513 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001514 break;
1515 default:
1516 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001517 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001518 break;
1519 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001520 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001521 break;
1522 }
1523#endif
aliguori376253e2009-03-05 23:01:23 +00001524 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001525}
1526
Luiz Capitulino98696222010-02-10 23:49:58 -02001527static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001528{
1529 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001530 uint32_t size = qdict_get_int(qdict, "size");
1531 const char *filename = qdict_get_str(qdict, "filename");
1532 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001533 uint32_t l;
1534 CPUState *env;
1535 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001536 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001537
1538 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001539
1540 f = fopen(filename, "wb");
1541 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001542 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001543 return -1;
bellardb371dc52007-01-03 15:20:39 +00001544 }
1545 while (size != 0) {
1546 l = sizeof(buf);
1547 if (l > size)
1548 l = size;
1549 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001550 if (fwrite(buf, 1, l, f) != l) {
1551 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1552 goto exit;
1553 }
bellardb371dc52007-01-03 15:20:39 +00001554 addr += l;
1555 size -= l;
1556 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001557
1558 ret = 0;
1559
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001560exit:
bellardb371dc52007-01-03 15:20:39 +00001561 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001562 return ret;
bellardb371dc52007-01-03 15:20:39 +00001563}
1564
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001565static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001566 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001567{
1568 FILE *f;
1569 uint32_t l;
1570 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001571 uint32_t size = qdict_get_int(qdict, "size");
1572 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001573 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001574 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001575
1576 f = fopen(filename, "wb");
1577 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001578 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001579 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001580 }
1581 while (size != 0) {
1582 l = sizeof(buf);
1583 if (l > size)
1584 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001585 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001586 if (fwrite(buf, 1, l, f) != l) {
1587 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1588 goto exit;
1589 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001590 fflush(f);
1591 addr += l;
1592 size -= l;
1593 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001594
1595 ret = 0;
1596
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001597exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001598 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001599 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001600}
1601
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001602static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001603{
1604 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001605 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001606 uint32_t start = qdict_get_int(qdict, "start");
1607 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001608
1609 sum = 0;
1610 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001611 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001612 /* BSD sum algorithm ('sum' Unix command) */
1613 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001614 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001615 }
aliguori376253e2009-03-05 23:01:23 +00001616 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001617}
1618
bellarda3a91a32004-06-04 11:06:21 +00001619typedef struct {
1620 int keycode;
1621 const char *name;
1622} KeyDef;
1623
1624static const KeyDef key_defs[] = {
1625 { 0x2a, "shift" },
1626 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001627
bellarda3a91a32004-06-04 11:06:21 +00001628 { 0x38, "alt" },
1629 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001630 { 0x64, "altgr" },
1631 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001632 { 0x1d, "ctrl" },
1633 { 0x9d, "ctrl_r" },
1634
1635 { 0xdd, "menu" },
1636
1637 { 0x01, "esc" },
1638
1639 { 0x02, "1" },
1640 { 0x03, "2" },
1641 { 0x04, "3" },
1642 { 0x05, "4" },
1643 { 0x06, "5" },
1644 { 0x07, "6" },
1645 { 0x08, "7" },
1646 { 0x09, "8" },
1647 { 0x0a, "9" },
1648 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001649 { 0x0c, "minus" },
1650 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001651 { 0x0e, "backspace" },
1652
1653 { 0x0f, "tab" },
1654 { 0x10, "q" },
1655 { 0x11, "w" },
1656 { 0x12, "e" },
1657 { 0x13, "r" },
1658 { 0x14, "t" },
1659 { 0x15, "y" },
1660 { 0x16, "u" },
1661 { 0x17, "i" },
1662 { 0x18, "o" },
1663 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001664 { 0x1a, "bracket_left" },
1665 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001666 { 0x1c, "ret" },
1667
1668 { 0x1e, "a" },
1669 { 0x1f, "s" },
1670 { 0x20, "d" },
1671 { 0x21, "f" },
1672 { 0x22, "g" },
1673 { 0x23, "h" },
1674 { 0x24, "j" },
1675 { 0x25, "k" },
1676 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001677 { 0x27, "semicolon" },
1678 { 0x28, "apostrophe" },
1679 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001680
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001681 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001682 { 0x2c, "z" },
1683 { 0x2d, "x" },
1684 { 0x2e, "c" },
1685 { 0x2f, "v" },
1686 { 0x30, "b" },
1687 { 0x31, "n" },
1688 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001689 { 0x33, "comma" },
1690 { 0x34, "dot" },
1691 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001692
balrog4d3b6f62008-02-10 16:33:14 +00001693 { 0x37, "asterisk" },
1694
bellarda3a91a32004-06-04 11:06:21 +00001695 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001696 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001697 { 0x3b, "f1" },
1698 { 0x3c, "f2" },
1699 { 0x3d, "f3" },
1700 { 0x3e, "f4" },
1701 { 0x3f, "f5" },
1702 { 0x40, "f6" },
1703 { 0x41, "f7" },
1704 { 0x42, "f8" },
1705 { 0x43, "f9" },
1706 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001707 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001708 { 0x46, "scroll_lock" },
1709
bellard64866c32006-05-07 18:03:31 +00001710 { 0xb5, "kp_divide" },
1711 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001712 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001713 { 0x4e, "kp_add" },
1714 { 0x9c, "kp_enter" },
1715 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001716 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001717
1718 { 0x52, "kp_0" },
1719 { 0x4f, "kp_1" },
1720 { 0x50, "kp_2" },
1721 { 0x51, "kp_3" },
1722 { 0x4b, "kp_4" },
1723 { 0x4c, "kp_5" },
1724 { 0x4d, "kp_6" },
1725 { 0x47, "kp_7" },
1726 { 0x48, "kp_8" },
1727 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001728
bellarda3a91a32004-06-04 11:06:21 +00001729 { 0x56, "<" },
1730
1731 { 0x57, "f11" },
1732 { 0x58, "f12" },
1733
1734 { 0xb7, "print" },
1735
1736 { 0xc7, "home" },
1737 { 0xc9, "pgup" },
1738 { 0xd1, "pgdn" },
1739 { 0xcf, "end" },
1740
1741 { 0xcb, "left" },
1742 { 0xc8, "up" },
1743 { 0xd0, "down" },
1744 { 0xcd, "right" },
1745
1746 { 0xd2, "insert" },
1747 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001748#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1749 { 0xf0, "stop" },
1750 { 0xf1, "again" },
1751 { 0xf2, "props" },
1752 { 0xf3, "undo" },
1753 { 0xf4, "front" },
1754 { 0xf5, "copy" },
1755 { 0xf6, "open" },
1756 { 0xf7, "paste" },
1757 { 0xf8, "find" },
1758 { 0xf9, "cut" },
1759 { 0xfa, "lf" },
1760 { 0xfb, "help" },
1761 { 0xfc, "meta_l" },
1762 { 0xfd, "meta_r" },
1763 { 0xfe, "compose" },
1764#endif
bellarda3a91a32004-06-04 11:06:21 +00001765 { 0, NULL },
1766};
1767
1768static int get_keycode(const char *key)
1769{
1770 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001771 char *endp;
1772 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001773
1774 for(p = key_defs; p->name != NULL; p++) {
1775 if (!strcmp(key, p->name))
1776 return p->keycode;
1777 }
bellard64866c32006-05-07 18:03:31 +00001778 if (strstart(key, "0x", NULL)) {
1779 ret = strtoul(key, &endp, 0);
1780 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1781 return ret;
1782 }
bellarda3a91a32004-06-04 11:06:21 +00001783 return -1;
1784}
1785
balrogc8256f92008-06-08 22:45:01 +00001786#define MAX_KEYCODES 16
1787static uint8_t keycodes[MAX_KEYCODES];
1788static int nb_pending_keycodes;
1789static QEMUTimer *key_timer;
1790
1791static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001792{
balrogc8256f92008-06-08 22:45:01 +00001793 int keycode;
1794
1795 while (nb_pending_keycodes > 0) {
1796 nb_pending_keycodes--;
1797 keycode = keycodes[nb_pending_keycodes];
1798 if (keycode & 0x80)
1799 kbd_put_keycode(0xe0);
1800 kbd_put_keycode(keycode | 0x80);
1801 }
1802}
1803
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001804static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001805{
balrog3401c0d2008-06-04 10:05:59 +00001806 char keyname_buf[16];
1807 char *separator;
1808 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001809 const char *string = qdict_get_str(qdict, "string");
1810 int has_hold_time = qdict_haskey(qdict, "hold_time");
1811 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001812
balrogc8256f92008-06-08 22:45:01 +00001813 if (nb_pending_keycodes > 0) {
1814 qemu_del_timer(key_timer);
1815 release_keys(NULL);
1816 }
1817 if (!has_hold_time)
1818 hold_time = 100;
1819 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001820 while (1) {
1821 separator = strchr(string, '-');
1822 keyname_len = separator ? separator - string : strlen(string);
1823 if (keyname_len > 0) {
1824 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1825 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001826 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001827 return;
bellarda3a91a32004-06-04 11:06:21 +00001828 }
balrogc8256f92008-06-08 22:45:01 +00001829 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001830 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001831 return;
1832 }
1833 keyname_buf[keyname_len] = 0;
1834 keycode = get_keycode(keyname_buf);
1835 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001836 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001837 return;
1838 }
balrogc8256f92008-06-08 22:45:01 +00001839 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001840 }
balrog3401c0d2008-06-04 10:05:59 +00001841 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001842 break;
balrog3401c0d2008-06-04 10:05:59 +00001843 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001844 }
balrogc8256f92008-06-08 22:45:01 +00001845 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001846 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001847 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001848 keycode = keycodes[i];
1849 if (keycode & 0x80)
1850 kbd_put_keycode(0xe0);
1851 kbd_put_keycode(keycode & 0x7f);
1852 }
balrogc8256f92008-06-08 22:45:01 +00001853 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001854 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001855 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001856}
1857
bellard13224a82006-07-14 22:03:35 +00001858static int mouse_button_state;
1859
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001860static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001861{
1862 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001863 const char *dx_str = qdict_get_str(qdict, "dx_str");
1864 const char *dy_str = qdict_get_str(qdict, "dy_str");
1865 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001866 dx = strtol(dx_str, NULL, 0);
1867 dy = strtol(dy_str, NULL, 0);
1868 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001869 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001870 dz = strtol(dz_str, NULL, 0);
1871 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1872}
1873
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001874static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001875{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001876 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001877 mouse_button_state = button_state;
1878 kbd_mouse_event(0, 0, 0, mouse_button_state);
1879}
1880
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001881static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001882{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001883 int size = qdict_get_int(qdict, "size");
1884 int addr = qdict_get_int(qdict, "addr");
1885 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001886 uint32_t val;
1887 int suffix;
1888
1889 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001890 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001891 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001892 addr++;
1893 }
1894 addr &= 0xffff;
1895
1896 switch(size) {
1897 default:
1898 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001899 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001900 suffix = 'b';
1901 break;
1902 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001903 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001904 suffix = 'w';
1905 break;
1906 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001907 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001908 suffix = 'l';
1909 break;
1910 }
aliguori376253e2009-03-05 23:01:23 +00001911 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1912 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001913}
bellarda3a91a32004-06-04 11:06:21 +00001914
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001915static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001916{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001917 int size = qdict_get_int(qdict, "size");
1918 int addr = qdict_get_int(qdict, "addr");
1919 int val = qdict_get_int(qdict, "val");
1920
Jan Kiszkaf1147842009-07-14 10:20:11 +02001921 addr &= IOPORTS_MASK;
1922
1923 switch (size) {
1924 default:
1925 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001926 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001927 break;
1928 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001929 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001930 break;
1931 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001932 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001933 break;
1934 }
1935}
1936
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001937static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001938{
1939 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001940 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001941
Jan Kiszka76e30d02009-07-02 00:19:02 +02001942 res = qemu_boot_set(bootdevice);
1943 if (res == 0) {
1944 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1945 } else if (res > 0) {
1946 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001947 } else {
aliguori376253e2009-03-05 23:01:23 +00001948 monitor_printf(mon, "no function defined to set boot device list for "
1949 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001950 }
1951}
1952
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001953/**
1954 * do_system_reset(): Issue a machine reset
1955 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001956static int do_system_reset(Monitor *mon, const QDict *qdict,
1957 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001958{
1959 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001960 return 0;
bellarde4f90822004-06-20 12:35:44 +00001961}
1962
Luiz Capitulino43076662009-10-07 13:41:59 -03001963/**
1964 * do_system_powerdown(): Issue a machine powerdown
1965 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001966static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1967 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001968{
1969 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001970 return 0;
bellard34751872005-07-02 14:31:34 +00001971}
1972
bellardb86bda52004-09-18 19:32:46 +00001973#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001974static void print_pte(Monitor *mon, target_phys_addr_t addr,
1975 target_phys_addr_t pte,
1976 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001977{
Blue Swirld65aaf32010-12-11 18:56:24 +00001978#ifdef TARGET_X86_64
1979 if (addr & (1ULL << 47)) {
1980 addr |= -1LL << 48;
1981 }
1982#endif
1983 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1984 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001985 addr,
1986 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001987 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001988 pte & PG_GLOBAL_MASK ? 'G' : '-',
1989 pte & PG_PSE_MASK ? 'P' : '-',
1990 pte & PG_DIRTY_MASK ? 'D' : '-',
1991 pte & PG_ACCESSED_MASK ? 'A' : '-',
1992 pte & PG_PCD_MASK ? 'C' : '-',
1993 pte & PG_PWT_MASK ? 'T' : '-',
1994 pte & PG_USER_MASK ? 'U' : '-',
1995 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001996}
1997
Blue Swirld65aaf32010-12-11 18:56:24 +00001998static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001999{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002000 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00002001 uint32_t pgd, pde, pte;
2002
bellardb86bda52004-09-18 19:32:46 +00002003 pgd = env->cr[3] & ~0xfff;
2004 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002005 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002006 pde = le32_to_cpu(pde);
2007 if (pde & PG_PRESENT_MASK) {
2008 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00002009 /* 4M pages */
2010 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00002011 } else {
2012 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002013 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002014 pte = le32_to_cpu(pte);
2015 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002016 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002017 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002018 ~0xfff);
2019 }
2020 }
2021 }
2022 }
2023 }
2024}
2025
Blue Swirld65aaf32010-12-11 18:56:24 +00002026static void tlb_info_pae32(Monitor *mon, CPUState *env)
2027{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002028 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002029 uint64_t pdpe, pde, pte;
2030 uint64_t pdp_addr, pd_addr, pt_addr;
2031
2032 pdp_addr = env->cr[3] & ~0x1f;
2033 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002034 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002035 pdpe = le64_to_cpu(pdpe);
2036 if (pdpe & PG_PRESENT_MASK) {
2037 pd_addr = pdpe & 0x3fffffffff000ULL;
2038 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002039 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002040 pde = le64_to_cpu(pde);
2041 if (pde & PG_PRESENT_MASK) {
2042 if (pde & PG_PSE_MASK) {
2043 /* 2M pages with PAE, CR4.PSE is ignored */
2044 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2045 ~((target_phys_addr_t)(1 << 20) - 1));
2046 } else {
2047 pt_addr = pde & 0x3fffffffff000ULL;
2048 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002049 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002050 pte = le64_to_cpu(pte);
2051 if (pte & PG_PRESENT_MASK) {
2052 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2053 + (l3 << 12),
2054 pte & ~PG_PSE_MASK,
2055 ~(target_phys_addr_t)0xfff);
2056 }
2057 }
2058 }
2059 }
2060 }
2061 }
2062 }
2063}
2064
2065#ifdef TARGET_X86_64
2066static void tlb_info_64(Monitor *mon, CPUState *env)
2067{
2068 uint64_t l1, l2, l3, l4;
2069 uint64_t pml4e, pdpe, pde, pte;
2070 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2071
2072 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2073 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002074 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002075 pml4e = le64_to_cpu(pml4e);
2076 if (pml4e & PG_PRESENT_MASK) {
2077 pdp_addr = pml4e & 0x3fffffffff000ULL;
2078 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002079 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002080 pdpe = le64_to_cpu(pdpe);
2081 if (pdpe & PG_PRESENT_MASK) {
2082 if (pdpe & PG_PSE_MASK) {
2083 /* 1G pages, CR4.PSE is ignored */
2084 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2085 0x3ffffc0000000ULL);
2086 } else {
2087 pd_addr = pdpe & 0x3fffffffff000ULL;
2088 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002089 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002090 pde = le64_to_cpu(pde);
2091 if (pde & PG_PRESENT_MASK) {
2092 if (pde & PG_PSE_MASK) {
2093 /* 2M pages, CR4.PSE is ignored */
2094 print_pte(mon, (l1 << 39) + (l2 << 30) +
2095 (l3 << 21), pde,
2096 0x3ffffffe00000ULL);
2097 } else {
2098 pt_addr = pde & 0x3fffffffff000ULL;
2099 for (l4 = 0; l4 < 512; l4++) {
2100 cpu_physical_memory_read(pt_addr
2101 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002102 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002103 pte = le64_to_cpu(pte);
2104 if (pte & PG_PRESENT_MASK) {
2105 print_pte(mon, (l1 << 39) +
2106 (l2 << 30) +
2107 (l3 << 21) + (l4 << 12),
2108 pte & ~PG_PSE_MASK,
2109 0x3fffffffff000ULL);
2110 }
2111 }
2112 }
2113 }
2114 }
2115 }
2116 }
2117 }
2118 }
2119 }
2120}
2121#endif
2122
2123static void tlb_info(Monitor *mon)
2124{
2125 CPUState *env;
2126
2127 env = mon_get_cpu();
2128
2129 if (!(env->cr[0] & CR0_PG_MASK)) {
2130 monitor_printf(mon, "PG disabled\n");
2131 return;
2132 }
2133 if (env->cr[4] & CR4_PAE_MASK) {
2134#ifdef TARGET_X86_64
2135 if (env->hflags & HF_LMA_MASK) {
2136 tlb_info_64(mon, env);
2137 } else
2138#endif
2139 {
2140 tlb_info_pae32(mon, env);
2141 }
2142 } else {
2143 tlb_info_32(mon, env);
2144 }
2145}
2146
Blue Swirl1b3cba62010-12-11 18:56:27 +00002147static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2148 int *plast_prot,
2149 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002150{
bellard9746b152004-11-11 18:30:24 +00002151 int prot1;
2152 prot1 = *plast_prot;
2153 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002154 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002155 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2156 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002157 *pstart, end, end - *pstart,
2158 prot1 & PG_USER_MASK ? 'u' : '-',
2159 'r',
2160 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002161 }
2162 if (prot != 0)
2163 *pstart = end;
2164 else
2165 *pstart = -1;
2166 *plast_prot = prot;
2167 }
2168}
2169
Blue Swirl1b3cba62010-12-11 18:56:27 +00002170static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002171{
Austin Clementsb49ca722011-08-14 23:19:21 -04002172 unsigned int l1, l2;
2173 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002174 uint32_t pgd, pde, pte;
2175 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002176
bellardb86bda52004-09-18 19:32:46 +00002177 pgd = env->cr[3] & ~0xfff;
2178 last_prot = 0;
2179 start = -1;
2180 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002181 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002182 pde = le32_to_cpu(pde);
2183 end = l1 << 22;
2184 if (pde & PG_PRESENT_MASK) {
2185 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2186 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002187 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002188 } else {
2189 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002190 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002191 pte = le32_to_cpu(pte);
2192 end = (l1 << 22) + (l2 << 12);
2193 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002194 prot = pte & pde &
2195 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002196 } else {
2197 prot = 0;
2198 }
aliguori376253e2009-03-05 23:01:23 +00002199 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002200 }
2201 }
2202 } else {
2203 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002204 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002205 }
2206 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002207 /* Flush last range */
2208 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002209}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002210
2211static void mem_info_pae32(Monitor *mon, CPUState *env)
2212{
Austin Clementsb49ca722011-08-14 23:19:21 -04002213 unsigned int l1, l2, l3;
2214 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002215 uint64_t pdpe, pde, pte;
2216 uint64_t pdp_addr, pd_addr, pt_addr;
2217 target_phys_addr_t start, end;
2218
2219 pdp_addr = env->cr[3] & ~0x1f;
2220 last_prot = 0;
2221 start = -1;
2222 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002223 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002224 pdpe = le64_to_cpu(pdpe);
2225 end = l1 << 30;
2226 if (pdpe & PG_PRESENT_MASK) {
2227 pd_addr = pdpe & 0x3fffffffff000ULL;
2228 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002229 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002230 pde = le64_to_cpu(pde);
2231 end = (l1 << 30) + (l2 << 21);
2232 if (pde & PG_PRESENT_MASK) {
2233 if (pde & PG_PSE_MASK) {
2234 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2235 PG_PRESENT_MASK);
2236 mem_print(mon, &start, &last_prot, end, prot);
2237 } else {
2238 pt_addr = pde & 0x3fffffffff000ULL;
2239 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002240 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002241 pte = le64_to_cpu(pte);
2242 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2243 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002244 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2245 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002246 } else {
2247 prot = 0;
2248 }
2249 mem_print(mon, &start, &last_prot, end, prot);
2250 }
2251 }
2252 } else {
2253 prot = 0;
2254 mem_print(mon, &start, &last_prot, end, prot);
2255 }
2256 }
2257 } else {
2258 prot = 0;
2259 mem_print(mon, &start, &last_prot, end, prot);
2260 }
2261 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002262 /* Flush last range */
2263 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002264}
2265
2266
2267#ifdef TARGET_X86_64
2268static void mem_info_64(Monitor *mon, CPUState *env)
2269{
2270 int prot, last_prot;
2271 uint64_t l1, l2, l3, l4;
2272 uint64_t pml4e, pdpe, pde, pte;
2273 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2274
2275 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2276 last_prot = 0;
2277 start = -1;
2278 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002279 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002280 pml4e = le64_to_cpu(pml4e);
2281 end = l1 << 39;
2282 if (pml4e & PG_PRESENT_MASK) {
2283 pdp_addr = pml4e & 0x3fffffffff000ULL;
2284 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002285 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002286 pdpe = le64_to_cpu(pdpe);
2287 end = (l1 << 39) + (l2 << 30);
2288 if (pdpe & PG_PRESENT_MASK) {
2289 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002290 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2291 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002292 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002293 mem_print(mon, &start, &last_prot, end, prot);
2294 } else {
2295 pd_addr = pdpe & 0x3fffffffff000ULL;
2296 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002297 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002298 pde = le64_to_cpu(pde);
2299 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2300 if (pde & PG_PRESENT_MASK) {
2301 if (pde & PG_PSE_MASK) {
2302 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2303 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002304 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002305 mem_print(mon, &start, &last_prot, end, prot);
2306 } else {
2307 pt_addr = pde & 0x3fffffffff000ULL;
2308 for (l4 = 0; l4 < 512; l4++) {
2309 cpu_physical_memory_read(pt_addr
2310 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002311 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002312 pte = le64_to_cpu(pte);
2313 end = (l1 << 39) + (l2 << 30) +
2314 (l3 << 21) + (l4 << 12);
2315 if (pte & PG_PRESENT_MASK) {
2316 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2317 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002318 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002319 } else {
2320 prot = 0;
2321 }
2322 mem_print(mon, &start, &last_prot, end, prot);
2323 }
2324 }
2325 } else {
2326 prot = 0;
2327 mem_print(mon, &start, &last_prot, end, prot);
2328 }
2329 }
2330 }
2331 } else {
2332 prot = 0;
2333 mem_print(mon, &start, &last_prot, end, prot);
2334 }
2335 }
2336 } else {
2337 prot = 0;
2338 mem_print(mon, &start, &last_prot, end, prot);
2339 }
2340 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002341 /* Flush last range */
2342 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002343}
2344#endif
2345
2346static void mem_info(Monitor *mon)
2347{
2348 CPUState *env;
2349
2350 env = mon_get_cpu();
2351
2352 if (!(env->cr[0] & CR0_PG_MASK)) {
2353 monitor_printf(mon, "PG disabled\n");
2354 return;
2355 }
2356 if (env->cr[4] & CR4_PAE_MASK) {
2357#ifdef TARGET_X86_64
2358 if (env->hflags & HF_LMA_MASK) {
2359 mem_info_64(mon, env);
2360 } else
2361#endif
2362 {
2363 mem_info_pae32(mon, env);
2364 }
2365 } else {
2366 mem_info_32(mon, env);
2367 }
2368}
bellardb86bda52004-09-18 19:32:46 +00002369#endif
2370
aurel327c664e22009-03-03 06:12:22 +00002371#if defined(TARGET_SH4)
2372
aliguori376253e2009-03-05 23:01:23 +00002373static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002374{
aliguori376253e2009-03-05 23:01:23 +00002375 monitor_printf(mon, " tlb%i:\t"
2376 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2377 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2378 "dirty=%hhu writethrough=%hhu\n",
2379 idx,
2380 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2381 tlb->v, tlb->sh, tlb->c, tlb->pr,
2382 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002383}
2384
aliguori376253e2009-03-05 23:01:23 +00002385static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002386{
2387 CPUState *env = mon_get_cpu();
2388 int i;
2389
aliguori376253e2009-03-05 23:01:23 +00002390 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002391 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002392 print_tlb (mon, i, &env->itlb[i]);
2393 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002394 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002395 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002396}
2397
2398#endif
2399
Blue Swirld41160a2010-12-19 13:42:56 +00002400#if defined(TARGET_SPARC)
2401static void tlb_info(Monitor *mon)
2402{
2403 CPUState *env1 = mon_get_cpu();
2404
2405 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2406}
2407#endif
2408
Blue Swirl314e2982011-09-11 20:22:05 +00002409static void do_info_mtree(Monitor *mon)
2410{
2411 mtree_info((fprintf_function)monitor_printf, mon);
2412}
2413
aliguori030ea372009-04-21 22:30:47 +00002414static void do_info_numa(Monitor *mon)
2415{
aliguorib28b6232009-04-22 20:20:29 +00002416 int i;
aliguori030ea372009-04-21 22:30:47 +00002417 CPUState *env;
2418
2419 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2420 for (i = 0; i < nb_numa_nodes; i++) {
2421 monitor_printf(mon, "node %d cpus:", i);
2422 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2423 if (env->numa_node == i) {
2424 monitor_printf(mon, " %d", env->cpu_index);
2425 }
2426 }
2427 monitor_printf(mon, "\n");
2428 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2429 node_mem[i] >> 20);
2430 }
2431}
2432
bellard5f1ce942006-02-08 22:40:15 +00002433#ifdef CONFIG_PROFILER
2434
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002435int64_t qemu_time;
2436int64_t dev_time;
2437
aliguori376253e2009-03-05 23:01:23 +00002438static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002439{
2440 int64_t total;
2441 total = qemu_time;
2442 if (total == 0)
2443 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002444 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002445 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002446 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002447 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002448 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002449 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002450}
2451#else
aliguori376253e2009-03-05 23:01:23 +00002452static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002453{
aliguori376253e2009-03-05 23:01:23 +00002454 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002455}
2456#endif
2457
bellardec36b692006-07-16 18:57:03 +00002458/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002459static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002460
aliguori376253e2009-03-05 23:01:23 +00002461static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002462{
2463 int i;
2464 CaptureState *s;
2465
2466 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002467 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002468 s->ops.info (s->opaque);
2469 }
2470}
2471
Blue Swirl23130862009-06-06 08:22:04 +00002472#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002473static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002474{
2475 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002476 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002477 CaptureState *s;
2478
2479 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2480 if (i == n) {
2481 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002482 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002483 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002484 return;
2485 }
2486 }
2487}
2488
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002489static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002490{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002491 const char *path = qdict_get_str(qdict, "path");
2492 int has_freq = qdict_haskey(qdict, "freq");
2493 int freq = qdict_get_try_int(qdict, "freq", -1);
2494 int has_bits = qdict_haskey(qdict, "bits");
2495 int bits = qdict_get_try_int(qdict, "bits", -1);
2496 int has_channels = qdict_haskey(qdict, "nchannels");
2497 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002498 CaptureState *s;
2499
Anthony Liguori7267c092011-08-20 22:09:37 -05002500 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002501
2502 freq = has_freq ? freq : 44100;
2503 bits = has_bits ? bits : 16;
2504 nchannels = has_channels ? nchannels : 2;
2505
2506 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002507 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002508 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002509 return;
bellardec36b692006-07-16 18:57:03 +00002510 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002511 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002512}
2513#endif
2514
aurel32dc1c0b72008-04-27 23:52:12 +00002515#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002516static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002517{
2518 CPUState *env;
2519
2520 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2521 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2522 }
2523
2524 return 0;
2525}
2526#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002527static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002528{
2529 qerror_report(QERR_UNSUPPORTED);
2530 return -1;
2531}
aurel32dc1c0b72008-04-27 23:52:12 +00002532#endif
2533
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002534static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002535{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002536 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002537
aliguori76655d62009-03-06 20:27:37 +00002538 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002539 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002540 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002541 return acl;
2542}
aliguori76655d62009-03-06 20:27:37 +00002543
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002544static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002545{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002546 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002547 qemu_acl *acl = find_acl(mon, aclname);
2548 qemu_acl_entry *entry;
2549 int i = 0;
2550
2551 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002552 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002553 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002554 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002555 i++;
2556 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002557 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002558 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002559 }
2560}
2561
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002562static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002563{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002564 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002565 qemu_acl *acl = find_acl(mon, aclname);
2566
2567 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002568 qemu_acl_reset(acl);
2569 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002570 }
2571}
aliguori76655d62009-03-06 20:27:37 +00002572
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002573static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002574{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002575 const char *aclname = qdict_get_str(qdict, "aclname");
2576 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002577 qemu_acl *acl = find_acl(mon, aclname);
2578
2579 if (acl) {
2580 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002581 acl->defaultDeny = 0;
2582 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002583 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002584 acl->defaultDeny = 1;
2585 monitor_printf(mon, "acl: policy set to 'deny'\n");
2586 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002587 monitor_printf(mon, "acl: unknown policy '%s', "
2588 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002589 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002590 }
2591}
aliguori76655d62009-03-06 20:27:37 +00002592
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002593static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002594{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002595 const char *aclname = qdict_get_str(qdict, "aclname");
2596 const char *match = qdict_get_str(qdict, "match");
2597 const char *policy = qdict_get_str(qdict, "policy");
2598 int has_index = qdict_haskey(qdict, "index");
2599 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002600 qemu_acl *acl = find_acl(mon, aclname);
2601 int deny, ret;
2602
2603 if (acl) {
2604 if (strcmp(policy, "allow") == 0) {
2605 deny = 0;
2606 } else if (strcmp(policy, "deny") == 0) {
2607 deny = 1;
2608 } else {
2609 monitor_printf(mon, "acl: unknown policy '%s', "
2610 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002611 return;
2612 }
aliguori28a76be2009-03-06 20:27:40 +00002613 if (has_index)
2614 ret = qemu_acl_insert(acl, deny, match, index);
2615 else
2616 ret = qemu_acl_append(acl, deny, match);
2617 if (ret < 0)
2618 monitor_printf(mon, "acl: unable to add acl entry\n");
2619 else
2620 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002621 }
2622}
aliguori76655d62009-03-06 20:27:37 +00002623
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002624static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002625{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002626 const char *aclname = qdict_get_str(qdict, "aclname");
2627 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002628 qemu_acl *acl = find_acl(mon, aclname);
2629 int ret;
aliguori76655d62009-03-06 20:27:37 +00002630
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002631 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002632 ret = qemu_acl_remove(acl, match);
2633 if (ret < 0)
2634 monitor_printf(mon, "acl: no matching acl entry\n");
2635 else
2636 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002637 }
2638}
2639
Huang Ying79c4f6b2009-06-23 10:05:14 +08002640#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002641static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002642{
2643 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002644 int cpu_index = qdict_get_int(qdict, "cpu_index");
2645 int bank = qdict_get_int(qdict, "bank");
2646 uint64_t status = qdict_get_int(qdict, "status");
2647 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2648 uint64_t addr = qdict_get_int(qdict, "addr");
2649 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002650 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002651
Jan Kiszka747461c2011-03-02 08:56:10 +01002652 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2653 flags |= MCE_INJECT_BROADCAST;
2654 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002655 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002656 if (cenv->cpu_index == cpu_index) {
2657 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002658 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002659 break;
2660 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002661 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002662}
2663#endif
2664
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002665static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002666{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002667 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002668 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002669 int fd;
2670
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002671 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002672 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002673 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002674 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002675 }
2676
2677 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002678 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2679 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002680 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002681 }
2682
Blue Swirl72cf2d42009-09-12 07:36:22 +00002683 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002684 if (strcmp(monfd->name, fdname) != 0) {
2685 continue;
2686 }
2687
2688 close(monfd->fd);
2689 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002690 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002691 }
2692
Anthony Liguori7267c092011-08-20 22:09:37 -05002693 monfd = g_malloc0(sizeof(mon_fd_t));
2694 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002695 monfd->fd = fd;
2696
Blue Swirl72cf2d42009-09-12 07:36:22 +00002697 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002698 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002699}
2700
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002701static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002702{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002703 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002704 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002705
Blue Swirl72cf2d42009-09-12 07:36:22 +00002706 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002707 if (strcmp(monfd->name, fdname) != 0) {
2708 continue;
2709 }
2710
Blue Swirl72cf2d42009-09-12 07:36:22 +00002711 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002712 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002713 g_free(monfd->name);
2714 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002715 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002716 }
2717
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002718 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002719 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002720}
2721
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002722static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002723{
Luiz Capitulino13548692011-07-29 15:36:43 -03002724 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002725 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002726
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002727 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002728
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002729 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002730 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002731 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002732}
2733
Mark McLoughlin7768e042009-07-22 09:11:41 +01002734int monitor_get_fd(Monitor *mon, const char *fdname)
2735{
Anthony Liguoric227f092009-10-01 16:12:16 -05002736 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002737
Blue Swirl72cf2d42009-09-12 07:36:22 +00002738 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002739 int fd;
2740
2741 if (strcmp(monfd->name, fdname) != 0) {
2742 continue;
2743 }
2744
2745 fd = monfd->fd;
2746
2747 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002748 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002749 g_free(monfd->name);
2750 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002751
2752 return fd;
2753 }
2754
2755 return -1;
2756}
2757
Anthony Liguoric227f092009-10-01 16:12:16 -05002758static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002759#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002760 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002761};
2762
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002763/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002764static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 {
2766 .name = "version",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002770 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
2772 {
2773 .name = "network",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002777 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
2779 {
2780 .name = "chardev",
2781 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002782 .params = "",
2783 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002784 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 },
2786 {
2787 .name = "block",
2788 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .params = "",
2790 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002791 .user_print = bdrv_info_print,
2792 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002793 },
2794 {
2795 .name = "blockstats",
2796 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 .params = "",
2798 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002799 .user_print = bdrv_stats_print,
2800 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 },
2802 {
2803 .name = "registers",
2804 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 .params = "",
2806 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002807 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 },
2809 {
2810 .name = "cpus",
2811 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002812 .params = "",
2813 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002814 .user_print = monitor_print_cpus,
2815 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 },
2817 {
2818 .name = "history",
2819 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .params = "",
2821 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002822 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
2824 {
2825 .name = "irq",
2826 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .params = "",
2828 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002829 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 },
2831 {
2832 .name = "pic",
2833 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 .params = "",
2835 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002836 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002837 },
2838 {
2839 .name = "pci",
2840 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 .params = "",
2842 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002843 .user_print = do_pci_info_print,
2844 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 },
Blue Swirld41160a2010-12-19 13:42:56 +00002846#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002847 {
2848 .name = "tlb",
2849 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 .params = "",
2851 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002852 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002853 },
aurel327c664e22009-03-03 06:12:22 +00002854#endif
2855#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002856 {
2857 .name = "mem",
2858 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002859 .params = "",
2860 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002861 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002862 },
bellardb86bda52004-09-18 19:32:46 +00002863#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 {
Blue Swirl314e2982011-09-11 20:22:05 +00002865 .name = "mtree",
2866 .args_type = "",
2867 .params = "",
2868 .help = "show memory tree",
2869 .mhandler.info = do_info_mtree,
2870 },
2871 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 .name = "jit",
2873 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002874 .params = "",
2875 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002876 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002877 },
2878 {
2879 .name = "kvm",
2880 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 .params = "",
2882 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002883 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002884 },
2885 {
2886 .name = "numa",
2887 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 .params = "",
2889 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002890 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 },
2892 {
2893 .name = "usb",
2894 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 .params = "",
2896 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002897 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002898 },
2899 {
2900 .name = "usbhost",
2901 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002902 .params = "",
2903 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002904 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 },
2906 {
2907 .name = "profile",
2908 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002909 .params = "",
2910 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002911 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 },
2913 {
2914 .name = "capture",
2915 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002916 .params = "",
2917 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002918 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 },
2920 {
2921 .name = "snapshots",
2922 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002923 .params = "",
2924 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002925 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 },
2927 {
2928 .name = "status",
2929 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002930 .params = "",
2931 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002932 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002933 },
2934 {
2935 .name = "pcmcia",
2936 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002937 .params = "",
2938 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002939 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002940 },
2941 {
2942 .name = "mice",
2943 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002944 .params = "",
2945 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002946 .user_print = do_info_mice_print,
2947 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002948 },
2949 {
2950 .name = "vnc",
2951 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002952 .params = "",
2953 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002954 .user_print = do_info_vnc_print,
2955 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002956 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002957#if defined(CONFIG_SPICE)
2958 {
2959 .name = "spice",
2960 .args_type = "",
2961 .params = "",
2962 .help = "show the spice server status",
2963 .user_print = do_info_spice_print,
2964 .mhandler.info_new = do_info_spice,
2965 },
2966#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002967 {
2968 .name = "name",
2969 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002970 .params = "",
2971 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002972 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002973 },
2974 {
2975 .name = "uuid",
2976 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002977 .params = "",
2978 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002979 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002980 },
j_mayer76a66252007-03-07 08:32:30 +00002981#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002982 {
2983 .name = "cpustats",
2984 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002985 .params = "",
2986 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002987 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002988 },
j_mayer76a66252007-03-07 08:32:30 +00002989#endif
blueswir131a60e22007-10-26 18:42:59 +00002990#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002991 {
2992 .name = "usernet",
2993 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002994 .params = "",
2995 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002996 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002997 },
blueswir131a60e22007-10-26 18:42:59 +00002998#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002999 {
3000 .name = "migrate",
3001 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003002 .params = "",
3003 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003004 .user_print = do_info_migrate_print,
3005 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003006 },
3007 {
3008 .name = "balloon",
3009 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003010 .params = "",
3011 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003012 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003013 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003014 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003015 },
3016 {
3017 .name = "qtree",
3018 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003019 .params = "",
3020 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003021 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003022 },
3023 {
3024 .name = "qdm",
3025 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003026 .params = "",
3027 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003028 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003029 },
3030 {
3031 .name = "roms",
3032 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003033 .params = "",
3034 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003035 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003036 },
Lluís6d8a7642011-08-31 20:30:43 +02003037#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303038 {
3039 .name = "trace",
3040 .args_type = "",
3041 .params = "",
3042 .help = "show current contents of trace buffer",
3043 .mhandler.info = do_info_trace,
3044 },
Lluís31965ae2011-08-31 20:31:24 +02003045#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303046 {
3047 .name = "trace-events",
3048 .args_type = "",
3049 .params = "",
3050 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003051 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303052 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003053 {
3054 .name = NULL,
3055 },
bellard9dc39cb2004-03-14 21:38:27 +00003056};
3057
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003058static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05003059#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003060 { /* NULL */ },
3061};
3062
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003063static const mon_cmd_t qmp_query_cmds[] = {
3064 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003065 .name = "block",
3066 .args_type = "",
3067 .params = "",
3068 .help = "show the block devices",
3069 .user_print = bdrv_info_print,
3070 .mhandler.info_new = bdrv_info,
3071 },
3072 {
3073 .name = "blockstats",
3074 .args_type = "",
3075 .params = "",
3076 .help = "show block device statistics",
3077 .user_print = bdrv_stats_print,
3078 .mhandler.info_new = bdrv_info_stats,
3079 },
3080 {
3081 .name = "cpus",
3082 .args_type = "",
3083 .params = "",
3084 .help = "show infos for each CPU",
3085 .user_print = monitor_print_cpus,
3086 .mhandler.info_new = do_info_cpus,
3087 },
3088 {
3089 .name = "pci",
3090 .args_type = "",
3091 .params = "",
3092 .help = "show PCI info",
3093 .user_print = do_pci_info_print,
3094 .mhandler.info_new = do_pci_info,
3095 },
3096 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003097 .name = "mice",
3098 .args_type = "",
3099 .params = "",
3100 .help = "show which guest mouse is receiving events",
3101 .user_print = do_info_mice_print,
3102 .mhandler.info_new = do_info_mice,
3103 },
3104 {
3105 .name = "vnc",
3106 .args_type = "",
3107 .params = "",
3108 .help = "show the vnc server status",
3109 .user_print = do_info_vnc_print,
3110 .mhandler.info_new = do_info_vnc,
3111 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003112#if defined(CONFIG_SPICE)
3113 {
3114 .name = "spice",
3115 .args_type = "",
3116 .params = "",
3117 .help = "show the spice server status",
3118 .user_print = do_info_spice_print,
3119 .mhandler.info_new = do_info_spice,
3120 },
3121#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003122 {
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003123 .name = "migrate",
3124 .args_type = "",
3125 .params = "",
3126 .help = "show migration status",
3127 .user_print = do_info_migrate_print,
3128 .mhandler.info_new = do_info_migrate,
3129 },
3130 {
3131 .name = "balloon",
3132 .args_type = "",
3133 .params = "",
3134 .help = "show balloon information",
3135 .user_print = monitor_print_balloon,
3136 .mhandler.info_async = do_info_balloon,
3137 .flags = MONITOR_CMD_ASYNC,
3138 },
3139 { /* NULL */ },
3140};
3141
bellard9307c4c2004-04-04 12:57:25 +00003142/*******************************************************************/
3143
3144static const char *pch;
3145static jmp_buf expr_env;
3146
bellard92a31b12005-02-10 22:00:52 +00003147#define MD_TLONG 0
3148#define MD_I32 1
3149
bellard9307c4c2004-04-04 12:57:25 +00003150typedef struct MonitorDef {
3151 const char *name;
3152 int offset;
blueswir18662d652008-10-02 18:32:44 +00003153 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003154 int type;
bellard9307c4c2004-04-04 12:57:25 +00003155} MonitorDef;
3156
bellard57206fd2004-04-25 18:54:52 +00003157#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003158static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003159{
bellard6a00d602005-11-21 23:25:50 +00003160 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003161 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003162}
3163#endif
3164
bellarda541f292004-04-12 20:39:29 +00003165#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003166static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003167{
bellard6a00d602005-11-21 23:25:50 +00003168 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003169 unsigned int u;
3170 int i;
3171
3172 u = 0;
3173 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003174 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003175
3176 return u;
3177}
3178
blueswir18662d652008-10-02 18:32:44 +00003179static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003180{
bellard6a00d602005-11-21 23:25:50 +00003181 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003182 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003183}
3184
blueswir18662d652008-10-02 18:32:44 +00003185static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003186{
bellard6a00d602005-11-21 23:25:50 +00003187 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003188 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003189}
bellard9fddaa02004-05-21 12:59:32 +00003190
blueswir18662d652008-10-02 18:32:44 +00003191static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003192{
bellard6a00d602005-11-21 23:25:50 +00003193 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003194 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003195}
3196
blueswir18662d652008-10-02 18:32:44 +00003197static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003198{
bellard6a00d602005-11-21 23:25:50 +00003199 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003200 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003201}
3202
blueswir18662d652008-10-02 18:32:44 +00003203static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003204{
bellard6a00d602005-11-21 23:25:50 +00003205 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003206 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003207}
bellarda541f292004-04-12 20:39:29 +00003208#endif
3209
bellarde95c8d52004-09-30 22:22:08 +00003210#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003211#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003212static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003213{
bellard6a00d602005-11-21 23:25:50 +00003214 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003215
3216 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003217}
bellard7b936c02005-10-30 17:05:13 +00003218#endif
bellarde95c8d52004-09-30 22:22:08 +00003219
blueswir18662d652008-10-02 18:32:44 +00003220static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003221{
bellard6a00d602005-11-21 23:25:50 +00003222 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003223 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003224}
3225#endif
3226
blueswir18662d652008-10-02 18:32:44 +00003227static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003228#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003229
3230#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003231 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003232 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003233 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003234
bellard9307c4c2004-04-04 12:57:25 +00003235 { "eax", offsetof(CPUState, regs[0]) },
3236 { "ecx", offsetof(CPUState, regs[1]) },
3237 { "edx", offsetof(CPUState, regs[2]) },
3238 { "ebx", offsetof(CPUState, regs[3]) },
3239 { "esp|sp", offsetof(CPUState, regs[4]) },
3240 { "ebp|fp", offsetof(CPUState, regs[5]) },
3241 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003242 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003243#ifdef TARGET_X86_64
3244 { "r8", offsetof(CPUState, regs[8]) },
3245 { "r9", offsetof(CPUState, regs[9]) },
3246 { "r10", offsetof(CPUState, regs[10]) },
3247 { "r11", offsetof(CPUState, regs[11]) },
3248 { "r12", offsetof(CPUState, regs[12]) },
3249 { "r13", offsetof(CPUState, regs[13]) },
3250 { "r14", offsetof(CPUState, regs[14]) },
3251 { "r15", offsetof(CPUState, regs[15]) },
3252#endif
bellard9307c4c2004-04-04 12:57:25 +00003253 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003254 { "eip", offsetof(CPUState, eip) },
3255 SEG("cs", R_CS)
3256 SEG("ds", R_DS)
3257 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003258 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003259 SEG("fs", R_FS)
3260 SEG("gs", R_GS)
3261 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003262#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003263 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003264 { "r0", offsetof(CPUState, gpr[0]) },
3265 { "r1", offsetof(CPUState, gpr[1]) },
3266 { "r2", offsetof(CPUState, gpr[2]) },
3267 { "r3", offsetof(CPUState, gpr[3]) },
3268 { "r4", offsetof(CPUState, gpr[4]) },
3269 { "r5", offsetof(CPUState, gpr[5]) },
3270 { "r6", offsetof(CPUState, gpr[6]) },
3271 { "r7", offsetof(CPUState, gpr[7]) },
3272 { "r8", offsetof(CPUState, gpr[8]) },
3273 { "r9", offsetof(CPUState, gpr[9]) },
3274 { "r10", offsetof(CPUState, gpr[10]) },
3275 { "r11", offsetof(CPUState, gpr[11]) },
3276 { "r12", offsetof(CPUState, gpr[12]) },
3277 { "r13", offsetof(CPUState, gpr[13]) },
3278 { "r14", offsetof(CPUState, gpr[14]) },
3279 { "r15", offsetof(CPUState, gpr[15]) },
3280 { "r16", offsetof(CPUState, gpr[16]) },
3281 { "r17", offsetof(CPUState, gpr[17]) },
3282 { "r18", offsetof(CPUState, gpr[18]) },
3283 { "r19", offsetof(CPUState, gpr[19]) },
3284 { "r20", offsetof(CPUState, gpr[20]) },
3285 { "r21", offsetof(CPUState, gpr[21]) },
3286 { "r22", offsetof(CPUState, gpr[22]) },
3287 { "r23", offsetof(CPUState, gpr[23]) },
3288 { "r24", offsetof(CPUState, gpr[24]) },
3289 { "r25", offsetof(CPUState, gpr[25]) },
3290 { "r26", offsetof(CPUState, gpr[26]) },
3291 { "r27", offsetof(CPUState, gpr[27]) },
3292 { "r28", offsetof(CPUState, gpr[28]) },
3293 { "r29", offsetof(CPUState, gpr[29]) },
3294 { "r30", offsetof(CPUState, gpr[30]) },
3295 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003296 /* Floating point registers */
3297 { "f0", offsetof(CPUState, fpr[0]) },
3298 { "f1", offsetof(CPUState, fpr[1]) },
3299 { "f2", offsetof(CPUState, fpr[2]) },
3300 { "f3", offsetof(CPUState, fpr[3]) },
3301 { "f4", offsetof(CPUState, fpr[4]) },
3302 { "f5", offsetof(CPUState, fpr[5]) },
3303 { "f6", offsetof(CPUState, fpr[6]) },
3304 { "f7", offsetof(CPUState, fpr[7]) },
3305 { "f8", offsetof(CPUState, fpr[8]) },
3306 { "f9", offsetof(CPUState, fpr[9]) },
3307 { "f10", offsetof(CPUState, fpr[10]) },
3308 { "f11", offsetof(CPUState, fpr[11]) },
3309 { "f12", offsetof(CPUState, fpr[12]) },
3310 { "f13", offsetof(CPUState, fpr[13]) },
3311 { "f14", offsetof(CPUState, fpr[14]) },
3312 { "f15", offsetof(CPUState, fpr[15]) },
3313 { "f16", offsetof(CPUState, fpr[16]) },
3314 { "f17", offsetof(CPUState, fpr[17]) },
3315 { "f18", offsetof(CPUState, fpr[18]) },
3316 { "f19", offsetof(CPUState, fpr[19]) },
3317 { "f20", offsetof(CPUState, fpr[20]) },
3318 { "f21", offsetof(CPUState, fpr[21]) },
3319 { "f22", offsetof(CPUState, fpr[22]) },
3320 { "f23", offsetof(CPUState, fpr[23]) },
3321 { "f24", offsetof(CPUState, fpr[24]) },
3322 { "f25", offsetof(CPUState, fpr[25]) },
3323 { "f26", offsetof(CPUState, fpr[26]) },
3324 { "f27", offsetof(CPUState, fpr[27]) },
3325 { "f28", offsetof(CPUState, fpr[28]) },
3326 { "f29", offsetof(CPUState, fpr[29]) },
3327 { "f30", offsetof(CPUState, fpr[30]) },
3328 { "f31", offsetof(CPUState, fpr[31]) },
3329 { "fpscr", offsetof(CPUState, fpscr) },
3330 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003331 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003332 { "lr", offsetof(CPUState, lr) },
3333 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003334 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003335 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003336 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003337 { "msr", 0, &monitor_get_msr, },
3338 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003339 { "tbu", 0, &monitor_get_tbu, },
3340 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003341#if defined(TARGET_PPC64)
3342 /* Address space register */
3343 { "asr", offsetof(CPUState, asr) },
3344#endif
3345 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003346 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003347 { "sr0", offsetof(CPUState, sr[0]) },
3348 { "sr1", offsetof(CPUState, sr[1]) },
3349 { "sr2", offsetof(CPUState, sr[2]) },
3350 { "sr3", offsetof(CPUState, sr[3]) },
3351 { "sr4", offsetof(CPUState, sr[4]) },
3352 { "sr5", offsetof(CPUState, sr[5]) },
3353 { "sr6", offsetof(CPUState, sr[6]) },
3354 { "sr7", offsetof(CPUState, sr[7]) },
3355 { "sr8", offsetof(CPUState, sr[8]) },
3356 { "sr9", offsetof(CPUState, sr[9]) },
3357 { "sr10", offsetof(CPUState, sr[10]) },
3358 { "sr11", offsetof(CPUState, sr[11]) },
3359 { "sr12", offsetof(CPUState, sr[12]) },
3360 { "sr13", offsetof(CPUState, sr[13]) },
3361 { "sr14", offsetof(CPUState, sr[14]) },
3362 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003363 /* Too lazy to put BATs... */
3364 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3365
3366 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3367 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3368 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3369 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3370 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3371 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3372 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3373 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3374 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3375 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3376 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3377 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3378 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3379 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3380 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3381 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3382 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3383 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3384 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3385 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3386 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3387 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3388 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3389 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3390 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3391 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3392 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3393 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3394 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3395 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3396 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3397 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3398 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3399 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3400 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3401 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3402 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3403 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3404 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3405 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3406 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3407 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3408 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3409 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3410 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3411 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3412 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3413 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3414 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3415 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3416 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3417 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3418 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3419 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3420 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3421 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3422 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3423 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3424 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3425 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3426 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3427 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3428 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3429 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3430 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3431 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3432
bellarde95c8d52004-09-30 22:22:08 +00003433#elif defined(TARGET_SPARC)
3434 { "g0", offsetof(CPUState, gregs[0]) },
3435 { "g1", offsetof(CPUState, gregs[1]) },
3436 { "g2", offsetof(CPUState, gregs[2]) },
3437 { "g3", offsetof(CPUState, gregs[3]) },
3438 { "g4", offsetof(CPUState, gregs[4]) },
3439 { "g5", offsetof(CPUState, gregs[5]) },
3440 { "g6", offsetof(CPUState, gregs[6]) },
3441 { "g7", offsetof(CPUState, gregs[7]) },
3442 { "o0", 0, monitor_get_reg },
3443 { "o1", 1, monitor_get_reg },
3444 { "o2", 2, monitor_get_reg },
3445 { "o3", 3, monitor_get_reg },
3446 { "o4", 4, monitor_get_reg },
3447 { "o5", 5, monitor_get_reg },
3448 { "o6", 6, monitor_get_reg },
3449 { "o7", 7, monitor_get_reg },
3450 { "l0", 8, monitor_get_reg },
3451 { "l1", 9, monitor_get_reg },
3452 { "l2", 10, monitor_get_reg },
3453 { "l3", 11, monitor_get_reg },
3454 { "l4", 12, monitor_get_reg },
3455 { "l5", 13, monitor_get_reg },
3456 { "l6", 14, monitor_get_reg },
3457 { "l7", 15, monitor_get_reg },
3458 { "i0", 16, monitor_get_reg },
3459 { "i1", 17, monitor_get_reg },
3460 { "i2", 18, monitor_get_reg },
3461 { "i3", 19, monitor_get_reg },
3462 { "i4", 20, monitor_get_reg },
3463 { "i5", 21, monitor_get_reg },
3464 { "i6", 22, monitor_get_reg },
3465 { "i7", 23, monitor_get_reg },
3466 { "pc", offsetof(CPUState, pc) },
3467 { "npc", offsetof(CPUState, npc) },
3468 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003469#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003470 { "psr", 0, &monitor_get_psr, },
3471 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003472#endif
bellarde95c8d52004-09-30 22:22:08 +00003473 { "tbr", offsetof(CPUState, tbr) },
3474 { "fsr", offsetof(CPUState, fsr) },
3475 { "f0", offsetof(CPUState, fpr[0]) },
3476 { "f1", offsetof(CPUState, fpr[1]) },
3477 { "f2", offsetof(CPUState, fpr[2]) },
3478 { "f3", offsetof(CPUState, fpr[3]) },
3479 { "f4", offsetof(CPUState, fpr[4]) },
3480 { "f5", offsetof(CPUState, fpr[5]) },
3481 { "f6", offsetof(CPUState, fpr[6]) },
3482 { "f7", offsetof(CPUState, fpr[7]) },
3483 { "f8", offsetof(CPUState, fpr[8]) },
3484 { "f9", offsetof(CPUState, fpr[9]) },
3485 { "f10", offsetof(CPUState, fpr[10]) },
3486 { "f11", offsetof(CPUState, fpr[11]) },
3487 { "f12", offsetof(CPUState, fpr[12]) },
3488 { "f13", offsetof(CPUState, fpr[13]) },
3489 { "f14", offsetof(CPUState, fpr[14]) },
3490 { "f15", offsetof(CPUState, fpr[15]) },
3491 { "f16", offsetof(CPUState, fpr[16]) },
3492 { "f17", offsetof(CPUState, fpr[17]) },
3493 { "f18", offsetof(CPUState, fpr[18]) },
3494 { "f19", offsetof(CPUState, fpr[19]) },
3495 { "f20", offsetof(CPUState, fpr[20]) },
3496 { "f21", offsetof(CPUState, fpr[21]) },
3497 { "f22", offsetof(CPUState, fpr[22]) },
3498 { "f23", offsetof(CPUState, fpr[23]) },
3499 { "f24", offsetof(CPUState, fpr[24]) },
3500 { "f25", offsetof(CPUState, fpr[25]) },
3501 { "f26", offsetof(CPUState, fpr[26]) },
3502 { "f27", offsetof(CPUState, fpr[27]) },
3503 { "f28", offsetof(CPUState, fpr[28]) },
3504 { "f29", offsetof(CPUState, fpr[29]) },
3505 { "f30", offsetof(CPUState, fpr[30]) },
3506 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003507#ifdef TARGET_SPARC64
3508 { "f32", offsetof(CPUState, fpr[32]) },
3509 { "f34", offsetof(CPUState, fpr[34]) },
3510 { "f36", offsetof(CPUState, fpr[36]) },
3511 { "f38", offsetof(CPUState, fpr[38]) },
3512 { "f40", offsetof(CPUState, fpr[40]) },
3513 { "f42", offsetof(CPUState, fpr[42]) },
3514 { "f44", offsetof(CPUState, fpr[44]) },
3515 { "f46", offsetof(CPUState, fpr[46]) },
3516 { "f48", offsetof(CPUState, fpr[48]) },
3517 { "f50", offsetof(CPUState, fpr[50]) },
3518 { "f52", offsetof(CPUState, fpr[52]) },
3519 { "f54", offsetof(CPUState, fpr[54]) },
3520 { "f56", offsetof(CPUState, fpr[56]) },
3521 { "f58", offsetof(CPUState, fpr[58]) },
3522 { "f60", offsetof(CPUState, fpr[60]) },
3523 { "f62", offsetof(CPUState, fpr[62]) },
3524 { "asi", offsetof(CPUState, asi) },
3525 { "pstate", offsetof(CPUState, pstate) },
3526 { "cansave", offsetof(CPUState, cansave) },
3527 { "canrestore", offsetof(CPUState, canrestore) },
3528 { "otherwin", offsetof(CPUState, otherwin) },
3529 { "wstate", offsetof(CPUState, wstate) },
3530 { "cleanwin", offsetof(CPUState, cleanwin) },
3531 { "fprs", offsetof(CPUState, fprs) },
3532#endif
bellard9307c4c2004-04-04 12:57:25 +00003533#endif
3534 { NULL },
3535};
3536
aliguori376253e2009-03-05 23:01:23 +00003537static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003538{
aliguori376253e2009-03-05 23:01:23 +00003539 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003540 longjmp(expr_env, 1);
3541}
3542
Markus Armbruster09b94182010-01-20 13:07:30 +01003543/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003544static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003545{
blueswir18662d652008-10-02 18:32:44 +00003546 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003547 void *ptr;
3548
bellard9307c4c2004-04-04 12:57:25 +00003549 for(md = monitor_defs; md->name != NULL; md++) {
3550 if (compare_cmd(name, md->name)) {
3551 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003552 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003553 } else {
bellard6a00d602005-11-21 23:25:50 +00003554 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003555 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003556 switch(md->type) {
3557 case MD_I32:
3558 *pval = *(int32_t *)ptr;
3559 break;
3560 case MD_TLONG:
3561 *pval = *(target_long *)ptr;
3562 break;
3563 default:
3564 *pval = 0;
3565 break;
3566 }
bellard9307c4c2004-04-04 12:57:25 +00003567 }
3568 return 0;
3569 }
3570 }
3571 return -1;
3572}
3573
3574static void next(void)
3575{
Blue Swirl660f11b2009-07-31 21:16:51 +00003576 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003577 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003578 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003579 pch++;
3580 }
3581}
3582
aliguori376253e2009-03-05 23:01:23 +00003583static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003584
aliguori376253e2009-03-05 23:01:23 +00003585static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003586{
blueswir1c2efc952007-09-25 17:28:42 +00003587 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003588 char *p;
bellard6a00d602005-11-21 23:25:50 +00003589 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003590
3591 switch(*pch) {
3592 case '+':
3593 next();
aliguori376253e2009-03-05 23:01:23 +00003594 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003595 break;
3596 case '-':
3597 next();
aliguori376253e2009-03-05 23:01:23 +00003598 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003599 break;
3600 case '~':
3601 next();
aliguori376253e2009-03-05 23:01:23 +00003602 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003603 break;
3604 case '(':
3605 next();
aliguori376253e2009-03-05 23:01:23 +00003606 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003607 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003608 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003609 }
3610 next();
3611 break;
bellard81d09122004-07-14 17:21:37 +00003612 case '\'':
3613 pch++;
3614 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003615 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003616 n = *pch;
3617 pch++;
3618 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003619 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003620 next();
3621 break;
bellard9307c4c2004-04-04 12:57:25 +00003622 case '$':
3623 {
3624 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003625 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003626
bellard9307c4c2004-04-04 12:57:25 +00003627 pch++;
3628 q = buf;
3629 while ((*pch >= 'a' && *pch <= 'z') ||
3630 (*pch >= 'A' && *pch <= 'Z') ||
3631 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003632 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003633 if ((q - buf) < sizeof(buf) - 1)
3634 *q++ = *pch;
3635 pch++;
3636 }
blueswir1cd390082008-11-16 13:53:32 +00003637 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003638 pch++;
3639 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003640 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003641 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003642 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003643 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003644 }
3645 break;
3646 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003647 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003648 n = 0;
3649 break;
3650 default:
blueswir17743e582007-09-24 18:39:04 +00003651#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003652 n = strtoull(pch, &p, 0);
3653#else
bellard9307c4c2004-04-04 12:57:25 +00003654 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003655#endif
bellard9307c4c2004-04-04 12:57:25 +00003656 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003657 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003658 }
3659 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003660 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003661 pch++;
3662 break;
3663 }
3664 return n;
3665}
3666
3667
aliguori376253e2009-03-05 23:01:23 +00003668static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003669{
blueswir1c2efc952007-09-25 17:28:42 +00003670 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003671 int op;
ths3b46e622007-09-17 08:09:54 +00003672
aliguori376253e2009-03-05 23:01:23 +00003673 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003674 for(;;) {
3675 op = *pch;
3676 if (op != '*' && op != '/' && op != '%')
3677 break;
3678 next();
aliguori376253e2009-03-05 23:01:23 +00003679 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003680 switch(op) {
3681 default:
3682 case '*':
3683 val *= val2;
3684 break;
3685 case '/':
3686 case '%':
ths5fafdf22007-09-16 21:08:06 +00003687 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003688 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003689 if (op == '/')
3690 val /= val2;
3691 else
3692 val %= val2;
3693 break;
3694 }
3695 }
3696 return val;
3697}
3698
aliguori376253e2009-03-05 23:01:23 +00003699static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003700{
blueswir1c2efc952007-09-25 17:28:42 +00003701 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003702 int op;
bellard9307c4c2004-04-04 12:57:25 +00003703
aliguori376253e2009-03-05 23:01:23 +00003704 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003705 for(;;) {
3706 op = *pch;
3707 if (op != '&' && op != '|' && op != '^')
3708 break;
3709 next();
aliguori376253e2009-03-05 23:01:23 +00003710 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003711 switch(op) {
3712 default:
3713 case '&':
3714 val &= val2;
3715 break;
3716 case '|':
3717 val |= val2;
3718 break;
3719 case '^':
3720 val ^= val2;
3721 break;
3722 }
3723 }
3724 return val;
3725}
3726
aliguori376253e2009-03-05 23:01:23 +00003727static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003728{
blueswir1c2efc952007-09-25 17:28:42 +00003729 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003730 int op;
bellard9307c4c2004-04-04 12:57:25 +00003731
aliguori376253e2009-03-05 23:01:23 +00003732 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003733 for(;;) {
3734 op = *pch;
3735 if (op != '+' && op != '-')
3736 break;
3737 next();
aliguori376253e2009-03-05 23:01:23 +00003738 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003739 if (op == '+')
3740 val += val2;
3741 else
3742 val -= val2;
3743 }
3744 return val;
3745}
3746
aliguori376253e2009-03-05 23:01:23 +00003747static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003748{
3749 pch = *pp;
3750 if (setjmp(expr_env)) {
3751 *pp = pch;
3752 return -1;
3753 }
blueswir1cd390082008-11-16 13:53:32 +00003754 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003755 pch++;
aliguori376253e2009-03-05 23:01:23 +00003756 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003757 *pp = pch;
3758 return 0;
3759}
3760
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003761static int get_double(Monitor *mon, double *pval, const char **pp)
3762{
3763 const char *p = *pp;
3764 char *tailp;
3765 double d;
3766
3767 d = strtod(p, &tailp);
3768 if (tailp == p) {
3769 monitor_printf(mon, "Number expected\n");
3770 return -1;
3771 }
3772 if (d != d || d - d != 0) {
3773 /* NaN or infinity */
3774 monitor_printf(mon, "Bad number\n");
3775 return -1;
3776 }
3777 *pval = d;
3778 *pp = tailp;
3779 return 0;
3780}
3781
bellard9307c4c2004-04-04 12:57:25 +00003782static int get_str(char *buf, int buf_size, const char **pp)
3783{
3784 const char *p;
3785 char *q;
3786 int c;
3787
bellard81d09122004-07-14 17:21:37 +00003788 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003789 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003790 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003791 p++;
3792 if (*p == '\0') {
3793 fail:
bellard81d09122004-07-14 17:21:37 +00003794 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003795 *pp = p;
3796 return -1;
3797 }
bellard9307c4c2004-04-04 12:57:25 +00003798 if (*p == '\"') {
3799 p++;
3800 while (*p != '\0' && *p != '\"') {
3801 if (*p == '\\') {
3802 p++;
3803 c = *p++;
3804 switch(c) {
3805 case 'n':
3806 c = '\n';
3807 break;
3808 case 'r':
3809 c = '\r';
3810 break;
3811 case '\\':
3812 case '\'':
3813 case '\"':
3814 break;
3815 default:
3816 qemu_printf("unsupported escape code: '\\%c'\n", c);
3817 goto fail;
3818 }
3819 if ((q - buf) < buf_size - 1) {
3820 *q++ = c;
3821 }
3822 } else {
3823 if ((q - buf) < buf_size - 1) {
3824 *q++ = *p;
3825 }
3826 p++;
3827 }
3828 }
3829 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003830 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003831 goto fail;
3832 }
3833 p++;
3834 } else {
blueswir1cd390082008-11-16 13:53:32 +00003835 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003836 if ((q - buf) < buf_size - 1) {
3837 *q++ = *p;
3838 }
3839 p++;
3840 }
bellard9307c4c2004-04-04 12:57:25 +00003841 }
bellard81d09122004-07-14 17:21:37 +00003842 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003843 *pp = p;
3844 return 0;
3845}
3846
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003847/*
3848 * Store the command-name in cmdname, and return a pointer to
3849 * the remaining of the command string.
3850 */
3851static const char *get_command_name(const char *cmdline,
3852 char *cmdname, size_t nlen)
3853{
3854 size_t len;
3855 const char *p, *pstart;
3856
3857 p = cmdline;
3858 while (qemu_isspace(*p))
3859 p++;
3860 if (*p == '\0')
3861 return NULL;
3862 pstart = p;
3863 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3864 p++;
3865 len = p - pstart;
3866 if (len > nlen - 1)
3867 len = nlen - 1;
3868 memcpy(cmdname, pstart, len);
3869 cmdname[len] = '\0';
3870 return p;
3871}
3872
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003873/**
3874 * Read key of 'type' into 'key' and return the current
3875 * 'type' pointer.
3876 */
3877static char *key_get_info(const char *type, char **key)
3878{
3879 size_t len;
3880 char *p, *str;
3881
3882 if (*type == ',')
3883 type++;
3884
3885 p = strchr(type, ':');
3886 if (!p) {
3887 *key = NULL;
3888 return NULL;
3889 }
3890 len = p - type;
3891
Anthony Liguori7267c092011-08-20 22:09:37 -05003892 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003893 memcpy(str, type, len);
3894 str[len] = '\0';
3895
3896 *key = str;
3897 return ++p;
3898}
3899
bellard9307c4c2004-04-04 12:57:25 +00003900static int default_fmt_format = 'x';
3901static int default_fmt_size = 4;
3902
3903#define MAX_ARGS 16
3904
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003905static int is_valid_option(const char *c, const char *typestr)
3906{
3907 char option[3];
3908
3909 option[0] = '-';
3910 option[1] = *c;
3911 option[2] = '\0';
3912
3913 typestr = strstr(typestr, option);
3914 return (typestr != NULL);
3915}
3916
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003917static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3918 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003919{
3920 const mon_cmd_t *cmd;
3921
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003922 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003923 if (compare_cmd(cmdname, cmd->name)) {
3924 return cmd;
3925 }
3926 }
3927
3928 return NULL;
3929}
3930
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003931static const mon_cmd_t *monitor_find_command(const char *cmdname)
3932{
3933 return search_dispatch_table(mon_cmds, cmdname);
3934}
3935
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003936static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3937{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003938 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003939}
3940
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003941static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3942{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003943 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003944}
3945
Anthony Liguoric227f092009-10-01 16:12:16 -05003946static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003947 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003948 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003949{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003950 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003951 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003952 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003953 char cmdname[256];
3954 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003955 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003956
3957#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003958 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003959#endif
ths3b46e622007-09-17 08:09:54 +00003960
bellard9307c4c2004-04-04 12:57:25 +00003961 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003962 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3963 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003964 return NULL;
ths3b46e622007-09-17 08:09:54 +00003965
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003966 cmd = monitor_find_command(cmdname);
3967 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003968 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003969 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003970 }
bellard9307c4c2004-04-04 12:57:25 +00003971
bellard9307c4c2004-04-04 12:57:25 +00003972 /* parse the parameters */
3973 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003974 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003975 typestr = key_get_info(typestr, &key);
3976 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003977 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003978 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003979 typestr++;
3980 switch(c) {
3981 case 'F':
bellard81d09122004-07-14 17:21:37 +00003982 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003983 case 's':
3984 {
3985 int ret;
ths3b46e622007-09-17 08:09:54 +00003986
blueswir1cd390082008-11-16 13:53:32 +00003987 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003988 p++;
3989 if (*typestr == '?') {
3990 typestr++;
3991 if (*p == '\0') {
3992 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003993 break;
bellard9307c4c2004-04-04 12:57:25 +00003994 }
3995 }
3996 ret = get_str(buf, sizeof(buf), &p);
3997 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003998 switch(c) {
3999 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004000 monitor_printf(mon, "%s: filename expected\n",
4001 cmdname);
bellard81d09122004-07-14 17:21:37 +00004002 break;
4003 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004004 monitor_printf(mon, "%s: block device name expected\n",
4005 cmdname);
bellard81d09122004-07-14 17:21:37 +00004006 break;
4007 default:
aliguori376253e2009-03-05 23:01:23 +00004008 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004009 break;
4010 }
bellard9307c4c2004-04-04 12:57:25 +00004011 goto fail;
4012 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004013 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004014 }
4015 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004016 case 'O':
4017 {
4018 QemuOptsList *opts_list;
4019 QemuOpts *opts;
4020
4021 opts_list = qemu_find_opts(key);
4022 if (!opts_list || opts_list->desc->name) {
4023 goto bad_type;
4024 }
4025 while (qemu_isspace(*p)) {
4026 p++;
4027 }
4028 if (!*p)
4029 break;
4030 if (get_str(buf, sizeof(buf), &p) < 0) {
4031 goto fail;
4032 }
4033 opts = qemu_opts_parse(opts_list, buf, 1);
4034 if (!opts) {
4035 goto fail;
4036 }
4037 qemu_opts_to_qdict(opts, qdict);
4038 qemu_opts_del(opts);
4039 }
4040 break;
bellard9307c4c2004-04-04 12:57:25 +00004041 case '/':
4042 {
4043 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004044
blueswir1cd390082008-11-16 13:53:32 +00004045 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004046 p++;
4047 if (*p == '/') {
4048 /* format found */
4049 p++;
4050 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004051 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004052 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004053 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004054 count = count * 10 + (*p - '0');
4055 p++;
4056 }
4057 }
4058 size = -1;
4059 format = -1;
4060 for(;;) {
4061 switch(*p) {
4062 case 'o':
4063 case 'd':
4064 case 'u':
4065 case 'x':
4066 case 'i':
4067 case 'c':
4068 format = *p++;
4069 break;
4070 case 'b':
4071 size = 1;
4072 p++;
4073 break;
4074 case 'h':
4075 size = 2;
4076 p++;
4077 break;
4078 case 'w':
4079 size = 4;
4080 p++;
4081 break;
4082 case 'g':
4083 case 'L':
4084 size = 8;
4085 p++;
4086 break;
4087 default:
4088 goto next;
4089 }
4090 }
4091 next:
blueswir1cd390082008-11-16 13:53:32 +00004092 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004093 monitor_printf(mon, "invalid char in format: '%c'\n",
4094 *p);
bellard9307c4c2004-04-04 12:57:25 +00004095 goto fail;
4096 }
bellard9307c4c2004-04-04 12:57:25 +00004097 if (format < 0)
4098 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004099 if (format != 'i') {
4100 /* for 'i', not specifying a size gives -1 as size */
4101 if (size < 0)
4102 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004103 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004104 }
bellard9307c4c2004-04-04 12:57:25 +00004105 default_fmt_format = format;
4106 } else {
4107 count = 1;
4108 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004109 if (format != 'i') {
4110 size = default_fmt_size;
4111 } else {
4112 size = -1;
4113 }
bellard9307c4c2004-04-04 12:57:25 +00004114 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004115 qdict_put(qdict, "count", qint_from_int(count));
4116 qdict_put(qdict, "format", qint_from_int(format));
4117 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004118 }
4119 break;
4120 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004121 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004122 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004123 {
blueswir1c2efc952007-09-25 17:28:42 +00004124 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004125
blueswir1cd390082008-11-16 13:53:32 +00004126 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004127 p++;
bellard34405572004-06-08 00:55:58 +00004128 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004129 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004130 if (*p == '\0') {
4131 typestr++;
4132 break;
4133 }
bellard34405572004-06-08 00:55:58 +00004134 } else {
4135 if (*p == '.') {
4136 p++;
blueswir1cd390082008-11-16 13:53:32 +00004137 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004138 p++;
bellard34405572004-06-08 00:55:58 +00004139 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004140 typestr++;
4141 break;
bellard34405572004-06-08 00:55:58 +00004142 }
4143 }
bellard13224a82006-07-14 22:03:35 +00004144 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004145 }
aliguori376253e2009-03-05 23:01:23 +00004146 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004147 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004148 /* Check if 'i' is greater than 32-bit */
4149 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4150 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4151 monitor_printf(mon, "integer is for 32-bit values\n");
4152 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004153 } else if (c == 'M') {
4154 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004155 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004156 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004157 }
4158 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004159 case 'o':
4160 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004161 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004162 char *end;
4163
4164 while (qemu_isspace(*p)) {
4165 p++;
4166 }
4167 if (*typestr == '?') {
4168 typestr++;
4169 if (*p == '\0') {
4170 break;
4171 }
4172 }
4173 val = strtosz(p, &end);
4174 if (val < 0) {
4175 monitor_printf(mon, "invalid size\n");
4176 goto fail;
4177 }
4178 qdict_put(qdict, key, qint_from_int(val));
4179 p = end;
4180 }
4181 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004182 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004183 {
4184 double val;
4185
4186 while (qemu_isspace(*p))
4187 p++;
4188 if (*typestr == '?') {
4189 typestr++;
4190 if (*p == '\0') {
4191 break;
4192 }
4193 }
4194 if (get_double(mon, &val, &p) < 0) {
4195 goto fail;
4196 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004197 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004198 switch (*p) {
4199 case 'm':
4200 val /= 1e3; p += 2; break;
4201 case 'u':
4202 val /= 1e6; p += 2; break;
4203 case 'n':
4204 val /= 1e9; p += 2; break;
4205 }
4206 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004207 if (*p && !qemu_isspace(*p)) {
4208 monitor_printf(mon, "Unknown unit suffix\n");
4209 goto fail;
4210 }
4211 qdict_put(qdict, key, qfloat_from_double(val));
4212 }
4213 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004214 case 'b':
4215 {
4216 const char *beg;
4217 int val;
4218
4219 while (qemu_isspace(*p)) {
4220 p++;
4221 }
4222 beg = p;
4223 while (qemu_isgraph(*p)) {
4224 p++;
4225 }
4226 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4227 val = 1;
4228 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4229 val = 0;
4230 } else {
4231 monitor_printf(mon, "Expected 'on' or 'off'\n");
4232 goto fail;
4233 }
4234 qdict_put(qdict, key, qbool_from_int(val));
4235 }
4236 break;
bellard9307c4c2004-04-04 12:57:25 +00004237 case '-':
4238 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004239 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004240 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004241 /* option */
ths3b46e622007-09-17 08:09:54 +00004242
bellard9307c4c2004-04-04 12:57:25 +00004243 c = *typestr++;
4244 if (c == '\0')
4245 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004246 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004247 p++;
bellard9307c4c2004-04-04 12:57:25 +00004248 if (*p == '-') {
4249 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004250 if(c != *p) {
4251 if(!is_valid_option(p, typestr)) {
4252
4253 monitor_printf(mon, "%s: unsupported option -%c\n",
4254 cmdname, *p);
4255 goto fail;
4256 } else {
4257 skip_key = 1;
4258 }
bellard9307c4c2004-04-04 12:57:25 +00004259 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004260 if(skip_key) {
4261 p = tmp;
4262 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004263 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004264 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004265 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004266 }
bellard9307c4c2004-04-04 12:57:25 +00004267 }
bellard9307c4c2004-04-04 12:57:25 +00004268 }
4269 break;
4270 default:
4271 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004272 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004273 goto fail;
4274 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004275 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004276 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004277 }
4278 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004279 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004280 p++;
4281 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004282 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4283 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004284 goto fail;
4285 }
4286
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004287 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004288
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004289fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004290 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004291 return NULL;
4292}
4293
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004294void monitor_set_error(Monitor *mon, QError *qerror)
4295{
4296 /* report only the first error */
4297 if (!mon->error) {
4298 mon->error = qerror;
4299 } else {
4300 MON_DEBUG("Additional error report at %s:%d\n",
4301 qerror->file, qerror->linenr);
4302 QDECREF(qerror);
4303 }
4304}
4305
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004306static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4307{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004308 if (ret && !monitor_has_error(mon)) {
4309 /*
4310 * If it returns failure, it must have passed on error.
4311 *
4312 * Action: Report an internal error to the client if in QMP.
4313 */
4314 qerror_report(QERR_UNDEFINED_ERROR);
4315 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4316 cmd->name);
4317 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004318
4319#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004320 if (!ret && monitor_has_error(mon)) {
4321 /*
4322 * If it returns success, it must not have passed an error.
4323 *
4324 * Action: Report the passed error to the client.
4325 */
4326 MON_DEBUG("command '%s' returned success but passed an error\n",
4327 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004328 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004329
4330 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4331 /*
4332 * Handlers should not call Monitor print functions.
4333 *
4334 * Action: Ignore them in QMP.
4335 *
4336 * (XXX: we don't check any 'info' or 'query' command here
4337 * because the user print function _is_ called by do_info(), hence
4338 * we will trigger this check. This problem will go away when we
4339 * make 'query' commands real and kill do_info())
4340 */
4341 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4342 cmd->name, mon_print_count_get(mon));
4343 }
4344#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004345}
4346
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004347static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004348{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004349 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004350 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004351
4352 qdict = qdict_new();
4353
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004354 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004355 if (!cmd)
4356 goto out;
4357
Luiz Capitulino4903de02010-09-16 11:01:32 -03004358 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004359 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004360 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004361 QObject *data = NULL;
4362
4363 /* XXX: ignores the error code */
4364 cmd->mhandler.cmd_new(mon, qdict, &data);
4365 assert(!monitor_has_error(mon));
4366 if (data) {
4367 cmd->user_print(mon, data);
4368 qobject_decref(data);
4369 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004370 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004371 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004372 }
4373
Luiz Capitulino13917be2009-10-07 13:41:54 -03004374out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004375 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004376}
4377
bellard81d09122004-07-14 17:21:37 +00004378static void cmd_completion(const char *name, const char *list)
4379{
4380 const char *p, *pstart;
4381 char cmd[128];
4382 int len;
4383
4384 p = list;
4385 for(;;) {
4386 pstart = p;
4387 p = strchr(p, '|');
4388 if (!p)
4389 p = pstart + strlen(pstart);
4390 len = p - pstart;
4391 if (len > sizeof(cmd) - 2)
4392 len = sizeof(cmd) - 2;
4393 memcpy(cmd, pstart, len);
4394 cmd[len] = '\0';
4395 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004396 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004397 }
4398 if (*p == '\0')
4399 break;
4400 p++;
4401 }
4402}
4403
4404static void file_completion(const char *input)
4405{
4406 DIR *ffs;
4407 struct dirent *d;
4408 char path[1024];
4409 char file[1024], file_prefix[1024];
4410 int input_path_len;
4411 const char *p;
4412
ths5fafdf22007-09-16 21:08:06 +00004413 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004414 if (!p) {
4415 input_path_len = 0;
4416 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004417 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004418 } else {
4419 input_path_len = p - input + 1;
4420 memcpy(path, input, input_path_len);
4421 if (input_path_len > sizeof(path) - 1)
4422 input_path_len = sizeof(path) - 1;
4423 path[input_path_len] = '\0';
4424 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4425 }
4426#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004427 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4428 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004429#endif
4430 ffs = opendir(path);
4431 if (!ffs)
4432 return;
4433 for(;;) {
4434 struct stat sb;
4435 d = readdir(ffs);
4436 if (!d)
4437 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004438
4439 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4440 continue;
4441 }
4442
bellard81d09122004-07-14 17:21:37 +00004443 if (strstart(d->d_name, file_prefix, NULL)) {
4444 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004445 if (input_path_len < sizeof(file))
4446 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4447 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004448 /* stat the file to find out if it's a directory.
4449 * In that case add a slash to speed up typing long paths
4450 */
4451 stat(file, &sb);
4452 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004453 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004454 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004455 }
4456 }
4457 closedir(ffs);
4458}
4459
aliguori51de9762009-03-05 23:00:43 +00004460static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004461{
aliguori51de9762009-03-05 23:00:43 +00004462 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004463 const char *input = opaque;
4464
4465 if (input[0] == '\0' ||
4466 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004467 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004468 }
4469}
4470
4471/* NOTE: this parser is an approximate form of the real command parser */
4472static void parse_cmdline(const char *cmdline,
4473 int *pnb_args, char **args)
4474{
4475 const char *p;
4476 int nb_args, ret;
4477 char buf[1024];
4478
4479 p = cmdline;
4480 nb_args = 0;
4481 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004482 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004483 p++;
4484 if (*p == '\0')
4485 break;
4486 if (nb_args >= MAX_ARGS)
4487 break;
4488 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004489 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004490 nb_args++;
4491 if (ret < 0)
4492 break;
4493 }
4494 *pnb_args = nb_args;
4495}
4496
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004497static const char *next_arg_type(const char *typestr)
4498{
4499 const char *p = strchr(typestr, ':');
4500 return (p != NULL ? ++p : typestr);
4501}
4502
aliguori4c36ba32009-03-05 23:01:37 +00004503static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004504{
4505 const char *cmdname;
4506 char *args[MAX_ARGS];
4507 int nb_args, i, len;
4508 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004509 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004510 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004511
4512 parse_cmdline(cmdline, &nb_args, args);
4513#ifdef DEBUG_COMPLETION
4514 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004515 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004516 }
4517#endif
4518
4519 /* if the line ends with a space, it means we want to complete the
4520 next arg */
4521 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004522 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004523 if (nb_args >= MAX_ARGS) {
4524 goto cleanup;
4525 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004526 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004527 }
4528 if (nb_args <= 1) {
4529 /* command completion */
4530 if (nb_args == 0)
4531 cmdname = "";
4532 else
4533 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004534 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004535 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004536 cmd_completion(cmdname, cmd->name);
4537 }
4538 } else {
4539 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004540 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4541 if (compare_cmd(args[0], cmd->name)) {
4542 break;
4543 }
bellard81d09122004-07-14 17:21:37 +00004544 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004545 if (!cmd->name) {
4546 goto cleanup;
4547 }
4548
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004549 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004550 for(i = 0; i < nb_args - 2; i++) {
4551 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004552 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004553 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004554 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004555 }
4556 }
4557 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004558 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004559 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004560 }
bellard81d09122004-07-14 17:21:37 +00004561 switch(*ptype) {
4562 case 'F':
4563 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004564 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004565 file_completion(str);
4566 break;
4567 case 'B':
4568 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004569 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004570 bdrv_iterate(block_completion_it, (void *)str);
4571 break;
bellard7fe48482004-10-09 18:08:01 +00004572 case 's':
4573 /* XXX: more generic ? */
4574 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004575 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004576 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4577 cmd_completion(str, cmd->name);
4578 }
bellard64866c32006-05-07 18:03:31 +00004579 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004580 char *sep = strrchr(str, '-');
4581 if (sep)
4582 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004583 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004584 for(key = key_defs; key->name != NULL; key++) {
4585 cmd_completion(str, key->name);
4586 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004587 } else if (!strcmp(cmd->name, "help|?")) {
4588 readline_set_completion_index(cur_mon->rs, strlen(str));
4589 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4590 cmd_completion(str, cmd->name);
4591 }
bellard7fe48482004-10-09 18:08:01 +00004592 }
4593 break;
bellard81d09122004-07-14 17:21:37 +00004594 default:
4595 break;
4596 }
4597 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004598
4599cleanup:
4600 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004601 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004602 }
bellard81d09122004-07-14 17:21:37 +00004603}
4604
aliguori731b0362009-03-05 23:01:42 +00004605static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004606{
aliguori731b0362009-03-05 23:01:42 +00004607 Monitor *mon = opaque;
4608
Jan Kiszkac62313b2009-12-04 14:05:29 +01004609 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004610}
4611
Luiz Capitulino09069b12010-02-04 18:10:06 -02004612static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4613{
4614 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4615 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4616}
4617
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004618/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004619 * Argument validation rules:
4620 *
4621 * 1. The argument must exist in cmd_args qdict
4622 * 2. The argument type must be the expected one
4623 *
4624 * Special case: If the argument doesn't exist in cmd_args and
4625 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4626 * checking is skipped for it.
4627 */
4628static int check_client_args_type(const QDict *client_args,
4629 const QDict *cmd_args, int flags)
4630{
4631 const QDictEntry *ent;
4632
4633 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4634 QObject *obj;
4635 QString *arg_type;
4636 const QObject *client_arg = qdict_entry_value(ent);
4637 const char *client_arg_name = qdict_entry_key(ent);
4638
4639 obj = qdict_get(cmd_args, client_arg_name);
4640 if (!obj) {
4641 if (flags & QMP_ACCEPT_UNKNOWNS) {
4642 /* handler accepts unknowns */
4643 continue;
4644 }
4645 /* client arg doesn't exist */
4646 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4647 return -1;
4648 }
4649
4650 arg_type = qobject_to_qstring(obj);
4651 assert(arg_type != NULL);
4652
4653 /* check if argument's type is correct */
4654 switch (qstring_get_str(arg_type)[0]) {
4655 case 'F':
4656 case 'B':
4657 case 's':
4658 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4659 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4660 "string");
4661 return -1;
4662 }
4663 break;
4664 case 'i':
4665 case 'l':
4666 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004667 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004668 if (qobject_type(client_arg) != QTYPE_QINT) {
4669 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4670 "int");
4671 return -1;
4672 }
4673 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004674 case 'T':
4675 if (qobject_type(client_arg) != QTYPE_QINT &&
4676 qobject_type(client_arg) != QTYPE_QFLOAT) {
4677 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4678 "number");
4679 return -1;
4680 }
4681 break;
4682 case 'b':
4683 case '-':
4684 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4685 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4686 "bool");
4687 return -1;
4688 }
4689 break;
4690 case 'O':
4691 assert(flags & QMP_ACCEPT_UNKNOWNS);
4692 break;
4693 case '/':
4694 case '.':
4695 /*
4696 * These types are not supported by QMP and thus are not
4697 * handled here. Fall through.
4698 */
4699 default:
4700 abort();
4701 }
4702 }
4703
4704 return 0;
4705}
4706
4707/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004708 * - Check if the client has passed all mandatory args
4709 * - Set special flags for argument validation
4710 */
4711static int check_mandatory_args(const QDict *cmd_args,
4712 const QDict *client_args, int *flags)
4713{
4714 const QDictEntry *ent;
4715
4716 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4717 const char *cmd_arg_name = qdict_entry_key(ent);
4718 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4719 assert(type != NULL);
4720
4721 if (qstring_get_str(type)[0] == 'O') {
4722 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4723 *flags |= QMP_ACCEPT_UNKNOWNS;
4724 } else if (qstring_get_str(type)[0] != '-' &&
4725 qstring_get_str(type)[1] != '?' &&
4726 !qdict_haskey(client_args, cmd_arg_name)) {
4727 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4728 return -1;
4729 }
4730 }
4731
4732 return 0;
4733}
4734
4735static QDict *qdict_from_args_type(const char *args_type)
4736{
4737 int i;
4738 QDict *qdict;
4739 QString *key, *type, *cur_qs;
4740
4741 assert(args_type != NULL);
4742
4743 qdict = qdict_new();
4744
4745 if (args_type == NULL || args_type[0] == '\0') {
4746 /* no args, empty qdict */
4747 goto out;
4748 }
4749
4750 key = qstring_new();
4751 type = qstring_new();
4752
4753 cur_qs = key;
4754
4755 for (i = 0;; i++) {
4756 switch (args_type[i]) {
4757 case ',':
4758 case '\0':
4759 qdict_put(qdict, qstring_get_str(key), type);
4760 QDECREF(key);
4761 if (args_type[i] == '\0') {
4762 goto out;
4763 }
4764 type = qstring_new(); /* qdict has ref */
4765 cur_qs = key = qstring_new();
4766 break;
4767 case ':':
4768 cur_qs = type;
4769 break;
4770 default:
4771 qstring_append_chr(cur_qs, args_type[i]);
4772 break;
4773 }
4774 }
4775
4776out:
4777 return qdict;
4778}
4779
4780/*
4781 * Client argument checking rules:
4782 *
4783 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004784 * 2. Each argument provided by the client must be expected
4785 * 3. Each argument provided by the client must have the type expected
4786 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004787 */
4788static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4789{
4790 int flags, err;
4791 QDict *cmd_args;
4792
4793 cmd_args = qdict_from_args_type(cmd->args_type);
4794
4795 flags = 0;
4796 err = check_mandatory_args(cmd_args, client_args, &flags);
4797 if (err) {
4798 goto out;
4799 }
4800
Luiz Capitulino4af91932010-06-22 11:44:05 -03004801 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004802
4803out:
4804 QDECREF(cmd_args);
4805 return err;
4806}
4807
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004808/*
4809 * Input object checking rules
4810 *
4811 * 1. Input object must be a dict
4812 * 2. The "execute" key must exist
4813 * 3. The "execute" key must be a string
4814 * 4. If the "arguments" key exists, it must be a dict
4815 * 5. If the "id" key exists, it can be anything (ie. json-value)
4816 * 6. Any argument not listed above is considered invalid
4817 */
4818static QDict *qmp_check_input_obj(QObject *input_obj)
4819{
4820 const QDictEntry *ent;
4821 int has_exec_key = 0;
4822 QDict *input_dict;
4823
4824 if (qobject_type(input_obj) != QTYPE_QDICT) {
4825 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4826 return NULL;
4827 }
4828
4829 input_dict = qobject_to_qdict(input_obj);
4830
4831 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4832 const char *arg_name = qdict_entry_key(ent);
4833 const QObject *arg_obj = qdict_entry_value(ent);
4834
4835 if (!strcmp(arg_name, "execute")) {
4836 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4837 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4838 "string");
4839 return NULL;
4840 }
4841 has_exec_key = 1;
4842 } else if (!strcmp(arg_name, "arguments")) {
4843 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4844 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4845 "object");
4846 return NULL;
4847 }
4848 } else if (!strcmp(arg_name, "id")) {
4849 /* FIXME: check duplicated IDs for async commands */
4850 } else {
4851 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4852 return NULL;
4853 }
4854 }
4855
4856 if (!has_exec_key) {
4857 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4858 return NULL;
4859 }
4860
4861 return input_dict;
4862}
4863
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004864static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4865{
4866 QObject *ret_data = NULL;
4867
Luiz Capitulino4903de02010-09-16 11:01:32 -03004868 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004869 qmp_async_info_handler(mon, cmd);
4870 if (monitor_has_error(mon)) {
4871 monitor_protocol_emitter(mon, NULL);
4872 }
4873 } else {
4874 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02004875 monitor_protocol_emitter(mon, ret_data);
4876 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004877 }
4878}
4879
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004880static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4881 const QDict *params)
4882{
4883 int ret;
4884 QObject *data = NULL;
4885
4886 mon_print_count_init(mon);
4887
4888 ret = cmd->mhandler.cmd_new(mon, params, &data);
4889 handler_audit(mon, cmd, ret);
4890 monitor_protocol_emitter(mon, data);
4891 qobject_decref(data);
4892}
4893
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004894static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4895{
4896 int err;
4897 QObject *obj;
4898 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004899 const mon_cmd_t *cmd;
4900 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004901 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004902
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004903 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004904 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004905
4906 obj = json_parser_parse(tokens, NULL);
4907 if (!obj) {
4908 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004909 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004910 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004911 }
4912
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004913 input = qmp_check_input_obj(obj);
4914 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004915 qobject_decref(obj);
4916 goto err_out;
4917 }
4918
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004919 mon->mc->id = qdict_get(input, "id");
4920 qobject_incref(mon->mc->id);
4921
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004922 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004923 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004924 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004925 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004926 }
4927
Anthony Liguorie3193602011-09-02 12:34:47 -05004928 cmd = qmp_find_cmd(cmd_name);
4929 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004930 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004931 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004932 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004933 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004934 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004935 }
4936
4937 obj = qdict_get(input, "arguments");
4938 if (!obj) {
4939 args = qdict_new();
4940 } else {
4941 args = qobject_to_qdict(obj);
4942 QINCREF(args);
4943 }
4944
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004945 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004946 if (err < 0) {
4947 goto err_out;
4948 }
4949
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004950 if (query_cmd) {
4951 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004952 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004953 err = qmp_async_cmd_handler(mon, cmd, args);
4954 if (err) {
4955 /* emit the error response */
4956 goto err_out;
4957 }
Adam Litke940cc302010-01-25 12:18:44 -06004958 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004959 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004960 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004961
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004962 goto out;
4963
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004964err_out:
4965 monitor_protocol_emitter(mon, NULL);
4966out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004967 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004968 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004969}
4970
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004971/**
4972 * monitor_control_read(): Read and handle QMP input
4973 */
4974static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4975{
4976 Monitor *old_mon = cur_mon;
4977
4978 cur_mon = opaque;
4979
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004980 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004981
4982 cur_mon = old_mon;
4983}
4984
aliguori731b0362009-03-05 23:01:42 +00004985static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004986{
aliguori731b0362009-03-05 23:01:42 +00004987 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004988 int i;
aliguori376253e2009-03-05 23:01:23 +00004989
aliguori731b0362009-03-05 23:01:42 +00004990 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004991
aliguoricde76ee2009-03-05 23:01:51 +00004992 if (cur_mon->rs) {
4993 for (i = 0; i < size; i++)
4994 readline_handle_byte(cur_mon->rs, buf[i]);
4995 } else {
4996 if (size == 0 || buf[size - 1] != 0)
4997 monitor_printf(cur_mon, "corrupted command\n");
4998 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004999 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005000 }
aliguori731b0362009-03-05 23:01:42 +00005001
5002 cur_mon = old_mon;
5003}
aliguorid8f44602008-10-06 13:52:44 +00005004
aliguori376253e2009-03-05 23:01:23 +00005005static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005006{
aliguori731b0362009-03-05 23:01:42 +00005007 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005008 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005009 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005010}
5011
aliguoricde76ee2009-03-05 23:01:51 +00005012int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005013{
aliguoricde76ee2009-03-05 23:01:51 +00005014 if (!mon->rs)
5015 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005016 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005017 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005018}
5019
aliguori376253e2009-03-05 23:01:23 +00005020void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005021{
aliguoricde76ee2009-03-05 23:01:51 +00005022 if (!mon->rs)
5023 return;
aliguori731b0362009-03-05 23:01:42 +00005024 if (--mon->suspend_cnt == 0)
5025 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005026}
5027
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005028static QObject *get_qmp_greeting(void)
5029{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005030 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005031
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005032 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005033 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5034}
5035
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005036/**
5037 * monitor_control_event(): Print QMP gretting
5038 */
5039static void monitor_control_event(void *opaque, int event)
5040{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005041 QObject *data;
5042 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005043
Luiz Capitulino47116d12010-02-08 17:01:30 -02005044 switch (event) {
5045 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005046 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005047 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005048 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005049 monitor_json_emitter(mon, data);
5050 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005051 break;
5052 case CHR_EVENT_CLOSED:
5053 json_message_parser_destroy(&mon->mc->parser);
5054 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005055 }
5056}
5057
aliguori731b0362009-03-05 23:01:42 +00005058static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005059{
aliguori376253e2009-03-05 23:01:23 +00005060 Monitor *mon = opaque;
5061
aliguori2724b182009-03-05 23:01:47 +00005062 switch (event) {
5063 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005064 mon->mux_out = 0;
5065 if (mon->reset_seen) {
5066 readline_restart(mon->rs);
5067 monitor_resume(mon);
5068 monitor_flush(mon);
5069 } else {
5070 mon->suspend_cnt = 0;
5071 }
aliguori2724b182009-03-05 23:01:47 +00005072 break;
ths86e94de2007-01-05 22:01:59 +00005073
aliguori2724b182009-03-05 23:01:47 +00005074 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005075 if (mon->reset_seen) {
5076 if (mon->suspend_cnt == 0) {
5077 monitor_printf(mon, "\n");
5078 }
5079 monitor_flush(mon);
5080 monitor_suspend(mon);
5081 } else {
5082 mon->suspend_cnt++;
5083 }
5084 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005085 break;
5086
Amit Shahb6b8df52009-10-07 18:31:16 +05305087 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005088 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5089 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005090 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005091 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005092 }
5093 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005094 break;
5095 }
ths86e94de2007-01-05 22:01:59 +00005096}
5097
aliguori76655d62009-03-06 20:27:37 +00005098
5099/*
5100 * Local variables:
5101 * c-indent-level: 4
5102 * c-basic-offset: 4
5103 * tab-width: 8
5104 * End:
5105 */
5106
aliguori731b0362009-03-05 23:01:42 +00005107void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005108{
aliguori731b0362009-03-05 23:01:42 +00005109 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005110 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005111
5112 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005113 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005114 is_first_init = 0;
5115 }
aliguori87127162009-03-05 23:01:29 +00005116
Anthony Liguori7267c092011-08-20 22:09:37 -05005117 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005118
aliguori87127162009-03-05 23:01:29 +00005119 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005120 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005121 if (flags & MONITOR_USE_READLINE) {
5122 mon->rs = readline_init(mon, monitor_find_completion);
5123 monitor_read_command(mon, 0);
5124 }
aliguori87127162009-03-05 23:01:29 +00005125
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005126 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005127 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005128 /* Control mode requires special handlers */
5129 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5130 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005131 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005132 } else {
5133 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5134 monitor_event, mon);
5135 }
aliguori87127162009-03-05 23:01:29 +00005136
Blue Swirl72cf2d42009-09-12 07:36:22 +00005137 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005138 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5139 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005140}
5141
aliguori376253e2009-03-05 23:01:23 +00005142static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005143{
aliguoribb5fc202009-03-05 23:01:15 +00005144 BlockDriverState *bs = opaque;
5145 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005146
aliguoribb5fc202009-03-05 23:01:15 +00005147 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005148 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005149 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005150 }
aliguori731b0362009-03-05 23:01:42 +00005151 if (mon->password_completion_cb)
5152 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005153
aliguori731b0362009-03-05 23:01:42 +00005154 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005155}
aliguoric0f4ce72009-03-05 23:01:01 +00005156
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005157int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5158 BlockDriverCompletionFunc *completion_cb,
5159 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005160{
aliguoricde76ee2009-03-05 23:01:51 +00005161 int err;
5162
aliguoribb5fc202009-03-05 23:01:15 +00005163 if (!bdrv_key_required(bs)) {
5164 if (completion_cb)
5165 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005166 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005167 }
aliguoric0f4ce72009-03-05 23:01:01 +00005168
Luiz Capitulino94171e12009-12-07 21:37:00 +01005169 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005170 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005171 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005172 }
5173
aliguori376253e2009-03-05 23:01:23 +00005174 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5175 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005176
aliguori731b0362009-03-05 23:01:42 +00005177 mon->password_completion_cb = completion_cb;
5178 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005179
aliguoricde76ee2009-03-05 23:01:51 +00005180 err = monitor_read_password(mon, bdrv_password_cb, bs);
5181
5182 if (err && completion_cb)
5183 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005184
5185 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005186}