blob: e267d5899c5bbc4f86d564582696aa06060bae1a [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000039#include "monitor.h"
40#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020042#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000044#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000045#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000046#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000047#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000048#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000049#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010051#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030052#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Lluís31965ae2011-08-31 20:31:24 +020060#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020061#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020062#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053063#endif
Lluísfc764102011-08-31 20:31:18 +020064#include "trace/control.h"
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Blue Swirl314e2982011-09-11 20:22:05 +000066#include "memory.h"
ths6a5bd302007-12-03 17:05:38 +000067
bellard9dc39cb2004-03-14 21:38:27 +000068//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000069//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000070
bellard9307c4c2004-04-04 12:57:25 +000071/*
72 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000073 *
bellard9307c4c2004-04-04 12:57:25 +000074 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000075 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000076 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010077 * 'O' option string of the form NAME=VALUE,...
78 * parsed according to QemuOptsList given by its name
79 * Example: 'device:O' uses qemu_device_opts.
80 * Restriction: only lists with empty desc are supported
81 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000082 * 'i' 32 bit integer
83 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010084 * 'M' just like 'l', except in user mode the value is
85 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020086 * 'o' octets (aka bytes)
87 * user mode accepts an optional T, t, G, g, M, m, K, k
88 * suffix, which multiplies the value by 2^40 for
89 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
90 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010091 * 'T' double
92 * user mode accepts an optional ms, us, ns suffix,
93 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000094 * '/' optional gdb-like print format (like "/10x")
95 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030096 * '?' optional type (for all types, except '/')
97 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010098 * 'b' boolean
99 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300100 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000101 *
102 */
103
Adam Litke940cc302010-01-25 12:18:44 -0600104typedef struct MonitorCompletionData MonitorCompletionData;
105struct MonitorCompletionData {
106 Monitor *mon;
107 void (*user_print)(Monitor *mon, const QObject *data);
108};
109
Anthony Liguoric227f092009-10-01 16:12:16 -0500110typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000111 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000112 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000113 const char *params;
114 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300115 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300116 union {
117 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300118 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600119 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300120 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200121 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600122 int (*cmd_async)(Monitor *mon, const QDict *params,
123 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500125 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200126 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500127} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000128
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100129/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500130typedef struct mon_fd_t mon_fd_t;
131struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100132 char *name;
133 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500134 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135};
136
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200137typedef struct MonitorControl {
138 QObject *id;
139 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200140 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200141} MonitorControl;
142
aliguori87127162009-03-05 23:01:29 +0000143struct Monitor {
144 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200145 int mux_out;
146 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000147 int flags;
148 int suspend_cnt;
149 uint8_t outbuf[1024];
150 int outbuf_index;
151 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200152 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000153 CPUState *mon_cpu;
154 BlockDriverCompletionFunc *password_completion_cb;
155 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200156#ifdef CONFIG_DEBUG_MONITOR
157 int print_calls_nr;
158#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200159 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500160 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000161 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000162};
163
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200164#ifdef CONFIG_DEBUG_MONITOR
165#define MON_DEBUG(fmt, ...) do { \
166 fprintf(stderr, "Monitor: "); \
167 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200168
169static inline void mon_print_count_inc(Monitor *mon)
170{
171 mon->print_calls_nr++;
172}
173
174static inline void mon_print_count_init(Monitor *mon)
175{
176 mon->print_calls_nr = 0;
177}
178
179static inline int mon_print_count_get(const Monitor *mon)
180{
181 return mon->print_calls_nr;
182}
183
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200184#else /* !CONFIG_DEBUG_MONITOR */
185#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200186static inline void mon_print_count_inc(Monitor *mon) { }
187static inline void mon_print_count_init(Monitor *mon) { }
188static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200189#endif /* CONFIG_DEBUG_MONITOR */
190
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300191/* QMP checker flags */
192#define QMP_ACCEPT_UNKNOWNS 1
193
Blue Swirl72cf2d42009-09-12 07:36:22 +0000194static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000195
Anthony Liguoric227f092009-10-01 16:12:16 -0500196static const mon_cmd_t mon_cmds[];
197static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000198
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300199static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300200static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300201
Markus Armbruster8631b602010-02-18 11:41:55 +0100202Monitor *cur_mon;
203Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000204
aliguori731b0362009-03-05 23:01:42 +0000205static void monitor_command_cb(Monitor *mon, const char *cmdline,
206 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000207
Luiz Capitulino09069b12010-02-04 18:10:06 -0200208static inline int qmp_cmd_mode(const Monitor *mon)
209{
210 return (mon->mc ? mon->mc->command_mode : 0);
211}
212
Luiz Capitulino418173c2009-11-26 22:58:51 -0200213/* Return true if in control mode, false otherwise */
214static inline int monitor_ctrl_mode(const Monitor *mon)
215{
216 return (mon->flags & MONITOR_USE_CONTROL);
217}
218
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100219/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
220int monitor_cur_is_qmp(void)
221{
222 return cur_mon && monitor_ctrl_mode(cur_mon);
223}
224
aliguori731b0362009-03-05 23:01:42 +0000225static void monitor_read_command(Monitor *mon, int show_prompt)
226{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200227 if (!mon->rs)
228 return;
229
aliguori731b0362009-03-05 23:01:42 +0000230 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
231 if (show_prompt)
232 readline_show_prompt(mon->rs);
233}
bellard6a00d602005-11-21 23:25:50 +0000234
aliguoricde76ee2009-03-05 23:01:51 +0000235static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
236 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000237{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100238 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100239 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100240 return -EINVAL;
241 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000242 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
243 /* prompt is printed on return from the command handler */
244 return 0;
245 } else {
246 monitor_printf(mon, "terminal does not support password prompting\n");
247 return -ENOTTY;
248 }
aliguoribb5fc202009-03-05 23:01:15 +0000249}
250
aliguori376253e2009-03-05 23:01:23 +0000251void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000252{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200253 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500254 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000255 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000256 }
257}
258
259/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000260static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000261{
ths60fe76f2007-12-16 03:02:09 +0000262 char c;
aliguori731b0362009-03-05 23:01:42 +0000263
bellard7e2515e2004-08-01 21:52:19 +0000264 for(;;) {
265 c = *str++;
266 if (c == '\0')
267 break;
bellard7ba12602006-07-14 20:26:42 +0000268 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000269 mon->outbuf[mon->outbuf_index++] = '\r';
270 mon->outbuf[mon->outbuf_index++] = c;
271 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
272 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000273 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000274 }
275}
276
aliguori376253e2009-03-05 23:01:23 +0000277void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000278{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200279 char buf[4096];
280
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200281 if (!mon)
282 return;
283
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200284 mon_print_count_inc(mon);
285
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200286 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200287 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200288 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200289
290 vsnprintf(buf, sizeof(buf), fmt, ap);
291 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000292}
293
aliguori376253e2009-03-05 23:01:23 +0000294void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000295{
296 va_list ap;
297 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000298 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000299 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000300}
301
aliguori376253e2009-03-05 23:01:23 +0000302void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000303{
304 int i;
305
306 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000307 switch (filename[i]) {
308 case ' ':
309 case '"':
310 case '\\':
311 monitor_printf(mon, "\\%c", filename[i]);
312 break;
313 case '\t':
314 monitor_printf(mon, "\\t");
315 break;
316 case '\r':
317 monitor_printf(mon, "\\r");
318 break;
319 case '\n':
320 monitor_printf(mon, "\\n");
321 break;
322 default:
323 monitor_printf(mon, "%c", filename[i]);
324 break;
325 }
thsfef30742006-12-22 14:11:32 +0000326 }
327}
328
Stefan Weil8b7968f2010-09-23 21:28:05 +0200329static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
330 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000331{
332 va_list ap;
333 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000334 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000335 va_end(ap);
336 return 0;
337}
338
Luiz Capitulino13c74252009-10-07 13:41:55 -0300339static void monitor_user_noop(Monitor *mon, const QObject *data) { }
340
Luiz Capitulino9e807212010-09-16 10:58:59 -0300341static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300342{
343 return cmd->user_print != NULL;
344}
345
Luiz Capitulino4903de02010-09-16 11:01:32 -0300346static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600347{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200348 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600349}
350
Luiz Capitulino8204a912009-11-18 23:05:31 -0200351static inline int monitor_has_error(const Monitor *mon)
352{
353 return mon->error != NULL;
354}
355
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200356static void monitor_json_emitter(Monitor *mon, const QObject *data)
357{
358 QString *json;
359
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200360 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
361 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200362 assert(json != NULL);
363
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200364 qstring_append_chr(json, '\n');
365 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200366
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200367 QDECREF(json);
368}
369
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200370static void monitor_protocol_emitter(Monitor *mon, QObject *data)
371{
372 QDict *qmp;
373
374 qmp = qdict_new();
375
376 if (!monitor_has_error(mon)) {
377 /* success response */
378 if (data) {
379 qobject_incref(data);
380 qdict_put_obj(qmp, "return", data);
381 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200382 /* return an empty QDict by default */
383 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 }
385 } else {
386 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100387 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200388 qdict_put(qmp, "error", mon->error->error);
389 QINCREF(mon->error->error);
390 QDECREF(mon->error);
391 mon->error = NULL;
392 }
393
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200394 if (mon->mc->id) {
395 qdict_put_obj(qmp, "id", mon->mc->id);
396 mon->mc->id = NULL;
397 }
398
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200399 monitor_json_emitter(mon, QOBJECT(qmp));
400 QDECREF(qmp);
401}
402
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200403static void timestamp_put(QDict *qdict)
404{
405 int err;
406 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000407 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200408
Blue Swirld08d6f02009-12-04 18:06:39 +0000409 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410 if (err < 0)
411 return;
412
413 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
414 "'microseconds': %" PRId64 " }",
415 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200416 qdict_put_obj(qdict, "timestamp", obj);
417}
418
419/**
420 * monitor_protocol_event(): Generate a Monitor event
421 *
422 * Event-specific data can be emitted through the (optional) 'data' parameter.
423 */
424void monitor_protocol_event(MonitorEvent event, QObject *data)
425{
426 QDict *qmp;
427 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600428 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200429
Blue Swirl242cd002009-12-04 18:05:45 +0000430 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200431
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200432 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000433 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200434 event_name = "SHUTDOWN";
435 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000436 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200437 event_name = "RESET";
438 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000439 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200440 event_name = "POWERDOWN";
441 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000442 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200443 event_name = "STOP";
444 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300445 case QEVENT_RESUME:
446 event_name = "RESUME";
447 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200448 case QEVENT_VNC_CONNECTED:
449 event_name = "VNC_CONNECTED";
450 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200451 case QEVENT_VNC_INITIALIZED:
452 event_name = "VNC_INITIALIZED";
453 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200454 case QEVENT_VNC_DISCONNECTED:
455 event_name = "VNC_DISCONNECTED";
456 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200457 case QEVENT_BLOCK_IO_ERROR:
458 event_name = "BLOCK_IO_ERROR";
459 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300460 case QEVENT_RTC_CHANGE:
461 event_name = "RTC_CHANGE";
462 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300463 case QEVENT_WATCHDOG:
464 event_name = "WATCHDOG";
465 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200466 case QEVENT_SPICE_CONNECTED:
467 event_name = "SPICE_CONNECTED";
468 break;
469 case QEVENT_SPICE_INITIALIZED:
470 event_name = "SPICE_INITIALIZED";
471 break;
472 case QEVENT_SPICE_DISCONNECTED:
473 event_name = "SPICE_DISCONNECTED";
474 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200475 default:
476 abort();
477 break;
478 }
479
480 qmp = qdict_new();
481 timestamp_put(qmp);
482 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200483 if (data) {
484 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200485 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200486 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200487
Adam Litkef039a562010-01-15 08:34:02 -0600488 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200489 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200490 monitor_json_emitter(mon, QOBJECT(qmp));
491 }
Adam Litkef039a562010-01-15 08:34:02 -0600492 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200493 QDECREF(qmp);
494}
495
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200496static int do_qmp_capabilities(Monitor *mon, const QDict *params,
497 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200498{
499 /* Will setup QMP capabilities in the future */
500 if (monitor_ctrl_mode(mon)) {
501 mon->mc->command_mode = 1;
502 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200503
504 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200505}
506
Luiz Capitulino0268d972010-10-22 10:08:02 -0200507static int mon_set_cpu(int cpu_index);
508static void handle_user_command(Monitor *mon, const char *cmdline);
509
510static int do_hmp_passthrough(Monitor *mon, const QDict *params,
511 QObject **ret_data)
512{
513 int ret = 0;
514 Monitor *old_mon, hmp;
515 CharDriverState mchar;
516
517 memset(&hmp, 0, sizeof(hmp));
518 qemu_chr_init_mem(&mchar);
519 hmp.chr = &mchar;
520
521 old_mon = cur_mon;
522 cur_mon = &hmp;
523
524 if (qdict_haskey(params, "cpu-index")) {
525 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
526 if (ret < 0) {
527 cur_mon = old_mon;
528 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
529 goto out;
530 }
531 }
532
533 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
534 cur_mon = old_mon;
535
536 if (qemu_chr_mem_osize(hmp.chr) > 0) {
537 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
538 }
539
540out:
541 qemu_chr_close_mem(hmp.chr);
542 return ret;
543}
544
bellard9dc39cb2004-03-14 21:38:27 +0000545static int compare_cmd(const char *name, const char *list)
546{
547 const char *p, *pstart;
548 int len;
549 len = strlen(name);
550 p = list;
551 for(;;) {
552 pstart = p;
553 p = strchr(p, '|');
554 if (!p)
555 p = pstart + strlen(pstart);
556 if ((p - pstart) == len && !memcmp(pstart, name, len))
557 return 1;
558 if (*p == '\0')
559 break;
560 p++;
561 }
562 return 0;
563}
564
Anthony Liguoric227f092009-10-01 16:12:16 -0500565static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000566 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000567{
Anthony Liguoric227f092009-10-01 16:12:16 -0500568 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000569
570 for(cmd = cmds; cmd->name != NULL; cmd++) {
571 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000572 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
573 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000574 }
575}
576
aliguori376253e2009-03-05 23:01:23 +0000577static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000578{
579 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000580 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000581 } else {
aliguori376253e2009-03-05 23:01:23 +0000582 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000583 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000584 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000585 monitor_printf(mon, "Log items (comma separated):\n");
586 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000587 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000588 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000589 }
590 }
bellard9dc39cb2004-03-14 21:38:27 +0000591 }
592}
593
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300594static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300595{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300596 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300597}
598
Lluísfc764102011-08-31 20:31:18 +0200599static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530600{
601 const char *tp_name = qdict_get_str(qdict, "name");
602 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200603 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000604
605 if (!ret) {
606 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
607 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530608}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100609
Lluís31965ae2011-08-31 20:31:24 +0200610#ifdef CONFIG_SIMPLE_TRACE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100611static void do_trace_file(Monitor *mon, const QDict *qdict)
612{
613 const char *op = qdict_get_try_str(qdict, "op");
614 const char *arg = qdict_get_try_str(qdict, "arg");
615
616 if (!op) {
617 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
618 } else if (!strcmp(op, "on")) {
619 st_set_trace_file_enabled(true);
620 } else if (!strcmp(op, "off")) {
621 st_set_trace_file_enabled(false);
622 } else if (!strcmp(op, "flush")) {
623 st_flush_trace_buffer();
624 } else if (!strcmp(op, "set")) {
625 if (arg) {
626 st_set_trace_file(arg);
627 }
628 } else {
629 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
630 help_cmd(mon, "trace-file");
631 }
632}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530633#endif
634
Adam Litke940cc302010-01-25 12:18:44 -0600635static void user_monitor_complete(void *opaque, QObject *ret_data)
636{
637 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
638
639 if (ret_data) {
640 data->user_print(data->mon, ret_data);
641 }
642 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500643 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600644}
645
646static void qmp_monitor_complete(void *opaque, QObject *ret_data)
647{
648 monitor_protocol_emitter(opaque, ret_data);
649}
650
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300651static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
652 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600653{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300654 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600655}
656
657static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
658{
659 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
660}
661
662static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
663 const QDict *params)
664{
665 int ret;
666
Anthony Liguori7267c092011-08-20 22:09:37 -0500667 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600668 cb_data->mon = mon;
669 cb_data->user_print = cmd->user_print;
670 monitor_suspend(mon);
671 ret = cmd->mhandler.cmd_async(mon, params,
672 user_monitor_complete, cb_data);
673 if (ret < 0) {
674 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500675 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600676 }
677}
678
679static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
680{
681 int ret;
682
Anthony Liguori7267c092011-08-20 22:09:37 -0500683 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600684 cb_data->mon = mon;
685 cb_data->user_print = cmd->user_print;
686 monitor_suspend(mon);
687 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
688 if (ret < 0) {
689 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500690 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600691 }
692}
693
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300694static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000695{
Anthony Liguoric227f092009-10-01 16:12:16 -0500696 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300697 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000698
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200699 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000700 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200701 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300702
703 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000704 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300705 break;
bellard9dc39cb2004-03-14 21:38:27 +0000706 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300707
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200708 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300709 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200710 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300711
Luiz Capitulino4903de02010-09-16 11:01:32 -0300712 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300713 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300714 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300715 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200716
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300717 cmd->mhandler.info_new(mon, &info_data);
718 if (info_data) {
719 cmd->user_print(mon, info_data);
720 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200721 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300722 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300723 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300724 }
725
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300726 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300727
728help:
729 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000730}
731
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200732static void do_info_version_print(Monitor *mon, const QObject *data)
733{
734 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300735 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200736
737 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300738 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200739
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300740 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
741 qdict_get_int(qemu, "major"),
742 qdict_get_int(qemu, "minor"),
743 qdict_get_int(qemu, "micro"),
744 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200745}
746
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300747static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000748{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300749 const char *version = QEMU_VERSION;
750 int major = 0, minor = 0, micro = 0;
751 char *tmp;
752
753 major = strtol(version, &tmp, 10);
754 tmp++;
755 minor = strtol(tmp, &tmp, 10);
756 tmp++;
757 micro = strtol(tmp, &tmp, 10);
758
759 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
760 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000761}
762
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200763static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000764{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200765 QDict *qdict;
766
767 qdict = qobject_to_qdict(data);
768 if (qdict_size(qdict) == 0) {
769 return;
770 }
771
772 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
773}
774
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200775static void do_info_name(Monitor *mon, QObject **ret_data)
776{
777 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
778 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000779}
780
Luiz Capitulino1a728672009-12-10 17:15:56 -0200781static QObject *get_cmd_dict(const char *name)
782{
783 const char *p;
784
785 /* Remove '|' from some commands */
786 p = strchr(name, '|');
787 if (p) {
788 p++;
789 } else {
790 p = name;
791 }
792
793 return qobject_from_jsonf("{ 'name': %s }", p);
794}
795
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200796static void do_info_commands(Monitor *mon, QObject **ret_data)
797{
798 QList *cmd_list;
799 const mon_cmd_t *cmd;
800
801 cmd_list = qlist_new();
802
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300803 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300804 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200805 }
806
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300807 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300808 char buf[128];
809 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
810 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200811 }
812
813 *ret_data = QOBJECT(cmd_list);
814}
815
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200816static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000817{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200818 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
819}
820
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200821static void do_info_uuid(Monitor *mon, QObject **ret_data)
822{
823 char uuid[64];
824
825 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000826 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
827 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
828 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
829 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200830 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000831}
832
bellard6a00d602005-11-21 23:25:50 +0000833/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000834static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000835{
836 CPUState *env;
837
838 for(env = first_cpu; env != NULL; env = env->next_cpu) {
839 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000840 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000841 return 0;
842 }
843 }
844 return -1;
845}
846
pbrook9596ebb2007-11-18 01:44:38 +0000847static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000848{
aliguori731b0362009-03-05 23:01:42 +0000849 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000850 mon_set_cpu(0);
851 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300852 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000853 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000854}
855
aliguori376253e2009-03-05 23:01:23 +0000856static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000857{
bellard6a00d602005-11-21 23:25:50 +0000858 CPUState *env;
859 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000860#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000861 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000862 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000863#else
aliguori376253e2009-03-05 23:01:23 +0000864 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000865 0);
bellard9307c4c2004-04-04 12:57:25 +0000866#endif
867}
868
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300869static void print_cpu_iter(QObject *obj, void *opaque)
870{
871 QDict *cpu;
872 int active = ' ';
873 Monitor *mon = opaque;
874
875 assert(qobject_type(obj) == QTYPE_QDICT);
876 cpu = qobject_to_qdict(obj);
877
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200878 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200880 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881
882 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
883
884#if defined(TARGET_I386)
885 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
886 (target_ulong) qdict_get_int(cpu, "pc"));
887#elif defined(TARGET_PPC)
888 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
889 (target_long) qdict_get_int(cpu, "nip"));
890#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500891 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300892 (target_long) qdict_get_int(cpu, "pc"));
893 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
894 (target_long) qdict_get_int(cpu, "npc"));
895#elif defined(TARGET_MIPS)
896 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
897 (target_long) qdict_get_int(cpu, "PC"));
898#endif
899
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200900 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300901 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200902 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300903
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100904 monitor_printf(mon, " thread_id=%" PRId64 " ",
905 qdict_get_int(cpu, "thread_id"));
906
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300907 monitor_printf(mon, "\n");
908}
909
910static void monitor_print_cpus(Monitor *mon, const QObject *data)
911{
912 QList *cpu_list;
913
914 assert(qobject_type(data) == QTYPE_QLIST);
915 cpu_list = qobject_to_qlist(data);
916 qlist_iter(cpu_list, print_cpu_iter, mon);
917}
918
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300919static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000920{
921 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300922 QList *cpu_list;
923
924 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000925
926 /* just to set the default cpu if not already done */
927 mon_get_cpu();
928
929 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200930 QDict *cpu;
931 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300932
Avi Kivity4c0960c2009-08-17 23:19:53 +0300933 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300934
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200935 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
936 env->cpu_index, env == mon->mon_cpu,
937 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200938
939 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300940
bellard6a00d602005-11-21 23:25:50 +0000941#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300942 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000943#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300944 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000945#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300946 qdict_put(cpu, "pc", qint_from_int(env->pc));
947 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000948#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300949 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000950#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100951 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300952
953 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000954 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300955
956 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000957}
958
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200959static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000960{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300961 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200962 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100963 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
964 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200965 return -1;
966 }
967 return 0;
bellard6a00d602005-11-21 23:25:50 +0000968}
969
aliguori376253e2009-03-05 23:01:23 +0000970static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000971{
aliguori376253e2009-03-05 23:01:23 +0000972 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000973}
974
aliguori376253e2009-03-05 23:01:23 +0000975static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000976{
977 int i;
bellard7e2515e2004-08-01 21:52:19 +0000978 const char *str;
ths3b46e622007-09-17 08:09:54 +0000979
aliguoricde76ee2009-03-05 23:01:51 +0000980 if (!mon->rs)
981 return;
bellard7e2515e2004-08-01 21:52:19 +0000982 i = 0;
983 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000984 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000985 if (!str)
986 break;
aliguori376253e2009-03-05 23:01:23 +0000987 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000988 i++;
bellardaa455482004-04-04 13:07:25 +0000989 }
990}
991
j_mayer76a66252007-03-07 08:32:30 +0000992#if defined(TARGET_PPC)
993/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000994static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000995{
996 CPUState *env;
997
998 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000999 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001000}
1001#endif
1002
Lluís6d8a7642011-08-31 20:30:43 +02001003#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301004static void do_info_trace(Monitor *mon)
1005{
1006 st_print_trace((FILE *)mon, &monitor_fprintf);
1007}
Lluís31965ae2011-08-31 20:31:24 +02001008#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05301009
Lluísfc764102011-08-31 20:31:18 +02001010static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301011{
Lluísfc764102011-08-31 20:31:18 +02001012 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301013}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301014
Luiz Capitulinob223f352009-10-07 13:41:56 -03001015/**
1016 * do_quit(): Quit QEMU execution
1017 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001018static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001019{
Luiz Capitulino39b59d22010-05-11 18:08:20 -03001020 monitor_suspend(mon);
1021 no_shutdown = 0;
1022 qemu_system_shutdown_request();
1023
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001024 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001025}
1026
Jes Sorensen821601e2011-03-16 13:33:36 +01001027#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001028static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001029{
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001030 if (!password || !password[0]) {
1031 if (vnc_display_disable_login(NULL)) {
1032 qerror_report(QERR_SET_PASSWD_FAILED);
1033 return -1;
1034 }
1035 return 0;
1036 }
1037
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001038 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001039 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001040 return -1;
1041 }
aliguoribb5fc202009-03-05 23:01:15 +00001042
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001043 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001044}
1045
1046static void change_vnc_password_cb(Monitor *mon, const char *password,
1047 void *opaque)
1048{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001049 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001050 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001051}
1052
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001053static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001054{
ths70848512007-08-25 01:37:05 +00001055 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001056 strcmp(target, "password") == 0) {
1057 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001058 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001059 strncpy(password, arg, sizeof(password));
1060 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001061 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001062 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001063 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001064 }
ths70848512007-08-25 01:37:05 +00001065 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001066 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001067 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001068 return -1;
1069 }
ths70848512007-08-25 01:37:05 +00001070 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001071
1072 return 0;
thse25a5822007-08-25 01:36:20 +00001073}
Jes Sorensen821601e2011-03-16 13:33:36 +01001074#else
1075static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1076{
1077 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1078 return -ENODEV;
1079}
1080#endif
thse25a5822007-08-25 01:36:20 +00001081
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001082/**
1083 * do_change(): Change a removable medium, or VNC configuration
1084 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001085static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001086{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001087 const char *device = qdict_get_str(qdict, "device");
1088 const char *target = qdict_get_str(qdict, "target");
1089 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001090 int ret;
1091
thse25a5822007-08-25 01:36:20 +00001092 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001093 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001094 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001095 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001096 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001097
1098 return ret;
thse25a5822007-08-25 01:36:20 +00001099}
1100
Gerd Hoffmann75721502010-10-07 12:22:54 +02001101static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1102{
1103 const char *protocol = qdict_get_str(qdict, "protocol");
1104 const char *password = qdict_get_str(qdict, "password");
1105 const char *connected = qdict_get_try_str(qdict, "connected");
1106 int disconnect_if_connected = 0;
1107 int fail_if_connected = 0;
1108 int rc;
1109
1110 if (connected) {
1111 if (strcmp(connected, "fail") == 0) {
1112 fail_if_connected = 1;
1113 } else if (strcmp(connected, "disconnect") == 0) {
1114 disconnect_if_connected = 1;
1115 } else if (strcmp(connected, "keep") == 0) {
1116 /* nothing */
1117 } else {
1118 qerror_report(QERR_INVALID_PARAMETER, "connected");
1119 return -1;
1120 }
1121 }
1122
1123 if (strcmp(protocol, "spice") == 0) {
1124 if (!using_spice) {
1125 /* correct one? spice isn't a device ,,, */
1126 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1127 return -1;
1128 }
1129 rc = qemu_spice_set_passwd(password, fail_if_connected,
1130 disconnect_if_connected);
1131 if (rc != 0) {
1132 qerror_report(QERR_SET_PASSWD_FAILED);
1133 return -1;
1134 }
1135 return 0;
1136 }
1137
1138 if (strcmp(protocol, "vnc") == 0) {
1139 if (fail_if_connected || disconnect_if_connected) {
1140 /* vnc supports "connected=keep" only */
1141 qerror_report(QERR_INVALID_PARAMETER, "connected");
1142 return -1;
1143 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001144 /* Note that setting an empty password will not disable login through
1145 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001146 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001147 }
1148
1149 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1150 return -1;
1151}
1152
1153static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1154{
1155 const char *protocol = qdict_get_str(qdict, "protocol");
1156 const char *whenstr = qdict_get_str(qdict, "time");
1157 time_t when;
1158 int rc;
1159
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001160 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001161 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001162 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001163 when = TIME_MAX;
1164 } else if (whenstr[0] == '+') {
1165 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1166 } else {
1167 when = strtoull(whenstr, NULL, 10);
1168 }
1169
1170 if (strcmp(protocol, "spice") == 0) {
1171 if (!using_spice) {
1172 /* correct one? spice isn't a device ,,, */
1173 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1174 return -1;
1175 }
1176 rc = qemu_spice_set_pw_expire(when);
1177 if (rc != 0) {
1178 qerror_report(QERR_SET_PASSWD_FAILED);
1179 return -1;
1180 }
1181 return 0;
1182 }
1183
1184 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001185 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001186 }
1187
1188 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1189 return -1;
1190}
1191
Daniel P. Berrange13661082011-06-23 13:31:42 +01001192static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1193{
1194 const char *protocol = qdict_get_str(qdict, "protocol");
1195 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001196 CharDriverState *s;
1197
1198 if (strcmp(protocol, "spice") == 0) {
1199 if (!using_spice) {
1200 /* correct one? spice isn't a device ,,, */
1201 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1202 return -1;
1203 }
1204 qerror_report(QERR_ADD_CLIENT_FAILED);
1205 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001206#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001207 } else if (strcmp(protocol, "vnc") == 0) {
1208 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001209 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001210 vnc_display_add_client(NULL, fd, skipauth);
1211 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001212#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001213 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1214 int fd = monitor_get_fd(mon, fdname);
1215 if (qemu_chr_add_client(s, fd) < 0) {
1216 qerror_report(QERR_ADD_CLIENT_FAILED);
1217 return -1;
1218 }
1219 return 0;
1220 }
1221
1222 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1223 return -1;
1224}
1225
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001226static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1227{
1228 const char *protocol = qdict_get_str(qdict, "protocol");
1229 const char *hostname = qdict_get_str(qdict, "hostname");
1230 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1231 int port = qdict_get_try_int(qdict, "port", -1);
1232 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1233 int ret;
1234
1235 if (strcmp(protocol, "spice") == 0) {
1236 if (!using_spice) {
1237 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1238 return -1;
1239 }
1240
1241 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1242 if (ret != 0) {
1243 qerror_report(QERR_UNDEFINED_ERROR);
1244 return -1;
1245 }
1246 return 0;
1247 }
1248
1249 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1250 return -1;
1251}
1252
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001253static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001254{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001255 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001256 return 0;
bellard59a983b2004-03-17 23:17:16 +00001257}
1258
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001259static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001260{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001261 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001262}
1263
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001264static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001265{
1266 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001267 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001268
bellard9307c4c2004-04-04 12:57:25 +00001269 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001270 mask = 0;
1271 } else {
bellard9307c4c2004-04-04 12:57:25 +00001272 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001273 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001274 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001275 return;
1276 }
1277 }
1278 cpu_set_log(mask);
1279}
1280
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001281static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001282{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001283 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001284 if (!option || !strcmp(option, "on")) {
1285 singlestep = 1;
1286 } else if (!strcmp(option, "off")) {
1287 singlestep = 0;
1288 } else {
1289 monitor_printf(mon, "unexpected option %s\n", option);
1290 }
1291}
1292
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001293/**
1294 * do_stop(): Stop VM execution
1295 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001296static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001297{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001298 vm_stop(RSTATE_PAUSED);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001299 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001300}
1301
aliguoribb5fc202009-03-05 23:01:15 +00001302static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001303
aliguori376253e2009-03-05 23:01:23 +00001304struct bdrv_iterate_context {
1305 Monitor *mon;
1306 int err;
1307};
aliguoric0f4ce72009-03-05 23:01:01 +00001308
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001309/**
1310 * do_cont(): Resume emulation.
1311 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001312static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001313{
1314 struct bdrv_iterate_context context = { mon, 0 };
1315
Luiz Capitulino1bcef682011-07-29 15:15:00 -03001316 if (runstate_check(RSTATE_IN_MIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301317 qerror_report(QERR_MIGRATION_EXPECTED);
1318 return -1;
Luiz Capitulino6667b232011-07-29 15:57:54 -03001319 } else if (runstate_check(RSTATE_PANICKED) ||
1320 runstate_check(RSTATE_SHUTDOWN)) {
1321 qerror_report(QERR_RESET_REQUIRED);
1322 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301323 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001324
aliguori376253e2009-03-05 23:01:23 +00001325 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001326 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001327 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001328 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001329 return 0;
1330 } else {
1331 return -1;
1332 }
bellard8a7ddc32004-03-31 19:00:16 +00001333}
1334
aliguoribb5fc202009-03-05 23:01:15 +00001335static void bdrv_key_cb(void *opaque, int err)
1336{
aliguori376253e2009-03-05 23:01:23 +00001337 Monitor *mon = opaque;
1338
aliguoribb5fc202009-03-05 23:01:15 +00001339 /* another key was set successfully, retry to continue */
1340 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001341 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001342}
1343
1344static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1345{
aliguori376253e2009-03-05 23:01:23 +00001346 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001347
aliguori376253e2009-03-05 23:01:23 +00001348 if (!context->err && bdrv_key_required(bs)) {
1349 context->err = -EBUSY;
1350 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1351 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001352 }
1353}
1354
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001355static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001356{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001357 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001358 if (!device)
1359 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1360 if (gdbserver_start(device) < 0) {
1361 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1362 device);
1363 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001364 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001365 } else {
aliguori59030a82009-04-05 18:43:41 +00001366 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1367 device);
bellard8a7ddc32004-03-31 19:00:16 +00001368 }
1369}
1370
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001371static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001372{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001373 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001374 if (select_watchdog_action(action) == -1) {
1375 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1376 }
1377}
1378
aliguori376253e2009-03-05 23:01:23 +00001379static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001380{
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001382 switch(c) {
1383 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001385 break;
1386 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001391 break;
1392 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001393 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001394 break;
1395 default:
1396 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001398 } else {
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001400 }
1401 break;
1402 }
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001404}
1405
aliguori376253e2009-03-05 23:01:23 +00001406static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001407 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001408{
bellard6a00d602005-11-21 23:25:50 +00001409 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001410 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001411 uint8_t buf[16];
1412 uint64_t v;
1413
1414 if (format == 'i') {
1415 int flags;
1416 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001417 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001418#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001419 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001420 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001421 } else if (wsize == 4) {
1422 flags = 0;
1423 } else {
bellard6a15fd12006-04-12 21:07:07 +00001424 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001425 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001426 if (env) {
1427#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001428 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001429 (env->segs[R_CS].flags & DESC_L_MASK))
1430 flags = 2;
1431 else
1432#endif
1433 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1434 flags = 1;
1435 }
bellard4c27ba22004-04-25 18:05:08 +00001436 }
1437#endif
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001439 return;
1440 }
1441
1442 len = wsize * count;
1443 if (wsize == 1)
1444 line_size = 8;
1445 else
1446 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001447 max_digits = 0;
1448
1449 switch(format) {
1450 case 'o':
1451 max_digits = (wsize * 8 + 2) / 3;
1452 break;
1453 default:
1454 case 'x':
1455 max_digits = (wsize * 8) / 4;
1456 break;
1457 case 'u':
1458 case 'd':
1459 max_digits = (wsize * 8 * 10 + 32) / 33;
1460 break;
1461 case 'c':
1462 wsize = 1;
1463 break;
1464 }
1465
1466 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001467 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001468 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001469 else
aliguori376253e2009-03-05 23:01:23 +00001470 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001471 l = len;
1472 if (l > line_size)
1473 l = line_size;
1474 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001475 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001476 } else {
bellard6a00d602005-11-21 23:25:50 +00001477 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001478 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001479 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001480 break;
1481 }
bellard9307c4c2004-04-04 12:57:25 +00001482 }
ths5fafdf22007-09-16 21:08:06 +00001483 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001484 while (i < l) {
1485 switch(wsize) {
1486 default:
1487 case 1:
1488 v = ldub_raw(buf + i);
1489 break;
1490 case 2:
1491 v = lduw_raw(buf + i);
1492 break;
1493 case 4:
bellard92a31b12005-02-10 22:00:52 +00001494 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001495 break;
1496 case 8:
1497 v = ldq_raw(buf + i);
1498 break;
1499 }
aliguori376253e2009-03-05 23:01:23 +00001500 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001501 switch(format) {
1502 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001503 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001504 break;
1505 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001506 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001507 break;
1508 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001510 break;
1511 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001513 break;
1514 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001515 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001516 break;
1517 }
1518 i += wsize;
1519 }
aliguori376253e2009-03-05 23:01:23 +00001520 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001521 addr += l;
1522 len -= l;
1523 }
1524}
1525
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001526static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001527{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001528 int count = qdict_get_int(qdict, "count");
1529 int format = qdict_get_int(qdict, "format");
1530 int size = qdict_get_int(qdict, "size");
1531 target_long addr = qdict_get_int(qdict, "addr");
1532
aliguori376253e2009-03-05 23:01:23 +00001533 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001534}
1535
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001536static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001537{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001538 int count = qdict_get_int(qdict, "count");
1539 int format = qdict_get_int(qdict, "format");
1540 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001541 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001542
aliguori376253e2009-03-05 23:01:23 +00001543 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001544}
1545
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001546static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001547{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001548 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001549 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001550
blueswir17743e582007-09-24 18:39:04 +00001551#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001552 switch(format) {
1553 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001554 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001555 break;
1556 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001557 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001558 break;
1559 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001560 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001561 break;
1562 default:
1563 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001564 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001565 break;
1566 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001567 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001568 break;
1569 }
bellard92a31b12005-02-10 22:00:52 +00001570#else
1571 switch(format) {
1572 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001573 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001574 break;
1575 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001577 break;
1578 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001579 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001580 break;
1581 default:
1582 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001584 break;
1585 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001586 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001587 break;
1588 }
1589#endif
aliguori376253e2009-03-05 23:01:23 +00001590 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001591}
1592
Luiz Capitulino98696222010-02-10 23:49:58 -02001593static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001594{
1595 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001596 uint32_t size = qdict_get_int(qdict, "size");
1597 const char *filename = qdict_get_str(qdict, "filename");
1598 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001599 uint32_t l;
1600 CPUState *env;
1601 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001602 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001603
1604 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001605
1606 f = fopen(filename, "wb");
1607 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001608 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001609 return -1;
bellardb371dc52007-01-03 15:20:39 +00001610 }
1611 while (size != 0) {
1612 l = sizeof(buf);
1613 if (l > size)
1614 l = size;
1615 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001616 if (fwrite(buf, 1, l, f) != l) {
1617 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1618 goto exit;
1619 }
bellardb371dc52007-01-03 15:20:39 +00001620 addr += l;
1621 size -= l;
1622 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001623
1624 ret = 0;
1625
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001626exit:
bellardb371dc52007-01-03 15:20:39 +00001627 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001628 return ret;
bellardb371dc52007-01-03 15:20:39 +00001629}
1630
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001631static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001632 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001633{
1634 FILE *f;
1635 uint32_t l;
1636 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001637 uint32_t size = qdict_get_int(qdict, "size");
1638 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001639 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001640 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001641
1642 f = fopen(filename, "wb");
1643 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001644 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001645 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001646 }
1647 while (size != 0) {
1648 l = sizeof(buf);
1649 if (l > size)
1650 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001651 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001652 if (fwrite(buf, 1, l, f) != l) {
1653 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1654 goto exit;
1655 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001656 fflush(f);
1657 addr += l;
1658 size -= l;
1659 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001660
1661 ret = 0;
1662
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001663exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001664 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001665 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001666}
1667
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001668static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001669{
1670 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001671 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001672 uint32_t start = qdict_get_int(qdict, "start");
1673 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001674
1675 sum = 0;
1676 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001677 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001678 /* BSD sum algorithm ('sum' Unix command) */
1679 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001680 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001681 }
aliguori376253e2009-03-05 23:01:23 +00001682 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001683}
1684
bellarda3a91a32004-06-04 11:06:21 +00001685typedef struct {
1686 int keycode;
1687 const char *name;
1688} KeyDef;
1689
1690static const KeyDef key_defs[] = {
1691 { 0x2a, "shift" },
1692 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001693
bellarda3a91a32004-06-04 11:06:21 +00001694 { 0x38, "alt" },
1695 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001696 { 0x64, "altgr" },
1697 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001698 { 0x1d, "ctrl" },
1699 { 0x9d, "ctrl_r" },
1700
1701 { 0xdd, "menu" },
1702
1703 { 0x01, "esc" },
1704
1705 { 0x02, "1" },
1706 { 0x03, "2" },
1707 { 0x04, "3" },
1708 { 0x05, "4" },
1709 { 0x06, "5" },
1710 { 0x07, "6" },
1711 { 0x08, "7" },
1712 { 0x09, "8" },
1713 { 0x0a, "9" },
1714 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001715 { 0x0c, "minus" },
1716 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001717 { 0x0e, "backspace" },
1718
1719 { 0x0f, "tab" },
1720 { 0x10, "q" },
1721 { 0x11, "w" },
1722 { 0x12, "e" },
1723 { 0x13, "r" },
1724 { 0x14, "t" },
1725 { 0x15, "y" },
1726 { 0x16, "u" },
1727 { 0x17, "i" },
1728 { 0x18, "o" },
1729 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001730 { 0x1a, "bracket_left" },
1731 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001732 { 0x1c, "ret" },
1733
1734 { 0x1e, "a" },
1735 { 0x1f, "s" },
1736 { 0x20, "d" },
1737 { 0x21, "f" },
1738 { 0x22, "g" },
1739 { 0x23, "h" },
1740 { 0x24, "j" },
1741 { 0x25, "k" },
1742 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001743 { 0x27, "semicolon" },
1744 { 0x28, "apostrophe" },
1745 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001746
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001747 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001748 { 0x2c, "z" },
1749 { 0x2d, "x" },
1750 { 0x2e, "c" },
1751 { 0x2f, "v" },
1752 { 0x30, "b" },
1753 { 0x31, "n" },
1754 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001755 { 0x33, "comma" },
1756 { 0x34, "dot" },
1757 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001758
balrog4d3b6f62008-02-10 16:33:14 +00001759 { 0x37, "asterisk" },
1760
bellarda3a91a32004-06-04 11:06:21 +00001761 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001762 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001763 { 0x3b, "f1" },
1764 { 0x3c, "f2" },
1765 { 0x3d, "f3" },
1766 { 0x3e, "f4" },
1767 { 0x3f, "f5" },
1768 { 0x40, "f6" },
1769 { 0x41, "f7" },
1770 { 0x42, "f8" },
1771 { 0x43, "f9" },
1772 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001773 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001774 { 0x46, "scroll_lock" },
1775
bellard64866c32006-05-07 18:03:31 +00001776 { 0xb5, "kp_divide" },
1777 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001778 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001779 { 0x4e, "kp_add" },
1780 { 0x9c, "kp_enter" },
1781 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001782 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001783
1784 { 0x52, "kp_0" },
1785 { 0x4f, "kp_1" },
1786 { 0x50, "kp_2" },
1787 { 0x51, "kp_3" },
1788 { 0x4b, "kp_4" },
1789 { 0x4c, "kp_5" },
1790 { 0x4d, "kp_6" },
1791 { 0x47, "kp_7" },
1792 { 0x48, "kp_8" },
1793 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001794
bellarda3a91a32004-06-04 11:06:21 +00001795 { 0x56, "<" },
1796
1797 { 0x57, "f11" },
1798 { 0x58, "f12" },
1799
1800 { 0xb7, "print" },
1801
1802 { 0xc7, "home" },
1803 { 0xc9, "pgup" },
1804 { 0xd1, "pgdn" },
1805 { 0xcf, "end" },
1806
1807 { 0xcb, "left" },
1808 { 0xc8, "up" },
1809 { 0xd0, "down" },
1810 { 0xcd, "right" },
1811
1812 { 0xd2, "insert" },
1813 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001814#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1815 { 0xf0, "stop" },
1816 { 0xf1, "again" },
1817 { 0xf2, "props" },
1818 { 0xf3, "undo" },
1819 { 0xf4, "front" },
1820 { 0xf5, "copy" },
1821 { 0xf6, "open" },
1822 { 0xf7, "paste" },
1823 { 0xf8, "find" },
1824 { 0xf9, "cut" },
1825 { 0xfa, "lf" },
1826 { 0xfb, "help" },
1827 { 0xfc, "meta_l" },
1828 { 0xfd, "meta_r" },
1829 { 0xfe, "compose" },
1830#endif
bellarda3a91a32004-06-04 11:06:21 +00001831 { 0, NULL },
1832};
1833
1834static int get_keycode(const char *key)
1835{
1836 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001837 char *endp;
1838 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001839
1840 for(p = key_defs; p->name != NULL; p++) {
1841 if (!strcmp(key, p->name))
1842 return p->keycode;
1843 }
bellard64866c32006-05-07 18:03:31 +00001844 if (strstart(key, "0x", NULL)) {
1845 ret = strtoul(key, &endp, 0);
1846 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1847 return ret;
1848 }
bellarda3a91a32004-06-04 11:06:21 +00001849 return -1;
1850}
1851
balrogc8256f92008-06-08 22:45:01 +00001852#define MAX_KEYCODES 16
1853static uint8_t keycodes[MAX_KEYCODES];
1854static int nb_pending_keycodes;
1855static QEMUTimer *key_timer;
1856
1857static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001858{
balrogc8256f92008-06-08 22:45:01 +00001859 int keycode;
1860
1861 while (nb_pending_keycodes > 0) {
1862 nb_pending_keycodes--;
1863 keycode = keycodes[nb_pending_keycodes];
1864 if (keycode & 0x80)
1865 kbd_put_keycode(0xe0);
1866 kbd_put_keycode(keycode | 0x80);
1867 }
1868}
1869
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001870static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001871{
balrog3401c0d2008-06-04 10:05:59 +00001872 char keyname_buf[16];
1873 char *separator;
1874 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001875 const char *string = qdict_get_str(qdict, "string");
1876 int has_hold_time = qdict_haskey(qdict, "hold_time");
1877 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001878
balrogc8256f92008-06-08 22:45:01 +00001879 if (nb_pending_keycodes > 0) {
1880 qemu_del_timer(key_timer);
1881 release_keys(NULL);
1882 }
1883 if (!has_hold_time)
1884 hold_time = 100;
1885 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001886 while (1) {
1887 separator = strchr(string, '-');
1888 keyname_len = separator ? separator - string : strlen(string);
1889 if (keyname_len > 0) {
1890 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1891 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001892 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001893 return;
bellarda3a91a32004-06-04 11:06:21 +00001894 }
balrogc8256f92008-06-08 22:45:01 +00001895 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001896 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001897 return;
1898 }
1899 keyname_buf[keyname_len] = 0;
1900 keycode = get_keycode(keyname_buf);
1901 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001902 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001903 return;
1904 }
balrogc8256f92008-06-08 22:45:01 +00001905 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001906 }
balrog3401c0d2008-06-04 10:05:59 +00001907 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001908 break;
balrog3401c0d2008-06-04 10:05:59 +00001909 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001910 }
balrogc8256f92008-06-08 22:45:01 +00001911 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001912 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001913 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001914 keycode = keycodes[i];
1915 if (keycode & 0x80)
1916 kbd_put_keycode(0xe0);
1917 kbd_put_keycode(keycode & 0x7f);
1918 }
balrogc8256f92008-06-08 22:45:01 +00001919 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001920 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001921 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001922}
1923
bellard13224a82006-07-14 22:03:35 +00001924static int mouse_button_state;
1925
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001926static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001927{
1928 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001929 const char *dx_str = qdict_get_str(qdict, "dx_str");
1930 const char *dy_str = qdict_get_str(qdict, "dy_str");
1931 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001932 dx = strtol(dx_str, NULL, 0);
1933 dy = strtol(dy_str, NULL, 0);
1934 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001935 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001936 dz = strtol(dz_str, NULL, 0);
1937 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1938}
1939
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001940static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001941{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001942 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001943 mouse_button_state = button_state;
1944 kbd_mouse_event(0, 0, 0, mouse_button_state);
1945}
1946
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001947static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001948{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001949 int size = qdict_get_int(qdict, "size");
1950 int addr = qdict_get_int(qdict, "addr");
1951 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001952 uint32_t val;
1953 int suffix;
1954
1955 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001956 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001957 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001958 addr++;
1959 }
1960 addr &= 0xffff;
1961
1962 switch(size) {
1963 default:
1964 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001965 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001966 suffix = 'b';
1967 break;
1968 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001969 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001970 suffix = 'w';
1971 break;
1972 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001973 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001974 suffix = 'l';
1975 break;
1976 }
aliguori376253e2009-03-05 23:01:23 +00001977 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1978 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001979}
bellarda3a91a32004-06-04 11:06:21 +00001980
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001981static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001982{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001983 int size = qdict_get_int(qdict, "size");
1984 int addr = qdict_get_int(qdict, "addr");
1985 int val = qdict_get_int(qdict, "val");
1986
Jan Kiszkaf1147842009-07-14 10:20:11 +02001987 addr &= IOPORTS_MASK;
1988
1989 switch (size) {
1990 default:
1991 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001992 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001993 break;
1994 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001995 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001996 break;
1997 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001998 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001999 break;
2000 }
2001}
2002
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002003static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00002004{
2005 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002006 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00002007
Jan Kiszka76e30d02009-07-02 00:19:02 +02002008 res = qemu_boot_set(bootdevice);
2009 if (res == 0) {
2010 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
2011 } else if (res > 0) {
2012 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00002013 } else {
aliguori376253e2009-03-05 23:01:23 +00002014 monitor_printf(mon, "no function defined to set boot device list for "
2015 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00002016 }
2017}
2018
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03002019/**
2020 * do_system_reset(): Issue a machine reset
2021 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002022static int do_system_reset(Monitor *mon, const QDict *qdict,
2023 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00002024{
2025 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002026 return 0;
bellarde4f90822004-06-20 12:35:44 +00002027}
2028
Luiz Capitulino43076662009-10-07 13:41:59 -03002029/**
2030 * do_system_powerdown(): Issue a machine powerdown
2031 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002032static int do_system_powerdown(Monitor *mon, const QDict *qdict,
2033 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00002034{
2035 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002036 return 0;
bellard34751872005-07-02 14:31:34 +00002037}
2038
bellardb86bda52004-09-18 19:32:46 +00002039#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00002040static void print_pte(Monitor *mon, target_phys_addr_t addr,
2041 target_phys_addr_t pte,
2042 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00002043{
Blue Swirld65aaf32010-12-11 18:56:24 +00002044#ifdef TARGET_X86_64
2045 if (addr & (1ULL << 47)) {
2046 addr |= -1LL << 48;
2047 }
2048#endif
2049 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
2050 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002051 addr,
2052 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00002053 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00002054 pte & PG_GLOBAL_MASK ? 'G' : '-',
2055 pte & PG_PSE_MASK ? 'P' : '-',
2056 pte & PG_DIRTY_MASK ? 'D' : '-',
2057 pte & PG_ACCESSED_MASK ? 'A' : '-',
2058 pte & PG_PCD_MASK ? 'C' : '-',
2059 pte & PG_PWT_MASK ? 'T' : '-',
2060 pte & PG_USER_MASK ? 'U' : '-',
2061 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00002062}
2063
Blue Swirld65aaf32010-12-11 18:56:24 +00002064static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002065{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002066 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00002067 uint32_t pgd, pde, pte;
2068
bellardb86bda52004-09-18 19:32:46 +00002069 pgd = env->cr[3] & ~0xfff;
2070 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002071 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002072 pde = le32_to_cpu(pde);
2073 if (pde & PG_PRESENT_MASK) {
2074 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00002075 /* 4M pages */
2076 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00002077 } else {
2078 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002079 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002080 pte = le32_to_cpu(pte);
2081 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002082 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002083 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002084 ~0xfff);
2085 }
2086 }
2087 }
2088 }
2089 }
2090}
2091
Blue Swirld65aaf32010-12-11 18:56:24 +00002092static void tlb_info_pae32(Monitor *mon, CPUState *env)
2093{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002094 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002095 uint64_t pdpe, pde, pte;
2096 uint64_t pdp_addr, pd_addr, pt_addr;
2097
2098 pdp_addr = env->cr[3] & ~0x1f;
2099 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002100 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002101 pdpe = le64_to_cpu(pdpe);
2102 if (pdpe & PG_PRESENT_MASK) {
2103 pd_addr = pdpe & 0x3fffffffff000ULL;
2104 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002105 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002106 pde = le64_to_cpu(pde);
2107 if (pde & PG_PRESENT_MASK) {
2108 if (pde & PG_PSE_MASK) {
2109 /* 2M pages with PAE, CR4.PSE is ignored */
2110 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2111 ~((target_phys_addr_t)(1 << 20) - 1));
2112 } else {
2113 pt_addr = pde & 0x3fffffffff000ULL;
2114 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002115 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002116 pte = le64_to_cpu(pte);
2117 if (pte & PG_PRESENT_MASK) {
2118 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2119 + (l3 << 12),
2120 pte & ~PG_PSE_MASK,
2121 ~(target_phys_addr_t)0xfff);
2122 }
2123 }
2124 }
2125 }
2126 }
2127 }
2128 }
2129}
2130
2131#ifdef TARGET_X86_64
2132static void tlb_info_64(Monitor *mon, CPUState *env)
2133{
2134 uint64_t l1, l2, l3, l4;
2135 uint64_t pml4e, pdpe, pde, pte;
2136 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2137
2138 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2139 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002140 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002141 pml4e = le64_to_cpu(pml4e);
2142 if (pml4e & PG_PRESENT_MASK) {
2143 pdp_addr = pml4e & 0x3fffffffff000ULL;
2144 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002145 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002146 pdpe = le64_to_cpu(pdpe);
2147 if (pdpe & PG_PRESENT_MASK) {
2148 if (pdpe & PG_PSE_MASK) {
2149 /* 1G pages, CR4.PSE is ignored */
2150 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2151 0x3ffffc0000000ULL);
2152 } else {
2153 pd_addr = pdpe & 0x3fffffffff000ULL;
2154 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002155 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002156 pde = le64_to_cpu(pde);
2157 if (pde & PG_PRESENT_MASK) {
2158 if (pde & PG_PSE_MASK) {
2159 /* 2M pages, CR4.PSE is ignored */
2160 print_pte(mon, (l1 << 39) + (l2 << 30) +
2161 (l3 << 21), pde,
2162 0x3ffffffe00000ULL);
2163 } else {
2164 pt_addr = pde & 0x3fffffffff000ULL;
2165 for (l4 = 0; l4 < 512; l4++) {
2166 cpu_physical_memory_read(pt_addr
2167 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002168 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002169 pte = le64_to_cpu(pte);
2170 if (pte & PG_PRESENT_MASK) {
2171 print_pte(mon, (l1 << 39) +
2172 (l2 << 30) +
2173 (l3 << 21) + (l4 << 12),
2174 pte & ~PG_PSE_MASK,
2175 0x3fffffffff000ULL);
2176 }
2177 }
2178 }
2179 }
2180 }
2181 }
2182 }
2183 }
2184 }
2185 }
2186}
2187#endif
2188
2189static void tlb_info(Monitor *mon)
2190{
2191 CPUState *env;
2192
2193 env = mon_get_cpu();
2194
2195 if (!(env->cr[0] & CR0_PG_MASK)) {
2196 monitor_printf(mon, "PG disabled\n");
2197 return;
2198 }
2199 if (env->cr[4] & CR4_PAE_MASK) {
2200#ifdef TARGET_X86_64
2201 if (env->hflags & HF_LMA_MASK) {
2202 tlb_info_64(mon, env);
2203 } else
2204#endif
2205 {
2206 tlb_info_pae32(mon, env);
2207 }
2208 } else {
2209 tlb_info_32(mon, env);
2210 }
2211}
2212
Blue Swirl1b3cba62010-12-11 18:56:27 +00002213static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2214 int *plast_prot,
2215 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002216{
bellard9746b152004-11-11 18:30:24 +00002217 int prot1;
2218 prot1 = *plast_prot;
2219 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002220 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002221 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2222 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002223 *pstart, end, end - *pstart,
2224 prot1 & PG_USER_MASK ? 'u' : '-',
2225 'r',
2226 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002227 }
2228 if (prot != 0)
2229 *pstart = end;
2230 else
2231 *pstart = -1;
2232 *plast_prot = prot;
2233 }
2234}
2235
Blue Swirl1b3cba62010-12-11 18:56:27 +00002236static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002237{
Austin Clementsb49ca722011-08-14 23:19:21 -04002238 unsigned int l1, l2;
2239 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002240 uint32_t pgd, pde, pte;
2241 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002242
bellardb86bda52004-09-18 19:32:46 +00002243 pgd = env->cr[3] & ~0xfff;
2244 last_prot = 0;
2245 start = -1;
2246 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002247 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002248 pde = le32_to_cpu(pde);
2249 end = l1 << 22;
2250 if (pde & PG_PRESENT_MASK) {
2251 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2252 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002253 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002254 } else {
2255 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002256 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002257 pte = le32_to_cpu(pte);
2258 end = (l1 << 22) + (l2 << 12);
2259 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002260 prot = pte & pde &
2261 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002262 } else {
2263 prot = 0;
2264 }
aliguori376253e2009-03-05 23:01:23 +00002265 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002266 }
2267 }
2268 } else {
2269 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002270 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002271 }
2272 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002273 /* Flush last range */
2274 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002275}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002276
2277static void mem_info_pae32(Monitor *mon, CPUState *env)
2278{
Austin Clementsb49ca722011-08-14 23:19:21 -04002279 unsigned int l1, l2, l3;
2280 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002281 uint64_t pdpe, pde, pte;
2282 uint64_t pdp_addr, pd_addr, pt_addr;
2283 target_phys_addr_t start, end;
2284
2285 pdp_addr = env->cr[3] & ~0x1f;
2286 last_prot = 0;
2287 start = -1;
2288 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002289 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002290 pdpe = le64_to_cpu(pdpe);
2291 end = l1 << 30;
2292 if (pdpe & PG_PRESENT_MASK) {
2293 pd_addr = pdpe & 0x3fffffffff000ULL;
2294 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002295 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002296 pde = le64_to_cpu(pde);
2297 end = (l1 << 30) + (l2 << 21);
2298 if (pde & PG_PRESENT_MASK) {
2299 if (pde & PG_PSE_MASK) {
2300 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2301 PG_PRESENT_MASK);
2302 mem_print(mon, &start, &last_prot, end, prot);
2303 } else {
2304 pt_addr = pde & 0x3fffffffff000ULL;
2305 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002306 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002307 pte = le64_to_cpu(pte);
2308 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2309 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002310 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2311 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002312 } else {
2313 prot = 0;
2314 }
2315 mem_print(mon, &start, &last_prot, end, prot);
2316 }
2317 }
2318 } else {
2319 prot = 0;
2320 mem_print(mon, &start, &last_prot, end, prot);
2321 }
2322 }
2323 } else {
2324 prot = 0;
2325 mem_print(mon, &start, &last_prot, end, prot);
2326 }
2327 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002328 /* Flush last range */
2329 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002330}
2331
2332
2333#ifdef TARGET_X86_64
2334static void mem_info_64(Monitor *mon, CPUState *env)
2335{
2336 int prot, last_prot;
2337 uint64_t l1, l2, l3, l4;
2338 uint64_t pml4e, pdpe, pde, pte;
2339 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2340
2341 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2342 last_prot = 0;
2343 start = -1;
2344 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002345 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002346 pml4e = le64_to_cpu(pml4e);
2347 end = l1 << 39;
2348 if (pml4e & PG_PRESENT_MASK) {
2349 pdp_addr = pml4e & 0x3fffffffff000ULL;
2350 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002351 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002352 pdpe = le64_to_cpu(pdpe);
2353 end = (l1 << 39) + (l2 << 30);
2354 if (pdpe & PG_PRESENT_MASK) {
2355 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002356 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2357 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002358 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002359 mem_print(mon, &start, &last_prot, end, prot);
2360 } else {
2361 pd_addr = pdpe & 0x3fffffffff000ULL;
2362 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002363 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002364 pde = le64_to_cpu(pde);
2365 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2366 if (pde & PG_PRESENT_MASK) {
2367 if (pde & PG_PSE_MASK) {
2368 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2369 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002370 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002371 mem_print(mon, &start, &last_prot, end, prot);
2372 } else {
2373 pt_addr = pde & 0x3fffffffff000ULL;
2374 for (l4 = 0; l4 < 512; l4++) {
2375 cpu_physical_memory_read(pt_addr
2376 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002377 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002378 pte = le64_to_cpu(pte);
2379 end = (l1 << 39) + (l2 << 30) +
2380 (l3 << 21) + (l4 << 12);
2381 if (pte & PG_PRESENT_MASK) {
2382 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2383 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002384 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002385 } else {
2386 prot = 0;
2387 }
2388 mem_print(mon, &start, &last_prot, end, prot);
2389 }
2390 }
2391 } else {
2392 prot = 0;
2393 mem_print(mon, &start, &last_prot, end, prot);
2394 }
2395 }
2396 }
2397 } else {
2398 prot = 0;
2399 mem_print(mon, &start, &last_prot, end, prot);
2400 }
2401 }
2402 } else {
2403 prot = 0;
2404 mem_print(mon, &start, &last_prot, end, prot);
2405 }
2406 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002407 /* Flush last range */
2408 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002409}
2410#endif
2411
2412static void mem_info(Monitor *mon)
2413{
2414 CPUState *env;
2415
2416 env = mon_get_cpu();
2417
2418 if (!(env->cr[0] & CR0_PG_MASK)) {
2419 monitor_printf(mon, "PG disabled\n");
2420 return;
2421 }
2422 if (env->cr[4] & CR4_PAE_MASK) {
2423#ifdef TARGET_X86_64
2424 if (env->hflags & HF_LMA_MASK) {
2425 mem_info_64(mon, env);
2426 } else
2427#endif
2428 {
2429 mem_info_pae32(mon, env);
2430 }
2431 } else {
2432 mem_info_32(mon, env);
2433 }
2434}
bellardb86bda52004-09-18 19:32:46 +00002435#endif
2436
aurel327c664e22009-03-03 06:12:22 +00002437#if defined(TARGET_SH4)
2438
aliguori376253e2009-03-05 23:01:23 +00002439static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002440{
aliguori376253e2009-03-05 23:01:23 +00002441 monitor_printf(mon, " tlb%i:\t"
2442 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2443 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2444 "dirty=%hhu writethrough=%hhu\n",
2445 idx,
2446 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2447 tlb->v, tlb->sh, tlb->c, tlb->pr,
2448 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002449}
2450
aliguori376253e2009-03-05 23:01:23 +00002451static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002452{
2453 CPUState *env = mon_get_cpu();
2454 int i;
2455
aliguori376253e2009-03-05 23:01:23 +00002456 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002457 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002458 print_tlb (mon, i, &env->itlb[i]);
2459 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002460 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002461 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002462}
2463
2464#endif
2465
Blue Swirld41160a2010-12-19 13:42:56 +00002466#if defined(TARGET_SPARC)
2467static void tlb_info(Monitor *mon)
2468{
2469 CPUState *env1 = mon_get_cpu();
2470
2471 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2472}
2473#endif
2474
Blue Swirl314e2982011-09-11 20:22:05 +00002475static void do_info_mtree(Monitor *mon)
2476{
2477 mtree_info((fprintf_function)monitor_printf, mon);
2478}
2479
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002480static void do_info_kvm_print(Monitor *mon, const QObject *data)
2481{
2482 QDict *qdict;
2483
2484 qdict = qobject_to_qdict(data);
2485
2486 monitor_printf(mon, "kvm support: ");
2487 if (qdict_get_bool(qdict, "present")) {
2488 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2489 "enabled" : "disabled");
2490 } else {
2491 monitor_printf(mon, "not compiled\n");
2492 }
2493}
2494
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002495static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002496{
2497#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002498 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2499 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002500#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002501 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002502#endif
2503}
2504
aliguori030ea372009-04-21 22:30:47 +00002505static void do_info_numa(Monitor *mon)
2506{
aliguorib28b6232009-04-22 20:20:29 +00002507 int i;
aliguori030ea372009-04-21 22:30:47 +00002508 CPUState *env;
2509
2510 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2511 for (i = 0; i < nb_numa_nodes; i++) {
2512 monitor_printf(mon, "node %d cpus:", i);
2513 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2514 if (env->numa_node == i) {
2515 monitor_printf(mon, " %d", env->cpu_index);
2516 }
2517 }
2518 monitor_printf(mon, "\n");
2519 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2520 node_mem[i] >> 20);
2521 }
2522}
2523
bellard5f1ce942006-02-08 22:40:15 +00002524#ifdef CONFIG_PROFILER
2525
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002526int64_t qemu_time;
2527int64_t dev_time;
2528
aliguori376253e2009-03-05 23:01:23 +00002529static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002530{
2531 int64_t total;
2532 total = qemu_time;
2533 if (total == 0)
2534 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002535 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002536 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002537 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002538 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002539 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002540 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002541}
2542#else
aliguori376253e2009-03-05 23:01:23 +00002543static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002544{
aliguori376253e2009-03-05 23:01:23 +00002545 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002546}
2547#endif
2548
bellardec36b692006-07-16 18:57:03 +00002549/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002550static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002551
aliguori376253e2009-03-05 23:01:23 +00002552static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002553{
2554 int i;
2555 CaptureState *s;
2556
2557 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002558 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002559 s->ops.info (s->opaque);
2560 }
2561}
2562
Blue Swirl23130862009-06-06 08:22:04 +00002563#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002564static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002565{
2566 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002567 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002568 CaptureState *s;
2569
2570 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2571 if (i == n) {
2572 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002573 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002574 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002575 return;
2576 }
2577 }
2578}
2579
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002580static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002581{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002582 const char *path = qdict_get_str(qdict, "path");
2583 int has_freq = qdict_haskey(qdict, "freq");
2584 int freq = qdict_get_try_int(qdict, "freq", -1);
2585 int has_bits = qdict_haskey(qdict, "bits");
2586 int bits = qdict_get_try_int(qdict, "bits", -1);
2587 int has_channels = qdict_haskey(qdict, "nchannels");
2588 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002589 CaptureState *s;
2590
Anthony Liguori7267c092011-08-20 22:09:37 -05002591 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002592
2593 freq = has_freq ? freq : 44100;
2594 bits = has_bits ? bits : 16;
2595 nchannels = has_channels ? nchannels : 2;
2596
2597 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002598 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002599 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002600 return;
bellardec36b692006-07-16 18:57:03 +00002601 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002602 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002603}
2604#endif
2605
aurel32dc1c0b72008-04-27 23:52:12 +00002606#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002607static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002608{
2609 CPUState *env;
2610
2611 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2612 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2613 }
2614
2615 return 0;
2616}
2617#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002618static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002619{
2620 qerror_report(QERR_UNSUPPORTED);
2621 return -1;
2622}
aurel32dc1c0b72008-04-27 23:52:12 +00002623#endif
2624
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002625static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002626{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002627 QDict *qdict;
Luiz Capitulino6e0ca882011-08-09 14:44:25 -03002628 const char *status;
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002629
2630 qdict = qobject_to_qdict(data);
2631
2632 monitor_printf(mon, "VM status: ");
2633 if (qdict_get_bool(qdict, "running")) {
2634 monitor_printf(mon, "running");
2635 if (qdict_get_bool(qdict, "singlestep")) {
2636 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002637 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002638 } else {
2639 monitor_printf(mon, "paused");
2640 }
2641
Luiz Capitulino6e0ca882011-08-09 14:44:25 -03002642 status = qdict_get_str(qdict, "status");
2643 if (strcmp(status, "paused") && strcmp(status, "running")) {
2644 monitor_printf(mon, " (%s)", status);
2645 }
2646
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002647 monitor_printf(mon, "\n");
2648}
2649
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002650static void do_info_status(Monitor *mon, QObject **ret_data)
2651{
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -03002652 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep, runstate_as_string());
aurel326f9c5ee2008-12-18 22:43:56 +00002653}
2654
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002655static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002656{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002657 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002658
aliguori76655d62009-03-06 20:27:37 +00002659 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002660 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002661 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002662 return acl;
2663}
aliguori76655d62009-03-06 20:27:37 +00002664
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002665static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002666{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002667 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002668 qemu_acl *acl = find_acl(mon, aclname);
2669 qemu_acl_entry *entry;
2670 int i = 0;
2671
2672 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002673 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002674 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002675 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002676 i++;
2677 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002678 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002679 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002680 }
2681}
2682
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002683static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002684{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002685 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002686 qemu_acl *acl = find_acl(mon, aclname);
2687
2688 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002689 qemu_acl_reset(acl);
2690 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002691 }
2692}
aliguori76655d62009-03-06 20:27:37 +00002693
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002694static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002695{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002696 const char *aclname = qdict_get_str(qdict, "aclname");
2697 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002698 qemu_acl *acl = find_acl(mon, aclname);
2699
2700 if (acl) {
2701 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002702 acl->defaultDeny = 0;
2703 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002704 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002705 acl->defaultDeny = 1;
2706 monitor_printf(mon, "acl: policy set to 'deny'\n");
2707 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002708 monitor_printf(mon, "acl: unknown policy '%s', "
2709 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002710 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002711 }
2712}
aliguori76655d62009-03-06 20:27:37 +00002713
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002714static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002715{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002716 const char *aclname = qdict_get_str(qdict, "aclname");
2717 const char *match = qdict_get_str(qdict, "match");
2718 const char *policy = qdict_get_str(qdict, "policy");
2719 int has_index = qdict_haskey(qdict, "index");
2720 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002721 qemu_acl *acl = find_acl(mon, aclname);
2722 int deny, ret;
2723
2724 if (acl) {
2725 if (strcmp(policy, "allow") == 0) {
2726 deny = 0;
2727 } else if (strcmp(policy, "deny") == 0) {
2728 deny = 1;
2729 } else {
2730 monitor_printf(mon, "acl: unknown policy '%s', "
2731 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002732 return;
2733 }
aliguori28a76be2009-03-06 20:27:40 +00002734 if (has_index)
2735 ret = qemu_acl_insert(acl, deny, match, index);
2736 else
2737 ret = qemu_acl_append(acl, deny, match);
2738 if (ret < 0)
2739 monitor_printf(mon, "acl: unable to add acl entry\n");
2740 else
2741 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002742 }
2743}
aliguori76655d62009-03-06 20:27:37 +00002744
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002745static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002746{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002747 const char *aclname = qdict_get_str(qdict, "aclname");
2748 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002749 qemu_acl *acl = find_acl(mon, aclname);
2750 int ret;
aliguori76655d62009-03-06 20:27:37 +00002751
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002752 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002753 ret = qemu_acl_remove(acl, match);
2754 if (ret < 0)
2755 monitor_printf(mon, "acl: no matching acl entry\n");
2756 else
2757 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002758 }
2759}
2760
Huang Ying79c4f6b2009-06-23 10:05:14 +08002761#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002762static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002763{
2764 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002765 int cpu_index = qdict_get_int(qdict, "cpu_index");
2766 int bank = qdict_get_int(qdict, "bank");
2767 uint64_t status = qdict_get_int(qdict, "status");
2768 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2769 uint64_t addr = qdict_get_int(qdict, "addr");
2770 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002771 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002772
Jan Kiszka747461c2011-03-02 08:56:10 +01002773 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2774 flags |= MCE_INJECT_BROADCAST;
2775 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002776 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002777 if (cenv->cpu_index == cpu_index) {
2778 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002779 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002780 break;
2781 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002782 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002783}
2784#endif
2785
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002786static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002787{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002788 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002789 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002790 int fd;
2791
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002792 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002793 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002794 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002795 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002796 }
2797
2798 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002799 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2800 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002801 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002802 }
2803
Blue Swirl72cf2d42009-09-12 07:36:22 +00002804 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002805 if (strcmp(monfd->name, fdname) != 0) {
2806 continue;
2807 }
2808
2809 close(monfd->fd);
2810 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002811 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002812 }
2813
Anthony Liguori7267c092011-08-20 22:09:37 -05002814 monfd = g_malloc0(sizeof(mon_fd_t));
2815 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002816 monfd->fd = fd;
2817
Blue Swirl72cf2d42009-09-12 07:36:22 +00002818 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002819 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002820}
2821
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002822static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002823{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002824 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002825 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002826
Blue Swirl72cf2d42009-09-12 07:36:22 +00002827 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002828 if (strcmp(monfd->name, fdname) != 0) {
2829 continue;
2830 }
2831
Blue Swirl72cf2d42009-09-12 07:36:22 +00002832 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002833 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002834 g_free(monfd->name);
2835 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002836 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002837 }
2838
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002839 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002840 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002841}
2842
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002843static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002844{
Luiz Capitulino13548692011-07-29 15:36:43 -03002845 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002846 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002847
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002848 vm_stop(RSTATE_RESTORE);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002849
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002850 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002851 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002852 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002853}
2854
Mark McLoughlin7768e042009-07-22 09:11:41 +01002855int monitor_get_fd(Monitor *mon, const char *fdname)
2856{
Anthony Liguoric227f092009-10-01 16:12:16 -05002857 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002858
Blue Swirl72cf2d42009-09-12 07:36:22 +00002859 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002860 int fd;
2861
2862 if (strcmp(monfd->name, fdname) != 0) {
2863 continue;
2864 }
2865
2866 fd = monfd->fd;
2867
2868 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002869 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002870 g_free(monfd->name);
2871 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002872
2873 return fd;
2874 }
2875
2876 return -1;
2877}
2878
Anthony Liguoric227f092009-10-01 16:12:16 -05002879static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002880#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002881 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002882};
2883
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002884/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002885static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002886 {
2887 .name = "version",
2888 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 .params = "",
2890 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002891 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002892 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002893 },
2894 {
2895 .name = "network",
2896 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002897 .params = "",
2898 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002899 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002900 },
2901 {
2902 .name = "chardev",
2903 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002904 .params = "",
2905 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002906 .user_print = qemu_chr_info_print,
2907 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002908 },
2909 {
2910 .name = "block",
2911 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002912 .params = "",
2913 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002914 .user_print = bdrv_info_print,
2915 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002916 },
2917 {
2918 .name = "blockstats",
2919 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002920 .params = "",
2921 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002922 .user_print = bdrv_stats_print,
2923 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002924 },
2925 {
2926 .name = "registers",
2927 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002928 .params = "",
2929 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002930 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002931 },
2932 {
2933 .name = "cpus",
2934 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002935 .params = "",
2936 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002937 .user_print = monitor_print_cpus,
2938 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002939 },
2940 {
2941 .name = "history",
2942 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002943 .params = "",
2944 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002945 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002946 },
2947 {
2948 .name = "irq",
2949 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002950 .params = "",
2951 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002952 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002953 },
2954 {
2955 .name = "pic",
2956 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002957 .params = "",
2958 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002959 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002960 },
2961 {
2962 .name = "pci",
2963 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002964 .params = "",
2965 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002966 .user_print = do_pci_info_print,
2967 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002968 },
Blue Swirld41160a2010-12-19 13:42:56 +00002969#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002970 {
2971 .name = "tlb",
2972 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002973 .params = "",
2974 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002975 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002976 },
aurel327c664e22009-03-03 06:12:22 +00002977#endif
2978#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002979 {
2980 .name = "mem",
2981 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002982 .params = "",
2983 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002984 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002985 },
bellardb86bda52004-09-18 19:32:46 +00002986#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002987 {
Blue Swirl314e2982011-09-11 20:22:05 +00002988 .name = "mtree",
2989 .args_type = "",
2990 .params = "",
2991 .help = "show memory tree",
2992 .mhandler.info = do_info_mtree,
2993 },
2994 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002995 .name = "jit",
2996 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002997 .params = "",
2998 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002999 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003000 },
3001 {
3002 .name = "kvm",
3003 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003004 .params = "",
3005 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02003006 .user_print = do_info_kvm_print,
3007 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003008 },
3009 {
3010 .name = "numa",
3011 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003012 .params = "",
3013 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003014 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003015 },
3016 {
3017 .name = "usb",
3018 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003019 .params = "",
3020 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003021 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003022 },
3023 {
3024 .name = "usbhost",
3025 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003026 .params = "",
3027 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003028 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003029 },
3030 {
3031 .name = "profile",
3032 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003033 .params = "",
3034 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003035 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003036 },
3037 {
3038 .name = "capture",
3039 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003040 .params = "",
3041 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003042 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003043 },
3044 {
3045 .name = "snapshots",
3046 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003047 .params = "",
3048 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03003049 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003050 },
3051 {
3052 .name = "status",
3053 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003054 .params = "",
3055 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02003056 .user_print = do_info_status_print,
3057 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003058 },
3059 {
3060 .name = "pcmcia",
3061 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003062 .params = "",
3063 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03003064 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003065 },
3066 {
3067 .name = "mice",
3068 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003069 .params = "",
3070 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02003071 .user_print = do_info_mice_print,
3072 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003073 },
3074 {
3075 .name = "vnc",
3076 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003077 .params = "",
3078 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02003079 .user_print = do_info_vnc_print,
3080 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003081 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003082#if defined(CONFIG_SPICE)
3083 {
3084 .name = "spice",
3085 .args_type = "",
3086 .params = "",
3087 .help = "show the spice server status",
3088 .user_print = do_info_spice_print,
3089 .mhandler.info_new = do_info_spice,
3090 },
3091#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003092 {
3093 .name = "name",
3094 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003095 .params = "",
3096 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02003097 .user_print = do_info_name_print,
3098 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003099 },
3100 {
3101 .name = "uuid",
3102 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003103 .params = "",
3104 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02003105 .user_print = do_info_uuid_print,
3106 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003107 },
j_mayer76a66252007-03-07 08:32:30 +00003108#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003109 {
3110 .name = "cpustats",
3111 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003112 .params = "",
3113 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03003114 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003115 },
j_mayer76a66252007-03-07 08:32:30 +00003116#endif
blueswir131a60e22007-10-26 18:42:59 +00003117#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003118 {
3119 .name = "usernet",
3120 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003121 .params = "",
3122 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03003123 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003124 },
blueswir131a60e22007-10-26 18:42:59 +00003125#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003126 {
3127 .name = "migrate",
3128 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003129 .params = "",
3130 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003131 .user_print = do_info_migrate_print,
3132 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003133 },
3134 {
3135 .name = "balloon",
3136 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003137 .params = "",
3138 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003139 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003140 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003141 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003142 },
3143 {
3144 .name = "qtree",
3145 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003146 .params = "",
3147 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003148 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003149 },
3150 {
3151 .name = "qdm",
3152 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003153 .params = "",
3154 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003155 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003156 },
3157 {
3158 .name = "roms",
3159 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003160 .params = "",
3161 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003162 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003163 },
Lluís6d8a7642011-08-31 20:30:43 +02003164#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303165 {
3166 .name = "trace",
3167 .args_type = "",
3168 .params = "",
3169 .help = "show current contents of trace buffer",
3170 .mhandler.info = do_info_trace,
3171 },
Lluís31965ae2011-08-31 20:31:24 +02003172#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303173 {
3174 .name = "trace-events",
3175 .args_type = "",
3176 .params = "",
3177 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003178 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303179 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003180 {
3181 .name = NULL,
3182 },
bellard9dc39cb2004-03-14 21:38:27 +00003183};
3184
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003185static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05003186#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003187 { /* NULL */ },
3188};
3189
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003190static const mon_cmd_t qmp_query_cmds[] = {
3191 {
3192 .name = "version",
3193 .args_type = "",
3194 .params = "",
3195 .help = "show the version of QEMU",
3196 .user_print = do_info_version_print,
3197 .mhandler.info_new = do_info_version,
3198 },
3199 {
3200 .name = "commands",
3201 .args_type = "",
3202 .params = "",
3203 .help = "list QMP available commands",
3204 .user_print = monitor_user_noop,
3205 .mhandler.info_new = do_info_commands,
3206 },
3207 {
3208 .name = "chardev",
3209 .args_type = "",
3210 .params = "",
3211 .help = "show the character devices",
3212 .user_print = qemu_chr_info_print,
3213 .mhandler.info_new = qemu_chr_info,
3214 },
3215 {
3216 .name = "block",
3217 .args_type = "",
3218 .params = "",
3219 .help = "show the block devices",
3220 .user_print = bdrv_info_print,
3221 .mhandler.info_new = bdrv_info,
3222 },
3223 {
3224 .name = "blockstats",
3225 .args_type = "",
3226 .params = "",
3227 .help = "show block device statistics",
3228 .user_print = bdrv_stats_print,
3229 .mhandler.info_new = bdrv_info_stats,
3230 },
3231 {
3232 .name = "cpus",
3233 .args_type = "",
3234 .params = "",
3235 .help = "show infos for each CPU",
3236 .user_print = monitor_print_cpus,
3237 .mhandler.info_new = do_info_cpus,
3238 },
3239 {
3240 .name = "pci",
3241 .args_type = "",
3242 .params = "",
3243 .help = "show PCI info",
3244 .user_print = do_pci_info_print,
3245 .mhandler.info_new = do_pci_info,
3246 },
3247 {
3248 .name = "kvm",
3249 .args_type = "",
3250 .params = "",
3251 .help = "show KVM information",
3252 .user_print = do_info_kvm_print,
3253 .mhandler.info_new = do_info_kvm,
3254 },
3255 {
3256 .name = "status",
3257 .args_type = "",
3258 .params = "",
3259 .help = "show the current VM status (running|paused)",
3260 .user_print = do_info_status_print,
3261 .mhandler.info_new = do_info_status,
3262 },
3263 {
3264 .name = "mice",
3265 .args_type = "",
3266 .params = "",
3267 .help = "show which guest mouse is receiving events",
3268 .user_print = do_info_mice_print,
3269 .mhandler.info_new = do_info_mice,
3270 },
3271 {
3272 .name = "vnc",
3273 .args_type = "",
3274 .params = "",
3275 .help = "show the vnc server status",
3276 .user_print = do_info_vnc_print,
3277 .mhandler.info_new = do_info_vnc,
3278 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003279#if defined(CONFIG_SPICE)
3280 {
3281 .name = "spice",
3282 .args_type = "",
3283 .params = "",
3284 .help = "show the spice server status",
3285 .user_print = do_info_spice_print,
3286 .mhandler.info_new = do_info_spice,
3287 },
3288#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003289 {
3290 .name = "name",
3291 .args_type = "",
3292 .params = "",
3293 .help = "show the current VM name",
3294 .user_print = do_info_name_print,
3295 .mhandler.info_new = do_info_name,
3296 },
3297 {
3298 .name = "uuid",
3299 .args_type = "",
3300 .params = "",
3301 .help = "show the current VM UUID",
3302 .user_print = do_info_uuid_print,
3303 .mhandler.info_new = do_info_uuid,
3304 },
3305 {
3306 .name = "migrate",
3307 .args_type = "",
3308 .params = "",
3309 .help = "show migration status",
3310 .user_print = do_info_migrate_print,
3311 .mhandler.info_new = do_info_migrate,
3312 },
3313 {
3314 .name = "balloon",
3315 .args_type = "",
3316 .params = "",
3317 .help = "show balloon information",
3318 .user_print = monitor_print_balloon,
3319 .mhandler.info_async = do_info_balloon,
3320 .flags = MONITOR_CMD_ASYNC,
3321 },
3322 { /* NULL */ },
3323};
3324
bellard9307c4c2004-04-04 12:57:25 +00003325/*******************************************************************/
3326
3327static const char *pch;
3328static jmp_buf expr_env;
3329
bellard92a31b12005-02-10 22:00:52 +00003330#define MD_TLONG 0
3331#define MD_I32 1
3332
bellard9307c4c2004-04-04 12:57:25 +00003333typedef struct MonitorDef {
3334 const char *name;
3335 int offset;
blueswir18662d652008-10-02 18:32:44 +00003336 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003337 int type;
bellard9307c4c2004-04-04 12:57:25 +00003338} MonitorDef;
3339
bellard57206fd2004-04-25 18:54:52 +00003340#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003341static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003342{
bellard6a00d602005-11-21 23:25:50 +00003343 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003344 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003345}
3346#endif
3347
bellarda541f292004-04-12 20:39:29 +00003348#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003349static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003350{
bellard6a00d602005-11-21 23:25:50 +00003351 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003352 unsigned int u;
3353 int i;
3354
3355 u = 0;
3356 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003357 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003358
3359 return u;
3360}
3361
blueswir18662d652008-10-02 18:32:44 +00003362static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003363{
bellard6a00d602005-11-21 23:25:50 +00003364 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003365 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003366}
3367
blueswir18662d652008-10-02 18:32:44 +00003368static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003369{
bellard6a00d602005-11-21 23:25:50 +00003370 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003371 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003372}
bellard9fddaa02004-05-21 12:59:32 +00003373
blueswir18662d652008-10-02 18:32:44 +00003374static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003375{
bellard6a00d602005-11-21 23:25:50 +00003376 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003377 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003378}
3379
blueswir18662d652008-10-02 18:32:44 +00003380static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003381{
bellard6a00d602005-11-21 23:25:50 +00003382 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003383 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003384}
3385
blueswir18662d652008-10-02 18:32:44 +00003386static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003387{
bellard6a00d602005-11-21 23:25:50 +00003388 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003389 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003390}
bellarda541f292004-04-12 20:39:29 +00003391#endif
3392
bellarde95c8d52004-09-30 22:22:08 +00003393#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003394#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003395static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003396{
bellard6a00d602005-11-21 23:25:50 +00003397 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003398
3399 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003400}
bellard7b936c02005-10-30 17:05:13 +00003401#endif
bellarde95c8d52004-09-30 22:22:08 +00003402
blueswir18662d652008-10-02 18:32:44 +00003403static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003404{
bellard6a00d602005-11-21 23:25:50 +00003405 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003406 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003407}
3408#endif
3409
blueswir18662d652008-10-02 18:32:44 +00003410static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003411#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003412
3413#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003414 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003415 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003416 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003417
bellard9307c4c2004-04-04 12:57:25 +00003418 { "eax", offsetof(CPUState, regs[0]) },
3419 { "ecx", offsetof(CPUState, regs[1]) },
3420 { "edx", offsetof(CPUState, regs[2]) },
3421 { "ebx", offsetof(CPUState, regs[3]) },
3422 { "esp|sp", offsetof(CPUState, regs[4]) },
3423 { "ebp|fp", offsetof(CPUState, regs[5]) },
3424 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003425 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003426#ifdef TARGET_X86_64
3427 { "r8", offsetof(CPUState, regs[8]) },
3428 { "r9", offsetof(CPUState, regs[9]) },
3429 { "r10", offsetof(CPUState, regs[10]) },
3430 { "r11", offsetof(CPUState, regs[11]) },
3431 { "r12", offsetof(CPUState, regs[12]) },
3432 { "r13", offsetof(CPUState, regs[13]) },
3433 { "r14", offsetof(CPUState, regs[14]) },
3434 { "r15", offsetof(CPUState, regs[15]) },
3435#endif
bellard9307c4c2004-04-04 12:57:25 +00003436 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003437 { "eip", offsetof(CPUState, eip) },
3438 SEG("cs", R_CS)
3439 SEG("ds", R_DS)
3440 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003441 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003442 SEG("fs", R_FS)
3443 SEG("gs", R_GS)
3444 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003445#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003446 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003447 { "r0", offsetof(CPUState, gpr[0]) },
3448 { "r1", offsetof(CPUState, gpr[1]) },
3449 { "r2", offsetof(CPUState, gpr[2]) },
3450 { "r3", offsetof(CPUState, gpr[3]) },
3451 { "r4", offsetof(CPUState, gpr[4]) },
3452 { "r5", offsetof(CPUState, gpr[5]) },
3453 { "r6", offsetof(CPUState, gpr[6]) },
3454 { "r7", offsetof(CPUState, gpr[7]) },
3455 { "r8", offsetof(CPUState, gpr[8]) },
3456 { "r9", offsetof(CPUState, gpr[9]) },
3457 { "r10", offsetof(CPUState, gpr[10]) },
3458 { "r11", offsetof(CPUState, gpr[11]) },
3459 { "r12", offsetof(CPUState, gpr[12]) },
3460 { "r13", offsetof(CPUState, gpr[13]) },
3461 { "r14", offsetof(CPUState, gpr[14]) },
3462 { "r15", offsetof(CPUState, gpr[15]) },
3463 { "r16", offsetof(CPUState, gpr[16]) },
3464 { "r17", offsetof(CPUState, gpr[17]) },
3465 { "r18", offsetof(CPUState, gpr[18]) },
3466 { "r19", offsetof(CPUState, gpr[19]) },
3467 { "r20", offsetof(CPUState, gpr[20]) },
3468 { "r21", offsetof(CPUState, gpr[21]) },
3469 { "r22", offsetof(CPUState, gpr[22]) },
3470 { "r23", offsetof(CPUState, gpr[23]) },
3471 { "r24", offsetof(CPUState, gpr[24]) },
3472 { "r25", offsetof(CPUState, gpr[25]) },
3473 { "r26", offsetof(CPUState, gpr[26]) },
3474 { "r27", offsetof(CPUState, gpr[27]) },
3475 { "r28", offsetof(CPUState, gpr[28]) },
3476 { "r29", offsetof(CPUState, gpr[29]) },
3477 { "r30", offsetof(CPUState, gpr[30]) },
3478 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003479 /* Floating point registers */
3480 { "f0", offsetof(CPUState, fpr[0]) },
3481 { "f1", offsetof(CPUState, fpr[1]) },
3482 { "f2", offsetof(CPUState, fpr[2]) },
3483 { "f3", offsetof(CPUState, fpr[3]) },
3484 { "f4", offsetof(CPUState, fpr[4]) },
3485 { "f5", offsetof(CPUState, fpr[5]) },
3486 { "f6", offsetof(CPUState, fpr[6]) },
3487 { "f7", offsetof(CPUState, fpr[7]) },
3488 { "f8", offsetof(CPUState, fpr[8]) },
3489 { "f9", offsetof(CPUState, fpr[9]) },
3490 { "f10", offsetof(CPUState, fpr[10]) },
3491 { "f11", offsetof(CPUState, fpr[11]) },
3492 { "f12", offsetof(CPUState, fpr[12]) },
3493 { "f13", offsetof(CPUState, fpr[13]) },
3494 { "f14", offsetof(CPUState, fpr[14]) },
3495 { "f15", offsetof(CPUState, fpr[15]) },
3496 { "f16", offsetof(CPUState, fpr[16]) },
3497 { "f17", offsetof(CPUState, fpr[17]) },
3498 { "f18", offsetof(CPUState, fpr[18]) },
3499 { "f19", offsetof(CPUState, fpr[19]) },
3500 { "f20", offsetof(CPUState, fpr[20]) },
3501 { "f21", offsetof(CPUState, fpr[21]) },
3502 { "f22", offsetof(CPUState, fpr[22]) },
3503 { "f23", offsetof(CPUState, fpr[23]) },
3504 { "f24", offsetof(CPUState, fpr[24]) },
3505 { "f25", offsetof(CPUState, fpr[25]) },
3506 { "f26", offsetof(CPUState, fpr[26]) },
3507 { "f27", offsetof(CPUState, fpr[27]) },
3508 { "f28", offsetof(CPUState, fpr[28]) },
3509 { "f29", offsetof(CPUState, fpr[29]) },
3510 { "f30", offsetof(CPUState, fpr[30]) },
3511 { "f31", offsetof(CPUState, fpr[31]) },
3512 { "fpscr", offsetof(CPUState, fpscr) },
3513 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003514 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003515 { "lr", offsetof(CPUState, lr) },
3516 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003517 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003518 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003519 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003520 { "msr", 0, &monitor_get_msr, },
3521 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003522 { "tbu", 0, &monitor_get_tbu, },
3523 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003524#if defined(TARGET_PPC64)
3525 /* Address space register */
3526 { "asr", offsetof(CPUState, asr) },
3527#endif
3528 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003529 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003530 { "sr0", offsetof(CPUState, sr[0]) },
3531 { "sr1", offsetof(CPUState, sr[1]) },
3532 { "sr2", offsetof(CPUState, sr[2]) },
3533 { "sr3", offsetof(CPUState, sr[3]) },
3534 { "sr4", offsetof(CPUState, sr[4]) },
3535 { "sr5", offsetof(CPUState, sr[5]) },
3536 { "sr6", offsetof(CPUState, sr[6]) },
3537 { "sr7", offsetof(CPUState, sr[7]) },
3538 { "sr8", offsetof(CPUState, sr[8]) },
3539 { "sr9", offsetof(CPUState, sr[9]) },
3540 { "sr10", offsetof(CPUState, sr[10]) },
3541 { "sr11", offsetof(CPUState, sr[11]) },
3542 { "sr12", offsetof(CPUState, sr[12]) },
3543 { "sr13", offsetof(CPUState, sr[13]) },
3544 { "sr14", offsetof(CPUState, sr[14]) },
3545 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003546 /* Too lazy to put BATs... */
3547 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3548
3549 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3550 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3551 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3552 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3553 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3554 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3555 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3556 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3557 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3558 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3559 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3560 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3561 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3562 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3563 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3564 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3565 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3566 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3567 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3568 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3569 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3570 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3571 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3572 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3573 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3574 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3575 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3576 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3577 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3578 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3579 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3580 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3581 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3582 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3583 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3584 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3585 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3586 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3587 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3588 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3589 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3590 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3591 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3592 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3593 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3594 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3595 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3596 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3597 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3598 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3599 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3600 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3601 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3602 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3603 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3604 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3605 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3606 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3607 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3608 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3609 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3610 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3611 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3612 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3613 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3614 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3615
bellarde95c8d52004-09-30 22:22:08 +00003616#elif defined(TARGET_SPARC)
3617 { "g0", offsetof(CPUState, gregs[0]) },
3618 { "g1", offsetof(CPUState, gregs[1]) },
3619 { "g2", offsetof(CPUState, gregs[2]) },
3620 { "g3", offsetof(CPUState, gregs[3]) },
3621 { "g4", offsetof(CPUState, gregs[4]) },
3622 { "g5", offsetof(CPUState, gregs[5]) },
3623 { "g6", offsetof(CPUState, gregs[6]) },
3624 { "g7", offsetof(CPUState, gregs[7]) },
3625 { "o0", 0, monitor_get_reg },
3626 { "o1", 1, monitor_get_reg },
3627 { "o2", 2, monitor_get_reg },
3628 { "o3", 3, monitor_get_reg },
3629 { "o4", 4, monitor_get_reg },
3630 { "o5", 5, monitor_get_reg },
3631 { "o6", 6, monitor_get_reg },
3632 { "o7", 7, monitor_get_reg },
3633 { "l0", 8, monitor_get_reg },
3634 { "l1", 9, monitor_get_reg },
3635 { "l2", 10, monitor_get_reg },
3636 { "l3", 11, monitor_get_reg },
3637 { "l4", 12, monitor_get_reg },
3638 { "l5", 13, monitor_get_reg },
3639 { "l6", 14, monitor_get_reg },
3640 { "l7", 15, monitor_get_reg },
3641 { "i0", 16, monitor_get_reg },
3642 { "i1", 17, monitor_get_reg },
3643 { "i2", 18, monitor_get_reg },
3644 { "i3", 19, monitor_get_reg },
3645 { "i4", 20, monitor_get_reg },
3646 { "i5", 21, monitor_get_reg },
3647 { "i6", 22, monitor_get_reg },
3648 { "i7", 23, monitor_get_reg },
3649 { "pc", offsetof(CPUState, pc) },
3650 { "npc", offsetof(CPUState, npc) },
3651 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003652#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003653 { "psr", 0, &monitor_get_psr, },
3654 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003655#endif
bellarde95c8d52004-09-30 22:22:08 +00003656 { "tbr", offsetof(CPUState, tbr) },
3657 { "fsr", offsetof(CPUState, fsr) },
3658 { "f0", offsetof(CPUState, fpr[0]) },
3659 { "f1", offsetof(CPUState, fpr[1]) },
3660 { "f2", offsetof(CPUState, fpr[2]) },
3661 { "f3", offsetof(CPUState, fpr[3]) },
3662 { "f4", offsetof(CPUState, fpr[4]) },
3663 { "f5", offsetof(CPUState, fpr[5]) },
3664 { "f6", offsetof(CPUState, fpr[6]) },
3665 { "f7", offsetof(CPUState, fpr[7]) },
3666 { "f8", offsetof(CPUState, fpr[8]) },
3667 { "f9", offsetof(CPUState, fpr[9]) },
3668 { "f10", offsetof(CPUState, fpr[10]) },
3669 { "f11", offsetof(CPUState, fpr[11]) },
3670 { "f12", offsetof(CPUState, fpr[12]) },
3671 { "f13", offsetof(CPUState, fpr[13]) },
3672 { "f14", offsetof(CPUState, fpr[14]) },
3673 { "f15", offsetof(CPUState, fpr[15]) },
3674 { "f16", offsetof(CPUState, fpr[16]) },
3675 { "f17", offsetof(CPUState, fpr[17]) },
3676 { "f18", offsetof(CPUState, fpr[18]) },
3677 { "f19", offsetof(CPUState, fpr[19]) },
3678 { "f20", offsetof(CPUState, fpr[20]) },
3679 { "f21", offsetof(CPUState, fpr[21]) },
3680 { "f22", offsetof(CPUState, fpr[22]) },
3681 { "f23", offsetof(CPUState, fpr[23]) },
3682 { "f24", offsetof(CPUState, fpr[24]) },
3683 { "f25", offsetof(CPUState, fpr[25]) },
3684 { "f26", offsetof(CPUState, fpr[26]) },
3685 { "f27", offsetof(CPUState, fpr[27]) },
3686 { "f28", offsetof(CPUState, fpr[28]) },
3687 { "f29", offsetof(CPUState, fpr[29]) },
3688 { "f30", offsetof(CPUState, fpr[30]) },
3689 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003690#ifdef TARGET_SPARC64
3691 { "f32", offsetof(CPUState, fpr[32]) },
3692 { "f34", offsetof(CPUState, fpr[34]) },
3693 { "f36", offsetof(CPUState, fpr[36]) },
3694 { "f38", offsetof(CPUState, fpr[38]) },
3695 { "f40", offsetof(CPUState, fpr[40]) },
3696 { "f42", offsetof(CPUState, fpr[42]) },
3697 { "f44", offsetof(CPUState, fpr[44]) },
3698 { "f46", offsetof(CPUState, fpr[46]) },
3699 { "f48", offsetof(CPUState, fpr[48]) },
3700 { "f50", offsetof(CPUState, fpr[50]) },
3701 { "f52", offsetof(CPUState, fpr[52]) },
3702 { "f54", offsetof(CPUState, fpr[54]) },
3703 { "f56", offsetof(CPUState, fpr[56]) },
3704 { "f58", offsetof(CPUState, fpr[58]) },
3705 { "f60", offsetof(CPUState, fpr[60]) },
3706 { "f62", offsetof(CPUState, fpr[62]) },
3707 { "asi", offsetof(CPUState, asi) },
3708 { "pstate", offsetof(CPUState, pstate) },
3709 { "cansave", offsetof(CPUState, cansave) },
3710 { "canrestore", offsetof(CPUState, canrestore) },
3711 { "otherwin", offsetof(CPUState, otherwin) },
3712 { "wstate", offsetof(CPUState, wstate) },
3713 { "cleanwin", offsetof(CPUState, cleanwin) },
3714 { "fprs", offsetof(CPUState, fprs) },
3715#endif
bellard9307c4c2004-04-04 12:57:25 +00003716#endif
3717 { NULL },
3718};
3719
aliguori376253e2009-03-05 23:01:23 +00003720static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003721{
aliguori376253e2009-03-05 23:01:23 +00003722 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003723 longjmp(expr_env, 1);
3724}
3725
Markus Armbruster09b94182010-01-20 13:07:30 +01003726/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003727static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003728{
blueswir18662d652008-10-02 18:32:44 +00003729 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003730 void *ptr;
3731
bellard9307c4c2004-04-04 12:57:25 +00003732 for(md = monitor_defs; md->name != NULL; md++) {
3733 if (compare_cmd(name, md->name)) {
3734 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003735 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003736 } else {
bellard6a00d602005-11-21 23:25:50 +00003737 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003738 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003739 switch(md->type) {
3740 case MD_I32:
3741 *pval = *(int32_t *)ptr;
3742 break;
3743 case MD_TLONG:
3744 *pval = *(target_long *)ptr;
3745 break;
3746 default:
3747 *pval = 0;
3748 break;
3749 }
bellard9307c4c2004-04-04 12:57:25 +00003750 }
3751 return 0;
3752 }
3753 }
3754 return -1;
3755}
3756
3757static void next(void)
3758{
Blue Swirl660f11b2009-07-31 21:16:51 +00003759 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003760 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003761 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003762 pch++;
3763 }
3764}
3765
aliguori376253e2009-03-05 23:01:23 +00003766static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003767
aliguori376253e2009-03-05 23:01:23 +00003768static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003769{
blueswir1c2efc952007-09-25 17:28:42 +00003770 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003771 char *p;
bellard6a00d602005-11-21 23:25:50 +00003772 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003773
3774 switch(*pch) {
3775 case '+':
3776 next();
aliguori376253e2009-03-05 23:01:23 +00003777 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003778 break;
3779 case '-':
3780 next();
aliguori376253e2009-03-05 23:01:23 +00003781 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003782 break;
3783 case '~':
3784 next();
aliguori376253e2009-03-05 23:01:23 +00003785 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003786 break;
3787 case '(':
3788 next();
aliguori376253e2009-03-05 23:01:23 +00003789 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003790 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003791 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003792 }
3793 next();
3794 break;
bellard81d09122004-07-14 17:21:37 +00003795 case '\'':
3796 pch++;
3797 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003798 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003799 n = *pch;
3800 pch++;
3801 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003802 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003803 next();
3804 break;
bellard9307c4c2004-04-04 12:57:25 +00003805 case '$':
3806 {
3807 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003808 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003809
bellard9307c4c2004-04-04 12:57:25 +00003810 pch++;
3811 q = buf;
3812 while ((*pch >= 'a' && *pch <= 'z') ||
3813 (*pch >= 'A' && *pch <= 'Z') ||
3814 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003815 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003816 if ((q - buf) < sizeof(buf) - 1)
3817 *q++ = *pch;
3818 pch++;
3819 }
blueswir1cd390082008-11-16 13:53:32 +00003820 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003821 pch++;
3822 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003823 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003824 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003825 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003826 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003827 }
3828 break;
3829 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003830 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003831 n = 0;
3832 break;
3833 default:
blueswir17743e582007-09-24 18:39:04 +00003834#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003835 n = strtoull(pch, &p, 0);
3836#else
bellard9307c4c2004-04-04 12:57:25 +00003837 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003838#endif
bellard9307c4c2004-04-04 12:57:25 +00003839 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003840 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003841 }
3842 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003843 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003844 pch++;
3845 break;
3846 }
3847 return n;
3848}
3849
3850
aliguori376253e2009-03-05 23:01:23 +00003851static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003852{
blueswir1c2efc952007-09-25 17:28:42 +00003853 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003854 int op;
ths3b46e622007-09-17 08:09:54 +00003855
aliguori376253e2009-03-05 23:01:23 +00003856 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003857 for(;;) {
3858 op = *pch;
3859 if (op != '*' && op != '/' && op != '%')
3860 break;
3861 next();
aliguori376253e2009-03-05 23:01:23 +00003862 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003863 switch(op) {
3864 default:
3865 case '*':
3866 val *= val2;
3867 break;
3868 case '/':
3869 case '%':
ths5fafdf22007-09-16 21:08:06 +00003870 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003871 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003872 if (op == '/')
3873 val /= val2;
3874 else
3875 val %= val2;
3876 break;
3877 }
3878 }
3879 return val;
3880}
3881
aliguori376253e2009-03-05 23:01:23 +00003882static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003883{
blueswir1c2efc952007-09-25 17:28:42 +00003884 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003885 int op;
bellard9307c4c2004-04-04 12:57:25 +00003886
aliguori376253e2009-03-05 23:01:23 +00003887 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003888 for(;;) {
3889 op = *pch;
3890 if (op != '&' && op != '|' && op != '^')
3891 break;
3892 next();
aliguori376253e2009-03-05 23:01:23 +00003893 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003894 switch(op) {
3895 default:
3896 case '&':
3897 val &= val2;
3898 break;
3899 case '|':
3900 val |= val2;
3901 break;
3902 case '^':
3903 val ^= val2;
3904 break;
3905 }
3906 }
3907 return val;
3908}
3909
aliguori376253e2009-03-05 23:01:23 +00003910static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003911{
blueswir1c2efc952007-09-25 17:28:42 +00003912 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003913 int op;
bellard9307c4c2004-04-04 12:57:25 +00003914
aliguori376253e2009-03-05 23:01:23 +00003915 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003916 for(;;) {
3917 op = *pch;
3918 if (op != '+' && op != '-')
3919 break;
3920 next();
aliguori376253e2009-03-05 23:01:23 +00003921 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003922 if (op == '+')
3923 val += val2;
3924 else
3925 val -= val2;
3926 }
3927 return val;
3928}
3929
aliguori376253e2009-03-05 23:01:23 +00003930static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003931{
3932 pch = *pp;
3933 if (setjmp(expr_env)) {
3934 *pp = pch;
3935 return -1;
3936 }
blueswir1cd390082008-11-16 13:53:32 +00003937 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003938 pch++;
aliguori376253e2009-03-05 23:01:23 +00003939 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003940 *pp = pch;
3941 return 0;
3942}
3943
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003944static int get_double(Monitor *mon, double *pval, const char **pp)
3945{
3946 const char *p = *pp;
3947 char *tailp;
3948 double d;
3949
3950 d = strtod(p, &tailp);
3951 if (tailp == p) {
3952 monitor_printf(mon, "Number expected\n");
3953 return -1;
3954 }
3955 if (d != d || d - d != 0) {
3956 /* NaN or infinity */
3957 monitor_printf(mon, "Bad number\n");
3958 return -1;
3959 }
3960 *pval = d;
3961 *pp = tailp;
3962 return 0;
3963}
3964
bellard9307c4c2004-04-04 12:57:25 +00003965static int get_str(char *buf, int buf_size, const char **pp)
3966{
3967 const char *p;
3968 char *q;
3969 int c;
3970
bellard81d09122004-07-14 17:21:37 +00003971 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003972 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003973 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003974 p++;
3975 if (*p == '\0') {
3976 fail:
bellard81d09122004-07-14 17:21:37 +00003977 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003978 *pp = p;
3979 return -1;
3980 }
bellard9307c4c2004-04-04 12:57:25 +00003981 if (*p == '\"') {
3982 p++;
3983 while (*p != '\0' && *p != '\"') {
3984 if (*p == '\\') {
3985 p++;
3986 c = *p++;
3987 switch(c) {
3988 case 'n':
3989 c = '\n';
3990 break;
3991 case 'r':
3992 c = '\r';
3993 break;
3994 case '\\':
3995 case '\'':
3996 case '\"':
3997 break;
3998 default:
3999 qemu_printf("unsupported escape code: '\\%c'\n", c);
4000 goto fail;
4001 }
4002 if ((q - buf) < buf_size - 1) {
4003 *q++ = c;
4004 }
4005 } else {
4006 if ((q - buf) < buf_size - 1) {
4007 *q++ = *p;
4008 }
4009 p++;
4010 }
4011 }
4012 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00004013 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00004014 goto fail;
4015 }
4016 p++;
4017 } else {
blueswir1cd390082008-11-16 13:53:32 +00004018 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004019 if ((q - buf) < buf_size - 1) {
4020 *q++ = *p;
4021 }
4022 p++;
4023 }
bellard9307c4c2004-04-04 12:57:25 +00004024 }
bellard81d09122004-07-14 17:21:37 +00004025 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00004026 *pp = p;
4027 return 0;
4028}
4029
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004030/*
4031 * Store the command-name in cmdname, and return a pointer to
4032 * the remaining of the command string.
4033 */
4034static const char *get_command_name(const char *cmdline,
4035 char *cmdname, size_t nlen)
4036{
4037 size_t len;
4038 const char *p, *pstart;
4039
4040 p = cmdline;
4041 while (qemu_isspace(*p))
4042 p++;
4043 if (*p == '\0')
4044 return NULL;
4045 pstart = p;
4046 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
4047 p++;
4048 len = p - pstart;
4049 if (len > nlen - 1)
4050 len = nlen - 1;
4051 memcpy(cmdname, pstart, len);
4052 cmdname[len] = '\0';
4053 return p;
4054}
4055
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004056/**
4057 * Read key of 'type' into 'key' and return the current
4058 * 'type' pointer.
4059 */
4060static char *key_get_info(const char *type, char **key)
4061{
4062 size_t len;
4063 char *p, *str;
4064
4065 if (*type == ',')
4066 type++;
4067
4068 p = strchr(type, ':');
4069 if (!p) {
4070 *key = NULL;
4071 return NULL;
4072 }
4073 len = p - type;
4074
Anthony Liguori7267c092011-08-20 22:09:37 -05004075 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004076 memcpy(str, type, len);
4077 str[len] = '\0';
4078
4079 *key = str;
4080 return ++p;
4081}
4082
bellard9307c4c2004-04-04 12:57:25 +00004083static int default_fmt_format = 'x';
4084static int default_fmt_size = 4;
4085
4086#define MAX_ARGS 16
4087
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004088static int is_valid_option(const char *c, const char *typestr)
4089{
4090 char option[3];
4091
4092 option[0] = '-';
4093 option[1] = *c;
4094 option[2] = '\0';
4095
4096 typestr = strstr(typestr, option);
4097 return (typestr != NULL);
4098}
4099
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004100static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
4101 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004102{
4103 const mon_cmd_t *cmd;
4104
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004105 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004106 if (compare_cmd(cmdname, cmd->name)) {
4107 return cmd;
4108 }
4109 }
4110
4111 return NULL;
4112}
4113
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004114static const mon_cmd_t *monitor_find_command(const char *cmdname)
4115{
4116 return search_dispatch_table(mon_cmds, cmdname);
4117}
4118
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004119static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
4120{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03004121 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004122}
4123
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004124static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
4125{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03004126 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004127}
4128
Anthony Liguoric227f092009-10-01 16:12:16 -05004129static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004130 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004131 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00004132{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004133 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03004134 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05004135 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00004136 char cmdname[256];
4137 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004138 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00004139
4140#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00004141 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00004142#endif
ths3b46e622007-09-17 08:09:54 +00004143
bellard9307c4c2004-04-04 12:57:25 +00004144 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004145 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
4146 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004147 return NULL;
ths3b46e622007-09-17 08:09:54 +00004148
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004149 cmd = monitor_find_command(cmdname);
4150 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004151 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004152 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004153 }
bellard9307c4c2004-04-04 12:57:25 +00004154
bellard9307c4c2004-04-04 12:57:25 +00004155 /* parse the parameters */
4156 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00004157 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004158 typestr = key_get_info(typestr, &key);
4159 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00004160 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004161 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00004162 typestr++;
4163 switch(c) {
4164 case 'F':
bellard81d09122004-07-14 17:21:37 +00004165 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00004166 case 's':
4167 {
4168 int ret;
ths3b46e622007-09-17 08:09:54 +00004169
blueswir1cd390082008-11-16 13:53:32 +00004170 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004171 p++;
4172 if (*typestr == '?') {
4173 typestr++;
4174 if (*p == '\0') {
4175 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03004176 break;
bellard9307c4c2004-04-04 12:57:25 +00004177 }
4178 }
4179 ret = get_str(buf, sizeof(buf), &p);
4180 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00004181 switch(c) {
4182 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004183 monitor_printf(mon, "%s: filename expected\n",
4184 cmdname);
bellard81d09122004-07-14 17:21:37 +00004185 break;
4186 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004187 monitor_printf(mon, "%s: block device name expected\n",
4188 cmdname);
bellard81d09122004-07-14 17:21:37 +00004189 break;
4190 default:
aliguori376253e2009-03-05 23:01:23 +00004191 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004192 break;
4193 }
bellard9307c4c2004-04-04 12:57:25 +00004194 goto fail;
4195 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004196 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004197 }
4198 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004199 case 'O':
4200 {
4201 QemuOptsList *opts_list;
4202 QemuOpts *opts;
4203
4204 opts_list = qemu_find_opts(key);
4205 if (!opts_list || opts_list->desc->name) {
4206 goto bad_type;
4207 }
4208 while (qemu_isspace(*p)) {
4209 p++;
4210 }
4211 if (!*p)
4212 break;
4213 if (get_str(buf, sizeof(buf), &p) < 0) {
4214 goto fail;
4215 }
4216 opts = qemu_opts_parse(opts_list, buf, 1);
4217 if (!opts) {
4218 goto fail;
4219 }
4220 qemu_opts_to_qdict(opts, qdict);
4221 qemu_opts_del(opts);
4222 }
4223 break;
bellard9307c4c2004-04-04 12:57:25 +00004224 case '/':
4225 {
4226 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004227
blueswir1cd390082008-11-16 13:53:32 +00004228 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004229 p++;
4230 if (*p == '/') {
4231 /* format found */
4232 p++;
4233 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004234 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004235 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004236 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004237 count = count * 10 + (*p - '0');
4238 p++;
4239 }
4240 }
4241 size = -1;
4242 format = -1;
4243 for(;;) {
4244 switch(*p) {
4245 case 'o':
4246 case 'd':
4247 case 'u':
4248 case 'x':
4249 case 'i':
4250 case 'c':
4251 format = *p++;
4252 break;
4253 case 'b':
4254 size = 1;
4255 p++;
4256 break;
4257 case 'h':
4258 size = 2;
4259 p++;
4260 break;
4261 case 'w':
4262 size = 4;
4263 p++;
4264 break;
4265 case 'g':
4266 case 'L':
4267 size = 8;
4268 p++;
4269 break;
4270 default:
4271 goto next;
4272 }
4273 }
4274 next:
blueswir1cd390082008-11-16 13:53:32 +00004275 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004276 monitor_printf(mon, "invalid char in format: '%c'\n",
4277 *p);
bellard9307c4c2004-04-04 12:57:25 +00004278 goto fail;
4279 }
bellard9307c4c2004-04-04 12:57:25 +00004280 if (format < 0)
4281 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004282 if (format != 'i') {
4283 /* for 'i', not specifying a size gives -1 as size */
4284 if (size < 0)
4285 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004286 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004287 }
bellard9307c4c2004-04-04 12:57:25 +00004288 default_fmt_format = format;
4289 } else {
4290 count = 1;
4291 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004292 if (format != 'i') {
4293 size = default_fmt_size;
4294 } else {
4295 size = -1;
4296 }
bellard9307c4c2004-04-04 12:57:25 +00004297 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004298 qdict_put(qdict, "count", qint_from_int(count));
4299 qdict_put(qdict, "format", qint_from_int(format));
4300 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004301 }
4302 break;
4303 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004304 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004305 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004306 {
blueswir1c2efc952007-09-25 17:28:42 +00004307 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004308
blueswir1cd390082008-11-16 13:53:32 +00004309 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004310 p++;
bellard34405572004-06-08 00:55:58 +00004311 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004312 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004313 if (*p == '\0') {
4314 typestr++;
4315 break;
4316 }
bellard34405572004-06-08 00:55:58 +00004317 } else {
4318 if (*p == '.') {
4319 p++;
blueswir1cd390082008-11-16 13:53:32 +00004320 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004321 p++;
bellard34405572004-06-08 00:55:58 +00004322 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004323 typestr++;
4324 break;
bellard34405572004-06-08 00:55:58 +00004325 }
4326 }
bellard13224a82006-07-14 22:03:35 +00004327 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004328 }
aliguori376253e2009-03-05 23:01:23 +00004329 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004330 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004331 /* Check if 'i' is greater than 32-bit */
4332 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4333 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4334 monitor_printf(mon, "integer is for 32-bit values\n");
4335 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004336 } else if (c == 'M') {
4337 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004338 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004339 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004340 }
4341 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004342 case 'o':
4343 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004344 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004345 char *end;
4346
4347 while (qemu_isspace(*p)) {
4348 p++;
4349 }
4350 if (*typestr == '?') {
4351 typestr++;
4352 if (*p == '\0') {
4353 break;
4354 }
4355 }
4356 val = strtosz(p, &end);
4357 if (val < 0) {
4358 monitor_printf(mon, "invalid size\n");
4359 goto fail;
4360 }
4361 qdict_put(qdict, key, qint_from_int(val));
4362 p = end;
4363 }
4364 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004365 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004366 {
4367 double val;
4368
4369 while (qemu_isspace(*p))
4370 p++;
4371 if (*typestr == '?') {
4372 typestr++;
4373 if (*p == '\0') {
4374 break;
4375 }
4376 }
4377 if (get_double(mon, &val, &p) < 0) {
4378 goto fail;
4379 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004380 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004381 switch (*p) {
4382 case 'm':
4383 val /= 1e3; p += 2; break;
4384 case 'u':
4385 val /= 1e6; p += 2; break;
4386 case 'n':
4387 val /= 1e9; p += 2; break;
4388 }
4389 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004390 if (*p && !qemu_isspace(*p)) {
4391 monitor_printf(mon, "Unknown unit suffix\n");
4392 goto fail;
4393 }
4394 qdict_put(qdict, key, qfloat_from_double(val));
4395 }
4396 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004397 case 'b':
4398 {
4399 const char *beg;
4400 int val;
4401
4402 while (qemu_isspace(*p)) {
4403 p++;
4404 }
4405 beg = p;
4406 while (qemu_isgraph(*p)) {
4407 p++;
4408 }
4409 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4410 val = 1;
4411 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4412 val = 0;
4413 } else {
4414 monitor_printf(mon, "Expected 'on' or 'off'\n");
4415 goto fail;
4416 }
4417 qdict_put(qdict, key, qbool_from_int(val));
4418 }
4419 break;
bellard9307c4c2004-04-04 12:57:25 +00004420 case '-':
4421 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004422 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004423 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004424 /* option */
ths3b46e622007-09-17 08:09:54 +00004425
bellard9307c4c2004-04-04 12:57:25 +00004426 c = *typestr++;
4427 if (c == '\0')
4428 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004429 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004430 p++;
bellard9307c4c2004-04-04 12:57:25 +00004431 if (*p == '-') {
4432 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004433 if(c != *p) {
4434 if(!is_valid_option(p, typestr)) {
4435
4436 monitor_printf(mon, "%s: unsupported option -%c\n",
4437 cmdname, *p);
4438 goto fail;
4439 } else {
4440 skip_key = 1;
4441 }
bellard9307c4c2004-04-04 12:57:25 +00004442 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004443 if(skip_key) {
4444 p = tmp;
4445 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004446 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004447 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004448 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004449 }
bellard9307c4c2004-04-04 12:57:25 +00004450 }
bellard9307c4c2004-04-04 12:57:25 +00004451 }
4452 break;
4453 default:
4454 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004455 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004456 goto fail;
4457 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004458 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004459 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004460 }
4461 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004462 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004463 p++;
4464 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004465 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4466 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004467 goto fail;
4468 }
4469
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004470 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004471
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004472fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004473 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004474 return NULL;
4475}
4476
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004477void monitor_set_error(Monitor *mon, QError *qerror)
4478{
4479 /* report only the first error */
4480 if (!mon->error) {
4481 mon->error = qerror;
4482 } else {
4483 MON_DEBUG("Additional error report at %s:%d\n",
4484 qerror->file, qerror->linenr);
4485 QDECREF(qerror);
4486 }
4487}
4488
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004489static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4490{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004491 if (ret && !monitor_has_error(mon)) {
4492 /*
4493 * If it returns failure, it must have passed on error.
4494 *
4495 * Action: Report an internal error to the client if in QMP.
4496 */
4497 qerror_report(QERR_UNDEFINED_ERROR);
4498 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4499 cmd->name);
4500 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004501
4502#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004503 if (!ret && monitor_has_error(mon)) {
4504 /*
4505 * If it returns success, it must not have passed an error.
4506 *
4507 * Action: Report the passed error to the client.
4508 */
4509 MON_DEBUG("command '%s' returned success but passed an error\n",
4510 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004511 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004512
4513 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4514 /*
4515 * Handlers should not call Monitor print functions.
4516 *
4517 * Action: Ignore them in QMP.
4518 *
4519 * (XXX: we don't check any 'info' or 'query' command here
4520 * because the user print function _is_ called by do_info(), hence
4521 * we will trigger this check. This problem will go away when we
4522 * make 'query' commands real and kill do_info())
4523 */
4524 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4525 cmd->name, mon_print_count_get(mon));
4526 }
4527#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004528}
4529
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004530static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004531{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004532 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004533 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004534
4535 qdict = qdict_new();
4536
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004537 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004538 if (!cmd)
4539 goto out;
4540
Luiz Capitulino4903de02010-09-16 11:01:32 -03004541 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004542 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004543 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004544 QObject *data = NULL;
4545
4546 /* XXX: ignores the error code */
4547 cmd->mhandler.cmd_new(mon, qdict, &data);
4548 assert(!monitor_has_error(mon));
4549 if (data) {
4550 cmd->user_print(mon, data);
4551 qobject_decref(data);
4552 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004553 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004554 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004555 }
4556
Luiz Capitulino13917be2009-10-07 13:41:54 -03004557out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004558 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004559}
4560
bellard81d09122004-07-14 17:21:37 +00004561static void cmd_completion(const char *name, const char *list)
4562{
4563 const char *p, *pstart;
4564 char cmd[128];
4565 int len;
4566
4567 p = list;
4568 for(;;) {
4569 pstart = p;
4570 p = strchr(p, '|');
4571 if (!p)
4572 p = pstart + strlen(pstart);
4573 len = p - pstart;
4574 if (len > sizeof(cmd) - 2)
4575 len = sizeof(cmd) - 2;
4576 memcpy(cmd, pstart, len);
4577 cmd[len] = '\0';
4578 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004579 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004580 }
4581 if (*p == '\0')
4582 break;
4583 p++;
4584 }
4585}
4586
4587static void file_completion(const char *input)
4588{
4589 DIR *ffs;
4590 struct dirent *d;
4591 char path[1024];
4592 char file[1024], file_prefix[1024];
4593 int input_path_len;
4594 const char *p;
4595
ths5fafdf22007-09-16 21:08:06 +00004596 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004597 if (!p) {
4598 input_path_len = 0;
4599 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004600 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004601 } else {
4602 input_path_len = p - input + 1;
4603 memcpy(path, input, input_path_len);
4604 if (input_path_len > sizeof(path) - 1)
4605 input_path_len = sizeof(path) - 1;
4606 path[input_path_len] = '\0';
4607 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4608 }
4609#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004610 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4611 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004612#endif
4613 ffs = opendir(path);
4614 if (!ffs)
4615 return;
4616 for(;;) {
4617 struct stat sb;
4618 d = readdir(ffs);
4619 if (!d)
4620 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004621
4622 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4623 continue;
4624 }
4625
bellard81d09122004-07-14 17:21:37 +00004626 if (strstart(d->d_name, file_prefix, NULL)) {
4627 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004628 if (input_path_len < sizeof(file))
4629 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4630 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004631 /* stat the file to find out if it's a directory.
4632 * In that case add a slash to speed up typing long paths
4633 */
4634 stat(file, &sb);
4635 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004636 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004637 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004638 }
4639 }
4640 closedir(ffs);
4641}
4642
aliguori51de9762009-03-05 23:00:43 +00004643static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004644{
aliguori51de9762009-03-05 23:00:43 +00004645 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004646 const char *input = opaque;
4647
4648 if (input[0] == '\0' ||
4649 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004650 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004651 }
4652}
4653
4654/* NOTE: this parser is an approximate form of the real command parser */
4655static void parse_cmdline(const char *cmdline,
4656 int *pnb_args, char **args)
4657{
4658 const char *p;
4659 int nb_args, ret;
4660 char buf[1024];
4661
4662 p = cmdline;
4663 nb_args = 0;
4664 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004665 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004666 p++;
4667 if (*p == '\0')
4668 break;
4669 if (nb_args >= MAX_ARGS)
4670 break;
4671 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004672 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004673 nb_args++;
4674 if (ret < 0)
4675 break;
4676 }
4677 *pnb_args = nb_args;
4678}
4679
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004680static const char *next_arg_type(const char *typestr)
4681{
4682 const char *p = strchr(typestr, ':');
4683 return (p != NULL ? ++p : typestr);
4684}
4685
aliguori4c36ba32009-03-05 23:01:37 +00004686static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004687{
4688 const char *cmdname;
4689 char *args[MAX_ARGS];
4690 int nb_args, i, len;
4691 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004692 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004693 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004694
4695 parse_cmdline(cmdline, &nb_args, args);
4696#ifdef DEBUG_COMPLETION
4697 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004698 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004699 }
4700#endif
4701
4702 /* if the line ends with a space, it means we want to complete the
4703 next arg */
4704 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004705 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004706 if (nb_args >= MAX_ARGS) {
4707 goto cleanup;
4708 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004709 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004710 }
4711 if (nb_args <= 1) {
4712 /* command completion */
4713 if (nb_args == 0)
4714 cmdname = "";
4715 else
4716 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004717 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004718 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004719 cmd_completion(cmdname, cmd->name);
4720 }
4721 } else {
4722 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004723 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4724 if (compare_cmd(args[0], cmd->name)) {
4725 break;
4726 }
bellard81d09122004-07-14 17:21:37 +00004727 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004728 if (!cmd->name) {
4729 goto cleanup;
4730 }
4731
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004732 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004733 for(i = 0; i < nb_args - 2; i++) {
4734 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004735 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004736 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004737 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004738 }
4739 }
4740 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004741 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004742 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004743 }
bellard81d09122004-07-14 17:21:37 +00004744 switch(*ptype) {
4745 case 'F':
4746 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004747 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004748 file_completion(str);
4749 break;
4750 case 'B':
4751 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004752 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004753 bdrv_iterate(block_completion_it, (void *)str);
4754 break;
bellard7fe48482004-10-09 18:08:01 +00004755 case 's':
4756 /* XXX: more generic ? */
4757 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004758 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004759 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4760 cmd_completion(str, cmd->name);
4761 }
bellard64866c32006-05-07 18:03:31 +00004762 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004763 char *sep = strrchr(str, '-');
4764 if (sep)
4765 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004766 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004767 for(key = key_defs; key->name != NULL; key++) {
4768 cmd_completion(str, key->name);
4769 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004770 } else if (!strcmp(cmd->name, "help|?")) {
4771 readline_set_completion_index(cur_mon->rs, strlen(str));
4772 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4773 cmd_completion(str, cmd->name);
4774 }
bellard7fe48482004-10-09 18:08:01 +00004775 }
4776 break;
bellard81d09122004-07-14 17:21:37 +00004777 default:
4778 break;
4779 }
4780 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004781
4782cleanup:
4783 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004784 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004785 }
bellard81d09122004-07-14 17:21:37 +00004786}
4787
aliguori731b0362009-03-05 23:01:42 +00004788static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004789{
aliguori731b0362009-03-05 23:01:42 +00004790 Monitor *mon = opaque;
4791
Jan Kiszkac62313b2009-12-04 14:05:29 +01004792 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004793}
4794
Luiz Capitulino09069b12010-02-04 18:10:06 -02004795static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4796{
4797 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4798 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4799}
4800
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004801/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004802 * Argument validation rules:
4803 *
4804 * 1. The argument must exist in cmd_args qdict
4805 * 2. The argument type must be the expected one
4806 *
4807 * Special case: If the argument doesn't exist in cmd_args and
4808 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4809 * checking is skipped for it.
4810 */
4811static int check_client_args_type(const QDict *client_args,
4812 const QDict *cmd_args, int flags)
4813{
4814 const QDictEntry *ent;
4815
4816 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4817 QObject *obj;
4818 QString *arg_type;
4819 const QObject *client_arg = qdict_entry_value(ent);
4820 const char *client_arg_name = qdict_entry_key(ent);
4821
4822 obj = qdict_get(cmd_args, client_arg_name);
4823 if (!obj) {
4824 if (flags & QMP_ACCEPT_UNKNOWNS) {
4825 /* handler accepts unknowns */
4826 continue;
4827 }
4828 /* client arg doesn't exist */
4829 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4830 return -1;
4831 }
4832
4833 arg_type = qobject_to_qstring(obj);
4834 assert(arg_type != NULL);
4835
4836 /* check if argument's type is correct */
4837 switch (qstring_get_str(arg_type)[0]) {
4838 case 'F':
4839 case 'B':
4840 case 's':
4841 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4842 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4843 "string");
4844 return -1;
4845 }
4846 break;
4847 case 'i':
4848 case 'l':
4849 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004850 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004851 if (qobject_type(client_arg) != QTYPE_QINT) {
4852 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4853 "int");
4854 return -1;
4855 }
4856 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004857 case 'T':
4858 if (qobject_type(client_arg) != QTYPE_QINT &&
4859 qobject_type(client_arg) != QTYPE_QFLOAT) {
4860 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4861 "number");
4862 return -1;
4863 }
4864 break;
4865 case 'b':
4866 case '-':
4867 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4868 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4869 "bool");
4870 return -1;
4871 }
4872 break;
4873 case 'O':
4874 assert(flags & QMP_ACCEPT_UNKNOWNS);
4875 break;
4876 case '/':
4877 case '.':
4878 /*
4879 * These types are not supported by QMP and thus are not
4880 * handled here. Fall through.
4881 */
4882 default:
4883 abort();
4884 }
4885 }
4886
4887 return 0;
4888}
4889
4890/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004891 * - Check if the client has passed all mandatory args
4892 * - Set special flags for argument validation
4893 */
4894static int check_mandatory_args(const QDict *cmd_args,
4895 const QDict *client_args, int *flags)
4896{
4897 const QDictEntry *ent;
4898
4899 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4900 const char *cmd_arg_name = qdict_entry_key(ent);
4901 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4902 assert(type != NULL);
4903
4904 if (qstring_get_str(type)[0] == 'O') {
4905 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4906 *flags |= QMP_ACCEPT_UNKNOWNS;
4907 } else if (qstring_get_str(type)[0] != '-' &&
4908 qstring_get_str(type)[1] != '?' &&
4909 !qdict_haskey(client_args, cmd_arg_name)) {
4910 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4911 return -1;
4912 }
4913 }
4914
4915 return 0;
4916}
4917
4918static QDict *qdict_from_args_type(const char *args_type)
4919{
4920 int i;
4921 QDict *qdict;
4922 QString *key, *type, *cur_qs;
4923
4924 assert(args_type != NULL);
4925
4926 qdict = qdict_new();
4927
4928 if (args_type == NULL || args_type[0] == '\0') {
4929 /* no args, empty qdict */
4930 goto out;
4931 }
4932
4933 key = qstring_new();
4934 type = qstring_new();
4935
4936 cur_qs = key;
4937
4938 for (i = 0;; i++) {
4939 switch (args_type[i]) {
4940 case ',':
4941 case '\0':
4942 qdict_put(qdict, qstring_get_str(key), type);
4943 QDECREF(key);
4944 if (args_type[i] == '\0') {
4945 goto out;
4946 }
4947 type = qstring_new(); /* qdict has ref */
4948 cur_qs = key = qstring_new();
4949 break;
4950 case ':':
4951 cur_qs = type;
4952 break;
4953 default:
4954 qstring_append_chr(cur_qs, args_type[i]);
4955 break;
4956 }
4957 }
4958
4959out:
4960 return qdict;
4961}
4962
4963/*
4964 * Client argument checking rules:
4965 *
4966 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004967 * 2. Each argument provided by the client must be expected
4968 * 3. Each argument provided by the client must have the type expected
4969 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004970 */
4971static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4972{
4973 int flags, err;
4974 QDict *cmd_args;
4975
4976 cmd_args = qdict_from_args_type(cmd->args_type);
4977
4978 flags = 0;
4979 err = check_mandatory_args(cmd_args, client_args, &flags);
4980 if (err) {
4981 goto out;
4982 }
4983
Luiz Capitulino4af91932010-06-22 11:44:05 -03004984 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004985
4986out:
4987 QDECREF(cmd_args);
4988 return err;
4989}
4990
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004991/*
4992 * Input object checking rules
4993 *
4994 * 1. Input object must be a dict
4995 * 2. The "execute" key must exist
4996 * 3. The "execute" key must be a string
4997 * 4. If the "arguments" key exists, it must be a dict
4998 * 5. If the "id" key exists, it can be anything (ie. json-value)
4999 * 6. Any argument not listed above is considered invalid
5000 */
5001static QDict *qmp_check_input_obj(QObject *input_obj)
5002{
5003 const QDictEntry *ent;
5004 int has_exec_key = 0;
5005 QDict *input_dict;
5006
5007 if (qobject_type(input_obj) != QTYPE_QDICT) {
5008 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
5009 return NULL;
5010 }
5011
5012 input_dict = qobject_to_qdict(input_obj);
5013
5014 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
5015 const char *arg_name = qdict_entry_key(ent);
5016 const QObject *arg_obj = qdict_entry_value(ent);
5017
5018 if (!strcmp(arg_name, "execute")) {
5019 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
5020 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
5021 "string");
5022 return NULL;
5023 }
5024 has_exec_key = 1;
5025 } else if (!strcmp(arg_name, "arguments")) {
5026 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5027 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5028 "object");
5029 return NULL;
5030 }
5031 } else if (!strcmp(arg_name, "id")) {
5032 /* FIXME: check duplicated IDs for async commands */
5033 } else {
5034 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5035 return NULL;
5036 }
5037 }
5038
5039 if (!has_exec_key) {
5040 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5041 return NULL;
5042 }
5043
5044 return input_dict;
5045}
5046
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005047static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
5048{
5049 QObject *ret_data = NULL;
5050
Luiz Capitulino4903de02010-09-16 11:01:32 -03005051 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005052 qmp_async_info_handler(mon, cmd);
5053 if (monitor_has_error(mon)) {
5054 monitor_protocol_emitter(mon, NULL);
5055 }
5056 } else {
5057 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02005058 monitor_protocol_emitter(mon, ret_data);
5059 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005060 }
5061}
5062
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005063static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5064 const QDict *params)
5065{
5066 int ret;
5067 QObject *data = NULL;
5068
5069 mon_print_count_init(mon);
5070
5071 ret = cmd->mhandler.cmd_new(mon, params, &data);
5072 handler_audit(mon, cmd, ret);
5073 monitor_protocol_emitter(mon, data);
5074 qobject_decref(data);
5075}
5076
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005077static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5078{
5079 int err;
5080 QObject *obj;
5081 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005082 const mon_cmd_t *cmd;
5083 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005084 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005085
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005086 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005087 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005088
5089 obj = json_parser_parse(tokens, NULL);
5090 if (!obj) {
5091 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005092 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005093 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005094 }
5095
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005096 input = qmp_check_input_obj(obj);
5097 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005098 qobject_decref(obj);
5099 goto err_out;
5100 }
5101
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005102 mon->mc->id = qdict_get(input, "id");
5103 qobject_incref(mon->mc->id);
5104
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005105 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02005106 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005107 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005108 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005109 }
5110
Anthony Liguorie3193602011-09-02 12:34:47 -05005111 cmd = qmp_find_cmd(cmd_name);
5112 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005113 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03005114 }
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005115 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005116 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005117 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005118 }
5119
5120 obj = qdict_get(input, "arguments");
5121 if (!obj) {
5122 args = qdict_new();
5123 } else {
5124 args = qobject_to_qdict(obj);
5125 QINCREF(args);
5126 }
5127
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005128 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005129 if (err < 0) {
5130 goto err_out;
5131 }
5132
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005133 if (query_cmd) {
5134 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03005135 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005136 err = qmp_async_cmd_handler(mon, cmd, args);
5137 if (err) {
5138 /* emit the error response */
5139 goto err_out;
5140 }
Adam Litke940cc302010-01-25 12:18:44 -06005141 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005142 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005143 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005145 goto out;
5146
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005147err_out:
5148 monitor_protocol_emitter(mon, NULL);
5149out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005150 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005151 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005152}
5153
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005154/**
5155 * monitor_control_read(): Read and handle QMP input
5156 */
5157static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5158{
5159 Monitor *old_mon = cur_mon;
5160
5161 cur_mon = opaque;
5162
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005163 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005164
5165 cur_mon = old_mon;
5166}
5167
aliguori731b0362009-03-05 23:01:42 +00005168static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005169{
aliguori731b0362009-03-05 23:01:42 +00005170 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005171 int i;
aliguori376253e2009-03-05 23:01:23 +00005172
aliguori731b0362009-03-05 23:01:42 +00005173 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005174
aliguoricde76ee2009-03-05 23:01:51 +00005175 if (cur_mon->rs) {
5176 for (i = 0; i < size; i++)
5177 readline_handle_byte(cur_mon->rs, buf[i]);
5178 } else {
5179 if (size == 0 || buf[size - 1] != 0)
5180 monitor_printf(cur_mon, "corrupted command\n");
5181 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005182 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005183 }
aliguori731b0362009-03-05 23:01:42 +00005184
5185 cur_mon = old_mon;
5186}
aliguorid8f44602008-10-06 13:52:44 +00005187
aliguori376253e2009-03-05 23:01:23 +00005188static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005189{
aliguori731b0362009-03-05 23:01:42 +00005190 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005191 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005192 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005193}
5194
aliguoricde76ee2009-03-05 23:01:51 +00005195int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005196{
aliguoricde76ee2009-03-05 23:01:51 +00005197 if (!mon->rs)
5198 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005199 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005200 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005201}
5202
aliguori376253e2009-03-05 23:01:23 +00005203void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005204{
aliguoricde76ee2009-03-05 23:01:51 +00005205 if (!mon->rs)
5206 return;
aliguori731b0362009-03-05 23:01:42 +00005207 if (--mon->suspend_cnt == 0)
5208 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005209}
5210
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005211static QObject *get_qmp_greeting(void)
5212{
5213 QObject *ver;
5214
5215 do_info_version(NULL, &ver);
5216 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5217}
5218
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005219/**
5220 * monitor_control_event(): Print QMP gretting
5221 */
5222static void monitor_control_event(void *opaque, int event)
5223{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005224 QObject *data;
5225 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005226
Luiz Capitulino47116d12010-02-08 17:01:30 -02005227 switch (event) {
5228 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005229 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005230 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005231 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005232 monitor_json_emitter(mon, data);
5233 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005234 break;
5235 case CHR_EVENT_CLOSED:
5236 json_message_parser_destroy(&mon->mc->parser);
5237 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005238 }
5239}
5240
aliguori731b0362009-03-05 23:01:42 +00005241static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005242{
aliguori376253e2009-03-05 23:01:23 +00005243 Monitor *mon = opaque;
5244
aliguori2724b182009-03-05 23:01:47 +00005245 switch (event) {
5246 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005247 mon->mux_out = 0;
5248 if (mon->reset_seen) {
5249 readline_restart(mon->rs);
5250 monitor_resume(mon);
5251 monitor_flush(mon);
5252 } else {
5253 mon->suspend_cnt = 0;
5254 }
aliguori2724b182009-03-05 23:01:47 +00005255 break;
ths86e94de2007-01-05 22:01:59 +00005256
aliguori2724b182009-03-05 23:01:47 +00005257 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005258 if (mon->reset_seen) {
5259 if (mon->suspend_cnt == 0) {
5260 monitor_printf(mon, "\n");
5261 }
5262 monitor_flush(mon);
5263 monitor_suspend(mon);
5264 } else {
5265 mon->suspend_cnt++;
5266 }
5267 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005268 break;
5269
Amit Shahb6b8df52009-10-07 18:31:16 +05305270 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005271 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5272 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005273 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005274 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005275 }
5276 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005277 break;
5278 }
ths86e94de2007-01-05 22:01:59 +00005279}
5280
aliguori76655d62009-03-06 20:27:37 +00005281
5282/*
5283 * Local variables:
5284 * c-indent-level: 4
5285 * c-basic-offset: 4
5286 * tab-width: 8
5287 * End:
5288 */
5289
aliguori731b0362009-03-05 23:01:42 +00005290void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005291{
aliguori731b0362009-03-05 23:01:42 +00005292 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005293 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005294
5295 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005296 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005297 is_first_init = 0;
5298 }
aliguori87127162009-03-05 23:01:29 +00005299
Anthony Liguori7267c092011-08-20 22:09:37 -05005300 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005301
aliguori87127162009-03-05 23:01:29 +00005302 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005303 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005304 if (flags & MONITOR_USE_READLINE) {
5305 mon->rs = readline_init(mon, monitor_find_completion);
5306 monitor_read_command(mon, 0);
5307 }
aliguori87127162009-03-05 23:01:29 +00005308
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005309 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005310 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005311 /* Control mode requires special handlers */
5312 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5313 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005314 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005315 } else {
5316 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5317 monitor_event, mon);
5318 }
aliguori87127162009-03-05 23:01:29 +00005319
Blue Swirl72cf2d42009-09-12 07:36:22 +00005320 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005321 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5322 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005323}
5324
aliguori376253e2009-03-05 23:01:23 +00005325static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005326{
aliguoribb5fc202009-03-05 23:01:15 +00005327 BlockDriverState *bs = opaque;
5328 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005329
aliguoribb5fc202009-03-05 23:01:15 +00005330 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005331 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005332 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005333 }
aliguori731b0362009-03-05 23:01:42 +00005334 if (mon->password_completion_cb)
5335 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005336
aliguori731b0362009-03-05 23:01:42 +00005337 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005338}
aliguoric0f4ce72009-03-05 23:01:01 +00005339
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005340int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5341 BlockDriverCompletionFunc *completion_cb,
5342 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005343{
aliguoricde76ee2009-03-05 23:01:51 +00005344 int err;
5345
aliguoribb5fc202009-03-05 23:01:15 +00005346 if (!bdrv_key_required(bs)) {
5347 if (completion_cb)
5348 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005349 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005350 }
aliguoric0f4ce72009-03-05 23:01:01 +00005351
Luiz Capitulino94171e12009-12-07 21:37:00 +01005352 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005353 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005354 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005355 }
5356
aliguori376253e2009-03-05 23:01:23 +00005357 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5358 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005359
aliguori731b0362009-03-05 23:01:42 +00005360 mon->password_completion_cb = completion_cb;
5361 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005362
aliguoricde76ee2009-03-05 23:01:51 +00005363 err = monitor_read_password(mon, bdrv_password_cb, bs);
5364
5365 if (err && completion_cb)
5366 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005367
5368 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005369}