blob: cf594427544aad3127e0b6f8787a7f6a18ed88d4 [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"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
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
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100373 trace_monitor_protocol_emitter(mon);
374
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200375 qmp = qdict_new();
376
377 if (!monitor_has_error(mon)) {
378 /* success response */
379 if (data) {
380 qobject_incref(data);
381 qdict_put_obj(qmp, "return", data);
382 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200383 /* return an empty QDict by default */
384 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200385 }
386 } else {
387 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100388 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200389 qdict_put(qmp, "error", mon->error->error);
390 QINCREF(mon->error->error);
391 QDECREF(mon->error);
392 mon->error = NULL;
393 }
394
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200395 if (mon->mc->id) {
396 qdict_put_obj(qmp, "id", mon->mc->id);
397 mon->mc->id = NULL;
398 }
399
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200400 monitor_json_emitter(mon, QOBJECT(qmp));
401 QDECREF(qmp);
402}
403
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200404static void timestamp_put(QDict *qdict)
405{
406 int err;
407 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000408 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409
Blue Swirld08d6f02009-12-04 18:06:39 +0000410 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200411 if (err < 0)
412 return;
413
414 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
415 "'microseconds': %" PRId64 " }",
416 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200417 qdict_put_obj(qdict, "timestamp", obj);
418}
419
420/**
421 * monitor_protocol_event(): Generate a Monitor event
422 *
423 * Event-specific data can be emitted through the (optional) 'data' parameter.
424 */
425void monitor_protocol_event(MonitorEvent event, QObject *data)
426{
427 QDict *qmp;
428 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600429 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200430
Blue Swirl242cd002009-12-04 18:05:45 +0000431 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200432
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200433 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000434 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200435 event_name = "SHUTDOWN";
436 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000437 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200438 event_name = "RESET";
439 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000440 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200441 event_name = "POWERDOWN";
442 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000443 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200444 event_name = "STOP";
445 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300446 case QEVENT_RESUME:
447 event_name = "RESUME";
448 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200449 case QEVENT_VNC_CONNECTED:
450 event_name = "VNC_CONNECTED";
451 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200452 case QEVENT_VNC_INITIALIZED:
453 event_name = "VNC_INITIALIZED";
454 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200455 case QEVENT_VNC_DISCONNECTED:
456 event_name = "VNC_DISCONNECTED";
457 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200458 case QEVENT_BLOCK_IO_ERROR:
459 event_name = "BLOCK_IO_ERROR";
460 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300461 case QEVENT_RTC_CHANGE:
462 event_name = "RTC_CHANGE";
463 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300464 case QEVENT_WATCHDOG:
465 event_name = "WATCHDOG";
466 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200467 case QEVENT_SPICE_CONNECTED:
468 event_name = "SPICE_CONNECTED";
469 break;
470 case QEVENT_SPICE_INITIALIZED:
471 event_name = "SPICE_INITIALIZED";
472 break;
473 case QEVENT_SPICE_DISCONNECTED:
474 event_name = "SPICE_DISCONNECTED";
475 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200476 default:
477 abort();
478 break;
479 }
480
481 qmp = qdict_new();
482 timestamp_put(qmp);
483 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200484 if (data) {
485 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200486 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200487 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200488
Adam Litkef039a562010-01-15 08:34:02 -0600489 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200490 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200491 monitor_json_emitter(mon, QOBJECT(qmp));
492 }
Adam Litkef039a562010-01-15 08:34:02 -0600493 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200494 QDECREF(qmp);
495}
496
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200497static int do_qmp_capabilities(Monitor *mon, const QDict *params,
498 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200499{
500 /* Will setup QMP capabilities in the future */
501 if (monitor_ctrl_mode(mon)) {
502 mon->mc->command_mode = 1;
503 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200504
505 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200506}
507
Luiz Capitulino0268d972010-10-22 10:08:02 -0200508static int mon_set_cpu(int cpu_index);
509static void handle_user_command(Monitor *mon, const char *cmdline);
510
511static int do_hmp_passthrough(Monitor *mon, const QDict *params,
512 QObject **ret_data)
513{
514 int ret = 0;
515 Monitor *old_mon, hmp;
516 CharDriverState mchar;
517
518 memset(&hmp, 0, sizeof(hmp));
519 qemu_chr_init_mem(&mchar);
520 hmp.chr = &mchar;
521
522 old_mon = cur_mon;
523 cur_mon = &hmp;
524
525 if (qdict_haskey(params, "cpu-index")) {
526 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
527 if (ret < 0) {
528 cur_mon = old_mon;
529 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
530 goto out;
531 }
532 }
533
534 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
535 cur_mon = old_mon;
536
537 if (qemu_chr_mem_osize(hmp.chr) > 0) {
538 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
539 }
540
541out:
542 qemu_chr_close_mem(hmp.chr);
543 return ret;
544}
545
bellard9dc39cb2004-03-14 21:38:27 +0000546static int compare_cmd(const char *name, const char *list)
547{
548 const char *p, *pstart;
549 int len;
550 len = strlen(name);
551 p = list;
552 for(;;) {
553 pstart = p;
554 p = strchr(p, '|');
555 if (!p)
556 p = pstart + strlen(pstart);
557 if ((p - pstart) == len && !memcmp(pstart, name, len))
558 return 1;
559 if (*p == '\0')
560 break;
561 p++;
562 }
563 return 0;
564}
565
Anthony Liguoric227f092009-10-01 16:12:16 -0500566static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000567 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000568{
Anthony Liguoric227f092009-10-01 16:12:16 -0500569 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000570
571 for(cmd = cmds; cmd->name != NULL; cmd++) {
572 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000573 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
574 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000575 }
576}
577
aliguori376253e2009-03-05 23:01:23 +0000578static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000579{
580 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000581 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000582 } else {
aliguori376253e2009-03-05 23:01:23 +0000583 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000584 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000585 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000586 monitor_printf(mon, "Log items (comma separated):\n");
587 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000588 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000589 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000590 }
591 }
bellard9dc39cb2004-03-14 21:38:27 +0000592 }
593}
594
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300595static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300596{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300597 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300598}
599
Lluísfc764102011-08-31 20:31:18 +0200600static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530601{
602 const char *tp_name = qdict_get_str(qdict, "name");
603 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200604 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000605
606 if (!ret) {
607 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
608 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530609}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100610
Michael Rothc45a8162011-10-02 08:44:37 -0500611#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100612static void do_trace_file(Monitor *mon, const QDict *qdict)
613{
614 const char *op = qdict_get_try_str(qdict, "op");
615 const char *arg = qdict_get_try_str(qdict, "arg");
616
617 if (!op) {
618 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
619 } else if (!strcmp(op, "on")) {
620 st_set_trace_file_enabled(true);
621 } else if (!strcmp(op, "off")) {
622 st_set_trace_file_enabled(false);
623 } else if (!strcmp(op, "flush")) {
624 st_flush_trace_buffer();
625 } else if (!strcmp(op, "set")) {
626 if (arg) {
627 st_set_trace_file(arg);
628 }
629 } else {
630 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
631 help_cmd(mon, "trace-file");
632 }
633}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530634#endif
635
Adam Litke940cc302010-01-25 12:18:44 -0600636static void user_monitor_complete(void *opaque, QObject *ret_data)
637{
638 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
639
640 if (ret_data) {
641 data->user_print(data->mon, ret_data);
642 }
643 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500644 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600645}
646
647static void qmp_monitor_complete(void *opaque, QObject *ret_data)
648{
649 monitor_protocol_emitter(opaque, ret_data);
650}
651
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300652static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
653 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600654{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300655 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600656}
657
658static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
659{
660 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
661}
662
663static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
664 const QDict *params)
665{
666 int ret;
667
Anthony Liguori7267c092011-08-20 22:09:37 -0500668 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600669 cb_data->mon = mon;
670 cb_data->user_print = cmd->user_print;
671 monitor_suspend(mon);
672 ret = cmd->mhandler.cmd_async(mon, params,
673 user_monitor_complete, cb_data);
674 if (ret < 0) {
675 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500676 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600677 }
678}
679
680static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
681{
682 int ret;
683
Anthony Liguori7267c092011-08-20 22:09:37 -0500684 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600685 cb_data->mon = mon;
686 cb_data->user_print = cmd->user_print;
687 monitor_suspend(mon);
688 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
689 if (ret < 0) {
690 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500691 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600692 }
693}
694
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300695static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000696{
Anthony Liguoric227f092009-10-01 16:12:16 -0500697 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300698 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000699
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200700 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000701 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200702 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300703
704 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000705 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300706 break;
bellard9dc39cb2004-03-14 21:38:27 +0000707 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300708
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200709 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300710 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200711 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300712
Luiz Capitulino4903de02010-09-16 11:01:32 -0300713 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300714 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300715 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300716 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200717
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300718 cmd->mhandler.info_new(mon, &info_data);
719 if (info_data) {
720 cmd->user_print(mon, info_data);
721 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200722 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300723 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300724 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300725 }
726
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300727 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300728
729help:
730 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000731}
732
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200733static void do_info_version_print(Monitor *mon, const QObject *data)
734{
735 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300736 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200737
738 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300739 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200740
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300741 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
742 qdict_get_int(qemu, "major"),
743 qdict_get_int(qemu, "minor"),
744 qdict_get_int(qemu, "micro"),
745 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200746}
747
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300748static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000749{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300750 const char *version = QEMU_VERSION;
751 int major = 0, minor = 0, micro = 0;
752 char *tmp;
753
754 major = strtol(version, &tmp, 10);
755 tmp++;
756 minor = strtol(tmp, &tmp, 10);
757 tmp++;
758 micro = strtol(tmp, &tmp, 10);
759
760 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
761 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000762}
763
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200764static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000765{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200766 QDict *qdict;
767
768 qdict = qobject_to_qdict(data);
769 if (qdict_size(qdict) == 0) {
770 return;
771 }
772
773 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
774}
775
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200776static void do_info_name(Monitor *mon, QObject **ret_data)
777{
778 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
779 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000780}
781
Luiz Capitulino1a728672009-12-10 17:15:56 -0200782static QObject *get_cmd_dict(const char *name)
783{
784 const char *p;
785
786 /* Remove '|' from some commands */
787 p = strchr(name, '|');
788 if (p) {
789 p++;
790 } else {
791 p = name;
792 }
793
794 return qobject_from_jsonf("{ 'name': %s }", p);
795}
796
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200797static void do_info_commands(Monitor *mon, QObject **ret_data)
798{
799 QList *cmd_list;
800 const mon_cmd_t *cmd;
801
802 cmd_list = qlist_new();
803
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300804 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300805 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200806 }
807
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300808 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300809 char buf[128];
810 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
811 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200812 }
813
814 *ret_data = QOBJECT(cmd_list);
815}
816
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200817static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000818{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200819 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
820}
821
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200822static void do_info_uuid(Monitor *mon, QObject **ret_data)
823{
824 char uuid[64];
825
826 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000827 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
828 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
829 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
830 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200831 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000832}
833
bellard6a00d602005-11-21 23:25:50 +0000834/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000835static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000836{
837 CPUState *env;
838
839 for(env = first_cpu; env != NULL; env = env->next_cpu) {
840 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000841 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000842 return 0;
843 }
844 }
845 return -1;
846}
847
pbrook9596ebb2007-11-18 01:44:38 +0000848static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000849{
aliguori731b0362009-03-05 23:01:42 +0000850 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000851 mon_set_cpu(0);
852 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300853 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000854 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000855}
856
aliguori376253e2009-03-05 23:01:23 +0000857static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000858{
bellard6a00d602005-11-21 23:25:50 +0000859 CPUState *env;
860 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000861#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000862 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000863 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000864#else
aliguori376253e2009-03-05 23:01:23 +0000865 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000866 0);
bellard9307c4c2004-04-04 12:57:25 +0000867#endif
868}
869
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300870static void print_cpu_iter(QObject *obj, void *opaque)
871{
872 QDict *cpu;
873 int active = ' ';
874 Monitor *mon = opaque;
875
876 assert(qobject_type(obj) == QTYPE_QDICT);
877 cpu = qobject_to_qdict(obj);
878
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200879 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200881 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300882
883 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
884
885#if defined(TARGET_I386)
886 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
887 (target_ulong) qdict_get_int(cpu, "pc"));
888#elif defined(TARGET_PPC)
889 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
890 (target_long) qdict_get_int(cpu, "nip"));
891#elif defined(TARGET_SPARC)
Nathan Kunkee3f9c3592011-09-08 21:58:31 -0500892 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300893 (target_long) qdict_get_int(cpu, "pc"));
894 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
895 (target_long) qdict_get_int(cpu, "npc"));
896#elif defined(TARGET_MIPS)
897 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
898 (target_long) qdict_get_int(cpu, "PC"));
899#endif
900
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200901 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300902 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200903 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300904
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100905 monitor_printf(mon, " thread_id=%" PRId64 " ",
906 qdict_get_int(cpu, "thread_id"));
907
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300908 monitor_printf(mon, "\n");
909}
910
911static void monitor_print_cpus(Monitor *mon, const QObject *data)
912{
913 QList *cpu_list;
914
915 assert(qobject_type(data) == QTYPE_QLIST);
916 cpu_list = qobject_to_qlist(data);
917 qlist_iter(cpu_list, print_cpu_iter, mon);
918}
919
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300920static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000921{
922 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300923 QList *cpu_list;
924
925 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000926
927 /* just to set the default cpu if not already done */
928 mon_get_cpu();
929
930 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200931 QDict *cpu;
932 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300933
Avi Kivity4c0960c2009-08-17 23:19:53 +0300934 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300935
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200936 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
937 env->cpu_index, env == mon->mon_cpu,
938 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200939
940 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300941
bellard6a00d602005-11-21 23:25:50 +0000942#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300943 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000944#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300945 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000946#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300947 qdict_put(cpu, "pc", qint_from_int(env->pc));
948 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000949#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300950 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000951#endif
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100952 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300953
954 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000955 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300956
957 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000958}
959
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200960static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000961{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300962 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200963 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100964 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
965 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200966 return -1;
967 }
968 return 0;
bellard6a00d602005-11-21 23:25:50 +0000969}
970
aliguori376253e2009-03-05 23:01:23 +0000971static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000972{
aliguori376253e2009-03-05 23:01:23 +0000973 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000974}
975
aliguori376253e2009-03-05 23:01:23 +0000976static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000977{
978 int i;
bellard7e2515e2004-08-01 21:52:19 +0000979 const char *str;
ths3b46e622007-09-17 08:09:54 +0000980
aliguoricde76ee2009-03-05 23:01:51 +0000981 if (!mon->rs)
982 return;
bellard7e2515e2004-08-01 21:52:19 +0000983 i = 0;
984 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000985 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000986 if (!str)
987 break;
aliguori376253e2009-03-05 23:01:23 +0000988 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000989 i++;
bellardaa455482004-04-04 13:07:25 +0000990 }
991}
992
j_mayer76a66252007-03-07 08:32:30 +0000993#if defined(TARGET_PPC)
994/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000995static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000996{
997 CPUState *env;
998
999 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +00001000 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001001}
1002#endif
1003
Lluís6d8a7642011-08-31 20:30:43 +02001004#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301005static void do_info_trace(Monitor *mon)
1006{
1007 st_print_trace((FILE *)mon, &monitor_fprintf);
1008}
Lluís31965ae2011-08-31 20:31:24 +02001009#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05301010
Lluísfc764102011-08-31 20:31:18 +02001011static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301012{
Lluísfc764102011-08-31 20:31:18 +02001013 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301014}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301015
Luiz Capitulinob223f352009-10-07 13:41:56 -03001016/**
1017 * do_quit(): Quit QEMU execution
1018 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001019static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001020{
Luiz Capitulino39b59d22010-05-11 18:08:20 -03001021 monitor_suspend(mon);
1022 no_shutdown = 0;
1023 qemu_system_shutdown_request();
1024
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001025 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001026}
1027
Jes Sorensen821601e2011-03-16 13:33:36 +01001028#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001029static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001030{
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001031 if (!password || !password[0]) {
1032 if (vnc_display_disable_login(NULL)) {
1033 qerror_report(QERR_SET_PASSWD_FAILED);
1034 return -1;
1035 }
1036 return 0;
1037 }
1038
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001039 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001040 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001041 return -1;
1042 }
aliguoribb5fc202009-03-05 23:01:15 +00001043
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001044 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001045}
1046
1047static void change_vnc_password_cb(Monitor *mon, const char *password,
1048 void *opaque)
1049{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001050 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001051 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001052}
1053
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001054static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001055{
ths70848512007-08-25 01:37:05 +00001056 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001057 strcmp(target, "password") == 0) {
1058 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001059 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001060 strncpy(password, arg, sizeof(password));
1061 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001062 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001063 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001064 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001065 }
ths70848512007-08-25 01:37:05 +00001066 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001067 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001068 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001069 return -1;
1070 }
ths70848512007-08-25 01:37:05 +00001071 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001072
1073 return 0;
thse25a5822007-08-25 01:36:20 +00001074}
Jes Sorensen821601e2011-03-16 13:33:36 +01001075#else
1076static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1077{
1078 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1079 return -ENODEV;
1080}
1081#endif
thse25a5822007-08-25 01:36:20 +00001082
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001083/**
1084 * do_change(): Change a removable medium, or VNC configuration
1085 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001086static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001087{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001088 const char *device = qdict_get_str(qdict, "device");
1089 const char *target = qdict_get_str(qdict, "target");
1090 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001091 int ret;
1092
thse25a5822007-08-25 01:36:20 +00001093 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001094 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001095 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001096 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001097 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001098
1099 return ret;
thse25a5822007-08-25 01:36:20 +00001100}
1101
Gerd Hoffmann75721502010-10-07 12:22:54 +02001102static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1103{
1104 const char *protocol = qdict_get_str(qdict, "protocol");
1105 const char *password = qdict_get_str(qdict, "password");
1106 const char *connected = qdict_get_try_str(qdict, "connected");
1107 int disconnect_if_connected = 0;
1108 int fail_if_connected = 0;
1109 int rc;
1110
1111 if (connected) {
1112 if (strcmp(connected, "fail") == 0) {
1113 fail_if_connected = 1;
1114 } else if (strcmp(connected, "disconnect") == 0) {
1115 disconnect_if_connected = 1;
1116 } else if (strcmp(connected, "keep") == 0) {
1117 /* nothing */
1118 } else {
1119 qerror_report(QERR_INVALID_PARAMETER, "connected");
1120 return -1;
1121 }
1122 }
1123
1124 if (strcmp(protocol, "spice") == 0) {
1125 if (!using_spice) {
1126 /* correct one? spice isn't a device ,,, */
1127 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1128 return -1;
1129 }
1130 rc = qemu_spice_set_passwd(password, fail_if_connected,
1131 disconnect_if_connected);
1132 if (rc != 0) {
1133 qerror_report(QERR_SET_PASSWD_FAILED);
1134 return -1;
1135 }
1136 return 0;
1137 }
1138
1139 if (strcmp(protocol, "vnc") == 0) {
1140 if (fail_if_connected || disconnect_if_connected) {
1141 /* vnc supports "connected=keep" only */
1142 qerror_report(QERR_INVALID_PARAMETER, "connected");
1143 return -1;
1144 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -06001145 /* Note that setting an empty password will not disable login through
1146 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +01001147 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001148 }
1149
1150 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1151 return -1;
1152}
1153
1154static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1155{
1156 const char *protocol = qdict_get_str(qdict, "protocol");
1157 const char *whenstr = qdict_get_str(qdict, "time");
1158 time_t when;
1159 int rc;
1160
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001161 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001162 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +01001163 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +02001164 when = TIME_MAX;
1165 } else if (whenstr[0] == '+') {
1166 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1167 } else {
1168 when = strtoull(whenstr, NULL, 10);
1169 }
1170
1171 if (strcmp(protocol, "spice") == 0) {
1172 if (!using_spice) {
1173 /* correct one? spice isn't a device ,,, */
1174 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1175 return -1;
1176 }
1177 rc = qemu_spice_set_pw_expire(when);
1178 if (rc != 0) {
1179 qerror_report(QERR_SET_PASSWD_FAILED);
1180 return -1;
1181 }
1182 return 0;
1183 }
1184
1185 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001186 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +02001187 }
1188
1189 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1190 return -1;
1191}
1192
Daniel P. Berrange13661082011-06-23 13:31:42 +01001193static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1194{
1195 const char *protocol = qdict_get_str(qdict, "protocol");
1196 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001197 CharDriverState *s;
1198
1199 if (strcmp(protocol, "spice") == 0) {
1200 if (!using_spice) {
1201 /* correct one? spice isn't a device ,,, */
1202 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1203 return -1;
1204 }
1205 qerror_report(QERR_ADD_CLIENT_FAILED);
1206 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001207#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001208 } else if (strcmp(protocol, "vnc") == 0) {
1209 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001210 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001211 vnc_display_add_client(NULL, fd, skipauth);
1212 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001213#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001214 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1215 int fd = monitor_get_fd(mon, fdname);
1216 if (qemu_chr_add_client(s, fd) < 0) {
1217 qerror_report(QERR_ADD_CLIENT_FAILED);
1218 return -1;
1219 }
1220 return 0;
1221 }
1222
1223 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1224 return -1;
1225}
1226
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001227static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1228{
1229 const char *protocol = qdict_get_str(qdict, "protocol");
1230 const char *hostname = qdict_get_str(qdict, "hostname");
1231 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1232 int port = qdict_get_try_int(qdict, "port", -1);
1233 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1234 int ret;
1235
1236 if (strcmp(protocol, "spice") == 0) {
1237 if (!using_spice) {
1238 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1239 return -1;
1240 }
1241
1242 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1243 if (ret != 0) {
1244 qerror_report(QERR_UNDEFINED_ERROR);
1245 return -1;
1246 }
1247 return 0;
1248 }
1249
1250 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1251 return -1;
1252}
1253
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001254static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001255{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001256 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001257 return 0;
bellard59a983b2004-03-17 23:17:16 +00001258}
1259
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001260static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001261{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001262 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001263}
1264
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001265static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001266{
1267 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001268 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001269
bellard9307c4c2004-04-04 12:57:25 +00001270 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001271 mask = 0;
1272 } else {
bellard9307c4c2004-04-04 12:57:25 +00001273 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001274 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001275 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001276 return;
1277 }
1278 }
1279 cpu_set_log(mask);
1280}
1281
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001282static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001283{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001284 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001285 if (!option || !strcmp(option, "on")) {
1286 singlestep = 1;
1287 } else if (!strcmp(option, "off")) {
1288 singlestep = 0;
1289 } else {
1290 monitor_printf(mon, "unexpected option %s\n", option);
1291 }
1292}
1293
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001294/**
1295 * do_stop(): Stop VM execution
1296 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001297static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001298{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001299 vm_stop(RSTATE_PAUSED);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001300 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001301}
1302
aliguoribb5fc202009-03-05 23:01:15 +00001303static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001304
aliguori376253e2009-03-05 23:01:23 +00001305struct bdrv_iterate_context {
1306 Monitor *mon;
1307 int err;
1308};
aliguoric0f4ce72009-03-05 23:01:01 +00001309
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001310/**
1311 * do_cont(): Resume emulation.
1312 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001313static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001314{
1315 struct bdrv_iterate_context context = { mon, 0 };
1316
Luiz Capitulino1bcef682011-07-29 15:15:00 -03001317 if (runstate_check(RSTATE_IN_MIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301318 qerror_report(QERR_MIGRATION_EXPECTED);
1319 return -1;
Luiz Capitulino6667b232011-07-29 15:57:54 -03001320 } else if (runstate_check(RSTATE_PANICKED) ||
1321 runstate_check(RSTATE_SHUTDOWN)) {
1322 qerror_report(QERR_RESET_REQUIRED);
1323 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301324 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001325
aliguori376253e2009-03-05 23:01:23 +00001326 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001327 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001328 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001329 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001330 return 0;
1331 } else {
1332 return -1;
1333 }
bellard8a7ddc32004-03-31 19:00:16 +00001334}
1335
aliguoribb5fc202009-03-05 23:01:15 +00001336static void bdrv_key_cb(void *opaque, int err)
1337{
aliguori376253e2009-03-05 23:01:23 +00001338 Monitor *mon = opaque;
1339
aliguoribb5fc202009-03-05 23:01:15 +00001340 /* another key was set successfully, retry to continue */
1341 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001342 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001343}
1344
1345static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1346{
aliguori376253e2009-03-05 23:01:23 +00001347 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001348
aliguori376253e2009-03-05 23:01:23 +00001349 if (!context->err && bdrv_key_required(bs)) {
1350 context->err = -EBUSY;
1351 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1352 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001353 }
1354}
1355
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001356static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001357{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001358 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001359 if (!device)
1360 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1361 if (gdbserver_start(device) < 0) {
1362 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1363 device);
1364 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001365 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001366 } else {
aliguori59030a82009-04-05 18:43:41 +00001367 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1368 device);
bellard8a7ddc32004-03-31 19:00:16 +00001369 }
1370}
1371
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001372static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001373{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001374 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001375 if (select_watchdog_action(action) == -1) {
1376 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1377 }
1378}
1379
aliguori376253e2009-03-05 23:01:23 +00001380static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001381{
aliguori376253e2009-03-05 23:01:23 +00001382 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001383 switch(c) {
1384 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001386 break;
1387 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001389 break;
1390 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 default:
1397 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001399 } else {
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001401 }
1402 break;
1403 }
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001405}
1406
aliguori376253e2009-03-05 23:01:23 +00001407static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001408 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001409{
bellard6a00d602005-11-21 23:25:50 +00001410 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001411 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001412 uint8_t buf[16];
1413 uint64_t v;
1414
1415 if (format == 'i') {
1416 int flags;
1417 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001418 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001419#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001420 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001421 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001422 } else if (wsize == 4) {
1423 flags = 0;
1424 } else {
bellard6a15fd12006-04-12 21:07:07 +00001425 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001426 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001427 if (env) {
1428#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001429 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001430 (env->segs[R_CS].flags & DESC_L_MASK))
1431 flags = 2;
1432 else
1433#endif
1434 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1435 flags = 1;
1436 }
bellard4c27ba22004-04-25 18:05:08 +00001437 }
1438#endif
aliguori376253e2009-03-05 23:01:23 +00001439 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001440 return;
1441 }
1442
1443 len = wsize * count;
1444 if (wsize == 1)
1445 line_size = 8;
1446 else
1447 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001448 max_digits = 0;
1449
1450 switch(format) {
1451 case 'o':
1452 max_digits = (wsize * 8 + 2) / 3;
1453 break;
1454 default:
1455 case 'x':
1456 max_digits = (wsize * 8) / 4;
1457 break;
1458 case 'u':
1459 case 'd':
1460 max_digits = (wsize * 8 * 10 + 32) / 33;
1461 break;
1462 case 'c':
1463 wsize = 1;
1464 break;
1465 }
1466
1467 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001468 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001470 else
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001472 l = len;
1473 if (l > line_size)
1474 l = line_size;
1475 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001476 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001477 } else {
bellard6a00d602005-11-21 23:25:50 +00001478 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001479 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001480 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001481 break;
1482 }
bellard9307c4c2004-04-04 12:57:25 +00001483 }
ths5fafdf22007-09-16 21:08:06 +00001484 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001485 while (i < l) {
1486 switch(wsize) {
1487 default:
1488 case 1:
1489 v = ldub_raw(buf + i);
1490 break;
1491 case 2:
1492 v = lduw_raw(buf + i);
1493 break;
1494 case 4:
bellard92a31b12005-02-10 22:00:52 +00001495 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001496 break;
1497 case 8:
1498 v = ldq_raw(buf + i);
1499 break;
1500 }
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001502 switch(format) {
1503 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001504 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001505 break;
1506 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001507 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001508 break;
1509 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001510 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001511 break;
1512 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001513 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001514 break;
1515 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001516 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001517 break;
1518 }
1519 i += wsize;
1520 }
aliguori376253e2009-03-05 23:01:23 +00001521 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001522 addr += l;
1523 len -= l;
1524 }
1525}
1526
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001527static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001528{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001529 int count = qdict_get_int(qdict, "count");
1530 int format = qdict_get_int(qdict, "format");
1531 int size = qdict_get_int(qdict, "size");
1532 target_long addr = qdict_get_int(qdict, "addr");
1533
aliguori376253e2009-03-05 23:01:23 +00001534 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001535}
1536
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001537static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001538{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001539 int count = qdict_get_int(qdict, "count");
1540 int format = qdict_get_int(qdict, "format");
1541 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001542 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001543
aliguori376253e2009-03-05 23:01:23 +00001544 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001545}
1546
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001547static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001548{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001549 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001550 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001551
blueswir17743e582007-09-24 18:39:04 +00001552#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001553 switch(format) {
1554 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001555 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001556 break;
1557 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001558 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001559 break;
1560 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001561 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001562 break;
1563 default:
1564 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001566 break;
1567 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001568 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001569 break;
1570 }
bellard92a31b12005-02-10 22:00:52 +00001571#else
1572 switch(format) {
1573 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001574 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001575 break;
1576 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001577 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001578 break;
1579 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001580 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001581 break;
1582 default:
1583 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001584 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001585 break;
1586 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001587 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001588 break;
1589 }
1590#endif
aliguori376253e2009-03-05 23:01:23 +00001591 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001592}
1593
Luiz Capitulino98696222010-02-10 23:49:58 -02001594static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001595{
1596 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001597 uint32_t size = qdict_get_int(qdict, "size");
1598 const char *filename = qdict_get_str(qdict, "filename");
1599 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001600 uint32_t l;
1601 CPUState *env;
1602 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001603 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001604
1605 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001606
1607 f = fopen(filename, "wb");
1608 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001609 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001610 return -1;
bellardb371dc52007-01-03 15:20:39 +00001611 }
1612 while (size != 0) {
1613 l = sizeof(buf);
1614 if (l > size)
1615 l = size;
1616 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001617 if (fwrite(buf, 1, l, f) != l) {
1618 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1619 goto exit;
1620 }
bellardb371dc52007-01-03 15:20:39 +00001621 addr += l;
1622 size -= l;
1623 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001624
1625 ret = 0;
1626
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001627exit:
bellardb371dc52007-01-03 15:20:39 +00001628 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001629 return ret;
bellardb371dc52007-01-03 15:20:39 +00001630}
1631
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001632static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001633 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001634{
1635 FILE *f;
1636 uint32_t l;
1637 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001638 uint32_t size = qdict_get_int(qdict, "size");
1639 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001640 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001641 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001642
1643 f = fopen(filename, "wb");
1644 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001645 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001646 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001647 }
1648 while (size != 0) {
1649 l = sizeof(buf);
1650 if (l > size)
1651 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001652 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001653 if (fwrite(buf, 1, l, f) != l) {
1654 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1655 goto exit;
1656 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001657 fflush(f);
1658 addr += l;
1659 size -= l;
1660 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001661
1662 ret = 0;
1663
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001664exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001665 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001666 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001667}
1668
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001669static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001670{
1671 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001672 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001673 uint32_t start = qdict_get_int(qdict, "start");
1674 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001675
1676 sum = 0;
1677 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001678 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001679 /* BSD sum algorithm ('sum' Unix command) */
1680 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001681 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001682 }
aliguori376253e2009-03-05 23:01:23 +00001683 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001684}
1685
bellarda3a91a32004-06-04 11:06:21 +00001686typedef struct {
1687 int keycode;
1688 const char *name;
1689} KeyDef;
1690
1691static const KeyDef key_defs[] = {
1692 { 0x2a, "shift" },
1693 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001694
bellarda3a91a32004-06-04 11:06:21 +00001695 { 0x38, "alt" },
1696 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001697 { 0x64, "altgr" },
1698 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001699 { 0x1d, "ctrl" },
1700 { 0x9d, "ctrl_r" },
1701
1702 { 0xdd, "menu" },
1703
1704 { 0x01, "esc" },
1705
1706 { 0x02, "1" },
1707 { 0x03, "2" },
1708 { 0x04, "3" },
1709 { 0x05, "4" },
1710 { 0x06, "5" },
1711 { 0x07, "6" },
1712 { 0x08, "7" },
1713 { 0x09, "8" },
1714 { 0x0a, "9" },
1715 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001716 { 0x0c, "minus" },
1717 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001718 { 0x0e, "backspace" },
1719
1720 { 0x0f, "tab" },
1721 { 0x10, "q" },
1722 { 0x11, "w" },
1723 { 0x12, "e" },
1724 { 0x13, "r" },
1725 { 0x14, "t" },
1726 { 0x15, "y" },
1727 { 0x16, "u" },
1728 { 0x17, "i" },
1729 { 0x18, "o" },
1730 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001731 { 0x1a, "bracket_left" },
1732 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001733 { 0x1c, "ret" },
1734
1735 { 0x1e, "a" },
1736 { 0x1f, "s" },
1737 { 0x20, "d" },
1738 { 0x21, "f" },
1739 { 0x22, "g" },
1740 { 0x23, "h" },
1741 { 0x24, "j" },
1742 { 0x25, "k" },
1743 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001744 { 0x27, "semicolon" },
1745 { 0x28, "apostrophe" },
1746 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001747
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001748 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001749 { 0x2c, "z" },
1750 { 0x2d, "x" },
1751 { 0x2e, "c" },
1752 { 0x2f, "v" },
1753 { 0x30, "b" },
1754 { 0x31, "n" },
1755 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001756 { 0x33, "comma" },
1757 { 0x34, "dot" },
1758 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001759
balrog4d3b6f62008-02-10 16:33:14 +00001760 { 0x37, "asterisk" },
1761
bellarda3a91a32004-06-04 11:06:21 +00001762 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001763 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001764 { 0x3b, "f1" },
1765 { 0x3c, "f2" },
1766 { 0x3d, "f3" },
1767 { 0x3e, "f4" },
1768 { 0x3f, "f5" },
1769 { 0x40, "f6" },
1770 { 0x41, "f7" },
1771 { 0x42, "f8" },
1772 { 0x43, "f9" },
1773 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001774 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001775 { 0x46, "scroll_lock" },
1776
bellard64866c32006-05-07 18:03:31 +00001777 { 0xb5, "kp_divide" },
1778 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001779 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001780 { 0x4e, "kp_add" },
1781 { 0x9c, "kp_enter" },
1782 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001783 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001784
1785 { 0x52, "kp_0" },
1786 { 0x4f, "kp_1" },
1787 { 0x50, "kp_2" },
1788 { 0x51, "kp_3" },
1789 { 0x4b, "kp_4" },
1790 { 0x4c, "kp_5" },
1791 { 0x4d, "kp_6" },
1792 { 0x47, "kp_7" },
1793 { 0x48, "kp_8" },
1794 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001795
bellarda3a91a32004-06-04 11:06:21 +00001796 { 0x56, "<" },
1797
1798 { 0x57, "f11" },
1799 { 0x58, "f12" },
1800
1801 { 0xb7, "print" },
1802
1803 { 0xc7, "home" },
1804 { 0xc9, "pgup" },
1805 { 0xd1, "pgdn" },
1806 { 0xcf, "end" },
1807
1808 { 0xcb, "left" },
1809 { 0xc8, "up" },
1810 { 0xd0, "down" },
1811 { 0xcd, "right" },
1812
1813 { 0xd2, "insert" },
1814 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001815#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1816 { 0xf0, "stop" },
1817 { 0xf1, "again" },
1818 { 0xf2, "props" },
1819 { 0xf3, "undo" },
1820 { 0xf4, "front" },
1821 { 0xf5, "copy" },
1822 { 0xf6, "open" },
1823 { 0xf7, "paste" },
1824 { 0xf8, "find" },
1825 { 0xf9, "cut" },
1826 { 0xfa, "lf" },
1827 { 0xfb, "help" },
1828 { 0xfc, "meta_l" },
1829 { 0xfd, "meta_r" },
1830 { 0xfe, "compose" },
1831#endif
bellarda3a91a32004-06-04 11:06:21 +00001832 { 0, NULL },
1833};
1834
1835static int get_keycode(const char *key)
1836{
1837 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001838 char *endp;
1839 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001840
1841 for(p = key_defs; p->name != NULL; p++) {
1842 if (!strcmp(key, p->name))
1843 return p->keycode;
1844 }
bellard64866c32006-05-07 18:03:31 +00001845 if (strstart(key, "0x", NULL)) {
1846 ret = strtoul(key, &endp, 0);
1847 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1848 return ret;
1849 }
bellarda3a91a32004-06-04 11:06:21 +00001850 return -1;
1851}
1852
balrogc8256f92008-06-08 22:45:01 +00001853#define MAX_KEYCODES 16
1854static uint8_t keycodes[MAX_KEYCODES];
1855static int nb_pending_keycodes;
1856static QEMUTimer *key_timer;
1857
1858static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001859{
balrogc8256f92008-06-08 22:45:01 +00001860 int keycode;
1861
1862 while (nb_pending_keycodes > 0) {
1863 nb_pending_keycodes--;
1864 keycode = keycodes[nb_pending_keycodes];
1865 if (keycode & 0x80)
1866 kbd_put_keycode(0xe0);
1867 kbd_put_keycode(keycode | 0x80);
1868 }
1869}
1870
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001871static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001872{
balrog3401c0d2008-06-04 10:05:59 +00001873 char keyname_buf[16];
1874 char *separator;
1875 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001876 const char *string = qdict_get_str(qdict, "string");
1877 int has_hold_time = qdict_haskey(qdict, "hold_time");
1878 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001879
balrogc8256f92008-06-08 22:45:01 +00001880 if (nb_pending_keycodes > 0) {
1881 qemu_del_timer(key_timer);
1882 release_keys(NULL);
1883 }
1884 if (!has_hold_time)
1885 hold_time = 100;
1886 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001887 while (1) {
1888 separator = strchr(string, '-');
1889 keyname_len = separator ? separator - string : strlen(string);
1890 if (keyname_len > 0) {
1891 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1892 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001893 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001894 return;
bellarda3a91a32004-06-04 11:06:21 +00001895 }
balrogc8256f92008-06-08 22:45:01 +00001896 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001897 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001898 return;
1899 }
1900 keyname_buf[keyname_len] = 0;
1901 keycode = get_keycode(keyname_buf);
1902 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001903 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001904 return;
1905 }
balrogc8256f92008-06-08 22:45:01 +00001906 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001907 }
balrog3401c0d2008-06-04 10:05:59 +00001908 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001909 break;
balrog3401c0d2008-06-04 10:05:59 +00001910 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001911 }
balrogc8256f92008-06-08 22:45:01 +00001912 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001913 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001914 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001915 keycode = keycodes[i];
1916 if (keycode & 0x80)
1917 kbd_put_keycode(0xe0);
1918 kbd_put_keycode(keycode & 0x7f);
1919 }
balrogc8256f92008-06-08 22:45:01 +00001920 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001921 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001922 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001923}
1924
bellard13224a82006-07-14 22:03:35 +00001925static int mouse_button_state;
1926
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001927static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001928{
1929 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001930 const char *dx_str = qdict_get_str(qdict, "dx_str");
1931 const char *dy_str = qdict_get_str(qdict, "dy_str");
1932 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001933 dx = strtol(dx_str, NULL, 0);
1934 dy = strtol(dy_str, NULL, 0);
1935 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001936 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001937 dz = strtol(dz_str, NULL, 0);
1938 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1939}
1940
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001941static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001942{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001943 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001944 mouse_button_state = button_state;
1945 kbd_mouse_event(0, 0, 0, mouse_button_state);
1946}
1947
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001948static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001949{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001950 int size = qdict_get_int(qdict, "size");
1951 int addr = qdict_get_int(qdict, "addr");
1952 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001953 uint32_t val;
1954 int suffix;
1955
1956 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001957 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001958 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001959 addr++;
1960 }
1961 addr &= 0xffff;
1962
1963 switch(size) {
1964 default:
1965 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001966 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001967 suffix = 'b';
1968 break;
1969 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001970 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001971 suffix = 'w';
1972 break;
1973 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001974 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001975 suffix = 'l';
1976 break;
1977 }
aliguori376253e2009-03-05 23:01:23 +00001978 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1979 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001980}
bellarda3a91a32004-06-04 11:06:21 +00001981
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001982static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001983{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001984 int size = qdict_get_int(qdict, "size");
1985 int addr = qdict_get_int(qdict, "addr");
1986 int val = qdict_get_int(qdict, "val");
1987
Jan Kiszkaf1147842009-07-14 10:20:11 +02001988 addr &= IOPORTS_MASK;
1989
1990 switch (size) {
1991 default:
1992 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001993 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001994 break;
1995 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001996 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001997 break;
1998 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001999 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02002000 break;
2001 }
2002}
2003
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002004static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00002005{
2006 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002007 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00002008
Jan Kiszka76e30d02009-07-02 00:19:02 +02002009 res = qemu_boot_set(bootdevice);
2010 if (res == 0) {
2011 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
2012 } else if (res > 0) {
2013 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00002014 } else {
aliguori376253e2009-03-05 23:01:23 +00002015 monitor_printf(mon, "no function defined to set boot device list for "
2016 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00002017 }
2018}
2019
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03002020/**
2021 * do_system_reset(): Issue a machine reset
2022 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002023static int do_system_reset(Monitor *mon, const QDict *qdict,
2024 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00002025{
2026 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002027 return 0;
bellarde4f90822004-06-20 12:35:44 +00002028}
2029
Luiz Capitulino43076662009-10-07 13:41:59 -03002030/**
2031 * do_system_powerdown(): Issue a machine powerdown
2032 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002033static int do_system_powerdown(Monitor *mon, const QDict *qdict,
2034 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00002035{
2036 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02002037 return 0;
bellard34751872005-07-02 14:31:34 +00002038}
2039
bellardb86bda52004-09-18 19:32:46 +00002040#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00002041static void print_pte(Monitor *mon, target_phys_addr_t addr,
2042 target_phys_addr_t pte,
2043 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00002044{
Blue Swirld65aaf32010-12-11 18:56:24 +00002045#ifdef TARGET_X86_64
2046 if (addr & (1ULL << 47)) {
2047 addr |= -1LL << 48;
2048 }
2049#endif
2050 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
2051 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002052 addr,
2053 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00002054 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00002055 pte & PG_GLOBAL_MASK ? 'G' : '-',
2056 pte & PG_PSE_MASK ? 'P' : '-',
2057 pte & PG_DIRTY_MASK ? 'D' : '-',
2058 pte & PG_ACCESSED_MASK ? 'A' : '-',
2059 pte & PG_PCD_MASK ? 'C' : '-',
2060 pte & PG_PWT_MASK ? 'T' : '-',
2061 pte & PG_USER_MASK ? 'U' : '-',
2062 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00002063}
2064
Blue Swirld65aaf32010-12-11 18:56:24 +00002065static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002066{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002067 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00002068 uint32_t pgd, pde, pte;
2069
bellardb86bda52004-09-18 19:32:46 +00002070 pgd = env->cr[3] & ~0xfff;
2071 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002072 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002073 pde = le32_to_cpu(pde);
2074 if (pde & PG_PRESENT_MASK) {
2075 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00002076 /* 4M pages */
2077 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00002078 } else {
2079 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002080 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002081 pte = le32_to_cpu(pte);
2082 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00002083 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00002084 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00002085 ~0xfff);
2086 }
2087 }
2088 }
2089 }
2090 }
2091}
2092
Blue Swirld65aaf32010-12-11 18:56:24 +00002093static void tlb_info_pae32(Monitor *mon, CPUState *env)
2094{
Austin Clements94ac5cd2011-08-21 14:49:45 -04002095 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00002096 uint64_t pdpe, pde, pte;
2097 uint64_t pdp_addr, pd_addr, pt_addr;
2098
2099 pdp_addr = env->cr[3] & ~0x1f;
2100 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002101 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002102 pdpe = le64_to_cpu(pdpe);
2103 if (pdpe & PG_PRESENT_MASK) {
2104 pd_addr = pdpe & 0x3fffffffff000ULL;
2105 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002106 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002107 pde = le64_to_cpu(pde);
2108 if (pde & PG_PRESENT_MASK) {
2109 if (pde & PG_PSE_MASK) {
2110 /* 2M pages with PAE, CR4.PSE is ignored */
2111 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2112 ~((target_phys_addr_t)(1 << 20) - 1));
2113 } else {
2114 pt_addr = pde & 0x3fffffffff000ULL;
2115 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002116 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002117 pte = le64_to_cpu(pte);
2118 if (pte & PG_PRESENT_MASK) {
2119 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2120 + (l3 << 12),
2121 pte & ~PG_PSE_MASK,
2122 ~(target_phys_addr_t)0xfff);
2123 }
2124 }
2125 }
2126 }
2127 }
2128 }
2129 }
2130}
2131
2132#ifdef TARGET_X86_64
2133static void tlb_info_64(Monitor *mon, CPUState *env)
2134{
2135 uint64_t l1, l2, l3, l4;
2136 uint64_t pml4e, pdpe, pde, pte;
2137 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2138
2139 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2140 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002141 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002142 pml4e = le64_to_cpu(pml4e);
2143 if (pml4e & PG_PRESENT_MASK) {
2144 pdp_addr = pml4e & 0x3fffffffff000ULL;
2145 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002146 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002147 pdpe = le64_to_cpu(pdpe);
2148 if (pdpe & PG_PRESENT_MASK) {
2149 if (pdpe & PG_PSE_MASK) {
2150 /* 1G pages, CR4.PSE is ignored */
2151 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2152 0x3ffffc0000000ULL);
2153 } else {
2154 pd_addr = pdpe & 0x3fffffffff000ULL;
2155 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002156 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002157 pde = le64_to_cpu(pde);
2158 if (pde & PG_PRESENT_MASK) {
2159 if (pde & PG_PSE_MASK) {
2160 /* 2M pages, CR4.PSE is ignored */
2161 print_pte(mon, (l1 << 39) + (l2 << 30) +
2162 (l3 << 21), pde,
2163 0x3ffffffe00000ULL);
2164 } else {
2165 pt_addr = pde & 0x3fffffffff000ULL;
2166 for (l4 = 0; l4 < 512; l4++) {
2167 cpu_physical_memory_read(pt_addr
2168 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002169 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00002170 pte = le64_to_cpu(pte);
2171 if (pte & PG_PRESENT_MASK) {
2172 print_pte(mon, (l1 << 39) +
2173 (l2 << 30) +
2174 (l3 << 21) + (l4 << 12),
2175 pte & ~PG_PSE_MASK,
2176 0x3fffffffff000ULL);
2177 }
2178 }
2179 }
2180 }
2181 }
2182 }
2183 }
2184 }
2185 }
2186 }
2187}
2188#endif
2189
2190static void tlb_info(Monitor *mon)
2191{
2192 CPUState *env;
2193
2194 env = mon_get_cpu();
2195
2196 if (!(env->cr[0] & CR0_PG_MASK)) {
2197 monitor_printf(mon, "PG disabled\n");
2198 return;
2199 }
2200 if (env->cr[4] & CR4_PAE_MASK) {
2201#ifdef TARGET_X86_64
2202 if (env->hflags & HF_LMA_MASK) {
2203 tlb_info_64(mon, env);
2204 } else
2205#endif
2206 {
2207 tlb_info_pae32(mon, env);
2208 }
2209 } else {
2210 tlb_info_32(mon, env);
2211 }
2212}
2213
Blue Swirl1b3cba62010-12-11 18:56:27 +00002214static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2215 int *plast_prot,
2216 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002217{
bellard9746b152004-11-11 18:30:24 +00002218 int prot1;
2219 prot1 = *plast_prot;
2220 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002221 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002222 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2223 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002224 *pstart, end, end - *pstart,
2225 prot1 & PG_USER_MASK ? 'u' : '-',
2226 'r',
2227 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002228 }
2229 if (prot != 0)
2230 *pstart = end;
2231 else
2232 *pstart = -1;
2233 *plast_prot = prot;
2234 }
2235}
2236
Blue Swirl1b3cba62010-12-11 18:56:27 +00002237static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002238{
Austin Clementsb49ca722011-08-14 23:19:21 -04002239 unsigned int l1, l2;
2240 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002241 uint32_t pgd, pde, pte;
2242 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002243
bellardb86bda52004-09-18 19:32:46 +00002244 pgd = env->cr[3] & ~0xfff;
2245 last_prot = 0;
2246 start = -1;
2247 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002248 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002249 pde = le32_to_cpu(pde);
2250 end = l1 << 22;
2251 if (pde & PG_PRESENT_MASK) {
2252 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2253 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002254 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002255 } else {
2256 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002257 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002258 pte = le32_to_cpu(pte);
2259 end = (l1 << 22) + (l2 << 12);
2260 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002261 prot = pte & pde &
2262 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002263 } else {
2264 prot = 0;
2265 }
aliguori376253e2009-03-05 23:01:23 +00002266 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002267 }
2268 }
2269 } else {
2270 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002271 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002272 }
2273 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002274 /* Flush last range */
2275 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002276}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002277
2278static void mem_info_pae32(Monitor *mon, CPUState *env)
2279{
Austin Clementsb49ca722011-08-14 23:19:21 -04002280 unsigned int l1, l2, l3;
2281 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002282 uint64_t pdpe, pde, pte;
2283 uint64_t pdp_addr, pd_addr, pt_addr;
2284 target_phys_addr_t start, end;
2285
2286 pdp_addr = env->cr[3] & ~0x1f;
2287 last_prot = 0;
2288 start = -1;
2289 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002290 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002291 pdpe = le64_to_cpu(pdpe);
2292 end = l1 << 30;
2293 if (pdpe & PG_PRESENT_MASK) {
2294 pd_addr = pdpe & 0x3fffffffff000ULL;
2295 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002296 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002297 pde = le64_to_cpu(pde);
2298 end = (l1 << 30) + (l2 << 21);
2299 if (pde & PG_PRESENT_MASK) {
2300 if (pde & PG_PSE_MASK) {
2301 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2302 PG_PRESENT_MASK);
2303 mem_print(mon, &start, &last_prot, end, prot);
2304 } else {
2305 pt_addr = pde & 0x3fffffffff000ULL;
2306 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002307 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002308 pte = le64_to_cpu(pte);
2309 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2310 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002311 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2312 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002313 } else {
2314 prot = 0;
2315 }
2316 mem_print(mon, &start, &last_prot, end, prot);
2317 }
2318 }
2319 } else {
2320 prot = 0;
2321 mem_print(mon, &start, &last_prot, end, prot);
2322 }
2323 }
2324 } else {
2325 prot = 0;
2326 mem_print(mon, &start, &last_prot, end, prot);
2327 }
2328 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002329 /* Flush last range */
2330 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002331}
2332
2333
2334#ifdef TARGET_X86_64
2335static void mem_info_64(Monitor *mon, CPUState *env)
2336{
2337 int prot, last_prot;
2338 uint64_t l1, l2, l3, l4;
2339 uint64_t pml4e, pdpe, pde, pte;
2340 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2341
2342 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2343 last_prot = 0;
2344 start = -1;
2345 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002346 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002347 pml4e = le64_to_cpu(pml4e);
2348 end = l1 << 39;
2349 if (pml4e & PG_PRESENT_MASK) {
2350 pdp_addr = pml4e & 0x3fffffffff000ULL;
2351 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002352 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002353 pdpe = le64_to_cpu(pdpe);
2354 end = (l1 << 39) + (l2 << 30);
2355 if (pdpe & PG_PRESENT_MASK) {
2356 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002357 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2358 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002359 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002360 mem_print(mon, &start, &last_prot, end, prot);
2361 } else {
2362 pd_addr = pdpe & 0x3fffffffff000ULL;
2363 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002364 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002365 pde = le64_to_cpu(pde);
2366 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2367 if (pde & PG_PRESENT_MASK) {
2368 if (pde & PG_PSE_MASK) {
2369 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2370 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002371 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002372 mem_print(mon, &start, &last_prot, end, prot);
2373 } else {
2374 pt_addr = pde & 0x3fffffffff000ULL;
2375 for (l4 = 0; l4 < 512; l4++) {
2376 cpu_physical_memory_read(pt_addr
2377 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002378 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002379 pte = le64_to_cpu(pte);
2380 end = (l1 << 39) + (l2 << 30) +
2381 (l3 << 21) + (l4 << 12);
2382 if (pte & PG_PRESENT_MASK) {
2383 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2384 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002385 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002386 } else {
2387 prot = 0;
2388 }
2389 mem_print(mon, &start, &last_prot, end, prot);
2390 }
2391 }
2392 } else {
2393 prot = 0;
2394 mem_print(mon, &start, &last_prot, end, prot);
2395 }
2396 }
2397 }
2398 } else {
2399 prot = 0;
2400 mem_print(mon, &start, &last_prot, end, prot);
2401 }
2402 }
2403 } else {
2404 prot = 0;
2405 mem_print(mon, &start, &last_prot, end, prot);
2406 }
2407 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002408 /* Flush last range */
2409 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002410}
2411#endif
2412
2413static void mem_info(Monitor *mon)
2414{
2415 CPUState *env;
2416
2417 env = mon_get_cpu();
2418
2419 if (!(env->cr[0] & CR0_PG_MASK)) {
2420 monitor_printf(mon, "PG disabled\n");
2421 return;
2422 }
2423 if (env->cr[4] & CR4_PAE_MASK) {
2424#ifdef TARGET_X86_64
2425 if (env->hflags & HF_LMA_MASK) {
2426 mem_info_64(mon, env);
2427 } else
2428#endif
2429 {
2430 mem_info_pae32(mon, env);
2431 }
2432 } else {
2433 mem_info_32(mon, env);
2434 }
2435}
bellardb86bda52004-09-18 19:32:46 +00002436#endif
2437
aurel327c664e22009-03-03 06:12:22 +00002438#if defined(TARGET_SH4)
2439
aliguori376253e2009-03-05 23:01:23 +00002440static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002441{
aliguori376253e2009-03-05 23:01:23 +00002442 monitor_printf(mon, " tlb%i:\t"
2443 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2444 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2445 "dirty=%hhu writethrough=%hhu\n",
2446 idx,
2447 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2448 tlb->v, tlb->sh, tlb->c, tlb->pr,
2449 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002450}
2451
aliguori376253e2009-03-05 23:01:23 +00002452static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002453{
2454 CPUState *env = mon_get_cpu();
2455 int i;
2456
aliguori376253e2009-03-05 23:01:23 +00002457 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002458 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002459 print_tlb (mon, i, &env->itlb[i]);
2460 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002461 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002462 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002463}
2464
2465#endif
2466
Blue Swirld41160a2010-12-19 13:42:56 +00002467#if defined(TARGET_SPARC)
2468static void tlb_info(Monitor *mon)
2469{
2470 CPUState *env1 = mon_get_cpu();
2471
2472 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2473}
2474#endif
2475
Blue Swirl314e2982011-09-11 20:22:05 +00002476static void do_info_mtree(Monitor *mon)
2477{
2478 mtree_info((fprintf_function)monitor_printf, mon);
2479}
2480
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002481static void do_info_kvm_print(Monitor *mon, const QObject *data)
2482{
2483 QDict *qdict;
2484
2485 qdict = qobject_to_qdict(data);
2486
2487 monitor_printf(mon, "kvm support: ");
2488 if (qdict_get_bool(qdict, "present")) {
2489 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2490 "enabled" : "disabled");
2491 } else {
2492 monitor_printf(mon, "not compiled\n");
2493 }
2494}
2495
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002496static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002497{
2498#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002499 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2500 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002501#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002502 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002503#endif
2504}
2505
aliguori030ea372009-04-21 22:30:47 +00002506static void do_info_numa(Monitor *mon)
2507{
aliguorib28b6232009-04-22 20:20:29 +00002508 int i;
aliguori030ea372009-04-21 22:30:47 +00002509 CPUState *env;
2510
2511 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2512 for (i = 0; i < nb_numa_nodes; i++) {
2513 monitor_printf(mon, "node %d cpus:", i);
2514 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2515 if (env->numa_node == i) {
2516 monitor_printf(mon, " %d", env->cpu_index);
2517 }
2518 }
2519 monitor_printf(mon, "\n");
2520 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2521 node_mem[i] >> 20);
2522 }
2523}
2524
bellard5f1ce942006-02-08 22:40:15 +00002525#ifdef CONFIG_PROFILER
2526
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002527int64_t qemu_time;
2528int64_t dev_time;
2529
aliguori376253e2009-03-05 23:01:23 +00002530static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002531{
2532 int64_t total;
2533 total = qemu_time;
2534 if (total == 0)
2535 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002536 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002537 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002538 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002539 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002540 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002541 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002542}
2543#else
aliguori376253e2009-03-05 23:01:23 +00002544static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002545{
aliguori376253e2009-03-05 23:01:23 +00002546 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002547}
2548#endif
2549
bellardec36b692006-07-16 18:57:03 +00002550/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002551static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002552
aliguori376253e2009-03-05 23:01:23 +00002553static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002554{
2555 int i;
2556 CaptureState *s;
2557
2558 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002559 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002560 s->ops.info (s->opaque);
2561 }
2562}
2563
Blue Swirl23130862009-06-06 08:22:04 +00002564#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002565static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002566{
2567 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002568 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002569 CaptureState *s;
2570
2571 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2572 if (i == n) {
2573 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002574 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002575 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002576 return;
2577 }
2578 }
2579}
2580
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002581static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002582{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002583 const char *path = qdict_get_str(qdict, "path");
2584 int has_freq = qdict_haskey(qdict, "freq");
2585 int freq = qdict_get_try_int(qdict, "freq", -1);
2586 int has_bits = qdict_haskey(qdict, "bits");
2587 int bits = qdict_get_try_int(qdict, "bits", -1);
2588 int has_channels = qdict_haskey(qdict, "nchannels");
2589 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002590 CaptureState *s;
2591
Anthony Liguori7267c092011-08-20 22:09:37 -05002592 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002593
2594 freq = has_freq ? freq : 44100;
2595 bits = has_bits ? bits : 16;
2596 nchannels = has_channels ? nchannels : 2;
2597
2598 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002599 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002600 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002601 return;
bellardec36b692006-07-16 18:57:03 +00002602 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002603 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002604}
2605#endif
2606
aurel32dc1c0b72008-04-27 23:52:12 +00002607#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002608static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002609{
2610 CPUState *env;
2611
2612 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2613 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2614 }
2615
2616 return 0;
2617}
2618#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002619static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002620{
2621 qerror_report(QERR_UNSUPPORTED);
2622 return -1;
2623}
aurel32dc1c0b72008-04-27 23:52:12 +00002624#endif
2625
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002626static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002627{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002628 QDict *qdict;
Luiz Capitulino6e0ca882011-08-09 14:44:25 -03002629 const char *status;
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002630
2631 qdict = qobject_to_qdict(data);
2632
2633 monitor_printf(mon, "VM status: ");
2634 if (qdict_get_bool(qdict, "running")) {
2635 monitor_printf(mon, "running");
2636 if (qdict_get_bool(qdict, "singlestep")) {
2637 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002638 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002639 } else {
2640 monitor_printf(mon, "paused");
2641 }
2642
Luiz Capitulino6e0ca882011-08-09 14:44:25 -03002643 status = qdict_get_str(qdict, "status");
2644 if (strcmp(status, "paused") && strcmp(status, "running")) {
2645 monitor_printf(mon, " (%s)", status);
2646 }
2647
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002648 monitor_printf(mon, "\n");
2649}
2650
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002651static void do_info_status(Monitor *mon, QObject **ret_data)
2652{
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -03002653 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep, runstate_as_string());
aurel326f9c5ee2008-12-18 22:43:56 +00002654}
2655
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002656static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002657{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002658 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002659
aliguori76655d62009-03-06 20:27:37 +00002660 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002661 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002662 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002663 return acl;
2664}
aliguori76655d62009-03-06 20:27:37 +00002665
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002666static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002667{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002668 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002669 qemu_acl *acl = find_acl(mon, aclname);
2670 qemu_acl_entry *entry;
2671 int i = 0;
2672
2673 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002674 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002675 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002676 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002677 i++;
2678 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002679 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002680 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002681 }
2682}
2683
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002684static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002685{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002686 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002687 qemu_acl *acl = find_acl(mon, aclname);
2688
2689 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002690 qemu_acl_reset(acl);
2691 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002692 }
2693}
aliguori76655d62009-03-06 20:27:37 +00002694
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002695static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002696{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002697 const char *aclname = qdict_get_str(qdict, "aclname");
2698 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002699 qemu_acl *acl = find_acl(mon, aclname);
2700
2701 if (acl) {
2702 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002703 acl->defaultDeny = 0;
2704 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002705 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002706 acl->defaultDeny = 1;
2707 monitor_printf(mon, "acl: policy set to 'deny'\n");
2708 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002709 monitor_printf(mon, "acl: unknown policy '%s', "
2710 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002711 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002712 }
2713}
aliguori76655d62009-03-06 20:27:37 +00002714
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002715static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002716{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002717 const char *aclname = qdict_get_str(qdict, "aclname");
2718 const char *match = qdict_get_str(qdict, "match");
2719 const char *policy = qdict_get_str(qdict, "policy");
2720 int has_index = qdict_haskey(qdict, "index");
2721 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002722 qemu_acl *acl = find_acl(mon, aclname);
2723 int deny, ret;
2724
2725 if (acl) {
2726 if (strcmp(policy, "allow") == 0) {
2727 deny = 0;
2728 } else if (strcmp(policy, "deny") == 0) {
2729 deny = 1;
2730 } else {
2731 monitor_printf(mon, "acl: unknown policy '%s', "
2732 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002733 return;
2734 }
aliguori28a76be2009-03-06 20:27:40 +00002735 if (has_index)
2736 ret = qemu_acl_insert(acl, deny, match, index);
2737 else
2738 ret = qemu_acl_append(acl, deny, match);
2739 if (ret < 0)
2740 monitor_printf(mon, "acl: unable to add acl entry\n");
2741 else
2742 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002743 }
2744}
aliguori76655d62009-03-06 20:27:37 +00002745
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002746static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002747{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002748 const char *aclname = qdict_get_str(qdict, "aclname");
2749 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002750 qemu_acl *acl = find_acl(mon, aclname);
2751 int ret;
aliguori76655d62009-03-06 20:27:37 +00002752
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002753 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002754 ret = qemu_acl_remove(acl, match);
2755 if (ret < 0)
2756 monitor_printf(mon, "acl: no matching acl entry\n");
2757 else
2758 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002759 }
2760}
2761
Huang Ying79c4f6b2009-06-23 10:05:14 +08002762#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002763static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002764{
2765 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002766 int cpu_index = qdict_get_int(qdict, "cpu_index");
2767 int bank = qdict_get_int(qdict, "bank");
2768 uint64_t status = qdict_get_int(qdict, "status");
2769 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2770 uint64_t addr = qdict_get_int(qdict, "addr");
2771 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002772 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002773
Jan Kiszka747461c2011-03-02 08:56:10 +01002774 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2775 flags |= MCE_INJECT_BROADCAST;
2776 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002777 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002778 if (cenv->cpu_index == cpu_index) {
2779 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002780 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002781 break;
2782 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002783 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002784}
2785#endif
2786
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002787static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002788{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002789 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002790 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002791 int fd;
2792
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002793 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002794 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002795 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002796 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002797 }
2798
2799 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002800 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2801 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002802 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002803 }
2804
Blue Swirl72cf2d42009-09-12 07:36:22 +00002805 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002806 if (strcmp(monfd->name, fdname) != 0) {
2807 continue;
2808 }
2809
2810 close(monfd->fd);
2811 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002812 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002813 }
2814
Anthony Liguori7267c092011-08-20 22:09:37 -05002815 monfd = g_malloc0(sizeof(mon_fd_t));
2816 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002817 monfd->fd = fd;
2818
Blue Swirl72cf2d42009-09-12 07:36:22 +00002819 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002820 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002821}
2822
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002823static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002824{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002825 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002826 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002827
Blue Swirl72cf2d42009-09-12 07:36:22 +00002828 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002829 if (strcmp(monfd->name, fdname) != 0) {
2830 continue;
2831 }
2832
Blue Swirl72cf2d42009-09-12 07:36:22 +00002833 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002834 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002835 g_free(monfd->name);
2836 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002837 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002838 }
2839
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002840 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002841 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002842}
2843
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002844static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002845{
Luiz Capitulino13548692011-07-29 15:36:43 -03002846 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002847 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002848
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002849 vm_stop(RSTATE_RESTORE);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002850
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002851 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002852 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002853 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002854}
2855
Mark McLoughlin7768e042009-07-22 09:11:41 +01002856int monitor_get_fd(Monitor *mon, const char *fdname)
2857{
Anthony Liguoric227f092009-10-01 16:12:16 -05002858 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002859
Blue Swirl72cf2d42009-09-12 07:36:22 +00002860 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002861 int fd;
2862
2863 if (strcmp(monfd->name, fdname) != 0) {
2864 continue;
2865 }
2866
2867 fd = monfd->fd;
2868
2869 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002870 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002871 g_free(monfd->name);
2872 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002873
2874 return fd;
2875 }
2876
2877 return -1;
2878}
2879
Anthony Liguoric227f092009-10-01 16:12:16 -05002880static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002881#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002882 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002883};
2884
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002885/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002886static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002887 {
2888 .name = "version",
2889 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002890 .params = "",
2891 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002892 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002893 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002894 },
2895 {
2896 .name = "network",
2897 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002898 .params = "",
2899 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002900 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002901 },
2902 {
2903 .name = "chardev",
2904 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 .params = "",
2906 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002907 .user_print = qemu_chr_info_print,
2908 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002909 },
2910 {
2911 .name = "block",
2912 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002913 .params = "",
2914 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002915 .user_print = bdrv_info_print,
2916 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002917 },
2918 {
2919 .name = "blockstats",
2920 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002921 .params = "",
2922 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002923 .user_print = bdrv_stats_print,
2924 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002925 },
2926 {
2927 .name = "registers",
2928 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 .params = "",
2930 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002931 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002932 },
2933 {
2934 .name = "cpus",
2935 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002936 .params = "",
2937 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002938 .user_print = monitor_print_cpus,
2939 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002940 },
2941 {
2942 .name = "history",
2943 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002944 .params = "",
2945 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002946 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002947 },
2948 {
2949 .name = "irq",
2950 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002951 .params = "",
2952 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002953 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002954 },
2955 {
2956 .name = "pic",
2957 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002958 .params = "",
2959 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002960 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002961 },
2962 {
2963 .name = "pci",
2964 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002965 .params = "",
2966 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002967 .user_print = do_pci_info_print,
2968 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002969 },
Blue Swirld41160a2010-12-19 13:42:56 +00002970#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002971 {
2972 .name = "tlb",
2973 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002974 .params = "",
2975 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002976 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002977 },
aurel327c664e22009-03-03 06:12:22 +00002978#endif
2979#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002980 {
2981 .name = "mem",
2982 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002983 .params = "",
2984 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002985 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002986 },
bellardb86bda52004-09-18 19:32:46 +00002987#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002988 {
Blue Swirl314e2982011-09-11 20:22:05 +00002989 .name = "mtree",
2990 .args_type = "",
2991 .params = "",
2992 .help = "show memory tree",
2993 .mhandler.info = do_info_mtree,
2994 },
2995 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002996 .name = "jit",
2997 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002998 .params = "",
2999 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03003000 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003001 },
3002 {
3003 .name = "kvm",
3004 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003005 .params = "",
3006 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02003007 .user_print = do_info_kvm_print,
3008 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003009 },
3010 {
3011 .name = "numa",
3012 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003013 .params = "",
3014 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003015 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003016 },
3017 {
3018 .name = "usb",
3019 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003020 .params = "",
3021 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003022 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003023 },
3024 {
3025 .name = "usbhost",
3026 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003027 .params = "",
3028 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03003029 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003030 },
3031 {
3032 .name = "profile",
3033 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003034 .params = "",
3035 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003036 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003037 },
3038 {
3039 .name = "capture",
3040 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003041 .params = "",
3042 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03003043 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003044 },
3045 {
3046 .name = "snapshots",
3047 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003048 .params = "",
3049 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03003050 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003051 },
3052 {
3053 .name = "status",
3054 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003055 .params = "",
3056 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02003057 .user_print = do_info_status_print,
3058 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003059 },
3060 {
3061 .name = "pcmcia",
3062 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003063 .params = "",
3064 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03003065 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003066 },
3067 {
3068 .name = "mice",
3069 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003070 .params = "",
3071 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02003072 .user_print = do_info_mice_print,
3073 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003074 },
3075 {
3076 .name = "vnc",
3077 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003078 .params = "",
3079 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02003080 .user_print = do_info_vnc_print,
3081 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003082 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003083#if defined(CONFIG_SPICE)
3084 {
3085 .name = "spice",
3086 .args_type = "",
3087 .params = "",
3088 .help = "show the spice server status",
3089 .user_print = do_info_spice_print,
3090 .mhandler.info_new = do_info_spice,
3091 },
3092#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003093 {
3094 .name = "name",
3095 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003096 .params = "",
3097 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02003098 .user_print = do_info_name_print,
3099 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003100 },
3101 {
3102 .name = "uuid",
3103 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003104 .params = "",
3105 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02003106 .user_print = do_info_uuid_print,
3107 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003108 },
j_mayer76a66252007-03-07 08:32:30 +00003109#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003110 {
3111 .name = "cpustats",
3112 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003113 .params = "",
3114 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03003115 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003116 },
j_mayer76a66252007-03-07 08:32:30 +00003117#endif
blueswir131a60e22007-10-26 18:42:59 +00003118#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003119 {
3120 .name = "usernet",
3121 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003122 .params = "",
3123 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03003124 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003125 },
blueswir131a60e22007-10-26 18:42:59 +00003126#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003127 {
3128 .name = "migrate",
3129 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003130 .params = "",
3131 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02003132 .user_print = do_info_migrate_print,
3133 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003134 },
3135 {
3136 .name = "balloon",
3137 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003138 .params = "",
3139 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03003140 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06003141 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02003142 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003143 },
3144 {
3145 .name = "qtree",
3146 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003147 .params = "",
3148 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03003149 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003150 },
3151 {
3152 .name = "qdm",
3153 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003154 .params = "",
3155 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03003156 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003157 },
3158 {
3159 .name = "roms",
3160 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003161 .params = "",
3162 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03003163 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003164 },
Lluís6d8a7642011-08-31 20:30:43 +02003165#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05303166 {
3167 .name = "trace",
3168 .args_type = "",
3169 .params = "",
3170 .help = "show current contents of trace buffer",
3171 .mhandler.info = do_info_trace,
3172 },
Lluís31965ae2011-08-31 20:31:24 +02003173#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05303174 {
3175 .name = "trace-events",
3176 .args_type = "",
3177 .params = "",
3178 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02003179 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05303180 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03003181 {
3182 .name = NULL,
3183 },
bellard9dc39cb2004-03-14 21:38:27 +00003184};
3185
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003186static const mon_cmd_t qmp_cmds[] = {
3187#include "qmp-commands.h"
3188 { /* NULL */ },
3189};
3190
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003191static const mon_cmd_t qmp_query_cmds[] = {
3192 {
3193 .name = "version",
3194 .args_type = "",
3195 .params = "",
3196 .help = "show the version of QEMU",
3197 .user_print = do_info_version_print,
3198 .mhandler.info_new = do_info_version,
3199 },
3200 {
3201 .name = "commands",
3202 .args_type = "",
3203 .params = "",
3204 .help = "list QMP available commands",
3205 .user_print = monitor_user_noop,
3206 .mhandler.info_new = do_info_commands,
3207 },
3208 {
3209 .name = "chardev",
3210 .args_type = "",
3211 .params = "",
3212 .help = "show the character devices",
3213 .user_print = qemu_chr_info_print,
3214 .mhandler.info_new = qemu_chr_info,
3215 },
3216 {
3217 .name = "block",
3218 .args_type = "",
3219 .params = "",
3220 .help = "show the block devices",
3221 .user_print = bdrv_info_print,
3222 .mhandler.info_new = bdrv_info,
3223 },
3224 {
3225 .name = "blockstats",
3226 .args_type = "",
3227 .params = "",
3228 .help = "show block device statistics",
3229 .user_print = bdrv_stats_print,
3230 .mhandler.info_new = bdrv_info_stats,
3231 },
3232 {
3233 .name = "cpus",
3234 .args_type = "",
3235 .params = "",
3236 .help = "show infos for each CPU",
3237 .user_print = monitor_print_cpus,
3238 .mhandler.info_new = do_info_cpus,
3239 },
3240 {
3241 .name = "pci",
3242 .args_type = "",
3243 .params = "",
3244 .help = "show PCI info",
3245 .user_print = do_pci_info_print,
3246 .mhandler.info_new = do_pci_info,
3247 },
3248 {
3249 .name = "kvm",
3250 .args_type = "",
3251 .params = "",
3252 .help = "show KVM information",
3253 .user_print = do_info_kvm_print,
3254 .mhandler.info_new = do_info_kvm,
3255 },
3256 {
3257 .name = "status",
3258 .args_type = "",
3259 .params = "",
3260 .help = "show the current VM status (running|paused)",
3261 .user_print = do_info_status_print,
3262 .mhandler.info_new = do_info_status,
3263 },
3264 {
3265 .name = "mice",
3266 .args_type = "",
3267 .params = "",
3268 .help = "show which guest mouse is receiving events",
3269 .user_print = do_info_mice_print,
3270 .mhandler.info_new = do_info_mice,
3271 },
3272 {
3273 .name = "vnc",
3274 .args_type = "",
3275 .params = "",
3276 .help = "show the vnc server status",
3277 .user_print = do_info_vnc_print,
3278 .mhandler.info_new = do_info_vnc,
3279 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01003280#if defined(CONFIG_SPICE)
3281 {
3282 .name = "spice",
3283 .args_type = "",
3284 .params = "",
3285 .help = "show the spice server status",
3286 .user_print = do_info_spice_print,
3287 .mhandler.info_new = do_info_spice,
3288 },
3289#endif
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003290 {
3291 .name = "name",
3292 .args_type = "",
3293 .params = "",
3294 .help = "show the current VM name",
3295 .user_print = do_info_name_print,
3296 .mhandler.info_new = do_info_name,
3297 },
3298 {
3299 .name = "uuid",
3300 .args_type = "",
3301 .params = "",
3302 .help = "show the current VM UUID",
3303 .user_print = do_info_uuid_print,
3304 .mhandler.info_new = do_info_uuid,
3305 },
3306 {
3307 .name = "migrate",
3308 .args_type = "",
3309 .params = "",
3310 .help = "show migration status",
3311 .user_print = do_info_migrate_print,
3312 .mhandler.info_new = do_info_migrate,
3313 },
3314 {
3315 .name = "balloon",
3316 .args_type = "",
3317 .params = "",
3318 .help = "show balloon information",
3319 .user_print = monitor_print_balloon,
3320 .mhandler.info_async = do_info_balloon,
3321 .flags = MONITOR_CMD_ASYNC,
3322 },
3323 { /* NULL */ },
3324};
3325
bellard9307c4c2004-04-04 12:57:25 +00003326/*******************************************************************/
3327
3328static const char *pch;
3329static jmp_buf expr_env;
3330
bellard92a31b12005-02-10 22:00:52 +00003331#define MD_TLONG 0
3332#define MD_I32 1
3333
bellard9307c4c2004-04-04 12:57:25 +00003334typedef struct MonitorDef {
3335 const char *name;
3336 int offset;
blueswir18662d652008-10-02 18:32:44 +00003337 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00003338 int type;
bellard9307c4c2004-04-04 12:57:25 +00003339} MonitorDef;
3340
bellard57206fd2004-04-25 18:54:52 +00003341#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003342static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003343{
bellard6a00d602005-11-21 23:25:50 +00003344 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003345 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003346}
3347#endif
3348
bellarda541f292004-04-12 20:39:29 +00003349#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003350static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003351{
bellard6a00d602005-11-21 23:25:50 +00003352 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003353 unsigned int u;
3354 int i;
3355
3356 u = 0;
3357 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003358 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003359
3360 return u;
3361}
3362
blueswir18662d652008-10-02 18:32:44 +00003363static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003364{
bellard6a00d602005-11-21 23:25:50 +00003365 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003366 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003367}
3368
blueswir18662d652008-10-02 18:32:44 +00003369static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003370{
bellard6a00d602005-11-21 23:25:50 +00003371 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003372 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003373}
bellard9fddaa02004-05-21 12:59:32 +00003374
blueswir18662d652008-10-02 18:32:44 +00003375static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003376{
bellard6a00d602005-11-21 23:25:50 +00003377 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003378 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003379}
3380
blueswir18662d652008-10-02 18:32:44 +00003381static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003382{
bellard6a00d602005-11-21 23:25:50 +00003383 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003384 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003385}
3386
blueswir18662d652008-10-02 18:32:44 +00003387static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003388{
bellard6a00d602005-11-21 23:25:50 +00003389 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003390 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003391}
bellarda541f292004-04-12 20:39:29 +00003392#endif
3393
bellarde95c8d52004-09-30 22:22:08 +00003394#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003395#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003396static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003397{
bellard6a00d602005-11-21 23:25:50 +00003398 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003399
3400 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003401}
bellard7b936c02005-10-30 17:05:13 +00003402#endif
bellarde95c8d52004-09-30 22:22:08 +00003403
blueswir18662d652008-10-02 18:32:44 +00003404static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003405{
bellard6a00d602005-11-21 23:25:50 +00003406 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003407 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003408}
3409#endif
3410
blueswir18662d652008-10-02 18:32:44 +00003411static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003412#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003413
3414#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003415 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003416 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003417 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003418
bellard9307c4c2004-04-04 12:57:25 +00003419 { "eax", offsetof(CPUState, regs[0]) },
3420 { "ecx", offsetof(CPUState, regs[1]) },
3421 { "edx", offsetof(CPUState, regs[2]) },
3422 { "ebx", offsetof(CPUState, regs[3]) },
3423 { "esp|sp", offsetof(CPUState, regs[4]) },
3424 { "ebp|fp", offsetof(CPUState, regs[5]) },
3425 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003426 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003427#ifdef TARGET_X86_64
3428 { "r8", offsetof(CPUState, regs[8]) },
3429 { "r9", offsetof(CPUState, regs[9]) },
3430 { "r10", offsetof(CPUState, regs[10]) },
3431 { "r11", offsetof(CPUState, regs[11]) },
3432 { "r12", offsetof(CPUState, regs[12]) },
3433 { "r13", offsetof(CPUState, regs[13]) },
3434 { "r14", offsetof(CPUState, regs[14]) },
3435 { "r15", offsetof(CPUState, regs[15]) },
3436#endif
bellard9307c4c2004-04-04 12:57:25 +00003437 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003438 { "eip", offsetof(CPUState, eip) },
3439 SEG("cs", R_CS)
3440 SEG("ds", R_DS)
3441 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003442 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003443 SEG("fs", R_FS)
3444 SEG("gs", R_GS)
3445 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003446#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003447 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003448 { "r0", offsetof(CPUState, gpr[0]) },
3449 { "r1", offsetof(CPUState, gpr[1]) },
3450 { "r2", offsetof(CPUState, gpr[2]) },
3451 { "r3", offsetof(CPUState, gpr[3]) },
3452 { "r4", offsetof(CPUState, gpr[4]) },
3453 { "r5", offsetof(CPUState, gpr[5]) },
3454 { "r6", offsetof(CPUState, gpr[6]) },
3455 { "r7", offsetof(CPUState, gpr[7]) },
3456 { "r8", offsetof(CPUState, gpr[8]) },
3457 { "r9", offsetof(CPUState, gpr[9]) },
3458 { "r10", offsetof(CPUState, gpr[10]) },
3459 { "r11", offsetof(CPUState, gpr[11]) },
3460 { "r12", offsetof(CPUState, gpr[12]) },
3461 { "r13", offsetof(CPUState, gpr[13]) },
3462 { "r14", offsetof(CPUState, gpr[14]) },
3463 { "r15", offsetof(CPUState, gpr[15]) },
3464 { "r16", offsetof(CPUState, gpr[16]) },
3465 { "r17", offsetof(CPUState, gpr[17]) },
3466 { "r18", offsetof(CPUState, gpr[18]) },
3467 { "r19", offsetof(CPUState, gpr[19]) },
3468 { "r20", offsetof(CPUState, gpr[20]) },
3469 { "r21", offsetof(CPUState, gpr[21]) },
3470 { "r22", offsetof(CPUState, gpr[22]) },
3471 { "r23", offsetof(CPUState, gpr[23]) },
3472 { "r24", offsetof(CPUState, gpr[24]) },
3473 { "r25", offsetof(CPUState, gpr[25]) },
3474 { "r26", offsetof(CPUState, gpr[26]) },
3475 { "r27", offsetof(CPUState, gpr[27]) },
3476 { "r28", offsetof(CPUState, gpr[28]) },
3477 { "r29", offsetof(CPUState, gpr[29]) },
3478 { "r30", offsetof(CPUState, gpr[30]) },
3479 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003480 /* Floating point registers */
3481 { "f0", offsetof(CPUState, fpr[0]) },
3482 { "f1", offsetof(CPUState, fpr[1]) },
3483 { "f2", offsetof(CPUState, fpr[2]) },
3484 { "f3", offsetof(CPUState, fpr[3]) },
3485 { "f4", offsetof(CPUState, fpr[4]) },
3486 { "f5", offsetof(CPUState, fpr[5]) },
3487 { "f6", offsetof(CPUState, fpr[6]) },
3488 { "f7", offsetof(CPUState, fpr[7]) },
3489 { "f8", offsetof(CPUState, fpr[8]) },
3490 { "f9", offsetof(CPUState, fpr[9]) },
3491 { "f10", offsetof(CPUState, fpr[10]) },
3492 { "f11", offsetof(CPUState, fpr[11]) },
3493 { "f12", offsetof(CPUState, fpr[12]) },
3494 { "f13", offsetof(CPUState, fpr[13]) },
3495 { "f14", offsetof(CPUState, fpr[14]) },
3496 { "f15", offsetof(CPUState, fpr[15]) },
3497 { "f16", offsetof(CPUState, fpr[16]) },
3498 { "f17", offsetof(CPUState, fpr[17]) },
3499 { "f18", offsetof(CPUState, fpr[18]) },
3500 { "f19", offsetof(CPUState, fpr[19]) },
3501 { "f20", offsetof(CPUState, fpr[20]) },
3502 { "f21", offsetof(CPUState, fpr[21]) },
3503 { "f22", offsetof(CPUState, fpr[22]) },
3504 { "f23", offsetof(CPUState, fpr[23]) },
3505 { "f24", offsetof(CPUState, fpr[24]) },
3506 { "f25", offsetof(CPUState, fpr[25]) },
3507 { "f26", offsetof(CPUState, fpr[26]) },
3508 { "f27", offsetof(CPUState, fpr[27]) },
3509 { "f28", offsetof(CPUState, fpr[28]) },
3510 { "f29", offsetof(CPUState, fpr[29]) },
3511 { "f30", offsetof(CPUState, fpr[30]) },
3512 { "f31", offsetof(CPUState, fpr[31]) },
3513 { "fpscr", offsetof(CPUState, fpscr) },
3514 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003515 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003516 { "lr", offsetof(CPUState, lr) },
3517 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003518 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003519 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003520 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003521 { "msr", 0, &monitor_get_msr, },
3522 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003523 { "tbu", 0, &monitor_get_tbu, },
3524 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003525#if defined(TARGET_PPC64)
3526 /* Address space register */
3527 { "asr", offsetof(CPUState, asr) },
3528#endif
3529 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003530 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003531 { "sr0", offsetof(CPUState, sr[0]) },
3532 { "sr1", offsetof(CPUState, sr[1]) },
3533 { "sr2", offsetof(CPUState, sr[2]) },
3534 { "sr3", offsetof(CPUState, sr[3]) },
3535 { "sr4", offsetof(CPUState, sr[4]) },
3536 { "sr5", offsetof(CPUState, sr[5]) },
3537 { "sr6", offsetof(CPUState, sr[6]) },
3538 { "sr7", offsetof(CPUState, sr[7]) },
3539 { "sr8", offsetof(CPUState, sr[8]) },
3540 { "sr9", offsetof(CPUState, sr[9]) },
3541 { "sr10", offsetof(CPUState, sr[10]) },
3542 { "sr11", offsetof(CPUState, sr[11]) },
3543 { "sr12", offsetof(CPUState, sr[12]) },
3544 { "sr13", offsetof(CPUState, sr[13]) },
3545 { "sr14", offsetof(CPUState, sr[14]) },
3546 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003547 /* Too lazy to put BATs... */
3548 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3549
3550 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3551 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3552 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3553 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3554 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3555 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3556 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3557 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3558 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3559 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3560 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3561 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3562 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3563 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3564 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3565 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3566 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3567 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3568 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3569 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3570 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3571 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3572 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3573 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3574 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3575 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3576 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3577 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3578 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3579 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3580 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3581 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3582 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3583 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3584 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3585 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3586 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3587 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3588 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3589 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3590 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3591 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3592 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3593 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3594 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3595 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3596 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3597 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3598 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3599 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3600 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3601 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3602 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3603 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3604 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3605 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3606 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3607 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3608 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3609 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3610 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3611 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3612 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3613 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3614 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3615 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3616
bellarde95c8d52004-09-30 22:22:08 +00003617#elif defined(TARGET_SPARC)
3618 { "g0", offsetof(CPUState, gregs[0]) },
3619 { "g1", offsetof(CPUState, gregs[1]) },
3620 { "g2", offsetof(CPUState, gregs[2]) },
3621 { "g3", offsetof(CPUState, gregs[3]) },
3622 { "g4", offsetof(CPUState, gregs[4]) },
3623 { "g5", offsetof(CPUState, gregs[5]) },
3624 { "g6", offsetof(CPUState, gregs[6]) },
3625 { "g7", offsetof(CPUState, gregs[7]) },
3626 { "o0", 0, monitor_get_reg },
3627 { "o1", 1, monitor_get_reg },
3628 { "o2", 2, monitor_get_reg },
3629 { "o3", 3, monitor_get_reg },
3630 { "o4", 4, monitor_get_reg },
3631 { "o5", 5, monitor_get_reg },
3632 { "o6", 6, monitor_get_reg },
3633 { "o7", 7, monitor_get_reg },
3634 { "l0", 8, monitor_get_reg },
3635 { "l1", 9, monitor_get_reg },
3636 { "l2", 10, monitor_get_reg },
3637 { "l3", 11, monitor_get_reg },
3638 { "l4", 12, monitor_get_reg },
3639 { "l5", 13, monitor_get_reg },
3640 { "l6", 14, monitor_get_reg },
3641 { "l7", 15, monitor_get_reg },
3642 { "i0", 16, monitor_get_reg },
3643 { "i1", 17, monitor_get_reg },
3644 { "i2", 18, monitor_get_reg },
3645 { "i3", 19, monitor_get_reg },
3646 { "i4", 20, monitor_get_reg },
3647 { "i5", 21, monitor_get_reg },
3648 { "i6", 22, monitor_get_reg },
3649 { "i7", 23, monitor_get_reg },
3650 { "pc", offsetof(CPUState, pc) },
3651 { "npc", offsetof(CPUState, npc) },
3652 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003653#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003654 { "psr", 0, &monitor_get_psr, },
3655 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003656#endif
bellarde95c8d52004-09-30 22:22:08 +00003657 { "tbr", offsetof(CPUState, tbr) },
3658 { "fsr", offsetof(CPUState, fsr) },
3659 { "f0", offsetof(CPUState, fpr[0]) },
3660 { "f1", offsetof(CPUState, fpr[1]) },
3661 { "f2", offsetof(CPUState, fpr[2]) },
3662 { "f3", offsetof(CPUState, fpr[3]) },
3663 { "f4", offsetof(CPUState, fpr[4]) },
3664 { "f5", offsetof(CPUState, fpr[5]) },
3665 { "f6", offsetof(CPUState, fpr[6]) },
3666 { "f7", offsetof(CPUState, fpr[7]) },
3667 { "f8", offsetof(CPUState, fpr[8]) },
3668 { "f9", offsetof(CPUState, fpr[9]) },
3669 { "f10", offsetof(CPUState, fpr[10]) },
3670 { "f11", offsetof(CPUState, fpr[11]) },
3671 { "f12", offsetof(CPUState, fpr[12]) },
3672 { "f13", offsetof(CPUState, fpr[13]) },
3673 { "f14", offsetof(CPUState, fpr[14]) },
3674 { "f15", offsetof(CPUState, fpr[15]) },
3675 { "f16", offsetof(CPUState, fpr[16]) },
3676 { "f17", offsetof(CPUState, fpr[17]) },
3677 { "f18", offsetof(CPUState, fpr[18]) },
3678 { "f19", offsetof(CPUState, fpr[19]) },
3679 { "f20", offsetof(CPUState, fpr[20]) },
3680 { "f21", offsetof(CPUState, fpr[21]) },
3681 { "f22", offsetof(CPUState, fpr[22]) },
3682 { "f23", offsetof(CPUState, fpr[23]) },
3683 { "f24", offsetof(CPUState, fpr[24]) },
3684 { "f25", offsetof(CPUState, fpr[25]) },
3685 { "f26", offsetof(CPUState, fpr[26]) },
3686 { "f27", offsetof(CPUState, fpr[27]) },
3687 { "f28", offsetof(CPUState, fpr[28]) },
3688 { "f29", offsetof(CPUState, fpr[29]) },
3689 { "f30", offsetof(CPUState, fpr[30]) },
3690 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003691#ifdef TARGET_SPARC64
3692 { "f32", offsetof(CPUState, fpr[32]) },
3693 { "f34", offsetof(CPUState, fpr[34]) },
3694 { "f36", offsetof(CPUState, fpr[36]) },
3695 { "f38", offsetof(CPUState, fpr[38]) },
3696 { "f40", offsetof(CPUState, fpr[40]) },
3697 { "f42", offsetof(CPUState, fpr[42]) },
3698 { "f44", offsetof(CPUState, fpr[44]) },
3699 { "f46", offsetof(CPUState, fpr[46]) },
3700 { "f48", offsetof(CPUState, fpr[48]) },
3701 { "f50", offsetof(CPUState, fpr[50]) },
3702 { "f52", offsetof(CPUState, fpr[52]) },
3703 { "f54", offsetof(CPUState, fpr[54]) },
3704 { "f56", offsetof(CPUState, fpr[56]) },
3705 { "f58", offsetof(CPUState, fpr[58]) },
3706 { "f60", offsetof(CPUState, fpr[60]) },
3707 { "f62", offsetof(CPUState, fpr[62]) },
3708 { "asi", offsetof(CPUState, asi) },
3709 { "pstate", offsetof(CPUState, pstate) },
3710 { "cansave", offsetof(CPUState, cansave) },
3711 { "canrestore", offsetof(CPUState, canrestore) },
3712 { "otherwin", offsetof(CPUState, otherwin) },
3713 { "wstate", offsetof(CPUState, wstate) },
3714 { "cleanwin", offsetof(CPUState, cleanwin) },
3715 { "fprs", offsetof(CPUState, fprs) },
3716#endif
bellard9307c4c2004-04-04 12:57:25 +00003717#endif
3718 { NULL },
3719};
3720
aliguori376253e2009-03-05 23:01:23 +00003721static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003722{
aliguori376253e2009-03-05 23:01:23 +00003723 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003724 longjmp(expr_env, 1);
3725}
3726
Markus Armbruster09b94182010-01-20 13:07:30 +01003727/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003728static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003729{
blueswir18662d652008-10-02 18:32:44 +00003730 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003731 void *ptr;
3732
bellard9307c4c2004-04-04 12:57:25 +00003733 for(md = monitor_defs; md->name != NULL; md++) {
3734 if (compare_cmd(name, md->name)) {
3735 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003736 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003737 } else {
bellard6a00d602005-11-21 23:25:50 +00003738 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003739 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003740 switch(md->type) {
3741 case MD_I32:
3742 *pval = *(int32_t *)ptr;
3743 break;
3744 case MD_TLONG:
3745 *pval = *(target_long *)ptr;
3746 break;
3747 default:
3748 *pval = 0;
3749 break;
3750 }
bellard9307c4c2004-04-04 12:57:25 +00003751 }
3752 return 0;
3753 }
3754 }
3755 return -1;
3756}
3757
3758static void next(void)
3759{
Blue Swirl660f11b2009-07-31 21:16:51 +00003760 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003761 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003762 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003763 pch++;
3764 }
3765}
3766
aliguori376253e2009-03-05 23:01:23 +00003767static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003768
aliguori376253e2009-03-05 23:01:23 +00003769static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003770{
blueswir1c2efc952007-09-25 17:28:42 +00003771 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003772 char *p;
bellard6a00d602005-11-21 23:25:50 +00003773 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003774
3775 switch(*pch) {
3776 case '+':
3777 next();
aliguori376253e2009-03-05 23:01:23 +00003778 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003779 break;
3780 case '-':
3781 next();
aliguori376253e2009-03-05 23:01:23 +00003782 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003783 break;
3784 case '~':
3785 next();
aliguori376253e2009-03-05 23:01:23 +00003786 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003787 break;
3788 case '(':
3789 next();
aliguori376253e2009-03-05 23:01:23 +00003790 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003791 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003792 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003793 }
3794 next();
3795 break;
bellard81d09122004-07-14 17:21:37 +00003796 case '\'':
3797 pch++;
3798 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003799 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003800 n = *pch;
3801 pch++;
3802 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003803 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003804 next();
3805 break;
bellard9307c4c2004-04-04 12:57:25 +00003806 case '$':
3807 {
3808 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003809 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003810
bellard9307c4c2004-04-04 12:57:25 +00003811 pch++;
3812 q = buf;
3813 while ((*pch >= 'a' && *pch <= 'z') ||
3814 (*pch >= 'A' && *pch <= 'Z') ||
3815 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003816 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003817 if ((q - buf) < sizeof(buf) - 1)
3818 *q++ = *pch;
3819 pch++;
3820 }
blueswir1cd390082008-11-16 13:53:32 +00003821 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003822 pch++;
3823 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003824 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003825 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003826 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003827 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003828 }
3829 break;
3830 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003831 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003832 n = 0;
3833 break;
3834 default:
blueswir17743e582007-09-24 18:39:04 +00003835#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003836 n = strtoull(pch, &p, 0);
3837#else
bellard9307c4c2004-04-04 12:57:25 +00003838 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003839#endif
bellard9307c4c2004-04-04 12:57:25 +00003840 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003841 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003842 }
3843 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003844 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003845 pch++;
3846 break;
3847 }
3848 return n;
3849}
3850
3851
aliguori376253e2009-03-05 23:01:23 +00003852static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003853{
blueswir1c2efc952007-09-25 17:28:42 +00003854 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003855 int op;
ths3b46e622007-09-17 08:09:54 +00003856
aliguori376253e2009-03-05 23:01:23 +00003857 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003858 for(;;) {
3859 op = *pch;
3860 if (op != '*' && op != '/' && op != '%')
3861 break;
3862 next();
aliguori376253e2009-03-05 23:01:23 +00003863 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003864 switch(op) {
3865 default:
3866 case '*':
3867 val *= val2;
3868 break;
3869 case '/':
3870 case '%':
ths5fafdf22007-09-16 21:08:06 +00003871 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003872 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003873 if (op == '/')
3874 val /= val2;
3875 else
3876 val %= val2;
3877 break;
3878 }
3879 }
3880 return val;
3881}
3882
aliguori376253e2009-03-05 23:01:23 +00003883static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003884{
blueswir1c2efc952007-09-25 17:28:42 +00003885 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003886 int op;
bellard9307c4c2004-04-04 12:57:25 +00003887
aliguori376253e2009-03-05 23:01:23 +00003888 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003889 for(;;) {
3890 op = *pch;
3891 if (op != '&' && op != '|' && op != '^')
3892 break;
3893 next();
aliguori376253e2009-03-05 23:01:23 +00003894 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003895 switch(op) {
3896 default:
3897 case '&':
3898 val &= val2;
3899 break;
3900 case '|':
3901 val |= val2;
3902 break;
3903 case '^':
3904 val ^= val2;
3905 break;
3906 }
3907 }
3908 return val;
3909}
3910
aliguori376253e2009-03-05 23:01:23 +00003911static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003912{
blueswir1c2efc952007-09-25 17:28:42 +00003913 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003914 int op;
bellard9307c4c2004-04-04 12:57:25 +00003915
aliguori376253e2009-03-05 23:01:23 +00003916 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003917 for(;;) {
3918 op = *pch;
3919 if (op != '+' && op != '-')
3920 break;
3921 next();
aliguori376253e2009-03-05 23:01:23 +00003922 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003923 if (op == '+')
3924 val += val2;
3925 else
3926 val -= val2;
3927 }
3928 return val;
3929}
3930
aliguori376253e2009-03-05 23:01:23 +00003931static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003932{
3933 pch = *pp;
3934 if (setjmp(expr_env)) {
3935 *pp = pch;
3936 return -1;
3937 }
blueswir1cd390082008-11-16 13:53:32 +00003938 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003939 pch++;
aliguori376253e2009-03-05 23:01:23 +00003940 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003941 *pp = pch;
3942 return 0;
3943}
3944
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003945static int get_double(Monitor *mon, double *pval, const char **pp)
3946{
3947 const char *p = *pp;
3948 char *tailp;
3949 double d;
3950
3951 d = strtod(p, &tailp);
3952 if (tailp == p) {
3953 monitor_printf(mon, "Number expected\n");
3954 return -1;
3955 }
3956 if (d != d || d - d != 0) {
3957 /* NaN or infinity */
3958 monitor_printf(mon, "Bad number\n");
3959 return -1;
3960 }
3961 *pval = d;
3962 *pp = tailp;
3963 return 0;
3964}
3965
bellard9307c4c2004-04-04 12:57:25 +00003966static int get_str(char *buf, int buf_size, const char **pp)
3967{
3968 const char *p;
3969 char *q;
3970 int c;
3971
bellard81d09122004-07-14 17:21:37 +00003972 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003973 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003974 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003975 p++;
3976 if (*p == '\0') {
3977 fail:
bellard81d09122004-07-14 17:21:37 +00003978 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003979 *pp = p;
3980 return -1;
3981 }
bellard9307c4c2004-04-04 12:57:25 +00003982 if (*p == '\"') {
3983 p++;
3984 while (*p != '\0' && *p != '\"') {
3985 if (*p == '\\') {
3986 p++;
3987 c = *p++;
3988 switch(c) {
3989 case 'n':
3990 c = '\n';
3991 break;
3992 case 'r':
3993 c = '\r';
3994 break;
3995 case '\\':
3996 case '\'':
3997 case '\"':
3998 break;
3999 default:
4000 qemu_printf("unsupported escape code: '\\%c'\n", c);
4001 goto fail;
4002 }
4003 if ((q - buf) < buf_size - 1) {
4004 *q++ = c;
4005 }
4006 } else {
4007 if ((q - buf) < buf_size - 1) {
4008 *q++ = *p;
4009 }
4010 p++;
4011 }
4012 }
4013 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00004014 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00004015 goto fail;
4016 }
4017 p++;
4018 } else {
blueswir1cd390082008-11-16 13:53:32 +00004019 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004020 if ((q - buf) < buf_size - 1) {
4021 *q++ = *p;
4022 }
4023 p++;
4024 }
bellard9307c4c2004-04-04 12:57:25 +00004025 }
bellard81d09122004-07-14 17:21:37 +00004026 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00004027 *pp = p;
4028 return 0;
4029}
4030
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004031/*
4032 * Store the command-name in cmdname, and return a pointer to
4033 * the remaining of the command string.
4034 */
4035static const char *get_command_name(const char *cmdline,
4036 char *cmdname, size_t nlen)
4037{
4038 size_t len;
4039 const char *p, *pstart;
4040
4041 p = cmdline;
4042 while (qemu_isspace(*p))
4043 p++;
4044 if (*p == '\0')
4045 return NULL;
4046 pstart = p;
4047 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
4048 p++;
4049 len = p - pstart;
4050 if (len > nlen - 1)
4051 len = nlen - 1;
4052 memcpy(cmdname, pstart, len);
4053 cmdname[len] = '\0';
4054 return p;
4055}
4056
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004057/**
4058 * Read key of 'type' into 'key' and return the current
4059 * 'type' pointer.
4060 */
4061static char *key_get_info(const char *type, char **key)
4062{
4063 size_t len;
4064 char *p, *str;
4065
4066 if (*type == ',')
4067 type++;
4068
4069 p = strchr(type, ':');
4070 if (!p) {
4071 *key = NULL;
4072 return NULL;
4073 }
4074 len = p - type;
4075
Anthony Liguori7267c092011-08-20 22:09:37 -05004076 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004077 memcpy(str, type, len);
4078 str[len] = '\0';
4079
4080 *key = str;
4081 return ++p;
4082}
4083
bellard9307c4c2004-04-04 12:57:25 +00004084static int default_fmt_format = 'x';
4085static int default_fmt_size = 4;
4086
4087#define MAX_ARGS 16
4088
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004089static int is_valid_option(const char *c, const char *typestr)
4090{
4091 char option[3];
4092
4093 option[0] = '-';
4094 option[1] = *c;
4095 option[2] = '\0';
4096
4097 typestr = strstr(typestr, option);
4098 return (typestr != NULL);
4099}
4100
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004101static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
4102 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004103{
4104 const mon_cmd_t *cmd;
4105
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004106 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004107 if (compare_cmd(cmdname, cmd->name)) {
4108 return cmd;
4109 }
4110 }
4111
4112 return NULL;
4113}
4114
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03004115static const mon_cmd_t *monitor_find_command(const char *cmdname)
4116{
4117 return search_dispatch_table(mon_cmds, cmdname);
4118}
4119
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004120static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
4121{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03004122 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004123}
4124
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004125static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
4126{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03004127 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004128}
4129
Anthony Liguoric227f092009-10-01 16:12:16 -05004130static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004131 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004132 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00004133{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004134 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03004135 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05004136 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00004137 char cmdname[256];
4138 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004139 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00004140
4141#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00004142 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00004143#endif
ths3b46e622007-09-17 08:09:54 +00004144
bellard9307c4c2004-04-04 12:57:25 +00004145 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03004146 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
4147 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004148 return NULL;
ths3b46e622007-09-17 08:09:54 +00004149
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02004150 cmd = monitor_find_command(cmdname);
4151 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004152 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004153 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03004154 }
bellard9307c4c2004-04-04 12:57:25 +00004155
bellard9307c4c2004-04-04 12:57:25 +00004156 /* parse the parameters */
4157 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00004158 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004159 typestr = key_get_info(typestr, &key);
4160 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00004161 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004162 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00004163 typestr++;
4164 switch(c) {
4165 case 'F':
bellard81d09122004-07-14 17:21:37 +00004166 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00004167 case 's':
4168 {
4169 int ret;
ths3b46e622007-09-17 08:09:54 +00004170
blueswir1cd390082008-11-16 13:53:32 +00004171 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004172 p++;
4173 if (*typestr == '?') {
4174 typestr++;
4175 if (*p == '\0') {
4176 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03004177 break;
bellard9307c4c2004-04-04 12:57:25 +00004178 }
4179 }
4180 ret = get_str(buf, sizeof(buf), &p);
4181 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00004182 switch(c) {
4183 case 'F':
aliguori376253e2009-03-05 23:01:23 +00004184 monitor_printf(mon, "%s: filename expected\n",
4185 cmdname);
bellard81d09122004-07-14 17:21:37 +00004186 break;
4187 case 'B':
aliguori376253e2009-03-05 23:01:23 +00004188 monitor_printf(mon, "%s: block device name expected\n",
4189 cmdname);
bellard81d09122004-07-14 17:21:37 +00004190 break;
4191 default:
aliguori376253e2009-03-05 23:01:23 +00004192 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00004193 break;
4194 }
bellard9307c4c2004-04-04 12:57:25 +00004195 goto fail;
4196 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004197 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00004198 }
4199 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004200 case 'O':
4201 {
4202 QemuOptsList *opts_list;
4203 QemuOpts *opts;
4204
4205 opts_list = qemu_find_opts(key);
4206 if (!opts_list || opts_list->desc->name) {
4207 goto bad_type;
4208 }
4209 while (qemu_isspace(*p)) {
4210 p++;
4211 }
4212 if (!*p)
4213 break;
4214 if (get_str(buf, sizeof(buf), &p) < 0) {
4215 goto fail;
4216 }
4217 opts = qemu_opts_parse(opts_list, buf, 1);
4218 if (!opts) {
4219 goto fail;
4220 }
4221 qemu_opts_to_qdict(opts, qdict);
4222 qemu_opts_del(opts);
4223 }
4224 break;
bellard9307c4c2004-04-04 12:57:25 +00004225 case '/':
4226 {
4227 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00004228
blueswir1cd390082008-11-16 13:53:32 +00004229 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004230 p++;
4231 if (*p == '/') {
4232 /* format found */
4233 p++;
4234 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00004235 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004236 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00004237 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00004238 count = count * 10 + (*p - '0');
4239 p++;
4240 }
4241 }
4242 size = -1;
4243 format = -1;
4244 for(;;) {
4245 switch(*p) {
4246 case 'o':
4247 case 'd':
4248 case 'u':
4249 case 'x':
4250 case 'i':
4251 case 'c':
4252 format = *p++;
4253 break;
4254 case 'b':
4255 size = 1;
4256 p++;
4257 break;
4258 case 'h':
4259 size = 2;
4260 p++;
4261 break;
4262 case 'w':
4263 size = 4;
4264 p++;
4265 break;
4266 case 'g':
4267 case 'L':
4268 size = 8;
4269 p++;
4270 break;
4271 default:
4272 goto next;
4273 }
4274 }
4275 next:
blueswir1cd390082008-11-16 13:53:32 +00004276 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00004277 monitor_printf(mon, "invalid char in format: '%c'\n",
4278 *p);
bellard9307c4c2004-04-04 12:57:25 +00004279 goto fail;
4280 }
bellard9307c4c2004-04-04 12:57:25 +00004281 if (format < 0)
4282 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004283 if (format != 'i') {
4284 /* for 'i', not specifying a size gives -1 as size */
4285 if (size < 0)
4286 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00004287 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00004288 }
bellard9307c4c2004-04-04 12:57:25 +00004289 default_fmt_format = format;
4290 } else {
4291 count = 1;
4292 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00004293 if (format != 'i') {
4294 size = default_fmt_size;
4295 } else {
4296 size = -1;
4297 }
bellard9307c4c2004-04-04 12:57:25 +00004298 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004299 qdict_put(qdict, "count", qint_from_int(count));
4300 qdict_put(qdict, "format", qint_from_int(format));
4301 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00004302 }
4303 break;
4304 case 'i':
bellard92a31b12005-02-10 22:00:52 +00004305 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004306 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00004307 {
blueswir1c2efc952007-09-25 17:28:42 +00004308 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00004309
blueswir1cd390082008-11-16 13:53:32 +00004310 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004311 p++;
bellard34405572004-06-08 00:55:58 +00004312 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00004313 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03004314 if (*p == '\0') {
4315 typestr++;
4316 break;
4317 }
bellard34405572004-06-08 00:55:58 +00004318 } else {
4319 if (*p == '.') {
4320 p++;
blueswir1cd390082008-11-16 13:53:32 +00004321 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00004322 p++;
bellard34405572004-06-08 00:55:58 +00004323 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03004324 typestr++;
4325 break;
bellard34405572004-06-08 00:55:58 +00004326 }
4327 }
bellard13224a82006-07-14 22:03:35 +00004328 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00004329 }
aliguori376253e2009-03-05 23:01:23 +00004330 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00004331 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004332 /* Check if 'i' is greater than 32-bit */
4333 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4334 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4335 monitor_printf(mon, "integer is for 32-bit values\n");
4336 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004337 } else if (c == 'M') {
4338 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03004339 }
Luiz Capitulino53773582009-08-28 15:27:25 -03004340 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00004341 }
4342 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004343 case 'o':
4344 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01004345 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02004346 char *end;
4347
4348 while (qemu_isspace(*p)) {
4349 p++;
4350 }
4351 if (*typestr == '?') {
4352 typestr++;
4353 if (*p == '\0') {
4354 break;
4355 }
4356 }
4357 val = strtosz(p, &end);
4358 if (val < 0) {
4359 monitor_printf(mon, "invalid size\n");
4360 goto fail;
4361 }
4362 qdict_put(qdict, key, qint_from_int(val));
4363 p = end;
4364 }
4365 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004366 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004367 {
4368 double val;
4369
4370 while (qemu_isspace(*p))
4371 p++;
4372 if (*typestr == '?') {
4373 typestr++;
4374 if (*p == '\0') {
4375 break;
4376 }
4377 }
4378 if (get_double(mon, &val, &p) < 0) {
4379 goto fail;
4380 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004381 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004382 switch (*p) {
4383 case 'm':
4384 val /= 1e3; p += 2; break;
4385 case 'u':
4386 val /= 1e6; p += 2; break;
4387 case 'n':
4388 val /= 1e9; p += 2; break;
4389 }
4390 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004391 if (*p && !qemu_isspace(*p)) {
4392 monitor_printf(mon, "Unknown unit suffix\n");
4393 goto fail;
4394 }
4395 qdict_put(qdict, key, qfloat_from_double(val));
4396 }
4397 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004398 case 'b':
4399 {
4400 const char *beg;
4401 int val;
4402
4403 while (qemu_isspace(*p)) {
4404 p++;
4405 }
4406 beg = p;
4407 while (qemu_isgraph(*p)) {
4408 p++;
4409 }
4410 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4411 val = 1;
4412 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4413 val = 0;
4414 } else {
4415 monitor_printf(mon, "Expected 'on' or 'off'\n");
4416 goto fail;
4417 }
4418 qdict_put(qdict, key, qbool_from_int(val));
4419 }
4420 break;
bellard9307c4c2004-04-04 12:57:25 +00004421 case '-':
4422 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004423 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004424 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004425 /* option */
ths3b46e622007-09-17 08:09:54 +00004426
bellard9307c4c2004-04-04 12:57:25 +00004427 c = *typestr++;
4428 if (c == '\0')
4429 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004430 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004431 p++;
bellard9307c4c2004-04-04 12:57:25 +00004432 if (*p == '-') {
4433 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004434 if(c != *p) {
4435 if(!is_valid_option(p, typestr)) {
4436
4437 monitor_printf(mon, "%s: unsupported option -%c\n",
4438 cmdname, *p);
4439 goto fail;
4440 } else {
4441 skip_key = 1;
4442 }
bellard9307c4c2004-04-04 12:57:25 +00004443 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004444 if(skip_key) {
4445 p = tmp;
4446 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004447 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004448 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004449 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004450 }
bellard9307c4c2004-04-04 12:57:25 +00004451 }
bellard9307c4c2004-04-04 12:57:25 +00004452 }
4453 break;
4454 default:
4455 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004456 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004457 goto fail;
4458 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004459 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004460 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004461 }
4462 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004463 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004464 p++;
4465 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004466 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4467 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004468 goto fail;
4469 }
4470
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004471 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004472
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004473fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004474 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004475 return NULL;
4476}
4477
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004478void monitor_set_error(Monitor *mon, QError *qerror)
4479{
4480 /* report only the first error */
4481 if (!mon->error) {
4482 mon->error = qerror;
4483 } else {
4484 MON_DEBUG("Additional error report at %s:%d\n",
4485 qerror->file, qerror->linenr);
4486 QDECREF(qerror);
4487 }
4488}
4489
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004490static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4491{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004492 if (ret && !monitor_has_error(mon)) {
4493 /*
4494 * If it returns failure, it must have passed on error.
4495 *
4496 * Action: Report an internal error to the client if in QMP.
4497 */
4498 qerror_report(QERR_UNDEFINED_ERROR);
4499 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4500 cmd->name);
4501 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004502
4503#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004504 if (!ret && monitor_has_error(mon)) {
4505 /*
4506 * If it returns success, it must not have passed an error.
4507 *
4508 * Action: Report the passed error to the client.
4509 */
4510 MON_DEBUG("command '%s' returned success but passed an error\n",
4511 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004512 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004513
4514 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4515 /*
4516 * Handlers should not call Monitor print functions.
4517 *
4518 * Action: Ignore them in QMP.
4519 *
4520 * (XXX: we don't check any 'info' or 'query' command here
4521 * because the user print function _is_ called by do_info(), hence
4522 * we will trigger this check. This problem will go away when we
4523 * make 'query' commands real and kill do_info())
4524 */
4525 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4526 cmd->name, mon_print_count_get(mon));
4527 }
4528#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004529}
4530
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004531static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004532{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004533 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004534 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004535
4536 qdict = qdict_new();
4537
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004538 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004539 if (!cmd)
4540 goto out;
4541
Luiz Capitulino4903de02010-09-16 11:01:32 -03004542 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004543 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004544 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004545 QObject *data = NULL;
4546
4547 /* XXX: ignores the error code */
4548 cmd->mhandler.cmd_new(mon, qdict, &data);
4549 assert(!monitor_has_error(mon));
4550 if (data) {
4551 cmd->user_print(mon, data);
4552 qobject_decref(data);
4553 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004554 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004555 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004556 }
4557
Luiz Capitulino13917be2009-10-07 13:41:54 -03004558out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004559 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004560}
4561
bellard81d09122004-07-14 17:21:37 +00004562static void cmd_completion(const char *name, const char *list)
4563{
4564 const char *p, *pstart;
4565 char cmd[128];
4566 int len;
4567
4568 p = list;
4569 for(;;) {
4570 pstart = p;
4571 p = strchr(p, '|');
4572 if (!p)
4573 p = pstart + strlen(pstart);
4574 len = p - pstart;
4575 if (len > sizeof(cmd) - 2)
4576 len = sizeof(cmd) - 2;
4577 memcpy(cmd, pstart, len);
4578 cmd[len] = '\0';
4579 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004580 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004581 }
4582 if (*p == '\0')
4583 break;
4584 p++;
4585 }
4586}
4587
4588static void file_completion(const char *input)
4589{
4590 DIR *ffs;
4591 struct dirent *d;
4592 char path[1024];
4593 char file[1024], file_prefix[1024];
4594 int input_path_len;
4595 const char *p;
4596
ths5fafdf22007-09-16 21:08:06 +00004597 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004598 if (!p) {
4599 input_path_len = 0;
4600 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004601 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004602 } else {
4603 input_path_len = p - input + 1;
4604 memcpy(path, input, input_path_len);
4605 if (input_path_len > sizeof(path) - 1)
4606 input_path_len = sizeof(path) - 1;
4607 path[input_path_len] = '\0';
4608 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4609 }
4610#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004611 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4612 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004613#endif
4614 ffs = opendir(path);
4615 if (!ffs)
4616 return;
4617 for(;;) {
4618 struct stat sb;
4619 d = readdir(ffs);
4620 if (!d)
4621 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004622
4623 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4624 continue;
4625 }
4626
bellard81d09122004-07-14 17:21:37 +00004627 if (strstart(d->d_name, file_prefix, NULL)) {
4628 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004629 if (input_path_len < sizeof(file))
4630 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4631 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004632 /* stat the file to find out if it's a directory.
4633 * In that case add a slash to speed up typing long paths
4634 */
4635 stat(file, &sb);
4636 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004637 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004638 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004639 }
4640 }
4641 closedir(ffs);
4642}
4643
aliguori51de9762009-03-05 23:00:43 +00004644static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004645{
aliguori51de9762009-03-05 23:00:43 +00004646 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004647 const char *input = opaque;
4648
4649 if (input[0] == '\0' ||
4650 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004651 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004652 }
4653}
4654
4655/* NOTE: this parser is an approximate form of the real command parser */
4656static void parse_cmdline(const char *cmdline,
4657 int *pnb_args, char **args)
4658{
4659 const char *p;
4660 int nb_args, ret;
4661 char buf[1024];
4662
4663 p = cmdline;
4664 nb_args = 0;
4665 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004666 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004667 p++;
4668 if (*p == '\0')
4669 break;
4670 if (nb_args >= MAX_ARGS)
4671 break;
4672 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004673 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004674 nb_args++;
4675 if (ret < 0)
4676 break;
4677 }
4678 *pnb_args = nb_args;
4679}
4680
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004681static const char *next_arg_type(const char *typestr)
4682{
4683 const char *p = strchr(typestr, ':');
4684 return (p != NULL ? ++p : typestr);
4685}
4686
aliguori4c36ba32009-03-05 23:01:37 +00004687static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004688{
4689 const char *cmdname;
4690 char *args[MAX_ARGS];
4691 int nb_args, i, len;
4692 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004693 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004694 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004695
4696 parse_cmdline(cmdline, &nb_args, args);
4697#ifdef DEBUG_COMPLETION
4698 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004699 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004700 }
4701#endif
4702
4703 /* if the line ends with a space, it means we want to complete the
4704 next arg */
4705 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004706 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004707 if (nb_args >= MAX_ARGS) {
4708 goto cleanup;
4709 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004710 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004711 }
4712 if (nb_args <= 1) {
4713 /* command completion */
4714 if (nb_args == 0)
4715 cmdname = "";
4716 else
4717 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004718 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004719 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004720 cmd_completion(cmdname, cmd->name);
4721 }
4722 } else {
4723 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004724 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4725 if (compare_cmd(args[0], cmd->name)) {
4726 break;
4727 }
bellard81d09122004-07-14 17:21:37 +00004728 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004729 if (!cmd->name) {
4730 goto cleanup;
4731 }
4732
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004733 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004734 for(i = 0; i < nb_args - 2; i++) {
4735 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004736 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004737 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004738 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004739 }
4740 }
4741 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004742 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004743 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004744 }
bellard81d09122004-07-14 17:21:37 +00004745 switch(*ptype) {
4746 case 'F':
4747 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004748 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004749 file_completion(str);
4750 break;
4751 case 'B':
4752 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004753 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004754 bdrv_iterate(block_completion_it, (void *)str);
4755 break;
bellard7fe48482004-10-09 18:08:01 +00004756 case 's':
4757 /* XXX: more generic ? */
4758 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004759 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004760 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4761 cmd_completion(str, cmd->name);
4762 }
bellard64866c32006-05-07 18:03:31 +00004763 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004764 char *sep = strrchr(str, '-');
4765 if (sep)
4766 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004767 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004768 for(key = key_defs; key->name != NULL; key++) {
4769 cmd_completion(str, key->name);
4770 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004771 } else if (!strcmp(cmd->name, "help|?")) {
4772 readline_set_completion_index(cur_mon->rs, strlen(str));
4773 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4774 cmd_completion(str, cmd->name);
4775 }
bellard7fe48482004-10-09 18:08:01 +00004776 }
4777 break;
bellard81d09122004-07-14 17:21:37 +00004778 default:
4779 break;
4780 }
4781 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004782
4783cleanup:
4784 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004785 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004786 }
bellard81d09122004-07-14 17:21:37 +00004787}
4788
aliguori731b0362009-03-05 23:01:42 +00004789static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004790{
aliguori731b0362009-03-05 23:01:42 +00004791 Monitor *mon = opaque;
4792
Jan Kiszkac62313b2009-12-04 14:05:29 +01004793 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004794}
4795
Luiz Capitulino09069b12010-02-04 18:10:06 -02004796static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4797{
4798 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4799 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4800}
4801
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004802/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004803 * Argument validation rules:
4804 *
4805 * 1. The argument must exist in cmd_args qdict
4806 * 2. The argument type must be the expected one
4807 *
4808 * Special case: If the argument doesn't exist in cmd_args and
4809 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4810 * checking is skipped for it.
4811 */
4812static int check_client_args_type(const QDict *client_args,
4813 const QDict *cmd_args, int flags)
4814{
4815 const QDictEntry *ent;
4816
4817 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4818 QObject *obj;
4819 QString *arg_type;
4820 const QObject *client_arg = qdict_entry_value(ent);
4821 const char *client_arg_name = qdict_entry_key(ent);
4822
4823 obj = qdict_get(cmd_args, client_arg_name);
4824 if (!obj) {
4825 if (flags & QMP_ACCEPT_UNKNOWNS) {
4826 /* handler accepts unknowns */
4827 continue;
4828 }
4829 /* client arg doesn't exist */
4830 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4831 return -1;
4832 }
4833
4834 arg_type = qobject_to_qstring(obj);
4835 assert(arg_type != NULL);
4836
4837 /* check if argument's type is correct */
4838 switch (qstring_get_str(arg_type)[0]) {
4839 case 'F':
4840 case 'B':
4841 case 's':
4842 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4843 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4844 "string");
4845 return -1;
4846 }
4847 break;
4848 case 'i':
4849 case 'l':
4850 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004851 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004852 if (qobject_type(client_arg) != QTYPE_QINT) {
4853 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4854 "int");
4855 return -1;
4856 }
4857 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004858 case 'T':
4859 if (qobject_type(client_arg) != QTYPE_QINT &&
4860 qobject_type(client_arg) != QTYPE_QFLOAT) {
4861 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4862 "number");
4863 return -1;
4864 }
4865 break;
4866 case 'b':
4867 case '-':
4868 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4869 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4870 "bool");
4871 return -1;
4872 }
4873 break;
4874 case 'O':
4875 assert(flags & QMP_ACCEPT_UNKNOWNS);
4876 break;
4877 case '/':
4878 case '.':
4879 /*
4880 * These types are not supported by QMP and thus are not
4881 * handled here. Fall through.
4882 */
4883 default:
4884 abort();
4885 }
4886 }
4887
4888 return 0;
4889}
4890
4891/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004892 * - Check if the client has passed all mandatory args
4893 * - Set special flags for argument validation
4894 */
4895static int check_mandatory_args(const QDict *cmd_args,
4896 const QDict *client_args, int *flags)
4897{
4898 const QDictEntry *ent;
4899
4900 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4901 const char *cmd_arg_name = qdict_entry_key(ent);
4902 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4903 assert(type != NULL);
4904
4905 if (qstring_get_str(type)[0] == 'O') {
4906 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4907 *flags |= QMP_ACCEPT_UNKNOWNS;
4908 } else if (qstring_get_str(type)[0] != '-' &&
4909 qstring_get_str(type)[1] != '?' &&
4910 !qdict_haskey(client_args, cmd_arg_name)) {
4911 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4912 return -1;
4913 }
4914 }
4915
4916 return 0;
4917}
4918
4919static QDict *qdict_from_args_type(const char *args_type)
4920{
4921 int i;
4922 QDict *qdict;
4923 QString *key, *type, *cur_qs;
4924
4925 assert(args_type != NULL);
4926
4927 qdict = qdict_new();
4928
4929 if (args_type == NULL || args_type[0] == '\0') {
4930 /* no args, empty qdict */
4931 goto out;
4932 }
4933
4934 key = qstring_new();
4935 type = qstring_new();
4936
4937 cur_qs = key;
4938
4939 for (i = 0;; i++) {
4940 switch (args_type[i]) {
4941 case ',':
4942 case '\0':
4943 qdict_put(qdict, qstring_get_str(key), type);
4944 QDECREF(key);
4945 if (args_type[i] == '\0') {
4946 goto out;
4947 }
4948 type = qstring_new(); /* qdict has ref */
4949 cur_qs = key = qstring_new();
4950 break;
4951 case ':':
4952 cur_qs = type;
4953 break;
4954 default:
4955 qstring_append_chr(cur_qs, args_type[i]);
4956 break;
4957 }
4958 }
4959
4960out:
4961 return qdict;
4962}
4963
4964/*
4965 * Client argument checking rules:
4966 *
4967 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004968 * 2. Each argument provided by the client must be expected
4969 * 3. Each argument provided by the client must have the type expected
4970 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004971 */
4972static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4973{
4974 int flags, err;
4975 QDict *cmd_args;
4976
4977 cmd_args = qdict_from_args_type(cmd->args_type);
4978
4979 flags = 0;
4980 err = check_mandatory_args(cmd_args, client_args, &flags);
4981 if (err) {
4982 goto out;
4983 }
4984
Luiz Capitulino4af91932010-06-22 11:44:05 -03004985 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004986
4987out:
4988 QDECREF(cmd_args);
4989 return err;
4990}
4991
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004992/*
4993 * Input object checking rules
4994 *
4995 * 1. Input object must be a dict
4996 * 2. The "execute" key must exist
4997 * 3. The "execute" key must be a string
4998 * 4. If the "arguments" key exists, it must be a dict
4999 * 5. If the "id" key exists, it can be anything (ie. json-value)
5000 * 6. Any argument not listed above is considered invalid
5001 */
5002static QDict *qmp_check_input_obj(QObject *input_obj)
5003{
5004 const QDictEntry *ent;
5005 int has_exec_key = 0;
5006 QDict *input_dict;
5007
5008 if (qobject_type(input_obj) != QTYPE_QDICT) {
5009 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
5010 return NULL;
5011 }
5012
5013 input_dict = qobject_to_qdict(input_obj);
5014
5015 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
5016 const char *arg_name = qdict_entry_key(ent);
5017 const QObject *arg_obj = qdict_entry_value(ent);
5018
5019 if (!strcmp(arg_name, "execute")) {
5020 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
5021 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
5022 "string");
5023 return NULL;
5024 }
5025 has_exec_key = 1;
5026 } else if (!strcmp(arg_name, "arguments")) {
5027 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5028 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5029 "object");
5030 return NULL;
5031 }
5032 } else if (!strcmp(arg_name, "id")) {
5033 /* FIXME: check duplicated IDs for async commands */
5034 } else {
5035 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5036 return NULL;
5037 }
5038 }
5039
5040 if (!has_exec_key) {
5041 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5042 return NULL;
5043 }
5044
5045 return input_dict;
5046}
5047
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005048static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
5049{
5050 QObject *ret_data = NULL;
5051
Luiz Capitulino4903de02010-09-16 11:01:32 -03005052 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005053 qmp_async_info_handler(mon, cmd);
5054 if (monitor_has_error(mon)) {
5055 monitor_protocol_emitter(mon, NULL);
5056 }
5057 } else {
5058 cmd->mhandler.info_new(mon, &ret_data);
Luiz Capitulinoc01e6882010-11-22 16:22:47 -02005059 monitor_protocol_emitter(mon, ret_data);
5060 qobject_decref(ret_data);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005061 }
5062}
5063
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005064static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5065 const QDict *params)
5066{
5067 int ret;
5068 QObject *data = NULL;
5069
5070 mon_print_count_init(mon);
5071
5072 ret = cmd->mhandler.cmd_new(mon, params, &data);
5073 handler_audit(mon, cmd, ret);
5074 monitor_protocol_emitter(mon, data);
5075 qobject_decref(data);
5076}
5077
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005078static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5079{
5080 int err;
5081 QObject *obj;
5082 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005083 const mon_cmd_t *cmd;
5084 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005085 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005086
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005087 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005088 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005089
5090 obj = json_parser_parse(tokens, NULL);
5091 if (!obj) {
5092 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005093 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005094 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005095 }
5096
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005097 input = qmp_check_input_obj(obj);
5098 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005099 qobject_decref(obj);
5100 goto err_out;
5101 }
5102
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005103 mon->mc->id = qdict_get(input, "id");
5104 qobject_incref(mon->mc->id);
5105
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005106 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005107 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02005108 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005109 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005110 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005111 }
5112
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005113 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005114 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02005115 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03005116 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03005117 }
5118
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005119 if (!cmd) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005120 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5121 goto err_out;
5122 }
5123
5124 obj = qdict_get(input, "arguments");
5125 if (!obj) {
5126 args = qdict_new();
5127 } else {
5128 args = qobject_to_qdict(obj);
5129 QINCREF(args);
5130 }
5131
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005132 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005133 if (err < 0) {
5134 goto err_out;
5135 }
5136
Luiz Capitulino030db6e2010-09-10 16:03:38 -03005137 if (query_cmd) {
5138 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03005139 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005140 err = qmp_async_cmd_handler(mon, cmd, args);
5141 if (err) {
5142 /* emit the error response */
5143 goto err_out;
5144 }
Adam Litke940cc302010-01-25 12:18:44 -06005145 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005146 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005147 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005148
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005149 goto out;
5150
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005151err_out:
5152 monitor_protocol_emitter(mon, NULL);
5153out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005154 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005155 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005156}
5157
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005158/**
5159 * monitor_control_read(): Read and handle QMP input
5160 */
5161static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5162{
5163 Monitor *old_mon = cur_mon;
5164
5165 cur_mon = opaque;
5166
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005167 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005168
5169 cur_mon = old_mon;
5170}
5171
aliguori731b0362009-03-05 23:01:42 +00005172static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005173{
aliguori731b0362009-03-05 23:01:42 +00005174 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005175 int i;
aliguori376253e2009-03-05 23:01:23 +00005176
aliguori731b0362009-03-05 23:01:42 +00005177 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005178
aliguoricde76ee2009-03-05 23:01:51 +00005179 if (cur_mon->rs) {
5180 for (i = 0; i < size; i++)
5181 readline_handle_byte(cur_mon->rs, buf[i]);
5182 } else {
5183 if (size == 0 || buf[size - 1] != 0)
5184 monitor_printf(cur_mon, "corrupted command\n");
5185 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005186 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005187 }
aliguori731b0362009-03-05 23:01:42 +00005188
5189 cur_mon = old_mon;
5190}
aliguorid8f44602008-10-06 13:52:44 +00005191
aliguori376253e2009-03-05 23:01:23 +00005192static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005193{
aliguori731b0362009-03-05 23:01:42 +00005194 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005195 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005196 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005197}
5198
aliguoricde76ee2009-03-05 23:01:51 +00005199int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005200{
aliguoricde76ee2009-03-05 23:01:51 +00005201 if (!mon->rs)
5202 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005203 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005204 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005205}
5206
aliguori376253e2009-03-05 23:01:23 +00005207void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005208{
aliguoricde76ee2009-03-05 23:01:51 +00005209 if (!mon->rs)
5210 return;
aliguori731b0362009-03-05 23:01:42 +00005211 if (--mon->suspend_cnt == 0)
5212 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005213}
5214
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005215static QObject *get_qmp_greeting(void)
5216{
5217 QObject *ver;
5218
5219 do_info_version(NULL, &ver);
5220 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5221}
5222
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005223/**
5224 * monitor_control_event(): Print QMP gretting
5225 */
5226static void monitor_control_event(void *opaque, int event)
5227{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005228 QObject *data;
5229 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005230
Luiz Capitulino47116d12010-02-08 17:01:30 -02005231 switch (event) {
5232 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005233 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005234 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005235 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005236 monitor_json_emitter(mon, data);
5237 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02005238 break;
5239 case CHR_EVENT_CLOSED:
5240 json_message_parser_destroy(&mon->mc->parser);
5241 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005242 }
5243}
5244
aliguori731b0362009-03-05 23:01:42 +00005245static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005246{
aliguori376253e2009-03-05 23:01:23 +00005247 Monitor *mon = opaque;
5248
aliguori2724b182009-03-05 23:01:47 +00005249 switch (event) {
5250 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005251 mon->mux_out = 0;
5252 if (mon->reset_seen) {
5253 readline_restart(mon->rs);
5254 monitor_resume(mon);
5255 monitor_flush(mon);
5256 } else {
5257 mon->suspend_cnt = 0;
5258 }
aliguori2724b182009-03-05 23:01:47 +00005259 break;
ths86e94de2007-01-05 22:01:59 +00005260
aliguori2724b182009-03-05 23:01:47 +00005261 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005262 if (mon->reset_seen) {
5263 if (mon->suspend_cnt == 0) {
5264 monitor_printf(mon, "\n");
5265 }
5266 monitor_flush(mon);
5267 monitor_suspend(mon);
5268 } else {
5269 mon->suspend_cnt++;
5270 }
5271 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005272 break;
5273
Amit Shahb6b8df52009-10-07 18:31:16 +05305274 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005275 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5276 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005277 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005278 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005279 }
5280 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00005281 break;
5282 }
ths86e94de2007-01-05 22:01:59 +00005283}
5284
aliguori76655d62009-03-06 20:27:37 +00005285
5286/*
5287 * Local variables:
5288 * c-indent-level: 4
5289 * c-basic-offset: 4
5290 * tab-width: 8
5291 * End:
5292 */
5293
aliguori731b0362009-03-05 23:01:42 +00005294void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005295{
aliguori731b0362009-03-05 23:01:42 +00005296 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005297 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005298
5299 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01005300 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00005301 is_first_init = 0;
5302 }
aliguori87127162009-03-05 23:01:29 +00005303
Anthony Liguori7267c092011-08-20 22:09:37 -05005304 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00005305
aliguori87127162009-03-05 23:01:29 +00005306 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005307 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005308 if (flags & MONITOR_USE_READLINE) {
5309 mon->rs = readline_init(mon, monitor_find_completion);
5310 monitor_read_command(mon, 0);
5311 }
aliguori87127162009-03-05 23:01:29 +00005312
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005313 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005314 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005315 /* Control mode requires special handlers */
5316 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5317 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005318 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005319 } else {
5320 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5321 monitor_event, mon);
5322 }
aliguori87127162009-03-05 23:01:29 +00005323
Blue Swirl72cf2d42009-09-12 07:36:22 +00005324 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005325 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5326 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005327}
5328
aliguori376253e2009-03-05 23:01:23 +00005329static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00005330{
aliguoribb5fc202009-03-05 23:01:15 +00005331 BlockDriverState *bs = opaque;
5332 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005333
aliguoribb5fc202009-03-05 23:01:15 +00005334 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005335 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005336 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005337 }
aliguori731b0362009-03-05 23:01:42 +00005338 if (mon->password_completion_cb)
5339 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005340
aliguori731b0362009-03-05 23:01:42 +00005341 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005342}
aliguoric0f4ce72009-03-05 23:01:01 +00005343
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005344int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5345 BlockDriverCompletionFunc *completion_cb,
5346 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005347{
aliguoricde76ee2009-03-05 23:01:51 +00005348 int err;
5349
aliguoribb5fc202009-03-05 23:01:15 +00005350 if (!bdrv_key_required(bs)) {
5351 if (completion_cb)
5352 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005353 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005354 }
aliguoric0f4ce72009-03-05 23:01:01 +00005355
Luiz Capitulino94171e12009-12-07 21:37:00 +01005356 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005357 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005358 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005359 }
5360
aliguori376253e2009-03-05 23:01:23 +00005361 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5362 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005363
aliguori731b0362009-03-05 23:01:42 +00005364 mon->password_completion_cb = completion_cb;
5365 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005366
aliguoricde76ee2009-03-05 23:01:51 +00005367 err = monitor_read_password(mon, bdrv_password_cb, bs);
5368
5369 if (err && completion_cb)
5370 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005371
5372 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005373}