blob: 342e83baea8bedf89ee19e832e7d47f3aebaebc3 [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"
Anthony Liguori48a32be2011-09-02 12:34:48 -050069#include "qmp-commands.h"
70#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010071#include "qemu/thread.h"
ths6a5bd302007-12-03 17:05:38 +000072
Jan Kiszka661f1922011-10-16 11:53:13 +020073/* for pic/irq_info */
74#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010075#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020076#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010077#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020078
bellard9dc39cb2004-03-14 21:38:27 +000079//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000080//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000081
bellard9307c4c2004-04-04 12:57:25 +000082/*
83 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000084 *
bellard9307c4c2004-04-04 12:57:25 +000085 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000086 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000087 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080088 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010089 * 'O' option string of the form NAME=VALUE,...
90 * parsed according to QemuOptsList given by its name
91 * Example: 'device:O' uses qemu_device_opts.
92 * Restriction: only lists with empty desc are supported
93 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000094 * 'i' 32 bit integer
95 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030096 * 'M' Non-negative target long (32 or 64 bit), in user mode the
97 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020098 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +020099 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
100 * K, k suffix, which multiplies the value by 2^60 for suffixes E
101 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
102 * 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 +0100103 * 'T' double
104 * user mode accepts an optional ms, us, ns suffix,
105 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000106 * '/' optional gdb-like print format (like "/10x")
107 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300108 * '?' optional type (for all types, except '/')
109 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100110 * 'b' boolean
111 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300112 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000113 *
114 */
115
Adam Litke940cc302010-01-25 12:18:44 -0600116typedef struct MonitorCompletionData MonitorCompletionData;
117struct MonitorCompletionData {
118 Monitor *mon;
119 void (*user_print)(Monitor *mon, const QObject *data);
120};
121
Anthony Liguoric227f092009-10-01 16:12:16 -0500122typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000123 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000124 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000125 const char *params;
126 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300127 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300128 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300129 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200130 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600131 int (*cmd_async)(Monitor *mon, const QDict *params,
132 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300133 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200134 int flags;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800135 /* @sub_table is a list of 2nd level of commands. If it do not exist,
136 * mhandler should be used. If it exist, sub_table[?].mhandler should be
137 * used, and mhandler of 1st level plays the role of help function.
138 */
139 struct mon_cmd_t *sub_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000141
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500143typedef struct mon_fd_t mon_fd_t;
144struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145 char *name;
146 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500147 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100148};
149
Corey Bryantba1c0482012-08-14 16:43:43 -0400150/* file descriptor associated with a file descriptor set */
151typedef struct MonFdsetFd MonFdsetFd;
152struct MonFdsetFd {
153 int fd;
154 bool removed;
155 char *opaque;
156 QLIST_ENTRY(MonFdsetFd) next;
157};
158
159/* file descriptor set containing fds passed via SCM_RIGHTS */
160typedef struct MonFdset MonFdset;
161struct MonFdset {
162 int64_t id;
163 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400164 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400165 QLIST_ENTRY(MonFdset) next;
166};
167
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200168typedef struct MonitorControl {
169 QObject *id;
170 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200171 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200172} MonitorControl;
173
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100174/*
175 * To prevent flooding clients, events can be throttled. The
176 * throttling is calculated globally, rather than per-Monitor
177 * instance.
178 */
179typedef struct MonitorEventState {
180 MonitorEvent event; /* Event being tracked */
181 int64_t rate; /* Period over which to throttle. 0 to disable */
182 int64_t last; /* Time at which event was last emitted */
183 QEMUTimer *timer; /* Timer for handling delayed events */
184 QObject *data; /* Event pending delayed dispatch */
185} MonitorEventState;
186
aliguori87127162009-03-05 23:01:29 +0000187struct Monitor {
188 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200189 int mux_out;
190 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000191 int flags;
192 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400193 bool skip_flush;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400194 QString *outbuf;
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200195 guint watch;
aliguori731b0362009-03-05 23:01:42 +0000196 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200197 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200198 CPUState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000199 BlockDriverCompletionFunc *password_completion_cb;
200 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800201 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200202 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500203 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000204 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000205};
206
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300207/* QMP checker flags */
208#define QMP_ACCEPT_UNKNOWNS 1
209
Blue Swirl72cf2d42009-09-12 07:36:22 +0000210static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400211static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400212static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000213
Wayne Xia816f8922011-10-12 11:32:41 +0800214static mon_cmd_t mon_cmds[];
215static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000216
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300217static const mon_cmd_t qmp_cmds[];
218
Markus Armbruster8631b602010-02-18 11:41:55 +0100219Monitor *cur_mon;
220Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000221
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100222static void monitor_command_cb(void *opaque, const char *cmdline,
223 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000224
Luiz Capitulino09069b12010-02-04 18:10:06 -0200225static inline int qmp_cmd_mode(const Monitor *mon)
226{
227 return (mon->mc ? mon->mc->command_mode : 0);
228}
229
Luiz Capitulino418173c2009-11-26 22:58:51 -0200230/* Return true if in control mode, false otherwise */
231static inline int monitor_ctrl_mode(const Monitor *mon)
232{
233 return (mon->flags & MONITOR_USE_CONTROL);
234}
235
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100236/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
237int monitor_cur_is_qmp(void)
238{
239 return cur_mon && monitor_ctrl_mode(cur_mon);
240}
241
Anthony Liguori7060b472011-09-02 12:34:50 -0500242void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000243{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200244 if (!mon->rs)
245 return;
246
aliguori731b0362009-03-05 23:01:42 +0000247 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
248 if (show_prompt)
249 readline_show_prompt(mon->rs);
250}
bellard6a00d602005-11-21 23:25:50 +0000251
Anthony Liguori7060b472011-09-02 12:34:50 -0500252int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
253 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000254{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100255 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100256 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100257 return -EINVAL;
258 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000259 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
260 /* prompt is printed on return from the command handler */
261 return 0;
262 } else {
263 monitor_printf(mon, "terminal does not support password prompting\n");
264 return -ENOTTY;
265 }
aliguoribb5fc202009-03-05 23:01:15 +0000266}
267
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100268static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
269 void *opaque)
270{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200271 Monitor *mon = opaque;
272
273 mon->watch = 0;
274 monitor_flush(mon);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100275 return FALSE;
276}
277
aliguori376253e2009-03-05 23:01:23 +0000278void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000279{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400281 size_t len;
282 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100283
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400284 if (mon->skip_flush) {
285 return;
286 }
287
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400288 buf = qstring_get_str(mon->outbuf);
289 len = qstring_get_length(mon->outbuf);
290
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200291 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400292 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200293 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
294 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400295 QDECREF(mon->outbuf);
296 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100297 return;
298 }
299 if (rc > 0) {
300 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 QString *tmp = qstring_from_str(buf + rc);
302 QDECREF(mon->outbuf);
303 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100304 }
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200305 if (mon->watch == 0) {
306 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
307 monitor_unblocked, mon);
308 }
bellard7e2515e2004-08-01 21:52:19 +0000309 }
310}
311
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400312/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000313static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000314{
ths60fe76f2007-12-16 03:02:09 +0000315 char c;
aliguori731b0362009-03-05 23:01:42 +0000316
bellard7e2515e2004-08-01 21:52:19 +0000317 for(;;) {
318 c = *str++;
319 if (c == '\0')
320 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400321 if (c == '\n') {
322 qstring_append_chr(mon->outbuf, '\r');
323 }
324 qstring_append_chr(mon->outbuf, c);
325 if (c == '\n') {
aliguori376253e2009-03-05 23:01:23 +0000326 monitor_flush(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400327 }
bellard7e2515e2004-08-01 21:52:19 +0000328 }
329}
330
aliguori376253e2009-03-05 23:01:23 +0000331void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000332{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400333 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200334
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200335 if (!mon)
336 return;
337
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200338 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200339 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200340 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200341
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200343 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400344 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000345}
346
aliguori376253e2009-03-05 23:01:23 +0000347void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000348{
349 va_list ap;
350 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000351 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000352 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000353}
354
aliguori376253e2009-03-05 23:01:23 +0000355void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000356{
357 int i;
358
359 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000360 switch (filename[i]) {
361 case ' ':
362 case '"':
363 case '\\':
364 monitor_printf(mon, "\\%c", filename[i]);
365 break;
366 case '\t':
367 monitor_printf(mon, "\\t");
368 break;
369 case '\r':
370 monitor_printf(mon, "\\r");
371 break;
372 case '\n':
373 monitor_printf(mon, "\\n");
374 break;
375 default:
376 monitor_printf(mon, "%c", filename[i]);
377 break;
378 }
thsfef30742006-12-22 14:11:32 +0000379 }
380}
381
Stefan Weil8b7968f2010-09-23 21:28:05 +0200382static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
383 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000384{
385 va_list ap;
386 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000387 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000388 va_end(ap);
389 return 0;
390}
391
Luiz Capitulino13c74252009-10-07 13:41:55 -0300392static void monitor_user_noop(Monitor *mon, const QObject *data) { }
393
Luiz Capitulino9e807212010-09-16 10:58:59 -0300394static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300395{
396 return cmd->user_print != NULL;
397}
398
Luiz Capitulino4903de02010-09-16 11:01:32 -0300399static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600400{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200401 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600402}
403
Luiz Capitulino8204a912009-11-18 23:05:31 -0200404static inline int monitor_has_error(const Monitor *mon)
405{
406 return mon->error != NULL;
407}
408
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200409static void monitor_json_emitter(Monitor *mon, const QObject *data)
410{
411 QString *json;
412
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200413 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
414 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200415 assert(json != NULL);
416
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200417 qstring_append_chr(json, '\n');
418 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200419
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200420 QDECREF(json);
421}
422
Luiz Capitulinode253f12012-07-27 16:18:16 -0300423static QDict *build_qmp_error_dict(const QError *err)
424{
425 QObject *obj;
426
427 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
428 ErrorClass_lookup[err->err_class],
429 qerror_human(err));
430
431 return qobject_to_qdict(obj);
432}
433
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200434static void monitor_protocol_emitter(Monitor *mon, QObject *data)
435{
436 QDict *qmp;
437
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100438 trace_monitor_protocol_emitter(mon);
439
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200440 if (!monitor_has_error(mon)) {
441 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300442 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200443 if (data) {
444 qobject_incref(data);
445 qdict_put_obj(qmp, "return", data);
446 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200447 /* return an empty QDict by default */
448 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200449 }
450 } else {
451 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300452 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200453 QDECREF(mon->error);
454 mon->error = NULL;
455 }
456
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200457 if (mon->mc->id) {
458 qdict_put_obj(qmp, "id", mon->mc->id);
459 mon->mc->id = NULL;
460 }
461
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200462 monitor_json_emitter(mon, QOBJECT(qmp));
463 QDECREF(qmp);
464}
465
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200466static void timestamp_put(QDict *qdict)
467{
468 int err;
469 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000470 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200471
Blue Swirld08d6f02009-12-04 18:06:39 +0000472 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200473 if (err < 0)
474 return;
475
476 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
477 "'microseconds': %" PRId64 " }",
478 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200479 qdict_put_obj(qdict, "timestamp", obj);
480}
481
Daniel P. Berrange48608532012-05-21 17:59:51 +0100482
483static const char *monitor_event_names[] = {
484 [QEVENT_SHUTDOWN] = "SHUTDOWN",
485 [QEVENT_RESET] = "RESET",
486 [QEVENT_POWERDOWN] = "POWERDOWN",
487 [QEVENT_STOP] = "STOP",
488 [QEVENT_RESUME] = "RESUME",
489 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
490 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
491 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
492 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
493 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
494 [QEVENT_WATCHDOG] = "WATCHDOG",
495 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
496 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
497 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
498 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
499 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
Paolo Bonzini32c81a42012-09-28 17:22:58 +0200500 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
Paolo Bonzinia66a2a32012-07-23 15:15:47 +0200501 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
Michael S. Tsirkin0402a5d2013-03-06 14:58:59 +0200502 [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100503 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
Amos Kongb1be4282013-06-14 15:45:52 +0800504 [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100505 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300506 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100507 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100508 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300509 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
Hu Taoc401a8a2013-04-26 11:24:41 +0800510 [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED",
Max Reitza40f1c22013-08-30 14:34:25 +0200511 [QEVENT_BLOCK_IMAGE_CORRUPTED] = "BLOCK_IMAGE_CORRUPTED",
Benoît Canet95c6bff2014-02-21 22:21:15 +0100512 [QEVENT_QUORUM_FAILURE] = "QUORUM_FAILURE",
513 [QEVENT_QUORUM_REPORT_BAD] = "QUORUM_REPORT_BAD",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100514};
515QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
516
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100517MonitorEventState monitor_event_state[QEVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518
519/*
520 * Emits the event to every monitor instance
521 */
522static void
523monitor_protocol_event_emit(MonitorEvent event,
524 QObject *data)
525{
526 Monitor *mon;
527
528 trace_monitor_protocol_event_emit(event, data);
529 QLIST_FOREACH(mon, &mon_list, entry) {
530 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
531 monitor_json_emitter(mon, data);
532 }
533 }
534}
535
536
537/*
538 * Queue a new event for emission to Monitor instances,
539 * applying any rate limiting if required.
540 */
541static void
542monitor_protocol_event_queue(MonitorEvent event,
543 QObject *data)
544{
545 MonitorEventState *evstate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100546 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100547 assert(event < QEVENT_MAX);
548
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 evstate = &(monitor_event_state[event]);
550 trace_monitor_protocol_event_queue(event,
551 data,
552 evstate->rate,
553 evstate->last,
554 now);
555
556 /* Rate limit of 0 indicates no throttling */
557 if (!evstate->rate) {
558 monitor_protocol_event_emit(event, data);
559 evstate->last = now;
560 } else {
561 int64_t delta = now - evstate->last;
562 if (evstate->data ||
563 delta < evstate->rate) {
564 /* If there's an existing event pending, replace
565 * it with the new event, otherwise schedule a
566 * timer for delayed emission
567 */
568 if (evstate->data) {
569 qobject_decref(evstate->data);
570 } else {
571 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100572 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100573 }
574 evstate->data = data;
575 qobject_incref(evstate->data);
576 } else {
577 monitor_protocol_event_emit(event, data);
578 evstate->last = now;
579 }
580 }
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100581}
582
583
584/*
585 * The callback invoked by QemuTimer when a delayed
586 * event is ready to be emitted
587 */
588static void monitor_protocol_event_handler(void *opaque)
589{
590 MonitorEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100591 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100592
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100593
594 trace_monitor_protocol_event_handler(evstate->event,
595 evstate->data,
596 evstate->last,
597 now);
598 if (evstate->data) {
599 monitor_protocol_event_emit(evstate->event, evstate->data);
600 qobject_decref(evstate->data);
601 evstate->data = NULL;
602 }
603 evstate->last = now;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100604}
605
606
607/*
608 * @event: the event ID to be limited
609 * @rate: the rate limit in milliseconds
610 *
611 * Sets a rate limit on a particular event, so no
612 * more than 1 event will be emitted within @rate
613 * milliseconds
614 */
615static void
616monitor_protocol_event_throttle(MonitorEvent event,
617 int64_t rate)
618{
619 MonitorEventState *evstate;
620 assert(event < QEVENT_MAX);
621
622 evstate = &(monitor_event_state[event]);
623
624 trace_monitor_protocol_event_throttle(event, rate);
625 evstate->event = event;
626 evstate->rate = rate * SCALE_MS;
Alex Blighbc72ad62013-08-21 16:03:08 +0100627 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100628 SCALE_MS,
629 monitor_protocol_event_handler,
630 evstate);
631 evstate->last = 0;
632 evstate->data = NULL;
633}
634
635
636/* Global, one-time initializer to configure the rate limiting
637 * and initialize state */
638static void monitor_protocol_event_init(void)
639{
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100640 /* Limit RTC & BALLOON events to 1 per second */
641 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
642 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
643 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
Benoît Canetc88a1de2014-02-21 22:21:20 +0100644 /* limit the rate of quorum events to avoid hammering the management */
645 monitor_protocol_event_throttle(QEVENT_QUORUM_REPORT_BAD, 1000);
646 monitor_protocol_event_throttle(QEVENT_QUORUM_FAILURE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100647}
648
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200649/**
650 * monitor_protocol_event(): Generate a Monitor event
651 *
652 * Event-specific data can be emitted through the (optional) 'data' parameter.
653 */
654void monitor_protocol_event(MonitorEvent event, QObject *data)
655{
656 QDict *qmp;
657 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200658
Blue Swirl242cd002009-12-04 18:05:45 +0000659 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200660
Daniel P. Berrange48608532012-05-21 17:59:51 +0100661 event_name = monitor_event_names[event];
662 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200663
664 qmp = qdict_new();
665 timestamp_put(qmp);
666 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200667 if (data) {
668 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200669 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200670 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200671
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100672 trace_monitor_protocol_event(event, event_name, qmp);
673 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200674 QDECREF(qmp);
675}
676
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200677static int do_qmp_capabilities(Monitor *mon, const QDict *params,
678 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200679{
680 /* Will setup QMP capabilities in the future */
681 if (monitor_ctrl_mode(mon)) {
682 mon->mc->command_mode = 1;
683 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200684
685 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200686}
687
Luiz Capitulino0268d972010-10-22 10:08:02 -0200688static void handle_user_command(Monitor *mon, const char *cmdline);
689
Wenchao Xiab01fe892013-08-27 20:38:19 +0800690static void monitor_data_init(Monitor *mon)
691{
692 memset(mon, 0, sizeof(Monitor));
693 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800694 /* Use *mon_cmds by default. */
695 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800696}
697
698static void monitor_data_destroy(Monitor *mon)
699{
700 QDECREF(mon->outbuf);
701}
702
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200703char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
704 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200705{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200706 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200707 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200708
Wenchao Xiab01fe892013-08-27 20:38:19 +0800709 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400710 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200711
712 old_mon = cur_mon;
713 cur_mon = &hmp;
714
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200715 if (has_cpu_index) {
716 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200717 if (ret < 0) {
718 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200719 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
720 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200721 goto out;
722 }
723 }
724
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200725 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200726 cur_mon = old_mon;
727
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400728 if (qstring_get_length(hmp.outbuf) > 0) {
729 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200730 } else {
731 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200732 }
733
734out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800735 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200736 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200737}
738
bellard9dc39cb2004-03-14 21:38:27 +0000739static int compare_cmd(const char *name, const char *list)
740{
741 const char *p, *pstart;
742 int len;
743 len = strlen(name);
744 p = list;
745 for(;;) {
746 pstart = p;
747 p = strchr(p, '|');
748 if (!p)
749 p = pstart + strlen(pstart);
750 if ((p - pstart) == len && !memcmp(pstart, name, len))
751 return 1;
752 if (*p == '\0')
753 break;
754 p++;
755 }
756 return 0;
757}
758
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800759static int get_str(char *buf, int buf_size, const char **pp)
760{
761 const char *p;
762 char *q;
763 int c;
764
765 q = buf;
766 p = *pp;
767 while (qemu_isspace(*p)) {
768 p++;
769 }
770 if (*p == '\0') {
771 fail:
772 *q = '\0';
773 *pp = p;
774 return -1;
775 }
776 if (*p == '\"') {
777 p++;
778 while (*p != '\0' && *p != '\"') {
779 if (*p == '\\') {
780 p++;
781 c = *p++;
782 switch (c) {
783 case 'n':
784 c = '\n';
785 break;
786 case 'r':
787 c = '\r';
788 break;
789 case '\\':
790 case '\'':
791 case '\"':
792 break;
793 default:
794 qemu_printf("unsupported escape code: '\\%c'\n", c);
795 goto fail;
796 }
797 if ((q - buf) < buf_size - 1) {
798 *q++ = c;
799 }
800 } else {
801 if ((q - buf) < buf_size - 1) {
802 *q++ = *p;
803 }
804 p++;
805 }
806 }
807 if (*p != '\"') {
808 qemu_printf("unterminated string\n");
809 goto fail;
810 }
811 p++;
812 } else {
813 while (*p != '\0' && !qemu_isspace(*p)) {
814 if ((q - buf) < buf_size - 1) {
815 *q++ = *p;
816 }
817 p++;
818 }
819 }
820 *q = '\0';
821 *pp = p;
822 return 0;
823}
824
825#define MAX_ARGS 16
826
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800827static void free_cmdline_args(char **args, int nb_args)
828{
829 int i;
830
831 assert(nb_args <= MAX_ARGS);
832
833 for (i = 0; i < nb_args; i++) {
834 g_free(args[i]);
835 }
836
837}
838
839/*
840 * Parse the command line to get valid args.
841 * @cmdline: command line to be parsed.
842 * @pnb_args: location to store the number of args, must NOT be NULL.
843 * @args: location to store the args, which should be freed by caller, must
844 * NOT be NULL.
845 *
846 * Returns 0 on success, negative on failure.
847 *
848 * NOTE: this parser is an approximate form of the real command parser. Number
849 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
850 * return with failure.
851 */
852static int parse_cmdline(const char *cmdline,
853 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800854{
855 const char *p;
856 int nb_args, ret;
857 char buf[1024];
858
859 p = cmdline;
860 nb_args = 0;
861 for (;;) {
862 while (qemu_isspace(*p)) {
863 p++;
864 }
865 if (*p == '\0') {
866 break;
867 }
868 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800869 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800870 }
871 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800872 if (ret < 0) {
873 goto fail;
874 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800875 args[nb_args] = g_strdup(buf);
876 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800877 }
878 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800879 return 0;
880
881 fail:
882 free_cmdline_args(args, nb_args);
883 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800884}
885
Wenchao Xia66855492013-08-27 20:38:23 +0800886static void help_cmd_dump_one(Monitor *mon,
887 const mon_cmd_t *cmd,
888 char **prefix_args,
889 int prefix_args_nb)
890{
891 int i;
892
893 for (i = 0; i < prefix_args_nb; i++) {
894 monitor_printf(mon, "%s ", prefix_args[i]);
895 }
896 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
897}
898
899/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500900static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800901 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000902{
Anthony Liguoric227f092009-10-01 16:12:16 -0500903 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000904
Wenchao Xia66855492013-08-27 20:38:23 +0800905 /* No valid arg need to compare with, dump all in *cmds */
906 if (arg_index >= nb_args) {
907 for (cmd = cmds; cmd->name != NULL; cmd++) {
908 help_cmd_dump_one(mon, cmd, args, arg_index);
909 }
910 return;
911 }
912
913 /* Find one entry to dump */
914 for (cmd = cmds; cmd->name != NULL; cmd++) {
915 if (compare_cmd(args[arg_index], cmd->name)) {
916 if (cmd->sub_table) {
917 /* continue with next arg */
918 help_cmd_dump(mon, cmd->sub_table,
919 args, nb_args, arg_index + 1);
920 } else {
921 help_cmd_dump_one(mon, cmd, args, arg_index);
922 }
923 break;
924 }
bellard9dc39cb2004-03-14 21:38:27 +0000925 }
926}
927
aliguori376253e2009-03-05 23:01:23 +0000928static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000929{
Wenchao Xia66855492013-08-27 20:38:23 +0800930 char *args[MAX_ARGS];
931 int nb_args = 0;
932
933 /* 1. parse user input */
934 if (name) {
935 /* special case for log, directly dump and return */
936 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000937 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000938 monitor_printf(mon, "Log items (comma separated):\n");
939 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000940 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000941 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000942 }
Wenchao Xia66855492013-08-27 20:38:23 +0800943 return;
944 }
945
946 if (parse_cmdline(name, &nb_args, args) < 0) {
947 return;
bellardf193c792004-03-21 17:06:25 +0000948 }
bellard9dc39cb2004-03-14 21:38:27 +0000949 }
Wenchao Xia66855492013-08-27 20:38:23 +0800950
951 /* 2. dump the contents according to parsed args */
952 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
953
954 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000955}
956
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300957static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300958{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300959 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300960}
961
Lluísfc764102011-08-31 20:31:18 +0200962static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530963{
964 const char *tp_name = qdict_get_str(qdict, "name");
965 bool new_state = qdict_get_bool(qdict, "option");
Blue Swirlf871d682010-10-13 19:14:29 +0000966
Lluís Vilanovaca285c32013-03-05 14:47:43 +0100967 bool found = false;
968 TraceEvent *ev = NULL;
969 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
970 found = true;
971 if (!trace_event_get_state_static(ev)) {
972 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
973 } else {
974 trace_event_set_state_dynamic(ev, new_state);
975 }
976 }
977 if (!trace_event_is_pattern(tp_name) && !found) {
Blue Swirlf871d682010-10-13 19:14:29 +0000978 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
979 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530980}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100981
Michael Rothc45a8162011-10-02 08:44:37 -0500982#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100983static void do_trace_file(Monitor *mon, const QDict *qdict)
984{
985 const char *op = qdict_get_try_str(qdict, "op");
986 const char *arg = qdict_get_try_str(qdict, "arg");
987
988 if (!op) {
989 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
990 } else if (!strcmp(op, "on")) {
991 st_set_trace_file_enabled(true);
992 } else if (!strcmp(op, "off")) {
993 st_set_trace_file_enabled(false);
994 } else if (!strcmp(op, "flush")) {
995 st_flush_trace_buffer();
996 } else if (!strcmp(op, "set")) {
997 if (arg) {
998 st_set_trace_file(arg);
999 }
1000 } else {
1001 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1002 help_cmd(mon, "trace-file");
1003 }
1004}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301005#endif
1006
Adam Litke940cc302010-01-25 12:18:44 -06001007static void user_monitor_complete(void *opaque, QObject *ret_data)
1008{
1009 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
1010
1011 if (ret_data) {
1012 data->user_print(data->mon, ret_data);
1013 }
1014 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001015 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -06001016}
1017
1018static void qmp_monitor_complete(void *opaque, QObject *ret_data)
1019{
1020 monitor_protocol_emitter(opaque, ret_data);
1021}
1022
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001023static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1024 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -06001025{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03001026 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -06001027}
1028
Adam Litke940cc302010-01-25 12:18:44 -06001029static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1030 const QDict *params)
1031{
1032 int ret;
1033
Anthony Liguori7267c092011-08-20 22:09:37 -05001034 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -06001035 cb_data->mon = mon;
1036 cb_data->user_print = cmd->user_print;
1037 monitor_suspend(mon);
1038 ret = cmd->mhandler.cmd_async(mon, params,
1039 user_monitor_complete, cb_data);
1040 if (ret < 0) {
1041 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -05001042 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -06001043 }
1044}
1045
Wenchao Xia84c44612013-01-14 14:06:29 +08001046static void do_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001047{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001048 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001049}
1050
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001051CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +00001052{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001053 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001054 const mon_cmd_t *cmd;
1055
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03001056 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -02001057 info = g_malloc0(sizeof(*info));
1058 info->value = g_malloc0(sizeof(*info->value));
1059 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001060
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001061 info->next = cmd_list;
1062 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001063 }
1064
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -03001065 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +00001066}
1067
Daniel P. Berrange48608532012-05-21 17:59:51 +01001068EventInfoList *qmp_query_events(Error **errp)
1069{
1070 EventInfoList *info, *ev_list = NULL;
1071 MonitorEvent e;
1072
1073 for (e = 0 ; e < QEVENT_MAX ; e++) {
1074 const char *event_name = monitor_event_names[e];
1075 assert(event_name != NULL);
1076 info = g_malloc0(sizeof(*info));
1077 info->value = g_malloc0(sizeof(*info->value));
1078 info->value->name = g_strdup(event_name);
1079
1080 info->next = ev_list;
1081 ev_list = info;
1082 }
1083
1084 return ev_list;
1085}
1086
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001087/* set the current CPU defined by the user */
1088int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001089{
Andreas Färber55e5c282012-12-17 06:18:02 +01001090 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001091
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001092 cpu = qemu_get_cpu(cpu_index);
1093 if (cpu == NULL) {
1094 return -1;
bellard6a00d602005-11-21 23:25:50 +00001095 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001096 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001097 return 0;
bellard6a00d602005-11-21 23:25:50 +00001098}
1099
Andreas Färber9349b4f2012-03-14 01:38:32 +01001100static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001101{
aliguori731b0362009-03-05 23:01:42 +00001102 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001103 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001104 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001105 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +02001106 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001107}
1108
Luiz Capitulino99b77962011-10-24 10:53:44 -02001109int monitor_get_cpu_index(void)
1110{
Andreas Färber55e5c282012-12-17 06:18:02 +01001111 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1112 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001113}
1114
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001115static void do_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001116{
Andreas Färber878096e2013-05-27 01:33:50 +02001117 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001118 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +00001119 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001120 cpu = ENV_GET_CPU(env);
1121 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001122}
1123
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001124static void do_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001125{
aliguori376253e2009-03-05 23:01:23 +00001126 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001127}
1128
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001129static void do_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001130{
1131 int i;
bellard7e2515e2004-08-01 21:52:19 +00001132 const char *str;
ths3b46e622007-09-17 08:09:54 +00001133
aliguoricde76ee2009-03-05 23:01:51 +00001134 if (!mon->rs)
1135 return;
bellard7e2515e2004-08-01 21:52:19 +00001136 i = 0;
1137 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001138 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001139 if (!str)
1140 break;
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001142 i++;
bellardaa455482004-04-04 13:07:25 +00001143 }
1144}
1145
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001146static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001147{
Andreas Färber878096e2013-05-27 01:33:50 +02001148 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001149 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001150
1151 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001152 cpu = ENV_GET_CPU(env);
1153 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001154}
j_mayer76a66252007-03-07 08:32:30 +00001155
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001156static void do_trace_print_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301157{
Lluísfc764102011-08-31 20:31:18 +02001158 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301159}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301160
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001161static int client_migrate_info(Monitor *mon, const QDict *qdict,
1162 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001163{
1164 const char *protocol = qdict_get_str(qdict, "protocol");
1165 const char *hostname = qdict_get_str(qdict, "hostname");
1166 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1167 int port = qdict_get_try_int(qdict, "port", -1);
1168 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1169 int ret;
1170
1171 if (strcmp(protocol, "spice") == 0) {
1172 if (!using_spice) {
1173 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1174 return -1;
1175 }
1176
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001177 if (port == -1 && tls_port == -1) {
1178 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1179 return -1;
1180 }
1181
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001182 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1183 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001184 if (ret != 0) {
1185 qerror_report(QERR_UNDEFINED_ERROR);
1186 return -1;
1187 }
1188 return 0;
1189 }
1190
1191 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1192 return -1;
1193}
1194
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001195static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001196{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001197 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001198}
1199
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001200static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001201{
1202 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001203 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001204
bellard9307c4c2004-04-04 12:57:25 +00001205 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001206 mask = 0;
1207 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001208 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001209 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001210 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001211 return;
1212 }
1213 }
Peter Maydell24537a02013-02-11 16:41:23 +00001214 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001215}
1216
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001217static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001218{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001219 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001220 if (!option || !strcmp(option, "on")) {
1221 singlestep = 1;
1222 } else if (!strcmp(option, "off")) {
1223 singlestep = 0;
1224 } else {
1225 monitor_printf(mon, "unexpected option %s\n", option);
1226 }
1227}
1228
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001229static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001230{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001231 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001232 if (!device)
1233 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1234 if (gdbserver_start(device) < 0) {
1235 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1236 device);
1237 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001238 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001239 } else {
aliguori59030a82009-04-05 18:43:41 +00001240 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1241 device);
bellard8a7ddc32004-03-31 19:00:16 +00001242 }
1243}
1244
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001245static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001246{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001247 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001248 if (select_watchdog_action(action) == -1) {
1249 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1250 }
1251}
1252
aliguori376253e2009-03-05 23:01:23 +00001253static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001254{
aliguori376253e2009-03-05 23:01:23 +00001255 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001256 switch(c) {
1257 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001258 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001259 break;
1260 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001261 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001262 break;
1263 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001264 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001265 break;
1266 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001268 break;
1269 default:
1270 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001272 } else {
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001274 }
1275 break;
1276 }
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001278}
1279
aliguori376253e2009-03-05 23:01:23 +00001280static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001281 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001282{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001283 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001284 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001285 uint8_t buf[16];
1286 uint64_t v;
1287
1288 if (format == 'i') {
1289 int flags;
1290 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001291 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001292#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001293 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001294 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001295 } else if (wsize == 4) {
1296 flags = 0;
1297 } else {
bellard6a15fd12006-04-12 21:07:07 +00001298 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001299 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001300 if (env) {
1301#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001302 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001303 (env->segs[R_CS].flags & DESC_L_MASK))
1304 flags = 2;
1305 else
1306#endif
1307 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1308 flags = 1;
1309 }
bellard4c27ba22004-04-25 18:05:08 +00001310 }
1311#endif
aliguori376253e2009-03-05 23:01:23 +00001312 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001313 return;
1314 }
1315
1316 len = wsize * count;
1317 if (wsize == 1)
1318 line_size = 8;
1319 else
1320 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001321 max_digits = 0;
1322
1323 switch(format) {
1324 case 'o':
1325 max_digits = (wsize * 8 + 2) / 3;
1326 break;
1327 default:
1328 case 'x':
1329 max_digits = (wsize * 8) / 4;
1330 break;
1331 case 'u':
1332 case 'd':
1333 max_digits = (wsize * 8 * 10 + 32) / 33;
1334 break;
1335 case 'c':
1336 wsize = 1;
1337 break;
1338 }
1339
1340 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001341 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001343 else
aliguori376253e2009-03-05 23:01:23 +00001344 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001345 l = len;
1346 if (l > line_size)
1347 l = line_size;
1348 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001349 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001350 } else {
bellard6a00d602005-11-21 23:25:50 +00001351 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001352 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001354 break;
1355 }
bellard9307c4c2004-04-04 12:57:25 +00001356 }
ths5fafdf22007-09-16 21:08:06 +00001357 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001358 while (i < l) {
1359 switch(wsize) {
1360 default:
1361 case 1:
1362 v = ldub_raw(buf + i);
1363 break;
1364 case 2:
1365 v = lduw_raw(buf + i);
1366 break;
1367 case 4:
bellard92a31b12005-02-10 22:00:52 +00001368 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001369 break;
1370 case 8:
1371 v = ldq_raw(buf + i);
1372 break;
1373 }
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001375 switch(format) {
1376 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001378 break;
1379 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001381 break;
1382 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 }
1392 i += wsize;
1393 }
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001395 addr += l;
1396 len -= l;
1397 }
1398}
1399
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001400static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001401{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001402 int count = qdict_get_int(qdict, "count");
1403 int format = qdict_get_int(qdict, "format");
1404 int size = qdict_get_int(qdict, "size");
1405 target_long addr = qdict_get_int(qdict, "addr");
1406
aliguori376253e2009-03-05 23:01:23 +00001407 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001408}
1409
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001410static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001411{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001412 int count = qdict_get_int(qdict, "count");
1413 int format = qdict_get_int(qdict, "format");
1414 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001415 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001416
aliguori376253e2009-03-05 23:01:23 +00001417 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001418}
1419
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001420static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001421{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001422 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001423 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001424
bellard9307c4c2004-04-04 12:57:25 +00001425 switch(format) {
1426 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001427 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001428 break;
1429 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001430 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001431 break;
1432 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001433 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001434 break;
1435 default:
1436 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001437 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001438 break;
1439 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001440 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001441 break;
1442 }
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001444}
1445
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001446static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001447{
1448 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001449 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001450 uint32_t start = qdict_get_int(qdict, "start");
1451 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001452
1453 sum = 0;
1454 for(addr = start; addr < (start + size); addr++) {
Edgar E. Iglesias2c174492013-12-17 14:05:40 +10001455 uint8_t val = ldub_phys(&address_space_memory, addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001456 /* BSD sum algorithm ('sum' Unix command) */
1457 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001458 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001459 }
aliguori376253e2009-03-05 23:01:23 +00001460 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001461}
1462
bellard13224a82006-07-14 22:03:35 +00001463static int mouse_button_state;
1464
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001465static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001466{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001467 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001468 const char *dx_str = qdict_get_str(qdict, "dx_str");
1469 const char *dy_str = qdict_get_str(qdict, "dy_str");
1470 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001471
bellard13224a82006-07-14 22:03:35 +00001472 dx = strtol(dx_str, NULL, 0);
1473 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001474 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1475 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1476
1477 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001478 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001479 if (dz != 0) {
1480 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1481 qemu_input_queue_btn(NULL, button, true);
1482 qemu_input_event_sync();
1483 qemu_input_queue_btn(NULL, button, false);
1484 }
1485 }
1486 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001487}
1488
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001489static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001490{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001491 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1492 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1493 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1494 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1495 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001496 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001497
1498 if (mouse_button_state == button_state) {
1499 return;
1500 }
1501 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1502 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001503 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001504}
1505
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001506static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001507{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001508 int size = qdict_get_int(qdict, "size");
1509 int addr = qdict_get_int(qdict, "addr");
1510 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001511 uint32_t val;
1512 int suffix;
1513
1514 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001515 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001516 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001517 addr++;
1518 }
1519 addr &= 0xffff;
1520
1521 switch(size) {
1522 default:
1523 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001524 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001525 suffix = 'b';
1526 break;
1527 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001528 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001529 suffix = 'w';
1530 break;
1531 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001532 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001533 suffix = 'l';
1534 break;
1535 }
aliguori376253e2009-03-05 23:01:23 +00001536 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1537 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001538}
bellarda3a91a32004-06-04 11:06:21 +00001539
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001540static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001541{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001542 int size = qdict_get_int(qdict, "size");
1543 int addr = qdict_get_int(qdict, "addr");
1544 int val = qdict_get_int(qdict, "val");
1545
Jan Kiszkaf1147842009-07-14 10:20:11 +02001546 addr &= IOPORTS_MASK;
1547
1548 switch (size) {
1549 default:
1550 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001551 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001552 break;
1553 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001554 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001555 break;
1556 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001557 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001558 break;
1559 }
1560}
1561
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001563{
1564 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001565 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001566
Jan Kiszka76e30d02009-07-02 00:19:02 +02001567 res = qemu_boot_set(bootdevice);
1568 if (res == 0) {
1569 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1570 } else if (res > 0) {
1571 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001572 } else {
aliguori376253e2009-03-05 23:01:23 +00001573 monitor_printf(mon, "no function defined to set boot device list for "
1574 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001575 }
1576}
1577
bellardb86bda52004-09-18 19:32:46 +00001578#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001579static void print_pte(Monitor *mon, hwaddr addr,
1580 hwaddr pte,
1581 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001582{
Blue Swirld65aaf32010-12-11 18:56:24 +00001583#ifdef TARGET_X86_64
1584 if (addr & (1ULL << 47)) {
1585 addr |= -1LL << 48;
1586 }
1587#endif
1588 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1589 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001590 addr,
1591 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001592 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001593 pte & PG_GLOBAL_MASK ? 'G' : '-',
1594 pte & PG_PSE_MASK ? 'P' : '-',
1595 pte & PG_DIRTY_MASK ? 'D' : '-',
1596 pte & PG_ACCESSED_MASK ? 'A' : '-',
1597 pte & PG_PCD_MASK ? 'C' : '-',
1598 pte & PG_PWT_MASK ? 'T' : '-',
1599 pte & PG_USER_MASK ? 'U' : '-',
1600 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001601}
1602
Andreas Färber9349b4f2012-03-14 01:38:32 +01001603static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001604{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001605 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001606 uint32_t pgd, pde, pte;
1607
bellardb86bda52004-09-18 19:32:46 +00001608 pgd = env->cr[3] & ~0xfff;
1609 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001610 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001611 pde = le32_to_cpu(pde);
1612 if (pde & PG_PRESENT_MASK) {
1613 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001614 /* 4M pages */
1615 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001616 } else {
1617 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001618 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001619 pte = le32_to_cpu(pte);
1620 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001621 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001622 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001623 ~0xfff);
1624 }
1625 }
1626 }
1627 }
1628 }
1629}
1630
Andreas Färber9349b4f2012-03-14 01:38:32 +01001631static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001632{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001633 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001634 uint64_t pdpe, pde, pte;
1635 uint64_t pdp_addr, pd_addr, pt_addr;
1636
1637 pdp_addr = env->cr[3] & ~0x1f;
1638 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001639 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001640 pdpe = le64_to_cpu(pdpe);
1641 if (pdpe & PG_PRESENT_MASK) {
1642 pd_addr = pdpe & 0x3fffffffff000ULL;
1643 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001644 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001645 pde = le64_to_cpu(pde);
1646 if (pde & PG_PRESENT_MASK) {
1647 if (pde & PG_PSE_MASK) {
1648 /* 2M pages with PAE, CR4.PSE is ignored */
1649 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001650 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001651 } else {
1652 pt_addr = pde & 0x3fffffffff000ULL;
1653 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001654 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001655 pte = le64_to_cpu(pte);
1656 if (pte & PG_PRESENT_MASK) {
1657 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1658 + (l3 << 12),
1659 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001660 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001661 }
1662 }
1663 }
1664 }
1665 }
1666 }
1667 }
1668}
1669
1670#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001671static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001672{
1673 uint64_t l1, l2, l3, l4;
1674 uint64_t pml4e, pdpe, pde, pte;
1675 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1676
1677 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1678 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001679 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001680 pml4e = le64_to_cpu(pml4e);
1681 if (pml4e & PG_PRESENT_MASK) {
1682 pdp_addr = pml4e & 0x3fffffffff000ULL;
1683 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001684 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001685 pdpe = le64_to_cpu(pdpe);
1686 if (pdpe & PG_PRESENT_MASK) {
1687 if (pdpe & PG_PSE_MASK) {
1688 /* 1G pages, CR4.PSE is ignored */
1689 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1690 0x3ffffc0000000ULL);
1691 } else {
1692 pd_addr = pdpe & 0x3fffffffff000ULL;
1693 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001694 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001695 pde = le64_to_cpu(pde);
1696 if (pde & PG_PRESENT_MASK) {
1697 if (pde & PG_PSE_MASK) {
1698 /* 2M pages, CR4.PSE is ignored */
1699 print_pte(mon, (l1 << 39) + (l2 << 30) +
1700 (l3 << 21), pde,
1701 0x3ffffffe00000ULL);
1702 } else {
1703 pt_addr = pde & 0x3fffffffff000ULL;
1704 for (l4 = 0; l4 < 512; l4++) {
1705 cpu_physical_memory_read(pt_addr
1706 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001707 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001708 pte = le64_to_cpu(pte);
1709 if (pte & PG_PRESENT_MASK) {
1710 print_pte(mon, (l1 << 39) +
1711 (l2 << 30) +
1712 (l3 << 21) + (l4 << 12),
1713 pte & ~PG_PSE_MASK,
1714 0x3fffffffff000ULL);
1715 }
1716 }
1717 }
1718 }
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725}
1726#endif
1727
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001728static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001729{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001730 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001731
1732 env = mon_get_cpu();
1733
1734 if (!(env->cr[0] & CR0_PG_MASK)) {
1735 monitor_printf(mon, "PG disabled\n");
1736 return;
1737 }
1738 if (env->cr[4] & CR4_PAE_MASK) {
1739#ifdef TARGET_X86_64
1740 if (env->hflags & HF_LMA_MASK) {
1741 tlb_info_64(mon, env);
1742 } else
1743#endif
1744 {
1745 tlb_info_pae32(mon, env);
1746 }
1747 } else {
1748 tlb_info_32(mon, env);
1749 }
1750}
1751
Avi Kivitya8170e52012-10-23 12:30:10 +02001752static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001753 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001754 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001755{
bellard9746b152004-11-11 18:30:24 +00001756 int prot1;
1757 prot1 = *plast_prot;
1758 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001759 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001760 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1761 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001762 *pstart, end, end - *pstart,
1763 prot1 & PG_USER_MASK ? 'u' : '-',
1764 'r',
1765 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001766 }
1767 if (prot != 0)
1768 *pstart = end;
1769 else
1770 *pstart = -1;
1771 *plast_prot = prot;
1772 }
1773}
1774
Andreas Färber9349b4f2012-03-14 01:38:32 +01001775static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001776{
Austin Clementsb49ca722011-08-14 23:19:21 -04001777 unsigned int l1, l2;
1778 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001779 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001780 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001781
bellardb86bda52004-09-18 19:32:46 +00001782 pgd = env->cr[3] & ~0xfff;
1783 last_prot = 0;
1784 start = -1;
1785 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001786 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001787 pde = le32_to_cpu(pde);
1788 end = l1 << 22;
1789 if (pde & PG_PRESENT_MASK) {
1790 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1791 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001792 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001793 } else {
1794 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001795 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001796 pte = le32_to_cpu(pte);
1797 end = (l1 << 22) + (l2 << 12);
1798 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001799 prot = pte & pde &
1800 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001801 } else {
1802 prot = 0;
1803 }
aliguori376253e2009-03-05 23:01:23 +00001804 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001805 }
1806 }
1807 } else {
1808 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001809 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001810 }
1811 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001812 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001813 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001814}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001815
Andreas Färber9349b4f2012-03-14 01:38:32 +01001816static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001817{
Austin Clementsb49ca722011-08-14 23:19:21 -04001818 unsigned int l1, l2, l3;
1819 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001820 uint64_t pdpe, pde, pte;
1821 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001822 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001823
1824 pdp_addr = env->cr[3] & ~0x1f;
1825 last_prot = 0;
1826 start = -1;
1827 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001828 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001829 pdpe = le64_to_cpu(pdpe);
1830 end = l1 << 30;
1831 if (pdpe & PG_PRESENT_MASK) {
1832 pd_addr = pdpe & 0x3fffffffff000ULL;
1833 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001834 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001835 pde = le64_to_cpu(pde);
1836 end = (l1 << 30) + (l2 << 21);
1837 if (pde & PG_PRESENT_MASK) {
1838 if (pde & PG_PSE_MASK) {
1839 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1840 PG_PRESENT_MASK);
1841 mem_print(mon, &start, &last_prot, end, prot);
1842 } else {
1843 pt_addr = pde & 0x3fffffffff000ULL;
1844 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001845 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001846 pte = le64_to_cpu(pte);
1847 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1848 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001849 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1850 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001851 } else {
1852 prot = 0;
1853 }
1854 mem_print(mon, &start, &last_prot, end, prot);
1855 }
1856 }
1857 } else {
1858 prot = 0;
1859 mem_print(mon, &start, &last_prot, end, prot);
1860 }
1861 }
1862 } else {
1863 prot = 0;
1864 mem_print(mon, &start, &last_prot, end, prot);
1865 }
1866 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001867 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001868 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001869}
1870
1871
1872#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001873static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001874{
1875 int prot, last_prot;
1876 uint64_t l1, l2, l3, l4;
1877 uint64_t pml4e, pdpe, pde, pte;
1878 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1879
1880 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1881 last_prot = 0;
1882 start = -1;
1883 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001884 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001885 pml4e = le64_to_cpu(pml4e);
1886 end = l1 << 39;
1887 if (pml4e & PG_PRESENT_MASK) {
1888 pdp_addr = pml4e & 0x3fffffffff000ULL;
1889 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001890 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001891 pdpe = le64_to_cpu(pdpe);
1892 end = (l1 << 39) + (l2 << 30);
1893 if (pdpe & PG_PRESENT_MASK) {
1894 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001895 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1896 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001897 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001898 mem_print(mon, &start, &last_prot, end, prot);
1899 } else {
1900 pd_addr = pdpe & 0x3fffffffff000ULL;
1901 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001902 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001903 pde = le64_to_cpu(pde);
1904 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1905 if (pde & PG_PRESENT_MASK) {
1906 if (pde & PG_PSE_MASK) {
1907 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1908 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001909 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001910 mem_print(mon, &start, &last_prot, end, prot);
1911 } else {
1912 pt_addr = pde & 0x3fffffffff000ULL;
1913 for (l4 = 0; l4 < 512; l4++) {
1914 cpu_physical_memory_read(pt_addr
1915 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001916 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001917 pte = le64_to_cpu(pte);
1918 end = (l1 << 39) + (l2 << 30) +
1919 (l3 << 21) + (l4 << 12);
1920 if (pte & PG_PRESENT_MASK) {
1921 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1922 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001923 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001924 } else {
1925 prot = 0;
1926 }
1927 mem_print(mon, &start, &last_prot, end, prot);
1928 }
1929 }
1930 } else {
1931 prot = 0;
1932 mem_print(mon, &start, &last_prot, end, prot);
1933 }
1934 }
1935 }
1936 } else {
1937 prot = 0;
1938 mem_print(mon, &start, &last_prot, end, prot);
1939 }
1940 }
1941 } else {
1942 prot = 0;
1943 mem_print(mon, &start, &last_prot, end, prot);
1944 }
1945 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001946 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001947 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001948}
1949#endif
1950
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001951static void mem_info(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001952{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001953 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001954
1955 env = mon_get_cpu();
1956
1957 if (!(env->cr[0] & CR0_PG_MASK)) {
1958 monitor_printf(mon, "PG disabled\n");
1959 return;
1960 }
1961 if (env->cr[4] & CR4_PAE_MASK) {
1962#ifdef TARGET_X86_64
1963 if (env->hflags & HF_LMA_MASK) {
1964 mem_info_64(mon, env);
1965 } else
1966#endif
1967 {
1968 mem_info_pae32(mon, env);
1969 }
1970 } else {
1971 mem_info_32(mon, env);
1972 }
1973}
bellardb86bda52004-09-18 19:32:46 +00001974#endif
1975
aurel327c664e22009-03-03 06:12:22 +00001976#if defined(TARGET_SH4)
1977
aliguori376253e2009-03-05 23:01:23 +00001978static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001979{
aliguori376253e2009-03-05 23:01:23 +00001980 monitor_printf(mon, " tlb%i:\t"
1981 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1982 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1983 "dirty=%hhu writethrough=%hhu\n",
1984 idx,
1985 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1986 tlb->v, tlb->sh, tlb->c, tlb->pr,
1987 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001988}
1989
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001990static void tlb_info(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001991{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001992 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001993 int i;
1994
aliguori376253e2009-03-05 23:01:23 +00001995 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001996 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001997 print_tlb (mon, i, &env->itlb[i]);
1998 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001999 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002000 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002001}
2002
2003#endif
2004
Max Filippov692f7372012-01-07 20:02:40 +04002005#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002006static void tlb_info(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00002007{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002008 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00002009
2010 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2011}
2012#endif
2013
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002014static void do_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00002015{
2016 mtree_info((fprintf_function)monitor_printf, mon);
2017}
2018
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002019static void do_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00002020{
aliguorib28b6232009-04-22 20:20:29 +00002021 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002022 CPUState *cpu;
aliguori030ea372009-04-21 22:30:47 +00002023
2024 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2025 for (i = 0; i < nb_numa_nodes; i++) {
2026 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02002027 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01002028 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01002029 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00002030 }
2031 }
2032 monitor_printf(mon, "\n");
2033 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2034 node_mem[i] >> 20);
2035 }
2036}
2037
bellard5f1ce942006-02-08 22:40:15 +00002038#ifdef CONFIG_PROFILER
2039
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002040int64_t qemu_time;
2041int64_t dev_time;
2042
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002043static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002044{
aliguori376253e2009-03-05 23:01:23 +00002045 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002046 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002047 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002048 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002049 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002050 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002051}
2052#else
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002053static void do_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002054{
aliguori376253e2009-03-05 23:01:23 +00002055 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002056}
2057#endif
2058
bellardec36b692006-07-16 18:57:03 +00002059/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002060static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002061
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002062static void do_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002063{
2064 int i;
2065 CaptureState *s;
2066
2067 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002068 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002069 s->ops.info (s->opaque);
2070 }
2071}
2072
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002073static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002074{
2075 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002076 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002077 CaptureState *s;
2078
2079 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2080 if (i == n) {
2081 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002082 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002083 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002084 return;
2085 }
2086 }
2087}
2088
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002089static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002090{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002091 const char *path = qdict_get_str(qdict, "path");
2092 int has_freq = qdict_haskey(qdict, "freq");
2093 int freq = qdict_get_try_int(qdict, "freq", -1);
2094 int has_bits = qdict_haskey(qdict, "bits");
2095 int bits = qdict_get_try_int(qdict, "bits", -1);
2096 int has_channels = qdict_haskey(qdict, "nchannels");
2097 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002098 CaptureState *s;
2099
Anthony Liguori7267c092011-08-20 22:09:37 -05002100 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002101
2102 freq = has_freq ? freq : 44100;
2103 bits = has_bits ? bits : 16;
2104 nchannels = has_channels ? nchannels : 2;
2105
2106 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002107 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002108 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002109 return;
bellardec36b692006-07-16 18:57:03 +00002110 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002111 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002112}
bellardec36b692006-07-16 18:57:03 +00002113
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002114static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002115{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002116 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002117
aliguori76655d62009-03-06 20:27:37 +00002118 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002119 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002120 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121 return acl;
2122}
aliguori76655d62009-03-06 20:27:37 +00002123
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002124static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002125{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002126 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002127 qemu_acl *acl = find_acl(mon, aclname);
2128 qemu_acl_entry *entry;
2129 int i = 0;
2130
2131 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002132 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002133 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002134 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002135 i++;
2136 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002137 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002138 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139 }
2140}
2141
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002142static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002143{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002144 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002145 qemu_acl *acl = find_acl(mon, aclname);
2146
2147 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002148 qemu_acl_reset(acl);
2149 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002150 }
2151}
aliguori76655d62009-03-06 20:27:37 +00002152
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002153static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002154{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002155 const char *aclname = qdict_get_str(qdict, "aclname");
2156 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157 qemu_acl *acl = find_acl(mon, aclname);
2158
2159 if (acl) {
2160 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002161 acl->defaultDeny = 0;
2162 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002163 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002164 acl->defaultDeny = 1;
2165 monitor_printf(mon, "acl: policy set to 'deny'\n");
2166 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002167 monitor_printf(mon, "acl: unknown policy '%s', "
2168 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002169 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170 }
2171}
aliguori76655d62009-03-06 20:27:37 +00002172
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002173static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002174{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002175 const char *aclname = qdict_get_str(qdict, "aclname");
2176 const char *match = qdict_get_str(qdict, "match");
2177 const char *policy = qdict_get_str(qdict, "policy");
2178 int has_index = qdict_haskey(qdict, "index");
2179 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180 qemu_acl *acl = find_acl(mon, aclname);
2181 int deny, ret;
2182
2183 if (acl) {
2184 if (strcmp(policy, "allow") == 0) {
2185 deny = 0;
2186 } else if (strcmp(policy, "deny") == 0) {
2187 deny = 1;
2188 } else {
2189 monitor_printf(mon, "acl: unknown policy '%s', "
2190 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002191 return;
2192 }
aliguori28a76be2009-03-06 20:27:40 +00002193 if (has_index)
2194 ret = qemu_acl_insert(acl, deny, match, index);
2195 else
2196 ret = qemu_acl_append(acl, deny, match);
2197 if (ret < 0)
2198 monitor_printf(mon, "acl: unable to add acl entry\n");
2199 else
2200 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002201 }
2202}
aliguori76655d62009-03-06 20:27:37 +00002203
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002204static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002205{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002206 const char *aclname = qdict_get_str(qdict, "aclname");
2207 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002208 qemu_acl *acl = find_acl(mon, aclname);
2209 int ret;
aliguori76655d62009-03-06 20:27:37 +00002210
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002211 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002212 ret = qemu_acl_remove(acl, match);
2213 if (ret < 0)
2214 monitor_printf(mon, "acl: no matching acl entry\n");
2215 else
2216 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002217 }
2218}
2219
Huang Ying79c4f6b2009-06-23 10:05:14 +08002220#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002221static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002222{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002223 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002224 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002225 int cpu_index = qdict_get_int(qdict, "cpu_index");
2226 int bank = qdict_get_int(qdict, "bank");
2227 uint64_t status = qdict_get_int(qdict, "status");
2228 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2229 uint64_t addr = qdict_get_int(qdict, "addr");
2230 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002231 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002232
Jan Kiszka747461c2011-03-02 08:56:10 +01002233 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2234 flags |= MCE_INJECT_BROADCAST;
2235 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002236 cs = qemu_get_cpu(cpu_index);
2237 if (cs != NULL) {
2238 cpu = X86_CPU(cs);
2239 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2240 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002241 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002242}
2243#endif
2244
Corey Bryant208c9d12012-06-22 14:36:09 -04002245void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002246{
Anthony Liguoric227f092009-10-01 16:12:16 -05002247 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002248 int fd;
2249
Corey Bryant208c9d12012-06-22 14:36:09 -04002250 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002251 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002252 error_set(errp, QERR_FD_NOT_SUPPLIED);
2253 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002254 }
2255
2256 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002257 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2258 "a name not starting with a digit");
2259 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002260 }
2261
Corey Bryant208c9d12012-06-22 14:36:09 -04002262 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002263 if (strcmp(monfd->name, fdname) != 0) {
2264 continue;
2265 }
2266
2267 close(monfd->fd);
2268 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002269 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002270 }
2271
Anthony Liguori7267c092011-08-20 22:09:37 -05002272 monfd = g_malloc0(sizeof(mon_fd_t));
2273 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002274 monfd->fd = fd;
2275
Corey Bryant208c9d12012-06-22 14:36:09 -04002276 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002277}
2278
Corey Bryant208c9d12012-06-22 14:36:09 -04002279void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002280{
Anthony Liguoric227f092009-10-01 16:12:16 -05002281 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002282
Corey Bryant208c9d12012-06-22 14:36:09 -04002283 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002284 if (strcmp(monfd->name, fdname) != 0) {
2285 continue;
2286 }
2287
Blue Swirl72cf2d42009-09-12 07:36:22 +00002288 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002289 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002290 g_free(monfd->name);
2291 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002292 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002293 }
2294
Corey Bryant208c9d12012-06-22 14:36:09 -04002295 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002296}
2297
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002298static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002299{
Luiz Capitulino13548692011-07-29 15:36:43 -03002300 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002301 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002302
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002303 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002304
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002305 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002306 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002307 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002308}
2309
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002310int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002311{
Anthony Liguoric227f092009-10-01 16:12:16 -05002312 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002313
Blue Swirl72cf2d42009-09-12 07:36:22 +00002314 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002315 int fd;
2316
2317 if (strcmp(monfd->name, fdname) != 0) {
2318 continue;
2319 }
2320
2321 fd = monfd->fd;
2322
2323 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002324 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002325 g_free(monfd->name);
2326 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002327
2328 return fd;
2329 }
2330
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002331 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002332 return -1;
2333}
2334
Corey Bryantba1c0482012-08-14 16:43:43 -04002335static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2336{
2337 MonFdsetFd *mon_fdset_fd;
2338 MonFdsetFd *mon_fdset_fd_next;
2339
2340 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002341 if ((mon_fdset_fd->removed ||
2342 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2343 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002344 close(mon_fdset_fd->fd);
2345 g_free(mon_fdset_fd->opaque);
2346 QLIST_REMOVE(mon_fdset_fd, next);
2347 g_free(mon_fdset_fd);
2348 }
2349 }
2350
Corey Bryantadb696f2012-08-14 16:43:47 -04002351 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002352 QLIST_REMOVE(mon_fdset, next);
2353 g_free(mon_fdset);
2354 }
2355}
2356
Corey Bryantefb87c12012-08-14 16:43:48 -04002357static void monitor_fdsets_cleanup(void)
2358{
2359 MonFdset *mon_fdset;
2360 MonFdset *mon_fdset_next;
2361
2362 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2363 monitor_fdset_cleanup(mon_fdset);
2364 }
2365}
2366
Corey Bryantba1c0482012-08-14 16:43:43 -04002367AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2368 const char *opaque, Error **errp)
2369{
2370 int fd;
2371 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002372 AddfdInfo *fdinfo;
2373
2374 fd = qemu_chr_fe_get_msgfd(mon->chr);
2375 if (fd == -1) {
2376 error_set(errp, QERR_FD_NOT_SUPPLIED);
2377 goto error;
2378 }
2379
Corey Bryante446f702012-10-18 15:19:32 -04002380 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2381 has_opaque, opaque, errp);
2382 if (fdinfo) {
2383 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002384 }
2385
Corey Bryantba1c0482012-08-14 16:43:43 -04002386error:
2387 if (fd != -1) {
2388 close(fd);
2389 }
2390 return NULL;
2391}
2392
2393void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2394{
2395 MonFdset *mon_fdset;
2396 MonFdsetFd *mon_fdset_fd;
2397 char fd_str[60];
2398
2399 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2400 if (mon_fdset->id != fdset_id) {
2401 continue;
2402 }
2403 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2404 if (has_fd) {
2405 if (mon_fdset_fd->fd != fd) {
2406 continue;
2407 }
2408 mon_fdset_fd->removed = true;
2409 break;
2410 } else {
2411 mon_fdset_fd->removed = true;
2412 }
2413 }
2414 if (has_fd && !mon_fdset_fd) {
2415 goto error;
2416 }
2417 monitor_fdset_cleanup(mon_fdset);
2418 return;
2419 }
2420
2421error:
2422 if (has_fd) {
2423 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2424 fdset_id, fd);
2425 } else {
2426 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2427 }
2428 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2429}
2430
2431FdsetInfoList *qmp_query_fdsets(Error **errp)
2432{
2433 MonFdset *mon_fdset;
2434 MonFdsetFd *mon_fdset_fd;
2435 FdsetInfoList *fdset_list = NULL;
2436
2437 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2438 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2439 FdsetFdInfoList *fdsetfd_list = NULL;
2440
2441 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2442 fdset_info->value->fdset_id = mon_fdset->id;
2443
2444 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2445 FdsetFdInfoList *fdsetfd_info;
2446
2447 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2448 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2449 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2450 if (mon_fdset_fd->opaque) {
2451 fdsetfd_info->value->has_opaque = true;
2452 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2453 } else {
2454 fdsetfd_info->value->has_opaque = false;
2455 }
2456
2457 fdsetfd_info->next = fdsetfd_list;
2458 fdsetfd_list = fdsetfd_info;
2459 }
2460
2461 fdset_info->value->fds = fdsetfd_list;
2462
2463 fdset_info->next = fdset_list;
2464 fdset_list = fdset_info;
2465 }
2466
2467 return fdset_list;
2468}
2469
Corey Bryante446f702012-10-18 15:19:32 -04002470AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2471 bool has_opaque, const char *opaque,
2472 Error **errp)
2473{
2474 MonFdset *mon_fdset = NULL;
2475 MonFdsetFd *mon_fdset_fd;
2476 AddfdInfo *fdinfo;
2477
2478 if (has_fdset_id) {
2479 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2480 /* Break if match found or match impossible due to ordering by ID */
2481 if (fdset_id <= mon_fdset->id) {
2482 if (fdset_id < mon_fdset->id) {
2483 mon_fdset = NULL;
2484 }
2485 break;
2486 }
2487 }
2488 }
2489
2490 if (mon_fdset == NULL) {
2491 int64_t fdset_id_prev = -1;
2492 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2493
2494 if (has_fdset_id) {
2495 if (fdset_id < 0) {
2496 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2497 "a non-negative value");
2498 return NULL;
2499 }
2500 /* Use specified fdset ID */
2501 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2502 mon_fdset_cur = mon_fdset;
2503 if (fdset_id < mon_fdset_cur->id) {
2504 break;
2505 }
2506 }
2507 } else {
2508 /* Use first available fdset ID */
2509 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2510 mon_fdset_cur = mon_fdset;
2511 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2512 fdset_id_prev = mon_fdset_cur->id;
2513 continue;
2514 }
2515 break;
2516 }
2517 }
2518
2519 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2520 if (has_fdset_id) {
2521 mon_fdset->id = fdset_id;
2522 } else {
2523 mon_fdset->id = fdset_id_prev + 1;
2524 }
2525
2526 /* The fdset list is ordered by fdset ID */
2527 if (!mon_fdset_cur) {
2528 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2529 } else if (mon_fdset->id < mon_fdset_cur->id) {
2530 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2531 } else {
2532 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2533 }
2534 }
2535
2536 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2537 mon_fdset_fd->fd = fd;
2538 mon_fdset_fd->removed = false;
2539 if (has_opaque) {
2540 mon_fdset_fd->opaque = g_strdup(opaque);
2541 }
2542 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2543
2544 fdinfo = g_malloc0(sizeof(*fdinfo));
2545 fdinfo->fdset_id = mon_fdset->id;
2546 fdinfo->fd = mon_fdset_fd->fd;
2547
2548 return fdinfo;
2549}
2550
Corey Bryantadb696f2012-08-14 16:43:47 -04002551int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2552{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002553#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002554 MonFdset *mon_fdset;
2555 MonFdsetFd *mon_fdset_fd;
2556 int mon_fd_flags;
2557
Corey Bryantadb696f2012-08-14 16:43:47 -04002558 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2559 if (mon_fdset->id != fdset_id) {
2560 continue;
2561 }
2562 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2563 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2564 if (mon_fd_flags == -1) {
2565 return -1;
2566 }
2567
2568 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2569 return mon_fdset_fd->fd;
2570 }
2571 }
2572 errno = EACCES;
2573 return -1;
2574 }
2575#endif
2576
2577 errno = ENOENT;
2578 return -1;
2579}
2580
2581int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2582{
2583 MonFdset *mon_fdset;
2584 MonFdsetFd *mon_fdset_fd_dup;
2585
2586 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2587 if (mon_fdset->id != fdset_id) {
2588 continue;
2589 }
2590 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2591 if (mon_fdset_fd_dup->fd == dup_fd) {
2592 return -1;
2593 }
2594 }
2595 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2596 mon_fdset_fd_dup->fd = dup_fd;
2597 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2598 return 0;
2599 }
2600 return -1;
2601}
2602
2603static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2604{
2605 MonFdset *mon_fdset;
2606 MonFdsetFd *mon_fdset_fd_dup;
2607
2608 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2609 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2610 if (mon_fdset_fd_dup->fd == dup_fd) {
2611 if (remove) {
2612 QLIST_REMOVE(mon_fdset_fd_dup, next);
2613 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2614 monitor_fdset_cleanup(mon_fdset);
2615 }
2616 }
2617 return mon_fdset->id;
2618 }
2619 }
2620 }
2621 return -1;
2622}
2623
2624int monitor_fdset_dup_fd_find(int dup_fd)
2625{
2626 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2627}
2628
2629int monitor_fdset_dup_fd_remove(int dup_fd)
2630{
2631 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2632}
2633
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002634int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2635{
2636 int fd;
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002637 Error *local_err = NULL;
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002638
2639 if (!qemu_isdigit(fdname[0]) && mon) {
2640
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002641 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002642 if (fd == -1) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002643 qerror_report_err(local_err);
2644 error_free(local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002645 return -1;
2646 }
2647 } else {
2648 fd = qemu_parse_fd(fdname);
2649 }
2650
2651 return fd;
2652}
2653
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002654/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002655static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 {
2657 .name = "version",
2658 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 .params = "",
2660 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002661 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 },
2663 {
2664 .name = "network",
2665 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .params = "",
2667 .help = "show the network state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002668 .mhandler.cmd = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "chardev",
2672 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 .params = "",
2674 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002675 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 },
2677 {
2678 .name = "block",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002679 .args_type = "verbose:-v,device:B?",
2680 .params = "[-v] [device]",
2681 .help = "show info of one block device or all block devices "
2682 "(and details of images with -v option)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002683 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
2685 {
2686 .name = "blockstats",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002690 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002693 .name = "block-jobs",
2694 .args_type = "",
2695 .params = "",
2696 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002697 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002698 },
2699 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 .name = "registers",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show the cpu registers",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002704 .mhandler.cmd = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "cpus",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002711 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
2713 {
2714 .name = "history",
2715 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 .params = "",
2717 .help = "show the command line history",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002718 .mhandler.cmd = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002720#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2721 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 {
2723 .name = "irq",
2724 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 .params = "",
2726 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002727#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002728 .mhandler.cmd = sun4m_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002729#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002730 .mhandler.cmd = lm32_irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002731#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002732 .mhandler.cmd = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002733#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
2735 {
2736 .name = "pic",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002740#ifdef TARGET_SPARC
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002741 .mhandler.cmd = sun4m_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002742#elif defined(TARGET_LM32)
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002743 .mhandler.cmd = lm32_do_pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002744#else
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002745 .mhandler.cmd = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002746#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002748#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 {
2750 .name = "pci",
2751 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 .params = "",
2753 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002754 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002756#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002757 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 {
2759 .name = "tlb",
2760 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 .params = "",
2762 .help = "show virtual to physical memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002763 .mhandler.cmd = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 },
aurel327c664e22009-03-03 06:12:22 +00002765#endif
2766#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 {
2768 .name = "mem",
2769 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 .params = "",
2771 .help = "show the active virtual memory mappings",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002772 .mhandler.cmd = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 },
bellardb86bda52004-09-18 19:32:46 +00002774#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 {
Blue Swirl314e2982011-09-11 20:22:05 +00002776 .name = "mtree",
2777 .args_type = "",
2778 .params = "",
2779 .help = "show memory tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002780 .mhandler.cmd = do_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002781 },
2782 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 .name = "jit",
2784 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 .params = "",
2786 .help = "show dynamic compiler info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002787 .mhandler.cmd = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 },
2789 {
2790 .name = "kvm",
2791 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .params = "",
2793 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002794 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "numa",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show NUMA information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002801 .mhandler.cmd = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 },
2803 {
2804 .name = "usb",
2805 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 .params = "",
2807 .help = "show guest USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002808 .mhandler.cmd = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 },
2810 {
2811 .name = "usbhost",
2812 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 .params = "",
2814 .help = "show host USB devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002815 .mhandler.cmd = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 },
2817 {
2818 .name = "profile",
2819 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .params = "",
2821 .help = "show profiling information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002822 .mhandler.cmd = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
2824 {
2825 .name = "capture",
2826 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .params = "",
2828 .help = "show capture information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002829 .mhandler.cmd = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 },
2831 {
2832 .name = "snapshots",
2833 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 .params = "",
2835 .help = "show the currently saved VM snapshots",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002836 .mhandler.cmd = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002837 },
2838 {
2839 .name = "status",
2840 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 .params = "",
2842 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002843 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002844 },
2845 {
2846 .name = "pcmcia",
2847 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 .params = "",
2849 .help = "show guest PCMCIA status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002850 .mhandler.cmd = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002851 },
2852 {
2853 .name = "mice",
2854 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002855 .params = "",
2856 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002857 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002858 },
2859 {
2860 .name = "vnc",
2861 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002862 .params = "",
2863 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002864 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002865 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002866#if defined(CONFIG_SPICE)
2867 {
2868 .name = "spice",
2869 .args_type = "",
2870 .params = "",
2871 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002872 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002873 },
2874#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 {
2876 .name = "name",
2877 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 .params = "",
2879 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002880 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 },
2882 {
2883 .name = "uuid",
2884 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 .params = "",
2886 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002887 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002888 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 {
2890 .name = "cpustats",
2891 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 .params = "",
2893 .help = "show CPU statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002894 .mhandler.cmd = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 },
blueswir131a60e22007-10-26 18:42:59 +00002896#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002897 {
2898 .name = "usernet",
2899 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002900 .params = "",
2901 .help = "show user network stack connection states",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002902 .mhandler.cmd = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 },
blueswir131a60e22007-10-26 18:42:59 +00002904#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002905 {
2906 .name = "migrate",
2907 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002908 .params = "",
2909 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002910 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002911 },
2912 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002913 .name = "migrate_capabilities",
2914 .args_type = "",
2915 .params = "",
2916 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002917 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002918 },
2919 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002920 .name = "migrate_cache_size",
2921 .args_type = "",
2922 .params = "",
2923 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002924 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002925 },
2926 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002927 .name = "balloon",
2928 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002929 .params = "",
2930 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002931 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002932 },
2933 {
2934 .name = "qtree",
2935 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002936 .params = "",
2937 .help = "show device tree",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002938 .mhandler.cmd = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002939 },
2940 {
2941 .name = "qdm",
2942 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002943 .params = "",
2944 .help = "show qdev device model list",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002945 .mhandler.cmd = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002946 },
2947 {
2948 .name = "roms",
2949 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002950 .params = "",
2951 .help = "show roms",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002952 .mhandler.cmd = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002953 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302954 {
2955 .name = "trace-events",
2956 .args_type = "",
2957 .params = "",
2958 .help = "show available trace-events & their state",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002959 .mhandler.cmd = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302960 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002961 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002962 .name = "tpm",
2963 .args_type = "",
2964 .params = "",
2965 .help = "show the TPM device",
2966 .mhandler.cmd = hmp_info_tpm,
2967 },
2968 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002969 .name = NULL,
2970 },
bellard9dc39cb2004-03-14 21:38:27 +00002971};
2972
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002973/* mon_cmds and info_cmds would be sorted at runtime */
2974static mon_cmd_t mon_cmds[] = {
2975#include "hmp-commands.h"
2976 { NULL, NULL, },
2977};
2978
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002979static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002980#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002981 { /* NULL */ },
2982};
2983
bellard9307c4c2004-04-04 12:57:25 +00002984/*******************************************************************/
2985
2986static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002987static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002988
bellard92a31b12005-02-10 22:00:52 +00002989#define MD_TLONG 0
2990#define MD_I32 1
2991
bellard9307c4c2004-04-04 12:57:25 +00002992typedef struct MonitorDef {
2993 const char *name;
2994 int offset;
blueswir18662d652008-10-02 18:32:44 +00002995 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002996 int type;
bellard9307c4c2004-04-04 12:57:25 +00002997} MonitorDef;
2998
bellard57206fd2004-04-25 18:54:52 +00002999#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00003000static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00003001{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003002 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003003 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00003004}
3005#endif
3006
bellarda541f292004-04-12 20:39:29 +00003007#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00003008static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003009{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003010 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00003011 unsigned int u;
3012 int i;
3013
3014 u = 0;
3015 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00003016 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00003017
3018 return u;
3019}
3020
blueswir18662d652008-10-02 18:32:44 +00003021static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003022{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003023 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003024 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003025}
3026
blueswir18662d652008-10-02 18:32:44 +00003027static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003028{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003029 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003030 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003031}
bellard9fddaa02004-05-21 12:59:32 +00003032
blueswir18662d652008-10-02 18:32:44 +00003033static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003034{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003035 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003036 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003037}
3038
blueswir18662d652008-10-02 18:32:44 +00003039static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003040{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003041 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003042 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003043}
3044
blueswir18662d652008-10-02 18:32:44 +00003045static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003046{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003047 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003048 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003049}
bellarda541f292004-04-12 20:39:29 +00003050#endif
3051
bellarde95c8d52004-09-30 22:22:08 +00003052#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003053#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003054static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003055{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003056 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003057
3058 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003059}
bellard7b936c02005-10-30 17:05:13 +00003060#endif
bellarde95c8d52004-09-30 22:22:08 +00003061
blueswir18662d652008-10-02 18:32:44 +00003062static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003063{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003064 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003065 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003066}
3067#endif
3068
blueswir18662d652008-10-02 18:32:44 +00003069static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003070#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003071
3072#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01003073 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3074 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3075 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003076
Andreas Färbere59d1672012-02-16 00:40:47 +01003077 { "eax", offsetof(CPUX86State, regs[0]) },
3078 { "ecx", offsetof(CPUX86State, regs[1]) },
3079 { "edx", offsetof(CPUX86State, regs[2]) },
3080 { "ebx", offsetof(CPUX86State, regs[3]) },
3081 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3082 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3083 { "esi", offsetof(CPUX86State, regs[6]) },
3084 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003085#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003086 { "r8", offsetof(CPUX86State, regs[8]) },
3087 { "r9", offsetof(CPUX86State, regs[9]) },
3088 { "r10", offsetof(CPUX86State, regs[10]) },
3089 { "r11", offsetof(CPUX86State, regs[11]) },
3090 { "r12", offsetof(CPUX86State, regs[12]) },
3091 { "r13", offsetof(CPUX86State, regs[13]) },
3092 { "r14", offsetof(CPUX86State, regs[14]) },
3093 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003094#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003095 { "eflags", offsetof(CPUX86State, eflags) },
3096 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003097 SEG("cs", R_CS)
3098 SEG("ds", R_DS)
3099 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003100 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003101 SEG("fs", R_FS)
3102 SEG("gs", R_GS)
3103 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003104#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003105 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003106 { "r0", offsetof(CPUPPCState, gpr[0]) },
3107 { "r1", offsetof(CPUPPCState, gpr[1]) },
3108 { "r2", offsetof(CPUPPCState, gpr[2]) },
3109 { "r3", offsetof(CPUPPCState, gpr[3]) },
3110 { "r4", offsetof(CPUPPCState, gpr[4]) },
3111 { "r5", offsetof(CPUPPCState, gpr[5]) },
3112 { "r6", offsetof(CPUPPCState, gpr[6]) },
3113 { "r7", offsetof(CPUPPCState, gpr[7]) },
3114 { "r8", offsetof(CPUPPCState, gpr[8]) },
3115 { "r9", offsetof(CPUPPCState, gpr[9]) },
3116 { "r10", offsetof(CPUPPCState, gpr[10]) },
3117 { "r11", offsetof(CPUPPCState, gpr[11]) },
3118 { "r12", offsetof(CPUPPCState, gpr[12]) },
3119 { "r13", offsetof(CPUPPCState, gpr[13]) },
3120 { "r14", offsetof(CPUPPCState, gpr[14]) },
3121 { "r15", offsetof(CPUPPCState, gpr[15]) },
3122 { "r16", offsetof(CPUPPCState, gpr[16]) },
3123 { "r17", offsetof(CPUPPCState, gpr[17]) },
3124 { "r18", offsetof(CPUPPCState, gpr[18]) },
3125 { "r19", offsetof(CPUPPCState, gpr[19]) },
3126 { "r20", offsetof(CPUPPCState, gpr[20]) },
3127 { "r21", offsetof(CPUPPCState, gpr[21]) },
3128 { "r22", offsetof(CPUPPCState, gpr[22]) },
3129 { "r23", offsetof(CPUPPCState, gpr[23]) },
3130 { "r24", offsetof(CPUPPCState, gpr[24]) },
3131 { "r25", offsetof(CPUPPCState, gpr[25]) },
3132 { "r26", offsetof(CPUPPCState, gpr[26]) },
3133 { "r27", offsetof(CPUPPCState, gpr[27]) },
3134 { "r28", offsetof(CPUPPCState, gpr[28]) },
3135 { "r29", offsetof(CPUPPCState, gpr[29]) },
3136 { "r30", offsetof(CPUPPCState, gpr[30]) },
3137 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003138 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003139 { "f0", offsetof(CPUPPCState, fpr[0]) },
3140 { "f1", offsetof(CPUPPCState, fpr[1]) },
3141 { "f2", offsetof(CPUPPCState, fpr[2]) },
3142 { "f3", offsetof(CPUPPCState, fpr[3]) },
3143 { "f4", offsetof(CPUPPCState, fpr[4]) },
3144 { "f5", offsetof(CPUPPCState, fpr[5]) },
3145 { "f6", offsetof(CPUPPCState, fpr[6]) },
3146 { "f7", offsetof(CPUPPCState, fpr[7]) },
3147 { "f8", offsetof(CPUPPCState, fpr[8]) },
3148 { "f9", offsetof(CPUPPCState, fpr[9]) },
3149 { "f10", offsetof(CPUPPCState, fpr[10]) },
3150 { "f11", offsetof(CPUPPCState, fpr[11]) },
3151 { "f12", offsetof(CPUPPCState, fpr[12]) },
3152 { "f13", offsetof(CPUPPCState, fpr[13]) },
3153 { "f14", offsetof(CPUPPCState, fpr[14]) },
3154 { "f15", offsetof(CPUPPCState, fpr[15]) },
3155 { "f16", offsetof(CPUPPCState, fpr[16]) },
3156 { "f17", offsetof(CPUPPCState, fpr[17]) },
3157 { "f18", offsetof(CPUPPCState, fpr[18]) },
3158 { "f19", offsetof(CPUPPCState, fpr[19]) },
3159 { "f20", offsetof(CPUPPCState, fpr[20]) },
3160 { "f21", offsetof(CPUPPCState, fpr[21]) },
3161 { "f22", offsetof(CPUPPCState, fpr[22]) },
3162 { "f23", offsetof(CPUPPCState, fpr[23]) },
3163 { "f24", offsetof(CPUPPCState, fpr[24]) },
3164 { "f25", offsetof(CPUPPCState, fpr[25]) },
3165 { "f26", offsetof(CPUPPCState, fpr[26]) },
3166 { "f27", offsetof(CPUPPCState, fpr[27]) },
3167 { "f28", offsetof(CPUPPCState, fpr[28]) },
3168 { "f29", offsetof(CPUPPCState, fpr[29]) },
3169 { "f30", offsetof(CPUPPCState, fpr[30]) },
3170 { "f31", offsetof(CPUPPCState, fpr[31]) },
3171 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003172 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003173 { "nip|pc", offsetof(CPUPPCState, nip) },
3174 { "lr", offsetof(CPUPPCState, lr) },
3175 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003176 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003177 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003178 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003179 { "msr", 0, &monitor_get_msr, },
3180 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003181 { "tbu", 0, &monitor_get_tbu, },
3182 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003183 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003184 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3185 { "sr0", offsetof(CPUPPCState, sr[0]) },
3186 { "sr1", offsetof(CPUPPCState, sr[1]) },
3187 { "sr2", offsetof(CPUPPCState, sr[2]) },
3188 { "sr3", offsetof(CPUPPCState, sr[3]) },
3189 { "sr4", offsetof(CPUPPCState, sr[4]) },
3190 { "sr5", offsetof(CPUPPCState, sr[5]) },
3191 { "sr6", offsetof(CPUPPCState, sr[6]) },
3192 { "sr7", offsetof(CPUPPCState, sr[7]) },
3193 { "sr8", offsetof(CPUPPCState, sr[8]) },
3194 { "sr9", offsetof(CPUPPCState, sr[9]) },
3195 { "sr10", offsetof(CPUPPCState, sr[10]) },
3196 { "sr11", offsetof(CPUPPCState, sr[11]) },
3197 { "sr12", offsetof(CPUPPCState, sr[12]) },
3198 { "sr13", offsetof(CPUPPCState, sr[13]) },
3199 { "sr14", offsetof(CPUPPCState, sr[14]) },
3200 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003201 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003202 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003203
Andreas Färbere59d1672012-02-16 00:40:47 +01003204 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3205 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003206 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3207 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3208 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003209 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3210 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3211 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3212 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3213 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3214 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3215 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3216 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3217 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3218 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3219 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3220 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3221 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3222 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3223 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3224 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3225 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3226 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3227 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3228 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3229 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3230 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3231 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3232 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3233 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3234 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3235 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3236 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3237 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3238 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3239 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3240 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3241 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3242 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3243 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3244 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3245 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3246 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3247 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3248 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3249 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3250 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3251 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3252 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3253 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3254 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3255 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3256 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3257 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3258 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3259 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3260 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3261 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3262 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3263 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3264 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3265 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3266 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3267 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3268 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3269 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3270 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3271 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3272 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003273
bellarde95c8d52004-09-30 22:22:08 +00003274#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003275 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3276 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3277 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3278 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3279 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3280 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3281 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3282 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003283 { "o0", 0, monitor_get_reg },
3284 { "o1", 1, monitor_get_reg },
3285 { "o2", 2, monitor_get_reg },
3286 { "o3", 3, monitor_get_reg },
3287 { "o4", 4, monitor_get_reg },
3288 { "o5", 5, monitor_get_reg },
3289 { "o6", 6, monitor_get_reg },
3290 { "o7", 7, monitor_get_reg },
3291 { "l0", 8, monitor_get_reg },
3292 { "l1", 9, monitor_get_reg },
3293 { "l2", 10, monitor_get_reg },
3294 { "l3", 11, monitor_get_reg },
3295 { "l4", 12, monitor_get_reg },
3296 { "l5", 13, monitor_get_reg },
3297 { "l6", 14, monitor_get_reg },
3298 { "l7", 15, monitor_get_reg },
3299 { "i0", 16, monitor_get_reg },
3300 { "i1", 17, monitor_get_reg },
3301 { "i2", 18, monitor_get_reg },
3302 { "i3", 19, monitor_get_reg },
3303 { "i4", 20, monitor_get_reg },
3304 { "i5", 21, monitor_get_reg },
3305 { "i6", 22, monitor_get_reg },
3306 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003307 { "pc", offsetof(CPUSPARCState, pc) },
3308 { "npc", offsetof(CPUSPARCState, npc) },
3309 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003310#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003311 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003312 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003313#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003314 { "tbr", offsetof(CPUSPARCState, tbr) },
3315 { "fsr", offsetof(CPUSPARCState, fsr) },
3316 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3317 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3318 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3319 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3320 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3321 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3322 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3323 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3324 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3325 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3326 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3327 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3328 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3329 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3330 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3331 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3332 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3333 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3334 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3335 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3336 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3337 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3338 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3339 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3340 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3341 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3342 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3343 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3344 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3345 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3346 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3347 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003348#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003349 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3350 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3351 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3352 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3353 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3354 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3355 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3356 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3357 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3358 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3359 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3360 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3361 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3362 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3363 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3364 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3365 { "asi", offsetof(CPUSPARCState, asi) },
3366 { "pstate", offsetof(CPUSPARCState, pstate) },
3367 { "cansave", offsetof(CPUSPARCState, cansave) },
3368 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3369 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3370 { "wstate", offsetof(CPUSPARCState, wstate) },
3371 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3372 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003373#endif
bellard9307c4c2004-04-04 12:57:25 +00003374#endif
3375 { NULL },
3376};
3377
Stefan Weil9c3175c2013-08-22 21:30:09 +02003378static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3379expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003380{
Fam Zheng277acfe2013-08-20 10:58:21 +08003381 va_list ap;
3382 va_start(ap, fmt);
3383 monitor_vprintf(mon, fmt, ap);
3384 monitor_printf(mon, "\n");
3385 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003386 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003387}
3388
Markus Armbruster09b94182010-01-20 13:07:30 +01003389/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003390static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003391{
blueswir18662d652008-10-02 18:32:44 +00003392 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003393 void *ptr;
3394
bellard9307c4c2004-04-04 12:57:25 +00003395 for(md = monitor_defs; md->name != NULL; md++) {
3396 if (compare_cmd(name, md->name)) {
3397 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003398 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003399 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003400 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003401 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003402 switch(md->type) {
3403 case MD_I32:
3404 *pval = *(int32_t *)ptr;
3405 break;
3406 case MD_TLONG:
3407 *pval = *(target_long *)ptr;
3408 break;
3409 default:
3410 *pval = 0;
3411 break;
3412 }
bellard9307c4c2004-04-04 12:57:25 +00003413 }
3414 return 0;
3415 }
3416 }
3417 return -1;
3418}
3419
3420static void next(void)
3421{
Blue Swirl660f11b2009-07-31 21:16:51 +00003422 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003423 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003424 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003425 pch++;
3426 }
3427}
3428
aliguori376253e2009-03-05 23:01:23 +00003429static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003430
aliguori376253e2009-03-05 23:01:23 +00003431static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003432{
blueswir1c2efc952007-09-25 17:28:42 +00003433 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003434 char *p;
bellard6a00d602005-11-21 23:25:50 +00003435 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003436
3437 switch(*pch) {
3438 case '+':
3439 next();
aliguori376253e2009-03-05 23:01:23 +00003440 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003441 break;
3442 case '-':
3443 next();
aliguori376253e2009-03-05 23:01:23 +00003444 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003445 break;
3446 case '~':
3447 next();
aliguori376253e2009-03-05 23:01:23 +00003448 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003449 break;
3450 case '(':
3451 next();
aliguori376253e2009-03-05 23:01:23 +00003452 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003453 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003454 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003455 }
3456 next();
3457 break;
bellard81d09122004-07-14 17:21:37 +00003458 case '\'':
3459 pch++;
3460 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003461 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003462 n = *pch;
3463 pch++;
3464 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003465 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003466 next();
3467 break;
bellard9307c4c2004-04-04 12:57:25 +00003468 case '$':
3469 {
3470 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003471 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003472
bellard9307c4c2004-04-04 12:57:25 +00003473 pch++;
3474 q = buf;
3475 while ((*pch >= 'a' && *pch <= 'z') ||
3476 (*pch >= 'A' && *pch <= 'Z') ||
3477 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003478 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003479 if ((q - buf) < sizeof(buf) - 1)
3480 *q++ = *pch;
3481 pch++;
3482 }
blueswir1cd390082008-11-16 13:53:32 +00003483 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003484 pch++;
3485 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003486 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003487 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003488 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003489 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003490 }
3491 break;
3492 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003493 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003494 n = 0;
3495 break;
3496 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003497 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003498 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003499 if (errno == ERANGE) {
3500 expr_error(mon, "number too large");
3501 }
bellard9307c4c2004-04-04 12:57:25 +00003502 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003503 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003504 }
3505 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003506 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003507 pch++;
3508 break;
3509 }
3510 return n;
3511}
3512
3513
aliguori376253e2009-03-05 23:01:23 +00003514static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003515{
blueswir1c2efc952007-09-25 17:28:42 +00003516 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003517 int op;
ths3b46e622007-09-17 08:09:54 +00003518
aliguori376253e2009-03-05 23:01:23 +00003519 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003520 for(;;) {
3521 op = *pch;
3522 if (op != '*' && op != '/' && op != '%')
3523 break;
3524 next();
aliguori376253e2009-03-05 23:01:23 +00003525 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003526 switch(op) {
3527 default:
3528 case '*':
3529 val *= val2;
3530 break;
3531 case '/':
3532 case '%':
ths5fafdf22007-09-16 21:08:06 +00003533 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003534 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003535 if (op == '/')
3536 val /= val2;
3537 else
3538 val %= val2;
3539 break;
3540 }
3541 }
3542 return val;
3543}
3544
aliguori376253e2009-03-05 23:01:23 +00003545static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003546{
blueswir1c2efc952007-09-25 17:28:42 +00003547 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003548 int op;
bellard9307c4c2004-04-04 12:57:25 +00003549
aliguori376253e2009-03-05 23:01:23 +00003550 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003551 for(;;) {
3552 op = *pch;
3553 if (op != '&' && op != '|' && op != '^')
3554 break;
3555 next();
aliguori376253e2009-03-05 23:01:23 +00003556 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003557 switch(op) {
3558 default:
3559 case '&':
3560 val &= val2;
3561 break;
3562 case '|':
3563 val |= val2;
3564 break;
3565 case '^':
3566 val ^= val2;
3567 break;
3568 }
3569 }
3570 return val;
3571}
3572
aliguori376253e2009-03-05 23:01:23 +00003573static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003574{
blueswir1c2efc952007-09-25 17:28:42 +00003575 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003576 int op;
bellard9307c4c2004-04-04 12:57:25 +00003577
aliguori376253e2009-03-05 23:01:23 +00003578 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003579 for(;;) {
3580 op = *pch;
3581 if (op != '+' && op != '-')
3582 break;
3583 next();
aliguori376253e2009-03-05 23:01:23 +00003584 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003585 if (op == '+')
3586 val += val2;
3587 else
3588 val -= val2;
3589 }
3590 return val;
3591}
3592
aliguori376253e2009-03-05 23:01:23 +00003593static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003594{
3595 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003596 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003597 *pp = pch;
3598 return -1;
3599 }
blueswir1cd390082008-11-16 13:53:32 +00003600 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003601 pch++;
aliguori376253e2009-03-05 23:01:23 +00003602 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003603 *pp = pch;
3604 return 0;
3605}
3606
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003607static int get_double(Monitor *mon, double *pval, const char **pp)
3608{
3609 const char *p = *pp;
3610 char *tailp;
3611 double d;
3612
3613 d = strtod(p, &tailp);
3614 if (tailp == p) {
3615 monitor_printf(mon, "Number expected\n");
3616 return -1;
3617 }
3618 if (d != d || d - d != 0) {
3619 /* NaN or infinity */
3620 monitor_printf(mon, "Bad number\n");
3621 return -1;
3622 }
3623 *pval = d;
3624 *pp = tailp;
3625 return 0;
3626}
3627
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003628/*
3629 * Store the command-name in cmdname, and return a pointer to
3630 * the remaining of the command string.
3631 */
3632static const char *get_command_name(const char *cmdline,
3633 char *cmdname, size_t nlen)
3634{
3635 size_t len;
3636 const char *p, *pstart;
3637
3638 p = cmdline;
3639 while (qemu_isspace(*p))
3640 p++;
3641 if (*p == '\0')
3642 return NULL;
3643 pstart = p;
3644 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3645 p++;
3646 len = p - pstart;
3647 if (len > nlen - 1)
3648 len = nlen - 1;
3649 memcpy(cmdname, pstart, len);
3650 cmdname[len] = '\0';
3651 return p;
3652}
3653
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003654/**
3655 * Read key of 'type' into 'key' and return the current
3656 * 'type' pointer.
3657 */
3658static char *key_get_info(const char *type, char **key)
3659{
3660 size_t len;
3661 char *p, *str;
3662
3663 if (*type == ',')
3664 type++;
3665
3666 p = strchr(type, ':');
3667 if (!p) {
3668 *key = NULL;
3669 return NULL;
3670 }
3671 len = p - type;
3672
Anthony Liguori7267c092011-08-20 22:09:37 -05003673 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003674 memcpy(str, type, len);
3675 str[len] = '\0';
3676
3677 *key = str;
3678 return ++p;
3679}
3680
bellard9307c4c2004-04-04 12:57:25 +00003681static int default_fmt_format = 'x';
3682static int default_fmt_size = 4;
3683
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003684static int is_valid_option(const char *c, const char *typestr)
3685{
3686 char option[3];
3687
3688 option[0] = '-';
3689 option[1] = *c;
3690 option[2] = '\0';
3691
3692 typestr = strstr(typestr, option);
3693 return (typestr != NULL);
3694}
3695
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003696static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3697 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003698{
3699 const mon_cmd_t *cmd;
3700
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003701 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003702 if (compare_cmd(cmdname, cmd->name)) {
3703 return cmd;
3704 }
3705 }
3706
3707 return NULL;
3708}
3709
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003710static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3711{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003712 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003713}
3714
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003715/*
3716 * Parse @cmdline according to command table @table.
3717 * If @cmdline is blank, return NULL.
3718 * If it can't be parsed, report to @mon, and return NULL.
3719 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003720 * If a sub-command table exists, and if @cmdline contains an additional string
3721 * for a sub-command, this function will try to search the sub-command table.
3722 * If no additional string for a sub-command is present, this function will
3723 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003724 * Do not assume the returned command points into @table! It doesn't
3725 * when the command is a sub-command.
3726 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003727static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003728 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003729 int start,
3730 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003731 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003732{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003733 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003734 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003735 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003736 char cmdname[256];
3737 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003738 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003739
3740#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003741 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003742#endif
ths3b46e622007-09-17 08:09:54 +00003743
bellard9307c4c2004-04-04 12:57:25 +00003744 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003745 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003746 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003747 return NULL;
ths3b46e622007-09-17 08:09:54 +00003748
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003749 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003750 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003751 monitor_printf(mon, "unknown command: '%.*s'\n",
3752 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003753 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003754 }
bellard9307c4c2004-04-04 12:57:25 +00003755
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003756 /* filter out following useless space */
3757 while (qemu_isspace(*p)) {
3758 p++;
3759 }
3760 /* search sub command */
3761 if (cmd->sub_table != NULL) {
3762 /* check if user set additional command */
3763 if (*p == '\0') {
3764 return cmd;
3765 }
3766 return monitor_parse_command(mon, cmdline, p - cmdline,
3767 cmd->sub_table, qdict);
3768 }
3769
bellard9307c4c2004-04-04 12:57:25 +00003770 /* parse the parameters */
3771 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003772 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003773 typestr = key_get_info(typestr, &key);
3774 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003775 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003776 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003777 typestr++;
3778 switch(c) {
3779 case 'F':
bellard81d09122004-07-14 17:21:37 +00003780 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003781 case 's':
3782 {
3783 int ret;
ths3b46e622007-09-17 08:09:54 +00003784
blueswir1cd390082008-11-16 13:53:32 +00003785 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003786 p++;
3787 if (*typestr == '?') {
3788 typestr++;
3789 if (*p == '\0') {
3790 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003791 break;
bellard9307c4c2004-04-04 12:57:25 +00003792 }
3793 }
3794 ret = get_str(buf, sizeof(buf), &p);
3795 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003796 switch(c) {
3797 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003798 monitor_printf(mon, "%s: filename expected\n",
3799 cmdname);
bellard81d09122004-07-14 17:21:37 +00003800 break;
3801 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003802 monitor_printf(mon, "%s: block device name expected\n",
3803 cmdname);
bellard81d09122004-07-14 17:21:37 +00003804 break;
3805 default:
aliguori376253e2009-03-05 23:01:23 +00003806 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003807 break;
3808 }
bellard9307c4c2004-04-04 12:57:25 +00003809 goto fail;
3810 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003811 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003812 }
3813 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003814 case 'O':
3815 {
3816 QemuOptsList *opts_list;
3817 QemuOpts *opts;
3818
3819 opts_list = qemu_find_opts(key);
3820 if (!opts_list || opts_list->desc->name) {
3821 goto bad_type;
3822 }
3823 while (qemu_isspace(*p)) {
3824 p++;
3825 }
3826 if (!*p)
3827 break;
3828 if (get_str(buf, sizeof(buf), &p) < 0) {
3829 goto fail;
3830 }
3831 opts = qemu_opts_parse(opts_list, buf, 1);
3832 if (!opts) {
3833 goto fail;
3834 }
3835 qemu_opts_to_qdict(opts, qdict);
3836 qemu_opts_del(opts);
3837 }
3838 break;
bellard9307c4c2004-04-04 12:57:25 +00003839 case '/':
3840 {
3841 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003842
blueswir1cd390082008-11-16 13:53:32 +00003843 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003844 p++;
3845 if (*p == '/') {
3846 /* format found */
3847 p++;
3848 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003849 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003850 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003851 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003852 count = count * 10 + (*p - '0');
3853 p++;
3854 }
3855 }
3856 size = -1;
3857 format = -1;
3858 for(;;) {
3859 switch(*p) {
3860 case 'o':
3861 case 'd':
3862 case 'u':
3863 case 'x':
3864 case 'i':
3865 case 'c':
3866 format = *p++;
3867 break;
3868 case 'b':
3869 size = 1;
3870 p++;
3871 break;
3872 case 'h':
3873 size = 2;
3874 p++;
3875 break;
3876 case 'w':
3877 size = 4;
3878 p++;
3879 break;
3880 case 'g':
3881 case 'L':
3882 size = 8;
3883 p++;
3884 break;
3885 default:
3886 goto next;
3887 }
3888 }
3889 next:
blueswir1cd390082008-11-16 13:53:32 +00003890 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003891 monitor_printf(mon, "invalid char in format: '%c'\n",
3892 *p);
bellard9307c4c2004-04-04 12:57:25 +00003893 goto fail;
3894 }
bellard9307c4c2004-04-04 12:57:25 +00003895 if (format < 0)
3896 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003897 if (format != 'i') {
3898 /* for 'i', not specifying a size gives -1 as size */
3899 if (size < 0)
3900 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003901 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003902 }
bellard9307c4c2004-04-04 12:57:25 +00003903 default_fmt_format = format;
3904 } else {
3905 count = 1;
3906 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003907 if (format != 'i') {
3908 size = default_fmt_size;
3909 } else {
3910 size = -1;
3911 }
bellard9307c4c2004-04-04 12:57:25 +00003912 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003913 qdict_put(qdict, "count", qint_from_int(count));
3914 qdict_put(qdict, "format", qint_from_int(format));
3915 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003916 }
3917 break;
3918 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003919 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003920 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003921 {
blueswir1c2efc952007-09-25 17:28:42 +00003922 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003923
blueswir1cd390082008-11-16 13:53:32 +00003924 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003925 p++;
bellard34405572004-06-08 00:55:58 +00003926 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003927 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003928 if (*p == '\0') {
3929 typestr++;
3930 break;
3931 }
bellard34405572004-06-08 00:55:58 +00003932 } else {
3933 if (*p == '.') {
3934 p++;
blueswir1cd390082008-11-16 13:53:32 +00003935 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003936 p++;
bellard34405572004-06-08 00:55:58 +00003937 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003938 typestr++;
3939 break;
bellard34405572004-06-08 00:55:58 +00003940 }
3941 }
bellard13224a82006-07-14 22:03:35 +00003942 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003943 }
aliguori376253e2009-03-05 23:01:23 +00003944 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003945 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003946 /* Check if 'i' is greater than 32-bit */
3947 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3948 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3949 monitor_printf(mon, "integer is for 32-bit values\n");
3950 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003951 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003952 if (val < 0) {
3953 monitor_printf(mon, "enter a positive value\n");
3954 goto fail;
3955 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003956 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003957 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003958 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003959 }
3960 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003961 case 'o':
3962 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003963 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003964 char *end;
3965
3966 while (qemu_isspace(*p)) {
3967 p++;
3968 }
3969 if (*typestr == '?') {
3970 typestr++;
3971 if (*p == '\0') {
3972 break;
3973 }
3974 }
3975 val = strtosz(p, &end);
3976 if (val < 0) {
3977 monitor_printf(mon, "invalid size\n");
3978 goto fail;
3979 }
3980 qdict_put(qdict, key, qint_from_int(val));
3981 p = end;
3982 }
3983 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003984 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003985 {
3986 double val;
3987
3988 while (qemu_isspace(*p))
3989 p++;
3990 if (*typestr == '?') {
3991 typestr++;
3992 if (*p == '\0') {
3993 break;
3994 }
3995 }
3996 if (get_double(mon, &val, &p) < 0) {
3997 goto fail;
3998 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003999 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004000 switch (*p) {
4001 case 'm':
4002 val /= 1e3; p += 2; break;
4003 case 'u':
4004 val /= 1e6; p += 2; break;
4005 case 'n':
4006 val /= 1e9; p += 2; break;
4007 }
4008 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004009 if (*p && !qemu_isspace(*p)) {
4010 monitor_printf(mon, "Unknown unit suffix\n");
4011 goto fail;
4012 }
4013 qdict_put(qdict, key, qfloat_from_double(val));
4014 }
4015 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004016 case 'b':
4017 {
4018 const char *beg;
4019 int val;
4020
4021 while (qemu_isspace(*p)) {
4022 p++;
4023 }
4024 beg = p;
4025 while (qemu_isgraph(*p)) {
4026 p++;
4027 }
4028 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4029 val = 1;
4030 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4031 val = 0;
4032 } else {
4033 monitor_printf(mon, "Expected 'on' or 'off'\n");
4034 goto fail;
4035 }
4036 qdict_put(qdict, key, qbool_from_int(val));
4037 }
4038 break;
bellard9307c4c2004-04-04 12:57:25 +00004039 case '-':
4040 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004041 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004042 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004043 /* option */
ths3b46e622007-09-17 08:09:54 +00004044
bellard9307c4c2004-04-04 12:57:25 +00004045 c = *typestr++;
4046 if (c == '\0')
4047 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004048 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004049 p++;
bellard9307c4c2004-04-04 12:57:25 +00004050 if (*p == '-') {
4051 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004052 if(c != *p) {
4053 if(!is_valid_option(p, typestr)) {
4054
4055 monitor_printf(mon, "%s: unsupported option -%c\n",
4056 cmdname, *p);
4057 goto fail;
4058 } else {
4059 skip_key = 1;
4060 }
bellard9307c4c2004-04-04 12:57:25 +00004061 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004062 if(skip_key) {
4063 p = tmp;
4064 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004065 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004066 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004067 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004068 }
bellard9307c4c2004-04-04 12:57:25 +00004069 }
bellard9307c4c2004-04-04 12:57:25 +00004070 }
4071 break;
Wenchao Xia129be002013-08-27 20:38:26 +08004072 case 'S':
4073 {
4074 /* package all remaining string */
4075 int len;
4076
4077 while (qemu_isspace(*p)) {
4078 p++;
4079 }
4080 if (*typestr == '?') {
4081 typestr++;
4082 if (*p == '\0') {
4083 /* no remaining string: NULL argument */
4084 break;
4085 }
4086 }
4087 len = strlen(p);
4088 if (len <= 0) {
4089 monitor_printf(mon, "%s: string expected\n",
4090 cmdname);
4091 break;
4092 }
4093 qdict_put(qdict, key, qstring_from_str(p));
4094 p += len;
4095 }
4096 break;
bellard9307c4c2004-04-04 12:57:25 +00004097 default:
4098 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004099 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004100 goto fail;
4101 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004102 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004103 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004104 }
4105 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004106 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004107 p++;
4108 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004109 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4110 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004111 goto fail;
4112 }
4113
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004114 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004115
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004116fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004117 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004118 return NULL;
4119}
4120
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004121void monitor_set_error(Monitor *mon, QError *qerror)
4122{
4123 /* report only the first error */
4124 if (!mon->error) {
4125 mon->error = qerror;
4126 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004127 QDECREF(qerror);
4128 }
4129}
4130
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004131static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4132{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004133 if (ret && !monitor_has_error(mon)) {
4134 /*
4135 * If it returns failure, it must have passed on error.
4136 *
4137 * Action: Report an internal error to the client if in QMP.
4138 */
4139 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004140 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004141}
4142
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004143static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004144{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004145 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004146 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004147
4148 qdict = qdict_new();
4149
Wenchao Xia77172392013-08-27 20:38:20 +08004150 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004151 if (!cmd)
4152 goto out;
4153
Luiz Capitulino4903de02010-09-16 11:01:32 -03004154 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004155 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004156 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004157 QObject *data = NULL;
4158
4159 /* XXX: ignores the error code */
4160 cmd->mhandler.cmd_new(mon, qdict, &data);
4161 assert(!monitor_has_error(mon));
4162 if (data) {
4163 cmd->user_print(mon, data);
4164 qobject_decref(data);
4165 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004166 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004167 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004168 }
4169
Luiz Capitulino13917be2009-10-07 13:41:54 -03004170out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004171 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004172}
4173
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004174static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004175{
4176 const char *p, *pstart;
4177 char cmd[128];
4178 int len;
4179
4180 p = list;
4181 for(;;) {
4182 pstart = p;
4183 p = strchr(p, '|');
4184 if (!p)
4185 p = pstart + strlen(pstart);
4186 len = p - pstart;
4187 if (len > sizeof(cmd) - 2)
4188 len = sizeof(cmd) - 2;
4189 memcpy(cmd, pstart, len);
4190 cmd[len] = '\0';
4191 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004192 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004193 }
4194 if (*p == '\0')
4195 break;
4196 p++;
4197 }
4198}
4199
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004200static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004201{
4202 DIR *ffs;
4203 struct dirent *d;
4204 char path[1024];
4205 char file[1024], file_prefix[1024];
4206 int input_path_len;
4207 const char *p;
4208
ths5fafdf22007-09-16 21:08:06 +00004209 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004210 if (!p) {
4211 input_path_len = 0;
4212 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004213 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004214 } else {
4215 input_path_len = p - input + 1;
4216 memcpy(path, input, input_path_len);
4217 if (input_path_len > sizeof(path) - 1)
4218 input_path_len = sizeof(path) - 1;
4219 path[input_path_len] = '\0';
4220 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4221 }
4222#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004223 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004224 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004225#endif
4226 ffs = opendir(path);
4227 if (!ffs)
4228 return;
4229 for(;;) {
4230 struct stat sb;
4231 d = readdir(ffs);
4232 if (!d)
4233 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004234
4235 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4236 continue;
4237 }
4238
bellard81d09122004-07-14 17:21:37 +00004239 if (strstart(d->d_name, file_prefix, NULL)) {
4240 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004241 if (input_path_len < sizeof(file))
4242 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4243 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004244 /* stat the file to find out if it's a directory.
4245 * In that case add a slash to speed up typing long paths
4246 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004247 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004248 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004249 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004250 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004251 }
4252 }
4253 closedir(ffs);
4254}
4255
Wenchao Xia599a9262013-08-27 20:38:15 +08004256typedef struct MonitorBlockComplete {
4257 Monitor *mon;
4258 const char *input;
4259} MonitorBlockComplete;
4260
aliguori51de9762009-03-05 23:00:43 +00004261static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004262{
aliguori51de9762009-03-05 23:00:43 +00004263 const char *name = bdrv_get_device_name(bs);
Wenchao Xia599a9262013-08-27 20:38:15 +08004264 MonitorBlockComplete *mbc = opaque;
4265 Monitor *mon = mbc->mon;
4266 const char *input = mbc->input;
bellard81d09122004-07-14 17:21:37 +00004267
4268 if (input[0] == '\0' ||
4269 !strncmp(name, (char *)input, strlen(input))) {
Wenchao Xia599a9262013-08-27 20:38:15 +08004270 readline_add_completion(mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004271 }
4272}
4273
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004274static const char *next_arg_type(const char *typestr)
4275{
4276 const char *p = strchr(typestr, ':');
4277 return (p != NULL ? ++p : typestr);
4278}
4279
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004280static void device_add_completion(ReadLineState *rs, const char *str)
4281{
4282 GSList *list, *elt;
4283 size_t len;
4284
4285 len = strlen(str);
4286 readline_set_completion_index(rs, len);
4287 list = elt = object_class_get_list(TYPE_DEVICE, false);
4288 while (elt) {
4289 const char *name;
4290 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4291 TYPE_DEVICE);
4292 name = object_class_get_name(OBJECT_CLASS(dc));
4293 if (!strncmp(name, str, len)) {
4294 readline_add_completion(rs, name);
4295 }
4296 elt = elt->next;
4297 }
4298 g_slist_free(list);
4299}
4300
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004301static void object_add_completion(ReadLineState *rs, const char *str)
4302{
4303 GSList *list, *elt;
4304 size_t len;
4305
4306 len = strlen(str);
4307 readline_set_completion_index(rs, len);
4308 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4309 while (elt) {
4310 const char *name;
4311
4312 name = object_class_get_name(OBJECT_CLASS(elt->data));
4313 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4314 readline_add_completion(rs, name);
4315 }
4316 elt = elt->next;
4317 }
4318 g_slist_free(list);
4319}
4320
Hani Benhabilesfca72d92014-02-06 23:30:10 +01004321static void device_del_completion(ReadLineState *rs, BusState *bus,
4322 const char *str, size_t len)
4323{
4324 BusChild *kid;
4325
4326 QTAILQ_FOREACH(kid, &bus->children, sibling) {
4327 DeviceState *dev = kid->child;
4328 BusState *dev_child;
4329
4330 if (dev->id && !strncmp(str, dev->id, len)) {
4331 readline_add_completion(rs, dev->id);
4332 }
4333
4334 QLIST_FOREACH(dev_child, &dev->child_bus, sibling) {
4335 device_del_completion(rs, dev_child, str, len);
4336 }
4337 }
4338}
4339
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004340static void object_del_completion(ReadLineState *rs, const char *str)
4341{
4342 ObjectPropertyInfoList *list, *start;
4343 size_t len;
4344
4345 len = strlen(str);
4346 readline_set_completion_index(rs, len);
4347
4348 start = list = qmp_qom_list("/objects", NULL);
4349 while (list) {
4350 ObjectPropertyInfo *info = list->value;
4351
4352 if (!strncmp(info->type, "child<", 5)
4353 && !strncmp(info->name, str, len)) {
4354 readline_add_completion(rs, info->name);
4355 }
4356 list = list->next;
4357 }
4358 qapi_free_ObjectPropertyInfoList(start);
4359}
4360
Wenchao Xiac35b6402013-08-27 20:38:24 +08004361static void monitor_find_completion_by_table(Monitor *mon,
4362 const mon_cmd_t *cmd_table,
4363 char **args,
4364 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004365{
4366 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004367 int i;
bellard81d09122004-07-14 17:21:37 +00004368 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004369 const mon_cmd_t *cmd;
Wenchao Xia599a9262013-08-27 20:38:15 +08004370 MonitorBlockComplete mbs;
bellard81d09122004-07-14 17:21:37 +00004371
bellard81d09122004-07-14 17:21:37 +00004372 if (nb_args <= 1) {
4373 /* command completion */
4374 if (nb_args == 0)
4375 cmdname = "";
4376 else
4377 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004378 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004379 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004380 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004381 }
4382 } else {
4383 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004384 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004385 if (compare_cmd(args[0], cmd->name)) {
4386 break;
4387 }
bellard81d09122004-07-14 17:21:37 +00004388 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004389 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004390 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004391 }
4392
Wenchao Xiad903a772013-08-27 20:38:25 +08004393 if (cmd->sub_table) {
4394 /* do the job again */
4395 return monitor_find_completion_by_table(mon, cmd->sub_table,
4396 &args[1], nb_args - 1);
4397 }
4398
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004399 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004400 for(i = 0; i < nb_args - 2; i++) {
4401 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004402 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004403 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004404 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004405 }
4406 }
4407 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004408 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004409 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004410 }
bellard81d09122004-07-14 17:21:37 +00004411 switch(*ptype) {
4412 case 'F':
4413 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004414 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004415 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004416 break;
4417 case 'B':
4418 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004419 mbs.mon = mon;
4420 mbs.input = str;
4421 readline_set_completion_index(mon->rs, strlen(str));
4422 bdrv_iterate(block_completion_it, &mbs);
bellard81d09122004-07-14 17:21:37 +00004423 break;
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004424 case 'O':
4425 if (!strcmp(cmd->name, "device_add") && nb_args == 2) {
4426 device_add_completion(mon->rs, str);
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004427 } else if (!strcmp(cmd->name, "object_add") && nb_args == 2) {
4428 object_add_completion(mon->rs, str);
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004429 }
4430 break;
bellard7fe48482004-10-09 18:08:01 +00004431 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004432 case 'S':
Wenchao Xiad903a772013-08-27 20:38:25 +08004433 if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004434 char *sep = strrchr(str, '-');
4435 if (sep)
4436 str = sep + 1;
Wenchao Xiad2674b22013-08-27 20:38:16 +08004437 readline_set_completion_index(mon->rs, strlen(str));
Amos Kong1048c882012-08-31 10:56:25 +08004438 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004439 cmd_completion(mon, str, QKeyCode_lookup[i]);
bellard64866c32006-05-07 18:03:31 +00004440 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004441 } else if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004442 monitor_find_completion_by_table(mon, cmd_table,
4443 &args[1], nb_args - 1);
Hani Benhabilesfca72d92014-02-06 23:30:10 +01004444 } else if (!strcmp(cmd->name, "device_del") && nb_args == 2) {
4445 size_t len = strlen(str);
4446 readline_set_completion_index(mon->rs, len);
4447 device_del_completion(mon->rs, sysbus_get_default(), str, len);
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004448 } else if (!strcmp(cmd->name, "object_del") && nb_args == 2) {
4449 object_del_completion(mon->rs, str);
bellard7fe48482004-10-09 18:08:01 +00004450 }
4451 break;
bellard81d09122004-07-14 17:21:37 +00004452 default:
4453 break;
4454 }
4455 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004456}
4457
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004458static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004459 const char *cmdline)
4460{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004461 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004462 char *args[MAX_ARGS];
4463 int nb_args, len;
4464
4465 /* 1. parse the cmdline */
4466 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4467 return;
4468 }
4469#ifdef DEBUG_COMPLETION
4470 for (i = 0; i < nb_args; i++) {
4471 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4472 }
4473#endif
4474
4475 /* if the line ends with a space, it means we want to complete the
4476 next arg */
4477 len = strlen(cmdline);
4478 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4479 if (nb_args >= MAX_ARGS) {
4480 goto cleanup;
4481 }
4482 args[nb_args++] = g_strdup("");
4483 }
4484
4485 /* 2. auto complete according to args */
4486 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004487
4488cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004489 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004490}
4491
aliguori731b0362009-03-05 23:01:42 +00004492static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004493{
aliguori731b0362009-03-05 23:01:42 +00004494 Monitor *mon = opaque;
4495
Jan Kiszkac62313b2009-12-04 14:05:29 +01004496 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004497}
4498
Luiz Capitulino09069b12010-02-04 18:10:06 -02004499static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4500{
4501 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4502 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4503}
4504
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004505/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004506 * Argument validation rules:
4507 *
4508 * 1. The argument must exist in cmd_args qdict
4509 * 2. The argument type must be the expected one
4510 *
4511 * Special case: If the argument doesn't exist in cmd_args and
4512 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4513 * checking is skipped for it.
4514 */
4515static int check_client_args_type(const QDict *client_args,
4516 const QDict *cmd_args, int flags)
4517{
4518 const QDictEntry *ent;
4519
4520 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4521 QObject *obj;
4522 QString *arg_type;
4523 const QObject *client_arg = qdict_entry_value(ent);
4524 const char *client_arg_name = qdict_entry_key(ent);
4525
4526 obj = qdict_get(cmd_args, client_arg_name);
4527 if (!obj) {
4528 if (flags & QMP_ACCEPT_UNKNOWNS) {
4529 /* handler accepts unknowns */
4530 continue;
4531 }
4532 /* client arg doesn't exist */
4533 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4534 return -1;
4535 }
4536
4537 arg_type = qobject_to_qstring(obj);
4538 assert(arg_type != NULL);
4539
4540 /* check if argument's type is correct */
4541 switch (qstring_get_str(arg_type)[0]) {
4542 case 'F':
4543 case 'B':
4544 case 's':
4545 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4546 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4547 "string");
4548 return -1;
4549 }
4550 break;
4551 case 'i':
4552 case 'l':
4553 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004554 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004555 if (qobject_type(client_arg) != QTYPE_QINT) {
4556 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4557 "int");
4558 return -1;
4559 }
4560 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004561 case 'T':
4562 if (qobject_type(client_arg) != QTYPE_QINT &&
4563 qobject_type(client_arg) != QTYPE_QFLOAT) {
4564 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4565 "number");
4566 return -1;
4567 }
4568 break;
4569 case 'b':
4570 case '-':
4571 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4572 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4573 "bool");
4574 return -1;
4575 }
4576 break;
4577 case 'O':
4578 assert(flags & QMP_ACCEPT_UNKNOWNS);
4579 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004580 case 'q':
4581 /* Any QObject can be passed. */
4582 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004583 case '/':
4584 case '.':
4585 /*
4586 * These types are not supported by QMP and thus are not
4587 * handled here. Fall through.
4588 */
4589 default:
4590 abort();
4591 }
4592 }
4593
4594 return 0;
4595}
4596
4597/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004598 * - Check if the client has passed all mandatory args
4599 * - Set special flags for argument validation
4600 */
4601static int check_mandatory_args(const QDict *cmd_args,
4602 const QDict *client_args, int *flags)
4603{
4604 const QDictEntry *ent;
4605
4606 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4607 const char *cmd_arg_name = qdict_entry_key(ent);
4608 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4609 assert(type != NULL);
4610
4611 if (qstring_get_str(type)[0] == 'O') {
4612 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4613 *flags |= QMP_ACCEPT_UNKNOWNS;
4614 } else if (qstring_get_str(type)[0] != '-' &&
4615 qstring_get_str(type)[1] != '?' &&
4616 !qdict_haskey(client_args, cmd_arg_name)) {
4617 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4618 return -1;
4619 }
4620 }
4621
4622 return 0;
4623}
4624
4625static QDict *qdict_from_args_type(const char *args_type)
4626{
4627 int i;
4628 QDict *qdict;
4629 QString *key, *type, *cur_qs;
4630
4631 assert(args_type != NULL);
4632
4633 qdict = qdict_new();
4634
4635 if (args_type == NULL || args_type[0] == '\0') {
4636 /* no args, empty qdict */
4637 goto out;
4638 }
4639
4640 key = qstring_new();
4641 type = qstring_new();
4642
4643 cur_qs = key;
4644
4645 for (i = 0;; i++) {
4646 switch (args_type[i]) {
4647 case ',':
4648 case '\0':
4649 qdict_put(qdict, qstring_get_str(key), type);
4650 QDECREF(key);
4651 if (args_type[i] == '\0') {
4652 goto out;
4653 }
4654 type = qstring_new(); /* qdict has ref */
4655 cur_qs = key = qstring_new();
4656 break;
4657 case ':':
4658 cur_qs = type;
4659 break;
4660 default:
4661 qstring_append_chr(cur_qs, args_type[i]);
4662 break;
4663 }
4664 }
4665
4666out:
4667 return qdict;
4668}
4669
4670/*
4671 * Client argument checking rules:
4672 *
4673 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004674 * 2. Each argument provided by the client must be expected
4675 * 3. Each argument provided by the client must have the type expected
4676 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004677 */
4678static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4679{
4680 int flags, err;
4681 QDict *cmd_args;
4682
4683 cmd_args = qdict_from_args_type(cmd->args_type);
4684
4685 flags = 0;
4686 err = check_mandatory_args(cmd_args, client_args, &flags);
4687 if (err) {
4688 goto out;
4689 }
4690
Luiz Capitulino4af91932010-06-22 11:44:05 -03004691 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004692
4693out:
4694 QDECREF(cmd_args);
4695 return err;
4696}
4697
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004698/*
4699 * Input object checking rules
4700 *
4701 * 1. Input object must be a dict
4702 * 2. The "execute" key must exist
4703 * 3. The "execute" key must be a string
4704 * 4. If the "arguments" key exists, it must be a dict
4705 * 5. If the "id" key exists, it can be anything (ie. json-value)
4706 * 6. Any argument not listed above is considered invalid
4707 */
4708static QDict *qmp_check_input_obj(QObject *input_obj)
4709{
4710 const QDictEntry *ent;
4711 int has_exec_key = 0;
4712 QDict *input_dict;
4713
4714 if (qobject_type(input_obj) != QTYPE_QDICT) {
4715 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4716 return NULL;
4717 }
4718
4719 input_dict = qobject_to_qdict(input_obj);
4720
4721 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4722 const char *arg_name = qdict_entry_key(ent);
4723 const QObject *arg_obj = qdict_entry_value(ent);
4724
4725 if (!strcmp(arg_name, "execute")) {
4726 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4727 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4728 "string");
4729 return NULL;
4730 }
4731 has_exec_key = 1;
4732 } else if (!strcmp(arg_name, "arguments")) {
4733 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4734 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4735 "object");
4736 return NULL;
4737 }
4738 } else if (!strcmp(arg_name, "id")) {
4739 /* FIXME: check duplicated IDs for async commands */
4740 } else {
4741 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4742 return NULL;
4743 }
4744 }
4745
4746 if (!has_exec_key) {
4747 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4748 return NULL;
4749 }
4750
4751 return input_dict;
4752}
4753
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004754static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4755 const QDict *params)
4756{
4757 int ret;
4758 QObject *data = NULL;
4759
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004760 ret = cmd->mhandler.cmd_new(mon, params, &data);
4761 handler_audit(mon, cmd, ret);
4762 monitor_protocol_emitter(mon, data);
4763 qobject_decref(data);
4764}
4765
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004766static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4767{
4768 int err;
4769 QObject *obj;
4770 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004771 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004772 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004773 Monitor *mon = cur_mon;
4774
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004775 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004776
4777 obj = json_parser_parse(tokens, NULL);
4778 if (!obj) {
4779 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004780 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004781 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004782 }
4783
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004784 input = qmp_check_input_obj(obj);
4785 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004786 qobject_decref(obj);
4787 goto err_out;
4788 }
4789
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004790 mon->mc->id = qdict_get(input, "id");
4791 qobject_incref(mon->mc->id);
4792
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004793 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004794 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004795 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004796 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004797 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004798 }
4799
Anthony Liguorie3193602011-09-02 12:34:47 -05004800 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004801 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004802 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004803 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004804 }
4805
4806 obj = qdict_get(input, "arguments");
4807 if (!obj) {
4808 args = qdict_new();
4809 } else {
4810 args = qobject_to_qdict(obj);
4811 QINCREF(args);
4812 }
4813
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004814 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004815 if (err < 0) {
4816 goto err_out;
4817 }
4818
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004819 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004820 err = qmp_async_cmd_handler(mon, cmd, args);
4821 if (err) {
4822 /* emit the error response */
4823 goto err_out;
4824 }
Adam Litke940cc302010-01-25 12:18:44 -06004825 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004826 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004827 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004828
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004829 goto out;
4830
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004831err_out:
4832 monitor_protocol_emitter(mon, NULL);
4833out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004834 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004835 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004836}
4837
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004838/**
4839 * monitor_control_read(): Read and handle QMP input
4840 */
4841static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4842{
4843 Monitor *old_mon = cur_mon;
4844
4845 cur_mon = opaque;
4846
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004847 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004848
4849 cur_mon = old_mon;
4850}
4851
aliguori731b0362009-03-05 23:01:42 +00004852static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004853{
aliguori731b0362009-03-05 23:01:42 +00004854 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004855 int i;
aliguori376253e2009-03-05 23:01:23 +00004856
aliguori731b0362009-03-05 23:01:42 +00004857 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004858
aliguoricde76ee2009-03-05 23:01:51 +00004859 if (cur_mon->rs) {
4860 for (i = 0; i < size; i++)
4861 readline_handle_byte(cur_mon->rs, buf[i]);
4862 } else {
4863 if (size == 0 || buf[size - 1] != 0)
4864 monitor_printf(cur_mon, "corrupted command\n");
4865 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004866 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004867 }
aliguori731b0362009-03-05 23:01:42 +00004868
4869 cur_mon = old_mon;
4870}
aliguorid8f44602008-10-06 13:52:44 +00004871
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004872static void monitor_command_cb(void *opaque, const char *cmdline,
4873 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004874{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004875 Monitor *mon = opaque;
4876
aliguori731b0362009-03-05 23:01:42 +00004877 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004878 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004879 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004880}
4881
aliguoricde76ee2009-03-05 23:01:51 +00004882int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004883{
aliguoricde76ee2009-03-05 23:01:51 +00004884 if (!mon->rs)
4885 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004886 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004887 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004888}
4889
aliguori376253e2009-03-05 23:01:23 +00004890void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004891{
aliguoricde76ee2009-03-05 23:01:51 +00004892 if (!mon->rs)
4893 return;
aliguori731b0362009-03-05 23:01:42 +00004894 if (--mon->suspend_cnt == 0)
4895 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004896}
4897
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004898static QObject *get_qmp_greeting(void)
4899{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004900 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004901
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004902 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004903 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4904}
4905
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004906/**
4907 * monitor_control_event(): Print QMP gretting
4908 */
4909static void monitor_control_event(void *opaque, int event)
4910{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004911 QObject *data;
4912 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004913
Luiz Capitulino47116d12010-02-08 17:01:30 -02004914 switch (event) {
4915 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004916 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004917 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004918 monitor_json_emitter(mon, data);
4919 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004920 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004921 break;
4922 case CHR_EVENT_CLOSED:
4923 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05004924 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004925 mon_refcount--;
4926 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004927 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004928 }
4929}
4930
aliguori731b0362009-03-05 23:01:42 +00004931static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004932{
aliguori376253e2009-03-05 23:01:23 +00004933 Monitor *mon = opaque;
4934
aliguori2724b182009-03-05 23:01:47 +00004935 switch (event) {
4936 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004937 mon->mux_out = 0;
4938 if (mon->reset_seen) {
4939 readline_restart(mon->rs);
4940 monitor_resume(mon);
4941 monitor_flush(mon);
4942 } else {
4943 mon->suspend_cnt = 0;
4944 }
aliguori2724b182009-03-05 23:01:47 +00004945 break;
ths86e94de2007-01-05 22:01:59 +00004946
aliguori2724b182009-03-05 23:01:47 +00004947 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004948 if (mon->reset_seen) {
4949 if (mon->suspend_cnt == 0) {
4950 monitor_printf(mon, "\n");
4951 }
4952 monitor_flush(mon);
4953 monitor_suspend(mon);
4954 } else {
4955 mon->suspend_cnt++;
4956 }
4957 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004958 break;
4959
Amit Shahb6b8df52009-10-07 18:31:16 +05304960 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004961 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4962 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004963 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004964 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004965 }
4966 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004967 mon_refcount++;
4968 break;
4969
4970 case CHR_EVENT_CLOSED:
4971 mon_refcount--;
4972 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004973 break;
4974 }
ths86e94de2007-01-05 22:01:59 +00004975}
4976
Wayne Xia816f8922011-10-12 11:32:41 +08004977static int
4978compare_mon_cmd(const void *a, const void *b)
4979{
4980 return strcmp(((const mon_cmd_t *)a)->name,
4981 ((const mon_cmd_t *)b)->name);
4982}
4983
4984static void sortcmdlist(void)
4985{
4986 int array_num;
4987 int elem_size = sizeof(mon_cmd_t);
4988
4989 array_num = sizeof(mon_cmds)/elem_size-1;
4990 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4991
4992 array_num = sizeof(info_cmds)/elem_size-1;
4993 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4994}
4995
aliguori76655d62009-03-06 20:27:37 +00004996
4997/*
4998 * Local variables:
4999 * c-indent-level: 4
5000 * c-basic-offset: 4
5001 * tab-width: 8
5002 * End:
5003 */
5004
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005005/* These functions just adapt the readline interface in a typesafe way. We
5006 * could cast function pointers but that discards compiler checks.
5007 */
Stefan Weild5d15072014-01-25 18:18:23 +01005008static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5009 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005010{
5011 va_list ap;
5012 va_start(ap, fmt);
5013 monitor_vprintf(opaque, fmt, ap);
5014 va_end(ap);
5015}
5016
5017static void monitor_readline_flush(void *opaque)
5018{
5019 monitor_flush(opaque);
5020}
5021
aliguori731b0362009-03-05 23:01:42 +00005022void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005023{
aliguori731b0362009-03-05 23:01:42 +00005024 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005025 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005026
5027 if (is_first_init) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01005028 monitor_protocol_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005029 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005030 is_first_init = 0;
5031 }
aliguori87127162009-03-05 23:01:29 +00005032
Wenchao Xiab01fe892013-08-27 20:38:19 +08005033 mon = g_malloc(sizeof(*mon));
5034 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005035
aliguori87127162009-03-05 23:01:29 +00005036 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005037 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005038 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005039 mon->rs = readline_init(monitor_readline_printf,
5040 monitor_readline_flush,
5041 mon,
5042 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005043 monitor_read_command(mon, 0);
5044 }
aliguori87127162009-03-05 23:01:29 +00005045
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005046 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005047 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005048 /* Control mode requires special handlers */
5049 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5050 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005051 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05005052
5053 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005054 } else {
5055 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5056 monitor_event, mon);
5057 }
aliguori87127162009-03-05 23:01:29 +00005058
Blue Swirl72cf2d42009-09-12 07:36:22 +00005059 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01005060 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5061 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005062}
5063
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005064static void bdrv_password_cb(void *opaque, const char *password,
5065 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005066{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005067 Monitor *mon = opaque;
5068 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005069 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00005070
aliguoribb5fc202009-03-05 23:01:15 +00005071 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00005072 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00005073 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005074 }
aliguori731b0362009-03-05 23:01:42 +00005075 if (mon->password_completion_cb)
5076 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005077
aliguori731b0362009-03-05 23:01:42 +00005078 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005079}
aliguoric0f4ce72009-03-05 23:01:01 +00005080
Anthony Liguori7060b472011-09-02 12:34:50 -05005081ReadLineState *monitor_get_rs(Monitor *mon)
5082{
5083 return mon->rs;
5084}
5085
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005086int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5087 BlockDriverCompletionFunc *completion_cb,
5088 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005089{
aliguoricde76ee2009-03-05 23:01:51 +00005090 int err;
5091
aliguoribb5fc202009-03-05 23:01:15 +00005092 if (!bdrv_key_required(bs)) {
5093 if (completion_cb)
5094 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005095 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00005096 }
aliguoric0f4ce72009-03-05 23:01:01 +00005097
Luiz Capitulino94171e12009-12-07 21:37:00 +01005098 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02005099 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
5100 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005101 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01005102 }
5103
aliguori376253e2009-03-05 23:01:23 +00005104 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5105 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005106
aliguori731b0362009-03-05 23:01:42 +00005107 mon->password_completion_cb = completion_cb;
5108 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005109
aliguoricde76ee2009-03-05 23:01:51 +00005110 err = monitor_read_password(mon, bdrv_password_cb, bs);
5111
5112 if (err && completion_cb)
5113 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005114
5115 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005116}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005117
5118int monitor_read_block_device_key(Monitor *mon, const char *device,
5119 BlockDriverCompletionFunc *completion_cb,
5120 void *opaque)
5121{
5122 BlockDriverState *bs;
5123
5124 bs = bdrv_find(device);
5125 if (!bs) {
5126 monitor_printf(mon, "Device not found %s\n", device);
5127 return -1;
5128 }
5129
5130 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
5131}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005132
5133QemuOptsList qemu_mon_opts = {
5134 .name = "mon",
5135 .implied_opt_name = "chardev",
5136 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5137 .desc = {
5138 {
5139 .name = "mode",
5140 .type = QEMU_OPT_STRING,
5141 },{
5142 .name = "chardev",
5143 .type = QEMU_OPT_STRING,
5144 },{
5145 .name = "default",
5146 .type = QEMU_OPT_BOOL,
5147 },{
5148 .name = "pretty",
5149 .type = QEMU_OPT_BOOL,
5150 },
5151 { /* end of list */ }
5152 },
5153};