blob: df0f622067a8a1f9912aeb7f540ad38b2edc5ec1 [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"
ths6a5bd302007-12-03 17:05:38 +000066
bellard9dc39cb2004-03-14 21:38:27 +000067//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000068//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000069
bellard9307c4c2004-04-04 12:57:25 +000070/*
71 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000072 *
bellard9307c4c2004-04-04 12:57:25 +000073 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000074 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000075 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010076 * 'O' option string of the form NAME=VALUE,...
77 * parsed according to QemuOptsList given by its name
78 * Example: 'device:O' uses qemu_device_opts.
79 * Restriction: only lists with empty desc are supported
80 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000081 * 'i' 32 bit integer
82 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010083 * 'M' just like 'l', except in user mode the value is
84 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020085 * 'o' octets (aka bytes)
86 * user mode accepts an optional T, t, G, g, M, m, K, k
87 * suffix, which multiplies the value by 2^40 for
88 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
89 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010090 * 'T' double
91 * user mode accepts an optional ms, us, ns suffix,
92 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000093 * '/' optional gdb-like print format (like "/10x")
94 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030095 * '?' optional type (for all types, except '/')
96 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010097 * 'b' boolean
98 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -030099 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000100 *
101 */
102
Adam Litke940cc302010-01-25 12:18:44 -0600103typedef struct MonitorCompletionData MonitorCompletionData;
104struct MonitorCompletionData {
105 Monitor *mon;
106 void (*user_print)(Monitor *mon, const QObject *data);
107};
108
Anthony Liguoric227f092009-10-01 16:12:16 -0500109typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000110 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000111 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000112 const char *params;
113 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300114 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300115 union {
116 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300117 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600118 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300119 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200120 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600121 int (*cmd_async)(Monitor *mon, const QDict *params,
122 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300123 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200124 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500125} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000126
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100127/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500128typedef struct mon_fd_t mon_fd_t;
129struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100130 char *name;
131 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500132 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100133};
134
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200135typedef struct MonitorControl {
136 QObject *id;
137 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200138 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200139} MonitorControl;
140
aliguori87127162009-03-05 23:01:29 +0000141struct Monitor {
142 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200143 int mux_out;
144 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000145 int flags;
146 int suspend_cnt;
147 uint8_t outbuf[1024];
148 int outbuf_index;
149 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200150 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000151 CPUState *mon_cpu;
152 BlockDriverCompletionFunc *password_completion_cb;
153 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200154#ifdef CONFIG_DEBUG_MONITOR
155 int print_calls_nr;
156#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200157 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500158 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000159 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000160};
161
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200162#ifdef CONFIG_DEBUG_MONITOR
163#define MON_DEBUG(fmt, ...) do { \
164 fprintf(stderr, "Monitor: "); \
165 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200166
167static inline void mon_print_count_inc(Monitor *mon)
168{
169 mon->print_calls_nr++;
170}
171
172static inline void mon_print_count_init(Monitor *mon)
173{
174 mon->print_calls_nr = 0;
175}
176
177static inline int mon_print_count_get(const Monitor *mon)
178{
179 return mon->print_calls_nr;
180}
181
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200182#else /* !CONFIG_DEBUG_MONITOR */
183#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200184static inline void mon_print_count_inc(Monitor *mon) { }
185static inline void mon_print_count_init(Monitor *mon) { }
186static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200187#endif /* CONFIG_DEBUG_MONITOR */
188
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300189/* QMP checker flags */
190#define QMP_ACCEPT_UNKNOWNS 1
191
Blue Swirl72cf2d42009-09-12 07:36:22 +0000192static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000193
Anthony Liguoric227f092009-10-01 16:12:16 -0500194static const mon_cmd_t mon_cmds[];
195static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000196
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300197static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300198static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300199
Markus Armbruster8631b602010-02-18 11:41:55 +0100200Monitor *cur_mon;
201Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000202
aliguori731b0362009-03-05 23:01:42 +0000203static void monitor_command_cb(Monitor *mon, const char *cmdline,
204 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000205
Luiz Capitulino09069b12010-02-04 18:10:06 -0200206static inline int qmp_cmd_mode(const Monitor *mon)
207{
208 return (mon->mc ? mon->mc->command_mode : 0);
209}
210
Luiz Capitulino418173c2009-11-26 22:58:51 -0200211/* Return true if in control mode, false otherwise */
212static inline int monitor_ctrl_mode(const Monitor *mon)
213{
214 return (mon->flags & MONITOR_USE_CONTROL);
215}
216
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100217/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
218int monitor_cur_is_qmp(void)
219{
220 return cur_mon && monitor_ctrl_mode(cur_mon);
221}
222
aliguori731b0362009-03-05 23:01:42 +0000223static void monitor_read_command(Monitor *mon, int show_prompt)
224{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200225 if (!mon->rs)
226 return;
227
aliguori731b0362009-03-05 23:01:42 +0000228 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
229 if (show_prompt)
230 readline_show_prompt(mon->rs);
231}
bellard6a00d602005-11-21 23:25:50 +0000232
aliguoricde76ee2009-03-05 23:01:51 +0000233static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
234 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000235{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100236 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100237 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100238 return -EINVAL;
239 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000240 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
241 /* prompt is printed on return from the command handler */
242 return 0;
243 } else {
244 monitor_printf(mon, "terminal does not support password prompting\n");
245 return -ENOTTY;
246 }
aliguoribb5fc202009-03-05 23:01:15 +0000247}
248
aliguori376253e2009-03-05 23:01:23 +0000249void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000250{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200251 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500252 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000253 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000254 }
255}
256
257/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000258static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000259{
ths60fe76f2007-12-16 03:02:09 +0000260 char c;
aliguori731b0362009-03-05 23:01:42 +0000261
bellard7e2515e2004-08-01 21:52:19 +0000262 for(;;) {
263 c = *str++;
264 if (c == '\0')
265 break;
bellard7ba12602006-07-14 20:26:42 +0000266 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000267 mon->outbuf[mon->outbuf_index++] = '\r';
268 mon->outbuf[mon->outbuf_index++] = c;
269 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
270 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000271 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000272 }
273}
274
aliguori376253e2009-03-05 23:01:23 +0000275void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000276{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200277 char buf[4096];
278
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200279 if (!mon)
280 return;
281
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200282 mon_print_count_inc(mon);
283
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200284 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200285 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200286 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200287
288 vsnprintf(buf, sizeof(buf), fmt, ap);
289 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000290}
291
aliguori376253e2009-03-05 23:01:23 +0000292void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000293{
294 va_list ap;
295 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000296 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000297 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000298}
299
aliguori376253e2009-03-05 23:01:23 +0000300void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000301{
302 int i;
303
304 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000305 switch (filename[i]) {
306 case ' ':
307 case '"':
308 case '\\':
309 monitor_printf(mon, "\\%c", filename[i]);
310 break;
311 case '\t':
312 monitor_printf(mon, "\\t");
313 break;
314 case '\r':
315 monitor_printf(mon, "\\r");
316 break;
317 case '\n':
318 monitor_printf(mon, "\\n");
319 break;
320 default:
321 monitor_printf(mon, "%c", filename[i]);
322 break;
323 }
thsfef30742006-12-22 14:11:32 +0000324 }
325}
326
Stefan Weil8b7968f2010-09-23 21:28:05 +0200327static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
328 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000329{
330 va_list ap;
331 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000332 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000333 va_end(ap);
334 return 0;
335}
336
Luiz Capitulino13c74252009-10-07 13:41:55 -0300337static void monitor_user_noop(Monitor *mon, const QObject *data) { }
338
Luiz Capitulino9e807212010-09-16 10:58:59 -0300339static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300340{
341 return cmd->user_print != NULL;
342}
343
Luiz Capitulino4903de02010-09-16 11:01:32 -0300344static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600345{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200346 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600347}
348
Luiz Capitulino8204a912009-11-18 23:05:31 -0200349static inline int monitor_has_error(const Monitor *mon)
350{
351 return mon->error != NULL;
352}
353
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200354static void monitor_json_emitter(Monitor *mon, const QObject *data)
355{
356 QString *json;
357
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200358 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
359 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200360 assert(json != NULL);
361
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362 qstring_append_chr(json, '\n');
363 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200364
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200365 QDECREF(json);
366}
367
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200368static void monitor_protocol_emitter(Monitor *mon, QObject *data)
369{
370 QDict *qmp;
371
372 qmp = qdict_new();
373
374 if (!monitor_has_error(mon)) {
375 /* success response */
376 if (data) {
377 qobject_incref(data);
378 qdict_put_obj(qmp, "return", data);
379 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200380 /* return an empty QDict by default */
381 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200382 }
383 } else {
384 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100385 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200386 qdict_put(qmp, "error", mon->error->error);
387 QINCREF(mon->error->error);
388 QDECREF(mon->error);
389 mon->error = NULL;
390 }
391
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200392 if (mon->mc->id) {
393 qdict_put_obj(qmp, "id", mon->mc->id);
394 mon->mc->id = NULL;
395 }
396
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200397 monitor_json_emitter(mon, QOBJECT(qmp));
398 QDECREF(qmp);
399}
400
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200401static void timestamp_put(QDict *qdict)
402{
403 int err;
404 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000405 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200406
Blue Swirld08d6f02009-12-04 18:06:39 +0000407 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200408 if (err < 0)
409 return;
410
411 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
412 "'microseconds': %" PRId64 " }",
413 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200414 qdict_put_obj(qdict, "timestamp", obj);
415}
416
417/**
418 * monitor_protocol_event(): Generate a Monitor event
419 *
420 * Event-specific data can be emitted through the (optional) 'data' parameter.
421 */
422void monitor_protocol_event(MonitorEvent event, QObject *data)
423{
424 QDict *qmp;
425 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600426 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200427
Blue Swirl242cd002009-12-04 18:05:45 +0000428 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200429
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200430 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000431 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200432 event_name = "SHUTDOWN";
433 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000434 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200435 event_name = "RESET";
436 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000437 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200438 event_name = "POWERDOWN";
439 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000440 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200441 event_name = "STOP";
442 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300443 case QEVENT_RESUME:
444 event_name = "RESUME";
445 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200446 case QEVENT_VNC_CONNECTED:
447 event_name = "VNC_CONNECTED";
448 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200449 case QEVENT_VNC_INITIALIZED:
450 event_name = "VNC_INITIALIZED";
451 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200452 case QEVENT_VNC_DISCONNECTED:
453 event_name = "VNC_DISCONNECTED";
454 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200455 case QEVENT_BLOCK_IO_ERROR:
456 event_name = "BLOCK_IO_ERROR";
457 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300458 case QEVENT_RTC_CHANGE:
459 event_name = "RTC_CHANGE";
460 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300461 case QEVENT_WATCHDOG:
462 event_name = "WATCHDOG";
463 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200464 case QEVENT_SPICE_CONNECTED:
465 event_name = "SPICE_CONNECTED";
466 break;
467 case QEVENT_SPICE_INITIALIZED:
468 event_name = "SPICE_INITIALIZED";
469 break;
470 case QEVENT_SPICE_DISCONNECTED:
471 event_name = "SPICE_DISCONNECTED";
472 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200473 default:
474 abort();
475 break;
476 }
477
478 qmp = qdict_new();
479 timestamp_put(qmp);
480 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200481 if (data) {
482 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200483 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200484 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200485
Adam Litkef039a562010-01-15 08:34:02 -0600486 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200487 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200488 monitor_json_emitter(mon, QOBJECT(qmp));
489 }
Adam Litkef039a562010-01-15 08:34:02 -0600490 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200491 QDECREF(qmp);
492}
493
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200494static int do_qmp_capabilities(Monitor *mon, const QDict *params,
495 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200496{
497 /* Will setup QMP capabilities in the future */
498 if (monitor_ctrl_mode(mon)) {
499 mon->mc->command_mode = 1;
500 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200501
502 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200503}
504
Luiz Capitulino0268d972010-10-22 10:08:02 -0200505static int mon_set_cpu(int cpu_index);
506static void handle_user_command(Monitor *mon, const char *cmdline);
507
508static int do_hmp_passthrough(Monitor *mon, const QDict *params,
509 QObject **ret_data)
510{
511 int ret = 0;
512 Monitor *old_mon, hmp;
513 CharDriverState mchar;
514
515 memset(&hmp, 0, sizeof(hmp));
516 qemu_chr_init_mem(&mchar);
517 hmp.chr = &mchar;
518
519 old_mon = cur_mon;
520 cur_mon = &hmp;
521
522 if (qdict_haskey(params, "cpu-index")) {
523 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
524 if (ret < 0) {
525 cur_mon = old_mon;
526 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
527 goto out;
528 }
529 }
530
531 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
532 cur_mon = old_mon;
533
534 if (qemu_chr_mem_osize(hmp.chr) > 0) {
535 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
536 }
537
538out:
539 qemu_chr_close_mem(hmp.chr);
540 return ret;
541}
542
bellard9dc39cb2004-03-14 21:38:27 +0000543static int compare_cmd(const char *name, const char *list)
544{
545 const char *p, *pstart;
546 int len;
547 len = strlen(name);
548 p = list;
549 for(;;) {
550 pstart = p;
551 p = strchr(p, '|');
552 if (!p)
553 p = pstart + strlen(pstart);
554 if ((p - pstart) == len && !memcmp(pstart, name, len))
555 return 1;
556 if (*p == '\0')
557 break;
558 p++;
559 }
560 return 0;
561}
562
Anthony Liguoric227f092009-10-01 16:12:16 -0500563static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000564 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000565{
Anthony Liguoric227f092009-10-01 16:12:16 -0500566 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000567
568 for(cmd = cmds; cmd->name != NULL; cmd++) {
569 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000570 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
571 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000572 }
573}
574
aliguori376253e2009-03-05 23:01:23 +0000575static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000576{
577 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000578 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000579 } else {
aliguori376253e2009-03-05 23:01:23 +0000580 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000581 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000582 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000583 monitor_printf(mon, "Log items (comma separated):\n");
584 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000585 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000586 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000587 }
588 }
bellard9dc39cb2004-03-14 21:38:27 +0000589 }
590}
591
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300592static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300593{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300594 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300595}
596
Lluísfc764102011-08-31 20:31:18 +0200597static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530598{
599 const char *tp_name = qdict_get_str(qdict, "name");
600 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200601 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000602
603 if (!ret) {
604 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
605 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530606}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100607
Lluís31965ae2011-08-31 20:31:24 +0200608#ifdef CONFIG_SIMPLE_TRACE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100609static void do_trace_file(Monitor *mon, const QDict *qdict)
610{
611 const char *op = qdict_get_try_str(qdict, "op");
612 const char *arg = qdict_get_try_str(qdict, "arg");
613
614 if (!op) {
615 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
616 } else if (!strcmp(op, "on")) {
617 st_set_trace_file_enabled(true);
618 } else if (!strcmp(op, "off")) {
619 st_set_trace_file_enabled(false);
620 } else if (!strcmp(op, "flush")) {
621 st_flush_trace_buffer();
622 } else if (!strcmp(op, "set")) {
623 if (arg) {
624 st_set_trace_file(arg);
625 }
626 } else {
627 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
628 help_cmd(mon, "trace-file");
629 }
630}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530631#endif
632
Adam Litke940cc302010-01-25 12:18:44 -0600633static void user_monitor_complete(void *opaque, QObject *ret_data)
634{
635 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
636
637 if (ret_data) {
638 data->user_print(data->mon, ret_data);
639 }
640 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500641 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600642}
643
644static void qmp_monitor_complete(void *opaque, QObject *ret_data)
645{
646 monitor_protocol_emitter(opaque, ret_data);
647}
648
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300649static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
650 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600651{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300652 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600653}
654
655static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
656{
657 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
658}
659
660static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
661 const QDict *params)
662{
663 int ret;
664
Anthony Liguori7267c092011-08-20 22:09:37 -0500665 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600666 cb_data->mon = mon;
667 cb_data->user_print = cmd->user_print;
668 monitor_suspend(mon);
669 ret = cmd->mhandler.cmd_async(mon, params,
670 user_monitor_complete, cb_data);
671 if (ret < 0) {
672 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500673 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600674 }
675}
676
677static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
678{
679 int ret;
680
Anthony Liguori7267c092011-08-20 22:09:37 -0500681 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600682 cb_data->mon = mon;
683 cb_data->user_print = cmd->user_print;
684 monitor_suspend(mon);
685 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
686 if (ret < 0) {
687 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500688 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600689 }
690}
691
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300692static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000693{
Anthony Liguoric227f092009-10-01 16:12:16 -0500694 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300695 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000696
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200697 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000698 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200699 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300700
701 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000702 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300703 break;
bellard9dc39cb2004-03-14 21:38:27 +0000704 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300705
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200706 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300707 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200708 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300709
Luiz Capitulino4903de02010-09-16 11:01:32 -0300710 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300711 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300712 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300713 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200714
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300715 cmd->mhandler.info_new(mon, &info_data);
716 if (info_data) {
717 cmd->user_print(mon, info_data);
718 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200719 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300720 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300721 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300722 }
723
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300724 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300725
726help:
727 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000728}
729
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200730static void do_info_version_print(Monitor *mon, const QObject *data)
731{
732 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300733 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200734
735 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300736 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200737
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300738 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
739 qdict_get_int(qemu, "major"),
740 qdict_get_int(qemu, "minor"),
741 qdict_get_int(qemu, "micro"),
742 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200743}
744
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300745static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000746{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300747 const char *version = QEMU_VERSION;
748 int major = 0, minor = 0, micro = 0;
749 char *tmp;
750
751 major = strtol(version, &tmp, 10);
752 tmp++;
753 minor = strtol(tmp, &tmp, 10);
754 tmp++;
755 micro = strtol(tmp, &tmp, 10);
756
757 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
758 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000759}
760
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200761static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000762{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200763 QDict *qdict;
764
765 qdict = qobject_to_qdict(data);
766 if (qdict_size(qdict) == 0) {
767 return;
768 }
769
770 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
771}
772
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200773static void do_info_name(Monitor *mon, QObject **ret_data)
774{
775 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
776 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000777}
778
Luiz Capitulino1a728672009-12-10 17:15:56 -0200779static QObject *get_cmd_dict(const char *name)
780{
781 const char *p;
782
783 /* Remove '|' from some commands */
784 p = strchr(name, '|');
785 if (p) {
786 p++;
787 } else {
788 p = name;
789 }
790
791 return qobject_from_jsonf("{ 'name': %s }", p);
792}
793
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200794static void do_info_commands(Monitor *mon, QObject **ret_data)
795{
796 QList *cmd_list;
797 const mon_cmd_t *cmd;
798
799 cmd_list = qlist_new();
800
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300801 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300802 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200803 }
804
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300805 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300806 char buf[128];
807 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
808 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200809 }
810
811 *ret_data = QOBJECT(cmd_list);
812}
813
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200814static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000815{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200816 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
817}
818
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200819static void do_info_uuid(Monitor *mon, QObject **ret_data)
820{
821 char uuid[64];
822
823 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000824 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
825 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
826 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
827 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200828 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000829}
830
bellard6a00d602005-11-21 23:25:50 +0000831/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000832static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000833{
834 CPUState *env;
835
836 for(env = first_cpu; env != NULL; env = env->next_cpu) {
837 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000838 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000839 return 0;
840 }
841 }
842 return -1;
843}
844
pbrook9596ebb2007-11-18 01:44:38 +0000845static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000846{
aliguori731b0362009-03-05 23:01:42 +0000847 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000848 mon_set_cpu(0);
849 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300850 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000851 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000852}
853
aliguori376253e2009-03-05 23:01:23 +0000854static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000855{
bellard6a00d602005-11-21 23:25:50 +0000856 CPUState *env;
857 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000858#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000859 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000860 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000861#else
aliguori376253e2009-03-05 23:01:23 +0000862 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000863 0);
bellard9307c4c2004-04-04 12:57:25 +0000864#endif
865}
866
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300867static void print_cpu_iter(QObject *obj, void *opaque)
868{
869 QDict *cpu;
870 int active = ' ';
871 Monitor *mon = opaque;
872
873 assert(qobject_type(obj) == QTYPE_QDICT);
874 cpu = qobject_to_qdict(obj);
875
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200876 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300877 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200878 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879
880 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
881
882#if defined(TARGET_I386)
883 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
884 (target_ulong) qdict_get_int(cpu, "pc"));
885#elif defined(TARGET_PPC)
886 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
887 (target_long) qdict_get_int(cpu, "nip"));
888#elif defined(TARGET_SPARC)
889 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
890 (target_long) qdict_get_int(cpu, "pc"));
891 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
892 (target_long) qdict_get_int(cpu, "npc"));
893#elif defined(TARGET_MIPS)
894 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
895 (target_long) qdict_get_int(cpu, "PC"));
896#endif
897
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200898 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300899 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200900 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300901
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100902 monitor_printf(mon, " thread_id=%" PRId64 " ",
903 qdict_get_int(cpu, "thread_id"));
904
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300905 monitor_printf(mon, "\n");
906}
907
908static void monitor_print_cpus(Monitor *mon, const QObject *data)
909{
910 QList *cpu_list;
911
912 assert(qobject_type(data) == QTYPE_QLIST);
913 cpu_list = qobject_to_qlist(data);
914 qlist_iter(cpu_list, print_cpu_iter, mon);
915}
916
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000918{
919 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300920 QList *cpu_list;
921
922 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000923
924 /* just to set the default cpu if not already done */
925 mon_get_cpu();
926
927 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200928 QDict *cpu;
929 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300930
Avi Kivity4c0960c2009-08-17 23:19:53 +0300931 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300932
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200933 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
934 env->cpu_index, env == mon->mon_cpu,
935 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200936
937 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300938
bellard6a00d602005-11-21 23:25:50 +0000939#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300940 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000941#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300942 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000943#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300944 qdict_put(cpu, "pc", qint_from_int(env->pc));
945 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000946#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300947 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000948#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100949 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300950
951 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000952 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300953
954 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000955}
956
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200957static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000958{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300959 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200960 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100961 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
962 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200963 return -1;
964 }
965 return 0;
bellard6a00d602005-11-21 23:25:50 +0000966}
967
aliguori376253e2009-03-05 23:01:23 +0000968static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000969{
aliguori376253e2009-03-05 23:01:23 +0000970 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000971}
972
aliguori376253e2009-03-05 23:01:23 +0000973static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000974{
975 int i;
bellard7e2515e2004-08-01 21:52:19 +0000976 const char *str;
ths3b46e622007-09-17 08:09:54 +0000977
aliguoricde76ee2009-03-05 23:01:51 +0000978 if (!mon->rs)
979 return;
bellard7e2515e2004-08-01 21:52:19 +0000980 i = 0;
981 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000982 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000983 if (!str)
984 break;
aliguori376253e2009-03-05 23:01:23 +0000985 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000986 i++;
bellardaa455482004-04-04 13:07:25 +0000987 }
988}
989
j_mayer76a66252007-03-07 08:32:30 +0000990#if defined(TARGET_PPC)
991/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000992static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000993{
994 CPUState *env;
995
996 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000997 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000998}
999#endif
1000
Lluís6d8a7642011-08-31 20:30:43 +02001001#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301002static void do_info_trace(Monitor *mon)
1003{
1004 st_print_trace((FILE *)mon, &monitor_fprintf);
1005}
Lluís31965ae2011-08-31 20:31:24 +02001006#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05301007
Lluísfc764102011-08-31 20:31:18 +02001008static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301009{
Lluísfc764102011-08-31 20:31:18 +02001010 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301011}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301012
Luiz Capitulinob223f352009-10-07 13:41:56 -03001013/**
1014 * do_quit(): Quit QEMU execution
1015 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001016static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001017{
Luiz Capitulino39b59d22010-05-11 18:08:20 -03001018 monitor_suspend(mon);
1019 no_shutdown = 0;
1020 qemu_system_shutdown_request();
1021
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001022 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001023}
1024
Jes Sorensen821601e2011-03-16 13:33:36 +01001025#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001026static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001027{
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001028 if (!password || !password[0]) {
1029 if (vnc_display_disable_login(NULL)) {
1030 qerror_report(QERR_SET_PASSWD_FAILED);
1031 return -1;
1032 }
1033 return 0;
1034 }
1035
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001036 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001037 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001038 return -1;
1039 }
aliguoribb5fc202009-03-05 23:01:15 +00001040
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001041 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001042}
1043
1044static void change_vnc_password_cb(Monitor *mon, const char *password,
1045 void *opaque)
1046{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001047 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001048 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001049}
1050
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001051static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001052{
ths70848512007-08-25 01:37:05 +00001053 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001054 strcmp(target, "password") == 0) {
1055 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001056 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001057 strncpy(password, arg, sizeof(password));
1058 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001059 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001060 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001061 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001062 }
ths70848512007-08-25 01:37:05 +00001063 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001064 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001065 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001066 return -1;
1067 }
ths70848512007-08-25 01:37:05 +00001068 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001069
1070 return 0;
thse25a5822007-08-25 01:36:20 +00001071}
Jes Sorensen821601e2011-03-16 13:33:36 +01001072#else
1073static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1074{
1075 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1076 return -ENODEV;
1077}
1078#endif
thse25a5822007-08-25 01:36:20 +00001079
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001080/**
1081 * do_change(): Change a removable medium, or VNC configuration
1082 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001083static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001084{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001085 const char *device = qdict_get_str(qdict, "device");
1086 const char *target = qdict_get_str(qdict, "target");
1087 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001088 int ret;
1089
thse25a5822007-08-25 01:36:20 +00001090 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001091 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001092 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001093 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001094 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001095
1096 return ret;
thse25a5822007-08-25 01:36:20 +00001097}
1098
Gerd Hoffmann75721502010-10-07 12:22:54 +02001099static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1100{
1101 const char *protocol = qdict_get_str(qdict, "protocol");
1102 const char *password = qdict_get_str(qdict, "password");
1103 const char *connected = qdict_get_try_str(qdict, "connected");
1104 int disconnect_if_connected = 0;
1105 int fail_if_connected = 0;
1106 int rc;
1107
1108 if (connected) {
1109 if (strcmp(connected, "fail") == 0) {
1110 fail_if_connected = 1;
1111 } else if (strcmp(connected, "disconnect") == 0) {
1112 disconnect_if_connected = 1;
1113 } else if (strcmp(connected, "keep") == 0) {
1114 /* nothing */
1115 } else {
1116 qerror_report(QERR_INVALID_PARAMETER, "connected");
1117 return -1;
1118 }
1119 }
1120
1121 if (strcmp(protocol, "spice") == 0) {
1122 if (!using_spice) {
1123 /* correct one? spice isn't a device ,,, */
1124 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1125 return -1;
1126 }
1127 rc = qemu_spice_set_passwd(password, fail_if_connected,
1128 disconnect_if_connected);
1129 if (rc != 0) {
1130 qerror_report(QERR_SET_PASSWD_FAILED);
1131 return -1;
1132 }
1133 return 0;
1134 }
1135
1136 if (strcmp(protocol, "vnc") == 0) {
1137 if (fail_if_connected || disconnect_if_connected) {
1138 /* vnc supports "connected=keep" only */
1139 qerror_report(QERR_INVALID_PARAMETER, "connected");
1140 return -1;
1141 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001142 /* Note that setting an empty password will not disable login through
1143 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001144 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001145 }
1146
1147 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1148 return -1;
1149}
1150
1151static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1152{
1153 const char *protocol = qdict_get_str(qdict, "protocol");
1154 const char *whenstr = qdict_get_str(qdict, "time");
1155 time_t when;
1156 int rc;
1157
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001158 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001159 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001160 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001161 when = TIME_MAX;
1162 } else if (whenstr[0] == '+') {
1163 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1164 } else {
1165 when = strtoull(whenstr, NULL, 10);
1166 }
1167
1168 if (strcmp(protocol, "spice") == 0) {
1169 if (!using_spice) {
1170 /* correct one? spice isn't a device ,,, */
1171 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1172 return -1;
1173 }
1174 rc = qemu_spice_set_pw_expire(when);
1175 if (rc != 0) {
1176 qerror_report(QERR_SET_PASSWD_FAILED);
1177 return -1;
1178 }
1179 return 0;
1180 }
1181
1182 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001183 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001184 }
1185
1186 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1187 return -1;
1188}
1189
Daniel P. Berrange13661082011-06-23 13:31:42 +01001190static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1191{
1192 const char *protocol = qdict_get_str(qdict, "protocol");
1193 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001194 CharDriverState *s;
1195
1196 if (strcmp(protocol, "spice") == 0) {
1197 if (!using_spice) {
1198 /* correct one? spice isn't a device ,,, */
1199 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1200 return -1;
1201 }
1202 qerror_report(QERR_ADD_CLIENT_FAILED);
1203 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001204#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001205 } else if (strcmp(protocol, "vnc") == 0) {
1206 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001207 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001208 vnc_display_add_client(NULL, fd, skipauth);
1209 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001210#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001211 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1212 int fd = monitor_get_fd(mon, fdname);
1213 if (qemu_chr_add_client(s, fd) < 0) {
1214 qerror_report(QERR_ADD_CLIENT_FAILED);
1215 return -1;
1216 }
1217 return 0;
1218 }
1219
1220 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1221 return -1;
1222}
1223
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001224static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1225{
1226 const char *protocol = qdict_get_str(qdict, "protocol");
1227 const char *hostname = qdict_get_str(qdict, "hostname");
1228 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1229 int port = qdict_get_try_int(qdict, "port", -1);
1230 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1231 int ret;
1232
1233 if (strcmp(protocol, "spice") == 0) {
1234 if (!using_spice) {
1235 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1236 return -1;
1237 }
1238
1239 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1240 if (ret != 0) {
1241 qerror_report(QERR_UNDEFINED_ERROR);
1242 return -1;
1243 }
1244 return 0;
1245 }
1246
1247 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1248 return -1;
1249}
1250
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001251static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001252{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001253 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001254 return 0;
bellard59a983b2004-03-17 23:17:16 +00001255}
1256
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001257static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001258{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001259 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001260}
1261
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001262static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001263{
1264 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001265 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001266
bellard9307c4c2004-04-04 12:57:25 +00001267 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001268 mask = 0;
1269 } else {
bellard9307c4c2004-04-04 12:57:25 +00001270 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001271 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001272 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001273 return;
1274 }
1275 }
1276 cpu_set_log(mask);
1277}
1278
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001279static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001280{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001281 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001282 if (!option || !strcmp(option, "on")) {
1283 singlestep = 1;
1284 } else if (!strcmp(option, "off")) {
1285 singlestep = 0;
1286 } else {
1287 monitor_printf(mon, "unexpected option %s\n", option);
1288 }
1289}
1290
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001291/**
1292 * do_stop(): Stop VM execution
1293 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001294static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001295{
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001296 vm_stop(VMSTOP_USER);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001297 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001298}
1299
aliguoribb5fc202009-03-05 23:01:15 +00001300static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001301
aliguori376253e2009-03-05 23:01:23 +00001302struct bdrv_iterate_context {
1303 Monitor *mon;
1304 int err;
1305};
aliguoric0f4ce72009-03-05 23:01:01 +00001306
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001307/**
1308 * do_cont(): Resume emulation.
1309 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001310static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001311{
1312 struct bdrv_iterate_context context = { mon, 0 };
1313
Amit Shah8e848652010-07-27 15:49:19 +05301314 if (incoming_expected) {
1315 qerror_report(QERR_MIGRATION_EXPECTED);
1316 return -1;
1317 }
aliguori376253e2009-03-05 23:01:23 +00001318 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001319 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001320 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001321 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001322 return 0;
1323 } else {
1324 return -1;
1325 }
bellard8a7ddc32004-03-31 19:00:16 +00001326}
1327
aliguoribb5fc202009-03-05 23:01:15 +00001328static void bdrv_key_cb(void *opaque, int err)
1329{
aliguori376253e2009-03-05 23:01:23 +00001330 Monitor *mon = opaque;
1331
aliguoribb5fc202009-03-05 23:01:15 +00001332 /* another key was set successfully, retry to continue */
1333 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001334 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001335}
1336
1337static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1338{
aliguori376253e2009-03-05 23:01:23 +00001339 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001340
aliguori376253e2009-03-05 23:01:23 +00001341 if (!context->err && bdrv_key_required(bs)) {
1342 context->err = -EBUSY;
1343 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1344 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001345 }
1346}
1347
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001348static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001349{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001350 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001351 if (!device)
1352 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1353 if (gdbserver_start(device) < 0) {
1354 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1355 device);
1356 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001357 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001358 } else {
aliguori59030a82009-04-05 18:43:41 +00001359 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1360 device);
bellard8a7ddc32004-03-31 19:00:16 +00001361 }
1362}
1363
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001364static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001365{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001366 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001367 if (select_watchdog_action(action) == -1) {
1368 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1369 }
1370}
1371
aliguori376253e2009-03-05 23:01:23 +00001372static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001373{
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001375 switch(c) {
1376 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001378 break;
1379 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001381 break;
1382 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 default:
1389 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001391 } else {
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001393 }
1394 break;
1395 }
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001397}
1398
aliguori376253e2009-03-05 23:01:23 +00001399static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001400 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001401{
bellard6a00d602005-11-21 23:25:50 +00001402 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001403 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001404 uint8_t buf[16];
1405 uint64_t v;
1406
1407 if (format == 'i') {
1408 int flags;
1409 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001410 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001411#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001412 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001413 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001414 } else if (wsize == 4) {
1415 flags = 0;
1416 } else {
bellard6a15fd12006-04-12 21:07:07 +00001417 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001418 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001419 if (env) {
1420#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001421 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001422 (env->segs[R_CS].flags & DESC_L_MASK))
1423 flags = 2;
1424 else
1425#endif
1426 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1427 flags = 1;
1428 }
bellard4c27ba22004-04-25 18:05:08 +00001429 }
1430#endif
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001432 return;
1433 }
1434
1435 len = wsize * count;
1436 if (wsize == 1)
1437 line_size = 8;
1438 else
1439 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001440 max_digits = 0;
1441
1442 switch(format) {
1443 case 'o':
1444 max_digits = (wsize * 8 + 2) / 3;
1445 break;
1446 default:
1447 case 'x':
1448 max_digits = (wsize * 8) / 4;
1449 break;
1450 case 'u':
1451 case 'd':
1452 max_digits = (wsize * 8 * 10 + 32) / 33;
1453 break;
1454 case 'c':
1455 wsize = 1;
1456 break;
1457 }
1458
1459 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001460 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001462 else
aliguori376253e2009-03-05 23:01:23 +00001463 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001464 l = len;
1465 if (l > line_size)
1466 l = line_size;
1467 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001468 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001469 } else {
bellard6a00d602005-11-21 23:25:50 +00001470 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001471 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001472 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001473 break;
1474 }
bellard9307c4c2004-04-04 12:57:25 +00001475 }
ths5fafdf22007-09-16 21:08:06 +00001476 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001477 while (i < l) {
1478 switch(wsize) {
1479 default:
1480 case 1:
1481 v = ldub_raw(buf + i);
1482 break;
1483 case 2:
1484 v = lduw_raw(buf + i);
1485 break;
1486 case 4:
bellard92a31b12005-02-10 22:00:52 +00001487 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001488 break;
1489 case 8:
1490 v = ldq_raw(buf + i);
1491 break;
1492 }
aliguori376253e2009-03-05 23:01:23 +00001493 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001494 switch(format) {
1495 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001497 break;
1498 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001499 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001500 break;
1501 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001503 break;
1504 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001506 break;
1507 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001509 break;
1510 }
1511 i += wsize;
1512 }
aliguori376253e2009-03-05 23:01:23 +00001513 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001514 addr += l;
1515 len -= l;
1516 }
1517}
1518
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001519static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001520{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001521 int count = qdict_get_int(qdict, "count");
1522 int format = qdict_get_int(qdict, "format");
1523 int size = qdict_get_int(qdict, "size");
1524 target_long addr = qdict_get_int(qdict, "addr");
1525
aliguori376253e2009-03-05 23:01:23 +00001526 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001527}
1528
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001529static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001530{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001531 int count = qdict_get_int(qdict, "count");
1532 int format = qdict_get_int(qdict, "format");
1533 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001534 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001535
aliguori376253e2009-03-05 23:01:23 +00001536 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001537}
1538
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001539static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001540{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001541 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001542 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001543
blueswir17743e582007-09-24 18:39:04 +00001544#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001545 switch(format) {
1546 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001547 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001548 break;
1549 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001550 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001551 break;
1552 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001553 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001554 break;
1555 default:
1556 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001557 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001558 break;
1559 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001560 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001561 break;
1562 }
bellard92a31b12005-02-10 22:00:52 +00001563#else
1564 switch(format) {
1565 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001567 break;
1568 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001569 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001570 break;
1571 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001572 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001573 break;
1574 default:
1575 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001577 break;
1578 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001579 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001580 break;
1581 }
1582#endif
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001584}
1585
Luiz Capitulino98696222010-02-10 23:49:58 -02001586static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001587{
1588 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001589 uint32_t size = qdict_get_int(qdict, "size");
1590 const char *filename = qdict_get_str(qdict, "filename");
1591 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001592 uint32_t l;
1593 CPUState *env;
1594 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001595 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001596
1597 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001598
1599 f = fopen(filename, "wb");
1600 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001601 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001602 return -1;
bellardb371dc52007-01-03 15:20:39 +00001603 }
1604 while (size != 0) {
1605 l = sizeof(buf);
1606 if (l > size)
1607 l = size;
1608 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001609 if (fwrite(buf, 1, l, f) != l) {
1610 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1611 goto exit;
1612 }
bellardb371dc52007-01-03 15:20:39 +00001613 addr += l;
1614 size -= l;
1615 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001616
1617 ret = 0;
1618
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001619exit:
bellardb371dc52007-01-03 15:20:39 +00001620 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001621 return ret;
bellardb371dc52007-01-03 15:20:39 +00001622}
1623
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001624static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001625 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001626{
1627 FILE *f;
1628 uint32_t l;
1629 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001630 uint32_t size = qdict_get_int(qdict, "size");
1631 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001632 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001633 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001634
1635 f = fopen(filename, "wb");
1636 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001637 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001638 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001639 }
1640 while (size != 0) {
1641 l = sizeof(buf);
1642 if (l > size)
1643 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001644 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001645 if (fwrite(buf, 1, l, f) != l) {
1646 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1647 goto exit;
1648 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001649 fflush(f);
1650 addr += l;
1651 size -= l;
1652 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001653
1654 ret = 0;
1655
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001656exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001657 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001658 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001659}
1660
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001661static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001662{
1663 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001664 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001665 uint32_t start = qdict_get_int(qdict, "start");
1666 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001667
1668 sum = 0;
1669 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001670 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001671 /* BSD sum algorithm ('sum' Unix command) */
1672 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001673 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001674 }
aliguori376253e2009-03-05 23:01:23 +00001675 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001676}
1677
bellarda3a91a32004-06-04 11:06:21 +00001678typedef struct {
1679 int keycode;
1680 const char *name;
1681} KeyDef;
1682
1683static const KeyDef key_defs[] = {
1684 { 0x2a, "shift" },
1685 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001686
bellarda3a91a32004-06-04 11:06:21 +00001687 { 0x38, "alt" },
1688 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001689 { 0x64, "altgr" },
1690 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001691 { 0x1d, "ctrl" },
1692 { 0x9d, "ctrl_r" },
1693
1694 { 0xdd, "menu" },
1695
1696 { 0x01, "esc" },
1697
1698 { 0x02, "1" },
1699 { 0x03, "2" },
1700 { 0x04, "3" },
1701 { 0x05, "4" },
1702 { 0x06, "5" },
1703 { 0x07, "6" },
1704 { 0x08, "7" },
1705 { 0x09, "8" },
1706 { 0x0a, "9" },
1707 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001708 { 0x0c, "minus" },
1709 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001710 { 0x0e, "backspace" },
1711
1712 { 0x0f, "tab" },
1713 { 0x10, "q" },
1714 { 0x11, "w" },
1715 { 0x12, "e" },
1716 { 0x13, "r" },
1717 { 0x14, "t" },
1718 { 0x15, "y" },
1719 { 0x16, "u" },
1720 { 0x17, "i" },
1721 { 0x18, "o" },
1722 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001723 { 0x1a, "bracket_left" },
1724 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001725 { 0x1c, "ret" },
1726
1727 { 0x1e, "a" },
1728 { 0x1f, "s" },
1729 { 0x20, "d" },
1730 { 0x21, "f" },
1731 { 0x22, "g" },
1732 { 0x23, "h" },
1733 { 0x24, "j" },
1734 { 0x25, "k" },
1735 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001736 { 0x27, "semicolon" },
1737 { 0x28, "apostrophe" },
1738 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001739
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001740 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001741 { 0x2c, "z" },
1742 { 0x2d, "x" },
1743 { 0x2e, "c" },
1744 { 0x2f, "v" },
1745 { 0x30, "b" },
1746 { 0x31, "n" },
1747 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001748 { 0x33, "comma" },
1749 { 0x34, "dot" },
1750 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001751
balrog4d3b6f62008-02-10 16:33:14 +00001752 { 0x37, "asterisk" },
1753
bellarda3a91a32004-06-04 11:06:21 +00001754 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001755 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001756 { 0x3b, "f1" },
1757 { 0x3c, "f2" },
1758 { 0x3d, "f3" },
1759 { 0x3e, "f4" },
1760 { 0x3f, "f5" },
1761 { 0x40, "f6" },
1762 { 0x41, "f7" },
1763 { 0x42, "f8" },
1764 { 0x43, "f9" },
1765 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001766 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001767 { 0x46, "scroll_lock" },
1768
bellard64866c32006-05-07 18:03:31 +00001769 { 0xb5, "kp_divide" },
1770 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001771 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001772 { 0x4e, "kp_add" },
1773 { 0x9c, "kp_enter" },
1774 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001775 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001776
1777 { 0x52, "kp_0" },
1778 { 0x4f, "kp_1" },
1779 { 0x50, "kp_2" },
1780 { 0x51, "kp_3" },
1781 { 0x4b, "kp_4" },
1782 { 0x4c, "kp_5" },
1783 { 0x4d, "kp_6" },
1784 { 0x47, "kp_7" },
1785 { 0x48, "kp_8" },
1786 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001787
bellarda3a91a32004-06-04 11:06:21 +00001788 { 0x56, "<" },
1789
1790 { 0x57, "f11" },
1791 { 0x58, "f12" },
1792
1793 { 0xb7, "print" },
1794
1795 { 0xc7, "home" },
1796 { 0xc9, "pgup" },
1797 { 0xd1, "pgdn" },
1798 { 0xcf, "end" },
1799
1800 { 0xcb, "left" },
1801 { 0xc8, "up" },
1802 { 0xd0, "down" },
1803 { 0xcd, "right" },
1804
1805 { 0xd2, "insert" },
1806 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001807#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1808 { 0xf0, "stop" },
1809 { 0xf1, "again" },
1810 { 0xf2, "props" },
1811 { 0xf3, "undo" },
1812 { 0xf4, "front" },
1813 { 0xf5, "copy" },
1814 { 0xf6, "open" },
1815 { 0xf7, "paste" },
1816 { 0xf8, "find" },
1817 { 0xf9, "cut" },
1818 { 0xfa, "lf" },
1819 { 0xfb, "help" },
1820 { 0xfc, "meta_l" },
1821 { 0xfd, "meta_r" },
1822 { 0xfe, "compose" },
1823#endif
bellarda3a91a32004-06-04 11:06:21 +00001824 { 0, NULL },
1825};
1826
1827static int get_keycode(const char *key)
1828{
1829 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001830 char *endp;
1831 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001832
1833 for(p = key_defs; p->name != NULL; p++) {
1834 if (!strcmp(key, p->name))
1835 return p->keycode;
1836 }
bellard64866c32006-05-07 18:03:31 +00001837 if (strstart(key, "0x", NULL)) {
1838 ret = strtoul(key, &endp, 0);
1839 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1840 return ret;
1841 }
bellarda3a91a32004-06-04 11:06:21 +00001842 return -1;
1843}
1844
balrogc8256f92008-06-08 22:45:01 +00001845#define MAX_KEYCODES 16
1846static uint8_t keycodes[MAX_KEYCODES];
1847static int nb_pending_keycodes;
1848static QEMUTimer *key_timer;
1849
1850static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001851{
balrogc8256f92008-06-08 22:45:01 +00001852 int keycode;
1853
1854 while (nb_pending_keycodes > 0) {
1855 nb_pending_keycodes--;
1856 keycode = keycodes[nb_pending_keycodes];
1857 if (keycode & 0x80)
1858 kbd_put_keycode(0xe0);
1859 kbd_put_keycode(keycode | 0x80);
1860 }
1861}
1862
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001863static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001864{
balrog3401c0d2008-06-04 10:05:59 +00001865 char keyname_buf[16];
1866 char *separator;
1867 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001868 const char *string = qdict_get_str(qdict, "string");
1869 int has_hold_time = qdict_haskey(qdict, "hold_time");
1870 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001871
balrogc8256f92008-06-08 22:45:01 +00001872 if (nb_pending_keycodes > 0) {
1873 qemu_del_timer(key_timer);
1874 release_keys(NULL);
1875 }
1876 if (!has_hold_time)
1877 hold_time = 100;
1878 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001879 while (1) {
1880 separator = strchr(string, '-');
1881 keyname_len = separator ? separator - string : strlen(string);
1882 if (keyname_len > 0) {
1883 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1884 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001885 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001886 return;
bellarda3a91a32004-06-04 11:06:21 +00001887 }
balrogc8256f92008-06-08 22:45:01 +00001888 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001889 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001890 return;
1891 }
1892 keyname_buf[keyname_len] = 0;
1893 keycode = get_keycode(keyname_buf);
1894 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001895 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001896 return;
1897 }
balrogc8256f92008-06-08 22:45:01 +00001898 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001899 }
balrog3401c0d2008-06-04 10:05:59 +00001900 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001901 break;
balrog3401c0d2008-06-04 10:05:59 +00001902 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001903 }
balrogc8256f92008-06-08 22:45:01 +00001904 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001905 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001906 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001907 keycode = keycodes[i];
1908 if (keycode & 0x80)
1909 kbd_put_keycode(0xe0);
1910 kbd_put_keycode(keycode & 0x7f);
1911 }
balrogc8256f92008-06-08 22:45:01 +00001912 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001913 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001914 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001915}
1916
bellard13224a82006-07-14 22:03:35 +00001917static int mouse_button_state;
1918
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001919static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001920{
1921 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001922 const char *dx_str = qdict_get_str(qdict, "dx_str");
1923 const char *dy_str = qdict_get_str(qdict, "dy_str");
1924 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001925 dx = strtol(dx_str, NULL, 0);
1926 dy = strtol(dy_str, NULL, 0);
1927 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001928 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001929 dz = strtol(dz_str, NULL, 0);
1930 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1931}
1932
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001933static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001934{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001935 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001936 mouse_button_state = button_state;
1937 kbd_mouse_event(0, 0, 0, mouse_button_state);
1938}
1939
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001940static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001941{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001942 int size = qdict_get_int(qdict, "size");
1943 int addr = qdict_get_int(qdict, "addr");
1944 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001945 uint32_t val;
1946 int suffix;
1947
1948 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001949 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001950 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001951 addr++;
1952 }
1953 addr &= 0xffff;
1954
1955 switch(size) {
1956 default:
1957 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001958 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001959 suffix = 'b';
1960 break;
1961 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001962 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001963 suffix = 'w';
1964 break;
1965 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001966 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001967 suffix = 'l';
1968 break;
1969 }
aliguori376253e2009-03-05 23:01:23 +00001970 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1971 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001972}
bellarda3a91a32004-06-04 11:06:21 +00001973
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001974static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001975{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001976 int size = qdict_get_int(qdict, "size");
1977 int addr = qdict_get_int(qdict, "addr");
1978 int val = qdict_get_int(qdict, "val");
1979
Jan Kiszkaf1147842009-07-14 10:20:11 +02001980 addr &= IOPORTS_MASK;
1981
1982 switch (size) {
1983 default:
1984 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001985 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001986 break;
1987 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001988 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001989 break;
1990 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001991 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001992 break;
1993 }
1994}
1995
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001996static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001997{
1998 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001999 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00002000
Jan Kiszka76e30d02009-07-02 00:19:02 +02002001 res = qemu_boot_set(bootdevice);
2002 if (res == 0) {
2003 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
2004 } else if (res > 0) {
2005 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00002006 } else {
aliguori376253e2009-03-05 23:01:23 +00002007 monitor_printf(mon, "no function defined to set boot device list for "
2008 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00002009 }
2010}
2011
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03002012/**
2013 * do_system_reset(): Issue a machine reset
2014 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002015static int do_system_reset(Monitor *mon, const QDict *qdict,
2016 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00002017{
2018 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002019 return 0;
bellarde4f90822004-06-20 12:35:44 +00002020}
2021
Luiz Capitulino43076662009-10-07 13:41:59 -03002022/**
2023 * do_system_powerdown(): Issue a machine powerdown
2024 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002025static int do_system_powerdown(Monitor *mon, const QDict *qdict,
2026 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00002027{
2028 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002029 return 0;
bellard34751872005-07-02 14:31:34 +00002030}
2031
bellardb86bda52004-09-18 19:32:46 +00002032#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00002033static void print_pte(Monitor *mon, target_phys_addr_t addr,
2034 target_phys_addr_t pte,
2035 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00002036{
Blue Swirld65aaf32010-12-11 18:56:24 +00002037#ifdef TARGET_X86_64
2038 if (addr & (1ULL << 47)) {
2039 addr |= -1LL << 48;
2040 }
2041#endif
2042 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
2043 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002044 addr,
2045 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00002046 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00002047 pte & PG_GLOBAL_MASK ? 'G' : '-',
2048 pte & PG_PSE_MASK ? 'P' : '-',
2049 pte & PG_DIRTY_MASK ? 'D' : '-',
2050 pte & PG_ACCESSED_MASK ? 'A' : '-',
2051 pte & PG_PCD_MASK ? 'C' : '-',
2052 pte & PG_PWT_MASK ? 'T' : '-',
2053 pte & PG_USER_MASK ? 'U' : '-',
2054 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00002055}
2056
Blue Swirld65aaf32010-12-11 18:56:24 +00002057static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002058{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002059 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00002060 uint32_t pgd, pde, pte;
2061
bellardb86bda52004-09-18 19:32:46 +00002062 pgd = env->cr[3] & ~0xfff;
2063 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002064 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002065 pde = le32_to_cpu(pde);
2066 if (pde & PG_PRESENT_MASK) {
2067 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00002068 /* 4M pages */
2069 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00002070 } else {
2071 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002072 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002073 pte = le32_to_cpu(pte);
2074 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002075 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002076 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002077 ~0xfff);
2078 }
2079 }
2080 }
2081 }
2082 }
2083}
2084
Blue Swirld65aaf32010-12-11 18:56:24 +00002085static void tlb_info_pae32(Monitor *mon, CPUState *env)
2086{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002087 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002088 uint64_t pdpe, pde, pte;
2089 uint64_t pdp_addr, pd_addr, pt_addr;
2090
2091 pdp_addr = env->cr[3] & ~0x1f;
2092 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002093 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002094 pdpe = le64_to_cpu(pdpe);
2095 if (pdpe & PG_PRESENT_MASK) {
2096 pd_addr = pdpe & 0x3fffffffff000ULL;
2097 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002098 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002099 pde = le64_to_cpu(pde);
2100 if (pde & PG_PRESENT_MASK) {
2101 if (pde & PG_PSE_MASK) {
2102 /* 2M pages with PAE, CR4.PSE is ignored */
2103 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2104 ~((target_phys_addr_t)(1 << 20) - 1));
2105 } else {
2106 pt_addr = pde & 0x3fffffffff000ULL;
2107 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002108 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002109 pte = le64_to_cpu(pte);
2110 if (pte & PG_PRESENT_MASK) {
2111 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2112 + (l3 << 12),
2113 pte & ~PG_PSE_MASK,
2114 ~(target_phys_addr_t)0xfff);
2115 }
2116 }
2117 }
2118 }
2119 }
2120 }
2121 }
2122}
2123
2124#ifdef TARGET_X86_64
2125static void tlb_info_64(Monitor *mon, CPUState *env)
2126{
2127 uint64_t l1, l2, l3, l4;
2128 uint64_t pml4e, pdpe, pde, pte;
2129 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2130
2131 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2132 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002133 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002134 pml4e = le64_to_cpu(pml4e);
2135 if (pml4e & PG_PRESENT_MASK) {
2136 pdp_addr = pml4e & 0x3fffffffff000ULL;
2137 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002138 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002139 pdpe = le64_to_cpu(pdpe);
2140 if (pdpe & PG_PRESENT_MASK) {
2141 if (pdpe & PG_PSE_MASK) {
2142 /* 1G pages, CR4.PSE is ignored */
2143 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2144 0x3ffffc0000000ULL);
2145 } else {
2146 pd_addr = pdpe & 0x3fffffffff000ULL;
2147 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002148 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002149 pde = le64_to_cpu(pde);
2150 if (pde & PG_PRESENT_MASK) {
2151 if (pde & PG_PSE_MASK) {
2152 /* 2M pages, CR4.PSE is ignored */
2153 print_pte(mon, (l1 << 39) + (l2 << 30) +
2154 (l3 << 21), pde,
2155 0x3ffffffe00000ULL);
2156 } else {
2157 pt_addr = pde & 0x3fffffffff000ULL;
2158 for (l4 = 0; l4 < 512; l4++) {
2159 cpu_physical_memory_read(pt_addr
2160 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002161 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002162 pte = le64_to_cpu(pte);
2163 if (pte & PG_PRESENT_MASK) {
2164 print_pte(mon, (l1 << 39) +
2165 (l2 << 30) +
2166 (l3 << 21) + (l4 << 12),
2167 pte & ~PG_PSE_MASK,
2168 0x3fffffffff000ULL);
2169 }
2170 }
2171 }
2172 }
2173 }
2174 }
2175 }
2176 }
2177 }
2178 }
2179}
2180#endif
2181
2182static void tlb_info(Monitor *mon)
2183{
2184 CPUState *env;
2185
2186 env = mon_get_cpu();
2187
2188 if (!(env->cr[0] & CR0_PG_MASK)) {
2189 monitor_printf(mon, "PG disabled\n");
2190 return;
2191 }
2192 if (env->cr[4] & CR4_PAE_MASK) {
2193#ifdef TARGET_X86_64
2194 if (env->hflags & HF_LMA_MASK) {
2195 tlb_info_64(mon, env);
2196 } else
2197#endif
2198 {
2199 tlb_info_pae32(mon, env);
2200 }
2201 } else {
2202 tlb_info_32(mon, env);
2203 }
2204}
2205
Blue Swirl1b3cba62010-12-11 18:56:27 +00002206static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2207 int *plast_prot,
2208 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002209{
bellard9746b152004-11-11 18:30:24 +00002210 int prot1;
2211 prot1 = *plast_prot;
2212 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002213 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002214 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2215 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002216 *pstart, end, end - *pstart,
2217 prot1 & PG_USER_MASK ? 'u' : '-',
2218 'r',
2219 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002220 }
2221 if (prot != 0)
2222 *pstart = end;
2223 else
2224 *pstart = -1;
2225 *plast_prot = prot;
2226 }
2227}
2228
Blue Swirl1b3cba62010-12-11 18:56:27 +00002229static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002230{
Austin Clementsb49ca722011-08-14 23:19:21 -04002231 unsigned int l1, l2;
2232 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002233 uint32_t pgd, pde, pte;
2234 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002235
bellardb86bda52004-09-18 19:32:46 +00002236 pgd = env->cr[3] & ~0xfff;
2237 last_prot = 0;
2238 start = -1;
2239 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002240 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002241 pde = le32_to_cpu(pde);
2242 end = l1 << 22;
2243 if (pde & PG_PRESENT_MASK) {
2244 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2245 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002246 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002247 } else {
2248 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002249 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002250 pte = le32_to_cpu(pte);
2251 end = (l1 << 22) + (l2 << 12);
2252 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002253 prot = pte & pde &
2254 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002255 } else {
2256 prot = 0;
2257 }
aliguori376253e2009-03-05 23:01:23 +00002258 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002259 }
2260 }
2261 } else {
2262 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002263 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002264 }
2265 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002266 /* Flush last range */
2267 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002268}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002269
2270static void mem_info_pae32(Monitor *mon, CPUState *env)
2271{
Austin Clementsb49ca722011-08-14 23:19:21 -04002272 unsigned int l1, l2, l3;
2273 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002274 uint64_t pdpe, pde, pte;
2275 uint64_t pdp_addr, pd_addr, pt_addr;
2276 target_phys_addr_t start, end;
2277
2278 pdp_addr = env->cr[3] & ~0x1f;
2279 last_prot = 0;
2280 start = -1;
2281 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002282 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002283 pdpe = le64_to_cpu(pdpe);
2284 end = l1 << 30;
2285 if (pdpe & PG_PRESENT_MASK) {
2286 pd_addr = pdpe & 0x3fffffffff000ULL;
2287 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002288 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002289 pde = le64_to_cpu(pde);
2290 end = (l1 << 30) + (l2 << 21);
2291 if (pde & PG_PRESENT_MASK) {
2292 if (pde & PG_PSE_MASK) {
2293 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2294 PG_PRESENT_MASK);
2295 mem_print(mon, &start, &last_prot, end, prot);
2296 } else {
2297 pt_addr = pde & 0x3fffffffff000ULL;
2298 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002299 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002300 pte = le64_to_cpu(pte);
2301 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2302 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002303 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2304 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002305 } else {
2306 prot = 0;
2307 }
2308 mem_print(mon, &start, &last_prot, end, prot);
2309 }
2310 }
2311 } else {
2312 prot = 0;
2313 mem_print(mon, &start, &last_prot, end, prot);
2314 }
2315 }
2316 } else {
2317 prot = 0;
2318 mem_print(mon, &start, &last_prot, end, prot);
2319 }
2320 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002321 /* Flush last range */
2322 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002323}
2324
2325
2326#ifdef TARGET_X86_64
2327static void mem_info_64(Monitor *mon, CPUState *env)
2328{
2329 int prot, last_prot;
2330 uint64_t l1, l2, l3, l4;
2331 uint64_t pml4e, pdpe, pde, pte;
2332 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2333
2334 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2335 last_prot = 0;
2336 start = -1;
2337 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002338 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002339 pml4e = le64_to_cpu(pml4e);
2340 end = l1 << 39;
2341 if (pml4e & PG_PRESENT_MASK) {
2342 pdp_addr = pml4e & 0x3fffffffff000ULL;
2343 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002344 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002345 pdpe = le64_to_cpu(pdpe);
2346 end = (l1 << 39) + (l2 << 30);
2347 if (pdpe & PG_PRESENT_MASK) {
2348 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002349 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2350 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002351 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002352 mem_print(mon, &start, &last_prot, end, prot);
2353 } else {
2354 pd_addr = pdpe & 0x3fffffffff000ULL;
2355 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002356 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002357 pde = le64_to_cpu(pde);
2358 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2359 if (pde & PG_PRESENT_MASK) {
2360 if (pde & PG_PSE_MASK) {
2361 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2362 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002363 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002364 mem_print(mon, &start, &last_prot, end, prot);
2365 } else {
2366 pt_addr = pde & 0x3fffffffff000ULL;
2367 for (l4 = 0; l4 < 512; l4++) {
2368 cpu_physical_memory_read(pt_addr
2369 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002370 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002371 pte = le64_to_cpu(pte);
2372 end = (l1 << 39) + (l2 << 30) +
2373 (l3 << 21) + (l4 << 12);
2374 if (pte & PG_PRESENT_MASK) {
2375 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2376 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002377 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002378 } else {
2379 prot = 0;
2380 }
2381 mem_print(mon, &start, &last_prot, end, prot);
2382 }
2383 }
2384 } else {
2385 prot = 0;
2386 mem_print(mon, &start, &last_prot, end, prot);
2387 }
2388 }
2389 }
2390 } else {
2391 prot = 0;
2392 mem_print(mon, &start, &last_prot, end, prot);
2393 }
2394 }
2395 } else {
2396 prot = 0;
2397 mem_print(mon, &start, &last_prot, end, prot);
2398 }
2399 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002400 /* Flush last range */
2401 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002402}
2403#endif
2404
2405static void mem_info(Monitor *mon)
2406{
2407 CPUState *env;
2408
2409 env = mon_get_cpu();
2410
2411 if (!(env->cr[0] & CR0_PG_MASK)) {
2412 monitor_printf(mon, "PG disabled\n");
2413 return;
2414 }
2415 if (env->cr[4] & CR4_PAE_MASK) {
2416#ifdef TARGET_X86_64
2417 if (env->hflags & HF_LMA_MASK) {
2418 mem_info_64(mon, env);
2419 } else
2420#endif
2421 {
2422 mem_info_pae32(mon, env);
2423 }
2424 } else {
2425 mem_info_32(mon, env);
2426 }
2427}
bellardb86bda52004-09-18 19:32:46 +00002428#endif
2429
aurel327c664e22009-03-03 06:12:22 +00002430#if defined(TARGET_SH4)
2431
aliguori376253e2009-03-05 23:01:23 +00002432static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002433{
aliguori376253e2009-03-05 23:01:23 +00002434 monitor_printf(mon, " tlb%i:\t"
2435 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2436 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2437 "dirty=%hhu writethrough=%hhu\n",
2438 idx,
2439 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2440 tlb->v, tlb->sh, tlb->c, tlb->pr,
2441 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002442}
2443
aliguori376253e2009-03-05 23:01:23 +00002444static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002445{
2446 CPUState *env = mon_get_cpu();
2447 int i;
2448
aliguori376253e2009-03-05 23:01:23 +00002449 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002450 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002451 print_tlb (mon, i, &env->itlb[i]);
2452 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002453 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002454 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002455}
2456
2457#endif
2458
Blue Swirld41160a2010-12-19 13:42:56 +00002459#if defined(TARGET_SPARC)
2460static void tlb_info(Monitor *mon)
2461{
2462 CPUState *env1 = mon_get_cpu();
2463
2464 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2465}
2466#endif
2467
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002468static void do_info_kvm_print(Monitor *mon, const QObject *data)
2469{
2470 QDict *qdict;
2471
2472 qdict = qobject_to_qdict(data);
2473
2474 monitor_printf(mon, "kvm support: ");
2475 if (qdict_get_bool(qdict, "present")) {
2476 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2477 "enabled" : "disabled");
2478 } else {
2479 monitor_printf(mon, "not compiled\n");
2480 }
2481}
2482
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002483static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002484{
2485#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002486 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2487 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002488#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002489 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002490#endif
2491}
2492
aliguori030ea372009-04-21 22:30:47 +00002493static void do_info_numa(Monitor *mon)
2494{
aliguorib28b6232009-04-22 20:20:29 +00002495 int i;
aliguori030ea372009-04-21 22:30:47 +00002496 CPUState *env;
2497
2498 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2499 for (i = 0; i < nb_numa_nodes; i++) {
2500 monitor_printf(mon, "node %d cpus:", i);
2501 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2502 if (env->numa_node == i) {
2503 monitor_printf(mon, " %d", env->cpu_index);
2504 }
2505 }
2506 monitor_printf(mon, "\n");
2507 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2508 node_mem[i] >> 20);
2509 }
2510}
2511
bellard5f1ce942006-02-08 22:40:15 +00002512#ifdef CONFIG_PROFILER
2513
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002514int64_t qemu_time;
2515int64_t dev_time;
2516
aliguori376253e2009-03-05 23:01:23 +00002517static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002518{
2519 int64_t total;
2520 total = qemu_time;
2521 if (total == 0)
2522 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002523 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002524 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002525 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002526 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002527 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002528 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002529}
2530#else
aliguori376253e2009-03-05 23:01:23 +00002531static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002532{
aliguori376253e2009-03-05 23:01:23 +00002533 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002534}
2535#endif
2536
bellardec36b692006-07-16 18:57:03 +00002537/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002538static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002539
aliguori376253e2009-03-05 23:01:23 +00002540static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002541{
2542 int i;
2543 CaptureState *s;
2544
2545 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002546 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002547 s->ops.info (s->opaque);
2548 }
2549}
2550
Blue Swirl23130862009-06-06 08:22:04 +00002551#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002552static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002553{
2554 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002555 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002556 CaptureState *s;
2557
2558 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2559 if (i == n) {
2560 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002561 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002562 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002563 return;
2564 }
2565 }
2566}
2567
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002568static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002569{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002570 const char *path = qdict_get_str(qdict, "path");
2571 int has_freq = qdict_haskey(qdict, "freq");
2572 int freq = qdict_get_try_int(qdict, "freq", -1);
2573 int has_bits = qdict_haskey(qdict, "bits");
2574 int bits = qdict_get_try_int(qdict, "bits", -1);
2575 int has_channels = qdict_haskey(qdict, "nchannels");
2576 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002577 CaptureState *s;
2578
Anthony Liguori7267c092011-08-20 22:09:37 -05002579 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002580
2581 freq = has_freq ? freq : 44100;
2582 bits = has_bits ? bits : 16;
2583 nchannels = has_channels ? nchannels : 2;
2584
2585 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002586 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002587 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002588 return;
bellardec36b692006-07-16 18:57:03 +00002589 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002590 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002591}
2592#endif
2593
aurel32dc1c0b72008-04-27 23:52:12 +00002594#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002595static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002596{
2597 CPUState *env;
2598
2599 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2600 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2601 }
2602
2603 return 0;
2604}
2605#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002606static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002607{
2608 qerror_report(QERR_UNSUPPORTED);
2609 return -1;
2610}
aurel32dc1c0b72008-04-27 23:52:12 +00002611#endif
2612
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002613static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002614{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002615 QDict *qdict;
2616
2617 qdict = qobject_to_qdict(data);
2618
2619 monitor_printf(mon, "VM status: ");
2620 if (qdict_get_bool(qdict, "running")) {
2621 monitor_printf(mon, "running");
2622 if (qdict_get_bool(qdict, "singlestep")) {
2623 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002624 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002625 } else {
2626 monitor_printf(mon, "paused");
2627 }
2628
2629 monitor_printf(mon, "\n");
2630}
2631
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002632static void do_info_status(Monitor *mon, QObject **ret_data)
2633{
2634 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2635 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002636}
2637
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002638static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002639{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002640 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002641
aliguori76655d62009-03-06 20:27:37 +00002642 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002643 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002644 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002645 return acl;
2646}
aliguori76655d62009-03-06 20:27:37 +00002647
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002648static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002649{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002650 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002651 qemu_acl *acl = find_acl(mon, aclname);
2652 qemu_acl_entry *entry;
2653 int i = 0;
2654
2655 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002656 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002657 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002658 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002659 i++;
2660 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002661 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002662 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002663 }
2664}
2665
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002666static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002667{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002668 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002669 qemu_acl *acl = find_acl(mon, aclname);
2670
2671 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002672 qemu_acl_reset(acl);
2673 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002674 }
2675}
aliguori76655d62009-03-06 20:27:37 +00002676
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002677static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002678{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002679 const char *aclname = qdict_get_str(qdict, "aclname");
2680 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002681 qemu_acl *acl = find_acl(mon, aclname);
2682
2683 if (acl) {
2684 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002685 acl->defaultDeny = 0;
2686 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002687 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002688 acl->defaultDeny = 1;
2689 monitor_printf(mon, "acl: policy set to 'deny'\n");
2690 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002691 monitor_printf(mon, "acl: unknown policy '%s', "
2692 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002693 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002694 }
2695}
aliguori76655d62009-03-06 20:27:37 +00002696
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002697static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002698{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002699 const char *aclname = qdict_get_str(qdict, "aclname");
2700 const char *match = qdict_get_str(qdict, "match");
2701 const char *policy = qdict_get_str(qdict, "policy");
2702 int has_index = qdict_haskey(qdict, "index");
2703 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002704 qemu_acl *acl = find_acl(mon, aclname);
2705 int deny, ret;
2706
2707 if (acl) {
2708 if (strcmp(policy, "allow") == 0) {
2709 deny = 0;
2710 } else if (strcmp(policy, "deny") == 0) {
2711 deny = 1;
2712 } else {
2713 monitor_printf(mon, "acl: unknown policy '%s', "
2714 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002715 return;
2716 }
aliguori28a76be2009-03-06 20:27:40 +00002717 if (has_index)
2718 ret = qemu_acl_insert(acl, deny, match, index);
2719 else
2720 ret = qemu_acl_append(acl, deny, match);
2721 if (ret < 0)
2722 monitor_printf(mon, "acl: unable to add acl entry\n");
2723 else
2724 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002725 }
2726}
aliguori76655d62009-03-06 20:27:37 +00002727
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002728static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002729{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002730 const char *aclname = qdict_get_str(qdict, "aclname");
2731 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002732 qemu_acl *acl = find_acl(mon, aclname);
2733 int ret;
aliguori76655d62009-03-06 20:27:37 +00002734
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002735 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002736 ret = qemu_acl_remove(acl, match);
2737 if (ret < 0)
2738 monitor_printf(mon, "acl: no matching acl entry\n");
2739 else
2740 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002741 }
2742}
2743
Huang Ying79c4f6b2009-06-23 10:05:14 +08002744#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002745static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002746{
2747 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002748 int cpu_index = qdict_get_int(qdict, "cpu_index");
2749 int bank = qdict_get_int(qdict, "bank");
2750 uint64_t status = qdict_get_int(qdict, "status");
2751 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2752 uint64_t addr = qdict_get_int(qdict, "addr");
2753 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002754 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002755
Jan Kiszka747461c2011-03-02 08:56:10 +01002756 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2757 flags |= MCE_INJECT_BROADCAST;
2758 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002759 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002760 if (cenv->cpu_index == cpu_index) {
2761 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002762 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002763 break;
2764 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002765 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002766}
2767#endif
2768
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002769static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002770{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002771 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002772 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002773 int fd;
2774
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002775 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002776 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002777 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002778 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002779 }
2780
2781 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002782 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2783 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002784 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002785 }
2786
Blue Swirl72cf2d42009-09-12 07:36:22 +00002787 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002788 if (strcmp(monfd->name, fdname) != 0) {
2789 continue;
2790 }
2791
2792 close(monfd->fd);
2793 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002794 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002795 }
2796
Anthony Liguori7267c092011-08-20 22:09:37 -05002797 monfd = g_malloc0(sizeof(mon_fd_t));
2798 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002799 monfd->fd = fd;
2800
Blue Swirl72cf2d42009-09-12 07:36:22 +00002801 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002802 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002803}
2804
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002805static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002806{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002807 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002808 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002809
Blue Swirl72cf2d42009-09-12 07:36:22 +00002810 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002811 if (strcmp(monfd->name, fdname) != 0) {
2812 continue;
2813 }
2814
Blue Swirl72cf2d42009-09-12 07:36:22 +00002815 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002816 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002817 g_free(monfd->name);
2818 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002819 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002820 }
2821
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002822 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002823 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002824}
2825
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002826static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002827{
2828 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002829 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002830
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002831 vm_stop(VMSTOP_LOADVM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002832
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002833 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002834 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002835 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002836}
2837
Mark McLoughlin7768e042009-07-22 09:11:41 +01002838int monitor_get_fd(Monitor *mon, const char *fdname)
2839{
Anthony Liguoric227f092009-10-01 16:12:16 -05002840 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002841
Blue Swirl72cf2d42009-09-12 07:36:22 +00002842 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002843 int fd;
2844
2845 if (strcmp(monfd->name, fdname) != 0) {
2846 continue;
2847 }
2848
2849 fd = monfd->fd;
2850
2851 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002852 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002853 g_free(monfd->name);
2854 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002855
2856 return fd;
2857 }
2858
2859 return -1;
2860}
2861
Anthony Liguoric227f092009-10-01 16:12:16 -05002862static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002863#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002864 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002865};
2866
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002867/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002868static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002869 {
2870 .name = "version",
2871 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 .params = "",
2873 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002874 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002875 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002876 },
2877 {
2878 .name = "network",
2879 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 .params = "",
2881 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002882 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002883 },
2884 {
2885 .name = "chardev",
2886 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 .params = "",
2888 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002889 .user_print = qemu_chr_info_print,
2890 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 },
2892 {
2893 .name = "block",
2894 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 .params = "",
2896 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002897 .user_print = bdrv_info_print,
2898 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 },
2900 {
2901 .name = "blockstats",
2902 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 .params = "",
2904 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002905 .user_print = bdrv_stats_print,
2906 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002907 },
2908 {
2909 .name = "registers",
2910 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002911 .params = "",
2912 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002913 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002914 },
2915 {
2916 .name = "cpus",
2917 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002918 .params = "",
2919 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002920 .user_print = monitor_print_cpus,
2921 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 },
2923 {
2924 .name = "history",
2925 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 .params = "",
2927 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002928 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 },
2930 {
2931 .name = "irq",
2932 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002933 .params = "",
2934 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002935 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002936 },
2937 {
2938 .name = "pic",
2939 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002940 .params = "",
2941 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002942 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002943 },
2944 {
2945 .name = "pci",
2946 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002947 .params = "",
2948 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002949 .user_print = do_pci_info_print,
2950 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002951 },
Blue Swirld41160a2010-12-19 13:42:56 +00002952#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002953 {
2954 .name = "tlb",
2955 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002956 .params = "",
2957 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002958 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002959 },
aurel327c664e22009-03-03 06:12:22 +00002960#endif
2961#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002962 {
2963 .name = "mem",
2964 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002965 .params = "",
2966 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002967 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002968 },
bellardb86bda52004-09-18 19:32:46 +00002969#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002970 {
2971 .name = "jit",
2972 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002973 .params = "",
2974 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002975 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002976 },
2977 {
2978 .name = "kvm",
2979 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002980 .params = "",
2981 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002982 .user_print = do_info_kvm_print,
2983 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002984 },
2985 {
2986 .name = "numa",
2987 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002988 .params = "",
2989 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002990 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002991 },
2992 {
2993 .name = "usb",
2994 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002995 .params = "",
2996 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002997 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002998 },
2999 {
3000 .name = "usbhost",
3001 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003002 .params = "",
3003 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003004 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003005 },
3006 {
3007 .name = "profile",
3008 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003009 .params = "",
3010 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003011 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003012 },
3013 {
3014 .name = "capture",
3015 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003016 .params = "",
3017 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003018 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003019 },
3020 {
3021 .name = "snapshots",
3022 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003023 .params = "",
3024 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03003025 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003026 },
3027 {
3028 .name = "status",
3029 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003030 .params = "",
3031 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02003032 .user_print = do_info_status_print,
3033 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003034 },
3035 {
3036 .name = "pcmcia",
3037 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003038 .params = "",
3039 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03003040 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003041 },
3042 {
3043 .name = "mice",
3044 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003045 .params = "",
3046 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02003047 .user_print = do_info_mice_print,
3048 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003049 },
3050 {
3051 .name = "vnc",
3052 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003053 .params = "",
3054 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02003055 .user_print = do_info_vnc_print,
3056 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003057 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003058#if defined(CONFIG_SPICE)
3059 {
3060 .name = "spice",
3061 .args_type = "",
3062 .params = "",
3063 .help = "show the spice server status",
3064 .user_print = do_info_spice_print,
3065 .mhandler.info_new = do_info_spice,
3066 },
3067#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003068 {
3069 .name = "name",
3070 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003071 .params = "",
3072 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02003073 .user_print = do_info_name_print,
3074 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003075 },
3076 {
3077 .name = "uuid",
3078 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003079 .params = "",
3080 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02003081 .user_print = do_info_uuid_print,
3082 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003083 },
j_mayer76a66252007-03-07 08:32:30 +00003084#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003085 {
3086 .name = "cpustats",
3087 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003088 .params = "",
3089 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03003090 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003091 },
j_mayer76a66252007-03-07 08:32:30 +00003092#endif
blueswir131a60e22007-10-26 18:42:59 +00003093#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003094 {
3095 .name = "usernet",
3096 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003097 .params = "",
3098 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03003099 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003100 },
blueswir131a60e22007-10-26 18:42:59 +00003101#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003102 {
3103 .name = "migrate",
3104 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003105 .params = "",
3106 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003107 .user_print = do_info_migrate_print,
3108 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003109 },
3110 {
3111 .name = "balloon",
3112 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003113 .params = "",
3114 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003115 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003116 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003117 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003118 },
3119 {
3120 .name = "qtree",
3121 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003122 .params = "",
3123 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003124 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003125 },
3126 {
3127 .name = "qdm",
3128 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003129 .params = "",
3130 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003131 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003132 },
3133 {
3134 .name = "roms",
3135 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003136 .params = "",
3137 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003138 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003139 },
Lluís6d8a7642011-08-31 20:30:43 +02003140#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303141 {
3142 .name = "trace",
3143 .args_type = "",
3144 .params = "",
3145 .help = "show current contents of trace buffer",
3146 .mhandler.info = do_info_trace,
3147 },
Lluís31965ae2011-08-31 20:31:24 +02003148#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303149 {
3150 .name = "trace-events",
3151 .args_type = "",
3152 .params = "",
3153 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003154 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303155 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003156 {
3157 .name = NULL,
3158 },
bellard9dc39cb2004-03-14 21:38:27 +00003159};
3160
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003161static const mon_cmd_t qmp_cmds[] = {
3162#include "qmp-commands.h"
3163 { /* NULL */ },
3164};
3165
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003166static const mon_cmd_t qmp_query_cmds[] = {
3167 {
3168 .name = "version",
3169 .args_type = "",
3170 .params = "",
3171 .help = "show the version of QEMU",
3172 .user_print = do_info_version_print,
3173 .mhandler.info_new = do_info_version,
3174 },
3175 {
3176 .name = "commands",
3177 .args_type = "",
3178 .params = "",
3179 .help = "list QMP available commands",
3180 .user_print = monitor_user_noop,
3181 .mhandler.info_new = do_info_commands,
3182 },
3183 {
3184 .name = "chardev",
3185 .args_type = "",
3186 .params = "",
3187 .help = "show the character devices",
3188 .user_print = qemu_chr_info_print,
3189 .mhandler.info_new = qemu_chr_info,
3190 },
3191 {
3192 .name = "block",
3193 .args_type = "",
3194 .params = "",
3195 .help = "show the block devices",
3196 .user_print = bdrv_info_print,
3197 .mhandler.info_new = bdrv_info,
3198 },
3199 {
3200 .name = "blockstats",
3201 .args_type = "",
3202 .params = "",
3203 .help = "show block device statistics",
3204 .user_print = bdrv_stats_print,
3205 .mhandler.info_new = bdrv_info_stats,
3206 },
3207 {
3208 .name = "cpus",
3209 .args_type = "",
3210 .params = "",
3211 .help = "show infos for each CPU",
3212 .user_print = monitor_print_cpus,
3213 .mhandler.info_new = do_info_cpus,
3214 },
3215 {
3216 .name = "pci",
3217 .args_type = "",
3218 .params = "",
3219 .help = "show PCI info",
3220 .user_print = do_pci_info_print,
3221 .mhandler.info_new = do_pci_info,
3222 },
3223 {
3224 .name = "kvm",
3225 .args_type = "",
3226 .params = "",
3227 .help = "show KVM information",
3228 .user_print = do_info_kvm_print,
3229 .mhandler.info_new = do_info_kvm,
3230 },
3231 {
3232 .name = "status",
3233 .args_type = "",
3234 .params = "",
3235 .help = "show the current VM status (running|paused)",
3236 .user_print = do_info_status_print,
3237 .mhandler.info_new = do_info_status,
3238 },
3239 {
3240 .name = "mice",
3241 .args_type = "",
3242 .params = "",
3243 .help = "show which guest mouse is receiving events",
3244 .user_print = do_info_mice_print,
3245 .mhandler.info_new = do_info_mice,
3246 },
3247 {
3248 .name = "vnc",
3249 .args_type = "",
3250 .params = "",
3251 .help = "show the vnc server status",
3252 .user_print = do_info_vnc_print,
3253 .mhandler.info_new = do_info_vnc,
3254 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003255#if defined(CONFIG_SPICE)
3256 {
3257 .name = "spice",
3258 .args_type = "",
3259 .params = "",
3260 .help = "show the spice server status",
3261 .user_print = do_info_spice_print,
3262 .mhandler.info_new = do_info_spice,
3263 },
3264#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003265 {
3266 .name = "name",
3267 .args_type = "",
3268 .params = "",
3269 .help = "show the current VM name",
3270 .user_print = do_info_name_print,
3271 .mhandler.info_new = do_info_name,
3272 },
3273 {
3274 .name = "uuid",
3275 .args_type = "",
3276 .params = "",
3277 .help = "show the current VM UUID",
3278 .user_print = do_info_uuid_print,
3279 .mhandler.info_new = do_info_uuid,
3280 },
3281 {
3282 .name = "migrate",
3283 .args_type = "",
3284 .params = "",
3285 .help = "show migration status",
3286 .user_print = do_info_migrate_print,
3287 .mhandler.info_new = do_info_migrate,
3288 },
3289 {
3290 .name = "balloon",
3291 .args_type = "",
3292 .params = "",
3293 .help = "show balloon information",
3294 .user_print = monitor_print_balloon,
3295 .mhandler.info_async = do_info_balloon,
3296 .flags = MONITOR_CMD_ASYNC,
3297 },
3298 { /* NULL */ },
3299};
3300
bellard9307c4c2004-04-04 12:57:25 +00003301/*******************************************************************/
3302
3303static const char *pch;
3304static jmp_buf expr_env;
3305
bellard92a31b12005-02-10 22:00:52 +00003306#define MD_TLONG 0
3307#define MD_I32 1
3308
bellard9307c4c2004-04-04 12:57:25 +00003309typedef struct MonitorDef {
3310 const char *name;
3311 int offset;
blueswir18662d652008-10-02 18:32:44 +00003312 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003313 int type;
bellard9307c4c2004-04-04 12:57:25 +00003314} MonitorDef;
3315
bellard57206fd2004-04-25 18:54:52 +00003316#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003317static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003318{
bellard6a00d602005-11-21 23:25:50 +00003319 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003320 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003321}
3322#endif
3323
bellarda541f292004-04-12 20:39:29 +00003324#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003325static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003326{
bellard6a00d602005-11-21 23:25:50 +00003327 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003328 unsigned int u;
3329 int i;
3330
3331 u = 0;
3332 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003333 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003334
3335 return u;
3336}
3337
blueswir18662d652008-10-02 18:32:44 +00003338static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003339{
bellard6a00d602005-11-21 23:25:50 +00003340 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003341 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003342}
3343
blueswir18662d652008-10-02 18:32:44 +00003344static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003345{
bellard6a00d602005-11-21 23:25:50 +00003346 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003347 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003348}
bellard9fddaa02004-05-21 12:59:32 +00003349
blueswir18662d652008-10-02 18:32:44 +00003350static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003351{
bellard6a00d602005-11-21 23:25:50 +00003352 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003353 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003354}
3355
blueswir18662d652008-10-02 18:32:44 +00003356static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003357{
bellard6a00d602005-11-21 23:25:50 +00003358 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003359 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003360}
3361
blueswir18662d652008-10-02 18:32:44 +00003362static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003363{
bellard6a00d602005-11-21 23:25:50 +00003364 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003365 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003366}
bellarda541f292004-04-12 20:39:29 +00003367#endif
3368
bellarde95c8d52004-09-30 22:22:08 +00003369#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003370#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003371static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003372{
bellard6a00d602005-11-21 23:25:50 +00003373 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003374
3375 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003376}
bellard7b936c02005-10-30 17:05:13 +00003377#endif
bellarde95c8d52004-09-30 22:22:08 +00003378
blueswir18662d652008-10-02 18:32:44 +00003379static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003380{
bellard6a00d602005-11-21 23:25:50 +00003381 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003382 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003383}
3384#endif
3385
blueswir18662d652008-10-02 18:32:44 +00003386static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003387#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003388
3389#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003390 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003391 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003392 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003393
bellard9307c4c2004-04-04 12:57:25 +00003394 { "eax", offsetof(CPUState, regs[0]) },
3395 { "ecx", offsetof(CPUState, regs[1]) },
3396 { "edx", offsetof(CPUState, regs[2]) },
3397 { "ebx", offsetof(CPUState, regs[3]) },
3398 { "esp|sp", offsetof(CPUState, regs[4]) },
3399 { "ebp|fp", offsetof(CPUState, regs[5]) },
3400 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003401 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003402#ifdef TARGET_X86_64
3403 { "r8", offsetof(CPUState, regs[8]) },
3404 { "r9", offsetof(CPUState, regs[9]) },
3405 { "r10", offsetof(CPUState, regs[10]) },
3406 { "r11", offsetof(CPUState, regs[11]) },
3407 { "r12", offsetof(CPUState, regs[12]) },
3408 { "r13", offsetof(CPUState, regs[13]) },
3409 { "r14", offsetof(CPUState, regs[14]) },
3410 { "r15", offsetof(CPUState, regs[15]) },
3411#endif
bellard9307c4c2004-04-04 12:57:25 +00003412 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003413 { "eip", offsetof(CPUState, eip) },
3414 SEG("cs", R_CS)
3415 SEG("ds", R_DS)
3416 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003417 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003418 SEG("fs", R_FS)
3419 SEG("gs", R_GS)
3420 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003421#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003422 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003423 { "r0", offsetof(CPUState, gpr[0]) },
3424 { "r1", offsetof(CPUState, gpr[1]) },
3425 { "r2", offsetof(CPUState, gpr[2]) },
3426 { "r3", offsetof(CPUState, gpr[3]) },
3427 { "r4", offsetof(CPUState, gpr[4]) },
3428 { "r5", offsetof(CPUState, gpr[5]) },
3429 { "r6", offsetof(CPUState, gpr[6]) },
3430 { "r7", offsetof(CPUState, gpr[7]) },
3431 { "r8", offsetof(CPUState, gpr[8]) },
3432 { "r9", offsetof(CPUState, gpr[9]) },
3433 { "r10", offsetof(CPUState, gpr[10]) },
3434 { "r11", offsetof(CPUState, gpr[11]) },
3435 { "r12", offsetof(CPUState, gpr[12]) },
3436 { "r13", offsetof(CPUState, gpr[13]) },
3437 { "r14", offsetof(CPUState, gpr[14]) },
3438 { "r15", offsetof(CPUState, gpr[15]) },
3439 { "r16", offsetof(CPUState, gpr[16]) },
3440 { "r17", offsetof(CPUState, gpr[17]) },
3441 { "r18", offsetof(CPUState, gpr[18]) },
3442 { "r19", offsetof(CPUState, gpr[19]) },
3443 { "r20", offsetof(CPUState, gpr[20]) },
3444 { "r21", offsetof(CPUState, gpr[21]) },
3445 { "r22", offsetof(CPUState, gpr[22]) },
3446 { "r23", offsetof(CPUState, gpr[23]) },
3447 { "r24", offsetof(CPUState, gpr[24]) },
3448 { "r25", offsetof(CPUState, gpr[25]) },
3449 { "r26", offsetof(CPUState, gpr[26]) },
3450 { "r27", offsetof(CPUState, gpr[27]) },
3451 { "r28", offsetof(CPUState, gpr[28]) },
3452 { "r29", offsetof(CPUState, gpr[29]) },
3453 { "r30", offsetof(CPUState, gpr[30]) },
3454 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003455 /* Floating point registers */
3456 { "f0", offsetof(CPUState, fpr[0]) },
3457 { "f1", offsetof(CPUState, fpr[1]) },
3458 { "f2", offsetof(CPUState, fpr[2]) },
3459 { "f3", offsetof(CPUState, fpr[3]) },
3460 { "f4", offsetof(CPUState, fpr[4]) },
3461 { "f5", offsetof(CPUState, fpr[5]) },
3462 { "f6", offsetof(CPUState, fpr[6]) },
3463 { "f7", offsetof(CPUState, fpr[7]) },
3464 { "f8", offsetof(CPUState, fpr[8]) },
3465 { "f9", offsetof(CPUState, fpr[9]) },
3466 { "f10", offsetof(CPUState, fpr[10]) },
3467 { "f11", offsetof(CPUState, fpr[11]) },
3468 { "f12", offsetof(CPUState, fpr[12]) },
3469 { "f13", offsetof(CPUState, fpr[13]) },
3470 { "f14", offsetof(CPUState, fpr[14]) },
3471 { "f15", offsetof(CPUState, fpr[15]) },
3472 { "f16", offsetof(CPUState, fpr[16]) },
3473 { "f17", offsetof(CPUState, fpr[17]) },
3474 { "f18", offsetof(CPUState, fpr[18]) },
3475 { "f19", offsetof(CPUState, fpr[19]) },
3476 { "f20", offsetof(CPUState, fpr[20]) },
3477 { "f21", offsetof(CPUState, fpr[21]) },
3478 { "f22", offsetof(CPUState, fpr[22]) },
3479 { "f23", offsetof(CPUState, fpr[23]) },
3480 { "f24", offsetof(CPUState, fpr[24]) },
3481 { "f25", offsetof(CPUState, fpr[25]) },
3482 { "f26", offsetof(CPUState, fpr[26]) },
3483 { "f27", offsetof(CPUState, fpr[27]) },
3484 { "f28", offsetof(CPUState, fpr[28]) },
3485 { "f29", offsetof(CPUState, fpr[29]) },
3486 { "f30", offsetof(CPUState, fpr[30]) },
3487 { "f31", offsetof(CPUState, fpr[31]) },
3488 { "fpscr", offsetof(CPUState, fpscr) },
3489 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003490 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003491 { "lr", offsetof(CPUState, lr) },
3492 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003493 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003494 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003495 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003496 { "msr", 0, &monitor_get_msr, },
3497 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003498 { "tbu", 0, &monitor_get_tbu, },
3499 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003500#if defined(TARGET_PPC64)
3501 /* Address space register */
3502 { "asr", offsetof(CPUState, asr) },
3503#endif
3504 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003505 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003506 { "sr0", offsetof(CPUState, sr[0]) },
3507 { "sr1", offsetof(CPUState, sr[1]) },
3508 { "sr2", offsetof(CPUState, sr[2]) },
3509 { "sr3", offsetof(CPUState, sr[3]) },
3510 { "sr4", offsetof(CPUState, sr[4]) },
3511 { "sr5", offsetof(CPUState, sr[5]) },
3512 { "sr6", offsetof(CPUState, sr[6]) },
3513 { "sr7", offsetof(CPUState, sr[7]) },
3514 { "sr8", offsetof(CPUState, sr[8]) },
3515 { "sr9", offsetof(CPUState, sr[9]) },
3516 { "sr10", offsetof(CPUState, sr[10]) },
3517 { "sr11", offsetof(CPUState, sr[11]) },
3518 { "sr12", offsetof(CPUState, sr[12]) },
3519 { "sr13", offsetof(CPUState, sr[13]) },
3520 { "sr14", offsetof(CPUState, sr[14]) },
3521 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003522 /* Too lazy to put BATs... */
3523 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3524
3525 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3526 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3527 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3528 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3529 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3530 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3531 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3532 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3533 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3534 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3535 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3536 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3537 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3538 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3539 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3540 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3541 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3542 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3543 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3544 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3545 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3546 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3547 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3548 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3549 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3550 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3551 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3552 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3553 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3554 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3555 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3556 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3557 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3558 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3559 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3560 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3561 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3562 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3563 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3564 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3565 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3566 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3567 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3568 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3569 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3570 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3571 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3572 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3573 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3574 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3575 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3576 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3577 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3578 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3579 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3580 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3581 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3582 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3583 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3584 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3585 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3586 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3587 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3588 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3589 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3590 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3591
bellarde95c8d52004-09-30 22:22:08 +00003592#elif defined(TARGET_SPARC)
3593 { "g0", offsetof(CPUState, gregs[0]) },
3594 { "g1", offsetof(CPUState, gregs[1]) },
3595 { "g2", offsetof(CPUState, gregs[2]) },
3596 { "g3", offsetof(CPUState, gregs[3]) },
3597 { "g4", offsetof(CPUState, gregs[4]) },
3598 { "g5", offsetof(CPUState, gregs[5]) },
3599 { "g6", offsetof(CPUState, gregs[6]) },
3600 { "g7", offsetof(CPUState, gregs[7]) },
3601 { "o0", 0, monitor_get_reg },
3602 { "o1", 1, monitor_get_reg },
3603 { "o2", 2, monitor_get_reg },
3604 { "o3", 3, monitor_get_reg },
3605 { "o4", 4, monitor_get_reg },
3606 { "o5", 5, monitor_get_reg },
3607 { "o6", 6, monitor_get_reg },
3608 { "o7", 7, monitor_get_reg },
3609 { "l0", 8, monitor_get_reg },
3610 { "l1", 9, monitor_get_reg },
3611 { "l2", 10, monitor_get_reg },
3612 { "l3", 11, monitor_get_reg },
3613 { "l4", 12, monitor_get_reg },
3614 { "l5", 13, monitor_get_reg },
3615 { "l6", 14, monitor_get_reg },
3616 { "l7", 15, monitor_get_reg },
3617 { "i0", 16, monitor_get_reg },
3618 { "i1", 17, monitor_get_reg },
3619 { "i2", 18, monitor_get_reg },
3620 { "i3", 19, monitor_get_reg },
3621 { "i4", 20, monitor_get_reg },
3622 { "i5", 21, monitor_get_reg },
3623 { "i6", 22, monitor_get_reg },
3624 { "i7", 23, monitor_get_reg },
3625 { "pc", offsetof(CPUState, pc) },
3626 { "npc", offsetof(CPUState, npc) },
3627 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003628#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003629 { "psr", 0, &monitor_get_psr, },
3630 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003631#endif
bellarde95c8d52004-09-30 22:22:08 +00003632 { "tbr", offsetof(CPUState, tbr) },
3633 { "fsr", offsetof(CPUState, fsr) },
3634 { "f0", offsetof(CPUState, fpr[0]) },
3635 { "f1", offsetof(CPUState, fpr[1]) },
3636 { "f2", offsetof(CPUState, fpr[2]) },
3637 { "f3", offsetof(CPUState, fpr[3]) },
3638 { "f4", offsetof(CPUState, fpr[4]) },
3639 { "f5", offsetof(CPUState, fpr[5]) },
3640 { "f6", offsetof(CPUState, fpr[6]) },
3641 { "f7", offsetof(CPUState, fpr[7]) },
3642 { "f8", offsetof(CPUState, fpr[8]) },
3643 { "f9", offsetof(CPUState, fpr[9]) },
3644 { "f10", offsetof(CPUState, fpr[10]) },
3645 { "f11", offsetof(CPUState, fpr[11]) },
3646 { "f12", offsetof(CPUState, fpr[12]) },
3647 { "f13", offsetof(CPUState, fpr[13]) },
3648 { "f14", offsetof(CPUState, fpr[14]) },
3649 { "f15", offsetof(CPUState, fpr[15]) },
3650 { "f16", offsetof(CPUState, fpr[16]) },
3651 { "f17", offsetof(CPUState, fpr[17]) },
3652 { "f18", offsetof(CPUState, fpr[18]) },
3653 { "f19", offsetof(CPUState, fpr[19]) },
3654 { "f20", offsetof(CPUState, fpr[20]) },
3655 { "f21", offsetof(CPUState, fpr[21]) },
3656 { "f22", offsetof(CPUState, fpr[22]) },
3657 { "f23", offsetof(CPUState, fpr[23]) },
3658 { "f24", offsetof(CPUState, fpr[24]) },
3659 { "f25", offsetof(CPUState, fpr[25]) },
3660 { "f26", offsetof(CPUState, fpr[26]) },
3661 { "f27", offsetof(CPUState, fpr[27]) },
3662 { "f28", offsetof(CPUState, fpr[28]) },
3663 { "f29", offsetof(CPUState, fpr[29]) },
3664 { "f30", offsetof(CPUState, fpr[30]) },
3665 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003666#ifdef TARGET_SPARC64
3667 { "f32", offsetof(CPUState, fpr[32]) },
3668 { "f34", offsetof(CPUState, fpr[34]) },
3669 { "f36", offsetof(CPUState, fpr[36]) },
3670 { "f38", offsetof(CPUState, fpr[38]) },
3671 { "f40", offsetof(CPUState, fpr[40]) },
3672 { "f42", offsetof(CPUState, fpr[42]) },
3673 { "f44", offsetof(CPUState, fpr[44]) },
3674 { "f46", offsetof(CPUState, fpr[46]) },
3675 { "f48", offsetof(CPUState, fpr[48]) },
3676 { "f50", offsetof(CPUState, fpr[50]) },
3677 { "f52", offsetof(CPUState, fpr[52]) },
3678 { "f54", offsetof(CPUState, fpr[54]) },
3679 { "f56", offsetof(CPUState, fpr[56]) },
3680 { "f58", offsetof(CPUState, fpr[58]) },
3681 { "f60", offsetof(CPUState, fpr[60]) },
3682 { "f62", offsetof(CPUState, fpr[62]) },
3683 { "asi", offsetof(CPUState, asi) },
3684 { "pstate", offsetof(CPUState, pstate) },
3685 { "cansave", offsetof(CPUState, cansave) },
3686 { "canrestore", offsetof(CPUState, canrestore) },
3687 { "otherwin", offsetof(CPUState, otherwin) },
3688 { "wstate", offsetof(CPUState, wstate) },
3689 { "cleanwin", offsetof(CPUState, cleanwin) },
3690 { "fprs", offsetof(CPUState, fprs) },
3691#endif
bellard9307c4c2004-04-04 12:57:25 +00003692#endif
3693 { NULL },
3694};
3695
aliguori376253e2009-03-05 23:01:23 +00003696static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003697{
aliguori376253e2009-03-05 23:01:23 +00003698 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003699 longjmp(expr_env, 1);
3700}
3701
Markus Armbruster09b94182010-01-20 13:07:30 +01003702/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003703static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003704{
blueswir18662d652008-10-02 18:32:44 +00003705 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003706 void *ptr;
3707
bellard9307c4c2004-04-04 12:57:25 +00003708 for(md = monitor_defs; md->name != NULL; md++) {
3709 if (compare_cmd(name, md->name)) {
3710 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003711 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003712 } else {
bellard6a00d602005-11-21 23:25:50 +00003713 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003714 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003715 switch(md->type) {
3716 case MD_I32:
3717 *pval = *(int32_t *)ptr;
3718 break;
3719 case MD_TLONG:
3720 *pval = *(target_long *)ptr;
3721 break;
3722 default:
3723 *pval = 0;
3724 break;
3725 }
bellard9307c4c2004-04-04 12:57:25 +00003726 }
3727 return 0;
3728 }
3729 }
3730 return -1;
3731}
3732
3733static void next(void)
3734{
Blue Swirl660f11b2009-07-31 21:16:51 +00003735 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003736 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003737 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003738 pch++;
3739 }
3740}
3741
aliguori376253e2009-03-05 23:01:23 +00003742static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003743
aliguori376253e2009-03-05 23:01:23 +00003744static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003745{
blueswir1c2efc952007-09-25 17:28:42 +00003746 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003747 char *p;
bellard6a00d602005-11-21 23:25:50 +00003748 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003749
3750 switch(*pch) {
3751 case '+':
3752 next();
aliguori376253e2009-03-05 23:01:23 +00003753 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003754 break;
3755 case '-':
3756 next();
aliguori376253e2009-03-05 23:01:23 +00003757 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003758 break;
3759 case '~':
3760 next();
aliguori376253e2009-03-05 23:01:23 +00003761 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003762 break;
3763 case '(':
3764 next();
aliguori376253e2009-03-05 23:01:23 +00003765 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003766 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003767 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003768 }
3769 next();
3770 break;
bellard81d09122004-07-14 17:21:37 +00003771 case '\'':
3772 pch++;
3773 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003774 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003775 n = *pch;
3776 pch++;
3777 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003778 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003779 next();
3780 break;
bellard9307c4c2004-04-04 12:57:25 +00003781 case '$':
3782 {
3783 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003784 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003785
bellard9307c4c2004-04-04 12:57:25 +00003786 pch++;
3787 q = buf;
3788 while ((*pch >= 'a' && *pch <= 'z') ||
3789 (*pch >= 'A' && *pch <= 'Z') ||
3790 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003791 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003792 if ((q - buf) < sizeof(buf) - 1)
3793 *q++ = *pch;
3794 pch++;
3795 }
blueswir1cd390082008-11-16 13:53:32 +00003796 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003797 pch++;
3798 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003799 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003800 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003801 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003802 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003803 }
3804 break;
3805 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003806 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003807 n = 0;
3808 break;
3809 default:
blueswir17743e582007-09-24 18:39:04 +00003810#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003811 n = strtoull(pch, &p, 0);
3812#else
bellard9307c4c2004-04-04 12:57:25 +00003813 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003814#endif
bellard9307c4c2004-04-04 12:57:25 +00003815 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003816 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003817 }
3818 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003819 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003820 pch++;
3821 break;
3822 }
3823 return n;
3824}
3825
3826
aliguori376253e2009-03-05 23:01:23 +00003827static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003828{
blueswir1c2efc952007-09-25 17:28:42 +00003829 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003830 int op;
ths3b46e622007-09-17 08:09:54 +00003831
aliguori376253e2009-03-05 23:01:23 +00003832 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003833 for(;;) {
3834 op = *pch;
3835 if (op != '*' && op != '/' && op != '%')
3836 break;
3837 next();
aliguori376253e2009-03-05 23:01:23 +00003838 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003839 switch(op) {
3840 default:
3841 case '*':
3842 val *= val2;
3843 break;
3844 case '/':
3845 case '%':
ths5fafdf22007-09-16 21:08:06 +00003846 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003847 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003848 if (op == '/')
3849 val /= val2;
3850 else
3851 val %= val2;
3852 break;
3853 }
3854 }
3855 return val;
3856}
3857
aliguori376253e2009-03-05 23:01:23 +00003858static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003859{
blueswir1c2efc952007-09-25 17:28:42 +00003860 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003861 int op;
bellard9307c4c2004-04-04 12:57:25 +00003862
aliguori376253e2009-03-05 23:01:23 +00003863 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003864 for(;;) {
3865 op = *pch;
3866 if (op != '&' && op != '|' && op != '^')
3867 break;
3868 next();
aliguori376253e2009-03-05 23:01:23 +00003869 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003870 switch(op) {
3871 default:
3872 case '&':
3873 val &= val2;
3874 break;
3875 case '|':
3876 val |= val2;
3877 break;
3878 case '^':
3879 val ^= val2;
3880 break;
3881 }
3882 }
3883 return val;
3884}
3885
aliguori376253e2009-03-05 23:01:23 +00003886static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003887{
blueswir1c2efc952007-09-25 17:28:42 +00003888 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003889 int op;
bellard9307c4c2004-04-04 12:57:25 +00003890
aliguori376253e2009-03-05 23:01:23 +00003891 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003892 for(;;) {
3893 op = *pch;
3894 if (op != '+' && op != '-')
3895 break;
3896 next();
aliguori376253e2009-03-05 23:01:23 +00003897 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003898 if (op == '+')
3899 val += val2;
3900 else
3901 val -= val2;
3902 }
3903 return val;
3904}
3905
aliguori376253e2009-03-05 23:01:23 +00003906static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003907{
3908 pch = *pp;
3909 if (setjmp(expr_env)) {
3910 *pp = pch;
3911 return -1;
3912 }
blueswir1cd390082008-11-16 13:53:32 +00003913 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003914 pch++;
aliguori376253e2009-03-05 23:01:23 +00003915 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003916 *pp = pch;
3917 return 0;
3918}
3919
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003920static int get_double(Monitor *mon, double *pval, const char **pp)
3921{
3922 const char *p = *pp;
3923 char *tailp;
3924 double d;
3925
3926 d = strtod(p, &tailp);
3927 if (tailp == p) {
3928 monitor_printf(mon, "Number expected\n");
3929 return -1;
3930 }
3931 if (d != d || d - d != 0) {
3932 /* NaN or infinity */
3933 monitor_printf(mon, "Bad number\n");
3934 return -1;
3935 }
3936 *pval = d;
3937 *pp = tailp;
3938 return 0;
3939}
3940
bellard9307c4c2004-04-04 12:57:25 +00003941static int get_str(char *buf, int buf_size, const char **pp)
3942{
3943 const char *p;
3944 char *q;
3945 int c;
3946
bellard81d09122004-07-14 17:21:37 +00003947 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003948 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003949 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003950 p++;
3951 if (*p == '\0') {
3952 fail:
bellard81d09122004-07-14 17:21:37 +00003953 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003954 *pp = p;
3955 return -1;
3956 }
bellard9307c4c2004-04-04 12:57:25 +00003957 if (*p == '\"') {
3958 p++;
3959 while (*p != '\0' && *p != '\"') {
3960 if (*p == '\\') {
3961 p++;
3962 c = *p++;
3963 switch(c) {
3964 case 'n':
3965 c = '\n';
3966 break;
3967 case 'r':
3968 c = '\r';
3969 break;
3970 case '\\':
3971 case '\'':
3972 case '\"':
3973 break;
3974 default:
3975 qemu_printf("unsupported escape code: '\\%c'\n", c);
3976 goto fail;
3977 }
3978 if ((q - buf) < buf_size - 1) {
3979 *q++ = c;
3980 }
3981 } else {
3982 if ((q - buf) < buf_size - 1) {
3983 *q++ = *p;
3984 }
3985 p++;
3986 }
3987 }
3988 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003989 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003990 goto fail;
3991 }
3992 p++;
3993 } else {
blueswir1cd390082008-11-16 13:53:32 +00003994 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003995 if ((q - buf) < buf_size - 1) {
3996 *q++ = *p;
3997 }
3998 p++;
3999 }
bellard9307c4c2004-04-04 12:57:25 +00004000 }
bellard81d09122004-07-14 17:21:37 +00004001 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00004002 *pp = p;
4003 return 0;
4004}
4005
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004006/*
4007 * Store the command-name in cmdname, and return a pointer to
4008 * the remaining of the command string.
4009 */
4010static const char *get_command_name(const char *cmdline,
4011 char *cmdname, size_t nlen)
4012{
4013 size_t len;
4014 const char *p, *pstart;
4015
4016 p = cmdline;
4017 while (qemu_isspace(*p))
4018 p++;
4019 if (*p == '\0')
4020 return NULL;
4021 pstart = p;
4022 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
4023 p++;
4024 len = p - pstart;
4025 if (len > nlen - 1)
4026 len = nlen - 1;
4027 memcpy(cmdname, pstart, len);
4028 cmdname[len] = '\0';
4029 return p;
4030}
4031
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004032/**
4033 * Read key of 'type' into 'key' and return the current
4034 * 'type' pointer.
4035 */
4036static char *key_get_info(const char *type, char **key)
4037{
4038 size_t len;
4039 char *p, *str;
4040
4041 if (*type == ',')
4042 type++;
4043
4044 p = strchr(type, ':');
4045 if (!p) {
4046 *key = NULL;
4047 return NULL;
4048 }
4049 len = p - type;
4050
Anthony Liguori7267c092011-08-20 22:09:37 -05004051 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004052 memcpy(str, type, len);
4053 str[len] = '\0';
4054
4055 *key = str;
4056 return ++p;
4057}
4058
bellard9307c4c2004-04-04 12:57:25 +00004059static int default_fmt_format = 'x';
4060static int default_fmt_size = 4;
4061
4062#define MAX_ARGS 16
4063
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004064static int is_valid_option(const char *c, const char *typestr)
4065{
4066 char option[3];
4067
4068 option[0] = '-';
4069 option[1] = *c;
4070 option[2] = '\0';
4071
4072 typestr = strstr(typestr, option);
4073 return (typestr != NULL);
4074}
4075
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004076static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
4077 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004078{
4079 const mon_cmd_t *cmd;
4080
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004081 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004082 if (compare_cmd(cmdname, cmd->name)) {
4083 return cmd;
4084 }
4085 }
4086
4087 return NULL;
4088}
4089
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004090static const mon_cmd_t *monitor_find_command(const char *cmdname)
4091{
4092 return search_dispatch_table(mon_cmds, cmdname);
4093}
4094
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004095static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
4096{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03004097 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004098}
4099
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004100static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
4101{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03004102 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004103}
4104
Anthony Liguoric227f092009-10-01 16:12:16 -05004105static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004106 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004107 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00004108{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004109 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03004110 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05004111 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00004112 char cmdname[256];
4113 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004114 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00004115
4116#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00004117 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00004118#endif
ths3b46e622007-09-17 08:09:54 +00004119
bellard9307c4c2004-04-04 12:57:25 +00004120 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004121 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
4122 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004123 return NULL;
ths3b46e622007-09-17 08:09:54 +00004124
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004125 cmd = monitor_find_command(cmdname);
4126 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004127 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004128 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004129 }
bellard9307c4c2004-04-04 12:57:25 +00004130
bellard9307c4c2004-04-04 12:57:25 +00004131 /* parse the parameters */
4132 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00004133 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004134 typestr = key_get_info(typestr, &key);
4135 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00004136 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004137 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00004138 typestr++;
4139 switch(c) {
4140 case 'F':
bellard81d09122004-07-14 17:21:37 +00004141 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00004142 case 's':
4143 {
4144 int ret;
ths3b46e622007-09-17 08:09:54 +00004145
blueswir1cd390082008-11-16 13:53:32 +00004146 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004147 p++;
4148 if (*typestr == '?') {
4149 typestr++;
4150 if (*p == '\0') {
4151 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03004152 break;
bellard9307c4c2004-04-04 12:57:25 +00004153 }
4154 }
4155 ret = get_str(buf, sizeof(buf), &p);
4156 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00004157 switch(c) {
4158 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004159 monitor_printf(mon, "%s: filename expected\n",
4160 cmdname);
bellard81d09122004-07-14 17:21:37 +00004161 break;
4162 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004163 monitor_printf(mon, "%s: block device name expected\n",
4164 cmdname);
bellard81d09122004-07-14 17:21:37 +00004165 break;
4166 default:
aliguori376253e2009-03-05 23:01:23 +00004167 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004168 break;
4169 }
bellard9307c4c2004-04-04 12:57:25 +00004170 goto fail;
4171 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004172 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004173 }
4174 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004175 case 'O':
4176 {
4177 QemuOptsList *opts_list;
4178 QemuOpts *opts;
4179
4180 opts_list = qemu_find_opts(key);
4181 if (!opts_list || opts_list->desc->name) {
4182 goto bad_type;
4183 }
4184 while (qemu_isspace(*p)) {
4185 p++;
4186 }
4187 if (!*p)
4188 break;
4189 if (get_str(buf, sizeof(buf), &p) < 0) {
4190 goto fail;
4191 }
4192 opts = qemu_opts_parse(opts_list, buf, 1);
4193 if (!opts) {
4194 goto fail;
4195 }
4196 qemu_opts_to_qdict(opts, qdict);
4197 qemu_opts_del(opts);
4198 }
4199 break;
bellard9307c4c2004-04-04 12:57:25 +00004200 case '/':
4201 {
4202 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004203
blueswir1cd390082008-11-16 13:53:32 +00004204 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004205 p++;
4206 if (*p == '/') {
4207 /* format found */
4208 p++;
4209 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004210 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004211 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004212 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004213 count = count * 10 + (*p - '0');
4214 p++;
4215 }
4216 }
4217 size = -1;
4218 format = -1;
4219 for(;;) {
4220 switch(*p) {
4221 case 'o':
4222 case 'd':
4223 case 'u':
4224 case 'x':
4225 case 'i':
4226 case 'c':
4227 format = *p++;
4228 break;
4229 case 'b':
4230 size = 1;
4231 p++;
4232 break;
4233 case 'h':
4234 size = 2;
4235 p++;
4236 break;
4237 case 'w':
4238 size = 4;
4239 p++;
4240 break;
4241 case 'g':
4242 case 'L':
4243 size = 8;
4244 p++;
4245 break;
4246 default:
4247 goto next;
4248 }
4249 }
4250 next:
blueswir1cd390082008-11-16 13:53:32 +00004251 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004252 monitor_printf(mon, "invalid char in format: '%c'\n",
4253 *p);
bellard9307c4c2004-04-04 12:57:25 +00004254 goto fail;
4255 }
bellard9307c4c2004-04-04 12:57:25 +00004256 if (format < 0)
4257 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004258 if (format != 'i') {
4259 /* for 'i', not specifying a size gives -1 as size */
4260 if (size < 0)
4261 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004262 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004263 }
bellard9307c4c2004-04-04 12:57:25 +00004264 default_fmt_format = format;
4265 } else {
4266 count = 1;
4267 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004268 if (format != 'i') {
4269 size = default_fmt_size;
4270 } else {
4271 size = -1;
4272 }
bellard9307c4c2004-04-04 12:57:25 +00004273 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004274 qdict_put(qdict, "count", qint_from_int(count));
4275 qdict_put(qdict, "format", qint_from_int(format));
4276 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004277 }
4278 break;
4279 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004280 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004281 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004282 {
blueswir1c2efc952007-09-25 17:28:42 +00004283 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004284
blueswir1cd390082008-11-16 13:53:32 +00004285 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004286 p++;
bellard34405572004-06-08 00:55:58 +00004287 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004288 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004289 if (*p == '\0') {
4290 typestr++;
4291 break;
4292 }
bellard34405572004-06-08 00:55:58 +00004293 } else {
4294 if (*p == '.') {
4295 p++;
blueswir1cd390082008-11-16 13:53:32 +00004296 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004297 p++;
bellard34405572004-06-08 00:55:58 +00004298 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004299 typestr++;
4300 break;
bellard34405572004-06-08 00:55:58 +00004301 }
4302 }
bellard13224a82006-07-14 22:03:35 +00004303 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004304 }
aliguori376253e2009-03-05 23:01:23 +00004305 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004306 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004307 /* Check if 'i' is greater than 32-bit */
4308 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4309 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4310 monitor_printf(mon, "integer is for 32-bit values\n");
4311 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004312 } else if (c == 'M') {
4313 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004314 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004315 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004316 }
4317 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004318 case 'o':
4319 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004320 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004321 char *end;
4322
4323 while (qemu_isspace(*p)) {
4324 p++;
4325 }
4326 if (*typestr == '?') {
4327 typestr++;
4328 if (*p == '\0') {
4329 break;
4330 }
4331 }
4332 val = strtosz(p, &end);
4333 if (val < 0) {
4334 monitor_printf(mon, "invalid size\n");
4335 goto fail;
4336 }
4337 qdict_put(qdict, key, qint_from_int(val));
4338 p = end;
4339 }
4340 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004341 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004342 {
4343 double val;
4344
4345 while (qemu_isspace(*p))
4346 p++;
4347 if (*typestr == '?') {
4348 typestr++;
4349 if (*p == '\0') {
4350 break;
4351 }
4352 }
4353 if (get_double(mon, &val, &p) < 0) {
4354 goto fail;
4355 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004356 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004357 switch (*p) {
4358 case 'm':
4359 val /= 1e3; p += 2; break;
4360 case 'u':
4361 val /= 1e6; p += 2; break;
4362 case 'n':
4363 val /= 1e9; p += 2; break;
4364 }
4365 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004366 if (*p && !qemu_isspace(*p)) {
4367 monitor_printf(mon, "Unknown unit suffix\n");
4368 goto fail;
4369 }
4370 qdict_put(qdict, key, qfloat_from_double(val));
4371 }
4372 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004373 case 'b':
4374 {
4375 const char *beg;
4376 int val;
4377
4378 while (qemu_isspace(*p)) {
4379 p++;
4380 }
4381 beg = p;
4382 while (qemu_isgraph(*p)) {
4383 p++;
4384 }
4385 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4386 val = 1;
4387 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4388 val = 0;
4389 } else {
4390 monitor_printf(mon, "Expected 'on' or 'off'\n");
4391 goto fail;
4392 }
4393 qdict_put(qdict, key, qbool_from_int(val));
4394 }
4395 break;
bellard9307c4c2004-04-04 12:57:25 +00004396 case '-':
4397 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004398 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004399 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004400 /* option */
ths3b46e622007-09-17 08:09:54 +00004401
bellard9307c4c2004-04-04 12:57:25 +00004402 c = *typestr++;
4403 if (c == '\0')
4404 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004405 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004406 p++;
bellard9307c4c2004-04-04 12:57:25 +00004407 if (*p == '-') {
4408 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004409 if(c != *p) {
4410 if(!is_valid_option(p, typestr)) {
4411
4412 monitor_printf(mon, "%s: unsupported option -%c\n",
4413 cmdname, *p);
4414 goto fail;
4415 } else {
4416 skip_key = 1;
4417 }
bellard9307c4c2004-04-04 12:57:25 +00004418 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004419 if(skip_key) {
4420 p = tmp;
4421 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004422 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004423 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004424 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004425 }
bellard9307c4c2004-04-04 12:57:25 +00004426 }
bellard9307c4c2004-04-04 12:57:25 +00004427 }
4428 break;
4429 default:
4430 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004431 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004432 goto fail;
4433 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004434 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004435 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004436 }
4437 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004438 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004439 p++;
4440 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004441 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4442 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004443 goto fail;
4444 }
4445
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004446 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004447
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004448fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004449 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004450 return NULL;
4451}
4452
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004453void monitor_set_error(Monitor *mon, QError *qerror)
4454{
4455 /* report only the first error */
4456 if (!mon->error) {
4457 mon->error = qerror;
4458 } else {
4459 MON_DEBUG("Additional error report at %s:%d\n",
4460 qerror->file, qerror->linenr);
4461 QDECREF(qerror);
4462 }
4463}
4464
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004465static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4466{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004467 if (ret && !monitor_has_error(mon)) {
4468 /*
4469 * If it returns failure, it must have passed on error.
4470 *
4471 * Action: Report an internal error to the client if in QMP.
4472 */
4473 qerror_report(QERR_UNDEFINED_ERROR);
4474 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4475 cmd->name);
4476 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004477
4478#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004479 if (!ret && monitor_has_error(mon)) {
4480 /*
4481 * If it returns success, it must not have passed an error.
4482 *
4483 * Action: Report the passed error to the client.
4484 */
4485 MON_DEBUG("command '%s' returned success but passed an error\n",
4486 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004487 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004488
4489 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4490 /*
4491 * Handlers should not call Monitor print functions.
4492 *
4493 * Action: Ignore them in QMP.
4494 *
4495 * (XXX: we don't check any 'info' or 'query' command here
4496 * because the user print function _is_ called by do_info(), hence
4497 * we will trigger this check. This problem will go away when we
4498 * make 'query' commands real and kill do_info())
4499 */
4500 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4501 cmd->name, mon_print_count_get(mon));
4502 }
4503#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004504}
4505
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004506static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004507{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004508 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004509 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004510
4511 qdict = qdict_new();
4512
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004513 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004514 if (!cmd)
4515 goto out;
4516
Luiz Capitulino4903de02010-09-16 11:01:32 -03004517 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004518 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004519 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004520 QObject *data = NULL;
4521
4522 /* XXX: ignores the error code */
4523 cmd->mhandler.cmd_new(mon, qdict, &data);
4524 assert(!monitor_has_error(mon));
4525 if (data) {
4526 cmd->user_print(mon, data);
4527 qobject_decref(data);
4528 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004529 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004530 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004531 }
4532
Luiz Capitulino13917be2009-10-07 13:41:54 -03004533out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004534 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004535}
4536
bellard81d09122004-07-14 17:21:37 +00004537static void cmd_completion(const char *name, const char *list)
4538{
4539 const char *p, *pstart;
4540 char cmd[128];
4541 int len;
4542
4543 p = list;
4544 for(;;) {
4545 pstart = p;
4546 p = strchr(p, '|');
4547 if (!p)
4548 p = pstart + strlen(pstart);
4549 len = p - pstart;
4550 if (len > sizeof(cmd) - 2)
4551 len = sizeof(cmd) - 2;
4552 memcpy(cmd, pstart, len);
4553 cmd[len] = '\0';
4554 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004555 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004556 }
4557 if (*p == '\0')
4558 break;
4559 p++;
4560 }
4561}
4562
4563static void file_completion(const char *input)
4564{
4565 DIR *ffs;
4566 struct dirent *d;
4567 char path[1024];
4568 char file[1024], file_prefix[1024];
4569 int input_path_len;
4570 const char *p;
4571
ths5fafdf22007-09-16 21:08:06 +00004572 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004573 if (!p) {
4574 input_path_len = 0;
4575 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004576 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004577 } else {
4578 input_path_len = p - input + 1;
4579 memcpy(path, input, input_path_len);
4580 if (input_path_len > sizeof(path) - 1)
4581 input_path_len = sizeof(path) - 1;
4582 path[input_path_len] = '\0';
4583 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4584 }
4585#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004586 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4587 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004588#endif
4589 ffs = opendir(path);
4590 if (!ffs)
4591 return;
4592 for(;;) {
4593 struct stat sb;
4594 d = readdir(ffs);
4595 if (!d)
4596 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004597
4598 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4599 continue;
4600 }
4601
bellard81d09122004-07-14 17:21:37 +00004602 if (strstart(d->d_name, file_prefix, NULL)) {
4603 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004604 if (input_path_len < sizeof(file))
4605 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4606 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004607 /* stat the file to find out if it's a directory.
4608 * In that case add a slash to speed up typing long paths
4609 */
4610 stat(file, &sb);
4611 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004612 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004613 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004614 }
4615 }
4616 closedir(ffs);
4617}
4618
aliguori51de9762009-03-05 23:00:43 +00004619static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004620{
aliguori51de9762009-03-05 23:00:43 +00004621 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004622 const char *input = opaque;
4623
4624 if (input[0] == '\0' ||
4625 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004626 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004627 }
4628}
4629
4630/* NOTE: this parser is an approximate form of the real command parser */
4631static void parse_cmdline(const char *cmdline,
4632 int *pnb_args, char **args)
4633{
4634 const char *p;
4635 int nb_args, ret;
4636 char buf[1024];
4637
4638 p = cmdline;
4639 nb_args = 0;
4640 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004641 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004642 p++;
4643 if (*p == '\0')
4644 break;
4645 if (nb_args >= MAX_ARGS)
4646 break;
4647 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004648 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004649 nb_args++;
4650 if (ret < 0)
4651 break;
4652 }
4653 *pnb_args = nb_args;
4654}
4655
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004656static const char *next_arg_type(const char *typestr)
4657{
4658 const char *p = strchr(typestr, ':');
4659 return (p != NULL ? ++p : typestr);
4660}
4661
aliguori4c36ba32009-03-05 23:01:37 +00004662static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004663{
4664 const char *cmdname;
4665 char *args[MAX_ARGS];
4666 int nb_args, i, len;
4667 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004668 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004669 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004670
4671 parse_cmdline(cmdline, &nb_args, args);
4672#ifdef DEBUG_COMPLETION
4673 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004674 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004675 }
4676#endif
4677
4678 /* if the line ends with a space, it means we want to complete the
4679 next arg */
4680 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004681 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004682 if (nb_args >= MAX_ARGS) {
4683 goto cleanup;
4684 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004685 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004686 }
4687 if (nb_args <= 1) {
4688 /* command completion */
4689 if (nb_args == 0)
4690 cmdname = "";
4691 else
4692 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004693 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004694 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004695 cmd_completion(cmdname, cmd->name);
4696 }
4697 } else {
4698 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004699 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4700 if (compare_cmd(args[0], cmd->name)) {
4701 break;
4702 }
bellard81d09122004-07-14 17:21:37 +00004703 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004704 if (!cmd->name) {
4705 goto cleanup;
4706 }
4707
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004708 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004709 for(i = 0; i < nb_args - 2; i++) {
4710 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004711 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004712 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004713 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004714 }
4715 }
4716 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004717 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004718 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004719 }
bellard81d09122004-07-14 17:21:37 +00004720 switch(*ptype) {
4721 case 'F':
4722 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004723 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004724 file_completion(str);
4725 break;
4726 case 'B':
4727 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004728 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004729 bdrv_iterate(block_completion_it, (void *)str);
4730 break;
bellard7fe48482004-10-09 18:08:01 +00004731 case 's':
4732 /* XXX: more generic ? */
4733 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004734 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004735 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4736 cmd_completion(str, cmd->name);
4737 }
bellard64866c32006-05-07 18:03:31 +00004738 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004739 char *sep = strrchr(str, '-');
4740 if (sep)
4741 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004742 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004743 for(key = key_defs; key->name != NULL; key++) {
4744 cmd_completion(str, key->name);
4745 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004746 } else if (!strcmp(cmd->name, "help|?")) {
4747 readline_set_completion_index(cur_mon->rs, strlen(str));
4748 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4749 cmd_completion(str, cmd->name);
4750 }
bellard7fe48482004-10-09 18:08:01 +00004751 }
4752 break;
bellard81d09122004-07-14 17:21:37 +00004753 default:
4754 break;
4755 }
4756 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004757
4758cleanup:
4759 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004760 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004761 }
bellard81d09122004-07-14 17:21:37 +00004762}
4763
aliguori731b0362009-03-05 23:01:42 +00004764static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004765{
aliguori731b0362009-03-05 23:01:42 +00004766 Monitor *mon = opaque;
4767
Jan Kiszkac62313b2009-12-04 14:05:29 +01004768 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004769}
4770
Luiz Capitulino09069b12010-02-04 18:10:06 -02004771static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4772{
4773 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4774 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4775}
4776
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004777/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004778 * Argument validation rules:
4779 *
4780 * 1. The argument must exist in cmd_args qdict
4781 * 2. The argument type must be the expected one
4782 *
4783 * Special case: If the argument doesn't exist in cmd_args and
4784 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4785 * checking is skipped for it.
4786 */
4787static int check_client_args_type(const QDict *client_args,
4788 const QDict *cmd_args, int flags)
4789{
4790 const QDictEntry *ent;
4791
4792 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4793 QObject *obj;
4794 QString *arg_type;
4795 const QObject *client_arg = qdict_entry_value(ent);
4796 const char *client_arg_name = qdict_entry_key(ent);
4797
4798 obj = qdict_get(cmd_args, client_arg_name);
4799 if (!obj) {
4800 if (flags & QMP_ACCEPT_UNKNOWNS) {
4801 /* handler accepts unknowns */
4802 continue;
4803 }
4804 /* client arg doesn't exist */
4805 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4806 return -1;
4807 }
4808
4809 arg_type = qobject_to_qstring(obj);
4810 assert(arg_type != NULL);
4811
4812 /* check if argument's type is correct */
4813 switch (qstring_get_str(arg_type)[0]) {
4814 case 'F':
4815 case 'B':
4816 case 's':
4817 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4818 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4819 "string");
4820 return -1;
4821 }
4822 break;
4823 case 'i':
4824 case 'l':
4825 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004826 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004827 if (qobject_type(client_arg) != QTYPE_QINT) {
4828 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4829 "int");
4830 return -1;
4831 }
4832 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004833 case 'T':
4834 if (qobject_type(client_arg) != QTYPE_QINT &&
4835 qobject_type(client_arg) != QTYPE_QFLOAT) {
4836 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4837 "number");
4838 return -1;
4839 }
4840 break;
4841 case 'b':
4842 case '-':
4843 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4844 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4845 "bool");
4846 return -1;
4847 }
4848 break;
4849 case 'O':
4850 assert(flags & QMP_ACCEPT_UNKNOWNS);
4851 break;
4852 case '/':
4853 case '.':
4854 /*
4855 * These types are not supported by QMP and thus are not
4856 * handled here. Fall through.
4857 */
4858 default:
4859 abort();
4860 }
4861 }
4862
4863 return 0;
4864}
4865
4866/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004867 * - Check if the client has passed all mandatory args
4868 * - Set special flags for argument validation
4869 */
4870static int check_mandatory_args(const QDict *cmd_args,
4871 const QDict *client_args, int *flags)
4872{
4873 const QDictEntry *ent;
4874
4875 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4876 const char *cmd_arg_name = qdict_entry_key(ent);
4877 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4878 assert(type != NULL);
4879
4880 if (qstring_get_str(type)[0] == 'O') {
4881 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4882 *flags |= QMP_ACCEPT_UNKNOWNS;
4883 } else if (qstring_get_str(type)[0] != '-' &&
4884 qstring_get_str(type)[1] != '?' &&
4885 !qdict_haskey(client_args, cmd_arg_name)) {
4886 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4887 return -1;
4888 }
4889 }
4890
4891 return 0;
4892}
4893
4894static QDict *qdict_from_args_type(const char *args_type)
4895{
4896 int i;
4897 QDict *qdict;
4898 QString *key, *type, *cur_qs;
4899
4900 assert(args_type != NULL);
4901
4902 qdict = qdict_new();
4903
4904 if (args_type == NULL || args_type[0] == '\0') {
4905 /* no args, empty qdict */
4906 goto out;
4907 }
4908
4909 key = qstring_new();
4910 type = qstring_new();
4911
4912 cur_qs = key;
4913
4914 for (i = 0;; i++) {
4915 switch (args_type[i]) {
4916 case ',':
4917 case '\0':
4918 qdict_put(qdict, qstring_get_str(key), type);
4919 QDECREF(key);
4920 if (args_type[i] == '\0') {
4921 goto out;
4922 }
4923 type = qstring_new(); /* qdict has ref */
4924 cur_qs = key = qstring_new();
4925 break;
4926 case ':':
4927 cur_qs = type;
4928 break;
4929 default:
4930 qstring_append_chr(cur_qs, args_type[i]);
4931 break;
4932 }
4933 }
4934
4935out:
4936 return qdict;
4937}
4938
4939/*
4940 * Client argument checking rules:
4941 *
4942 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004943 * 2. Each argument provided by the client must be expected
4944 * 3. Each argument provided by the client must have the type expected
4945 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004946 */
4947static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4948{
4949 int flags, err;
4950 QDict *cmd_args;
4951
4952 cmd_args = qdict_from_args_type(cmd->args_type);
4953
4954 flags = 0;
4955 err = check_mandatory_args(cmd_args, client_args, &flags);
4956 if (err) {
4957 goto out;
4958 }
4959
Luiz Capitulino4af91932010-06-22 11:44:05 -03004960 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004961
4962out:
4963 QDECREF(cmd_args);
4964 return err;
4965}
4966
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004967/*
4968 * Input object checking rules
4969 *
4970 * 1. Input object must be a dict
4971 * 2. The "execute" key must exist
4972 * 3. The "execute" key must be a string
4973 * 4. If the "arguments" key exists, it must be a dict
4974 * 5. If the "id" key exists, it can be anything (ie. json-value)
4975 * 6. Any argument not listed above is considered invalid
4976 */
4977static QDict *qmp_check_input_obj(QObject *input_obj)
4978{
4979 const QDictEntry *ent;
4980 int has_exec_key = 0;
4981 QDict *input_dict;
4982
4983 if (qobject_type(input_obj) != QTYPE_QDICT) {
4984 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4985 return NULL;
4986 }
4987
4988 input_dict = qobject_to_qdict(input_obj);
4989
4990 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4991 const char *arg_name = qdict_entry_key(ent);
4992 const QObject *arg_obj = qdict_entry_value(ent);
4993
4994 if (!strcmp(arg_name, "execute")) {
4995 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4996 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4997 "string");
4998 return NULL;
4999 }
5000 has_exec_key = 1;
5001 } else if (!strcmp(arg_name, "arguments")) {
5002 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5003 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5004 "object");
5005 return NULL;
5006 }
5007 } else if (!strcmp(arg_name, "id")) {
5008 /* FIXME: check duplicated IDs for async commands */
5009 } else {
5010 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5011 return NULL;
5012 }
5013 }
5014
5015 if (!has_exec_key) {
5016 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5017 return NULL;
5018 }
5019
5020 return input_dict;
5021}
5022
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005023static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
5024{
5025 QObject *ret_data = NULL;
5026
Luiz Capitulino4903de02010-09-16 11:01:32 -03005027 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005028 qmp_async_info_handler(mon, cmd);
5029 if (monitor_has_error(mon)) {
5030 monitor_protocol_emitter(mon, NULL);
5031 }
5032 } else {
5033 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02005034 monitor_protocol_emitter(mon, ret_data);
5035 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005036 }
5037}
5038
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005039static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5040 const QDict *params)
5041{
5042 int ret;
5043 QObject *data = NULL;
5044
5045 mon_print_count_init(mon);
5046
5047 ret = cmd->mhandler.cmd_new(mon, params, &data);
5048 handler_audit(mon, cmd, ret);
5049 monitor_protocol_emitter(mon, data);
5050 qobject_decref(data);
5051}
5052
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005053static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5054{
5055 int err;
5056 QObject *obj;
5057 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005058 const mon_cmd_t *cmd;
5059 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005060 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005061
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005062 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005063 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005064
5065 obj = json_parser_parse(tokens, NULL);
5066 if (!obj) {
5067 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005068 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005069 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005070 }
5071
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005072 input = qmp_check_input_obj(obj);
5073 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005074 qobject_decref(obj);
5075 goto err_out;
5076 }
5077
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005078 mon->mc->id = qdict_get(input, "id");
5079 qobject_incref(mon->mc->id);
5080
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005081 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02005082 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005083 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005084 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005085 }
5086
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005087 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005088 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02005089 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03005090 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03005091 }
5092
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005093 if (!cmd) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005094 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5095 goto err_out;
5096 }
5097
5098 obj = qdict_get(input, "arguments");
5099 if (!obj) {
5100 args = qdict_new();
5101 } else {
5102 args = qobject_to_qdict(obj);
5103 QINCREF(args);
5104 }
5105
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005106 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005107 if (err < 0) {
5108 goto err_out;
5109 }
5110
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005111 if (query_cmd) {
5112 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03005113 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005114 err = qmp_async_cmd_handler(mon, cmd, args);
5115 if (err) {
5116 /* emit the error response */
5117 goto err_out;
5118 }
Adam Litke940cc302010-01-25 12:18:44 -06005119 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005120 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005121 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005122
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005123 goto out;
5124
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005125err_out:
5126 monitor_protocol_emitter(mon, NULL);
5127out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005128 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005129 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005130}
5131
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005132/**
5133 * monitor_control_read(): Read and handle QMP input
5134 */
5135static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5136{
5137 Monitor *old_mon = cur_mon;
5138
5139 cur_mon = opaque;
5140
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005141 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005142
5143 cur_mon = old_mon;
5144}
5145
aliguori731b0362009-03-05 23:01:42 +00005146static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005147{
aliguori731b0362009-03-05 23:01:42 +00005148 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005149 int i;
aliguori376253e2009-03-05 23:01:23 +00005150
aliguori731b0362009-03-05 23:01:42 +00005151 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005152
aliguoricde76ee2009-03-05 23:01:51 +00005153 if (cur_mon->rs) {
5154 for (i = 0; i < size; i++)
5155 readline_handle_byte(cur_mon->rs, buf[i]);
5156 } else {
5157 if (size == 0 || buf[size - 1] != 0)
5158 monitor_printf(cur_mon, "corrupted command\n");
5159 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005160 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005161 }
aliguori731b0362009-03-05 23:01:42 +00005162
5163 cur_mon = old_mon;
5164}
aliguorid8f44602008-10-06 13:52:44 +00005165
aliguori376253e2009-03-05 23:01:23 +00005166static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005167{
aliguori731b0362009-03-05 23:01:42 +00005168 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005169 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005170 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005171}
5172
aliguoricde76ee2009-03-05 23:01:51 +00005173int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005174{
aliguoricde76ee2009-03-05 23:01:51 +00005175 if (!mon->rs)
5176 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005177 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005178 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005179}
5180
aliguori376253e2009-03-05 23:01:23 +00005181void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005182{
aliguoricde76ee2009-03-05 23:01:51 +00005183 if (!mon->rs)
5184 return;
aliguori731b0362009-03-05 23:01:42 +00005185 if (--mon->suspend_cnt == 0)
5186 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005187}
5188
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005189static QObject *get_qmp_greeting(void)
5190{
5191 QObject *ver;
5192
5193 do_info_version(NULL, &ver);
5194 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5195}
5196
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005197/**
5198 * monitor_control_event(): Print QMP gretting
5199 */
5200static void monitor_control_event(void *opaque, int event)
5201{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005202 QObject *data;
5203 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005204
Luiz Capitulino47116d12010-02-08 17:01:30 -02005205 switch (event) {
5206 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005207 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005208 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005209 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005210 monitor_json_emitter(mon, data);
5211 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005212 break;
5213 case CHR_EVENT_CLOSED:
5214 json_message_parser_destroy(&mon->mc->parser);
5215 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005216 }
5217}
5218
aliguori731b0362009-03-05 23:01:42 +00005219static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005220{
aliguori376253e2009-03-05 23:01:23 +00005221 Monitor *mon = opaque;
5222
aliguori2724b182009-03-05 23:01:47 +00005223 switch (event) {
5224 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005225 mon->mux_out = 0;
5226 if (mon->reset_seen) {
5227 readline_restart(mon->rs);
5228 monitor_resume(mon);
5229 monitor_flush(mon);
5230 } else {
5231 mon->suspend_cnt = 0;
5232 }
aliguori2724b182009-03-05 23:01:47 +00005233 break;
ths86e94de2007-01-05 22:01:59 +00005234
aliguori2724b182009-03-05 23:01:47 +00005235 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005236 if (mon->reset_seen) {
5237 if (mon->suspend_cnt == 0) {
5238 monitor_printf(mon, "\n");
5239 }
5240 monitor_flush(mon);
5241 monitor_suspend(mon);
5242 } else {
5243 mon->suspend_cnt++;
5244 }
5245 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005246 break;
5247
Amit Shahb6b8df52009-10-07 18:31:16 +05305248 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005249 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5250 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005251 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005252 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005253 }
5254 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005255 break;
5256 }
ths86e94de2007-01-05 22:01:59 +00005257}
5258
aliguori76655d62009-03-06 20:27:37 +00005259
5260/*
5261 * Local variables:
5262 * c-indent-level: 4
5263 * c-basic-offset: 4
5264 * tab-width: 8
5265 * End:
5266 */
5267
aliguori731b0362009-03-05 23:01:42 +00005268void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005269{
aliguori731b0362009-03-05 23:01:42 +00005270 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005271 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005272
5273 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005274 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005275 is_first_init = 0;
5276 }
aliguori87127162009-03-05 23:01:29 +00005277
Anthony Liguori7267c092011-08-20 22:09:37 -05005278 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005279
aliguori87127162009-03-05 23:01:29 +00005280 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005281 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005282 if (flags & MONITOR_USE_READLINE) {
5283 mon->rs = readline_init(mon, monitor_find_completion);
5284 monitor_read_command(mon, 0);
5285 }
aliguori87127162009-03-05 23:01:29 +00005286
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005287 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005288 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005289 /* Control mode requires special handlers */
5290 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5291 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005292 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005293 } else {
5294 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5295 monitor_event, mon);
5296 }
aliguori87127162009-03-05 23:01:29 +00005297
Blue Swirl72cf2d42009-09-12 07:36:22 +00005298 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005299 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5300 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005301}
5302
aliguori376253e2009-03-05 23:01:23 +00005303static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005304{
aliguoribb5fc202009-03-05 23:01:15 +00005305 BlockDriverState *bs = opaque;
5306 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005307
aliguoribb5fc202009-03-05 23:01:15 +00005308 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005309 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005310 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005311 }
aliguori731b0362009-03-05 23:01:42 +00005312 if (mon->password_completion_cb)
5313 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005314
aliguori731b0362009-03-05 23:01:42 +00005315 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005316}
aliguoric0f4ce72009-03-05 23:01:01 +00005317
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005318int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5319 BlockDriverCompletionFunc *completion_cb,
5320 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005321{
aliguoricde76ee2009-03-05 23:01:51 +00005322 int err;
5323
aliguoribb5fc202009-03-05 23:01:15 +00005324 if (!bdrv_key_required(bs)) {
5325 if (completion_cb)
5326 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005327 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005328 }
aliguoric0f4ce72009-03-05 23:01:01 +00005329
Luiz Capitulino94171e12009-12-07 21:37:00 +01005330 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005331 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005332 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005333 }
5334
aliguori376253e2009-03-05 23:01:23 +00005335 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5336 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005337
aliguori731b0362009-03-05 23:01:42 +00005338 mon->password_completion_cb = completion_cb;
5339 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005340
aliguoricde76ee2009-03-05 23:01:51 +00005341 err = monitor_read_password(mon, bdrv_password_cb, bs);
5342
5343 if (err && completion_cb)
5344 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005345
5346 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005347}