blob: 38ff972755032b7c1d2f7266ace5908237d61c82 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080076#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000077
Markus Armbruster1ce6be22015-02-06 14:18:24 +010078/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020079#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010080#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020081#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083
bellard9dc39cb2004-03-14 21:38:27 +000084//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000085//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000086
bellard9307c4c2004-04-04 12:57:25 +000087/*
88 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000089 *
bellard9307c4c2004-04-04 12:57:25 +000090 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000091 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000092 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080093 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010094 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000099 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200103 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000111 * '/' optional gdb-like print format (like "/10x")
112 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100115 * 'b' boolean
116 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000118 *
119 */
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000123 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *params;
125 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300126 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300127 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300130 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800131 /* @sub_table is a list of 2nd level of commands. If it do not exist,
132 * mhandler should be used. If it exist, sub_table[?].mhandler should be
133 * used, and mhandler of 1st level plays the role of help function.
134 */
135 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100136 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500137} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000138
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100139/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500140typedef struct mon_fd_t mon_fd_t;
141struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142 char *name;
143 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500144 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145};
146
Corey Bryantba1c0482012-08-14 16:43:43 -0400147/* file descriptor associated with a file descriptor set */
148typedef struct MonFdsetFd MonFdsetFd;
149struct MonFdsetFd {
150 int fd;
151 bool removed;
152 char *opaque;
153 QLIST_ENTRY(MonFdsetFd) next;
154};
155
156/* file descriptor set containing fds passed via SCM_RIGHTS */
157typedef struct MonFdset MonFdset;
158struct MonFdset {
159 int64_t id;
160 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400161 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400162 QLIST_ENTRY(MonFdset) next;
163};
164
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165typedef struct MonitorControl {
166 QObject *id;
167 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200168 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169} MonitorControl;
170
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100171/*
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
174 * instance.
175 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200176typedef struct MonitorQAPIEventState {
177 QAPIEvent event; /* Event being tracked */
178 int64_t rate; /* Minimum time (in ns) between two events */
179 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100180 QEMUTimer *timer; /* Timer for handling delayed events */
181 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200182} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100183
aliguori87127162009-03-05 23:01:29 +0000184struct Monitor {
185 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200186 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000187 int flags;
188 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400189 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200190
191 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400192 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200193 guint out_watch;
194
195 /* Read under either BQL or out_lock, written with BQL+out_lock. */
196 int mux_out;
197
aliguori731b0362009-03-05 23:01:42 +0000198 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200199 MonitorControl *mc;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200200 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200201 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000202 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800203 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200204 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500205 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000206 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000207};
208
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300209/* QMP checker flags */
210#define QMP_ACCEPT_UNKNOWNS 1
211
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200212/* Protects mon_list, monitor_event_state. */
213static QemuMutex monitor_lock;
214
Blue Swirl72cf2d42009-09-12 07:36:22 +0000215static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400216static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400217static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000218
Wayne Xia816f8922011-10-12 11:32:41 +0800219static mon_cmd_t mon_cmds[];
220static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000221
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300222static const mon_cmd_t qmp_cmds[];
223
Markus Armbruster8631b602010-02-18 11:41:55 +0100224Monitor *cur_mon;
225Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000226
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100227static void monitor_command_cb(void *opaque, const char *cmdline,
228 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000229
Luiz Capitulino09069b12010-02-04 18:10:06 -0200230static inline int qmp_cmd_mode(const Monitor *mon)
231{
232 return (mon->mc ? mon->mc->command_mode : 0);
233}
234
Luiz Capitulino418173c2009-11-26 22:58:51 -0200235/* Return true if in control mode, false otherwise */
236static inline int monitor_ctrl_mode(const Monitor *mon)
237{
238 return (mon->flags & MONITOR_USE_CONTROL);
239}
240
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100241/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
242int monitor_cur_is_qmp(void)
243{
244 return cur_mon && monitor_ctrl_mode(cur_mon);
245}
246
Anthony Liguori7060b472011-09-02 12:34:50 -0500247void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000248{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200249 if (!mon->rs)
250 return;
251
aliguori731b0362009-03-05 23:01:42 +0000252 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
253 if (show_prompt)
254 readline_show_prompt(mon->rs);
255}
bellard6a00d602005-11-21 23:25:50 +0000256
Anthony Liguori7060b472011-09-02 12:34:50 -0500257int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
258 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000259{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100260 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000261 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
262 /* prompt is printed on return from the command handler */
263 return 0;
264 } else {
265 monitor_printf(mon, "terminal does not support password prompting\n");
266 return -ENOTTY;
267 }
aliguoribb5fc202009-03-05 23:01:15 +0000268}
269
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200270static void monitor_flush_locked(Monitor *mon);
271
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100272static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
273 void *opaque)
274{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200275 Monitor *mon = opaque;
276
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200277 qemu_mutex_lock(&mon->out_lock);
278 mon->out_watch = 0;
279 monitor_flush_locked(mon);
280 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100281 return FALSE;
282}
283
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200284/* Called with mon->out_lock held. */
285static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000286{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100287 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400288 size_t len;
289 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400291 if (mon->skip_flush) {
292 return;
293 }
294
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400295 buf = qstring_get_str(mon->outbuf);
296 len = qstring_get_length(mon->outbuf);
297
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200298 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200300 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
301 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 QDECREF(mon->outbuf);
303 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100304 return;
305 }
306 if (rc > 0) {
307 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400308 QString *tmp = qstring_from_str(buf + rc);
309 QDECREF(mon->outbuf);
310 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100311 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200312 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200313 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200314 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200315 }
bellard7e2515e2004-08-01 21:52:19 +0000316 }
317}
318
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200319void monitor_flush(Monitor *mon)
320{
321 qemu_mutex_lock(&mon->out_lock);
322 monitor_flush_locked(mon);
323 qemu_mutex_unlock(&mon->out_lock);
324}
325
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400326/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000327static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000328{
ths60fe76f2007-12-16 03:02:09 +0000329 char c;
aliguori731b0362009-03-05 23:01:42 +0000330
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200331 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000332 for(;;) {
333 c = *str++;
334 if (c == '\0')
335 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400336 if (c == '\n') {
337 qstring_append_chr(mon->outbuf, '\r');
338 }
339 qstring_append_chr(mon->outbuf, c);
340 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200341 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 }
bellard7e2515e2004-08-01 21:52:19 +0000343 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200344 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000345}
346
aliguori376253e2009-03-05 23:01:23 +0000347void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000348{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400349 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200350
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200351 if (!mon)
352 return;
353
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200355 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200356 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400358 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400360 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000361}
362
aliguori376253e2009-03-05 23:01:23 +0000363void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000364{
365 va_list ap;
366 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000368 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000369}
370
Stefan Weil8b7968f2010-09-23 21:28:05 +0200371static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
372 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000373{
374 va_list ap;
375 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000377 va_end(ap);
378 return 0;
379}
380
Luiz Capitulino13c74252009-10-07 13:41:55 -0300381static void monitor_user_noop(Monitor *mon, const QObject *data) { }
382
Luiz Capitulino9e807212010-09-16 10:58:59 -0300383static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300384{
385 return cmd->user_print != NULL;
386}
387
Luiz Capitulino8204a912009-11-18 23:05:31 -0200388static inline int monitor_has_error(const Monitor *mon)
389{
390 return mon->error != NULL;
391}
392
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200393static void monitor_json_emitter(Monitor *mon, const QObject *data)
394{
395 QString *json;
396
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200397 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
398 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200399 assert(json != NULL);
400
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200401 qstring_append_chr(json, '\n');
402 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200403
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200404 QDECREF(json);
405}
406
Luiz Capitulinode253f12012-07-27 16:18:16 -0300407static QDict *build_qmp_error_dict(const QError *err)
408{
409 QObject *obj;
410
411 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
412 ErrorClass_lookup[err->err_class],
413 qerror_human(err));
414
415 return qobject_to_qdict(obj);
416}
417
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418static void monitor_protocol_emitter(Monitor *mon, QObject *data)
419{
420 QDict *qmp;
421
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100422 trace_monitor_protocol_emitter(mon);
423
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 if (!monitor_has_error(mon)) {
425 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300426 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 if (data) {
428 qobject_incref(data);
429 qdict_put_obj(qmp, "return", data);
430 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200431 /* return an empty QDict by default */
432 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200433 }
434 } else {
435 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300436 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 QDECREF(mon->error);
438 mon->error = NULL;
439 }
440
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200441 if (mon->mc->id) {
442 qdict_put_obj(qmp, "id", mon->mc->id);
443 mon->mc->id = NULL;
444 }
445
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200446 monitor_json_emitter(mon, QOBJECT(qmp));
447 QDECREF(qmp);
448}
449
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200450
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200451static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100452
453/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200454 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200455 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100456 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200457static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458{
459 Monitor *mon;
460
461 trace_monitor_protocol_event_emit(event, data);
462 QLIST_FOREACH(mon, &mon_list, entry) {
463 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
464 monitor_json_emitter(mon, data);
465 }
466 }
467}
468
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469/*
470 * Queue a new event for emission to Monitor instances,
471 * applying any rate limiting if required.
472 */
473static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200474monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100475{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200476 MonitorQAPIEventState *evstate;
477 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100478 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200480 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 trace_monitor_protocol_event_queue(event,
482 data,
483 evstate->rate,
484 evstate->last,
485 now);
486
487 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200488 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100489 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200490 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 evstate->last = now;
492 } else {
493 int64_t delta = now - evstate->last;
494 if (evstate->data ||
495 delta < evstate->rate) {
496 /* If there's an existing event pending, replace
497 * it with the new event, otherwise schedule a
498 * timer for delayed emission
499 */
500 if (evstate->data) {
501 qobject_decref(evstate->data);
502 } else {
503 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100504 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100505 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200506 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507 qobject_incref(evstate->data);
508 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200509 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510 evstate->last = now;
511 }
512 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200513 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514}
515
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516/*
517 * The callback invoked by QemuTimer when a delayed
518 * event is ready to be emitted
519 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200520static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100521{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200522 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100523 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100524
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525 trace_monitor_protocol_event_handler(evstate->event,
526 evstate->data,
527 evstate->last,
528 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200529 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100530 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200531 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100532 qobject_decref(evstate->data);
533 evstate->data = NULL;
534 }
535 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200536 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537}
538
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539/*
540 * @event: the event ID to be limited
541 * @rate: the rate limit in milliseconds
542 *
543 * Sets a rate limit on a particular event, so no
544 * more than 1 event will be emitted within @rate
545 * milliseconds
546 */
547static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200548monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200550 MonitorQAPIEventState *evstate;
551 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100552
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200553 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554
555 trace_monitor_protocol_event_throttle(event, rate);
556 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700557 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100559 evstate->last = 0;
560 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200561 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
562 SCALE_MS,
563 monitor_qapi_event_handler,
564 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100565}
566
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200567static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200569 /* Limit guest-triggerable events to 1 per second */
570 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200571 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200572 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200573 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
574 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200575 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100576
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200577 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200578}
579
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200580static int do_qmp_capabilities(Monitor *mon, const QDict *params,
581 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200582{
583 /* Will setup QMP capabilities in the future */
584 if (monitor_ctrl_mode(mon)) {
585 mon->mc->command_mode = 1;
586 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200587
588 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200589}
590
Luiz Capitulino0268d972010-10-22 10:08:02 -0200591static void handle_user_command(Monitor *mon, const char *cmdline);
592
Wenchao Xiab01fe892013-08-27 20:38:19 +0800593static void monitor_data_init(Monitor *mon)
594{
595 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200596 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800597 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800598 /* Use *mon_cmds by default. */
599 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800600}
601
602static void monitor_data_destroy(Monitor *mon)
603{
604 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200605 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800606}
607
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200608char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
609 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200611 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200612 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200613
Wenchao Xiab01fe892013-08-27 20:38:19 +0800614 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400615 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616
617 old_mon = cur_mon;
618 cur_mon = &hmp;
619
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 if (has_cpu_index) {
621 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 if (ret < 0) {
623 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200624 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
625 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626 goto out;
627 }
628 }
629
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200630 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200631 cur_mon = old_mon;
632
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200633 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400634 if (qstring_get_length(hmp.outbuf) > 0) {
635 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200636 } else {
637 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200638 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200639 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200640
641out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800642 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200643 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200644}
645
bellard9dc39cb2004-03-14 21:38:27 +0000646static int compare_cmd(const char *name, const char *list)
647{
648 const char *p, *pstart;
649 int len;
650 len = strlen(name);
651 p = list;
652 for(;;) {
653 pstart = p;
654 p = strchr(p, '|');
655 if (!p)
656 p = pstart + strlen(pstart);
657 if ((p - pstart) == len && !memcmp(pstart, name, len))
658 return 1;
659 if (*p == '\0')
660 break;
661 p++;
662 }
663 return 0;
664}
665
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800666static int get_str(char *buf, int buf_size, const char **pp)
667{
668 const char *p;
669 char *q;
670 int c;
671
672 q = buf;
673 p = *pp;
674 while (qemu_isspace(*p)) {
675 p++;
676 }
677 if (*p == '\0') {
678 fail:
679 *q = '\0';
680 *pp = p;
681 return -1;
682 }
683 if (*p == '\"') {
684 p++;
685 while (*p != '\0' && *p != '\"') {
686 if (*p == '\\') {
687 p++;
688 c = *p++;
689 switch (c) {
690 case 'n':
691 c = '\n';
692 break;
693 case 'r':
694 c = '\r';
695 break;
696 case '\\':
697 case '\'':
698 case '\"':
699 break;
700 default:
701 qemu_printf("unsupported escape code: '\\%c'\n", c);
702 goto fail;
703 }
704 if ((q - buf) < buf_size - 1) {
705 *q++ = c;
706 }
707 } else {
708 if ((q - buf) < buf_size - 1) {
709 *q++ = *p;
710 }
711 p++;
712 }
713 }
714 if (*p != '\"') {
715 qemu_printf("unterminated string\n");
716 goto fail;
717 }
718 p++;
719 } else {
720 while (*p != '\0' && !qemu_isspace(*p)) {
721 if ((q - buf) < buf_size - 1) {
722 *q++ = *p;
723 }
724 p++;
725 }
726 }
727 *q = '\0';
728 *pp = p;
729 return 0;
730}
731
732#define MAX_ARGS 16
733
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800734static void free_cmdline_args(char **args, int nb_args)
735{
736 int i;
737
738 assert(nb_args <= MAX_ARGS);
739
740 for (i = 0; i < nb_args; i++) {
741 g_free(args[i]);
742 }
743
744}
745
746/*
747 * Parse the command line to get valid args.
748 * @cmdline: command line to be parsed.
749 * @pnb_args: location to store the number of args, must NOT be NULL.
750 * @args: location to store the args, which should be freed by caller, must
751 * NOT be NULL.
752 *
753 * Returns 0 on success, negative on failure.
754 *
755 * NOTE: this parser is an approximate form of the real command parser. Number
756 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
757 * return with failure.
758 */
759static int parse_cmdline(const char *cmdline,
760 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761{
762 const char *p;
763 int nb_args, ret;
764 char buf[1024];
765
766 p = cmdline;
767 nb_args = 0;
768 for (;;) {
769 while (qemu_isspace(*p)) {
770 p++;
771 }
772 if (*p == '\0') {
773 break;
774 }
775 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800776 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800777 }
778 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800779 if (ret < 0) {
780 goto fail;
781 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800782 args[nb_args] = g_strdup(buf);
783 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800784 }
785 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800786 return 0;
787
788 fail:
789 free_cmdline_args(args, nb_args);
790 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800791}
792
Wenchao Xia66855492013-08-27 20:38:23 +0800793static void help_cmd_dump_one(Monitor *mon,
794 const mon_cmd_t *cmd,
795 char **prefix_args,
796 int prefix_args_nb)
797{
798 int i;
799
800 for (i = 0; i < prefix_args_nb; i++) {
801 monitor_printf(mon, "%s ", prefix_args[i]);
802 }
803 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
804}
805
806/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500807static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800808 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000809{
Anthony Liguoric227f092009-10-01 16:12:16 -0500810 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000811
Wenchao Xia66855492013-08-27 20:38:23 +0800812 /* No valid arg need to compare with, dump all in *cmds */
813 if (arg_index >= nb_args) {
814 for (cmd = cmds; cmd->name != NULL; cmd++) {
815 help_cmd_dump_one(mon, cmd, args, arg_index);
816 }
817 return;
818 }
819
820 /* Find one entry to dump */
821 for (cmd = cmds; cmd->name != NULL; cmd++) {
822 if (compare_cmd(args[arg_index], cmd->name)) {
823 if (cmd->sub_table) {
824 /* continue with next arg */
825 help_cmd_dump(mon, cmd->sub_table,
826 args, nb_args, arg_index + 1);
827 } else {
828 help_cmd_dump_one(mon, cmd, args, arg_index);
829 }
830 break;
831 }
bellard9dc39cb2004-03-14 21:38:27 +0000832 }
833}
834
aliguori376253e2009-03-05 23:01:23 +0000835static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000836{
Wenchao Xia66855492013-08-27 20:38:23 +0800837 char *args[MAX_ARGS];
838 int nb_args = 0;
839
840 /* 1. parse user input */
841 if (name) {
842 /* special case for log, directly dump and return */
843 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000844 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000845 monitor_printf(mon, "Log items (comma separated):\n");
846 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000847 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000849 }
Wenchao Xia66855492013-08-27 20:38:23 +0800850 return;
851 }
852
853 if (parse_cmdline(name, &nb_args, args) < 0) {
854 return;
bellardf193c792004-03-21 17:06:25 +0000855 }
bellard9dc39cb2004-03-14 21:38:27 +0000856 }
Wenchao Xia66855492013-08-27 20:38:23 +0800857
858 /* 2. dump the contents according to parsed args */
859 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
860
861 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000862}
863
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300864static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300865{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300866 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300867}
868
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100869static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530870{
871 const char *tp_name = qdict_get_str(qdict, "name");
872 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200873 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000874
Lluís Vilanova14101d02014-08-25 13:20:03 +0200875 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
876 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100877 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000878 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530879}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100880
Michael Rothc45a8162011-10-02 08:44:37 -0500881#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100882static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100883{
884 const char *op = qdict_get_try_str(qdict, "op");
885 const char *arg = qdict_get_try_str(qdict, "arg");
886
887 if (!op) {
888 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
889 } else if (!strcmp(op, "on")) {
890 st_set_trace_file_enabled(true);
891 } else if (!strcmp(op, "off")) {
892 st_set_trace_file_enabled(false);
893 } else if (!strcmp(op, "flush")) {
894 st_flush_trace_buffer();
895 } else if (!strcmp(op, "set")) {
896 if (arg) {
897 st_set_trace_file(arg);
898 }
899 } else {
900 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
901 help_cmd(mon, "trace-file");
902 }
903}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530904#endif
905
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100906static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000907{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300908 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000909}
910
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300911CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000912{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300913 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200914 const mon_cmd_t *cmd;
915
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300916 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200917 info = g_malloc0(sizeof(*info));
918 info->value = g_malloc0(sizeof(*info->value));
919 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200920
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300921 info->next = cmd_list;
922 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200923 }
924
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300925 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000926}
927
Daniel P. Berrange48608532012-05-21 17:59:51 +0100928EventInfoList *qmp_query_events(Error **errp)
929{
930 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200931 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100932
Wenchao Xia75175172014-06-18 08:43:54 +0200933 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
934 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100935 assert(event_name != NULL);
936 info = g_malloc0(sizeof(*info));
937 info->value = g_malloc0(sizeof(*info->value));
938 info->value->name = g_strdup(event_name);
939
940 info->next = ev_list;
941 ev_list = info;
942 }
943
944 return ev_list;
945}
946
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300947/* set the current CPU defined by the user */
948int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000949{
Andreas Färber55e5c282012-12-17 06:18:02 +0100950 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000951
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100952 cpu = qemu_get_cpu(cpu_index);
953 if (cpu == NULL) {
954 return -1;
bellard6a00d602005-11-21 23:25:50 +0000955 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200956 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100957 return 0;
bellard6a00d602005-11-21 23:25:50 +0000958}
959
Andreas Färber9349b4f2012-03-14 01:38:32 +0100960static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000961{
aliguori731b0362009-03-05 23:01:42 +0000962 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300963 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000964 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300965 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200966 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000967}
968
Luiz Capitulino99b77962011-10-24 10:53:44 -0200969int monitor_get_cpu_index(void)
970{
Andreas Färber55e5c282012-12-17 06:18:02 +0100971 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
972 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000976{
Andreas Färber878096e2013-05-27 01:33:50 +0200977 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100978 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000979 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200980 cpu = ENV_GET_CPU(env);
981 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000982}
983
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100984static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000985{
aliguori376253e2009-03-05 23:01:23 +0000986 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200987 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000988}
989
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100990static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300991{
992 dump_opcount_info((FILE *)mon, monitor_fprintf);
993}
994
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100995static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000996{
997 int i;
bellard7e2515e2004-08-01 21:52:19 +0000998 const char *str;
ths3b46e622007-09-17 08:09:54 +0000999
aliguoricde76ee2009-03-05 23:01:51 +00001000 if (!mon->rs)
1001 return;
bellard7e2515e2004-08-01 21:52:19 +00001002 i = 0;
1003 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001004 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001005 if (!str)
1006 break;
aliguori376253e2009-03-05 23:01:23 +00001007 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001008 i++;
bellardaa455482004-04-04 13:07:25 +00001009 }
1010}
1011
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001012static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001013{
Andreas Färber878096e2013-05-27 01:33:50 +02001014 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001015 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001016
1017 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001018 cpu = ENV_GET_CPU(env);
1019 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001020}
j_mayer76a66252007-03-07 08:32:30 +00001021
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001022static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301023{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001024 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1025 TraceEventInfoList *elem;
1026
1027 for (elem = events; elem != NULL; elem = elem->next) {
1028 monitor_printf(mon, "%s : state %u\n",
1029 elem->value->name,
1030 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1031 }
1032 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301033}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301034
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001035void qmp_client_migrate_info(const char *protocol, const char *hostname,
1036 bool has_port, int64_t port,
1037 bool has_tls_port, int64_t tls_port,
1038 bool has_cert_subject, const char *cert_subject,
1039 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001040{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001041 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001042 if (!qemu_using_spice(errp)) {
1043 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001044 }
1045
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001046 if (!has_port && !has_tls_port) {
1047 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1048 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001049 }
1050
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001051 if (qemu_spice_migrate_info(hostname,
1052 has_port ? port : -1,
1053 has_tls_port ? tls_port : -1,
1054 cert_subject)) {
1055 error_set(errp, QERR_UNDEFINED_ERROR);
1056 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001057 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001058 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001059 }
1060
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001061 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001062}
1063
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001064static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001065{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001066 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001067}
1068
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001069static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001070{
1071 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001072 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001073
bellard9307c4c2004-04-04 12:57:25 +00001074 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001075 mask = 0;
1076 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001077 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001078 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001079 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001080 return;
1081 }
1082 }
Peter Maydell24537a02013-02-11 16:41:23 +00001083 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001084}
1085
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001086static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001087{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001088 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001089 if (!option || !strcmp(option, "on")) {
1090 singlestep = 1;
1091 } else if (!strcmp(option, "off")) {
1092 singlestep = 0;
1093 } else {
1094 monitor_printf(mon, "unexpected option %s\n", option);
1095 }
1096}
1097
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001098static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001099{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001100 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001101 if (!device)
1102 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1103 if (gdbserver_start(device) < 0) {
1104 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1105 device);
1106 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001107 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001108 } else {
aliguori59030a82009-04-05 18:43:41 +00001109 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1110 device);
bellard8a7ddc32004-03-31 19:00:16 +00001111 }
1112}
1113
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001114static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001115{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001116 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001117 if (select_watchdog_action(action) == -1) {
1118 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1119 }
1120}
1121
aliguori376253e2009-03-05 23:01:23 +00001122static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001123{
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001125 switch(c) {
1126 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001127 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001128 break;
1129 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001130 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001131 break;
1132 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001133 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001134 break;
1135 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001136 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001137 break;
1138 default:
1139 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001141 } else {
aliguori376253e2009-03-05 23:01:23 +00001142 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001143 }
1144 break;
1145 }
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001147}
1148
aliguori376253e2009-03-05 23:01:23 +00001149static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001150 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001151{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001152 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001153 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001154 uint8_t buf[16];
1155 uint64_t v;
1156
1157 if (format == 'i') {
1158 int flags;
1159 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001160 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001161#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001162 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001163 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001164 } else if (wsize == 4) {
1165 flags = 0;
1166 } else {
bellard6a15fd12006-04-12 21:07:07 +00001167 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001168 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001169 if (env) {
1170#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001171 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001172 (env->segs[R_CS].flags & DESC_L_MASK))
1173 flags = 2;
1174 else
1175#endif
1176 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1177 flags = 1;
1178 }
bellard4c27ba22004-04-25 18:05:08 +00001179 }
1180#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001181#ifdef TARGET_PPC
1182 flags = msr_le << 16;
1183 flags |= env->bfd_mach;
1184#endif
aliguori376253e2009-03-05 23:01:23 +00001185 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001186 return;
1187 }
1188
1189 len = wsize * count;
1190 if (wsize == 1)
1191 line_size = 8;
1192 else
1193 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001194 max_digits = 0;
1195
1196 switch(format) {
1197 case 'o':
1198 max_digits = (wsize * 8 + 2) / 3;
1199 break;
1200 default:
1201 case 'x':
1202 max_digits = (wsize * 8) / 4;
1203 break;
1204 case 'u':
1205 case 'd':
1206 max_digits = (wsize * 8 * 10 + 32) / 33;
1207 break;
1208 case 'c':
1209 wsize = 1;
1210 break;
1211 }
1212
1213 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001214 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001215 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001216 else
aliguori376253e2009-03-05 23:01:23 +00001217 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001218 l = len;
1219 if (l > line_size)
1220 l = line_size;
1221 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001222 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001223 } else {
bellard6a00d602005-11-21 23:25:50 +00001224 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001225 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001227 break;
1228 }
bellard9307c4c2004-04-04 12:57:25 +00001229 }
ths5fafdf22007-09-16 21:08:06 +00001230 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001231 while (i < l) {
1232 switch(wsize) {
1233 default:
1234 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001235 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001236 break;
1237 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001238 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001239 break;
1240 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001241 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001242 break;
1243 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001244 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001245 break;
1246 }
aliguori376253e2009-03-05 23:01:23 +00001247 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001248 switch(format) {
1249 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001250 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001251 break;
1252 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001254 break;
1255 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001257 break;
1258 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 }
1265 i += wsize;
1266 }
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001268 addr += l;
1269 len -= l;
1270 }
1271}
1272
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001273static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001274{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001275 int count = qdict_get_int(qdict, "count");
1276 int format = qdict_get_int(qdict, "format");
1277 int size = qdict_get_int(qdict, "size");
1278 target_long addr = qdict_get_int(qdict, "addr");
1279
aliguori376253e2009-03-05 23:01:23 +00001280 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001281}
1282
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001283static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001284{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001285 int count = qdict_get_int(qdict, "count");
1286 int format = qdict_get_int(qdict, "format");
1287 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001288 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001289
aliguori376253e2009-03-05 23:01:23 +00001290 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001291}
1292
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001293static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001294{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001295 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001296 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001297
bellard9307c4c2004-04-04 12:57:25 +00001298 switch(format) {
1299 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001300 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001301 break;
1302 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001303 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001304 break;
1305 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001306 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001307 break;
1308 default:
1309 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001310 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001311 break;
1312 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 }
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001317}
1318
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001319static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001320{
1321 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001322 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001323 uint32_t start = qdict_get_int(qdict, "start");
1324 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001325
1326 sum = 0;
1327 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001328 uint8_t val = address_space_ldub(&address_space_memory, addr,
1329 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001330 /* BSD sum algorithm ('sum' Unix command) */
1331 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001332 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001333 }
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001335}
1336
bellard13224a82006-07-14 22:03:35 +00001337static int mouse_button_state;
1338
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001339static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001340{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001341 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001342 const char *dx_str = qdict_get_str(qdict, "dx_str");
1343 const char *dy_str = qdict_get_str(qdict, "dy_str");
1344 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001345
bellard13224a82006-07-14 22:03:35 +00001346 dx = strtol(dx_str, NULL, 0);
1347 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001348 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1349 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1350
1351 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001352 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001353 if (dz != 0) {
1354 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1355 qemu_input_queue_btn(NULL, button, true);
1356 qemu_input_event_sync();
1357 qemu_input_queue_btn(NULL, button, false);
1358 }
1359 }
1360 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001361}
1362
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001363static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001364{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001365 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1366 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1367 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1368 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1369 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001370 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001371
1372 if (mouse_button_state == button_state) {
1373 return;
1374 }
1375 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1376 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001377 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001378}
1379
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001380static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001381{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001382 int size = qdict_get_int(qdict, "size");
1383 int addr = qdict_get_int(qdict, "addr");
1384 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001385 uint32_t val;
1386 int suffix;
1387
1388 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001389 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001390 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001391 addr++;
1392 }
1393 addr &= 0xffff;
1394
1395 switch(size) {
1396 default:
1397 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001398 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001399 suffix = 'b';
1400 break;
1401 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001402 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001403 suffix = 'w';
1404 break;
1405 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001406 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001407 suffix = 'l';
1408 break;
1409 }
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1411 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001412}
bellarda3a91a32004-06-04 11:06:21 +00001413
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001414static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001415{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001416 int size = qdict_get_int(qdict, "size");
1417 int addr = qdict_get_int(qdict, "addr");
1418 int val = qdict_get_int(qdict, "val");
1419
Jan Kiszkaf1147842009-07-14 10:20:11 +02001420 addr &= IOPORTS_MASK;
1421
1422 switch (size) {
1423 default:
1424 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001425 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001426 break;
1427 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001428 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001429 break;
1430 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001431 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001432 break;
1433 }
1434}
1435
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001436static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001437{
Gongleif1839932014-12-03 18:20:58 +00001438 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001439 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001440
Gongleif1839932014-12-03 18:20:58 +00001441 qemu_boot_set(bootdevice, &local_err);
1442 if (local_err) {
1443 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1444 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001445 } else {
Gongleif1839932014-12-03 18:20:58 +00001446 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001447 }
1448}
1449
bellardb86bda52004-09-18 19:32:46 +00001450#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001451static void print_pte(Monitor *mon, hwaddr addr,
1452 hwaddr pte,
1453 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001454{
Blue Swirld65aaf32010-12-11 18:56:24 +00001455#ifdef TARGET_X86_64
1456 if (addr & (1ULL << 47)) {
1457 addr |= -1LL << 48;
1458 }
1459#endif
1460 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1461 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001462 addr,
1463 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001464 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001465 pte & PG_GLOBAL_MASK ? 'G' : '-',
1466 pte & PG_PSE_MASK ? 'P' : '-',
1467 pte & PG_DIRTY_MASK ? 'D' : '-',
1468 pte & PG_ACCESSED_MASK ? 'A' : '-',
1469 pte & PG_PCD_MASK ? 'C' : '-',
1470 pte & PG_PWT_MASK ? 'T' : '-',
1471 pte & PG_USER_MASK ? 'U' : '-',
1472 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001473}
1474
Andreas Färber9349b4f2012-03-14 01:38:32 +01001475static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001476{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001477 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001478 uint32_t pgd, pde, pte;
1479
bellardb86bda52004-09-18 19:32:46 +00001480 pgd = env->cr[3] & ~0xfff;
1481 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001482 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001483 pde = le32_to_cpu(pde);
1484 if (pde & PG_PRESENT_MASK) {
1485 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001486 /* 4M pages */
1487 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001488 } else {
1489 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001490 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001491 pte = le32_to_cpu(pte);
1492 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001493 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001494 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001495 ~0xfff);
1496 }
1497 }
1498 }
1499 }
1500 }
1501}
1502
Andreas Färber9349b4f2012-03-14 01:38:32 +01001503static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001504{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001505 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001506 uint64_t pdpe, pde, pte;
1507 uint64_t pdp_addr, pd_addr, pt_addr;
1508
1509 pdp_addr = env->cr[3] & ~0x1f;
1510 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001511 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001512 pdpe = le64_to_cpu(pdpe);
1513 if (pdpe & PG_PRESENT_MASK) {
1514 pd_addr = pdpe & 0x3fffffffff000ULL;
1515 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001516 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001517 pde = le64_to_cpu(pde);
1518 if (pde & PG_PRESENT_MASK) {
1519 if (pde & PG_PSE_MASK) {
1520 /* 2M pages with PAE, CR4.PSE is ignored */
1521 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001522 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001523 } else {
1524 pt_addr = pde & 0x3fffffffff000ULL;
1525 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001526 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001527 pte = le64_to_cpu(pte);
1528 if (pte & PG_PRESENT_MASK) {
1529 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1530 + (l3 << 12),
1531 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001532 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001533 }
1534 }
1535 }
1536 }
1537 }
1538 }
1539 }
1540}
1541
1542#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001543static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001544{
1545 uint64_t l1, l2, l3, l4;
1546 uint64_t pml4e, pdpe, pde, pte;
1547 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1548
1549 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1550 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001551 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001552 pml4e = le64_to_cpu(pml4e);
1553 if (pml4e & PG_PRESENT_MASK) {
1554 pdp_addr = pml4e & 0x3fffffffff000ULL;
1555 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001556 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001557 pdpe = le64_to_cpu(pdpe);
1558 if (pdpe & PG_PRESENT_MASK) {
1559 if (pdpe & PG_PSE_MASK) {
1560 /* 1G pages, CR4.PSE is ignored */
1561 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1562 0x3ffffc0000000ULL);
1563 } else {
1564 pd_addr = pdpe & 0x3fffffffff000ULL;
1565 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001566 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001567 pde = le64_to_cpu(pde);
1568 if (pde & PG_PRESENT_MASK) {
1569 if (pde & PG_PSE_MASK) {
1570 /* 2M pages, CR4.PSE is ignored */
1571 print_pte(mon, (l1 << 39) + (l2 << 30) +
1572 (l3 << 21), pde,
1573 0x3ffffffe00000ULL);
1574 } else {
1575 pt_addr = pde & 0x3fffffffff000ULL;
1576 for (l4 = 0; l4 < 512; l4++) {
1577 cpu_physical_memory_read(pt_addr
1578 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001579 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001580 pte = le64_to_cpu(pte);
1581 if (pte & PG_PRESENT_MASK) {
1582 print_pte(mon, (l1 << 39) +
1583 (l2 << 30) +
1584 (l3 << 21) + (l4 << 12),
1585 pte & ~PG_PSE_MASK,
1586 0x3fffffffff000ULL);
1587 }
1588 }
1589 }
1590 }
1591 }
1592 }
1593 }
1594 }
1595 }
1596 }
1597}
1598#endif
1599
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001600static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001601{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001602 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001603
1604 env = mon_get_cpu();
1605
1606 if (!(env->cr[0] & CR0_PG_MASK)) {
1607 monitor_printf(mon, "PG disabled\n");
1608 return;
1609 }
1610 if (env->cr[4] & CR4_PAE_MASK) {
1611#ifdef TARGET_X86_64
1612 if (env->hflags & HF_LMA_MASK) {
1613 tlb_info_64(mon, env);
1614 } else
1615#endif
1616 {
1617 tlb_info_pae32(mon, env);
1618 }
1619 } else {
1620 tlb_info_32(mon, env);
1621 }
1622}
1623
Avi Kivitya8170e52012-10-23 12:30:10 +02001624static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001625 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001626 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001627{
bellard9746b152004-11-11 18:30:24 +00001628 int prot1;
1629 prot1 = *plast_prot;
1630 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001631 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001632 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1633 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001634 *pstart, end, end - *pstart,
1635 prot1 & PG_USER_MASK ? 'u' : '-',
1636 'r',
1637 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001638 }
1639 if (prot != 0)
1640 *pstart = end;
1641 else
1642 *pstart = -1;
1643 *plast_prot = prot;
1644 }
1645}
1646
Andreas Färber9349b4f2012-03-14 01:38:32 +01001647static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001648{
Austin Clementsb49ca722011-08-14 23:19:21 -04001649 unsigned int l1, l2;
1650 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001651 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001652 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001653
bellardb86bda52004-09-18 19:32:46 +00001654 pgd = env->cr[3] & ~0xfff;
1655 last_prot = 0;
1656 start = -1;
1657 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001658 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001659 pde = le32_to_cpu(pde);
1660 end = l1 << 22;
1661 if (pde & PG_PRESENT_MASK) {
1662 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1663 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001664 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001665 } else {
1666 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001667 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001668 pte = le32_to_cpu(pte);
1669 end = (l1 << 22) + (l2 << 12);
1670 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001671 prot = pte & pde &
1672 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001673 } else {
1674 prot = 0;
1675 }
aliguori376253e2009-03-05 23:01:23 +00001676 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001677 }
1678 }
1679 } else {
1680 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001681 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001682 }
1683 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001684 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001685 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001686}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001687
Andreas Färber9349b4f2012-03-14 01:38:32 +01001688static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001689{
Austin Clementsb49ca722011-08-14 23:19:21 -04001690 unsigned int l1, l2, l3;
1691 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001692 uint64_t pdpe, pde, pte;
1693 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001694 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001695
1696 pdp_addr = env->cr[3] & ~0x1f;
1697 last_prot = 0;
1698 start = -1;
1699 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001700 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001701 pdpe = le64_to_cpu(pdpe);
1702 end = l1 << 30;
1703 if (pdpe & PG_PRESENT_MASK) {
1704 pd_addr = pdpe & 0x3fffffffff000ULL;
1705 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001706 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001707 pde = le64_to_cpu(pde);
1708 end = (l1 << 30) + (l2 << 21);
1709 if (pde & PG_PRESENT_MASK) {
1710 if (pde & PG_PSE_MASK) {
1711 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1712 PG_PRESENT_MASK);
1713 mem_print(mon, &start, &last_prot, end, prot);
1714 } else {
1715 pt_addr = pde & 0x3fffffffff000ULL;
1716 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001717 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001718 pte = le64_to_cpu(pte);
1719 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1720 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001721 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1722 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001723 } else {
1724 prot = 0;
1725 }
1726 mem_print(mon, &start, &last_prot, end, prot);
1727 }
1728 }
1729 } else {
1730 prot = 0;
1731 mem_print(mon, &start, &last_prot, end, prot);
1732 }
1733 }
1734 } else {
1735 prot = 0;
1736 mem_print(mon, &start, &last_prot, end, prot);
1737 }
1738 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001739 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001740 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001741}
1742
1743
1744#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001745static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001746{
1747 int prot, last_prot;
1748 uint64_t l1, l2, l3, l4;
1749 uint64_t pml4e, pdpe, pde, pte;
1750 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1751
1752 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1753 last_prot = 0;
1754 start = -1;
1755 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001756 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001757 pml4e = le64_to_cpu(pml4e);
1758 end = l1 << 39;
1759 if (pml4e & PG_PRESENT_MASK) {
1760 pdp_addr = pml4e & 0x3fffffffff000ULL;
1761 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001762 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001763 pdpe = le64_to_cpu(pdpe);
1764 end = (l1 << 39) + (l2 << 30);
1765 if (pdpe & PG_PRESENT_MASK) {
1766 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001767 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1768 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001769 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001770 mem_print(mon, &start, &last_prot, end, prot);
1771 } else {
1772 pd_addr = pdpe & 0x3fffffffff000ULL;
1773 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001774 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001775 pde = le64_to_cpu(pde);
1776 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1777 if (pde & PG_PRESENT_MASK) {
1778 if (pde & PG_PSE_MASK) {
1779 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1780 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001781 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001782 mem_print(mon, &start, &last_prot, end, prot);
1783 } else {
1784 pt_addr = pde & 0x3fffffffff000ULL;
1785 for (l4 = 0; l4 < 512; l4++) {
1786 cpu_physical_memory_read(pt_addr
1787 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001788 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001789 pte = le64_to_cpu(pte);
1790 end = (l1 << 39) + (l2 << 30) +
1791 (l3 << 21) + (l4 << 12);
1792 if (pte & PG_PRESENT_MASK) {
1793 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1794 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001795 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001796 } else {
1797 prot = 0;
1798 }
1799 mem_print(mon, &start, &last_prot, end, prot);
1800 }
1801 }
1802 } else {
1803 prot = 0;
1804 mem_print(mon, &start, &last_prot, end, prot);
1805 }
1806 }
1807 }
1808 } else {
1809 prot = 0;
1810 mem_print(mon, &start, &last_prot, end, prot);
1811 }
1812 }
1813 } else {
1814 prot = 0;
1815 mem_print(mon, &start, &last_prot, end, prot);
1816 }
1817 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001818 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001819 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001820}
1821#endif
1822
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001823static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001824{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001825 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001826
1827 env = mon_get_cpu();
1828
1829 if (!(env->cr[0] & CR0_PG_MASK)) {
1830 monitor_printf(mon, "PG disabled\n");
1831 return;
1832 }
1833 if (env->cr[4] & CR4_PAE_MASK) {
1834#ifdef TARGET_X86_64
1835 if (env->hflags & HF_LMA_MASK) {
1836 mem_info_64(mon, env);
1837 } else
1838#endif
1839 {
1840 mem_info_pae32(mon, env);
1841 }
1842 } else {
1843 mem_info_32(mon, env);
1844 }
1845}
bellardb86bda52004-09-18 19:32:46 +00001846#endif
1847
aurel327c664e22009-03-03 06:12:22 +00001848#if defined(TARGET_SH4)
1849
aliguori376253e2009-03-05 23:01:23 +00001850static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001851{
aliguori376253e2009-03-05 23:01:23 +00001852 monitor_printf(mon, " tlb%i:\t"
1853 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1854 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1855 "dirty=%hhu writethrough=%hhu\n",
1856 idx,
1857 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1858 tlb->v, tlb->sh, tlb->c, tlb->pr,
1859 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001860}
1861
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001862static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001863{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001864 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001865 int i;
1866
aliguori376253e2009-03-05 23:01:23 +00001867 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001868 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001869 print_tlb (mon, i, &env->itlb[i]);
1870 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001871 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001872 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001873}
1874
1875#endif
1876
Max Filippov692f7372012-01-07 20:02:40 +04001877#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001878static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001879{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001880 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001881
1882 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1883}
1884#endif
1885
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001886static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001887{
1888 mtree_info((fprintf_function)monitor_printf, mon);
1889}
1890
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001891static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001892{
aliguorib28b6232009-04-22 20:20:29 +00001893 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001894 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001895 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001896
zhanghailiang5b009e42014-11-04 19:49:30 +08001897 node_mem = g_new0(uint64_t, nb_numa_nodes);
1898 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001899 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1900 for (i = 0; i < nb_numa_nodes; i++) {
1901 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001902 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001903 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001904 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001905 }
1906 }
1907 monitor_printf(mon, "\n");
1908 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001909 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001910 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001911 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001912}
1913
bellard5f1ce942006-02-08 22:40:15 +00001914#ifdef CONFIG_PROFILER
1915
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001916int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001917int64_t dev_time;
1918
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001919static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001920{
aliguori376253e2009-03-05 23:01:23 +00001921 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001922 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001923 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001924 tcg_time, tcg_time / (double)get_ticks_per_sec());
1925 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001926 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001927}
1928#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001929static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001930{
aliguori376253e2009-03-05 23:01:23 +00001931 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001932}
1933#endif
1934
bellardec36b692006-07-16 18:57:03 +00001935/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001936static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001937
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001938static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001939{
1940 int i;
1941 CaptureState *s;
1942
1943 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001944 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001945 s->ops.info (s->opaque);
1946 }
1947}
1948
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001949static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001950{
1951 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001952 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001953 CaptureState *s;
1954
1955 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1956 if (i == n) {
1957 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001958 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001959 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001960 return;
1961 }
1962 }
1963}
1964
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001965static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001966{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001967 const char *path = qdict_get_str(qdict, "path");
1968 int has_freq = qdict_haskey(qdict, "freq");
1969 int freq = qdict_get_try_int(qdict, "freq", -1);
1970 int has_bits = qdict_haskey(qdict, "bits");
1971 int bits = qdict_get_try_int(qdict, "bits", -1);
1972 int has_channels = qdict_haskey(qdict, "nchannels");
1973 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001974 CaptureState *s;
1975
Anthony Liguori7267c092011-08-20 22:09:37 -05001976 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001977
1978 freq = has_freq ? freq : 44100;
1979 bits = has_bits ? bits : 16;
1980 nchannels = has_channels ? nchannels : 2;
1981
1982 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001983 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001984 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001985 return;
bellardec36b692006-07-16 18:57:03 +00001986 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001987 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001988}
bellardec36b692006-07-16 18:57:03 +00001989
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001990static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001991{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001992 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001993
aliguori76655d62009-03-06 20:27:37 +00001994 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001995 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001996 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001997 return acl;
1998}
aliguori76655d62009-03-06 20:27:37 +00001999
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002000static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002001{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002002 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002003 qemu_acl *acl = find_acl(mon, aclname);
2004 qemu_acl_entry *entry;
2005 int i = 0;
2006
2007 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002008 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002009 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002010 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002011 i++;
2012 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002013 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002014 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002015 }
2016}
2017
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002018static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002019{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002020 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002021 qemu_acl *acl = find_acl(mon, aclname);
2022
2023 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002024 qemu_acl_reset(acl);
2025 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002026 }
2027}
aliguori76655d62009-03-06 20:27:37 +00002028
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002029static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002030{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002031 const char *aclname = qdict_get_str(qdict, "aclname");
2032 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002033 qemu_acl *acl = find_acl(mon, aclname);
2034
2035 if (acl) {
2036 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002037 acl->defaultDeny = 0;
2038 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002039 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002040 acl->defaultDeny = 1;
2041 monitor_printf(mon, "acl: policy set to 'deny'\n");
2042 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002043 monitor_printf(mon, "acl: unknown policy '%s', "
2044 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002045 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002046 }
2047}
aliguori76655d62009-03-06 20:27:37 +00002048
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002049static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002050{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002051 const char *aclname = qdict_get_str(qdict, "aclname");
2052 const char *match = qdict_get_str(qdict, "match");
2053 const char *policy = qdict_get_str(qdict, "policy");
2054 int has_index = qdict_haskey(qdict, "index");
2055 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002056 qemu_acl *acl = find_acl(mon, aclname);
2057 int deny, ret;
2058
2059 if (acl) {
2060 if (strcmp(policy, "allow") == 0) {
2061 deny = 0;
2062 } else if (strcmp(policy, "deny") == 0) {
2063 deny = 1;
2064 } else {
2065 monitor_printf(mon, "acl: unknown policy '%s', "
2066 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002067 return;
2068 }
aliguori28a76be2009-03-06 20:27:40 +00002069 if (has_index)
2070 ret = qemu_acl_insert(acl, deny, match, index);
2071 else
2072 ret = qemu_acl_append(acl, deny, match);
2073 if (ret < 0)
2074 monitor_printf(mon, "acl: unable to add acl entry\n");
2075 else
2076 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002077 }
2078}
aliguori76655d62009-03-06 20:27:37 +00002079
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002080static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002081{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002082 const char *aclname = qdict_get_str(qdict, "aclname");
2083 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002084 qemu_acl *acl = find_acl(mon, aclname);
2085 int ret;
aliguori76655d62009-03-06 20:27:37 +00002086
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002087 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002088 ret = qemu_acl_remove(acl, match);
2089 if (ret < 0)
2090 monitor_printf(mon, "acl: no matching acl entry\n");
2091 else
2092 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002093 }
2094}
2095
Huang Ying79c4f6b2009-06-23 10:05:14 +08002096#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002097static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002098{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002099 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002100 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002101 int cpu_index = qdict_get_int(qdict, "cpu_index");
2102 int bank = qdict_get_int(qdict, "bank");
2103 uint64_t status = qdict_get_int(qdict, "status");
2104 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2105 uint64_t addr = qdict_get_int(qdict, "addr");
2106 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002107 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002108
Jan Kiszka747461c2011-03-02 08:56:10 +01002109 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2110 flags |= MCE_INJECT_BROADCAST;
2111 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002112 cs = qemu_get_cpu(cpu_index);
2113 if (cs != NULL) {
2114 cpu = X86_CPU(cs);
2115 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2116 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002117 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002118}
2119#endif
2120
Corey Bryant208c9d12012-06-22 14:36:09 -04002121void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002122{
Anthony Liguoric227f092009-10-01 16:12:16 -05002123 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002124 int fd;
2125
Corey Bryant208c9d12012-06-22 14:36:09 -04002126 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002127 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002128 error_set(errp, QERR_FD_NOT_SUPPLIED);
2129 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002130 }
2131
2132 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002133 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002134 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2135 "a name not starting with a digit");
2136 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002137 }
2138
Corey Bryant208c9d12012-06-22 14:36:09 -04002139 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002140 if (strcmp(monfd->name, fdname) != 0) {
2141 continue;
2142 }
2143
2144 close(monfd->fd);
2145 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002146 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002147 }
2148
Anthony Liguori7267c092011-08-20 22:09:37 -05002149 monfd = g_malloc0(sizeof(mon_fd_t));
2150 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002151 monfd->fd = fd;
2152
Corey Bryant208c9d12012-06-22 14:36:09 -04002153 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002154}
2155
Corey Bryant208c9d12012-06-22 14:36:09 -04002156void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002157{
Anthony Liguoric227f092009-10-01 16:12:16 -05002158 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002159
Corey Bryant208c9d12012-06-22 14:36:09 -04002160 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002161 if (strcmp(monfd->name, fdname) != 0) {
2162 continue;
2163 }
2164
Blue Swirl72cf2d42009-09-12 07:36:22 +00002165 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002166 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002167 g_free(monfd->name);
2168 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002169 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002170 }
2171
Corey Bryant208c9d12012-06-22 14:36:09 -04002172 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002173}
2174
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002175static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002176{
Luiz Capitulino13548692011-07-29 15:36:43 -03002177 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002178 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002179
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002180 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002181
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002182 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002183 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002184 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002185}
2186
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002187int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002188{
Anthony Liguoric227f092009-10-01 16:12:16 -05002189 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002190
Blue Swirl72cf2d42009-09-12 07:36:22 +00002191 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002192 int fd;
2193
2194 if (strcmp(monfd->name, fdname) != 0) {
2195 continue;
2196 }
2197
2198 fd = monfd->fd;
2199
2200 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002201 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002202 g_free(monfd->name);
2203 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002204
2205 return fd;
2206 }
2207
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002208 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002209 return -1;
2210}
2211
Corey Bryantba1c0482012-08-14 16:43:43 -04002212static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2213{
2214 MonFdsetFd *mon_fdset_fd;
2215 MonFdsetFd *mon_fdset_fd_next;
2216
2217 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002218 if ((mon_fdset_fd->removed ||
2219 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2220 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002221 close(mon_fdset_fd->fd);
2222 g_free(mon_fdset_fd->opaque);
2223 QLIST_REMOVE(mon_fdset_fd, next);
2224 g_free(mon_fdset_fd);
2225 }
2226 }
2227
Corey Bryantadb696f2012-08-14 16:43:47 -04002228 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002229 QLIST_REMOVE(mon_fdset, next);
2230 g_free(mon_fdset);
2231 }
2232}
2233
Corey Bryantefb87c12012-08-14 16:43:48 -04002234static void monitor_fdsets_cleanup(void)
2235{
2236 MonFdset *mon_fdset;
2237 MonFdset *mon_fdset_next;
2238
2239 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2240 monitor_fdset_cleanup(mon_fdset);
2241 }
2242}
2243
Corey Bryantba1c0482012-08-14 16:43:43 -04002244AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2245 const char *opaque, Error **errp)
2246{
2247 int fd;
2248 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002249 AddfdInfo *fdinfo;
2250
2251 fd = qemu_chr_fe_get_msgfd(mon->chr);
2252 if (fd == -1) {
2253 error_set(errp, QERR_FD_NOT_SUPPLIED);
2254 goto error;
2255 }
2256
Corey Bryante446f702012-10-18 15:19:32 -04002257 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2258 has_opaque, opaque, errp);
2259 if (fdinfo) {
2260 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002261 }
2262
Corey Bryantba1c0482012-08-14 16:43:43 -04002263error:
2264 if (fd != -1) {
2265 close(fd);
2266 }
2267 return NULL;
2268}
2269
2270void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2271{
2272 MonFdset *mon_fdset;
2273 MonFdsetFd *mon_fdset_fd;
2274 char fd_str[60];
2275
2276 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2277 if (mon_fdset->id != fdset_id) {
2278 continue;
2279 }
2280 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2281 if (has_fd) {
2282 if (mon_fdset_fd->fd != fd) {
2283 continue;
2284 }
2285 mon_fdset_fd->removed = true;
2286 break;
2287 } else {
2288 mon_fdset_fd->removed = true;
2289 }
2290 }
2291 if (has_fd && !mon_fdset_fd) {
2292 goto error;
2293 }
2294 monitor_fdset_cleanup(mon_fdset);
2295 return;
2296 }
2297
2298error:
2299 if (has_fd) {
2300 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2301 fdset_id, fd);
2302 } else {
2303 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2304 }
2305 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2306}
2307
2308FdsetInfoList *qmp_query_fdsets(Error **errp)
2309{
2310 MonFdset *mon_fdset;
2311 MonFdsetFd *mon_fdset_fd;
2312 FdsetInfoList *fdset_list = NULL;
2313
2314 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2315 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2316 FdsetFdInfoList *fdsetfd_list = NULL;
2317
2318 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2319 fdset_info->value->fdset_id = mon_fdset->id;
2320
2321 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2322 FdsetFdInfoList *fdsetfd_info;
2323
2324 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2325 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2326 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2327 if (mon_fdset_fd->opaque) {
2328 fdsetfd_info->value->has_opaque = true;
2329 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2330 } else {
2331 fdsetfd_info->value->has_opaque = false;
2332 }
2333
2334 fdsetfd_info->next = fdsetfd_list;
2335 fdsetfd_list = fdsetfd_info;
2336 }
2337
2338 fdset_info->value->fds = fdsetfd_list;
2339
2340 fdset_info->next = fdset_list;
2341 fdset_list = fdset_info;
2342 }
2343
2344 return fdset_list;
2345}
2346
Corey Bryante446f702012-10-18 15:19:32 -04002347AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2348 bool has_opaque, const char *opaque,
2349 Error **errp)
2350{
2351 MonFdset *mon_fdset = NULL;
2352 MonFdsetFd *mon_fdset_fd;
2353 AddfdInfo *fdinfo;
2354
2355 if (has_fdset_id) {
2356 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2357 /* Break if match found or match impossible due to ordering by ID */
2358 if (fdset_id <= mon_fdset->id) {
2359 if (fdset_id < mon_fdset->id) {
2360 mon_fdset = NULL;
2361 }
2362 break;
2363 }
2364 }
2365 }
2366
2367 if (mon_fdset == NULL) {
2368 int64_t fdset_id_prev = -1;
2369 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2370
2371 if (has_fdset_id) {
2372 if (fdset_id < 0) {
2373 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2374 "a non-negative value");
2375 return NULL;
2376 }
2377 /* Use specified fdset ID */
2378 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2379 mon_fdset_cur = mon_fdset;
2380 if (fdset_id < mon_fdset_cur->id) {
2381 break;
2382 }
2383 }
2384 } else {
2385 /* Use first available fdset ID */
2386 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2387 mon_fdset_cur = mon_fdset;
2388 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2389 fdset_id_prev = mon_fdset_cur->id;
2390 continue;
2391 }
2392 break;
2393 }
2394 }
2395
2396 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2397 if (has_fdset_id) {
2398 mon_fdset->id = fdset_id;
2399 } else {
2400 mon_fdset->id = fdset_id_prev + 1;
2401 }
2402
2403 /* The fdset list is ordered by fdset ID */
2404 if (!mon_fdset_cur) {
2405 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2406 } else if (mon_fdset->id < mon_fdset_cur->id) {
2407 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2408 } else {
2409 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2410 }
2411 }
2412
2413 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2414 mon_fdset_fd->fd = fd;
2415 mon_fdset_fd->removed = false;
2416 if (has_opaque) {
2417 mon_fdset_fd->opaque = g_strdup(opaque);
2418 }
2419 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2420
2421 fdinfo = g_malloc0(sizeof(*fdinfo));
2422 fdinfo->fdset_id = mon_fdset->id;
2423 fdinfo->fd = mon_fdset_fd->fd;
2424
2425 return fdinfo;
2426}
2427
Corey Bryantadb696f2012-08-14 16:43:47 -04002428int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2429{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002430#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002431 MonFdset *mon_fdset;
2432 MonFdsetFd *mon_fdset_fd;
2433 int mon_fd_flags;
2434
Corey Bryantadb696f2012-08-14 16:43:47 -04002435 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2436 if (mon_fdset->id != fdset_id) {
2437 continue;
2438 }
2439 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2440 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2441 if (mon_fd_flags == -1) {
2442 return -1;
2443 }
2444
2445 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2446 return mon_fdset_fd->fd;
2447 }
2448 }
2449 errno = EACCES;
2450 return -1;
2451 }
2452#endif
2453
2454 errno = ENOENT;
2455 return -1;
2456}
2457
2458int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2459{
2460 MonFdset *mon_fdset;
2461 MonFdsetFd *mon_fdset_fd_dup;
2462
2463 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2464 if (mon_fdset->id != fdset_id) {
2465 continue;
2466 }
2467 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2468 if (mon_fdset_fd_dup->fd == dup_fd) {
2469 return -1;
2470 }
2471 }
2472 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2473 mon_fdset_fd_dup->fd = dup_fd;
2474 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2475 return 0;
2476 }
2477 return -1;
2478}
2479
2480static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2481{
2482 MonFdset *mon_fdset;
2483 MonFdsetFd *mon_fdset_fd_dup;
2484
2485 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2486 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2487 if (mon_fdset_fd_dup->fd == dup_fd) {
2488 if (remove) {
2489 QLIST_REMOVE(mon_fdset_fd_dup, next);
2490 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2491 monitor_fdset_cleanup(mon_fdset);
2492 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002493 return -1;
2494 } else {
2495 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002496 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002497 }
2498 }
2499 }
2500 return -1;
2501}
2502
2503int monitor_fdset_dup_fd_find(int dup_fd)
2504{
2505 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2506}
2507
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002508void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002509{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002510 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002511}
2512
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002513int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002514{
2515 int fd;
2516 Error *local_err = NULL;
2517
2518 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002519 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002520 } else {
2521 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002522 if (fd == -1) {
2523 error_setg(&local_err, "Invalid file descriptor number '%s'",
2524 fdname);
2525 }
2526 }
2527 if (local_err) {
2528 error_propagate(errp, local_err);
2529 assert(fd == -1);
2530 } else {
2531 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002532 }
2533
2534 return fd;
2535}
2536
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002537/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002538static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002539 {
2540 .name = "version",
2541 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 .params = "",
2543 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002544 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 },
2546 {
2547 .name = "network",
2548 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002549 .params = "",
2550 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002551 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 },
2553 {
2554 .name = "chardev",
2555 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 .params = "",
2557 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002558 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 },
2560 {
2561 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002562 .args_type = "nodes:-n,verbose:-v,device:B?",
2563 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002564 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002565 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002566 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 },
2568 {
2569 .name = "blockstats",
2570 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 .params = "",
2572 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002573 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 },
2575 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002576 .name = "block-jobs",
2577 .args_type = "",
2578 .params = "",
2579 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002580 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002581 },
2582 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 .name = "registers",
2584 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 .params = "",
2586 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002587 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 },
2589 {
2590 .name = "cpus",
2591 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 .params = "",
2593 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002594 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 },
2596 {
2597 .name = "history",
2598 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 .params = "",
2600 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002601 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002603#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2604 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 {
2606 .name = "irq",
2607 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 .params = "",
2609 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002610#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002611 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002612#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002613 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002614#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002615 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002616#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 },
2618 {
2619 .name = "pic",
2620 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 .params = "",
2622 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002623#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002624 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002625#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002626 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002627#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002628 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002629#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002631#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 {
2633 .name = "pci",
2634 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 .params = "",
2636 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002637 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002639#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002640 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 {
2642 .name = "tlb",
2643 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 .params = "",
2645 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002646 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 },
aurel327c664e22009-03-03 06:12:22 +00002648#endif
2649#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 {
2651 .name = "mem",
2652 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 .params = "",
2654 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002655 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 },
bellardb86bda52004-09-18 19:32:46 +00002657#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 {
Blue Swirl314e2982011-09-11 20:22:05 +00002659 .name = "mtree",
2660 .args_type = "",
2661 .params = "",
2662 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002663 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002664 },
2665 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .name = "jit",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002670 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
Max Filippov246ae242014-11-02 11:04:18 +03002673 .name = "opcount",
2674 .args_type = "",
2675 .params = "",
2676 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002677 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002678 },
2679 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .name = "kvm",
2681 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .params = "",
2683 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002684 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 },
2686 {
2687 .name = "numa",
2688 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 .params = "",
2690 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002691 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 },
2693 {
2694 .name = "usb",
2695 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .params = "",
2697 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002698 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
2700 {
2701 .name = "usbhost",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002705 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
2707 {
2708 .name = "profile",
2709 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 .params = "",
2711 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002712 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 },
2714 {
2715 .name = "capture",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002719 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 },
2721 {
2722 .name = "snapshots",
2723 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002724 .params = "",
2725 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002726 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 },
2728 {
2729 .name = "status",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002733 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
2735 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 .name = "mice",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002740 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 },
2742 {
2743 .name = "vnc",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002747 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002749#if defined(CONFIG_SPICE)
2750 {
2751 .name = "spice",
2752 .args_type = "",
2753 .params = "",
2754 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002755 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002756 },
2757#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 {
2759 .name = "name",
2760 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 .params = "",
2762 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002763 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 },
2765 {
2766 .name = "uuid",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002770 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 {
2773 .name = "cpustats",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002777 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 },
blueswir131a60e22007-10-26 18:42:59 +00002779#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 {
2781 .name = "usernet",
2782 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 .params = "",
2784 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002785 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002786 },
blueswir131a60e22007-10-26 18:42:59 +00002787#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 {
2789 .name = "migrate",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002793 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 },
2795 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002796 .name = "migrate_capabilities",
2797 .args_type = "",
2798 .params = "",
2799 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002800 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002801 },
2802 {
Liang Li50e9a622015-03-23 16:32:29 +08002803 .name = "migrate_parameters",
2804 .args_type = "",
2805 .params = "",
2806 .help = "show current migration parameters",
2807 .mhandler.cmd = hmp_info_migrate_parameters,
2808 },
2809 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002810 .name = "migrate_cache_size",
2811 .args_type = "",
2812 .params = "",
2813 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002814 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002815 },
2816 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002817 .name = "balloon",
2818 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002819 .params = "",
2820 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002821 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 },
2823 {
2824 .name = "qtree",
2825 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002826 .params = "",
2827 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002828 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 },
2830 {
2831 .name = "qdm",
2832 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 .params = "",
2834 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002835 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002836 },
2837 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002838 .name = "qom-tree",
2839 .args_type = "path:s?",
2840 .params = "[path]",
2841 .help = "show QOM composition tree",
2842 .mhandler.cmd = hmp_info_qom_tree,
2843 },
2844 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 .name = "roms",
2846 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002847 .params = "",
2848 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002849 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302851 {
2852 .name = "trace-events",
2853 .args_type = "",
2854 .params = "",
2855 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002856 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302857 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002858 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002859 .name = "tpm",
2860 .args_type = "",
2861 .params = "",
2862 .help = "show the TPM device",
2863 .mhandler.cmd = hmp_info_tpm,
2864 },
2865 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002866 .name = "memdev",
2867 .args_type = "",
2868 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002869 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002870 .mhandler.cmd = hmp_info_memdev,
2871 },
2872 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002873 .name = "memory-devices",
2874 .args_type = "",
2875 .params = "",
2876 .help = "show memory devices",
2877 .mhandler.cmd = hmp_info_memory_devices,
2878 },
2879 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 .name = NULL,
2881 },
bellard9dc39cb2004-03-14 21:38:27 +00002882};
2883
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002884/* mon_cmds and info_cmds would be sorted at runtime */
2885static mon_cmd_t mon_cmds[] = {
2886#include "hmp-commands.h"
2887 { NULL, NULL, },
2888};
2889
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002890static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002891#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002892 { /* NULL */ },
2893};
2894
bellard9307c4c2004-04-04 12:57:25 +00002895/*******************************************************************/
2896
2897static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002898static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002899
bellard92a31b12005-02-10 22:00:52 +00002900#define MD_TLONG 0
2901#define MD_I32 1
2902
bellard9307c4c2004-04-04 12:57:25 +00002903typedef struct MonitorDef {
2904 const char *name;
2905 int offset;
blueswir18662d652008-10-02 18:32:44 +00002906 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002907 int type;
bellard9307c4c2004-04-04 12:57:25 +00002908} MonitorDef;
2909
bellard57206fd2004-04-25 18:54:52 +00002910#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002911static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002912{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002913 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002914 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002915}
2916#endif
2917
bellarda541f292004-04-12 20:39:29 +00002918#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002919static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002920{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002921 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002922 unsigned int u;
2923 int i;
2924
2925 u = 0;
2926 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002927 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002928
2929 return u;
2930}
2931
blueswir18662d652008-10-02 18:32:44 +00002932static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002933{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002934 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002935 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002936}
2937
blueswir18662d652008-10-02 18:32:44 +00002938static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002939{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002940 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002941 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002942}
bellard9fddaa02004-05-21 12:59:32 +00002943
blueswir18662d652008-10-02 18:32:44 +00002944static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002945{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002946 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002947 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002948}
2949
blueswir18662d652008-10-02 18:32:44 +00002950static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002951{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002952 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002953 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002954}
2955
blueswir18662d652008-10-02 18:32:44 +00002956static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002957{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002958 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002959 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002960}
bellarda541f292004-04-12 20:39:29 +00002961#endif
2962
bellarde95c8d52004-09-30 22:22:08 +00002963#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002964#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002965static target_long monitor_get_psr (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();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002968
2969 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002970}
bellard7b936c02005-10-30 17:05:13 +00002971#endif
bellarde95c8d52004-09-30 22:22:08 +00002972
blueswir18662d652008-10-02 18:32:44 +00002973static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002974{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002975 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002976 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002977}
2978#endif
2979
blueswir18662d652008-10-02 18:32:44 +00002980static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002981#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002982
2983#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002984 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2985 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2986 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002987
Andreas Färbere59d1672012-02-16 00:40:47 +01002988 { "eax", offsetof(CPUX86State, regs[0]) },
2989 { "ecx", offsetof(CPUX86State, regs[1]) },
2990 { "edx", offsetof(CPUX86State, regs[2]) },
2991 { "ebx", offsetof(CPUX86State, regs[3]) },
2992 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2993 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2994 { "esi", offsetof(CPUX86State, regs[6]) },
2995 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002996#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002997 { "r8", offsetof(CPUX86State, regs[8]) },
2998 { "r9", offsetof(CPUX86State, regs[9]) },
2999 { "r10", offsetof(CPUX86State, regs[10]) },
3000 { "r11", offsetof(CPUX86State, regs[11]) },
3001 { "r12", offsetof(CPUX86State, regs[12]) },
3002 { "r13", offsetof(CPUX86State, regs[13]) },
3003 { "r14", offsetof(CPUX86State, regs[14]) },
3004 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003005#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003006 { "eflags", offsetof(CPUX86State, eflags) },
3007 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003008 SEG("cs", R_CS)
3009 SEG("ds", R_DS)
3010 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003011 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003012 SEG("fs", R_FS)
3013 SEG("gs", R_GS)
3014 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003015#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003016 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003017 { "r0", offsetof(CPUPPCState, gpr[0]) },
3018 { "r1", offsetof(CPUPPCState, gpr[1]) },
3019 { "r2", offsetof(CPUPPCState, gpr[2]) },
3020 { "r3", offsetof(CPUPPCState, gpr[3]) },
3021 { "r4", offsetof(CPUPPCState, gpr[4]) },
3022 { "r5", offsetof(CPUPPCState, gpr[5]) },
3023 { "r6", offsetof(CPUPPCState, gpr[6]) },
3024 { "r7", offsetof(CPUPPCState, gpr[7]) },
3025 { "r8", offsetof(CPUPPCState, gpr[8]) },
3026 { "r9", offsetof(CPUPPCState, gpr[9]) },
3027 { "r10", offsetof(CPUPPCState, gpr[10]) },
3028 { "r11", offsetof(CPUPPCState, gpr[11]) },
3029 { "r12", offsetof(CPUPPCState, gpr[12]) },
3030 { "r13", offsetof(CPUPPCState, gpr[13]) },
3031 { "r14", offsetof(CPUPPCState, gpr[14]) },
3032 { "r15", offsetof(CPUPPCState, gpr[15]) },
3033 { "r16", offsetof(CPUPPCState, gpr[16]) },
3034 { "r17", offsetof(CPUPPCState, gpr[17]) },
3035 { "r18", offsetof(CPUPPCState, gpr[18]) },
3036 { "r19", offsetof(CPUPPCState, gpr[19]) },
3037 { "r20", offsetof(CPUPPCState, gpr[20]) },
3038 { "r21", offsetof(CPUPPCState, gpr[21]) },
3039 { "r22", offsetof(CPUPPCState, gpr[22]) },
3040 { "r23", offsetof(CPUPPCState, gpr[23]) },
3041 { "r24", offsetof(CPUPPCState, gpr[24]) },
3042 { "r25", offsetof(CPUPPCState, gpr[25]) },
3043 { "r26", offsetof(CPUPPCState, gpr[26]) },
3044 { "r27", offsetof(CPUPPCState, gpr[27]) },
3045 { "r28", offsetof(CPUPPCState, gpr[28]) },
3046 { "r29", offsetof(CPUPPCState, gpr[29]) },
3047 { "r30", offsetof(CPUPPCState, gpr[30]) },
3048 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003049 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003050 { "f0", offsetof(CPUPPCState, fpr[0]) },
3051 { "f1", offsetof(CPUPPCState, fpr[1]) },
3052 { "f2", offsetof(CPUPPCState, fpr[2]) },
3053 { "f3", offsetof(CPUPPCState, fpr[3]) },
3054 { "f4", offsetof(CPUPPCState, fpr[4]) },
3055 { "f5", offsetof(CPUPPCState, fpr[5]) },
3056 { "f6", offsetof(CPUPPCState, fpr[6]) },
3057 { "f7", offsetof(CPUPPCState, fpr[7]) },
3058 { "f8", offsetof(CPUPPCState, fpr[8]) },
3059 { "f9", offsetof(CPUPPCState, fpr[9]) },
3060 { "f10", offsetof(CPUPPCState, fpr[10]) },
3061 { "f11", offsetof(CPUPPCState, fpr[11]) },
3062 { "f12", offsetof(CPUPPCState, fpr[12]) },
3063 { "f13", offsetof(CPUPPCState, fpr[13]) },
3064 { "f14", offsetof(CPUPPCState, fpr[14]) },
3065 { "f15", offsetof(CPUPPCState, fpr[15]) },
3066 { "f16", offsetof(CPUPPCState, fpr[16]) },
3067 { "f17", offsetof(CPUPPCState, fpr[17]) },
3068 { "f18", offsetof(CPUPPCState, fpr[18]) },
3069 { "f19", offsetof(CPUPPCState, fpr[19]) },
3070 { "f20", offsetof(CPUPPCState, fpr[20]) },
3071 { "f21", offsetof(CPUPPCState, fpr[21]) },
3072 { "f22", offsetof(CPUPPCState, fpr[22]) },
3073 { "f23", offsetof(CPUPPCState, fpr[23]) },
3074 { "f24", offsetof(CPUPPCState, fpr[24]) },
3075 { "f25", offsetof(CPUPPCState, fpr[25]) },
3076 { "f26", offsetof(CPUPPCState, fpr[26]) },
3077 { "f27", offsetof(CPUPPCState, fpr[27]) },
3078 { "f28", offsetof(CPUPPCState, fpr[28]) },
3079 { "f29", offsetof(CPUPPCState, fpr[29]) },
3080 { "f30", offsetof(CPUPPCState, fpr[30]) },
3081 { "f31", offsetof(CPUPPCState, fpr[31]) },
3082 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003083 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003084 { "nip|pc", offsetof(CPUPPCState, nip) },
3085 { "lr", offsetof(CPUPPCState, lr) },
3086 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003087 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003088 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003089 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003090 { "msr", 0, &monitor_get_msr, },
3091 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003092 { "tbu", 0, &monitor_get_tbu, },
3093 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003094 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003095 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3096 { "sr0", offsetof(CPUPPCState, sr[0]) },
3097 { "sr1", offsetof(CPUPPCState, sr[1]) },
3098 { "sr2", offsetof(CPUPPCState, sr[2]) },
3099 { "sr3", offsetof(CPUPPCState, sr[3]) },
3100 { "sr4", offsetof(CPUPPCState, sr[4]) },
3101 { "sr5", offsetof(CPUPPCState, sr[5]) },
3102 { "sr6", offsetof(CPUPPCState, sr[6]) },
3103 { "sr7", offsetof(CPUPPCState, sr[7]) },
3104 { "sr8", offsetof(CPUPPCState, sr[8]) },
3105 { "sr9", offsetof(CPUPPCState, sr[9]) },
3106 { "sr10", offsetof(CPUPPCState, sr[10]) },
3107 { "sr11", offsetof(CPUPPCState, sr[11]) },
3108 { "sr12", offsetof(CPUPPCState, sr[12]) },
3109 { "sr13", offsetof(CPUPPCState, sr[13]) },
3110 { "sr14", offsetof(CPUPPCState, sr[14]) },
3111 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003112 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003113 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003114
Andreas Färbere59d1672012-02-16 00:40:47 +01003115 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3116 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003117 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3118 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3119 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003120 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3121 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3122 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3123 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3124 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3125 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3126 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3127 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3128 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3129 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3130 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3131 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3132 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3133 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3134 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3135 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3136 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3137 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3138 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3139 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3140 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3141 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3142 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3143 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3144 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3145 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3146 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3147 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3148 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3149 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3150 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3151 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3152 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3153 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3154 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3155 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3156 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3157 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3158 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3159 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3160 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3161 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3162 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3163 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3164 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3165 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3166 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3167 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3168 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3169 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3170 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3171 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3172 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3173 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3174 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3175 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3176 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3177 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3178 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3179 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3180 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3181 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3182 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3183 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003184
bellarde95c8d52004-09-30 22:22:08 +00003185#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003186 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3187 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3188 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3189 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3190 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3191 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3192 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3193 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003194 { "o0", 0, monitor_get_reg },
3195 { "o1", 1, monitor_get_reg },
3196 { "o2", 2, monitor_get_reg },
3197 { "o3", 3, monitor_get_reg },
3198 { "o4", 4, monitor_get_reg },
3199 { "o5", 5, monitor_get_reg },
3200 { "o6", 6, monitor_get_reg },
3201 { "o7", 7, monitor_get_reg },
3202 { "l0", 8, monitor_get_reg },
3203 { "l1", 9, monitor_get_reg },
3204 { "l2", 10, monitor_get_reg },
3205 { "l3", 11, monitor_get_reg },
3206 { "l4", 12, monitor_get_reg },
3207 { "l5", 13, monitor_get_reg },
3208 { "l6", 14, monitor_get_reg },
3209 { "l7", 15, monitor_get_reg },
3210 { "i0", 16, monitor_get_reg },
3211 { "i1", 17, monitor_get_reg },
3212 { "i2", 18, monitor_get_reg },
3213 { "i3", 19, monitor_get_reg },
3214 { "i4", 20, monitor_get_reg },
3215 { "i5", 21, monitor_get_reg },
3216 { "i6", 22, monitor_get_reg },
3217 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003218 { "pc", offsetof(CPUSPARCState, pc) },
3219 { "npc", offsetof(CPUSPARCState, npc) },
3220 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003221#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003222 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003223 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003224#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003225 { "tbr", offsetof(CPUSPARCState, tbr) },
3226 { "fsr", offsetof(CPUSPARCState, fsr) },
3227 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3228 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3229 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3230 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3231 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3232 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3233 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3234 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3235 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3236 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3237 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3238 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3239 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3240 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3241 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3242 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3243 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3244 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3245 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3246 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3247 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3248 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3249 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3250 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3251 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3252 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3253 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3254 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3255 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3256 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3257 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3258 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003259#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003260 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3261 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3262 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3263 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3264 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3265 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3266 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3267 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3268 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3269 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3270 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3271 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3272 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3273 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3274 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3275 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3276 { "asi", offsetof(CPUSPARCState, asi) },
3277 { "pstate", offsetof(CPUSPARCState, pstate) },
3278 { "cansave", offsetof(CPUSPARCState, cansave) },
3279 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3280 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3281 { "wstate", offsetof(CPUSPARCState, wstate) },
3282 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3283 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003284#endif
bellard9307c4c2004-04-04 12:57:25 +00003285#endif
3286 { NULL },
3287};
3288
Stefan Weil9c3175c2013-08-22 21:30:09 +02003289static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3290expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003291{
Fam Zheng277acfe2013-08-20 10:58:21 +08003292 va_list ap;
3293 va_start(ap, fmt);
3294 monitor_vprintf(mon, fmt, ap);
3295 monitor_printf(mon, "\n");
3296 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003297 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003298}
3299
Markus Armbruster09b94182010-01-20 13:07:30 +01003300/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003301static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003302{
blueswir18662d652008-10-02 18:32:44 +00003303 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003304 void *ptr;
3305
bellard9307c4c2004-04-04 12:57:25 +00003306 for(md = monitor_defs; md->name != NULL; md++) {
3307 if (compare_cmd(name, md->name)) {
3308 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003309 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003310 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003311 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003312 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003313 switch(md->type) {
3314 case MD_I32:
3315 *pval = *(int32_t *)ptr;
3316 break;
3317 case MD_TLONG:
3318 *pval = *(target_long *)ptr;
3319 break;
3320 default:
3321 *pval = 0;
3322 break;
3323 }
bellard9307c4c2004-04-04 12:57:25 +00003324 }
3325 return 0;
3326 }
3327 }
3328 return -1;
3329}
3330
3331static void next(void)
3332{
Blue Swirl660f11b2009-07-31 21:16:51 +00003333 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003334 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003335 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003336 pch++;
3337 }
3338}
3339
aliguori376253e2009-03-05 23:01:23 +00003340static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003341
aliguori376253e2009-03-05 23:01:23 +00003342static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003343{
blueswir1c2efc952007-09-25 17:28:42 +00003344 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003345 char *p;
bellard6a00d602005-11-21 23:25:50 +00003346 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003347
3348 switch(*pch) {
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_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003356 break;
3357 case '~':
3358 next();
aliguori376253e2009-03-05 23:01:23 +00003359 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003360 break;
3361 case '(':
3362 next();
aliguori376253e2009-03-05 23:01:23 +00003363 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003364 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003365 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003366 }
3367 next();
3368 break;
bellard81d09122004-07-14 17:21:37 +00003369 case '\'':
3370 pch++;
3371 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003372 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003373 n = *pch;
3374 pch++;
3375 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003376 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003377 next();
3378 break;
bellard9307c4c2004-04-04 12:57:25 +00003379 case '$':
3380 {
3381 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003382 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003383
bellard9307c4c2004-04-04 12:57:25 +00003384 pch++;
3385 q = buf;
3386 while ((*pch >= 'a' && *pch <= 'z') ||
3387 (*pch >= 'A' && *pch <= 'Z') ||
3388 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003389 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003390 if ((q - buf) < sizeof(buf) - 1)
3391 *q++ = *pch;
3392 pch++;
3393 }
blueswir1cd390082008-11-16 13:53:32 +00003394 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003395 pch++;
3396 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003397 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003398 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003399 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003400 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003401 }
3402 break;
3403 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003404 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003405 n = 0;
3406 break;
3407 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003408 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003409 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003410 if (errno == ERANGE) {
3411 expr_error(mon, "number too large");
3412 }
bellard9307c4c2004-04-04 12:57:25 +00003413 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003414 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003415 }
3416 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003417 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003418 pch++;
3419 break;
3420 }
3421 return n;
3422}
3423
3424
aliguori376253e2009-03-05 23:01:23 +00003425static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003426{
blueswir1c2efc952007-09-25 17:28:42 +00003427 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003428 int op;
ths3b46e622007-09-17 08:09:54 +00003429
aliguori376253e2009-03-05 23:01:23 +00003430 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003431 for(;;) {
3432 op = *pch;
3433 if (op != '*' && op != '/' && op != '%')
3434 break;
3435 next();
aliguori376253e2009-03-05 23:01:23 +00003436 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003437 switch(op) {
3438 default:
3439 case '*':
3440 val *= val2;
3441 break;
3442 case '/':
3443 case '%':
ths5fafdf22007-09-16 21:08:06 +00003444 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003445 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003446 if (op == '/')
3447 val /= val2;
3448 else
3449 val %= val2;
3450 break;
3451 }
3452 }
3453 return val;
3454}
3455
aliguori376253e2009-03-05 23:01:23 +00003456static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003457{
blueswir1c2efc952007-09-25 17:28:42 +00003458 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003459 int op;
bellard9307c4c2004-04-04 12:57:25 +00003460
aliguori376253e2009-03-05 23:01:23 +00003461 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003462 for(;;) {
3463 op = *pch;
3464 if (op != '&' && op != '|' && op != '^')
3465 break;
3466 next();
aliguori376253e2009-03-05 23:01:23 +00003467 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003468 switch(op) {
3469 default:
3470 case '&':
3471 val &= val2;
3472 break;
3473 case '|':
3474 val |= val2;
3475 break;
3476 case '^':
3477 val ^= val2;
3478 break;
3479 }
3480 }
3481 return val;
3482}
3483
aliguori376253e2009-03-05 23:01:23 +00003484static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003485{
blueswir1c2efc952007-09-25 17:28:42 +00003486 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003487 int op;
bellard9307c4c2004-04-04 12:57:25 +00003488
aliguori376253e2009-03-05 23:01:23 +00003489 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003490 for(;;) {
3491 op = *pch;
3492 if (op != '+' && op != '-')
3493 break;
3494 next();
aliguori376253e2009-03-05 23:01:23 +00003495 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003496 if (op == '+')
3497 val += val2;
3498 else
3499 val -= val2;
3500 }
3501 return val;
3502}
3503
aliguori376253e2009-03-05 23:01:23 +00003504static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003505{
3506 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003507 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003508 *pp = pch;
3509 return -1;
3510 }
blueswir1cd390082008-11-16 13:53:32 +00003511 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003512 pch++;
aliguori376253e2009-03-05 23:01:23 +00003513 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003514 *pp = pch;
3515 return 0;
3516}
3517
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003518static int get_double(Monitor *mon, double *pval, const char **pp)
3519{
3520 const char *p = *pp;
3521 char *tailp;
3522 double d;
3523
3524 d = strtod(p, &tailp);
3525 if (tailp == p) {
3526 monitor_printf(mon, "Number expected\n");
3527 return -1;
3528 }
3529 if (d != d || d - d != 0) {
3530 /* NaN or infinity */
3531 monitor_printf(mon, "Bad number\n");
3532 return -1;
3533 }
3534 *pval = d;
3535 *pp = tailp;
3536 return 0;
3537}
3538
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003539/*
3540 * Store the command-name in cmdname, and return a pointer to
3541 * the remaining of the command string.
3542 */
3543static const char *get_command_name(const char *cmdline,
3544 char *cmdname, size_t nlen)
3545{
3546 size_t len;
3547 const char *p, *pstart;
3548
3549 p = cmdline;
3550 while (qemu_isspace(*p))
3551 p++;
3552 if (*p == '\0')
3553 return NULL;
3554 pstart = p;
3555 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3556 p++;
3557 len = p - pstart;
3558 if (len > nlen - 1)
3559 len = nlen - 1;
3560 memcpy(cmdname, pstart, len);
3561 cmdname[len] = '\0';
3562 return p;
3563}
3564
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003565/**
3566 * Read key of 'type' into 'key' and return the current
3567 * 'type' pointer.
3568 */
3569static char *key_get_info(const char *type, char **key)
3570{
3571 size_t len;
3572 char *p, *str;
3573
3574 if (*type == ',')
3575 type++;
3576
3577 p = strchr(type, ':');
3578 if (!p) {
3579 *key = NULL;
3580 return NULL;
3581 }
3582 len = p - type;
3583
Anthony Liguori7267c092011-08-20 22:09:37 -05003584 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003585 memcpy(str, type, len);
3586 str[len] = '\0';
3587
3588 *key = str;
3589 return ++p;
3590}
3591
bellard9307c4c2004-04-04 12:57:25 +00003592static int default_fmt_format = 'x';
3593static int default_fmt_size = 4;
3594
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003595static int is_valid_option(const char *c, const char *typestr)
3596{
3597 char option[3];
3598
3599 option[0] = '-';
3600 option[1] = *c;
3601 option[2] = '\0';
3602
3603 typestr = strstr(typestr, option);
3604 return (typestr != NULL);
3605}
3606
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003607static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3608 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003609{
3610 const mon_cmd_t *cmd;
3611
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003612 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003613 if (compare_cmd(cmdname, cmd->name)) {
3614 return cmd;
3615 }
3616 }
3617
3618 return NULL;
3619}
3620
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003621static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3622{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003623 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003624}
3625
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003626/*
3627 * Parse @cmdline according to command table @table.
3628 * If @cmdline is blank, return NULL.
3629 * If it can't be parsed, report to @mon, and return NULL.
3630 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003631 * If a sub-command table exists, and if @cmdline contains an additional string
3632 * for a sub-command, this function will try to search the sub-command table.
3633 * If no additional string for a sub-command is present, this function will
3634 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003635 * Do not assume the returned command points into @table! It doesn't
3636 * when the command is a sub-command.
3637 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003638static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003639 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003640 int start,
3641 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003642 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003643{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003644 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003645 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003646 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003647 char cmdname[256];
3648 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003649 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003650
3651#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003652 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003653#endif
ths3b46e622007-09-17 08:09:54 +00003654
bellard9307c4c2004-04-04 12:57:25 +00003655 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003656 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003657 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003658 return NULL;
ths3b46e622007-09-17 08:09:54 +00003659
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003660 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003661 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003662 monitor_printf(mon, "unknown command: '%.*s'\n",
3663 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003664 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003665 }
bellard9307c4c2004-04-04 12:57:25 +00003666
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003667 /* filter out following useless space */
3668 while (qemu_isspace(*p)) {
3669 p++;
3670 }
3671 /* search sub command */
3672 if (cmd->sub_table != NULL) {
3673 /* check if user set additional command */
3674 if (*p == '\0') {
3675 return cmd;
3676 }
3677 return monitor_parse_command(mon, cmdline, p - cmdline,
3678 cmd->sub_table, qdict);
3679 }
3680
bellard9307c4c2004-04-04 12:57:25 +00003681 /* parse the parameters */
3682 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003683 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003684 typestr = key_get_info(typestr, &key);
3685 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003686 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003687 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003688 typestr++;
3689 switch(c) {
3690 case 'F':
bellard81d09122004-07-14 17:21:37 +00003691 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003692 case 's':
3693 {
3694 int ret;
ths3b46e622007-09-17 08:09:54 +00003695
blueswir1cd390082008-11-16 13:53:32 +00003696 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003697 p++;
3698 if (*typestr == '?') {
3699 typestr++;
3700 if (*p == '\0') {
3701 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003702 break;
bellard9307c4c2004-04-04 12:57:25 +00003703 }
3704 }
3705 ret = get_str(buf, sizeof(buf), &p);
3706 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003707 switch(c) {
3708 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003709 monitor_printf(mon, "%s: filename expected\n",
3710 cmdname);
bellard81d09122004-07-14 17:21:37 +00003711 break;
3712 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003713 monitor_printf(mon, "%s: block device name expected\n",
3714 cmdname);
bellard81d09122004-07-14 17:21:37 +00003715 break;
3716 default:
aliguori376253e2009-03-05 23:01:23 +00003717 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003718 break;
3719 }
bellard9307c4c2004-04-04 12:57:25 +00003720 goto fail;
3721 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003722 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003723 }
3724 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003725 case 'O':
3726 {
3727 QemuOptsList *opts_list;
3728 QemuOpts *opts;
3729
3730 opts_list = qemu_find_opts(key);
3731 if (!opts_list || opts_list->desc->name) {
3732 goto bad_type;
3733 }
3734 while (qemu_isspace(*p)) {
3735 p++;
3736 }
3737 if (!*p)
3738 break;
3739 if (get_str(buf, sizeof(buf), &p) < 0) {
3740 goto fail;
3741 }
3742 opts = qemu_opts_parse(opts_list, buf, 1);
3743 if (!opts) {
3744 goto fail;
3745 }
3746 qemu_opts_to_qdict(opts, qdict);
3747 qemu_opts_del(opts);
3748 }
3749 break;
bellard9307c4c2004-04-04 12:57:25 +00003750 case '/':
3751 {
3752 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003753
blueswir1cd390082008-11-16 13:53:32 +00003754 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003755 p++;
3756 if (*p == '/') {
3757 /* format found */
3758 p++;
3759 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003760 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003761 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003762 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003763 count = count * 10 + (*p - '0');
3764 p++;
3765 }
3766 }
3767 size = -1;
3768 format = -1;
3769 for(;;) {
3770 switch(*p) {
3771 case 'o':
3772 case 'd':
3773 case 'u':
3774 case 'x':
3775 case 'i':
3776 case 'c':
3777 format = *p++;
3778 break;
3779 case 'b':
3780 size = 1;
3781 p++;
3782 break;
3783 case 'h':
3784 size = 2;
3785 p++;
3786 break;
3787 case 'w':
3788 size = 4;
3789 p++;
3790 break;
3791 case 'g':
3792 case 'L':
3793 size = 8;
3794 p++;
3795 break;
3796 default:
3797 goto next;
3798 }
3799 }
3800 next:
blueswir1cd390082008-11-16 13:53:32 +00003801 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003802 monitor_printf(mon, "invalid char in format: '%c'\n",
3803 *p);
bellard9307c4c2004-04-04 12:57:25 +00003804 goto fail;
3805 }
bellard9307c4c2004-04-04 12:57:25 +00003806 if (format < 0)
3807 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003808 if (format != 'i') {
3809 /* for 'i', not specifying a size gives -1 as size */
3810 if (size < 0)
3811 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003812 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003813 }
bellard9307c4c2004-04-04 12:57:25 +00003814 default_fmt_format = format;
3815 } else {
3816 count = 1;
3817 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003818 if (format != 'i') {
3819 size = default_fmt_size;
3820 } else {
3821 size = -1;
3822 }
bellard9307c4c2004-04-04 12:57:25 +00003823 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003824 qdict_put(qdict, "count", qint_from_int(count));
3825 qdict_put(qdict, "format", qint_from_int(format));
3826 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003827 }
3828 break;
3829 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003830 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003831 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003832 {
blueswir1c2efc952007-09-25 17:28:42 +00003833 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003834
blueswir1cd390082008-11-16 13:53:32 +00003835 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003836 p++;
bellard34405572004-06-08 00:55:58 +00003837 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003838 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003839 if (*p == '\0') {
3840 typestr++;
3841 break;
3842 }
bellard34405572004-06-08 00:55:58 +00003843 } else {
3844 if (*p == '.') {
3845 p++;
blueswir1cd390082008-11-16 13:53:32 +00003846 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003847 p++;
bellard34405572004-06-08 00:55:58 +00003848 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003849 typestr++;
3850 break;
bellard34405572004-06-08 00:55:58 +00003851 }
3852 }
bellard13224a82006-07-14 22:03:35 +00003853 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003854 }
aliguori376253e2009-03-05 23:01:23 +00003855 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003856 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003857 /* Check if 'i' is greater than 32-bit */
3858 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3859 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3860 monitor_printf(mon, "integer is for 32-bit values\n");
3861 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003862 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003863 if (val < 0) {
3864 monitor_printf(mon, "enter a positive value\n");
3865 goto fail;
3866 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003867 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003868 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003869 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003870 }
3871 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003872 case 'o':
3873 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003874 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003875 char *end;
3876
3877 while (qemu_isspace(*p)) {
3878 p++;
3879 }
3880 if (*typestr == '?') {
3881 typestr++;
3882 if (*p == '\0') {
3883 break;
3884 }
3885 }
3886 val = strtosz(p, &end);
3887 if (val < 0) {
3888 monitor_printf(mon, "invalid size\n");
3889 goto fail;
3890 }
3891 qdict_put(qdict, key, qint_from_int(val));
3892 p = end;
3893 }
3894 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003895 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003896 {
3897 double val;
3898
3899 while (qemu_isspace(*p))
3900 p++;
3901 if (*typestr == '?') {
3902 typestr++;
3903 if (*p == '\0') {
3904 break;
3905 }
3906 }
3907 if (get_double(mon, &val, &p) < 0) {
3908 goto fail;
3909 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003910 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003911 switch (*p) {
3912 case 'm':
3913 val /= 1e3; p += 2; break;
3914 case 'u':
3915 val /= 1e6; p += 2; break;
3916 case 'n':
3917 val /= 1e9; p += 2; break;
3918 }
3919 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003920 if (*p && !qemu_isspace(*p)) {
3921 monitor_printf(mon, "Unknown unit suffix\n");
3922 goto fail;
3923 }
3924 qdict_put(qdict, key, qfloat_from_double(val));
3925 }
3926 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003927 case 'b':
3928 {
3929 const char *beg;
3930 int val;
3931
3932 while (qemu_isspace(*p)) {
3933 p++;
3934 }
3935 beg = p;
3936 while (qemu_isgraph(*p)) {
3937 p++;
3938 }
3939 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3940 val = 1;
3941 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3942 val = 0;
3943 } else {
3944 monitor_printf(mon, "Expected 'on' or 'off'\n");
3945 goto fail;
3946 }
3947 qdict_put(qdict, key, qbool_from_int(val));
3948 }
3949 break;
bellard9307c4c2004-04-04 12:57:25 +00003950 case '-':
3951 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003952 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003953 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003954 /* option */
ths3b46e622007-09-17 08:09:54 +00003955
bellard9307c4c2004-04-04 12:57:25 +00003956 c = *typestr++;
3957 if (c == '\0')
3958 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003959 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003960 p++;
bellard9307c4c2004-04-04 12:57:25 +00003961 if (*p == '-') {
3962 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003963 if(c != *p) {
3964 if(!is_valid_option(p, typestr)) {
3965
3966 monitor_printf(mon, "%s: unsupported option -%c\n",
3967 cmdname, *p);
3968 goto fail;
3969 } else {
3970 skip_key = 1;
3971 }
bellard9307c4c2004-04-04 12:57:25 +00003972 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003973 if(skip_key) {
3974 p = tmp;
3975 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003976 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003977 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003978 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003979 }
bellard9307c4c2004-04-04 12:57:25 +00003980 }
bellard9307c4c2004-04-04 12:57:25 +00003981 }
3982 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003983 case 'S':
3984 {
3985 /* package all remaining string */
3986 int len;
3987
3988 while (qemu_isspace(*p)) {
3989 p++;
3990 }
3991 if (*typestr == '?') {
3992 typestr++;
3993 if (*p == '\0') {
3994 /* no remaining string: NULL argument */
3995 break;
3996 }
3997 }
3998 len = strlen(p);
3999 if (len <= 0) {
4000 monitor_printf(mon, "%s: string expected\n",
4001 cmdname);
4002 break;
4003 }
4004 qdict_put(qdict, key, qstring_from_str(p));
4005 p += len;
4006 }
4007 break;
bellard9307c4c2004-04-04 12:57:25 +00004008 default:
4009 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004010 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004011 goto fail;
4012 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004013 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004014 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004015 }
4016 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004017 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004018 p++;
4019 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004020 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4021 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004022 goto fail;
4023 }
4024
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004025 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004026
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004027fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004028 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004029 return NULL;
4030}
4031
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004032void monitor_set_error(Monitor *mon, QError *qerror)
4033{
4034 /* report only the first error */
4035 if (!mon->error) {
4036 mon->error = qerror;
4037 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004038 QDECREF(qerror);
4039 }
4040}
4041
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004042static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004043{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004044 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004045 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004046
4047 qdict = qdict_new();
4048
Wenchao Xia77172392013-08-27 20:38:20 +08004049 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004050 if (!cmd)
4051 goto out;
4052
Markus Armbruster65207c52015-03-05 14:35:26 +01004053 if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004054 QObject *data = NULL;
4055
4056 /* XXX: ignores the error code */
4057 cmd->mhandler.cmd_new(mon, qdict, &data);
4058 assert(!monitor_has_error(mon));
4059 if (data) {
4060 cmd->user_print(mon, data);
4061 qobject_decref(data);
4062 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004063 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004064 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004065 }
4066
Luiz Capitulino13917be2009-10-07 13:41:54 -03004067out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004068 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004069}
4070
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004071static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004072{
4073 const char *p, *pstart;
4074 char cmd[128];
4075 int len;
4076
4077 p = list;
4078 for(;;) {
4079 pstart = p;
4080 p = strchr(p, '|');
4081 if (!p)
4082 p = pstart + strlen(pstart);
4083 len = p - pstart;
4084 if (len > sizeof(cmd) - 2)
4085 len = sizeof(cmd) - 2;
4086 memcpy(cmd, pstart, len);
4087 cmd[len] = '\0';
4088 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004089 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004090 }
4091 if (*p == '\0')
4092 break;
4093 p++;
4094 }
4095}
4096
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004097static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004098{
4099 DIR *ffs;
4100 struct dirent *d;
4101 char path[1024];
4102 char file[1024], file_prefix[1024];
4103 int input_path_len;
4104 const char *p;
4105
ths5fafdf22007-09-16 21:08:06 +00004106 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004107 if (!p) {
4108 input_path_len = 0;
4109 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004110 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004111 } else {
4112 input_path_len = p - input + 1;
4113 memcpy(path, input, input_path_len);
4114 if (input_path_len > sizeof(path) - 1)
4115 input_path_len = sizeof(path) - 1;
4116 path[input_path_len] = '\0';
4117 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4118 }
4119#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004120 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004121 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004122#endif
4123 ffs = opendir(path);
4124 if (!ffs)
4125 return;
4126 for(;;) {
4127 struct stat sb;
4128 d = readdir(ffs);
4129 if (!d)
4130 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004131
4132 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4133 continue;
4134 }
4135
bellard81d09122004-07-14 17:21:37 +00004136 if (strstart(d->d_name, file_prefix, NULL)) {
4137 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004138 if (input_path_len < sizeof(file))
4139 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4140 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004141 /* stat the file to find out if it's a directory.
4142 * In that case add a slash to speed up typing long paths
4143 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004144 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004145 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004146 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004147 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004148 }
4149 }
4150 closedir(ffs);
4151}
4152
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004153static const char *next_arg_type(const char *typestr)
4154{
4155 const char *p = strchr(typestr, ':');
4156 return (p != NULL ? ++p : typestr);
4157}
4158
Hani Benhabiles40d19392014-05-07 23:41:30 +01004159static void add_completion_option(ReadLineState *rs, const char *str,
4160 const char *option)
4161{
4162 if (!str || !option) {
4163 return;
4164 }
4165 if (!strncmp(option, str, strlen(str))) {
4166 readline_add_completion(rs, option);
4167 }
4168}
4169
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004170void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4171{
4172 size_t len;
4173 ChardevBackendInfoList *list, *start;
4174
4175 if (nb_args != 2) {
4176 return;
4177 }
4178 len = strlen(str);
4179 readline_set_completion_index(rs, len);
4180
4181 start = list = qmp_query_chardev_backends(NULL);
4182 while (list) {
4183 const char *chr_name = list->value->name;
4184
4185 if (!strncmp(chr_name, str, len)) {
4186 readline_add_completion(rs, chr_name);
4187 }
4188 list = list->next;
4189 }
4190 qapi_free_ChardevBackendInfoList(start);
4191}
4192
Hani Benhabilesb162b492014-05-07 23:41:31 +01004193void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4194{
4195 size_t len;
4196 int i;
4197
4198 if (nb_args != 2) {
4199 return;
4200 }
4201 len = strlen(str);
4202 readline_set_completion_index(rs, len);
4203 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4204 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4205 }
4206}
4207
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004208void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004209{
4210 GSList *list, *elt;
4211 size_t len;
4212
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004213 if (nb_args != 2) {
4214 return;
4215 }
4216
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004217 len = strlen(str);
4218 readline_set_completion_index(rs, len);
4219 list = elt = object_class_get_list(TYPE_DEVICE, false);
4220 while (elt) {
4221 const char *name;
4222 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4223 TYPE_DEVICE);
4224 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004225
4226 if (!dc->cannot_instantiate_with_device_add_yet
4227 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004228 readline_add_completion(rs, name);
4229 }
4230 elt = elt->next;
4231 }
4232 g_slist_free(list);
4233}
4234
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004235void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004236{
4237 GSList *list, *elt;
4238 size_t len;
4239
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004240 if (nb_args != 2) {
4241 return;
4242 }
4243
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004244 len = strlen(str);
4245 readline_set_completion_index(rs, len);
4246 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4247 while (elt) {
4248 const char *name;
4249
4250 name = object_class_get_name(OBJECT_CLASS(elt->data));
4251 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4252 readline_add_completion(rs, name);
4253 }
4254 elt = elt->next;
4255 }
4256 g_slist_free(list);
4257}
4258
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004259static void peripheral_device_del_completion(ReadLineState *rs,
4260 const char *str, size_t len)
4261{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004262 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4263 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004264
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004265 list = qdev_build_hotpluggable_device_list(peripheral);
4266 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004267 return;
4268 }
4269
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004270 for (item = list; item; item = g_slist_next(item)) {
4271 DeviceState *dev = item->data;
4272
4273 if (dev->id && !strncmp(str, dev->id, len)) {
4274 readline_add_completion(rs, dev->id);
4275 }
4276 }
4277
4278 g_slist_free(list);
4279}
4280
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004281void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4282{
4283 size_t len;
4284 ChardevInfoList *list, *start;
4285
4286 if (nb_args != 2) {
4287 return;
4288 }
4289 len = strlen(str);
4290 readline_set_completion_index(rs, len);
4291
4292 start = list = qmp_query_chardev(NULL);
4293 while (list) {
4294 ChardevInfo *chr = list->value;
4295
4296 if (!strncmp(chr->label, str, len)) {
4297 readline_add_completion(rs, chr->label);
4298 }
4299 list = list->next;
4300 }
4301 qapi_free_ChardevInfoList(start);
4302}
4303
Hani Benhabiles8e597772014-05-27 23:39:30 +01004304static void ringbuf_completion(ReadLineState *rs, const char *str)
4305{
4306 size_t len;
4307 ChardevInfoList *list, *start;
4308
4309 len = strlen(str);
4310 readline_set_completion_index(rs, len);
4311
4312 start = list = qmp_query_chardev(NULL);
4313 while (list) {
4314 ChardevInfo *chr_info = list->value;
4315
4316 if (!strncmp(chr_info->label, str, len)) {
4317 CharDriverState *chr = qemu_chr_find(chr_info->label);
4318 if (chr && chr_is_ringbuf(chr)) {
4319 readline_add_completion(rs, chr_info->label);
4320 }
4321 }
4322 list = list->next;
4323 }
4324 qapi_free_ChardevInfoList(start);
4325}
4326
Hani Benhabiles8e597772014-05-27 23:39:30 +01004327void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4328{
4329 if (nb_args != 2) {
4330 return;
4331 }
4332 ringbuf_completion(rs, str);
4333}
4334
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004335void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4336{
4337 size_t len;
4338
4339 if (nb_args != 2) {
4340 return;
4341 }
4342
4343 len = strlen(str);
4344 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004345 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004346}
4347
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004348void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004349{
4350 ObjectPropertyInfoList *list, *start;
4351 size_t len;
4352
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004353 if (nb_args != 2) {
4354 return;
4355 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004356 len = strlen(str);
4357 readline_set_completion_index(rs, len);
4358
4359 start = list = qmp_qom_list("/objects", NULL);
4360 while (list) {
4361 ObjectPropertyInfo *info = list->value;
4362
4363 if (!strncmp(info->type, "child<", 5)
4364 && !strncmp(info->name, str, len)) {
4365 readline_add_completion(rs, info->name);
4366 }
4367 list = list->next;
4368 }
4369 qapi_free_ObjectPropertyInfoList(start);
4370}
4371
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004372void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4373{
4374 int i;
4375 char *sep;
4376 size_t len;
4377
4378 if (nb_args != 2) {
4379 return;
4380 }
4381 sep = strrchr(str, '-');
4382 if (sep) {
4383 str = sep + 1;
4384 }
4385 len = strlen(str);
4386 readline_set_completion_index(rs, len);
4387 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4388 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4389 readline_add_completion(rs, QKeyCode_lookup[i]);
4390 }
4391 }
4392}
4393
Hani Benhabiles40d19392014-05-07 23:41:30 +01004394void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4395{
4396 size_t len;
4397
4398 len = strlen(str);
4399 readline_set_completion_index(rs, len);
4400 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004401 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004402 int count, i;
4403 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004404 NET_CLIENT_OPTIONS_KIND_NONE,
4405 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004406 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004407 const char *name = ncs[i]->name;
4408 if (!strncmp(str, name, len)) {
4409 readline_add_completion(rs, name);
4410 }
4411 }
4412 } else if (nb_args == 3) {
4413 add_completion_option(rs, str, "on");
4414 add_completion_option(rs, str, "off");
4415 }
4416}
4417
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004418void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4419{
4420 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004421 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004422
4423 if (nb_args != 2) {
4424 return;
4425 }
4426
4427 len = strlen(str);
4428 readline_set_completion_index(rs, len);
4429 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004430 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004431 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004432 QemuOpts *opts;
4433 const char *name = ncs[i]->name;
4434 if (strncmp(str, name, len)) {
4435 continue;
4436 }
4437 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4438 if (opts) {
4439 readline_add_completion(rs, name);
4440 }
4441 }
4442}
4443
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004444void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4445{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004446 int i;
4447
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004448 if (nb_args != 2) {
4449 return;
4450 }
4451 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004452 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4453 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4454 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004455}
4456
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004457void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4458 const char *str)
4459{
4460 size_t len;
4461
4462 len = strlen(str);
4463 readline_set_completion_index(rs, len);
4464 if (nb_args == 2) {
4465 int i;
4466 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4467 const char *name = MigrationCapability_lookup[i];
4468 if (!strncmp(str, name, len)) {
4469 readline_add_completion(rs, name);
4470 }
4471 }
4472 } else if (nb_args == 3) {
4473 add_completion_option(rs, str, "on");
4474 add_completion_option(rs, str, "off");
4475 }
4476}
4477
Liang Li50e9a622015-03-23 16:32:29 +08004478void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4479 const char *str)
4480{
4481 size_t len;
4482
4483 len = strlen(str);
4484 readline_set_completion_index(rs, len);
4485 if (nb_args == 2) {
4486 int i;
4487 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4488 const char *name = MigrationParameter_lookup[i];
4489 if (!strncmp(str, name, len)) {
4490 readline_add_completion(rs, name);
4491 }
4492 }
4493 }
4494}
4495
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004496void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4497{
4498 int i;
4499 size_t len;
4500 if (nb_args != 2) {
4501 return;
4502 }
4503 len = strlen(str);
4504 readline_set_completion_index(rs, len);
4505 for (i = 0; host_net_devices[i]; i++) {
4506 if (!strncmp(host_net_devices[i], str, len)) {
4507 readline_add_completion(rs, host_net_devices[i]);
4508 }
4509 }
4510}
4511
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004512void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4513{
Jason Wangeaed4832015-04-23 14:21:38 +08004514 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004515 int count, i, len;
4516
4517 len = strlen(str);
4518 readline_set_completion_index(rs, len);
4519 if (nb_args == 2) {
4520 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004521 NET_CLIENT_OPTIONS_KIND_NONE,
4522 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004523 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004524 int id;
4525 char name[16];
4526
4527 if (net_hub_id_for_client(ncs[i], &id)) {
4528 continue;
4529 }
4530 snprintf(name, sizeof(name), "%d", id);
4531 if (!strncmp(str, name, len)) {
4532 readline_add_completion(rs, name);
4533 }
4534 }
4535 return;
4536 } else if (nb_args == 3) {
4537 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004538 NET_CLIENT_OPTIONS_KIND_NIC,
4539 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004540 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004541 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004542 const char *name;
4543
Jason Wang2c4681f2015-02-02 15:06:38 +08004544 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4545 net_hub_id_for_client(ncs[i], &id)) {
4546 continue;
4547 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004548 name = ncs[i]->name;
4549 if (!strncmp(str, name, len)) {
4550 readline_add_completion(rs, name);
4551 }
4552 }
4553 return;
4554 }
4555}
4556
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004557static void vm_completion(ReadLineState *rs, const char *str)
4558{
4559 size_t len;
4560 BlockDriverState *bs = NULL;
4561
4562 len = strlen(str);
4563 readline_set_completion_index(rs, len);
4564 while ((bs = bdrv_next(bs))) {
4565 SnapshotInfoList *snapshots, *snapshot;
4566
4567 if (!bdrv_can_snapshot(bs)) {
4568 continue;
4569 }
4570 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4571 continue;
4572 }
4573 snapshot = snapshots;
4574 while (snapshot) {
4575 char *completion = snapshot->value->name;
4576 if (!strncmp(str, completion, len)) {
4577 readline_add_completion(rs, completion);
4578 }
4579 completion = snapshot->value->id;
4580 if (!strncmp(str, completion, len)) {
4581 readline_add_completion(rs, completion);
4582 }
4583 snapshot = snapshot->next;
4584 }
4585 qapi_free_SnapshotInfoList(snapshots);
4586 }
4587
4588}
4589
4590void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4591{
4592 if (nb_args == 2) {
4593 vm_completion(rs, str);
4594 }
4595}
4596
4597void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4598{
4599 if (nb_args == 2) {
4600 vm_completion(rs, str);
4601 }
4602}
4603
Wenchao Xiac35b6402013-08-27 20:38:24 +08004604static void monitor_find_completion_by_table(Monitor *mon,
4605 const mon_cmd_t *cmd_table,
4606 char **args,
4607 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004608{
4609 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004610 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004611 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004612 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004613 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004614
bellard81d09122004-07-14 17:21:37 +00004615 if (nb_args <= 1) {
4616 /* command completion */
4617 if (nb_args == 0)
4618 cmdname = "";
4619 else
4620 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004621 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004622 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004623 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004624 }
4625 } else {
4626 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004627 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004628 if (compare_cmd(args[0], cmd->name)) {
4629 break;
4630 }
bellard81d09122004-07-14 17:21:37 +00004631 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004632 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004633 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004634 }
4635
Wenchao Xiad903a772013-08-27 20:38:25 +08004636 if (cmd->sub_table) {
4637 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004638 monitor_find_completion_by_table(mon, cmd->sub_table,
4639 &args[1], nb_args - 1);
4640 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004641 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004642 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004643 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4644 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004645 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004646
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004647 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004648 for(i = 0; i < nb_args - 2; i++) {
4649 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004650 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004651 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004652 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004653 }
4654 }
4655 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004656 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004657 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004658 }
bellard81d09122004-07-14 17:21:37 +00004659 switch(*ptype) {
4660 case 'F':
4661 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004662 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004663 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004664 break;
4665 case 'B':
4666 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004667 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004668 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4669 name = bdrv_get_device_name(bs);
4670 if (str[0] == '\0' ||
4671 !strncmp(name, str, strlen(str))) {
4672 readline_add_completion(mon->rs, name);
4673 }
4674 }
bellard81d09122004-07-14 17:21:37 +00004675 break;
bellard7fe48482004-10-09 18:08:01 +00004676 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004677 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004678 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004679 monitor_find_completion_by_table(mon, cmd_table,
4680 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004681 }
4682 break;
bellard81d09122004-07-14 17:21:37 +00004683 default:
4684 break;
4685 }
4686 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004687}
4688
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004689static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004690 const char *cmdline)
4691{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004692 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004693 char *args[MAX_ARGS];
4694 int nb_args, len;
4695
4696 /* 1. parse the cmdline */
4697 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4698 return;
4699 }
4700#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004701 {
4702 int i;
4703 for (i = 0; i < nb_args; i++) {
4704 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4705 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004706 }
4707#endif
4708
4709 /* if the line ends with a space, it means we want to complete the
4710 next arg */
4711 len = strlen(cmdline);
4712 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4713 if (nb_args >= MAX_ARGS) {
4714 goto cleanup;
4715 }
4716 args[nb_args++] = g_strdup("");
4717 }
4718
4719 /* 2. auto complete according to args */
4720 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004721
4722cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004723 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004724}
4725
aliguori731b0362009-03-05 23:01:42 +00004726static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004727{
aliguori731b0362009-03-05 23:01:42 +00004728 Monitor *mon = opaque;
4729
Jan Kiszkac62313b2009-12-04 14:05:29 +01004730 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004731}
4732
Eric Blake2d5a8342015-04-15 09:19:23 -06004733static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004734{
Eric Blake2d5a8342015-04-15 09:19:23 -06004735 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
4736 if (is_cap && qmp_cmd_mode(mon)) {
4737 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
4738 "Capabilities negotiation is already complete, command "
4739 "'%s' ignored", cmd->name);
4740 return true;
4741 }
4742 if (!is_cap && !qmp_cmd_mode(mon)) {
4743 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
4744 "Expecting capabilities negotiation with "
4745 "'qmp_capabilities' before command '%s'", cmd->name);
4746 return true;
4747 }
4748 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004749}
4750
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004751/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004752 * Argument validation rules:
4753 *
4754 * 1. The argument must exist in cmd_args qdict
4755 * 2. The argument type must be the expected one
4756 *
4757 * Special case: If the argument doesn't exist in cmd_args and
4758 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4759 * checking is skipped for it.
4760 */
4761static int check_client_args_type(const QDict *client_args,
4762 const QDict *cmd_args, int flags)
4763{
4764 const QDictEntry *ent;
4765
4766 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4767 QObject *obj;
4768 QString *arg_type;
4769 const QObject *client_arg = qdict_entry_value(ent);
4770 const char *client_arg_name = qdict_entry_key(ent);
4771
4772 obj = qdict_get(cmd_args, client_arg_name);
4773 if (!obj) {
4774 if (flags & QMP_ACCEPT_UNKNOWNS) {
4775 /* handler accepts unknowns */
4776 continue;
4777 }
4778 /* client arg doesn't exist */
4779 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4780 return -1;
4781 }
4782
4783 arg_type = qobject_to_qstring(obj);
4784 assert(arg_type != NULL);
4785
4786 /* check if argument's type is correct */
4787 switch (qstring_get_str(arg_type)[0]) {
4788 case 'F':
4789 case 'B':
4790 case 's':
4791 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4792 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4793 "string");
4794 return -1;
4795 }
4796 break;
4797 case 'i':
4798 case 'l':
4799 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004800 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004801 if (qobject_type(client_arg) != QTYPE_QINT) {
4802 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4803 "int");
4804 return -1;
4805 }
4806 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004807 case 'T':
4808 if (qobject_type(client_arg) != QTYPE_QINT &&
4809 qobject_type(client_arg) != QTYPE_QFLOAT) {
4810 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4811 "number");
4812 return -1;
4813 }
4814 break;
4815 case 'b':
4816 case '-':
4817 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4818 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4819 "bool");
4820 return -1;
4821 }
4822 break;
4823 case 'O':
4824 assert(flags & QMP_ACCEPT_UNKNOWNS);
4825 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004826 case 'q':
4827 /* Any QObject can be passed. */
4828 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004829 case '/':
4830 case '.':
4831 /*
4832 * These types are not supported by QMP and thus are not
4833 * handled here. Fall through.
4834 */
4835 default:
4836 abort();
4837 }
4838 }
4839
4840 return 0;
4841}
4842
4843/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004844 * - Check if the client has passed all mandatory args
4845 * - Set special flags for argument validation
4846 */
4847static int check_mandatory_args(const QDict *cmd_args,
4848 const QDict *client_args, int *flags)
4849{
4850 const QDictEntry *ent;
4851
4852 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4853 const char *cmd_arg_name = qdict_entry_key(ent);
4854 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4855 assert(type != NULL);
4856
4857 if (qstring_get_str(type)[0] == 'O') {
4858 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4859 *flags |= QMP_ACCEPT_UNKNOWNS;
4860 } else if (qstring_get_str(type)[0] != '-' &&
4861 qstring_get_str(type)[1] != '?' &&
4862 !qdict_haskey(client_args, cmd_arg_name)) {
4863 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4864 return -1;
4865 }
4866 }
4867
4868 return 0;
4869}
4870
4871static QDict *qdict_from_args_type(const char *args_type)
4872{
4873 int i;
4874 QDict *qdict;
4875 QString *key, *type, *cur_qs;
4876
4877 assert(args_type != NULL);
4878
4879 qdict = qdict_new();
4880
4881 if (args_type == NULL || args_type[0] == '\0') {
4882 /* no args, empty qdict */
4883 goto out;
4884 }
4885
4886 key = qstring_new();
4887 type = qstring_new();
4888
4889 cur_qs = key;
4890
4891 for (i = 0;; i++) {
4892 switch (args_type[i]) {
4893 case ',':
4894 case '\0':
4895 qdict_put(qdict, qstring_get_str(key), type);
4896 QDECREF(key);
4897 if (args_type[i] == '\0') {
4898 goto out;
4899 }
4900 type = qstring_new(); /* qdict has ref */
4901 cur_qs = key = qstring_new();
4902 break;
4903 case ':':
4904 cur_qs = type;
4905 break;
4906 default:
4907 qstring_append_chr(cur_qs, args_type[i]);
4908 break;
4909 }
4910 }
4911
4912out:
4913 return qdict;
4914}
4915
4916/*
4917 * Client argument checking rules:
4918 *
4919 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004920 * 2. Each argument provided by the client must be expected
4921 * 3. Each argument provided by the client must have the type expected
4922 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004923 */
4924static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4925{
4926 int flags, err;
4927 QDict *cmd_args;
4928
4929 cmd_args = qdict_from_args_type(cmd->args_type);
4930
4931 flags = 0;
4932 err = check_mandatory_args(cmd_args, client_args, &flags);
4933 if (err) {
4934 goto out;
4935 }
4936
Luiz Capitulino4af91932010-06-22 11:44:05 -03004937 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004938
4939out:
4940 QDECREF(cmd_args);
4941 return err;
4942}
4943
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004944/*
4945 * Input object checking rules
4946 *
4947 * 1. Input object must be a dict
4948 * 2. The "execute" key must exist
4949 * 3. The "execute" key must be a string
4950 * 4. If the "arguments" key exists, it must be a dict
4951 * 5. If the "id" key exists, it can be anything (ie. json-value)
4952 * 6. Any argument not listed above is considered invalid
4953 */
4954static QDict *qmp_check_input_obj(QObject *input_obj)
4955{
4956 const QDictEntry *ent;
4957 int has_exec_key = 0;
4958 QDict *input_dict;
4959
4960 if (qobject_type(input_obj) != QTYPE_QDICT) {
4961 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4962 return NULL;
4963 }
4964
4965 input_dict = qobject_to_qdict(input_obj);
4966
4967 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4968 const char *arg_name = qdict_entry_key(ent);
4969 const QObject *arg_obj = qdict_entry_value(ent);
4970
4971 if (!strcmp(arg_name, "execute")) {
4972 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4973 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4974 "string");
4975 return NULL;
4976 }
4977 has_exec_key = 1;
4978 } else if (!strcmp(arg_name, "arguments")) {
4979 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4980 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4981 "object");
4982 return NULL;
4983 }
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004984 } else {
4985 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4986 return NULL;
4987 }
4988 }
4989
4990 if (!has_exec_key) {
4991 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4992 return NULL;
4993 }
4994
4995 return input_dict;
4996}
4997
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004998static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4999{
5000 int err;
Markus Armbruster84add862015-03-05 16:45:15 +01005001 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005002 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005003 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005004 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005005 Monitor *mon = cur_mon;
5006
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005007 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01005008 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005009
5010 obj = json_parser_parse(tokens, NULL);
5011 if (!obj) {
5012 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01005013 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005014 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005015 }
5016
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005017 input = qmp_check_input_obj(obj);
5018 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005019 qobject_decref(obj);
5020 goto err_out;
5021 }
5022
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005023 mon->mc->id = qdict_get(input, "id");
5024 qobject_incref(mon->mc->id);
5025
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005026 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005027 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005028 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005029 if (!cmd) {
Markus Armbrustera6c90cb2015-01-13 16:16:35 +01005030 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND,
5031 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005032 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005033 }
Eric Blake2d5a8342015-04-15 09:19:23 -06005034 if (invalid_qmp_mode(mon, cmd)) {
5035 goto err_out;
5036 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005037
5038 obj = qdict_get(input, "arguments");
5039 if (!obj) {
5040 args = qdict_new();
5041 } else {
5042 args = qobject_to_qdict(obj);
5043 QINCREF(args);
5044 }
5045
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03005046 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005047 if (err < 0) {
5048 goto err_out;
5049 }
5050
Markus Armbruster84add862015-03-05 16:45:15 +01005051 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5052 /* Command failed... */
5053 if (!monitor_has_error(mon)) {
5054 /* ... without setting an error, so make one up */
5055 qerror_report(QERR_UNDEFINED_ERROR);
5056 }
5057 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005058
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005059err_out:
Markus Armbruster84add862015-03-05 16:45:15 +01005060 monitor_protocol_emitter(mon, data);
5061 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005062 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005063 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005064}
5065
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005066/**
5067 * monitor_control_read(): Read and handle QMP input
5068 */
5069static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
5070{
5071 Monitor *old_mon = cur_mon;
5072
5073 cur_mon = opaque;
5074
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005075 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005076
5077 cur_mon = old_mon;
5078}
5079
aliguori731b0362009-03-05 23:01:42 +00005080static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005081{
aliguori731b0362009-03-05 23:01:42 +00005082 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005083 int i;
aliguori376253e2009-03-05 23:01:23 +00005084
aliguori731b0362009-03-05 23:01:42 +00005085 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005086
aliguoricde76ee2009-03-05 23:01:51 +00005087 if (cur_mon->rs) {
5088 for (i = 0; i < size; i++)
5089 readline_handle_byte(cur_mon->rs, buf[i]);
5090 } else {
5091 if (size == 0 || buf[size - 1] != 0)
5092 monitor_printf(cur_mon, "corrupted command\n");
5093 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005094 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005095 }
aliguori731b0362009-03-05 23:01:42 +00005096
5097 cur_mon = old_mon;
5098}
aliguorid8f44602008-10-06 13:52:44 +00005099
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005100static void monitor_command_cb(void *opaque, const char *cmdline,
5101 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005102{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005103 Monitor *mon = opaque;
5104
aliguori731b0362009-03-05 23:01:42 +00005105 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02005106 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005107 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005108}
5109
aliguoricde76ee2009-03-05 23:01:51 +00005110int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005111{
aliguoricde76ee2009-03-05 23:01:51 +00005112 if (!mon->rs)
5113 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005114 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005115 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005116}
5117
aliguori376253e2009-03-05 23:01:23 +00005118void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005119{
aliguoricde76ee2009-03-05 23:01:51 +00005120 if (!mon->rs)
5121 return;
aliguori731b0362009-03-05 23:01:42 +00005122 if (--mon->suspend_cnt == 0)
5123 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005124}
5125
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005126static QObject *get_qmp_greeting(void)
5127{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005128 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005129
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005130 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005131 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5132}
5133
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005134/**
5135 * monitor_control_event(): Print QMP gretting
5136 */
5137static void monitor_control_event(void *opaque, int event)
5138{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005139 QObject *data;
5140 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005141
Luiz Capitulino47116d12010-02-08 17:01:30 -02005142 switch (event) {
5143 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02005144 mon->mc->command_mode = 0;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005145 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005146 monitor_json_emitter(mon, data);
5147 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005148 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005149 break;
5150 case CHR_EVENT_CLOSED:
5151 json_message_parser_destroy(&mon->mc->parser);
Anthony Liguori58617a72012-08-23 08:03:21 -05005152 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005153 mon_refcount--;
5154 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005155 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005156 }
5157}
5158
aliguori731b0362009-03-05 23:01:42 +00005159static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005160{
aliguori376253e2009-03-05 23:01:23 +00005161 Monitor *mon = opaque;
5162
aliguori2724b182009-03-05 23:01:47 +00005163 switch (event) {
5164 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005165 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005166 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005167 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005168 if (mon->reset_seen) {
5169 readline_restart(mon->rs);
5170 monitor_resume(mon);
5171 monitor_flush(mon);
5172 } else {
5173 mon->suspend_cnt = 0;
5174 }
aliguori2724b182009-03-05 23:01:47 +00005175 break;
ths86e94de2007-01-05 22:01:59 +00005176
aliguori2724b182009-03-05 23:01:47 +00005177 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005178 if (mon->reset_seen) {
5179 if (mon->suspend_cnt == 0) {
5180 monitor_printf(mon, "\n");
5181 }
5182 monitor_flush(mon);
5183 monitor_suspend(mon);
5184 } else {
5185 mon->suspend_cnt++;
5186 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005187 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005188 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005189 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005190 break;
5191
Amit Shahb6b8df52009-10-07 18:31:16 +05305192 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005193 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5194 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005195 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005196 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005197 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005198 }
5199 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005200 mon_refcount++;
5201 break;
5202
5203 case CHR_EVENT_CLOSED:
5204 mon_refcount--;
5205 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005206 break;
5207 }
ths86e94de2007-01-05 22:01:59 +00005208}
5209
Wayne Xia816f8922011-10-12 11:32:41 +08005210static int
5211compare_mon_cmd(const void *a, const void *b)
5212{
5213 return strcmp(((const mon_cmd_t *)a)->name,
5214 ((const mon_cmd_t *)b)->name);
5215}
5216
5217static void sortcmdlist(void)
5218{
5219 int array_num;
5220 int elem_size = sizeof(mon_cmd_t);
5221
5222 array_num = sizeof(mon_cmds)/elem_size-1;
5223 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5224
5225 array_num = sizeof(info_cmds)/elem_size-1;
5226 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5227}
5228
aliguori76655d62009-03-06 20:27:37 +00005229
5230/*
5231 * Local variables:
5232 * c-indent-level: 4
5233 * c-basic-offset: 4
5234 * tab-width: 8
5235 * End:
5236 */
5237
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005238/* These functions just adapt the readline interface in a typesafe way. We
5239 * could cast function pointers but that discards compiler checks.
5240 */
Stefan Weild5d15072014-01-25 18:18:23 +01005241static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5242 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005243{
5244 va_list ap;
5245 va_start(ap, fmt);
5246 monitor_vprintf(opaque, fmt, ap);
5247 va_end(ap);
5248}
5249
5250static void monitor_readline_flush(void *opaque)
5251{
5252 monitor_flush(opaque);
5253}
5254
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005255static void __attribute__((constructor)) monitor_lock_init(void)
5256{
5257 qemu_mutex_init(&monitor_lock);
5258}
5259
aliguori731b0362009-03-05 23:01:42 +00005260void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005261{
aliguori731b0362009-03-05 23:01:42 +00005262 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005263 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005264
5265 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005266 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005267 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005268 is_first_init = 0;
5269 }
aliguori87127162009-03-05 23:01:29 +00005270
Wenchao Xiab01fe892013-08-27 20:38:19 +08005271 mon = g_malloc(sizeof(*mon));
5272 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005273
aliguori87127162009-03-05 23:01:29 +00005274 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005275 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005276 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005277 mon->rs = readline_init(monitor_readline_printf,
5278 monitor_readline_flush,
5279 mon,
5280 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005281 monitor_read_command(mon, 0);
5282 }
aliguori87127162009-03-05 23:01:29 +00005283
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005284 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05005285 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005286 /* Control mode requires special handlers */
5287 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5288 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005289 qemu_chr_fe_set_echo(chr, true);
Anthony Liguori26efaca2012-08-23 13:49:02 -05005290
5291 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005292 } else {
5293 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5294 monitor_event, mon);
5295 }
aliguori87127162009-03-05 23:01:29 +00005296
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005297 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005298 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005299 qemu_mutex_unlock(&monitor_lock);
5300
Markus Armbruster8631b602010-02-18 11:41:55 +01005301 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5302 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005303}
5304
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005305static void bdrv_password_cb(void *opaque, const char *password,
5306 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005307{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005308 Monitor *mon = opaque;
5309 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005310 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005311 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005312
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005313 bdrv_add_key(bs, password, &local_err);
5314 if (local_err) {
5315 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5316 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005317 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005318 }
aliguori731b0362009-03-05 23:01:42 +00005319 if (mon->password_completion_cb)
5320 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005321
aliguori731b0362009-03-05 23:01:42 +00005322 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005323}
aliguoric0f4ce72009-03-05 23:01:01 +00005324
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005325int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005326 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005327 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005328{
aliguoricde76ee2009-03-05 23:01:51 +00005329 int err;
5330
aliguori376253e2009-03-05 23:01:23 +00005331 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5332 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005333
aliguori731b0362009-03-05 23:01:42 +00005334 mon->password_completion_cb = completion_cb;
5335 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005336
aliguoricde76ee2009-03-05 23:01:51 +00005337 err = monitor_read_password(mon, bdrv_password_cb, bs);
5338
5339 if (err && completion_cb)
5340 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005341
5342 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005343}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005344
5345int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005346 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005347 void *opaque)
5348{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005349 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005350 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005351
Fam Zheng55606252015-03-02 19:36:46 +08005352 blk = blk_by_name(device);
5353 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005354 monitor_printf(mon, "Device not found %s\n", device);
5355 return -1;
5356 }
5357
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005358 bdrv_add_key(blk_bs(blk), NULL, &err);
5359 if (err) {
5360 error_free(err);
5361 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5362 }
5363
5364 if (completion_cb) {
5365 completion_cb(opaque, 0);
5366 }
5367 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005368}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005369
5370QemuOptsList qemu_mon_opts = {
5371 .name = "mon",
5372 .implied_opt_name = "chardev",
5373 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5374 .desc = {
5375 {
5376 .name = "mode",
5377 .type = QEMU_OPT_STRING,
5378 },{
5379 .name = "chardev",
5380 .type = QEMU_OPT_STRING,
5381 },{
5382 .name = "default",
5383 .type = QEMU_OPT_BOOL,
5384 },{
5385 .name = "pretty",
5386 .type = QEMU_OPT_BOOL,
5387 },
5388 { /* end of list */ }
5389 },
5390};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005391
5392#ifndef TARGET_I386
5393void qmp_rtc_reset_reinjection(Error **errp)
5394{
5395 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5396}
5397#endif