blob: 61ea3464e31d5eb0688bc8827436597299ef61a5 [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 Capitulino910df892009-10-07 13:41:51 -0300126 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800130 /* @sub_table is a list of 2nd level of commands. If it do not exist,
131 * mhandler should be used. If it exist, sub_table[?].mhandler should be
132 * used, and mhandler of 1st level plays the role of help function.
133 */
134 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100135 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500136} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000137
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500139typedef struct mon_fd_t mon_fd_t;
140struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141 char *name;
142 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144};
145
Corey Bryantba1c0482012-08-14 16:43:43 -0400146/* file descriptor associated with a file descriptor set */
147typedef struct MonFdsetFd MonFdsetFd;
148struct MonFdsetFd {
149 int fd;
150 bool removed;
151 char *opaque;
152 QLIST_ENTRY(MonFdsetFd) next;
153};
154
155/* file descriptor set containing fds passed via SCM_RIGHTS */
156typedef struct MonFdset MonFdset;
157struct MonFdset {
158 int64_t id;
159 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400160 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400161 QLIST_ENTRY(MonFdset) next;
162};
163
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200164typedef struct MonitorControl {
165 QObject *id;
166 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200167 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200168} MonitorControl;
169
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100170/*
171 * To prevent flooding clients, events can be throttled. The
172 * throttling is calculated globally, rather than per-Monitor
173 * instance.
174 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200175typedef struct MonitorQAPIEventState {
176 QAPIEvent event; /* Event being tracked */
177 int64_t rate; /* Minimum time (in ns) between two events */
178 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100179 QEMUTimer *timer; /* Timer for handling delayed events */
180 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200181} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100182
aliguori87127162009-03-05 23:01:29 +0000183struct Monitor {
184 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200185 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000186 int flags;
187 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400188 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200189
190 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400191 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200192 guint out_watch;
193
194 /* Read under either BQL or out_lock, written with BQL+out_lock. */
195 int mux_out;
196
aliguori731b0362009-03-05 23:01:42 +0000197 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200198 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200199 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200200 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000201 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800202 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200203 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500204 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000205 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000206};
207
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300208/* QMP checker flags */
209#define QMP_ACCEPT_UNKNOWNS 1
210
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200211/* Protects mon_list, monitor_event_state. */
212static QemuMutex monitor_lock;
213
Blue Swirl72cf2d42009-09-12 07:36:22 +0000214static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400215static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400216static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000217
Wayne Xia816f8922011-10-12 11:32:41 +0800218static mon_cmd_t mon_cmds[];
219static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000220
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300221static const mon_cmd_t qmp_cmds[];
222
Markus Armbruster8631b602010-02-18 11:41:55 +0100223Monitor *cur_mon;
224Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000225
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100226static void monitor_command_cb(void *opaque, const char *cmdline,
227 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000228
Luiz Capitulino09069b12010-02-04 18:10:06 -0200229static inline int qmp_cmd_mode(const Monitor *mon)
230{
231 return (mon->mc ? mon->mc->command_mode : 0);
232}
233
Luiz Capitulino418173c2009-11-26 22:58:51 -0200234/* Return true if in control mode, false otherwise */
235static inline int monitor_ctrl_mode(const Monitor *mon)
236{
237 return (mon->flags & MONITOR_USE_CONTROL);
238}
239
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100240/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
241int monitor_cur_is_qmp(void)
242{
243 return cur_mon && monitor_ctrl_mode(cur_mon);
244}
245
Anthony Liguori7060b472011-09-02 12:34:50 -0500246void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000247{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200248 if (!mon->rs)
249 return;
250
aliguori731b0362009-03-05 23:01:42 +0000251 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
252 if (show_prompt)
253 readline_show_prompt(mon->rs);
254}
bellard6a00d602005-11-21 23:25:50 +0000255
Anthony Liguori7060b472011-09-02 12:34:50 -0500256int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
257 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000258{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100259 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000260 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
261 /* prompt is printed on return from the command handler */
262 return 0;
263 } else {
264 monitor_printf(mon, "terminal does not support password prompting\n");
265 return -ENOTTY;
266 }
aliguoribb5fc202009-03-05 23:01:15 +0000267}
268
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200269static void monitor_flush_locked(Monitor *mon);
270
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100271static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
272 void *opaque)
273{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200274 Monitor *mon = opaque;
275
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200276 qemu_mutex_lock(&mon->out_lock);
277 mon->out_watch = 0;
278 monitor_flush_locked(mon);
279 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 return FALSE;
281}
282
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200283/* Called with mon->out_lock held. */
284static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000285{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400287 size_t len;
288 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400290 if (mon->skip_flush) {
291 return;
292 }
293
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 buf = qstring_get_str(mon->outbuf);
295 len = qstring_get_length(mon->outbuf);
296
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200297 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200299 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
300 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 QDECREF(mon->outbuf);
302 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100303 return;
304 }
305 if (rc > 0) {
306 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 QString *tmp = qstring_from_str(buf + rc);
308 QDECREF(mon->outbuf);
309 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100310 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200311 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200312 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200313 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200314 }
bellard7e2515e2004-08-01 21:52:19 +0000315 }
316}
317
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318void monitor_flush(Monitor *mon)
319{
320 qemu_mutex_lock(&mon->out_lock);
321 monitor_flush_locked(mon);
322 qemu_mutex_unlock(&mon->out_lock);
323}
324
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400325/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000326static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000327{
ths60fe76f2007-12-16 03:02:09 +0000328 char c;
aliguori731b0362009-03-05 23:01:42 +0000329
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200330 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000331 for(;;) {
332 c = *str++;
333 if (c == '\0')
334 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400335 if (c == '\n') {
336 qstring_append_chr(mon->outbuf, '\r');
337 }
338 qstring_append_chr(mon->outbuf, c);
339 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200340 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400341 }
bellard7e2515e2004-08-01 21:52:19 +0000342 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000344}
345
aliguori376253e2009-03-05 23:01:23 +0000346void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000347{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200349
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200350 if (!mon)
351 return;
352
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200355 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400357 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000360}
361
aliguori376253e2009-03-05 23:01:23 +0000362void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000363{
364 va_list ap;
365 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000366 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000367 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000368}
369
Stefan Weil8b7968f2010-09-23 21:28:05 +0200370static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
371 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000372{
373 va_list ap;
374 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000376 va_end(ap);
377 return 0;
378}
379
Luiz Capitulino8204a912009-11-18 23:05:31 -0200380static inline int monitor_has_error(const Monitor *mon)
381{
382 return mon->error != NULL;
383}
384
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200385static void monitor_json_emitter(Monitor *mon, const QObject *data)
386{
387 QString *json;
388
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200389 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
390 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 assert(json != NULL);
392
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200393 qstring_append_chr(json, '\n');
394 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200395
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396 QDECREF(json);
397}
398
Luiz Capitulinode253f12012-07-27 16:18:16 -0300399static QDict *build_qmp_error_dict(const QError *err)
400{
401 QObject *obj;
402
403 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
404 ErrorClass_lookup[err->err_class],
405 qerror_human(err));
406
407 return qobject_to_qdict(obj);
408}
409
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200410static void monitor_protocol_emitter(Monitor *mon, QObject *data)
411{
412 QDict *qmp;
413
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100414 trace_monitor_protocol_emitter(mon);
415
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200416 if (!monitor_has_error(mon)) {
417 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300418 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200419 if (data) {
420 qobject_incref(data);
421 qdict_put_obj(qmp, "return", data);
422 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200423 /* return an empty QDict by default */
424 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426 } else {
427 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300428 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200429 QDECREF(mon->error);
430 mon->error = NULL;
431 }
432
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200433 if (mon->mc->id) {
434 qdict_put_obj(qmp, "id", mon->mc->id);
435 mon->mc->id = NULL;
436 }
437
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200438 monitor_json_emitter(mon, QOBJECT(qmp));
439 QDECREF(qmp);
440}
441
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200442
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200443static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444
445/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200446 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200447 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100448 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200449static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100450{
451 Monitor *mon;
452
453 trace_monitor_protocol_event_emit(event, data);
454 QLIST_FOREACH(mon, &mon_list, entry) {
455 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
456 monitor_json_emitter(mon, data);
457 }
458 }
459}
460
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100461/*
462 * Queue a new event for emission to Monitor instances,
463 * applying any rate limiting if required.
464 */
465static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100467{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200468 MonitorQAPIEventState *evstate;
469 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100470 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200472 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100473 trace_monitor_protocol_event_queue(event,
474 data,
475 evstate->rate,
476 evstate->last,
477 now);
478
479 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200480 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200482 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100483 evstate->last = now;
484 } else {
485 int64_t delta = now - evstate->last;
486 if (evstate->data ||
487 delta < evstate->rate) {
488 /* If there's an existing event pending, replace
489 * it with the new event, otherwise schedule a
490 * timer for delayed emission
491 */
492 if (evstate->data) {
493 qobject_decref(evstate->data);
494 } else {
495 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100496 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200498 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100499 qobject_incref(evstate->data);
500 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200501 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502 evstate->last = now;
503 }
504 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200505 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100506}
507
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100508/*
509 * The callback invoked by QemuTimer when a delayed
510 * event is ready to be emitted
511 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200512static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100513{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200514 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100515 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100517 trace_monitor_protocol_event_handler(evstate->event,
518 evstate->data,
519 evstate->last,
520 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200521 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200523 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100524 qobject_decref(evstate->data);
525 evstate->data = NULL;
526 }
527 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200528 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100529}
530
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100531/*
532 * @event: the event ID to be limited
533 * @rate: the rate limit in milliseconds
534 *
535 * Sets a rate limit on a particular event, so no
536 * more than 1 event will be emitted within @rate
537 * milliseconds
538 */
539static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200540monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100541{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200542 MonitorQAPIEventState *evstate;
543 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200545 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100546
547 trace_monitor_protocol_event_throttle(event, rate);
548 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700549 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100550 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100551 evstate->last = 0;
552 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200553 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
554 SCALE_MS,
555 monitor_qapi_event_handler,
556 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100557}
558
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200559static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100560{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200561 /* Limit guest-triggerable events to 1 per second */
562 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200564 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200565 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
566 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200567 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200569 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200570}
571
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200572static int do_qmp_capabilities(Monitor *mon, const QDict *params,
573 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200574{
575 /* Will setup QMP capabilities in the future */
576 if (monitor_ctrl_mode(mon)) {
577 mon->mc->command_mode = 1;
578 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200579
580 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200581}
582
Luiz Capitulino0268d972010-10-22 10:08:02 -0200583static void handle_user_command(Monitor *mon, const char *cmdline);
584
Wenchao Xiab01fe892013-08-27 20:38:19 +0800585static void monitor_data_init(Monitor *mon)
586{
587 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200588 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800589 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800590 /* Use *mon_cmds by default. */
591 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800592}
593
594static void monitor_data_destroy(Monitor *mon)
595{
596 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200597 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598}
599
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200600char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
601 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200602{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200603 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200604 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200605
Wenchao Xiab01fe892013-08-27 20:38:19 +0800606 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400607 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608
609 old_mon = cur_mon;
610 cur_mon = &hmp;
611
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200612 if (has_cpu_index) {
613 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200614 if (ret < 0) {
615 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200616 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
617 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200618 goto out;
619 }
620 }
621
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200622 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623 cur_mon = old_mon;
624
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200625 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400626 if (qstring_get_length(hmp.outbuf) > 0) {
627 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200628 } else {
629 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200630 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200631 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200632
633out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800634 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200635 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200636}
637
bellard9dc39cb2004-03-14 21:38:27 +0000638static int compare_cmd(const char *name, const char *list)
639{
640 const char *p, *pstart;
641 int len;
642 len = strlen(name);
643 p = list;
644 for(;;) {
645 pstart = p;
646 p = strchr(p, '|');
647 if (!p)
648 p = pstart + strlen(pstart);
649 if ((p - pstart) == len && !memcmp(pstart, name, len))
650 return 1;
651 if (*p == '\0')
652 break;
653 p++;
654 }
655 return 0;
656}
657
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800658static int get_str(char *buf, int buf_size, const char **pp)
659{
660 const char *p;
661 char *q;
662 int c;
663
664 q = buf;
665 p = *pp;
666 while (qemu_isspace(*p)) {
667 p++;
668 }
669 if (*p == '\0') {
670 fail:
671 *q = '\0';
672 *pp = p;
673 return -1;
674 }
675 if (*p == '\"') {
676 p++;
677 while (*p != '\0' && *p != '\"') {
678 if (*p == '\\') {
679 p++;
680 c = *p++;
681 switch (c) {
682 case 'n':
683 c = '\n';
684 break;
685 case 'r':
686 c = '\r';
687 break;
688 case '\\':
689 case '\'':
690 case '\"':
691 break;
692 default:
693 qemu_printf("unsupported escape code: '\\%c'\n", c);
694 goto fail;
695 }
696 if ((q - buf) < buf_size - 1) {
697 *q++ = c;
698 }
699 } else {
700 if ((q - buf) < buf_size - 1) {
701 *q++ = *p;
702 }
703 p++;
704 }
705 }
706 if (*p != '\"') {
707 qemu_printf("unterminated string\n");
708 goto fail;
709 }
710 p++;
711 } else {
712 while (*p != '\0' && !qemu_isspace(*p)) {
713 if ((q - buf) < buf_size - 1) {
714 *q++ = *p;
715 }
716 p++;
717 }
718 }
719 *q = '\0';
720 *pp = p;
721 return 0;
722}
723
724#define MAX_ARGS 16
725
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800726static void free_cmdline_args(char **args, int nb_args)
727{
728 int i;
729
730 assert(nb_args <= MAX_ARGS);
731
732 for (i = 0; i < nb_args; i++) {
733 g_free(args[i]);
734 }
735
736}
737
738/*
739 * Parse the command line to get valid args.
740 * @cmdline: command line to be parsed.
741 * @pnb_args: location to store the number of args, must NOT be NULL.
742 * @args: location to store the args, which should be freed by caller, must
743 * NOT be NULL.
744 *
745 * Returns 0 on success, negative on failure.
746 *
747 * NOTE: this parser is an approximate form of the real command parser. Number
748 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
749 * return with failure.
750 */
751static int parse_cmdline(const char *cmdline,
752 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800753{
754 const char *p;
755 int nb_args, ret;
756 char buf[1024];
757
758 p = cmdline;
759 nb_args = 0;
760 for (;;) {
761 while (qemu_isspace(*p)) {
762 p++;
763 }
764 if (*p == '\0') {
765 break;
766 }
767 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800768 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800769 }
770 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800771 if (ret < 0) {
772 goto fail;
773 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800774 args[nb_args] = g_strdup(buf);
775 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800776 }
777 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800778 return 0;
779
780 fail:
781 free_cmdline_args(args, nb_args);
782 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800783}
784
Wenchao Xia66855492013-08-27 20:38:23 +0800785static void help_cmd_dump_one(Monitor *mon,
786 const mon_cmd_t *cmd,
787 char **prefix_args,
788 int prefix_args_nb)
789{
790 int i;
791
792 for (i = 0; i < prefix_args_nb; i++) {
793 monitor_printf(mon, "%s ", prefix_args[i]);
794 }
795 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
796}
797
798/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500799static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800800 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000801{
Anthony Liguoric227f092009-10-01 16:12:16 -0500802 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000803
Wenchao Xia66855492013-08-27 20:38:23 +0800804 /* No valid arg need to compare with, dump all in *cmds */
805 if (arg_index >= nb_args) {
806 for (cmd = cmds; cmd->name != NULL; cmd++) {
807 help_cmd_dump_one(mon, cmd, args, arg_index);
808 }
809 return;
810 }
811
812 /* Find one entry to dump */
813 for (cmd = cmds; cmd->name != NULL; cmd++) {
814 if (compare_cmd(args[arg_index], cmd->name)) {
815 if (cmd->sub_table) {
816 /* continue with next arg */
817 help_cmd_dump(mon, cmd->sub_table,
818 args, nb_args, arg_index + 1);
819 } else {
820 help_cmd_dump_one(mon, cmd, args, arg_index);
821 }
822 break;
823 }
bellard9dc39cb2004-03-14 21:38:27 +0000824 }
825}
826
aliguori376253e2009-03-05 23:01:23 +0000827static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000828{
Wenchao Xia66855492013-08-27 20:38:23 +0800829 char *args[MAX_ARGS];
830 int nb_args = 0;
831
832 /* 1. parse user input */
833 if (name) {
834 /* special case for log, directly dump and return */
835 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000836 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000837 monitor_printf(mon, "Log items (comma separated):\n");
838 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000839 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000840 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000841 }
Wenchao Xia66855492013-08-27 20:38:23 +0800842 return;
843 }
844
845 if (parse_cmdline(name, &nb_args, args) < 0) {
846 return;
bellardf193c792004-03-21 17:06:25 +0000847 }
bellard9dc39cb2004-03-14 21:38:27 +0000848 }
Wenchao Xia66855492013-08-27 20:38:23 +0800849
850 /* 2. dump the contents according to parsed args */
851 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
852
853 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000854}
855
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300856static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300857{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300858 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300859}
860
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100861static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530862{
863 const char *tp_name = qdict_get_str(qdict, "name");
864 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200865 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000866
Lluís Vilanova14101d02014-08-25 13:20:03 +0200867 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
868 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100869 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000870 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530871}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100872
Michael Rothc45a8162011-10-02 08:44:37 -0500873#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100874static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100875{
876 const char *op = qdict_get_try_str(qdict, "op");
877 const char *arg = qdict_get_try_str(qdict, "arg");
878
879 if (!op) {
880 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
881 } else if (!strcmp(op, "on")) {
882 st_set_trace_file_enabled(true);
883 } else if (!strcmp(op, "off")) {
884 st_set_trace_file_enabled(false);
885 } else if (!strcmp(op, "flush")) {
886 st_flush_trace_buffer();
887 } else if (!strcmp(op, "set")) {
888 if (arg) {
889 st_set_trace_file(arg);
890 }
891 } else {
892 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
893 help_cmd(mon, "trace-file");
894 }
895}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530896#endif
897
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100898static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000899{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300900 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000901}
902
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300903CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000904{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300905 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200906 const mon_cmd_t *cmd;
907
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300908 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200909 info = g_malloc0(sizeof(*info));
910 info->value = g_malloc0(sizeof(*info->value));
911 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200912
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300913 info->next = cmd_list;
914 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200915 }
916
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300917 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000918}
919
Daniel P. Berrange48608532012-05-21 17:59:51 +0100920EventInfoList *qmp_query_events(Error **errp)
921{
922 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200923 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100924
Wenchao Xia75175172014-06-18 08:43:54 +0200925 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
926 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100927 assert(event_name != NULL);
928 info = g_malloc0(sizeof(*info));
929 info->value = g_malloc0(sizeof(*info->value));
930 info->value->name = g_strdup(event_name);
931
932 info->next = ev_list;
933 ev_list = info;
934 }
935
936 return ev_list;
937}
938
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300939/* set the current CPU defined by the user */
940int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000941{
Andreas Färber55e5c282012-12-17 06:18:02 +0100942 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000943
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100944 cpu = qemu_get_cpu(cpu_index);
945 if (cpu == NULL) {
946 return -1;
bellard6a00d602005-11-21 23:25:50 +0000947 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200948 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100949 return 0;
bellard6a00d602005-11-21 23:25:50 +0000950}
951
Andreas Färber9349b4f2012-03-14 01:38:32 +0100952static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000953{
aliguori731b0362009-03-05 23:01:42 +0000954 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300955 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000956 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300957 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200958 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000959}
960
Luiz Capitulino99b77962011-10-24 10:53:44 -0200961int monitor_get_cpu_index(void)
962{
Andreas Färber55e5c282012-12-17 06:18:02 +0100963 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
964 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200965}
966
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100967static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000968{
Andreas Färber878096e2013-05-27 01:33:50 +0200969 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100970 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000971 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200972 cpu = ENV_GET_CPU(env);
973 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000974}
975
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100976static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000977{
aliguori376253e2009-03-05 23:01:23 +0000978 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200979 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000980}
981
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100982static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300983{
984 dump_opcount_info((FILE *)mon, monitor_fprintf);
985}
986
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100987static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000988{
989 int i;
bellard7e2515e2004-08-01 21:52:19 +0000990 const char *str;
ths3b46e622007-09-17 08:09:54 +0000991
aliguoricde76ee2009-03-05 23:01:51 +0000992 if (!mon->rs)
993 return;
bellard7e2515e2004-08-01 21:52:19 +0000994 i = 0;
995 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000996 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000997 if (!str)
998 break;
aliguori376253e2009-03-05 23:01:23 +0000999 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001000 i++;
bellardaa455482004-04-04 13:07:25 +00001001 }
1002}
1003
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001004static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001005{
Andreas Färber878096e2013-05-27 01:33:50 +02001006 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001007 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001008
1009 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001010 cpu = ENV_GET_CPU(env);
1011 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001012}
j_mayer76a66252007-03-07 08:32:30 +00001013
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001014static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301015{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001016 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1017 TraceEventInfoList *elem;
1018
1019 for (elem = events; elem != NULL; elem = elem->next) {
1020 monitor_printf(mon, "%s : state %u\n",
1021 elem->value->name,
1022 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1023 }
1024 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301025}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301026
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001027void qmp_client_migrate_info(const char *protocol, const char *hostname,
1028 bool has_port, int64_t port,
1029 bool has_tls_port, int64_t tls_port,
1030 bool has_cert_subject, const char *cert_subject,
1031 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001032{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001033 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001034 if (!qemu_using_spice(errp)) {
1035 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001036 }
1037
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001038 if (!has_port && !has_tls_port) {
1039 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1040 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001041 }
1042
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001043 if (qemu_spice_migrate_info(hostname,
1044 has_port ? port : -1,
1045 has_tls_port ? tls_port : -1,
1046 cert_subject)) {
1047 error_set(errp, QERR_UNDEFINED_ERROR);
1048 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001049 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001050 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001051 }
1052
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001053 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001054}
1055
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001056static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001057{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001058 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001059}
1060
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001061static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001062{
1063 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001064 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001065
bellard9307c4c2004-04-04 12:57:25 +00001066 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001067 mask = 0;
1068 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001069 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001070 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001071 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001072 return;
1073 }
1074 }
Peter Maydell24537a02013-02-11 16:41:23 +00001075 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001076}
1077
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001078static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001079{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001080 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001081 if (!option || !strcmp(option, "on")) {
1082 singlestep = 1;
1083 } else if (!strcmp(option, "off")) {
1084 singlestep = 0;
1085 } else {
1086 monitor_printf(mon, "unexpected option %s\n", option);
1087 }
1088}
1089
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001090static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001091{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001092 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001093 if (!device)
1094 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1095 if (gdbserver_start(device) < 0) {
1096 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1097 device);
1098 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001099 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001100 } else {
aliguori59030a82009-04-05 18:43:41 +00001101 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1102 device);
bellard8a7ddc32004-03-31 19:00:16 +00001103 }
1104}
1105
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001106static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001107{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001108 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001109 if (select_watchdog_action(action) == -1) {
1110 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1111 }
1112}
1113
aliguori376253e2009-03-05 23:01:23 +00001114static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001115{
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001117 switch(c) {
1118 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001120 break;
1121 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001123 break;
1124 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001126 break;
1127 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001129 break;
1130 default:
1131 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001132 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001133 } else {
aliguori376253e2009-03-05 23:01:23 +00001134 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001135 }
1136 break;
1137 }
aliguori376253e2009-03-05 23:01:23 +00001138 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001139}
1140
aliguori376253e2009-03-05 23:01:23 +00001141static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001142 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001143{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001144 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001145 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001146 uint8_t buf[16];
1147 uint64_t v;
1148
1149 if (format == 'i') {
1150 int flags;
1151 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001152 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001153#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001154 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001155 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001156 } else if (wsize == 4) {
1157 flags = 0;
1158 } else {
bellard6a15fd12006-04-12 21:07:07 +00001159 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001160 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001161 if (env) {
1162#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001163 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001164 (env->segs[R_CS].flags & DESC_L_MASK))
1165 flags = 2;
1166 else
1167#endif
1168 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1169 flags = 1;
1170 }
bellard4c27ba22004-04-25 18:05:08 +00001171 }
1172#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001173#ifdef TARGET_PPC
1174 flags = msr_le << 16;
1175 flags |= env->bfd_mach;
1176#endif
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001178 return;
1179 }
1180
1181 len = wsize * count;
1182 if (wsize == 1)
1183 line_size = 8;
1184 else
1185 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001186 max_digits = 0;
1187
1188 switch(format) {
1189 case 'o':
1190 max_digits = (wsize * 8 + 2) / 3;
1191 break;
1192 default:
1193 case 'x':
1194 max_digits = (wsize * 8) / 4;
1195 break;
1196 case 'u':
1197 case 'd':
1198 max_digits = (wsize * 8 * 10 + 32) / 33;
1199 break;
1200 case 'c':
1201 wsize = 1;
1202 break;
1203 }
1204
1205 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001206 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001208 else
aliguori376253e2009-03-05 23:01:23 +00001209 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001210 l = len;
1211 if (l > line_size)
1212 l = line_size;
1213 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001214 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001215 } else {
bellard6a00d602005-11-21 23:25:50 +00001216 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001217 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001219 break;
1220 }
bellard9307c4c2004-04-04 12:57:25 +00001221 }
ths5fafdf22007-09-16 21:08:06 +00001222 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001223 while (i < l) {
1224 switch(wsize) {
1225 default:
1226 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001227 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001230 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001233 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001234 break;
1235 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001236 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 }
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001240 switch(format) {
1241 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001243 break;
1244 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001246 break;
1247 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001249 break;
1250 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001252 break;
1253 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001255 break;
1256 }
1257 i += wsize;
1258 }
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001260 addr += l;
1261 len -= l;
1262 }
1263}
1264
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001265static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001266{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001267 int count = qdict_get_int(qdict, "count");
1268 int format = qdict_get_int(qdict, "format");
1269 int size = qdict_get_int(qdict, "size");
1270 target_long addr = qdict_get_int(qdict, "addr");
1271
aliguori376253e2009-03-05 23:01:23 +00001272 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001273}
1274
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001275static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001276{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001277 int count = qdict_get_int(qdict, "count");
1278 int format = qdict_get_int(qdict, "format");
1279 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281
aliguori376253e2009-03-05 23:01:23 +00001282 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001283}
1284
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001285static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001286{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001287 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001288 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001289
bellard9307c4c2004-04-04 12:57:25 +00001290 switch(format) {
1291 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001292 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001295 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001298 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 default:
1301 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001302 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001306 break;
1307 }
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001309}
1310
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001311static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001312{
1313 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001314 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001315 uint32_t start = qdict_get_int(qdict, "start");
1316 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001317
1318 sum = 0;
1319 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001320 uint8_t val = address_space_ldub(&address_space_memory, addr,
1321 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001322 /* BSD sum algorithm ('sum' Unix command) */
1323 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001324 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001325 }
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001327}
1328
bellard13224a82006-07-14 22:03:35 +00001329static int mouse_button_state;
1330
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001331static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001332{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001333 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001334 const char *dx_str = qdict_get_str(qdict, "dx_str");
1335 const char *dy_str = qdict_get_str(qdict, "dy_str");
1336 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001337
bellard13224a82006-07-14 22:03:35 +00001338 dx = strtol(dx_str, NULL, 0);
1339 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001340 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1341 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1342
1343 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001344 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001345 if (dz != 0) {
1346 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1347 qemu_input_queue_btn(NULL, button, true);
1348 qemu_input_event_sync();
1349 qemu_input_queue_btn(NULL, button, false);
1350 }
1351 }
1352 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001353}
1354
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001355static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001356{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001357 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1358 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1359 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1360 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1361 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001362 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001363
1364 if (mouse_button_state == button_state) {
1365 return;
1366 }
1367 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1368 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001369 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001370}
1371
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001372static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001373{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001374 int size = qdict_get_int(qdict, "size");
1375 int addr = qdict_get_int(qdict, "addr");
1376 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001377 uint32_t val;
1378 int suffix;
1379
1380 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001381 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001382 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001383 addr++;
1384 }
1385 addr &= 0xffff;
1386
1387 switch(size) {
1388 default:
1389 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001390 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001391 suffix = 'b';
1392 break;
1393 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001394 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001395 suffix = 'w';
1396 break;
1397 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001398 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001399 suffix = 'l';
1400 break;
1401 }
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1403 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001404}
bellarda3a91a32004-06-04 11:06:21 +00001405
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001406static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001407{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001408 int size = qdict_get_int(qdict, "size");
1409 int addr = qdict_get_int(qdict, "addr");
1410 int val = qdict_get_int(qdict, "val");
1411
Jan Kiszkaf1147842009-07-14 10:20:11 +02001412 addr &= IOPORTS_MASK;
1413
1414 switch (size) {
1415 default:
1416 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001417 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001418 break;
1419 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001420 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001421 break;
1422 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001423 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001424 break;
1425 }
1426}
1427
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001428static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001429{
Gongleif1839932014-12-03 18:20:58 +00001430 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001431 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001432
Gongleif1839932014-12-03 18:20:58 +00001433 qemu_boot_set(bootdevice, &local_err);
1434 if (local_err) {
1435 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1436 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001437 } else {
Gongleif1839932014-12-03 18:20:58 +00001438 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001439 }
1440}
1441
bellardb86bda52004-09-18 19:32:46 +00001442#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001443static void print_pte(Monitor *mon, hwaddr addr,
1444 hwaddr pte,
1445 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001446{
Blue Swirld65aaf32010-12-11 18:56:24 +00001447#ifdef TARGET_X86_64
1448 if (addr & (1ULL << 47)) {
1449 addr |= -1LL << 48;
1450 }
1451#endif
1452 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1453 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001454 addr,
1455 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001456 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001457 pte & PG_GLOBAL_MASK ? 'G' : '-',
1458 pte & PG_PSE_MASK ? 'P' : '-',
1459 pte & PG_DIRTY_MASK ? 'D' : '-',
1460 pte & PG_ACCESSED_MASK ? 'A' : '-',
1461 pte & PG_PCD_MASK ? 'C' : '-',
1462 pte & PG_PWT_MASK ? 'T' : '-',
1463 pte & PG_USER_MASK ? 'U' : '-',
1464 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001465}
1466
Andreas Färber9349b4f2012-03-14 01:38:32 +01001467static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001468{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001469 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001470 uint32_t pgd, pde, pte;
1471
bellardb86bda52004-09-18 19:32:46 +00001472 pgd = env->cr[3] & ~0xfff;
1473 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001474 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001475 pde = le32_to_cpu(pde);
1476 if (pde & PG_PRESENT_MASK) {
1477 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001478 /* 4M pages */
1479 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001480 } else {
1481 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001482 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001483 pte = le32_to_cpu(pte);
1484 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001485 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001486 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001487 ~0xfff);
1488 }
1489 }
1490 }
1491 }
1492 }
1493}
1494
Andreas Färber9349b4f2012-03-14 01:38:32 +01001495static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001496{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001497 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001498 uint64_t pdpe, pde, pte;
1499 uint64_t pdp_addr, pd_addr, pt_addr;
1500
1501 pdp_addr = env->cr[3] & ~0x1f;
1502 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001503 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001504 pdpe = le64_to_cpu(pdpe);
1505 if (pdpe & PG_PRESENT_MASK) {
1506 pd_addr = pdpe & 0x3fffffffff000ULL;
1507 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001508 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001509 pde = le64_to_cpu(pde);
1510 if (pde & PG_PRESENT_MASK) {
1511 if (pde & PG_PSE_MASK) {
1512 /* 2M pages with PAE, CR4.PSE is ignored */
1513 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001514 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001515 } else {
1516 pt_addr = pde & 0x3fffffffff000ULL;
1517 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001518 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001519 pte = le64_to_cpu(pte);
1520 if (pte & PG_PRESENT_MASK) {
1521 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1522 + (l3 << 12),
1523 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001524 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001525 }
1526 }
1527 }
1528 }
1529 }
1530 }
1531 }
1532}
1533
1534#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001535static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001536{
1537 uint64_t l1, l2, l3, l4;
1538 uint64_t pml4e, pdpe, pde, pte;
1539 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1540
1541 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1542 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001543 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001544 pml4e = le64_to_cpu(pml4e);
1545 if (pml4e & PG_PRESENT_MASK) {
1546 pdp_addr = pml4e & 0x3fffffffff000ULL;
1547 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001548 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001549 pdpe = le64_to_cpu(pdpe);
1550 if (pdpe & PG_PRESENT_MASK) {
1551 if (pdpe & PG_PSE_MASK) {
1552 /* 1G pages, CR4.PSE is ignored */
1553 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1554 0x3ffffc0000000ULL);
1555 } else {
1556 pd_addr = pdpe & 0x3fffffffff000ULL;
1557 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001558 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001559 pde = le64_to_cpu(pde);
1560 if (pde & PG_PRESENT_MASK) {
1561 if (pde & PG_PSE_MASK) {
1562 /* 2M pages, CR4.PSE is ignored */
1563 print_pte(mon, (l1 << 39) + (l2 << 30) +
1564 (l3 << 21), pde,
1565 0x3ffffffe00000ULL);
1566 } else {
1567 pt_addr = pde & 0x3fffffffff000ULL;
1568 for (l4 = 0; l4 < 512; l4++) {
1569 cpu_physical_memory_read(pt_addr
1570 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001571 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001572 pte = le64_to_cpu(pte);
1573 if (pte & PG_PRESENT_MASK) {
1574 print_pte(mon, (l1 << 39) +
1575 (l2 << 30) +
1576 (l3 << 21) + (l4 << 12),
1577 pte & ~PG_PSE_MASK,
1578 0x3fffffffff000ULL);
1579 }
1580 }
1581 }
1582 }
1583 }
1584 }
1585 }
1586 }
1587 }
1588 }
1589}
1590#endif
1591
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001592static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001593{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001594 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001595
1596 env = mon_get_cpu();
1597
1598 if (!(env->cr[0] & CR0_PG_MASK)) {
1599 monitor_printf(mon, "PG disabled\n");
1600 return;
1601 }
1602 if (env->cr[4] & CR4_PAE_MASK) {
1603#ifdef TARGET_X86_64
1604 if (env->hflags & HF_LMA_MASK) {
1605 tlb_info_64(mon, env);
1606 } else
1607#endif
1608 {
1609 tlb_info_pae32(mon, env);
1610 }
1611 } else {
1612 tlb_info_32(mon, env);
1613 }
1614}
1615
Avi Kivitya8170e52012-10-23 12:30:10 +02001616static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001617 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001618 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001619{
bellard9746b152004-11-11 18:30:24 +00001620 int prot1;
1621 prot1 = *plast_prot;
1622 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001623 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001624 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1625 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001626 *pstart, end, end - *pstart,
1627 prot1 & PG_USER_MASK ? 'u' : '-',
1628 'r',
1629 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001630 }
1631 if (prot != 0)
1632 *pstart = end;
1633 else
1634 *pstart = -1;
1635 *plast_prot = prot;
1636 }
1637}
1638
Andreas Färber9349b4f2012-03-14 01:38:32 +01001639static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001640{
Austin Clementsb49ca722011-08-14 23:19:21 -04001641 unsigned int l1, l2;
1642 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001643 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001644 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001645
bellardb86bda52004-09-18 19:32:46 +00001646 pgd = env->cr[3] & ~0xfff;
1647 last_prot = 0;
1648 start = -1;
1649 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001650 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001651 pde = le32_to_cpu(pde);
1652 end = l1 << 22;
1653 if (pde & PG_PRESENT_MASK) {
1654 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1655 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001656 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001657 } else {
1658 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001659 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001660 pte = le32_to_cpu(pte);
1661 end = (l1 << 22) + (l2 << 12);
1662 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001663 prot = pte & pde &
1664 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001665 } else {
1666 prot = 0;
1667 }
aliguori376253e2009-03-05 23:01:23 +00001668 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001669 }
1670 }
1671 } else {
1672 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001673 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001674 }
1675 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001676 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001677 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001678}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001679
Andreas Färber9349b4f2012-03-14 01:38:32 +01001680static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001681{
Austin Clementsb49ca722011-08-14 23:19:21 -04001682 unsigned int l1, l2, l3;
1683 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001684 uint64_t pdpe, pde, pte;
1685 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001686 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001687
1688 pdp_addr = env->cr[3] & ~0x1f;
1689 last_prot = 0;
1690 start = -1;
1691 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001692 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001693 pdpe = le64_to_cpu(pdpe);
1694 end = l1 << 30;
1695 if (pdpe & PG_PRESENT_MASK) {
1696 pd_addr = pdpe & 0x3fffffffff000ULL;
1697 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001698 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001699 pde = le64_to_cpu(pde);
1700 end = (l1 << 30) + (l2 << 21);
1701 if (pde & PG_PRESENT_MASK) {
1702 if (pde & PG_PSE_MASK) {
1703 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1704 PG_PRESENT_MASK);
1705 mem_print(mon, &start, &last_prot, end, prot);
1706 } else {
1707 pt_addr = pde & 0x3fffffffff000ULL;
1708 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001709 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001710 pte = le64_to_cpu(pte);
1711 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1712 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001713 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1714 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001715 } else {
1716 prot = 0;
1717 }
1718 mem_print(mon, &start, &last_prot, end, prot);
1719 }
1720 }
1721 } else {
1722 prot = 0;
1723 mem_print(mon, &start, &last_prot, end, prot);
1724 }
1725 }
1726 } else {
1727 prot = 0;
1728 mem_print(mon, &start, &last_prot, end, prot);
1729 }
1730 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001731 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001732 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001733}
1734
1735
1736#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001737static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001738{
1739 int prot, last_prot;
1740 uint64_t l1, l2, l3, l4;
1741 uint64_t pml4e, pdpe, pde, pte;
1742 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1743
1744 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1745 last_prot = 0;
1746 start = -1;
1747 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001748 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001749 pml4e = le64_to_cpu(pml4e);
1750 end = l1 << 39;
1751 if (pml4e & PG_PRESENT_MASK) {
1752 pdp_addr = pml4e & 0x3fffffffff000ULL;
1753 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001754 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001755 pdpe = le64_to_cpu(pdpe);
1756 end = (l1 << 39) + (l2 << 30);
1757 if (pdpe & PG_PRESENT_MASK) {
1758 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001759 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1760 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001761 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001762 mem_print(mon, &start, &last_prot, end, prot);
1763 } else {
1764 pd_addr = pdpe & 0x3fffffffff000ULL;
1765 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001766 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001767 pde = le64_to_cpu(pde);
1768 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1769 if (pde & PG_PRESENT_MASK) {
1770 if (pde & PG_PSE_MASK) {
1771 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1772 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001773 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001774 mem_print(mon, &start, &last_prot, end, prot);
1775 } else {
1776 pt_addr = pde & 0x3fffffffff000ULL;
1777 for (l4 = 0; l4 < 512; l4++) {
1778 cpu_physical_memory_read(pt_addr
1779 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001780 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001781 pte = le64_to_cpu(pte);
1782 end = (l1 << 39) + (l2 << 30) +
1783 (l3 << 21) + (l4 << 12);
1784 if (pte & PG_PRESENT_MASK) {
1785 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1786 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001787 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001788 } else {
1789 prot = 0;
1790 }
1791 mem_print(mon, &start, &last_prot, end, prot);
1792 }
1793 }
1794 } else {
1795 prot = 0;
1796 mem_print(mon, &start, &last_prot, end, prot);
1797 }
1798 }
1799 }
1800 } else {
1801 prot = 0;
1802 mem_print(mon, &start, &last_prot, end, prot);
1803 }
1804 }
1805 } else {
1806 prot = 0;
1807 mem_print(mon, &start, &last_prot, end, prot);
1808 }
1809 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001810 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001811 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001812}
1813#endif
1814
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001815static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001816{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001817 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001818
1819 env = mon_get_cpu();
1820
1821 if (!(env->cr[0] & CR0_PG_MASK)) {
1822 monitor_printf(mon, "PG disabled\n");
1823 return;
1824 }
1825 if (env->cr[4] & CR4_PAE_MASK) {
1826#ifdef TARGET_X86_64
1827 if (env->hflags & HF_LMA_MASK) {
1828 mem_info_64(mon, env);
1829 } else
1830#endif
1831 {
1832 mem_info_pae32(mon, env);
1833 }
1834 } else {
1835 mem_info_32(mon, env);
1836 }
1837}
bellardb86bda52004-09-18 19:32:46 +00001838#endif
1839
aurel327c664e22009-03-03 06:12:22 +00001840#if defined(TARGET_SH4)
1841
aliguori376253e2009-03-05 23:01:23 +00001842static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001843{
aliguori376253e2009-03-05 23:01:23 +00001844 monitor_printf(mon, " tlb%i:\t"
1845 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1846 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1847 "dirty=%hhu writethrough=%hhu\n",
1848 idx,
1849 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1850 tlb->v, tlb->sh, tlb->c, tlb->pr,
1851 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001852}
1853
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001854static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001855{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001856 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001857 int i;
1858
aliguori376253e2009-03-05 23:01:23 +00001859 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001860 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001861 print_tlb (mon, i, &env->itlb[i]);
1862 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001863 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001864 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001865}
1866
1867#endif
1868
Max Filippov692f7372012-01-07 20:02:40 +04001869#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001870static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001871{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001872 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001873
1874 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1875}
1876#endif
1877
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001878static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001879{
1880 mtree_info((fprintf_function)monitor_printf, mon);
1881}
1882
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001883static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001884{
aliguorib28b6232009-04-22 20:20:29 +00001885 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001886 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001887 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001888
zhanghailiang5b009e42014-11-04 19:49:30 +08001889 node_mem = g_new0(uint64_t, nb_numa_nodes);
1890 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001891 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1892 for (i = 0; i < nb_numa_nodes; i++) {
1893 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001894 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001895 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001896 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001897 }
1898 }
1899 monitor_printf(mon, "\n");
1900 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001901 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001902 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001903 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001904}
1905
bellard5f1ce942006-02-08 22:40:15 +00001906#ifdef CONFIG_PROFILER
1907
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001908int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001909int64_t dev_time;
1910
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001911static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001912{
aliguori376253e2009-03-05 23:01:23 +00001913 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001914 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001915 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001916 tcg_time, tcg_time / (double)get_ticks_per_sec());
1917 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001918 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001919}
1920#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001921static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001922{
aliguori376253e2009-03-05 23:01:23 +00001923 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001924}
1925#endif
1926
bellardec36b692006-07-16 18:57:03 +00001927/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001928static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001929
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001930static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001931{
1932 int i;
1933 CaptureState *s;
1934
1935 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001936 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001937 s->ops.info (s->opaque);
1938 }
1939}
1940
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001941static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001942{
1943 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001944 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001945 CaptureState *s;
1946
1947 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1948 if (i == n) {
1949 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001950 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001951 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001952 return;
1953 }
1954 }
1955}
1956
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001957static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001958{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001959 const char *path = qdict_get_str(qdict, "path");
1960 int has_freq = qdict_haskey(qdict, "freq");
1961 int freq = qdict_get_try_int(qdict, "freq", -1);
1962 int has_bits = qdict_haskey(qdict, "bits");
1963 int bits = qdict_get_try_int(qdict, "bits", -1);
1964 int has_channels = qdict_haskey(qdict, "nchannels");
1965 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001966 CaptureState *s;
1967
Anthony Liguori7267c092011-08-20 22:09:37 -05001968 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001969
1970 freq = has_freq ? freq : 44100;
1971 bits = has_bits ? bits : 16;
1972 nchannels = has_channels ? nchannels : 2;
1973
1974 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001975 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001976 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001977 return;
bellardec36b692006-07-16 18:57:03 +00001978 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001979 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001980}
bellardec36b692006-07-16 18:57:03 +00001981
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001982static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001983{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001984 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001985
aliguori76655d62009-03-06 20:27:37 +00001986 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001987 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001988 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001989 return acl;
1990}
aliguori76655d62009-03-06 20:27:37 +00001991
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001992static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001993{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001994 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001995 qemu_acl *acl = find_acl(mon, aclname);
1996 qemu_acl_entry *entry;
1997 int i = 0;
1998
1999 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002000 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002001 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002002 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002003 i++;
2004 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002005 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002006 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002007 }
2008}
2009
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002010static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002011{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002012 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002013 qemu_acl *acl = find_acl(mon, aclname);
2014
2015 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002016 qemu_acl_reset(acl);
2017 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002018 }
2019}
aliguori76655d62009-03-06 20:27:37 +00002020
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002021static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002022{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002023 const char *aclname = qdict_get_str(qdict, "aclname");
2024 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002025 qemu_acl *acl = find_acl(mon, aclname);
2026
2027 if (acl) {
2028 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002029 acl->defaultDeny = 0;
2030 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002031 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002032 acl->defaultDeny = 1;
2033 monitor_printf(mon, "acl: policy set to 'deny'\n");
2034 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002035 monitor_printf(mon, "acl: unknown policy '%s', "
2036 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002037 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002038 }
2039}
aliguori76655d62009-03-06 20:27:37 +00002040
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002041static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002042{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002043 const char *aclname = qdict_get_str(qdict, "aclname");
2044 const char *match = qdict_get_str(qdict, "match");
2045 const char *policy = qdict_get_str(qdict, "policy");
2046 int has_index = qdict_haskey(qdict, "index");
2047 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002048 qemu_acl *acl = find_acl(mon, aclname);
2049 int deny, ret;
2050
2051 if (acl) {
2052 if (strcmp(policy, "allow") == 0) {
2053 deny = 0;
2054 } else if (strcmp(policy, "deny") == 0) {
2055 deny = 1;
2056 } else {
2057 monitor_printf(mon, "acl: unknown policy '%s', "
2058 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002059 return;
2060 }
aliguori28a76be2009-03-06 20:27:40 +00002061 if (has_index)
2062 ret = qemu_acl_insert(acl, deny, match, index);
2063 else
2064 ret = qemu_acl_append(acl, deny, match);
2065 if (ret < 0)
2066 monitor_printf(mon, "acl: unable to add acl entry\n");
2067 else
2068 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002069 }
2070}
aliguori76655d62009-03-06 20:27:37 +00002071
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002072static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002073{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002074 const char *aclname = qdict_get_str(qdict, "aclname");
2075 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002076 qemu_acl *acl = find_acl(mon, aclname);
2077 int ret;
aliguori76655d62009-03-06 20:27:37 +00002078
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002079 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002080 ret = qemu_acl_remove(acl, match);
2081 if (ret < 0)
2082 monitor_printf(mon, "acl: no matching acl entry\n");
2083 else
2084 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002085 }
2086}
2087
Huang Ying79c4f6b2009-06-23 10:05:14 +08002088#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002089static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002090{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002091 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002092 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002093 int cpu_index = qdict_get_int(qdict, "cpu_index");
2094 int bank = qdict_get_int(qdict, "bank");
2095 uint64_t status = qdict_get_int(qdict, "status");
2096 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2097 uint64_t addr = qdict_get_int(qdict, "addr");
2098 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002099 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002100
Jan Kiszka747461c2011-03-02 08:56:10 +01002101 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2102 flags |= MCE_INJECT_BROADCAST;
2103 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002104 cs = qemu_get_cpu(cpu_index);
2105 if (cs != NULL) {
2106 cpu = X86_CPU(cs);
2107 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2108 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002109 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002110}
2111#endif
2112
Corey Bryant208c9d12012-06-22 14:36:09 -04002113void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002114{
Anthony Liguoric227f092009-10-01 16:12:16 -05002115 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002116 int fd;
2117
Corey Bryant208c9d12012-06-22 14:36:09 -04002118 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002119 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002120 error_set(errp, QERR_FD_NOT_SUPPLIED);
2121 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002122 }
2123
2124 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002125 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002126 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2127 "a name not starting with a digit");
2128 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002129 }
2130
Corey Bryant208c9d12012-06-22 14:36:09 -04002131 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002132 if (strcmp(monfd->name, fdname) != 0) {
2133 continue;
2134 }
2135
2136 close(monfd->fd);
2137 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002138 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002139 }
2140
Anthony Liguori7267c092011-08-20 22:09:37 -05002141 monfd = g_malloc0(sizeof(mon_fd_t));
2142 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143 monfd->fd = fd;
2144
Corey Bryant208c9d12012-06-22 14:36:09 -04002145 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002146}
2147
Corey Bryant208c9d12012-06-22 14:36:09 -04002148void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002149{
Anthony Liguoric227f092009-10-01 16:12:16 -05002150 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002151
Corey Bryant208c9d12012-06-22 14:36:09 -04002152 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002153 if (strcmp(monfd->name, fdname) != 0) {
2154 continue;
2155 }
2156
Blue Swirl72cf2d42009-09-12 07:36:22 +00002157 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002158 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002159 g_free(monfd->name);
2160 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002161 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002162 }
2163
Corey Bryant208c9d12012-06-22 14:36:09 -04002164 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002165}
2166
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002167static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002168{
Luiz Capitulino13548692011-07-29 15:36:43 -03002169 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002170 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002171
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002172 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002173
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002174 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002175 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002176 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002177}
2178
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002179int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002180{
Anthony Liguoric227f092009-10-01 16:12:16 -05002181 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002182
Blue Swirl72cf2d42009-09-12 07:36:22 +00002183 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002184 int fd;
2185
2186 if (strcmp(monfd->name, fdname) != 0) {
2187 continue;
2188 }
2189
2190 fd = monfd->fd;
2191
2192 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002193 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002194 g_free(monfd->name);
2195 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002196
2197 return fd;
2198 }
2199
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002200 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002201 return -1;
2202}
2203
Corey Bryantba1c0482012-08-14 16:43:43 -04002204static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2205{
2206 MonFdsetFd *mon_fdset_fd;
2207 MonFdsetFd *mon_fdset_fd_next;
2208
2209 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002210 if ((mon_fdset_fd->removed ||
2211 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2212 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002213 close(mon_fdset_fd->fd);
2214 g_free(mon_fdset_fd->opaque);
2215 QLIST_REMOVE(mon_fdset_fd, next);
2216 g_free(mon_fdset_fd);
2217 }
2218 }
2219
Corey Bryantadb696f2012-08-14 16:43:47 -04002220 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002221 QLIST_REMOVE(mon_fdset, next);
2222 g_free(mon_fdset);
2223 }
2224}
2225
Corey Bryantefb87c12012-08-14 16:43:48 -04002226static void monitor_fdsets_cleanup(void)
2227{
2228 MonFdset *mon_fdset;
2229 MonFdset *mon_fdset_next;
2230
2231 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2232 monitor_fdset_cleanup(mon_fdset);
2233 }
2234}
2235
Corey Bryantba1c0482012-08-14 16:43:43 -04002236AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2237 const char *opaque, Error **errp)
2238{
2239 int fd;
2240 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002241 AddfdInfo *fdinfo;
2242
2243 fd = qemu_chr_fe_get_msgfd(mon->chr);
2244 if (fd == -1) {
2245 error_set(errp, QERR_FD_NOT_SUPPLIED);
2246 goto error;
2247 }
2248
Corey Bryante446f702012-10-18 15:19:32 -04002249 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2250 has_opaque, opaque, errp);
2251 if (fdinfo) {
2252 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002253 }
2254
Corey Bryantba1c0482012-08-14 16:43:43 -04002255error:
2256 if (fd != -1) {
2257 close(fd);
2258 }
2259 return NULL;
2260}
2261
2262void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2263{
2264 MonFdset *mon_fdset;
2265 MonFdsetFd *mon_fdset_fd;
2266 char fd_str[60];
2267
2268 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2269 if (mon_fdset->id != fdset_id) {
2270 continue;
2271 }
2272 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2273 if (has_fd) {
2274 if (mon_fdset_fd->fd != fd) {
2275 continue;
2276 }
2277 mon_fdset_fd->removed = true;
2278 break;
2279 } else {
2280 mon_fdset_fd->removed = true;
2281 }
2282 }
2283 if (has_fd && !mon_fdset_fd) {
2284 goto error;
2285 }
2286 monitor_fdset_cleanup(mon_fdset);
2287 return;
2288 }
2289
2290error:
2291 if (has_fd) {
2292 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2293 fdset_id, fd);
2294 } else {
2295 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2296 }
2297 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2298}
2299
2300FdsetInfoList *qmp_query_fdsets(Error **errp)
2301{
2302 MonFdset *mon_fdset;
2303 MonFdsetFd *mon_fdset_fd;
2304 FdsetInfoList *fdset_list = NULL;
2305
2306 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2307 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2308 FdsetFdInfoList *fdsetfd_list = NULL;
2309
2310 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2311 fdset_info->value->fdset_id = mon_fdset->id;
2312
2313 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2314 FdsetFdInfoList *fdsetfd_info;
2315
2316 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2317 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2318 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2319 if (mon_fdset_fd->opaque) {
2320 fdsetfd_info->value->has_opaque = true;
2321 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2322 } else {
2323 fdsetfd_info->value->has_opaque = false;
2324 }
2325
2326 fdsetfd_info->next = fdsetfd_list;
2327 fdsetfd_list = fdsetfd_info;
2328 }
2329
2330 fdset_info->value->fds = fdsetfd_list;
2331
2332 fdset_info->next = fdset_list;
2333 fdset_list = fdset_info;
2334 }
2335
2336 return fdset_list;
2337}
2338
Corey Bryante446f702012-10-18 15:19:32 -04002339AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2340 bool has_opaque, const char *opaque,
2341 Error **errp)
2342{
2343 MonFdset *mon_fdset = NULL;
2344 MonFdsetFd *mon_fdset_fd;
2345 AddfdInfo *fdinfo;
2346
2347 if (has_fdset_id) {
2348 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2349 /* Break if match found or match impossible due to ordering by ID */
2350 if (fdset_id <= mon_fdset->id) {
2351 if (fdset_id < mon_fdset->id) {
2352 mon_fdset = NULL;
2353 }
2354 break;
2355 }
2356 }
2357 }
2358
2359 if (mon_fdset == NULL) {
2360 int64_t fdset_id_prev = -1;
2361 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2362
2363 if (has_fdset_id) {
2364 if (fdset_id < 0) {
2365 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2366 "a non-negative value");
2367 return NULL;
2368 }
2369 /* Use specified fdset ID */
2370 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2371 mon_fdset_cur = mon_fdset;
2372 if (fdset_id < mon_fdset_cur->id) {
2373 break;
2374 }
2375 }
2376 } else {
2377 /* Use first available fdset ID */
2378 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2379 mon_fdset_cur = mon_fdset;
2380 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2381 fdset_id_prev = mon_fdset_cur->id;
2382 continue;
2383 }
2384 break;
2385 }
2386 }
2387
2388 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2389 if (has_fdset_id) {
2390 mon_fdset->id = fdset_id;
2391 } else {
2392 mon_fdset->id = fdset_id_prev + 1;
2393 }
2394
2395 /* The fdset list is ordered by fdset ID */
2396 if (!mon_fdset_cur) {
2397 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2398 } else if (mon_fdset->id < mon_fdset_cur->id) {
2399 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2400 } else {
2401 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2402 }
2403 }
2404
2405 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2406 mon_fdset_fd->fd = fd;
2407 mon_fdset_fd->removed = false;
2408 if (has_opaque) {
2409 mon_fdset_fd->opaque = g_strdup(opaque);
2410 }
2411 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2412
2413 fdinfo = g_malloc0(sizeof(*fdinfo));
2414 fdinfo->fdset_id = mon_fdset->id;
2415 fdinfo->fd = mon_fdset_fd->fd;
2416
2417 return fdinfo;
2418}
2419
Corey Bryantadb696f2012-08-14 16:43:47 -04002420int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2421{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002422#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002423 MonFdset *mon_fdset;
2424 MonFdsetFd *mon_fdset_fd;
2425 int mon_fd_flags;
2426
Corey Bryantadb696f2012-08-14 16:43:47 -04002427 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2428 if (mon_fdset->id != fdset_id) {
2429 continue;
2430 }
2431 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2432 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2433 if (mon_fd_flags == -1) {
2434 return -1;
2435 }
2436
2437 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2438 return mon_fdset_fd->fd;
2439 }
2440 }
2441 errno = EACCES;
2442 return -1;
2443 }
2444#endif
2445
2446 errno = ENOENT;
2447 return -1;
2448}
2449
2450int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2451{
2452 MonFdset *mon_fdset;
2453 MonFdsetFd *mon_fdset_fd_dup;
2454
2455 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2456 if (mon_fdset->id != fdset_id) {
2457 continue;
2458 }
2459 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2460 if (mon_fdset_fd_dup->fd == dup_fd) {
2461 return -1;
2462 }
2463 }
2464 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2465 mon_fdset_fd_dup->fd = dup_fd;
2466 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2467 return 0;
2468 }
2469 return -1;
2470}
2471
2472static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2473{
2474 MonFdset *mon_fdset;
2475 MonFdsetFd *mon_fdset_fd_dup;
2476
2477 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2478 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2479 if (mon_fdset_fd_dup->fd == dup_fd) {
2480 if (remove) {
2481 QLIST_REMOVE(mon_fdset_fd_dup, next);
2482 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2483 monitor_fdset_cleanup(mon_fdset);
2484 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002485 return -1;
2486 } else {
2487 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002488 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002489 }
2490 }
2491 }
2492 return -1;
2493}
2494
2495int monitor_fdset_dup_fd_find(int dup_fd)
2496{
2497 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2498}
2499
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002500void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002501{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002502 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002503}
2504
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002505int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002506{
2507 int fd;
2508 Error *local_err = NULL;
2509
2510 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002511 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002512 } else {
2513 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002514 if (fd == -1) {
2515 error_setg(&local_err, "Invalid file descriptor number '%s'",
2516 fdname);
2517 }
2518 }
2519 if (local_err) {
2520 error_propagate(errp, local_err);
2521 assert(fd == -1);
2522 } else {
2523 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002524 }
2525
2526 return fd;
2527}
2528
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002529/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002530static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 {
2532 .name = "version",
2533 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 .params = "",
2535 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002536 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 },
2538 {
2539 .name = "network",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002543 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "chardev",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002550 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
2553 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002554 .args_type = "nodes:-n,verbose:-v,device:B?",
2555 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002556 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002557 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002558 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 },
2560 {
2561 .name = "blockstats",
2562 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 .params = "",
2564 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002565 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002566 },
2567 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002568 .name = "block-jobs",
2569 .args_type = "",
2570 .params = "",
2571 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002572 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002573 },
2574 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 .name = "registers",
2576 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 .params = "",
2578 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002579 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = "cpus",
2583 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 .params = "",
2585 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002586 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
2588 {
2589 .name = "history",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002593 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002595#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2596 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002597 {
2598 .name = "irq",
2599 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 .params = "",
2601 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002602#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002603 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002604#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002605 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002606#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002607 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002608#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 },
2610 {
2611 .name = "pic",
2612 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 .params = "",
2614 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002615#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002616 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002617#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002618 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002619#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002620 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002621#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002623#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 {
2625 .name = "pci",
2626 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 .params = "",
2628 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002629 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002631#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002632 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 {
2634 .name = "tlb",
2635 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 .params = "",
2637 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002638 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 },
aurel327c664e22009-03-03 06:12:22 +00002640#endif
2641#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 {
2643 .name = "mem",
2644 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 .params = "",
2646 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002647 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 },
bellardb86bda52004-09-18 19:32:46 +00002649#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 {
Blue Swirl314e2982011-09-11 20:22:05 +00002651 .name = "mtree",
2652 .args_type = "",
2653 .params = "",
2654 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002655 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002656 },
2657 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 .name = "jit",
2659 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 .params = "",
2661 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002662 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002663 },
2664 {
Max Filippov246ae242014-11-02 11:04:18 +03002665 .name = "opcount",
2666 .args_type = "",
2667 .params = "",
2668 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002669 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002670 },
2671 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002672 .name = "kvm",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002676 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
2679 .name = "numa",
2680 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 .params = "",
2682 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002683 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
2685 {
2686 .name = "usb",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002690 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
2693 .name = "usbhost",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002697 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "profile",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002704 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "capture",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002711 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
2713 {
2714 .name = "snapshots",
2715 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 .params = "",
2717 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002718 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
2720 {
2721 .name = "status",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002725 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 },
2727 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 .name = "mice",
2729 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 .params = "",
2731 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002732 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 },
2734 {
2735 .name = "vnc",
2736 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002737 .params = "",
2738 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002739 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002741#if defined(CONFIG_SPICE)
2742 {
2743 .name = "spice",
2744 .args_type = "",
2745 .params = "",
2746 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002747 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002748 },
2749#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 {
2751 .name = "name",
2752 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 .params = "",
2754 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002755 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
2757 {
2758 .name = "uuid",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002762 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 {
2765 .name = "cpustats",
2766 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 .params = "",
2768 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002769 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002770 },
blueswir131a60e22007-10-26 18:42:59 +00002771#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 {
2773 .name = "usernet",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002777 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
blueswir131a60e22007-10-26 18:42:59 +00002779#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 {
2781 .name = "migrate",
2782 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 .params = "",
2784 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002785 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002786 },
2787 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002788 .name = "migrate_capabilities",
2789 .args_type = "",
2790 .params = "",
2791 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002792 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002793 },
2794 {
Liang Li50e9a622015-03-23 16:32:29 +08002795 .name = "migrate_parameters",
2796 .args_type = "",
2797 .params = "",
2798 .help = "show current migration parameters",
2799 .mhandler.cmd = hmp_info_migrate_parameters,
2800 },
2801 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002802 .name = "migrate_cache_size",
2803 .args_type = "",
2804 .params = "",
2805 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002806 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002807 },
2808 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 .name = "balloon",
2810 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 .params = "",
2812 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002813 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 },
2815 {
2816 .name = "qtree",
2817 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 .params = "",
2819 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002820 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002821 },
2822 {
2823 .name = "qdm",
2824 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 .params = "",
2826 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002827 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002828 },
2829 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002830 .name = "qom-tree",
2831 .args_type = "path:s?",
2832 .params = "[path]",
2833 .help = "show QOM composition tree",
2834 .mhandler.cmd = hmp_info_qom_tree,
2835 },
2836 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002837 .name = "roms",
2838 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002839 .params = "",
2840 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002841 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002842 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302843 {
2844 .name = "trace-events",
2845 .args_type = "",
2846 .params = "",
2847 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002848 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302849 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002851 .name = "tpm",
2852 .args_type = "",
2853 .params = "",
2854 .help = "show the TPM device",
2855 .mhandler.cmd = hmp_info_tpm,
2856 },
2857 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002858 .name = "memdev",
2859 .args_type = "",
2860 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002861 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002862 .mhandler.cmd = hmp_info_memdev,
2863 },
2864 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002865 .name = "memory-devices",
2866 .args_type = "",
2867 .params = "",
2868 .help = "show memory devices",
2869 .mhandler.cmd = hmp_info_memory_devices,
2870 },
2871 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 .name = NULL,
2873 },
bellard9dc39cb2004-03-14 21:38:27 +00002874};
2875
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002876/* mon_cmds and info_cmds would be sorted at runtime */
2877static mon_cmd_t mon_cmds[] = {
2878#include "hmp-commands.h"
2879 { NULL, NULL, },
2880};
2881
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002882static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002883#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002884 { /* NULL */ },
2885};
2886
bellard9307c4c2004-04-04 12:57:25 +00002887/*******************************************************************/
2888
2889static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002890static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002891
bellard92a31b12005-02-10 22:00:52 +00002892#define MD_TLONG 0
2893#define MD_I32 1
2894
bellard9307c4c2004-04-04 12:57:25 +00002895typedef struct MonitorDef {
2896 const char *name;
2897 int offset;
blueswir18662d652008-10-02 18:32:44 +00002898 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002899 int type;
bellard9307c4c2004-04-04 12:57:25 +00002900} MonitorDef;
2901
bellard57206fd2004-04-25 18:54:52 +00002902#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002903static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002904{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002905 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002906 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002907}
2908#endif
2909
bellarda541f292004-04-12 20:39:29 +00002910#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002911static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002912{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002913 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002914 unsigned int u;
2915 int i;
2916
2917 u = 0;
2918 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002919 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002920
2921 return u;
2922}
2923
blueswir18662d652008-10-02 18:32:44 +00002924static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002925{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002926 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002927 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002928}
2929
blueswir18662d652008-10-02 18:32:44 +00002930static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002931{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002932 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002933 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002934}
bellard9fddaa02004-05-21 12:59:32 +00002935
blueswir18662d652008-10-02 18:32:44 +00002936static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002937{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002938 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002939 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002940}
2941
blueswir18662d652008-10-02 18:32:44 +00002942static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002943{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002944 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002945 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002946}
2947
blueswir18662d652008-10-02 18:32:44 +00002948static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002949{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002950 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002951 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002952}
bellarda541f292004-04-12 20:39:29 +00002953#endif
2954
bellarde95c8d52004-09-30 22:22:08 +00002955#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002956#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002957static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002958{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002959 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002960
2961 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002962}
bellard7b936c02005-10-30 17:05:13 +00002963#endif
bellarde95c8d52004-09-30 22:22:08 +00002964
blueswir18662d652008-10-02 18:32:44 +00002965static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002966{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002967 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002968 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002969}
2970#endif
2971
blueswir18662d652008-10-02 18:32:44 +00002972static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002973#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002974
2975#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002976 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2977 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2978 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002979
Andreas Färbere59d1672012-02-16 00:40:47 +01002980 { "eax", offsetof(CPUX86State, regs[0]) },
2981 { "ecx", offsetof(CPUX86State, regs[1]) },
2982 { "edx", offsetof(CPUX86State, regs[2]) },
2983 { "ebx", offsetof(CPUX86State, regs[3]) },
2984 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2985 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2986 { "esi", offsetof(CPUX86State, regs[6]) },
2987 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002988#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002989 { "r8", offsetof(CPUX86State, regs[8]) },
2990 { "r9", offsetof(CPUX86State, regs[9]) },
2991 { "r10", offsetof(CPUX86State, regs[10]) },
2992 { "r11", offsetof(CPUX86State, regs[11]) },
2993 { "r12", offsetof(CPUX86State, regs[12]) },
2994 { "r13", offsetof(CPUX86State, regs[13]) },
2995 { "r14", offsetof(CPUX86State, regs[14]) },
2996 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002997#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002998 { "eflags", offsetof(CPUX86State, eflags) },
2999 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003000 SEG("cs", R_CS)
3001 SEG("ds", R_DS)
3002 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003003 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003004 SEG("fs", R_FS)
3005 SEG("gs", R_GS)
3006 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003007#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003008 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003009 { "r0", offsetof(CPUPPCState, gpr[0]) },
3010 { "r1", offsetof(CPUPPCState, gpr[1]) },
3011 { "r2", offsetof(CPUPPCState, gpr[2]) },
3012 { "r3", offsetof(CPUPPCState, gpr[3]) },
3013 { "r4", offsetof(CPUPPCState, gpr[4]) },
3014 { "r5", offsetof(CPUPPCState, gpr[5]) },
3015 { "r6", offsetof(CPUPPCState, gpr[6]) },
3016 { "r7", offsetof(CPUPPCState, gpr[7]) },
3017 { "r8", offsetof(CPUPPCState, gpr[8]) },
3018 { "r9", offsetof(CPUPPCState, gpr[9]) },
3019 { "r10", offsetof(CPUPPCState, gpr[10]) },
3020 { "r11", offsetof(CPUPPCState, gpr[11]) },
3021 { "r12", offsetof(CPUPPCState, gpr[12]) },
3022 { "r13", offsetof(CPUPPCState, gpr[13]) },
3023 { "r14", offsetof(CPUPPCState, gpr[14]) },
3024 { "r15", offsetof(CPUPPCState, gpr[15]) },
3025 { "r16", offsetof(CPUPPCState, gpr[16]) },
3026 { "r17", offsetof(CPUPPCState, gpr[17]) },
3027 { "r18", offsetof(CPUPPCState, gpr[18]) },
3028 { "r19", offsetof(CPUPPCState, gpr[19]) },
3029 { "r20", offsetof(CPUPPCState, gpr[20]) },
3030 { "r21", offsetof(CPUPPCState, gpr[21]) },
3031 { "r22", offsetof(CPUPPCState, gpr[22]) },
3032 { "r23", offsetof(CPUPPCState, gpr[23]) },
3033 { "r24", offsetof(CPUPPCState, gpr[24]) },
3034 { "r25", offsetof(CPUPPCState, gpr[25]) },
3035 { "r26", offsetof(CPUPPCState, gpr[26]) },
3036 { "r27", offsetof(CPUPPCState, gpr[27]) },
3037 { "r28", offsetof(CPUPPCState, gpr[28]) },
3038 { "r29", offsetof(CPUPPCState, gpr[29]) },
3039 { "r30", offsetof(CPUPPCState, gpr[30]) },
3040 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003041 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003042 { "f0", offsetof(CPUPPCState, fpr[0]) },
3043 { "f1", offsetof(CPUPPCState, fpr[1]) },
3044 { "f2", offsetof(CPUPPCState, fpr[2]) },
3045 { "f3", offsetof(CPUPPCState, fpr[3]) },
3046 { "f4", offsetof(CPUPPCState, fpr[4]) },
3047 { "f5", offsetof(CPUPPCState, fpr[5]) },
3048 { "f6", offsetof(CPUPPCState, fpr[6]) },
3049 { "f7", offsetof(CPUPPCState, fpr[7]) },
3050 { "f8", offsetof(CPUPPCState, fpr[8]) },
3051 { "f9", offsetof(CPUPPCState, fpr[9]) },
3052 { "f10", offsetof(CPUPPCState, fpr[10]) },
3053 { "f11", offsetof(CPUPPCState, fpr[11]) },
3054 { "f12", offsetof(CPUPPCState, fpr[12]) },
3055 { "f13", offsetof(CPUPPCState, fpr[13]) },
3056 { "f14", offsetof(CPUPPCState, fpr[14]) },
3057 { "f15", offsetof(CPUPPCState, fpr[15]) },
3058 { "f16", offsetof(CPUPPCState, fpr[16]) },
3059 { "f17", offsetof(CPUPPCState, fpr[17]) },
3060 { "f18", offsetof(CPUPPCState, fpr[18]) },
3061 { "f19", offsetof(CPUPPCState, fpr[19]) },
3062 { "f20", offsetof(CPUPPCState, fpr[20]) },
3063 { "f21", offsetof(CPUPPCState, fpr[21]) },
3064 { "f22", offsetof(CPUPPCState, fpr[22]) },
3065 { "f23", offsetof(CPUPPCState, fpr[23]) },
3066 { "f24", offsetof(CPUPPCState, fpr[24]) },
3067 { "f25", offsetof(CPUPPCState, fpr[25]) },
3068 { "f26", offsetof(CPUPPCState, fpr[26]) },
3069 { "f27", offsetof(CPUPPCState, fpr[27]) },
3070 { "f28", offsetof(CPUPPCState, fpr[28]) },
3071 { "f29", offsetof(CPUPPCState, fpr[29]) },
3072 { "f30", offsetof(CPUPPCState, fpr[30]) },
3073 { "f31", offsetof(CPUPPCState, fpr[31]) },
3074 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003075 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003076 { "nip|pc", offsetof(CPUPPCState, nip) },
3077 { "lr", offsetof(CPUPPCState, lr) },
3078 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003079 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003080 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003081 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003082 { "msr", 0, &monitor_get_msr, },
3083 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003084 { "tbu", 0, &monitor_get_tbu, },
3085 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003086 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003087 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3088 { "sr0", offsetof(CPUPPCState, sr[0]) },
3089 { "sr1", offsetof(CPUPPCState, sr[1]) },
3090 { "sr2", offsetof(CPUPPCState, sr[2]) },
3091 { "sr3", offsetof(CPUPPCState, sr[3]) },
3092 { "sr4", offsetof(CPUPPCState, sr[4]) },
3093 { "sr5", offsetof(CPUPPCState, sr[5]) },
3094 { "sr6", offsetof(CPUPPCState, sr[6]) },
3095 { "sr7", offsetof(CPUPPCState, sr[7]) },
3096 { "sr8", offsetof(CPUPPCState, sr[8]) },
3097 { "sr9", offsetof(CPUPPCState, sr[9]) },
3098 { "sr10", offsetof(CPUPPCState, sr[10]) },
3099 { "sr11", offsetof(CPUPPCState, sr[11]) },
3100 { "sr12", offsetof(CPUPPCState, sr[12]) },
3101 { "sr13", offsetof(CPUPPCState, sr[13]) },
3102 { "sr14", offsetof(CPUPPCState, sr[14]) },
3103 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003104 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003105 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003106
Andreas Färbere59d1672012-02-16 00:40:47 +01003107 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3108 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003109 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3110 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3111 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003112 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3113 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3114 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3115 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3116 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3117 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3118 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3119 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3120 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3121 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3122 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3123 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3124 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3125 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3126 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3127 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3128 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3129 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3130 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3131 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3132 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3133 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3134 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3135 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3136 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3137 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3138 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3139 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3140 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3141 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3142 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3143 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3144 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3145 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3146 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3147 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3148 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3149 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3150 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3151 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3152 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3153 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3154 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3155 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3156 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3157 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3158 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3159 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3160 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3161 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3162 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3163 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3164 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3165 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3166 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3167 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3168 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3169 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3170 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3171 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3172 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3173 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3174 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3175 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003176
bellarde95c8d52004-09-30 22:22:08 +00003177#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003178 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3179 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3180 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3181 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3182 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3183 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3184 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3185 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003186 { "o0", 0, monitor_get_reg },
3187 { "o1", 1, monitor_get_reg },
3188 { "o2", 2, monitor_get_reg },
3189 { "o3", 3, monitor_get_reg },
3190 { "o4", 4, monitor_get_reg },
3191 { "o5", 5, monitor_get_reg },
3192 { "o6", 6, monitor_get_reg },
3193 { "o7", 7, monitor_get_reg },
3194 { "l0", 8, monitor_get_reg },
3195 { "l1", 9, monitor_get_reg },
3196 { "l2", 10, monitor_get_reg },
3197 { "l3", 11, monitor_get_reg },
3198 { "l4", 12, monitor_get_reg },
3199 { "l5", 13, monitor_get_reg },
3200 { "l6", 14, monitor_get_reg },
3201 { "l7", 15, monitor_get_reg },
3202 { "i0", 16, monitor_get_reg },
3203 { "i1", 17, monitor_get_reg },
3204 { "i2", 18, monitor_get_reg },
3205 { "i3", 19, monitor_get_reg },
3206 { "i4", 20, monitor_get_reg },
3207 { "i5", 21, monitor_get_reg },
3208 { "i6", 22, monitor_get_reg },
3209 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003210 { "pc", offsetof(CPUSPARCState, pc) },
3211 { "npc", offsetof(CPUSPARCState, npc) },
3212 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003213#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003214 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003215 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003216#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003217 { "tbr", offsetof(CPUSPARCState, tbr) },
3218 { "fsr", offsetof(CPUSPARCState, fsr) },
3219 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3220 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3221 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3222 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3223 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3224 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3225 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3226 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3227 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3228 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3229 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3230 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3231 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3232 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3233 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3234 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3235 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3236 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3237 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3238 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3239 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3240 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3241 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3242 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3243 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3244 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3245 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3246 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3247 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3248 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3249 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3250 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003251#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003252 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3253 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3254 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3255 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3256 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3257 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3258 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3259 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3260 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3261 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3262 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3263 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3264 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3265 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3266 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3267 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3268 { "asi", offsetof(CPUSPARCState, asi) },
3269 { "pstate", offsetof(CPUSPARCState, pstate) },
3270 { "cansave", offsetof(CPUSPARCState, cansave) },
3271 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3272 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3273 { "wstate", offsetof(CPUSPARCState, wstate) },
3274 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3275 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003276#endif
bellard9307c4c2004-04-04 12:57:25 +00003277#endif
3278 { NULL },
3279};
3280
Stefan Weil9c3175c2013-08-22 21:30:09 +02003281static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3282expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003283{
Fam Zheng277acfe2013-08-20 10:58:21 +08003284 va_list ap;
3285 va_start(ap, fmt);
3286 monitor_vprintf(mon, fmt, ap);
3287 monitor_printf(mon, "\n");
3288 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003289 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003290}
3291
Markus Armbruster09b94182010-01-20 13:07:30 +01003292/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003293static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003294{
blueswir18662d652008-10-02 18:32:44 +00003295 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003296 void *ptr;
3297
bellard9307c4c2004-04-04 12:57:25 +00003298 for(md = monitor_defs; md->name != NULL; md++) {
3299 if (compare_cmd(name, md->name)) {
3300 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003301 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003302 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003303 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003304 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003305 switch(md->type) {
3306 case MD_I32:
3307 *pval = *(int32_t *)ptr;
3308 break;
3309 case MD_TLONG:
3310 *pval = *(target_long *)ptr;
3311 break;
3312 default:
3313 *pval = 0;
3314 break;
3315 }
bellard9307c4c2004-04-04 12:57:25 +00003316 }
3317 return 0;
3318 }
3319 }
3320 return -1;
3321}
3322
3323static void next(void)
3324{
Blue Swirl660f11b2009-07-31 21:16:51 +00003325 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003326 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003327 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003328 pch++;
3329 }
3330}
3331
aliguori376253e2009-03-05 23:01:23 +00003332static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003333
aliguori376253e2009-03-05 23:01:23 +00003334static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003335{
blueswir1c2efc952007-09-25 17:28:42 +00003336 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003337 char *p;
bellard6a00d602005-11-21 23:25:50 +00003338 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003339
3340 switch(*pch) {
3341 case '+':
3342 next();
aliguori376253e2009-03-05 23:01:23 +00003343 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003344 break;
3345 case '-':
3346 next();
aliguori376253e2009-03-05 23:01:23 +00003347 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003348 break;
3349 case '~':
3350 next();
aliguori376253e2009-03-05 23:01:23 +00003351 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003352 break;
3353 case '(':
3354 next();
aliguori376253e2009-03-05 23:01:23 +00003355 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003356 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003357 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003358 }
3359 next();
3360 break;
bellard81d09122004-07-14 17:21:37 +00003361 case '\'':
3362 pch++;
3363 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003364 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003365 n = *pch;
3366 pch++;
3367 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003368 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003369 next();
3370 break;
bellard9307c4c2004-04-04 12:57:25 +00003371 case '$':
3372 {
3373 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003374 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003375
bellard9307c4c2004-04-04 12:57:25 +00003376 pch++;
3377 q = buf;
3378 while ((*pch >= 'a' && *pch <= 'z') ||
3379 (*pch >= 'A' && *pch <= 'Z') ||
3380 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003381 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003382 if ((q - buf) < sizeof(buf) - 1)
3383 *q++ = *pch;
3384 pch++;
3385 }
blueswir1cd390082008-11-16 13:53:32 +00003386 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003387 pch++;
3388 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003389 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003390 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003391 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003392 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003393 }
3394 break;
3395 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003396 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003397 n = 0;
3398 break;
3399 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003400 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003401 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003402 if (errno == ERANGE) {
3403 expr_error(mon, "number too large");
3404 }
bellard9307c4c2004-04-04 12:57:25 +00003405 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003406 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003407 }
3408 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003409 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003410 pch++;
3411 break;
3412 }
3413 return n;
3414}
3415
3416
aliguori376253e2009-03-05 23:01:23 +00003417static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003418{
blueswir1c2efc952007-09-25 17:28:42 +00003419 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003420 int op;
ths3b46e622007-09-17 08:09:54 +00003421
aliguori376253e2009-03-05 23:01:23 +00003422 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003423 for(;;) {
3424 op = *pch;
3425 if (op != '*' && op != '/' && op != '%')
3426 break;
3427 next();
aliguori376253e2009-03-05 23:01:23 +00003428 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003429 switch(op) {
3430 default:
3431 case '*':
3432 val *= val2;
3433 break;
3434 case '/':
3435 case '%':
ths5fafdf22007-09-16 21:08:06 +00003436 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003437 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003438 if (op == '/')
3439 val /= val2;
3440 else
3441 val %= val2;
3442 break;
3443 }
3444 }
3445 return val;
3446}
3447
aliguori376253e2009-03-05 23:01:23 +00003448static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003449{
blueswir1c2efc952007-09-25 17:28:42 +00003450 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003451 int op;
bellard9307c4c2004-04-04 12:57:25 +00003452
aliguori376253e2009-03-05 23:01:23 +00003453 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003454 for(;;) {
3455 op = *pch;
3456 if (op != '&' && op != '|' && op != '^')
3457 break;
3458 next();
aliguori376253e2009-03-05 23:01:23 +00003459 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003460 switch(op) {
3461 default:
3462 case '&':
3463 val &= val2;
3464 break;
3465 case '|':
3466 val |= val2;
3467 break;
3468 case '^':
3469 val ^= val2;
3470 break;
3471 }
3472 }
3473 return val;
3474}
3475
aliguori376253e2009-03-05 23:01:23 +00003476static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003477{
blueswir1c2efc952007-09-25 17:28:42 +00003478 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003479 int op;
bellard9307c4c2004-04-04 12:57:25 +00003480
aliguori376253e2009-03-05 23:01:23 +00003481 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003482 for(;;) {
3483 op = *pch;
3484 if (op != '+' && op != '-')
3485 break;
3486 next();
aliguori376253e2009-03-05 23:01:23 +00003487 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003488 if (op == '+')
3489 val += val2;
3490 else
3491 val -= val2;
3492 }
3493 return val;
3494}
3495
aliguori376253e2009-03-05 23:01:23 +00003496static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003497{
3498 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003499 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003500 *pp = pch;
3501 return -1;
3502 }
blueswir1cd390082008-11-16 13:53:32 +00003503 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003504 pch++;
aliguori376253e2009-03-05 23:01:23 +00003505 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003506 *pp = pch;
3507 return 0;
3508}
3509
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003510static int get_double(Monitor *mon, double *pval, const char **pp)
3511{
3512 const char *p = *pp;
3513 char *tailp;
3514 double d;
3515
3516 d = strtod(p, &tailp);
3517 if (tailp == p) {
3518 monitor_printf(mon, "Number expected\n");
3519 return -1;
3520 }
3521 if (d != d || d - d != 0) {
3522 /* NaN or infinity */
3523 monitor_printf(mon, "Bad number\n");
3524 return -1;
3525 }
3526 *pval = d;
3527 *pp = tailp;
3528 return 0;
3529}
3530
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003531/*
3532 * Store the command-name in cmdname, and return a pointer to
3533 * the remaining of the command string.
3534 */
3535static const char *get_command_name(const char *cmdline,
3536 char *cmdname, size_t nlen)
3537{
3538 size_t len;
3539 const char *p, *pstart;
3540
3541 p = cmdline;
3542 while (qemu_isspace(*p))
3543 p++;
3544 if (*p == '\0')
3545 return NULL;
3546 pstart = p;
3547 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3548 p++;
3549 len = p - pstart;
3550 if (len > nlen - 1)
3551 len = nlen - 1;
3552 memcpy(cmdname, pstart, len);
3553 cmdname[len] = '\0';
3554 return p;
3555}
3556
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003557/**
3558 * Read key of 'type' into 'key' and return the current
3559 * 'type' pointer.
3560 */
3561static char *key_get_info(const char *type, char **key)
3562{
3563 size_t len;
3564 char *p, *str;
3565
3566 if (*type == ',')
3567 type++;
3568
3569 p = strchr(type, ':');
3570 if (!p) {
3571 *key = NULL;
3572 return NULL;
3573 }
3574 len = p - type;
3575
Anthony Liguori7267c092011-08-20 22:09:37 -05003576 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003577 memcpy(str, type, len);
3578 str[len] = '\0';
3579
3580 *key = str;
3581 return ++p;
3582}
3583
bellard9307c4c2004-04-04 12:57:25 +00003584static int default_fmt_format = 'x';
3585static int default_fmt_size = 4;
3586
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003587static int is_valid_option(const char *c, const char *typestr)
3588{
3589 char option[3];
3590
3591 option[0] = '-';
3592 option[1] = *c;
3593 option[2] = '\0';
3594
3595 typestr = strstr(typestr, option);
3596 return (typestr != NULL);
3597}
3598
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003599static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3600 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003601{
3602 const mon_cmd_t *cmd;
3603
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003604 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003605 if (compare_cmd(cmdname, cmd->name)) {
3606 return cmd;
3607 }
3608 }
3609
3610 return NULL;
3611}
3612
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003613static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3614{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003615 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003616}
3617
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003618/*
3619 * Parse @cmdline according to command table @table.
3620 * If @cmdline is blank, return NULL.
3621 * If it can't be parsed, report to @mon, and return NULL.
3622 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003623 * If a sub-command table exists, and if @cmdline contains an additional string
3624 * for a sub-command, this function will try to search the sub-command table.
3625 * If no additional string for a sub-command is present, this function will
3626 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003627 * Do not assume the returned command points into @table! It doesn't
3628 * when the command is a sub-command.
3629 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003630static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003631 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003632 int start,
3633 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003634 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003635{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003636 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003637 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003638 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003639 char cmdname[256];
3640 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003641 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003642
3643#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003644 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003645#endif
ths3b46e622007-09-17 08:09:54 +00003646
bellard9307c4c2004-04-04 12:57:25 +00003647 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003648 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003649 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003650 return NULL;
ths3b46e622007-09-17 08:09:54 +00003651
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003652 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003653 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003654 monitor_printf(mon, "unknown command: '%.*s'\n",
3655 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003656 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003657 }
bellard9307c4c2004-04-04 12:57:25 +00003658
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003659 /* filter out following useless space */
3660 while (qemu_isspace(*p)) {
3661 p++;
3662 }
3663 /* search sub command */
3664 if (cmd->sub_table != NULL) {
3665 /* check if user set additional command */
3666 if (*p == '\0') {
3667 return cmd;
3668 }
3669 return monitor_parse_command(mon, cmdline, p - cmdline,
3670 cmd->sub_table, qdict);
3671 }
3672
bellard9307c4c2004-04-04 12:57:25 +00003673 /* parse the parameters */
3674 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003675 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003676 typestr = key_get_info(typestr, &key);
3677 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003678 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003679 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003680 typestr++;
3681 switch(c) {
3682 case 'F':
bellard81d09122004-07-14 17:21:37 +00003683 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003684 case 's':
3685 {
3686 int ret;
ths3b46e622007-09-17 08:09:54 +00003687
blueswir1cd390082008-11-16 13:53:32 +00003688 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003689 p++;
3690 if (*typestr == '?') {
3691 typestr++;
3692 if (*p == '\0') {
3693 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003694 break;
bellard9307c4c2004-04-04 12:57:25 +00003695 }
3696 }
3697 ret = get_str(buf, sizeof(buf), &p);
3698 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003699 switch(c) {
3700 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003701 monitor_printf(mon, "%s: filename expected\n",
3702 cmdname);
bellard81d09122004-07-14 17:21:37 +00003703 break;
3704 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003705 monitor_printf(mon, "%s: block device name expected\n",
3706 cmdname);
bellard81d09122004-07-14 17:21:37 +00003707 break;
3708 default:
aliguori376253e2009-03-05 23:01:23 +00003709 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003710 break;
3711 }
bellard9307c4c2004-04-04 12:57:25 +00003712 goto fail;
3713 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003714 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003715 }
3716 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003717 case 'O':
3718 {
3719 QemuOptsList *opts_list;
3720 QemuOpts *opts;
3721
3722 opts_list = qemu_find_opts(key);
3723 if (!opts_list || opts_list->desc->name) {
3724 goto bad_type;
3725 }
3726 while (qemu_isspace(*p)) {
3727 p++;
3728 }
3729 if (!*p)
3730 break;
3731 if (get_str(buf, sizeof(buf), &p) < 0) {
3732 goto fail;
3733 }
3734 opts = qemu_opts_parse(opts_list, buf, 1);
3735 if (!opts) {
3736 goto fail;
3737 }
3738 qemu_opts_to_qdict(opts, qdict);
3739 qemu_opts_del(opts);
3740 }
3741 break;
bellard9307c4c2004-04-04 12:57:25 +00003742 case '/':
3743 {
3744 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003745
blueswir1cd390082008-11-16 13:53:32 +00003746 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003747 p++;
3748 if (*p == '/') {
3749 /* format found */
3750 p++;
3751 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003752 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003753 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003754 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003755 count = count * 10 + (*p - '0');
3756 p++;
3757 }
3758 }
3759 size = -1;
3760 format = -1;
3761 for(;;) {
3762 switch(*p) {
3763 case 'o':
3764 case 'd':
3765 case 'u':
3766 case 'x':
3767 case 'i':
3768 case 'c':
3769 format = *p++;
3770 break;
3771 case 'b':
3772 size = 1;
3773 p++;
3774 break;
3775 case 'h':
3776 size = 2;
3777 p++;
3778 break;
3779 case 'w':
3780 size = 4;
3781 p++;
3782 break;
3783 case 'g':
3784 case 'L':
3785 size = 8;
3786 p++;
3787 break;
3788 default:
3789 goto next;
3790 }
3791 }
3792 next:
blueswir1cd390082008-11-16 13:53:32 +00003793 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003794 monitor_printf(mon, "invalid char in format: '%c'\n",
3795 *p);
bellard9307c4c2004-04-04 12:57:25 +00003796 goto fail;
3797 }
bellard9307c4c2004-04-04 12:57:25 +00003798 if (format < 0)
3799 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003800 if (format != 'i') {
3801 /* for 'i', not specifying a size gives -1 as size */
3802 if (size < 0)
3803 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003804 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003805 }
bellard9307c4c2004-04-04 12:57:25 +00003806 default_fmt_format = format;
3807 } else {
3808 count = 1;
3809 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003810 if (format != 'i') {
3811 size = default_fmt_size;
3812 } else {
3813 size = -1;
3814 }
bellard9307c4c2004-04-04 12:57:25 +00003815 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003816 qdict_put(qdict, "count", qint_from_int(count));
3817 qdict_put(qdict, "format", qint_from_int(format));
3818 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003819 }
3820 break;
3821 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003822 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003823 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003824 {
blueswir1c2efc952007-09-25 17:28:42 +00003825 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003826
blueswir1cd390082008-11-16 13:53:32 +00003827 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003828 p++;
bellard34405572004-06-08 00:55:58 +00003829 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003830 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003831 if (*p == '\0') {
3832 typestr++;
3833 break;
3834 }
bellard34405572004-06-08 00:55:58 +00003835 } else {
3836 if (*p == '.') {
3837 p++;
blueswir1cd390082008-11-16 13:53:32 +00003838 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003839 p++;
bellard34405572004-06-08 00:55:58 +00003840 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003841 typestr++;
3842 break;
bellard34405572004-06-08 00:55:58 +00003843 }
3844 }
bellard13224a82006-07-14 22:03:35 +00003845 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003846 }
aliguori376253e2009-03-05 23:01:23 +00003847 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003848 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003849 /* Check if 'i' is greater than 32-bit */
3850 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3851 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3852 monitor_printf(mon, "integer is for 32-bit values\n");
3853 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003854 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003855 if (val < 0) {
3856 monitor_printf(mon, "enter a positive value\n");
3857 goto fail;
3858 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003859 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003860 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003861 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003862 }
3863 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003864 case 'o':
3865 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003866 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003867 char *end;
3868
3869 while (qemu_isspace(*p)) {
3870 p++;
3871 }
3872 if (*typestr == '?') {
3873 typestr++;
3874 if (*p == '\0') {
3875 break;
3876 }
3877 }
3878 val = strtosz(p, &end);
3879 if (val < 0) {
3880 monitor_printf(mon, "invalid size\n");
3881 goto fail;
3882 }
3883 qdict_put(qdict, key, qint_from_int(val));
3884 p = end;
3885 }
3886 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003887 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003888 {
3889 double val;
3890
3891 while (qemu_isspace(*p))
3892 p++;
3893 if (*typestr == '?') {
3894 typestr++;
3895 if (*p == '\0') {
3896 break;
3897 }
3898 }
3899 if (get_double(mon, &val, &p) < 0) {
3900 goto fail;
3901 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003902 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003903 switch (*p) {
3904 case 'm':
3905 val /= 1e3; p += 2; break;
3906 case 'u':
3907 val /= 1e6; p += 2; break;
3908 case 'n':
3909 val /= 1e9; p += 2; break;
3910 }
3911 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003912 if (*p && !qemu_isspace(*p)) {
3913 monitor_printf(mon, "Unknown unit suffix\n");
3914 goto fail;
3915 }
3916 qdict_put(qdict, key, qfloat_from_double(val));
3917 }
3918 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003919 case 'b':
3920 {
3921 const char *beg;
3922 int val;
3923
3924 while (qemu_isspace(*p)) {
3925 p++;
3926 }
3927 beg = p;
3928 while (qemu_isgraph(*p)) {
3929 p++;
3930 }
3931 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3932 val = 1;
3933 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3934 val = 0;
3935 } else {
3936 monitor_printf(mon, "Expected 'on' or 'off'\n");
3937 goto fail;
3938 }
3939 qdict_put(qdict, key, qbool_from_int(val));
3940 }
3941 break;
bellard9307c4c2004-04-04 12:57:25 +00003942 case '-':
3943 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003944 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003945 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003946 /* option */
ths3b46e622007-09-17 08:09:54 +00003947
bellard9307c4c2004-04-04 12:57:25 +00003948 c = *typestr++;
3949 if (c == '\0')
3950 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003951 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003952 p++;
bellard9307c4c2004-04-04 12:57:25 +00003953 if (*p == '-') {
3954 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003955 if(c != *p) {
3956 if(!is_valid_option(p, typestr)) {
3957
3958 monitor_printf(mon, "%s: unsupported option -%c\n",
3959 cmdname, *p);
3960 goto fail;
3961 } else {
3962 skip_key = 1;
3963 }
bellard9307c4c2004-04-04 12:57:25 +00003964 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003965 if(skip_key) {
3966 p = tmp;
3967 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003968 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003969 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003970 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003971 }
bellard9307c4c2004-04-04 12:57:25 +00003972 }
bellard9307c4c2004-04-04 12:57:25 +00003973 }
3974 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003975 case 'S':
3976 {
3977 /* package all remaining string */
3978 int len;
3979
3980 while (qemu_isspace(*p)) {
3981 p++;
3982 }
3983 if (*typestr == '?') {
3984 typestr++;
3985 if (*p == '\0') {
3986 /* no remaining string: NULL argument */
3987 break;
3988 }
3989 }
3990 len = strlen(p);
3991 if (len <= 0) {
3992 monitor_printf(mon, "%s: string expected\n",
3993 cmdname);
3994 break;
3995 }
3996 qdict_put(qdict, key, qstring_from_str(p));
3997 p += len;
3998 }
3999 break;
bellard9307c4c2004-04-04 12:57:25 +00004000 default:
4001 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004002 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004003 goto fail;
4004 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004005 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004006 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004007 }
4008 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004009 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004010 p++;
4011 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004012 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4013 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004014 goto fail;
4015 }
4016
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004017 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004018
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004019fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004020 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004021 return NULL;
4022}
4023
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004024void monitor_set_error(Monitor *mon, QError *qerror)
4025{
4026 /* report only the first error */
4027 if (!mon->error) {
4028 mon->error = qerror;
4029 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004030 QDECREF(qerror);
4031 }
4032}
4033
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004034static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004035{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004036 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004037 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004038
4039 qdict = qdict_new();
4040
Wenchao Xia77172392013-08-27 20:38:20 +08004041 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Markus Armbruster8a4f5012015-03-05 18:50:05 +01004042 if (cmd) {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004043 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004044 }
4045
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004046 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004047}
4048
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004049static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004050{
4051 const char *p, *pstart;
4052 char cmd[128];
4053 int len;
4054
4055 p = list;
4056 for(;;) {
4057 pstart = p;
4058 p = strchr(p, '|');
4059 if (!p)
4060 p = pstart + strlen(pstart);
4061 len = p - pstart;
4062 if (len > sizeof(cmd) - 2)
4063 len = sizeof(cmd) - 2;
4064 memcpy(cmd, pstart, len);
4065 cmd[len] = '\0';
4066 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004067 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004068 }
4069 if (*p == '\0')
4070 break;
4071 p++;
4072 }
4073}
4074
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004075static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004076{
4077 DIR *ffs;
4078 struct dirent *d;
4079 char path[1024];
4080 char file[1024], file_prefix[1024];
4081 int input_path_len;
4082 const char *p;
4083
ths5fafdf22007-09-16 21:08:06 +00004084 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004085 if (!p) {
4086 input_path_len = 0;
4087 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004088 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004089 } else {
4090 input_path_len = p - input + 1;
4091 memcpy(path, input, input_path_len);
4092 if (input_path_len > sizeof(path) - 1)
4093 input_path_len = sizeof(path) - 1;
4094 path[input_path_len] = '\0';
4095 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4096 }
4097#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004098 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004099 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004100#endif
4101 ffs = opendir(path);
4102 if (!ffs)
4103 return;
4104 for(;;) {
4105 struct stat sb;
4106 d = readdir(ffs);
4107 if (!d)
4108 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004109
4110 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4111 continue;
4112 }
4113
bellard81d09122004-07-14 17:21:37 +00004114 if (strstart(d->d_name, file_prefix, NULL)) {
4115 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004116 if (input_path_len < sizeof(file))
4117 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4118 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004119 /* stat the file to find out if it's a directory.
4120 * In that case add a slash to speed up typing long paths
4121 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004122 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004123 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004124 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004125 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004126 }
4127 }
4128 closedir(ffs);
4129}
4130
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004131static const char *next_arg_type(const char *typestr)
4132{
4133 const char *p = strchr(typestr, ':');
4134 return (p != NULL ? ++p : typestr);
4135}
4136
Hani Benhabiles40d19392014-05-07 23:41:30 +01004137static void add_completion_option(ReadLineState *rs, const char *str,
4138 const char *option)
4139{
4140 if (!str || !option) {
4141 return;
4142 }
4143 if (!strncmp(option, str, strlen(str))) {
4144 readline_add_completion(rs, option);
4145 }
4146}
4147
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004148void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4149{
4150 size_t len;
4151 ChardevBackendInfoList *list, *start;
4152
4153 if (nb_args != 2) {
4154 return;
4155 }
4156 len = strlen(str);
4157 readline_set_completion_index(rs, len);
4158
4159 start = list = qmp_query_chardev_backends(NULL);
4160 while (list) {
4161 const char *chr_name = list->value->name;
4162
4163 if (!strncmp(chr_name, str, len)) {
4164 readline_add_completion(rs, chr_name);
4165 }
4166 list = list->next;
4167 }
4168 qapi_free_ChardevBackendInfoList(start);
4169}
4170
Hani Benhabilesb162b492014-05-07 23:41:31 +01004171void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4172{
4173 size_t len;
4174 int i;
4175
4176 if (nb_args != 2) {
4177 return;
4178 }
4179 len = strlen(str);
4180 readline_set_completion_index(rs, len);
4181 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4182 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4183 }
4184}
4185
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004186void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004187{
4188 GSList *list, *elt;
4189 size_t len;
4190
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004191 if (nb_args != 2) {
4192 return;
4193 }
4194
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004195 len = strlen(str);
4196 readline_set_completion_index(rs, len);
4197 list = elt = object_class_get_list(TYPE_DEVICE, false);
4198 while (elt) {
4199 const char *name;
4200 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4201 TYPE_DEVICE);
4202 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004203
4204 if (!dc->cannot_instantiate_with_device_add_yet
4205 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004206 readline_add_completion(rs, name);
4207 }
4208 elt = elt->next;
4209 }
4210 g_slist_free(list);
4211}
4212
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004213void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004214{
4215 GSList *list, *elt;
4216 size_t len;
4217
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004218 if (nb_args != 2) {
4219 return;
4220 }
4221
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004222 len = strlen(str);
4223 readline_set_completion_index(rs, len);
4224 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4225 while (elt) {
4226 const char *name;
4227
4228 name = object_class_get_name(OBJECT_CLASS(elt->data));
4229 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4230 readline_add_completion(rs, name);
4231 }
4232 elt = elt->next;
4233 }
4234 g_slist_free(list);
4235}
4236
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004237static void peripheral_device_del_completion(ReadLineState *rs,
4238 const char *str, size_t len)
4239{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004240 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4241 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004242
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004243 list = qdev_build_hotpluggable_device_list(peripheral);
4244 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004245 return;
4246 }
4247
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004248 for (item = list; item; item = g_slist_next(item)) {
4249 DeviceState *dev = item->data;
4250
4251 if (dev->id && !strncmp(str, dev->id, len)) {
4252 readline_add_completion(rs, dev->id);
4253 }
4254 }
4255
4256 g_slist_free(list);
4257}
4258
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004259void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4260{
4261 size_t len;
4262 ChardevInfoList *list, *start;
4263
4264 if (nb_args != 2) {
4265 return;
4266 }
4267 len = strlen(str);
4268 readline_set_completion_index(rs, len);
4269
4270 start = list = qmp_query_chardev(NULL);
4271 while (list) {
4272 ChardevInfo *chr = list->value;
4273
4274 if (!strncmp(chr->label, str, len)) {
4275 readline_add_completion(rs, chr->label);
4276 }
4277 list = list->next;
4278 }
4279 qapi_free_ChardevInfoList(start);
4280}
4281
Hani Benhabiles8e597772014-05-27 23:39:30 +01004282static void ringbuf_completion(ReadLineState *rs, const char *str)
4283{
4284 size_t len;
4285 ChardevInfoList *list, *start;
4286
4287 len = strlen(str);
4288 readline_set_completion_index(rs, len);
4289
4290 start = list = qmp_query_chardev(NULL);
4291 while (list) {
4292 ChardevInfo *chr_info = list->value;
4293
4294 if (!strncmp(chr_info->label, str, len)) {
4295 CharDriverState *chr = qemu_chr_find(chr_info->label);
4296 if (chr && chr_is_ringbuf(chr)) {
4297 readline_add_completion(rs, chr_info->label);
4298 }
4299 }
4300 list = list->next;
4301 }
4302 qapi_free_ChardevInfoList(start);
4303}
4304
Hani Benhabiles8e597772014-05-27 23:39:30 +01004305void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4306{
4307 if (nb_args != 2) {
4308 return;
4309 }
4310 ringbuf_completion(rs, str);
4311}
4312
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004313void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4314{
4315 size_t len;
4316
4317 if (nb_args != 2) {
4318 return;
4319 }
4320
4321 len = strlen(str);
4322 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004323 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004324}
4325
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004326void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004327{
4328 ObjectPropertyInfoList *list, *start;
4329 size_t len;
4330
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004331 if (nb_args != 2) {
4332 return;
4333 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004334 len = strlen(str);
4335 readline_set_completion_index(rs, len);
4336
4337 start = list = qmp_qom_list("/objects", NULL);
4338 while (list) {
4339 ObjectPropertyInfo *info = list->value;
4340
4341 if (!strncmp(info->type, "child<", 5)
4342 && !strncmp(info->name, str, len)) {
4343 readline_add_completion(rs, info->name);
4344 }
4345 list = list->next;
4346 }
4347 qapi_free_ObjectPropertyInfoList(start);
4348}
4349
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004350void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4351{
4352 int i;
4353 char *sep;
4354 size_t len;
4355
4356 if (nb_args != 2) {
4357 return;
4358 }
4359 sep = strrchr(str, '-');
4360 if (sep) {
4361 str = sep + 1;
4362 }
4363 len = strlen(str);
4364 readline_set_completion_index(rs, len);
4365 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4366 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4367 readline_add_completion(rs, QKeyCode_lookup[i]);
4368 }
4369 }
4370}
4371
Hani Benhabiles40d19392014-05-07 23:41:30 +01004372void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4373{
4374 size_t len;
4375
4376 len = strlen(str);
4377 readline_set_completion_index(rs, len);
4378 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004379 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004380 int count, i;
4381 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004382 NET_CLIENT_OPTIONS_KIND_NONE,
4383 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004384 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004385 const char *name = ncs[i]->name;
4386 if (!strncmp(str, name, len)) {
4387 readline_add_completion(rs, name);
4388 }
4389 }
4390 } else if (nb_args == 3) {
4391 add_completion_option(rs, str, "on");
4392 add_completion_option(rs, str, "off");
4393 }
4394}
4395
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004396void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4397{
4398 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004399 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004400
4401 if (nb_args != 2) {
4402 return;
4403 }
4404
4405 len = strlen(str);
4406 readline_set_completion_index(rs, len);
4407 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004408 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004409 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004410 QemuOpts *opts;
4411 const char *name = ncs[i]->name;
4412 if (strncmp(str, name, len)) {
4413 continue;
4414 }
4415 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4416 if (opts) {
4417 readline_add_completion(rs, name);
4418 }
4419 }
4420}
4421
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004422void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4423{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004424 int i;
4425
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004426 if (nb_args != 2) {
4427 return;
4428 }
4429 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004430 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4431 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4432 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004433}
4434
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004435void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4436 const char *str)
4437{
4438 size_t len;
4439
4440 len = strlen(str);
4441 readline_set_completion_index(rs, len);
4442 if (nb_args == 2) {
4443 int i;
4444 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4445 const char *name = MigrationCapability_lookup[i];
4446 if (!strncmp(str, name, len)) {
4447 readline_add_completion(rs, name);
4448 }
4449 }
4450 } else if (nb_args == 3) {
4451 add_completion_option(rs, str, "on");
4452 add_completion_option(rs, str, "off");
4453 }
4454}
4455
Liang Li50e9a622015-03-23 16:32:29 +08004456void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4457 const char *str)
4458{
4459 size_t len;
4460
4461 len = strlen(str);
4462 readline_set_completion_index(rs, len);
4463 if (nb_args == 2) {
4464 int i;
4465 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4466 const char *name = MigrationParameter_lookup[i];
4467 if (!strncmp(str, name, len)) {
4468 readline_add_completion(rs, name);
4469 }
4470 }
4471 }
4472}
4473
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004474void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4475{
4476 int i;
4477 size_t len;
4478 if (nb_args != 2) {
4479 return;
4480 }
4481 len = strlen(str);
4482 readline_set_completion_index(rs, len);
4483 for (i = 0; host_net_devices[i]; i++) {
4484 if (!strncmp(host_net_devices[i], str, len)) {
4485 readline_add_completion(rs, host_net_devices[i]);
4486 }
4487 }
4488}
4489
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004490void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4491{
Jason Wangeaed4832015-04-23 14:21:38 +08004492 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004493 int count, i, len;
4494
4495 len = strlen(str);
4496 readline_set_completion_index(rs, len);
4497 if (nb_args == 2) {
4498 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004499 NET_CLIENT_OPTIONS_KIND_NONE,
4500 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004501 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004502 int id;
4503 char name[16];
4504
4505 if (net_hub_id_for_client(ncs[i], &id)) {
4506 continue;
4507 }
4508 snprintf(name, sizeof(name), "%d", id);
4509 if (!strncmp(str, name, len)) {
4510 readline_add_completion(rs, name);
4511 }
4512 }
4513 return;
4514 } else if (nb_args == 3) {
4515 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004516 NET_CLIENT_OPTIONS_KIND_NIC,
4517 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004518 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004519 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004520 const char *name;
4521
Jason Wang2c4681f2015-02-02 15:06:38 +08004522 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4523 net_hub_id_for_client(ncs[i], &id)) {
4524 continue;
4525 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004526 name = ncs[i]->name;
4527 if (!strncmp(str, name, len)) {
4528 readline_add_completion(rs, name);
4529 }
4530 }
4531 return;
4532 }
4533}
4534
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004535static void vm_completion(ReadLineState *rs, const char *str)
4536{
4537 size_t len;
4538 BlockDriverState *bs = NULL;
4539
4540 len = strlen(str);
4541 readline_set_completion_index(rs, len);
4542 while ((bs = bdrv_next(bs))) {
4543 SnapshotInfoList *snapshots, *snapshot;
4544
4545 if (!bdrv_can_snapshot(bs)) {
4546 continue;
4547 }
4548 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4549 continue;
4550 }
4551 snapshot = snapshots;
4552 while (snapshot) {
4553 char *completion = snapshot->value->name;
4554 if (!strncmp(str, completion, len)) {
4555 readline_add_completion(rs, completion);
4556 }
4557 completion = snapshot->value->id;
4558 if (!strncmp(str, completion, len)) {
4559 readline_add_completion(rs, completion);
4560 }
4561 snapshot = snapshot->next;
4562 }
4563 qapi_free_SnapshotInfoList(snapshots);
4564 }
4565
4566}
4567
4568void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4569{
4570 if (nb_args == 2) {
4571 vm_completion(rs, str);
4572 }
4573}
4574
4575void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4576{
4577 if (nb_args == 2) {
4578 vm_completion(rs, str);
4579 }
4580}
4581
Wenchao Xiac35b6402013-08-27 20:38:24 +08004582static void monitor_find_completion_by_table(Monitor *mon,
4583 const mon_cmd_t *cmd_table,
4584 char **args,
4585 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004586{
4587 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004588 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004589 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004590 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004591 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004592
bellard81d09122004-07-14 17:21:37 +00004593 if (nb_args <= 1) {
4594 /* command completion */
4595 if (nb_args == 0)
4596 cmdname = "";
4597 else
4598 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004599 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004600 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004601 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004602 }
4603 } else {
4604 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004605 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004606 if (compare_cmd(args[0], cmd->name)) {
4607 break;
4608 }
bellard81d09122004-07-14 17:21:37 +00004609 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004610 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004611 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004612 }
4613
Wenchao Xiad903a772013-08-27 20:38:25 +08004614 if (cmd->sub_table) {
4615 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004616 monitor_find_completion_by_table(mon, cmd->sub_table,
4617 &args[1], nb_args - 1);
4618 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004619 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004620 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004621 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4622 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004623 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004624
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004625 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004626 for(i = 0; i < nb_args - 2; i++) {
4627 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004628 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004629 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004630 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004631 }
4632 }
4633 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004634 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004635 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004636 }
bellard81d09122004-07-14 17:21:37 +00004637 switch(*ptype) {
4638 case 'F':
4639 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004640 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004641 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004642 break;
4643 case 'B':
4644 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004645 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004646 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4647 name = bdrv_get_device_name(bs);
4648 if (str[0] == '\0' ||
4649 !strncmp(name, str, strlen(str))) {
4650 readline_add_completion(mon->rs, name);
4651 }
4652 }
bellard81d09122004-07-14 17:21:37 +00004653 break;
bellard7fe48482004-10-09 18:08:01 +00004654 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004655 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004656 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004657 monitor_find_completion_by_table(mon, cmd_table,
4658 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004659 }
4660 break;
bellard81d09122004-07-14 17:21:37 +00004661 default:
4662 break;
4663 }
4664 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004665}
4666
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004667static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004668 const char *cmdline)
4669{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004670 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004671 char *args[MAX_ARGS];
4672 int nb_args, len;
4673
4674 /* 1. parse the cmdline */
4675 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4676 return;
4677 }
4678#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004679 {
4680 int i;
4681 for (i = 0; i < nb_args; i++) {
4682 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4683 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004684 }
4685#endif
4686
4687 /* if the line ends with a space, it means we want to complete the
4688 next arg */
4689 len = strlen(cmdline);
4690 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4691 if (nb_args >= MAX_ARGS) {
4692 goto cleanup;
4693 }
4694 args[nb_args++] = g_strdup("");
4695 }
4696
4697 /* 2. auto complete according to args */
4698 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004699
4700cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004701 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004702}
4703
aliguori731b0362009-03-05 23:01:42 +00004704static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004705{
aliguori731b0362009-03-05 23:01:42 +00004706 Monitor *mon = opaque;
4707
Jan Kiszkac62313b2009-12-04 14:05:29 +01004708 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004709}
4710
Eric Blake2d5a8342015-04-15 09:19:23 -06004711static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004712{
Eric Blake2d5a8342015-04-15 09:19:23 -06004713 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
4714 if (is_cap && qmp_cmd_mode(mon)) {
4715 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
4716 "Capabilities negotiation is already complete, command "
4717 "'%s' ignored", cmd->name);
4718 return true;
4719 }
4720 if (!is_cap && !qmp_cmd_mode(mon)) {
4721 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
4722 "Expecting capabilities negotiation with "
4723 "'qmp_capabilities' before command '%s'", cmd->name);
4724 return true;
4725 }
4726 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004727}
4728
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004729/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004730 * Argument validation rules:
4731 *
4732 * 1. The argument must exist in cmd_args qdict
4733 * 2. The argument type must be the expected one
4734 *
4735 * Special case: If the argument doesn't exist in cmd_args and
4736 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4737 * checking is skipped for it.
4738 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004739static void check_client_args_type(const QDict *client_args,
4740 const QDict *cmd_args, int flags,
4741 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004742{
4743 const QDictEntry *ent;
4744
4745 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4746 QObject *obj;
4747 QString *arg_type;
4748 const QObject *client_arg = qdict_entry_value(ent);
4749 const char *client_arg_name = qdict_entry_key(ent);
4750
4751 obj = qdict_get(cmd_args, client_arg_name);
4752 if (!obj) {
4753 if (flags & QMP_ACCEPT_UNKNOWNS) {
4754 /* handler accepts unknowns */
4755 continue;
4756 }
4757 /* client arg doesn't exist */
Markus Armbruster326283a2015-03-02 18:39:09 +01004758 error_set(errp, QERR_INVALID_PARAMETER, client_arg_name);
4759 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004760 }
4761
4762 arg_type = qobject_to_qstring(obj);
4763 assert(arg_type != NULL);
4764
4765 /* check if argument's type is correct */
4766 switch (qstring_get_str(arg_type)[0]) {
4767 case 'F':
4768 case 'B':
4769 case 's':
4770 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004771 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4772 client_arg_name, "string");
4773 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004774 }
4775 break;
4776 case 'i':
4777 case 'l':
4778 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004779 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004780 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004781 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4782 client_arg_name, "int");
4783 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004784 }
4785 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004786 case 'T':
4787 if (qobject_type(client_arg) != QTYPE_QINT &&
4788 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004789 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4790 client_arg_name, "number");
4791 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004792 }
4793 break;
4794 case 'b':
4795 case '-':
4796 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004797 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4798 client_arg_name, "bool");
4799 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004800 }
4801 break;
4802 case 'O':
4803 assert(flags & QMP_ACCEPT_UNKNOWNS);
4804 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004805 case 'q':
4806 /* Any QObject can be passed. */
4807 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004808 case '/':
4809 case '.':
4810 /*
4811 * These types are not supported by QMP and thus are not
4812 * handled here. Fall through.
4813 */
4814 default:
4815 abort();
4816 }
4817 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004818}
4819
4820/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004821 * - Check if the client has passed all mandatory args
4822 * - Set special flags for argument validation
4823 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004824static void check_mandatory_args(const QDict *cmd_args,
4825 const QDict *client_args, int *flags,
4826 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004827{
4828 const QDictEntry *ent;
4829
4830 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4831 const char *cmd_arg_name = qdict_entry_key(ent);
4832 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4833 assert(type != NULL);
4834
4835 if (qstring_get_str(type)[0] == 'O') {
4836 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4837 *flags |= QMP_ACCEPT_UNKNOWNS;
4838 } else if (qstring_get_str(type)[0] != '-' &&
4839 qstring_get_str(type)[1] != '?' &&
4840 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004841 error_set(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4842 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004843 }
4844 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004845}
4846
4847static QDict *qdict_from_args_type(const char *args_type)
4848{
4849 int i;
4850 QDict *qdict;
4851 QString *key, *type, *cur_qs;
4852
4853 assert(args_type != NULL);
4854
4855 qdict = qdict_new();
4856
4857 if (args_type == NULL || args_type[0] == '\0') {
4858 /* no args, empty qdict */
4859 goto out;
4860 }
4861
4862 key = qstring_new();
4863 type = qstring_new();
4864
4865 cur_qs = key;
4866
4867 for (i = 0;; i++) {
4868 switch (args_type[i]) {
4869 case ',':
4870 case '\0':
4871 qdict_put(qdict, qstring_get_str(key), type);
4872 QDECREF(key);
4873 if (args_type[i] == '\0') {
4874 goto out;
4875 }
4876 type = qstring_new(); /* qdict has ref */
4877 cur_qs = key = qstring_new();
4878 break;
4879 case ':':
4880 cur_qs = type;
4881 break;
4882 default:
4883 qstring_append_chr(cur_qs, args_type[i]);
4884 break;
4885 }
4886 }
4887
4888out:
4889 return qdict;
4890}
4891
4892/*
4893 * Client argument checking rules:
4894 *
4895 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004896 * 2. Each argument provided by the client must be expected
4897 * 3. Each argument provided by the client must have the type expected
4898 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004899 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004900static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4901 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004902{
Markus Armbruster326283a2015-03-02 18:39:09 +01004903 Error *err = NULL;
4904 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004905 QDict *cmd_args;
4906
4907 cmd_args = qdict_from_args_type(cmd->args_type);
4908
4909 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004910 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004911 if (err) {
4912 goto out;
4913 }
4914
Markus Armbruster326283a2015-03-02 18:39:09 +01004915 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004916
4917out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004918 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004919 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004920}
4921
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004922/*
4923 * Input object checking rules
4924 *
4925 * 1. Input object must be a dict
4926 * 2. The "execute" key must exist
4927 * 3. The "execute" key must be a string
4928 * 4. If the "arguments" key exists, it must be a dict
4929 * 5. If the "id" key exists, it can be anything (ie. json-value)
4930 * 6. Any argument not listed above is considered invalid
4931 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004932static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004933{
4934 const QDictEntry *ent;
4935 int has_exec_key = 0;
4936 QDict *input_dict;
4937
4938 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004939 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004940 return NULL;
4941 }
4942
4943 input_dict = qobject_to_qdict(input_obj);
4944
4945 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4946 const char *arg_name = qdict_entry_key(ent);
4947 const QObject *arg_obj = qdict_entry_value(ent);
4948
4949 if (!strcmp(arg_name, "execute")) {
4950 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004951 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4952 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004953 return NULL;
4954 }
4955 has_exec_key = 1;
4956 } else if (!strcmp(arg_name, "arguments")) {
4957 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004958 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4959 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004960 return NULL;
4961 }
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004962 } else {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004963 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004964 return NULL;
4965 }
4966 }
4967
4968 if (!has_exec_key) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004969 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004970 return NULL;
4971 }
4972
4973 return input_dict;
4974}
4975
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004976static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4977{
Markus Armbruster326283a2015-03-02 18:39:09 +01004978 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004979 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004980 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004981 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004982 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004983 Monitor *mon = cur_mon;
4984
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004985 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004986 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004987
4988 obj = json_parser_parse(tokens, NULL);
4989 if (!obj) {
4990 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004991 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004992 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004993 }
4994
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004995 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004996 if (!input) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004997 qerror_report_err(local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004998 qobject_decref(obj);
4999 goto err_out;
5000 }
5001
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005002 mon->mc->id = qdict_get(input, "id");
5003 qobject_incref(mon->mc->id);
5004
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005005 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005006 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005007 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005008 if (!cmd) {
Markus Armbrustera6c90cb2015-01-13 16:16:35 +01005009 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
5010 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005011 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005012 }
Eric Blake2d5a8342015-04-15 09:19:23 -06005013 if (invalid_qmp_mode(mon, cmd)) {
5014 goto err_out;
5015 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005016
5017 obj = qdict_get(input, "arguments");
5018 if (!obj) {
5019 args = qdict_new();
5020 } else {
5021 args = qobject_to_qdict(obj);
5022 QINCREF(args);
5023 }
5024
Markus Armbruster326283a2015-03-02 18:39:09 +01005025 qmp_check_client_args(cmd, args, &local_err);
5026 if (local_err) {
5027 qerror_report_err(local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005028 goto err_out;
5029 }
5030
Markus Armbruster84add862015-03-05 16:45:15 +01005031 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5032 /* Command failed... */
5033 if (!monitor_has_error(mon)) {
5034 /* ... without setting an error, so make one up */
5035 qerror_report(QERR_UNDEFINED_ERROR);
5036 }
5037 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005038
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005039err_out:
Markus Armbruster84add862015-03-05 16:45:15 +01005040 monitor_protocol_emitter(mon, data);
5041 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005042 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005043 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005044}
5045
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005046/**
5047 * monitor_control_read(): Read and handle QMP input
5048 */
5049static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5050{
5051 Monitor *old_mon = cur_mon;
5052
5053 cur_mon = opaque;
5054
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005055 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005056
5057 cur_mon = old_mon;
5058}
5059
aliguori731b0362009-03-05 23:01:42 +00005060static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005061{
aliguori731b0362009-03-05 23:01:42 +00005062 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005063 int i;
aliguori376253e2009-03-05 23:01:23 +00005064
aliguori731b0362009-03-05 23:01:42 +00005065 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005066
aliguoricde76ee2009-03-05 23:01:51 +00005067 if (cur_mon->rs) {
5068 for (i = 0; i < size; i++)
5069 readline_handle_byte(cur_mon->rs, buf[i]);
5070 } else {
5071 if (size == 0 || buf[size - 1] != 0)
5072 monitor_printf(cur_mon, "corrupted command\n");
5073 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005074 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005075 }
aliguori731b0362009-03-05 23:01:42 +00005076
5077 cur_mon = old_mon;
5078}
aliguorid8f44602008-10-06 13:52:44 +00005079
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005080static void monitor_command_cb(void *opaque, const char *cmdline,
5081 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005082{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005083 Monitor *mon = opaque;
5084
aliguori731b0362009-03-05 23:01:42 +00005085 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005086 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005087 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005088}
5089
aliguoricde76ee2009-03-05 23:01:51 +00005090int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005091{
aliguoricde76ee2009-03-05 23:01:51 +00005092 if (!mon->rs)
5093 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005094 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005095 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005096}
5097
aliguori376253e2009-03-05 23:01:23 +00005098void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005099{
aliguoricde76ee2009-03-05 23:01:51 +00005100 if (!mon->rs)
5101 return;
aliguori731b0362009-03-05 23:01:42 +00005102 if (--mon->suspend_cnt == 0)
5103 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005104}
5105
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005106static QObject *get_qmp_greeting(void)
5107{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005108 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005109
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005110 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005111 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5112}
5113
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005114/**
5115 * monitor_control_event(): Print QMP gretting
5116 */
5117static void monitor_control_event(void *opaque, int event)
5118{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005119 QObject *data;
5120 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005121
Luiz Capitulino47116d12010-02-08 17:01:30 -02005122 switch (event) {
5123 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005124 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005125 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005126 monitor_json_emitter(mon, data);
5127 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005128 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005129 break;
5130 case CHR_EVENT_CLOSED:
5131 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05005132 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005133 mon_refcount--;
5134 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005135 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005136 }
5137}
5138
aliguori731b0362009-03-05 23:01:42 +00005139static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005140{
aliguori376253e2009-03-05 23:01:23 +00005141 Monitor *mon = opaque;
5142
aliguori2724b182009-03-05 23:01:47 +00005143 switch (event) {
5144 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005145 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005146 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005147 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005148 if (mon->reset_seen) {
5149 readline_restart(mon->rs);
5150 monitor_resume(mon);
5151 monitor_flush(mon);
5152 } else {
5153 mon->suspend_cnt = 0;
5154 }
aliguori2724b182009-03-05 23:01:47 +00005155 break;
ths86e94de2007-01-05 22:01:59 +00005156
aliguori2724b182009-03-05 23:01:47 +00005157 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005158 if (mon->reset_seen) {
5159 if (mon->suspend_cnt == 0) {
5160 monitor_printf(mon, "\n");
5161 }
5162 monitor_flush(mon);
5163 monitor_suspend(mon);
5164 } else {
5165 mon->suspend_cnt++;
5166 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005167 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005168 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005169 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005170 break;
5171
Amit Shahb6b8df52009-10-07 18:31:16 +05305172 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005173 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5174 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005175 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005176 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005177 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005178 }
5179 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005180 mon_refcount++;
5181 break;
5182
5183 case CHR_EVENT_CLOSED:
5184 mon_refcount--;
5185 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005186 break;
5187 }
ths86e94de2007-01-05 22:01:59 +00005188}
5189
Wayne Xia816f8922011-10-12 11:32:41 +08005190static int
5191compare_mon_cmd(const void *a, const void *b)
5192{
5193 return strcmp(((const mon_cmd_t *)a)->name,
5194 ((const mon_cmd_t *)b)->name);
5195}
5196
5197static void sortcmdlist(void)
5198{
5199 int array_num;
5200 int elem_size = sizeof(mon_cmd_t);
5201
5202 array_num = sizeof(mon_cmds)/elem_size-1;
5203 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5204
5205 array_num = sizeof(info_cmds)/elem_size-1;
5206 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5207}
5208
aliguori76655d62009-03-06 20:27:37 +00005209
5210/*
5211 * Local variables:
5212 * c-indent-level: 4
5213 * c-basic-offset: 4
5214 * tab-width: 8
5215 * End:
5216 */
5217
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005218/* These functions just adapt the readline interface in a typesafe way. We
5219 * could cast function pointers but that discards compiler checks.
5220 */
Stefan Weild5d15072014-01-25 18:18:23 +01005221static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5222 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005223{
5224 va_list ap;
5225 va_start(ap, fmt);
5226 monitor_vprintf(opaque, fmt, ap);
5227 va_end(ap);
5228}
5229
5230static void monitor_readline_flush(void *opaque)
5231{
5232 monitor_flush(opaque);
5233}
5234
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005235static void __attribute__((constructor)) monitor_lock_init(void)
5236{
5237 qemu_mutex_init(&monitor_lock);
5238}
5239
aliguori731b0362009-03-05 23:01:42 +00005240void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005241{
aliguori731b0362009-03-05 23:01:42 +00005242 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005243 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005244
5245 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005246 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005247 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005248 is_first_init = 0;
5249 }
aliguori87127162009-03-05 23:01:29 +00005250
Wenchao Xiab01fe892013-08-27 20:38:19 +08005251 mon = g_malloc(sizeof(*mon));
5252 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005253
aliguori87127162009-03-05 23:01:29 +00005254 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005255 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005256 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005257 mon->rs = readline_init(monitor_readline_printf,
5258 monitor_readline_flush,
5259 mon,
5260 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005261 monitor_read_command(mon, 0);
5262 }
aliguori87127162009-03-05 23:01:29 +00005263
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005264 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005265 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005266 /* Control mode requires special handlers */
5267 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5268 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005269 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05005270
5271 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005272 } else {
5273 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5274 monitor_event, mon);
5275 }
aliguori87127162009-03-05 23:01:29 +00005276
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005277 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005278 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005279 qemu_mutex_unlock(&monitor_lock);
5280
Markus Armbruster8631b602010-02-18 11:41:55 +01005281 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5282 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005283}
5284
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005285static void bdrv_password_cb(void *opaque, const char *password,
5286 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005287{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005288 Monitor *mon = opaque;
5289 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005290 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005291 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005292
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005293 bdrv_add_key(bs, password, &local_err);
5294 if (local_err) {
5295 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5296 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005297 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005298 }
aliguori731b0362009-03-05 23:01:42 +00005299 if (mon->password_completion_cb)
5300 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005301
aliguori731b0362009-03-05 23:01:42 +00005302 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005303}
aliguoric0f4ce72009-03-05 23:01:01 +00005304
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005305int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005306 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005307 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005308{
aliguoricde76ee2009-03-05 23:01:51 +00005309 int err;
5310
aliguori376253e2009-03-05 23:01:23 +00005311 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5312 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005313
aliguori731b0362009-03-05 23:01:42 +00005314 mon->password_completion_cb = completion_cb;
5315 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005316
aliguoricde76ee2009-03-05 23:01:51 +00005317 err = monitor_read_password(mon, bdrv_password_cb, bs);
5318
5319 if (err && completion_cb)
5320 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005321
5322 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005323}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005324
5325int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005326 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005327 void *opaque)
5328{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005329 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005330 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005331
Fam Zheng55606252015-03-02 19:36:46 +08005332 blk = blk_by_name(device);
5333 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005334 monitor_printf(mon, "Device not found %s\n", device);
5335 return -1;
5336 }
5337
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005338 bdrv_add_key(blk_bs(blk), NULL, &err);
5339 if (err) {
5340 error_free(err);
5341 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5342 }
5343
5344 if (completion_cb) {
5345 completion_cb(opaque, 0);
5346 }
5347 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005348}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005349
5350QemuOptsList qemu_mon_opts = {
5351 .name = "mon",
5352 .implied_opt_name = "chardev",
5353 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5354 .desc = {
5355 {
5356 .name = "mode",
5357 .type = QEMU_OPT_STRING,
5358 },{
5359 .name = "chardev",
5360 .type = QEMU_OPT_STRING,
5361 },{
5362 .name = "default",
5363 .type = QEMU_OPT_BOOL,
5364 },{
5365 .name = "pretty",
5366 .type = QEMU_OPT_BOOL,
5367 },
5368 { /* end of list */ }
5369 },
5370};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005371
5372#ifndef TARGET_I386
5373void qmp_rtc_reset_reinjection(Error **errp)
5374{
5375 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5376}
5377#endif