blob: ab71d2d9e536c2c1b863145f452789419cfb0976 [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 Clements8a94b8ca2011-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 Clements8a94b8ca2011-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 Clements8a94b8ca2011-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{
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -03002639 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep, runstate_as_string());
aurel326f9c5ee2008-12-18 22:43:56 +00002640}
2641
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002642static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002643{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002644 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002645
aliguori76655d62009-03-06 20:27:37 +00002646 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002647 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002648 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002649 return acl;
2650}
aliguori76655d62009-03-06 20:27:37 +00002651
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002652static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002653{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002654 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002655 qemu_acl *acl = find_acl(mon, aclname);
2656 qemu_acl_entry *entry;
2657 int i = 0;
2658
2659 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002660 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002661 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002662 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002663 i++;
2664 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002665 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002666 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002667 }
2668}
2669
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002670static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002671{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002672 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002673 qemu_acl *acl = find_acl(mon, aclname);
2674
2675 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002676 qemu_acl_reset(acl);
2677 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002678 }
2679}
aliguori76655d62009-03-06 20:27:37 +00002680
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002681static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002682{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002683 const char *aclname = qdict_get_str(qdict, "aclname");
2684 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002685 qemu_acl *acl = find_acl(mon, aclname);
2686
2687 if (acl) {
2688 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002689 acl->defaultDeny = 0;
2690 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002691 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002692 acl->defaultDeny = 1;
2693 monitor_printf(mon, "acl: policy set to 'deny'\n");
2694 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002695 monitor_printf(mon, "acl: unknown policy '%s', "
2696 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002697 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002698 }
2699}
aliguori76655d62009-03-06 20:27:37 +00002700
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002701static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002702{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002703 const char *aclname = qdict_get_str(qdict, "aclname");
2704 const char *match = qdict_get_str(qdict, "match");
2705 const char *policy = qdict_get_str(qdict, "policy");
2706 int has_index = qdict_haskey(qdict, "index");
2707 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002708 qemu_acl *acl = find_acl(mon, aclname);
2709 int deny, ret;
2710
2711 if (acl) {
2712 if (strcmp(policy, "allow") == 0) {
2713 deny = 0;
2714 } else if (strcmp(policy, "deny") == 0) {
2715 deny = 1;
2716 } else {
2717 monitor_printf(mon, "acl: unknown policy '%s', "
2718 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002719 return;
2720 }
aliguori28a76be2009-03-06 20:27:40 +00002721 if (has_index)
2722 ret = qemu_acl_insert(acl, deny, match, index);
2723 else
2724 ret = qemu_acl_append(acl, deny, match);
2725 if (ret < 0)
2726 monitor_printf(mon, "acl: unable to add acl entry\n");
2727 else
2728 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002729 }
2730}
aliguori76655d62009-03-06 20:27:37 +00002731
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002732static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002733{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002734 const char *aclname = qdict_get_str(qdict, "aclname");
2735 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002736 qemu_acl *acl = find_acl(mon, aclname);
2737 int ret;
aliguori76655d62009-03-06 20:27:37 +00002738
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002739 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002740 ret = qemu_acl_remove(acl, match);
2741 if (ret < 0)
2742 monitor_printf(mon, "acl: no matching acl entry\n");
2743 else
2744 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002745 }
2746}
2747
Huang Ying79c4f6b2009-06-23 10:05:14 +08002748#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002749static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002750{
2751 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002752 int cpu_index = qdict_get_int(qdict, "cpu_index");
2753 int bank = qdict_get_int(qdict, "bank");
2754 uint64_t status = qdict_get_int(qdict, "status");
2755 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2756 uint64_t addr = qdict_get_int(qdict, "addr");
2757 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002758 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002759
Jan Kiszka747461c2011-03-02 08:56:10 +01002760 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2761 flags |= MCE_INJECT_BROADCAST;
2762 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002763 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002764 if (cenv->cpu_index == cpu_index) {
2765 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002766 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002767 break;
2768 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002769 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002770}
2771#endif
2772
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002773static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002774{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002775 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002776 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002777 int fd;
2778
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002779 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002780 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002781 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002782 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002783 }
2784
2785 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002786 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2787 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002788 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002789 }
2790
Blue Swirl72cf2d42009-09-12 07:36:22 +00002791 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002792 if (strcmp(monfd->name, fdname) != 0) {
2793 continue;
2794 }
2795
2796 close(monfd->fd);
2797 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002798 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002799 }
2800
Anthony Liguori7267c092011-08-20 22:09:37 -05002801 monfd = g_malloc0(sizeof(mon_fd_t));
2802 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002803 monfd->fd = fd;
2804
Blue Swirl72cf2d42009-09-12 07:36:22 +00002805 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002806 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002807}
2808
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002809static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002810{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002811 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002812 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002813
Blue Swirl72cf2d42009-09-12 07:36:22 +00002814 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002815 if (strcmp(monfd->name, fdname) != 0) {
2816 continue;
2817 }
2818
Blue Swirl72cf2d42009-09-12 07:36:22 +00002819 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002820 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002821 g_free(monfd->name);
2822 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002823 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002824 }
2825
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002826 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002827 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002828}
2829
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002830static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002831{
Luiz Capitulino13548692011-07-29 15:36:43 -03002832 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002833 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002834
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002835 vm_stop(RSTATE_RESTORE);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002836
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002837 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002838 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002839 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002840}
2841
Mark McLoughlin7768e042009-07-22 09:11:41 +01002842int monitor_get_fd(Monitor *mon, const char *fdname)
2843{
Anthony Liguoric227f092009-10-01 16:12:16 -05002844 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002845
Blue Swirl72cf2d42009-09-12 07:36:22 +00002846 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002847 int fd;
2848
2849 if (strcmp(monfd->name, fdname) != 0) {
2850 continue;
2851 }
2852
2853 fd = monfd->fd;
2854
2855 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002856 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002857 g_free(monfd->name);
2858 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002859
2860 return fd;
2861 }
2862
2863 return -1;
2864}
2865
Anthony Liguoric227f092009-10-01 16:12:16 -05002866static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002867#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002868 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002869};
2870
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002871/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002872static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002873 {
2874 .name = "version",
2875 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002876 .params = "",
2877 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002878 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002879 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 },
2881 {
2882 .name = "network",
2883 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002884 .params = "",
2885 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002886 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 },
2888 {
2889 .name = "chardev",
2890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002891 .params = "",
2892 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002893 .user_print = qemu_chr_info_print,
2894 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 },
2896 {
2897 .name = "block",
2898 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 .params = "",
2900 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002901 .user_print = bdrv_info_print,
2902 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 },
2904 {
2905 .name = "blockstats",
2906 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002907 .params = "",
2908 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002909 .user_print = bdrv_stats_print,
2910 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002911 },
2912 {
2913 .name = "registers",
2914 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002915 .params = "",
2916 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002917 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002918 },
2919 {
2920 .name = "cpus",
2921 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 .params = "",
2923 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002924 .user_print = monitor_print_cpus,
2925 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002926 },
2927 {
2928 .name = "history",
2929 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002930 .params = "",
2931 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002932 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002933 },
2934 {
2935 .name = "irq",
2936 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002937 .params = "",
2938 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002939 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002940 },
2941 {
2942 .name = "pic",
2943 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002944 .params = "",
2945 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002946 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002947 },
2948 {
2949 .name = "pci",
2950 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002951 .params = "",
2952 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002953 .user_print = do_pci_info_print,
2954 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002955 },
Blue Swirld41160a2010-12-19 13:42:56 +00002956#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002957 {
2958 .name = "tlb",
2959 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002960 .params = "",
2961 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002962 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002963 },
aurel327c664e22009-03-03 06:12:22 +00002964#endif
2965#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002966 {
2967 .name = "mem",
2968 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002969 .params = "",
2970 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002971 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002972 },
bellardb86bda52004-09-18 19:32:46 +00002973#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002974 {
2975 .name = "jit",
2976 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002977 .params = "",
2978 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002979 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002980 },
2981 {
2982 .name = "kvm",
2983 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002984 .params = "",
2985 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002986 .user_print = do_info_kvm_print,
2987 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002988 },
2989 {
2990 .name = "numa",
2991 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002992 .params = "",
2993 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002994 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002995 },
2996 {
2997 .name = "usb",
2998 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002999 .params = "",
3000 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003001 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003002 },
3003 {
3004 .name = "usbhost",
3005 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003006 .params = "",
3007 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003008 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003009 },
3010 {
3011 .name = "profile",
3012 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003013 .params = "",
3014 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003015 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003016 },
3017 {
3018 .name = "capture",
3019 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003020 .params = "",
3021 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003022 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003023 },
3024 {
3025 .name = "snapshots",
3026 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003027 .params = "",
3028 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03003029 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003030 },
3031 {
3032 .name = "status",
3033 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003034 .params = "",
3035 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02003036 .user_print = do_info_status_print,
3037 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003038 },
3039 {
3040 .name = "pcmcia",
3041 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003042 .params = "",
3043 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03003044 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003045 },
3046 {
3047 .name = "mice",
3048 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003049 .params = "",
3050 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02003051 .user_print = do_info_mice_print,
3052 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003053 },
3054 {
3055 .name = "vnc",
3056 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003057 .params = "",
3058 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02003059 .user_print = do_info_vnc_print,
3060 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003061 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003062#if defined(CONFIG_SPICE)
3063 {
3064 .name = "spice",
3065 .args_type = "",
3066 .params = "",
3067 .help = "show the spice server status",
3068 .user_print = do_info_spice_print,
3069 .mhandler.info_new = do_info_spice,
3070 },
3071#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003072 {
3073 .name = "name",
3074 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003075 .params = "",
3076 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02003077 .user_print = do_info_name_print,
3078 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003079 },
3080 {
3081 .name = "uuid",
3082 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003083 .params = "",
3084 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02003085 .user_print = do_info_uuid_print,
3086 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003087 },
j_mayer76a66252007-03-07 08:32:30 +00003088#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003089 {
3090 .name = "cpustats",
3091 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003092 .params = "",
3093 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03003094 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003095 },
j_mayer76a66252007-03-07 08:32:30 +00003096#endif
blueswir131a60e22007-10-26 18:42:59 +00003097#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003098 {
3099 .name = "usernet",
3100 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003101 .params = "",
3102 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03003103 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003104 },
blueswir131a60e22007-10-26 18:42:59 +00003105#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003106 {
3107 .name = "migrate",
3108 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003109 .params = "",
3110 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003111 .user_print = do_info_migrate_print,
3112 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003113 },
3114 {
3115 .name = "balloon",
3116 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003117 .params = "",
3118 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003119 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003120 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003121 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003122 },
3123 {
3124 .name = "qtree",
3125 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003126 .params = "",
3127 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003128 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003129 },
3130 {
3131 .name = "qdm",
3132 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003133 .params = "",
3134 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003135 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003136 },
3137 {
3138 .name = "roms",
3139 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003140 .params = "",
3141 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003142 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003143 },
Lluís6d8a7642011-08-31 20:30:43 +02003144#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303145 {
3146 .name = "trace",
3147 .args_type = "",
3148 .params = "",
3149 .help = "show current contents of trace buffer",
3150 .mhandler.info = do_info_trace,
3151 },
Lluís31965ae2011-08-31 20:31:24 +02003152#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303153 {
3154 .name = "trace-events",
3155 .args_type = "",
3156 .params = "",
3157 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003158 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303159 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003160 {
3161 .name = NULL,
3162 },
bellard9dc39cb2004-03-14 21:38:27 +00003163};
3164
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003165static const mon_cmd_t qmp_cmds[] = {
3166#include "qmp-commands.h"
3167 { /* NULL */ },
3168};
3169
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003170static const mon_cmd_t qmp_query_cmds[] = {
3171 {
3172 .name = "version",
3173 .args_type = "",
3174 .params = "",
3175 .help = "show the version of QEMU",
3176 .user_print = do_info_version_print,
3177 .mhandler.info_new = do_info_version,
3178 },
3179 {
3180 .name = "commands",
3181 .args_type = "",
3182 .params = "",
3183 .help = "list QMP available commands",
3184 .user_print = monitor_user_noop,
3185 .mhandler.info_new = do_info_commands,
3186 },
3187 {
3188 .name = "chardev",
3189 .args_type = "",
3190 .params = "",
3191 .help = "show the character devices",
3192 .user_print = qemu_chr_info_print,
3193 .mhandler.info_new = qemu_chr_info,
3194 },
3195 {
3196 .name = "block",
3197 .args_type = "",
3198 .params = "",
3199 .help = "show the block devices",
3200 .user_print = bdrv_info_print,
3201 .mhandler.info_new = bdrv_info,
3202 },
3203 {
3204 .name = "blockstats",
3205 .args_type = "",
3206 .params = "",
3207 .help = "show block device statistics",
3208 .user_print = bdrv_stats_print,
3209 .mhandler.info_new = bdrv_info_stats,
3210 },
3211 {
3212 .name = "cpus",
3213 .args_type = "",
3214 .params = "",
3215 .help = "show infos for each CPU",
3216 .user_print = monitor_print_cpus,
3217 .mhandler.info_new = do_info_cpus,
3218 },
3219 {
3220 .name = "pci",
3221 .args_type = "",
3222 .params = "",
3223 .help = "show PCI info",
3224 .user_print = do_pci_info_print,
3225 .mhandler.info_new = do_pci_info,
3226 },
3227 {
3228 .name = "kvm",
3229 .args_type = "",
3230 .params = "",
3231 .help = "show KVM information",
3232 .user_print = do_info_kvm_print,
3233 .mhandler.info_new = do_info_kvm,
3234 },
3235 {
3236 .name = "status",
3237 .args_type = "",
3238 .params = "",
3239 .help = "show the current VM status (running|paused)",
3240 .user_print = do_info_status_print,
3241 .mhandler.info_new = do_info_status,
3242 },
3243 {
3244 .name = "mice",
3245 .args_type = "",
3246 .params = "",
3247 .help = "show which guest mouse is receiving events",
3248 .user_print = do_info_mice_print,
3249 .mhandler.info_new = do_info_mice,
3250 },
3251 {
3252 .name = "vnc",
3253 .args_type = "",
3254 .params = "",
3255 .help = "show the vnc server status",
3256 .user_print = do_info_vnc_print,
3257 .mhandler.info_new = do_info_vnc,
3258 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003259#if defined(CONFIG_SPICE)
3260 {
3261 .name = "spice",
3262 .args_type = "",
3263 .params = "",
3264 .help = "show the spice server status",
3265 .user_print = do_info_spice_print,
3266 .mhandler.info_new = do_info_spice,
3267 },
3268#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003269 {
3270 .name = "name",
3271 .args_type = "",
3272 .params = "",
3273 .help = "show the current VM name",
3274 .user_print = do_info_name_print,
3275 .mhandler.info_new = do_info_name,
3276 },
3277 {
3278 .name = "uuid",
3279 .args_type = "",
3280 .params = "",
3281 .help = "show the current VM UUID",
3282 .user_print = do_info_uuid_print,
3283 .mhandler.info_new = do_info_uuid,
3284 },
3285 {
3286 .name = "migrate",
3287 .args_type = "",
3288 .params = "",
3289 .help = "show migration status",
3290 .user_print = do_info_migrate_print,
3291 .mhandler.info_new = do_info_migrate,
3292 },
3293 {
3294 .name = "balloon",
3295 .args_type = "",
3296 .params = "",
3297 .help = "show balloon information",
3298 .user_print = monitor_print_balloon,
3299 .mhandler.info_async = do_info_balloon,
3300 .flags = MONITOR_CMD_ASYNC,
3301 },
3302 { /* NULL */ },
3303};
3304
bellard9307c4c2004-04-04 12:57:25 +00003305/*******************************************************************/
3306
3307static const char *pch;
3308static jmp_buf expr_env;
3309
bellard92a31b12005-02-10 22:00:52 +00003310#define MD_TLONG 0
3311#define MD_I32 1
3312
bellard9307c4c2004-04-04 12:57:25 +00003313typedef struct MonitorDef {
3314 const char *name;
3315 int offset;
blueswir18662d652008-10-02 18:32:44 +00003316 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003317 int type;
bellard9307c4c2004-04-04 12:57:25 +00003318} MonitorDef;
3319
bellard57206fd2004-04-25 18:54:52 +00003320#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003321static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003322{
bellard6a00d602005-11-21 23:25:50 +00003323 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003324 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003325}
3326#endif
3327
bellarda541f292004-04-12 20:39:29 +00003328#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003329static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003330{
bellard6a00d602005-11-21 23:25:50 +00003331 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003332 unsigned int u;
3333 int i;
3334
3335 u = 0;
3336 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003337 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003338
3339 return u;
3340}
3341
blueswir18662d652008-10-02 18:32:44 +00003342static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003343{
bellard6a00d602005-11-21 23:25:50 +00003344 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003345 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003346}
3347
blueswir18662d652008-10-02 18:32:44 +00003348static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003349{
bellard6a00d602005-11-21 23:25:50 +00003350 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003351 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003352}
bellard9fddaa02004-05-21 12:59:32 +00003353
blueswir18662d652008-10-02 18:32:44 +00003354static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003355{
bellard6a00d602005-11-21 23:25:50 +00003356 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003357 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003358}
3359
blueswir18662d652008-10-02 18:32:44 +00003360static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003361{
bellard6a00d602005-11-21 23:25:50 +00003362 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003363 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003364}
3365
blueswir18662d652008-10-02 18:32:44 +00003366static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003367{
bellard6a00d602005-11-21 23:25:50 +00003368 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003369 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003370}
bellarda541f292004-04-12 20:39:29 +00003371#endif
3372
bellarde95c8d52004-09-30 22:22:08 +00003373#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003374#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003375static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003376{
bellard6a00d602005-11-21 23:25:50 +00003377 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003378
3379 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003380}
bellard7b936c02005-10-30 17:05:13 +00003381#endif
bellarde95c8d52004-09-30 22:22:08 +00003382
blueswir18662d652008-10-02 18:32:44 +00003383static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003384{
bellard6a00d602005-11-21 23:25:50 +00003385 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003386 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003387}
3388#endif
3389
blueswir18662d652008-10-02 18:32:44 +00003390static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003391#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003392
3393#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003394 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003395 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003396 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003397
bellard9307c4c2004-04-04 12:57:25 +00003398 { "eax", offsetof(CPUState, regs[0]) },
3399 { "ecx", offsetof(CPUState, regs[1]) },
3400 { "edx", offsetof(CPUState, regs[2]) },
3401 { "ebx", offsetof(CPUState, regs[3]) },
3402 { "esp|sp", offsetof(CPUState, regs[4]) },
3403 { "ebp|fp", offsetof(CPUState, regs[5]) },
3404 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003405 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003406#ifdef TARGET_X86_64
3407 { "r8", offsetof(CPUState, regs[8]) },
3408 { "r9", offsetof(CPUState, regs[9]) },
3409 { "r10", offsetof(CPUState, regs[10]) },
3410 { "r11", offsetof(CPUState, regs[11]) },
3411 { "r12", offsetof(CPUState, regs[12]) },
3412 { "r13", offsetof(CPUState, regs[13]) },
3413 { "r14", offsetof(CPUState, regs[14]) },
3414 { "r15", offsetof(CPUState, regs[15]) },
3415#endif
bellard9307c4c2004-04-04 12:57:25 +00003416 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003417 { "eip", offsetof(CPUState, eip) },
3418 SEG("cs", R_CS)
3419 SEG("ds", R_DS)
3420 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003421 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003422 SEG("fs", R_FS)
3423 SEG("gs", R_GS)
3424 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003425#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003426 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003427 { "r0", offsetof(CPUState, gpr[0]) },
3428 { "r1", offsetof(CPUState, gpr[1]) },
3429 { "r2", offsetof(CPUState, gpr[2]) },
3430 { "r3", offsetof(CPUState, gpr[3]) },
3431 { "r4", offsetof(CPUState, gpr[4]) },
3432 { "r5", offsetof(CPUState, gpr[5]) },
3433 { "r6", offsetof(CPUState, gpr[6]) },
3434 { "r7", offsetof(CPUState, gpr[7]) },
3435 { "r8", offsetof(CPUState, gpr[8]) },
3436 { "r9", offsetof(CPUState, gpr[9]) },
3437 { "r10", offsetof(CPUState, gpr[10]) },
3438 { "r11", offsetof(CPUState, gpr[11]) },
3439 { "r12", offsetof(CPUState, gpr[12]) },
3440 { "r13", offsetof(CPUState, gpr[13]) },
3441 { "r14", offsetof(CPUState, gpr[14]) },
3442 { "r15", offsetof(CPUState, gpr[15]) },
3443 { "r16", offsetof(CPUState, gpr[16]) },
3444 { "r17", offsetof(CPUState, gpr[17]) },
3445 { "r18", offsetof(CPUState, gpr[18]) },
3446 { "r19", offsetof(CPUState, gpr[19]) },
3447 { "r20", offsetof(CPUState, gpr[20]) },
3448 { "r21", offsetof(CPUState, gpr[21]) },
3449 { "r22", offsetof(CPUState, gpr[22]) },
3450 { "r23", offsetof(CPUState, gpr[23]) },
3451 { "r24", offsetof(CPUState, gpr[24]) },
3452 { "r25", offsetof(CPUState, gpr[25]) },
3453 { "r26", offsetof(CPUState, gpr[26]) },
3454 { "r27", offsetof(CPUState, gpr[27]) },
3455 { "r28", offsetof(CPUState, gpr[28]) },
3456 { "r29", offsetof(CPUState, gpr[29]) },
3457 { "r30", offsetof(CPUState, gpr[30]) },
3458 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003459 /* Floating point registers */
3460 { "f0", offsetof(CPUState, fpr[0]) },
3461 { "f1", offsetof(CPUState, fpr[1]) },
3462 { "f2", offsetof(CPUState, fpr[2]) },
3463 { "f3", offsetof(CPUState, fpr[3]) },
3464 { "f4", offsetof(CPUState, fpr[4]) },
3465 { "f5", offsetof(CPUState, fpr[5]) },
3466 { "f6", offsetof(CPUState, fpr[6]) },
3467 { "f7", offsetof(CPUState, fpr[7]) },
3468 { "f8", offsetof(CPUState, fpr[8]) },
3469 { "f9", offsetof(CPUState, fpr[9]) },
3470 { "f10", offsetof(CPUState, fpr[10]) },
3471 { "f11", offsetof(CPUState, fpr[11]) },
3472 { "f12", offsetof(CPUState, fpr[12]) },
3473 { "f13", offsetof(CPUState, fpr[13]) },
3474 { "f14", offsetof(CPUState, fpr[14]) },
3475 { "f15", offsetof(CPUState, fpr[15]) },
3476 { "f16", offsetof(CPUState, fpr[16]) },
3477 { "f17", offsetof(CPUState, fpr[17]) },
3478 { "f18", offsetof(CPUState, fpr[18]) },
3479 { "f19", offsetof(CPUState, fpr[19]) },
3480 { "f20", offsetof(CPUState, fpr[20]) },
3481 { "f21", offsetof(CPUState, fpr[21]) },
3482 { "f22", offsetof(CPUState, fpr[22]) },
3483 { "f23", offsetof(CPUState, fpr[23]) },
3484 { "f24", offsetof(CPUState, fpr[24]) },
3485 { "f25", offsetof(CPUState, fpr[25]) },
3486 { "f26", offsetof(CPUState, fpr[26]) },
3487 { "f27", offsetof(CPUState, fpr[27]) },
3488 { "f28", offsetof(CPUState, fpr[28]) },
3489 { "f29", offsetof(CPUState, fpr[29]) },
3490 { "f30", offsetof(CPUState, fpr[30]) },
3491 { "f31", offsetof(CPUState, fpr[31]) },
3492 { "fpscr", offsetof(CPUState, fpscr) },
3493 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003494 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003495 { "lr", offsetof(CPUState, lr) },
3496 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003497 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003498 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003499 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003500 { "msr", 0, &monitor_get_msr, },
3501 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003502 { "tbu", 0, &monitor_get_tbu, },
3503 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003504#if defined(TARGET_PPC64)
3505 /* Address space register */
3506 { "asr", offsetof(CPUState, asr) },
3507#endif
3508 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003509 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003510 { "sr0", offsetof(CPUState, sr[0]) },
3511 { "sr1", offsetof(CPUState, sr[1]) },
3512 { "sr2", offsetof(CPUState, sr[2]) },
3513 { "sr3", offsetof(CPUState, sr[3]) },
3514 { "sr4", offsetof(CPUState, sr[4]) },
3515 { "sr5", offsetof(CPUState, sr[5]) },
3516 { "sr6", offsetof(CPUState, sr[6]) },
3517 { "sr7", offsetof(CPUState, sr[7]) },
3518 { "sr8", offsetof(CPUState, sr[8]) },
3519 { "sr9", offsetof(CPUState, sr[9]) },
3520 { "sr10", offsetof(CPUState, sr[10]) },
3521 { "sr11", offsetof(CPUState, sr[11]) },
3522 { "sr12", offsetof(CPUState, sr[12]) },
3523 { "sr13", offsetof(CPUState, sr[13]) },
3524 { "sr14", offsetof(CPUState, sr[14]) },
3525 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003526 /* Too lazy to put BATs... */
3527 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3528
3529 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3530 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3531 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3532 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3533 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3534 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3535 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3536 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3537 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3538 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3539 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3540 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3541 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3542 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3543 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3544 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3545 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3546 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3547 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3548 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3549 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3550 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3551 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3552 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3553 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3554 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3555 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3556 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3557 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3558 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3559 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3560 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3561 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3562 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3563 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3564 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3565 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3566 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3567 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3568 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3569 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3570 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3571 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3572 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3573 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3574 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3575 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3576 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3577 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3578 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3579 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3580 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3581 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3582 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3583 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3584 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3585 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3586 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3587 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3588 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3589 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3590 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3591 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3592 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3593 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3594 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3595
bellarde95c8d52004-09-30 22:22:08 +00003596#elif defined(TARGET_SPARC)
3597 { "g0", offsetof(CPUState, gregs[0]) },
3598 { "g1", offsetof(CPUState, gregs[1]) },
3599 { "g2", offsetof(CPUState, gregs[2]) },
3600 { "g3", offsetof(CPUState, gregs[3]) },
3601 { "g4", offsetof(CPUState, gregs[4]) },
3602 { "g5", offsetof(CPUState, gregs[5]) },
3603 { "g6", offsetof(CPUState, gregs[6]) },
3604 { "g7", offsetof(CPUState, gregs[7]) },
3605 { "o0", 0, monitor_get_reg },
3606 { "o1", 1, monitor_get_reg },
3607 { "o2", 2, monitor_get_reg },
3608 { "o3", 3, monitor_get_reg },
3609 { "o4", 4, monitor_get_reg },
3610 { "o5", 5, monitor_get_reg },
3611 { "o6", 6, monitor_get_reg },
3612 { "o7", 7, monitor_get_reg },
3613 { "l0", 8, monitor_get_reg },
3614 { "l1", 9, monitor_get_reg },
3615 { "l2", 10, monitor_get_reg },
3616 { "l3", 11, monitor_get_reg },
3617 { "l4", 12, monitor_get_reg },
3618 { "l5", 13, monitor_get_reg },
3619 { "l6", 14, monitor_get_reg },
3620 { "l7", 15, monitor_get_reg },
3621 { "i0", 16, monitor_get_reg },
3622 { "i1", 17, monitor_get_reg },
3623 { "i2", 18, monitor_get_reg },
3624 { "i3", 19, monitor_get_reg },
3625 { "i4", 20, monitor_get_reg },
3626 { "i5", 21, monitor_get_reg },
3627 { "i6", 22, monitor_get_reg },
3628 { "i7", 23, monitor_get_reg },
3629 { "pc", offsetof(CPUState, pc) },
3630 { "npc", offsetof(CPUState, npc) },
3631 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003632#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003633 { "psr", 0, &monitor_get_psr, },
3634 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003635#endif
bellarde95c8d52004-09-30 22:22:08 +00003636 { "tbr", offsetof(CPUState, tbr) },
3637 { "fsr", offsetof(CPUState, fsr) },
3638 { "f0", offsetof(CPUState, fpr[0]) },
3639 { "f1", offsetof(CPUState, fpr[1]) },
3640 { "f2", offsetof(CPUState, fpr[2]) },
3641 { "f3", offsetof(CPUState, fpr[3]) },
3642 { "f4", offsetof(CPUState, fpr[4]) },
3643 { "f5", offsetof(CPUState, fpr[5]) },
3644 { "f6", offsetof(CPUState, fpr[6]) },
3645 { "f7", offsetof(CPUState, fpr[7]) },
3646 { "f8", offsetof(CPUState, fpr[8]) },
3647 { "f9", offsetof(CPUState, fpr[9]) },
3648 { "f10", offsetof(CPUState, fpr[10]) },
3649 { "f11", offsetof(CPUState, fpr[11]) },
3650 { "f12", offsetof(CPUState, fpr[12]) },
3651 { "f13", offsetof(CPUState, fpr[13]) },
3652 { "f14", offsetof(CPUState, fpr[14]) },
3653 { "f15", offsetof(CPUState, fpr[15]) },
3654 { "f16", offsetof(CPUState, fpr[16]) },
3655 { "f17", offsetof(CPUState, fpr[17]) },
3656 { "f18", offsetof(CPUState, fpr[18]) },
3657 { "f19", offsetof(CPUState, fpr[19]) },
3658 { "f20", offsetof(CPUState, fpr[20]) },
3659 { "f21", offsetof(CPUState, fpr[21]) },
3660 { "f22", offsetof(CPUState, fpr[22]) },
3661 { "f23", offsetof(CPUState, fpr[23]) },
3662 { "f24", offsetof(CPUState, fpr[24]) },
3663 { "f25", offsetof(CPUState, fpr[25]) },
3664 { "f26", offsetof(CPUState, fpr[26]) },
3665 { "f27", offsetof(CPUState, fpr[27]) },
3666 { "f28", offsetof(CPUState, fpr[28]) },
3667 { "f29", offsetof(CPUState, fpr[29]) },
3668 { "f30", offsetof(CPUState, fpr[30]) },
3669 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003670#ifdef TARGET_SPARC64
3671 { "f32", offsetof(CPUState, fpr[32]) },
3672 { "f34", offsetof(CPUState, fpr[34]) },
3673 { "f36", offsetof(CPUState, fpr[36]) },
3674 { "f38", offsetof(CPUState, fpr[38]) },
3675 { "f40", offsetof(CPUState, fpr[40]) },
3676 { "f42", offsetof(CPUState, fpr[42]) },
3677 { "f44", offsetof(CPUState, fpr[44]) },
3678 { "f46", offsetof(CPUState, fpr[46]) },
3679 { "f48", offsetof(CPUState, fpr[48]) },
3680 { "f50", offsetof(CPUState, fpr[50]) },
3681 { "f52", offsetof(CPUState, fpr[52]) },
3682 { "f54", offsetof(CPUState, fpr[54]) },
3683 { "f56", offsetof(CPUState, fpr[56]) },
3684 { "f58", offsetof(CPUState, fpr[58]) },
3685 { "f60", offsetof(CPUState, fpr[60]) },
3686 { "f62", offsetof(CPUState, fpr[62]) },
3687 { "asi", offsetof(CPUState, asi) },
3688 { "pstate", offsetof(CPUState, pstate) },
3689 { "cansave", offsetof(CPUState, cansave) },
3690 { "canrestore", offsetof(CPUState, canrestore) },
3691 { "otherwin", offsetof(CPUState, otherwin) },
3692 { "wstate", offsetof(CPUState, wstate) },
3693 { "cleanwin", offsetof(CPUState, cleanwin) },
3694 { "fprs", offsetof(CPUState, fprs) },
3695#endif
bellard9307c4c2004-04-04 12:57:25 +00003696#endif
3697 { NULL },
3698};
3699
aliguori376253e2009-03-05 23:01:23 +00003700static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003701{
aliguori376253e2009-03-05 23:01:23 +00003702 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003703 longjmp(expr_env, 1);
3704}
3705
Markus Armbruster09b94182010-01-20 13:07:30 +01003706/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003707static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003708{
blueswir18662d652008-10-02 18:32:44 +00003709 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003710 void *ptr;
3711
bellard9307c4c2004-04-04 12:57:25 +00003712 for(md = monitor_defs; md->name != NULL; md++) {
3713 if (compare_cmd(name, md->name)) {
3714 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003715 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003716 } else {
bellard6a00d602005-11-21 23:25:50 +00003717 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003718 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003719 switch(md->type) {
3720 case MD_I32:
3721 *pval = *(int32_t *)ptr;
3722 break;
3723 case MD_TLONG:
3724 *pval = *(target_long *)ptr;
3725 break;
3726 default:
3727 *pval = 0;
3728 break;
3729 }
bellard9307c4c2004-04-04 12:57:25 +00003730 }
3731 return 0;
3732 }
3733 }
3734 return -1;
3735}
3736
3737static void next(void)
3738{
Blue Swirl660f11b2009-07-31 21:16:51 +00003739 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003740 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003741 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003742 pch++;
3743 }
3744}
3745
aliguori376253e2009-03-05 23:01:23 +00003746static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003747
aliguori376253e2009-03-05 23:01:23 +00003748static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003749{
blueswir1c2efc952007-09-25 17:28:42 +00003750 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003751 char *p;
bellard6a00d602005-11-21 23:25:50 +00003752 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003753
3754 switch(*pch) {
3755 case '+':
3756 next();
aliguori376253e2009-03-05 23:01:23 +00003757 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003758 break;
3759 case '-':
3760 next();
aliguori376253e2009-03-05 23:01:23 +00003761 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003762 break;
3763 case '~':
3764 next();
aliguori376253e2009-03-05 23:01:23 +00003765 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003766 break;
3767 case '(':
3768 next();
aliguori376253e2009-03-05 23:01:23 +00003769 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003770 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003771 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003772 }
3773 next();
3774 break;
bellard81d09122004-07-14 17:21:37 +00003775 case '\'':
3776 pch++;
3777 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003778 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003779 n = *pch;
3780 pch++;
3781 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003782 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003783 next();
3784 break;
bellard9307c4c2004-04-04 12:57:25 +00003785 case '$':
3786 {
3787 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003788 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003789
bellard9307c4c2004-04-04 12:57:25 +00003790 pch++;
3791 q = buf;
3792 while ((*pch >= 'a' && *pch <= 'z') ||
3793 (*pch >= 'A' && *pch <= 'Z') ||
3794 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003795 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003796 if ((q - buf) < sizeof(buf) - 1)
3797 *q++ = *pch;
3798 pch++;
3799 }
blueswir1cd390082008-11-16 13:53:32 +00003800 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003801 pch++;
3802 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003803 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003804 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003805 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003806 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003807 }
3808 break;
3809 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003810 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003811 n = 0;
3812 break;
3813 default:
blueswir17743e582007-09-24 18:39:04 +00003814#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003815 n = strtoull(pch, &p, 0);
3816#else
bellard9307c4c2004-04-04 12:57:25 +00003817 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003818#endif
bellard9307c4c2004-04-04 12:57:25 +00003819 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003820 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003821 }
3822 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003823 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003824 pch++;
3825 break;
3826 }
3827 return n;
3828}
3829
3830
aliguori376253e2009-03-05 23:01:23 +00003831static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003832{
blueswir1c2efc952007-09-25 17:28:42 +00003833 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003834 int op;
ths3b46e622007-09-17 08:09:54 +00003835
aliguori376253e2009-03-05 23:01:23 +00003836 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003837 for(;;) {
3838 op = *pch;
3839 if (op != '*' && op != '/' && op != '%')
3840 break;
3841 next();
aliguori376253e2009-03-05 23:01:23 +00003842 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003843 switch(op) {
3844 default:
3845 case '*':
3846 val *= val2;
3847 break;
3848 case '/':
3849 case '%':
ths5fafdf22007-09-16 21:08:06 +00003850 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003851 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003852 if (op == '/')
3853 val /= val2;
3854 else
3855 val %= val2;
3856 break;
3857 }
3858 }
3859 return val;
3860}
3861
aliguori376253e2009-03-05 23:01:23 +00003862static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003863{
blueswir1c2efc952007-09-25 17:28:42 +00003864 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003865 int op;
bellard9307c4c2004-04-04 12:57:25 +00003866
aliguori376253e2009-03-05 23:01:23 +00003867 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003868 for(;;) {
3869 op = *pch;
3870 if (op != '&' && op != '|' && op != '^')
3871 break;
3872 next();
aliguori376253e2009-03-05 23:01:23 +00003873 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003874 switch(op) {
3875 default:
3876 case '&':
3877 val &= val2;
3878 break;
3879 case '|':
3880 val |= val2;
3881 break;
3882 case '^':
3883 val ^= val2;
3884 break;
3885 }
3886 }
3887 return val;
3888}
3889
aliguori376253e2009-03-05 23:01:23 +00003890static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003891{
blueswir1c2efc952007-09-25 17:28:42 +00003892 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003893 int op;
bellard9307c4c2004-04-04 12:57:25 +00003894
aliguori376253e2009-03-05 23:01:23 +00003895 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003896 for(;;) {
3897 op = *pch;
3898 if (op != '+' && op != '-')
3899 break;
3900 next();
aliguori376253e2009-03-05 23:01:23 +00003901 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003902 if (op == '+')
3903 val += val2;
3904 else
3905 val -= val2;
3906 }
3907 return val;
3908}
3909
aliguori376253e2009-03-05 23:01:23 +00003910static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003911{
3912 pch = *pp;
3913 if (setjmp(expr_env)) {
3914 *pp = pch;
3915 return -1;
3916 }
blueswir1cd390082008-11-16 13:53:32 +00003917 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003918 pch++;
aliguori376253e2009-03-05 23:01:23 +00003919 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003920 *pp = pch;
3921 return 0;
3922}
3923
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003924static int get_double(Monitor *mon, double *pval, const char **pp)
3925{
3926 const char *p = *pp;
3927 char *tailp;
3928 double d;
3929
3930 d = strtod(p, &tailp);
3931 if (tailp == p) {
3932 monitor_printf(mon, "Number expected\n");
3933 return -1;
3934 }
3935 if (d != d || d - d != 0) {
3936 /* NaN or infinity */
3937 monitor_printf(mon, "Bad number\n");
3938 return -1;
3939 }
3940 *pval = d;
3941 *pp = tailp;
3942 return 0;
3943}
3944
bellard9307c4c2004-04-04 12:57:25 +00003945static int get_str(char *buf, int buf_size, const char **pp)
3946{
3947 const char *p;
3948 char *q;
3949 int c;
3950
bellard81d09122004-07-14 17:21:37 +00003951 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003952 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003953 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003954 p++;
3955 if (*p == '\0') {
3956 fail:
bellard81d09122004-07-14 17:21:37 +00003957 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003958 *pp = p;
3959 return -1;
3960 }
bellard9307c4c2004-04-04 12:57:25 +00003961 if (*p == '\"') {
3962 p++;
3963 while (*p != '\0' && *p != '\"') {
3964 if (*p == '\\') {
3965 p++;
3966 c = *p++;
3967 switch(c) {
3968 case 'n':
3969 c = '\n';
3970 break;
3971 case 'r':
3972 c = '\r';
3973 break;
3974 case '\\':
3975 case '\'':
3976 case '\"':
3977 break;
3978 default:
3979 qemu_printf("unsupported escape code: '\\%c'\n", c);
3980 goto fail;
3981 }
3982 if ((q - buf) < buf_size - 1) {
3983 *q++ = c;
3984 }
3985 } else {
3986 if ((q - buf) < buf_size - 1) {
3987 *q++ = *p;
3988 }
3989 p++;
3990 }
3991 }
3992 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003993 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003994 goto fail;
3995 }
3996 p++;
3997 } else {
blueswir1cd390082008-11-16 13:53:32 +00003998 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003999 if ((q - buf) < buf_size - 1) {
4000 *q++ = *p;
4001 }
4002 p++;
4003 }
bellard9307c4c2004-04-04 12:57:25 +00004004 }
bellard81d09122004-07-14 17:21:37 +00004005 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00004006 *pp = p;
4007 return 0;
4008}
4009
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004010/*
4011 * Store the command-name in cmdname, and return a pointer to
4012 * the remaining of the command string.
4013 */
4014static const char *get_command_name(const char *cmdline,
4015 char *cmdname, size_t nlen)
4016{
4017 size_t len;
4018 const char *p, *pstart;
4019
4020 p = cmdline;
4021 while (qemu_isspace(*p))
4022 p++;
4023 if (*p == '\0')
4024 return NULL;
4025 pstart = p;
4026 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
4027 p++;
4028 len = p - pstart;
4029 if (len > nlen - 1)
4030 len = nlen - 1;
4031 memcpy(cmdname, pstart, len);
4032 cmdname[len] = '\0';
4033 return p;
4034}
4035
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004036/**
4037 * Read key of 'type' into 'key' and return the current
4038 * 'type' pointer.
4039 */
4040static char *key_get_info(const char *type, char **key)
4041{
4042 size_t len;
4043 char *p, *str;
4044
4045 if (*type == ',')
4046 type++;
4047
4048 p = strchr(type, ':');
4049 if (!p) {
4050 *key = NULL;
4051 return NULL;
4052 }
4053 len = p - type;
4054
Anthony Liguori7267c092011-08-20 22:09:37 -05004055 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004056 memcpy(str, type, len);
4057 str[len] = '\0';
4058
4059 *key = str;
4060 return ++p;
4061}
4062
bellard9307c4c2004-04-04 12:57:25 +00004063static int default_fmt_format = 'x';
4064static int default_fmt_size = 4;
4065
4066#define MAX_ARGS 16
4067
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004068static int is_valid_option(const char *c, const char *typestr)
4069{
4070 char option[3];
4071
4072 option[0] = '-';
4073 option[1] = *c;
4074 option[2] = '\0';
4075
4076 typestr = strstr(typestr, option);
4077 return (typestr != NULL);
4078}
4079
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004080static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
4081 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004082{
4083 const mon_cmd_t *cmd;
4084
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004085 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004086 if (compare_cmd(cmdname, cmd->name)) {
4087 return cmd;
4088 }
4089 }
4090
4091 return NULL;
4092}
4093
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004094static const mon_cmd_t *monitor_find_command(const char *cmdname)
4095{
4096 return search_dispatch_table(mon_cmds, cmdname);
4097}
4098
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004099static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
4100{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03004101 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004102}
4103
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004104static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
4105{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03004106 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004107}
4108
Anthony Liguoric227f092009-10-01 16:12:16 -05004109static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004110 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004111 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00004112{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004113 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03004114 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05004115 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00004116 char cmdname[256];
4117 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004118 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00004119
4120#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00004121 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00004122#endif
ths3b46e622007-09-17 08:09:54 +00004123
bellard9307c4c2004-04-04 12:57:25 +00004124 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004125 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
4126 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004127 return NULL;
ths3b46e622007-09-17 08:09:54 +00004128
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004129 cmd = monitor_find_command(cmdname);
4130 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004131 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004132 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004133 }
bellard9307c4c2004-04-04 12:57:25 +00004134
bellard9307c4c2004-04-04 12:57:25 +00004135 /* parse the parameters */
4136 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00004137 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004138 typestr = key_get_info(typestr, &key);
4139 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00004140 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004141 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00004142 typestr++;
4143 switch(c) {
4144 case 'F':
bellard81d09122004-07-14 17:21:37 +00004145 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00004146 case 's':
4147 {
4148 int ret;
ths3b46e622007-09-17 08:09:54 +00004149
blueswir1cd390082008-11-16 13:53:32 +00004150 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004151 p++;
4152 if (*typestr == '?') {
4153 typestr++;
4154 if (*p == '\0') {
4155 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03004156 break;
bellard9307c4c2004-04-04 12:57:25 +00004157 }
4158 }
4159 ret = get_str(buf, sizeof(buf), &p);
4160 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00004161 switch(c) {
4162 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004163 monitor_printf(mon, "%s: filename expected\n",
4164 cmdname);
bellard81d09122004-07-14 17:21:37 +00004165 break;
4166 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004167 monitor_printf(mon, "%s: block device name expected\n",
4168 cmdname);
bellard81d09122004-07-14 17:21:37 +00004169 break;
4170 default:
aliguori376253e2009-03-05 23:01:23 +00004171 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004172 break;
4173 }
bellard9307c4c2004-04-04 12:57:25 +00004174 goto fail;
4175 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004176 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004177 }
4178 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004179 case 'O':
4180 {
4181 QemuOptsList *opts_list;
4182 QemuOpts *opts;
4183
4184 opts_list = qemu_find_opts(key);
4185 if (!opts_list || opts_list->desc->name) {
4186 goto bad_type;
4187 }
4188 while (qemu_isspace(*p)) {
4189 p++;
4190 }
4191 if (!*p)
4192 break;
4193 if (get_str(buf, sizeof(buf), &p) < 0) {
4194 goto fail;
4195 }
4196 opts = qemu_opts_parse(opts_list, buf, 1);
4197 if (!opts) {
4198 goto fail;
4199 }
4200 qemu_opts_to_qdict(opts, qdict);
4201 qemu_opts_del(opts);
4202 }
4203 break;
bellard9307c4c2004-04-04 12:57:25 +00004204 case '/':
4205 {
4206 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004207
blueswir1cd390082008-11-16 13:53:32 +00004208 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004209 p++;
4210 if (*p == '/') {
4211 /* format found */
4212 p++;
4213 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004214 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004215 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004216 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004217 count = count * 10 + (*p - '0');
4218 p++;
4219 }
4220 }
4221 size = -1;
4222 format = -1;
4223 for(;;) {
4224 switch(*p) {
4225 case 'o':
4226 case 'd':
4227 case 'u':
4228 case 'x':
4229 case 'i':
4230 case 'c':
4231 format = *p++;
4232 break;
4233 case 'b':
4234 size = 1;
4235 p++;
4236 break;
4237 case 'h':
4238 size = 2;
4239 p++;
4240 break;
4241 case 'w':
4242 size = 4;
4243 p++;
4244 break;
4245 case 'g':
4246 case 'L':
4247 size = 8;
4248 p++;
4249 break;
4250 default:
4251 goto next;
4252 }
4253 }
4254 next:
blueswir1cd390082008-11-16 13:53:32 +00004255 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004256 monitor_printf(mon, "invalid char in format: '%c'\n",
4257 *p);
bellard9307c4c2004-04-04 12:57:25 +00004258 goto fail;
4259 }
bellard9307c4c2004-04-04 12:57:25 +00004260 if (format < 0)
4261 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004262 if (format != 'i') {
4263 /* for 'i', not specifying a size gives -1 as size */
4264 if (size < 0)
4265 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004266 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004267 }
bellard9307c4c2004-04-04 12:57:25 +00004268 default_fmt_format = format;
4269 } else {
4270 count = 1;
4271 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004272 if (format != 'i') {
4273 size = default_fmt_size;
4274 } else {
4275 size = -1;
4276 }
bellard9307c4c2004-04-04 12:57:25 +00004277 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004278 qdict_put(qdict, "count", qint_from_int(count));
4279 qdict_put(qdict, "format", qint_from_int(format));
4280 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004281 }
4282 break;
4283 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004284 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004285 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004286 {
blueswir1c2efc952007-09-25 17:28:42 +00004287 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004288
blueswir1cd390082008-11-16 13:53:32 +00004289 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004290 p++;
bellard34405572004-06-08 00:55:58 +00004291 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004292 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004293 if (*p == '\0') {
4294 typestr++;
4295 break;
4296 }
bellard34405572004-06-08 00:55:58 +00004297 } else {
4298 if (*p == '.') {
4299 p++;
blueswir1cd390082008-11-16 13:53:32 +00004300 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004301 p++;
bellard34405572004-06-08 00:55:58 +00004302 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004303 typestr++;
4304 break;
bellard34405572004-06-08 00:55:58 +00004305 }
4306 }
bellard13224a82006-07-14 22:03:35 +00004307 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004308 }
aliguori376253e2009-03-05 23:01:23 +00004309 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004310 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004311 /* Check if 'i' is greater than 32-bit */
4312 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4313 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4314 monitor_printf(mon, "integer is for 32-bit values\n");
4315 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004316 } else if (c == 'M') {
4317 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004318 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004319 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004320 }
4321 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004322 case 'o':
4323 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004324 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004325 char *end;
4326
4327 while (qemu_isspace(*p)) {
4328 p++;
4329 }
4330 if (*typestr == '?') {
4331 typestr++;
4332 if (*p == '\0') {
4333 break;
4334 }
4335 }
4336 val = strtosz(p, &end);
4337 if (val < 0) {
4338 monitor_printf(mon, "invalid size\n");
4339 goto fail;
4340 }
4341 qdict_put(qdict, key, qint_from_int(val));
4342 p = end;
4343 }
4344 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004345 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004346 {
4347 double val;
4348
4349 while (qemu_isspace(*p))
4350 p++;
4351 if (*typestr == '?') {
4352 typestr++;
4353 if (*p == '\0') {
4354 break;
4355 }
4356 }
4357 if (get_double(mon, &val, &p) < 0) {
4358 goto fail;
4359 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004360 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004361 switch (*p) {
4362 case 'm':
4363 val /= 1e3; p += 2; break;
4364 case 'u':
4365 val /= 1e6; p += 2; break;
4366 case 'n':
4367 val /= 1e9; p += 2; break;
4368 }
4369 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004370 if (*p && !qemu_isspace(*p)) {
4371 monitor_printf(mon, "Unknown unit suffix\n");
4372 goto fail;
4373 }
4374 qdict_put(qdict, key, qfloat_from_double(val));
4375 }
4376 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004377 case 'b':
4378 {
4379 const char *beg;
4380 int val;
4381
4382 while (qemu_isspace(*p)) {
4383 p++;
4384 }
4385 beg = p;
4386 while (qemu_isgraph(*p)) {
4387 p++;
4388 }
4389 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4390 val = 1;
4391 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4392 val = 0;
4393 } else {
4394 monitor_printf(mon, "Expected 'on' or 'off'\n");
4395 goto fail;
4396 }
4397 qdict_put(qdict, key, qbool_from_int(val));
4398 }
4399 break;
bellard9307c4c2004-04-04 12:57:25 +00004400 case '-':
4401 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004402 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004403 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004404 /* option */
ths3b46e622007-09-17 08:09:54 +00004405
bellard9307c4c2004-04-04 12:57:25 +00004406 c = *typestr++;
4407 if (c == '\0')
4408 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004409 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004410 p++;
bellard9307c4c2004-04-04 12:57:25 +00004411 if (*p == '-') {
4412 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004413 if(c != *p) {
4414 if(!is_valid_option(p, typestr)) {
4415
4416 monitor_printf(mon, "%s: unsupported option -%c\n",
4417 cmdname, *p);
4418 goto fail;
4419 } else {
4420 skip_key = 1;
4421 }
bellard9307c4c2004-04-04 12:57:25 +00004422 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004423 if(skip_key) {
4424 p = tmp;
4425 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004426 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004427 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004428 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004429 }
bellard9307c4c2004-04-04 12:57:25 +00004430 }
bellard9307c4c2004-04-04 12:57:25 +00004431 }
4432 break;
4433 default:
4434 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004435 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004436 goto fail;
4437 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004438 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004439 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004440 }
4441 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004442 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004443 p++;
4444 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004445 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4446 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004447 goto fail;
4448 }
4449
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004450 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004451
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004452fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004453 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004454 return NULL;
4455}
4456
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004457void monitor_set_error(Monitor *mon, QError *qerror)
4458{
4459 /* report only the first error */
4460 if (!mon->error) {
4461 mon->error = qerror;
4462 } else {
4463 MON_DEBUG("Additional error report at %s:%d\n",
4464 qerror->file, qerror->linenr);
4465 QDECREF(qerror);
4466 }
4467}
4468
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004469static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4470{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004471 if (ret && !monitor_has_error(mon)) {
4472 /*
4473 * If it returns failure, it must have passed on error.
4474 *
4475 * Action: Report an internal error to the client if in QMP.
4476 */
4477 qerror_report(QERR_UNDEFINED_ERROR);
4478 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4479 cmd->name);
4480 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004481
4482#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004483 if (!ret && monitor_has_error(mon)) {
4484 /*
4485 * If it returns success, it must not have passed an error.
4486 *
4487 * Action: Report the passed error to the client.
4488 */
4489 MON_DEBUG("command '%s' returned success but passed an error\n",
4490 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004491 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004492
4493 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4494 /*
4495 * Handlers should not call Monitor print functions.
4496 *
4497 * Action: Ignore them in QMP.
4498 *
4499 * (XXX: we don't check any 'info' or 'query' command here
4500 * because the user print function _is_ called by do_info(), hence
4501 * we will trigger this check. This problem will go away when we
4502 * make 'query' commands real and kill do_info())
4503 */
4504 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4505 cmd->name, mon_print_count_get(mon));
4506 }
4507#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004508}
4509
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004510static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004511{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004512 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004513 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004514
4515 qdict = qdict_new();
4516
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004517 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004518 if (!cmd)
4519 goto out;
4520
Luiz Capitulino4903de02010-09-16 11:01:32 -03004521 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004522 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004523 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004524 QObject *data = NULL;
4525
4526 /* XXX: ignores the error code */
4527 cmd->mhandler.cmd_new(mon, qdict, &data);
4528 assert(!monitor_has_error(mon));
4529 if (data) {
4530 cmd->user_print(mon, data);
4531 qobject_decref(data);
4532 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004533 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004534 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004535 }
4536
Luiz Capitulino13917be2009-10-07 13:41:54 -03004537out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004538 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004539}
4540
bellard81d09122004-07-14 17:21:37 +00004541static void cmd_completion(const char *name, const char *list)
4542{
4543 const char *p, *pstart;
4544 char cmd[128];
4545 int len;
4546
4547 p = list;
4548 for(;;) {
4549 pstart = p;
4550 p = strchr(p, '|');
4551 if (!p)
4552 p = pstart + strlen(pstart);
4553 len = p - pstart;
4554 if (len > sizeof(cmd) - 2)
4555 len = sizeof(cmd) - 2;
4556 memcpy(cmd, pstart, len);
4557 cmd[len] = '\0';
4558 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004559 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004560 }
4561 if (*p == '\0')
4562 break;
4563 p++;
4564 }
4565}
4566
4567static void file_completion(const char *input)
4568{
4569 DIR *ffs;
4570 struct dirent *d;
4571 char path[1024];
4572 char file[1024], file_prefix[1024];
4573 int input_path_len;
4574 const char *p;
4575
ths5fafdf22007-09-16 21:08:06 +00004576 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004577 if (!p) {
4578 input_path_len = 0;
4579 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004580 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004581 } else {
4582 input_path_len = p - input + 1;
4583 memcpy(path, input, input_path_len);
4584 if (input_path_len > sizeof(path) - 1)
4585 input_path_len = sizeof(path) - 1;
4586 path[input_path_len] = '\0';
4587 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4588 }
4589#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004590 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4591 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004592#endif
4593 ffs = opendir(path);
4594 if (!ffs)
4595 return;
4596 for(;;) {
4597 struct stat sb;
4598 d = readdir(ffs);
4599 if (!d)
4600 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004601
4602 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4603 continue;
4604 }
4605
bellard81d09122004-07-14 17:21:37 +00004606 if (strstart(d->d_name, file_prefix, NULL)) {
4607 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004608 if (input_path_len < sizeof(file))
4609 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4610 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004611 /* stat the file to find out if it's a directory.
4612 * In that case add a slash to speed up typing long paths
4613 */
4614 stat(file, &sb);
4615 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004616 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004617 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004618 }
4619 }
4620 closedir(ffs);
4621}
4622
aliguori51de9762009-03-05 23:00:43 +00004623static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004624{
aliguori51de9762009-03-05 23:00:43 +00004625 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004626 const char *input = opaque;
4627
4628 if (input[0] == '\0' ||
4629 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004630 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004631 }
4632}
4633
4634/* NOTE: this parser is an approximate form of the real command parser */
4635static void parse_cmdline(const char *cmdline,
4636 int *pnb_args, char **args)
4637{
4638 const char *p;
4639 int nb_args, ret;
4640 char buf[1024];
4641
4642 p = cmdline;
4643 nb_args = 0;
4644 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004645 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004646 p++;
4647 if (*p == '\0')
4648 break;
4649 if (nb_args >= MAX_ARGS)
4650 break;
4651 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004652 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004653 nb_args++;
4654 if (ret < 0)
4655 break;
4656 }
4657 *pnb_args = nb_args;
4658}
4659
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004660static const char *next_arg_type(const char *typestr)
4661{
4662 const char *p = strchr(typestr, ':');
4663 return (p != NULL ? ++p : typestr);
4664}
4665
aliguori4c36ba32009-03-05 23:01:37 +00004666static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004667{
4668 const char *cmdname;
4669 char *args[MAX_ARGS];
4670 int nb_args, i, len;
4671 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004672 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004673 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004674
4675 parse_cmdline(cmdline, &nb_args, args);
4676#ifdef DEBUG_COMPLETION
4677 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004678 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004679 }
4680#endif
4681
4682 /* if the line ends with a space, it means we want to complete the
4683 next arg */
4684 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004685 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004686 if (nb_args >= MAX_ARGS) {
4687 goto cleanup;
4688 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004689 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004690 }
4691 if (nb_args <= 1) {
4692 /* command completion */
4693 if (nb_args == 0)
4694 cmdname = "";
4695 else
4696 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004697 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004698 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004699 cmd_completion(cmdname, cmd->name);
4700 }
4701 } else {
4702 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004703 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4704 if (compare_cmd(args[0], cmd->name)) {
4705 break;
4706 }
bellard81d09122004-07-14 17:21:37 +00004707 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004708 if (!cmd->name) {
4709 goto cleanup;
4710 }
4711
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004712 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004713 for(i = 0; i < nb_args - 2; i++) {
4714 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004715 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004716 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004717 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004718 }
4719 }
4720 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004721 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004722 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004723 }
bellard81d09122004-07-14 17:21:37 +00004724 switch(*ptype) {
4725 case 'F':
4726 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004727 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004728 file_completion(str);
4729 break;
4730 case 'B':
4731 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004732 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004733 bdrv_iterate(block_completion_it, (void *)str);
4734 break;
bellard7fe48482004-10-09 18:08:01 +00004735 case 's':
4736 /* XXX: more generic ? */
4737 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004738 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004739 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4740 cmd_completion(str, cmd->name);
4741 }
bellard64866c32006-05-07 18:03:31 +00004742 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004743 char *sep = strrchr(str, '-');
4744 if (sep)
4745 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004746 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004747 for(key = key_defs; key->name != NULL; key++) {
4748 cmd_completion(str, key->name);
4749 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004750 } else if (!strcmp(cmd->name, "help|?")) {
4751 readline_set_completion_index(cur_mon->rs, strlen(str));
4752 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4753 cmd_completion(str, cmd->name);
4754 }
bellard7fe48482004-10-09 18:08:01 +00004755 }
4756 break;
bellard81d09122004-07-14 17:21:37 +00004757 default:
4758 break;
4759 }
4760 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004761
4762cleanup:
4763 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004764 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004765 }
bellard81d09122004-07-14 17:21:37 +00004766}
4767
aliguori731b0362009-03-05 23:01:42 +00004768static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004769{
aliguori731b0362009-03-05 23:01:42 +00004770 Monitor *mon = opaque;
4771
Jan Kiszkac62313b2009-12-04 14:05:29 +01004772 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004773}
4774
Luiz Capitulino09069b12010-02-04 18:10:06 -02004775static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4776{
4777 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4778 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4779}
4780
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004781/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004782 * Argument validation rules:
4783 *
4784 * 1. The argument must exist in cmd_args qdict
4785 * 2. The argument type must be the expected one
4786 *
4787 * Special case: If the argument doesn't exist in cmd_args and
4788 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4789 * checking is skipped for it.
4790 */
4791static int check_client_args_type(const QDict *client_args,
4792 const QDict *cmd_args, int flags)
4793{
4794 const QDictEntry *ent;
4795
4796 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4797 QObject *obj;
4798 QString *arg_type;
4799 const QObject *client_arg = qdict_entry_value(ent);
4800 const char *client_arg_name = qdict_entry_key(ent);
4801
4802 obj = qdict_get(cmd_args, client_arg_name);
4803 if (!obj) {
4804 if (flags & QMP_ACCEPT_UNKNOWNS) {
4805 /* handler accepts unknowns */
4806 continue;
4807 }
4808 /* client arg doesn't exist */
4809 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4810 return -1;
4811 }
4812
4813 arg_type = qobject_to_qstring(obj);
4814 assert(arg_type != NULL);
4815
4816 /* check if argument's type is correct */
4817 switch (qstring_get_str(arg_type)[0]) {
4818 case 'F':
4819 case 'B':
4820 case 's':
4821 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4822 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4823 "string");
4824 return -1;
4825 }
4826 break;
4827 case 'i':
4828 case 'l':
4829 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004830 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004831 if (qobject_type(client_arg) != QTYPE_QINT) {
4832 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4833 "int");
4834 return -1;
4835 }
4836 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004837 case 'T':
4838 if (qobject_type(client_arg) != QTYPE_QINT &&
4839 qobject_type(client_arg) != QTYPE_QFLOAT) {
4840 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4841 "number");
4842 return -1;
4843 }
4844 break;
4845 case 'b':
4846 case '-':
4847 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4848 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4849 "bool");
4850 return -1;
4851 }
4852 break;
4853 case 'O':
4854 assert(flags & QMP_ACCEPT_UNKNOWNS);
4855 break;
4856 case '/':
4857 case '.':
4858 /*
4859 * These types are not supported by QMP and thus are not
4860 * handled here. Fall through.
4861 */
4862 default:
4863 abort();
4864 }
4865 }
4866
4867 return 0;
4868}
4869
4870/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004871 * - Check if the client has passed all mandatory args
4872 * - Set special flags for argument validation
4873 */
4874static int check_mandatory_args(const QDict *cmd_args,
4875 const QDict *client_args, int *flags)
4876{
4877 const QDictEntry *ent;
4878
4879 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4880 const char *cmd_arg_name = qdict_entry_key(ent);
4881 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4882 assert(type != NULL);
4883
4884 if (qstring_get_str(type)[0] == 'O') {
4885 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4886 *flags |= QMP_ACCEPT_UNKNOWNS;
4887 } else if (qstring_get_str(type)[0] != '-' &&
4888 qstring_get_str(type)[1] != '?' &&
4889 !qdict_haskey(client_args, cmd_arg_name)) {
4890 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4891 return -1;
4892 }
4893 }
4894
4895 return 0;
4896}
4897
4898static QDict *qdict_from_args_type(const char *args_type)
4899{
4900 int i;
4901 QDict *qdict;
4902 QString *key, *type, *cur_qs;
4903
4904 assert(args_type != NULL);
4905
4906 qdict = qdict_new();
4907
4908 if (args_type == NULL || args_type[0] == '\0') {
4909 /* no args, empty qdict */
4910 goto out;
4911 }
4912
4913 key = qstring_new();
4914 type = qstring_new();
4915
4916 cur_qs = key;
4917
4918 for (i = 0;; i++) {
4919 switch (args_type[i]) {
4920 case ',':
4921 case '\0':
4922 qdict_put(qdict, qstring_get_str(key), type);
4923 QDECREF(key);
4924 if (args_type[i] == '\0') {
4925 goto out;
4926 }
4927 type = qstring_new(); /* qdict has ref */
4928 cur_qs = key = qstring_new();
4929 break;
4930 case ':':
4931 cur_qs = type;
4932 break;
4933 default:
4934 qstring_append_chr(cur_qs, args_type[i]);
4935 break;
4936 }
4937 }
4938
4939out:
4940 return qdict;
4941}
4942
4943/*
4944 * Client argument checking rules:
4945 *
4946 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004947 * 2. Each argument provided by the client must be expected
4948 * 3. Each argument provided by the client must have the type expected
4949 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004950 */
4951static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4952{
4953 int flags, err;
4954 QDict *cmd_args;
4955
4956 cmd_args = qdict_from_args_type(cmd->args_type);
4957
4958 flags = 0;
4959 err = check_mandatory_args(cmd_args, client_args, &flags);
4960 if (err) {
4961 goto out;
4962 }
4963
Luiz Capitulino4af91932010-06-22 11:44:05 -03004964 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004965
4966out:
4967 QDECREF(cmd_args);
4968 return err;
4969}
4970
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004971/*
4972 * Input object checking rules
4973 *
4974 * 1. Input object must be a dict
4975 * 2. The "execute" key must exist
4976 * 3. The "execute" key must be a string
4977 * 4. If the "arguments" key exists, it must be a dict
4978 * 5. If the "id" key exists, it can be anything (ie. json-value)
4979 * 6. Any argument not listed above is considered invalid
4980 */
4981static QDict *qmp_check_input_obj(QObject *input_obj)
4982{
4983 const QDictEntry *ent;
4984 int has_exec_key = 0;
4985 QDict *input_dict;
4986
4987 if (qobject_type(input_obj) != QTYPE_QDICT) {
4988 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4989 return NULL;
4990 }
4991
4992 input_dict = qobject_to_qdict(input_obj);
4993
4994 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4995 const char *arg_name = qdict_entry_key(ent);
4996 const QObject *arg_obj = qdict_entry_value(ent);
4997
4998 if (!strcmp(arg_name, "execute")) {
4999 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
5000 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
5001 "string");
5002 return NULL;
5003 }
5004 has_exec_key = 1;
5005 } else if (!strcmp(arg_name, "arguments")) {
5006 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5007 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5008 "object");
5009 return NULL;
5010 }
5011 } else if (!strcmp(arg_name, "id")) {
5012 /* FIXME: check duplicated IDs for async commands */
5013 } else {
5014 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5015 return NULL;
5016 }
5017 }
5018
5019 if (!has_exec_key) {
5020 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5021 return NULL;
5022 }
5023
5024 return input_dict;
5025}
5026
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005027static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
5028{
5029 QObject *ret_data = NULL;
5030
Luiz Capitulino4903de02010-09-16 11:01:32 -03005031 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005032 qmp_async_info_handler(mon, cmd);
5033 if (monitor_has_error(mon)) {
5034 monitor_protocol_emitter(mon, NULL);
5035 }
5036 } else {
5037 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02005038 monitor_protocol_emitter(mon, ret_data);
5039 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005040 }
5041}
5042
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005043static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5044 const QDict *params)
5045{
5046 int ret;
5047 QObject *data = NULL;
5048
5049 mon_print_count_init(mon);
5050
5051 ret = cmd->mhandler.cmd_new(mon, params, &data);
5052 handler_audit(mon, cmd, ret);
5053 monitor_protocol_emitter(mon, data);
5054 qobject_decref(data);
5055}
5056
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005057static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5058{
5059 int err;
5060 QObject *obj;
5061 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005062 const mon_cmd_t *cmd;
5063 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005064 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005065
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005066 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005067 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005068
5069 obj = json_parser_parse(tokens, NULL);
5070 if (!obj) {
5071 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005072 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005073 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005074 }
5075
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005076 input = qmp_check_input_obj(obj);
5077 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005078 qobject_decref(obj);
5079 goto err_out;
5080 }
5081
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005082 mon->mc->id = qdict_get(input, "id");
5083 qobject_incref(mon->mc->id);
5084
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005085 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02005086 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005087 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005088 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005089 }
5090
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005091 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005092 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02005093 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03005094 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03005095 }
5096
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005097 if (!cmd) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005098 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5099 goto err_out;
5100 }
5101
5102 obj = qdict_get(input, "arguments");
5103 if (!obj) {
5104 args = qdict_new();
5105 } else {
5106 args = qobject_to_qdict(obj);
5107 QINCREF(args);
5108 }
5109
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005110 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005111 if (err < 0) {
5112 goto err_out;
5113 }
5114
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005115 if (query_cmd) {
5116 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03005117 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005118 err = qmp_async_cmd_handler(mon, cmd, args);
5119 if (err) {
5120 /* emit the error response */
5121 goto err_out;
5122 }
Adam Litke940cc302010-01-25 12:18:44 -06005123 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005124 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005125 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005126
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005127 goto out;
5128
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005129err_out:
5130 monitor_protocol_emitter(mon, NULL);
5131out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005132 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005133 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005134}
5135
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005136/**
5137 * monitor_control_read(): Read and handle QMP input
5138 */
5139static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5140{
5141 Monitor *old_mon = cur_mon;
5142
5143 cur_mon = opaque;
5144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005145 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005146
5147 cur_mon = old_mon;
5148}
5149
aliguori731b0362009-03-05 23:01:42 +00005150static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005151{
aliguori731b0362009-03-05 23:01:42 +00005152 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005153 int i;
aliguori376253e2009-03-05 23:01:23 +00005154
aliguori731b0362009-03-05 23:01:42 +00005155 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005156
aliguoricde76ee2009-03-05 23:01:51 +00005157 if (cur_mon->rs) {
5158 for (i = 0; i < size; i++)
5159 readline_handle_byte(cur_mon->rs, buf[i]);
5160 } else {
5161 if (size == 0 || buf[size - 1] != 0)
5162 monitor_printf(cur_mon, "corrupted command\n");
5163 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005164 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005165 }
aliguori731b0362009-03-05 23:01:42 +00005166
5167 cur_mon = old_mon;
5168}
aliguorid8f44602008-10-06 13:52:44 +00005169
aliguori376253e2009-03-05 23:01:23 +00005170static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005171{
aliguori731b0362009-03-05 23:01:42 +00005172 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005173 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005174 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005175}
5176
aliguoricde76ee2009-03-05 23:01:51 +00005177int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005178{
aliguoricde76ee2009-03-05 23:01:51 +00005179 if (!mon->rs)
5180 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005181 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005182 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005183}
5184
aliguori376253e2009-03-05 23:01:23 +00005185void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005186{
aliguoricde76ee2009-03-05 23:01:51 +00005187 if (!mon->rs)
5188 return;
aliguori731b0362009-03-05 23:01:42 +00005189 if (--mon->suspend_cnt == 0)
5190 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005191}
5192
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005193static QObject *get_qmp_greeting(void)
5194{
5195 QObject *ver;
5196
5197 do_info_version(NULL, &ver);
5198 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5199}
5200
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005201/**
5202 * monitor_control_event(): Print QMP gretting
5203 */
5204static void monitor_control_event(void *opaque, int event)
5205{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005206 QObject *data;
5207 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005208
Luiz Capitulino47116d12010-02-08 17:01:30 -02005209 switch (event) {
5210 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005211 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005212 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005213 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005214 monitor_json_emitter(mon, data);
5215 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005216 break;
5217 case CHR_EVENT_CLOSED:
5218 json_message_parser_destroy(&mon->mc->parser);
5219 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005220 }
5221}
5222
aliguori731b0362009-03-05 23:01:42 +00005223static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005224{
aliguori376253e2009-03-05 23:01:23 +00005225 Monitor *mon = opaque;
5226
aliguori2724b182009-03-05 23:01:47 +00005227 switch (event) {
5228 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005229 mon->mux_out = 0;
5230 if (mon->reset_seen) {
5231 readline_restart(mon->rs);
5232 monitor_resume(mon);
5233 monitor_flush(mon);
5234 } else {
5235 mon->suspend_cnt = 0;
5236 }
aliguori2724b182009-03-05 23:01:47 +00005237 break;
ths86e94de2007-01-05 22:01:59 +00005238
aliguori2724b182009-03-05 23:01:47 +00005239 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005240 if (mon->reset_seen) {
5241 if (mon->suspend_cnt == 0) {
5242 monitor_printf(mon, "\n");
5243 }
5244 monitor_flush(mon);
5245 monitor_suspend(mon);
5246 } else {
5247 mon->suspend_cnt++;
5248 }
5249 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005250 break;
5251
Amit Shahb6b8df52009-10-07 18:31:16 +05305252 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005253 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5254 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005255 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005256 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005257 }
5258 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005259 break;
5260 }
ths86e94de2007-01-05 22:01:59 +00005261}
5262
aliguori76655d62009-03-06 20:27:37 +00005263
5264/*
5265 * Local variables:
5266 * c-indent-level: 4
5267 * c-basic-offset: 4
5268 * tab-width: 8
5269 * End:
5270 */
5271
aliguori731b0362009-03-05 23:01:42 +00005272void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005273{
aliguori731b0362009-03-05 23:01:42 +00005274 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005275 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005276
5277 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005278 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005279 is_first_init = 0;
5280 }
aliguori87127162009-03-05 23:01:29 +00005281
Anthony Liguori7267c092011-08-20 22:09:37 -05005282 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005283
aliguori87127162009-03-05 23:01:29 +00005284 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005285 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005286 if (flags & MONITOR_USE_READLINE) {
5287 mon->rs = readline_init(mon, monitor_find_completion);
5288 monitor_read_command(mon, 0);
5289 }
aliguori87127162009-03-05 23:01:29 +00005290
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005291 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005292 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005293 /* Control mode requires special handlers */
5294 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5295 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005296 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005297 } else {
5298 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5299 monitor_event, mon);
5300 }
aliguori87127162009-03-05 23:01:29 +00005301
Blue Swirl72cf2d42009-09-12 07:36:22 +00005302 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005303 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5304 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005305}
5306
aliguori376253e2009-03-05 23:01:23 +00005307static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005308{
aliguoribb5fc202009-03-05 23:01:15 +00005309 BlockDriverState *bs = opaque;
5310 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005311
aliguoribb5fc202009-03-05 23:01:15 +00005312 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005313 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005314 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005315 }
aliguori731b0362009-03-05 23:01:42 +00005316 if (mon->password_completion_cb)
5317 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005318
aliguori731b0362009-03-05 23:01:42 +00005319 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005320}
aliguoric0f4ce72009-03-05 23:01:01 +00005321
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005322int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5323 BlockDriverCompletionFunc *completion_cb,
5324 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005325{
aliguoricde76ee2009-03-05 23:01:51 +00005326 int err;
5327
aliguoribb5fc202009-03-05 23:01:15 +00005328 if (!bdrv_key_required(bs)) {
5329 if (completion_cb)
5330 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005331 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005332 }
aliguoric0f4ce72009-03-05 23:01:01 +00005333
Luiz Capitulino94171e12009-12-07 21:37:00 +01005334 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005335 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005336 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005337 }
5338
aliguori376253e2009-03-05 23:01:23 +00005339 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5340 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005341
aliguori731b0362009-03-05 23:01:42 +00005342 mon->password_completion_cb = completion_cb;
5343 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005344
aliguoricde76ee2009-03-05 23:01:51 +00005345 err = monitor_read_password(mon, bdrv_password_cb, bs);
5346
5347 if (err && completion_cb)
5348 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005349
5350 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005351}