blob: d6c4f85909db5941e614ca85f02b271afe1f34bd [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"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020036#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
ths6a5bd302007-12-03 17:05:38 +000073
Jan Kiszka661f1922011-10-16 11:53:13 +020074/* for pic/irq_info */
75#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010076#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020077#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010078#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020079
bellard9dc39cb2004-03-14 21:38:27 +000080//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000081//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000082
bellard9307c4c2004-04-04 12:57:25 +000083/*
84 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000085 *
bellard9307c4c2004-04-04 12:57:25 +000086 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000087 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000088 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080089 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010090 * 'O' option string of the form NAME=VALUE,...
91 * parsed according to QemuOptsList given by its name
92 * Example: 'device:O' uses qemu_device_opts.
93 * Restriction: only lists with empty desc are supported
94 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000095 * 'i' 32 bit integer
96 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030097 * 'M' Non-negative target long (32 or 64 bit), in user mode the
98 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020099 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200100 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
101 * K, k suffix, which multiplies the value by 2^60 for suffixes E
102 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
103 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100104 * 'T' double
105 * user mode accepts an optional ms, us, ns suffix,
106 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000107 * '/' optional gdb-like print format (like "/10x")
108 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300109 * '?' optional type (for all types, except '/')
110 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100111 * 'b' boolean
112 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000114 *
115 */
116
Adam Litke940cc302010-01-25 12:18:44 -0600117typedef struct MonitorCompletionData MonitorCompletionData;
118struct MonitorCompletionData {
119 Monitor *mon;
120 void (*user_print)(Monitor *mon, const QObject *data);
121};
122
Anthony Liguoric227f092009-10-01 16:12:16 -0500123typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000125 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000126 const char *params;
127 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300128 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300130 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200131 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600132 int (*cmd_async)(Monitor *mon, const QDict *params,
133 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300134 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200135 int flags;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800136 /* @sub_table is a list of 2nd level of commands. If it do not exist,
137 * mhandler should be used. If it exist, sub_table[?].mhandler should be
138 * used, and mhandler of 1st level plays the role of help function.
139 */
140 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100141 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500142} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000143
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500145typedef struct mon_fd_t mon_fd_t;
146struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147 char *name;
148 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500149 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100150};
151
Corey Bryantba1c0482012-08-14 16:43:43 -0400152/* file descriptor associated with a file descriptor set */
153typedef struct MonFdsetFd MonFdsetFd;
154struct MonFdsetFd {
155 int fd;
156 bool removed;
157 char *opaque;
158 QLIST_ENTRY(MonFdsetFd) next;
159};
160
161/* file descriptor set containing fds passed via SCM_RIGHTS */
162typedef struct MonFdset MonFdset;
163struct MonFdset {
164 int64_t id;
165 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400166 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400167 QLIST_ENTRY(MonFdset) next;
168};
169
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200170typedef struct MonitorControl {
171 QObject *id;
172 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200173 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200174} MonitorControl;
175
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100176/*
177 * To prevent flooding clients, events can be throttled. The
178 * throttling is calculated globally, rather than per-Monitor
179 * instance.
180 */
181typedef struct MonitorEventState {
182 MonitorEvent event; /* Event being tracked */
183 int64_t rate; /* Period over which to throttle. 0 to disable */
184 int64_t last; /* Time at which event was last emitted */
185 QEMUTimer *timer; /* Timer for handling delayed events */
186 QObject *data; /* Event pending delayed dispatch */
187} MonitorEventState;
188
aliguori87127162009-03-05 23:01:29 +0000189struct Monitor {
190 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200191 int mux_out;
192 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000193 int flags;
194 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400195 bool skip_flush;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400196 QString *outbuf;
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200197 guint watch;
aliguori731b0362009-03-05 23:01:42 +0000198 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200199 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200200 CPUState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000201 BlockDriverCompletionFunc *password_completion_cb;
202 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800203 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200204 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500205 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000206 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000207};
208
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300209/* QMP checker flags */
210#define QMP_ACCEPT_UNKNOWNS 1
211
Blue Swirl72cf2d42009-09-12 07:36:22 +0000212static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400213static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400214static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000215
Wayne Xia816f8922011-10-12 11:32:41 +0800216static mon_cmd_t mon_cmds[];
217static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000218
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300219static const mon_cmd_t qmp_cmds[];
220
Markus Armbruster8631b602010-02-18 11:41:55 +0100221Monitor *cur_mon;
222Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000223
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100224static void monitor_command_cb(void *opaque, const char *cmdline,
225 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000226
Luiz Capitulino09069b12010-02-04 18:10:06 -0200227static inline int qmp_cmd_mode(const Monitor *mon)
228{
229 return (mon->mc ? mon->mc->command_mode : 0);
230}
231
Luiz Capitulino418173c2009-11-26 22:58:51 -0200232/* Return true if in control mode, false otherwise */
233static inline int monitor_ctrl_mode(const Monitor *mon)
234{
235 return (mon->flags & MONITOR_USE_CONTROL);
236}
237
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100238/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
239int monitor_cur_is_qmp(void)
240{
241 return cur_mon && monitor_ctrl_mode(cur_mon);
242}
243
Anthony Liguori7060b472011-09-02 12:34:50 -0500244void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000245{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200246 if (!mon->rs)
247 return;
248
aliguori731b0362009-03-05 23:01:42 +0000249 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
250 if (show_prompt)
251 readline_show_prompt(mon->rs);
252}
bellard6a00d602005-11-21 23:25:50 +0000253
Anthony Liguori7060b472011-09-02 12:34:50 -0500254int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
255 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000256{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100257 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100258 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100259 return -EINVAL;
260 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000261 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
262 /* prompt is printed on return from the command handler */
263 return 0;
264 } else {
265 monitor_printf(mon, "terminal does not support password prompting\n");
266 return -ENOTTY;
267 }
aliguoribb5fc202009-03-05 23:01:15 +0000268}
269
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100270static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
271 void *opaque)
272{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200273 Monitor *mon = opaque;
274
275 mon->watch = 0;
276 monitor_flush(mon);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100277 return FALSE;
278}
279
aliguori376253e2009-03-05 23:01:23 +0000280void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000281{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100282 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400283 size_t len;
284 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100285
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400286 if (mon->skip_flush) {
287 return;
288 }
289
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400290 buf = qstring_get_str(mon->outbuf);
291 len = qstring_get_length(mon->outbuf);
292
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200293 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200295 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
296 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400297 QDECREF(mon->outbuf);
298 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100299 return;
300 }
301 if (rc > 0) {
302 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400303 QString *tmp = qstring_from_str(buf + rc);
304 QDECREF(mon->outbuf);
305 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100306 }
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200307 if (mon->watch == 0) {
308 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
309 monitor_unblocked, mon);
310 }
bellard7e2515e2004-08-01 21:52:19 +0000311 }
312}
313
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400314/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000315static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000316{
ths60fe76f2007-12-16 03:02:09 +0000317 char c;
aliguori731b0362009-03-05 23:01:42 +0000318
bellard7e2515e2004-08-01 21:52:19 +0000319 for(;;) {
320 c = *str++;
321 if (c == '\0')
322 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400323 if (c == '\n') {
324 qstring_append_chr(mon->outbuf, '\r');
325 }
326 qstring_append_chr(mon->outbuf, c);
327 if (c == '\n') {
aliguori376253e2009-03-05 23:01:23 +0000328 monitor_flush(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400329 }
bellard7e2515e2004-08-01 21:52:19 +0000330 }
331}
332
aliguori376253e2009-03-05 23:01:23 +0000333void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000334{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400335 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200336
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200337 if (!mon)
338 return;
339
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200340 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200341 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200342 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200343
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400344 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200345 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400346 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000347}
348
aliguori376253e2009-03-05 23:01:23 +0000349void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000350{
351 va_list ap;
352 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000353 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000354 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000355}
356
Stefan Weil8b7968f2010-09-23 21:28:05 +0200357static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
358 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000359{
360 va_list ap;
361 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000362 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000363 va_end(ap);
364 return 0;
365}
366
Luiz Capitulino13c74252009-10-07 13:41:55 -0300367static void monitor_user_noop(Monitor *mon, const QObject *data) { }
368
Luiz Capitulino9e807212010-09-16 10:58:59 -0300369static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300370{
371 return cmd->user_print != NULL;
372}
373
Luiz Capitulino4903de02010-09-16 11:01:32 -0300374static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600375{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200376 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600377}
378
Luiz Capitulino8204a912009-11-18 23:05:31 -0200379static inline int monitor_has_error(const Monitor *mon)
380{
381 return mon->error != NULL;
382}
383
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200384static void monitor_json_emitter(Monitor *mon, const QObject *data)
385{
386 QString *json;
387
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200388 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
389 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390 assert(json != NULL);
391
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200392 qstring_append_chr(json, '\n');
393 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200394
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200395 QDECREF(json);
396}
397
Luiz Capitulinode253f12012-07-27 16:18:16 -0300398static QDict *build_qmp_error_dict(const QError *err)
399{
400 QObject *obj;
401
402 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
403 ErrorClass_lookup[err->err_class],
404 qerror_human(err));
405
406 return qobject_to_qdict(obj);
407}
408
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200409static void monitor_protocol_emitter(Monitor *mon, QObject *data)
410{
411 QDict *qmp;
412
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100413 trace_monitor_protocol_emitter(mon);
414
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200415 if (!monitor_has_error(mon)) {
416 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300417 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418 if (data) {
419 qobject_incref(data);
420 qdict_put_obj(qmp, "return", data);
421 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200422 /* return an empty QDict by default */
423 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 }
425 } else {
426 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300427 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200428 QDECREF(mon->error);
429 mon->error = NULL;
430 }
431
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200432 if (mon->mc->id) {
433 qdict_put_obj(qmp, "id", mon->mc->id);
434 mon->mc->id = NULL;
435 }
436
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 monitor_json_emitter(mon, QOBJECT(qmp));
438 QDECREF(qmp);
439}
440
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441static void timestamp_put(QDict *qdict)
442{
443 int err;
444 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000445 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200446
Blue Swirld08d6f02009-12-04 18:06:39 +0000447 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200448 if (err < 0)
449 return;
450
451 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
452 "'microseconds': %" PRId64 " }",
453 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200454 qdict_put_obj(qdict, "timestamp", obj);
455}
456
Daniel P. Berrange48608532012-05-21 17:59:51 +0100457
458static const char *monitor_event_names[] = {
459 [QEVENT_SHUTDOWN] = "SHUTDOWN",
460 [QEVENT_RESET] = "RESET",
461 [QEVENT_POWERDOWN] = "POWERDOWN",
462 [QEVENT_STOP] = "STOP",
463 [QEVENT_RESUME] = "RESUME",
464 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
465 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
466 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
467 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
468 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
469 [QEVENT_WATCHDOG] = "WATCHDOG",
470 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
471 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
472 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
473 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
474 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
Paolo Bonzini32c81a42012-09-28 17:22:58 +0200475 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
Paolo Bonzinia66a2a32012-07-23 15:15:47 +0200476 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
Michael S. Tsirkin0402a5d2013-03-06 14:58:59 +0200477 [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100478 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
Amos Kongb1be4282013-06-14 15:45:52 +0800479 [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100480 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300481 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100482 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100483 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300484 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Hu Taoc401a8a2013-04-26 11:24:41 +0800485 [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED",
Max Reitza40f1c22013-08-30 14:34:25 +0200486 [QEVENT_BLOCK_IMAGE_CORRUPTED] = "BLOCK_IMAGE_CORRUPTED",
Benoît Canet95c6bff2014-02-21 22:21:15 +0100487 [QEVENT_QUORUM_FAILURE] = "QUORUM_FAILURE",
488 [QEVENT_QUORUM_REPORT_BAD] = "QUORUM_REPORT_BAD",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100489};
490QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
491
Stefan Weil79f32022014-05-02 22:43:57 +0200492static MonitorEventState monitor_event_state[QEVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493
494/*
495 * Emits the event to every monitor instance
496 */
497static void
498monitor_protocol_event_emit(MonitorEvent event,
499 QObject *data)
500{
501 Monitor *mon;
502
503 trace_monitor_protocol_event_emit(event, data);
504 QLIST_FOREACH(mon, &mon_list, entry) {
505 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
506 monitor_json_emitter(mon, data);
507 }
508 }
509}
510
511
512/*
513 * Queue a new event for emission to Monitor instances,
514 * applying any rate limiting if required.
515 */
516static void
517monitor_protocol_event_queue(MonitorEvent event,
518 QObject *data)
519{
520 MonitorEventState *evstate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100521 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 assert(event < QEVENT_MAX);
523
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100524 evstate = &(monitor_event_state[event]);
525 trace_monitor_protocol_event_queue(event,
526 data,
527 evstate->rate,
528 evstate->last,
529 now);
530
531 /* Rate limit of 0 indicates no throttling */
532 if (!evstate->rate) {
533 monitor_protocol_event_emit(event, data);
534 evstate->last = now;
535 } else {
536 int64_t delta = now - evstate->last;
537 if (evstate->data ||
538 delta < evstate->rate) {
539 /* If there's an existing event pending, replace
540 * it with the new event, otherwise schedule a
541 * timer for delayed emission
542 */
543 if (evstate->data) {
544 qobject_decref(evstate->data);
545 } else {
546 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100547 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548 }
549 evstate->data = data;
550 qobject_incref(evstate->data);
551 } else {
552 monitor_protocol_event_emit(event, data);
553 evstate->last = now;
554 }
555 }
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100556}
557
558
559/*
560 * The callback invoked by QemuTimer when a delayed
561 * event is ready to be emitted
562 */
563static void monitor_protocol_event_handler(void *opaque)
564{
565 MonitorEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100566 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100567
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568
569 trace_monitor_protocol_event_handler(evstate->event,
570 evstate->data,
571 evstate->last,
572 now);
573 if (evstate->data) {
574 monitor_protocol_event_emit(evstate->event, evstate->data);
575 qobject_decref(evstate->data);
576 evstate->data = NULL;
577 }
578 evstate->last = now;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100579}
580
581
582/*
583 * @event: the event ID to be limited
584 * @rate: the rate limit in milliseconds
585 *
586 * Sets a rate limit on a particular event, so no
587 * more than 1 event will be emitted within @rate
588 * milliseconds
589 */
590static void
591monitor_protocol_event_throttle(MonitorEvent event,
592 int64_t rate)
593{
594 MonitorEventState *evstate;
595 assert(event < QEVENT_MAX);
596
597 evstate = &(monitor_event_state[event]);
598
599 trace_monitor_protocol_event_throttle(event, rate);
600 evstate->event = event;
601 evstate->rate = rate * SCALE_MS;
Alex Blighbc72ad62013-08-21 16:03:08 +0100602 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100603 SCALE_MS,
604 monitor_protocol_event_handler,
605 evstate);
606 evstate->last = 0;
607 evstate->data = NULL;
608}
609
610
611/* Global, one-time initializer to configure the rate limiting
612 * and initialize state */
613static void monitor_protocol_event_init(void)
614{
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100615 /* Limit RTC & BALLOON events to 1 per second */
616 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
617 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
618 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
Benoît Canetc88a1de2014-02-21 22:21:20 +0100619 /* limit the rate of quorum events to avoid hammering the management */
620 monitor_protocol_event_throttle(QEVENT_QUORUM_REPORT_BAD, 1000);
621 monitor_protocol_event_throttle(QEVENT_QUORUM_FAILURE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100622}
623
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200624/**
625 * monitor_protocol_event(): Generate a Monitor event
626 *
627 * Event-specific data can be emitted through the (optional) 'data' parameter.
628 */
629void monitor_protocol_event(MonitorEvent event, QObject *data)
630{
631 QDict *qmp;
632 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200633
Blue Swirl242cd002009-12-04 18:05:45 +0000634 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200635
Daniel P. Berrange48608532012-05-21 17:59:51 +0100636 event_name = monitor_event_names[event];
637 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200638
639 qmp = qdict_new();
640 timestamp_put(qmp);
641 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200642 if (data) {
643 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200644 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200645 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200646
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100647 trace_monitor_protocol_event(event, event_name, qmp);
648 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200649 QDECREF(qmp);
650}
651
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200652static int do_qmp_capabilities(Monitor *mon, const QDict *params,
653 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200654{
655 /* Will setup QMP capabilities in the future */
656 if (monitor_ctrl_mode(mon)) {
657 mon->mc->command_mode = 1;
658 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200659
660 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200661}
662
Luiz Capitulino0268d972010-10-22 10:08:02 -0200663static void handle_user_command(Monitor *mon, const char *cmdline);
664
Wenchao Xiab01fe892013-08-27 20:38:19 +0800665static void monitor_data_init(Monitor *mon)
666{
667 memset(mon, 0, sizeof(Monitor));
668 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800669 /* Use *mon_cmds by default. */
670 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800671}
672
673static void monitor_data_destroy(Monitor *mon)
674{
675 QDECREF(mon->outbuf);
676}
677
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200678char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
679 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200680{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200681 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200682 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200683
Wenchao Xiab01fe892013-08-27 20:38:19 +0800684 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400685 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200686
687 old_mon = cur_mon;
688 cur_mon = &hmp;
689
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200690 if (has_cpu_index) {
691 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200692 if (ret < 0) {
693 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200694 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
695 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200696 goto out;
697 }
698 }
699
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200700 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200701 cur_mon = old_mon;
702
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400703 if (qstring_get_length(hmp.outbuf) > 0) {
704 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200705 } else {
706 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200707 }
708
709out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800710 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200711 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200712}
713
bellard9dc39cb2004-03-14 21:38:27 +0000714static int compare_cmd(const char *name, const char *list)
715{
716 const char *p, *pstart;
717 int len;
718 len = strlen(name);
719 p = list;
720 for(;;) {
721 pstart = p;
722 p = strchr(p, '|');
723 if (!p)
724 p = pstart + strlen(pstart);
725 if ((p - pstart) == len && !memcmp(pstart, name, len))
726 return 1;
727 if (*p == '\0')
728 break;
729 p++;
730 }
731 return 0;
732}
733
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800734static int get_str(char *buf, int buf_size, const char **pp)
735{
736 const char *p;
737 char *q;
738 int c;
739
740 q = buf;
741 p = *pp;
742 while (qemu_isspace(*p)) {
743 p++;
744 }
745 if (*p == '\0') {
746 fail:
747 *q = '\0';
748 *pp = p;
749 return -1;
750 }
751 if (*p == '\"') {
752 p++;
753 while (*p != '\0' && *p != '\"') {
754 if (*p == '\\') {
755 p++;
756 c = *p++;
757 switch (c) {
758 case 'n':
759 c = '\n';
760 break;
761 case 'r':
762 c = '\r';
763 break;
764 case '\\':
765 case '\'':
766 case '\"':
767 break;
768 default:
769 qemu_printf("unsupported escape code: '\\%c'\n", c);
770 goto fail;
771 }
772 if ((q - buf) < buf_size - 1) {
773 *q++ = c;
774 }
775 } else {
776 if ((q - buf) < buf_size - 1) {
777 *q++ = *p;
778 }
779 p++;
780 }
781 }
782 if (*p != '\"') {
783 qemu_printf("unterminated string\n");
784 goto fail;
785 }
786 p++;
787 } else {
788 while (*p != '\0' && !qemu_isspace(*p)) {
789 if ((q - buf) < buf_size - 1) {
790 *q++ = *p;
791 }
792 p++;
793 }
794 }
795 *q = '\0';
796 *pp = p;
797 return 0;
798}
799
800#define MAX_ARGS 16
801
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800802static void free_cmdline_args(char **args, int nb_args)
803{
804 int i;
805
806 assert(nb_args <= MAX_ARGS);
807
808 for (i = 0; i < nb_args; i++) {
809 g_free(args[i]);
810 }
811
812}
813
814/*
815 * Parse the command line to get valid args.
816 * @cmdline: command line to be parsed.
817 * @pnb_args: location to store the number of args, must NOT be NULL.
818 * @args: location to store the args, which should be freed by caller, must
819 * NOT be NULL.
820 *
821 * Returns 0 on success, negative on failure.
822 *
823 * NOTE: this parser is an approximate form of the real command parser. Number
824 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
825 * return with failure.
826 */
827static int parse_cmdline(const char *cmdline,
828 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800829{
830 const char *p;
831 int nb_args, ret;
832 char buf[1024];
833
834 p = cmdline;
835 nb_args = 0;
836 for (;;) {
837 while (qemu_isspace(*p)) {
838 p++;
839 }
840 if (*p == '\0') {
841 break;
842 }
843 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800844 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800845 }
846 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800847 if (ret < 0) {
848 goto fail;
849 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800850 args[nb_args] = g_strdup(buf);
851 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800852 }
853 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800854 return 0;
855
856 fail:
857 free_cmdline_args(args, nb_args);
858 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800859}
860
Wenchao Xia66855492013-08-27 20:38:23 +0800861static void help_cmd_dump_one(Monitor *mon,
862 const mon_cmd_t *cmd,
863 char **prefix_args,
864 int prefix_args_nb)
865{
866 int i;
867
868 for (i = 0; i < prefix_args_nb; i++) {
869 monitor_printf(mon, "%s ", prefix_args[i]);
870 }
871 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
872}
873
874/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500875static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800876 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000877{
Anthony Liguoric227f092009-10-01 16:12:16 -0500878 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000879
Wenchao Xia66855492013-08-27 20:38:23 +0800880 /* No valid arg need to compare with, dump all in *cmds */
881 if (arg_index >= nb_args) {
882 for (cmd = cmds; cmd->name != NULL; cmd++) {
883 help_cmd_dump_one(mon, cmd, args, arg_index);
884 }
885 return;
886 }
887
888 /* Find one entry to dump */
889 for (cmd = cmds; cmd->name != NULL; cmd++) {
890 if (compare_cmd(args[arg_index], cmd->name)) {
891 if (cmd->sub_table) {
892 /* continue with next arg */
893 help_cmd_dump(mon, cmd->sub_table,
894 args, nb_args, arg_index + 1);
895 } else {
896 help_cmd_dump_one(mon, cmd, args, arg_index);
897 }
898 break;
899 }
bellard9dc39cb2004-03-14 21:38:27 +0000900 }
901}
902
aliguori376253e2009-03-05 23:01:23 +0000903static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000904{
Wenchao Xia66855492013-08-27 20:38:23 +0800905 char *args[MAX_ARGS];
906 int nb_args = 0;
907
908 /* 1. parse user input */
909 if (name) {
910 /* special case for log, directly dump and return */
911 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000912 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000913 monitor_printf(mon, "Log items (comma separated):\n");
914 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000915 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000916 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000917 }
Wenchao Xia66855492013-08-27 20:38:23 +0800918 return;
919 }
920
921 if (parse_cmdline(name, &nb_args, args) < 0) {
922 return;
bellardf193c792004-03-21 17:06:25 +0000923 }
bellard9dc39cb2004-03-14 21:38:27 +0000924 }
Wenchao Xia66855492013-08-27 20:38:23 +0800925
926 /* 2. dump the contents according to parsed args */
927 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
928
929 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000930}
931
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300932static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300933{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300934 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300935}
936
Lluísfc764102011-08-31 20:31:18 +0200937static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530938{
939 const char *tp_name = qdict_get_str(qdict, "name");
940 bool new_state = qdict_get_bool(qdict, "option");
Blue Swirlf871d682010-10-13 19:14:29 +0000941
Lluís Vilanovaca285c32013-03-05 14:47:43 +0100942 bool found = false;
943 TraceEvent *ev = NULL;
944 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
945 found = true;
946 if (!trace_event_get_state_static(ev)) {
947 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
948 } else {
949 trace_event_set_state_dynamic(ev, new_state);
950 }
951 }
952 if (!trace_event_is_pattern(tp_name) && !found) {
Blue Swirlf871d682010-10-13 19:14:29 +0000953 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
954 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530955}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100956
Michael Rothc45a8162011-10-02 08:44:37 -0500957#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100958static void do_trace_file(Monitor *mon, const QDict *qdict)
959{
960 const char *op = qdict_get_try_str(qdict, "op");
961 const char *arg = qdict_get_try_str(qdict, "arg");
962
963 if (!op) {
964 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
965 } else if (!strcmp(op, "on")) {
966 st_set_trace_file_enabled(true);
967 } else if (!strcmp(op, "off")) {
968 st_set_trace_file_enabled(false);
969 } else if (!strcmp(op, "flush")) {
970 st_flush_trace_buffer();
971 } else if (!strcmp(op, "set")) {
972 if (arg) {
973 st_set_trace_file(arg);
974 }
975 } else {
976 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
977 help_cmd(mon, "trace-file");
978 }
979}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530980#endif
981
Adam Litke940cc302010-01-25 12:18:44 -0600982static void user_monitor_complete(void *opaque, QObject *ret_data)
983{
984 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
985
986 if (ret_data) {
987 data->user_print(data->mon, ret_data);
988 }
989 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500990 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600991}
992
993static void qmp_monitor_complete(void *opaque, QObject *ret_data)
994{
995 monitor_protocol_emitter(opaque, ret_data);
996}
997
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300998static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
999 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -06001000{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001001 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -06001002}
1003
Adam Litke940cc302010-01-25 12:18:44 -06001004static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1005 const QDict *params)
1006{
1007 int ret;
1008
Anthony Liguori7267c092011-08-20 22:09:37 -05001009 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -06001010 cb_data->mon = mon;
1011 cb_data->user_print = cmd->user_print;
1012 monitor_suspend(mon);
1013 ret = cmd->mhandler.cmd_async(mon, params,
1014 user_monitor_complete, cb_data);
1015 if (ret < 0) {
1016 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001017 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -06001018 }
1019}
1020
Wenchao Xia84c44612013-01-14 14:06:29 +08001021static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001022{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001023 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001024}
1025
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001026CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +00001027{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001028 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001029 const mon_cmd_t *cmd;
1030
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03001031 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -02001032 info = g_malloc0(sizeof(*info));
1033 info->value = g_malloc0(sizeof(*info->value));
1034 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001035
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001036 info->next = cmd_list;
1037 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001038 }
1039
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001040 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +00001041}
1042
Daniel P. Berrange48608532012-05-21 17:59:51 +01001043EventInfoList *qmp_query_events(Error **errp)
1044{
1045 EventInfoList *info, *ev_list = NULL;
1046 MonitorEvent e;
1047
1048 for (e = 0 ; e < QEVENT_MAX ; e++) {
1049 const char *event_name = monitor_event_names[e];
1050 assert(event_name != NULL);
1051 info = g_malloc0(sizeof(*info));
1052 info->value = g_malloc0(sizeof(*info->value));
1053 info->value->name = g_strdup(event_name);
1054
1055 info->next = ev_list;
1056 ev_list = info;
1057 }
1058
1059 return ev_list;
1060}
1061
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001062/* set the current CPU defined by the user */
1063int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001064{
Andreas Färber55e5c282012-12-17 06:18:02 +01001065 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001066
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001067 cpu = qemu_get_cpu(cpu_index);
1068 if (cpu == NULL) {
1069 return -1;
bellard6a00d602005-11-21 23:25:50 +00001070 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001071 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001072 return 0;
bellard6a00d602005-11-21 23:25:50 +00001073}
1074
Andreas Färber9349b4f2012-03-14 01:38:32 +01001075static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001076{
aliguori731b0362009-03-05 23:01:42 +00001077 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001078 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001079 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001080 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +02001081 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001082}
1083
Luiz Capitulino99b77962011-10-24 10:53:44 -02001084int monitor_get_cpu_index(void)
1085{
Andreas Färber55e5c282012-12-17 06:18:02 +01001086 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1087 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001088}
1089
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001090static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001091{
Andreas Färber878096e2013-05-27 01:33:50 +02001092 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001093 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +00001094 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001095 cpu = ENV_GET_CPU(env);
1096 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001097}
1098
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001099static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001100{
aliguori376253e2009-03-05 23:01:23 +00001101 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001102}
1103
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001104static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001105{
1106 int i;
bellard7e2515e2004-08-01 21:52:19 +00001107 const char *str;
ths3b46e622007-09-17 08:09:54 +00001108
aliguoricde76ee2009-03-05 23:01:51 +00001109 if (!mon->rs)
1110 return;
bellard7e2515e2004-08-01 21:52:19 +00001111 i = 0;
1112 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001113 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001114 if (!str)
1115 break;
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001117 i++;
bellardaa455482004-04-04 13:07:25 +00001118 }
1119}
1120
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001121static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001122{
Andreas Färber878096e2013-05-27 01:33:50 +02001123 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001124 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001125
1126 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001127 cpu = ENV_GET_CPU(env);
1128 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001129}
j_mayer76a66252007-03-07 08:32:30 +00001130
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001131static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301132{
Lluísfc764102011-08-31 20:31:18 +02001133 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301134}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301135
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001136static int client_migrate_info(Monitor *mon, const QDict *qdict,
1137 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001138{
1139 const char *protocol = qdict_get_str(qdict, "protocol");
1140 const char *hostname = qdict_get_str(qdict, "hostname");
1141 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1142 int port = qdict_get_try_int(qdict, "port", -1);
1143 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1144 int ret;
1145
1146 if (strcmp(protocol, "spice") == 0) {
1147 if (!using_spice) {
1148 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1149 return -1;
1150 }
1151
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001152 if (port == -1 && tls_port == -1) {
1153 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1154 return -1;
1155 }
1156
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001157 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1158 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001159 if (ret != 0) {
1160 qerror_report(QERR_UNDEFINED_ERROR);
1161 return -1;
1162 }
1163 return 0;
1164 }
1165
1166 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1167 return -1;
1168}
1169
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001170static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001171{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001172 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001173}
1174
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001175static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001176{
1177 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001178 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001179
bellard9307c4c2004-04-04 12:57:25 +00001180 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001181 mask = 0;
1182 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001183 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001184 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001185 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001186 return;
1187 }
1188 }
Peter Maydell24537a02013-02-11 16:41:23 +00001189 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001190}
1191
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001192static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001193{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001194 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001195 if (!option || !strcmp(option, "on")) {
1196 singlestep = 1;
1197 } else if (!strcmp(option, "off")) {
1198 singlestep = 0;
1199 } else {
1200 monitor_printf(mon, "unexpected option %s\n", option);
1201 }
1202}
1203
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001204static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001205{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001206 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001207 if (!device)
1208 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1209 if (gdbserver_start(device) < 0) {
1210 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1211 device);
1212 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001213 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001214 } else {
aliguori59030a82009-04-05 18:43:41 +00001215 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1216 device);
bellard8a7ddc32004-03-31 19:00:16 +00001217 }
1218}
1219
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001220static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001221{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001222 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001223 if (select_watchdog_action(action) == -1) {
1224 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1225 }
1226}
1227
aliguori376253e2009-03-05 23:01:23 +00001228static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001229{
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001231 switch(c) {
1232 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001234 break;
1235 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001240 break;
1241 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001243 break;
1244 default:
1245 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001247 } else {
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001249 }
1250 break;
1251 }
aliguori376253e2009-03-05 23:01:23 +00001252 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001253}
1254
aliguori376253e2009-03-05 23:01:23 +00001255static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001256 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001257{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001258 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001259 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001260 uint8_t buf[16];
1261 uint64_t v;
1262
1263 if (format == 'i') {
1264 int flags;
1265 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001266 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001267#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001268 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001269 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001270 } else if (wsize == 4) {
1271 flags = 0;
1272 } else {
bellard6a15fd12006-04-12 21:07:07 +00001273 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001274 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001275 if (env) {
1276#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001277 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001278 (env->segs[R_CS].flags & DESC_L_MASK))
1279 flags = 2;
1280 else
1281#endif
1282 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1283 flags = 1;
1284 }
bellard4c27ba22004-04-25 18:05:08 +00001285 }
1286#endif
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001288 return;
1289 }
1290
1291 len = wsize * count;
1292 if (wsize == 1)
1293 line_size = 8;
1294 else
1295 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001296 max_digits = 0;
1297
1298 switch(format) {
1299 case 'o':
1300 max_digits = (wsize * 8 + 2) / 3;
1301 break;
1302 default:
1303 case 'x':
1304 max_digits = (wsize * 8) / 4;
1305 break;
1306 case 'u':
1307 case 'd':
1308 max_digits = (wsize * 8 * 10 + 32) / 33;
1309 break;
1310 case 'c':
1311 wsize = 1;
1312 break;
1313 }
1314
1315 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001316 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001318 else
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001320 l = len;
1321 if (l > line_size)
1322 l = line_size;
1323 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001324 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001325 } else {
bellard6a00d602005-11-21 23:25:50 +00001326 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001327 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001329 break;
1330 }
bellard9307c4c2004-04-04 12:57:25 +00001331 }
ths5fafdf22007-09-16 21:08:06 +00001332 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001333 while (i < l) {
1334 switch(wsize) {
1335 default:
1336 case 1:
1337 v = ldub_raw(buf + i);
1338 break;
1339 case 2:
1340 v = lduw_raw(buf + i);
1341 break;
1342 case 4:
bellard92a31b12005-02-10 22:00:52 +00001343 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001344 break;
1345 case 8:
1346 v = ldq_raw(buf + i);
1347 break;
1348 }
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001350 switch(format) {
1351 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001353 break;
1354 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001356 break;
1357 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001359 break;
1360 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001361 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001362 break;
1363 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001365 break;
1366 }
1367 i += wsize;
1368 }
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001370 addr += l;
1371 len -= l;
1372 }
1373}
1374
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001375static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001376{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001377 int count = qdict_get_int(qdict, "count");
1378 int format = qdict_get_int(qdict, "format");
1379 int size = qdict_get_int(qdict, "size");
1380 target_long addr = qdict_get_int(qdict, "addr");
1381
aliguori376253e2009-03-05 23:01:23 +00001382 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001383}
1384
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001385static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001386{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001387 int count = qdict_get_int(qdict, "count");
1388 int format = qdict_get_int(qdict, "format");
1389 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001390 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001391
aliguori376253e2009-03-05 23:01:23 +00001392 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001393}
1394
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001395static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001396{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001397 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001398 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001399
bellard9307c4c2004-04-04 12:57:25 +00001400 switch(format) {
1401 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001402 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001403 break;
1404 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001405 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001406 break;
1407 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001408 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001409 break;
1410 default:
1411 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001412 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001413 break;
1414 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001416 break;
1417 }
aliguori376253e2009-03-05 23:01:23 +00001418 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001419}
1420
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001421static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001422{
1423 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001424 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001425 uint32_t start = qdict_get_int(qdict, "start");
1426 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001427
1428 sum = 0;
1429 for(addr = start; addr < (start + size); addr++) {
Edgar E. Iglesias2c174492013-12-17 14:05:40 +10001430 uint8_t val = ldub_phys(&address_space_memory, addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001431 /* BSD sum algorithm ('sum' Unix command) */
1432 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001433 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001434 }
aliguori376253e2009-03-05 23:01:23 +00001435 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001436}
1437
bellard13224a82006-07-14 22:03:35 +00001438static int mouse_button_state;
1439
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001440static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001441{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001442 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001443 const char *dx_str = qdict_get_str(qdict, "dx_str");
1444 const char *dy_str = qdict_get_str(qdict, "dy_str");
1445 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001446
bellard13224a82006-07-14 22:03:35 +00001447 dx = strtol(dx_str, NULL, 0);
1448 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001449 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1450 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1451
1452 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001453 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001454 if (dz != 0) {
1455 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1456 qemu_input_queue_btn(NULL, button, true);
1457 qemu_input_event_sync();
1458 qemu_input_queue_btn(NULL, button, false);
1459 }
1460 }
1461 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001462}
1463
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001464static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001465{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001466 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1467 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1468 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1469 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1470 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001471 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001472
1473 if (mouse_button_state == button_state) {
1474 return;
1475 }
1476 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1477 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001478 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001479}
1480
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001481static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001482{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001483 int size = qdict_get_int(qdict, "size");
1484 int addr = qdict_get_int(qdict, "addr");
1485 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001486 uint32_t val;
1487 int suffix;
1488
1489 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001490 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001491 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001492 addr++;
1493 }
1494 addr &= 0xffff;
1495
1496 switch(size) {
1497 default:
1498 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001499 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001500 suffix = 'b';
1501 break;
1502 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001503 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001504 suffix = 'w';
1505 break;
1506 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001507 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001508 suffix = 'l';
1509 break;
1510 }
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1512 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001513}
bellarda3a91a32004-06-04 11:06:21 +00001514
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001515static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001516{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001517 int size = qdict_get_int(qdict, "size");
1518 int addr = qdict_get_int(qdict, "addr");
1519 int val = qdict_get_int(qdict, "val");
1520
Jan Kiszkaf1147842009-07-14 10:20:11 +02001521 addr &= IOPORTS_MASK;
1522
1523 switch (size) {
1524 default:
1525 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001526 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001527 break;
1528 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001529 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001530 break;
1531 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001532 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001533 break;
1534 }
1535}
1536
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001537static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001538{
1539 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001540 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001541
Jan Kiszka76e30d02009-07-02 00:19:02 +02001542 res = qemu_boot_set(bootdevice);
1543 if (res == 0) {
1544 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1545 } else if (res > 0) {
1546 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001547 } else {
aliguori376253e2009-03-05 23:01:23 +00001548 monitor_printf(mon, "no function defined to set boot device list for "
1549 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001550 }
1551}
1552
bellardb86bda52004-09-18 19:32:46 +00001553#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001554static void print_pte(Monitor *mon, hwaddr addr,
1555 hwaddr pte,
1556 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001557{
Blue Swirld65aaf32010-12-11 18:56:24 +00001558#ifdef TARGET_X86_64
1559 if (addr & (1ULL << 47)) {
1560 addr |= -1LL << 48;
1561 }
1562#endif
1563 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1564 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001565 addr,
1566 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001567 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001568 pte & PG_GLOBAL_MASK ? 'G' : '-',
1569 pte & PG_PSE_MASK ? 'P' : '-',
1570 pte & PG_DIRTY_MASK ? 'D' : '-',
1571 pte & PG_ACCESSED_MASK ? 'A' : '-',
1572 pte & PG_PCD_MASK ? 'C' : '-',
1573 pte & PG_PWT_MASK ? 'T' : '-',
1574 pte & PG_USER_MASK ? 'U' : '-',
1575 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001576}
1577
Andreas Färber9349b4f2012-03-14 01:38:32 +01001578static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001579{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001580 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001581 uint32_t pgd, pde, pte;
1582
bellardb86bda52004-09-18 19:32:46 +00001583 pgd = env->cr[3] & ~0xfff;
1584 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001585 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001586 pde = le32_to_cpu(pde);
1587 if (pde & PG_PRESENT_MASK) {
1588 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001589 /* 4M pages */
1590 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001591 } else {
1592 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001593 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001594 pte = le32_to_cpu(pte);
1595 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001596 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001597 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001598 ~0xfff);
1599 }
1600 }
1601 }
1602 }
1603 }
1604}
1605
Andreas Färber9349b4f2012-03-14 01:38:32 +01001606static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001607{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001608 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001609 uint64_t pdpe, pde, pte;
1610 uint64_t pdp_addr, pd_addr, pt_addr;
1611
1612 pdp_addr = env->cr[3] & ~0x1f;
1613 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001614 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001615 pdpe = le64_to_cpu(pdpe);
1616 if (pdpe & PG_PRESENT_MASK) {
1617 pd_addr = pdpe & 0x3fffffffff000ULL;
1618 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001619 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001620 pde = le64_to_cpu(pde);
1621 if (pde & PG_PRESENT_MASK) {
1622 if (pde & PG_PSE_MASK) {
1623 /* 2M pages with PAE, CR4.PSE is ignored */
1624 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001625 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001626 } else {
1627 pt_addr = pde & 0x3fffffffff000ULL;
1628 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001629 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001630 pte = le64_to_cpu(pte);
1631 if (pte & PG_PRESENT_MASK) {
1632 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1633 + (l3 << 12),
1634 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001635 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001636 }
1637 }
1638 }
1639 }
1640 }
1641 }
1642 }
1643}
1644
1645#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001646static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001647{
1648 uint64_t l1, l2, l3, l4;
1649 uint64_t pml4e, pdpe, pde, pte;
1650 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1651
1652 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1653 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001654 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001655 pml4e = le64_to_cpu(pml4e);
1656 if (pml4e & PG_PRESENT_MASK) {
1657 pdp_addr = pml4e & 0x3fffffffff000ULL;
1658 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001659 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001660 pdpe = le64_to_cpu(pdpe);
1661 if (pdpe & PG_PRESENT_MASK) {
1662 if (pdpe & PG_PSE_MASK) {
1663 /* 1G pages, CR4.PSE is ignored */
1664 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1665 0x3ffffc0000000ULL);
1666 } else {
1667 pd_addr = pdpe & 0x3fffffffff000ULL;
1668 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001669 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001670 pde = le64_to_cpu(pde);
1671 if (pde & PG_PRESENT_MASK) {
1672 if (pde & PG_PSE_MASK) {
1673 /* 2M pages, CR4.PSE is ignored */
1674 print_pte(mon, (l1 << 39) + (l2 << 30) +
1675 (l3 << 21), pde,
1676 0x3ffffffe00000ULL);
1677 } else {
1678 pt_addr = pde & 0x3fffffffff000ULL;
1679 for (l4 = 0; l4 < 512; l4++) {
1680 cpu_physical_memory_read(pt_addr
1681 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001682 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001683 pte = le64_to_cpu(pte);
1684 if (pte & PG_PRESENT_MASK) {
1685 print_pte(mon, (l1 << 39) +
1686 (l2 << 30) +
1687 (l3 << 21) + (l4 << 12),
1688 pte & ~PG_PSE_MASK,
1689 0x3fffffffff000ULL);
1690 }
1691 }
1692 }
1693 }
1694 }
1695 }
1696 }
1697 }
1698 }
1699 }
1700}
1701#endif
1702
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001703static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001704{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001705 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001706
1707 env = mon_get_cpu();
1708
1709 if (!(env->cr[0] & CR0_PG_MASK)) {
1710 monitor_printf(mon, "PG disabled\n");
1711 return;
1712 }
1713 if (env->cr[4] & CR4_PAE_MASK) {
1714#ifdef TARGET_X86_64
1715 if (env->hflags & HF_LMA_MASK) {
1716 tlb_info_64(mon, env);
1717 } else
1718#endif
1719 {
1720 tlb_info_pae32(mon, env);
1721 }
1722 } else {
1723 tlb_info_32(mon, env);
1724 }
1725}
1726
Avi Kivitya8170e52012-10-23 12:30:10 +02001727static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001728 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001729 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001730{
bellard9746b152004-11-11 18:30:24 +00001731 int prot1;
1732 prot1 = *plast_prot;
1733 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001734 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001735 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1736 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001737 *pstart, end, end - *pstart,
1738 prot1 & PG_USER_MASK ? 'u' : '-',
1739 'r',
1740 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001741 }
1742 if (prot != 0)
1743 *pstart = end;
1744 else
1745 *pstart = -1;
1746 *plast_prot = prot;
1747 }
1748}
1749
Andreas Färber9349b4f2012-03-14 01:38:32 +01001750static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001751{
Austin Clementsb49ca722011-08-14 23:19:21 -04001752 unsigned int l1, l2;
1753 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001754 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001755 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001756
bellardb86bda52004-09-18 19:32:46 +00001757 pgd = env->cr[3] & ~0xfff;
1758 last_prot = 0;
1759 start = -1;
1760 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001761 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001762 pde = le32_to_cpu(pde);
1763 end = l1 << 22;
1764 if (pde & PG_PRESENT_MASK) {
1765 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1766 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001767 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001768 } else {
1769 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001770 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001771 pte = le32_to_cpu(pte);
1772 end = (l1 << 22) + (l2 << 12);
1773 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001774 prot = pte & pde &
1775 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001776 } else {
1777 prot = 0;
1778 }
aliguori376253e2009-03-05 23:01:23 +00001779 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001780 }
1781 }
1782 } else {
1783 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001784 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001785 }
1786 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001787 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001788 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001789}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001790
Andreas Färber9349b4f2012-03-14 01:38:32 +01001791static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001792{
Austin Clementsb49ca722011-08-14 23:19:21 -04001793 unsigned int l1, l2, l3;
1794 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001795 uint64_t pdpe, pde, pte;
1796 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001797 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001798
1799 pdp_addr = env->cr[3] & ~0x1f;
1800 last_prot = 0;
1801 start = -1;
1802 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001803 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001804 pdpe = le64_to_cpu(pdpe);
1805 end = l1 << 30;
1806 if (pdpe & PG_PRESENT_MASK) {
1807 pd_addr = pdpe & 0x3fffffffff000ULL;
1808 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001809 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001810 pde = le64_to_cpu(pde);
1811 end = (l1 << 30) + (l2 << 21);
1812 if (pde & PG_PRESENT_MASK) {
1813 if (pde & PG_PSE_MASK) {
1814 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1815 PG_PRESENT_MASK);
1816 mem_print(mon, &start, &last_prot, end, prot);
1817 } else {
1818 pt_addr = pde & 0x3fffffffff000ULL;
1819 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001820 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001821 pte = le64_to_cpu(pte);
1822 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1823 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001824 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1825 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001826 } else {
1827 prot = 0;
1828 }
1829 mem_print(mon, &start, &last_prot, end, prot);
1830 }
1831 }
1832 } else {
1833 prot = 0;
1834 mem_print(mon, &start, &last_prot, end, prot);
1835 }
1836 }
1837 } else {
1838 prot = 0;
1839 mem_print(mon, &start, &last_prot, end, prot);
1840 }
1841 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001842 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001843 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001844}
1845
1846
1847#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001848static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001849{
1850 int prot, last_prot;
1851 uint64_t l1, l2, l3, l4;
1852 uint64_t pml4e, pdpe, pde, pte;
1853 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1854
1855 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1856 last_prot = 0;
1857 start = -1;
1858 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001859 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001860 pml4e = le64_to_cpu(pml4e);
1861 end = l1 << 39;
1862 if (pml4e & PG_PRESENT_MASK) {
1863 pdp_addr = pml4e & 0x3fffffffff000ULL;
1864 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001865 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001866 pdpe = le64_to_cpu(pdpe);
1867 end = (l1 << 39) + (l2 << 30);
1868 if (pdpe & PG_PRESENT_MASK) {
1869 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001870 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1871 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001872 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001873 mem_print(mon, &start, &last_prot, end, prot);
1874 } else {
1875 pd_addr = pdpe & 0x3fffffffff000ULL;
1876 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001877 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001878 pde = le64_to_cpu(pde);
1879 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1880 if (pde & PG_PRESENT_MASK) {
1881 if (pde & PG_PSE_MASK) {
1882 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1883 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001884 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001885 mem_print(mon, &start, &last_prot, end, prot);
1886 } else {
1887 pt_addr = pde & 0x3fffffffff000ULL;
1888 for (l4 = 0; l4 < 512; l4++) {
1889 cpu_physical_memory_read(pt_addr
1890 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001891 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001892 pte = le64_to_cpu(pte);
1893 end = (l1 << 39) + (l2 << 30) +
1894 (l3 << 21) + (l4 << 12);
1895 if (pte & PG_PRESENT_MASK) {
1896 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1897 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001898 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001899 } else {
1900 prot = 0;
1901 }
1902 mem_print(mon, &start, &last_prot, end, prot);
1903 }
1904 }
1905 } else {
1906 prot = 0;
1907 mem_print(mon, &start, &last_prot, end, prot);
1908 }
1909 }
1910 }
1911 } else {
1912 prot = 0;
1913 mem_print(mon, &start, &last_prot, end, prot);
1914 }
1915 }
1916 } else {
1917 prot = 0;
1918 mem_print(mon, &start, &last_prot, end, prot);
1919 }
1920 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001921 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001922 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001923}
1924#endif
1925
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001926static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001927{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001928 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001929
1930 env = mon_get_cpu();
1931
1932 if (!(env->cr[0] & CR0_PG_MASK)) {
1933 monitor_printf(mon, "PG disabled\n");
1934 return;
1935 }
1936 if (env->cr[4] & CR4_PAE_MASK) {
1937#ifdef TARGET_X86_64
1938 if (env->hflags & HF_LMA_MASK) {
1939 mem_info_64(mon, env);
1940 } else
1941#endif
1942 {
1943 mem_info_pae32(mon, env);
1944 }
1945 } else {
1946 mem_info_32(mon, env);
1947 }
1948}
bellardb86bda52004-09-18 19:32:46 +00001949#endif
1950
aurel327c664e22009-03-03 06:12:22 +00001951#if defined(TARGET_SH4)
1952
aliguori376253e2009-03-05 23:01:23 +00001953static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001954{
aliguori376253e2009-03-05 23:01:23 +00001955 monitor_printf(mon, " tlb%i:\t"
1956 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1957 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1958 "dirty=%hhu writethrough=%hhu\n",
1959 idx,
1960 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1961 tlb->v, tlb->sh, tlb->c, tlb->pr,
1962 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001963}
1964
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001965static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001966{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001967 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001968 int i;
1969
aliguori376253e2009-03-05 23:01:23 +00001970 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001971 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001972 print_tlb (mon, i, &env->itlb[i]);
1973 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001974 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001975 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001976}
1977
1978#endif
1979
Max Filippov692f7372012-01-07 20:02:40 +04001980#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001981static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001982{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001983 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001984
1985 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1986}
1987#endif
1988
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001989static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001990{
1991 mtree_info((fprintf_function)monitor_printf, mon);
1992}
1993
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001994static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001995{
aliguorib28b6232009-04-22 20:20:29 +00001996 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001997 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00001998
1999 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2000 for (i = 0; i < nb_numa_nodes; i++) {
2001 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02002002 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002003 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01002004 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00002005 }
2006 }
2007 monitor_printf(mon, "\n");
2008 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2009 node_mem[i] >> 20);
2010 }
2011}
2012
bellard5f1ce942006-02-08 22:40:15 +00002013#ifdef CONFIG_PROFILER
2014
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002015int64_t qemu_time;
2016int64_t dev_time;
2017
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002018static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002019{
aliguori376253e2009-03-05 23:01:23 +00002020 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002021 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002022 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002023 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002024 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002025 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002026}
2027#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002028static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002029{
aliguori376253e2009-03-05 23:01:23 +00002030 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002031}
2032#endif
2033
bellardec36b692006-07-16 18:57:03 +00002034/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002035static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002036
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002037static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002038{
2039 int i;
2040 CaptureState *s;
2041
2042 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002043 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002044 s->ops.info (s->opaque);
2045 }
2046}
2047
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002048static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002049{
2050 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002051 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002052 CaptureState *s;
2053
2054 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2055 if (i == n) {
2056 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002057 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002058 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002059 return;
2060 }
2061 }
2062}
2063
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002064static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002065{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002066 const char *path = qdict_get_str(qdict, "path");
2067 int has_freq = qdict_haskey(qdict, "freq");
2068 int freq = qdict_get_try_int(qdict, "freq", -1);
2069 int has_bits = qdict_haskey(qdict, "bits");
2070 int bits = qdict_get_try_int(qdict, "bits", -1);
2071 int has_channels = qdict_haskey(qdict, "nchannels");
2072 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002073 CaptureState *s;
2074
Anthony Liguori7267c092011-08-20 22:09:37 -05002075 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002076
2077 freq = has_freq ? freq : 44100;
2078 bits = has_bits ? bits : 16;
2079 nchannels = has_channels ? nchannels : 2;
2080
2081 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002082 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002083 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002084 return;
bellardec36b692006-07-16 18:57:03 +00002085 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002086 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002087}
bellardec36b692006-07-16 18:57:03 +00002088
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002089static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002090{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002091 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002092
aliguori76655d62009-03-06 20:27:37 +00002093 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002094 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002095 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002096 return acl;
2097}
aliguori76655d62009-03-06 20:27:37 +00002098
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002099static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002100{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002101 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002102 qemu_acl *acl = find_acl(mon, aclname);
2103 qemu_acl_entry *entry;
2104 int i = 0;
2105
2106 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002107 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002108 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002109 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002110 i++;
2111 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002112 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002113 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002114 }
2115}
2116
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002117static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002118{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002119 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002120 qemu_acl *acl = find_acl(mon, aclname);
2121
2122 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002123 qemu_acl_reset(acl);
2124 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002125 }
2126}
aliguori76655d62009-03-06 20:27:37 +00002127
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002128static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002129{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002130 const char *aclname = qdict_get_str(qdict, "aclname");
2131 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002132 qemu_acl *acl = find_acl(mon, aclname);
2133
2134 if (acl) {
2135 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002136 acl->defaultDeny = 0;
2137 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002138 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002139 acl->defaultDeny = 1;
2140 monitor_printf(mon, "acl: policy set to 'deny'\n");
2141 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002142 monitor_printf(mon, "acl: unknown policy '%s', "
2143 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002144 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002145 }
2146}
aliguori76655d62009-03-06 20:27:37 +00002147
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002148static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002149{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002150 const char *aclname = qdict_get_str(qdict, "aclname");
2151 const char *match = qdict_get_str(qdict, "match");
2152 const char *policy = qdict_get_str(qdict, "policy");
2153 int has_index = qdict_haskey(qdict, "index");
2154 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002155 qemu_acl *acl = find_acl(mon, aclname);
2156 int deny, ret;
2157
2158 if (acl) {
2159 if (strcmp(policy, "allow") == 0) {
2160 deny = 0;
2161 } else if (strcmp(policy, "deny") == 0) {
2162 deny = 1;
2163 } else {
2164 monitor_printf(mon, "acl: unknown policy '%s', "
2165 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002166 return;
2167 }
aliguori28a76be2009-03-06 20:27:40 +00002168 if (has_index)
2169 ret = qemu_acl_insert(acl, deny, match, index);
2170 else
2171 ret = qemu_acl_append(acl, deny, match);
2172 if (ret < 0)
2173 monitor_printf(mon, "acl: unable to add acl entry\n");
2174 else
2175 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002176 }
2177}
aliguori76655d62009-03-06 20:27:37 +00002178
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002179static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002181 const char *aclname = qdict_get_str(qdict, "aclname");
2182 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183 qemu_acl *acl = find_acl(mon, aclname);
2184 int ret;
aliguori76655d62009-03-06 20:27:37 +00002185
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002186 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002187 ret = qemu_acl_remove(acl, match);
2188 if (ret < 0)
2189 monitor_printf(mon, "acl: no matching acl entry\n");
2190 else
2191 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002192 }
2193}
2194
Huang Ying79c4f6b2009-06-23 10:05:14 +08002195#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002196static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002197{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002198 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002199 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002200 int cpu_index = qdict_get_int(qdict, "cpu_index");
2201 int bank = qdict_get_int(qdict, "bank");
2202 uint64_t status = qdict_get_int(qdict, "status");
2203 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2204 uint64_t addr = qdict_get_int(qdict, "addr");
2205 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002206 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002207
Jan Kiszka747461c2011-03-02 08:56:10 +01002208 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2209 flags |= MCE_INJECT_BROADCAST;
2210 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002211 cs = qemu_get_cpu(cpu_index);
2212 if (cs != NULL) {
2213 cpu = X86_CPU(cs);
2214 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2215 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002216 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002217}
2218#endif
2219
Corey Bryant208c9d12012-06-22 14:36:09 -04002220void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002221{
Anthony Liguoric227f092009-10-01 16:12:16 -05002222 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002223 int fd;
2224
Corey Bryant208c9d12012-06-22 14:36:09 -04002225 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002226 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002227 error_set(errp, QERR_FD_NOT_SUPPLIED);
2228 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229 }
2230
2231 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002232 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002233 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2234 "a name not starting with a digit");
2235 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002236 }
2237
Corey Bryant208c9d12012-06-22 14:36:09 -04002238 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002239 if (strcmp(monfd->name, fdname) != 0) {
2240 continue;
2241 }
2242
2243 close(monfd->fd);
2244 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002245 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002246 }
2247
Anthony Liguori7267c092011-08-20 22:09:37 -05002248 monfd = g_malloc0(sizeof(mon_fd_t));
2249 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250 monfd->fd = fd;
2251
Corey Bryant208c9d12012-06-22 14:36:09 -04002252 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253}
2254
Corey Bryant208c9d12012-06-22 14:36:09 -04002255void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002256{
Anthony Liguoric227f092009-10-01 16:12:16 -05002257 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002258
Corey Bryant208c9d12012-06-22 14:36:09 -04002259 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002260 if (strcmp(monfd->name, fdname) != 0) {
2261 continue;
2262 }
2263
Blue Swirl72cf2d42009-09-12 07:36:22 +00002264 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002265 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002266 g_free(monfd->name);
2267 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002268 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002269 }
2270
Corey Bryant208c9d12012-06-22 14:36:09 -04002271 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002272}
2273
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002274static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002275{
Luiz Capitulino13548692011-07-29 15:36:43 -03002276 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002277 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002278
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002279 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002280
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002281 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002282 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002283 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002284}
2285
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002286int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002287{
Anthony Liguoric227f092009-10-01 16:12:16 -05002288 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002289
Blue Swirl72cf2d42009-09-12 07:36:22 +00002290 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002291 int fd;
2292
2293 if (strcmp(monfd->name, fdname) != 0) {
2294 continue;
2295 }
2296
2297 fd = monfd->fd;
2298
2299 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002300 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002301 g_free(monfd->name);
2302 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002303
2304 return fd;
2305 }
2306
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002307 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002308 return -1;
2309}
2310
Corey Bryantba1c0482012-08-14 16:43:43 -04002311static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2312{
2313 MonFdsetFd *mon_fdset_fd;
2314 MonFdsetFd *mon_fdset_fd_next;
2315
2316 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002317 if ((mon_fdset_fd->removed ||
2318 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2319 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002320 close(mon_fdset_fd->fd);
2321 g_free(mon_fdset_fd->opaque);
2322 QLIST_REMOVE(mon_fdset_fd, next);
2323 g_free(mon_fdset_fd);
2324 }
2325 }
2326
Corey Bryantadb696f2012-08-14 16:43:47 -04002327 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002328 QLIST_REMOVE(mon_fdset, next);
2329 g_free(mon_fdset);
2330 }
2331}
2332
Corey Bryantefb87c12012-08-14 16:43:48 -04002333static void monitor_fdsets_cleanup(void)
2334{
2335 MonFdset *mon_fdset;
2336 MonFdset *mon_fdset_next;
2337
2338 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2339 monitor_fdset_cleanup(mon_fdset);
2340 }
2341}
2342
Corey Bryantba1c0482012-08-14 16:43:43 -04002343AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2344 const char *opaque, Error **errp)
2345{
2346 int fd;
2347 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002348 AddfdInfo *fdinfo;
2349
2350 fd = qemu_chr_fe_get_msgfd(mon->chr);
2351 if (fd == -1) {
2352 error_set(errp, QERR_FD_NOT_SUPPLIED);
2353 goto error;
2354 }
2355
Corey Bryante446f702012-10-18 15:19:32 -04002356 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2357 has_opaque, opaque, errp);
2358 if (fdinfo) {
2359 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002360 }
2361
Corey Bryantba1c0482012-08-14 16:43:43 -04002362error:
2363 if (fd != -1) {
2364 close(fd);
2365 }
2366 return NULL;
2367}
2368
2369void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2370{
2371 MonFdset *mon_fdset;
2372 MonFdsetFd *mon_fdset_fd;
2373 char fd_str[60];
2374
2375 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2376 if (mon_fdset->id != fdset_id) {
2377 continue;
2378 }
2379 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2380 if (has_fd) {
2381 if (mon_fdset_fd->fd != fd) {
2382 continue;
2383 }
2384 mon_fdset_fd->removed = true;
2385 break;
2386 } else {
2387 mon_fdset_fd->removed = true;
2388 }
2389 }
2390 if (has_fd && !mon_fdset_fd) {
2391 goto error;
2392 }
2393 monitor_fdset_cleanup(mon_fdset);
2394 return;
2395 }
2396
2397error:
2398 if (has_fd) {
2399 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2400 fdset_id, fd);
2401 } else {
2402 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2403 }
2404 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2405}
2406
2407FdsetInfoList *qmp_query_fdsets(Error **errp)
2408{
2409 MonFdset *mon_fdset;
2410 MonFdsetFd *mon_fdset_fd;
2411 FdsetInfoList *fdset_list = NULL;
2412
2413 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2414 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2415 FdsetFdInfoList *fdsetfd_list = NULL;
2416
2417 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2418 fdset_info->value->fdset_id = mon_fdset->id;
2419
2420 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2421 FdsetFdInfoList *fdsetfd_info;
2422
2423 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2424 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2425 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2426 if (mon_fdset_fd->opaque) {
2427 fdsetfd_info->value->has_opaque = true;
2428 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2429 } else {
2430 fdsetfd_info->value->has_opaque = false;
2431 }
2432
2433 fdsetfd_info->next = fdsetfd_list;
2434 fdsetfd_list = fdsetfd_info;
2435 }
2436
2437 fdset_info->value->fds = fdsetfd_list;
2438
2439 fdset_info->next = fdset_list;
2440 fdset_list = fdset_info;
2441 }
2442
2443 return fdset_list;
2444}
2445
Corey Bryante446f702012-10-18 15:19:32 -04002446AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2447 bool has_opaque, const char *opaque,
2448 Error **errp)
2449{
2450 MonFdset *mon_fdset = NULL;
2451 MonFdsetFd *mon_fdset_fd;
2452 AddfdInfo *fdinfo;
2453
2454 if (has_fdset_id) {
2455 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2456 /* Break if match found or match impossible due to ordering by ID */
2457 if (fdset_id <= mon_fdset->id) {
2458 if (fdset_id < mon_fdset->id) {
2459 mon_fdset = NULL;
2460 }
2461 break;
2462 }
2463 }
2464 }
2465
2466 if (mon_fdset == NULL) {
2467 int64_t fdset_id_prev = -1;
2468 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2469
2470 if (has_fdset_id) {
2471 if (fdset_id < 0) {
2472 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2473 "a non-negative value");
2474 return NULL;
2475 }
2476 /* Use specified fdset ID */
2477 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2478 mon_fdset_cur = mon_fdset;
2479 if (fdset_id < mon_fdset_cur->id) {
2480 break;
2481 }
2482 }
2483 } else {
2484 /* Use first available fdset ID */
2485 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2486 mon_fdset_cur = mon_fdset;
2487 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2488 fdset_id_prev = mon_fdset_cur->id;
2489 continue;
2490 }
2491 break;
2492 }
2493 }
2494
2495 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2496 if (has_fdset_id) {
2497 mon_fdset->id = fdset_id;
2498 } else {
2499 mon_fdset->id = fdset_id_prev + 1;
2500 }
2501
2502 /* The fdset list is ordered by fdset ID */
2503 if (!mon_fdset_cur) {
2504 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2505 } else if (mon_fdset->id < mon_fdset_cur->id) {
2506 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2507 } else {
2508 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2509 }
2510 }
2511
2512 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2513 mon_fdset_fd->fd = fd;
2514 mon_fdset_fd->removed = false;
2515 if (has_opaque) {
2516 mon_fdset_fd->opaque = g_strdup(opaque);
2517 }
2518 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2519
2520 fdinfo = g_malloc0(sizeof(*fdinfo));
2521 fdinfo->fdset_id = mon_fdset->id;
2522 fdinfo->fd = mon_fdset_fd->fd;
2523
2524 return fdinfo;
2525}
2526
Corey Bryantadb696f2012-08-14 16:43:47 -04002527int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2528{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002529#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002530 MonFdset *mon_fdset;
2531 MonFdsetFd *mon_fdset_fd;
2532 int mon_fd_flags;
2533
Corey Bryantadb696f2012-08-14 16:43:47 -04002534 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2535 if (mon_fdset->id != fdset_id) {
2536 continue;
2537 }
2538 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2539 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2540 if (mon_fd_flags == -1) {
2541 return -1;
2542 }
2543
2544 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2545 return mon_fdset_fd->fd;
2546 }
2547 }
2548 errno = EACCES;
2549 return -1;
2550 }
2551#endif
2552
2553 errno = ENOENT;
2554 return -1;
2555}
2556
2557int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2558{
2559 MonFdset *mon_fdset;
2560 MonFdsetFd *mon_fdset_fd_dup;
2561
2562 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2563 if (mon_fdset->id != fdset_id) {
2564 continue;
2565 }
2566 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2567 if (mon_fdset_fd_dup->fd == dup_fd) {
2568 return -1;
2569 }
2570 }
2571 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2572 mon_fdset_fd_dup->fd = dup_fd;
2573 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2574 return 0;
2575 }
2576 return -1;
2577}
2578
2579static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2580{
2581 MonFdset *mon_fdset;
2582 MonFdsetFd *mon_fdset_fd_dup;
2583
2584 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2585 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2586 if (mon_fdset_fd_dup->fd == dup_fd) {
2587 if (remove) {
2588 QLIST_REMOVE(mon_fdset_fd_dup, next);
2589 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2590 monitor_fdset_cleanup(mon_fdset);
2591 }
2592 }
2593 return mon_fdset->id;
2594 }
2595 }
2596 }
2597 return -1;
2598}
2599
2600int monitor_fdset_dup_fd_find(int dup_fd)
2601{
2602 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2603}
2604
2605int monitor_fdset_dup_fd_remove(int dup_fd)
2606{
2607 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2608}
2609
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002610int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2611{
2612 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002613 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002614
Laszlo Ersek59063662014-04-10 10:24:31 +02002615 fd = monitor_handle_fd_param2(mon, fdname, &local_err);
2616 if (local_err) {
2617 qerror_report_err(local_err);
2618 error_free(local_err);
2619 }
2620 return fd;
2621}
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002622
Laszlo Ersek59063662014-04-10 10:24:31 +02002623int monitor_handle_fd_param2(Monitor *mon, const char *fdname, Error **errp)
2624{
2625 int fd;
2626 Error *local_err = NULL;
2627
2628 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002629 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002630 } else {
2631 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002632 if (fd == -1) {
2633 error_setg(&local_err, "Invalid file descriptor number '%s'",
2634 fdname);
2635 }
2636 }
2637 if (local_err) {
2638 error_propagate(errp, local_err);
2639 assert(fd == -1);
2640 } else {
2641 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002642 }
2643
2644 return fd;
2645}
2646
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002647/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002648static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002649 {
2650 .name = "version",
2651 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 .params = "",
2653 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002654 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 },
2656 {
2657 .name = "network",
2658 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 .params = "",
2660 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002661 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 },
2663 {
2664 .name = "chardev",
2665 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .params = "",
2667 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002668 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "block",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002672 .args_type = "verbose:-v,device:B?",
2673 .params = "[-v] [device]",
2674 .help = "show info of one block device or all block devices "
2675 "(and details of images with -v option)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002676 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
2679 .name = "blockstats",
2680 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 .params = "",
2682 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002683 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
2685 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002686 .name = "block-jobs",
2687 .args_type = "",
2688 .params = "",
2689 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002690 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002691 },
2692 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 .name = "registers",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002697 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "cpus",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002704 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "history",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002711 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002713#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2714 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 {
2716 .name = "irq",
2717 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 .params = "",
2719 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002720#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002721 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002722#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002723 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002724#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002725 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002726#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 },
2728 {
2729 .name = "pic",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002733#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002734 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002735#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002736 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002737#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002738 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002739#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002741#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 {
2743 .name = "pci",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002747 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002749#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002750 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 {
2752 .name = "tlb",
2753 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 .params = "",
2755 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002756 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 },
aurel327c664e22009-03-03 06:12:22 +00002758#endif
2759#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 {
2761 .name = "mem",
2762 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 .params = "",
2764 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002765 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 },
bellardb86bda52004-09-18 19:32:46 +00002767#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 {
Blue Swirl314e2982011-09-11 20:22:05 +00002769 .name = "mtree",
2770 .args_type = "",
2771 .params = "",
2772 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002773 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002774 },
2775 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 .name = "jit",
2777 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 .params = "",
2779 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002780 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 },
2782 {
2783 .name = "kvm",
2784 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 .params = "",
2786 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002787 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 },
2789 {
2790 .name = "numa",
2791 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .params = "",
2793 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002794 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "usb",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002801 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 },
2803 {
2804 .name = "usbhost",
2805 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 .params = "",
2807 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002808 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 },
2810 {
2811 .name = "profile",
2812 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 .params = "",
2814 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002815 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 },
2817 {
2818 .name = "capture",
2819 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .params = "",
2821 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002822 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
2824 {
2825 .name = "snapshots",
2826 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .params = "",
2828 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002829 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 },
2831 {
2832 .name = "status",
2833 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 .params = "",
2835 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002836 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002837 },
2838 {
2839 .name = "pcmcia",
2840 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 .params = "",
2842 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002843 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002844 },
2845 {
2846 .name = "mice",
2847 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 .params = "",
2849 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002850 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002851 },
2852 {
2853 .name = "vnc",
2854 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002855 .params = "",
2856 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002857 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002858 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002859#if defined(CONFIG_SPICE)
2860 {
2861 .name = "spice",
2862 .args_type = "",
2863 .params = "",
2864 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002865 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002866 },
2867#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002868 {
2869 .name = "name",
2870 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002871 .params = "",
2872 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002873 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002874 },
2875 {
2876 .name = "uuid",
2877 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 .params = "",
2879 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002880 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002882 {
2883 .name = "cpustats",
2884 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 .params = "",
2886 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002887 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 },
blueswir131a60e22007-10-26 18:42:59 +00002889#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002890 {
2891 .name = "usernet",
2892 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002893 .params = "",
2894 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002895 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 },
blueswir131a60e22007-10-26 18:42:59 +00002897#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002898 {
2899 .name = "migrate",
2900 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002901 .params = "",
2902 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002903 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002904 },
2905 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002906 .name = "migrate_capabilities",
2907 .args_type = "",
2908 .params = "",
2909 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002910 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002911 },
2912 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002913 .name = "migrate_cache_size",
2914 .args_type = "",
2915 .params = "",
2916 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002917 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002918 },
2919 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002920 .name = "balloon",
2921 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 .params = "",
2923 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002924 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002925 },
2926 {
2927 .name = "qtree",
2928 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 .params = "",
2930 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002931 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002932 },
2933 {
2934 .name = "qdm",
2935 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002936 .params = "",
2937 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002938 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002939 },
2940 {
2941 .name = "roms",
2942 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002943 .params = "",
2944 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002945 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002946 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302947 {
2948 .name = "trace-events",
2949 .args_type = "",
2950 .params = "",
2951 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002952 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302953 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002954 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002955 .name = "tpm",
2956 .args_type = "",
2957 .params = "",
2958 .help = "show the TPM device",
2959 .mhandler.cmd = hmp_info_tpm,
2960 },
2961 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002962 .name = NULL,
2963 },
bellard9dc39cb2004-03-14 21:38:27 +00002964};
2965
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002966/* mon_cmds and info_cmds would be sorted at runtime */
2967static mon_cmd_t mon_cmds[] = {
2968#include "hmp-commands.h"
2969 { NULL, NULL, },
2970};
2971
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002972static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002973#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002974 { /* NULL */ },
2975};
2976
bellard9307c4c2004-04-04 12:57:25 +00002977/*******************************************************************/
2978
2979static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002980static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002981
bellard92a31b12005-02-10 22:00:52 +00002982#define MD_TLONG 0
2983#define MD_I32 1
2984
bellard9307c4c2004-04-04 12:57:25 +00002985typedef struct MonitorDef {
2986 const char *name;
2987 int offset;
blueswir18662d652008-10-02 18:32:44 +00002988 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002989 int type;
bellard9307c4c2004-04-04 12:57:25 +00002990} MonitorDef;
2991
bellard57206fd2004-04-25 18:54:52 +00002992#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002993static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002994{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002995 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002996 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002997}
2998#endif
2999
bellarda541f292004-04-12 20:39:29 +00003000#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003001static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003002{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003003 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003004 unsigned int u;
3005 int i;
3006
3007 u = 0;
3008 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003009 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003010
3011 return u;
3012}
3013
blueswir18662d652008-10-02 18:32:44 +00003014static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003015{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003016 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003017 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003018}
3019
blueswir18662d652008-10-02 18:32:44 +00003020static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003021{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003022 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003023 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003024}
bellard9fddaa02004-05-21 12:59:32 +00003025
blueswir18662d652008-10-02 18:32:44 +00003026static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003027{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003028 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003029 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003030}
3031
blueswir18662d652008-10-02 18:32:44 +00003032static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003033{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003034 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003035 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003036}
3037
blueswir18662d652008-10-02 18:32:44 +00003038static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003039{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003040 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003041 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003042}
bellarda541f292004-04-12 20:39:29 +00003043#endif
3044
bellarde95c8d52004-09-30 22:22:08 +00003045#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003046#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003047static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003048{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003049 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003050
3051 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003052}
bellard7b936c02005-10-30 17:05:13 +00003053#endif
bellarde95c8d52004-09-30 22:22:08 +00003054
blueswir18662d652008-10-02 18:32:44 +00003055static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003056{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003057 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003058 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003059}
3060#endif
3061
blueswir18662d652008-10-02 18:32:44 +00003062static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003063#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003064
3065#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01003066 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3067 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3068 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003069
Andreas Färbere59d1672012-02-16 00:40:47 +01003070 { "eax", offsetof(CPUX86State, regs[0]) },
3071 { "ecx", offsetof(CPUX86State, regs[1]) },
3072 { "edx", offsetof(CPUX86State, regs[2]) },
3073 { "ebx", offsetof(CPUX86State, regs[3]) },
3074 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3075 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3076 { "esi", offsetof(CPUX86State, regs[6]) },
3077 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003078#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003079 { "r8", offsetof(CPUX86State, regs[8]) },
3080 { "r9", offsetof(CPUX86State, regs[9]) },
3081 { "r10", offsetof(CPUX86State, regs[10]) },
3082 { "r11", offsetof(CPUX86State, regs[11]) },
3083 { "r12", offsetof(CPUX86State, regs[12]) },
3084 { "r13", offsetof(CPUX86State, regs[13]) },
3085 { "r14", offsetof(CPUX86State, regs[14]) },
3086 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003087#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003088 { "eflags", offsetof(CPUX86State, eflags) },
3089 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003090 SEG("cs", R_CS)
3091 SEG("ds", R_DS)
3092 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003093 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003094 SEG("fs", R_FS)
3095 SEG("gs", R_GS)
3096 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003097#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003098 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003099 { "r0", offsetof(CPUPPCState, gpr[0]) },
3100 { "r1", offsetof(CPUPPCState, gpr[1]) },
3101 { "r2", offsetof(CPUPPCState, gpr[2]) },
3102 { "r3", offsetof(CPUPPCState, gpr[3]) },
3103 { "r4", offsetof(CPUPPCState, gpr[4]) },
3104 { "r5", offsetof(CPUPPCState, gpr[5]) },
3105 { "r6", offsetof(CPUPPCState, gpr[6]) },
3106 { "r7", offsetof(CPUPPCState, gpr[7]) },
3107 { "r8", offsetof(CPUPPCState, gpr[8]) },
3108 { "r9", offsetof(CPUPPCState, gpr[9]) },
3109 { "r10", offsetof(CPUPPCState, gpr[10]) },
3110 { "r11", offsetof(CPUPPCState, gpr[11]) },
3111 { "r12", offsetof(CPUPPCState, gpr[12]) },
3112 { "r13", offsetof(CPUPPCState, gpr[13]) },
3113 { "r14", offsetof(CPUPPCState, gpr[14]) },
3114 { "r15", offsetof(CPUPPCState, gpr[15]) },
3115 { "r16", offsetof(CPUPPCState, gpr[16]) },
3116 { "r17", offsetof(CPUPPCState, gpr[17]) },
3117 { "r18", offsetof(CPUPPCState, gpr[18]) },
3118 { "r19", offsetof(CPUPPCState, gpr[19]) },
3119 { "r20", offsetof(CPUPPCState, gpr[20]) },
3120 { "r21", offsetof(CPUPPCState, gpr[21]) },
3121 { "r22", offsetof(CPUPPCState, gpr[22]) },
3122 { "r23", offsetof(CPUPPCState, gpr[23]) },
3123 { "r24", offsetof(CPUPPCState, gpr[24]) },
3124 { "r25", offsetof(CPUPPCState, gpr[25]) },
3125 { "r26", offsetof(CPUPPCState, gpr[26]) },
3126 { "r27", offsetof(CPUPPCState, gpr[27]) },
3127 { "r28", offsetof(CPUPPCState, gpr[28]) },
3128 { "r29", offsetof(CPUPPCState, gpr[29]) },
3129 { "r30", offsetof(CPUPPCState, gpr[30]) },
3130 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003131 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003132 { "f0", offsetof(CPUPPCState, fpr[0]) },
3133 { "f1", offsetof(CPUPPCState, fpr[1]) },
3134 { "f2", offsetof(CPUPPCState, fpr[2]) },
3135 { "f3", offsetof(CPUPPCState, fpr[3]) },
3136 { "f4", offsetof(CPUPPCState, fpr[4]) },
3137 { "f5", offsetof(CPUPPCState, fpr[5]) },
3138 { "f6", offsetof(CPUPPCState, fpr[6]) },
3139 { "f7", offsetof(CPUPPCState, fpr[7]) },
3140 { "f8", offsetof(CPUPPCState, fpr[8]) },
3141 { "f9", offsetof(CPUPPCState, fpr[9]) },
3142 { "f10", offsetof(CPUPPCState, fpr[10]) },
3143 { "f11", offsetof(CPUPPCState, fpr[11]) },
3144 { "f12", offsetof(CPUPPCState, fpr[12]) },
3145 { "f13", offsetof(CPUPPCState, fpr[13]) },
3146 { "f14", offsetof(CPUPPCState, fpr[14]) },
3147 { "f15", offsetof(CPUPPCState, fpr[15]) },
3148 { "f16", offsetof(CPUPPCState, fpr[16]) },
3149 { "f17", offsetof(CPUPPCState, fpr[17]) },
3150 { "f18", offsetof(CPUPPCState, fpr[18]) },
3151 { "f19", offsetof(CPUPPCState, fpr[19]) },
3152 { "f20", offsetof(CPUPPCState, fpr[20]) },
3153 { "f21", offsetof(CPUPPCState, fpr[21]) },
3154 { "f22", offsetof(CPUPPCState, fpr[22]) },
3155 { "f23", offsetof(CPUPPCState, fpr[23]) },
3156 { "f24", offsetof(CPUPPCState, fpr[24]) },
3157 { "f25", offsetof(CPUPPCState, fpr[25]) },
3158 { "f26", offsetof(CPUPPCState, fpr[26]) },
3159 { "f27", offsetof(CPUPPCState, fpr[27]) },
3160 { "f28", offsetof(CPUPPCState, fpr[28]) },
3161 { "f29", offsetof(CPUPPCState, fpr[29]) },
3162 { "f30", offsetof(CPUPPCState, fpr[30]) },
3163 { "f31", offsetof(CPUPPCState, fpr[31]) },
3164 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003165 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003166 { "nip|pc", offsetof(CPUPPCState, nip) },
3167 { "lr", offsetof(CPUPPCState, lr) },
3168 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003169 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003170 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003171 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003172 { "msr", 0, &monitor_get_msr, },
3173 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003174 { "tbu", 0, &monitor_get_tbu, },
3175 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003176 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003177 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3178 { "sr0", offsetof(CPUPPCState, sr[0]) },
3179 { "sr1", offsetof(CPUPPCState, sr[1]) },
3180 { "sr2", offsetof(CPUPPCState, sr[2]) },
3181 { "sr3", offsetof(CPUPPCState, sr[3]) },
3182 { "sr4", offsetof(CPUPPCState, sr[4]) },
3183 { "sr5", offsetof(CPUPPCState, sr[5]) },
3184 { "sr6", offsetof(CPUPPCState, sr[6]) },
3185 { "sr7", offsetof(CPUPPCState, sr[7]) },
3186 { "sr8", offsetof(CPUPPCState, sr[8]) },
3187 { "sr9", offsetof(CPUPPCState, sr[9]) },
3188 { "sr10", offsetof(CPUPPCState, sr[10]) },
3189 { "sr11", offsetof(CPUPPCState, sr[11]) },
3190 { "sr12", offsetof(CPUPPCState, sr[12]) },
3191 { "sr13", offsetof(CPUPPCState, sr[13]) },
3192 { "sr14", offsetof(CPUPPCState, sr[14]) },
3193 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003194 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003195 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003196
Andreas Färbere59d1672012-02-16 00:40:47 +01003197 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3198 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003199 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3200 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3201 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003202 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3203 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3204 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3205 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3206 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3207 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3208 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3209 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3210 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3211 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3212 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3213 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3214 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3215 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3216 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3217 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3218 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3219 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3220 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3221 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3222 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3223 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3224 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3225 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3226 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3227 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3228 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3229 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3230 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3231 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3232 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3233 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3234 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3235 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3236 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3237 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3238 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3239 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3240 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3241 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3242 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3243 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3244 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3245 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3246 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3247 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3248 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3249 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3250 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3251 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3252 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3253 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3254 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3255 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3256 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3257 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3258 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3259 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3260 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3261 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3262 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3263 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3264 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3265 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003266
bellarde95c8d52004-09-30 22:22:08 +00003267#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003268 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3269 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3270 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3271 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3272 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3273 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3274 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3275 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003276 { "o0", 0, monitor_get_reg },
3277 { "o1", 1, monitor_get_reg },
3278 { "o2", 2, monitor_get_reg },
3279 { "o3", 3, monitor_get_reg },
3280 { "o4", 4, monitor_get_reg },
3281 { "o5", 5, monitor_get_reg },
3282 { "o6", 6, monitor_get_reg },
3283 { "o7", 7, monitor_get_reg },
3284 { "l0", 8, monitor_get_reg },
3285 { "l1", 9, monitor_get_reg },
3286 { "l2", 10, monitor_get_reg },
3287 { "l3", 11, monitor_get_reg },
3288 { "l4", 12, monitor_get_reg },
3289 { "l5", 13, monitor_get_reg },
3290 { "l6", 14, monitor_get_reg },
3291 { "l7", 15, monitor_get_reg },
3292 { "i0", 16, monitor_get_reg },
3293 { "i1", 17, monitor_get_reg },
3294 { "i2", 18, monitor_get_reg },
3295 { "i3", 19, monitor_get_reg },
3296 { "i4", 20, monitor_get_reg },
3297 { "i5", 21, monitor_get_reg },
3298 { "i6", 22, monitor_get_reg },
3299 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003300 { "pc", offsetof(CPUSPARCState, pc) },
3301 { "npc", offsetof(CPUSPARCState, npc) },
3302 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003303#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003304 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003305 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003306#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003307 { "tbr", offsetof(CPUSPARCState, tbr) },
3308 { "fsr", offsetof(CPUSPARCState, fsr) },
3309 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3310 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3311 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3312 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3313 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3314 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3315 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3316 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3317 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3318 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3319 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3320 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3321 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3322 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3323 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3324 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3325 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3326 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3327 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3328 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3329 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3330 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3331 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3332 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3333 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3334 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3335 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3336 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3337 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3338 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3339 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3340 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003341#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003342 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3343 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3344 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3345 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3346 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3347 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3348 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3349 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3350 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3351 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3352 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3353 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3354 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3355 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3356 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3357 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3358 { "asi", offsetof(CPUSPARCState, asi) },
3359 { "pstate", offsetof(CPUSPARCState, pstate) },
3360 { "cansave", offsetof(CPUSPARCState, cansave) },
3361 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3362 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3363 { "wstate", offsetof(CPUSPARCState, wstate) },
3364 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3365 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003366#endif
bellard9307c4c2004-04-04 12:57:25 +00003367#endif
3368 { NULL },
3369};
3370
Stefan Weil9c3175c2013-08-22 21:30:09 +02003371static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3372expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003373{
Fam Zheng277acfe2013-08-20 10:58:21 +08003374 va_list ap;
3375 va_start(ap, fmt);
3376 monitor_vprintf(mon, fmt, ap);
3377 monitor_printf(mon, "\n");
3378 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003379 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003380}
3381
Markus Armbruster09b94182010-01-20 13:07:30 +01003382/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003383static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003384{
blueswir18662d652008-10-02 18:32:44 +00003385 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003386 void *ptr;
3387
bellard9307c4c2004-04-04 12:57:25 +00003388 for(md = monitor_defs; md->name != NULL; md++) {
3389 if (compare_cmd(name, md->name)) {
3390 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003391 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003392 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003393 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003394 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003395 switch(md->type) {
3396 case MD_I32:
3397 *pval = *(int32_t *)ptr;
3398 break;
3399 case MD_TLONG:
3400 *pval = *(target_long *)ptr;
3401 break;
3402 default:
3403 *pval = 0;
3404 break;
3405 }
bellard9307c4c2004-04-04 12:57:25 +00003406 }
3407 return 0;
3408 }
3409 }
3410 return -1;
3411}
3412
3413static void next(void)
3414{
Blue Swirl660f11b2009-07-31 21:16:51 +00003415 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003416 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003417 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003418 pch++;
3419 }
3420}
3421
aliguori376253e2009-03-05 23:01:23 +00003422static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003423
aliguori376253e2009-03-05 23:01:23 +00003424static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003425{
blueswir1c2efc952007-09-25 17:28:42 +00003426 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003427 char *p;
bellard6a00d602005-11-21 23:25:50 +00003428 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003429
3430 switch(*pch) {
3431 case '+':
3432 next();
aliguori376253e2009-03-05 23:01:23 +00003433 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003434 break;
3435 case '-':
3436 next();
aliguori376253e2009-03-05 23:01:23 +00003437 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003438 break;
3439 case '~':
3440 next();
aliguori376253e2009-03-05 23:01:23 +00003441 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003442 break;
3443 case '(':
3444 next();
aliguori376253e2009-03-05 23:01:23 +00003445 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003446 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003447 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003448 }
3449 next();
3450 break;
bellard81d09122004-07-14 17:21:37 +00003451 case '\'':
3452 pch++;
3453 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003454 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003455 n = *pch;
3456 pch++;
3457 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003458 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003459 next();
3460 break;
bellard9307c4c2004-04-04 12:57:25 +00003461 case '$':
3462 {
3463 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003464 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003465
bellard9307c4c2004-04-04 12:57:25 +00003466 pch++;
3467 q = buf;
3468 while ((*pch >= 'a' && *pch <= 'z') ||
3469 (*pch >= 'A' && *pch <= 'Z') ||
3470 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003471 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003472 if ((q - buf) < sizeof(buf) - 1)
3473 *q++ = *pch;
3474 pch++;
3475 }
blueswir1cd390082008-11-16 13:53:32 +00003476 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003477 pch++;
3478 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003479 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003480 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003481 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003482 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003483 }
3484 break;
3485 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003486 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003487 n = 0;
3488 break;
3489 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003490 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003491 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003492 if (errno == ERANGE) {
3493 expr_error(mon, "number too large");
3494 }
bellard9307c4c2004-04-04 12:57:25 +00003495 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003496 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003497 }
3498 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003499 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003500 pch++;
3501 break;
3502 }
3503 return n;
3504}
3505
3506
aliguori376253e2009-03-05 23:01:23 +00003507static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003508{
blueswir1c2efc952007-09-25 17:28:42 +00003509 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003510 int op;
ths3b46e622007-09-17 08:09:54 +00003511
aliguori376253e2009-03-05 23:01:23 +00003512 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003513 for(;;) {
3514 op = *pch;
3515 if (op != '*' && op != '/' && op != '%')
3516 break;
3517 next();
aliguori376253e2009-03-05 23:01:23 +00003518 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003519 switch(op) {
3520 default:
3521 case '*':
3522 val *= val2;
3523 break;
3524 case '/':
3525 case '%':
ths5fafdf22007-09-16 21:08:06 +00003526 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003527 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003528 if (op == '/')
3529 val /= val2;
3530 else
3531 val %= val2;
3532 break;
3533 }
3534 }
3535 return val;
3536}
3537
aliguori376253e2009-03-05 23:01:23 +00003538static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003539{
blueswir1c2efc952007-09-25 17:28:42 +00003540 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003541 int op;
bellard9307c4c2004-04-04 12:57:25 +00003542
aliguori376253e2009-03-05 23:01:23 +00003543 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003544 for(;;) {
3545 op = *pch;
3546 if (op != '&' && op != '|' && op != '^')
3547 break;
3548 next();
aliguori376253e2009-03-05 23:01:23 +00003549 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003550 switch(op) {
3551 default:
3552 case '&':
3553 val &= val2;
3554 break;
3555 case '|':
3556 val |= val2;
3557 break;
3558 case '^':
3559 val ^= val2;
3560 break;
3561 }
3562 }
3563 return val;
3564}
3565
aliguori376253e2009-03-05 23:01:23 +00003566static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003567{
blueswir1c2efc952007-09-25 17:28:42 +00003568 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003569 int op;
bellard9307c4c2004-04-04 12:57:25 +00003570
aliguori376253e2009-03-05 23:01:23 +00003571 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003572 for(;;) {
3573 op = *pch;
3574 if (op != '+' && op != '-')
3575 break;
3576 next();
aliguori376253e2009-03-05 23:01:23 +00003577 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003578 if (op == '+')
3579 val += val2;
3580 else
3581 val -= val2;
3582 }
3583 return val;
3584}
3585
aliguori376253e2009-03-05 23:01:23 +00003586static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003587{
3588 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003589 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003590 *pp = pch;
3591 return -1;
3592 }
blueswir1cd390082008-11-16 13:53:32 +00003593 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003594 pch++;
aliguori376253e2009-03-05 23:01:23 +00003595 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003596 *pp = pch;
3597 return 0;
3598}
3599
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003600static int get_double(Monitor *mon, double *pval, const char **pp)
3601{
3602 const char *p = *pp;
3603 char *tailp;
3604 double d;
3605
3606 d = strtod(p, &tailp);
3607 if (tailp == p) {
3608 monitor_printf(mon, "Number expected\n");
3609 return -1;
3610 }
3611 if (d != d || d - d != 0) {
3612 /* NaN or infinity */
3613 monitor_printf(mon, "Bad number\n");
3614 return -1;
3615 }
3616 *pval = d;
3617 *pp = tailp;
3618 return 0;
3619}
3620
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003621/*
3622 * Store the command-name in cmdname, and return a pointer to
3623 * the remaining of the command string.
3624 */
3625static const char *get_command_name(const char *cmdline,
3626 char *cmdname, size_t nlen)
3627{
3628 size_t len;
3629 const char *p, *pstart;
3630
3631 p = cmdline;
3632 while (qemu_isspace(*p))
3633 p++;
3634 if (*p == '\0')
3635 return NULL;
3636 pstart = p;
3637 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3638 p++;
3639 len = p - pstart;
3640 if (len > nlen - 1)
3641 len = nlen - 1;
3642 memcpy(cmdname, pstart, len);
3643 cmdname[len] = '\0';
3644 return p;
3645}
3646
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003647/**
3648 * Read key of 'type' into 'key' and return the current
3649 * 'type' pointer.
3650 */
3651static char *key_get_info(const char *type, char **key)
3652{
3653 size_t len;
3654 char *p, *str;
3655
3656 if (*type == ',')
3657 type++;
3658
3659 p = strchr(type, ':');
3660 if (!p) {
3661 *key = NULL;
3662 return NULL;
3663 }
3664 len = p - type;
3665
Anthony Liguori7267c092011-08-20 22:09:37 -05003666 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003667 memcpy(str, type, len);
3668 str[len] = '\0';
3669
3670 *key = str;
3671 return ++p;
3672}
3673
bellard9307c4c2004-04-04 12:57:25 +00003674static int default_fmt_format = 'x';
3675static int default_fmt_size = 4;
3676
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003677static int is_valid_option(const char *c, const char *typestr)
3678{
3679 char option[3];
3680
3681 option[0] = '-';
3682 option[1] = *c;
3683 option[2] = '\0';
3684
3685 typestr = strstr(typestr, option);
3686 return (typestr != NULL);
3687}
3688
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003689static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3690 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003691{
3692 const mon_cmd_t *cmd;
3693
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003694 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003695 if (compare_cmd(cmdname, cmd->name)) {
3696 return cmd;
3697 }
3698 }
3699
3700 return NULL;
3701}
3702
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003703static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3704{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003705 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003706}
3707
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003708/*
3709 * Parse @cmdline according to command table @table.
3710 * If @cmdline is blank, return NULL.
3711 * If it can't be parsed, report to @mon, and return NULL.
3712 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003713 * If a sub-command table exists, and if @cmdline contains an additional string
3714 * for a sub-command, this function will try to search the sub-command table.
3715 * If no additional string for a sub-command is present, this function will
3716 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003717 * Do not assume the returned command points into @table! It doesn't
3718 * when the command is a sub-command.
3719 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003720static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003721 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003722 int start,
3723 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003724 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003725{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003726 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003727 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003728 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003729 char cmdname[256];
3730 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003731 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003732
3733#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003734 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003735#endif
ths3b46e622007-09-17 08:09:54 +00003736
bellard9307c4c2004-04-04 12:57:25 +00003737 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003738 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003739 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003740 return NULL;
ths3b46e622007-09-17 08:09:54 +00003741
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003742 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003743 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003744 monitor_printf(mon, "unknown command: '%.*s'\n",
3745 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003746 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003747 }
bellard9307c4c2004-04-04 12:57:25 +00003748
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003749 /* filter out following useless space */
3750 while (qemu_isspace(*p)) {
3751 p++;
3752 }
3753 /* search sub command */
3754 if (cmd->sub_table != NULL) {
3755 /* check if user set additional command */
3756 if (*p == '\0') {
3757 return cmd;
3758 }
3759 return monitor_parse_command(mon, cmdline, p - cmdline,
3760 cmd->sub_table, qdict);
3761 }
3762
bellard9307c4c2004-04-04 12:57:25 +00003763 /* parse the parameters */
3764 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003765 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003766 typestr = key_get_info(typestr, &key);
3767 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003768 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003769 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003770 typestr++;
3771 switch(c) {
3772 case 'F':
bellard81d09122004-07-14 17:21:37 +00003773 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003774 case 's':
3775 {
3776 int ret;
ths3b46e622007-09-17 08:09:54 +00003777
blueswir1cd390082008-11-16 13:53:32 +00003778 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003779 p++;
3780 if (*typestr == '?') {
3781 typestr++;
3782 if (*p == '\0') {
3783 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003784 break;
bellard9307c4c2004-04-04 12:57:25 +00003785 }
3786 }
3787 ret = get_str(buf, sizeof(buf), &p);
3788 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003789 switch(c) {
3790 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003791 monitor_printf(mon, "%s: filename expected\n",
3792 cmdname);
bellard81d09122004-07-14 17:21:37 +00003793 break;
3794 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003795 monitor_printf(mon, "%s: block device name expected\n",
3796 cmdname);
bellard81d09122004-07-14 17:21:37 +00003797 break;
3798 default:
aliguori376253e2009-03-05 23:01:23 +00003799 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003800 break;
3801 }
bellard9307c4c2004-04-04 12:57:25 +00003802 goto fail;
3803 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003804 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003805 }
3806 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003807 case 'O':
3808 {
3809 QemuOptsList *opts_list;
3810 QemuOpts *opts;
3811
3812 opts_list = qemu_find_opts(key);
3813 if (!opts_list || opts_list->desc->name) {
3814 goto bad_type;
3815 }
3816 while (qemu_isspace(*p)) {
3817 p++;
3818 }
3819 if (!*p)
3820 break;
3821 if (get_str(buf, sizeof(buf), &p) < 0) {
3822 goto fail;
3823 }
3824 opts = qemu_opts_parse(opts_list, buf, 1);
3825 if (!opts) {
3826 goto fail;
3827 }
3828 qemu_opts_to_qdict(opts, qdict);
3829 qemu_opts_del(opts);
3830 }
3831 break;
bellard9307c4c2004-04-04 12:57:25 +00003832 case '/':
3833 {
3834 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003835
blueswir1cd390082008-11-16 13:53:32 +00003836 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003837 p++;
3838 if (*p == '/') {
3839 /* format found */
3840 p++;
3841 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003842 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003843 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003844 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003845 count = count * 10 + (*p - '0');
3846 p++;
3847 }
3848 }
3849 size = -1;
3850 format = -1;
3851 for(;;) {
3852 switch(*p) {
3853 case 'o':
3854 case 'd':
3855 case 'u':
3856 case 'x':
3857 case 'i':
3858 case 'c':
3859 format = *p++;
3860 break;
3861 case 'b':
3862 size = 1;
3863 p++;
3864 break;
3865 case 'h':
3866 size = 2;
3867 p++;
3868 break;
3869 case 'w':
3870 size = 4;
3871 p++;
3872 break;
3873 case 'g':
3874 case 'L':
3875 size = 8;
3876 p++;
3877 break;
3878 default:
3879 goto next;
3880 }
3881 }
3882 next:
blueswir1cd390082008-11-16 13:53:32 +00003883 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003884 monitor_printf(mon, "invalid char in format: '%c'\n",
3885 *p);
bellard9307c4c2004-04-04 12:57:25 +00003886 goto fail;
3887 }
bellard9307c4c2004-04-04 12:57:25 +00003888 if (format < 0)
3889 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003890 if (format != 'i') {
3891 /* for 'i', not specifying a size gives -1 as size */
3892 if (size < 0)
3893 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003894 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003895 }
bellard9307c4c2004-04-04 12:57:25 +00003896 default_fmt_format = format;
3897 } else {
3898 count = 1;
3899 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003900 if (format != 'i') {
3901 size = default_fmt_size;
3902 } else {
3903 size = -1;
3904 }
bellard9307c4c2004-04-04 12:57:25 +00003905 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003906 qdict_put(qdict, "count", qint_from_int(count));
3907 qdict_put(qdict, "format", qint_from_int(format));
3908 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003909 }
3910 break;
3911 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003912 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003913 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003914 {
blueswir1c2efc952007-09-25 17:28:42 +00003915 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003916
blueswir1cd390082008-11-16 13:53:32 +00003917 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003918 p++;
bellard34405572004-06-08 00:55:58 +00003919 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003920 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003921 if (*p == '\0') {
3922 typestr++;
3923 break;
3924 }
bellard34405572004-06-08 00:55:58 +00003925 } else {
3926 if (*p == '.') {
3927 p++;
blueswir1cd390082008-11-16 13:53:32 +00003928 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003929 p++;
bellard34405572004-06-08 00:55:58 +00003930 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003931 typestr++;
3932 break;
bellard34405572004-06-08 00:55:58 +00003933 }
3934 }
bellard13224a82006-07-14 22:03:35 +00003935 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003936 }
aliguori376253e2009-03-05 23:01:23 +00003937 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003938 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003939 /* Check if 'i' is greater than 32-bit */
3940 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3941 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3942 monitor_printf(mon, "integer is for 32-bit values\n");
3943 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003944 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003945 if (val < 0) {
3946 monitor_printf(mon, "enter a positive value\n");
3947 goto fail;
3948 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003949 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003950 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003951 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003952 }
3953 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003954 case 'o':
3955 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003956 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003957 char *end;
3958
3959 while (qemu_isspace(*p)) {
3960 p++;
3961 }
3962 if (*typestr == '?') {
3963 typestr++;
3964 if (*p == '\0') {
3965 break;
3966 }
3967 }
3968 val = strtosz(p, &end);
3969 if (val < 0) {
3970 monitor_printf(mon, "invalid size\n");
3971 goto fail;
3972 }
3973 qdict_put(qdict, key, qint_from_int(val));
3974 p = end;
3975 }
3976 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003977 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003978 {
3979 double val;
3980
3981 while (qemu_isspace(*p))
3982 p++;
3983 if (*typestr == '?') {
3984 typestr++;
3985 if (*p == '\0') {
3986 break;
3987 }
3988 }
3989 if (get_double(mon, &val, &p) < 0) {
3990 goto fail;
3991 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003992 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003993 switch (*p) {
3994 case 'm':
3995 val /= 1e3; p += 2; break;
3996 case 'u':
3997 val /= 1e6; p += 2; break;
3998 case 'n':
3999 val /= 1e9; p += 2; break;
4000 }
4001 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004002 if (*p && !qemu_isspace(*p)) {
4003 monitor_printf(mon, "Unknown unit suffix\n");
4004 goto fail;
4005 }
4006 qdict_put(qdict, key, qfloat_from_double(val));
4007 }
4008 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004009 case 'b':
4010 {
4011 const char *beg;
4012 int val;
4013
4014 while (qemu_isspace(*p)) {
4015 p++;
4016 }
4017 beg = p;
4018 while (qemu_isgraph(*p)) {
4019 p++;
4020 }
4021 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4022 val = 1;
4023 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4024 val = 0;
4025 } else {
4026 monitor_printf(mon, "Expected 'on' or 'off'\n");
4027 goto fail;
4028 }
4029 qdict_put(qdict, key, qbool_from_int(val));
4030 }
4031 break;
bellard9307c4c2004-04-04 12:57:25 +00004032 case '-':
4033 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004034 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004035 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004036 /* option */
ths3b46e622007-09-17 08:09:54 +00004037
bellard9307c4c2004-04-04 12:57:25 +00004038 c = *typestr++;
4039 if (c == '\0')
4040 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004041 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004042 p++;
bellard9307c4c2004-04-04 12:57:25 +00004043 if (*p == '-') {
4044 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004045 if(c != *p) {
4046 if(!is_valid_option(p, typestr)) {
4047
4048 monitor_printf(mon, "%s: unsupported option -%c\n",
4049 cmdname, *p);
4050 goto fail;
4051 } else {
4052 skip_key = 1;
4053 }
bellard9307c4c2004-04-04 12:57:25 +00004054 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004055 if(skip_key) {
4056 p = tmp;
4057 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004058 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004059 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004060 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004061 }
bellard9307c4c2004-04-04 12:57:25 +00004062 }
bellard9307c4c2004-04-04 12:57:25 +00004063 }
4064 break;
Wenchao Xia129be002013-08-27 20:38:26 +08004065 case 'S':
4066 {
4067 /* package all remaining string */
4068 int len;
4069
4070 while (qemu_isspace(*p)) {
4071 p++;
4072 }
4073 if (*typestr == '?') {
4074 typestr++;
4075 if (*p == '\0') {
4076 /* no remaining string: NULL argument */
4077 break;
4078 }
4079 }
4080 len = strlen(p);
4081 if (len <= 0) {
4082 monitor_printf(mon, "%s: string expected\n",
4083 cmdname);
4084 break;
4085 }
4086 qdict_put(qdict, key, qstring_from_str(p));
4087 p += len;
4088 }
4089 break;
bellard9307c4c2004-04-04 12:57:25 +00004090 default:
4091 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004092 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004093 goto fail;
4094 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004095 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004096 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004097 }
4098 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004099 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004100 p++;
4101 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004102 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4103 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004104 goto fail;
4105 }
4106
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004107 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004108
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004109fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004110 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004111 return NULL;
4112}
4113
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004114void monitor_set_error(Monitor *mon, QError *qerror)
4115{
4116 /* report only the first error */
4117 if (!mon->error) {
4118 mon->error = qerror;
4119 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004120 QDECREF(qerror);
4121 }
4122}
4123
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004124static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4125{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004126 if (ret && !monitor_has_error(mon)) {
4127 /*
4128 * If it returns failure, it must have passed on error.
4129 *
4130 * Action: Report an internal error to the client if in QMP.
4131 */
4132 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004133 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004134}
4135
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004136static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004137{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004138 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004139 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004140
4141 qdict = qdict_new();
4142
Wenchao Xia77172392013-08-27 20:38:20 +08004143 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004144 if (!cmd)
4145 goto out;
4146
Luiz Capitulino4903de02010-09-16 11:01:32 -03004147 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004148 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004149 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004150 QObject *data = NULL;
4151
4152 /* XXX: ignores the error code */
4153 cmd->mhandler.cmd_new(mon, qdict, &data);
4154 assert(!monitor_has_error(mon));
4155 if (data) {
4156 cmd->user_print(mon, data);
4157 qobject_decref(data);
4158 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004159 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004160 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004161 }
4162
Luiz Capitulino13917be2009-10-07 13:41:54 -03004163out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004164 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004165}
4166
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004167static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004168{
4169 const char *p, *pstart;
4170 char cmd[128];
4171 int len;
4172
4173 p = list;
4174 for(;;) {
4175 pstart = p;
4176 p = strchr(p, '|');
4177 if (!p)
4178 p = pstart + strlen(pstart);
4179 len = p - pstart;
4180 if (len > sizeof(cmd) - 2)
4181 len = sizeof(cmd) - 2;
4182 memcpy(cmd, pstart, len);
4183 cmd[len] = '\0';
4184 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004185 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004186 }
4187 if (*p == '\0')
4188 break;
4189 p++;
4190 }
4191}
4192
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004193static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004194{
4195 DIR *ffs;
4196 struct dirent *d;
4197 char path[1024];
4198 char file[1024], file_prefix[1024];
4199 int input_path_len;
4200 const char *p;
4201
ths5fafdf22007-09-16 21:08:06 +00004202 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004203 if (!p) {
4204 input_path_len = 0;
4205 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004206 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004207 } else {
4208 input_path_len = p - input + 1;
4209 memcpy(path, input, input_path_len);
4210 if (input_path_len > sizeof(path) - 1)
4211 input_path_len = sizeof(path) - 1;
4212 path[input_path_len] = '\0';
4213 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4214 }
4215#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004216 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004217 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004218#endif
4219 ffs = opendir(path);
4220 if (!ffs)
4221 return;
4222 for(;;) {
4223 struct stat sb;
4224 d = readdir(ffs);
4225 if (!d)
4226 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004227
4228 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4229 continue;
4230 }
4231
bellard81d09122004-07-14 17:21:37 +00004232 if (strstart(d->d_name, file_prefix, NULL)) {
4233 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004234 if (input_path_len < sizeof(file))
4235 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4236 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004237 /* stat the file to find out if it's a directory.
4238 * In that case add a slash to speed up typing long paths
4239 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004240 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004241 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004242 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004243 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004244 }
4245 }
4246 closedir(ffs);
4247}
4248
Wenchao Xia599a9262013-08-27 20:38:15 +08004249typedef struct MonitorBlockComplete {
4250 Monitor *mon;
4251 const char *input;
4252} MonitorBlockComplete;
4253
aliguori51de9762009-03-05 23:00:43 +00004254static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004255{
aliguori51de9762009-03-05 23:00:43 +00004256 const char *name = bdrv_get_device_name(bs);
Wenchao Xia599a9262013-08-27 20:38:15 +08004257 MonitorBlockComplete *mbc = opaque;
4258 Monitor *mon = mbc->mon;
4259 const char *input = mbc->input;
bellard81d09122004-07-14 17:21:37 +00004260
4261 if (input[0] == '\0' ||
4262 !strncmp(name, (char *)input, strlen(input))) {
Wenchao Xia599a9262013-08-27 20:38:15 +08004263 readline_add_completion(mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004264 }
4265}
4266
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004267static const char *next_arg_type(const char *typestr)
4268{
4269 const char *p = strchr(typestr, ':');
4270 return (p != NULL ? ++p : typestr);
4271}
4272
Hani Benhabiles40d19392014-05-07 23:41:30 +01004273static void add_completion_option(ReadLineState *rs, const char *str,
4274 const char *option)
4275{
4276 if (!str || !option) {
4277 return;
4278 }
4279 if (!strncmp(option, str, strlen(str))) {
4280 readline_add_completion(rs, option);
4281 }
4282}
4283
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004284void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4285{
4286 size_t len;
4287 ChardevBackendInfoList *list, *start;
4288
4289 if (nb_args != 2) {
4290 return;
4291 }
4292 len = strlen(str);
4293 readline_set_completion_index(rs, len);
4294
4295 start = list = qmp_query_chardev_backends(NULL);
4296 while (list) {
4297 const char *chr_name = list->value->name;
4298
4299 if (!strncmp(chr_name, str, len)) {
4300 readline_add_completion(rs, chr_name);
4301 }
4302 list = list->next;
4303 }
4304 qapi_free_ChardevBackendInfoList(start);
4305}
4306
Hani Benhabilesb162b492014-05-07 23:41:31 +01004307void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4308{
4309 size_t len;
4310 int i;
4311
4312 if (nb_args != 2) {
4313 return;
4314 }
4315 len = strlen(str);
4316 readline_set_completion_index(rs, len);
4317 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4318 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4319 }
4320}
4321
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004322void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004323{
4324 GSList *list, *elt;
4325 size_t len;
4326
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004327 if (nb_args != 2) {
4328 return;
4329 }
4330
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004331 len = strlen(str);
4332 readline_set_completion_index(rs, len);
4333 list = elt = object_class_get_list(TYPE_DEVICE, false);
4334 while (elt) {
4335 const char *name;
4336 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4337 TYPE_DEVICE);
4338 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004339
4340 if (!dc->cannot_instantiate_with_device_add_yet
4341 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004342 readline_add_completion(rs, name);
4343 }
4344 elt = elt->next;
4345 }
4346 g_slist_free(list);
4347}
4348
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004349void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004350{
4351 GSList *list, *elt;
4352 size_t len;
4353
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004354 if (nb_args != 2) {
4355 return;
4356 }
4357
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004358 len = strlen(str);
4359 readline_set_completion_index(rs, len);
4360 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4361 while (elt) {
4362 const char *name;
4363
4364 name = object_class_get_name(OBJECT_CLASS(elt->data));
4365 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4366 readline_add_completion(rs, name);
4367 }
4368 elt = elt->next;
4369 }
4370 g_slist_free(list);
4371}
4372
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004373static void device_del_bus_completion(ReadLineState *rs, BusState *bus,
4374 const char *str, size_t len)
Hani Benhabilesfca72d92014-02-06 23:30:10 +01004375{
4376 BusChild *kid;
4377
4378 QTAILQ_FOREACH(kid, &bus->children, sibling) {
4379 DeviceState *dev = kid->child;
4380 BusState *dev_child;
4381
4382 if (dev->id && !strncmp(str, dev->id, len)) {
4383 readline_add_completion(rs, dev->id);
4384 }
4385
4386 QLIST_FOREACH(dev_child, &dev->child_bus, sibling) {
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004387 device_del_bus_completion(rs, dev_child, str, len);
Hani Benhabilesfca72d92014-02-06 23:30:10 +01004388 }
4389 }
4390}
4391
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004392void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4393{
4394 size_t len;
4395 ChardevInfoList *list, *start;
4396
4397 if (nb_args != 2) {
4398 return;
4399 }
4400 len = strlen(str);
4401 readline_set_completion_index(rs, len);
4402
4403 start = list = qmp_query_chardev(NULL);
4404 while (list) {
4405 ChardevInfo *chr = list->value;
4406
4407 if (!strncmp(chr->label, str, len)) {
4408 readline_add_completion(rs, chr->label);
4409 }
4410 list = list->next;
4411 }
4412 qapi_free_ChardevInfoList(start);
4413}
4414
Hani Benhabiles8e597772014-05-27 23:39:30 +01004415static void ringbuf_completion(ReadLineState *rs, const char *str)
4416{
4417 size_t len;
4418 ChardevInfoList *list, *start;
4419
4420 len = strlen(str);
4421 readline_set_completion_index(rs, len);
4422
4423 start = list = qmp_query_chardev(NULL);
4424 while (list) {
4425 ChardevInfo *chr_info = list->value;
4426
4427 if (!strncmp(chr_info->label, str, len)) {
4428 CharDriverState *chr = qemu_chr_find(chr_info->label);
4429 if (chr && chr_is_ringbuf(chr)) {
4430 readline_add_completion(rs, chr_info->label);
4431 }
4432 }
4433 list = list->next;
4434 }
4435 qapi_free_ChardevInfoList(start);
4436}
4437
4438void ringbuf_read_completion(ReadLineState *rs, int nb_args, const char *str)
4439{
4440 if (nb_args != 2) {
4441 return;
4442 }
4443 ringbuf_completion(rs, str);
4444}
4445
4446void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4447{
4448 if (nb_args != 2) {
4449 return;
4450 }
4451 ringbuf_completion(rs, str);
4452}
4453
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004454void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4455{
4456 size_t len;
4457
4458 if (nb_args != 2) {
4459 return;
4460 }
4461
4462 len = strlen(str);
4463 readline_set_completion_index(rs, len);
4464 device_del_bus_completion(rs, sysbus_get_default(), str, len);
4465}
4466
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004467void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004468{
4469 ObjectPropertyInfoList *list, *start;
4470 size_t len;
4471
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004472 if (nb_args != 2) {
4473 return;
4474 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004475 len = strlen(str);
4476 readline_set_completion_index(rs, len);
4477
4478 start = list = qmp_qom_list("/objects", NULL);
4479 while (list) {
4480 ObjectPropertyInfo *info = list->value;
4481
4482 if (!strncmp(info->type, "child<", 5)
4483 && !strncmp(info->name, str, len)) {
4484 readline_add_completion(rs, info->name);
4485 }
4486 list = list->next;
4487 }
4488 qapi_free_ObjectPropertyInfoList(start);
4489}
4490
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004491void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4492{
4493 int i;
4494 char *sep;
4495 size_t len;
4496
4497 if (nb_args != 2) {
4498 return;
4499 }
4500 sep = strrchr(str, '-');
4501 if (sep) {
4502 str = sep + 1;
4503 }
4504 len = strlen(str);
4505 readline_set_completion_index(rs, len);
4506 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4507 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4508 readline_add_completion(rs, QKeyCode_lookup[i]);
4509 }
4510 }
4511}
4512
Hani Benhabiles40d19392014-05-07 23:41:30 +01004513void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4514{
4515 size_t len;
4516
4517 len = strlen(str);
4518 readline_set_completion_index(rs, len);
4519 if (nb_args == 2) {
4520 NetClientState *ncs[255];
4521 int count, i;
4522 count = qemu_find_net_clients_except(NULL, ncs,
4523 NET_CLIENT_OPTIONS_KIND_NONE, 255);
4524 for (i = 0; i < count; i++) {
4525 const char *name = ncs[i]->name;
4526 if (!strncmp(str, name, len)) {
4527 readline_add_completion(rs, name);
4528 }
4529 }
4530 } else if (nb_args == 3) {
4531 add_completion_option(rs, str, "on");
4532 add_completion_option(rs, str, "off");
4533 }
4534}
4535
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004536void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4537{
4538 int len, count, i;
4539 NetClientState *ncs[255];
4540
4541 if (nb_args != 2) {
4542 return;
4543 }
4544
4545 len = strlen(str);
4546 readline_set_completion_index(rs, len);
4547 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
4548 255);
4549 for (i = 0; i < count; i++) {
4550 QemuOpts *opts;
4551 const char *name = ncs[i]->name;
4552 if (strncmp(str, name, len)) {
4553 continue;
4554 }
4555 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4556 if (opts) {
4557 readline_add_completion(rs, name);
4558 }
4559 }
4560}
4561
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004562void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4563{
4564 if (nb_args != 2) {
4565 return;
4566 }
4567 readline_set_completion_index(rs, strlen(str));
4568 add_completion_option(rs, str, "reset");
4569 add_completion_option(rs, str, "shutdown");
4570 add_completion_option(rs, str, "poweroff");
4571 add_completion_option(rs, str, "pause");
4572 add_completion_option(rs, str, "debug");
4573 add_completion_option(rs, str, "none");
4574}
4575
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004576void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4577 const char *str)
4578{
4579 size_t len;
4580
4581 len = strlen(str);
4582 readline_set_completion_index(rs, len);
4583 if (nb_args == 2) {
4584 int i;
4585 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4586 const char *name = MigrationCapability_lookup[i];
4587 if (!strncmp(str, name, len)) {
4588 readline_add_completion(rs, name);
4589 }
4590 }
4591 } else if (nb_args == 3) {
4592 add_completion_option(rs, str, "on");
4593 add_completion_option(rs, str, "off");
4594 }
4595}
4596
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004597void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4598{
4599 int i;
4600 size_t len;
4601 if (nb_args != 2) {
4602 return;
4603 }
4604 len = strlen(str);
4605 readline_set_completion_index(rs, len);
4606 for (i = 0; host_net_devices[i]; i++) {
4607 if (!strncmp(host_net_devices[i], str, len)) {
4608 readline_add_completion(rs, host_net_devices[i]);
4609 }
4610 }
4611}
4612
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004613void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4614{
4615 NetClientState *ncs[255];
4616 int count, i, len;
4617
4618 len = strlen(str);
4619 readline_set_completion_index(rs, len);
4620 if (nb_args == 2) {
4621 count = qemu_find_net_clients_except(NULL, ncs,
4622 NET_CLIENT_OPTIONS_KIND_NONE, 255);
4623 for (i = 0; i < count; i++) {
4624 int id;
4625 char name[16];
4626
4627 if (net_hub_id_for_client(ncs[i], &id)) {
4628 continue;
4629 }
4630 snprintf(name, sizeof(name), "%d", id);
4631 if (!strncmp(str, name, len)) {
4632 readline_add_completion(rs, name);
4633 }
4634 }
4635 return;
4636 } else if (nb_args == 3) {
4637 count = qemu_find_net_clients_except(NULL, ncs,
4638 NET_CLIENT_OPTIONS_KIND_NIC, 255);
4639 for (i = 0; i < count; i++) {
4640 const char *name;
4641
4642 name = ncs[i]->name;
4643 if (!strncmp(str, name, len)) {
4644 readline_add_completion(rs, name);
4645 }
4646 }
4647 return;
4648 }
4649}
4650
Wenchao Xiac35b6402013-08-27 20:38:24 +08004651static void monitor_find_completion_by_table(Monitor *mon,
4652 const mon_cmd_t *cmd_table,
4653 char **args,
4654 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004655{
4656 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004657 int i;
bellard81d09122004-07-14 17:21:37 +00004658 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004659 const mon_cmd_t *cmd;
Wenchao Xia599a9262013-08-27 20:38:15 +08004660 MonitorBlockComplete mbs;
bellard81d09122004-07-14 17:21:37 +00004661
bellard81d09122004-07-14 17:21:37 +00004662 if (nb_args <= 1) {
4663 /* command completion */
4664 if (nb_args == 0)
4665 cmdname = "";
4666 else
4667 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004668 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004669 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004670 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004671 }
4672 } else {
4673 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004674 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004675 if (compare_cmd(args[0], cmd->name)) {
4676 break;
4677 }
bellard81d09122004-07-14 17:21:37 +00004678 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004679 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004680 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004681 }
4682
Wenchao Xiad903a772013-08-27 20:38:25 +08004683 if (cmd->sub_table) {
4684 /* do the job again */
4685 return monitor_find_completion_by_table(mon, cmd->sub_table,
4686 &args[1], nb_args - 1);
4687 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004688 if (cmd->command_completion) {
4689 return cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4690 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004691
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004692 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004693 for(i = 0; i < nb_args - 2; i++) {
4694 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004695 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004696 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004697 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004698 }
4699 }
4700 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004701 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004702 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004703 }
bellard81d09122004-07-14 17:21:37 +00004704 switch(*ptype) {
4705 case 'F':
4706 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004707 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004708 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004709 break;
4710 case 'B':
4711 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004712 mbs.mon = mon;
4713 mbs.input = str;
4714 readline_set_completion_index(mon->rs, strlen(str));
4715 bdrv_iterate(block_completion_it, &mbs);
bellard81d09122004-07-14 17:21:37 +00004716 break;
bellard7fe48482004-10-09 18:08:01 +00004717 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004718 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004719 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004720 monitor_find_completion_by_table(mon, cmd_table,
4721 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004722 }
4723 break;
bellard81d09122004-07-14 17:21:37 +00004724 default:
4725 break;
4726 }
4727 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004728}
4729
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004730static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004731 const char *cmdline)
4732{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004733 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004734 char *args[MAX_ARGS];
4735 int nb_args, len;
4736
4737 /* 1. parse the cmdline */
4738 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4739 return;
4740 }
4741#ifdef DEBUG_COMPLETION
4742 for (i = 0; i < nb_args; i++) {
4743 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4744 }
4745#endif
4746
4747 /* if the line ends with a space, it means we want to complete the
4748 next arg */
4749 len = strlen(cmdline);
4750 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4751 if (nb_args >= MAX_ARGS) {
4752 goto cleanup;
4753 }
4754 args[nb_args++] = g_strdup("");
4755 }
4756
4757 /* 2. auto complete according to args */
4758 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004759
4760cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004761 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004762}
4763
aliguori731b0362009-03-05 23:01:42 +00004764static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004765{
aliguori731b0362009-03-05 23:01:42 +00004766 Monitor *mon = opaque;
4767
Jan Kiszkac62313b2009-12-04 14:05:29 +01004768 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004769}
4770
Luiz Capitulino09069b12010-02-04 18:10:06 -02004771static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4772{
4773 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4774 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4775}
4776
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004777/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004778 * Argument validation rules:
4779 *
4780 * 1. The argument must exist in cmd_args qdict
4781 * 2. The argument type must be the expected one
4782 *
4783 * Special case: If the argument doesn't exist in cmd_args and
4784 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4785 * checking is skipped for it.
4786 */
4787static int check_client_args_type(const QDict *client_args,
4788 const QDict *cmd_args, int flags)
4789{
4790 const QDictEntry *ent;
4791
4792 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4793 QObject *obj;
4794 QString *arg_type;
4795 const QObject *client_arg = qdict_entry_value(ent);
4796 const char *client_arg_name = qdict_entry_key(ent);
4797
4798 obj = qdict_get(cmd_args, client_arg_name);
4799 if (!obj) {
4800 if (flags & QMP_ACCEPT_UNKNOWNS) {
4801 /* handler accepts unknowns */
4802 continue;
4803 }
4804 /* client arg doesn't exist */
4805 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4806 return -1;
4807 }
4808
4809 arg_type = qobject_to_qstring(obj);
4810 assert(arg_type != NULL);
4811
4812 /* check if argument's type is correct */
4813 switch (qstring_get_str(arg_type)[0]) {
4814 case 'F':
4815 case 'B':
4816 case 's':
4817 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4818 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4819 "string");
4820 return -1;
4821 }
4822 break;
4823 case 'i':
4824 case 'l':
4825 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004826 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004827 if (qobject_type(client_arg) != QTYPE_QINT) {
4828 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4829 "int");
4830 return -1;
4831 }
4832 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004833 case 'T':
4834 if (qobject_type(client_arg) != QTYPE_QINT &&
4835 qobject_type(client_arg) != QTYPE_QFLOAT) {
4836 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4837 "number");
4838 return -1;
4839 }
4840 break;
4841 case 'b':
4842 case '-':
4843 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4844 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4845 "bool");
4846 return -1;
4847 }
4848 break;
4849 case 'O':
4850 assert(flags & QMP_ACCEPT_UNKNOWNS);
4851 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004852 case 'q':
4853 /* Any QObject can be passed. */
4854 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004855 case '/':
4856 case '.':
4857 /*
4858 * These types are not supported by QMP and thus are not
4859 * handled here. Fall through.
4860 */
4861 default:
4862 abort();
4863 }
4864 }
4865
4866 return 0;
4867}
4868
4869/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004870 * - Check if the client has passed all mandatory args
4871 * - Set special flags for argument validation
4872 */
4873static int check_mandatory_args(const QDict *cmd_args,
4874 const QDict *client_args, int *flags)
4875{
4876 const QDictEntry *ent;
4877
4878 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4879 const char *cmd_arg_name = qdict_entry_key(ent);
4880 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4881 assert(type != NULL);
4882
4883 if (qstring_get_str(type)[0] == 'O') {
4884 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4885 *flags |= QMP_ACCEPT_UNKNOWNS;
4886 } else if (qstring_get_str(type)[0] != '-' &&
4887 qstring_get_str(type)[1] != '?' &&
4888 !qdict_haskey(client_args, cmd_arg_name)) {
4889 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4890 return -1;
4891 }
4892 }
4893
4894 return 0;
4895}
4896
4897static QDict *qdict_from_args_type(const char *args_type)
4898{
4899 int i;
4900 QDict *qdict;
4901 QString *key, *type, *cur_qs;
4902
4903 assert(args_type != NULL);
4904
4905 qdict = qdict_new();
4906
4907 if (args_type == NULL || args_type[0] == '\0') {
4908 /* no args, empty qdict */
4909 goto out;
4910 }
4911
4912 key = qstring_new();
4913 type = qstring_new();
4914
4915 cur_qs = key;
4916
4917 for (i = 0;; i++) {
4918 switch (args_type[i]) {
4919 case ',':
4920 case '\0':
4921 qdict_put(qdict, qstring_get_str(key), type);
4922 QDECREF(key);
4923 if (args_type[i] == '\0') {
4924 goto out;
4925 }
4926 type = qstring_new(); /* qdict has ref */
4927 cur_qs = key = qstring_new();
4928 break;
4929 case ':':
4930 cur_qs = type;
4931 break;
4932 default:
4933 qstring_append_chr(cur_qs, args_type[i]);
4934 break;
4935 }
4936 }
4937
4938out:
4939 return qdict;
4940}
4941
4942/*
4943 * Client argument checking rules:
4944 *
4945 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004946 * 2. Each argument provided by the client must be expected
4947 * 3. Each argument provided by the client must have the type expected
4948 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004949 */
4950static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4951{
4952 int flags, err;
4953 QDict *cmd_args;
4954
4955 cmd_args = qdict_from_args_type(cmd->args_type);
4956
4957 flags = 0;
4958 err = check_mandatory_args(cmd_args, client_args, &flags);
4959 if (err) {
4960 goto out;
4961 }
4962
Luiz Capitulino4af91932010-06-22 11:44:05 -03004963 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004964
4965out:
4966 QDECREF(cmd_args);
4967 return err;
4968}
4969
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004970/*
4971 * Input object checking rules
4972 *
4973 * 1. Input object must be a dict
4974 * 2. The "execute" key must exist
4975 * 3. The "execute" key must be a string
4976 * 4. If the "arguments" key exists, it must be a dict
4977 * 5. If the "id" key exists, it can be anything (ie. json-value)
4978 * 6. Any argument not listed above is considered invalid
4979 */
4980static QDict *qmp_check_input_obj(QObject *input_obj)
4981{
4982 const QDictEntry *ent;
4983 int has_exec_key = 0;
4984 QDict *input_dict;
4985
4986 if (qobject_type(input_obj) != QTYPE_QDICT) {
4987 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4988 return NULL;
4989 }
4990
4991 input_dict = qobject_to_qdict(input_obj);
4992
4993 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4994 const char *arg_name = qdict_entry_key(ent);
4995 const QObject *arg_obj = qdict_entry_value(ent);
4996
4997 if (!strcmp(arg_name, "execute")) {
4998 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4999 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
5000 "string");
5001 return NULL;
5002 }
5003 has_exec_key = 1;
5004 } else if (!strcmp(arg_name, "arguments")) {
5005 if (qobject_type(arg_obj) != QTYPE_QDICT) {
5006 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
5007 "object");
5008 return NULL;
5009 }
5010 } else if (!strcmp(arg_name, "id")) {
5011 /* FIXME: check duplicated IDs for async commands */
5012 } else {
5013 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
5014 return NULL;
5015 }
5016 }
5017
5018 if (!has_exec_key) {
5019 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5020 return NULL;
5021 }
5022
5023 return input_dict;
5024}
5025
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005026static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
5027 const QDict *params)
5028{
5029 int ret;
5030 QObject *data = NULL;
5031
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005032 ret = cmd->mhandler.cmd_new(mon, params, &data);
5033 handler_audit(mon, cmd, ret);
5034 monitor_protocol_emitter(mon, data);
5035 qobject_decref(data);
5036}
5037
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005038static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5039{
5040 int err;
5041 QObject *obj;
5042 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005043 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005044 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005045 Monitor *mon = cur_mon;
5046
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005047 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005048
5049 obj = json_parser_parse(tokens, NULL);
5050 if (!obj) {
5051 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005052 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005053 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005054 }
5055
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005056 input = qmp_check_input_obj(obj);
5057 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005058 qobject_decref(obj);
5059 goto err_out;
5060 }
5061
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005062 mon->mc->id = qdict_get(input, "id");
5063 qobject_incref(mon->mc->id);
5064
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005065 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005066 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02005067 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005068 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005069 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02005070 }
5071
Anthony Liguorie3193602011-09-02 12:34:47 -05005072 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03005073 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005074 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005075 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005076 }
5077
5078 obj = qdict_get(input, "arguments");
5079 if (!obj) {
5080 args = qdict_new();
5081 } else {
5082 args = qobject_to_qdict(obj);
5083 QINCREF(args);
5084 }
5085
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005086 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005087 if (err < 0) {
5088 goto err_out;
5089 }
5090
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005091 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03005092 err = qmp_async_cmd_handler(mon, cmd, args);
5093 if (err) {
5094 /* emit the error response */
5095 goto err_out;
5096 }
Adam Litke940cc302010-01-25 12:18:44 -06005097 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03005098 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06005099 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005100
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005101 goto out;
5102
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005103err_out:
5104 monitor_protocol_emitter(mon, NULL);
5105out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005106 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005107 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005108}
5109
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005110/**
5111 * monitor_control_read(): Read and handle QMP input
5112 */
5113static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5114{
5115 Monitor *old_mon = cur_mon;
5116
5117 cur_mon = opaque;
5118
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005119 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005120
5121 cur_mon = old_mon;
5122}
5123
aliguori731b0362009-03-05 23:01:42 +00005124static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005125{
aliguori731b0362009-03-05 23:01:42 +00005126 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005127 int i;
aliguori376253e2009-03-05 23:01:23 +00005128
aliguori731b0362009-03-05 23:01:42 +00005129 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005130
aliguoricde76ee2009-03-05 23:01:51 +00005131 if (cur_mon->rs) {
5132 for (i = 0; i < size; i++)
5133 readline_handle_byte(cur_mon->rs, buf[i]);
5134 } else {
5135 if (size == 0 || buf[size - 1] != 0)
5136 monitor_printf(cur_mon, "corrupted command\n");
5137 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005138 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005139 }
aliguori731b0362009-03-05 23:01:42 +00005140
5141 cur_mon = old_mon;
5142}
aliguorid8f44602008-10-06 13:52:44 +00005143
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005144static void monitor_command_cb(void *opaque, const char *cmdline,
5145 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005146{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005147 Monitor *mon = opaque;
5148
aliguori731b0362009-03-05 23:01:42 +00005149 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005150 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005151 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005152}
5153
aliguoricde76ee2009-03-05 23:01:51 +00005154int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005155{
aliguoricde76ee2009-03-05 23:01:51 +00005156 if (!mon->rs)
5157 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005158 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005159 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005160}
5161
aliguori376253e2009-03-05 23:01:23 +00005162void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005163{
aliguoricde76ee2009-03-05 23:01:51 +00005164 if (!mon->rs)
5165 return;
aliguori731b0362009-03-05 23:01:42 +00005166 if (--mon->suspend_cnt == 0)
5167 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005168}
5169
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005170static QObject *get_qmp_greeting(void)
5171{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005172 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005173
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005174 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005175 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5176}
5177
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005178/**
5179 * monitor_control_event(): Print QMP gretting
5180 */
5181static void monitor_control_event(void *opaque, int event)
5182{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005183 QObject *data;
5184 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005185
Luiz Capitulino47116d12010-02-08 17:01:30 -02005186 switch (event) {
5187 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005188 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005189 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005190 monitor_json_emitter(mon, data);
5191 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005192 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005193 break;
5194 case CHR_EVENT_CLOSED:
5195 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05005196 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005197 mon_refcount--;
5198 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005199 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005200 }
5201}
5202
aliguori731b0362009-03-05 23:01:42 +00005203static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005204{
aliguori376253e2009-03-05 23:01:23 +00005205 Monitor *mon = opaque;
5206
aliguori2724b182009-03-05 23:01:47 +00005207 switch (event) {
5208 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005209 mon->mux_out = 0;
5210 if (mon->reset_seen) {
5211 readline_restart(mon->rs);
5212 monitor_resume(mon);
5213 monitor_flush(mon);
5214 } else {
5215 mon->suspend_cnt = 0;
5216 }
aliguori2724b182009-03-05 23:01:47 +00005217 break;
ths86e94de2007-01-05 22:01:59 +00005218
aliguori2724b182009-03-05 23:01:47 +00005219 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005220 if (mon->reset_seen) {
5221 if (mon->suspend_cnt == 0) {
5222 monitor_printf(mon, "\n");
5223 }
5224 monitor_flush(mon);
5225 monitor_suspend(mon);
5226 } else {
5227 mon->suspend_cnt++;
5228 }
5229 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00005230 break;
5231
Amit Shahb6b8df52009-10-07 18:31:16 +05305232 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005233 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5234 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005235 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00005236 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005237 }
5238 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005239 mon_refcount++;
5240 break;
5241
5242 case CHR_EVENT_CLOSED:
5243 mon_refcount--;
5244 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005245 break;
5246 }
ths86e94de2007-01-05 22:01:59 +00005247}
5248
Wayne Xia816f8922011-10-12 11:32:41 +08005249static int
5250compare_mon_cmd(const void *a, const void *b)
5251{
5252 return strcmp(((const mon_cmd_t *)a)->name,
5253 ((const mon_cmd_t *)b)->name);
5254}
5255
5256static void sortcmdlist(void)
5257{
5258 int array_num;
5259 int elem_size = sizeof(mon_cmd_t);
5260
5261 array_num = sizeof(mon_cmds)/elem_size-1;
5262 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5263
5264 array_num = sizeof(info_cmds)/elem_size-1;
5265 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5266}
5267
aliguori76655d62009-03-06 20:27:37 +00005268
5269/*
5270 * Local variables:
5271 * c-indent-level: 4
5272 * c-basic-offset: 4
5273 * tab-width: 8
5274 * End:
5275 */
5276
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005277/* These functions just adapt the readline interface in a typesafe way. We
5278 * could cast function pointers but that discards compiler checks.
5279 */
Stefan Weild5d15072014-01-25 18:18:23 +01005280static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5281 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005282{
5283 va_list ap;
5284 va_start(ap, fmt);
5285 monitor_vprintf(opaque, fmt, ap);
5286 va_end(ap);
5287}
5288
5289static void monitor_readline_flush(void *opaque)
5290{
5291 monitor_flush(opaque);
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) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01005300 monitor_protocol_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005301 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005302 is_first_init = 0;
5303 }
aliguori87127162009-03-05 23:01:29 +00005304
Wenchao Xiab01fe892013-08-27 20:38:19 +08005305 mon = g_malloc(sizeof(*mon));
5306 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005307
aliguori87127162009-03-05 23:01:29 +00005308 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005309 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005310 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005311 mon->rs = readline_init(monitor_readline_printf,
5312 monitor_readline_flush,
5313 mon,
5314 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005315 monitor_read_command(mon, 0);
5316 }
aliguori87127162009-03-05 23:01:29 +00005317
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005318 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005319 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005320 /* Control mode requires special handlers */
5321 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5322 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005323 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05005324
5325 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005326 } else {
5327 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5328 monitor_event, mon);
5329 }
aliguori87127162009-03-05 23:01:29 +00005330
Blue Swirl72cf2d42009-09-12 07:36:22 +00005331 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005332 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5333 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005334}
5335
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005336static void bdrv_password_cb(void *opaque, const char *password,
5337 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005338{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005339 Monitor *mon = opaque;
5340 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005341 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005342
aliguoribb5fc202009-03-05 23:01:15 +00005343 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005344 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005345 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005346 }
aliguori731b0362009-03-05 23:01:42 +00005347 if (mon->password_completion_cb)
5348 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005349
aliguori731b0362009-03-05 23:01:42 +00005350 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005351}
aliguoric0f4ce72009-03-05 23:01:01 +00005352
Anthony Liguori7060b472011-09-02 12:34:50 -05005353ReadLineState *monitor_get_rs(Monitor *mon)
5354{
5355 return mon->rs;
5356}
5357
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005358int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5359 BlockDriverCompletionFunc *completion_cb,
5360 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005361{
aliguoricde76ee2009-03-05 23:01:51 +00005362 int err;
5363
aliguoribb5fc202009-03-05 23:01:15 +00005364 if (!bdrv_key_required(bs)) {
5365 if (completion_cb)
5366 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005367 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005368 }
aliguoric0f4ce72009-03-05 23:01:01 +00005369
Luiz Capitulino94171e12009-12-07 21:37:00 +01005370 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02005371 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
5372 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005373 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005374 }
5375
aliguori376253e2009-03-05 23:01:23 +00005376 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5377 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005378
aliguori731b0362009-03-05 23:01:42 +00005379 mon->password_completion_cb = completion_cb;
5380 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005381
aliguoricde76ee2009-03-05 23:01:51 +00005382 err = monitor_read_password(mon, bdrv_password_cb, bs);
5383
5384 if (err && completion_cb)
5385 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005386
5387 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005388}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005389
5390int monitor_read_block_device_key(Monitor *mon, const char *device,
5391 BlockDriverCompletionFunc *completion_cb,
5392 void *opaque)
5393{
5394 BlockDriverState *bs;
5395
5396 bs = bdrv_find(device);
5397 if (!bs) {
5398 monitor_printf(mon, "Device not found %s\n", device);
5399 return -1;
5400 }
5401
5402 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
5403}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005404
5405QemuOptsList qemu_mon_opts = {
5406 .name = "mon",
5407 .implied_opt_name = "chardev",
5408 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5409 .desc = {
5410 {
5411 .name = "mode",
5412 .type = QEMU_OPT_STRING,
5413 },{
5414 .name = "chardev",
5415 .type = QEMU_OPT_STRING,
5416 },{
5417 .name = "default",
5418 .type = QEMU_OPT_BOOL,
5419 },{
5420 .name = "pretty",
5421 .type = QEMU_OPT_BOOL,
5422 },
5423 { /* end of list */ }
5424 },
5425};