blob: ac68edf98b6abab9b5fc421379ab8598ebd9e1c9 [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)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500889 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890 (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{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001296 vm_stop(RSTATE_PAUSED);
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
Luiz Capitulino1bcef682011-07-29 15:15:00 -03001314 if (runstate_check(RSTATE_IN_MIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301315 qerror_report(QERR_MIGRATION_EXPECTED);
1316 return -1;
Luiz Capitulino6667b232011-07-29 15:57:54 -03001317 } else if (runstate_check(RSTATE_PANICKED) ||
1318 runstate_check(RSTATE_SHUTDOWN)) {
1319 qerror_report(QERR_RESET_REQUIRED);
1320 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301321 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001322
aliguori376253e2009-03-05 23:01:23 +00001323 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001324 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001325 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001326 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001327 return 0;
1328 } else {
1329 return -1;
1330 }
bellard8a7ddc32004-03-31 19:00:16 +00001331}
1332
aliguoribb5fc202009-03-05 23:01:15 +00001333static void bdrv_key_cb(void *opaque, int err)
1334{
aliguori376253e2009-03-05 23:01:23 +00001335 Monitor *mon = opaque;
1336
aliguoribb5fc202009-03-05 23:01:15 +00001337 /* another key was set successfully, retry to continue */
1338 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001339 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001340}
1341
1342static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1343{
aliguori376253e2009-03-05 23:01:23 +00001344 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001345
aliguori376253e2009-03-05 23:01:23 +00001346 if (!context->err && bdrv_key_required(bs)) {
1347 context->err = -EBUSY;
1348 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1349 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001350 }
1351}
1352
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001353static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001354{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001355 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001356 if (!device)
1357 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1358 if (gdbserver_start(device) < 0) {
1359 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1360 device);
1361 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001362 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001363 } else {
aliguori59030a82009-04-05 18:43:41 +00001364 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1365 device);
bellard8a7ddc32004-03-31 19:00:16 +00001366 }
1367}
1368
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001369static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001370{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001371 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001372 if (select_watchdog_action(action) == -1) {
1373 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1374 }
1375}
1376
aliguori376253e2009-03-05 23:01:23 +00001377static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001378{
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001380 switch(c) {
1381 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001382 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001383 break;
1384 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001386 break;
1387 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001389 break;
1390 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 default:
1394 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001396 } else {
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001398 }
1399 break;
1400 }
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001402}
1403
aliguori376253e2009-03-05 23:01:23 +00001404static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001405 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001406{
bellard6a00d602005-11-21 23:25:50 +00001407 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001408 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001409 uint8_t buf[16];
1410 uint64_t v;
1411
1412 if (format == 'i') {
1413 int flags;
1414 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001415 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001416#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001417 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001418 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001419 } else if (wsize == 4) {
1420 flags = 0;
1421 } else {
bellard6a15fd12006-04-12 21:07:07 +00001422 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001423 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001424 if (env) {
1425#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001426 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001427 (env->segs[R_CS].flags & DESC_L_MASK))
1428 flags = 2;
1429 else
1430#endif
1431 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1432 flags = 1;
1433 }
bellard4c27ba22004-04-25 18:05:08 +00001434 }
1435#endif
aliguori376253e2009-03-05 23:01:23 +00001436 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001437 return;
1438 }
1439
1440 len = wsize * count;
1441 if (wsize == 1)
1442 line_size = 8;
1443 else
1444 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001445 max_digits = 0;
1446
1447 switch(format) {
1448 case 'o':
1449 max_digits = (wsize * 8 + 2) / 3;
1450 break;
1451 default:
1452 case 'x':
1453 max_digits = (wsize * 8) / 4;
1454 break;
1455 case 'u':
1456 case 'd':
1457 max_digits = (wsize * 8 * 10 + 32) / 33;
1458 break;
1459 case 'c':
1460 wsize = 1;
1461 break;
1462 }
1463
1464 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001465 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001466 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001467 else
aliguori376253e2009-03-05 23:01:23 +00001468 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001469 l = len;
1470 if (l > line_size)
1471 l = line_size;
1472 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001473 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001474 } else {
bellard6a00d602005-11-21 23:25:50 +00001475 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001476 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001477 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001478 break;
1479 }
bellard9307c4c2004-04-04 12:57:25 +00001480 }
ths5fafdf22007-09-16 21:08:06 +00001481 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001482 while (i < l) {
1483 switch(wsize) {
1484 default:
1485 case 1:
1486 v = ldub_raw(buf + i);
1487 break;
1488 case 2:
1489 v = lduw_raw(buf + i);
1490 break;
1491 case 4:
bellard92a31b12005-02-10 22:00:52 +00001492 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001493 break;
1494 case 8:
1495 v = ldq_raw(buf + i);
1496 break;
1497 }
aliguori376253e2009-03-05 23:01:23 +00001498 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001499 switch(format) {
1500 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001502 break;
1503 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001504 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001505 break;
1506 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001507 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001508 break;
1509 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001511 break;
1512 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001513 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001514 break;
1515 }
1516 i += wsize;
1517 }
aliguori376253e2009-03-05 23:01:23 +00001518 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001519 addr += l;
1520 len -= l;
1521 }
1522}
1523
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001524static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001525{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001526 int count = qdict_get_int(qdict, "count");
1527 int format = qdict_get_int(qdict, "format");
1528 int size = qdict_get_int(qdict, "size");
1529 target_long addr = qdict_get_int(qdict, "addr");
1530
aliguori376253e2009-03-05 23:01:23 +00001531 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001532}
1533
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001534static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001535{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001536 int count = qdict_get_int(qdict, "count");
1537 int format = qdict_get_int(qdict, "format");
1538 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001539 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001540
aliguori376253e2009-03-05 23:01:23 +00001541 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001542}
1543
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001544static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001545{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001546 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001547 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001548
blueswir17743e582007-09-24 18:39:04 +00001549#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001550 switch(format) {
1551 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001552 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001553 break;
1554 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001555 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001556 break;
1557 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001558 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001559 break;
1560 default:
1561 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001562 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001563 break;
1564 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001566 break;
1567 }
bellard92a31b12005-02-10 22:00:52 +00001568#else
1569 switch(format) {
1570 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001571 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001572 break;
1573 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001574 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001575 break;
1576 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001577 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001578 break;
1579 default:
1580 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001581 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001582 break;
1583 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001584 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001585 break;
1586 }
1587#endif
aliguori376253e2009-03-05 23:01:23 +00001588 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001589}
1590
Luiz Capitulino98696222010-02-10 23:49:58 -02001591static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001592{
1593 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001594 uint32_t size = qdict_get_int(qdict, "size");
1595 const char *filename = qdict_get_str(qdict, "filename");
1596 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001597 uint32_t l;
1598 CPUState *env;
1599 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001600 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001601
1602 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001603
1604 f = fopen(filename, "wb");
1605 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001606 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001607 return -1;
bellardb371dc52007-01-03 15:20:39 +00001608 }
1609 while (size != 0) {
1610 l = sizeof(buf);
1611 if (l > size)
1612 l = size;
1613 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001614 if (fwrite(buf, 1, l, f) != l) {
1615 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1616 goto exit;
1617 }
bellardb371dc52007-01-03 15:20:39 +00001618 addr += l;
1619 size -= l;
1620 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001621
1622 ret = 0;
1623
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001624exit:
bellardb371dc52007-01-03 15:20:39 +00001625 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001626 return ret;
bellardb371dc52007-01-03 15:20:39 +00001627}
1628
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001629static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001630 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001631{
1632 FILE *f;
1633 uint32_t l;
1634 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001635 uint32_t size = qdict_get_int(qdict, "size");
1636 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001637 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001638 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001639
1640 f = fopen(filename, "wb");
1641 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001642 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001643 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001644 }
1645 while (size != 0) {
1646 l = sizeof(buf);
1647 if (l > size)
1648 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001649 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001650 if (fwrite(buf, 1, l, f) != l) {
1651 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1652 goto exit;
1653 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001654 fflush(f);
1655 addr += l;
1656 size -= l;
1657 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001658
1659 ret = 0;
1660
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001661exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001662 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001663 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001664}
1665
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001666static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001667{
1668 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001669 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001670 uint32_t start = qdict_get_int(qdict, "start");
1671 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001672
1673 sum = 0;
1674 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001675 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001676 /* BSD sum algorithm ('sum' Unix command) */
1677 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001678 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001679 }
aliguori376253e2009-03-05 23:01:23 +00001680 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001681}
1682
bellarda3a91a32004-06-04 11:06:21 +00001683typedef struct {
1684 int keycode;
1685 const char *name;
1686} KeyDef;
1687
1688static const KeyDef key_defs[] = {
1689 { 0x2a, "shift" },
1690 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001691
bellarda3a91a32004-06-04 11:06:21 +00001692 { 0x38, "alt" },
1693 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001694 { 0x64, "altgr" },
1695 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001696 { 0x1d, "ctrl" },
1697 { 0x9d, "ctrl_r" },
1698
1699 { 0xdd, "menu" },
1700
1701 { 0x01, "esc" },
1702
1703 { 0x02, "1" },
1704 { 0x03, "2" },
1705 { 0x04, "3" },
1706 { 0x05, "4" },
1707 { 0x06, "5" },
1708 { 0x07, "6" },
1709 { 0x08, "7" },
1710 { 0x09, "8" },
1711 { 0x0a, "9" },
1712 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001713 { 0x0c, "minus" },
1714 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001715 { 0x0e, "backspace" },
1716
1717 { 0x0f, "tab" },
1718 { 0x10, "q" },
1719 { 0x11, "w" },
1720 { 0x12, "e" },
1721 { 0x13, "r" },
1722 { 0x14, "t" },
1723 { 0x15, "y" },
1724 { 0x16, "u" },
1725 { 0x17, "i" },
1726 { 0x18, "o" },
1727 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001728 { 0x1a, "bracket_left" },
1729 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001730 { 0x1c, "ret" },
1731
1732 { 0x1e, "a" },
1733 { 0x1f, "s" },
1734 { 0x20, "d" },
1735 { 0x21, "f" },
1736 { 0x22, "g" },
1737 { 0x23, "h" },
1738 { 0x24, "j" },
1739 { 0x25, "k" },
1740 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001741 { 0x27, "semicolon" },
1742 { 0x28, "apostrophe" },
1743 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001744
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001745 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001746 { 0x2c, "z" },
1747 { 0x2d, "x" },
1748 { 0x2e, "c" },
1749 { 0x2f, "v" },
1750 { 0x30, "b" },
1751 { 0x31, "n" },
1752 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001753 { 0x33, "comma" },
1754 { 0x34, "dot" },
1755 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001756
balrog4d3b6f62008-02-10 16:33:14 +00001757 { 0x37, "asterisk" },
1758
bellarda3a91a32004-06-04 11:06:21 +00001759 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001760 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001761 { 0x3b, "f1" },
1762 { 0x3c, "f2" },
1763 { 0x3d, "f3" },
1764 { 0x3e, "f4" },
1765 { 0x3f, "f5" },
1766 { 0x40, "f6" },
1767 { 0x41, "f7" },
1768 { 0x42, "f8" },
1769 { 0x43, "f9" },
1770 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001771 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001772 { 0x46, "scroll_lock" },
1773
bellard64866c32006-05-07 18:03:31 +00001774 { 0xb5, "kp_divide" },
1775 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001776 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001777 { 0x4e, "kp_add" },
1778 { 0x9c, "kp_enter" },
1779 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001780 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001781
1782 { 0x52, "kp_0" },
1783 { 0x4f, "kp_1" },
1784 { 0x50, "kp_2" },
1785 { 0x51, "kp_3" },
1786 { 0x4b, "kp_4" },
1787 { 0x4c, "kp_5" },
1788 { 0x4d, "kp_6" },
1789 { 0x47, "kp_7" },
1790 { 0x48, "kp_8" },
1791 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001792
bellarda3a91a32004-06-04 11:06:21 +00001793 { 0x56, "<" },
1794
1795 { 0x57, "f11" },
1796 { 0x58, "f12" },
1797
1798 { 0xb7, "print" },
1799
1800 { 0xc7, "home" },
1801 { 0xc9, "pgup" },
1802 { 0xd1, "pgdn" },
1803 { 0xcf, "end" },
1804
1805 { 0xcb, "left" },
1806 { 0xc8, "up" },
1807 { 0xd0, "down" },
1808 { 0xcd, "right" },
1809
1810 { 0xd2, "insert" },
1811 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001812#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1813 { 0xf0, "stop" },
1814 { 0xf1, "again" },
1815 { 0xf2, "props" },
1816 { 0xf3, "undo" },
1817 { 0xf4, "front" },
1818 { 0xf5, "copy" },
1819 { 0xf6, "open" },
1820 { 0xf7, "paste" },
1821 { 0xf8, "find" },
1822 { 0xf9, "cut" },
1823 { 0xfa, "lf" },
1824 { 0xfb, "help" },
1825 { 0xfc, "meta_l" },
1826 { 0xfd, "meta_r" },
1827 { 0xfe, "compose" },
1828#endif
bellarda3a91a32004-06-04 11:06:21 +00001829 { 0, NULL },
1830};
1831
1832static int get_keycode(const char *key)
1833{
1834 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001835 char *endp;
1836 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001837
1838 for(p = key_defs; p->name != NULL; p++) {
1839 if (!strcmp(key, p->name))
1840 return p->keycode;
1841 }
bellard64866c32006-05-07 18:03:31 +00001842 if (strstart(key, "0x", NULL)) {
1843 ret = strtoul(key, &endp, 0);
1844 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1845 return ret;
1846 }
bellarda3a91a32004-06-04 11:06:21 +00001847 return -1;
1848}
1849
balrogc8256f92008-06-08 22:45:01 +00001850#define MAX_KEYCODES 16
1851static uint8_t keycodes[MAX_KEYCODES];
1852static int nb_pending_keycodes;
1853static QEMUTimer *key_timer;
1854
1855static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001856{
balrogc8256f92008-06-08 22:45:01 +00001857 int keycode;
1858
1859 while (nb_pending_keycodes > 0) {
1860 nb_pending_keycodes--;
1861 keycode = keycodes[nb_pending_keycodes];
1862 if (keycode & 0x80)
1863 kbd_put_keycode(0xe0);
1864 kbd_put_keycode(keycode | 0x80);
1865 }
1866}
1867
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001868static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001869{
balrog3401c0d2008-06-04 10:05:59 +00001870 char keyname_buf[16];
1871 char *separator;
1872 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001873 const char *string = qdict_get_str(qdict, "string");
1874 int has_hold_time = qdict_haskey(qdict, "hold_time");
1875 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001876
balrogc8256f92008-06-08 22:45:01 +00001877 if (nb_pending_keycodes > 0) {
1878 qemu_del_timer(key_timer);
1879 release_keys(NULL);
1880 }
1881 if (!has_hold_time)
1882 hold_time = 100;
1883 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001884 while (1) {
1885 separator = strchr(string, '-');
1886 keyname_len = separator ? separator - string : strlen(string);
1887 if (keyname_len > 0) {
1888 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1889 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001890 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001891 return;
bellarda3a91a32004-06-04 11:06:21 +00001892 }
balrogc8256f92008-06-08 22:45:01 +00001893 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001894 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001895 return;
1896 }
1897 keyname_buf[keyname_len] = 0;
1898 keycode = get_keycode(keyname_buf);
1899 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001900 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001901 return;
1902 }
balrogc8256f92008-06-08 22:45:01 +00001903 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001904 }
balrog3401c0d2008-06-04 10:05:59 +00001905 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001906 break;
balrog3401c0d2008-06-04 10:05:59 +00001907 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001908 }
balrogc8256f92008-06-08 22:45:01 +00001909 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001910 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001911 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001912 keycode = keycodes[i];
1913 if (keycode & 0x80)
1914 kbd_put_keycode(0xe0);
1915 kbd_put_keycode(keycode & 0x7f);
1916 }
balrogc8256f92008-06-08 22:45:01 +00001917 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001918 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001919 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001920}
1921
bellard13224a82006-07-14 22:03:35 +00001922static int mouse_button_state;
1923
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001924static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001925{
1926 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001927 const char *dx_str = qdict_get_str(qdict, "dx_str");
1928 const char *dy_str = qdict_get_str(qdict, "dy_str");
1929 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001930 dx = strtol(dx_str, NULL, 0);
1931 dy = strtol(dy_str, NULL, 0);
1932 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001933 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001934 dz = strtol(dz_str, NULL, 0);
1935 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1936}
1937
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001938static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001939{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001940 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001941 mouse_button_state = button_state;
1942 kbd_mouse_event(0, 0, 0, mouse_button_state);
1943}
1944
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001945static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001946{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001947 int size = qdict_get_int(qdict, "size");
1948 int addr = qdict_get_int(qdict, "addr");
1949 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001950 uint32_t val;
1951 int suffix;
1952
1953 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001954 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001955 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001956 addr++;
1957 }
1958 addr &= 0xffff;
1959
1960 switch(size) {
1961 default:
1962 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001963 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001964 suffix = 'b';
1965 break;
1966 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001967 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001968 suffix = 'w';
1969 break;
1970 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001971 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001972 suffix = 'l';
1973 break;
1974 }
aliguori376253e2009-03-05 23:01:23 +00001975 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1976 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001977}
bellarda3a91a32004-06-04 11:06:21 +00001978
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001979static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001980{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001981 int size = qdict_get_int(qdict, "size");
1982 int addr = qdict_get_int(qdict, "addr");
1983 int val = qdict_get_int(qdict, "val");
1984
Jan Kiszkaf1147842009-07-14 10:20:11 +02001985 addr &= IOPORTS_MASK;
1986
1987 switch (size) {
1988 default:
1989 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001990 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001991 break;
1992 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001993 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001994 break;
1995 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001996 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001997 break;
1998 }
1999}
2000
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002001static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00002002{
2003 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002004 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00002005
Jan Kiszka76e30d02009-07-02 00:19:02 +02002006 res = qemu_boot_set(bootdevice);
2007 if (res == 0) {
2008 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
2009 } else if (res > 0) {
2010 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00002011 } else {
aliguori376253e2009-03-05 23:01:23 +00002012 monitor_printf(mon, "no function defined to set boot device list for "
2013 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00002014 }
2015}
2016
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03002017/**
2018 * do_system_reset(): Issue a machine reset
2019 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002020static int do_system_reset(Monitor *mon, const QDict *qdict,
2021 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00002022{
2023 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002024 return 0;
bellarde4f90822004-06-20 12:35:44 +00002025}
2026
Luiz Capitulino43076662009-10-07 13:41:59 -03002027/**
2028 * do_system_powerdown(): Issue a machine powerdown
2029 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002030static int do_system_powerdown(Monitor *mon, const QDict *qdict,
2031 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00002032{
2033 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002034 return 0;
bellard34751872005-07-02 14:31:34 +00002035}
2036
bellardb86bda52004-09-18 19:32:46 +00002037#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00002038static void print_pte(Monitor *mon, target_phys_addr_t addr,
2039 target_phys_addr_t pte,
2040 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00002041{
Blue Swirld65aaf32010-12-11 18:56:24 +00002042#ifdef TARGET_X86_64
2043 if (addr & (1ULL << 47)) {
2044 addr |= -1LL << 48;
2045 }
2046#endif
2047 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
2048 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002049 addr,
2050 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00002051 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00002052 pte & PG_GLOBAL_MASK ? 'G' : '-',
2053 pte & PG_PSE_MASK ? 'P' : '-',
2054 pte & PG_DIRTY_MASK ? 'D' : '-',
2055 pte & PG_ACCESSED_MASK ? 'A' : '-',
2056 pte & PG_PCD_MASK ? 'C' : '-',
2057 pte & PG_PWT_MASK ? 'T' : '-',
2058 pte & PG_USER_MASK ? 'U' : '-',
2059 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00002060}
2061
Blue Swirld65aaf32010-12-11 18:56:24 +00002062static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002063{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002064 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00002065 uint32_t pgd, pde, pte;
2066
bellardb86bda52004-09-18 19:32:46 +00002067 pgd = env->cr[3] & ~0xfff;
2068 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002069 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002070 pde = le32_to_cpu(pde);
2071 if (pde & PG_PRESENT_MASK) {
2072 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00002073 /* 4M pages */
2074 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00002075 } else {
2076 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002077 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002078 pte = le32_to_cpu(pte);
2079 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002080 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002081 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002082 ~0xfff);
2083 }
2084 }
2085 }
2086 }
2087 }
2088}
2089
Blue Swirld65aaf32010-12-11 18:56:24 +00002090static void tlb_info_pae32(Monitor *mon, CPUState *env)
2091{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002092 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002093 uint64_t pdpe, pde, pte;
2094 uint64_t pdp_addr, pd_addr, pt_addr;
2095
2096 pdp_addr = env->cr[3] & ~0x1f;
2097 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002098 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002099 pdpe = le64_to_cpu(pdpe);
2100 if (pdpe & PG_PRESENT_MASK) {
2101 pd_addr = pdpe & 0x3fffffffff000ULL;
2102 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002103 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002104 pde = le64_to_cpu(pde);
2105 if (pde & PG_PRESENT_MASK) {
2106 if (pde & PG_PSE_MASK) {
2107 /* 2M pages with PAE, CR4.PSE is ignored */
2108 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2109 ~((target_phys_addr_t)(1 << 20) - 1));
2110 } else {
2111 pt_addr = pde & 0x3fffffffff000ULL;
2112 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002113 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002114 pte = le64_to_cpu(pte);
2115 if (pte & PG_PRESENT_MASK) {
2116 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2117 + (l3 << 12),
2118 pte & ~PG_PSE_MASK,
2119 ~(target_phys_addr_t)0xfff);
2120 }
2121 }
2122 }
2123 }
2124 }
2125 }
2126 }
2127}
2128
2129#ifdef TARGET_X86_64
2130static void tlb_info_64(Monitor *mon, CPUState *env)
2131{
2132 uint64_t l1, l2, l3, l4;
2133 uint64_t pml4e, pdpe, pde, pte;
2134 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2135
2136 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2137 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002138 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002139 pml4e = le64_to_cpu(pml4e);
2140 if (pml4e & PG_PRESENT_MASK) {
2141 pdp_addr = pml4e & 0x3fffffffff000ULL;
2142 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002143 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002144 pdpe = le64_to_cpu(pdpe);
2145 if (pdpe & PG_PRESENT_MASK) {
2146 if (pdpe & PG_PSE_MASK) {
2147 /* 1G pages, CR4.PSE is ignored */
2148 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2149 0x3ffffc0000000ULL);
2150 } else {
2151 pd_addr = pdpe & 0x3fffffffff000ULL;
2152 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002153 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002154 pde = le64_to_cpu(pde);
2155 if (pde & PG_PRESENT_MASK) {
2156 if (pde & PG_PSE_MASK) {
2157 /* 2M pages, CR4.PSE is ignored */
2158 print_pte(mon, (l1 << 39) + (l2 << 30) +
2159 (l3 << 21), pde,
2160 0x3ffffffe00000ULL);
2161 } else {
2162 pt_addr = pde & 0x3fffffffff000ULL;
2163 for (l4 = 0; l4 < 512; l4++) {
2164 cpu_physical_memory_read(pt_addr
2165 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002166 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002167 pte = le64_to_cpu(pte);
2168 if (pte & PG_PRESENT_MASK) {
2169 print_pte(mon, (l1 << 39) +
2170 (l2 << 30) +
2171 (l3 << 21) + (l4 << 12),
2172 pte & ~PG_PSE_MASK,
2173 0x3fffffffff000ULL);
2174 }
2175 }
2176 }
2177 }
2178 }
2179 }
2180 }
2181 }
2182 }
2183 }
2184}
2185#endif
2186
2187static void tlb_info(Monitor *mon)
2188{
2189 CPUState *env;
2190
2191 env = mon_get_cpu();
2192
2193 if (!(env->cr[0] & CR0_PG_MASK)) {
2194 monitor_printf(mon, "PG disabled\n");
2195 return;
2196 }
2197 if (env->cr[4] & CR4_PAE_MASK) {
2198#ifdef TARGET_X86_64
2199 if (env->hflags & HF_LMA_MASK) {
2200 tlb_info_64(mon, env);
2201 } else
2202#endif
2203 {
2204 tlb_info_pae32(mon, env);
2205 }
2206 } else {
2207 tlb_info_32(mon, env);
2208 }
2209}
2210
Blue Swirl1b3cba62010-12-11 18:56:27 +00002211static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2212 int *plast_prot,
2213 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002214{
bellard9746b152004-11-11 18:30:24 +00002215 int prot1;
2216 prot1 = *plast_prot;
2217 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002218 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002219 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2220 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002221 *pstart, end, end - *pstart,
2222 prot1 & PG_USER_MASK ? 'u' : '-',
2223 'r',
2224 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002225 }
2226 if (prot != 0)
2227 *pstart = end;
2228 else
2229 *pstart = -1;
2230 *plast_prot = prot;
2231 }
2232}
2233
Blue Swirl1b3cba62010-12-11 18:56:27 +00002234static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002235{
Austin Clementsb49ca722011-08-14 23:19:21 -04002236 unsigned int l1, l2;
2237 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002238 uint32_t pgd, pde, pte;
2239 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002240
bellardb86bda52004-09-18 19:32:46 +00002241 pgd = env->cr[3] & ~0xfff;
2242 last_prot = 0;
2243 start = -1;
2244 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002245 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002246 pde = le32_to_cpu(pde);
2247 end = l1 << 22;
2248 if (pde & PG_PRESENT_MASK) {
2249 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2250 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002251 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002252 } else {
2253 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002254 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002255 pte = le32_to_cpu(pte);
2256 end = (l1 << 22) + (l2 << 12);
2257 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002258 prot = pte & pde &
2259 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002260 } else {
2261 prot = 0;
2262 }
aliguori376253e2009-03-05 23:01:23 +00002263 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002264 }
2265 }
2266 } else {
2267 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002268 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002269 }
2270 }
Austin Clements8a94b8c2011-08-14 23:22:04 -04002271 /* Flush last range */
2272 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002273}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002274
2275static void mem_info_pae32(Monitor *mon, CPUState *env)
2276{
Austin Clementsb49ca722011-08-14 23:19:21 -04002277 unsigned int l1, l2, l3;
2278 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002279 uint64_t pdpe, pde, pte;
2280 uint64_t pdp_addr, pd_addr, pt_addr;
2281 target_phys_addr_t start, end;
2282
2283 pdp_addr = env->cr[3] & ~0x1f;
2284 last_prot = 0;
2285 start = -1;
2286 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002287 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002288 pdpe = le64_to_cpu(pdpe);
2289 end = l1 << 30;
2290 if (pdpe & PG_PRESENT_MASK) {
2291 pd_addr = pdpe & 0x3fffffffff000ULL;
2292 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002293 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002294 pde = le64_to_cpu(pde);
2295 end = (l1 << 30) + (l2 << 21);
2296 if (pde & PG_PRESENT_MASK) {
2297 if (pde & PG_PSE_MASK) {
2298 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2299 PG_PRESENT_MASK);
2300 mem_print(mon, &start, &last_prot, end, prot);
2301 } else {
2302 pt_addr = pde & 0x3fffffffff000ULL;
2303 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002304 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002305 pte = le64_to_cpu(pte);
2306 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2307 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002308 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2309 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002310 } else {
2311 prot = 0;
2312 }
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 }
2321 } else {
2322 prot = 0;
2323 mem_print(mon, &start, &last_prot, end, prot);
2324 }
2325 }
Austin Clements8a94b8c2011-08-14 23:22:04 -04002326 /* Flush last range */
2327 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002328}
2329
2330
2331#ifdef TARGET_X86_64
2332static void mem_info_64(Monitor *mon, CPUState *env)
2333{
2334 int prot, last_prot;
2335 uint64_t l1, l2, l3, l4;
2336 uint64_t pml4e, pdpe, pde, pte;
2337 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2338
2339 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2340 last_prot = 0;
2341 start = -1;
2342 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002343 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002344 pml4e = le64_to_cpu(pml4e);
2345 end = l1 << 39;
2346 if (pml4e & PG_PRESENT_MASK) {
2347 pdp_addr = pml4e & 0x3fffffffff000ULL;
2348 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002349 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002350 pdpe = le64_to_cpu(pdpe);
2351 end = (l1 << 39) + (l2 << 30);
2352 if (pdpe & PG_PRESENT_MASK) {
2353 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002354 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2355 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002356 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002357 mem_print(mon, &start, &last_prot, end, prot);
2358 } else {
2359 pd_addr = pdpe & 0x3fffffffff000ULL;
2360 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002361 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002362 pde = le64_to_cpu(pde);
2363 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2364 if (pde & PG_PRESENT_MASK) {
2365 if (pde & PG_PSE_MASK) {
2366 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2367 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002368 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002369 mem_print(mon, &start, &last_prot, end, prot);
2370 } else {
2371 pt_addr = pde & 0x3fffffffff000ULL;
2372 for (l4 = 0; l4 < 512; l4++) {
2373 cpu_physical_memory_read(pt_addr
2374 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002375 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002376 pte = le64_to_cpu(pte);
2377 end = (l1 << 39) + (l2 << 30) +
2378 (l3 << 21) + (l4 << 12);
2379 if (pte & PG_PRESENT_MASK) {
2380 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2381 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002382 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002383 } else {
2384 prot = 0;
2385 }
2386 mem_print(mon, &start, &last_prot, end, prot);
2387 }
2388 }
2389 } else {
2390 prot = 0;
2391 mem_print(mon, &start, &last_prot, end, prot);
2392 }
2393 }
2394 }
2395 } else {
2396 prot = 0;
2397 mem_print(mon, &start, &last_prot, end, prot);
2398 }
2399 }
2400 } else {
2401 prot = 0;
2402 mem_print(mon, &start, &last_prot, end, prot);
2403 }
2404 }
Austin Clements8a94b8c2011-08-14 23:22:04 -04002405 /* Flush last range */
2406 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002407}
2408#endif
2409
2410static void mem_info(Monitor *mon)
2411{
2412 CPUState *env;
2413
2414 env = mon_get_cpu();
2415
2416 if (!(env->cr[0] & CR0_PG_MASK)) {
2417 monitor_printf(mon, "PG disabled\n");
2418 return;
2419 }
2420 if (env->cr[4] & CR4_PAE_MASK) {
2421#ifdef TARGET_X86_64
2422 if (env->hflags & HF_LMA_MASK) {
2423 mem_info_64(mon, env);
2424 } else
2425#endif
2426 {
2427 mem_info_pae32(mon, env);
2428 }
2429 } else {
2430 mem_info_32(mon, env);
2431 }
2432}
bellardb86bda52004-09-18 19:32:46 +00002433#endif
2434
aurel327c664e22009-03-03 06:12:22 +00002435#if defined(TARGET_SH4)
2436
aliguori376253e2009-03-05 23:01:23 +00002437static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002438{
aliguori376253e2009-03-05 23:01:23 +00002439 monitor_printf(mon, " tlb%i:\t"
2440 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2441 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2442 "dirty=%hhu writethrough=%hhu\n",
2443 idx,
2444 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2445 tlb->v, tlb->sh, tlb->c, tlb->pr,
2446 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002447}
2448
aliguori376253e2009-03-05 23:01:23 +00002449static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002450{
2451 CPUState *env = mon_get_cpu();
2452 int i;
2453
aliguori376253e2009-03-05 23:01:23 +00002454 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002455 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002456 print_tlb (mon, i, &env->itlb[i]);
2457 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002458 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002459 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002460}
2461
2462#endif
2463
Blue Swirld41160a2010-12-19 13:42:56 +00002464#if defined(TARGET_SPARC)
2465static void tlb_info(Monitor *mon)
2466{
2467 CPUState *env1 = mon_get_cpu();
2468
2469 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2470}
2471#endif
2472
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002473static void do_info_kvm_print(Monitor *mon, const QObject *data)
2474{
2475 QDict *qdict;
2476
2477 qdict = qobject_to_qdict(data);
2478
2479 monitor_printf(mon, "kvm support: ");
2480 if (qdict_get_bool(qdict, "present")) {
2481 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2482 "enabled" : "disabled");
2483 } else {
2484 monitor_printf(mon, "not compiled\n");
2485 }
2486}
2487
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002488static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002489{
2490#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002491 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2492 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002493#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002494 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002495#endif
2496}
2497
aliguori030ea372009-04-21 22:30:47 +00002498static void do_info_numa(Monitor *mon)
2499{
aliguorib28b6232009-04-22 20:20:29 +00002500 int i;
aliguori030ea372009-04-21 22:30:47 +00002501 CPUState *env;
2502
2503 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2504 for (i = 0; i < nb_numa_nodes; i++) {
2505 monitor_printf(mon, "node %d cpus:", i);
2506 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2507 if (env->numa_node == i) {
2508 monitor_printf(mon, " %d", env->cpu_index);
2509 }
2510 }
2511 monitor_printf(mon, "\n");
2512 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2513 node_mem[i] >> 20);
2514 }
2515}
2516
bellard5f1ce942006-02-08 22:40:15 +00002517#ifdef CONFIG_PROFILER
2518
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002519int64_t qemu_time;
2520int64_t dev_time;
2521
aliguori376253e2009-03-05 23:01:23 +00002522static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002523{
2524 int64_t total;
2525 total = qemu_time;
2526 if (total == 0)
2527 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002528 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002529 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002530 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002531 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002532 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002533 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002534}
2535#else
aliguori376253e2009-03-05 23:01:23 +00002536static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002537{
aliguori376253e2009-03-05 23:01:23 +00002538 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002539}
2540#endif
2541
bellardec36b692006-07-16 18:57:03 +00002542/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002543static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002544
aliguori376253e2009-03-05 23:01:23 +00002545static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002546{
2547 int i;
2548 CaptureState *s;
2549
2550 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002551 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002552 s->ops.info (s->opaque);
2553 }
2554}
2555
Blue Swirl23130862009-06-06 08:22:04 +00002556#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002557static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002558{
2559 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002560 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002561 CaptureState *s;
2562
2563 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2564 if (i == n) {
2565 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002566 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002567 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002568 return;
2569 }
2570 }
2571}
2572
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002573static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002574{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002575 const char *path = qdict_get_str(qdict, "path");
2576 int has_freq = qdict_haskey(qdict, "freq");
2577 int freq = qdict_get_try_int(qdict, "freq", -1);
2578 int has_bits = qdict_haskey(qdict, "bits");
2579 int bits = qdict_get_try_int(qdict, "bits", -1);
2580 int has_channels = qdict_haskey(qdict, "nchannels");
2581 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002582 CaptureState *s;
2583
Anthony Liguori7267c092011-08-20 22:09:37 -05002584 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002585
2586 freq = has_freq ? freq : 44100;
2587 bits = has_bits ? bits : 16;
2588 nchannels = has_channels ? nchannels : 2;
2589
2590 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002591 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002592 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002593 return;
bellardec36b692006-07-16 18:57:03 +00002594 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002595 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002596}
2597#endif
2598
aurel32dc1c0b72008-04-27 23:52:12 +00002599#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002600static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002601{
2602 CPUState *env;
2603
2604 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2605 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2606 }
2607
2608 return 0;
2609}
2610#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002611static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002612{
2613 qerror_report(QERR_UNSUPPORTED);
2614 return -1;
2615}
aurel32dc1c0b72008-04-27 23:52:12 +00002616#endif
2617
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002618static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002619{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002620 QDict *qdict;
2621
2622 qdict = qobject_to_qdict(data);
2623
2624 monitor_printf(mon, "VM status: ");
2625 if (qdict_get_bool(qdict, "running")) {
2626 monitor_printf(mon, "running");
2627 if (qdict_get_bool(qdict, "singlestep")) {
2628 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002629 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002630 } else {
2631 monitor_printf(mon, "paused");
2632 }
2633
2634 monitor_printf(mon, "\n");
2635}
2636
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002637static void do_info_status(Monitor *mon, QObject **ret_data)
2638{
2639 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
Luiz Capitulino13548692011-07-29 15:36:43 -03002640 runstate_is_running(), singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002641}
2642
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002643static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002644{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002645 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002646
aliguori76655d62009-03-06 20:27:37 +00002647 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002648 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002649 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002650 return acl;
2651}
aliguori76655d62009-03-06 20:27:37 +00002652
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002653static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002654{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002655 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002656 qemu_acl *acl = find_acl(mon, aclname);
2657 qemu_acl_entry *entry;
2658 int i = 0;
2659
2660 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002661 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002662 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002663 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002664 i++;
2665 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002666 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002667 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002668 }
2669}
2670
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002671static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002672{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002673 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002674 qemu_acl *acl = find_acl(mon, aclname);
2675
2676 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002677 qemu_acl_reset(acl);
2678 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002679 }
2680}
aliguori76655d62009-03-06 20:27:37 +00002681
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002682static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002683{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002684 const char *aclname = qdict_get_str(qdict, "aclname");
2685 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002686 qemu_acl *acl = find_acl(mon, aclname);
2687
2688 if (acl) {
2689 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002690 acl->defaultDeny = 0;
2691 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002692 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002693 acl->defaultDeny = 1;
2694 monitor_printf(mon, "acl: policy set to 'deny'\n");
2695 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002696 monitor_printf(mon, "acl: unknown policy '%s', "
2697 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002698 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002699 }
2700}
aliguori76655d62009-03-06 20:27:37 +00002701
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002702static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002703{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002704 const char *aclname = qdict_get_str(qdict, "aclname");
2705 const char *match = qdict_get_str(qdict, "match");
2706 const char *policy = qdict_get_str(qdict, "policy");
2707 int has_index = qdict_haskey(qdict, "index");
2708 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002709 qemu_acl *acl = find_acl(mon, aclname);
2710 int deny, ret;
2711
2712 if (acl) {
2713 if (strcmp(policy, "allow") == 0) {
2714 deny = 0;
2715 } else if (strcmp(policy, "deny") == 0) {
2716 deny = 1;
2717 } else {
2718 monitor_printf(mon, "acl: unknown policy '%s', "
2719 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002720 return;
2721 }
aliguori28a76be2009-03-06 20:27:40 +00002722 if (has_index)
2723 ret = qemu_acl_insert(acl, deny, match, index);
2724 else
2725 ret = qemu_acl_append(acl, deny, match);
2726 if (ret < 0)
2727 monitor_printf(mon, "acl: unable to add acl entry\n");
2728 else
2729 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002730 }
2731}
aliguori76655d62009-03-06 20:27:37 +00002732
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002733static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002734{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002735 const char *aclname = qdict_get_str(qdict, "aclname");
2736 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002737 qemu_acl *acl = find_acl(mon, aclname);
2738 int ret;
aliguori76655d62009-03-06 20:27:37 +00002739
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002740 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002741 ret = qemu_acl_remove(acl, match);
2742 if (ret < 0)
2743 monitor_printf(mon, "acl: no matching acl entry\n");
2744 else
2745 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002746 }
2747}
2748
Huang Ying79c4f6b2009-06-23 10:05:14 +08002749#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002750static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002751{
2752 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002753 int cpu_index = qdict_get_int(qdict, "cpu_index");
2754 int bank = qdict_get_int(qdict, "bank");
2755 uint64_t status = qdict_get_int(qdict, "status");
2756 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2757 uint64_t addr = qdict_get_int(qdict, "addr");
2758 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002759 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002760
Jan Kiszka747461c2011-03-02 08:56:10 +01002761 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2762 flags |= MCE_INJECT_BROADCAST;
2763 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002764 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002765 if (cenv->cpu_index == cpu_index) {
2766 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002767 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002768 break;
2769 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002770 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002771}
2772#endif
2773
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002774static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002775{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002776 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002777 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002778 int fd;
2779
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002780 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002781 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002782 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002783 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002784 }
2785
2786 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002787 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2788 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002789 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002790 }
2791
Blue Swirl72cf2d42009-09-12 07:36:22 +00002792 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002793 if (strcmp(monfd->name, fdname) != 0) {
2794 continue;
2795 }
2796
2797 close(monfd->fd);
2798 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002799 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002800 }
2801
Anthony Liguori7267c092011-08-20 22:09:37 -05002802 monfd = g_malloc0(sizeof(mon_fd_t));
2803 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002804 monfd->fd = fd;
2805
Blue Swirl72cf2d42009-09-12 07:36:22 +00002806 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002807 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002808}
2809
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002810static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002811{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002812 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002813 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002814
Blue Swirl72cf2d42009-09-12 07:36:22 +00002815 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002816 if (strcmp(monfd->name, fdname) != 0) {
2817 continue;
2818 }
2819
Blue Swirl72cf2d42009-09-12 07:36:22 +00002820 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002821 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002822 g_free(monfd->name);
2823 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002824 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002825 }
2826
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002827 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002828 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002829}
2830
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002831static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002832{
Luiz Capitulino13548692011-07-29 15:36:43 -03002833 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002834 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002835
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002836 vm_stop(RSTATE_RESTORE);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002837
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002838 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002839 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002840 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002841}
2842
Mark McLoughlin7768e042009-07-22 09:11:41 +01002843int monitor_get_fd(Monitor *mon, const char *fdname)
2844{
Anthony Liguoric227f092009-10-01 16:12:16 -05002845 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002846
Blue Swirl72cf2d42009-09-12 07:36:22 +00002847 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002848 int fd;
2849
2850 if (strcmp(monfd->name, fdname) != 0) {
2851 continue;
2852 }
2853
2854 fd = monfd->fd;
2855
2856 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002857 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002858 g_free(monfd->name);
2859 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002860
2861 return fd;
2862 }
2863
2864 return -1;
2865}
2866
Anthony Liguoric227f092009-10-01 16:12:16 -05002867static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002868#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002869 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002870};
2871
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002872/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002873static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002874 {
2875 .name = "version",
2876 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002877 .params = "",
2878 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002879 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002880 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 },
2882 {
2883 .name = "network",
2884 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 .params = "",
2886 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002887 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 },
2889 {
2890 .name = "chardev",
2891 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 .params = "",
2893 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002894 .user_print = qemu_chr_info_print,
2895 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 },
2897 {
2898 .name = "block",
2899 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002900 .params = "",
2901 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002902 .user_print = bdrv_info_print,
2903 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002904 },
2905 {
2906 .name = "blockstats",
2907 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002908 .params = "",
2909 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002910 .user_print = bdrv_stats_print,
2911 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 },
2913 {
2914 .name = "registers",
2915 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002916 .params = "",
2917 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002918 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 },
2920 {
2921 .name = "cpus",
2922 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002923 .params = "",
2924 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002925 .user_print = monitor_print_cpus,
2926 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002927 },
2928 {
2929 .name = "history",
2930 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002931 .params = "",
2932 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002933 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002934 },
2935 {
2936 .name = "irq",
2937 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002938 .params = "",
2939 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002940 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002941 },
2942 {
2943 .name = "pic",
2944 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002945 .params = "",
2946 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002947 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002948 },
2949 {
2950 .name = "pci",
2951 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002952 .params = "",
2953 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002954 .user_print = do_pci_info_print,
2955 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002956 },
Blue Swirld41160a2010-12-19 13:42:56 +00002957#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002958 {
2959 .name = "tlb",
2960 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002961 .params = "",
2962 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002963 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002964 },
aurel327c664e22009-03-03 06:12:22 +00002965#endif
2966#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002967 {
2968 .name = "mem",
2969 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002970 .params = "",
2971 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002972 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002973 },
bellardb86bda52004-09-18 19:32:46 +00002974#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002975 {
2976 .name = "jit",
2977 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002978 .params = "",
2979 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002980 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002981 },
2982 {
2983 .name = "kvm",
2984 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002985 .params = "",
2986 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002987 .user_print = do_info_kvm_print,
2988 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002989 },
2990 {
2991 .name = "numa",
2992 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002993 .params = "",
2994 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002995 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002996 },
2997 {
2998 .name = "usb",
2999 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003000 .params = "",
3001 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003002 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003003 },
3004 {
3005 .name = "usbhost",
3006 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003007 .params = "",
3008 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003009 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003010 },
3011 {
3012 .name = "profile",
3013 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003014 .params = "",
3015 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003016 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003017 },
3018 {
3019 .name = "capture",
3020 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003021 .params = "",
3022 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003023 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003024 },
3025 {
3026 .name = "snapshots",
3027 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003028 .params = "",
3029 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03003030 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003031 },
3032 {
3033 .name = "status",
3034 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003035 .params = "",
3036 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02003037 .user_print = do_info_status_print,
3038 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003039 },
3040 {
3041 .name = "pcmcia",
3042 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003043 .params = "",
3044 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03003045 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003046 },
3047 {
3048 .name = "mice",
3049 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003050 .params = "",
3051 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02003052 .user_print = do_info_mice_print,
3053 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003054 },
3055 {
3056 .name = "vnc",
3057 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003058 .params = "",
3059 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02003060 .user_print = do_info_vnc_print,
3061 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003062 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003063#if defined(CONFIG_SPICE)
3064 {
3065 .name = "spice",
3066 .args_type = "",
3067 .params = "",
3068 .help = "show the spice server status",
3069 .user_print = do_info_spice_print,
3070 .mhandler.info_new = do_info_spice,
3071 },
3072#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003073 {
3074 .name = "name",
3075 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003076 .params = "",
3077 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02003078 .user_print = do_info_name_print,
3079 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003080 },
3081 {
3082 .name = "uuid",
3083 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003084 .params = "",
3085 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02003086 .user_print = do_info_uuid_print,
3087 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003088 },
j_mayer76a66252007-03-07 08:32:30 +00003089#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003090 {
3091 .name = "cpustats",
3092 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003093 .params = "",
3094 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03003095 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003096 },
j_mayer76a66252007-03-07 08:32:30 +00003097#endif
blueswir131a60e22007-10-26 18:42:59 +00003098#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003099 {
3100 .name = "usernet",
3101 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003102 .params = "",
3103 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03003104 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003105 },
blueswir131a60e22007-10-26 18:42:59 +00003106#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003107 {
3108 .name = "migrate",
3109 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003110 .params = "",
3111 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003112 .user_print = do_info_migrate_print,
3113 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003114 },
3115 {
3116 .name = "balloon",
3117 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003118 .params = "",
3119 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003120 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003121 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003122 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003123 },
3124 {
3125 .name = "qtree",
3126 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003127 .params = "",
3128 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003129 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003130 },
3131 {
3132 .name = "qdm",
3133 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003134 .params = "",
3135 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003136 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003137 },
3138 {
3139 .name = "roms",
3140 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003141 .params = "",
3142 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003143 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003144 },
Lluís6d8a7642011-08-31 20:30:43 +02003145#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303146 {
3147 .name = "trace",
3148 .args_type = "",
3149 .params = "",
3150 .help = "show current contents of trace buffer",
3151 .mhandler.info = do_info_trace,
3152 },
Lluís31965ae2011-08-31 20:31:24 +02003153#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303154 {
3155 .name = "trace-events",
3156 .args_type = "",
3157 .params = "",
3158 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003159 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303160 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003161 {
3162 .name = NULL,
3163 },
bellard9dc39cb2004-03-14 21:38:27 +00003164};
3165
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003166static const mon_cmd_t qmp_cmds[] = {
3167#include "qmp-commands.h"
3168 { /* NULL */ },
3169};
3170
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003171static const mon_cmd_t qmp_query_cmds[] = {
3172 {
3173 .name = "version",
3174 .args_type = "",
3175 .params = "",
3176 .help = "show the version of QEMU",
3177 .user_print = do_info_version_print,
3178 .mhandler.info_new = do_info_version,
3179 },
3180 {
3181 .name = "commands",
3182 .args_type = "",
3183 .params = "",
3184 .help = "list QMP available commands",
3185 .user_print = monitor_user_noop,
3186 .mhandler.info_new = do_info_commands,
3187 },
3188 {
3189 .name = "chardev",
3190 .args_type = "",
3191 .params = "",
3192 .help = "show the character devices",
3193 .user_print = qemu_chr_info_print,
3194 .mhandler.info_new = qemu_chr_info,
3195 },
3196 {
3197 .name = "block",
3198 .args_type = "",
3199 .params = "",
3200 .help = "show the block devices",
3201 .user_print = bdrv_info_print,
3202 .mhandler.info_new = bdrv_info,
3203 },
3204 {
3205 .name = "blockstats",
3206 .args_type = "",
3207 .params = "",
3208 .help = "show block device statistics",
3209 .user_print = bdrv_stats_print,
3210 .mhandler.info_new = bdrv_info_stats,
3211 },
3212 {
3213 .name = "cpus",
3214 .args_type = "",
3215 .params = "",
3216 .help = "show infos for each CPU",
3217 .user_print = monitor_print_cpus,
3218 .mhandler.info_new = do_info_cpus,
3219 },
3220 {
3221 .name = "pci",
3222 .args_type = "",
3223 .params = "",
3224 .help = "show PCI info",
3225 .user_print = do_pci_info_print,
3226 .mhandler.info_new = do_pci_info,
3227 },
3228 {
3229 .name = "kvm",
3230 .args_type = "",
3231 .params = "",
3232 .help = "show KVM information",
3233 .user_print = do_info_kvm_print,
3234 .mhandler.info_new = do_info_kvm,
3235 },
3236 {
3237 .name = "status",
3238 .args_type = "",
3239 .params = "",
3240 .help = "show the current VM status (running|paused)",
3241 .user_print = do_info_status_print,
3242 .mhandler.info_new = do_info_status,
3243 },
3244 {
3245 .name = "mice",
3246 .args_type = "",
3247 .params = "",
3248 .help = "show which guest mouse is receiving events",
3249 .user_print = do_info_mice_print,
3250 .mhandler.info_new = do_info_mice,
3251 },
3252 {
3253 .name = "vnc",
3254 .args_type = "",
3255 .params = "",
3256 .help = "show the vnc server status",
3257 .user_print = do_info_vnc_print,
3258 .mhandler.info_new = do_info_vnc,
3259 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003260#if defined(CONFIG_SPICE)
3261 {
3262 .name = "spice",
3263 .args_type = "",
3264 .params = "",
3265 .help = "show the spice server status",
3266 .user_print = do_info_spice_print,
3267 .mhandler.info_new = do_info_spice,
3268 },
3269#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003270 {
3271 .name = "name",
3272 .args_type = "",
3273 .params = "",
3274 .help = "show the current VM name",
3275 .user_print = do_info_name_print,
3276 .mhandler.info_new = do_info_name,
3277 },
3278 {
3279 .name = "uuid",
3280 .args_type = "",
3281 .params = "",
3282 .help = "show the current VM UUID",
3283 .user_print = do_info_uuid_print,
3284 .mhandler.info_new = do_info_uuid,
3285 },
3286 {
3287 .name = "migrate",
3288 .args_type = "",
3289 .params = "",
3290 .help = "show migration status",
3291 .user_print = do_info_migrate_print,
3292 .mhandler.info_new = do_info_migrate,
3293 },
3294 {
3295 .name = "balloon",
3296 .args_type = "",
3297 .params = "",
3298 .help = "show balloon information",
3299 .user_print = monitor_print_balloon,
3300 .mhandler.info_async = do_info_balloon,
3301 .flags = MONITOR_CMD_ASYNC,
3302 },
3303 { /* NULL */ },
3304};
3305
bellard9307c4c2004-04-04 12:57:25 +00003306/*******************************************************************/
3307
3308static const char *pch;
3309static jmp_buf expr_env;
3310
bellard92a31b12005-02-10 22:00:52 +00003311#define MD_TLONG 0
3312#define MD_I32 1
3313
bellard9307c4c2004-04-04 12:57:25 +00003314typedef struct MonitorDef {
3315 const char *name;
3316 int offset;
blueswir18662d652008-10-02 18:32:44 +00003317 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003318 int type;
bellard9307c4c2004-04-04 12:57:25 +00003319} MonitorDef;
3320
bellard57206fd2004-04-25 18:54:52 +00003321#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003322static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003323{
bellard6a00d602005-11-21 23:25:50 +00003324 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003325 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003326}
3327#endif
3328
bellarda541f292004-04-12 20:39:29 +00003329#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003330static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003331{
bellard6a00d602005-11-21 23:25:50 +00003332 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003333 unsigned int u;
3334 int i;
3335
3336 u = 0;
3337 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003338 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003339
3340 return u;
3341}
3342
blueswir18662d652008-10-02 18:32:44 +00003343static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003344{
bellard6a00d602005-11-21 23:25:50 +00003345 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003346 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003347}
3348
blueswir18662d652008-10-02 18:32:44 +00003349static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003350{
bellard6a00d602005-11-21 23:25:50 +00003351 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003352 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003353}
bellard9fddaa02004-05-21 12:59:32 +00003354
blueswir18662d652008-10-02 18:32:44 +00003355static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003356{
bellard6a00d602005-11-21 23:25:50 +00003357 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003358 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003359}
3360
blueswir18662d652008-10-02 18:32:44 +00003361static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003362{
bellard6a00d602005-11-21 23:25:50 +00003363 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003364 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003365}
3366
blueswir18662d652008-10-02 18:32:44 +00003367static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003368{
bellard6a00d602005-11-21 23:25:50 +00003369 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003370 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003371}
bellarda541f292004-04-12 20:39:29 +00003372#endif
3373
bellarde95c8d52004-09-30 22:22:08 +00003374#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003375#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003376static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003377{
bellard6a00d602005-11-21 23:25:50 +00003378 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003379
3380 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003381}
bellard7b936c02005-10-30 17:05:13 +00003382#endif
bellarde95c8d52004-09-30 22:22:08 +00003383
blueswir18662d652008-10-02 18:32:44 +00003384static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003385{
bellard6a00d602005-11-21 23:25:50 +00003386 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003387 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003388}
3389#endif
3390
blueswir18662d652008-10-02 18:32:44 +00003391static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003392#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003393
3394#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003395 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003396 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003397 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003398
bellard9307c4c2004-04-04 12:57:25 +00003399 { "eax", offsetof(CPUState, regs[0]) },
3400 { "ecx", offsetof(CPUState, regs[1]) },
3401 { "edx", offsetof(CPUState, regs[2]) },
3402 { "ebx", offsetof(CPUState, regs[3]) },
3403 { "esp|sp", offsetof(CPUState, regs[4]) },
3404 { "ebp|fp", offsetof(CPUState, regs[5]) },
3405 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003406 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003407#ifdef TARGET_X86_64
3408 { "r8", offsetof(CPUState, regs[8]) },
3409 { "r9", offsetof(CPUState, regs[9]) },
3410 { "r10", offsetof(CPUState, regs[10]) },
3411 { "r11", offsetof(CPUState, regs[11]) },
3412 { "r12", offsetof(CPUState, regs[12]) },
3413 { "r13", offsetof(CPUState, regs[13]) },
3414 { "r14", offsetof(CPUState, regs[14]) },
3415 { "r15", offsetof(CPUState, regs[15]) },
3416#endif
bellard9307c4c2004-04-04 12:57:25 +00003417 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003418 { "eip", offsetof(CPUState, eip) },
3419 SEG("cs", R_CS)
3420 SEG("ds", R_DS)
3421 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003422 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003423 SEG("fs", R_FS)
3424 SEG("gs", R_GS)
3425 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003426#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003427 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003428 { "r0", offsetof(CPUState, gpr[0]) },
3429 { "r1", offsetof(CPUState, gpr[1]) },
3430 { "r2", offsetof(CPUState, gpr[2]) },
3431 { "r3", offsetof(CPUState, gpr[3]) },
3432 { "r4", offsetof(CPUState, gpr[4]) },
3433 { "r5", offsetof(CPUState, gpr[5]) },
3434 { "r6", offsetof(CPUState, gpr[6]) },
3435 { "r7", offsetof(CPUState, gpr[7]) },
3436 { "r8", offsetof(CPUState, gpr[8]) },
3437 { "r9", offsetof(CPUState, gpr[9]) },
3438 { "r10", offsetof(CPUState, gpr[10]) },
3439 { "r11", offsetof(CPUState, gpr[11]) },
3440 { "r12", offsetof(CPUState, gpr[12]) },
3441 { "r13", offsetof(CPUState, gpr[13]) },
3442 { "r14", offsetof(CPUState, gpr[14]) },
3443 { "r15", offsetof(CPUState, gpr[15]) },
3444 { "r16", offsetof(CPUState, gpr[16]) },
3445 { "r17", offsetof(CPUState, gpr[17]) },
3446 { "r18", offsetof(CPUState, gpr[18]) },
3447 { "r19", offsetof(CPUState, gpr[19]) },
3448 { "r20", offsetof(CPUState, gpr[20]) },
3449 { "r21", offsetof(CPUState, gpr[21]) },
3450 { "r22", offsetof(CPUState, gpr[22]) },
3451 { "r23", offsetof(CPUState, gpr[23]) },
3452 { "r24", offsetof(CPUState, gpr[24]) },
3453 { "r25", offsetof(CPUState, gpr[25]) },
3454 { "r26", offsetof(CPUState, gpr[26]) },
3455 { "r27", offsetof(CPUState, gpr[27]) },
3456 { "r28", offsetof(CPUState, gpr[28]) },
3457 { "r29", offsetof(CPUState, gpr[29]) },
3458 { "r30", offsetof(CPUState, gpr[30]) },
3459 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003460 /* Floating point registers */
3461 { "f0", offsetof(CPUState, fpr[0]) },
3462 { "f1", offsetof(CPUState, fpr[1]) },
3463 { "f2", offsetof(CPUState, fpr[2]) },
3464 { "f3", offsetof(CPUState, fpr[3]) },
3465 { "f4", offsetof(CPUState, fpr[4]) },
3466 { "f5", offsetof(CPUState, fpr[5]) },
3467 { "f6", offsetof(CPUState, fpr[6]) },
3468 { "f7", offsetof(CPUState, fpr[7]) },
3469 { "f8", offsetof(CPUState, fpr[8]) },
3470 { "f9", offsetof(CPUState, fpr[9]) },
3471 { "f10", offsetof(CPUState, fpr[10]) },
3472 { "f11", offsetof(CPUState, fpr[11]) },
3473 { "f12", offsetof(CPUState, fpr[12]) },
3474 { "f13", offsetof(CPUState, fpr[13]) },
3475 { "f14", offsetof(CPUState, fpr[14]) },
3476 { "f15", offsetof(CPUState, fpr[15]) },
3477 { "f16", offsetof(CPUState, fpr[16]) },
3478 { "f17", offsetof(CPUState, fpr[17]) },
3479 { "f18", offsetof(CPUState, fpr[18]) },
3480 { "f19", offsetof(CPUState, fpr[19]) },
3481 { "f20", offsetof(CPUState, fpr[20]) },
3482 { "f21", offsetof(CPUState, fpr[21]) },
3483 { "f22", offsetof(CPUState, fpr[22]) },
3484 { "f23", offsetof(CPUState, fpr[23]) },
3485 { "f24", offsetof(CPUState, fpr[24]) },
3486 { "f25", offsetof(CPUState, fpr[25]) },
3487 { "f26", offsetof(CPUState, fpr[26]) },
3488 { "f27", offsetof(CPUState, fpr[27]) },
3489 { "f28", offsetof(CPUState, fpr[28]) },
3490 { "f29", offsetof(CPUState, fpr[29]) },
3491 { "f30", offsetof(CPUState, fpr[30]) },
3492 { "f31", offsetof(CPUState, fpr[31]) },
3493 { "fpscr", offsetof(CPUState, fpscr) },
3494 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003495 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003496 { "lr", offsetof(CPUState, lr) },
3497 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003498 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003499 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003500 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003501 { "msr", 0, &monitor_get_msr, },
3502 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003503 { "tbu", 0, &monitor_get_tbu, },
3504 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003505#if defined(TARGET_PPC64)
3506 /* Address space register */
3507 { "asr", offsetof(CPUState, asr) },
3508#endif
3509 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003510 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003511 { "sr0", offsetof(CPUState, sr[0]) },
3512 { "sr1", offsetof(CPUState, sr[1]) },
3513 { "sr2", offsetof(CPUState, sr[2]) },
3514 { "sr3", offsetof(CPUState, sr[3]) },
3515 { "sr4", offsetof(CPUState, sr[4]) },
3516 { "sr5", offsetof(CPUState, sr[5]) },
3517 { "sr6", offsetof(CPUState, sr[6]) },
3518 { "sr7", offsetof(CPUState, sr[7]) },
3519 { "sr8", offsetof(CPUState, sr[8]) },
3520 { "sr9", offsetof(CPUState, sr[9]) },
3521 { "sr10", offsetof(CPUState, sr[10]) },
3522 { "sr11", offsetof(CPUState, sr[11]) },
3523 { "sr12", offsetof(CPUState, sr[12]) },
3524 { "sr13", offsetof(CPUState, sr[13]) },
3525 { "sr14", offsetof(CPUState, sr[14]) },
3526 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003527 /* Too lazy to put BATs... */
3528 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3529
3530 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3531 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3532 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3533 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3534 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3535 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3536 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3537 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3538 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3539 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3540 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3541 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3542 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3543 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3544 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3545 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3546 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3547 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3548 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3549 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3550 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3551 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3552 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3553 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3554 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3555 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3556 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3557 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3558 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3559 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3560 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3561 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3562 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3563 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3564 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3565 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3566 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3567 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3568 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3569 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3570 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3571 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3572 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3573 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3574 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3575 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3576 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3577 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3578 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3579 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3580 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3581 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3582 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3583 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3584 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3585 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3586 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3587 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3588 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3589 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3590 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3591 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3592 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3593 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3594 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3595 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3596
bellarde95c8d52004-09-30 22:22:08 +00003597#elif defined(TARGET_SPARC)
3598 { "g0", offsetof(CPUState, gregs[0]) },
3599 { "g1", offsetof(CPUState, gregs[1]) },
3600 { "g2", offsetof(CPUState, gregs[2]) },
3601 { "g3", offsetof(CPUState, gregs[3]) },
3602 { "g4", offsetof(CPUState, gregs[4]) },
3603 { "g5", offsetof(CPUState, gregs[5]) },
3604 { "g6", offsetof(CPUState, gregs[6]) },
3605 { "g7", offsetof(CPUState, gregs[7]) },
3606 { "o0", 0, monitor_get_reg },
3607 { "o1", 1, monitor_get_reg },
3608 { "o2", 2, monitor_get_reg },
3609 { "o3", 3, monitor_get_reg },
3610 { "o4", 4, monitor_get_reg },
3611 { "o5", 5, monitor_get_reg },
3612 { "o6", 6, monitor_get_reg },
3613 { "o7", 7, monitor_get_reg },
3614 { "l0", 8, monitor_get_reg },
3615 { "l1", 9, monitor_get_reg },
3616 { "l2", 10, monitor_get_reg },
3617 { "l3", 11, monitor_get_reg },
3618 { "l4", 12, monitor_get_reg },
3619 { "l5", 13, monitor_get_reg },
3620 { "l6", 14, monitor_get_reg },
3621 { "l7", 15, monitor_get_reg },
3622 { "i0", 16, monitor_get_reg },
3623 { "i1", 17, monitor_get_reg },
3624 { "i2", 18, monitor_get_reg },
3625 { "i3", 19, monitor_get_reg },
3626 { "i4", 20, monitor_get_reg },
3627 { "i5", 21, monitor_get_reg },
3628 { "i6", 22, monitor_get_reg },
3629 { "i7", 23, monitor_get_reg },
3630 { "pc", offsetof(CPUState, pc) },
3631 { "npc", offsetof(CPUState, npc) },
3632 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003633#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003634 { "psr", 0, &monitor_get_psr, },
3635 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003636#endif
bellarde95c8d52004-09-30 22:22:08 +00003637 { "tbr", offsetof(CPUState, tbr) },
3638 { "fsr", offsetof(CPUState, fsr) },
3639 { "f0", offsetof(CPUState, fpr[0]) },
3640 { "f1", offsetof(CPUState, fpr[1]) },
3641 { "f2", offsetof(CPUState, fpr[2]) },
3642 { "f3", offsetof(CPUState, fpr[3]) },
3643 { "f4", offsetof(CPUState, fpr[4]) },
3644 { "f5", offsetof(CPUState, fpr[5]) },
3645 { "f6", offsetof(CPUState, fpr[6]) },
3646 { "f7", offsetof(CPUState, fpr[7]) },
3647 { "f8", offsetof(CPUState, fpr[8]) },
3648 { "f9", offsetof(CPUState, fpr[9]) },
3649 { "f10", offsetof(CPUState, fpr[10]) },
3650 { "f11", offsetof(CPUState, fpr[11]) },
3651 { "f12", offsetof(CPUState, fpr[12]) },
3652 { "f13", offsetof(CPUState, fpr[13]) },
3653 { "f14", offsetof(CPUState, fpr[14]) },
3654 { "f15", offsetof(CPUState, fpr[15]) },
3655 { "f16", offsetof(CPUState, fpr[16]) },
3656 { "f17", offsetof(CPUState, fpr[17]) },
3657 { "f18", offsetof(CPUState, fpr[18]) },
3658 { "f19", offsetof(CPUState, fpr[19]) },
3659 { "f20", offsetof(CPUState, fpr[20]) },
3660 { "f21", offsetof(CPUState, fpr[21]) },
3661 { "f22", offsetof(CPUState, fpr[22]) },
3662 { "f23", offsetof(CPUState, fpr[23]) },
3663 { "f24", offsetof(CPUState, fpr[24]) },
3664 { "f25", offsetof(CPUState, fpr[25]) },
3665 { "f26", offsetof(CPUState, fpr[26]) },
3666 { "f27", offsetof(CPUState, fpr[27]) },
3667 { "f28", offsetof(CPUState, fpr[28]) },
3668 { "f29", offsetof(CPUState, fpr[29]) },
3669 { "f30", offsetof(CPUState, fpr[30]) },
3670 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003671#ifdef TARGET_SPARC64
3672 { "f32", offsetof(CPUState, fpr[32]) },
3673 { "f34", offsetof(CPUState, fpr[34]) },
3674 { "f36", offsetof(CPUState, fpr[36]) },
3675 { "f38", offsetof(CPUState, fpr[38]) },
3676 { "f40", offsetof(CPUState, fpr[40]) },
3677 { "f42", offsetof(CPUState, fpr[42]) },
3678 { "f44", offsetof(CPUState, fpr[44]) },
3679 { "f46", offsetof(CPUState, fpr[46]) },
3680 { "f48", offsetof(CPUState, fpr[48]) },
3681 { "f50", offsetof(CPUState, fpr[50]) },
3682 { "f52", offsetof(CPUState, fpr[52]) },
3683 { "f54", offsetof(CPUState, fpr[54]) },
3684 { "f56", offsetof(CPUState, fpr[56]) },
3685 { "f58", offsetof(CPUState, fpr[58]) },
3686 { "f60", offsetof(CPUState, fpr[60]) },
3687 { "f62", offsetof(CPUState, fpr[62]) },
3688 { "asi", offsetof(CPUState, asi) },
3689 { "pstate", offsetof(CPUState, pstate) },
3690 { "cansave", offsetof(CPUState, cansave) },
3691 { "canrestore", offsetof(CPUState, canrestore) },
3692 { "otherwin", offsetof(CPUState, otherwin) },
3693 { "wstate", offsetof(CPUState, wstate) },
3694 { "cleanwin", offsetof(CPUState, cleanwin) },
3695 { "fprs", offsetof(CPUState, fprs) },
3696#endif
bellard9307c4c2004-04-04 12:57:25 +00003697#endif
3698 { NULL },
3699};
3700
aliguori376253e2009-03-05 23:01:23 +00003701static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003702{
aliguori376253e2009-03-05 23:01:23 +00003703 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003704 longjmp(expr_env, 1);
3705}
3706
Markus Armbruster09b94182010-01-20 13:07:30 +01003707/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003708static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003709{
blueswir18662d652008-10-02 18:32:44 +00003710 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003711 void *ptr;
3712
bellard9307c4c2004-04-04 12:57:25 +00003713 for(md = monitor_defs; md->name != NULL; md++) {
3714 if (compare_cmd(name, md->name)) {
3715 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003716 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003717 } else {
bellard6a00d602005-11-21 23:25:50 +00003718 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003719 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003720 switch(md->type) {
3721 case MD_I32:
3722 *pval = *(int32_t *)ptr;
3723 break;
3724 case MD_TLONG:
3725 *pval = *(target_long *)ptr;
3726 break;
3727 default:
3728 *pval = 0;
3729 break;
3730 }
bellard9307c4c2004-04-04 12:57:25 +00003731 }
3732 return 0;
3733 }
3734 }
3735 return -1;
3736}
3737
3738static void next(void)
3739{
Blue Swirl660f11b2009-07-31 21:16:51 +00003740 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003741 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003742 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003743 pch++;
3744 }
3745}
3746
aliguori376253e2009-03-05 23:01:23 +00003747static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003748
aliguori376253e2009-03-05 23:01:23 +00003749static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003750{
blueswir1c2efc952007-09-25 17:28:42 +00003751 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003752 char *p;
bellard6a00d602005-11-21 23:25:50 +00003753 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003754
3755 switch(*pch) {
3756 case '+':
3757 next();
aliguori376253e2009-03-05 23:01:23 +00003758 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003759 break;
3760 case '-':
3761 next();
aliguori376253e2009-03-05 23:01:23 +00003762 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003763 break;
3764 case '~':
3765 next();
aliguori376253e2009-03-05 23:01:23 +00003766 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003767 break;
3768 case '(':
3769 next();
aliguori376253e2009-03-05 23:01:23 +00003770 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003771 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003772 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003773 }
3774 next();
3775 break;
bellard81d09122004-07-14 17:21:37 +00003776 case '\'':
3777 pch++;
3778 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003779 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003780 n = *pch;
3781 pch++;
3782 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003783 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003784 next();
3785 break;
bellard9307c4c2004-04-04 12:57:25 +00003786 case '$':
3787 {
3788 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003789 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003790
bellard9307c4c2004-04-04 12:57:25 +00003791 pch++;
3792 q = buf;
3793 while ((*pch >= 'a' && *pch <= 'z') ||
3794 (*pch >= 'A' && *pch <= 'Z') ||
3795 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003796 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003797 if ((q - buf) < sizeof(buf) - 1)
3798 *q++ = *pch;
3799 pch++;
3800 }
blueswir1cd390082008-11-16 13:53:32 +00003801 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003802 pch++;
3803 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003804 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003805 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003806 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003807 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003808 }
3809 break;
3810 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003811 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003812 n = 0;
3813 break;
3814 default:
blueswir17743e582007-09-24 18:39:04 +00003815#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003816 n = strtoull(pch, &p, 0);
3817#else
bellard9307c4c2004-04-04 12:57:25 +00003818 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003819#endif
bellard9307c4c2004-04-04 12:57:25 +00003820 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003821 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003822 }
3823 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003824 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003825 pch++;
3826 break;
3827 }
3828 return n;
3829}
3830
3831
aliguori376253e2009-03-05 23:01:23 +00003832static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003833{
blueswir1c2efc952007-09-25 17:28:42 +00003834 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003835 int op;
ths3b46e622007-09-17 08:09:54 +00003836
aliguori376253e2009-03-05 23:01:23 +00003837 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003838 for(;;) {
3839 op = *pch;
3840 if (op != '*' && op != '/' && op != '%')
3841 break;
3842 next();
aliguori376253e2009-03-05 23:01:23 +00003843 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003844 switch(op) {
3845 default:
3846 case '*':
3847 val *= val2;
3848 break;
3849 case '/':
3850 case '%':
ths5fafdf22007-09-16 21:08:06 +00003851 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003852 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003853 if (op == '/')
3854 val /= val2;
3855 else
3856 val %= val2;
3857 break;
3858 }
3859 }
3860 return val;
3861}
3862
aliguori376253e2009-03-05 23:01:23 +00003863static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003864{
blueswir1c2efc952007-09-25 17:28:42 +00003865 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003866 int op;
bellard9307c4c2004-04-04 12:57:25 +00003867
aliguori376253e2009-03-05 23:01:23 +00003868 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003869 for(;;) {
3870 op = *pch;
3871 if (op != '&' && op != '|' && op != '^')
3872 break;
3873 next();
aliguori376253e2009-03-05 23:01:23 +00003874 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003875 switch(op) {
3876 default:
3877 case '&':
3878 val &= val2;
3879 break;
3880 case '|':
3881 val |= val2;
3882 break;
3883 case '^':
3884 val ^= val2;
3885 break;
3886 }
3887 }
3888 return val;
3889}
3890
aliguori376253e2009-03-05 23:01:23 +00003891static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003892{
blueswir1c2efc952007-09-25 17:28:42 +00003893 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003894 int op;
bellard9307c4c2004-04-04 12:57:25 +00003895
aliguori376253e2009-03-05 23:01:23 +00003896 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003897 for(;;) {
3898 op = *pch;
3899 if (op != '+' && op != '-')
3900 break;
3901 next();
aliguori376253e2009-03-05 23:01:23 +00003902 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003903 if (op == '+')
3904 val += val2;
3905 else
3906 val -= val2;
3907 }
3908 return val;
3909}
3910
aliguori376253e2009-03-05 23:01:23 +00003911static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003912{
3913 pch = *pp;
3914 if (setjmp(expr_env)) {
3915 *pp = pch;
3916 return -1;
3917 }
blueswir1cd390082008-11-16 13:53:32 +00003918 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003919 pch++;
aliguori376253e2009-03-05 23:01:23 +00003920 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003921 *pp = pch;
3922 return 0;
3923}
3924
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003925static int get_double(Monitor *mon, double *pval, const char **pp)
3926{
3927 const char *p = *pp;
3928 char *tailp;
3929 double d;
3930
3931 d = strtod(p, &tailp);
3932 if (tailp == p) {
3933 monitor_printf(mon, "Number expected\n");
3934 return -1;
3935 }
3936 if (d != d || d - d != 0) {
3937 /* NaN or infinity */
3938 monitor_printf(mon, "Bad number\n");
3939 return -1;
3940 }
3941 *pval = d;
3942 *pp = tailp;
3943 return 0;
3944}
3945
bellard9307c4c2004-04-04 12:57:25 +00003946static int get_str(char *buf, int buf_size, const char **pp)
3947{
3948 const char *p;
3949 char *q;
3950 int c;
3951
bellard81d09122004-07-14 17:21:37 +00003952 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003953 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003954 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003955 p++;
3956 if (*p == '\0') {
3957 fail:
bellard81d09122004-07-14 17:21:37 +00003958 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003959 *pp = p;
3960 return -1;
3961 }
bellard9307c4c2004-04-04 12:57:25 +00003962 if (*p == '\"') {
3963 p++;
3964 while (*p != '\0' && *p != '\"') {
3965 if (*p == '\\') {
3966 p++;
3967 c = *p++;
3968 switch(c) {
3969 case 'n':
3970 c = '\n';
3971 break;
3972 case 'r':
3973 c = '\r';
3974 break;
3975 case '\\':
3976 case '\'':
3977 case '\"':
3978 break;
3979 default:
3980 qemu_printf("unsupported escape code: '\\%c'\n", c);
3981 goto fail;
3982 }
3983 if ((q - buf) < buf_size - 1) {
3984 *q++ = c;
3985 }
3986 } else {
3987 if ((q - buf) < buf_size - 1) {
3988 *q++ = *p;
3989 }
3990 p++;
3991 }
3992 }
3993 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003994 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003995 goto fail;
3996 }
3997 p++;
3998 } else {
blueswir1cd390082008-11-16 13:53:32 +00003999 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004000 if ((q - buf) < buf_size - 1) {
4001 *q++ = *p;
4002 }
4003 p++;
4004 }
bellard9307c4c2004-04-04 12:57:25 +00004005 }
bellard81d09122004-07-14 17:21:37 +00004006 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00004007 *pp = p;
4008 return 0;
4009}
4010
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004011/*
4012 * Store the command-name in cmdname, and return a pointer to
4013 * the remaining of the command string.
4014 */
4015static const char *get_command_name(const char *cmdline,
4016 char *cmdname, size_t nlen)
4017{
4018 size_t len;
4019 const char *p, *pstart;
4020
4021 p = cmdline;
4022 while (qemu_isspace(*p))
4023 p++;
4024 if (*p == '\0')
4025 return NULL;
4026 pstart = p;
4027 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
4028 p++;
4029 len = p - pstart;
4030 if (len > nlen - 1)
4031 len = nlen - 1;
4032 memcpy(cmdname, pstart, len);
4033 cmdname[len] = '\0';
4034 return p;
4035}
4036
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004037/**
4038 * Read key of 'type' into 'key' and return the current
4039 * 'type' pointer.
4040 */
4041static char *key_get_info(const char *type, char **key)
4042{
4043 size_t len;
4044 char *p, *str;
4045
4046 if (*type == ',')
4047 type++;
4048
4049 p = strchr(type, ':');
4050 if (!p) {
4051 *key = NULL;
4052 return NULL;
4053 }
4054 len = p - type;
4055
Anthony Liguori7267c092011-08-20 22:09:37 -05004056 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004057 memcpy(str, type, len);
4058 str[len] = '\0';
4059
4060 *key = str;
4061 return ++p;
4062}
4063
bellard9307c4c2004-04-04 12:57:25 +00004064static int default_fmt_format = 'x';
4065static int default_fmt_size = 4;
4066
4067#define MAX_ARGS 16
4068
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004069static int is_valid_option(const char *c, const char *typestr)
4070{
4071 char option[3];
4072
4073 option[0] = '-';
4074 option[1] = *c;
4075 option[2] = '\0';
4076
4077 typestr = strstr(typestr, option);
4078 return (typestr != NULL);
4079}
4080
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004081static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
4082 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004083{
4084 const mon_cmd_t *cmd;
4085
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004086 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004087 if (compare_cmd(cmdname, cmd->name)) {
4088 return cmd;
4089 }
4090 }
4091
4092 return NULL;
4093}
4094
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004095static const mon_cmd_t *monitor_find_command(const char *cmdname)
4096{
4097 return search_dispatch_table(mon_cmds, cmdname);
4098}
4099
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004100static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
4101{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03004102 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004103}
4104
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004105static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
4106{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03004107 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004108}
4109
Anthony Liguoric227f092009-10-01 16:12:16 -05004110static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004111 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004112 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00004113{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004114 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03004115 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05004116 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00004117 char cmdname[256];
4118 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004119 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00004120
4121#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00004122 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00004123#endif
ths3b46e622007-09-17 08:09:54 +00004124
bellard9307c4c2004-04-04 12:57:25 +00004125 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004126 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
4127 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004128 return NULL;
ths3b46e622007-09-17 08:09:54 +00004129
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004130 cmd = monitor_find_command(cmdname);
4131 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004132 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004133 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004134 }
bellard9307c4c2004-04-04 12:57:25 +00004135
bellard9307c4c2004-04-04 12:57:25 +00004136 /* parse the parameters */
4137 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00004138 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004139 typestr = key_get_info(typestr, &key);
4140 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00004141 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004142 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00004143 typestr++;
4144 switch(c) {
4145 case 'F':
bellard81d09122004-07-14 17:21:37 +00004146 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00004147 case 's':
4148 {
4149 int ret;
ths3b46e622007-09-17 08:09:54 +00004150
blueswir1cd390082008-11-16 13:53:32 +00004151 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004152 p++;
4153 if (*typestr == '?') {
4154 typestr++;
4155 if (*p == '\0') {
4156 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03004157 break;
bellard9307c4c2004-04-04 12:57:25 +00004158 }
4159 }
4160 ret = get_str(buf, sizeof(buf), &p);
4161 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00004162 switch(c) {
4163 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004164 monitor_printf(mon, "%s: filename expected\n",
4165 cmdname);
bellard81d09122004-07-14 17:21:37 +00004166 break;
4167 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004168 monitor_printf(mon, "%s: block device name expected\n",
4169 cmdname);
bellard81d09122004-07-14 17:21:37 +00004170 break;
4171 default:
aliguori376253e2009-03-05 23:01:23 +00004172 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004173 break;
4174 }
bellard9307c4c2004-04-04 12:57:25 +00004175 goto fail;
4176 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004177 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004178 }
4179 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004180 case 'O':
4181 {
4182 QemuOptsList *opts_list;
4183 QemuOpts *opts;
4184
4185 opts_list = qemu_find_opts(key);
4186 if (!opts_list || opts_list->desc->name) {
4187 goto bad_type;
4188 }
4189 while (qemu_isspace(*p)) {
4190 p++;
4191 }
4192 if (!*p)
4193 break;
4194 if (get_str(buf, sizeof(buf), &p) < 0) {
4195 goto fail;
4196 }
4197 opts = qemu_opts_parse(opts_list, buf, 1);
4198 if (!opts) {
4199 goto fail;
4200 }
4201 qemu_opts_to_qdict(opts, qdict);
4202 qemu_opts_del(opts);
4203 }
4204 break;
bellard9307c4c2004-04-04 12:57:25 +00004205 case '/':
4206 {
4207 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004208
blueswir1cd390082008-11-16 13:53:32 +00004209 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004210 p++;
4211 if (*p == '/') {
4212 /* format found */
4213 p++;
4214 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004215 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004216 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004217 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004218 count = count * 10 + (*p - '0');
4219 p++;
4220 }
4221 }
4222 size = -1;
4223 format = -1;
4224 for(;;) {
4225 switch(*p) {
4226 case 'o':
4227 case 'd':
4228 case 'u':
4229 case 'x':
4230 case 'i':
4231 case 'c':
4232 format = *p++;
4233 break;
4234 case 'b':
4235 size = 1;
4236 p++;
4237 break;
4238 case 'h':
4239 size = 2;
4240 p++;
4241 break;
4242 case 'w':
4243 size = 4;
4244 p++;
4245 break;
4246 case 'g':
4247 case 'L':
4248 size = 8;
4249 p++;
4250 break;
4251 default:
4252 goto next;
4253 }
4254 }
4255 next:
blueswir1cd390082008-11-16 13:53:32 +00004256 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004257 monitor_printf(mon, "invalid char in format: '%c'\n",
4258 *p);
bellard9307c4c2004-04-04 12:57:25 +00004259 goto fail;
4260 }
bellard9307c4c2004-04-04 12:57:25 +00004261 if (format < 0)
4262 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004263 if (format != 'i') {
4264 /* for 'i', not specifying a size gives -1 as size */
4265 if (size < 0)
4266 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004267 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004268 }
bellard9307c4c2004-04-04 12:57:25 +00004269 default_fmt_format = format;
4270 } else {
4271 count = 1;
4272 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004273 if (format != 'i') {
4274 size = default_fmt_size;
4275 } else {
4276 size = -1;
4277 }
bellard9307c4c2004-04-04 12:57:25 +00004278 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004279 qdict_put(qdict, "count", qint_from_int(count));
4280 qdict_put(qdict, "format", qint_from_int(format));
4281 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004282 }
4283 break;
4284 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004285 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004286 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004287 {
blueswir1c2efc952007-09-25 17:28:42 +00004288 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004289
blueswir1cd390082008-11-16 13:53:32 +00004290 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004291 p++;
bellard34405572004-06-08 00:55:58 +00004292 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004293 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004294 if (*p == '\0') {
4295 typestr++;
4296 break;
4297 }
bellard34405572004-06-08 00:55:58 +00004298 } else {
4299 if (*p == '.') {
4300 p++;
blueswir1cd390082008-11-16 13:53:32 +00004301 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004302 p++;
bellard34405572004-06-08 00:55:58 +00004303 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004304 typestr++;
4305 break;
bellard34405572004-06-08 00:55:58 +00004306 }
4307 }
bellard13224a82006-07-14 22:03:35 +00004308 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004309 }
aliguori376253e2009-03-05 23:01:23 +00004310 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004311 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004312 /* Check if 'i' is greater than 32-bit */
4313 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4314 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4315 monitor_printf(mon, "integer is for 32-bit values\n");
4316 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004317 } else if (c == 'M') {
4318 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004319 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004320 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004321 }
4322 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004323 case 'o':
4324 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004325 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004326 char *end;
4327
4328 while (qemu_isspace(*p)) {
4329 p++;
4330 }
4331 if (*typestr == '?') {
4332 typestr++;
4333 if (*p == '\0') {
4334 break;
4335 }
4336 }
4337 val = strtosz(p, &end);
4338 if (val < 0) {
4339 monitor_printf(mon, "invalid size\n");
4340 goto fail;
4341 }
4342 qdict_put(qdict, key, qint_from_int(val));
4343 p = end;
4344 }
4345 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004346 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004347 {
4348 double val;
4349
4350 while (qemu_isspace(*p))
4351 p++;
4352 if (*typestr == '?') {
4353 typestr++;
4354 if (*p == '\0') {
4355 break;
4356 }
4357 }
4358 if (get_double(mon, &val, &p) < 0) {
4359 goto fail;
4360 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004361 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004362 switch (*p) {
4363 case 'm':
4364 val /= 1e3; p += 2; break;
4365 case 'u':
4366 val /= 1e6; p += 2; break;
4367 case 'n':
4368 val /= 1e9; p += 2; break;
4369 }
4370 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004371 if (*p && !qemu_isspace(*p)) {
4372 monitor_printf(mon, "Unknown unit suffix\n");
4373 goto fail;
4374 }
4375 qdict_put(qdict, key, qfloat_from_double(val));
4376 }
4377 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004378 case 'b':
4379 {
4380 const char *beg;
4381 int val;
4382
4383 while (qemu_isspace(*p)) {
4384 p++;
4385 }
4386 beg = p;
4387 while (qemu_isgraph(*p)) {
4388 p++;
4389 }
4390 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4391 val = 1;
4392 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4393 val = 0;
4394 } else {
4395 monitor_printf(mon, "Expected 'on' or 'off'\n");
4396 goto fail;
4397 }
4398 qdict_put(qdict, key, qbool_from_int(val));
4399 }
4400 break;
bellard9307c4c2004-04-04 12:57:25 +00004401 case '-':
4402 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004403 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004404 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004405 /* option */
ths3b46e622007-09-17 08:09:54 +00004406
bellard9307c4c2004-04-04 12:57:25 +00004407 c = *typestr++;
4408 if (c == '\0')
4409 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004410 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004411 p++;
bellard9307c4c2004-04-04 12:57:25 +00004412 if (*p == '-') {
4413 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004414 if(c != *p) {
4415 if(!is_valid_option(p, typestr)) {
4416
4417 monitor_printf(mon, "%s: unsupported option -%c\n",
4418 cmdname, *p);
4419 goto fail;
4420 } else {
4421 skip_key = 1;
4422 }
bellard9307c4c2004-04-04 12:57:25 +00004423 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004424 if(skip_key) {
4425 p = tmp;
4426 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004427 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004428 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004429 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004430 }
bellard9307c4c2004-04-04 12:57:25 +00004431 }
bellard9307c4c2004-04-04 12:57:25 +00004432 }
4433 break;
4434 default:
4435 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004436 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004437 goto fail;
4438 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004439 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004440 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004441 }
4442 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004443 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004444 p++;
4445 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004446 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4447 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004448 goto fail;
4449 }
4450
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004451 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004452
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004453fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004454 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004455 return NULL;
4456}
4457
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004458void monitor_set_error(Monitor *mon, QError *qerror)
4459{
4460 /* report only the first error */
4461 if (!mon->error) {
4462 mon->error = qerror;
4463 } else {
4464 MON_DEBUG("Additional error report at %s:%d\n",
4465 qerror->file, qerror->linenr);
4466 QDECREF(qerror);
4467 }
4468}
4469
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004470static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4471{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004472 if (ret && !monitor_has_error(mon)) {
4473 /*
4474 * If it returns failure, it must have passed on error.
4475 *
4476 * Action: Report an internal error to the client if in QMP.
4477 */
4478 qerror_report(QERR_UNDEFINED_ERROR);
4479 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4480 cmd->name);
4481 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004482
4483#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004484 if (!ret && monitor_has_error(mon)) {
4485 /*
4486 * If it returns success, it must not have passed an error.
4487 *
4488 * Action: Report the passed error to the client.
4489 */
4490 MON_DEBUG("command '%s' returned success but passed an error\n",
4491 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004492 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004493
4494 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4495 /*
4496 * Handlers should not call Monitor print functions.
4497 *
4498 * Action: Ignore them in QMP.
4499 *
4500 * (XXX: we don't check any 'info' or 'query' command here
4501 * because the user print function _is_ called by do_info(), hence
4502 * we will trigger this check. This problem will go away when we
4503 * make 'query' commands real and kill do_info())
4504 */
4505 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4506 cmd->name, mon_print_count_get(mon));
4507 }
4508#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004509}
4510
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004511static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004512{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004513 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004514 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004515
4516 qdict = qdict_new();
4517
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004518 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004519 if (!cmd)
4520 goto out;
4521
Luiz Capitulino4903de02010-09-16 11:01:32 -03004522 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004523 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004524 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004525 QObject *data = NULL;
4526
4527 /* XXX: ignores the error code */
4528 cmd->mhandler.cmd_new(mon, qdict, &data);
4529 assert(!monitor_has_error(mon));
4530 if (data) {
4531 cmd->user_print(mon, data);
4532 qobject_decref(data);
4533 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004534 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004535 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004536 }
4537
Luiz Capitulino13917be2009-10-07 13:41:54 -03004538out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004539 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004540}
4541
bellard81d09122004-07-14 17:21:37 +00004542static void cmd_completion(const char *name, const char *list)
4543{
4544 const char *p, *pstart;
4545 char cmd[128];
4546 int len;
4547
4548 p = list;
4549 for(;;) {
4550 pstart = p;
4551 p = strchr(p, '|');
4552 if (!p)
4553 p = pstart + strlen(pstart);
4554 len = p - pstart;
4555 if (len > sizeof(cmd) - 2)
4556 len = sizeof(cmd) - 2;
4557 memcpy(cmd, pstart, len);
4558 cmd[len] = '\0';
4559 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004560 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004561 }
4562 if (*p == '\0')
4563 break;
4564 p++;
4565 }
4566}
4567
4568static void file_completion(const char *input)
4569{
4570 DIR *ffs;
4571 struct dirent *d;
4572 char path[1024];
4573 char file[1024], file_prefix[1024];
4574 int input_path_len;
4575 const char *p;
4576
ths5fafdf22007-09-16 21:08:06 +00004577 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004578 if (!p) {
4579 input_path_len = 0;
4580 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004581 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004582 } else {
4583 input_path_len = p - input + 1;
4584 memcpy(path, input, input_path_len);
4585 if (input_path_len > sizeof(path) - 1)
4586 input_path_len = sizeof(path) - 1;
4587 path[input_path_len] = '\0';
4588 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4589 }
4590#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004591 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4592 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004593#endif
4594 ffs = opendir(path);
4595 if (!ffs)
4596 return;
4597 for(;;) {
4598 struct stat sb;
4599 d = readdir(ffs);
4600 if (!d)
4601 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004602
4603 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4604 continue;
4605 }
4606
bellard81d09122004-07-14 17:21:37 +00004607 if (strstart(d->d_name, file_prefix, NULL)) {
4608 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004609 if (input_path_len < sizeof(file))
4610 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4611 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004612 /* stat the file to find out if it's a directory.
4613 * In that case add a slash to speed up typing long paths
4614 */
4615 stat(file, &sb);
4616 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004617 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004618 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004619 }
4620 }
4621 closedir(ffs);
4622}
4623
aliguori51de9762009-03-05 23:00:43 +00004624static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004625{
aliguori51de9762009-03-05 23:00:43 +00004626 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004627 const char *input = opaque;
4628
4629 if (input[0] == '\0' ||
4630 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004631 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004632 }
4633}
4634
4635/* NOTE: this parser is an approximate form of the real command parser */
4636static void parse_cmdline(const char *cmdline,
4637 int *pnb_args, char **args)
4638{
4639 const char *p;
4640 int nb_args, ret;
4641 char buf[1024];
4642
4643 p = cmdline;
4644 nb_args = 0;
4645 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004646 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004647 p++;
4648 if (*p == '\0')
4649 break;
4650 if (nb_args >= MAX_ARGS)
4651 break;
4652 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004653 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004654 nb_args++;
4655 if (ret < 0)
4656 break;
4657 }
4658 *pnb_args = nb_args;
4659}
4660
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004661static const char *next_arg_type(const char *typestr)
4662{
4663 const char *p = strchr(typestr, ':');
4664 return (p != NULL ? ++p : typestr);
4665}
4666
aliguori4c36ba32009-03-05 23:01:37 +00004667static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004668{
4669 const char *cmdname;
4670 char *args[MAX_ARGS];
4671 int nb_args, i, len;
4672 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004673 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004674 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004675
4676 parse_cmdline(cmdline, &nb_args, args);
4677#ifdef DEBUG_COMPLETION
4678 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004679 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004680 }
4681#endif
4682
4683 /* if the line ends with a space, it means we want to complete the
4684 next arg */
4685 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004686 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004687 if (nb_args >= MAX_ARGS) {
4688 goto cleanup;
4689 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004690 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004691 }
4692 if (nb_args <= 1) {
4693 /* command completion */
4694 if (nb_args == 0)
4695 cmdname = "";
4696 else
4697 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004698 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004699 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004700 cmd_completion(cmdname, cmd->name);
4701 }
4702 } else {
4703 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004704 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4705 if (compare_cmd(args[0], cmd->name)) {
4706 break;
4707 }
bellard81d09122004-07-14 17:21:37 +00004708 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004709 if (!cmd->name) {
4710 goto cleanup;
4711 }
4712
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004713 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004714 for(i = 0; i < nb_args - 2; i++) {
4715 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004716 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004717 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004718 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004719 }
4720 }
4721 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004722 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004723 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004724 }
bellard81d09122004-07-14 17:21:37 +00004725 switch(*ptype) {
4726 case 'F':
4727 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004728 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004729 file_completion(str);
4730 break;
4731 case 'B':
4732 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004733 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004734 bdrv_iterate(block_completion_it, (void *)str);
4735 break;
bellard7fe48482004-10-09 18:08:01 +00004736 case 's':
4737 /* XXX: more generic ? */
4738 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004739 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004740 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4741 cmd_completion(str, cmd->name);
4742 }
bellard64866c32006-05-07 18:03:31 +00004743 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004744 char *sep = strrchr(str, '-');
4745 if (sep)
4746 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004747 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004748 for(key = key_defs; key->name != NULL; key++) {
4749 cmd_completion(str, key->name);
4750 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004751 } else if (!strcmp(cmd->name, "help|?")) {
4752 readline_set_completion_index(cur_mon->rs, strlen(str));
4753 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4754 cmd_completion(str, cmd->name);
4755 }
bellard7fe48482004-10-09 18:08:01 +00004756 }
4757 break;
bellard81d09122004-07-14 17:21:37 +00004758 default:
4759 break;
4760 }
4761 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004762
4763cleanup:
4764 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004765 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004766 }
bellard81d09122004-07-14 17:21:37 +00004767}
4768
aliguori731b0362009-03-05 23:01:42 +00004769static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004770{
aliguori731b0362009-03-05 23:01:42 +00004771 Monitor *mon = opaque;
4772
Jan Kiszkac62313b2009-12-04 14:05:29 +01004773 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004774}
4775
Luiz Capitulino09069b12010-02-04 18:10:06 -02004776static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4777{
4778 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4779 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4780}
4781
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004782/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004783 * Argument validation rules:
4784 *
4785 * 1. The argument must exist in cmd_args qdict
4786 * 2. The argument type must be the expected one
4787 *
4788 * Special case: If the argument doesn't exist in cmd_args and
4789 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4790 * checking is skipped for it.
4791 */
4792static int check_client_args_type(const QDict *client_args,
4793 const QDict *cmd_args, int flags)
4794{
4795 const QDictEntry *ent;
4796
4797 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4798 QObject *obj;
4799 QString *arg_type;
4800 const QObject *client_arg = qdict_entry_value(ent);
4801 const char *client_arg_name = qdict_entry_key(ent);
4802
4803 obj = qdict_get(cmd_args, client_arg_name);
4804 if (!obj) {
4805 if (flags & QMP_ACCEPT_UNKNOWNS) {
4806 /* handler accepts unknowns */
4807 continue;
4808 }
4809 /* client arg doesn't exist */
4810 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4811 return -1;
4812 }
4813
4814 arg_type = qobject_to_qstring(obj);
4815 assert(arg_type != NULL);
4816
4817 /* check if argument's type is correct */
4818 switch (qstring_get_str(arg_type)[0]) {
4819 case 'F':
4820 case 'B':
4821 case 's':
4822 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4823 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4824 "string");
4825 return -1;
4826 }
4827 break;
4828 case 'i':
4829 case 'l':
4830 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004831 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004832 if (qobject_type(client_arg) != QTYPE_QINT) {
4833 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4834 "int");
4835 return -1;
4836 }
4837 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004838 case 'T':
4839 if (qobject_type(client_arg) != QTYPE_QINT &&
4840 qobject_type(client_arg) != QTYPE_QFLOAT) {
4841 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4842 "number");
4843 return -1;
4844 }
4845 break;
4846 case 'b':
4847 case '-':
4848 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4849 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4850 "bool");
4851 return -1;
4852 }
4853 break;
4854 case 'O':
4855 assert(flags & QMP_ACCEPT_UNKNOWNS);
4856 break;
4857 case '/':
4858 case '.':
4859 /*
4860 * These types are not supported by QMP and thus are not
4861 * handled here. Fall through.
4862 */
4863 default:
4864 abort();
4865 }
4866 }
4867
4868 return 0;
4869}
4870
4871/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004872 * - Check if the client has passed all mandatory args
4873 * - Set special flags for argument validation
4874 */
4875static int check_mandatory_args(const QDict *cmd_args,
4876 const QDict *client_args, int *flags)
4877{
4878 const QDictEntry *ent;
4879
4880 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4881 const char *cmd_arg_name = qdict_entry_key(ent);
4882 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4883 assert(type != NULL);
4884
4885 if (qstring_get_str(type)[0] == 'O') {
4886 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4887 *flags |= QMP_ACCEPT_UNKNOWNS;
4888 } else if (qstring_get_str(type)[0] != '-' &&
4889 qstring_get_str(type)[1] != '?' &&
4890 !qdict_haskey(client_args, cmd_arg_name)) {
4891 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4892 return -1;
4893 }
4894 }
4895
4896 return 0;
4897}
4898
4899static QDict *qdict_from_args_type(const char *args_type)
4900{
4901 int i;
4902 QDict *qdict;
4903 QString *key, *type, *cur_qs;
4904
4905 assert(args_type != NULL);
4906
4907 qdict = qdict_new();
4908
4909 if (args_type == NULL || args_type[0] == '\0') {
4910 /* no args, empty qdict */
4911 goto out;
4912 }
4913
4914 key = qstring_new();
4915 type = qstring_new();
4916
4917 cur_qs = key;
4918
4919 for (i = 0;; i++) {
4920 switch (args_type[i]) {
4921 case ',':
4922 case '\0':
4923 qdict_put(qdict, qstring_get_str(key), type);
4924 QDECREF(key);
4925 if (args_type[i] == '\0') {
4926 goto out;
4927 }
4928 type = qstring_new(); /* qdict has ref */
4929 cur_qs = key = qstring_new();
4930 break;
4931 case ':':
4932 cur_qs = type;
4933 break;
4934 default:
4935 qstring_append_chr(cur_qs, args_type[i]);
4936 break;
4937 }
4938 }
4939
4940out:
4941 return qdict;
4942}
4943
4944/*
4945 * Client argument checking rules:
4946 *
4947 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004948 * 2. Each argument provided by the client must be expected
4949 * 3. Each argument provided by the client must have the type expected
4950 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004951 */
4952static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4953{
4954 int flags, err;
4955 QDict *cmd_args;
4956
4957 cmd_args = qdict_from_args_type(cmd->args_type);
4958
4959 flags = 0;
4960 err = check_mandatory_args(cmd_args, client_args, &flags);
4961 if (err) {
4962 goto out;
4963 }
4964
Luiz Capitulino4af91932010-06-22 11:44:05 -03004965 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004966
4967out:
4968 QDECREF(cmd_args);
4969 return err;
4970}
4971
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004972/*
4973 * Input object checking rules
4974 *
4975 * 1. Input object must be a dict
4976 * 2. The "execute" key must exist
4977 * 3. The "execute" key must be a string
4978 * 4. If the "arguments" key exists, it must be a dict
4979 * 5. If the "id" key exists, it can be anything (ie. json-value)
4980 * 6. Any argument not listed above is considered invalid
4981 */
4982static QDict *qmp_check_input_obj(QObject *input_obj)
4983{
4984 const QDictEntry *ent;
4985 int has_exec_key = 0;
4986 QDict *input_dict;
4987
4988 if (qobject_type(input_obj) != QTYPE_QDICT) {
4989 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4990 return NULL;
4991 }
4992
4993 input_dict = qobject_to_qdict(input_obj);
4994
4995 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4996 const char *arg_name = qdict_entry_key(ent);
4997 const QObject *arg_obj = qdict_entry_value(ent);
4998
4999 if (!strcmp(arg_name, "execute")) {
5000 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
5001 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
5002 "string");
5003 return NULL;
5004 }
5005 has_exec_key = 1;
5006 } else if (!strcmp(arg_name, "arguments")) {
5007 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5008 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5009 "object");
5010 return NULL;
5011 }
5012 } else if (!strcmp(arg_name, "id")) {
5013 /* FIXME: check duplicated IDs for async commands */
5014 } else {
5015 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5016 return NULL;
5017 }
5018 }
5019
5020 if (!has_exec_key) {
5021 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5022 return NULL;
5023 }
5024
5025 return input_dict;
5026}
5027
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005028static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
5029{
5030 QObject *ret_data = NULL;
5031
Luiz Capitulino4903de02010-09-16 11:01:32 -03005032 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005033 qmp_async_info_handler(mon, cmd);
5034 if (monitor_has_error(mon)) {
5035 monitor_protocol_emitter(mon, NULL);
5036 }
5037 } else {
5038 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02005039 monitor_protocol_emitter(mon, ret_data);
5040 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005041 }
5042}
5043
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005044static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5045 const QDict *params)
5046{
5047 int ret;
5048 QObject *data = NULL;
5049
5050 mon_print_count_init(mon);
5051
5052 ret = cmd->mhandler.cmd_new(mon, params, &data);
5053 handler_audit(mon, cmd, ret);
5054 monitor_protocol_emitter(mon, data);
5055 qobject_decref(data);
5056}
5057
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005058static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5059{
5060 int err;
5061 QObject *obj;
5062 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005063 const mon_cmd_t *cmd;
5064 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005065 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005066
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005067 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005068 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005069
5070 obj = json_parser_parse(tokens, NULL);
5071 if (!obj) {
5072 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005073 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005074 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005075 }
5076
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005077 input = qmp_check_input_obj(obj);
5078 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005079 qobject_decref(obj);
5080 goto err_out;
5081 }
5082
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005083 mon->mc->id = qdict_get(input, "id");
5084 qobject_incref(mon->mc->id);
5085
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005086 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02005087 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005088 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005089 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005090 }
5091
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005092 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005093 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02005094 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03005095 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03005096 }
5097
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005098 if (!cmd) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005099 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5100 goto err_out;
5101 }
5102
5103 obj = qdict_get(input, "arguments");
5104 if (!obj) {
5105 args = qdict_new();
5106 } else {
5107 args = qobject_to_qdict(obj);
5108 QINCREF(args);
5109 }
5110
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005111 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005112 if (err < 0) {
5113 goto err_out;
5114 }
5115
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005116 if (query_cmd) {
5117 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03005118 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005119 err = qmp_async_cmd_handler(mon, cmd, args);
5120 if (err) {
5121 /* emit the error response */
5122 goto err_out;
5123 }
Adam Litke940cc302010-01-25 12:18:44 -06005124 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005125 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005126 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005127
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005128 goto out;
5129
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005130err_out:
5131 monitor_protocol_emitter(mon, NULL);
5132out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005133 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005134 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005135}
5136
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005137/**
5138 * monitor_control_read(): Read and handle QMP input
5139 */
5140static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5141{
5142 Monitor *old_mon = cur_mon;
5143
5144 cur_mon = opaque;
5145
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005146 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005147
5148 cur_mon = old_mon;
5149}
5150
aliguori731b0362009-03-05 23:01:42 +00005151static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005152{
aliguori731b0362009-03-05 23:01:42 +00005153 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005154 int i;
aliguori376253e2009-03-05 23:01:23 +00005155
aliguori731b0362009-03-05 23:01:42 +00005156 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005157
aliguoricde76ee2009-03-05 23:01:51 +00005158 if (cur_mon->rs) {
5159 for (i = 0; i < size; i++)
5160 readline_handle_byte(cur_mon->rs, buf[i]);
5161 } else {
5162 if (size == 0 || buf[size - 1] != 0)
5163 monitor_printf(cur_mon, "corrupted command\n");
5164 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005165 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005166 }
aliguori731b0362009-03-05 23:01:42 +00005167
5168 cur_mon = old_mon;
5169}
aliguorid8f44602008-10-06 13:52:44 +00005170
aliguori376253e2009-03-05 23:01:23 +00005171static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005172{
aliguori731b0362009-03-05 23:01:42 +00005173 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005174 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005175 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005176}
5177
aliguoricde76ee2009-03-05 23:01:51 +00005178int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005179{
aliguoricde76ee2009-03-05 23:01:51 +00005180 if (!mon->rs)
5181 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005182 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005183 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005184}
5185
aliguori376253e2009-03-05 23:01:23 +00005186void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005187{
aliguoricde76ee2009-03-05 23:01:51 +00005188 if (!mon->rs)
5189 return;
aliguori731b0362009-03-05 23:01:42 +00005190 if (--mon->suspend_cnt == 0)
5191 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005192}
5193
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005194static QObject *get_qmp_greeting(void)
5195{
5196 QObject *ver;
5197
5198 do_info_version(NULL, &ver);
5199 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5200}
5201
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005202/**
5203 * monitor_control_event(): Print QMP gretting
5204 */
5205static void monitor_control_event(void *opaque, int event)
5206{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005207 QObject *data;
5208 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005209
Luiz Capitulino47116d12010-02-08 17:01:30 -02005210 switch (event) {
5211 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005212 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005213 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005214 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005215 monitor_json_emitter(mon, data);
5216 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005217 break;
5218 case CHR_EVENT_CLOSED:
5219 json_message_parser_destroy(&mon->mc->parser);
5220 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005221 }
5222}
5223
aliguori731b0362009-03-05 23:01:42 +00005224static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005225{
aliguori376253e2009-03-05 23:01:23 +00005226 Monitor *mon = opaque;
5227
aliguori2724b182009-03-05 23:01:47 +00005228 switch (event) {
5229 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005230 mon->mux_out = 0;
5231 if (mon->reset_seen) {
5232 readline_restart(mon->rs);
5233 monitor_resume(mon);
5234 monitor_flush(mon);
5235 } else {
5236 mon->suspend_cnt = 0;
5237 }
aliguori2724b182009-03-05 23:01:47 +00005238 break;
ths86e94de2007-01-05 22:01:59 +00005239
aliguori2724b182009-03-05 23:01:47 +00005240 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005241 if (mon->reset_seen) {
5242 if (mon->suspend_cnt == 0) {
5243 monitor_printf(mon, "\n");
5244 }
5245 monitor_flush(mon);
5246 monitor_suspend(mon);
5247 } else {
5248 mon->suspend_cnt++;
5249 }
5250 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005251 break;
5252
Amit Shahb6b8df52009-10-07 18:31:16 +05305253 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005254 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5255 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005256 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005257 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005258 }
5259 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005260 break;
5261 }
ths86e94de2007-01-05 22:01:59 +00005262}
5263
aliguori76655d62009-03-06 20:27:37 +00005264
5265/*
5266 * Local variables:
5267 * c-indent-level: 4
5268 * c-basic-offset: 4
5269 * tab-width: 8
5270 * End:
5271 */
5272
aliguori731b0362009-03-05 23:01:42 +00005273void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005274{
aliguori731b0362009-03-05 23:01:42 +00005275 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005276 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005277
5278 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005279 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005280 is_first_init = 0;
5281 }
aliguori87127162009-03-05 23:01:29 +00005282
Anthony Liguori7267c092011-08-20 22:09:37 -05005283 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005284
aliguori87127162009-03-05 23:01:29 +00005285 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005286 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005287 if (flags & MONITOR_USE_READLINE) {
5288 mon->rs = readline_init(mon, monitor_find_completion);
5289 monitor_read_command(mon, 0);
5290 }
aliguori87127162009-03-05 23:01:29 +00005291
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005292 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005293 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005294 /* Control mode requires special handlers */
5295 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5296 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005297 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005298 } else {
5299 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5300 monitor_event, mon);
5301 }
aliguori87127162009-03-05 23:01:29 +00005302
Blue Swirl72cf2d42009-09-12 07:36:22 +00005303 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005304 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5305 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005306}
5307
aliguori376253e2009-03-05 23:01:23 +00005308static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005309{
aliguoribb5fc202009-03-05 23:01:15 +00005310 BlockDriverState *bs = opaque;
5311 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005312
aliguoribb5fc202009-03-05 23:01:15 +00005313 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005314 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005315 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005316 }
aliguori731b0362009-03-05 23:01:42 +00005317 if (mon->password_completion_cb)
5318 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005319
aliguori731b0362009-03-05 23:01:42 +00005320 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005321}
aliguoric0f4ce72009-03-05 23:01:01 +00005322
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005323int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5324 BlockDriverCompletionFunc *completion_cb,
5325 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005326{
aliguoricde76ee2009-03-05 23:01:51 +00005327 int err;
5328
aliguoribb5fc202009-03-05 23:01:15 +00005329 if (!bdrv_key_required(bs)) {
5330 if (completion_cb)
5331 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005332 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005333 }
aliguoric0f4ce72009-03-05 23:01:01 +00005334
Luiz Capitulino94171e12009-12-07 21:37:00 +01005335 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005336 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005337 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005338 }
5339
aliguori376253e2009-03-05 23:01:23 +00005340 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5341 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005342
aliguori731b0362009-03-05 23:01:42 +00005343 mon->password_completion_cb = completion_cb;
5344 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005345
aliguoricde76ee2009-03-05 23:01:51 +00005346 err = monitor_read_password(mon, bdrv_password_cb, bs);
5347
5348 if (err && completion_cb)
5349 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005350
5351 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005352}