blob: 416ba107304c2616b3c77a8bb0091069eb8bf237 [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"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080076#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000077
Markus Armbruster1ce6be22015-02-06 14:18:24 +010078/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020079#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010080#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020081#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083
bellard9dc39cb2004-03-14 21:38:27 +000084//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000085//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000086
bellard9307c4c2004-04-04 12:57:25 +000087/*
88 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000089 *
bellard9307c4c2004-04-04 12:57:25 +000090 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000091 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000092 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080093 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010094 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000099 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200103 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 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 +0100108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000111 * '/' optional gdb-like print format (like "/10x")
112 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100115 * 'b' boolean
116 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000118 *
119 */
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000123 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *params;
125 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300126 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300127 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300130 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800131 /* @sub_table is a list of 2nd level of commands. If it do not exist,
132 * mhandler should be used. If it exist, sub_table[?].mhandler should be
133 * used, and mhandler of 1st level plays the role of help function.
134 */
135 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100136 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500137} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000138
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100139/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500140typedef struct mon_fd_t mon_fd_t;
141struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142 char *name;
143 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500144 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145};
146
Corey Bryantba1c0482012-08-14 16:43:43 -0400147/* file descriptor associated with a file descriptor set */
148typedef struct MonFdsetFd MonFdsetFd;
149struct MonFdsetFd {
150 int fd;
151 bool removed;
152 char *opaque;
153 QLIST_ENTRY(MonFdsetFd) next;
154};
155
156/* file descriptor set containing fds passed via SCM_RIGHTS */
157typedef struct MonFdset MonFdset;
158struct MonFdset {
159 int64_t id;
160 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400161 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400162 QLIST_ENTRY(MonFdset) next;
163};
164
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165typedef struct MonitorControl {
166 QObject *id;
167 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200168 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169} MonitorControl;
170
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100171/*
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
174 * instance.
175 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200176typedef struct MonitorQAPIEventState {
177 QAPIEvent event; /* Event being tracked */
178 int64_t rate; /* Minimum time (in ns) between two events */
179 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100180 QEMUTimer *timer; /* Timer for handling delayed events */
181 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200182} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100183
aliguori87127162009-03-05 23:01:29 +0000184struct Monitor {
185 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200186 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000187 int flags;
188 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400189 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200190
191 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400192 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200193 guint out_watch;
194
195 /* Read under either BQL or out_lock, written with BQL+out_lock. */
196 int mux_out;
197
aliguori731b0362009-03-05 23:01:42 +0000198 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200199 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200200 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200201 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000202 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800203 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200204 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500205 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000206 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000207};
208
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300209/* QMP checker flags */
210#define QMP_ACCEPT_UNKNOWNS 1
211
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200212/* Protects mon_list, monitor_event_state. */
213static QemuMutex monitor_lock;
214
Blue Swirl72cf2d42009-09-12 07:36:22 +0000215static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400216static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400217static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000218
Wayne Xia816f8922011-10-12 11:32:41 +0800219static mon_cmd_t mon_cmds[];
220static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000221
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300222static const mon_cmd_t qmp_cmds[];
223
Markus Armbruster8631b602010-02-18 11:41:55 +0100224Monitor *cur_mon;
225Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000226
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100227static void monitor_command_cb(void *opaque, const char *cmdline,
228 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000229
Luiz Capitulino09069b12010-02-04 18:10:06 -0200230static inline int qmp_cmd_mode(const Monitor *mon)
231{
232 return (mon->mc ? mon->mc->command_mode : 0);
233}
234
Luiz Capitulino418173c2009-11-26 22:58:51 -0200235/* Return true if in control mode, false otherwise */
236static inline int monitor_ctrl_mode(const Monitor *mon)
237{
238 return (mon->flags & MONITOR_USE_CONTROL);
239}
240
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100241/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
242int monitor_cur_is_qmp(void)
243{
244 return cur_mon && monitor_ctrl_mode(cur_mon);
245}
246
Anthony Liguori7060b472011-09-02 12:34:50 -0500247void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000248{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200249 if (!mon->rs)
250 return;
251
aliguori731b0362009-03-05 23:01:42 +0000252 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
253 if (show_prompt)
254 readline_show_prompt(mon->rs);
255}
bellard6a00d602005-11-21 23:25:50 +0000256
Anthony Liguori7060b472011-09-02 12:34:50 -0500257int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
258 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000259{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100260 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000261 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
262 /* prompt is printed on return from the command handler */
263 return 0;
264 } else {
265 monitor_printf(mon, "terminal does not support password prompting\n");
266 return -ENOTTY;
267 }
aliguoribb5fc202009-03-05 23:01:15 +0000268}
269
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200270static void monitor_flush_locked(Monitor *mon);
271
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100272static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
273 void *opaque)
274{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200275 Monitor *mon = opaque;
276
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200277 qemu_mutex_lock(&mon->out_lock);
278 mon->out_watch = 0;
279 monitor_flush_locked(mon);
280 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100281 return FALSE;
282}
283
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200284/* Called with mon->out_lock held. */
285static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000286{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100287 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400288 size_t len;
289 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400291 if (mon->skip_flush) {
292 return;
293 }
294
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400295 buf = qstring_get_str(mon->outbuf);
296 len = qstring_get_length(mon->outbuf);
297
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200298 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200300 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
301 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 QDECREF(mon->outbuf);
303 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100304 return;
305 }
306 if (rc > 0) {
307 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400308 QString *tmp = qstring_from_str(buf + rc);
309 QDECREF(mon->outbuf);
310 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100311 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200312 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200313 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200314 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200315 }
bellard7e2515e2004-08-01 21:52:19 +0000316 }
317}
318
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200319void monitor_flush(Monitor *mon)
320{
321 qemu_mutex_lock(&mon->out_lock);
322 monitor_flush_locked(mon);
323 qemu_mutex_unlock(&mon->out_lock);
324}
325
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400326/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000327static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000328{
ths60fe76f2007-12-16 03:02:09 +0000329 char c;
aliguori731b0362009-03-05 23:01:42 +0000330
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200331 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000332 for(;;) {
333 c = *str++;
334 if (c == '\0')
335 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400336 if (c == '\n') {
337 qstring_append_chr(mon->outbuf, '\r');
338 }
339 qstring_append_chr(mon->outbuf, c);
340 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200341 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 }
bellard7e2515e2004-08-01 21:52:19 +0000343 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200344 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000345}
346
aliguori376253e2009-03-05 23:01:23 +0000347void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000348{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400349 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200350
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200351 if (!mon)
352 return;
353
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200355 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200356 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400358 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400360 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000361}
362
aliguori376253e2009-03-05 23:01:23 +0000363void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000364{
365 va_list ap;
366 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000368 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000369}
370
Stefan Weil8b7968f2010-09-23 21:28:05 +0200371static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
372 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000373{
374 va_list ap;
375 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000377 va_end(ap);
378 return 0;
379}
380
Luiz Capitulino13c74252009-10-07 13:41:55 -0300381static void monitor_user_noop(Monitor *mon, const QObject *data) { }
382
Luiz Capitulino9e807212010-09-16 10:58:59 -0300383static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300384{
385 return cmd->user_print != NULL;
386}
387
Luiz Capitulino8204a912009-11-18 23:05:31 -0200388static inline int monitor_has_error(const Monitor *mon)
389{
390 return mon->error != NULL;
391}
392
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200393static void monitor_json_emitter(Monitor *mon, const QObject *data)
394{
395 QString *json;
396
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200397 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
398 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200399 assert(json != NULL);
400
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200401 qstring_append_chr(json, '\n');
402 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200403
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200404 QDECREF(json);
405}
406
Luiz Capitulinode253f12012-07-27 16:18:16 -0300407static QDict *build_qmp_error_dict(const QError *err)
408{
409 QObject *obj;
410
411 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
412 ErrorClass_lookup[err->err_class],
413 qerror_human(err));
414
415 return qobject_to_qdict(obj);
416}
417
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418static void monitor_protocol_emitter(Monitor *mon, QObject *data)
419{
420 QDict *qmp;
421
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100422 trace_monitor_protocol_emitter(mon);
423
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 if (!monitor_has_error(mon)) {
425 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300426 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 if (data) {
428 qobject_incref(data);
429 qdict_put_obj(qmp, "return", data);
430 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200431 /* return an empty QDict by default */
432 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200433 }
434 } else {
435 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300436 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 QDECREF(mon->error);
438 mon->error = NULL;
439 }
440
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200441 if (mon->mc->id) {
442 qdict_put_obj(qmp, "id", mon->mc->id);
443 mon->mc->id = NULL;
444 }
445
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200446 monitor_json_emitter(mon, QOBJECT(qmp));
447 QDECREF(qmp);
448}
449
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200450
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200451static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100452
453/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200454 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200455 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100456 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200457static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458{
459 Monitor *mon;
460
461 trace_monitor_protocol_event_emit(event, data);
462 QLIST_FOREACH(mon, &mon_list, entry) {
463 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
464 monitor_json_emitter(mon, data);
465 }
466 }
467}
468
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469/*
470 * Queue a new event for emission to Monitor instances,
471 * applying any rate limiting if required.
472 */
473static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200474monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100475{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200476 MonitorQAPIEventState *evstate;
477 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100478 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200480 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 trace_monitor_protocol_event_queue(event,
482 data,
483 evstate->rate,
484 evstate->last,
485 now);
486
487 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200488 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100489 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200490 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 evstate->last = now;
492 } else {
493 int64_t delta = now - evstate->last;
494 if (evstate->data ||
495 delta < evstate->rate) {
496 /* If there's an existing event pending, replace
497 * it with the new event, otherwise schedule a
498 * timer for delayed emission
499 */
500 if (evstate->data) {
501 qobject_decref(evstate->data);
502 } else {
503 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100504 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100505 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200506 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507 qobject_incref(evstate->data);
508 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200509 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510 evstate->last = now;
511 }
512 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200513 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514}
515
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516/*
517 * The callback invoked by QemuTimer when a delayed
518 * event is ready to be emitted
519 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200520static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100521{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200522 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100523 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100524
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525 trace_monitor_protocol_event_handler(evstate->event,
526 evstate->data,
527 evstate->last,
528 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200529 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100530 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200531 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100532 qobject_decref(evstate->data);
533 evstate->data = NULL;
534 }
535 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200536 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537}
538
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539/*
540 * @event: the event ID to be limited
541 * @rate: the rate limit in milliseconds
542 *
543 * Sets a rate limit on a particular event, so no
544 * more than 1 event will be emitted within @rate
545 * milliseconds
546 */
547static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200548monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200550 MonitorQAPIEventState *evstate;
551 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100552
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200553 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554
555 trace_monitor_protocol_event_throttle(event, rate);
556 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700557 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100559 evstate->last = 0;
560 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200561 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
562 SCALE_MS,
563 monitor_qapi_event_handler,
564 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100565}
566
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200567static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200569 /* Limit guest-triggerable events to 1 per second */
570 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200571 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200572 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200573 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
574 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200575 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100576
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200577 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200578}
579
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200580static int do_qmp_capabilities(Monitor *mon, const QDict *params,
581 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200582{
583 /* Will setup QMP capabilities in the future */
584 if (monitor_ctrl_mode(mon)) {
585 mon->mc->command_mode = 1;
586 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200587
588 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200589}
590
Luiz Capitulino0268d972010-10-22 10:08:02 -0200591static void handle_user_command(Monitor *mon, const char *cmdline);
592
Wenchao Xiab01fe892013-08-27 20:38:19 +0800593static void monitor_data_init(Monitor *mon)
594{
595 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200596 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800597 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800598 /* Use *mon_cmds by default. */
599 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800600}
601
602static void monitor_data_destroy(Monitor *mon)
603{
604 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200605 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800606}
607
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200608char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
609 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200611 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200612 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200613
Wenchao Xiab01fe892013-08-27 20:38:19 +0800614 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400615 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616
617 old_mon = cur_mon;
618 cur_mon = &hmp;
619
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 if (has_cpu_index) {
621 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 if (ret < 0) {
623 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200624 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
625 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626 goto out;
627 }
628 }
629
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200630 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200631 cur_mon = old_mon;
632
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200633 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400634 if (qstring_get_length(hmp.outbuf) > 0) {
635 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200636 } else {
637 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200638 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200639 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200640
641out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800642 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200643 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200644}
645
bellard9dc39cb2004-03-14 21:38:27 +0000646static int compare_cmd(const char *name, const char *list)
647{
648 const char *p, *pstart;
649 int len;
650 len = strlen(name);
651 p = list;
652 for(;;) {
653 pstart = p;
654 p = strchr(p, '|');
655 if (!p)
656 p = pstart + strlen(pstart);
657 if ((p - pstart) == len && !memcmp(pstart, name, len))
658 return 1;
659 if (*p == '\0')
660 break;
661 p++;
662 }
663 return 0;
664}
665
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800666static int get_str(char *buf, int buf_size, const char **pp)
667{
668 const char *p;
669 char *q;
670 int c;
671
672 q = buf;
673 p = *pp;
674 while (qemu_isspace(*p)) {
675 p++;
676 }
677 if (*p == '\0') {
678 fail:
679 *q = '\0';
680 *pp = p;
681 return -1;
682 }
683 if (*p == '\"') {
684 p++;
685 while (*p != '\0' && *p != '\"') {
686 if (*p == '\\') {
687 p++;
688 c = *p++;
689 switch (c) {
690 case 'n':
691 c = '\n';
692 break;
693 case 'r':
694 c = '\r';
695 break;
696 case '\\':
697 case '\'':
698 case '\"':
699 break;
700 default:
701 qemu_printf("unsupported escape code: '\\%c'\n", c);
702 goto fail;
703 }
704 if ((q - buf) < buf_size - 1) {
705 *q++ = c;
706 }
707 } else {
708 if ((q - buf) < buf_size - 1) {
709 *q++ = *p;
710 }
711 p++;
712 }
713 }
714 if (*p != '\"') {
715 qemu_printf("unterminated string\n");
716 goto fail;
717 }
718 p++;
719 } else {
720 while (*p != '\0' && !qemu_isspace(*p)) {
721 if ((q - buf) < buf_size - 1) {
722 *q++ = *p;
723 }
724 p++;
725 }
726 }
727 *q = '\0';
728 *pp = p;
729 return 0;
730}
731
732#define MAX_ARGS 16
733
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800734static void free_cmdline_args(char **args, int nb_args)
735{
736 int i;
737
738 assert(nb_args <= MAX_ARGS);
739
740 for (i = 0; i < nb_args; i++) {
741 g_free(args[i]);
742 }
743
744}
745
746/*
747 * Parse the command line to get valid args.
748 * @cmdline: command line to be parsed.
749 * @pnb_args: location to store the number of args, must NOT be NULL.
750 * @args: location to store the args, which should be freed by caller, must
751 * NOT be NULL.
752 *
753 * Returns 0 on success, negative on failure.
754 *
755 * NOTE: this parser is an approximate form of the real command parser. Number
756 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
757 * return with failure.
758 */
759static int parse_cmdline(const char *cmdline,
760 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761{
762 const char *p;
763 int nb_args, ret;
764 char buf[1024];
765
766 p = cmdline;
767 nb_args = 0;
768 for (;;) {
769 while (qemu_isspace(*p)) {
770 p++;
771 }
772 if (*p == '\0') {
773 break;
774 }
775 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800776 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800777 }
778 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800779 if (ret < 0) {
780 goto fail;
781 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800782 args[nb_args] = g_strdup(buf);
783 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800784 }
785 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800786 return 0;
787
788 fail:
789 free_cmdline_args(args, nb_args);
790 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800791}
792
Wenchao Xia66855492013-08-27 20:38:23 +0800793static void help_cmd_dump_one(Monitor *mon,
794 const mon_cmd_t *cmd,
795 char **prefix_args,
796 int prefix_args_nb)
797{
798 int i;
799
800 for (i = 0; i < prefix_args_nb; i++) {
801 monitor_printf(mon, "%s ", prefix_args[i]);
802 }
803 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
804}
805
806/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500807static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800808 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000809{
Anthony Liguoric227f092009-10-01 16:12:16 -0500810 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000811
Wenchao Xia66855492013-08-27 20:38:23 +0800812 /* No valid arg need to compare with, dump all in *cmds */
813 if (arg_index >= nb_args) {
814 for (cmd = cmds; cmd->name != NULL; cmd++) {
815 help_cmd_dump_one(mon, cmd, args, arg_index);
816 }
817 return;
818 }
819
820 /* Find one entry to dump */
821 for (cmd = cmds; cmd->name != NULL; cmd++) {
822 if (compare_cmd(args[arg_index], cmd->name)) {
823 if (cmd->sub_table) {
824 /* continue with next arg */
825 help_cmd_dump(mon, cmd->sub_table,
826 args, nb_args, arg_index + 1);
827 } else {
828 help_cmd_dump_one(mon, cmd, args, arg_index);
829 }
830 break;
831 }
bellard9dc39cb2004-03-14 21:38:27 +0000832 }
833}
834
aliguori376253e2009-03-05 23:01:23 +0000835static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000836{
Wenchao Xia66855492013-08-27 20:38:23 +0800837 char *args[MAX_ARGS];
838 int nb_args = 0;
839
840 /* 1. parse user input */
841 if (name) {
842 /* special case for log, directly dump and return */
843 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000844 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000845 monitor_printf(mon, "Log items (comma separated):\n");
846 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000847 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000849 }
Wenchao Xia66855492013-08-27 20:38:23 +0800850 return;
851 }
852
853 if (parse_cmdline(name, &nb_args, args) < 0) {
854 return;
bellardf193c792004-03-21 17:06:25 +0000855 }
bellard9dc39cb2004-03-14 21:38:27 +0000856 }
Wenchao Xia66855492013-08-27 20:38:23 +0800857
858 /* 2. dump the contents according to parsed args */
859 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
860
861 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000862}
863
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300864static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300865{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300866 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300867}
868
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100869static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530870{
871 const char *tp_name = qdict_get_str(qdict, "name");
872 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200873 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000874
Lluís Vilanova14101d02014-08-25 13:20:03 +0200875 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
876 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100877 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000878 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530879}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100880
Michael Rothc45a8162011-10-02 08:44:37 -0500881#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100882static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100883{
884 const char *op = qdict_get_try_str(qdict, "op");
885 const char *arg = qdict_get_try_str(qdict, "arg");
886
887 if (!op) {
888 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
889 } else if (!strcmp(op, "on")) {
890 st_set_trace_file_enabled(true);
891 } else if (!strcmp(op, "off")) {
892 st_set_trace_file_enabled(false);
893 } else if (!strcmp(op, "flush")) {
894 st_flush_trace_buffer();
895 } else if (!strcmp(op, "set")) {
896 if (arg) {
897 st_set_trace_file(arg);
898 }
899 } else {
900 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
901 help_cmd(mon, "trace-file");
902 }
903}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530904#endif
905
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100906static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000907{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300908 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000909}
910
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300911CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000912{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300913 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200914 const mon_cmd_t *cmd;
915
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300916 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200917 info = g_malloc0(sizeof(*info));
918 info->value = g_malloc0(sizeof(*info->value));
919 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200920
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300921 info->next = cmd_list;
922 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200923 }
924
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300925 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000926}
927
Daniel P. Berrange48608532012-05-21 17:59:51 +0100928EventInfoList *qmp_query_events(Error **errp)
929{
930 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200931 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100932
Wenchao Xia75175172014-06-18 08:43:54 +0200933 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
934 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100935 assert(event_name != NULL);
936 info = g_malloc0(sizeof(*info));
937 info->value = g_malloc0(sizeof(*info->value));
938 info->value->name = g_strdup(event_name);
939
940 info->next = ev_list;
941 ev_list = info;
942 }
943
944 return ev_list;
945}
946
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300947/* set the current CPU defined by the user */
948int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000949{
Andreas Färber55e5c282012-12-17 06:18:02 +0100950 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000951
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100952 cpu = qemu_get_cpu(cpu_index);
953 if (cpu == NULL) {
954 return -1;
bellard6a00d602005-11-21 23:25:50 +0000955 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200956 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100957 return 0;
bellard6a00d602005-11-21 23:25:50 +0000958}
959
Andreas Färber9349b4f2012-03-14 01:38:32 +0100960static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000961{
aliguori731b0362009-03-05 23:01:42 +0000962 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300963 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000964 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300965 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200966 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000967}
968
Luiz Capitulino99b77962011-10-24 10:53:44 -0200969int monitor_get_cpu_index(void)
970{
Andreas Färber55e5c282012-12-17 06:18:02 +0100971 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
972 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000976{
Andreas Färber878096e2013-05-27 01:33:50 +0200977 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100978 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000979 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200980 cpu = ENV_GET_CPU(env);
981 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000982}
983
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100984static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000985{
aliguori376253e2009-03-05 23:01:23 +0000986 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200987 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000988}
989
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100990static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300991{
992 dump_opcount_info((FILE *)mon, monitor_fprintf);
993}
994
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100995static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000996{
997 int i;
bellard7e2515e2004-08-01 21:52:19 +0000998 const char *str;
ths3b46e622007-09-17 08:09:54 +0000999
aliguoricde76ee2009-03-05 23:01:51 +00001000 if (!mon->rs)
1001 return;
bellard7e2515e2004-08-01 21:52:19 +00001002 i = 0;
1003 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001004 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001005 if (!str)
1006 break;
aliguori376253e2009-03-05 23:01:23 +00001007 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001008 i++;
bellardaa455482004-04-04 13:07:25 +00001009 }
1010}
1011
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001012static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001013{
Andreas Färber878096e2013-05-27 01:33:50 +02001014 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001015 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001016
1017 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001018 cpu = ENV_GET_CPU(env);
1019 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001020}
j_mayer76a66252007-03-07 08:32:30 +00001021
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001022static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301023{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001024 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1025 TraceEventInfoList *elem;
1026
1027 for (elem = events; elem != NULL; elem = elem->next) {
1028 monitor_printf(mon, "%s : state %u\n",
1029 elem->value->name,
1030 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1031 }
1032 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301033}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301034
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001035static int client_migrate_info(Monitor *mon, const QDict *qdict,
Markus Armbruster3b5704b2015-03-05 09:30:16 +01001036 QObject **ret_data)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001037{
1038 const char *protocol = qdict_get_str(qdict, "protocol");
1039 const char *hostname = qdict_get_str(qdict, "hostname");
1040 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1041 int port = qdict_get_try_int(qdict, "port", -1);
1042 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
Gonglei606ee8f2015-02-12 09:57:20 +08001043 Error *err = NULL;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001044 int ret;
1045
1046 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +01001047 if (!qemu_using_spice(&err)) {
1048 qerror_report_err(err);
Gonglei606ee8f2015-02-12 09:57:20 +08001049 error_free(err);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001050 return -1;
1051 }
1052
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001053 if (port == -1 && tls_port == -1) {
1054 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1055 return -1;
1056 }
1057
Markus Armbruster3b5704b2015-03-05 09:30:16 +01001058 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001059 if (ret != 0) {
1060 qerror_report(QERR_UNDEFINED_ERROR);
1061 return -1;
1062 }
1063 return 0;
1064 }
1065
1066 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1067 return -1;
1068}
1069
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001070static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001071{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001072 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001073}
1074
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001075static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001076{
1077 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001078 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001079
bellard9307c4c2004-04-04 12:57:25 +00001080 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001081 mask = 0;
1082 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001083 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001084 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001085 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001086 return;
1087 }
1088 }
Peter Maydell24537a02013-02-11 16:41:23 +00001089 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001090}
1091
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001092static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001093{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001095 if (!option || !strcmp(option, "on")) {
1096 singlestep = 1;
1097 } else if (!strcmp(option, "off")) {
1098 singlestep = 0;
1099 } else {
1100 monitor_printf(mon, "unexpected option %s\n", option);
1101 }
1102}
1103
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001104static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001105{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001106 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001107 if (!device)
1108 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1109 if (gdbserver_start(device) < 0) {
1110 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1111 device);
1112 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001113 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001114 } else {
aliguori59030a82009-04-05 18:43:41 +00001115 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1116 device);
bellard8a7ddc32004-03-31 19:00:16 +00001117 }
1118}
1119
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001120static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001121{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001122 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001123 if (select_watchdog_action(action) == -1) {
1124 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1125 }
1126}
1127
aliguori376253e2009-03-05 23:01:23 +00001128static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001129{
aliguori376253e2009-03-05 23:01:23 +00001130 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001131 switch(c) {
1132 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001133 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001134 break;
1135 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001136 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001137 break;
1138 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001139 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001140 break;
1141 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001142 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001143 break;
1144 default:
1145 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001147 } else {
aliguori376253e2009-03-05 23:01:23 +00001148 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001149 }
1150 break;
1151 }
aliguori376253e2009-03-05 23:01:23 +00001152 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001153}
1154
aliguori376253e2009-03-05 23:01:23 +00001155static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001156 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001157{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001158 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001159 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001160 uint8_t buf[16];
1161 uint64_t v;
1162
1163 if (format == 'i') {
1164 int flags;
1165 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001166 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001167#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001168 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001169 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001170 } else if (wsize == 4) {
1171 flags = 0;
1172 } else {
bellard6a15fd12006-04-12 21:07:07 +00001173 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001174 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001175 if (env) {
1176#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001177 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001178 (env->segs[R_CS].flags & DESC_L_MASK))
1179 flags = 2;
1180 else
1181#endif
1182 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1183 flags = 1;
1184 }
bellard4c27ba22004-04-25 18:05:08 +00001185 }
1186#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001187#ifdef TARGET_PPC
1188 flags = msr_le << 16;
1189 flags |= env->bfd_mach;
1190#endif
aliguori376253e2009-03-05 23:01:23 +00001191 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001192 return;
1193 }
1194
1195 len = wsize * count;
1196 if (wsize == 1)
1197 line_size = 8;
1198 else
1199 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001200 max_digits = 0;
1201
1202 switch(format) {
1203 case 'o':
1204 max_digits = (wsize * 8 + 2) / 3;
1205 break;
1206 default:
1207 case 'x':
1208 max_digits = (wsize * 8) / 4;
1209 break;
1210 case 'u':
1211 case 'd':
1212 max_digits = (wsize * 8 * 10 + 32) / 33;
1213 break;
1214 case 'c':
1215 wsize = 1;
1216 break;
1217 }
1218
1219 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001220 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001221 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001222 else
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001224 l = len;
1225 if (l > line_size)
1226 l = line_size;
1227 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001228 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001229 } else {
bellard6a00d602005-11-21 23:25:50 +00001230 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001231 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001233 break;
1234 }
bellard9307c4c2004-04-04 12:57:25 +00001235 }
ths5fafdf22007-09-16 21:08:06 +00001236 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001237 while (i < l) {
1238 switch(wsize) {
1239 default:
1240 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001241 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001242 break;
1243 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001244 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001245 break;
1246 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001247 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001248 break;
1249 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001250 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001251 break;
1252 }
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001254 switch(format) {
1255 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001257 break;
1258 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001265 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001269 break;
1270 }
1271 i += wsize;
1272 }
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001274 addr += l;
1275 len -= l;
1276 }
1277}
1278
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001279static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001280{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281 int count = qdict_get_int(qdict, "count");
1282 int format = qdict_get_int(qdict, "format");
1283 int size = qdict_get_int(qdict, "size");
1284 target_long addr = qdict_get_int(qdict, "addr");
1285
aliguori376253e2009-03-05 23:01:23 +00001286 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001287}
1288
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001289static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001290{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001291 int count = qdict_get_int(qdict, "count");
1292 int format = qdict_get_int(qdict, "format");
1293 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001294 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001295
aliguori376253e2009-03-05 23:01:23 +00001296 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001297}
1298
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001299static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001300{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001301 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001302 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001303
bellard9307c4c2004-04-04 12:57:25 +00001304 switch(format) {
1305 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001306 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001307 break;
1308 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001309 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001310 break;
1311 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001312 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001313 break;
1314 default:
1315 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001316 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001317 break;
1318 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001320 break;
1321 }
aliguori376253e2009-03-05 23:01:23 +00001322 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001323}
1324
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001325static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001326{
1327 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001328 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001329 uint32_t start = qdict_get_int(qdict, "start");
1330 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001331
1332 sum = 0;
1333 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001334 uint8_t val = address_space_ldub(&address_space_memory, addr,
1335 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001336 /* BSD sum algorithm ('sum' Unix command) */
1337 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001338 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001339 }
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001341}
1342
bellard13224a82006-07-14 22:03:35 +00001343static int mouse_button_state;
1344
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001345static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001346{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001347 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001348 const char *dx_str = qdict_get_str(qdict, "dx_str");
1349 const char *dy_str = qdict_get_str(qdict, "dy_str");
1350 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001351
bellard13224a82006-07-14 22:03:35 +00001352 dx = strtol(dx_str, NULL, 0);
1353 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001354 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1355 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1356
1357 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001358 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001359 if (dz != 0) {
1360 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1361 qemu_input_queue_btn(NULL, button, true);
1362 qemu_input_event_sync();
1363 qemu_input_queue_btn(NULL, button, false);
1364 }
1365 }
1366 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001367}
1368
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001369static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001370{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001371 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1372 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1373 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1374 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1375 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001376 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001377
1378 if (mouse_button_state == button_state) {
1379 return;
1380 }
1381 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1382 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001383 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001384}
1385
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001386static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001387{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001388 int size = qdict_get_int(qdict, "size");
1389 int addr = qdict_get_int(qdict, "addr");
1390 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001391 uint32_t val;
1392 int suffix;
1393
1394 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001395 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001396 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001397 addr++;
1398 }
1399 addr &= 0xffff;
1400
1401 switch(size) {
1402 default:
1403 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001404 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001405 suffix = 'b';
1406 break;
1407 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001408 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001409 suffix = 'w';
1410 break;
1411 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001412 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001413 suffix = 'l';
1414 break;
1415 }
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1417 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001418}
bellarda3a91a32004-06-04 11:06:21 +00001419
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001420static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001421{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001422 int size = qdict_get_int(qdict, "size");
1423 int addr = qdict_get_int(qdict, "addr");
1424 int val = qdict_get_int(qdict, "val");
1425
Jan Kiszkaf1147842009-07-14 10:20:11 +02001426 addr &= IOPORTS_MASK;
1427
1428 switch (size) {
1429 default:
1430 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001431 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001432 break;
1433 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001434 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001435 break;
1436 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001437 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001438 break;
1439 }
1440}
1441
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001442static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001443{
Gongleif1839932014-12-03 18:20:58 +00001444 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001445 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001446
Gongleif1839932014-12-03 18:20:58 +00001447 qemu_boot_set(bootdevice, &local_err);
1448 if (local_err) {
1449 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1450 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001451 } else {
Gongleif1839932014-12-03 18:20:58 +00001452 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001453 }
1454}
1455
bellardb86bda52004-09-18 19:32:46 +00001456#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001457static void print_pte(Monitor *mon, hwaddr addr,
1458 hwaddr pte,
1459 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001460{
Blue Swirld65aaf32010-12-11 18:56:24 +00001461#ifdef TARGET_X86_64
1462 if (addr & (1ULL << 47)) {
1463 addr |= -1LL << 48;
1464 }
1465#endif
1466 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1467 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001468 addr,
1469 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001470 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001471 pte & PG_GLOBAL_MASK ? 'G' : '-',
1472 pte & PG_PSE_MASK ? 'P' : '-',
1473 pte & PG_DIRTY_MASK ? 'D' : '-',
1474 pte & PG_ACCESSED_MASK ? 'A' : '-',
1475 pte & PG_PCD_MASK ? 'C' : '-',
1476 pte & PG_PWT_MASK ? 'T' : '-',
1477 pte & PG_USER_MASK ? 'U' : '-',
1478 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001479}
1480
Andreas Färber9349b4f2012-03-14 01:38:32 +01001481static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001482{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001483 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001484 uint32_t pgd, pde, pte;
1485
bellardb86bda52004-09-18 19:32:46 +00001486 pgd = env->cr[3] & ~0xfff;
1487 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001488 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001489 pde = le32_to_cpu(pde);
1490 if (pde & PG_PRESENT_MASK) {
1491 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001492 /* 4M pages */
1493 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001494 } else {
1495 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001496 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001497 pte = le32_to_cpu(pte);
1498 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001499 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001500 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001501 ~0xfff);
1502 }
1503 }
1504 }
1505 }
1506 }
1507}
1508
Andreas Färber9349b4f2012-03-14 01:38:32 +01001509static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001510{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001511 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001512 uint64_t pdpe, pde, pte;
1513 uint64_t pdp_addr, pd_addr, pt_addr;
1514
1515 pdp_addr = env->cr[3] & ~0x1f;
1516 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001517 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001518 pdpe = le64_to_cpu(pdpe);
1519 if (pdpe & PG_PRESENT_MASK) {
1520 pd_addr = pdpe & 0x3fffffffff000ULL;
1521 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001522 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001523 pde = le64_to_cpu(pde);
1524 if (pde & PG_PRESENT_MASK) {
1525 if (pde & PG_PSE_MASK) {
1526 /* 2M pages with PAE, CR4.PSE is ignored */
1527 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001528 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001529 } else {
1530 pt_addr = pde & 0x3fffffffff000ULL;
1531 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001532 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001533 pte = le64_to_cpu(pte);
1534 if (pte & PG_PRESENT_MASK) {
1535 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1536 + (l3 << 12),
1537 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001538 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001539 }
1540 }
1541 }
1542 }
1543 }
1544 }
1545 }
1546}
1547
1548#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001549static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001550{
1551 uint64_t l1, l2, l3, l4;
1552 uint64_t pml4e, pdpe, pde, pte;
1553 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1554
1555 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1556 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001557 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001558 pml4e = le64_to_cpu(pml4e);
1559 if (pml4e & PG_PRESENT_MASK) {
1560 pdp_addr = pml4e & 0x3fffffffff000ULL;
1561 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001562 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001563 pdpe = le64_to_cpu(pdpe);
1564 if (pdpe & PG_PRESENT_MASK) {
1565 if (pdpe & PG_PSE_MASK) {
1566 /* 1G pages, CR4.PSE is ignored */
1567 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1568 0x3ffffc0000000ULL);
1569 } else {
1570 pd_addr = pdpe & 0x3fffffffff000ULL;
1571 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001572 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001573 pde = le64_to_cpu(pde);
1574 if (pde & PG_PRESENT_MASK) {
1575 if (pde & PG_PSE_MASK) {
1576 /* 2M pages, CR4.PSE is ignored */
1577 print_pte(mon, (l1 << 39) + (l2 << 30) +
1578 (l3 << 21), pde,
1579 0x3ffffffe00000ULL);
1580 } else {
1581 pt_addr = pde & 0x3fffffffff000ULL;
1582 for (l4 = 0; l4 < 512; l4++) {
1583 cpu_physical_memory_read(pt_addr
1584 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001585 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001586 pte = le64_to_cpu(pte);
1587 if (pte & PG_PRESENT_MASK) {
1588 print_pte(mon, (l1 << 39) +
1589 (l2 << 30) +
1590 (l3 << 21) + (l4 << 12),
1591 pte & ~PG_PSE_MASK,
1592 0x3fffffffff000ULL);
1593 }
1594 }
1595 }
1596 }
1597 }
1598 }
1599 }
1600 }
1601 }
1602 }
1603}
1604#endif
1605
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001606static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001607{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001608 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001609
1610 env = mon_get_cpu();
1611
1612 if (!(env->cr[0] & CR0_PG_MASK)) {
1613 monitor_printf(mon, "PG disabled\n");
1614 return;
1615 }
1616 if (env->cr[4] & CR4_PAE_MASK) {
1617#ifdef TARGET_X86_64
1618 if (env->hflags & HF_LMA_MASK) {
1619 tlb_info_64(mon, env);
1620 } else
1621#endif
1622 {
1623 tlb_info_pae32(mon, env);
1624 }
1625 } else {
1626 tlb_info_32(mon, env);
1627 }
1628}
1629
Avi Kivitya8170e52012-10-23 12:30:10 +02001630static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001631 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001632 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001633{
bellard9746b152004-11-11 18:30:24 +00001634 int prot1;
1635 prot1 = *plast_prot;
1636 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001637 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001638 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1639 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001640 *pstart, end, end - *pstart,
1641 prot1 & PG_USER_MASK ? 'u' : '-',
1642 'r',
1643 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001644 }
1645 if (prot != 0)
1646 *pstart = end;
1647 else
1648 *pstart = -1;
1649 *plast_prot = prot;
1650 }
1651}
1652
Andreas Färber9349b4f2012-03-14 01:38:32 +01001653static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001654{
Austin Clementsb49ca722011-08-14 23:19:21 -04001655 unsigned int l1, l2;
1656 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001657 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001658 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001659
bellardb86bda52004-09-18 19:32:46 +00001660 pgd = env->cr[3] & ~0xfff;
1661 last_prot = 0;
1662 start = -1;
1663 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001664 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001665 pde = le32_to_cpu(pde);
1666 end = l1 << 22;
1667 if (pde & PG_PRESENT_MASK) {
1668 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1669 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001670 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001671 } else {
1672 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001673 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001674 pte = le32_to_cpu(pte);
1675 end = (l1 << 22) + (l2 << 12);
1676 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001677 prot = pte & pde &
1678 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001679 } else {
1680 prot = 0;
1681 }
aliguori376253e2009-03-05 23:01:23 +00001682 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001683 }
1684 }
1685 } else {
1686 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001687 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001688 }
1689 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001690 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001691 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001692}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001693
Andreas Färber9349b4f2012-03-14 01:38:32 +01001694static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001695{
Austin Clementsb49ca722011-08-14 23:19:21 -04001696 unsigned int l1, l2, l3;
1697 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001698 uint64_t pdpe, pde, pte;
1699 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001700 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001701
1702 pdp_addr = env->cr[3] & ~0x1f;
1703 last_prot = 0;
1704 start = -1;
1705 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001706 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001707 pdpe = le64_to_cpu(pdpe);
1708 end = l1 << 30;
1709 if (pdpe & PG_PRESENT_MASK) {
1710 pd_addr = pdpe & 0x3fffffffff000ULL;
1711 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001712 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001713 pde = le64_to_cpu(pde);
1714 end = (l1 << 30) + (l2 << 21);
1715 if (pde & PG_PRESENT_MASK) {
1716 if (pde & PG_PSE_MASK) {
1717 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1718 PG_PRESENT_MASK);
1719 mem_print(mon, &start, &last_prot, end, prot);
1720 } else {
1721 pt_addr = pde & 0x3fffffffff000ULL;
1722 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001723 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001724 pte = le64_to_cpu(pte);
1725 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1726 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001727 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1728 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001729 } else {
1730 prot = 0;
1731 }
1732 mem_print(mon, &start, &last_prot, end, prot);
1733 }
1734 }
1735 } else {
1736 prot = 0;
1737 mem_print(mon, &start, &last_prot, end, prot);
1738 }
1739 }
1740 } else {
1741 prot = 0;
1742 mem_print(mon, &start, &last_prot, end, prot);
1743 }
1744 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001745 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001746 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001747}
1748
1749
1750#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001751static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001752{
1753 int prot, last_prot;
1754 uint64_t l1, l2, l3, l4;
1755 uint64_t pml4e, pdpe, pde, pte;
1756 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1757
1758 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1759 last_prot = 0;
1760 start = -1;
1761 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001762 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001763 pml4e = le64_to_cpu(pml4e);
1764 end = l1 << 39;
1765 if (pml4e & PG_PRESENT_MASK) {
1766 pdp_addr = pml4e & 0x3fffffffff000ULL;
1767 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001768 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001769 pdpe = le64_to_cpu(pdpe);
1770 end = (l1 << 39) + (l2 << 30);
1771 if (pdpe & PG_PRESENT_MASK) {
1772 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001773 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1774 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001775 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001776 mem_print(mon, &start, &last_prot, end, prot);
1777 } else {
1778 pd_addr = pdpe & 0x3fffffffff000ULL;
1779 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001780 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001781 pde = le64_to_cpu(pde);
1782 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1783 if (pde & PG_PRESENT_MASK) {
1784 if (pde & PG_PSE_MASK) {
1785 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1786 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001787 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001788 mem_print(mon, &start, &last_prot, end, prot);
1789 } else {
1790 pt_addr = pde & 0x3fffffffff000ULL;
1791 for (l4 = 0; l4 < 512; l4++) {
1792 cpu_physical_memory_read(pt_addr
1793 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001794 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001795 pte = le64_to_cpu(pte);
1796 end = (l1 << 39) + (l2 << 30) +
1797 (l3 << 21) + (l4 << 12);
1798 if (pte & PG_PRESENT_MASK) {
1799 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1800 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001801 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001802 } else {
1803 prot = 0;
1804 }
1805 mem_print(mon, &start, &last_prot, end, prot);
1806 }
1807 }
1808 } else {
1809 prot = 0;
1810 mem_print(mon, &start, &last_prot, end, prot);
1811 }
1812 }
1813 }
1814 } else {
1815 prot = 0;
1816 mem_print(mon, &start, &last_prot, end, prot);
1817 }
1818 }
1819 } else {
1820 prot = 0;
1821 mem_print(mon, &start, &last_prot, end, prot);
1822 }
1823 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001824 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001825 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001826}
1827#endif
1828
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001829static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001830{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001831 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001832
1833 env = mon_get_cpu();
1834
1835 if (!(env->cr[0] & CR0_PG_MASK)) {
1836 monitor_printf(mon, "PG disabled\n");
1837 return;
1838 }
1839 if (env->cr[4] & CR4_PAE_MASK) {
1840#ifdef TARGET_X86_64
1841 if (env->hflags & HF_LMA_MASK) {
1842 mem_info_64(mon, env);
1843 } else
1844#endif
1845 {
1846 mem_info_pae32(mon, env);
1847 }
1848 } else {
1849 mem_info_32(mon, env);
1850 }
1851}
bellardb86bda52004-09-18 19:32:46 +00001852#endif
1853
aurel327c664e22009-03-03 06:12:22 +00001854#if defined(TARGET_SH4)
1855
aliguori376253e2009-03-05 23:01:23 +00001856static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001857{
aliguori376253e2009-03-05 23:01:23 +00001858 monitor_printf(mon, " tlb%i:\t"
1859 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1860 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1861 "dirty=%hhu writethrough=%hhu\n",
1862 idx,
1863 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1864 tlb->v, tlb->sh, tlb->c, tlb->pr,
1865 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001866}
1867
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001868static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001869{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001870 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001871 int i;
1872
aliguori376253e2009-03-05 23:01:23 +00001873 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001874 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001875 print_tlb (mon, i, &env->itlb[i]);
1876 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001877 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001878 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001879}
1880
1881#endif
1882
Max Filippov692f7372012-01-07 20:02:40 +04001883#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001884static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001885{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001886 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001887
1888 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1889}
1890#endif
1891
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001892static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001893{
1894 mtree_info((fprintf_function)monitor_printf, mon);
1895}
1896
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001897static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001898{
aliguorib28b6232009-04-22 20:20:29 +00001899 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001900 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001901 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001902
zhanghailiang5b009e42014-11-04 19:49:30 +08001903 node_mem = g_new0(uint64_t, nb_numa_nodes);
1904 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001905 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1906 for (i = 0; i < nb_numa_nodes; i++) {
1907 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001908 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001909 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001910 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001911 }
1912 }
1913 monitor_printf(mon, "\n");
1914 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001915 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001916 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001917 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001918}
1919
bellard5f1ce942006-02-08 22:40:15 +00001920#ifdef CONFIG_PROFILER
1921
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001922int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001923int64_t dev_time;
1924
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001925static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001926{
aliguori376253e2009-03-05 23:01:23 +00001927 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001928 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001929 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001930 tcg_time, tcg_time / (double)get_ticks_per_sec());
1931 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001932 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001933}
1934#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001935static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001936{
aliguori376253e2009-03-05 23:01:23 +00001937 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001938}
1939#endif
1940
bellardec36b692006-07-16 18:57:03 +00001941/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001942static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001943
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001944static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001945{
1946 int i;
1947 CaptureState *s;
1948
1949 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001950 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001951 s->ops.info (s->opaque);
1952 }
1953}
1954
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001955static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001956{
1957 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001958 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001959 CaptureState *s;
1960
1961 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1962 if (i == n) {
1963 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001964 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001965 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001966 return;
1967 }
1968 }
1969}
1970
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001971static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001972{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001973 const char *path = qdict_get_str(qdict, "path");
1974 int has_freq = qdict_haskey(qdict, "freq");
1975 int freq = qdict_get_try_int(qdict, "freq", -1);
1976 int has_bits = qdict_haskey(qdict, "bits");
1977 int bits = qdict_get_try_int(qdict, "bits", -1);
1978 int has_channels = qdict_haskey(qdict, "nchannels");
1979 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001980 CaptureState *s;
1981
Anthony Liguori7267c092011-08-20 22:09:37 -05001982 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001983
1984 freq = has_freq ? freq : 44100;
1985 bits = has_bits ? bits : 16;
1986 nchannels = has_channels ? nchannels : 2;
1987
1988 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001989 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001990 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001991 return;
bellardec36b692006-07-16 18:57:03 +00001992 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001993 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001994}
bellardec36b692006-07-16 18:57:03 +00001995
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001996static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001997{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001998 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001999
aliguori76655d62009-03-06 20:27:37 +00002000 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002001 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002002 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002003 return acl;
2004}
aliguori76655d62009-03-06 20:27:37 +00002005
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002006static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002007{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002008 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002009 qemu_acl *acl = find_acl(mon, aclname);
2010 qemu_acl_entry *entry;
2011 int i = 0;
2012
2013 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002014 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002015 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002016 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002017 i++;
2018 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002019 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002020 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002021 }
2022}
2023
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002024static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002025{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002026 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002027 qemu_acl *acl = find_acl(mon, aclname);
2028
2029 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002030 qemu_acl_reset(acl);
2031 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002032 }
2033}
aliguori76655d62009-03-06 20:27:37 +00002034
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002035static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002036{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002037 const char *aclname = qdict_get_str(qdict, "aclname");
2038 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002039 qemu_acl *acl = find_acl(mon, aclname);
2040
2041 if (acl) {
2042 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002043 acl->defaultDeny = 0;
2044 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002045 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002046 acl->defaultDeny = 1;
2047 monitor_printf(mon, "acl: policy set to 'deny'\n");
2048 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002049 monitor_printf(mon, "acl: unknown policy '%s', "
2050 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002051 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002052 }
2053}
aliguori76655d62009-03-06 20:27:37 +00002054
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002055static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002056{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002057 const char *aclname = qdict_get_str(qdict, "aclname");
2058 const char *match = qdict_get_str(qdict, "match");
2059 const char *policy = qdict_get_str(qdict, "policy");
2060 int has_index = qdict_haskey(qdict, "index");
2061 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002062 qemu_acl *acl = find_acl(mon, aclname);
2063 int deny, ret;
2064
2065 if (acl) {
2066 if (strcmp(policy, "allow") == 0) {
2067 deny = 0;
2068 } else if (strcmp(policy, "deny") == 0) {
2069 deny = 1;
2070 } else {
2071 monitor_printf(mon, "acl: unknown policy '%s', "
2072 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002073 return;
2074 }
aliguori28a76be2009-03-06 20:27:40 +00002075 if (has_index)
2076 ret = qemu_acl_insert(acl, deny, match, index);
2077 else
2078 ret = qemu_acl_append(acl, deny, match);
2079 if (ret < 0)
2080 monitor_printf(mon, "acl: unable to add acl entry\n");
2081 else
2082 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002083 }
2084}
aliguori76655d62009-03-06 20:27:37 +00002085
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002086static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002087{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002088 const char *aclname = qdict_get_str(qdict, "aclname");
2089 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002090 qemu_acl *acl = find_acl(mon, aclname);
2091 int ret;
aliguori76655d62009-03-06 20:27:37 +00002092
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002093 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002094 ret = qemu_acl_remove(acl, match);
2095 if (ret < 0)
2096 monitor_printf(mon, "acl: no matching acl entry\n");
2097 else
2098 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002099 }
2100}
2101
Huang Ying79c4f6b2009-06-23 10:05:14 +08002102#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002103static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002104{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002105 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002106 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002107 int cpu_index = qdict_get_int(qdict, "cpu_index");
2108 int bank = qdict_get_int(qdict, "bank");
2109 uint64_t status = qdict_get_int(qdict, "status");
2110 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2111 uint64_t addr = qdict_get_int(qdict, "addr");
2112 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002113 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002114
Jan Kiszka747461c2011-03-02 08:56:10 +01002115 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2116 flags |= MCE_INJECT_BROADCAST;
2117 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002118 cs = qemu_get_cpu(cpu_index);
2119 if (cs != NULL) {
2120 cpu = X86_CPU(cs);
2121 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2122 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002123 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002124}
2125#endif
2126
Corey Bryant208c9d12012-06-22 14:36:09 -04002127void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002128{
Anthony Liguoric227f092009-10-01 16:12:16 -05002129 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002130 int fd;
2131
Corey Bryant208c9d12012-06-22 14:36:09 -04002132 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002133 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002134 error_set(errp, QERR_FD_NOT_SUPPLIED);
2135 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002136 }
2137
2138 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002139 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002140 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2141 "a name not starting with a digit");
2142 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143 }
2144
Corey Bryant208c9d12012-06-22 14:36:09 -04002145 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002146 if (strcmp(monfd->name, fdname) != 0) {
2147 continue;
2148 }
2149
2150 close(monfd->fd);
2151 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002152 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002153 }
2154
Anthony Liguori7267c092011-08-20 22:09:37 -05002155 monfd = g_malloc0(sizeof(mon_fd_t));
2156 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002157 monfd->fd = fd;
2158
Corey Bryant208c9d12012-06-22 14:36:09 -04002159 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002160}
2161
Corey Bryant208c9d12012-06-22 14:36:09 -04002162void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002163{
Anthony Liguoric227f092009-10-01 16:12:16 -05002164 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002165
Corey Bryant208c9d12012-06-22 14:36:09 -04002166 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002167 if (strcmp(monfd->name, fdname) != 0) {
2168 continue;
2169 }
2170
Blue Swirl72cf2d42009-09-12 07:36:22 +00002171 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002172 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002173 g_free(monfd->name);
2174 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002175 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002176 }
2177
Corey Bryant208c9d12012-06-22 14:36:09 -04002178 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002179}
2180
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002181static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002182{
Luiz Capitulino13548692011-07-29 15:36:43 -03002183 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002184 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002185
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002186 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002187
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002188 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002189 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002190 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002191}
2192
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002193int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002194{
Anthony Liguoric227f092009-10-01 16:12:16 -05002195 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002196
Blue Swirl72cf2d42009-09-12 07:36:22 +00002197 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002198 int fd;
2199
2200 if (strcmp(monfd->name, fdname) != 0) {
2201 continue;
2202 }
2203
2204 fd = monfd->fd;
2205
2206 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002207 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002208 g_free(monfd->name);
2209 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002210
2211 return fd;
2212 }
2213
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002214 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002215 return -1;
2216}
2217
Corey Bryantba1c0482012-08-14 16:43:43 -04002218static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2219{
2220 MonFdsetFd *mon_fdset_fd;
2221 MonFdsetFd *mon_fdset_fd_next;
2222
2223 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002224 if ((mon_fdset_fd->removed ||
2225 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2226 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002227 close(mon_fdset_fd->fd);
2228 g_free(mon_fdset_fd->opaque);
2229 QLIST_REMOVE(mon_fdset_fd, next);
2230 g_free(mon_fdset_fd);
2231 }
2232 }
2233
Corey Bryantadb696f2012-08-14 16:43:47 -04002234 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002235 QLIST_REMOVE(mon_fdset, next);
2236 g_free(mon_fdset);
2237 }
2238}
2239
Corey Bryantefb87c12012-08-14 16:43:48 -04002240static void monitor_fdsets_cleanup(void)
2241{
2242 MonFdset *mon_fdset;
2243 MonFdset *mon_fdset_next;
2244
2245 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2246 monitor_fdset_cleanup(mon_fdset);
2247 }
2248}
2249
Corey Bryantba1c0482012-08-14 16:43:43 -04002250AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2251 const char *opaque, Error **errp)
2252{
2253 int fd;
2254 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002255 AddfdInfo *fdinfo;
2256
2257 fd = qemu_chr_fe_get_msgfd(mon->chr);
2258 if (fd == -1) {
2259 error_set(errp, QERR_FD_NOT_SUPPLIED);
2260 goto error;
2261 }
2262
Corey Bryante446f702012-10-18 15:19:32 -04002263 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2264 has_opaque, opaque, errp);
2265 if (fdinfo) {
2266 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002267 }
2268
Corey Bryantba1c0482012-08-14 16:43:43 -04002269error:
2270 if (fd != -1) {
2271 close(fd);
2272 }
2273 return NULL;
2274}
2275
2276void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2277{
2278 MonFdset *mon_fdset;
2279 MonFdsetFd *mon_fdset_fd;
2280 char fd_str[60];
2281
2282 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2283 if (mon_fdset->id != fdset_id) {
2284 continue;
2285 }
2286 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2287 if (has_fd) {
2288 if (mon_fdset_fd->fd != fd) {
2289 continue;
2290 }
2291 mon_fdset_fd->removed = true;
2292 break;
2293 } else {
2294 mon_fdset_fd->removed = true;
2295 }
2296 }
2297 if (has_fd && !mon_fdset_fd) {
2298 goto error;
2299 }
2300 monitor_fdset_cleanup(mon_fdset);
2301 return;
2302 }
2303
2304error:
2305 if (has_fd) {
2306 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2307 fdset_id, fd);
2308 } else {
2309 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2310 }
2311 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2312}
2313
2314FdsetInfoList *qmp_query_fdsets(Error **errp)
2315{
2316 MonFdset *mon_fdset;
2317 MonFdsetFd *mon_fdset_fd;
2318 FdsetInfoList *fdset_list = NULL;
2319
2320 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2321 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2322 FdsetFdInfoList *fdsetfd_list = NULL;
2323
2324 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2325 fdset_info->value->fdset_id = mon_fdset->id;
2326
2327 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2328 FdsetFdInfoList *fdsetfd_info;
2329
2330 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2331 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2332 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2333 if (mon_fdset_fd->opaque) {
2334 fdsetfd_info->value->has_opaque = true;
2335 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2336 } else {
2337 fdsetfd_info->value->has_opaque = false;
2338 }
2339
2340 fdsetfd_info->next = fdsetfd_list;
2341 fdsetfd_list = fdsetfd_info;
2342 }
2343
2344 fdset_info->value->fds = fdsetfd_list;
2345
2346 fdset_info->next = fdset_list;
2347 fdset_list = fdset_info;
2348 }
2349
2350 return fdset_list;
2351}
2352
Corey Bryante446f702012-10-18 15:19:32 -04002353AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2354 bool has_opaque, const char *opaque,
2355 Error **errp)
2356{
2357 MonFdset *mon_fdset = NULL;
2358 MonFdsetFd *mon_fdset_fd;
2359 AddfdInfo *fdinfo;
2360
2361 if (has_fdset_id) {
2362 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2363 /* Break if match found or match impossible due to ordering by ID */
2364 if (fdset_id <= mon_fdset->id) {
2365 if (fdset_id < mon_fdset->id) {
2366 mon_fdset = NULL;
2367 }
2368 break;
2369 }
2370 }
2371 }
2372
2373 if (mon_fdset == NULL) {
2374 int64_t fdset_id_prev = -1;
2375 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2376
2377 if (has_fdset_id) {
2378 if (fdset_id < 0) {
2379 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2380 "a non-negative value");
2381 return NULL;
2382 }
2383 /* Use specified fdset ID */
2384 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2385 mon_fdset_cur = mon_fdset;
2386 if (fdset_id < mon_fdset_cur->id) {
2387 break;
2388 }
2389 }
2390 } else {
2391 /* Use first available fdset ID */
2392 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2393 mon_fdset_cur = mon_fdset;
2394 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2395 fdset_id_prev = mon_fdset_cur->id;
2396 continue;
2397 }
2398 break;
2399 }
2400 }
2401
2402 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2403 if (has_fdset_id) {
2404 mon_fdset->id = fdset_id;
2405 } else {
2406 mon_fdset->id = fdset_id_prev + 1;
2407 }
2408
2409 /* The fdset list is ordered by fdset ID */
2410 if (!mon_fdset_cur) {
2411 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2412 } else if (mon_fdset->id < mon_fdset_cur->id) {
2413 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2414 } else {
2415 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2416 }
2417 }
2418
2419 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2420 mon_fdset_fd->fd = fd;
2421 mon_fdset_fd->removed = false;
2422 if (has_opaque) {
2423 mon_fdset_fd->opaque = g_strdup(opaque);
2424 }
2425 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2426
2427 fdinfo = g_malloc0(sizeof(*fdinfo));
2428 fdinfo->fdset_id = mon_fdset->id;
2429 fdinfo->fd = mon_fdset_fd->fd;
2430
2431 return fdinfo;
2432}
2433
Corey Bryantadb696f2012-08-14 16:43:47 -04002434int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2435{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002436#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002437 MonFdset *mon_fdset;
2438 MonFdsetFd *mon_fdset_fd;
2439 int mon_fd_flags;
2440
Corey Bryantadb696f2012-08-14 16:43:47 -04002441 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2442 if (mon_fdset->id != fdset_id) {
2443 continue;
2444 }
2445 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2446 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2447 if (mon_fd_flags == -1) {
2448 return -1;
2449 }
2450
2451 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2452 return mon_fdset_fd->fd;
2453 }
2454 }
2455 errno = EACCES;
2456 return -1;
2457 }
2458#endif
2459
2460 errno = ENOENT;
2461 return -1;
2462}
2463
2464int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2465{
2466 MonFdset *mon_fdset;
2467 MonFdsetFd *mon_fdset_fd_dup;
2468
2469 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2470 if (mon_fdset->id != fdset_id) {
2471 continue;
2472 }
2473 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2474 if (mon_fdset_fd_dup->fd == dup_fd) {
2475 return -1;
2476 }
2477 }
2478 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2479 mon_fdset_fd_dup->fd = dup_fd;
2480 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2481 return 0;
2482 }
2483 return -1;
2484}
2485
2486static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2487{
2488 MonFdset *mon_fdset;
2489 MonFdsetFd *mon_fdset_fd_dup;
2490
2491 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2492 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2493 if (mon_fdset_fd_dup->fd == dup_fd) {
2494 if (remove) {
2495 QLIST_REMOVE(mon_fdset_fd_dup, next);
2496 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2497 monitor_fdset_cleanup(mon_fdset);
2498 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002499 return -1;
2500 } else {
2501 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002502 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002503 }
2504 }
2505 }
2506 return -1;
2507}
2508
2509int monitor_fdset_dup_fd_find(int dup_fd)
2510{
2511 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2512}
2513
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002514void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002515{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002516 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002517}
2518
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002519int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002520{
2521 int fd;
2522 Error *local_err = NULL;
2523
2524 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002525 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002526 } else {
2527 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002528 if (fd == -1) {
2529 error_setg(&local_err, "Invalid file descriptor number '%s'",
2530 fdname);
2531 }
2532 }
2533 if (local_err) {
2534 error_propagate(errp, local_err);
2535 assert(fd == -1);
2536 } else {
2537 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002538 }
2539
2540 return fd;
2541}
2542
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002543/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002544static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 {
2546 .name = "version",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002550 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
2553 .name = "network",
2554 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .params = "",
2556 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002557 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 },
2559 {
2560 .name = "chardev",
2561 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 .params = "",
2563 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002564 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 },
2566 {
2567 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002568 .args_type = "nodes:-n,verbose:-v,device:B?",
2569 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002570 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002571 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002572 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 },
2574 {
2575 .name = "blockstats",
2576 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 .params = "",
2578 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002579 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002582 .name = "block-jobs",
2583 .args_type = "",
2584 .params = "",
2585 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002586 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002587 },
2588 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 .name = "registers",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002593 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
2595 {
2596 .name = "cpus",
2597 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 .params = "",
2599 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002600 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 },
2602 {
2603 .name = "history",
2604 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 .params = "",
2606 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002607 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002609#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2610 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 {
2612 .name = "irq",
2613 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 .params = "",
2615 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002616#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002617 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002618#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002619 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002620#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002621 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002622#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 },
2624 {
2625 .name = "pic",
2626 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 .params = "",
2628 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002629#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002630 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002631#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002632 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002633#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002634 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002635#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002637#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 {
2639 .name = "pci",
2640 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 .params = "",
2642 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002643 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002645#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002646 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 {
2648 .name = "tlb",
2649 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 .params = "",
2651 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002652 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 },
aurel327c664e22009-03-03 06:12:22 +00002654#endif
2655#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 {
2657 .name = "mem",
2658 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 .params = "",
2660 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002661 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 },
bellardb86bda52004-09-18 19:32:46 +00002663#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 {
Blue Swirl314e2982011-09-11 20:22:05 +00002665 .name = "mtree",
2666 .args_type = "",
2667 .params = "",
2668 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002669 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002670 },
2671 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002672 .name = "jit",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002676 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
Max Filippov246ae242014-11-02 11:04:18 +03002679 .name = "opcount",
2680 .args_type = "",
2681 .params = "",
2682 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002683 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002684 },
2685 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 .name = "kvm",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002690 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
2693 .name = "numa",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002697 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "usb",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002704 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "usbhost",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002711 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
2713 {
2714 .name = "profile",
2715 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 .params = "",
2717 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002718 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
2720 {
2721 .name = "capture",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002725 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 },
2727 {
2728 .name = "snapshots",
2729 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 .params = "",
2731 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002732 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 },
2734 {
2735 .name = "status",
2736 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002737 .params = "",
2738 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002739 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 },
2741 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 .name = "mice",
2743 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 .params = "",
2745 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002746 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 },
2748 {
2749 .name = "vnc",
2750 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 .params = "",
2752 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002753 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002755#if defined(CONFIG_SPICE)
2756 {
2757 .name = "spice",
2758 .args_type = "",
2759 .params = "",
2760 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002761 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002762 },
2763#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 {
2765 .name = "name",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002769 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
2771 {
2772 .name = "uuid",
2773 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 .params = "",
2775 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002776 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 {
2779 .name = "cpustats",
2780 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 .params = "",
2782 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002783 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 },
blueswir131a60e22007-10-26 18:42:59 +00002785#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002786 {
2787 .name = "usernet",
2788 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .params = "",
2790 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002791 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 },
blueswir131a60e22007-10-26 18:42:59 +00002793#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 {
2795 .name = "migrate",
2796 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 .params = "",
2798 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002799 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002800 },
2801 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002802 .name = "migrate_capabilities",
2803 .args_type = "",
2804 .params = "",
2805 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002806 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002807 },
2808 {
Liang Li50e9a622015-03-23 16:32:29 +08002809 .name = "migrate_parameters",
2810 .args_type = "",
2811 .params = "",
2812 .help = "show current migration parameters",
2813 .mhandler.cmd = hmp_info_migrate_parameters,
2814 },
2815 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002816 .name = "migrate_cache_size",
2817 .args_type = "",
2818 .params = "",
2819 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002820 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002821 },
2822 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 .name = "balloon",
2824 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 .params = "",
2826 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002827 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002828 },
2829 {
2830 .name = "qtree",
2831 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002832 .params = "",
2833 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002834 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002835 },
2836 {
2837 .name = "qdm",
2838 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002839 .params = "",
2840 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002841 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002842 },
2843 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002844 .name = "qom-tree",
2845 .args_type = "path:s?",
2846 .params = "[path]",
2847 .help = "show QOM composition tree",
2848 .mhandler.cmd = hmp_info_qom_tree,
2849 },
2850 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002851 .name = "roms",
2852 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002853 .params = "",
2854 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002855 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002856 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302857 {
2858 .name = "trace-events",
2859 .args_type = "",
2860 .params = "",
2861 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002862 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302863 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002865 .name = "tpm",
2866 .args_type = "",
2867 .params = "",
2868 .help = "show the TPM device",
2869 .mhandler.cmd = hmp_info_tpm,
2870 },
2871 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002872 .name = "memdev",
2873 .args_type = "",
2874 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002875 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002876 .mhandler.cmd = hmp_info_memdev,
2877 },
2878 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002879 .name = "memory-devices",
2880 .args_type = "",
2881 .params = "",
2882 .help = "show memory devices",
2883 .mhandler.cmd = hmp_info_memory_devices,
2884 },
2885 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002886 .name = NULL,
2887 },
bellard9dc39cb2004-03-14 21:38:27 +00002888};
2889
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002890/* mon_cmds and info_cmds would be sorted at runtime */
2891static mon_cmd_t mon_cmds[] = {
2892#include "hmp-commands.h"
2893 { NULL, NULL, },
2894};
2895
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002896static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002897#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002898 { /* NULL */ },
2899};
2900
bellard9307c4c2004-04-04 12:57:25 +00002901/*******************************************************************/
2902
2903static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002904static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002905
bellard92a31b12005-02-10 22:00:52 +00002906#define MD_TLONG 0
2907#define MD_I32 1
2908
bellard9307c4c2004-04-04 12:57:25 +00002909typedef struct MonitorDef {
2910 const char *name;
2911 int offset;
blueswir18662d652008-10-02 18:32:44 +00002912 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002913 int type;
bellard9307c4c2004-04-04 12:57:25 +00002914} MonitorDef;
2915
bellard57206fd2004-04-25 18:54:52 +00002916#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002917static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002918{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002919 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002920 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002921}
2922#endif
2923
bellarda541f292004-04-12 20:39:29 +00002924#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002925static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002926{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002927 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002928 unsigned int u;
2929 int i;
2930
2931 u = 0;
2932 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002933 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002934
2935 return u;
2936}
2937
blueswir18662d652008-10-02 18:32:44 +00002938static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002939{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002940 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002941 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002942}
2943
blueswir18662d652008-10-02 18:32:44 +00002944static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002945{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002946 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002947 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002948}
bellard9fddaa02004-05-21 12:59:32 +00002949
blueswir18662d652008-10-02 18:32:44 +00002950static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002951{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002952 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002953 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002954}
2955
blueswir18662d652008-10-02 18:32:44 +00002956static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002957{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002958 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002959 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002960}
2961
blueswir18662d652008-10-02 18:32:44 +00002962static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002963{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002964 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002965 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002966}
bellarda541f292004-04-12 20:39:29 +00002967#endif
2968
bellarde95c8d52004-09-30 22:22:08 +00002969#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002970#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002971static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002972{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002973 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002974
2975 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002976}
bellard7b936c02005-10-30 17:05:13 +00002977#endif
bellarde95c8d52004-09-30 22:22:08 +00002978
blueswir18662d652008-10-02 18:32:44 +00002979static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002980{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002981 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002982 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002983}
2984#endif
2985
blueswir18662d652008-10-02 18:32:44 +00002986static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002987#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002988
2989#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002990 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2991 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2992 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002993
Andreas Färbere59d1672012-02-16 00:40:47 +01002994 { "eax", offsetof(CPUX86State, regs[0]) },
2995 { "ecx", offsetof(CPUX86State, regs[1]) },
2996 { "edx", offsetof(CPUX86State, regs[2]) },
2997 { "ebx", offsetof(CPUX86State, regs[3]) },
2998 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2999 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3000 { "esi", offsetof(CPUX86State, regs[6]) },
3001 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003002#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003003 { "r8", offsetof(CPUX86State, regs[8]) },
3004 { "r9", offsetof(CPUX86State, regs[9]) },
3005 { "r10", offsetof(CPUX86State, regs[10]) },
3006 { "r11", offsetof(CPUX86State, regs[11]) },
3007 { "r12", offsetof(CPUX86State, regs[12]) },
3008 { "r13", offsetof(CPUX86State, regs[13]) },
3009 { "r14", offsetof(CPUX86State, regs[14]) },
3010 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003011#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003012 { "eflags", offsetof(CPUX86State, eflags) },
3013 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003014 SEG("cs", R_CS)
3015 SEG("ds", R_DS)
3016 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003017 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003018 SEG("fs", R_FS)
3019 SEG("gs", R_GS)
3020 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003021#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003022 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003023 { "r0", offsetof(CPUPPCState, gpr[0]) },
3024 { "r1", offsetof(CPUPPCState, gpr[1]) },
3025 { "r2", offsetof(CPUPPCState, gpr[2]) },
3026 { "r3", offsetof(CPUPPCState, gpr[3]) },
3027 { "r4", offsetof(CPUPPCState, gpr[4]) },
3028 { "r5", offsetof(CPUPPCState, gpr[5]) },
3029 { "r6", offsetof(CPUPPCState, gpr[6]) },
3030 { "r7", offsetof(CPUPPCState, gpr[7]) },
3031 { "r8", offsetof(CPUPPCState, gpr[8]) },
3032 { "r9", offsetof(CPUPPCState, gpr[9]) },
3033 { "r10", offsetof(CPUPPCState, gpr[10]) },
3034 { "r11", offsetof(CPUPPCState, gpr[11]) },
3035 { "r12", offsetof(CPUPPCState, gpr[12]) },
3036 { "r13", offsetof(CPUPPCState, gpr[13]) },
3037 { "r14", offsetof(CPUPPCState, gpr[14]) },
3038 { "r15", offsetof(CPUPPCState, gpr[15]) },
3039 { "r16", offsetof(CPUPPCState, gpr[16]) },
3040 { "r17", offsetof(CPUPPCState, gpr[17]) },
3041 { "r18", offsetof(CPUPPCState, gpr[18]) },
3042 { "r19", offsetof(CPUPPCState, gpr[19]) },
3043 { "r20", offsetof(CPUPPCState, gpr[20]) },
3044 { "r21", offsetof(CPUPPCState, gpr[21]) },
3045 { "r22", offsetof(CPUPPCState, gpr[22]) },
3046 { "r23", offsetof(CPUPPCState, gpr[23]) },
3047 { "r24", offsetof(CPUPPCState, gpr[24]) },
3048 { "r25", offsetof(CPUPPCState, gpr[25]) },
3049 { "r26", offsetof(CPUPPCState, gpr[26]) },
3050 { "r27", offsetof(CPUPPCState, gpr[27]) },
3051 { "r28", offsetof(CPUPPCState, gpr[28]) },
3052 { "r29", offsetof(CPUPPCState, gpr[29]) },
3053 { "r30", offsetof(CPUPPCState, gpr[30]) },
3054 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003055 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003056 { "f0", offsetof(CPUPPCState, fpr[0]) },
3057 { "f1", offsetof(CPUPPCState, fpr[1]) },
3058 { "f2", offsetof(CPUPPCState, fpr[2]) },
3059 { "f3", offsetof(CPUPPCState, fpr[3]) },
3060 { "f4", offsetof(CPUPPCState, fpr[4]) },
3061 { "f5", offsetof(CPUPPCState, fpr[5]) },
3062 { "f6", offsetof(CPUPPCState, fpr[6]) },
3063 { "f7", offsetof(CPUPPCState, fpr[7]) },
3064 { "f8", offsetof(CPUPPCState, fpr[8]) },
3065 { "f9", offsetof(CPUPPCState, fpr[9]) },
3066 { "f10", offsetof(CPUPPCState, fpr[10]) },
3067 { "f11", offsetof(CPUPPCState, fpr[11]) },
3068 { "f12", offsetof(CPUPPCState, fpr[12]) },
3069 { "f13", offsetof(CPUPPCState, fpr[13]) },
3070 { "f14", offsetof(CPUPPCState, fpr[14]) },
3071 { "f15", offsetof(CPUPPCState, fpr[15]) },
3072 { "f16", offsetof(CPUPPCState, fpr[16]) },
3073 { "f17", offsetof(CPUPPCState, fpr[17]) },
3074 { "f18", offsetof(CPUPPCState, fpr[18]) },
3075 { "f19", offsetof(CPUPPCState, fpr[19]) },
3076 { "f20", offsetof(CPUPPCState, fpr[20]) },
3077 { "f21", offsetof(CPUPPCState, fpr[21]) },
3078 { "f22", offsetof(CPUPPCState, fpr[22]) },
3079 { "f23", offsetof(CPUPPCState, fpr[23]) },
3080 { "f24", offsetof(CPUPPCState, fpr[24]) },
3081 { "f25", offsetof(CPUPPCState, fpr[25]) },
3082 { "f26", offsetof(CPUPPCState, fpr[26]) },
3083 { "f27", offsetof(CPUPPCState, fpr[27]) },
3084 { "f28", offsetof(CPUPPCState, fpr[28]) },
3085 { "f29", offsetof(CPUPPCState, fpr[29]) },
3086 { "f30", offsetof(CPUPPCState, fpr[30]) },
3087 { "f31", offsetof(CPUPPCState, fpr[31]) },
3088 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003089 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003090 { "nip|pc", offsetof(CPUPPCState, nip) },
3091 { "lr", offsetof(CPUPPCState, lr) },
3092 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003093 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003094 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003095 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003096 { "msr", 0, &monitor_get_msr, },
3097 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003098 { "tbu", 0, &monitor_get_tbu, },
3099 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003100 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003101 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3102 { "sr0", offsetof(CPUPPCState, sr[0]) },
3103 { "sr1", offsetof(CPUPPCState, sr[1]) },
3104 { "sr2", offsetof(CPUPPCState, sr[2]) },
3105 { "sr3", offsetof(CPUPPCState, sr[3]) },
3106 { "sr4", offsetof(CPUPPCState, sr[4]) },
3107 { "sr5", offsetof(CPUPPCState, sr[5]) },
3108 { "sr6", offsetof(CPUPPCState, sr[6]) },
3109 { "sr7", offsetof(CPUPPCState, sr[7]) },
3110 { "sr8", offsetof(CPUPPCState, sr[8]) },
3111 { "sr9", offsetof(CPUPPCState, sr[9]) },
3112 { "sr10", offsetof(CPUPPCState, sr[10]) },
3113 { "sr11", offsetof(CPUPPCState, sr[11]) },
3114 { "sr12", offsetof(CPUPPCState, sr[12]) },
3115 { "sr13", offsetof(CPUPPCState, sr[13]) },
3116 { "sr14", offsetof(CPUPPCState, sr[14]) },
3117 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003118 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003119 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003120
Andreas Färbere59d1672012-02-16 00:40:47 +01003121 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3122 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003123 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3124 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3125 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003126 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3127 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3128 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3129 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3130 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3131 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3132 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3133 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3134 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3135 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3136 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3137 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3138 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3139 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3140 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3141 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3142 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3143 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3144 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3145 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3146 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3147 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3148 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3149 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3150 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3151 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3152 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3153 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3154 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3155 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3156 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3157 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3158 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3159 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3160 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3161 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3162 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3163 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3164 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3165 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3166 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3167 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3168 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3169 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3170 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3171 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3172 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3173 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3174 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3175 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3176 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3177 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3178 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3179 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3180 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3181 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3182 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3183 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3184 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3185 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3186 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3187 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3188 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3189 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003190
bellarde95c8d52004-09-30 22:22:08 +00003191#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003192 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3193 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3194 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3195 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3196 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3197 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3198 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3199 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003200 { "o0", 0, monitor_get_reg },
3201 { "o1", 1, monitor_get_reg },
3202 { "o2", 2, monitor_get_reg },
3203 { "o3", 3, monitor_get_reg },
3204 { "o4", 4, monitor_get_reg },
3205 { "o5", 5, monitor_get_reg },
3206 { "o6", 6, monitor_get_reg },
3207 { "o7", 7, monitor_get_reg },
3208 { "l0", 8, monitor_get_reg },
3209 { "l1", 9, monitor_get_reg },
3210 { "l2", 10, monitor_get_reg },
3211 { "l3", 11, monitor_get_reg },
3212 { "l4", 12, monitor_get_reg },
3213 { "l5", 13, monitor_get_reg },
3214 { "l6", 14, monitor_get_reg },
3215 { "l7", 15, monitor_get_reg },
3216 { "i0", 16, monitor_get_reg },
3217 { "i1", 17, monitor_get_reg },
3218 { "i2", 18, monitor_get_reg },
3219 { "i3", 19, monitor_get_reg },
3220 { "i4", 20, monitor_get_reg },
3221 { "i5", 21, monitor_get_reg },
3222 { "i6", 22, monitor_get_reg },
3223 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003224 { "pc", offsetof(CPUSPARCState, pc) },
3225 { "npc", offsetof(CPUSPARCState, npc) },
3226 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003227#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003228 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003229 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003230#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003231 { "tbr", offsetof(CPUSPARCState, tbr) },
3232 { "fsr", offsetof(CPUSPARCState, fsr) },
3233 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3234 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3235 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3236 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3237 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3238 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3239 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3240 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3241 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3242 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3243 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3244 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3245 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3246 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3247 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3248 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3249 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3250 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3251 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3252 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3253 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3254 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3255 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3256 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3257 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3258 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3259 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3260 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3261 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3262 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3263 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3264 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003265#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003266 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3267 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3268 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3269 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3270 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3271 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3272 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3273 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3274 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3275 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3276 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3277 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3278 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3279 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3280 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3281 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3282 { "asi", offsetof(CPUSPARCState, asi) },
3283 { "pstate", offsetof(CPUSPARCState, pstate) },
3284 { "cansave", offsetof(CPUSPARCState, cansave) },
3285 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3286 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3287 { "wstate", offsetof(CPUSPARCState, wstate) },
3288 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3289 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003290#endif
bellard9307c4c2004-04-04 12:57:25 +00003291#endif
3292 { NULL },
3293};
3294
Stefan Weil9c3175c2013-08-22 21:30:09 +02003295static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3296expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003297{
Fam Zheng277acfe2013-08-20 10:58:21 +08003298 va_list ap;
3299 va_start(ap, fmt);
3300 monitor_vprintf(mon, fmt, ap);
3301 monitor_printf(mon, "\n");
3302 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003303 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003304}
3305
Markus Armbruster09b94182010-01-20 13:07:30 +01003306/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003307static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003308{
blueswir18662d652008-10-02 18:32:44 +00003309 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003310 void *ptr;
3311
bellard9307c4c2004-04-04 12:57:25 +00003312 for(md = monitor_defs; md->name != NULL; md++) {
3313 if (compare_cmd(name, md->name)) {
3314 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003315 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003316 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003317 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003318 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003319 switch(md->type) {
3320 case MD_I32:
3321 *pval = *(int32_t *)ptr;
3322 break;
3323 case MD_TLONG:
3324 *pval = *(target_long *)ptr;
3325 break;
3326 default:
3327 *pval = 0;
3328 break;
3329 }
bellard9307c4c2004-04-04 12:57:25 +00003330 }
3331 return 0;
3332 }
3333 }
3334 return -1;
3335}
3336
3337static void next(void)
3338{
Blue Swirl660f11b2009-07-31 21:16:51 +00003339 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003340 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003341 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003342 pch++;
3343 }
3344}
3345
aliguori376253e2009-03-05 23:01:23 +00003346static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003347
aliguori376253e2009-03-05 23:01:23 +00003348static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003349{
blueswir1c2efc952007-09-25 17:28:42 +00003350 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003351 char *p;
bellard6a00d602005-11-21 23:25:50 +00003352 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003353
3354 switch(*pch) {
3355 case '+':
3356 next();
aliguori376253e2009-03-05 23:01:23 +00003357 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003358 break;
3359 case '-':
3360 next();
aliguori376253e2009-03-05 23:01:23 +00003361 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003362 break;
3363 case '~':
3364 next();
aliguori376253e2009-03-05 23:01:23 +00003365 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003366 break;
3367 case '(':
3368 next();
aliguori376253e2009-03-05 23:01:23 +00003369 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003370 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003371 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003372 }
3373 next();
3374 break;
bellard81d09122004-07-14 17:21:37 +00003375 case '\'':
3376 pch++;
3377 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003378 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003379 n = *pch;
3380 pch++;
3381 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003382 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003383 next();
3384 break;
bellard9307c4c2004-04-04 12:57:25 +00003385 case '$':
3386 {
3387 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003388 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003389
bellard9307c4c2004-04-04 12:57:25 +00003390 pch++;
3391 q = buf;
3392 while ((*pch >= 'a' && *pch <= 'z') ||
3393 (*pch >= 'A' && *pch <= 'Z') ||
3394 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003395 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003396 if ((q - buf) < sizeof(buf) - 1)
3397 *q++ = *pch;
3398 pch++;
3399 }
blueswir1cd390082008-11-16 13:53:32 +00003400 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003401 pch++;
3402 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003403 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003404 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003405 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003406 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003407 }
3408 break;
3409 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003410 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003411 n = 0;
3412 break;
3413 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003414 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003415 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003416 if (errno == ERANGE) {
3417 expr_error(mon, "number too large");
3418 }
bellard9307c4c2004-04-04 12:57:25 +00003419 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003420 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003421 }
3422 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003423 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003424 pch++;
3425 break;
3426 }
3427 return n;
3428}
3429
3430
aliguori376253e2009-03-05 23:01:23 +00003431static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003432{
blueswir1c2efc952007-09-25 17:28:42 +00003433 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003434 int op;
ths3b46e622007-09-17 08:09:54 +00003435
aliguori376253e2009-03-05 23:01:23 +00003436 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003437 for(;;) {
3438 op = *pch;
3439 if (op != '*' && op != '/' && op != '%')
3440 break;
3441 next();
aliguori376253e2009-03-05 23:01:23 +00003442 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003443 switch(op) {
3444 default:
3445 case '*':
3446 val *= val2;
3447 break;
3448 case '/':
3449 case '%':
ths5fafdf22007-09-16 21:08:06 +00003450 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003451 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003452 if (op == '/')
3453 val /= val2;
3454 else
3455 val %= val2;
3456 break;
3457 }
3458 }
3459 return val;
3460}
3461
aliguori376253e2009-03-05 23:01:23 +00003462static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003463{
blueswir1c2efc952007-09-25 17:28:42 +00003464 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003465 int op;
bellard9307c4c2004-04-04 12:57:25 +00003466
aliguori376253e2009-03-05 23:01:23 +00003467 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003468 for(;;) {
3469 op = *pch;
3470 if (op != '&' && op != '|' && op != '^')
3471 break;
3472 next();
aliguori376253e2009-03-05 23:01:23 +00003473 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003474 switch(op) {
3475 default:
3476 case '&':
3477 val &= val2;
3478 break;
3479 case '|':
3480 val |= val2;
3481 break;
3482 case '^':
3483 val ^= val2;
3484 break;
3485 }
3486 }
3487 return val;
3488}
3489
aliguori376253e2009-03-05 23:01:23 +00003490static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003491{
blueswir1c2efc952007-09-25 17:28:42 +00003492 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003493 int op;
bellard9307c4c2004-04-04 12:57:25 +00003494
aliguori376253e2009-03-05 23:01:23 +00003495 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003496 for(;;) {
3497 op = *pch;
3498 if (op != '+' && op != '-')
3499 break;
3500 next();
aliguori376253e2009-03-05 23:01:23 +00003501 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003502 if (op == '+')
3503 val += val2;
3504 else
3505 val -= val2;
3506 }
3507 return val;
3508}
3509
aliguori376253e2009-03-05 23:01:23 +00003510static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003511{
3512 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003513 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003514 *pp = pch;
3515 return -1;
3516 }
blueswir1cd390082008-11-16 13:53:32 +00003517 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003518 pch++;
aliguori376253e2009-03-05 23:01:23 +00003519 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003520 *pp = pch;
3521 return 0;
3522}
3523
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003524static int get_double(Monitor *mon, double *pval, const char **pp)
3525{
3526 const char *p = *pp;
3527 char *tailp;
3528 double d;
3529
3530 d = strtod(p, &tailp);
3531 if (tailp == p) {
3532 monitor_printf(mon, "Number expected\n");
3533 return -1;
3534 }
3535 if (d != d || d - d != 0) {
3536 /* NaN or infinity */
3537 monitor_printf(mon, "Bad number\n");
3538 return -1;
3539 }
3540 *pval = d;
3541 *pp = tailp;
3542 return 0;
3543}
3544
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003545/*
3546 * Store the command-name in cmdname, and return a pointer to
3547 * the remaining of the command string.
3548 */
3549static const char *get_command_name(const char *cmdline,
3550 char *cmdname, size_t nlen)
3551{
3552 size_t len;
3553 const char *p, *pstart;
3554
3555 p = cmdline;
3556 while (qemu_isspace(*p))
3557 p++;
3558 if (*p == '\0')
3559 return NULL;
3560 pstart = p;
3561 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3562 p++;
3563 len = p - pstart;
3564 if (len > nlen - 1)
3565 len = nlen - 1;
3566 memcpy(cmdname, pstart, len);
3567 cmdname[len] = '\0';
3568 return p;
3569}
3570
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003571/**
3572 * Read key of 'type' into 'key' and return the current
3573 * 'type' pointer.
3574 */
3575static char *key_get_info(const char *type, char **key)
3576{
3577 size_t len;
3578 char *p, *str;
3579
3580 if (*type == ',')
3581 type++;
3582
3583 p = strchr(type, ':');
3584 if (!p) {
3585 *key = NULL;
3586 return NULL;
3587 }
3588 len = p - type;
3589
Anthony Liguori7267c092011-08-20 22:09:37 -05003590 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003591 memcpy(str, type, len);
3592 str[len] = '\0';
3593
3594 *key = str;
3595 return ++p;
3596}
3597
bellard9307c4c2004-04-04 12:57:25 +00003598static int default_fmt_format = 'x';
3599static int default_fmt_size = 4;
3600
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003601static int is_valid_option(const char *c, const char *typestr)
3602{
3603 char option[3];
3604
3605 option[0] = '-';
3606 option[1] = *c;
3607 option[2] = '\0';
3608
3609 typestr = strstr(typestr, option);
3610 return (typestr != NULL);
3611}
3612
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003613static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3614 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003615{
3616 const mon_cmd_t *cmd;
3617
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003618 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003619 if (compare_cmd(cmdname, cmd->name)) {
3620 return cmd;
3621 }
3622 }
3623
3624 return NULL;
3625}
3626
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003627static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3628{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003629 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003630}
3631
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003632/*
3633 * Parse @cmdline according to command table @table.
3634 * If @cmdline is blank, return NULL.
3635 * If it can't be parsed, report to @mon, and return NULL.
3636 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003637 * If a sub-command table exists, and if @cmdline contains an additional string
3638 * for a sub-command, this function will try to search the sub-command table.
3639 * If no additional string for a sub-command is present, this function will
3640 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003641 * Do not assume the returned command points into @table! It doesn't
3642 * when the command is a sub-command.
3643 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003644static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003645 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003646 int start,
3647 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003648 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003649{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003650 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003651 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003652 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003653 char cmdname[256];
3654 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003655 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003656
3657#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003658 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003659#endif
ths3b46e622007-09-17 08:09:54 +00003660
bellard9307c4c2004-04-04 12:57:25 +00003661 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003662 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003663 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003664 return NULL;
ths3b46e622007-09-17 08:09:54 +00003665
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003666 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003667 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003668 monitor_printf(mon, "unknown command: '%.*s'\n",
3669 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003670 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003671 }
bellard9307c4c2004-04-04 12:57:25 +00003672
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003673 /* filter out following useless space */
3674 while (qemu_isspace(*p)) {
3675 p++;
3676 }
3677 /* search sub command */
3678 if (cmd->sub_table != NULL) {
3679 /* check if user set additional command */
3680 if (*p == '\0') {
3681 return cmd;
3682 }
3683 return monitor_parse_command(mon, cmdline, p - cmdline,
3684 cmd->sub_table, qdict);
3685 }
3686
bellard9307c4c2004-04-04 12:57:25 +00003687 /* parse the parameters */
3688 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003689 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003690 typestr = key_get_info(typestr, &key);
3691 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003692 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003693 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003694 typestr++;
3695 switch(c) {
3696 case 'F':
bellard81d09122004-07-14 17:21:37 +00003697 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003698 case 's':
3699 {
3700 int ret;
ths3b46e622007-09-17 08:09:54 +00003701
blueswir1cd390082008-11-16 13:53:32 +00003702 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003703 p++;
3704 if (*typestr == '?') {
3705 typestr++;
3706 if (*p == '\0') {
3707 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003708 break;
bellard9307c4c2004-04-04 12:57:25 +00003709 }
3710 }
3711 ret = get_str(buf, sizeof(buf), &p);
3712 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003713 switch(c) {
3714 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003715 monitor_printf(mon, "%s: filename expected\n",
3716 cmdname);
bellard81d09122004-07-14 17:21:37 +00003717 break;
3718 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003719 monitor_printf(mon, "%s: block device name expected\n",
3720 cmdname);
bellard81d09122004-07-14 17:21:37 +00003721 break;
3722 default:
aliguori376253e2009-03-05 23:01:23 +00003723 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003724 break;
3725 }
bellard9307c4c2004-04-04 12:57:25 +00003726 goto fail;
3727 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003728 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003729 }
3730 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003731 case 'O':
3732 {
3733 QemuOptsList *opts_list;
3734 QemuOpts *opts;
3735
3736 opts_list = qemu_find_opts(key);
3737 if (!opts_list || opts_list->desc->name) {
3738 goto bad_type;
3739 }
3740 while (qemu_isspace(*p)) {
3741 p++;
3742 }
3743 if (!*p)
3744 break;
3745 if (get_str(buf, sizeof(buf), &p) < 0) {
3746 goto fail;
3747 }
3748 opts = qemu_opts_parse(opts_list, buf, 1);
3749 if (!opts) {
3750 goto fail;
3751 }
3752 qemu_opts_to_qdict(opts, qdict);
3753 qemu_opts_del(opts);
3754 }
3755 break;
bellard9307c4c2004-04-04 12:57:25 +00003756 case '/':
3757 {
3758 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003759
blueswir1cd390082008-11-16 13:53:32 +00003760 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003761 p++;
3762 if (*p == '/') {
3763 /* format found */
3764 p++;
3765 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003766 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003767 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003768 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003769 count = count * 10 + (*p - '0');
3770 p++;
3771 }
3772 }
3773 size = -1;
3774 format = -1;
3775 for(;;) {
3776 switch(*p) {
3777 case 'o':
3778 case 'd':
3779 case 'u':
3780 case 'x':
3781 case 'i':
3782 case 'c':
3783 format = *p++;
3784 break;
3785 case 'b':
3786 size = 1;
3787 p++;
3788 break;
3789 case 'h':
3790 size = 2;
3791 p++;
3792 break;
3793 case 'w':
3794 size = 4;
3795 p++;
3796 break;
3797 case 'g':
3798 case 'L':
3799 size = 8;
3800 p++;
3801 break;
3802 default:
3803 goto next;
3804 }
3805 }
3806 next:
blueswir1cd390082008-11-16 13:53:32 +00003807 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003808 monitor_printf(mon, "invalid char in format: '%c'\n",
3809 *p);
bellard9307c4c2004-04-04 12:57:25 +00003810 goto fail;
3811 }
bellard9307c4c2004-04-04 12:57:25 +00003812 if (format < 0)
3813 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003814 if (format != 'i') {
3815 /* for 'i', not specifying a size gives -1 as size */
3816 if (size < 0)
3817 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003818 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003819 }
bellard9307c4c2004-04-04 12:57:25 +00003820 default_fmt_format = format;
3821 } else {
3822 count = 1;
3823 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003824 if (format != 'i') {
3825 size = default_fmt_size;
3826 } else {
3827 size = -1;
3828 }
bellard9307c4c2004-04-04 12:57:25 +00003829 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003830 qdict_put(qdict, "count", qint_from_int(count));
3831 qdict_put(qdict, "format", qint_from_int(format));
3832 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003833 }
3834 break;
3835 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003836 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003837 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003838 {
blueswir1c2efc952007-09-25 17:28:42 +00003839 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003840
blueswir1cd390082008-11-16 13:53:32 +00003841 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003842 p++;
bellard34405572004-06-08 00:55:58 +00003843 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003844 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003845 if (*p == '\0') {
3846 typestr++;
3847 break;
3848 }
bellard34405572004-06-08 00:55:58 +00003849 } else {
3850 if (*p == '.') {
3851 p++;
blueswir1cd390082008-11-16 13:53:32 +00003852 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003853 p++;
bellard34405572004-06-08 00:55:58 +00003854 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003855 typestr++;
3856 break;
bellard34405572004-06-08 00:55:58 +00003857 }
3858 }
bellard13224a82006-07-14 22:03:35 +00003859 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003860 }
aliguori376253e2009-03-05 23:01:23 +00003861 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003862 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003863 /* Check if 'i' is greater than 32-bit */
3864 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3865 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3866 monitor_printf(mon, "integer is for 32-bit values\n");
3867 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003868 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003869 if (val < 0) {
3870 monitor_printf(mon, "enter a positive value\n");
3871 goto fail;
3872 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003873 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003874 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003875 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003876 }
3877 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003878 case 'o':
3879 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003880 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003881 char *end;
3882
3883 while (qemu_isspace(*p)) {
3884 p++;
3885 }
3886 if (*typestr == '?') {
3887 typestr++;
3888 if (*p == '\0') {
3889 break;
3890 }
3891 }
3892 val = strtosz(p, &end);
3893 if (val < 0) {
3894 monitor_printf(mon, "invalid size\n");
3895 goto fail;
3896 }
3897 qdict_put(qdict, key, qint_from_int(val));
3898 p = end;
3899 }
3900 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003901 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003902 {
3903 double val;
3904
3905 while (qemu_isspace(*p))
3906 p++;
3907 if (*typestr == '?') {
3908 typestr++;
3909 if (*p == '\0') {
3910 break;
3911 }
3912 }
3913 if (get_double(mon, &val, &p) < 0) {
3914 goto fail;
3915 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003916 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003917 switch (*p) {
3918 case 'm':
3919 val /= 1e3; p += 2; break;
3920 case 'u':
3921 val /= 1e6; p += 2; break;
3922 case 'n':
3923 val /= 1e9; p += 2; break;
3924 }
3925 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003926 if (*p && !qemu_isspace(*p)) {
3927 monitor_printf(mon, "Unknown unit suffix\n");
3928 goto fail;
3929 }
3930 qdict_put(qdict, key, qfloat_from_double(val));
3931 }
3932 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003933 case 'b':
3934 {
3935 const char *beg;
3936 int val;
3937
3938 while (qemu_isspace(*p)) {
3939 p++;
3940 }
3941 beg = p;
3942 while (qemu_isgraph(*p)) {
3943 p++;
3944 }
3945 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3946 val = 1;
3947 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3948 val = 0;
3949 } else {
3950 monitor_printf(mon, "Expected 'on' or 'off'\n");
3951 goto fail;
3952 }
3953 qdict_put(qdict, key, qbool_from_int(val));
3954 }
3955 break;
bellard9307c4c2004-04-04 12:57:25 +00003956 case '-':
3957 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003958 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003959 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003960 /* option */
ths3b46e622007-09-17 08:09:54 +00003961
bellard9307c4c2004-04-04 12:57:25 +00003962 c = *typestr++;
3963 if (c == '\0')
3964 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003965 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003966 p++;
bellard9307c4c2004-04-04 12:57:25 +00003967 if (*p == '-') {
3968 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003969 if(c != *p) {
3970 if(!is_valid_option(p, typestr)) {
3971
3972 monitor_printf(mon, "%s: unsupported option -%c\n",
3973 cmdname, *p);
3974 goto fail;
3975 } else {
3976 skip_key = 1;
3977 }
bellard9307c4c2004-04-04 12:57:25 +00003978 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003979 if(skip_key) {
3980 p = tmp;
3981 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003982 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003983 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003984 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003985 }
bellard9307c4c2004-04-04 12:57:25 +00003986 }
bellard9307c4c2004-04-04 12:57:25 +00003987 }
3988 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003989 case 'S':
3990 {
3991 /* package all remaining string */
3992 int len;
3993
3994 while (qemu_isspace(*p)) {
3995 p++;
3996 }
3997 if (*typestr == '?') {
3998 typestr++;
3999 if (*p == '\0') {
4000 /* no remaining string: NULL argument */
4001 break;
4002 }
4003 }
4004 len = strlen(p);
4005 if (len <= 0) {
4006 monitor_printf(mon, "%s: string expected\n",
4007 cmdname);
4008 break;
4009 }
4010 qdict_put(qdict, key, qstring_from_str(p));
4011 p += len;
4012 }
4013 break;
bellard9307c4c2004-04-04 12:57:25 +00004014 default:
4015 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004016 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004017 goto fail;
4018 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004019 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004020 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004021 }
4022 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004023 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004024 p++;
4025 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004026 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4027 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004028 goto fail;
4029 }
4030
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004031 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004032
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004033fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004034 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004035 return NULL;
4036}
4037
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004038void monitor_set_error(Monitor *mon, QError *qerror)
4039{
4040 /* report only the first error */
4041 if (!mon->error) {
4042 mon->error = qerror;
4043 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004044 QDECREF(qerror);
4045 }
4046}
4047
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004048static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004049{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004050 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004051 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004052
4053 qdict = qdict_new();
4054
Wenchao Xia77172392013-08-27 20:38:20 +08004055 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004056 if (!cmd)
4057 goto out;
4058
Markus Armbruster65207c52015-03-05 14:35:26 +01004059 if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004060 QObject *data = NULL;
4061
4062 /* XXX: ignores the error code */
4063 cmd->mhandler.cmd_new(mon, qdict, &data);
4064 assert(!monitor_has_error(mon));
4065 if (data) {
4066 cmd->user_print(mon, data);
4067 qobject_decref(data);
4068 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004069 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004070 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004071 }
4072
Luiz Capitulino13917be2009-10-07 13:41:54 -03004073out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004074 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004075}
4076
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004077static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004078{
4079 const char *p, *pstart;
4080 char cmd[128];
4081 int len;
4082
4083 p = list;
4084 for(;;) {
4085 pstart = p;
4086 p = strchr(p, '|');
4087 if (!p)
4088 p = pstart + strlen(pstart);
4089 len = p - pstart;
4090 if (len > sizeof(cmd) - 2)
4091 len = sizeof(cmd) - 2;
4092 memcpy(cmd, pstart, len);
4093 cmd[len] = '\0';
4094 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004095 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004096 }
4097 if (*p == '\0')
4098 break;
4099 p++;
4100 }
4101}
4102
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004103static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004104{
4105 DIR *ffs;
4106 struct dirent *d;
4107 char path[1024];
4108 char file[1024], file_prefix[1024];
4109 int input_path_len;
4110 const char *p;
4111
ths5fafdf22007-09-16 21:08:06 +00004112 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004113 if (!p) {
4114 input_path_len = 0;
4115 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004116 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004117 } else {
4118 input_path_len = p - input + 1;
4119 memcpy(path, input, input_path_len);
4120 if (input_path_len > sizeof(path) - 1)
4121 input_path_len = sizeof(path) - 1;
4122 path[input_path_len] = '\0';
4123 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4124 }
4125#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004126 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004127 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004128#endif
4129 ffs = opendir(path);
4130 if (!ffs)
4131 return;
4132 for(;;) {
4133 struct stat sb;
4134 d = readdir(ffs);
4135 if (!d)
4136 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004137
4138 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4139 continue;
4140 }
4141
bellard81d09122004-07-14 17:21:37 +00004142 if (strstart(d->d_name, file_prefix, NULL)) {
4143 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004144 if (input_path_len < sizeof(file))
4145 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4146 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004147 /* stat the file to find out if it's a directory.
4148 * In that case add a slash to speed up typing long paths
4149 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004150 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004151 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004152 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004153 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004154 }
4155 }
4156 closedir(ffs);
4157}
4158
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004159static const char *next_arg_type(const char *typestr)
4160{
4161 const char *p = strchr(typestr, ':');
4162 return (p != NULL ? ++p : typestr);
4163}
4164
Hani Benhabiles40d19392014-05-07 23:41:30 +01004165static void add_completion_option(ReadLineState *rs, const char *str,
4166 const char *option)
4167{
4168 if (!str || !option) {
4169 return;
4170 }
4171 if (!strncmp(option, str, strlen(str))) {
4172 readline_add_completion(rs, option);
4173 }
4174}
4175
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004176void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4177{
4178 size_t len;
4179 ChardevBackendInfoList *list, *start;
4180
4181 if (nb_args != 2) {
4182 return;
4183 }
4184 len = strlen(str);
4185 readline_set_completion_index(rs, len);
4186
4187 start = list = qmp_query_chardev_backends(NULL);
4188 while (list) {
4189 const char *chr_name = list->value->name;
4190
4191 if (!strncmp(chr_name, str, len)) {
4192 readline_add_completion(rs, chr_name);
4193 }
4194 list = list->next;
4195 }
4196 qapi_free_ChardevBackendInfoList(start);
4197}
4198
Hani Benhabilesb162b492014-05-07 23:41:31 +01004199void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4200{
4201 size_t len;
4202 int i;
4203
4204 if (nb_args != 2) {
4205 return;
4206 }
4207 len = strlen(str);
4208 readline_set_completion_index(rs, len);
4209 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4210 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4211 }
4212}
4213
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004214void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004215{
4216 GSList *list, *elt;
4217 size_t len;
4218
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004219 if (nb_args != 2) {
4220 return;
4221 }
4222
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004223 len = strlen(str);
4224 readline_set_completion_index(rs, len);
4225 list = elt = object_class_get_list(TYPE_DEVICE, false);
4226 while (elt) {
4227 const char *name;
4228 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4229 TYPE_DEVICE);
4230 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004231
4232 if (!dc->cannot_instantiate_with_device_add_yet
4233 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004234 readline_add_completion(rs, name);
4235 }
4236 elt = elt->next;
4237 }
4238 g_slist_free(list);
4239}
4240
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004241void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004242{
4243 GSList *list, *elt;
4244 size_t len;
4245
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004246 if (nb_args != 2) {
4247 return;
4248 }
4249
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004250 len = strlen(str);
4251 readline_set_completion_index(rs, len);
4252 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4253 while (elt) {
4254 const char *name;
4255
4256 name = object_class_get_name(OBJECT_CLASS(elt->data));
4257 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4258 readline_add_completion(rs, name);
4259 }
4260 elt = elt->next;
4261 }
4262 g_slist_free(list);
4263}
4264
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004265static void peripheral_device_del_completion(ReadLineState *rs,
4266 const char *str, size_t len)
4267{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004268 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4269 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004270
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004271 list = qdev_build_hotpluggable_device_list(peripheral);
4272 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004273 return;
4274 }
4275
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004276 for (item = list; item; item = g_slist_next(item)) {
4277 DeviceState *dev = item->data;
4278
4279 if (dev->id && !strncmp(str, dev->id, len)) {
4280 readline_add_completion(rs, dev->id);
4281 }
4282 }
4283
4284 g_slist_free(list);
4285}
4286
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004287void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4288{
4289 size_t len;
4290 ChardevInfoList *list, *start;
4291
4292 if (nb_args != 2) {
4293 return;
4294 }
4295 len = strlen(str);
4296 readline_set_completion_index(rs, len);
4297
4298 start = list = qmp_query_chardev(NULL);
4299 while (list) {
4300 ChardevInfo *chr = list->value;
4301
4302 if (!strncmp(chr->label, str, len)) {
4303 readline_add_completion(rs, chr->label);
4304 }
4305 list = list->next;
4306 }
4307 qapi_free_ChardevInfoList(start);
4308}
4309
Hani Benhabiles8e597772014-05-27 23:39:30 +01004310static void ringbuf_completion(ReadLineState *rs, const char *str)
4311{
4312 size_t len;
4313 ChardevInfoList *list, *start;
4314
4315 len = strlen(str);
4316 readline_set_completion_index(rs, len);
4317
4318 start = list = qmp_query_chardev(NULL);
4319 while (list) {
4320 ChardevInfo *chr_info = list->value;
4321
4322 if (!strncmp(chr_info->label, str, len)) {
4323 CharDriverState *chr = qemu_chr_find(chr_info->label);
4324 if (chr && chr_is_ringbuf(chr)) {
4325 readline_add_completion(rs, chr_info->label);
4326 }
4327 }
4328 list = list->next;
4329 }
4330 qapi_free_ChardevInfoList(start);
4331}
4332
Hani Benhabiles8e597772014-05-27 23:39:30 +01004333void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4334{
4335 if (nb_args != 2) {
4336 return;
4337 }
4338 ringbuf_completion(rs, str);
4339}
4340
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004341void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4342{
4343 size_t len;
4344
4345 if (nb_args != 2) {
4346 return;
4347 }
4348
4349 len = strlen(str);
4350 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004351 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004352}
4353
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004354void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004355{
4356 ObjectPropertyInfoList *list, *start;
4357 size_t len;
4358
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004359 if (nb_args != 2) {
4360 return;
4361 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004362 len = strlen(str);
4363 readline_set_completion_index(rs, len);
4364
4365 start = list = qmp_qom_list("/objects", NULL);
4366 while (list) {
4367 ObjectPropertyInfo *info = list->value;
4368
4369 if (!strncmp(info->type, "child<", 5)
4370 && !strncmp(info->name, str, len)) {
4371 readline_add_completion(rs, info->name);
4372 }
4373 list = list->next;
4374 }
4375 qapi_free_ObjectPropertyInfoList(start);
4376}
4377
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004378void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4379{
4380 int i;
4381 char *sep;
4382 size_t len;
4383
4384 if (nb_args != 2) {
4385 return;
4386 }
4387 sep = strrchr(str, '-');
4388 if (sep) {
4389 str = sep + 1;
4390 }
4391 len = strlen(str);
4392 readline_set_completion_index(rs, len);
4393 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4394 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4395 readline_add_completion(rs, QKeyCode_lookup[i]);
4396 }
4397 }
4398}
4399
Hani Benhabiles40d19392014-05-07 23:41:30 +01004400void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4401{
4402 size_t len;
4403
4404 len = strlen(str);
4405 readline_set_completion_index(rs, len);
4406 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004407 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004408 int count, i;
4409 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004410 NET_CLIENT_OPTIONS_KIND_NONE,
4411 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004412 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004413 const char *name = ncs[i]->name;
4414 if (!strncmp(str, name, len)) {
4415 readline_add_completion(rs, name);
4416 }
4417 }
4418 } else if (nb_args == 3) {
4419 add_completion_option(rs, str, "on");
4420 add_completion_option(rs, str, "off");
4421 }
4422}
4423
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004424void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4425{
4426 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004427 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004428
4429 if (nb_args != 2) {
4430 return;
4431 }
4432
4433 len = strlen(str);
4434 readline_set_completion_index(rs, len);
4435 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004436 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004437 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004438 QemuOpts *opts;
4439 const char *name = ncs[i]->name;
4440 if (strncmp(str, name, len)) {
4441 continue;
4442 }
4443 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4444 if (opts) {
4445 readline_add_completion(rs, name);
4446 }
4447 }
4448}
4449
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004450void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4451{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004452 int i;
4453
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004454 if (nb_args != 2) {
4455 return;
4456 }
4457 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004458 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4459 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4460 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004461}
4462
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004463void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4464 const char *str)
4465{
4466 size_t len;
4467
4468 len = strlen(str);
4469 readline_set_completion_index(rs, len);
4470 if (nb_args == 2) {
4471 int i;
4472 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4473 const char *name = MigrationCapability_lookup[i];
4474 if (!strncmp(str, name, len)) {
4475 readline_add_completion(rs, name);
4476 }
4477 }
4478 } else if (nb_args == 3) {
4479 add_completion_option(rs, str, "on");
4480 add_completion_option(rs, str, "off");
4481 }
4482}
4483
Liang Li50e9a622015-03-23 16:32:29 +08004484void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4485 const char *str)
4486{
4487 size_t len;
4488
4489 len = strlen(str);
4490 readline_set_completion_index(rs, len);
4491 if (nb_args == 2) {
4492 int i;
4493 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4494 const char *name = MigrationParameter_lookup[i];
4495 if (!strncmp(str, name, len)) {
4496 readline_add_completion(rs, name);
4497 }
4498 }
4499 }
4500}
4501
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004502void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4503{
4504 int i;
4505 size_t len;
4506 if (nb_args != 2) {
4507 return;
4508 }
4509 len = strlen(str);
4510 readline_set_completion_index(rs, len);
4511 for (i = 0; host_net_devices[i]; i++) {
4512 if (!strncmp(host_net_devices[i], str, len)) {
4513 readline_add_completion(rs, host_net_devices[i]);
4514 }
4515 }
4516}
4517
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004518void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4519{
Jason Wangeaed4832015-04-23 14:21:38 +08004520 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004521 int count, i, len;
4522
4523 len = strlen(str);
4524 readline_set_completion_index(rs, len);
4525 if (nb_args == 2) {
4526 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004527 NET_CLIENT_OPTIONS_KIND_NONE,
4528 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004529 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004530 int id;
4531 char name[16];
4532
4533 if (net_hub_id_for_client(ncs[i], &id)) {
4534 continue;
4535 }
4536 snprintf(name, sizeof(name), "%d", id);
4537 if (!strncmp(str, name, len)) {
4538 readline_add_completion(rs, name);
4539 }
4540 }
4541 return;
4542 } else if (nb_args == 3) {
4543 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004544 NET_CLIENT_OPTIONS_KIND_NIC,
4545 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004546 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004547 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004548 const char *name;
4549
Jason Wang2c4681f2015-02-02 15:06:38 +08004550 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4551 net_hub_id_for_client(ncs[i], &id)) {
4552 continue;
4553 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004554 name = ncs[i]->name;
4555 if (!strncmp(str, name, len)) {
4556 readline_add_completion(rs, name);
4557 }
4558 }
4559 return;
4560 }
4561}
4562
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004563static void vm_completion(ReadLineState *rs, const char *str)
4564{
4565 size_t len;
4566 BlockDriverState *bs = NULL;
4567
4568 len = strlen(str);
4569 readline_set_completion_index(rs, len);
4570 while ((bs = bdrv_next(bs))) {
4571 SnapshotInfoList *snapshots, *snapshot;
4572
4573 if (!bdrv_can_snapshot(bs)) {
4574 continue;
4575 }
4576 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4577 continue;
4578 }
4579 snapshot = snapshots;
4580 while (snapshot) {
4581 char *completion = snapshot->value->name;
4582 if (!strncmp(str, completion, len)) {
4583 readline_add_completion(rs, completion);
4584 }
4585 completion = snapshot->value->id;
4586 if (!strncmp(str, completion, len)) {
4587 readline_add_completion(rs, completion);
4588 }
4589 snapshot = snapshot->next;
4590 }
4591 qapi_free_SnapshotInfoList(snapshots);
4592 }
4593
4594}
4595
4596void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4597{
4598 if (nb_args == 2) {
4599 vm_completion(rs, str);
4600 }
4601}
4602
4603void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4604{
4605 if (nb_args == 2) {
4606 vm_completion(rs, str);
4607 }
4608}
4609
Wenchao Xiac35b6402013-08-27 20:38:24 +08004610static void monitor_find_completion_by_table(Monitor *mon,
4611 const mon_cmd_t *cmd_table,
4612 char **args,
4613 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004614{
4615 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004616 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004617 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004618 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004619 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004620
bellard81d09122004-07-14 17:21:37 +00004621 if (nb_args <= 1) {
4622 /* command completion */
4623 if (nb_args == 0)
4624 cmdname = "";
4625 else
4626 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004627 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004628 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004629 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004630 }
4631 } else {
4632 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004633 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004634 if (compare_cmd(args[0], cmd->name)) {
4635 break;
4636 }
bellard81d09122004-07-14 17:21:37 +00004637 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004638 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004639 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004640 }
4641
Wenchao Xiad903a772013-08-27 20:38:25 +08004642 if (cmd->sub_table) {
4643 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004644 monitor_find_completion_by_table(mon, cmd->sub_table,
4645 &args[1], nb_args - 1);
4646 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004647 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004648 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004649 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4650 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004651 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004652
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004653 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004654 for(i = 0; i < nb_args - 2; i++) {
4655 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004656 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004657 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004658 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004659 }
4660 }
4661 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004662 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004663 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004664 }
bellard81d09122004-07-14 17:21:37 +00004665 switch(*ptype) {
4666 case 'F':
4667 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004668 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004669 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004670 break;
4671 case 'B':
4672 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004673 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004674 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4675 name = bdrv_get_device_name(bs);
4676 if (str[0] == '\0' ||
4677 !strncmp(name, str, strlen(str))) {
4678 readline_add_completion(mon->rs, name);
4679 }
4680 }
bellard81d09122004-07-14 17:21:37 +00004681 break;
bellard7fe48482004-10-09 18:08:01 +00004682 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004683 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004684 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004685 monitor_find_completion_by_table(mon, cmd_table,
4686 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004687 }
4688 break;
bellard81d09122004-07-14 17:21:37 +00004689 default:
4690 break;
4691 }
4692 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004693}
4694
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004695static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004696 const char *cmdline)
4697{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004698 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004699 char *args[MAX_ARGS];
4700 int nb_args, len;
4701
4702 /* 1. parse the cmdline */
4703 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4704 return;
4705 }
4706#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004707 {
4708 int i;
4709 for (i = 0; i < nb_args; i++) {
4710 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4711 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004712 }
4713#endif
4714
4715 /* if the line ends with a space, it means we want to complete the
4716 next arg */
4717 len = strlen(cmdline);
4718 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4719 if (nb_args >= MAX_ARGS) {
4720 goto cleanup;
4721 }
4722 args[nb_args++] = g_strdup("");
4723 }
4724
4725 /* 2. auto complete according to args */
4726 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004727
4728cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004729 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004730}
4731
aliguori731b0362009-03-05 23:01:42 +00004732static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004733{
aliguori731b0362009-03-05 23:01:42 +00004734 Monitor *mon = opaque;
4735
Jan Kiszkac62313b2009-12-04 14:05:29 +01004736 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004737}
4738
Eric Blake2d5a8342015-04-15 09:19:23 -06004739static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004740{
Eric Blake2d5a8342015-04-15 09:19:23 -06004741 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
4742 if (is_cap && qmp_cmd_mode(mon)) {
4743 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
4744 "Capabilities negotiation is already complete, command "
4745 "'%s' ignored", cmd->name);
4746 return true;
4747 }
4748 if (!is_cap && !qmp_cmd_mode(mon)) {
4749 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
4750 "Expecting capabilities negotiation with "
4751 "'qmp_capabilities' before command '%s'", cmd->name);
4752 return true;
4753 }
4754 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004755}
4756
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004757/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004758 * Argument validation rules:
4759 *
4760 * 1. The argument must exist in cmd_args qdict
4761 * 2. The argument type must be the expected one
4762 *
4763 * Special case: If the argument doesn't exist in cmd_args and
4764 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4765 * checking is skipped for it.
4766 */
4767static int check_client_args_type(const QDict *client_args,
4768 const QDict *cmd_args, int flags)
4769{
4770 const QDictEntry *ent;
4771
4772 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4773 QObject *obj;
4774 QString *arg_type;
4775 const QObject *client_arg = qdict_entry_value(ent);
4776 const char *client_arg_name = qdict_entry_key(ent);
4777
4778 obj = qdict_get(cmd_args, client_arg_name);
4779 if (!obj) {
4780 if (flags & QMP_ACCEPT_UNKNOWNS) {
4781 /* handler accepts unknowns */
4782 continue;
4783 }
4784 /* client arg doesn't exist */
4785 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4786 return -1;
4787 }
4788
4789 arg_type = qobject_to_qstring(obj);
4790 assert(arg_type != NULL);
4791
4792 /* check if argument's type is correct */
4793 switch (qstring_get_str(arg_type)[0]) {
4794 case 'F':
4795 case 'B':
4796 case 's':
4797 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4798 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4799 "string");
4800 return -1;
4801 }
4802 break;
4803 case 'i':
4804 case 'l':
4805 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004806 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004807 if (qobject_type(client_arg) != QTYPE_QINT) {
4808 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4809 "int");
4810 return -1;
4811 }
4812 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004813 case 'T':
4814 if (qobject_type(client_arg) != QTYPE_QINT &&
4815 qobject_type(client_arg) != QTYPE_QFLOAT) {
4816 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4817 "number");
4818 return -1;
4819 }
4820 break;
4821 case 'b':
4822 case '-':
4823 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4824 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4825 "bool");
4826 return -1;
4827 }
4828 break;
4829 case 'O':
4830 assert(flags & QMP_ACCEPT_UNKNOWNS);
4831 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004832 case 'q':
4833 /* Any QObject can be passed. */
4834 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004835 case '/':
4836 case '.':
4837 /*
4838 * These types are not supported by QMP and thus are not
4839 * handled here. Fall through.
4840 */
4841 default:
4842 abort();
4843 }
4844 }
4845
4846 return 0;
4847}
4848
4849/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004850 * - Check if the client has passed all mandatory args
4851 * - Set special flags for argument validation
4852 */
4853static int check_mandatory_args(const QDict *cmd_args,
4854 const QDict *client_args, int *flags)
4855{
4856 const QDictEntry *ent;
4857
4858 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4859 const char *cmd_arg_name = qdict_entry_key(ent);
4860 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4861 assert(type != NULL);
4862
4863 if (qstring_get_str(type)[0] == 'O') {
4864 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4865 *flags |= QMP_ACCEPT_UNKNOWNS;
4866 } else if (qstring_get_str(type)[0] != '-' &&
4867 qstring_get_str(type)[1] != '?' &&
4868 !qdict_haskey(client_args, cmd_arg_name)) {
4869 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4870 return -1;
4871 }
4872 }
4873
4874 return 0;
4875}
4876
4877static QDict *qdict_from_args_type(const char *args_type)
4878{
4879 int i;
4880 QDict *qdict;
4881 QString *key, *type, *cur_qs;
4882
4883 assert(args_type != NULL);
4884
4885 qdict = qdict_new();
4886
4887 if (args_type == NULL || args_type[0] == '\0') {
4888 /* no args, empty qdict */
4889 goto out;
4890 }
4891
4892 key = qstring_new();
4893 type = qstring_new();
4894
4895 cur_qs = key;
4896
4897 for (i = 0;; i++) {
4898 switch (args_type[i]) {
4899 case ',':
4900 case '\0':
4901 qdict_put(qdict, qstring_get_str(key), type);
4902 QDECREF(key);
4903 if (args_type[i] == '\0') {
4904 goto out;
4905 }
4906 type = qstring_new(); /* qdict has ref */
4907 cur_qs = key = qstring_new();
4908 break;
4909 case ':':
4910 cur_qs = type;
4911 break;
4912 default:
4913 qstring_append_chr(cur_qs, args_type[i]);
4914 break;
4915 }
4916 }
4917
4918out:
4919 return qdict;
4920}
4921
4922/*
4923 * Client argument checking rules:
4924 *
4925 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004926 * 2. Each argument provided by the client must be expected
4927 * 3. Each argument provided by the client must have the type expected
4928 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004929 */
4930static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4931{
4932 int flags, err;
4933 QDict *cmd_args;
4934
4935 cmd_args = qdict_from_args_type(cmd->args_type);
4936
4937 flags = 0;
4938 err = check_mandatory_args(cmd_args, client_args, &flags);
4939 if (err) {
4940 goto out;
4941 }
4942
Luiz Capitulino4af91932010-06-22 11:44:05 -03004943 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004944
4945out:
4946 QDECREF(cmd_args);
4947 return err;
4948}
4949
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004950/*
4951 * Input object checking rules
4952 *
4953 * 1. Input object must be a dict
4954 * 2. The "execute" key must exist
4955 * 3. The "execute" key must be a string
4956 * 4. If the "arguments" key exists, it must be a dict
4957 * 5. If the "id" key exists, it can be anything (ie. json-value)
4958 * 6. Any argument not listed above is considered invalid
4959 */
4960static QDict *qmp_check_input_obj(QObject *input_obj)
4961{
4962 const QDictEntry *ent;
4963 int has_exec_key = 0;
4964 QDict *input_dict;
4965
4966 if (qobject_type(input_obj) != QTYPE_QDICT) {
4967 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4968 return NULL;
4969 }
4970
4971 input_dict = qobject_to_qdict(input_obj);
4972
4973 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4974 const char *arg_name = qdict_entry_key(ent);
4975 const QObject *arg_obj = qdict_entry_value(ent);
4976
4977 if (!strcmp(arg_name, "execute")) {
4978 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4979 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4980 "string");
4981 return NULL;
4982 }
4983 has_exec_key = 1;
4984 } else if (!strcmp(arg_name, "arguments")) {
4985 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4986 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4987 "object");
4988 return NULL;
4989 }
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004990 } else {
4991 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4992 return NULL;
4993 }
4994 }
4995
4996 if (!has_exec_key) {
4997 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4998 return NULL;
4999 }
5000
5001 return input_dict;
5002}
5003
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005004static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5005{
5006 int err;
Markus Armbruster84add862015-03-05 16:45:15 +01005007 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005008 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005009 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005010 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005011 Monitor *mon = cur_mon;
5012
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005013 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01005014 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005015
5016 obj = json_parser_parse(tokens, NULL);
5017 if (!obj) {
5018 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005019 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005020 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005021 }
5022
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005023 input = qmp_check_input_obj(obj);
5024 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005025 qobject_decref(obj);
5026 goto err_out;
5027 }
5028
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005029 mon->mc->id = qdict_get(input, "id");
5030 qobject_incref(mon->mc->id);
5031
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005032 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005033 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005034 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005035 if (!cmd) {
Markus Armbrustera6c90cb2015-01-13 16:16:35 +01005036 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
5037 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005038 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005039 }
Eric Blake2d5a8342015-04-15 09:19:23 -06005040 if (invalid_qmp_mode(mon, cmd)) {
5041 goto err_out;
5042 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005043
5044 obj = qdict_get(input, "arguments");
5045 if (!obj) {
5046 args = qdict_new();
5047 } else {
5048 args = qobject_to_qdict(obj);
5049 QINCREF(args);
5050 }
5051
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005052 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005053 if (err < 0) {
5054 goto err_out;
5055 }
5056
Markus Armbruster84add862015-03-05 16:45:15 +01005057 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5058 /* Command failed... */
5059 if (!monitor_has_error(mon)) {
5060 /* ... without setting an error, so make one up */
5061 qerror_report(QERR_UNDEFINED_ERROR);
5062 }
5063 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005064
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005065err_out:
Markus Armbruster84add862015-03-05 16:45:15 +01005066 monitor_protocol_emitter(mon, data);
5067 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005068 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005069 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005070}
5071
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005072/**
5073 * monitor_control_read(): Read and handle QMP input
5074 */
5075static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5076{
5077 Monitor *old_mon = cur_mon;
5078
5079 cur_mon = opaque;
5080
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005081 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005082
5083 cur_mon = old_mon;
5084}
5085
aliguori731b0362009-03-05 23:01:42 +00005086static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005087{
aliguori731b0362009-03-05 23:01:42 +00005088 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005089 int i;
aliguori376253e2009-03-05 23:01:23 +00005090
aliguori731b0362009-03-05 23:01:42 +00005091 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005092
aliguoricde76ee2009-03-05 23:01:51 +00005093 if (cur_mon->rs) {
5094 for (i = 0; i < size; i++)
5095 readline_handle_byte(cur_mon->rs, buf[i]);
5096 } else {
5097 if (size == 0 || buf[size - 1] != 0)
5098 monitor_printf(cur_mon, "corrupted command\n");
5099 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005100 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005101 }
aliguori731b0362009-03-05 23:01:42 +00005102
5103 cur_mon = old_mon;
5104}
aliguorid8f44602008-10-06 13:52:44 +00005105
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005106static void monitor_command_cb(void *opaque, const char *cmdline,
5107 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005108{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005109 Monitor *mon = opaque;
5110
aliguori731b0362009-03-05 23:01:42 +00005111 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005112 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005113 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005114}
5115
aliguoricde76ee2009-03-05 23:01:51 +00005116int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005117{
aliguoricde76ee2009-03-05 23:01:51 +00005118 if (!mon->rs)
5119 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005120 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005121 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005122}
5123
aliguori376253e2009-03-05 23:01:23 +00005124void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005125{
aliguoricde76ee2009-03-05 23:01:51 +00005126 if (!mon->rs)
5127 return;
aliguori731b0362009-03-05 23:01:42 +00005128 if (--mon->suspend_cnt == 0)
5129 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005130}
5131
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005132static QObject *get_qmp_greeting(void)
5133{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005134 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005135
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005136 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005137 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5138}
5139
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005140/**
5141 * monitor_control_event(): Print QMP gretting
5142 */
5143static void monitor_control_event(void *opaque, int event)
5144{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005145 QObject *data;
5146 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005147
Luiz Capitulino47116d12010-02-08 17:01:30 -02005148 switch (event) {
5149 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005150 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005151 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005152 monitor_json_emitter(mon, data);
5153 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005154 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005155 break;
5156 case CHR_EVENT_CLOSED:
5157 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05005158 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005159 mon_refcount--;
5160 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005161 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005162 }
5163}
5164
aliguori731b0362009-03-05 23:01:42 +00005165static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005166{
aliguori376253e2009-03-05 23:01:23 +00005167 Monitor *mon = opaque;
5168
aliguori2724b182009-03-05 23:01:47 +00005169 switch (event) {
5170 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005171 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005172 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005173 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005174 if (mon->reset_seen) {
5175 readline_restart(mon->rs);
5176 monitor_resume(mon);
5177 monitor_flush(mon);
5178 } else {
5179 mon->suspend_cnt = 0;
5180 }
aliguori2724b182009-03-05 23:01:47 +00005181 break;
ths86e94de2007-01-05 22:01:59 +00005182
aliguori2724b182009-03-05 23:01:47 +00005183 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005184 if (mon->reset_seen) {
5185 if (mon->suspend_cnt == 0) {
5186 monitor_printf(mon, "\n");
5187 }
5188 monitor_flush(mon);
5189 monitor_suspend(mon);
5190 } else {
5191 mon->suspend_cnt++;
5192 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005193 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005194 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005195 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005196 break;
5197
Amit Shahb6b8df52009-10-07 18:31:16 +05305198 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005199 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5200 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005201 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005202 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005203 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005204 }
5205 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005206 mon_refcount++;
5207 break;
5208
5209 case CHR_EVENT_CLOSED:
5210 mon_refcount--;
5211 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005212 break;
5213 }
ths86e94de2007-01-05 22:01:59 +00005214}
5215
Wayne Xia816f8922011-10-12 11:32:41 +08005216static int
5217compare_mon_cmd(const void *a, const void *b)
5218{
5219 return strcmp(((const mon_cmd_t *)a)->name,
5220 ((const mon_cmd_t *)b)->name);
5221}
5222
5223static void sortcmdlist(void)
5224{
5225 int array_num;
5226 int elem_size = sizeof(mon_cmd_t);
5227
5228 array_num = sizeof(mon_cmds)/elem_size-1;
5229 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5230
5231 array_num = sizeof(info_cmds)/elem_size-1;
5232 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5233}
5234
aliguori76655d62009-03-06 20:27:37 +00005235
5236/*
5237 * Local variables:
5238 * c-indent-level: 4
5239 * c-basic-offset: 4
5240 * tab-width: 8
5241 * End:
5242 */
5243
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005244/* These functions just adapt the readline interface in a typesafe way. We
5245 * could cast function pointers but that discards compiler checks.
5246 */
Stefan Weild5d15072014-01-25 18:18:23 +01005247static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5248 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005249{
5250 va_list ap;
5251 va_start(ap, fmt);
5252 monitor_vprintf(opaque, fmt, ap);
5253 va_end(ap);
5254}
5255
5256static void monitor_readline_flush(void *opaque)
5257{
5258 monitor_flush(opaque);
5259}
5260
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005261static void __attribute__((constructor)) monitor_lock_init(void)
5262{
5263 qemu_mutex_init(&monitor_lock);
5264}
5265
aliguori731b0362009-03-05 23:01:42 +00005266void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005267{
aliguori731b0362009-03-05 23:01:42 +00005268 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005269 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005270
5271 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005272 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005273 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005274 is_first_init = 0;
5275 }
aliguori87127162009-03-05 23:01:29 +00005276
Wenchao Xiab01fe892013-08-27 20:38:19 +08005277 mon = g_malloc(sizeof(*mon));
5278 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005279
aliguori87127162009-03-05 23:01:29 +00005280 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005281 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005282 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005283 mon->rs = readline_init(monitor_readline_printf,
5284 monitor_readline_flush,
5285 mon,
5286 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005287 monitor_read_command(mon, 0);
5288 }
aliguori87127162009-03-05 23:01:29 +00005289
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005290 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005291 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005292 /* Control mode requires special handlers */
5293 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5294 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005295 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05005296
5297 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005298 } else {
5299 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5300 monitor_event, mon);
5301 }
aliguori87127162009-03-05 23:01:29 +00005302
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005303 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005304 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005305 qemu_mutex_unlock(&monitor_lock);
5306
Markus Armbruster8631b602010-02-18 11:41:55 +01005307 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5308 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005309}
5310
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005311static void bdrv_password_cb(void *opaque, const char *password,
5312 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005313{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005314 Monitor *mon = opaque;
5315 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005316 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005317 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005318
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005319 bdrv_add_key(bs, password, &local_err);
5320 if (local_err) {
5321 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5322 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005323 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005324 }
aliguori731b0362009-03-05 23:01:42 +00005325 if (mon->password_completion_cb)
5326 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005327
aliguori731b0362009-03-05 23:01:42 +00005328 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005329}
aliguoric0f4ce72009-03-05 23:01:01 +00005330
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005331int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005332 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005333 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005334{
aliguoricde76ee2009-03-05 23:01:51 +00005335 int err;
5336
aliguori376253e2009-03-05 23:01:23 +00005337 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5338 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005339
aliguori731b0362009-03-05 23:01:42 +00005340 mon->password_completion_cb = completion_cb;
5341 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005342
aliguoricde76ee2009-03-05 23:01:51 +00005343 err = monitor_read_password(mon, bdrv_password_cb, bs);
5344
5345 if (err && completion_cb)
5346 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005347
5348 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005349}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005350
5351int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005352 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005353 void *opaque)
5354{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005355 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005356 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005357
Fam Zheng55606252015-03-02 19:36:46 +08005358 blk = blk_by_name(device);
5359 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005360 monitor_printf(mon, "Device not found %s\n", device);
5361 return -1;
5362 }
5363
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005364 bdrv_add_key(blk_bs(blk), NULL, &err);
5365 if (err) {
5366 error_free(err);
5367 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5368 }
5369
5370 if (completion_cb) {
5371 completion_cb(opaque, 0);
5372 }
5373 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005374}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005375
5376QemuOptsList qemu_mon_opts = {
5377 .name = "mon",
5378 .implied_opt_name = "chardev",
5379 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5380 .desc = {
5381 {
5382 .name = "mode",
5383 .type = QEMU_OPT_STRING,
5384 },{
5385 .name = "chardev",
5386 .type = QEMU_OPT_STRING,
5387 },{
5388 .name = "default",
5389 .type = QEMU_OPT_BOOL,
5390 },{
5391 .name = "pretty",
5392 .type = QEMU_OPT_BOOL,
5393 },
5394 { /* end of list */ }
5395 },
5396};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005397
5398#ifndef TARGET_I386
5399void qmp_rtc_reset_reinjection(Error **errp)
5400{
5401 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5402}
5403#endif