blob: 08f6197863861284810652388ffbe2de0bb10380 [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;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200125 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500126} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000127
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100128/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500129typedef struct mon_fd_t mon_fd_t;
130struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100131 char *name;
132 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500133 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100134};
135
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200136typedef struct MonitorControl {
137 QObject *id;
138 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200139 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200140} MonitorControl;
141
aliguori87127162009-03-05 23:01:29 +0000142struct Monitor {
143 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200144 int mux_out;
145 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000146 int flags;
147 int suspend_cnt;
148 uint8_t outbuf[1024];
149 int outbuf_index;
150 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200151 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000152 CPUState *mon_cpu;
153 BlockDriverCompletionFunc *password_completion_cb;
154 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200155#ifdef CONFIG_DEBUG_MONITOR
156 int print_calls_nr;
157#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200158 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500159 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000160 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000161};
162
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200163#ifdef CONFIG_DEBUG_MONITOR
164#define MON_DEBUG(fmt, ...) do { \
165 fprintf(stderr, "Monitor: "); \
166 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200167
168static inline void mon_print_count_inc(Monitor *mon)
169{
170 mon->print_calls_nr++;
171}
172
173static inline void mon_print_count_init(Monitor *mon)
174{
175 mon->print_calls_nr = 0;
176}
177
178static inline int mon_print_count_get(const Monitor *mon)
179{
180 return mon->print_calls_nr;
181}
182
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200183#else /* !CONFIG_DEBUG_MONITOR */
184#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200185static inline void mon_print_count_inc(Monitor *mon) { }
186static inline void mon_print_count_init(Monitor *mon) { }
187static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200188#endif /* CONFIG_DEBUG_MONITOR */
189
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300190/* QMP checker flags */
191#define QMP_ACCEPT_UNKNOWNS 1
192
Blue Swirl72cf2d42009-09-12 07:36:22 +0000193static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000194
Anthony Liguoric227f092009-10-01 16:12:16 -0500195static const mon_cmd_t mon_cmds[];
196static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000197
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300198static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300199static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300200
Markus Armbruster8631b602010-02-18 11:41:55 +0100201Monitor *cur_mon;
202Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000203
aliguori731b0362009-03-05 23:01:42 +0000204static void monitor_command_cb(Monitor *mon, const char *cmdline,
205 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000206
Luiz Capitulino09069b12010-02-04 18:10:06 -0200207static inline int qmp_cmd_mode(const Monitor *mon)
208{
209 return (mon->mc ? mon->mc->command_mode : 0);
210}
211
Luiz Capitulino418173c2009-11-26 22:58:51 -0200212/* Return true if in control mode, false otherwise */
213static inline int monitor_ctrl_mode(const Monitor *mon)
214{
215 return (mon->flags & MONITOR_USE_CONTROL);
216}
217
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100218/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
219int monitor_cur_is_qmp(void)
220{
221 return cur_mon && monitor_ctrl_mode(cur_mon);
222}
223
aliguori731b0362009-03-05 23:01:42 +0000224static void monitor_read_command(Monitor *mon, int show_prompt)
225{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200226 if (!mon->rs)
227 return;
228
aliguori731b0362009-03-05 23:01:42 +0000229 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
230 if (show_prompt)
231 readline_show_prompt(mon->rs);
232}
bellard6a00d602005-11-21 23:25:50 +0000233
aliguoricde76ee2009-03-05 23:01:51 +0000234static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
235 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000236{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100237 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100238 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100239 return -EINVAL;
240 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000241 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
242 /* prompt is printed on return from the command handler */
243 return 0;
244 } else {
245 monitor_printf(mon, "terminal does not support password prompting\n");
246 return -ENOTTY;
247 }
aliguoribb5fc202009-03-05 23:01:15 +0000248}
249
aliguori376253e2009-03-05 23:01:23 +0000250void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000251{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200252 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500253 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000254 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000255 }
256}
257
258/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000259static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000260{
ths60fe76f2007-12-16 03:02:09 +0000261 char c;
aliguori731b0362009-03-05 23:01:42 +0000262
bellard7e2515e2004-08-01 21:52:19 +0000263 for(;;) {
264 c = *str++;
265 if (c == '\0')
266 break;
bellard7ba12602006-07-14 20:26:42 +0000267 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000268 mon->outbuf[mon->outbuf_index++] = '\r';
269 mon->outbuf[mon->outbuf_index++] = c;
270 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
271 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000272 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000273 }
274}
275
aliguori376253e2009-03-05 23:01:23 +0000276void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000277{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200278 char buf[4096];
279
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200280 if (!mon)
281 return;
282
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200283 mon_print_count_inc(mon);
284
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200285 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200286 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200287 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200288
289 vsnprintf(buf, sizeof(buf), fmt, ap);
290 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000291}
292
aliguori376253e2009-03-05 23:01:23 +0000293void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000294{
295 va_list ap;
296 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000297 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000298 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000299}
300
aliguori376253e2009-03-05 23:01:23 +0000301void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000302{
303 int i;
304
305 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000306 switch (filename[i]) {
307 case ' ':
308 case '"':
309 case '\\':
310 monitor_printf(mon, "\\%c", filename[i]);
311 break;
312 case '\t':
313 monitor_printf(mon, "\\t");
314 break;
315 case '\r':
316 monitor_printf(mon, "\\r");
317 break;
318 case '\n':
319 monitor_printf(mon, "\\n");
320 break;
321 default:
322 monitor_printf(mon, "%c", filename[i]);
323 break;
324 }
thsfef30742006-12-22 14:11:32 +0000325 }
326}
327
Stefan Weil8b7968f2010-09-23 21:28:05 +0200328static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
329 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000330{
331 va_list ap;
332 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000333 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000334 va_end(ap);
335 return 0;
336}
337
Luiz Capitulino13c74252009-10-07 13:41:55 -0300338static void monitor_user_noop(Monitor *mon, const QObject *data) { }
339
Luiz Capitulino9e807212010-09-16 10:58:59 -0300340static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300341{
342 return cmd->user_print != NULL;
343}
344
Luiz Capitulino4903de02010-09-16 11:01:32 -0300345static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600346{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200347 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600348}
349
Luiz Capitulino8204a912009-11-18 23:05:31 -0200350static inline int monitor_has_error(const Monitor *mon)
351{
352 return mon->error != NULL;
353}
354
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200355static void monitor_json_emitter(Monitor *mon, const QObject *data)
356{
357 QString *json;
358
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200359 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
360 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200361 assert(json != NULL);
362
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200363 qstring_append_chr(json, '\n');
364 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200365
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200366 QDECREF(json);
367}
368
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200369static void monitor_protocol_emitter(Monitor *mon, QObject *data)
370{
371 QDict *qmp;
372
373 qmp = qdict_new();
374
375 if (!monitor_has_error(mon)) {
376 /* success response */
377 if (data) {
378 qobject_incref(data);
379 qdict_put_obj(qmp, "return", data);
380 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200381 /* return an empty QDict by default */
382 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200383 }
384 } else {
385 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100386 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200387 qdict_put(qmp, "error", mon->error->error);
388 QINCREF(mon->error->error);
389 QDECREF(mon->error);
390 mon->error = NULL;
391 }
392
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200393 if (mon->mc->id) {
394 qdict_put_obj(qmp, "id", mon->mc->id);
395 mon->mc->id = NULL;
396 }
397
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200398 monitor_json_emitter(mon, QOBJECT(qmp));
399 QDECREF(qmp);
400}
401
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200402static void timestamp_put(QDict *qdict)
403{
404 int err;
405 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000406 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200407
Blue Swirld08d6f02009-12-04 18:06:39 +0000408 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409 if (err < 0)
410 return;
411
412 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
413 "'microseconds': %" PRId64 " }",
414 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200415 qdict_put_obj(qdict, "timestamp", obj);
416}
417
418/**
419 * monitor_protocol_event(): Generate a Monitor event
420 *
421 * Event-specific data can be emitted through the (optional) 'data' parameter.
422 */
423void monitor_protocol_event(MonitorEvent event, QObject *data)
424{
425 QDict *qmp;
426 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600427 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200428
Blue Swirl242cd002009-12-04 18:05:45 +0000429 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200430
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200431 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000432 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200433 event_name = "SHUTDOWN";
434 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000435 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200436 event_name = "RESET";
437 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000438 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200439 event_name = "POWERDOWN";
440 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000441 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200442 event_name = "STOP";
443 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300444 case QEVENT_RESUME:
445 event_name = "RESUME";
446 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200447 case QEVENT_VNC_CONNECTED:
448 event_name = "VNC_CONNECTED";
449 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200450 case QEVENT_VNC_INITIALIZED:
451 event_name = "VNC_INITIALIZED";
452 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200453 case QEVENT_VNC_DISCONNECTED:
454 event_name = "VNC_DISCONNECTED";
455 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200456 case QEVENT_BLOCK_IO_ERROR:
457 event_name = "BLOCK_IO_ERROR";
458 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300459 case QEVENT_RTC_CHANGE:
460 event_name = "RTC_CHANGE";
461 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300462 case QEVENT_WATCHDOG:
463 event_name = "WATCHDOG";
464 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200465 case QEVENT_SPICE_CONNECTED:
466 event_name = "SPICE_CONNECTED";
467 break;
468 case QEVENT_SPICE_INITIALIZED:
469 event_name = "SPICE_INITIALIZED";
470 break;
471 case QEVENT_SPICE_DISCONNECTED:
472 event_name = "SPICE_DISCONNECTED";
473 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200474 default:
475 abort();
476 break;
477 }
478
479 qmp = qdict_new();
480 timestamp_put(qmp);
481 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200482 if (data) {
483 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200484 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200485 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200486
Adam Litkef039a562010-01-15 08:34:02 -0600487 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200488 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200489 monitor_json_emitter(mon, QOBJECT(qmp));
490 }
Adam Litkef039a562010-01-15 08:34:02 -0600491 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200492 QDECREF(qmp);
493}
494
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200495static int do_qmp_capabilities(Monitor *mon, const QDict *params,
496 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200497{
498 /* Will setup QMP capabilities in the future */
499 if (monitor_ctrl_mode(mon)) {
500 mon->mc->command_mode = 1;
501 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200502
503 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200504}
505
Luiz Capitulino0268d972010-10-22 10:08:02 -0200506static int mon_set_cpu(int cpu_index);
507static void handle_user_command(Monitor *mon, const char *cmdline);
508
509static int do_hmp_passthrough(Monitor *mon, const QDict *params,
510 QObject **ret_data)
511{
512 int ret = 0;
513 Monitor *old_mon, hmp;
514 CharDriverState mchar;
515
516 memset(&hmp, 0, sizeof(hmp));
517 qemu_chr_init_mem(&mchar);
518 hmp.chr = &mchar;
519
520 old_mon = cur_mon;
521 cur_mon = &hmp;
522
523 if (qdict_haskey(params, "cpu-index")) {
524 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
525 if (ret < 0) {
526 cur_mon = old_mon;
527 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
528 goto out;
529 }
530 }
531
532 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
533 cur_mon = old_mon;
534
535 if (qemu_chr_mem_osize(hmp.chr) > 0) {
536 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
537 }
538
539out:
540 qemu_chr_close_mem(hmp.chr);
541 return ret;
542}
543
bellard9dc39cb2004-03-14 21:38:27 +0000544static int compare_cmd(const char *name, const char *list)
545{
546 const char *p, *pstart;
547 int len;
548 len = strlen(name);
549 p = list;
550 for(;;) {
551 pstart = p;
552 p = strchr(p, '|');
553 if (!p)
554 p = pstart + strlen(pstart);
555 if ((p - pstart) == len && !memcmp(pstart, name, len))
556 return 1;
557 if (*p == '\0')
558 break;
559 p++;
560 }
561 return 0;
562}
563
Anthony Liguoric227f092009-10-01 16:12:16 -0500564static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000565 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000566{
Anthony Liguoric227f092009-10-01 16:12:16 -0500567 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000568
569 for(cmd = cmds; cmd->name != NULL; cmd++) {
570 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000571 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
572 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000573 }
574}
575
aliguori376253e2009-03-05 23:01:23 +0000576static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000577{
578 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000579 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000580 } else {
aliguori376253e2009-03-05 23:01:23 +0000581 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000582 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000583 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000584 monitor_printf(mon, "Log items (comma separated):\n");
585 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000586 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000587 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000588 }
589 }
bellard9dc39cb2004-03-14 21:38:27 +0000590 }
591}
592
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300593static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300594{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300595 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300596}
597
Lluísfc764102011-08-31 20:31:18 +0200598static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530599{
600 const char *tp_name = qdict_get_str(qdict, "name");
601 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200602 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000603
604 if (!ret) {
605 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
606 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530607}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100608
Michael Rothc45a8162011-10-02 08:44:37 -0500609#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100610static void do_trace_file(Monitor *mon, const QDict *qdict)
611{
612 const char *op = qdict_get_try_str(qdict, "op");
613 const char *arg = qdict_get_try_str(qdict, "arg");
614
615 if (!op) {
616 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
617 } else if (!strcmp(op, "on")) {
618 st_set_trace_file_enabled(true);
619 } else if (!strcmp(op, "off")) {
620 st_set_trace_file_enabled(false);
621 } else if (!strcmp(op, "flush")) {
622 st_flush_trace_buffer();
623 } else if (!strcmp(op, "set")) {
624 if (arg) {
625 st_set_trace_file(arg);
626 }
627 } else {
628 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
629 help_cmd(mon, "trace-file");
630 }
631}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530632#endif
633
Adam Litke940cc302010-01-25 12:18:44 -0600634static void user_monitor_complete(void *opaque, QObject *ret_data)
635{
636 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
637
638 if (ret_data) {
639 data->user_print(data->mon, ret_data);
640 }
641 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500642 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600643}
644
645static void qmp_monitor_complete(void *opaque, QObject *ret_data)
646{
647 monitor_protocol_emitter(opaque, ret_data);
648}
649
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300650static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
651 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600652{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300653 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600654}
655
656static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
657{
658 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
659}
660
661static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
662 const QDict *params)
663{
664 int ret;
665
Anthony Liguori7267c092011-08-20 22:09:37 -0500666 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600667 cb_data->mon = mon;
668 cb_data->user_print = cmd->user_print;
669 monitor_suspend(mon);
670 ret = cmd->mhandler.cmd_async(mon, params,
671 user_monitor_complete, cb_data);
672 if (ret < 0) {
673 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500674 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600675 }
676}
677
678static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
679{
680 int ret;
681
Anthony Liguori7267c092011-08-20 22:09:37 -0500682 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600683 cb_data->mon = mon;
684 cb_data->user_print = cmd->user_print;
685 monitor_suspend(mon);
686 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
687 if (ret < 0) {
688 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500689 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600690 }
691}
692
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300693static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000694{
Anthony Liguoric227f092009-10-01 16:12:16 -0500695 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300696 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000697
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200698 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000699 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200700 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300701
702 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000703 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300704 break;
bellard9dc39cb2004-03-14 21:38:27 +0000705 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300706
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200707 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300708 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200709 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300710
Luiz Capitulino4903de02010-09-16 11:01:32 -0300711 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300712 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300713 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300714 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200715
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300716 cmd->mhandler.info_new(mon, &info_data);
717 if (info_data) {
718 cmd->user_print(mon, info_data);
719 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200720 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300721 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300722 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300723 }
724
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300725 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300726
727help:
728 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000729}
730
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200731static void do_info_version_print(Monitor *mon, const QObject *data)
732{
733 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300734 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200735
736 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300737 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200738
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300739 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
740 qdict_get_int(qemu, "major"),
741 qdict_get_int(qemu, "minor"),
742 qdict_get_int(qemu, "micro"),
743 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200744}
745
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300746static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000747{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300748 const char *version = QEMU_VERSION;
749 int major = 0, minor = 0, micro = 0;
750 char *tmp;
751
752 major = strtol(version, &tmp, 10);
753 tmp++;
754 minor = strtol(tmp, &tmp, 10);
755 tmp++;
756 micro = strtol(tmp, &tmp, 10);
757
758 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
759 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000760}
761
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200762static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000763{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200764 QDict *qdict;
765
766 qdict = qobject_to_qdict(data);
767 if (qdict_size(qdict) == 0) {
768 return;
769 }
770
771 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
772}
773
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200774static void do_info_name(Monitor *mon, QObject **ret_data)
775{
776 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
777 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000778}
779
Luiz Capitulino1a728672009-12-10 17:15:56 -0200780static QObject *get_cmd_dict(const char *name)
781{
782 const char *p;
783
784 /* Remove '|' from some commands */
785 p = strchr(name, '|');
786 if (p) {
787 p++;
788 } else {
789 p = name;
790 }
791
792 return qobject_from_jsonf("{ 'name': %s }", p);
793}
794
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200795static void do_info_commands(Monitor *mon, QObject **ret_data)
796{
797 QList *cmd_list;
798 const mon_cmd_t *cmd;
799
800 cmd_list = qlist_new();
801
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300802 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300803 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200804 }
805
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300806 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300807 char buf[128];
808 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
809 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200810 }
811
812 *ret_data = QOBJECT(cmd_list);
813}
814
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200815static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000816{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200817 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
818}
819
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200820static void do_info_uuid(Monitor *mon, QObject **ret_data)
821{
822 char uuid[64];
823
824 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000825 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
826 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
827 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
828 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200829 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000830}
831
bellard6a00d602005-11-21 23:25:50 +0000832/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000833static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000834{
835 CPUState *env;
836
837 for(env = first_cpu; env != NULL; env = env->next_cpu) {
838 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000839 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000840 return 0;
841 }
842 }
843 return -1;
844}
845
pbrook9596ebb2007-11-18 01:44:38 +0000846static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000847{
aliguori731b0362009-03-05 23:01:42 +0000848 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000849 mon_set_cpu(0);
850 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300851 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000852 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000853}
854
aliguori376253e2009-03-05 23:01:23 +0000855static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000856{
bellard6a00d602005-11-21 23:25:50 +0000857 CPUState *env;
858 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000859#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000860 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000861 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000862#else
aliguori376253e2009-03-05 23:01:23 +0000863 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000864 0);
bellard9307c4c2004-04-04 12:57:25 +0000865#endif
866}
867
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300868static void print_cpu_iter(QObject *obj, void *opaque)
869{
870 QDict *cpu;
871 int active = ' ';
872 Monitor *mon = opaque;
873
874 assert(qobject_type(obj) == QTYPE_QDICT);
875 cpu = qobject_to_qdict(obj);
876
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200879 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880
881 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
882
883#if defined(TARGET_I386)
884 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
885 (target_ulong) qdict_get_int(cpu, "pc"));
886#elif defined(TARGET_PPC)
887 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
888 (target_long) qdict_get_int(cpu, "nip"));
889#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500890 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300891 (target_long) qdict_get_int(cpu, "pc"));
892 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
893 (target_long) qdict_get_int(cpu, "npc"));
894#elif defined(TARGET_MIPS)
895 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
896 (target_long) qdict_get_int(cpu, "PC"));
897#endif
898
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200899 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300900 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200901 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300902
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100903 monitor_printf(mon, " thread_id=%" PRId64 " ",
904 qdict_get_int(cpu, "thread_id"));
905
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300906 monitor_printf(mon, "\n");
907}
908
909static void monitor_print_cpus(Monitor *mon, const QObject *data)
910{
911 QList *cpu_list;
912
913 assert(qobject_type(data) == QTYPE_QLIST);
914 cpu_list = qobject_to_qlist(data);
915 qlist_iter(cpu_list, print_cpu_iter, mon);
916}
917
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300918static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000919{
920 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300921 QList *cpu_list;
922
923 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000924
925 /* just to set the default cpu if not already done */
926 mon_get_cpu();
927
928 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200929 QDict *cpu;
930 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300931
Avi Kivity4c0960c2009-08-17 23:19:53 +0300932 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300933
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200934 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
935 env->cpu_index, env == mon->mon_cpu,
936 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200937
938 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300939
bellard6a00d602005-11-21 23:25:50 +0000940#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300941 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000942#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300943 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000944#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300945 qdict_put(cpu, "pc", qint_from_int(env->pc));
946 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000947#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300948 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000949#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100950 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300951
952 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000953 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300954
955 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000956}
957
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200958static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000959{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300960 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200961 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100962 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
963 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200964 return -1;
965 }
966 return 0;
bellard6a00d602005-11-21 23:25:50 +0000967}
968
aliguori376253e2009-03-05 23:01:23 +0000969static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000970{
aliguori376253e2009-03-05 23:01:23 +0000971 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000972}
973
aliguori376253e2009-03-05 23:01:23 +0000974static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000975{
976 int i;
bellard7e2515e2004-08-01 21:52:19 +0000977 const char *str;
ths3b46e622007-09-17 08:09:54 +0000978
aliguoricde76ee2009-03-05 23:01:51 +0000979 if (!mon->rs)
980 return;
bellard7e2515e2004-08-01 21:52:19 +0000981 i = 0;
982 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000983 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000984 if (!str)
985 break;
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000987 i++;
bellardaa455482004-04-04 13:07:25 +0000988 }
989}
990
j_mayer76a66252007-03-07 08:32:30 +0000991#if defined(TARGET_PPC)
992/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000993static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000994{
995 CPUState *env;
996
997 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000998 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000999}
1000#endif
1001
Lluís6d8a7642011-08-31 20:30:43 +02001002#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301003static void do_info_trace(Monitor *mon)
1004{
1005 st_print_trace((FILE *)mon, &monitor_fprintf);
1006}
Lluís31965ae2011-08-31 20:31:24 +02001007#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05301008
Lluísfc764102011-08-31 20:31:18 +02001009static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301010{
Lluísfc764102011-08-31 20:31:18 +02001011 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301012}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301013
Luiz Capitulinob223f352009-10-07 13:41:56 -03001014/**
1015 * do_quit(): Quit QEMU execution
1016 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001017static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001018{
Luiz Capitulino39b59d22010-05-11 18:08:20 -03001019 monitor_suspend(mon);
1020 no_shutdown = 0;
1021 qemu_system_shutdown_request();
1022
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001023 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001024}
1025
Jes Sorensen821601e2011-03-16 13:33:36 +01001026#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001028{
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001029 if (!password || !password[0]) {
1030 if (vnc_display_disable_login(NULL)) {
1031 qerror_report(QERR_SET_PASSWD_FAILED);
1032 return -1;
1033 }
1034 return 0;
1035 }
1036
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001037 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001038 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001039 return -1;
1040 }
aliguoribb5fc202009-03-05 23:01:15 +00001041
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001042 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001043}
1044
1045static void change_vnc_password_cb(Monitor *mon, const char *password,
1046 void *opaque)
1047{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001048 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001049 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001050}
1051
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001052static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001053{
ths70848512007-08-25 01:37:05 +00001054 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001055 strcmp(target, "password") == 0) {
1056 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001057 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001058 strncpy(password, arg, sizeof(password));
1059 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001060 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001061 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001062 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001063 }
ths70848512007-08-25 01:37:05 +00001064 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001065 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001066 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001067 return -1;
1068 }
ths70848512007-08-25 01:37:05 +00001069 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001070
1071 return 0;
thse25a5822007-08-25 01:36:20 +00001072}
Jes Sorensen821601e2011-03-16 13:33:36 +01001073#else
1074static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1075{
1076 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1077 return -ENODEV;
1078}
1079#endif
thse25a5822007-08-25 01:36:20 +00001080
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001081/**
1082 * do_change(): Change a removable medium, or VNC configuration
1083 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001084static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001085{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001086 const char *device = qdict_get_str(qdict, "device");
1087 const char *target = qdict_get_str(qdict, "target");
1088 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001089 int ret;
1090
thse25a5822007-08-25 01:36:20 +00001091 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001092 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001093 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001094 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001095 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001096
1097 return ret;
thse25a5822007-08-25 01:36:20 +00001098}
1099
Gerd Hoffmann75721502010-10-07 12:22:54 +02001100static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1101{
1102 const char *protocol = qdict_get_str(qdict, "protocol");
1103 const char *password = qdict_get_str(qdict, "password");
1104 const char *connected = qdict_get_try_str(qdict, "connected");
1105 int disconnect_if_connected = 0;
1106 int fail_if_connected = 0;
1107 int rc;
1108
1109 if (connected) {
1110 if (strcmp(connected, "fail") == 0) {
1111 fail_if_connected = 1;
1112 } else if (strcmp(connected, "disconnect") == 0) {
1113 disconnect_if_connected = 1;
1114 } else if (strcmp(connected, "keep") == 0) {
1115 /* nothing */
1116 } else {
1117 qerror_report(QERR_INVALID_PARAMETER, "connected");
1118 return -1;
1119 }
1120 }
1121
1122 if (strcmp(protocol, "spice") == 0) {
1123 if (!using_spice) {
1124 /* correct one? spice isn't a device ,,, */
1125 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1126 return -1;
1127 }
1128 rc = qemu_spice_set_passwd(password, fail_if_connected,
1129 disconnect_if_connected);
1130 if (rc != 0) {
1131 qerror_report(QERR_SET_PASSWD_FAILED);
1132 return -1;
1133 }
1134 return 0;
1135 }
1136
1137 if (strcmp(protocol, "vnc") == 0) {
1138 if (fail_if_connected || disconnect_if_connected) {
1139 /* vnc supports "connected=keep" only */
1140 qerror_report(QERR_INVALID_PARAMETER, "connected");
1141 return -1;
1142 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001143 /* Note that setting an empty password will not disable login through
1144 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001145 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001146 }
1147
1148 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1149 return -1;
1150}
1151
1152static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1153{
1154 const char *protocol = qdict_get_str(qdict, "protocol");
1155 const char *whenstr = qdict_get_str(qdict, "time");
1156 time_t when;
1157 int rc;
1158
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001159 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001160 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001161 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001162 when = TIME_MAX;
1163 } else if (whenstr[0] == '+') {
1164 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1165 } else {
1166 when = strtoull(whenstr, NULL, 10);
1167 }
1168
1169 if (strcmp(protocol, "spice") == 0) {
1170 if (!using_spice) {
1171 /* correct one? spice isn't a device ,,, */
1172 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1173 return -1;
1174 }
1175 rc = qemu_spice_set_pw_expire(when);
1176 if (rc != 0) {
1177 qerror_report(QERR_SET_PASSWD_FAILED);
1178 return -1;
1179 }
1180 return 0;
1181 }
1182
1183 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001184 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001185 }
1186
1187 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1188 return -1;
1189}
1190
Daniel P. Berrange13661082011-06-23 13:31:42 +01001191static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1192{
1193 const char *protocol = qdict_get_str(qdict, "protocol");
1194 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001195 CharDriverState *s;
1196
1197 if (strcmp(protocol, "spice") == 0) {
1198 if (!using_spice) {
1199 /* correct one? spice isn't a device ,,, */
1200 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1201 return -1;
1202 }
1203 qerror_report(QERR_ADD_CLIENT_FAILED);
1204 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001205#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001206 } else if (strcmp(protocol, "vnc") == 0) {
1207 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001208 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001209 vnc_display_add_client(NULL, fd, skipauth);
1210 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001211#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001212 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1213 int fd = monitor_get_fd(mon, fdname);
1214 if (qemu_chr_add_client(s, fd) < 0) {
1215 qerror_report(QERR_ADD_CLIENT_FAILED);
1216 return -1;
1217 }
1218 return 0;
1219 }
1220
1221 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1222 return -1;
1223}
1224
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001225static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1226{
1227 const char *protocol = qdict_get_str(qdict, "protocol");
1228 const char *hostname = qdict_get_str(qdict, "hostname");
1229 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1230 int port = qdict_get_try_int(qdict, "port", -1);
1231 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1232 int ret;
1233
1234 if (strcmp(protocol, "spice") == 0) {
1235 if (!using_spice) {
1236 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1237 return -1;
1238 }
1239
1240 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1241 if (ret != 0) {
1242 qerror_report(QERR_UNDEFINED_ERROR);
1243 return -1;
1244 }
1245 return 0;
1246 }
1247
1248 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1249 return -1;
1250}
1251
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001252static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001253{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001254 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001255 return 0;
bellard59a983b2004-03-17 23:17:16 +00001256}
1257
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001258static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001259{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001260 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001261}
1262
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001263static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001264{
1265 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001266 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001267
bellard9307c4c2004-04-04 12:57:25 +00001268 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001269 mask = 0;
1270 } else {
bellard9307c4c2004-04-04 12:57:25 +00001271 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001272 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001273 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001274 return;
1275 }
1276 }
1277 cpu_set_log(mask);
1278}
1279
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001280static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001281{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001282 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001283 if (!option || !strcmp(option, "on")) {
1284 singlestep = 1;
1285 } else if (!strcmp(option, "off")) {
1286 singlestep = 0;
1287 } else {
1288 monitor_printf(mon, "unexpected option %s\n", option);
1289 }
1290}
1291
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001292/**
1293 * do_stop(): Stop VM execution
1294 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001295static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001296{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001297 vm_stop(RSTATE_PAUSED);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001298 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001299}
1300
aliguoribb5fc202009-03-05 23:01:15 +00001301static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001302
aliguori376253e2009-03-05 23:01:23 +00001303struct bdrv_iterate_context {
1304 Monitor *mon;
1305 int err;
1306};
aliguoric0f4ce72009-03-05 23:01:01 +00001307
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001308/**
1309 * do_cont(): Resume emulation.
1310 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001311static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001312{
1313 struct bdrv_iterate_context context = { mon, 0 };
1314
Luiz Capitulino1bcef682011-07-29 15:15:00 -03001315 if (runstate_check(RSTATE_IN_MIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301316 qerror_report(QERR_MIGRATION_EXPECTED);
1317 return -1;
Luiz Capitulino6667b232011-07-29 15:57:54 -03001318 } else if (runstate_check(RSTATE_PANICKED) ||
1319 runstate_check(RSTATE_SHUTDOWN)) {
1320 qerror_report(QERR_RESET_REQUIRED);
1321 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301322 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001323
aliguori376253e2009-03-05 23:01:23 +00001324 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001325 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001326 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001327 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001328 return 0;
1329 } else {
1330 return -1;
1331 }
bellard8a7ddc32004-03-31 19:00:16 +00001332}
1333
aliguoribb5fc202009-03-05 23:01:15 +00001334static void bdrv_key_cb(void *opaque, int err)
1335{
aliguori376253e2009-03-05 23:01:23 +00001336 Monitor *mon = opaque;
1337
aliguoribb5fc202009-03-05 23:01:15 +00001338 /* another key was set successfully, retry to continue */
1339 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001340 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001341}
1342
1343static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1344{
aliguori376253e2009-03-05 23:01:23 +00001345 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001346
aliguori376253e2009-03-05 23:01:23 +00001347 if (!context->err && bdrv_key_required(bs)) {
1348 context->err = -EBUSY;
1349 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1350 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001351 }
1352}
1353
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001354static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001355{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001356 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001357 if (!device)
1358 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1359 if (gdbserver_start(device) < 0) {
1360 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1361 device);
1362 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001363 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001364 } else {
aliguori59030a82009-04-05 18:43:41 +00001365 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1366 device);
bellard8a7ddc32004-03-31 19:00:16 +00001367 }
1368}
1369
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001370static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001371{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001372 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001373 if (select_watchdog_action(action) == -1) {
1374 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1375 }
1376}
1377
aliguori376253e2009-03-05 23:01:23 +00001378static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001379{
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001381 switch(c) {
1382 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 default:
1395 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001397 } else {
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001399 }
1400 break;
1401 }
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001403}
1404
aliguori376253e2009-03-05 23:01:23 +00001405static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001406 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001407{
bellard6a00d602005-11-21 23:25:50 +00001408 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001409 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001410 uint8_t buf[16];
1411 uint64_t v;
1412
1413 if (format == 'i') {
1414 int flags;
1415 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001416 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001417#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001418 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001419 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001420 } else if (wsize == 4) {
1421 flags = 0;
1422 } else {
bellard6a15fd12006-04-12 21:07:07 +00001423 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001424 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001425 if (env) {
1426#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001427 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001428 (env->segs[R_CS].flags & DESC_L_MASK))
1429 flags = 2;
1430 else
1431#endif
1432 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1433 flags = 1;
1434 }
bellard4c27ba22004-04-25 18:05:08 +00001435 }
1436#endif
aliguori376253e2009-03-05 23:01:23 +00001437 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001438 return;
1439 }
1440
1441 len = wsize * count;
1442 if (wsize == 1)
1443 line_size = 8;
1444 else
1445 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001446 max_digits = 0;
1447
1448 switch(format) {
1449 case 'o':
1450 max_digits = (wsize * 8 + 2) / 3;
1451 break;
1452 default:
1453 case 'x':
1454 max_digits = (wsize * 8) / 4;
1455 break;
1456 case 'u':
1457 case 'd':
1458 max_digits = (wsize * 8 * 10 + 32) / 33;
1459 break;
1460 case 'c':
1461 wsize = 1;
1462 break;
1463 }
1464
1465 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001466 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001467 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001468 else
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001470 l = len;
1471 if (l > line_size)
1472 l = line_size;
1473 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001474 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001475 } else {
bellard6a00d602005-11-21 23:25:50 +00001476 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001477 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001478 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001479 break;
1480 }
bellard9307c4c2004-04-04 12:57:25 +00001481 }
ths5fafdf22007-09-16 21:08:06 +00001482 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001483 while (i < l) {
1484 switch(wsize) {
1485 default:
1486 case 1:
1487 v = ldub_raw(buf + i);
1488 break;
1489 case 2:
1490 v = lduw_raw(buf + i);
1491 break;
1492 case 4:
bellard92a31b12005-02-10 22:00:52 +00001493 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001494 break;
1495 case 8:
1496 v = ldq_raw(buf + i);
1497 break;
1498 }
aliguori376253e2009-03-05 23:01:23 +00001499 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001500 switch(format) {
1501 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001503 break;
1504 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001505 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001506 break;
1507 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001509 break;
1510 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001512 break;
1513 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001514 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001515 break;
1516 }
1517 i += wsize;
1518 }
aliguori376253e2009-03-05 23:01:23 +00001519 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001520 addr += l;
1521 len -= l;
1522 }
1523}
1524
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001525static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001526{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001527 int count = qdict_get_int(qdict, "count");
1528 int format = qdict_get_int(qdict, "format");
1529 int size = qdict_get_int(qdict, "size");
1530 target_long addr = qdict_get_int(qdict, "addr");
1531
aliguori376253e2009-03-05 23:01:23 +00001532 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001533}
1534
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001535static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001536{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001537 int count = qdict_get_int(qdict, "count");
1538 int format = qdict_get_int(qdict, "format");
1539 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001540 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001541
aliguori376253e2009-03-05 23:01:23 +00001542 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001543}
1544
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001545static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001546{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001547 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001548 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001549
blueswir17743e582007-09-24 18:39:04 +00001550#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001551 switch(format) {
1552 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001553 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001554 break;
1555 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001556 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001557 break;
1558 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001559 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001560 break;
1561 default:
1562 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001563 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001564 break;
1565 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001567 break;
1568 }
bellard92a31b12005-02-10 22:00:52 +00001569#else
1570 switch(format) {
1571 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001572 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001573 break;
1574 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001575 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001576 break;
1577 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001578 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001579 break;
1580 default:
1581 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001582 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001583 break;
1584 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001585 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001586 break;
1587 }
1588#endif
aliguori376253e2009-03-05 23:01:23 +00001589 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001590}
1591
Luiz Capitulino98696222010-02-10 23:49:58 -02001592static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001593{
1594 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001595 uint32_t size = qdict_get_int(qdict, "size");
1596 const char *filename = qdict_get_str(qdict, "filename");
1597 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001598 uint32_t l;
1599 CPUState *env;
1600 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001601 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001602
1603 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001604
1605 f = fopen(filename, "wb");
1606 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001607 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001608 return -1;
bellardb371dc52007-01-03 15:20:39 +00001609 }
1610 while (size != 0) {
1611 l = sizeof(buf);
1612 if (l > size)
1613 l = size;
1614 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001615 if (fwrite(buf, 1, l, f) != l) {
1616 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1617 goto exit;
1618 }
bellardb371dc52007-01-03 15:20:39 +00001619 addr += l;
1620 size -= l;
1621 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001622
1623 ret = 0;
1624
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001625exit:
bellardb371dc52007-01-03 15:20:39 +00001626 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001627 return ret;
bellardb371dc52007-01-03 15:20:39 +00001628}
1629
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001630static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001631 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001632{
1633 FILE *f;
1634 uint32_t l;
1635 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001636 uint32_t size = qdict_get_int(qdict, "size");
1637 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001638 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001639 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001640
1641 f = fopen(filename, "wb");
1642 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001643 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001644 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001645 }
1646 while (size != 0) {
1647 l = sizeof(buf);
1648 if (l > size)
1649 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001650 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001651 if (fwrite(buf, 1, l, f) != l) {
1652 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1653 goto exit;
1654 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001655 fflush(f);
1656 addr += l;
1657 size -= l;
1658 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001659
1660 ret = 0;
1661
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001662exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001663 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001664 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001665}
1666
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001667static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001668{
1669 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001670 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001671 uint32_t start = qdict_get_int(qdict, "start");
1672 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001673
1674 sum = 0;
1675 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001676 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001677 /* BSD sum algorithm ('sum' Unix command) */
1678 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001679 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001680 }
aliguori376253e2009-03-05 23:01:23 +00001681 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001682}
1683
bellarda3a91a32004-06-04 11:06:21 +00001684typedef struct {
1685 int keycode;
1686 const char *name;
1687} KeyDef;
1688
1689static const KeyDef key_defs[] = {
1690 { 0x2a, "shift" },
1691 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001692
bellarda3a91a32004-06-04 11:06:21 +00001693 { 0x38, "alt" },
1694 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001695 { 0x64, "altgr" },
1696 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001697 { 0x1d, "ctrl" },
1698 { 0x9d, "ctrl_r" },
1699
1700 { 0xdd, "menu" },
1701
1702 { 0x01, "esc" },
1703
1704 { 0x02, "1" },
1705 { 0x03, "2" },
1706 { 0x04, "3" },
1707 { 0x05, "4" },
1708 { 0x06, "5" },
1709 { 0x07, "6" },
1710 { 0x08, "7" },
1711 { 0x09, "8" },
1712 { 0x0a, "9" },
1713 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001714 { 0x0c, "minus" },
1715 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001716 { 0x0e, "backspace" },
1717
1718 { 0x0f, "tab" },
1719 { 0x10, "q" },
1720 { 0x11, "w" },
1721 { 0x12, "e" },
1722 { 0x13, "r" },
1723 { 0x14, "t" },
1724 { 0x15, "y" },
1725 { 0x16, "u" },
1726 { 0x17, "i" },
1727 { 0x18, "o" },
1728 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001729 { 0x1a, "bracket_left" },
1730 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001731 { 0x1c, "ret" },
1732
1733 { 0x1e, "a" },
1734 { 0x1f, "s" },
1735 { 0x20, "d" },
1736 { 0x21, "f" },
1737 { 0x22, "g" },
1738 { 0x23, "h" },
1739 { 0x24, "j" },
1740 { 0x25, "k" },
1741 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001742 { 0x27, "semicolon" },
1743 { 0x28, "apostrophe" },
1744 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001745
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001746 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001747 { 0x2c, "z" },
1748 { 0x2d, "x" },
1749 { 0x2e, "c" },
1750 { 0x2f, "v" },
1751 { 0x30, "b" },
1752 { 0x31, "n" },
1753 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001754 { 0x33, "comma" },
1755 { 0x34, "dot" },
1756 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001757
balrog4d3b6f62008-02-10 16:33:14 +00001758 { 0x37, "asterisk" },
1759
bellarda3a91a32004-06-04 11:06:21 +00001760 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001761 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001762 { 0x3b, "f1" },
1763 { 0x3c, "f2" },
1764 { 0x3d, "f3" },
1765 { 0x3e, "f4" },
1766 { 0x3f, "f5" },
1767 { 0x40, "f6" },
1768 { 0x41, "f7" },
1769 { 0x42, "f8" },
1770 { 0x43, "f9" },
1771 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001772 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001773 { 0x46, "scroll_lock" },
1774
bellard64866c32006-05-07 18:03:31 +00001775 { 0xb5, "kp_divide" },
1776 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001777 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001778 { 0x4e, "kp_add" },
1779 { 0x9c, "kp_enter" },
1780 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001781 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001782
1783 { 0x52, "kp_0" },
1784 { 0x4f, "kp_1" },
1785 { 0x50, "kp_2" },
1786 { 0x51, "kp_3" },
1787 { 0x4b, "kp_4" },
1788 { 0x4c, "kp_5" },
1789 { 0x4d, "kp_6" },
1790 { 0x47, "kp_7" },
1791 { 0x48, "kp_8" },
1792 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001793
bellarda3a91a32004-06-04 11:06:21 +00001794 { 0x56, "<" },
1795
1796 { 0x57, "f11" },
1797 { 0x58, "f12" },
1798
1799 { 0xb7, "print" },
1800
1801 { 0xc7, "home" },
1802 { 0xc9, "pgup" },
1803 { 0xd1, "pgdn" },
1804 { 0xcf, "end" },
1805
1806 { 0xcb, "left" },
1807 { 0xc8, "up" },
1808 { 0xd0, "down" },
1809 { 0xcd, "right" },
1810
1811 { 0xd2, "insert" },
1812 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001813#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1814 { 0xf0, "stop" },
1815 { 0xf1, "again" },
1816 { 0xf2, "props" },
1817 { 0xf3, "undo" },
1818 { 0xf4, "front" },
1819 { 0xf5, "copy" },
1820 { 0xf6, "open" },
1821 { 0xf7, "paste" },
1822 { 0xf8, "find" },
1823 { 0xf9, "cut" },
1824 { 0xfa, "lf" },
1825 { 0xfb, "help" },
1826 { 0xfc, "meta_l" },
1827 { 0xfd, "meta_r" },
1828 { 0xfe, "compose" },
1829#endif
bellarda3a91a32004-06-04 11:06:21 +00001830 { 0, NULL },
1831};
1832
1833static int get_keycode(const char *key)
1834{
1835 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001836 char *endp;
1837 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001838
1839 for(p = key_defs; p->name != NULL; p++) {
1840 if (!strcmp(key, p->name))
1841 return p->keycode;
1842 }
bellard64866c32006-05-07 18:03:31 +00001843 if (strstart(key, "0x", NULL)) {
1844 ret = strtoul(key, &endp, 0);
1845 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1846 return ret;
1847 }
bellarda3a91a32004-06-04 11:06:21 +00001848 return -1;
1849}
1850
balrogc8256f92008-06-08 22:45:01 +00001851#define MAX_KEYCODES 16
1852static uint8_t keycodes[MAX_KEYCODES];
1853static int nb_pending_keycodes;
1854static QEMUTimer *key_timer;
1855
1856static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001857{
balrogc8256f92008-06-08 22:45:01 +00001858 int keycode;
1859
1860 while (nb_pending_keycodes > 0) {
1861 nb_pending_keycodes--;
1862 keycode = keycodes[nb_pending_keycodes];
1863 if (keycode & 0x80)
1864 kbd_put_keycode(0xe0);
1865 kbd_put_keycode(keycode | 0x80);
1866 }
1867}
1868
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001869static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001870{
balrog3401c0d2008-06-04 10:05:59 +00001871 char keyname_buf[16];
1872 char *separator;
1873 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001874 const char *string = qdict_get_str(qdict, "string");
1875 int has_hold_time = qdict_haskey(qdict, "hold_time");
1876 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001877
balrogc8256f92008-06-08 22:45:01 +00001878 if (nb_pending_keycodes > 0) {
1879 qemu_del_timer(key_timer);
1880 release_keys(NULL);
1881 }
1882 if (!has_hold_time)
1883 hold_time = 100;
1884 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001885 while (1) {
1886 separator = strchr(string, '-');
1887 keyname_len = separator ? separator - string : strlen(string);
1888 if (keyname_len > 0) {
1889 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1890 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001891 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001892 return;
bellarda3a91a32004-06-04 11:06:21 +00001893 }
balrogc8256f92008-06-08 22:45:01 +00001894 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001895 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001896 return;
1897 }
1898 keyname_buf[keyname_len] = 0;
1899 keycode = get_keycode(keyname_buf);
1900 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001901 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001902 return;
1903 }
balrogc8256f92008-06-08 22:45:01 +00001904 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001905 }
balrog3401c0d2008-06-04 10:05:59 +00001906 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001907 break;
balrog3401c0d2008-06-04 10:05:59 +00001908 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001909 }
balrogc8256f92008-06-08 22:45:01 +00001910 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001911 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001912 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001913 keycode = keycodes[i];
1914 if (keycode & 0x80)
1915 kbd_put_keycode(0xe0);
1916 kbd_put_keycode(keycode & 0x7f);
1917 }
balrogc8256f92008-06-08 22:45:01 +00001918 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001919 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001920 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001921}
1922
bellard13224a82006-07-14 22:03:35 +00001923static int mouse_button_state;
1924
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001925static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001926{
1927 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001928 const char *dx_str = qdict_get_str(qdict, "dx_str");
1929 const char *dy_str = qdict_get_str(qdict, "dy_str");
1930 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001931 dx = strtol(dx_str, NULL, 0);
1932 dy = strtol(dy_str, NULL, 0);
1933 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001934 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001935 dz = strtol(dz_str, NULL, 0);
1936 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1937}
1938
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001939static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001940{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001941 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001942 mouse_button_state = button_state;
1943 kbd_mouse_event(0, 0, 0, mouse_button_state);
1944}
1945
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001946static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001947{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001948 int size = qdict_get_int(qdict, "size");
1949 int addr = qdict_get_int(qdict, "addr");
1950 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001951 uint32_t val;
1952 int suffix;
1953
1954 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001955 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001956 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001957 addr++;
1958 }
1959 addr &= 0xffff;
1960
1961 switch(size) {
1962 default:
1963 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001964 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001965 suffix = 'b';
1966 break;
1967 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001968 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001969 suffix = 'w';
1970 break;
1971 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001972 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001973 suffix = 'l';
1974 break;
1975 }
aliguori376253e2009-03-05 23:01:23 +00001976 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1977 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001978}
bellarda3a91a32004-06-04 11:06:21 +00001979
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001980static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001981{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001982 int size = qdict_get_int(qdict, "size");
1983 int addr = qdict_get_int(qdict, "addr");
1984 int val = qdict_get_int(qdict, "val");
1985
Jan Kiszkaf1147842009-07-14 10:20:11 +02001986 addr &= IOPORTS_MASK;
1987
1988 switch (size) {
1989 default:
1990 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001991 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001992 break;
1993 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001994 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001995 break;
1996 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001997 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001998 break;
1999 }
2000}
2001
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002002static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00002003{
2004 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002005 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00002006
Jan Kiszka76e30d02009-07-02 00:19:02 +02002007 res = qemu_boot_set(bootdevice);
2008 if (res == 0) {
2009 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
2010 } else if (res > 0) {
2011 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00002012 } else {
aliguori376253e2009-03-05 23:01:23 +00002013 monitor_printf(mon, "no function defined to set boot device list for "
2014 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00002015 }
2016}
2017
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03002018/**
2019 * do_system_reset(): Issue a machine reset
2020 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002021static int do_system_reset(Monitor *mon, const QDict *qdict,
2022 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00002023{
2024 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002025 return 0;
bellarde4f90822004-06-20 12:35:44 +00002026}
2027
Luiz Capitulino43076662009-10-07 13:41:59 -03002028/**
2029 * do_system_powerdown(): Issue a machine powerdown
2030 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002031static int do_system_powerdown(Monitor *mon, const QDict *qdict,
2032 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00002033{
2034 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002035 return 0;
bellard34751872005-07-02 14:31:34 +00002036}
2037
bellardb86bda52004-09-18 19:32:46 +00002038#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00002039static void print_pte(Monitor *mon, target_phys_addr_t addr,
2040 target_phys_addr_t pte,
2041 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00002042{
Blue Swirld65aaf32010-12-11 18:56:24 +00002043#ifdef TARGET_X86_64
2044 if (addr & (1ULL << 47)) {
2045 addr |= -1LL << 48;
2046 }
2047#endif
2048 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
2049 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002050 addr,
2051 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00002052 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00002053 pte & PG_GLOBAL_MASK ? 'G' : '-',
2054 pte & PG_PSE_MASK ? 'P' : '-',
2055 pte & PG_DIRTY_MASK ? 'D' : '-',
2056 pte & PG_ACCESSED_MASK ? 'A' : '-',
2057 pte & PG_PCD_MASK ? 'C' : '-',
2058 pte & PG_PWT_MASK ? 'T' : '-',
2059 pte & PG_USER_MASK ? 'U' : '-',
2060 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00002061}
2062
Blue Swirld65aaf32010-12-11 18:56:24 +00002063static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002064{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002065 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00002066 uint32_t pgd, pde, pte;
2067
bellardb86bda52004-09-18 19:32:46 +00002068 pgd = env->cr[3] & ~0xfff;
2069 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002070 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002071 pde = le32_to_cpu(pde);
2072 if (pde & PG_PRESENT_MASK) {
2073 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00002074 /* 4M pages */
2075 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00002076 } else {
2077 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002078 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002079 pte = le32_to_cpu(pte);
2080 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002081 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002082 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002083 ~0xfff);
2084 }
2085 }
2086 }
2087 }
2088 }
2089}
2090
Blue Swirld65aaf32010-12-11 18:56:24 +00002091static void tlb_info_pae32(Monitor *mon, CPUState *env)
2092{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002093 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002094 uint64_t pdpe, pde, pte;
2095 uint64_t pdp_addr, pd_addr, pt_addr;
2096
2097 pdp_addr = env->cr[3] & ~0x1f;
2098 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002099 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002100 pdpe = le64_to_cpu(pdpe);
2101 if (pdpe & PG_PRESENT_MASK) {
2102 pd_addr = pdpe & 0x3fffffffff000ULL;
2103 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002104 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002105 pde = le64_to_cpu(pde);
2106 if (pde & PG_PRESENT_MASK) {
2107 if (pde & PG_PSE_MASK) {
2108 /* 2M pages with PAE, CR4.PSE is ignored */
2109 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2110 ~((target_phys_addr_t)(1 << 20) - 1));
2111 } else {
2112 pt_addr = pde & 0x3fffffffff000ULL;
2113 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002114 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002115 pte = le64_to_cpu(pte);
2116 if (pte & PG_PRESENT_MASK) {
2117 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2118 + (l3 << 12),
2119 pte & ~PG_PSE_MASK,
2120 ~(target_phys_addr_t)0xfff);
2121 }
2122 }
2123 }
2124 }
2125 }
2126 }
2127 }
2128}
2129
2130#ifdef TARGET_X86_64
2131static void tlb_info_64(Monitor *mon, CPUState *env)
2132{
2133 uint64_t l1, l2, l3, l4;
2134 uint64_t pml4e, pdpe, pde, pte;
2135 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2136
2137 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2138 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002139 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002140 pml4e = le64_to_cpu(pml4e);
2141 if (pml4e & PG_PRESENT_MASK) {
2142 pdp_addr = pml4e & 0x3fffffffff000ULL;
2143 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002144 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002145 pdpe = le64_to_cpu(pdpe);
2146 if (pdpe & PG_PRESENT_MASK) {
2147 if (pdpe & PG_PSE_MASK) {
2148 /* 1G pages, CR4.PSE is ignored */
2149 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2150 0x3ffffc0000000ULL);
2151 } else {
2152 pd_addr = pdpe & 0x3fffffffff000ULL;
2153 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002154 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002155 pde = le64_to_cpu(pde);
2156 if (pde & PG_PRESENT_MASK) {
2157 if (pde & PG_PSE_MASK) {
2158 /* 2M pages, CR4.PSE is ignored */
2159 print_pte(mon, (l1 << 39) + (l2 << 30) +
2160 (l3 << 21), pde,
2161 0x3ffffffe00000ULL);
2162 } else {
2163 pt_addr = pde & 0x3fffffffff000ULL;
2164 for (l4 = 0; l4 < 512; l4++) {
2165 cpu_physical_memory_read(pt_addr
2166 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002167 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002168 pte = le64_to_cpu(pte);
2169 if (pte & PG_PRESENT_MASK) {
2170 print_pte(mon, (l1 << 39) +
2171 (l2 << 30) +
2172 (l3 << 21) + (l4 << 12),
2173 pte & ~PG_PSE_MASK,
2174 0x3fffffffff000ULL);
2175 }
2176 }
2177 }
2178 }
2179 }
2180 }
2181 }
2182 }
2183 }
2184 }
2185}
2186#endif
2187
2188static void tlb_info(Monitor *mon)
2189{
2190 CPUState *env;
2191
2192 env = mon_get_cpu();
2193
2194 if (!(env->cr[0] & CR0_PG_MASK)) {
2195 monitor_printf(mon, "PG disabled\n");
2196 return;
2197 }
2198 if (env->cr[4] & CR4_PAE_MASK) {
2199#ifdef TARGET_X86_64
2200 if (env->hflags & HF_LMA_MASK) {
2201 tlb_info_64(mon, env);
2202 } else
2203#endif
2204 {
2205 tlb_info_pae32(mon, env);
2206 }
2207 } else {
2208 tlb_info_32(mon, env);
2209 }
2210}
2211
Blue Swirl1b3cba62010-12-11 18:56:27 +00002212static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2213 int *plast_prot,
2214 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002215{
bellard9746b152004-11-11 18:30:24 +00002216 int prot1;
2217 prot1 = *plast_prot;
2218 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002219 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002220 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2221 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002222 *pstart, end, end - *pstart,
2223 prot1 & PG_USER_MASK ? 'u' : '-',
2224 'r',
2225 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002226 }
2227 if (prot != 0)
2228 *pstart = end;
2229 else
2230 *pstart = -1;
2231 *plast_prot = prot;
2232 }
2233}
2234
Blue Swirl1b3cba62010-12-11 18:56:27 +00002235static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002236{
Austin Clementsb49ca722011-08-14 23:19:21 -04002237 unsigned int l1, l2;
2238 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002239 uint32_t pgd, pde, pte;
2240 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002241
bellardb86bda52004-09-18 19:32:46 +00002242 pgd = env->cr[3] & ~0xfff;
2243 last_prot = 0;
2244 start = -1;
2245 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002246 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002247 pde = le32_to_cpu(pde);
2248 end = l1 << 22;
2249 if (pde & PG_PRESENT_MASK) {
2250 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2251 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002252 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002253 } else {
2254 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002255 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002256 pte = le32_to_cpu(pte);
2257 end = (l1 << 22) + (l2 << 12);
2258 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002259 prot = pte & pde &
2260 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002261 } else {
2262 prot = 0;
2263 }
aliguori376253e2009-03-05 23:01:23 +00002264 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002265 }
2266 }
2267 } else {
2268 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002269 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002270 }
2271 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002272 /* Flush last range */
2273 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002274}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002275
2276static void mem_info_pae32(Monitor *mon, CPUState *env)
2277{
Austin Clementsb49ca722011-08-14 23:19:21 -04002278 unsigned int l1, l2, l3;
2279 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002280 uint64_t pdpe, pde, pte;
2281 uint64_t pdp_addr, pd_addr, pt_addr;
2282 target_phys_addr_t start, end;
2283
2284 pdp_addr = env->cr[3] & ~0x1f;
2285 last_prot = 0;
2286 start = -1;
2287 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002288 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002289 pdpe = le64_to_cpu(pdpe);
2290 end = l1 << 30;
2291 if (pdpe & PG_PRESENT_MASK) {
2292 pd_addr = pdpe & 0x3fffffffff000ULL;
2293 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002294 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002295 pde = le64_to_cpu(pde);
2296 end = (l1 << 30) + (l2 << 21);
2297 if (pde & PG_PRESENT_MASK) {
2298 if (pde & PG_PSE_MASK) {
2299 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2300 PG_PRESENT_MASK);
2301 mem_print(mon, &start, &last_prot, end, prot);
2302 } else {
2303 pt_addr = pde & 0x3fffffffff000ULL;
2304 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002305 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002306 pte = le64_to_cpu(pte);
2307 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2308 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002309 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2310 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002311 } else {
2312 prot = 0;
2313 }
2314 mem_print(mon, &start, &last_prot, end, prot);
2315 }
2316 }
2317 } else {
2318 prot = 0;
2319 mem_print(mon, &start, &last_prot, end, prot);
2320 }
2321 }
2322 } else {
2323 prot = 0;
2324 mem_print(mon, &start, &last_prot, end, prot);
2325 }
2326 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002327 /* Flush last range */
2328 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002329}
2330
2331
2332#ifdef TARGET_X86_64
2333static void mem_info_64(Monitor *mon, CPUState *env)
2334{
2335 int prot, last_prot;
2336 uint64_t l1, l2, l3, l4;
2337 uint64_t pml4e, pdpe, pde, pte;
2338 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2339
2340 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2341 last_prot = 0;
2342 start = -1;
2343 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002344 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002345 pml4e = le64_to_cpu(pml4e);
2346 end = l1 << 39;
2347 if (pml4e & PG_PRESENT_MASK) {
2348 pdp_addr = pml4e & 0x3fffffffff000ULL;
2349 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002350 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002351 pdpe = le64_to_cpu(pdpe);
2352 end = (l1 << 39) + (l2 << 30);
2353 if (pdpe & PG_PRESENT_MASK) {
2354 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002355 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2356 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002357 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002358 mem_print(mon, &start, &last_prot, end, prot);
2359 } else {
2360 pd_addr = pdpe & 0x3fffffffff000ULL;
2361 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002362 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002363 pde = le64_to_cpu(pde);
2364 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2365 if (pde & PG_PRESENT_MASK) {
2366 if (pde & PG_PSE_MASK) {
2367 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2368 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002369 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002370 mem_print(mon, &start, &last_prot, end, prot);
2371 } else {
2372 pt_addr = pde & 0x3fffffffff000ULL;
2373 for (l4 = 0; l4 < 512; l4++) {
2374 cpu_physical_memory_read(pt_addr
2375 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002376 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002377 pte = le64_to_cpu(pte);
2378 end = (l1 << 39) + (l2 << 30) +
2379 (l3 << 21) + (l4 << 12);
2380 if (pte & PG_PRESENT_MASK) {
2381 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2382 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002383 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002384 } else {
2385 prot = 0;
2386 }
2387 mem_print(mon, &start, &last_prot, end, prot);
2388 }
2389 }
2390 } else {
2391 prot = 0;
2392 mem_print(mon, &start, &last_prot, end, prot);
2393 }
2394 }
2395 }
2396 } else {
2397 prot = 0;
2398 mem_print(mon, &start, &last_prot, end, prot);
2399 }
2400 }
2401 } else {
2402 prot = 0;
2403 mem_print(mon, &start, &last_prot, end, prot);
2404 }
2405 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002406 /* Flush last range */
2407 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002408}
2409#endif
2410
2411static void mem_info(Monitor *mon)
2412{
2413 CPUState *env;
2414
2415 env = mon_get_cpu();
2416
2417 if (!(env->cr[0] & CR0_PG_MASK)) {
2418 monitor_printf(mon, "PG disabled\n");
2419 return;
2420 }
2421 if (env->cr[4] & CR4_PAE_MASK) {
2422#ifdef TARGET_X86_64
2423 if (env->hflags & HF_LMA_MASK) {
2424 mem_info_64(mon, env);
2425 } else
2426#endif
2427 {
2428 mem_info_pae32(mon, env);
2429 }
2430 } else {
2431 mem_info_32(mon, env);
2432 }
2433}
bellardb86bda52004-09-18 19:32:46 +00002434#endif
2435
aurel327c664e22009-03-03 06:12:22 +00002436#if defined(TARGET_SH4)
2437
aliguori376253e2009-03-05 23:01:23 +00002438static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002439{
aliguori376253e2009-03-05 23:01:23 +00002440 monitor_printf(mon, " tlb%i:\t"
2441 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2442 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2443 "dirty=%hhu writethrough=%hhu\n",
2444 idx,
2445 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2446 tlb->v, tlb->sh, tlb->c, tlb->pr,
2447 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002448}
2449
aliguori376253e2009-03-05 23:01:23 +00002450static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002451{
2452 CPUState *env = mon_get_cpu();
2453 int i;
2454
aliguori376253e2009-03-05 23:01:23 +00002455 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002456 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002457 print_tlb (mon, i, &env->itlb[i]);
2458 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002459 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002460 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002461}
2462
2463#endif
2464
Blue Swirld41160a2010-12-19 13:42:56 +00002465#if defined(TARGET_SPARC)
2466static void tlb_info(Monitor *mon)
2467{
2468 CPUState *env1 = mon_get_cpu();
2469
2470 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2471}
2472#endif
2473
Blue Swirl314e2982011-09-11 20:22:05 +00002474static void do_info_mtree(Monitor *mon)
2475{
2476 mtree_info((fprintf_function)monitor_printf, mon);
2477}
2478
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002479static void do_info_kvm_print(Monitor *mon, const QObject *data)
2480{
2481 QDict *qdict;
2482
2483 qdict = qobject_to_qdict(data);
2484
2485 monitor_printf(mon, "kvm support: ");
2486 if (qdict_get_bool(qdict, "present")) {
2487 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2488 "enabled" : "disabled");
2489 } else {
2490 monitor_printf(mon, "not compiled\n");
2491 }
2492}
2493
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002494static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002495{
2496#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002497 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2498 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002499#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002500 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002501#endif
2502}
2503
aliguori030ea372009-04-21 22:30:47 +00002504static void do_info_numa(Monitor *mon)
2505{
aliguorib28b6232009-04-22 20:20:29 +00002506 int i;
aliguori030ea372009-04-21 22:30:47 +00002507 CPUState *env;
2508
2509 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2510 for (i = 0; i < nb_numa_nodes; i++) {
2511 monitor_printf(mon, "node %d cpus:", i);
2512 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2513 if (env->numa_node == i) {
2514 monitor_printf(mon, " %d", env->cpu_index);
2515 }
2516 }
2517 monitor_printf(mon, "\n");
2518 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2519 node_mem[i] >> 20);
2520 }
2521}
2522
bellard5f1ce942006-02-08 22:40:15 +00002523#ifdef CONFIG_PROFILER
2524
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002525int64_t qemu_time;
2526int64_t dev_time;
2527
aliguori376253e2009-03-05 23:01:23 +00002528static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002529{
2530 int64_t total;
2531 total = qemu_time;
2532 if (total == 0)
2533 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002534 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002535 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002536 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002537 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002538 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002539 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002540}
2541#else
aliguori376253e2009-03-05 23:01:23 +00002542static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002543{
aliguori376253e2009-03-05 23:01:23 +00002544 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002545}
2546#endif
2547
bellardec36b692006-07-16 18:57:03 +00002548/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002549static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002550
aliguori376253e2009-03-05 23:01:23 +00002551static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002552{
2553 int i;
2554 CaptureState *s;
2555
2556 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002557 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002558 s->ops.info (s->opaque);
2559 }
2560}
2561
Blue Swirl23130862009-06-06 08:22:04 +00002562#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002563static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002564{
2565 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002566 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002567 CaptureState *s;
2568
2569 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2570 if (i == n) {
2571 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002572 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002573 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002574 return;
2575 }
2576 }
2577}
2578
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002579static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002580{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002581 const char *path = qdict_get_str(qdict, "path");
2582 int has_freq = qdict_haskey(qdict, "freq");
2583 int freq = qdict_get_try_int(qdict, "freq", -1);
2584 int has_bits = qdict_haskey(qdict, "bits");
2585 int bits = qdict_get_try_int(qdict, "bits", -1);
2586 int has_channels = qdict_haskey(qdict, "nchannels");
2587 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002588 CaptureState *s;
2589
Anthony Liguori7267c092011-08-20 22:09:37 -05002590 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002591
2592 freq = has_freq ? freq : 44100;
2593 bits = has_bits ? bits : 16;
2594 nchannels = has_channels ? nchannels : 2;
2595
2596 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002597 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002598 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002599 return;
bellardec36b692006-07-16 18:57:03 +00002600 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002601 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002602}
2603#endif
2604
aurel32dc1c0b72008-04-27 23:52:12 +00002605#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002606static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002607{
2608 CPUState *env;
2609
2610 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2611 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2612 }
2613
2614 return 0;
2615}
2616#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002617static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002618{
2619 qerror_report(QERR_UNSUPPORTED);
2620 return -1;
2621}
aurel32dc1c0b72008-04-27 23:52:12 +00002622#endif
2623
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002624static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002625{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002626 QDict *qdict;
Luiz Capitulino6e0ca882011-08-09 14:44:25 -03002627 const char *status;
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002628
2629 qdict = qobject_to_qdict(data);
2630
2631 monitor_printf(mon, "VM status: ");
2632 if (qdict_get_bool(qdict, "running")) {
2633 monitor_printf(mon, "running");
2634 if (qdict_get_bool(qdict, "singlestep")) {
2635 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002636 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002637 } else {
2638 monitor_printf(mon, "paused");
2639 }
2640
Luiz Capitulino6e0ca882011-08-09 14:44:25 -03002641 status = qdict_get_str(qdict, "status");
2642 if (strcmp(status, "paused") && strcmp(status, "running")) {
2643 monitor_printf(mon, " (%s)", status);
2644 }
2645
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002646 monitor_printf(mon, "\n");
2647}
2648
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002649static void do_info_status(Monitor *mon, QObject **ret_data)
2650{
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -03002651 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep, runstate_as_string());
aurel326f9c5ee2008-12-18 22:43:56 +00002652}
2653
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002654static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002655{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002656 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002657
aliguori76655d62009-03-06 20:27:37 +00002658 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002659 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002660 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002661 return acl;
2662}
aliguori76655d62009-03-06 20:27:37 +00002663
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002664static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002665{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002666 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002667 qemu_acl *acl = find_acl(mon, aclname);
2668 qemu_acl_entry *entry;
2669 int i = 0;
2670
2671 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002672 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002673 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002674 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002675 i++;
2676 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002677 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002678 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002679 }
2680}
2681
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002682static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002683{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002684 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002685 qemu_acl *acl = find_acl(mon, aclname);
2686
2687 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002688 qemu_acl_reset(acl);
2689 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002690 }
2691}
aliguori76655d62009-03-06 20:27:37 +00002692
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002693static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002694{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002695 const char *aclname = qdict_get_str(qdict, "aclname");
2696 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002697 qemu_acl *acl = find_acl(mon, aclname);
2698
2699 if (acl) {
2700 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002701 acl->defaultDeny = 0;
2702 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002703 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002704 acl->defaultDeny = 1;
2705 monitor_printf(mon, "acl: policy set to 'deny'\n");
2706 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002707 monitor_printf(mon, "acl: unknown policy '%s', "
2708 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002709 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002710 }
2711}
aliguori76655d62009-03-06 20:27:37 +00002712
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002713static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002714{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002715 const char *aclname = qdict_get_str(qdict, "aclname");
2716 const char *match = qdict_get_str(qdict, "match");
2717 const char *policy = qdict_get_str(qdict, "policy");
2718 int has_index = qdict_haskey(qdict, "index");
2719 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002720 qemu_acl *acl = find_acl(mon, aclname);
2721 int deny, ret;
2722
2723 if (acl) {
2724 if (strcmp(policy, "allow") == 0) {
2725 deny = 0;
2726 } else if (strcmp(policy, "deny") == 0) {
2727 deny = 1;
2728 } else {
2729 monitor_printf(mon, "acl: unknown policy '%s', "
2730 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002731 return;
2732 }
aliguori28a76be2009-03-06 20:27:40 +00002733 if (has_index)
2734 ret = qemu_acl_insert(acl, deny, match, index);
2735 else
2736 ret = qemu_acl_append(acl, deny, match);
2737 if (ret < 0)
2738 monitor_printf(mon, "acl: unable to add acl entry\n");
2739 else
2740 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002741 }
2742}
aliguori76655d62009-03-06 20:27:37 +00002743
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002744static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002745{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002746 const char *aclname = qdict_get_str(qdict, "aclname");
2747 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002748 qemu_acl *acl = find_acl(mon, aclname);
2749 int ret;
aliguori76655d62009-03-06 20:27:37 +00002750
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002751 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002752 ret = qemu_acl_remove(acl, match);
2753 if (ret < 0)
2754 monitor_printf(mon, "acl: no matching acl entry\n");
2755 else
2756 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002757 }
2758}
2759
Huang Ying79c4f6b2009-06-23 10:05:14 +08002760#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002761static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002762{
2763 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002764 int cpu_index = qdict_get_int(qdict, "cpu_index");
2765 int bank = qdict_get_int(qdict, "bank");
2766 uint64_t status = qdict_get_int(qdict, "status");
2767 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2768 uint64_t addr = qdict_get_int(qdict, "addr");
2769 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002770 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002771
Jan Kiszka747461c2011-03-02 08:56:10 +01002772 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2773 flags |= MCE_INJECT_BROADCAST;
2774 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002775 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002776 if (cenv->cpu_index == cpu_index) {
2777 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002778 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002779 break;
2780 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002781 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002782}
2783#endif
2784
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002785static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002786{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002787 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002788 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002789 int fd;
2790
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002791 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002792 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002793 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002794 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002795 }
2796
2797 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002798 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2799 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002800 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002801 }
2802
Blue Swirl72cf2d42009-09-12 07:36:22 +00002803 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002804 if (strcmp(monfd->name, fdname) != 0) {
2805 continue;
2806 }
2807
2808 close(monfd->fd);
2809 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002810 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002811 }
2812
Anthony Liguori7267c092011-08-20 22:09:37 -05002813 monfd = g_malloc0(sizeof(mon_fd_t));
2814 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002815 monfd->fd = fd;
2816
Blue Swirl72cf2d42009-09-12 07:36:22 +00002817 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002818 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002819}
2820
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002821static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002822{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002823 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002824 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002825
Blue Swirl72cf2d42009-09-12 07:36:22 +00002826 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002827 if (strcmp(monfd->name, fdname) != 0) {
2828 continue;
2829 }
2830
Blue Swirl72cf2d42009-09-12 07:36:22 +00002831 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002832 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002833 g_free(monfd->name);
2834 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002835 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002836 }
2837
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002838 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002839 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002840}
2841
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002842static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002843{
Luiz Capitulino13548692011-07-29 15:36:43 -03002844 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002845 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002846
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002847 vm_stop(RSTATE_RESTORE);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002848
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002849 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002850 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002851 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002852}
2853
Mark McLoughlin7768e042009-07-22 09:11:41 +01002854int monitor_get_fd(Monitor *mon, const char *fdname)
2855{
Anthony Liguoric227f092009-10-01 16:12:16 -05002856 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002857
Blue Swirl72cf2d42009-09-12 07:36:22 +00002858 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002859 int fd;
2860
2861 if (strcmp(monfd->name, fdname) != 0) {
2862 continue;
2863 }
2864
2865 fd = monfd->fd;
2866
2867 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002868 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002869 g_free(monfd->name);
2870 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002871
2872 return fd;
2873 }
2874
2875 return -1;
2876}
2877
Anthony Liguoric227f092009-10-01 16:12:16 -05002878static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002879#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002880 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002881};
2882
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002883/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002884static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 {
2886 .name = "version",
2887 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 .params = "",
2889 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002890 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002891 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 },
2893 {
2894 .name = "network",
2895 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 .params = "",
2897 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002898 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 },
2900 {
2901 .name = "chardev",
2902 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 .params = "",
2904 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002905 .user_print = qemu_chr_info_print,
2906 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002907 },
2908 {
2909 .name = "block",
2910 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002911 .params = "",
2912 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002913 .user_print = bdrv_info_print,
2914 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002915 },
2916 {
2917 .name = "blockstats",
2918 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002919 .params = "",
2920 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002921 .user_print = bdrv_stats_print,
2922 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002923 },
2924 {
2925 .name = "registers",
2926 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002927 .params = "",
2928 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002929 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002930 },
2931 {
2932 .name = "cpus",
2933 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002934 .params = "",
2935 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002936 .user_print = monitor_print_cpus,
2937 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002938 },
2939 {
2940 .name = "history",
2941 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002942 .params = "",
2943 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002944 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002945 },
2946 {
2947 .name = "irq",
2948 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002949 .params = "",
2950 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002951 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002952 },
2953 {
2954 .name = "pic",
2955 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002956 .params = "",
2957 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002958 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002959 },
2960 {
2961 .name = "pci",
2962 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002963 .params = "",
2964 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002965 .user_print = do_pci_info_print,
2966 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002967 },
Blue Swirld41160a2010-12-19 13:42:56 +00002968#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002969 {
2970 .name = "tlb",
2971 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002972 .params = "",
2973 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002974 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002975 },
aurel327c664e22009-03-03 06:12:22 +00002976#endif
2977#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002978 {
2979 .name = "mem",
2980 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002981 .params = "",
2982 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002983 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002984 },
bellardb86bda52004-09-18 19:32:46 +00002985#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002986 {
Blue Swirl314e2982011-09-11 20:22:05 +00002987 .name = "mtree",
2988 .args_type = "",
2989 .params = "",
2990 .help = "show memory tree",
2991 .mhandler.info = do_info_mtree,
2992 },
2993 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002994 .name = "jit",
2995 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002996 .params = "",
2997 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002998 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002999 },
3000 {
3001 .name = "kvm",
3002 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003003 .params = "",
3004 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02003005 .user_print = do_info_kvm_print,
3006 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003007 },
3008 {
3009 .name = "numa",
3010 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003011 .params = "",
3012 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003013 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003014 },
3015 {
3016 .name = "usb",
3017 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003018 .params = "",
3019 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003020 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003021 },
3022 {
3023 .name = "usbhost",
3024 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003025 .params = "",
3026 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003027 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003028 },
3029 {
3030 .name = "profile",
3031 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003032 .params = "",
3033 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003034 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003035 },
3036 {
3037 .name = "capture",
3038 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003039 .params = "",
3040 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003041 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003042 },
3043 {
3044 .name = "snapshots",
3045 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003046 .params = "",
3047 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03003048 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003049 },
3050 {
3051 .name = "status",
3052 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003053 .params = "",
3054 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02003055 .user_print = do_info_status_print,
3056 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003057 },
3058 {
3059 .name = "pcmcia",
3060 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003061 .params = "",
3062 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03003063 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003064 },
3065 {
3066 .name = "mice",
3067 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003068 .params = "",
3069 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02003070 .user_print = do_info_mice_print,
3071 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003072 },
3073 {
3074 .name = "vnc",
3075 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003076 .params = "",
3077 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02003078 .user_print = do_info_vnc_print,
3079 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003080 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003081#if defined(CONFIG_SPICE)
3082 {
3083 .name = "spice",
3084 .args_type = "",
3085 .params = "",
3086 .help = "show the spice server status",
3087 .user_print = do_info_spice_print,
3088 .mhandler.info_new = do_info_spice,
3089 },
3090#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003091 {
3092 .name = "name",
3093 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003094 .params = "",
3095 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02003096 .user_print = do_info_name_print,
3097 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003098 },
3099 {
3100 .name = "uuid",
3101 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003102 .params = "",
3103 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02003104 .user_print = do_info_uuid_print,
3105 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003106 },
j_mayer76a66252007-03-07 08:32:30 +00003107#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003108 {
3109 .name = "cpustats",
3110 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003111 .params = "",
3112 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03003113 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003114 },
j_mayer76a66252007-03-07 08:32:30 +00003115#endif
blueswir131a60e22007-10-26 18:42:59 +00003116#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003117 {
3118 .name = "usernet",
3119 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003120 .params = "",
3121 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03003122 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003123 },
blueswir131a60e22007-10-26 18:42:59 +00003124#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003125 {
3126 .name = "migrate",
3127 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003128 .params = "",
3129 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003130 .user_print = do_info_migrate_print,
3131 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003132 },
3133 {
3134 .name = "balloon",
3135 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003136 .params = "",
3137 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003138 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003139 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003140 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003141 },
3142 {
3143 .name = "qtree",
3144 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003145 .params = "",
3146 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003147 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003148 },
3149 {
3150 .name = "qdm",
3151 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003152 .params = "",
3153 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003154 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003155 },
3156 {
3157 .name = "roms",
3158 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003159 .params = "",
3160 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003161 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003162 },
Lluís6d8a7642011-08-31 20:30:43 +02003163#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303164 {
3165 .name = "trace",
3166 .args_type = "",
3167 .params = "",
3168 .help = "show current contents of trace buffer",
3169 .mhandler.info = do_info_trace,
3170 },
Lluís31965ae2011-08-31 20:31:24 +02003171#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303172 {
3173 .name = "trace-events",
3174 .args_type = "",
3175 .params = "",
3176 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003177 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303178 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003179 {
3180 .name = NULL,
3181 },
bellard9dc39cb2004-03-14 21:38:27 +00003182};
3183
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003184static const mon_cmd_t qmp_cmds[] = {
3185#include "qmp-commands.h"
3186 { /* NULL */ },
3187};
3188
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003189static const mon_cmd_t qmp_query_cmds[] = {
3190 {
3191 .name = "version",
3192 .args_type = "",
3193 .params = "",
3194 .help = "show the version of QEMU",
3195 .user_print = do_info_version_print,
3196 .mhandler.info_new = do_info_version,
3197 },
3198 {
3199 .name = "commands",
3200 .args_type = "",
3201 .params = "",
3202 .help = "list QMP available commands",
3203 .user_print = monitor_user_noop,
3204 .mhandler.info_new = do_info_commands,
3205 },
3206 {
3207 .name = "chardev",
3208 .args_type = "",
3209 .params = "",
3210 .help = "show the character devices",
3211 .user_print = qemu_chr_info_print,
3212 .mhandler.info_new = qemu_chr_info,
3213 },
3214 {
3215 .name = "block",
3216 .args_type = "",
3217 .params = "",
3218 .help = "show the block devices",
3219 .user_print = bdrv_info_print,
3220 .mhandler.info_new = bdrv_info,
3221 },
3222 {
3223 .name = "blockstats",
3224 .args_type = "",
3225 .params = "",
3226 .help = "show block device statistics",
3227 .user_print = bdrv_stats_print,
3228 .mhandler.info_new = bdrv_info_stats,
3229 },
3230 {
3231 .name = "cpus",
3232 .args_type = "",
3233 .params = "",
3234 .help = "show infos for each CPU",
3235 .user_print = monitor_print_cpus,
3236 .mhandler.info_new = do_info_cpus,
3237 },
3238 {
3239 .name = "pci",
3240 .args_type = "",
3241 .params = "",
3242 .help = "show PCI info",
3243 .user_print = do_pci_info_print,
3244 .mhandler.info_new = do_pci_info,
3245 },
3246 {
3247 .name = "kvm",
3248 .args_type = "",
3249 .params = "",
3250 .help = "show KVM information",
3251 .user_print = do_info_kvm_print,
3252 .mhandler.info_new = do_info_kvm,
3253 },
3254 {
3255 .name = "status",
3256 .args_type = "",
3257 .params = "",
3258 .help = "show the current VM status (running|paused)",
3259 .user_print = do_info_status_print,
3260 .mhandler.info_new = do_info_status,
3261 },
3262 {
3263 .name = "mice",
3264 .args_type = "",
3265 .params = "",
3266 .help = "show which guest mouse is receiving events",
3267 .user_print = do_info_mice_print,
3268 .mhandler.info_new = do_info_mice,
3269 },
3270 {
3271 .name = "vnc",
3272 .args_type = "",
3273 .params = "",
3274 .help = "show the vnc server status",
3275 .user_print = do_info_vnc_print,
3276 .mhandler.info_new = do_info_vnc,
3277 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003278#if defined(CONFIG_SPICE)
3279 {
3280 .name = "spice",
3281 .args_type = "",
3282 .params = "",
3283 .help = "show the spice server status",
3284 .user_print = do_info_spice_print,
3285 .mhandler.info_new = do_info_spice,
3286 },
3287#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003288 {
3289 .name = "name",
3290 .args_type = "",
3291 .params = "",
3292 .help = "show the current VM name",
3293 .user_print = do_info_name_print,
3294 .mhandler.info_new = do_info_name,
3295 },
3296 {
3297 .name = "uuid",
3298 .args_type = "",
3299 .params = "",
3300 .help = "show the current VM UUID",
3301 .user_print = do_info_uuid_print,
3302 .mhandler.info_new = do_info_uuid,
3303 },
3304 {
3305 .name = "migrate",
3306 .args_type = "",
3307 .params = "",
3308 .help = "show migration status",
3309 .user_print = do_info_migrate_print,
3310 .mhandler.info_new = do_info_migrate,
3311 },
3312 {
3313 .name = "balloon",
3314 .args_type = "",
3315 .params = "",
3316 .help = "show balloon information",
3317 .user_print = monitor_print_balloon,
3318 .mhandler.info_async = do_info_balloon,
3319 .flags = MONITOR_CMD_ASYNC,
3320 },
3321 { /* NULL */ },
3322};
3323
bellard9307c4c2004-04-04 12:57:25 +00003324/*******************************************************************/
3325
3326static const char *pch;
3327static jmp_buf expr_env;
3328
bellard92a31b12005-02-10 22:00:52 +00003329#define MD_TLONG 0
3330#define MD_I32 1
3331
bellard9307c4c2004-04-04 12:57:25 +00003332typedef struct MonitorDef {
3333 const char *name;
3334 int offset;
blueswir18662d652008-10-02 18:32:44 +00003335 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003336 int type;
bellard9307c4c2004-04-04 12:57:25 +00003337} MonitorDef;
3338
bellard57206fd2004-04-25 18:54:52 +00003339#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003340static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003341{
bellard6a00d602005-11-21 23:25:50 +00003342 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003343 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003344}
3345#endif
3346
bellarda541f292004-04-12 20:39:29 +00003347#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003348static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003349{
bellard6a00d602005-11-21 23:25:50 +00003350 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003351 unsigned int u;
3352 int i;
3353
3354 u = 0;
3355 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003356 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003357
3358 return u;
3359}
3360
blueswir18662d652008-10-02 18:32:44 +00003361static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003362{
bellard6a00d602005-11-21 23:25:50 +00003363 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003364 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003365}
3366
blueswir18662d652008-10-02 18:32:44 +00003367static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003368{
bellard6a00d602005-11-21 23:25:50 +00003369 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003370 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003371}
bellard9fddaa02004-05-21 12:59:32 +00003372
blueswir18662d652008-10-02 18:32:44 +00003373static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003374{
bellard6a00d602005-11-21 23:25:50 +00003375 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003376 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003377}
3378
blueswir18662d652008-10-02 18:32:44 +00003379static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003380{
bellard6a00d602005-11-21 23:25:50 +00003381 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003382 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003383}
3384
blueswir18662d652008-10-02 18:32:44 +00003385static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003386{
bellard6a00d602005-11-21 23:25:50 +00003387 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003388 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003389}
bellarda541f292004-04-12 20:39:29 +00003390#endif
3391
bellarde95c8d52004-09-30 22:22:08 +00003392#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003393#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003394static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003395{
bellard6a00d602005-11-21 23:25:50 +00003396 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003397
3398 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003399}
bellard7b936c02005-10-30 17:05:13 +00003400#endif
bellarde95c8d52004-09-30 22:22:08 +00003401
blueswir18662d652008-10-02 18:32:44 +00003402static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003403{
bellard6a00d602005-11-21 23:25:50 +00003404 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003405 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003406}
3407#endif
3408
blueswir18662d652008-10-02 18:32:44 +00003409static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003410#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003411
3412#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003413 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003414 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003415 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003416
bellard9307c4c2004-04-04 12:57:25 +00003417 { "eax", offsetof(CPUState, regs[0]) },
3418 { "ecx", offsetof(CPUState, regs[1]) },
3419 { "edx", offsetof(CPUState, regs[2]) },
3420 { "ebx", offsetof(CPUState, regs[3]) },
3421 { "esp|sp", offsetof(CPUState, regs[4]) },
3422 { "ebp|fp", offsetof(CPUState, regs[5]) },
3423 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003424 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003425#ifdef TARGET_X86_64
3426 { "r8", offsetof(CPUState, regs[8]) },
3427 { "r9", offsetof(CPUState, regs[9]) },
3428 { "r10", offsetof(CPUState, regs[10]) },
3429 { "r11", offsetof(CPUState, regs[11]) },
3430 { "r12", offsetof(CPUState, regs[12]) },
3431 { "r13", offsetof(CPUState, regs[13]) },
3432 { "r14", offsetof(CPUState, regs[14]) },
3433 { "r15", offsetof(CPUState, regs[15]) },
3434#endif
bellard9307c4c2004-04-04 12:57:25 +00003435 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003436 { "eip", offsetof(CPUState, eip) },
3437 SEG("cs", R_CS)
3438 SEG("ds", R_DS)
3439 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003440 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003441 SEG("fs", R_FS)
3442 SEG("gs", R_GS)
3443 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003444#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003445 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003446 { "r0", offsetof(CPUState, gpr[0]) },
3447 { "r1", offsetof(CPUState, gpr[1]) },
3448 { "r2", offsetof(CPUState, gpr[2]) },
3449 { "r3", offsetof(CPUState, gpr[3]) },
3450 { "r4", offsetof(CPUState, gpr[4]) },
3451 { "r5", offsetof(CPUState, gpr[5]) },
3452 { "r6", offsetof(CPUState, gpr[6]) },
3453 { "r7", offsetof(CPUState, gpr[7]) },
3454 { "r8", offsetof(CPUState, gpr[8]) },
3455 { "r9", offsetof(CPUState, gpr[9]) },
3456 { "r10", offsetof(CPUState, gpr[10]) },
3457 { "r11", offsetof(CPUState, gpr[11]) },
3458 { "r12", offsetof(CPUState, gpr[12]) },
3459 { "r13", offsetof(CPUState, gpr[13]) },
3460 { "r14", offsetof(CPUState, gpr[14]) },
3461 { "r15", offsetof(CPUState, gpr[15]) },
3462 { "r16", offsetof(CPUState, gpr[16]) },
3463 { "r17", offsetof(CPUState, gpr[17]) },
3464 { "r18", offsetof(CPUState, gpr[18]) },
3465 { "r19", offsetof(CPUState, gpr[19]) },
3466 { "r20", offsetof(CPUState, gpr[20]) },
3467 { "r21", offsetof(CPUState, gpr[21]) },
3468 { "r22", offsetof(CPUState, gpr[22]) },
3469 { "r23", offsetof(CPUState, gpr[23]) },
3470 { "r24", offsetof(CPUState, gpr[24]) },
3471 { "r25", offsetof(CPUState, gpr[25]) },
3472 { "r26", offsetof(CPUState, gpr[26]) },
3473 { "r27", offsetof(CPUState, gpr[27]) },
3474 { "r28", offsetof(CPUState, gpr[28]) },
3475 { "r29", offsetof(CPUState, gpr[29]) },
3476 { "r30", offsetof(CPUState, gpr[30]) },
3477 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003478 /* Floating point registers */
3479 { "f0", offsetof(CPUState, fpr[0]) },
3480 { "f1", offsetof(CPUState, fpr[1]) },
3481 { "f2", offsetof(CPUState, fpr[2]) },
3482 { "f3", offsetof(CPUState, fpr[3]) },
3483 { "f4", offsetof(CPUState, fpr[4]) },
3484 { "f5", offsetof(CPUState, fpr[5]) },
3485 { "f6", offsetof(CPUState, fpr[6]) },
3486 { "f7", offsetof(CPUState, fpr[7]) },
3487 { "f8", offsetof(CPUState, fpr[8]) },
3488 { "f9", offsetof(CPUState, fpr[9]) },
3489 { "f10", offsetof(CPUState, fpr[10]) },
3490 { "f11", offsetof(CPUState, fpr[11]) },
3491 { "f12", offsetof(CPUState, fpr[12]) },
3492 { "f13", offsetof(CPUState, fpr[13]) },
3493 { "f14", offsetof(CPUState, fpr[14]) },
3494 { "f15", offsetof(CPUState, fpr[15]) },
3495 { "f16", offsetof(CPUState, fpr[16]) },
3496 { "f17", offsetof(CPUState, fpr[17]) },
3497 { "f18", offsetof(CPUState, fpr[18]) },
3498 { "f19", offsetof(CPUState, fpr[19]) },
3499 { "f20", offsetof(CPUState, fpr[20]) },
3500 { "f21", offsetof(CPUState, fpr[21]) },
3501 { "f22", offsetof(CPUState, fpr[22]) },
3502 { "f23", offsetof(CPUState, fpr[23]) },
3503 { "f24", offsetof(CPUState, fpr[24]) },
3504 { "f25", offsetof(CPUState, fpr[25]) },
3505 { "f26", offsetof(CPUState, fpr[26]) },
3506 { "f27", offsetof(CPUState, fpr[27]) },
3507 { "f28", offsetof(CPUState, fpr[28]) },
3508 { "f29", offsetof(CPUState, fpr[29]) },
3509 { "f30", offsetof(CPUState, fpr[30]) },
3510 { "f31", offsetof(CPUState, fpr[31]) },
3511 { "fpscr", offsetof(CPUState, fpscr) },
3512 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003513 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003514 { "lr", offsetof(CPUState, lr) },
3515 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003516 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003517 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003518 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003519 { "msr", 0, &monitor_get_msr, },
3520 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003521 { "tbu", 0, &monitor_get_tbu, },
3522 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003523#if defined(TARGET_PPC64)
3524 /* Address space register */
3525 { "asr", offsetof(CPUState, asr) },
3526#endif
3527 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003528 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003529 { "sr0", offsetof(CPUState, sr[0]) },
3530 { "sr1", offsetof(CPUState, sr[1]) },
3531 { "sr2", offsetof(CPUState, sr[2]) },
3532 { "sr3", offsetof(CPUState, sr[3]) },
3533 { "sr4", offsetof(CPUState, sr[4]) },
3534 { "sr5", offsetof(CPUState, sr[5]) },
3535 { "sr6", offsetof(CPUState, sr[6]) },
3536 { "sr7", offsetof(CPUState, sr[7]) },
3537 { "sr8", offsetof(CPUState, sr[8]) },
3538 { "sr9", offsetof(CPUState, sr[9]) },
3539 { "sr10", offsetof(CPUState, sr[10]) },
3540 { "sr11", offsetof(CPUState, sr[11]) },
3541 { "sr12", offsetof(CPUState, sr[12]) },
3542 { "sr13", offsetof(CPUState, sr[13]) },
3543 { "sr14", offsetof(CPUState, sr[14]) },
3544 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003545 /* Too lazy to put BATs... */
3546 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3547
3548 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3549 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3550 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3551 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3552 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3553 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3554 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3555 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3556 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3557 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3558 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3559 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3560 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3561 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3562 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3563 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3564 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3565 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3566 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3567 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3568 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3569 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3570 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3571 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3572 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3573 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3574 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3575 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3576 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3577 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3578 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3579 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3580 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3581 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3582 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3583 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3584 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3585 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3586 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3587 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3588 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3589 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3590 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3591 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3592 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3593 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3594 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3595 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3596 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3597 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3598 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3599 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3600 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3601 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3602 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3603 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3604 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3605 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3606 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3607 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3608 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3609 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3610 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3611 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3612 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3613 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3614
bellarde95c8d52004-09-30 22:22:08 +00003615#elif defined(TARGET_SPARC)
3616 { "g0", offsetof(CPUState, gregs[0]) },
3617 { "g1", offsetof(CPUState, gregs[1]) },
3618 { "g2", offsetof(CPUState, gregs[2]) },
3619 { "g3", offsetof(CPUState, gregs[3]) },
3620 { "g4", offsetof(CPUState, gregs[4]) },
3621 { "g5", offsetof(CPUState, gregs[5]) },
3622 { "g6", offsetof(CPUState, gregs[6]) },
3623 { "g7", offsetof(CPUState, gregs[7]) },
3624 { "o0", 0, monitor_get_reg },
3625 { "o1", 1, monitor_get_reg },
3626 { "o2", 2, monitor_get_reg },
3627 { "o3", 3, monitor_get_reg },
3628 { "o4", 4, monitor_get_reg },
3629 { "o5", 5, monitor_get_reg },
3630 { "o6", 6, monitor_get_reg },
3631 { "o7", 7, monitor_get_reg },
3632 { "l0", 8, monitor_get_reg },
3633 { "l1", 9, monitor_get_reg },
3634 { "l2", 10, monitor_get_reg },
3635 { "l3", 11, monitor_get_reg },
3636 { "l4", 12, monitor_get_reg },
3637 { "l5", 13, monitor_get_reg },
3638 { "l6", 14, monitor_get_reg },
3639 { "l7", 15, monitor_get_reg },
3640 { "i0", 16, monitor_get_reg },
3641 { "i1", 17, monitor_get_reg },
3642 { "i2", 18, monitor_get_reg },
3643 { "i3", 19, monitor_get_reg },
3644 { "i4", 20, monitor_get_reg },
3645 { "i5", 21, monitor_get_reg },
3646 { "i6", 22, monitor_get_reg },
3647 { "i7", 23, monitor_get_reg },
3648 { "pc", offsetof(CPUState, pc) },
3649 { "npc", offsetof(CPUState, npc) },
3650 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003651#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003652 { "psr", 0, &monitor_get_psr, },
3653 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003654#endif
bellarde95c8d52004-09-30 22:22:08 +00003655 { "tbr", offsetof(CPUState, tbr) },
3656 { "fsr", offsetof(CPUState, fsr) },
3657 { "f0", offsetof(CPUState, fpr[0]) },
3658 { "f1", offsetof(CPUState, fpr[1]) },
3659 { "f2", offsetof(CPUState, fpr[2]) },
3660 { "f3", offsetof(CPUState, fpr[3]) },
3661 { "f4", offsetof(CPUState, fpr[4]) },
3662 { "f5", offsetof(CPUState, fpr[5]) },
3663 { "f6", offsetof(CPUState, fpr[6]) },
3664 { "f7", offsetof(CPUState, fpr[7]) },
3665 { "f8", offsetof(CPUState, fpr[8]) },
3666 { "f9", offsetof(CPUState, fpr[9]) },
3667 { "f10", offsetof(CPUState, fpr[10]) },
3668 { "f11", offsetof(CPUState, fpr[11]) },
3669 { "f12", offsetof(CPUState, fpr[12]) },
3670 { "f13", offsetof(CPUState, fpr[13]) },
3671 { "f14", offsetof(CPUState, fpr[14]) },
3672 { "f15", offsetof(CPUState, fpr[15]) },
3673 { "f16", offsetof(CPUState, fpr[16]) },
3674 { "f17", offsetof(CPUState, fpr[17]) },
3675 { "f18", offsetof(CPUState, fpr[18]) },
3676 { "f19", offsetof(CPUState, fpr[19]) },
3677 { "f20", offsetof(CPUState, fpr[20]) },
3678 { "f21", offsetof(CPUState, fpr[21]) },
3679 { "f22", offsetof(CPUState, fpr[22]) },
3680 { "f23", offsetof(CPUState, fpr[23]) },
3681 { "f24", offsetof(CPUState, fpr[24]) },
3682 { "f25", offsetof(CPUState, fpr[25]) },
3683 { "f26", offsetof(CPUState, fpr[26]) },
3684 { "f27", offsetof(CPUState, fpr[27]) },
3685 { "f28", offsetof(CPUState, fpr[28]) },
3686 { "f29", offsetof(CPUState, fpr[29]) },
3687 { "f30", offsetof(CPUState, fpr[30]) },
3688 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003689#ifdef TARGET_SPARC64
3690 { "f32", offsetof(CPUState, fpr[32]) },
3691 { "f34", offsetof(CPUState, fpr[34]) },
3692 { "f36", offsetof(CPUState, fpr[36]) },
3693 { "f38", offsetof(CPUState, fpr[38]) },
3694 { "f40", offsetof(CPUState, fpr[40]) },
3695 { "f42", offsetof(CPUState, fpr[42]) },
3696 { "f44", offsetof(CPUState, fpr[44]) },
3697 { "f46", offsetof(CPUState, fpr[46]) },
3698 { "f48", offsetof(CPUState, fpr[48]) },
3699 { "f50", offsetof(CPUState, fpr[50]) },
3700 { "f52", offsetof(CPUState, fpr[52]) },
3701 { "f54", offsetof(CPUState, fpr[54]) },
3702 { "f56", offsetof(CPUState, fpr[56]) },
3703 { "f58", offsetof(CPUState, fpr[58]) },
3704 { "f60", offsetof(CPUState, fpr[60]) },
3705 { "f62", offsetof(CPUState, fpr[62]) },
3706 { "asi", offsetof(CPUState, asi) },
3707 { "pstate", offsetof(CPUState, pstate) },
3708 { "cansave", offsetof(CPUState, cansave) },
3709 { "canrestore", offsetof(CPUState, canrestore) },
3710 { "otherwin", offsetof(CPUState, otherwin) },
3711 { "wstate", offsetof(CPUState, wstate) },
3712 { "cleanwin", offsetof(CPUState, cleanwin) },
3713 { "fprs", offsetof(CPUState, fprs) },
3714#endif
bellard9307c4c2004-04-04 12:57:25 +00003715#endif
3716 { NULL },
3717};
3718
aliguori376253e2009-03-05 23:01:23 +00003719static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003720{
aliguori376253e2009-03-05 23:01:23 +00003721 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003722 longjmp(expr_env, 1);
3723}
3724
Markus Armbruster09b94182010-01-20 13:07:30 +01003725/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003726static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003727{
blueswir18662d652008-10-02 18:32:44 +00003728 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003729 void *ptr;
3730
bellard9307c4c2004-04-04 12:57:25 +00003731 for(md = monitor_defs; md->name != NULL; md++) {
3732 if (compare_cmd(name, md->name)) {
3733 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003734 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003735 } else {
bellard6a00d602005-11-21 23:25:50 +00003736 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003737 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003738 switch(md->type) {
3739 case MD_I32:
3740 *pval = *(int32_t *)ptr;
3741 break;
3742 case MD_TLONG:
3743 *pval = *(target_long *)ptr;
3744 break;
3745 default:
3746 *pval = 0;
3747 break;
3748 }
bellard9307c4c2004-04-04 12:57:25 +00003749 }
3750 return 0;
3751 }
3752 }
3753 return -1;
3754}
3755
3756static void next(void)
3757{
Blue Swirl660f11b2009-07-31 21:16:51 +00003758 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003759 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003760 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003761 pch++;
3762 }
3763}
3764
aliguori376253e2009-03-05 23:01:23 +00003765static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003766
aliguori376253e2009-03-05 23:01:23 +00003767static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003768{
blueswir1c2efc952007-09-25 17:28:42 +00003769 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003770 char *p;
bellard6a00d602005-11-21 23:25:50 +00003771 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003772
3773 switch(*pch) {
3774 case '+':
3775 next();
aliguori376253e2009-03-05 23:01:23 +00003776 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003777 break;
3778 case '-':
3779 next();
aliguori376253e2009-03-05 23:01:23 +00003780 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003781 break;
3782 case '~':
3783 next();
aliguori376253e2009-03-05 23:01:23 +00003784 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003785 break;
3786 case '(':
3787 next();
aliguori376253e2009-03-05 23:01:23 +00003788 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003789 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003790 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003791 }
3792 next();
3793 break;
bellard81d09122004-07-14 17:21:37 +00003794 case '\'':
3795 pch++;
3796 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003797 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003798 n = *pch;
3799 pch++;
3800 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003801 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003802 next();
3803 break;
bellard9307c4c2004-04-04 12:57:25 +00003804 case '$':
3805 {
3806 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003807 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003808
bellard9307c4c2004-04-04 12:57:25 +00003809 pch++;
3810 q = buf;
3811 while ((*pch >= 'a' && *pch <= 'z') ||
3812 (*pch >= 'A' && *pch <= 'Z') ||
3813 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003814 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003815 if ((q - buf) < sizeof(buf) - 1)
3816 *q++ = *pch;
3817 pch++;
3818 }
blueswir1cd390082008-11-16 13:53:32 +00003819 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003820 pch++;
3821 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003822 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003823 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003824 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003825 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003826 }
3827 break;
3828 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003829 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003830 n = 0;
3831 break;
3832 default:
blueswir17743e582007-09-24 18:39:04 +00003833#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003834 n = strtoull(pch, &p, 0);
3835#else
bellard9307c4c2004-04-04 12:57:25 +00003836 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003837#endif
bellard9307c4c2004-04-04 12:57:25 +00003838 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003839 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003840 }
3841 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003842 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003843 pch++;
3844 break;
3845 }
3846 return n;
3847}
3848
3849
aliguori376253e2009-03-05 23:01:23 +00003850static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003851{
blueswir1c2efc952007-09-25 17:28:42 +00003852 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003853 int op;
ths3b46e622007-09-17 08:09:54 +00003854
aliguori376253e2009-03-05 23:01:23 +00003855 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003856 for(;;) {
3857 op = *pch;
3858 if (op != '*' && op != '/' && op != '%')
3859 break;
3860 next();
aliguori376253e2009-03-05 23:01:23 +00003861 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003862 switch(op) {
3863 default:
3864 case '*':
3865 val *= val2;
3866 break;
3867 case '/':
3868 case '%':
ths5fafdf22007-09-16 21:08:06 +00003869 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003870 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003871 if (op == '/')
3872 val /= val2;
3873 else
3874 val %= val2;
3875 break;
3876 }
3877 }
3878 return val;
3879}
3880
aliguori376253e2009-03-05 23:01:23 +00003881static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003882{
blueswir1c2efc952007-09-25 17:28:42 +00003883 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003884 int op;
bellard9307c4c2004-04-04 12:57:25 +00003885
aliguori376253e2009-03-05 23:01:23 +00003886 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003887 for(;;) {
3888 op = *pch;
3889 if (op != '&' && op != '|' && op != '^')
3890 break;
3891 next();
aliguori376253e2009-03-05 23:01:23 +00003892 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003893 switch(op) {
3894 default:
3895 case '&':
3896 val &= val2;
3897 break;
3898 case '|':
3899 val |= val2;
3900 break;
3901 case '^':
3902 val ^= val2;
3903 break;
3904 }
3905 }
3906 return val;
3907}
3908
aliguori376253e2009-03-05 23:01:23 +00003909static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003910{
blueswir1c2efc952007-09-25 17:28:42 +00003911 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003912 int op;
bellard9307c4c2004-04-04 12:57:25 +00003913
aliguori376253e2009-03-05 23:01:23 +00003914 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003915 for(;;) {
3916 op = *pch;
3917 if (op != '+' && op != '-')
3918 break;
3919 next();
aliguori376253e2009-03-05 23:01:23 +00003920 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003921 if (op == '+')
3922 val += val2;
3923 else
3924 val -= val2;
3925 }
3926 return val;
3927}
3928
aliguori376253e2009-03-05 23:01:23 +00003929static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003930{
3931 pch = *pp;
3932 if (setjmp(expr_env)) {
3933 *pp = pch;
3934 return -1;
3935 }
blueswir1cd390082008-11-16 13:53:32 +00003936 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003937 pch++;
aliguori376253e2009-03-05 23:01:23 +00003938 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003939 *pp = pch;
3940 return 0;
3941}
3942
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003943static int get_double(Monitor *mon, double *pval, const char **pp)
3944{
3945 const char *p = *pp;
3946 char *tailp;
3947 double d;
3948
3949 d = strtod(p, &tailp);
3950 if (tailp == p) {
3951 monitor_printf(mon, "Number expected\n");
3952 return -1;
3953 }
3954 if (d != d || d - d != 0) {
3955 /* NaN or infinity */
3956 monitor_printf(mon, "Bad number\n");
3957 return -1;
3958 }
3959 *pval = d;
3960 *pp = tailp;
3961 return 0;
3962}
3963
bellard9307c4c2004-04-04 12:57:25 +00003964static int get_str(char *buf, int buf_size, const char **pp)
3965{
3966 const char *p;
3967 char *q;
3968 int c;
3969
bellard81d09122004-07-14 17:21:37 +00003970 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003971 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003972 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003973 p++;
3974 if (*p == '\0') {
3975 fail:
bellard81d09122004-07-14 17:21:37 +00003976 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003977 *pp = p;
3978 return -1;
3979 }
bellard9307c4c2004-04-04 12:57:25 +00003980 if (*p == '\"') {
3981 p++;
3982 while (*p != '\0' && *p != '\"') {
3983 if (*p == '\\') {
3984 p++;
3985 c = *p++;
3986 switch(c) {
3987 case 'n':
3988 c = '\n';
3989 break;
3990 case 'r':
3991 c = '\r';
3992 break;
3993 case '\\':
3994 case '\'':
3995 case '\"':
3996 break;
3997 default:
3998 qemu_printf("unsupported escape code: '\\%c'\n", c);
3999 goto fail;
4000 }
4001 if ((q - buf) < buf_size - 1) {
4002 *q++ = c;
4003 }
4004 } else {
4005 if ((q - buf) < buf_size - 1) {
4006 *q++ = *p;
4007 }
4008 p++;
4009 }
4010 }
4011 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00004012 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00004013 goto fail;
4014 }
4015 p++;
4016 } else {
blueswir1cd390082008-11-16 13:53:32 +00004017 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004018 if ((q - buf) < buf_size - 1) {
4019 *q++ = *p;
4020 }
4021 p++;
4022 }
bellard9307c4c2004-04-04 12:57:25 +00004023 }
bellard81d09122004-07-14 17:21:37 +00004024 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00004025 *pp = p;
4026 return 0;
4027}
4028
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004029/*
4030 * Store the command-name in cmdname, and return a pointer to
4031 * the remaining of the command string.
4032 */
4033static const char *get_command_name(const char *cmdline,
4034 char *cmdname, size_t nlen)
4035{
4036 size_t len;
4037 const char *p, *pstart;
4038
4039 p = cmdline;
4040 while (qemu_isspace(*p))
4041 p++;
4042 if (*p == '\0')
4043 return NULL;
4044 pstart = p;
4045 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
4046 p++;
4047 len = p - pstart;
4048 if (len > nlen - 1)
4049 len = nlen - 1;
4050 memcpy(cmdname, pstart, len);
4051 cmdname[len] = '\0';
4052 return p;
4053}
4054
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004055/**
4056 * Read key of 'type' into 'key' and return the current
4057 * 'type' pointer.
4058 */
4059static char *key_get_info(const char *type, char **key)
4060{
4061 size_t len;
4062 char *p, *str;
4063
4064 if (*type == ',')
4065 type++;
4066
4067 p = strchr(type, ':');
4068 if (!p) {
4069 *key = NULL;
4070 return NULL;
4071 }
4072 len = p - type;
4073
Anthony Liguori7267c092011-08-20 22:09:37 -05004074 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004075 memcpy(str, type, len);
4076 str[len] = '\0';
4077
4078 *key = str;
4079 return ++p;
4080}
4081
bellard9307c4c2004-04-04 12:57:25 +00004082static int default_fmt_format = 'x';
4083static int default_fmt_size = 4;
4084
4085#define MAX_ARGS 16
4086
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004087static int is_valid_option(const char *c, const char *typestr)
4088{
4089 char option[3];
4090
4091 option[0] = '-';
4092 option[1] = *c;
4093 option[2] = '\0';
4094
4095 typestr = strstr(typestr, option);
4096 return (typestr != NULL);
4097}
4098
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004099static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
4100 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004101{
4102 const mon_cmd_t *cmd;
4103
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004104 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004105 if (compare_cmd(cmdname, cmd->name)) {
4106 return cmd;
4107 }
4108 }
4109
4110 return NULL;
4111}
4112
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004113static const mon_cmd_t *monitor_find_command(const char *cmdname)
4114{
4115 return search_dispatch_table(mon_cmds, cmdname);
4116}
4117
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004118static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
4119{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03004120 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004121}
4122
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004123static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
4124{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03004125 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004126}
4127
Anthony Liguoric227f092009-10-01 16:12:16 -05004128static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004129 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004130 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00004131{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004132 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03004133 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05004134 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00004135 char cmdname[256];
4136 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004137 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00004138
4139#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00004140 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00004141#endif
ths3b46e622007-09-17 08:09:54 +00004142
bellard9307c4c2004-04-04 12:57:25 +00004143 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004144 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
4145 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004146 return NULL;
ths3b46e622007-09-17 08:09:54 +00004147
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004148 cmd = monitor_find_command(cmdname);
4149 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004150 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004151 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004152 }
bellard9307c4c2004-04-04 12:57:25 +00004153
bellard9307c4c2004-04-04 12:57:25 +00004154 /* parse the parameters */
4155 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00004156 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004157 typestr = key_get_info(typestr, &key);
4158 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00004159 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004160 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00004161 typestr++;
4162 switch(c) {
4163 case 'F':
bellard81d09122004-07-14 17:21:37 +00004164 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00004165 case 's':
4166 {
4167 int ret;
ths3b46e622007-09-17 08:09:54 +00004168
blueswir1cd390082008-11-16 13:53:32 +00004169 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004170 p++;
4171 if (*typestr == '?') {
4172 typestr++;
4173 if (*p == '\0') {
4174 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03004175 break;
bellard9307c4c2004-04-04 12:57:25 +00004176 }
4177 }
4178 ret = get_str(buf, sizeof(buf), &p);
4179 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00004180 switch(c) {
4181 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004182 monitor_printf(mon, "%s: filename expected\n",
4183 cmdname);
bellard81d09122004-07-14 17:21:37 +00004184 break;
4185 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004186 monitor_printf(mon, "%s: block device name expected\n",
4187 cmdname);
bellard81d09122004-07-14 17:21:37 +00004188 break;
4189 default:
aliguori376253e2009-03-05 23:01:23 +00004190 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004191 break;
4192 }
bellard9307c4c2004-04-04 12:57:25 +00004193 goto fail;
4194 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004195 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004196 }
4197 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004198 case 'O':
4199 {
4200 QemuOptsList *opts_list;
4201 QemuOpts *opts;
4202
4203 opts_list = qemu_find_opts(key);
4204 if (!opts_list || opts_list->desc->name) {
4205 goto bad_type;
4206 }
4207 while (qemu_isspace(*p)) {
4208 p++;
4209 }
4210 if (!*p)
4211 break;
4212 if (get_str(buf, sizeof(buf), &p) < 0) {
4213 goto fail;
4214 }
4215 opts = qemu_opts_parse(opts_list, buf, 1);
4216 if (!opts) {
4217 goto fail;
4218 }
4219 qemu_opts_to_qdict(opts, qdict);
4220 qemu_opts_del(opts);
4221 }
4222 break;
bellard9307c4c2004-04-04 12:57:25 +00004223 case '/':
4224 {
4225 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004226
blueswir1cd390082008-11-16 13:53:32 +00004227 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004228 p++;
4229 if (*p == '/') {
4230 /* format found */
4231 p++;
4232 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004233 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004234 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004235 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004236 count = count * 10 + (*p - '0');
4237 p++;
4238 }
4239 }
4240 size = -1;
4241 format = -1;
4242 for(;;) {
4243 switch(*p) {
4244 case 'o':
4245 case 'd':
4246 case 'u':
4247 case 'x':
4248 case 'i':
4249 case 'c':
4250 format = *p++;
4251 break;
4252 case 'b':
4253 size = 1;
4254 p++;
4255 break;
4256 case 'h':
4257 size = 2;
4258 p++;
4259 break;
4260 case 'w':
4261 size = 4;
4262 p++;
4263 break;
4264 case 'g':
4265 case 'L':
4266 size = 8;
4267 p++;
4268 break;
4269 default:
4270 goto next;
4271 }
4272 }
4273 next:
blueswir1cd390082008-11-16 13:53:32 +00004274 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004275 monitor_printf(mon, "invalid char in format: '%c'\n",
4276 *p);
bellard9307c4c2004-04-04 12:57:25 +00004277 goto fail;
4278 }
bellard9307c4c2004-04-04 12:57:25 +00004279 if (format < 0)
4280 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004281 if (format != 'i') {
4282 /* for 'i', not specifying a size gives -1 as size */
4283 if (size < 0)
4284 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004285 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004286 }
bellard9307c4c2004-04-04 12:57:25 +00004287 default_fmt_format = format;
4288 } else {
4289 count = 1;
4290 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004291 if (format != 'i') {
4292 size = default_fmt_size;
4293 } else {
4294 size = -1;
4295 }
bellard9307c4c2004-04-04 12:57:25 +00004296 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004297 qdict_put(qdict, "count", qint_from_int(count));
4298 qdict_put(qdict, "format", qint_from_int(format));
4299 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004300 }
4301 break;
4302 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004303 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004304 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004305 {
blueswir1c2efc952007-09-25 17:28:42 +00004306 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004307
blueswir1cd390082008-11-16 13:53:32 +00004308 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004309 p++;
bellard34405572004-06-08 00:55:58 +00004310 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004311 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004312 if (*p == '\0') {
4313 typestr++;
4314 break;
4315 }
bellard34405572004-06-08 00:55:58 +00004316 } else {
4317 if (*p == '.') {
4318 p++;
blueswir1cd390082008-11-16 13:53:32 +00004319 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004320 p++;
bellard34405572004-06-08 00:55:58 +00004321 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004322 typestr++;
4323 break;
bellard34405572004-06-08 00:55:58 +00004324 }
4325 }
bellard13224a82006-07-14 22:03:35 +00004326 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004327 }
aliguori376253e2009-03-05 23:01:23 +00004328 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004329 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004330 /* Check if 'i' is greater than 32-bit */
4331 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4332 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4333 monitor_printf(mon, "integer is for 32-bit values\n");
4334 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004335 } else if (c == 'M') {
4336 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004337 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004338 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004339 }
4340 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004341 case 'o':
4342 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004343 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004344 char *end;
4345
4346 while (qemu_isspace(*p)) {
4347 p++;
4348 }
4349 if (*typestr == '?') {
4350 typestr++;
4351 if (*p == '\0') {
4352 break;
4353 }
4354 }
4355 val = strtosz(p, &end);
4356 if (val < 0) {
4357 monitor_printf(mon, "invalid size\n");
4358 goto fail;
4359 }
4360 qdict_put(qdict, key, qint_from_int(val));
4361 p = end;
4362 }
4363 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004364 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004365 {
4366 double val;
4367
4368 while (qemu_isspace(*p))
4369 p++;
4370 if (*typestr == '?') {
4371 typestr++;
4372 if (*p == '\0') {
4373 break;
4374 }
4375 }
4376 if (get_double(mon, &val, &p) < 0) {
4377 goto fail;
4378 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004379 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004380 switch (*p) {
4381 case 'm':
4382 val /= 1e3; p += 2; break;
4383 case 'u':
4384 val /= 1e6; p += 2; break;
4385 case 'n':
4386 val /= 1e9; p += 2; break;
4387 }
4388 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004389 if (*p && !qemu_isspace(*p)) {
4390 monitor_printf(mon, "Unknown unit suffix\n");
4391 goto fail;
4392 }
4393 qdict_put(qdict, key, qfloat_from_double(val));
4394 }
4395 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004396 case 'b':
4397 {
4398 const char *beg;
4399 int val;
4400
4401 while (qemu_isspace(*p)) {
4402 p++;
4403 }
4404 beg = p;
4405 while (qemu_isgraph(*p)) {
4406 p++;
4407 }
4408 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4409 val = 1;
4410 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4411 val = 0;
4412 } else {
4413 monitor_printf(mon, "Expected 'on' or 'off'\n");
4414 goto fail;
4415 }
4416 qdict_put(qdict, key, qbool_from_int(val));
4417 }
4418 break;
bellard9307c4c2004-04-04 12:57:25 +00004419 case '-':
4420 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004421 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004422 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004423 /* option */
ths3b46e622007-09-17 08:09:54 +00004424
bellard9307c4c2004-04-04 12:57:25 +00004425 c = *typestr++;
4426 if (c == '\0')
4427 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004428 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004429 p++;
bellard9307c4c2004-04-04 12:57:25 +00004430 if (*p == '-') {
4431 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004432 if(c != *p) {
4433 if(!is_valid_option(p, typestr)) {
4434
4435 monitor_printf(mon, "%s: unsupported option -%c\n",
4436 cmdname, *p);
4437 goto fail;
4438 } else {
4439 skip_key = 1;
4440 }
bellard9307c4c2004-04-04 12:57:25 +00004441 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004442 if(skip_key) {
4443 p = tmp;
4444 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004445 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004446 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004447 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004448 }
bellard9307c4c2004-04-04 12:57:25 +00004449 }
bellard9307c4c2004-04-04 12:57:25 +00004450 }
4451 break;
4452 default:
4453 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004454 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004455 goto fail;
4456 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004457 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004458 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004459 }
4460 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004461 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004462 p++;
4463 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004464 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4465 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004466 goto fail;
4467 }
4468
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004469 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004470
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004471fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004472 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004473 return NULL;
4474}
4475
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004476void monitor_set_error(Monitor *mon, QError *qerror)
4477{
4478 /* report only the first error */
4479 if (!mon->error) {
4480 mon->error = qerror;
4481 } else {
4482 MON_DEBUG("Additional error report at %s:%d\n",
4483 qerror->file, qerror->linenr);
4484 QDECREF(qerror);
4485 }
4486}
4487
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004488static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4489{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004490 if (ret && !monitor_has_error(mon)) {
4491 /*
4492 * If it returns failure, it must have passed on error.
4493 *
4494 * Action: Report an internal error to the client if in QMP.
4495 */
4496 qerror_report(QERR_UNDEFINED_ERROR);
4497 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4498 cmd->name);
4499 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004500
4501#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004502 if (!ret && monitor_has_error(mon)) {
4503 /*
4504 * If it returns success, it must not have passed an error.
4505 *
4506 * Action: Report the passed error to the client.
4507 */
4508 MON_DEBUG("command '%s' returned success but passed an error\n",
4509 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004510 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004511
4512 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4513 /*
4514 * Handlers should not call Monitor print functions.
4515 *
4516 * Action: Ignore them in QMP.
4517 *
4518 * (XXX: we don't check any 'info' or 'query' command here
4519 * because the user print function _is_ called by do_info(), hence
4520 * we will trigger this check. This problem will go away when we
4521 * make 'query' commands real and kill do_info())
4522 */
4523 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4524 cmd->name, mon_print_count_get(mon));
4525 }
4526#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004527}
4528
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004529static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004530{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004531 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004532 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004533
4534 qdict = qdict_new();
4535
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004536 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004537 if (!cmd)
4538 goto out;
4539
Luiz Capitulino4903de02010-09-16 11:01:32 -03004540 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004541 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004542 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004543 QObject *data = NULL;
4544
4545 /* XXX: ignores the error code */
4546 cmd->mhandler.cmd_new(mon, qdict, &data);
4547 assert(!monitor_has_error(mon));
4548 if (data) {
4549 cmd->user_print(mon, data);
4550 qobject_decref(data);
4551 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004552 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004553 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004554 }
4555
Luiz Capitulino13917be2009-10-07 13:41:54 -03004556out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004557 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004558}
4559
bellard81d09122004-07-14 17:21:37 +00004560static void cmd_completion(const char *name, const char *list)
4561{
4562 const char *p, *pstart;
4563 char cmd[128];
4564 int len;
4565
4566 p = list;
4567 for(;;) {
4568 pstart = p;
4569 p = strchr(p, '|');
4570 if (!p)
4571 p = pstart + strlen(pstart);
4572 len = p - pstart;
4573 if (len > sizeof(cmd) - 2)
4574 len = sizeof(cmd) - 2;
4575 memcpy(cmd, pstart, len);
4576 cmd[len] = '\0';
4577 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004578 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004579 }
4580 if (*p == '\0')
4581 break;
4582 p++;
4583 }
4584}
4585
4586static void file_completion(const char *input)
4587{
4588 DIR *ffs;
4589 struct dirent *d;
4590 char path[1024];
4591 char file[1024], file_prefix[1024];
4592 int input_path_len;
4593 const char *p;
4594
ths5fafdf22007-09-16 21:08:06 +00004595 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004596 if (!p) {
4597 input_path_len = 0;
4598 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004599 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004600 } else {
4601 input_path_len = p - input + 1;
4602 memcpy(path, input, input_path_len);
4603 if (input_path_len > sizeof(path) - 1)
4604 input_path_len = sizeof(path) - 1;
4605 path[input_path_len] = '\0';
4606 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4607 }
4608#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004609 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4610 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004611#endif
4612 ffs = opendir(path);
4613 if (!ffs)
4614 return;
4615 for(;;) {
4616 struct stat sb;
4617 d = readdir(ffs);
4618 if (!d)
4619 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004620
4621 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4622 continue;
4623 }
4624
bellard81d09122004-07-14 17:21:37 +00004625 if (strstart(d->d_name, file_prefix, NULL)) {
4626 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004627 if (input_path_len < sizeof(file))
4628 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4629 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004630 /* stat the file to find out if it's a directory.
4631 * In that case add a slash to speed up typing long paths
4632 */
4633 stat(file, &sb);
4634 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004635 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004636 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004637 }
4638 }
4639 closedir(ffs);
4640}
4641
aliguori51de9762009-03-05 23:00:43 +00004642static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004643{
aliguori51de9762009-03-05 23:00:43 +00004644 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004645 const char *input = opaque;
4646
4647 if (input[0] == '\0' ||
4648 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004649 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004650 }
4651}
4652
4653/* NOTE: this parser is an approximate form of the real command parser */
4654static void parse_cmdline(const char *cmdline,
4655 int *pnb_args, char **args)
4656{
4657 const char *p;
4658 int nb_args, ret;
4659 char buf[1024];
4660
4661 p = cmdline;
4662 nb_args = 0;
4663 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004664 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004665 p++;
4666 if (*p == '\0')
4667 break;
4668 if (nb_args >= MAX_ARGS)
4669 break;
4670 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004671 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004672 nb_args++;
4673 if (ret < 0)
4674 break;
4675 }
4676 *pnb_args = nb_args;
4677}
4678
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004679static const char *next_arg_type(const char *typestr)
4680{
4681 const char *p = strchr(typestr, ':');
4682 return (p != NULL ? ++p : typestr);
4683}
4684
aliguori4c36ba32009-03-05 23:01:37 +00004685static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004686{
4687 const char *cmdname;
4688 char *args[MAX_ARGS];
4689 int nb_args, i, len;
4690 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004691 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004692 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004693
4694 parse_cmdline(cmdline, &nb_args, args);
4695#ifdef DEBUG_COMPLETION
4696 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004697 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004698 }
4699#endif
4700
4701 /* if the line ends with a space, it means we want to complete the
4702 next arg */
4703 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004704 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004705 if (nb_args >= MAX_ARGS) {
4706 goto cleanup;
4707 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004708 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004709 }
4710 if (nb_args <= 1) {
4711 /* command completion */
4712 if (nb_args == 0)
4713 cmdname = "";
4714 else
4715 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004716 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004717 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004718 cmd_completion(cmdname, cmd->name);
4719 }
4720 } else {
4721 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004722 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4723 if (compare_cmd(args[0], cmd->name)) {
4724 break;
4725 }
bellard81d09122004-07-14 17:21:37 +00004726 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004727 if (!cmd->name) {
4728 goto cleanup;
4729 }
4730
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004731 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004732 for(i = 0; i < nb_args - 2; i++) {
4733 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004734 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004735 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004736 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004737 }
4738 }
4739 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004740 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004741 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004742 }
bellard81d09122004-07-14 17:21:37 +00004743 switch(*ptype) {
4744 case 'F':
4745 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004746 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004747 file_completion(str);
4748 break;
4749 case 'B':
4750 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004751 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004752 bdrv_iterate(block_completion_it, (void *)str);
4753 break;
bellard7fe48482004-10-09 18:08:01 +00004754 case 's':
4755 /* XXX: more generic ? */
4756 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004757 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004758 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4759 cmd_completion(str, cmd->name);
4760 }
bellard64866c32006-05-07 18:03:31 +00004761 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004762 char *sep = strrchr(str, '-');
4763 if (sep)
4764 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004765 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004766 for(key = key_defs; key->name != NULL; key++) {
4767 cmd_completion(str, key->name);
4768 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004769 } else if (!strcmp(cmd->name, "help|?")) {
4770 readline_set_completion_index(cur_mon->rs, strlen(str));
4771 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4772 cmd_completion(str, cmd->name);
4773 }
bellard7fe48482004-10-09 18:08:01 +00004774 }
4775 break;
bellard81d09122004-07-14 17:21:37 +00004776 default:
4777 break;
4778 }
4779 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004780
4781cleanup:
4782 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004783 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004784 }
bellard81d09122004-07-14 17:21:37 +00004785}
4786
aliguori731b0362009-03-05 23:01:42 +00004787static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004788{
aliguori731b0362009-03-05 23:01:42 +00004789 Monitor *mon = opaque;
4790
Jan Kiszkac62313b2009-12-04 14:05:29 +01004791 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004792}
4793
Luiz Capitulino09069b12010-02-04 18:10:06 -02004794static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4795{
4796 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4797 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4798}
4799
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004800/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004801 * Argument validation rules:
4802 *
4803 * 1. The argument must exist in cmd_args qdict
4804 * 2. The argument type must be the expected one
4805 *
4806 * Special case: If the argument doesn't exist in cmd_args and
4807 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4808 * checking is skipped for it.
4809 */
4810static int check_client_args_type(const QDict *client_args,
4811 const QDict *cmd_args, int flags)
4812{
4813 const QDictEntry *ent;
4814
4815 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4816 QObject *obj;
4817 QString *arg_type;
4818 const QObject *client_arg = qdict_entry_value(ent);
4819 const char *client_arg_name = qdict_entry_key(ent);
4820
4821 obj = qdict_get(cmd_args, client_arg_name);
4822 if (!obj) {
4823 if (flags & QMP_ACCEPT_UNKNOWNS) {
4824 /* handler accepts unknowns */
4825 continue;
4826 }
4827 /* client arg doesn't exist */
4828 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4829 return -1;
4830 }
4831
4832 arg_type = qobject_to_qstring(obj);
4833 assert(arg_type != NULL);
4834
4835 /* check if argument's type is correct */
4836 switch (qstring_get_str(arg_type)[0]) {
4837 case 'F':
4838 case 'B':
4839 case 's':
4840 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4841 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4842 "string");
4843 return -1;
4844 }
4845 break;
4846 case 'i':
4847 case 'l':
4848 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004849 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004850 if (qobject_type(client_arg) != QTYPE_QINT) {
4851 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4852 "int");
4853 return -1;
4854 }
4855 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004856 case 'T':
4857 if (qobject_type(client_arg) != QTYPE_QINT &&
4858 qobject_type(client_arg) != QTYPE_QFLOAT) {
4859 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4860 "number");
4861 return -1;
4862 }
4863 break;
4864 case 'b':
4865 case '-':
4866 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4867 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4868 "bool");
4869 return -1;
4870 }
4871 break;
4872 case 'O':
4873 assert(flags & QMP_ACCEPT_UNKNOWNS);
4874 break;
4875 case '/':
4876 case '.':
4877 /*
4878 * These types are not supported by QMP and thus are not
4879 * handled here. Fall through.
4880 */
4881 default:
4882 abort();
4883 }
4884 }
4885
4886 return 0;
4887}
4888
4889/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004890 * - Check if the client has passed all mandatory args
4891 * - Set special flags for argument validation
4892 */
4893static int check_mandatory_args(const QDict *cmd_args,
4894 const QDict *client_args, int *flags)
4895{
4896 const QDictEntry *ent;
4897
4898 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4899 const char *cmd_arg_name = qdict_entry_key(ent);
4900 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4901 assert(type != NULL);
4902
4903 if (qstring_get_str(type)[0] == 'O') {
4904 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4905 *flags |= QMP_ACCEPT_UNKNOWNS;
4906 } else if (qstring_get_str(type)[0] != '-' &&
4907 qstring_get_str(type)[1] != '?' &&
4908 !qdict_haskey(client_args, cmd_arg_name)) {
4909 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4910 return -1;
4911 }
4912 }
4913
4914 return 0;
4915}
4916
4917static QDict *qdict_from_args_type(const char *args_type)
4918{
4919 int i;
4920 QDict *qdict;
4921 QString *key, *type, *cur_qs;
4922
4923 assert(args_type != NULL);
4924
4925 qdict = qdict_new();
4926
4927 if (args_type == NULL || args_type[0] == '\0') {
4928 /* no args, empty qdict */
4929 goto out;
4930 }
4931
4932 key = qstring_new();
4933 type = qstring_new();
4934
4935 cur_qs = key;
4936
4937 for (i = 0;; i++) {
4938 switch (args_type[i]) {
4939 case ',':
4940 case '\0':
4941 qdict_put(qdict, qstring_get_str(key), type);
4942 QDECREF(key);
4943 if (args_type[i] == '\0') {
4944 goto out;
4945 }
4946 type = qstring_new(); /* qdict has ref */
4947 cur_qs = key = qstring_new();
4948 break;
4949 case ':':
4950 cur_qs = type;
4951 break;
4952 default:
4953 qstring_append_chr(cur_qs, args_type[i]);
4954 break;
4955 }
4956 }
4957
4958out:
4959 return qdict;
4960}
4961
4962/*
4963 * Client argument checking rules:
4964 *
4965 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004966 * 2. Each argument provided by the client must be expected
4967 * 3. Each argument provided by the client must have the type expected
4968 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004969 */
4970static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4971{
4972 int flags, err;
4973 QDict *cmd_args;
4974
4975 cmd_args = qdict_from_args_type(cmd->args_type);
4976
4977 flags = 0;
4978 err = check_mandatory_args(cmd_args, client_args, &flags);
4979 if (err) {
4980 goto out;
4981 }
4982
Luiz Capitulino4af91932010-06-22 11:44:05 -03004983 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004984
4985out:
4986 QDECREF(cmd_args);
4987 return err;
4988}
4989
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004990/*
4991 * Input object checking rules
4992 *
4993 * 1. Input object must be a dict
4994 * 2. The "execute" key must exist
4995 * 3. The "execute" key must be a string
4996 * 4. If the "arguments" key exists, it must be a dict
4997 * 5. If the "id" key exists, it can be anything (ie. json-value)
4998 * 6. Any argument not listed above is considered invalid
4999 */
5000static QDict *qmp_check_input_obj(QObject *input_obj)
5001{
5002 const QDictEntry *ent;
5003 int has_exec_key = 0;
5004 QDict *input_dict;
5005
5006 if (qobject_type(input_obj) != QTYPE_QDICT) {
5007 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
5008 return NULL;
5009 }
5010
5011 input_dict = qobject_to_qdict(input_obj);
5012
5013 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
5014 const char *arg_name = qdict_entry_key(ent);
5015 const QObject *arg_obj = qdict_entry_value(ent);
5016
5017 if (!strcmp(arg_name, "execute")) {
5018 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
5019 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
5020 "string");
5021 return NULL;
5022 }
5023 has_exec_key = 1;
5024 } else if (!strcmp(arg_name, "arguments")) {
5025 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5026 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5027 "object");
5028 return NULL;
5029 }
5030 } else if (!strcmp(arg_name, "id")) {
5031 /* FIXME: check duplicated IDs for async commands */
5032 } else {
5033 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5034 return NULL;
5035 }
5036 }
5037
5038 if (!has_exec_key) {
5039 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5040 return NULL;
5041 }
5042
5043 return input_dict;
5044}
5045
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005046static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
5047{
5048 QObject *ret_data = NULL;
5049
Luiz Capitulino4903de02010-09-16 11:01:32 -03005050 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005051 qmp_async_info_handler(mon, cmd);
5052 if (monitor_has_error(mon)) {
5053 monitor_protocol_emitter(mon, NULL);
5054 }
5055 } else {
5056 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02005057 monitor_protocol_emitter(mon, ret_data);
5058 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005059 }
5060}
5061
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005062static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5063 const QDict *params)
5064{
5065 int ret;
5066 QObject *data = NULL;
5067
5068 mon_print_count_init(mon);
5069
5070 ret = cmd->mhandler.cmd_new(mon, params, &data);
5071 handler_audit(mon, cmd, ret);
5072 monitor_protocol_emitter(mon, data);
5073 qobject_decref(data);
5074}
5075
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005076static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5077{
5078 int err;
5079 QObject *obj;
5080 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005081 const mon_cmd_t *cmd;
5082 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005083 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005084
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005085 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005086 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005087
5088 obj = json_parser_parse(tokens, NULL);
5089 if (!obj) {
5090 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005091 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005092 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005093 }
5094
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005095 input = qmp_check_input_obj(obj);
5096 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005097 qobject_decref(obj);
5098 goto err_out;
5099 }
5100
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005101 mon->mc->id = qdict_get(input, "id");
5102 qobject_incref(mon->mc->id);
5103
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005104 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02005105 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005106 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005107 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005108 }
5109
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005110 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005111 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02005112 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03005113 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03005114 }
5115
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005116 if (!cmd) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005117 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5118 goto err_out;
5119 }
5120
5121 obj = qdict_get(input, "arguments");
5122 if (!obj) {
5123 args = qdict_new();
5124 } else {
5125 args = qobject_to_qdict(obj);
5126 QINCREF(args);
5127 }
5128
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005129 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005130 if (err < 0) {
5131 goto err_out;
5132 }
5133
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005134 if (query_cmd) {
5135 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03005136 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005137 err = qmp_async_cmd_handler(mon, cmd, args);
5138 if (err) {
5139 /* emit the error response */
5140 goto err_out;
5141 }
Adam Litke940cc302010-01-25 12:18:44 -06005142 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005143 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005144 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005145
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005146 goto out;
5147
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005148err_out:
5149 monitor_protocol_emitter(mon, NULL);
5150out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005151 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005152 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005153}
5154
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005155/**
5156 * monitor_control_read(): Read and handle QMP input
5157 */
5158static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5159{
5160 Monitor *old_mon = cur_mon;
5161
5162 cur_mon = opaque;
5163
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005164 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005165
5166 cur_mon = old_mon;
5167}
5168
aliguori731b0362009-03-05 23:01:42 +00005169static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005170{
aliguori731b0362009-03-05 23:01:42 +00005171 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005172 int i;
aliguori376253e2009-03-05 23:01:23 +00005173
aliguori731b0362009-03-05 23:01:42 +00005174 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005175
aliguoricde76ee2009-03-05 23:01:51 +00005176 if (cur_mon->rs) {
5177 for (i = 0; i < size; i++)
5178 readline_handle_byte(cur_mon->rs, buf[i]);
5179 } else {
5180 if (size == 0 || buf[size - 1] != 0)
5181 monitor_printf(cur_mon, "corrupted command\n");
5182 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005183 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005184 }
aliguori731b0362009-03-05 23:01:42 +00005185
5186 cur_mon = old_mon;
5187}
aliguorid8f44602008-10-06 13:52:44 +00005188
aliguori376253e2009-03-05 23:01:23 +00005189static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005190{
aliguori731b0362009-03-05 23:01:42 +00005191 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005192 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005193 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005194}
5195
aliguoricde76ee2009-03-05 23:01:51 +00005196int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005197{
aliguoricde76ee2009-03-05 23:01:51 +00005198 if (!mon->rs)
5199 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005200 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005201 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005202}
5203
aliguori376253e2009-03-05 23:01:23 +00005204void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005205{
aliguoricde76ee2009-03-05 23:01:51 +00005206 if (!mon->rs)
5207 return;
aliguori731b0362009-03-05 23:01:42 +00005208 if (--mon->suspend_cnt == 0)
5209 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005210}
5211
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005212static QObject *get_qmp_greeting(void)
5213{
5214 QObject *ver;
5215
5216 do_info_version(NULL, &ver);
5217 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5218}
5219
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005220/**
5221 * monitor_control_event(): Print QMP gretting
5222 */
5223static void monitor_control_event(void *opaque, int event)
5224{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005225 QObject *data;
5226 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005227
Luiz Capitulino47116d12010-02-08 17:01:30 -02005228 switch (event) {
5229 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005230 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005231 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005232 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005233 monitor_json_emitter(mon, data);
5234 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005235 break;
5236 case CHR_EVENT_CLOSED:
5237 json_message_parser_destroy(&mon->mc->parser);
5238 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005239 }
5240}
5241
aliguori731b0362009-03-05 23:01:42 +00005242static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005243{
aliguori376253e2009-03-05 23:01:23 +00005244 Monitor *mon = opaque;
5245
aliguori2724b182009-03-05 23:01:47 +00005246 switch (event) {
5247 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005248 mon->mux_out = 0;
5249 if (mon->reset_seen) {
5250 readline_restart(mon->rs);
5251 monitor_resume(mon);
5252 monitor_flush(mon);
5253 } else {
5254 mon->suspend_cnt = 0;
5255 }
aliguori2724b182009-03-05 23:01:47 +00005256 break;
ths86e94de2007-01-05 22:01:59 +00005257
aliguori2724b182009-03-05 23:01:47 +00005258 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005259 if (mon->reset_seen) {
5260 if (mon->suspend_cnt == 0) {
5261 monitor_printf(mon, "\n");
5262 }
5263 monitor_flush(mon);
5264 monitor_suspend(mon);
5265 } else {
5266 mon->suspend_cnt++;
5267 }
5268 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005269 break;
5270
Amit Shahb6b8df52009-10-07 18:31:16 +05305271 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005272 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5273 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005274 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005275 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005276 }
5277 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005278 break;
5279 }
ths86e94de2007-01-05 22:01:59 +00005280}
5281
aliguori76655d62009-03-06 20:27:37 +00005282
5283/*
5284 * Local variables:
5285 * c-indent-level: 4
5286 * c-basic-offset: 4
5287 * tab-width: 8
5288 * End:
5289 */
5290
aliguori731b0362009-03-05 23:01:42 +00005291void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005292{
aliguori731b0362009-03-05 23:01:42 +00005293 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005294 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005295
5296 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005297 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005298 is_first_init = 0;
5299 }
aliguori87127162009-03-05 23:01:29 +00005300
Anthony Liguori7267c092011-08-20 22:09:37 -05005301 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005302
aliguori87127162009-03-05 23:01:29 +00005303 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005304 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005305 if (flags & MONITOR_USE_READLINE) {
5306 mon->rs = readline_init(mon, monitor_find_completion);
5307 monitor_read_command(mon, 0);
5308 }
aliguori87127162009-03-05 23:01:29 +00005309
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005310 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005311 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005312 /* Control mode requires special handlers */
5313 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5314 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005315 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005316 } else {
5317 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5318 monitor_event, mon);
5319 }
aliguori87127162009-03-05 23:01:29 +00005320
Blue Swirl72cf2d42009-09-12 07:36:22 +00005321 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005322 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5323 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005324}
5325
aliguori376253e2009-03-05 23:01:23 +00005326static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005327{
aliguoribb5fc202009-03-05 23:01:15 +00005328 BlockDriverState *bs = opaque;
5329 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005330
aliguoribb5fc202009-03-05 23:01:15 +00005331 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005332 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005333 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005334 }
aliguori731b0362009-03-05 23:01:42 +00005335 if (mon->password_completion_cb)
5336 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005337
aliguori731b0362009-03-05 23:01:42 +00005338 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005339}
aliguoric0f4ce72009-03-05 23:01:01 +00005340
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005341int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5342 BlockDriverCompletionFunc *completion_cb,
5343 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005344{
aliguoricde76ee2009-03-05 23:01:51 +00005345 int err;
5346
aliguoribb5fc202009-03-05 23:01:15 +00005347 if (!bdrv_key_required(bs)) {
5348 if (completion_cb)
5349 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005350 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005351 }
aliguoric0f4ce72009-03-05 23:01:01 +00005352
Luiz Capitulino94171e12009-12-07 21:37:00 +01005353 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005354 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005355 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005356 }
5357
aliguori376253e2009-03-05 23:01:23 +00005358 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5359 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005360
aliguori731b0362009-03-05 23:01:42 +00005361 mon->password_completion_cb = completion_cb;
5362 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005363
aliguoricde76ee2009-03-05 23:01:51 +00005364 err = monitor_read_password(mon, bdrv_password_cb, bs);
5365
5366 if (err && completion_cb)
5367 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005368
5369 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005370}