blob: 4cdc873922c8ce36f71348ed24c08c818bfe4e6b [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
bellard9307c4c2004-04-04 12:57:25 +000084/*
85 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000086 *
bellard9307c4c2004-04-04 12:57:25 +000087 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000088 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000089 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080090 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010091 * 'O' option string of the form NAME=VALUE,...
92 * parsed according to QemuOptsList given by its name
93 * Example: 'device:O' uses qemu_device_opts.
94 * Restriction: only lists with empty desc are supported
95 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000096 * 'i' 32 bit integer
97 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030098 * 'M' Non-negative target long (32 or 64 bit), in user mode the
99 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200100 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200101 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
102 * K, k suffix, which multiplies the value by 2^60 for suffixes E
103 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
104 * 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 +0100105 * 'T' double
106 * user mode accepts an optional ms, us, ns suffix,
107 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000108 * '/' optional gdb-like print format (like "/10x")
109 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300110 * '?' optional type (for all types, except '/')
111 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100112 * 'b' boolean
113 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300114 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000115 *
116 */
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300123 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300124 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200125 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800127 /* @sub_table is a list of 2nd level of commands. If it do not exist,
128 * mhandler should be used. If it exist, sub_table[?].mhandler should be
129 * used, and mhandler of 1st level plays the role of help function.
130 */
131 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100132 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Corey Bryantba1c0482012-08-14 16:43:43 -0400143/* file descriptor associated with a file descriptor set */
144typedef struct MonFdsetFd MonFdsetFd;
145struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
150};
151
152/* file descriptor set containing fds passed via SCM_RIGHTS */
153typedef struct MonFdset MonFdset;
154struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400157 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400158 QLIST_ENTRY(MonFdset) next;
159};
160
Markus Armbruster74358f22015-03-06 19:35:59 +0100161typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200162 QObject *id;
163 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100164 /*
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
167 * mode.
168 */
169 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100170} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200171
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100172/*
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
176 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200177typedef struct MonitorQAPIEventState {
178 QAPIEvent event; /* Event being tracked */
179 int64_t rate; /* Minimum time (in ns) between two events */
180 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100181 QEMUTimer *timer; /* Timer for handling delayed events */
182 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200183} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100184
aliguori87127162009-03-05 23:01:29 +0000185struct Monitor {
186 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200187 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000188 int flags;
189 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400190 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200191
192 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400193 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200194 guint out_watch;
195
196 /* Read under either BQL or out_lock, written with BQL+out_lock. */
197 int mux_out;
198
aliguori731b0362009-03-05 23:01:42 +0000199 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100200 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200201 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200202 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000203 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800204 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200205 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500206 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000207 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000208};
209
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300210/* QMP checker flags */
211#define QMP_ACCEPT_UNKNOWNS 1
212
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200213/* Protects mon_list, monitor_event_state. */
214static QemuMutex monitor_lock;
215
Blue Swirl72cf2d42009-09-12 07:36:22 +0000216static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400217static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400218static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000219
Wayne Xia816f8922011-10-12 11:32:41 +0800220static mon_cmd_t mon_cmds[];
221static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000222
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300223static const mon_cmd_t qmp_cmds[];
224
Markus Armbruster8631b602010-02-18 11:41:55 +0100225Monitor *cur_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
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100230/**
231 * Is @mon a QMP monitor?
232 */
233static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200234{
235 return (mon->flags & MONITOR_USE_CONTROL);
236}
237
Markus Armbruster489653b2015-03-06 20:01:05 +0100238/**
239 * Is the current monitor, if any, a QMP monitor?
240 */
241bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100242{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100243 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100244}
245
Anthony Liguori7060b472011-09-02 12:34:50 -0500246void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000247{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200248 if (!mon->rs)
249 return;
250
aliguori731b0362009-03-05 23:01:42 +0000251 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
252 if (show_prompt)
253 readline_show_prompt(mon->rs);
254}
bellard6a00d602005-11-21 23:25:50 +0000255
Anthony Liguori7060b472011-09-02 12:34:50 -0500256int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
257 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000258{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100259 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000260 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
261 /* prompt is printed on return from the command handler */
262 return 0;
263 } else {
264 monitor_printf(mon, "terminal does not support password prompting\n");
265 return -ENOTTY;
266 }
aliguoribb5fc202009-03-05 23:01:15 +0000267}
268
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200269static void monitor_flush_locked(Monitor *mon);
270
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100271static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
272 void *opaque)
273{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200274 Monitor *mon = opaque;
275
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200276 qemu_mutex_lock(&mon->out_lock);
277 mon->out_watch = 0;
278 monitor_flush_locked(mon);
279 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 return FALSE;
281}
282
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200283/* Called with mon->out_lock held. */
284static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000285{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400287 size_t len;
288 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400290 if (mon->skip_flush) {
291 return;
292 }
293
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 buf = qstring_get_str(mon->outbuf);
295 len = qstring_get_length(mon->outbuf);
296
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200297 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200299 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
300 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 QDECREF(mon->outbuf);
302 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100303 return;
304 }
305 if (rc > 0) {
306 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 QString *tmp = qstring_from_str(buf + rc);
308 QDECREF(mon->outbuf);
309 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100310 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200311 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200312 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200313 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200314 }
bellard7e2515e2004-08-01 21:52:19 +0000315 }
316}
317
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318void monitor_flush(Monitor *mon)
319{
320 qemu_mutex_lock(&mon->out_lock);
321 monitor_flush_locked(mon);
322 qemu_mutex_unlock(&mon->out_lock);
323}
324
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400325/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000326static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000327{
ths60fe76f2007-12-16 03:02:09 +0000328 char c;
aliguori731b0362009-03-05 23:01:42 +0000329
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200330 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000331 for(;;) {
332 c = *str++;
333 if (c == '\0')
334 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400335 if (c == '\n') {
336 qstring_append_chr(mon->outbuf, '\r');
337 }
338 qstring_append_chr(mon->outbuf, c);
339 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200340 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400341 }
bellard7e2515e2004-08-01 21:52:19 +0000342 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000344}
345
aliguori376253e2009-03-05 23:01:23 +0000346void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000347{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200349
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200350 if (!mon)
351 return;
352
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100353 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200355 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400357 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000360}
361
aliguori376253e2009-03-05 23:01:23 +0000362void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000363{
364 va_list ap;
365 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000366 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000367 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000368}
369
Stefan Weil8b7968f2010-09-23 21:28:05 +0200370static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
371 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000372{
373 va_list ap;
374 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000376 va_end(ap);
377 return 0;
378}
379
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200380static void monitor_json_emitter(Monitor *mon, const QObject *data)
381{
382 QString *json;
383
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200384 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
385 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200386 assert(json != NULL);
387
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200388 qstring_append_chr(json, '\n');
389 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200390
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 QDECREF(json);
392}
393
Markus Armbruster710aec92015-03-06 11:28:00 +0100394static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300395{
396 QObject *obj;
397
Markus Armbruster710aec92015-03-06 11:28:00 +0100398 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
399 ErrorClass_lookup[error_get_class(err)],
400 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300401
402 return qobject_to_qdict(obj);
403}
404
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100405static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100406 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200407{
408 QDict *qmp;
409
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100410 trace_monitor_protocol_emitter(mon);
411
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100412 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200413 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300414 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200415 if (data) {
416 qobject_incref(data);
417 qdict_put_obj(qmp, "return", data);
418 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200419 /* return an empty QDict by default */
420 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421 }
422 } else {
423 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100424 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426
Markus Armbruster74358f22015-03-06 19:35:59 +0100427 if (mon->qmp.id) {
428 qdict_put_obj(qmp, "id", mon->qmp.id);
429 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200430 }
431
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200432 monitor_json_emitter(mon, QOBJECT(qmp));
433 QDECREF(qmp);
434}
435
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200436
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200437static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100438
439/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200440 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200441 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200443static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444{
445 Monitor *mon;
446
447 trace_monitor_protocol_event_emit(event, data);
448 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100449 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100450 monitor_json_emitter(mon, data);
451 }
452 }
453}
454
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455/*
456 * Queue a new event for emission to Monitor instances,
457 * applying any rate limiting if required.
458 */
459static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200460monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100461{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200462 MonitorQAPIEventState *evstate;
463 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100464 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100467 trace_monitor_protocol_event_queue(event,
468 data,
469 evstate->rate,
470 evstate->last,
471 now);
472
473 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200474 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100475 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200476 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477 evstate->last = now;
478 } else {
479 int64_t delta = now - evstate->last;
480 if (evstate->data ||
481 delta < evstate->rate) {
482 /* If there's an existing event pending, replace
483 * it with the new event, otherwise schedule a
484 * timer for delayed emission
485 */
486 if (evstate->data) {
487 qobject_decref(evstate->data);
488 } else {
489 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100490 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200492 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493 qobject_incref(evstate->data);
494 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200495 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 evstate->last = now;
497 }
498 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200499 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500}
501
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502/*
503 * The callback invoked by QemuTimer when a delayed
504 * event is ready to be emitted
505 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200506static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200508 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100509 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511 trace_monitor_protocol_event_handler(evstate->event,
512 evstate->data,
513 evstate->last,
514 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200515 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200517 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518 qobject_decref(evstate->data);
519 evstate->data = NULL;
520 }
521 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200522 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100523}
524
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525/*
526 * @event: the event ID to be limited
527 * @rate: the rate limit in milliseconds
528 *
529 * Sets a rate limit on a particular event, so no
530 * more than 1 event will be emitted within @rate
531 * milliseconds
532 */
533static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200534monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100535{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200536 MonitorQAPIEventState *evstate;
537 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100538
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200539 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540
541 trace_monitor_protocol_event_throttle(event, rate);
542 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700543 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545 evstate->last = 0;
546 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200547 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
548 SCALE_MS,
549 monitor_qapi_event_handler,
550 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100551}
552
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200553static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200555 /* Limit guest-triggerable events to 1 per second */
556 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200557 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200558 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
560 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100562
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200563 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200564}
565
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200566static int do_qmp_capabilities(Monitor *mon, const QDict *params,
567 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200568{
Markus Armbrusterf994b252015-03-06 19:51:51 +0100569 mon->qmp.in_command_mode = true;
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200570 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200571}
572
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100573static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200574
Wenchao Xiab01fe892013-08-27 20:38:19 +0800575static void monitor_data_init(Monitor *mon)
576{
577 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200578 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800579 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800580 /* Use *mon_cmds by default. */
581 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800582}
583
584static void monitor_data_destroy(Monitor *mon)
585{
586 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200587 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800588}
589
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200590char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
591 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200592{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595
Wenchao Xiab01fe892013-08-27 20:38:19 +0800596 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400597 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
599 old_mon = cur_mon;
600 cur_mon = &hmp;
601
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200602 if (has_cpu_index) {
603 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200604 if (ret < 0) {
605 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100606 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
607 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608 goto out;
609 }
610 }
611
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100612 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200613 cur_mon = old_mon;
614
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200615 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400616 if (qstring_get_length(hmp.outbuf) > 0) {
617 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200618 } else {
619 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200620 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200621 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622
623out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800624 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200625 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626}
627
bellard9dc39cb2004-03-14 21:38:27 +0000628static int compare_cmd(const char *name, const char *list)
629{
630 const char *p, *pstart;
631 int len;
632 len = strlen(name);
633 p = list;
634 for(;;) {
635 pstart = p;
636 p = strchr(p, '|');
637 if (!p)
638 p = pstart + strlen(pstart);
639 if ((p - pstart) == len && !memcmp(pstart, name, len))
640 return 1;
641 if (*p == '\0')
642 break;
643 p++;
644 }
645 return 0;
646}
647
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800648static int get_str(char *buf, int buf_size, const char **pp)
649{
650 const char *p;
651 char *q;
652 int c;
653
654 q = buf;
655 p = *pp;
656 while (qemu_isspace(*p)) {
657 p++;
658 }
659 if (*p == '\0') {
660 fail:
661 *q = '\0';
662 *pp = p;
663 return -1;
664 }
665 if (*p == '\"') {
666 p++;
667 while (*p != '\0' && *p != '\"') {
668 if (*p == '\\') {
669 p++;
670 c = *p++;
671 switch (c) {
672 case 'n':
673 c = '\n';
674 break;
675 case 'r':
676 c = '\r';
677 break;
678 case '\\':
679 case '\'':
680 case '\"':
681 break;
682 default:
683 qemu_printf("unsupported escape code: '\\%c'\n", c);
684 goto fail;
685 }
686 if ((q - buf) < buf_size - 1) {
687 *q++ = c;
688 }
689 } else {
690 if ((q - buf) < buf_size - 1) {
691 *q++ = *p;
692 }
693 p++;
694 }
695 }
696 if (*p != '\"') {
697 qemu_printf("unterminated string\n");
698 goto fail;
699 }
700 p++;
701 } else {
702 while (*p != '\0' && !qemu_isspace(*p)) {
703 if ((q - buf) < buf_size - 1) {
704 *q++ = *p;
705 }
706 p++;
707 }
708 }
709 *q = '\0';
710 *pp = p;
711 return 0;
712}
713
714#define MAX_ARGS 16
715
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800716static void free_cmdline_args(char **args, int nb_args)
717{
718 int i;
719
720 assert(nb_args <= MAX_ARGS);
721
722 for (i = 0; i < nb_args; i++) {
723 g_free(args[i]);
724 }
725
726}
727
728/*
729 * Parse the command line to get valid args.
730 * @cmdline: command line to be parsed.
731 * @pnb_args: location to store the number of args, must NOT be NULL.
732 * @args: location to store the args, which should be freed by caller, must
733 * NOT be NULL.
734 *
735 * Returns 0 on success, negative on failure.
736 *
737 * NOTE: this parser is an approximate form of the real command parser. Number
738 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
739 * return with failure.
740 */
741static int parse_cmdline(const char *cmdline,
742 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800743{
744 const char *p;
745 int nb_args, ret;
746 char buf[1024];
747
748 p = cmdline;
749 nb_args = 0;
750 for (;;) {
751 while (qemu_isspace(*p)) {
752 p++;
753 }
754 if (*p == '\0') {
755 break;
756 }
757 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800758 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800759 }
760 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 if (ret < 0) {
762 goto fail;
763 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800764 args[nb_args] = g_strdup(buf);
765 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 }
767 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800768 return 0;
769
770 fail:
771 free_cmdline_args(args, nb_args);
772 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800773}
774
Wenchao Xia66855492013-08-27 20:38:23 +0800775static void help_cmd_dump_one(Monitor *mon,
776 const mon_cmd_t *cmd,
777 char **prefix_args,
778 int prefix_args_nb)
779{
780 int i;
781
782 for (i = 0; i < prefix_args_nb; i++) {
783 monitor_printf(mon, "%s ", prefix_args[i]);
784 }
785 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
786}
787
788/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500789static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800790 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000791{
Anthony Liguoric227f092009-10-01 16:12:16 -0500792 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000793
Wenchao Xia66855492013-08-27 20:38:23 +0800794 /* No valid arg need to compare with, dump all in *cmds */
795 if (arg_index >= nb_args) {
796 for (cmd = cmds; cmd->name != NULL; cmd++) {
797 help_cmd_dump_one(mon, cmd, args, arg_index);
798 }
799 return;
800 }
801
802 /* Find one entry to dump */
803 for (cmd = cmds; cmd->name != NULL; cmd++) {
804 if (compare_cmd(args[arg_index], cmd->name)) {
805 if (cmd->sub_table) {
806 /* continue with next arg */
807 help_cmd_dump(mon, cmd->sub_table,
808 args, nb_args, arg_index + 1);
809 } else {
810 help_cmd_dump_one(mon, cmd, args, arg_index);
811 }
812 break;
813 }
bellard9dc39cb2004-03-14 21:38:27 +0000814 }
815}
816
aliguori376253e2009-03-05 23:01:23 +0000817static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000818{
Wenchao Xia66855492013-08-27 20:38:23 +0800819 char *args[MAX_ARGS];
820 int nb_args = 0;
821
822 /* 1. parse user input */
823 if (name) {
824 /* special case for log, directly dump and return */
825 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000826 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "Log items (comma separated):\n");
828 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000831 }
Wenchao Xia66855492013-08-27 20:38:23 +0800832 return;
833 }
834
835 if (parse_cmdline(name, &nb_args, args) < 0) {
836 return;
bellardf193c792004-03-21 17:06:25 +0000837 }
bellard9dc39cb2004-03-14 21:38:27 +0000838 }
Wenchao Xia66855492013-08-27 20:38:23 +0800839
840 /* 2. dump the contents according to parsed args */
841 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
842
843 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000844}
845
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300846static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300847{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300849}
850
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100851static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530852{
853 const char *tp_name = qdict_get_str(qdict, "name");
854 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200855 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000856
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
858 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100859 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000860 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530861}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100862
Michael Rothc45a8162011-10-02 08:44:37 -0500863#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100864static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100865{
866 const char *op = qdict_get_try_str(qdict, "op");
867 const char *arg = qdict_get_try_str(qdict, "arg");
868
869 if (!op) {
870 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
871 } else if (!strcmp(op, "on")) {
872 st_set_trace_file_enabled(true);
873 } else if (!strcmp(op, "off")) {
874 st_set_trace_file_enabled(false);
875 } else if (!strcmp(op, "flush")) {
876 st_flush_trace_buffer();
877 } else if (!strcmp(op, "set")) {
878 if (arg) {
879 st_set_trace_file(arg);
880 }
881 } else {
882 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
883 help_cmd(mon, "trace-file");
884 }
885}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530886#endif
887
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100888static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000889{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300890 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000891}
892
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300893CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000894{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300895 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200896 const mon_cmd_t *cmd;
897
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300898 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200899 info = g_malloc0(sizeof(*info));
900 info->value = g_malloc0(sizeof(*info->value));
901 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200902
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300903 info->next = cmd_list;
904 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905 }
906
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300907 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000908}
909
Daniel P. Berrange48608532012-05-21 17:59:51 +0100910EventInfoList *qmp_query_events(Error **errp)
911{
912 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200913 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100914
Wenchao Xia75175172014-06-18 08:43:54 +0200915 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
916 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100917 assert(event_name != NULL);
918 info = g_malloc0(sizeof(*info));
919 info->value = g_malloc0(sizeof(*info->value));
920 info->value->name = g_strdup(event_name);
921
922 info->next = ev_list;
923 ev_list = info;
924 }
925
926 return ev_list;
927}
928
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300929/* set the current CPU defined by the user */
930int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000931{
Andreas Färber55e5c282012-12-17 06:18:02 +0100932 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000933
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100934 cpu = qemu_get_cpu(cpu_index);
935 if (cpu == NULL) {
936 return -1;
bellard6a00d602005-11-21 23:25:50 +0000937 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200938 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100939 return 0;
bellard6a00d602005-11-21 23:25:50 +0000940}
941
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700942static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000943{
aliguori731b0362009-03-05 23:01:42 +0000944 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300945 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000946 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300947 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700948 return cur_mon->mon_cpu;
949}
950
951static CPUArchState *mon_get_cpu_env(void)
952{
953 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000954}
955
Luiz Capitulino99b77962011-10-24 10:53:44 -0200956int monitor_get_cpu_index(void)
957{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700958 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200959}
960
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100961static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000962{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700963 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000964}
965
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100966static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000967{
aliguori376253e2009-03-05 23:01:23 +0000968 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200969 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000970}
971
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100972static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300973{
974 dump_opcount_info((FILE *)mon, monitor_fprintf);
975}
976
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100977static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000978{
979 int i;
bellard7e2515e2004-08-01 21:52:19 +0000980 const char *str;
ths3b46e622007-09-17 08:09:54 +0000981
aliguoricde76ee2009-03-05 23:01:51 +0000982 if (!mon->rs)
983 return;
bellard7e2515e2004-08-01 21:52:19 +0000984 i = 0;
985 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000986 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000987 if (!str)
988 break;
aliguori376253e2009-03-05 23:01:23 +0000989 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000990 i++;
bellardaa455482004-04-04 13:07:25 +0000991 }
992}
993
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100994static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000995{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700996 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000997}
j_mayer76a66252007-03-07 08:32:30 +0000998
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100999static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301000{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001001 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1002 TraceEventInfoList *elem;
1003
1004 for (elem = events; elem != NULL; elem = elem->next) {
1005 monitor_printf(mon, "%s : state %u\n",
1006 elem->value->name,
1007 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1008 }
1009 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301010}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301011
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001012void qmp_client_migrate_info(const char *protocol, const char *hostname,
1013 bool has_port, int64_t port,
1014 bool has_tls_port, int64_t tls_port,
1015 bool has_cert_subject, const char *cert_subject,
1016 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001017{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001018 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001019 if (!qemu_using_spice(errp)) {
1020 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001021 }
1022
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001023 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001024 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001025 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001026 }
1027
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001028 if (qemu_spice_migrate_info(hostname,
1029 has_port ? port : -1,
1030 has_tls_port ? tls_port : -1,
1031 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001032 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001033 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001034 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001035 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001036 }
1037
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001038 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001039}
1040
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001041static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001042{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001043 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001044}
1045
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001046static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001047{
1048 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001049 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001050
bellard9307c4c2004-04-04 12:57:25 +00001051 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001052 mask = 0;
1053 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001054 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001055 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001056 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001057 return;
1058 }
1059 }
Peter Maydell24537a02013-02-11 16:41:23 +00001060 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001061}
1062
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001063static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001064{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001065 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001066 if (!option || !strcmp(option, "on")) {
1067 singlestep = 1;
1068 } else if (!strcmp(option, "off")) {
1069 singlestep = 0;
1070 } else {
1071 monitor_printf(mon, "unexpected option %s\n", option);
1072 }
1073}
1074
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001075static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001076{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001077 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001078 if (!device)
1079 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1080 if (gdbserver_start(device) < 0) {
1081 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1082 device);
1083 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001084 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001085 } else {
aliguori59030a82009-04-05 18:43:41 +00001086 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1087 device);
bellard8a7ddc32004-03-31 19:00:16 +00001088 }
1089}
1090
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001091static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001092{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001093 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001094 if (select_watchdog_action(action) == -1) {
1095 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1096 }
1097}
1098
aliguori376253e2009-03-05 23:01:23 +00001099static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001100{
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001102 switch(c) {
1103 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001105 break;
1106 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001107 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001108 break;
1109 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001111 break;
1112 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001114 break;
1115 default:
1116 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001117 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001118 } else {
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001120 }
1121 break;
1122 }
aliguori376253e2009-03-05 23:01:23 +00001123 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001124}
1125
aliguori376253e2009-03-05 23:01:23 +00001126static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001127 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001128{
Blue Swirl23842aa2010-01-12 20:27:43 +00001129 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001130 uint8_t buf[16];
1131 uint64_t v;
1132
1133 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001134 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001135#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001136 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001137 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001138 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001139 } else if (wsize == 4) {
1140 flags = 0;
1141 } else {
bellard6a15fd12006-04-12 21:07:07 +00001142 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001143 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001144 if (env) {
1145#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001146 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001147 (env->segs[R_CS].flags & DESC_L_MASK))
1148 flags = 2;
1149 else
1150#endif
1151 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1152 flags = 1;
1153 }
bellard4c27ba22004-04-25 18:05:08 +00001154 }
1155#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001156#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001157 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001158 flags = msr_le << 16;
1159 flags |= env->bfd_mach;
1160#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001161 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001162 return;
1163 }
1164
1165 len = wsize * count;
1166 if (wsize == 1)
1167 line_size = 8;
1168 else
1169 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001170 max_digits = 0;
1171
1172 switch(format) {
1173 case 'o':
1174 max_digits = (wsize * 8 + 2) / 3;
1175 break;
1176 default:
1177 case 'x':
1178 max_digits = (wsize * 8) / 4;
1179 break;
1180 case 'u':
1181 case 'd':
1182 max_digits = (wsize * 8 * 10 + 32) / 33;
1183 break;
1184 case 'c':
1185 wsize = 1;
1186 break;
1187 }
1188
1189 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001190 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001191 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001192 else
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001194 l = len;
1195 if (l > line_size)
1196 l = line_size;
1197 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001198 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001199 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001200 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001202 break;
1203 }
bellard9307c4c2004-04-04 12:57:25 +00001204 }
ths5fafdf22007-09-16 21:08:06 +00001205 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001206 while (i < l) {
1207 switch(wsize) {
1208 default:
1209 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001210 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001211 break;
1212 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001213 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001214 break;
1215 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001216 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001217 break;
1218 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001219 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 }
aliguori376253e2009-03-05 23:01:23 +00001222 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001223 switch(format) {
1224 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001226 break;
1227 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001228 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001232 break;
1233 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001238 break;
1239 }
1240 i += wsize;
1241 }
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001243 addr += l;
1244 len -= l;
1245 }
1246}
1247
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001248static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001249{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001250 int count = qdict_get_int(qdict, "count");
1251 int format = qdict_get_int(qdict, "format");
1252 int size = qdict_get_int(qdict, "size");
1253 target_long addr = qdict_get_int(qdict, "addr");
1254
aliguori376253e2009-03-05 23:01:23 +00001255 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001256}
1257
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001258static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001259{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001260 int count = qdict_get_int(qdict, "count");
1261 int format = qdict_get_int(qdict, "format");
1262 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001263 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001264
aliguori376253e2009-03-05 23:01:23 +00001265 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001266}
1267
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001268static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001269{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001270 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001271 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001272
bellard9307c4c2004-04-04 12:57:25 +00001273 switch(format) {
1274 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001275 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001278 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001281 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 default:
1284 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001285 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 }
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001292}
1293
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001294static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001295{
1296 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001297 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001298 uint32_t start = qdict_get_int(qdict, "start");
1299 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001300
1301 sum = 0;
1302 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001303 uint8_t val = address_space_ldub(&address_space_memory, addr,
1304 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001305 /* BSD sum algorithm ('sum' Unix command) */
1306 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001307 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001308 }
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001310}
1311
bellard13224a82006-07-14 22:03:35 +00001312static int mouse_button_state;
1313
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001314static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001315{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001316 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001317 const char *dx_str = qdict_get_str(qdict, "dx_str");
1318 const char *dy_str = qdict_get_str(qdict, "dy_str");
1319 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001320
bellard13224a82006-07-14 22:03:35 +00001321 dx = strtol(dx_str, NULL, 0);
1322 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001323 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1324 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1325
1326 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001327 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001328 if (dz != 0) {
1329 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1330 qemu_input_queue_btn(NULL, button, true);
1331 qemu_input_event_sync();
1332 qemu_input_queue_btn(NULL, button, false);
1333 }
1334 }
1335 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001336}
1337
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001338static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001339{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001340 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1341 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1342 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1343 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1344 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001345 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001346
1347 if (mouse_button_state == button_state) {
1348 return;
1349 }
1350 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1351 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001352 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001353}
1354
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001355static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001356{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001357 int size = qdict_get_int(qdict, "size");
1358 int addr = qdict_get_int(qdict, "addr");
1359 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001360 uint32_t val;
1361 int suffix;
1362
1363 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001364 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001365 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001366 addr++;
1367 }
1368 addr &= 0xffff;
1369
1370 switch(size) {
1371 default:
1372 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001373 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001374 suffix = 'b';
1375 break;
1376 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001377 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001378 suffix = 'w';
1379 break;
1380 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001381 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001382 suffix = 'l';
1383 break;
1384 }
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1386 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001387}
bellarda3a91a32004-06-04 11:06:21 +00001388
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001389static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001390{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001391 int size = qdict_get_int(qdict, "size");
1392 int addr = qdict_get_int(qdict, "addr");
1393 int val = qdict_get_int(qdict, "val");
1394
Jan Kiszkaf1147842009-07-14 10:20:11 +02001395 addr &= IOPORTS_MASK;
1396
1397 switch (size) {
1398 default:
1399 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001400 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001401 break;
1402 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001403 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001404 break;
1405 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001406 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001407 break;
1408 }
1409}
1410
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001411static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001412{
Gongleif1839932014-12-03 18:20:58 +00001413 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001414 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001415
Gongleif1839932014-12-03 18:20:58 +00001416 qemu_boot_set(bootdevice, &local_err);
1417 if (local_err) {
1418 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1419 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001420 } else {
Gongleif1839932014-12-03 18:20:58 +00001421 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001422 }
1423}
1424
bellardb86bda52004-09-18 19:32:46 +00001425#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001426static void print_pte(Monitor *mon, hwaddr addr,
1427 hwaddr pte,
1428 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001429{
Blue Swirld65aaf32010-12-11 18:56:24 +00001430#ifdef TARGET_X86_64
1431 if (addr & (1ULL << 47)) {
1432 addr |= -1LL << 48;
1433 }
1434#endif
1435 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1436 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001437 addr,
1438 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001439 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001440 pte & PG_GLOBAL_MASK ? 'G' : '-',
1441 pte & PG_PSE_MASK ? 'P' : '-',
1442 pte & PG_DIRTY_MASK ? 'D' : '-',
1443 pte & PG_ACCESSED_MASK ? 'A' : '-',
1444 pte & PG_PCD_MASK ? 'C' : '-',
1445 pte & PG_PWT_MASK ? 'T' : '-',
1446 pte & PG_USER_MASK ? 'U' : '-',
1447 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001448}
1449
Andreas Färber9349b4f2012-03-14 01:38:32 +01001450static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001451{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001452 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001453 uint32_t pgd, pde, pte;
1454
bellardb86bda52004-09-18 19:32:46 +00001455 pgd = env->cr[3] & ~0xfff;
1456 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001457 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001458 pde = le32_to_cpu(pde);
1459 if (pde & PG_PRESENT_MASK) {
1460 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001461 /* 4M pages */
1462 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001463 } else {
1464 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001465 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001466 pte = le32_to_cpu(pte);
1467 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001468 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001469 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001470 ~0xfff);
1471 }
1472 }
1473 }
1474 }
1475 }
1476}
1477
Andreas Färber9349b4f2012-03-14 01:38:32 +01001478static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001479{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001480 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001481 uint64_t pdpe, pde, pte;
1482 uint64_t pdp_addr, pd_addr, pt_addr;
1483
1484 pdp_addr = env->cr[3] & ~0x1f;
1485 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001486 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001487 pdpe = le64_to_cpu(pdpe);
1488 if (pdpe & PG_PRESENT_MASK) {
1489 pd_addr = pdpe & 0x3fffffffff000ULL;
1490 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001491 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001492 pde = le64_to_cpu(pde);
1493 if (pde & PG_PRESENT_MASK) {
1494 if (pde & PG_PSE_MASK) {
1495 /* 2M pages with PAE, CR4.PSE is ignored */
1496 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001497 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001498 } else {
1499 pt_addr = pde & 0x3fffffffff000ULL;
1500 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001501 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001502 pte = le64_to_cpu(pte);
1503 if (pte & PG_PRESENT_MASK) {
1504 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1505 + (l3 << 12),
1506 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001507 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001508 }
1509 }
1510 }
1511 }
1512 }
1513 }
1514 }
1515}
1516
1517#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001518static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001519{
1520 uint64_t l1, l2, l3, l4;
1521 uint64_t pml4e, pdpe, pde, pte;
1522 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1523
1524 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1525 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001526 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001527 pml4e = le64_to_cpu(pml4e);
1528 if (pml4e & PG_PRESENT_MASK) {
1529 pdp_addr = pml4e & 0x3fffffffff000ULL;
1530 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001531 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001532 pdpe = le64_to_cpu(pdpe);
1533 if (pdpe & PG_PRESENT_MASK) {
1534 if (pdpe & PG_PSE_MASK) {
1535 /* 1G pages, CR4.PSE is ignored */
1536 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1537 0x3ffffc0000000ULL);
1538 } else {
1539 pd_addr = pdpe & 0x3fffffffff000ULL;
1540 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001541 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001542 pde = le64_to_cpu(pde);
1543 if (pde & PG_PRESENT_MASK) {
1544 if (pde & PG_PSE_MASK) {
1545 /* 2M pages, CR4.PSE is ignored */
1546 print_pte(mon, (l1 << 39) + (l2 << 30) +
1547 (l3 << 21), pde,
1548 0x3ffffffe00000ULL);
1549 } else {
1550 pt_addr = pde & 0x3fffffffff000ULL;
1551 for (l4 = 0; l4 < 512; l4++) {
1552 cpu_physical_memory_read(pt_addr
1553 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001554 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001555 pte = le64_to_cpu(pte);
1556 if (pte & PG_PRESENT_MASK) {
1557 print_pte(mon, (l1 << 39) +
1558 (l2 << 30) +
1559 (l3 << 21) + (l4 << 12),
1560 pte & ~PG_PSE_MASK,
1561 0x3fffffffff000ULL);
1562 }
1563 }
1564 }
1565 }
1566 }
1567 }
1568 }
1569 }
1570 }
1571 }
1572}
1573#endif
1574
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001575static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001576{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001577 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001578
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001579 env = mon_get_cpu_env();
Blue Swirld65aaf32010-12-11 18:56:24 +00001580
1581 if (!(env->cr[0] & CR0_PG_MASK)) {
1582 monitor_printf(mon, "PG disabled\n");
1583 return;
1584 }
1585 if (env->cr[4] & CR4_PAE_MASK) {
1586#ifdef TARGET_X86_64
1587 if (env->hflags & HF_LMA_MASK) {
1588 tlb_info_64(mon, env);
1589 } else
1590#endif
1591 {
1592 tlb_info_pae32(mon, env);
1593 }
1594 } else {
1595 tlb_info_32(mon, env);
1596 }
1597}
1598
Avi Kivitya8170e52012-10-23 12:30:10 +02001599static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001600 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001601 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001602{
bellard9746b152004-11-11 18:30:24 +00001603 int prot1;
1604 prot1 = *plast_prot;
1605 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001606 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001607 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1608 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001609 *pstart, end, end - *pstart,
1610 prot1 & PG_USER_MASK ? 'u' : '-',
1611 'r',
1612 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001613 }
1614 if (prot != 0)
1615 *pstart = end;
1616 else
1617 *pstart = -1;
1618 *plast_prot = prot;
1619 }
1620}
1621
Andreas Färber9349b4f2012-03-14 01:38:32 +01001622static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001623{
Austin Clementsb49ca722011-08-14 23:19:21 -04001624 unsigned int l1, l2;
1625 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001626 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001627 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001628
bellardb86bda52004-09-18 19:32:46 +00001629 pgd = env->cr[3] & ~0xfff;
1630 last_prot = 0;
1631 start = -1;
1632 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001633 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001634 pde = le32_to_cpu(pde);
1635 end = l1 << 22;
1636 if (pde & PG_PRESENT_MASK) {
1637 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1638 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001639 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001640 } else {
1641 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001642 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001643 pte = le32_to_cpu(pte);
1644 end = (l1 << 22) + (l2 << 12);
1645 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001646 prot = pte & pde &
1647 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001648 } else {
1649 prot = 0;
1650 }
aliguori376253e2009-03-05 23:01:23 +00001651 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001652 }
1653 }
1654 } else {
1655 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001656 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001657 }
1658 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001659 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001660 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001661}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001662
Andreas Färber9349b4f2012-03-14 01:38:32 +01001663static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001664{
Austin Clementsb49ca722011-08-14 23:19:21 -04001665 unsigned int l1, l2, l3;
1666 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001667 uint64_t pdpe, pde, pte;
1668 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001669 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001670
1671 pdp_addr = env->cr[3] & ~0x1f;
1672 last_prot = 0;
1673 start = -1;
1674 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001675 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001676 pdpe = le64_to_cpu(pdpe);
1677 end = l1 << 30;
1678 if (pdpe & PG_PRESENT_MASK) {
1679 pd_addr = pdpe & 0x3fffffffff000ULL;
1680 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001681 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001682 pde = le64_to_cpu(pde);
1683 end = (l1 << 30) + (l2 << 21);
1684 if (pde & PG_PRESENT_MASK) {
1685 if (pde & PG_PSE_MASK) {
1686 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1687 PG_PRESENT_MASK);
1688 mem_print(mon, &start, &last_prot, end, prot);
1689 } else {
1690 pt_addr = pde & 0x3fffffffff000ULL;
1691 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001692 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001693 pte = le64_to_cpu(pte);
1694 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1695 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001696 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1697 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001698 } else {
1699 prot = 0;
1700 }
1701 mem_print(mon, &start, &last_prot, end, prot);
1702 }
1703 }
1704 } else {
1705 prot = 0;
1706 mem_print(mon, &start, &last_prot, end, prot);
1707 }
1708 }
1709 } else {
1710 prot = 0;
1711 mem_print(mon, &start, &last_prot, end, prot);
1712 }
1713 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001714 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001715 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001716}
1717
1718
1719#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001720static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001721{
1722 int prot, last_prot;
1723 uint64_t l1, l2, l3, l4;
1724 uint64_t pml4e, pdpe, pde, pte;
1725 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1726
1727 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1728 last_prot = 0;
1729 start = -1;
1730 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001731 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001732 pml4e = le64_to_cpu(pml4e);
1733 end = l1 << 39;
1734 if (pml4e & PG_PRESENT_MASK) {
1735 pdp_addr = pml4e & 0x3fffffffff000ULL;
1736 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001737 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001738 pdpe = le64_to_cpu(pdpe);
1739 end = (l1 << 39) + (l2 << 30);
1740 if (pdpe & PG_PRESENT_MASK) {
1741 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001742 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1743 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001744 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001745 mem_print(mon, &start, &last_prot, end, prot);
1746 } else {
1747 pd_addr = pdpe & 0x3fffffffff000ULL;
1748 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001749 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001750 pde = le64_to_cpu(pde);
1751 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1752 if (pde & PG_PRESENT_MASK) {
1753 if (pde & PG_PSE_MASK) {
1754 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1755 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001756 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001757 mem_print(mon, &start, &last_prot, end, prot);
1758 } else {
1759 pt_addr = pde & 0x3fffffffff000ULL;
1760 for (l4 = 0; l4 < 512; l4++) {
1761 cpu_physical_memory_read(pt_addr
1762 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001763 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001764 pte = le64_to_cpu(pte);
1765 end = (l1 << 39) + (l2 << 30) +
1766 (l3 << 21) + (l4 << 12);
1767 if (pte & PG_PRESENT_MASK) {
1768 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1769 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001770 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001771 } else {
1772 prot = 0;
1773 }
1774 mem_print(mon, &start, &last_prot, end, prot);
1775 }
1776 }
1777 } else {
1778 prot = 0;
1779 mem_print(mon, &start, &last_prot, end, prot);
1780 }
1781 }
1782 }
1783 } else {
1784 prot = 0;
1785 mem_print(mon, &start, &last_prot, end, prot);
1786 }
1787 }
1788 } else {
1789 prot = 0;
1790 mem_print(mon, &start, &last_prot, end, prot);
1791 }
1792 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001793 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001794 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001795}
1796#endif
1797
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001798static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001799{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001800 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001801
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001802 env = mon_get_cpu_env();
Blue Swirl1b3cba62010-12-11 18:56:27 +00001803
1804 if (!(env->cr[0] & CR0_PG_MASK)) {
1805 monitor_printf(mon, "PG disabled\n");
1806 return;
1807 }
1808 if (env->cr[4] & CR4_PAE_MASK) {
1809#ifdef TARGET_X86_64
1810 if (env->hflags & HF_LMA_MASK) {
1811 mem_info_64(mon, env);
1812 } else
1813#endif
1814 {
1815 mem_info_pae32(mon, env);
1816 }
1817 } else {
1818 mem_info_32(mon, env);
1819 }
1820}
bellardb86bda52004-09-18 19:32:46 +00001821#endif
1822
aurel327c664e22009-03-03 06:12:22 +00001823#if defined(TARGET_SH4)
1824
aliguori376253e2009-03-05 23:01:23 +00001825static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001826{
aliguori376253e2009-03-05 23:01:23 +00001827 monitor_printf(mon, " tlb%i:\t"
1828 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1829 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1830 "dirty=%hhu writethrough=%hhu\n",
1831 idx,
1832 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1833 tlb->v, tlb->sh, tlb->c, tlb->pr,
1834 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001835}
1836
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001837static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001838{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001839 CPUArchState *env = mon_get_cpu_env();
aurel327c664e22009-03-03 06:12:22 +00001840 int i;
1841
aliguori376253e2009-03-05 23:01:23 +00001842 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001843 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001844 print_tlb (mon, i, &env->itlb[i]);
1845 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001846 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001847 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001848}
1849
1850#endif
1851
Max Filippov692f7372012-01-07 20:02:40 +04001852#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001853static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001854{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001855 CPUArchState *env1 = mon_get_cpu_env();
Blue Swirld41160a2010-12-19 13:42:56 +00001856
1857 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1858}
1859#endif
1860
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001861static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001862{
1863 mtree_info((fprintf_function)monitor_printf, mon);
1864}
1865
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001866static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001867{
aliguorib28b6232009-04-22 20:20:29 +00001868 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001869 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001870 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001871
zhanghailiang5b009e42014-11-04 19:49:30 +08001872 node_mem = g_new0(uint64_t, nb_numa_nodes);
1873 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001874 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1875 for (i = 0; i < nb_numa_nodes; i++) {
1876 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001877 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001878 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001879 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001880 }
1881 }
1882 monitor_printf(mon, "\n");
1883 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001884 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001885 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001886 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001887}
1888
bellard5f1ce942006-02-08 22:40:15 +00001889#ifdef CONFIG_PROFILER
1890
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001891int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001892int64_t dev_time;
1893
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001894static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001895{
aliguori376253e2009-03-05 23:01:23 +00001896 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001897 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001898 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001899 tcg_time, tcg_time / (double)get_ticks_per_sec());
1900 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001901 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001902}
1903#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001904static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001905{
aliguori376253e2009-03-05 23:01:23 +00001906 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001907}
1908#endif
1909
bellardec36b692006-07-16 18:57:03 +00001910/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001911static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001912
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001913static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001914{
1915 int i;
1916 CaptureState *s;
1917
1918 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001919 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001920 s->ops.info (s->opaque);
1921 }
1922}
1923
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001924static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001925{
1926 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001927 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001928 CaptureState *s;
1929
1930 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1931 if (i == n) {
1932 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001933 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001934 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001935 return;
1936 }
1937 }
1938}
1939
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001940static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001941{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001942 const char *path = qdict_get_str(qdict, "path");
1943 int has_freq = qdict_haskey(qdict, "freq");
1944 int freq = qdict_get_try_int(qdict, "freq", -1);
1945 int has_bits = qdict_haskey(qdict, "bits");
1946 int bits = qdict_get_try_int(qdict, "bits", -1);
1947 int has_channels = qdict_haskey(qdict, "nchannels");
1948 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001949 CaptureState *s;
1950
Anthony Liguori7267c092011-08-20 22:09:37 -05001951 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001952
1953 freq = has_freq ? freq : 44100;
1954 bits = has_bits ? bits : 16;
1955 nchannels = has_channels ? nchannels : 2;
1956
1957 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001958 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001959 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001960 return;
bellardec36b692006-07-16 18:57:03 +00001961 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001962 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001963}
bellardec36b692006-07-16 18:57:03 +00001964
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001965static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001966{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001967 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001968
aliguori76655d62009-03-06 20:27:37 +00001969 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001970 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001971 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001972 return acl;
1973}
aliguori76655d62009-03-06 20:27:37 +00001974
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001975static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001976{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001977 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001978 qemu_acl *acl = find_acl(mon, aclname);
1979 qemu_acl_entry *entry;
1980 int i = 0;
1981
1982 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001983 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001984 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001985 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001986 i++;
1987 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001988 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001989 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001990 }
1991}
1992
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001993static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001994{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001995 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001996 qemu_acl *acl = find_acl(mon, aclname);
1997
1998 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001999 qemu_acl_reset(acl);
2000 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002001 }
2002}
aliguori76655d62009-03-06 20:27:37 +00002003
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002004static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002005{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002006 const char *aclname = qdict_get_str(qdict, "aclname");
2007 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002008 qemu_acl *acl = find_acl(mon, aclname);
2009
2010 if (acl) {
2011 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002012 acl->defaultDeny = 0;
2013 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002014 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002015 acl->defaultDeny = 1;
2016 monitor_printf(mon, "acl: policy set to 'deny'\n");
2017 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002018 monitor_printf(mon, "acl: unknown policy '%s', "
2019 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002020 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002021 }
2022}
aliguori76655d62009-03-06 20:27:37 +00002023
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002024static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002025{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002026 const char *aclname = qdict_get_str(qdict, "aclname");
2027 const char *match = qdict_get_str(qdict, "match");
2028 const char *policy = qdict_get_str(qdict, "policy");
2029 int has_index = qdict_haskey(qdict, "index");
2030 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002031 qemu_acl *acl = find_acl(mon, aclname);
2032 int deny, ret;
2033
2034 if (acl) {
2035 if (strcmp(policy, "allow") == 0) {
2036 deny = 0;
2037 } else if (strcmp(policy, "deny") == 0) {
2038 deny = 1;
2039 } else {
2040 monitor_printf(mon, "acl: unknown policy '%s', "
2041 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002042 return;
2043 }
aliguori28a76be2009-03-06 20:27:40 +00002044 if (has_index)
2045 ret = qemu_acl_insert(acl, deny, match, index);
2046 else
2047 ret = qemu_acl_append(acl, deny, match);
2048 if (ret < 0)
2049 monitor_printf(mon, "acl: unable to add acl entry\n");
2050 else
2051 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002052 }
2053}
aliguori76655d62009-03-06 20:27:37 +00002054
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002055static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002056{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002057 const char *aclname = qdict_get_str(qdict, "aclname");
2058 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002059 qemu_acl *acl = find_acl(mon, aclname);
2060 int ret;
aliguori76655d62009-03-06 20:27:37 +00002061
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002062 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002063 ret = qemu_acl_remove(acl, match);
2064 if (ret < 0)
2065 monitor_printf(mon, "acl: no matching acl entry\n");
2066 else
2067 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002068 }
2069}
2070
Huang Ying79c4f6b2009-06-23 10:05:14 +08002071#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002072static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002073{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002074 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002075 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002076 int cpu_index = qdict_get_int(qdict, "cpu_index");
2077 int bank = qdict_get_int(qdict, "bank");
2078 uint64_t status = qdict_get_int(qdict, "status");
2079 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2080 uint64_t addr = qdict_get_int(qdict, "addr");
2081 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002082 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002083
Eric Blake34acbc92015-05-15 16:25:00 -06002084 if (qdict_get_try_bool(qdict, "broadcast", false)) {
Jan Kiszka747461c2011-03-02 08:56:10 +01002085 flags |= MCE_INJECT_BROADCAST;
2086 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002087 cs = qemu_get_cpu(cpu_index);
2088 if (cs != NULL) {
2089 cpu = X86_CPU(cs);
2090 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2091 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002092 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002093}
2094#endif
2095
Corey Bryant208c9d12012-06-22 14:36:09 -04002096void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002097{
Anthony Liguoric227f092009-10-01 16:12:16 -05002098 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002099 int fd;
2100
Corey Bryant208c9d12012-06-22 14:36:09 -04002101 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002102 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002103 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002104 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002105 }
2106
2107 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002108 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002109 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2110 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002111 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002112 }
2113
Corey Bryant208c9d12012-06-22 14:36:09 -04002114 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002115 if (strcmp(monfd->name, fdname) != 0) {
2116 continue;
2117 }
2118
2119 close(monfd->fd);
2120 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002121 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002122 }
2123
Anthony Liguori7267c092011-08-20 22:09:37 -05002124 monfd = g_malloc0(sizeof(mon_fd_t));
2125 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002126 monfd->fd = fd;
2127
Corey Bryant208c9d12012-06-22 14:36:09 -04002128 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002129}
2130
Corey Bryant208c9d12012-06-22 14:36:09 -04002131void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002132{
Anthony Liguoric227f092009-10-01 16:12:16 -05002133 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002134
Corey Bryant208c9d12012-06-22 14:36:09 -04002135 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002136 if (strcmp(monfd->name, fdname) != 0) {
2137 continue;
2138 }
2139
Blue Swirl72cf2d42009-09-12 07:36:22 +00002140 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002141 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002142 g_free(monfd->name);
2143 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002144 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002145 }
2146
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002147 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002148}
2149
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002150static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002151{
Luiz Capitulino13548692011-07-29 15:36:43 -03002152 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002153 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002154
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002155 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002156
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002157 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002158 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002159 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002160}
2161
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002162int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002163{
Anthony Liguoric227f092009-10-01 16:12:16 -05002164 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002165
Blue Swirl72cf2d42009-09-12 07:36:22 +00002166 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002167 int fd;
2168
2169 if (strcmp(monfd->name, fdname) != 0) {
2170 continue;
2171 }
2172
2173 fd = monfd->fd;
2174
2175 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002176 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002177 g_free(monfd->name);
2178 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002179
2180 return fd;
2181 }
2182
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002183 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002184 return -1;
2185}
2186
Corey Bryantba1c0482012-08-14 16:43:43 -04002187static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2188{
2189 MonFdsetFd *mon_fdset_fd;
2190 MonFdsetFd *mon_fdset_fd_next;
2191
2192 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002193 if ((mon_fdset_fd->removed ||
2194 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2195 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002196 close(mon_fdset_fd->fd);
2197 g_free(mon_fdset_fd->opaque);
2198 QLIST_REMOVE(mon_fdset_fd, next);
2199 g_free(mon_fdset_fd);
2200 }
2201 }
2202
Corey Bryantadb696f2012-08-14 16:43:47 -04002203 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002204 QLIST_REMOVE(mon_fdset, next);
2205 g_free(mon_fdset);
2206 }
2207}
2208
Corey Bryantefb87c12012-08-14 16:43:48 -04002209static void monitor_fdsets_cleanup(void)
2210{
2211 MonFdset *mon_fdset;
2212 MonFdset *mon_fdset_next;
2213
2214 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2215 monitor_fdset_cleanup(mon_fdset);
2216 }
2217}
2218
Corey Bryantba1c0482012-08-14 16:43:43 -04002219AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2220 const char *opaque, Error **errp)
2221{
2222 int fd;
2223 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002224 AddfdInfo *fdinfo;
2225
2226 fd = qemu_chr_fe_get_msgfd(mon->chr);
2227 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002228 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002229 goto error;
2230 }
2231
Corey Bryante446f702012-10-18 15:19:32 -04002232 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2233 has_opaque, opaque, errp);
2234 if (fdinfo) {
2235 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002236 }
2237
Corey Bryantba1c0482012-08-14 16:43:43 -04002238error:
2239 if (fd != -1) {
2240 close(fd);
2241 }
2242 return NULL;
2243}
2244
2245void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2246{
2247 MonFdset *mon_fdset;
2248 MonFdsetFd *mon_fdset_fd;
2249 char fd_str[60];
2250
2251 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2252 if (mon_fdset->id != fdset_id) {
2253 continue;
2254 }
2255 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2256 if (has_fd) {
2257 if (mon_fdset_fd->fd != fd) {
2258 continue;
2259 }
2260 mon_fdset_fd->removed = true;
2261 break;
2262 } else {
2263 mon_fdset_fd->removed = true;
2264 }
2265 }
2266 if (has_fd && !mon_fdset_fd) {
2267 goto error;
2268 }
2269 monitor_fdset_cleanup(mon_fdset);
2270 return;
2271 }
2272
2273error:
2274 if (has_fd) {
2275 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2276 fdset_id, fd);
2277 } else {
2278 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2279 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002280 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002281}
2282
2283FdsetInfoList *qmp_query_fdsets(Error **errp)
2284{
2285 MonFdset *mon_fdset;
2286 MonFdsetFd *mon_fdset_fd;
2287 FdsetInfoList *fdset_list = NULL;
2288
2289 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2290 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2291 FdsetFdInfoList *fdsetfd_list = NULL;
2292
2293 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2294 fdset_info->value->fdset_id = mon_fdset->id;
2295
2296 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2297 FdsetFdInfoList *fdsetfd_info;
2298
2299 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2300 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2301 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2302 if (mon_fdset_fd->opaque) {
2303 fdsetfd_info->value->has_opaque = true;
2304 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2305 } else {
2306 fdsetfd_info->value->has_opaque = false;
2307 }
2308
2309 fdsetfd_info->next = fdsetfd_list;
2310 fdsetfd_list = fdsetfd_info;
2311 }
2312
2313 fdset_info->value->fds = fdsetfd_list;
2314
2315 fdset_info->next = fdset_list;
2316 fdset_list = fdset_info;
2317 }
2318
2319 return fdset_list;
2320}
2321
Corey Bryante446f702012-10-18 15:19:32 -04002322AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2323 bool has_opaque, const char *opaque,
2324 Error **errp)
2325{
2326 MonFdset *mon_fdset = NULL;
2327 MonFdsetFd *mon_fdset_fd;
2328 AddfdInfo *fdinfo;
2329
2330 if (has_fdset_id) {
2331 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2332 /* Break if match found or match impossible due to ordering by ID */
2333 if (fdset_id <= mon_fdset->id) {
2334 if (fdset_id < mon_fdset->id) {
2335 mon_fdset = NULL;
2336 }
2337 break;
2338 }
2339 }
2340 }
2341
2342 if (mon_fdset == NULL) {
2343 int64_t fdset_id_prev = -1;
2344 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2345
2346 if (has_fdset_id) {
2347 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002348 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2349 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002350 return NULL;
2351 }
2352 /* Use specified fdset ID */
2353 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2354 mon_fdset_cur = mon_fdset;
2355 if (fdset_id < mon_fdset_cur->id) {
2356 break;
2357 }
2358 }
2359 } else {
2360 /* Use first available fdset ID */
2361 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2362 mon_fdset_cur = mon_fdset;
2363 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2364 fdset_id_prev = mon_fdset_cur->id;
2365 continue;
2366 }
2367 break;
2368 }
2369 }
2370
2371 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2372 if (has_fdset_id) {
2373 mon_fdset->id = fdset_id;
2374 } else {
2375 mon_fdset->id = fdset_id_prev + 1;
2376 }
2377
2378 /* The fdset list is ordered by fdset ID */
2379 if (!mon_fdset_cur) {
2380 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2381 } else if (mon_fdset->id < mon_fdset_cur->id) {
2382 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2383 } else {
2384 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2385 }
2386 }
2387
2388 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2389 mon_fdset_fd->fd = fd;
2390 mon_fdset_fd->removed = false;
2391 if (has_opaque) {
2392 mon_fdset_fd->opaque = g_strdup(opaque);
2393 }
2394 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2395
2396 fdinfo = g_malloc0(sizeof(*fdinfo));
2397 fdinfo->fdset_id = mon_fdset->id;
2398 fdinfo->fd = mon_fdset_fd->fd;
2399
2400 return fdinfo;
2401}
2402
Corey Bryantadb696f2012-08-14 16:43:47 -04002403int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2404{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002405#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002406 MonFdset *mon_fdset;
2407 MonFdsetFd *mon_fdset_fd;
2408 int mon_fd_flags;
2409
Corey Bryantadb696f2012-08-14 16:43:47 -04002410 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2411 if (mon_fdset->id != fdset_id) {
2412 continue;
2413 }
2414 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2415 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2416 if (mon_fd_flags == -1) {
2417 return -1;
2418 }
2419
2420 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2421 return mon_fdset_fd->fd;
2422 }
2423 }
2424 errno = EACCES;
2425 return -1;
2426 }
2427#endif
2428
2429 errno = ENOENT;
2430 return -1;
2431}
2432
2433int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2434{
2435 MonFdset *mon_fdset;
2436 MonFdsetFd *mon_fdset_fd_dup;
2437
2438 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2439 if (mon_fdset->id != fdset_id) {
2440 continue;
2441 }
2442 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2443 if (mon_fdset_fd_dup->fd == dup_fd) {
2444 return -1;
2445 }
2446 }
2447 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2448 mon_fdset_fd_dup->fd = dup_fd;
2449 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2450 return 0;
2451 }
2452 return -1;
2453}
2454
2455static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2456{
2457 MonFdset *mon_fdset;
2458 MonFdsetFd *mon_fdset_fd_dup;
2459
2460 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2461 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2462 if (mon_fdset_fd_dup->fd == dup_fd) {
2463 if (remove) {
2464 QLIST_REMOVE(mon_fdset_fd_dup, next);
2465 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2466 monitor_fdset_cleanup(mon_fdset);
2467 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002468 return -1;
2469 } else {
2470 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002471 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002472 }
2473 }
2474 }
2475 return -1;
2476}
2477
2478int monitor_fdset_dup_fd_find(int dup_fd)
2479{
2480 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2481}
2482
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002483void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002484{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002485 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002486}
2487
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002488int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002489{
2490 int fd;
2491 Error *local_err = NULL;
2492
2493 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002494 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002495 } else {
2496 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002497 if (fd == -1) {
2498 error_setg(&local_err, "Invalid file descriptor number '%s'",
2499 fdname);
2500 }
2501 }
2502 if (local_err) {
2503 error_propagate(errp, local_err);
2504 assert(fd == -1);
2505 } else {
2506 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002507 }
2508
2509 return fd;
2510}
2511
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002512/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002513static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 {
2515 .name = "version",
2516 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 .params = "",
2518 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002519 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 },
2521 {
2522 .name = "network",
2523 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 .params = "",
2525 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002526 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 },
2528 {
2529 .name = "chardev",
2530 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 .params = "",
2532 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002533 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 },
2535 {
2536 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002537 .args_type = "nodes:-n,verbose:-v,device:B?",
2538 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002539 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002540 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002541 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 },
2543 {
2544 .name = "blockstats",
2545 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002546 .params = "",
2547 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002548 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002549 },
2550 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002551 .name = "block-jobs",
2552 .args_type = "",
2553 .params = "",
2554 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002555 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002556 },
2557 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .name = "registers",
2559 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 .params = "",
2561 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002562 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 },
2564 {
2565 .name = "cpus",
2566 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .params = "",
2568 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002569 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 },
2571 {
2572 .name = "history",
2573 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 .params = "",
2575 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002576 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002578#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2579 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 {
2581 .name = "irq",
2582 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 .params = "",
2584 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002585#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002586 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002587#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002588 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002589#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002590 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002591#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 },
2593 {
2594 .name = "pic",
2595 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 .params = "",
2597 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002598#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002599 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002600#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002601 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002602#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002603 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002604#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002606#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 {
2608 .name = "pci",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002612 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002614#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002615 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 {
2617 .name = "tlb",
2618 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002619 .params = "",
2620 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002621 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 },
aurel327c664e22009-03-03 06:12:22 +00002623#endif
2624#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 {
2626 .name = "mem",
2627 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 .params = "",
2629 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002630 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 },
bellardb86bda52004-09-18 19:32:46 +00002632#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 {
Blue Swirl314e2982011-09-11 20:22:05 +00002634 .name = "mtree",
2635 .args_type = "",
2636 .params = "",
2637 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002638 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002639 },
2640 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 .name = "jit",
2642 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .params = "",
2644 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002645 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 },
2647 {
Max Filippov246ae242014-11-02 11:04:18 +03002648 .name = "opcount",
2649 .args_type = "",
2650 .params = "",
2651 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002652 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002653 },
2654 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 .name = "kvm",
2656 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 .params = "",
2658 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002659 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
2661 {
2662 .name = "numa",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002666 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "usb",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002673 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 },
2675 {
2676 .name = "usbhost",
2677 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 .params = "",
2679 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002680 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 },
2682 {
2683 .name = "profile",
2684 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 .params = "",
2686 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002687 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 },
2689 {
2690 .name = "capture",
2691 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 .params = "",
2693 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002694 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 },
2696 {
2697 .name = "snapshots",
2698 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 .params = "",
2700 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002701 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 },
2703 {
2704 .name = "status",
2705 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 .params = "",
2707 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002708 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 },
2710 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 .name = "mice",
2712 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 .params = "",
2714 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002715 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 },
2717 {
2718 .name = "vnc",
2719 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 .params = "",
2721 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002722 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002724#if defined(CONFIG_SPICE)
2725 {
2726 .name = "spice",
2727 .args_type = "",
2728 .params = "",
2729 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002730 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002731 },
2732#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 {
2734 .name = "name",
2735 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 .params = "",
2737 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002738 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 },
2740 {
2741 .name = "uuid",
2742 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 .params = "",
2744 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002745 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 {
2748 .name = "cpustats",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002752 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
blueswir131a60e22007-10-26 18:42:59 +00002754#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 {
2756 .name = "usernet",
2757 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 .params = "",
2759 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002760 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 },
blueswir131a60e22007-10-26 18:42:59 +00002762#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 {
2764 .name = "migrate",
2765 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 .params = "",
2767 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002768 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002769 },
2770 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002771 .name = "migrate_capabilities",
2772 .args_type = "",
2773 .params = "",
2774 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002775 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002776 },
2777 {
Liang Li50e9a622015-03-23 16:32:29 +08002778 .name = "migrate_parameters",
2779 .args_type = "",
2780 .params = "",
2781 .help = "show current migration parameters",
2782 .mhandler.cmd = hmp_info_migrate_parameters,
2783 },
2784 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002785 .name = "migrate_cache_size",
2786 .args_type = "",
2787 .params = "",
2788 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002789 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002790 },
2791 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .name = "balloon",
2793 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 .params = "",
2795 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002796 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 },
2798 {
2799 .name = "qtree",
2800 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 .params = "",
2802 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002803 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002804 },
2805 {
2806 .name = "qdm",
2807 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 .params = "",
2809 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002810 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 },
2812 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002813 .name = "qom-tree",
2814 .args_type = "path:s?",
2815 .params = "[path]",
2816 .help = "show QOM composition tree",
2817 .mhandler.cmd = hmp_info_qom_tree,
2818 },
2819 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .name = "roms",
2821 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 .params = "",
2823 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002824 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302826 {
2827 .name = "trace-events",
2828 .args_type = "",
2829 .params = "",
2830 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002831 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302832 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002834 .name = "tpm",
2835 .args_type = "",
2836 .params = "",
2837 .help = "show the TPM device",
2838 .mhandler.cmd = hmp_info_tpm,
2839 },
2840 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002841 .name = "memdev",
2842 .args_type = "",
2843 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002844 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002845 .mhandler.cmd = hmp_info_memdev,
2846 },
2847 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002848 .name = "memory-devices",
2849 .args_type = "",
2850 .params = "",
2851 .help = "show memory devices",
2852 .mhandler.cmd = hmp_info_memory_devices,
2853 },
2854 {
Scott Feldmanfafa4d52015-06-10 18:21:21 -07002855 .name = "rocker",
2856 .args_type = "name:s",
2857 .params = "name",
2858 .help = "Show rocker switch",
2859 .mhandler.cmd = hmp_rocker,
2860 },
2861 {
2862 .name = "rocker-ports",
2863 .args_type = "name:s",
2864 .params = "name",
2865 .help = "Show rocker ports",
2866 .mhandler.cmd = hmp_rocker_ports,
2867 },
2868 {
2869 .name = "rocker-of-dpa-flows",
2870 .args_type = "name:s,tbl_id:i?",
2871 .params = "name [tbl_id]",
2872 .help = "Show rocker OF-DPA flow tables",
2873 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2874 },
2875 {
2876 .name = "rocker-of-dpa-groups",
2877 .args_type = "name:s,type:i?",
2878 .params = "name [type]",
2879 .help = "Show rocker OF-DPA groups",
2880 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2881 },
2882 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002883 .name = NULL,
2884 },
bellard9dc39cb2004-03-14 21:38:27 +00002885};
2886
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002887/* mon_cmds and info_cmds would be sorted at runtime */
2888static mon_cmd_t mon_cmds[] = {
2889#include "hmp-commands.h"
2890 { NULL, NULL, },
2891};
2892
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002893static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002894#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002895 { /* NULL */ },
2896};
2897
bellard9307c4c2004-04-04 12:57:25 +00002898/*******************************************************************/
2899
2900static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002901static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002902
bellard92a31b12005-02-10 22:00:52 +00002903#define MD_TLONG 0
2904#define MD_I32 1
2905
bellard9307c4c2004-04-04 12:57:25 +00002906typedef struct MonitorDef {
2907 const char *name;
2908 int offset;
blueswir18662d652008-10-02 18:32:44 +00002909 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002910 int type;
bellard9307c4c2004-04-04 12:57:25 +00002911} MonitorDef;
2912
bellard57206fd2004-04-25 18:54:52 +00002913#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002914static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002915{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002916 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002917 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002918}
2919#endif
2920
bellarda541f292004-04-12 20:39:29 +00002921#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002922static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002923{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002924 CPUArchState *env = mon_get_cpu_env();
bellarda541f292004-04-12 20:39:29 +00002925 unsigned int u;
2926 int i;
2927
2928 u = 0;
2929 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002930 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002931
2932 return u;
2933}
2934
blueswir18662d652008-10-02 18:32:44 +00002935static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002936{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002937 CPUArchState *env = mon_get_cpu_env();
j_mayer0411a972007-10-25 21:35:50 +00002938 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002939}
2940
blueswir18662d652008-10-02 18:32:44 +00002941static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002942{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002943 CPUArchState *env = mon_get_cpu_env();
aurel323d7b4172008-10-21 11:28:46 +00002944 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002945}
bellard9fddaa02004-05-21 12:59:32 +00002946
blueswir18662d652008-10-02 18:32:44 +00002947static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002948{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002949 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002950 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002951}
2952
blueswir18662d652008-10-02 18:32:44 +00002953static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002954{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002955 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002956 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002957}
2958
blueswir18662d652008-10-02 18:32:44 +00002959static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002960{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002961 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002962 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002963}
bellarda541f292004-04-12 20:39:29 +00002964#endif
2965
bellarde95c8d52004-09-30 22:22:08 +00002966#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002967#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002968static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002969{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002970 CPUArchState *env = mon_get_cpu_env();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002971
2972 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002973}
bellard7b936c02005-10-30 17:05:13 +00002974#endif
bellarde95c8d52004-09-30 22:22:08 +00002975
blueswir18662d652008-10-02 18:32:44 +00002976static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002977{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002978 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002979 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002980}
2981#endif
2982
blueswir18662d652008-10-02 18:32:44 +00002983static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002984#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002985
2986#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002987 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2988 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2989 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002990
Andreas Färbere59d1672012-02-16 00:40:47 +01002991 { "eax", offsetof(CPUX86State, regs[0]) },
2992 { "ecx", offsetof(CPUX86State, regs[1]) },
2993 { "edx", offsetof(CPUX86State, regs[2]) },
2994 { "ebx", offsetof(CPUX86State, regs[3]) },
2995 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2996 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2997 { "esi", offsetof(CPUX86State, regs[6]) },
2998 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002999#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003000 { "r8", offsetof(CPUX86State, regs[8]) },
3001 { "r9", offsetof(CPUX86State, regs[9]) },
3002 { "r10", offsetof(CPUX86State, regs[10]) },
3003 { "r11", offsetof(CPUX86State, regs[11]) },
3004 { "r12", offsetof(CPUX86State, regs[12]) },
3005 { "r13", offsetof(CPUX86State, regs[13]) },
3006 { "r14", offsetof(CPUX86State, regs[14]) },
3007 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003008#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003009 { "eflags", offsetof(CPUX86State, eflags) },
3010 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003011 SEG("cs", R_CS)
3012 SEG("ds", R_DS)
3013 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003014 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003015 SEG("fs", R_FS)
3016 SEG("gs", R_GS)
3017 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003018#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003019 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003020 { "r0", offsetof(CPUPPCState, gpr[0]) },
3021 { "r1", offsetof(CPUPPCState, gpr[1]) },
3022 { "r2", offsetof(CPUPPCState, gpr[2]) },
3023 { "r3", offsetof(CPUPPCState, gpr[3]) },
3024 { "r4", offsetof(CPUPPCState, gpr[4]) },
3025 { "r5", offsetof(CPUPPCState, gpr[5]) },
3026 { "r6", offsetof(CPUPPCState, gpr[6]) },
3027 { "r7", offsetof(CPUPPCState, gpr[7]) },
3028 { "r8", offsetof(CPUPPCState, gpr[8]) },
3029 { "r9", offsetof(CPUPPCState, gpr[9]) },
3030 { "r10", offsetof(CPUPPCState, gpr[10]) },
3031 { "r11", offsetof(CPUPPCState, gpr[11]) },
3032 { "r12", offsetof(CPUPPCState, gpr[12]) },
3033 { "r13", offsetof(CPUPPCState, gpr[13]) },
3034 { "r14", offsetof(CPUPPCState, gpr[14]) },
3035 { "r15", offsetof(CPUPPCState, gpr[15]) },
3036 { "r16", offsetof(CPUPPCState, gpr[16]) },
3037 { "r17", offsetof(CPUPPCState, gpr[17]) },
3038 { "r18", offsetof(CPUPPCState, gpr[18]) },
3039 { "r19", offsetof(CPUPPCState, gpr[19]) },
3040 { "r20", offsetof(CPUPPCState, gpr[20]) },
3041 { "r21", offsetof(CPUPPCState, gpr[21]) },
3042 { "r22", offsetof(CPUPPCState, gpr[22]) },
3043 { "r23", offsetof(CPUPPCState, gpr[23]) },
3044 { "r24", offsetof(CPUPPCState, gpr[24]) },
3045 { "r25", offsetof(CPUPPCState, gpr[25]) },
3046 { "r26", offsetof(CPUPPCState, gpr[26]) },
3047 { "r27", offsetof(CPUPPCState, gpr[27]) },
3048 { "r28", offsetof(CPUPPCState, gpr[28]) },
3049 { "r29", offsetof(CPUPPCState, gpr[29]) },
3050 { "r30", offsetof(CPUPPCState, gpr[30]) },
3051 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003052 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003053 { "f0", offsetof(CPUPPCState, fpr[0]) },
3054 { "f1", offsetof(CPUPPCState, fpr[1]) },
3055 { "f2", offsetof(CPUPPCState, fpr[2]) },
3056 { "f3", offsetof(CPUPPCState, fpr[3]) },
3057 { "f4", offsetof(CPUPPCState, fpr[4]) },
3058 { "f5", offsetof(CPUPPCState, fpr[5]) },
3059 { "f6", offsetof(CPUPPCState, fpr[6]) },
3060 { "f7", offsetof(CPUPPCState, fpr[7]) },
3061 { "f8", offsetof(CPUPPCState, fpr[8]) },
3062 { "f9", offsetof(CPUPPCState, fpr[9]) },
3063 { "f10", offsetof(CPUPPCState, fpr[10]) },
3064 { "f11", offsetof(CPUPPCState, fpr[11]) },
3065 { "f12", offsetof(CPUPPCState, fpr[12]) },
3066 { "f13", offsetof(CPUPPCState, fpr[13]) },
3067 { "f14", offsetof(CPUPPCState, fpr[14]) },
3068 { "f15", offsetof(CPUPPCState, fpr[15]) },
3069 { "f16", offsetof(CPUPPCState, fpr[16]) },
3070 { "f17", offsetof(CPUPPCState, fpr[17]) },
3071 { "f18", offsetof(CPUPPCState, fpr[18]) },
3072 { "f19", offsetof(CPUPPCState, fpr[19]) },
3073 { "f20", offsetof(CPUPPCState, fpr[20]) },
3074 { "f21", offsetof(CPUPPCState, fpr[21]) },
3075 { "f22", offsetof(CPUPPCState, fpr[22]) },
3076 { "f23", offsetof(CPUPPCState, fpr[23]) },
3077 { "f24", offsetof(CPUPPCState, fpr[24]) },
3078 { "f25", offsetof(CPUPPCState, fpr[25]) },
3079 { "f26", offsetof(CPUPPCState, fpr[26]) },
3080 { "f27", offsetof(CPUPPCState, fpr[27]) },
3081 { "f28", offsetof(CPUPPCState, fpr[28]) },
3082 { "f29", offsetof(CPUPPCState, fpr[29]) },
3083 { "f30", offsetof(CPUPPCState, fpr[30]) },
3084 { "f31", offsetof(CPUPPCState, fpr[31]) },
3085 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003086 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003087 { "nip|pc", offsetof(CPUPPCState, nip) },
3088 { "lr", offsetof(CPUPPCState, lr) },
3089 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003090 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003091 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003092 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003093 { "msr", 0, &monitor_get_msr, },
3094 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003095 { "tbu", 0, &monitor_get_tbu, },
3096 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003097 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003098 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3099 { "sr0", offsetof(CPUPPCState, sr[0]) },
3100 { "sr1", offsetof(CPUPPCState, sr[1]) },
3101 { "sr2", offsetof(CPUPPCState, sr[2]) },
3102 { "sr3", offsetof(CPUPPCState, sr[3]) },
3103 { "sr4", offsetof(CPUPPCState, sr[4]) },
3104 { "sr5", offsetof(CPUPPCState, sr[5]) },
3105 { "sr6", offsetof(CPUPPCState, sr[6]) },
3106 { "sr7", offsetof(CPUPPCState, sr[7]) },
3107 { "sr8", offsetof(CPUPPCState, sr[8]) },
3108 { "sr9", offsetof(CPUPPCState, sr[9]) },
3109 { "sr10", offsetof(CPUPPCState, sr[10]) },
3110 { "sr11", offsetof(CPUPPCState, sr[11]) },
3111 { "sr12", offsetof(CPUPPCState, sr[12]) },
3112 { "sr13", offsetof(CPUPPCState, sr[13]) },
3113 { "sr14", offsetof(CPUPPCState, sr[14]) },
3114 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003115 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003116 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003117
Andreas Färbere59d1672012-02-16 00:40:47 +01003118 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3119 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003120 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3121 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3122 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003123 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3124 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3125 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3126 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3127 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3128 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3129 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3130 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3131 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3132 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3133 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3134 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3135 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3136 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3137 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3138 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3139 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3140 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3141 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3142 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3143 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3144 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3145 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3146 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3147 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3148 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3149 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3150 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3151 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3152 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3153 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3154 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3155 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3156 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3157 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3158 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3159 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3160 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3161 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3162 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3163 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3164 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3165 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3166 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3167 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3168 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3169 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3170 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3171 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3172 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3173 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3174 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3175 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3176 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3177 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3178 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3179 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3180 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3181 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3182 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3183 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3184 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3185 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3186 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003187
bellarde95c8d52004-09-30 22:22:08 +00003188#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003189 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3190 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3191 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3192 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3193 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3194 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3195 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3196 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003197 { "o0", 0, monitor_get_reg },
3198 { "o1", 1, monitor_get_reg },
3199 { "o2", 2, monitor_get_reg },
3200 { "o3", 3, monitor_get_reg },
3201 { "o4", 4, monitor_get_reg },
3202 { "o5", 5, monitor_get_reg },
3203 { "o6", 6, monitor_get_reg },
3204 { "o7", 7, monitor_get_reg },
3205 { "l0", 8, monitor_get_reg },
3206 { "l1", 9, monitor_get_reg },
3207 { "l2", 10, monitor_get_reg },
3208 { "l3", 11, monitor_get_reg },
3209 { "l4", 12, monitor_get_reg },
3210 { "l5", 13, monitor_get_reg },
3211 { "l6", 14, monitor_get_reg },
3212 { "l7", 15, monitor_get_reg },
3213 { "i0", 16, monitor_get_reg },
3214 { "i1", 17, monitor_get_reg },
3215 { "i2", 18, monitor_get_reg },
3216 { "i3", 19, monitor_get_reg },
3217 { "i4", 20, monitor_get_reg },
3218 { "i5", 21, monitor_get_reg },
3219 { "i6", 22, monitor_get_reg },
3220 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003221 { "pc", offsetof(CPUSPARCState, pc) },
3222 { "npc", offsetof(CPUSPARCState, npc) },
3223 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003224#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003225 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003226 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003227#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003228 { "tbr", offsetof(CPUSPARCState, tbr) },
3229 { "fsr", offsetof(CPUSPARCState, fsr) },
3230 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3231 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3232 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3233 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3234 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3235 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3236 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3237 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3238 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3239 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3240 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3241 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3242 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3243 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3244 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3245 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3246 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3247 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3248 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3249 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3250 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3251 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3252 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3253 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3254 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3255 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3256 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3257 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3258 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3259 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3260 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3261 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003262#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003263 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3264 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3265 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3266 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3267 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3268 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3269 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3270 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3271 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3272 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3273 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3274 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3275 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3276 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3277 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3278 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3279 { "asi", offsetof(CPUSPARCState, asi) },
3280 { "pstate", offsetof(CPUSPARCState, pstate) },
3281 { "cansave", offsetof(CPUSPARCState, cansave) },
3282 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3283 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3284 { "wstate", offsetof(CPUSPARCState, wstate) },
3285 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3286 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003287#endif
bellard9307c4c2004-04-04 12:57:25 +00003288#endif
3289 { NULL },
3290};
3291
Stefan Weil9c3175c2013-08-22 21:30:09 +02003292static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3293expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003294{
Fam Zheng277acfe2013-08-20 10:58:21 +08003295 va_list ap;
3296 va_start(ap, fmt);
3297 monitor_vprintf(mon, fmt, ap);
3298 monitor_printf(mon, "\n");
3299 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003300 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003301}
3302
Markus Armbruster09b94182010-01-20 13:07:30 +01003303/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003304static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003305{
blueswir18662d652008-10-02 18:32:44 +00003306 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003307 void *ptr;
3308
bellard9307c4c2004-04-04 12:57:25 +00003309 for(md = monitor_defs; md->name != NULL; md++) {
3310 if (compare_cmd(name, md->name)) {
3311 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003312 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003313 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07003314 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00003315 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003316 switch(md->type) {
3317 case MD_I32:
3318 *pval = *(int32_t *)ptr;
3319 break;
3320 case MD_TLONG:
3321 *pval = *(target_long *)ptr;
3322 break;
3323 default:
3324 *pval = 0;
3325 break;
3326 }
bellard9307c4c2004-04-04 12:57:25 +00003327 }
3328 return 0;
3329 }
3330 }
3331 return -1;
3332}
3333
3334static void next(void)
3335{
Blue Swirl660f11b2009-07-31 21:16:51 +00003336 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003337 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003338 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003339 pch++;
3340 }
3341}
3342
aliguori376253e2009-03-05 23:01:23 +00003343static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003344
aliguori376253e2009-03-05 23:01:23 +00003345static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003346{
blueswir1c2efc952007-09-25 17:28:42 +00003347 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003348 char *p;
bellard6a00d602005-11-21 23:25:50 +00003349 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003350
3351 switch(*pch) {
3352 case '+':
3353 next();
aliguori376253e2009-03-05 23:01:23 +00003354 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003355 break;
3356 case '-':
3357 next();
aliguori376253e2009-03-05 23:01:23 +00003358 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003359 break;
3360 case '~':
3361 next();
aliguori376253e2009-03-05 23:01:23 +00003362 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003363 break;
3364 case '(':
3365 next();
aliguori376253e2009-03-05 23:01:23 +00003366 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003367 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003368 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003369 }
3370 next();
3371 break;
bellard81d09122004-07-14 17:21:37 +00003372 case '\'':
3373 pch++;
3374 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003375 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003376 n = *pch;
3377 pch++;
3378 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003379 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003380 next();
3381 break;
bellard9307c4c2004-04-04 12:57:25 +00003382 case '$':
3383 {
3384 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003385 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003386
bellard9307c4c2004-04-04 12:57:25 +00003387 pch++;
3388 q = buf;
3389 while ((*pch >= 'a' && *pch <= 'z') ||
3390 (*pch >= 'A' && *pch <= 'Z') ||
3391 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003392 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003393 if ((q - buf) < sizeof(buf) - 1)
3394 *q++ = *pch;
3395 pch++;
3396 }
blueswir1cd390082008-11-16 13:53:32 +00003397 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003398 pch++;
3399 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003400 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003401 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003402 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003403 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003404 }
3405 break;
3406 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003407 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003408 n = 0;
3409 break;
3410 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003411 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003412 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003413 if (errno == ERANGE) {
3414 expr_error(mon, "number too large");
3415 }
bellard9307c4c2004-04-04 12:57:25 +00003416 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003417 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003418 }
3419 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003420 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003421 pch++;
3422 break;
3423 }
3424 return n;
3425}
3426
3427
aliguori376253e2009-03-05 23:01:23 +00003428static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003429{
blueswir1c2efc952007-09-25 17:28:42 +00003430 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003431 int op;
ths3b46e622007-09-17 08:09:54 +00003432
aliguori376253e2009-03-05 23:01:23 +00003433 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003434 for(;;) {
3435 op = *pch;
3436 if (op != '*' && op != '/' && op != '%')
3437 break;
3438 next();
aliguori376253e2009-03-05 23:01:23 +00003439 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003440 switch(op) {
3441 default:
3442 case '*':
3443 val *= val2;
3444 break;
3445 case '/':
3446 case '%':
ths5fafdf22007-09-16 21:08:06 +00003447 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003448 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003449 if (op == '/')
3450 val /= val2;
3451 else
3452 val %= val2;
3453 break;
3454 }
3455 }
3456 return val;
3457}
3458
aliguori376253e2009-03-05 23:01:23 +00003459static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003460{
blueswir1c2efc952007-09-25 17:28:42 +00003461 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003462 int op;
bellard9307c4c2004-04-04 12:57:25 +00003463
aliguori376253e2009-03-05 23:01:23 +00003464 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003465 for(;;) {
3466 op = *pch;
3467 if (op != '&' && op != '|' && op != '^')
3468 break;
3469 next();
aliguori376253e2009-03-05 23:01:23 +00003470 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003471 switch(op) {
3472 default:
3473 case '&':
3474 val &= val2;
3475 break;
3476 case '|':
3477 val |= val2;
3478 break;
3479 case '^':
3480 val ^= val2;
3481 break;
3482 }
3483 }
3484 return val;
3485}
3486
aliguori376253e2009-03-05 23:01:23 +00003487static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003488{
blueswir1c2efc952007-09-25 17:28:42 +00003489 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003490 int op;
bellard9307c4c2004-04-04 12:57:25 +00003491
aliguori376253e2009-03-05 23:01:23 +00003492 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003493 for(;;) {
3494 op = *pch;
3495 if (op != '+' && op != '-')
3496 break;
3497 next();
aliguori376253e2009-03-05 23:01:23 +00003498 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003499 if (op == '+')
3500 val += val2;
3501 else
3502 val -= val2;
3503 }
3504 return val;
3505}
3506
aliguori376253e2009-03-05 23:01:23 +00003507static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003508{
3509 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003510 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003511 *pp = pch;
3512 return -1;
3513 }
blueswir1cd390082008-11-16 13:53:32 +00003514 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003515 pch++;
aliguori376253e2009-03-05 23:01:23 +00003516 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003517 *pp = pch;
3518 return 0;
3519}
3520
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003521static int get_double(Monitor *mon, double *pval, const char **pp)
3522{
3523 const char *p = *pp;
3524 char *tailp;
3525 double d;
3526
3527 d = strtod(p, &tailp);
3528 if (tailp == p) {
3529 monitor_printf(mon, "Number expected\n");
3530 return -1;
3531 }
3532 if (d != d || d - d != 0) {
3533 /* NaN or infinity */
3534 monitor_printf(mon, "Bad number\n");
3535 return -1;
3536 }
3537 *pval = d;
3538 *pp = tailp;
3539 return 0;
3540}
3541
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003542/*
3543 * Store the command-name in cmdname, and return a pointer to
3544 * the remaining of the command string.
3545 */
3546static const char *get_command_name(const char *cmdline,
3547 char *cmdname, size_t nlen)
3548{
3549 size_t len;
3550 const char *p, *pstart;
3551
3552 p = cmdline;
3553 while (qemu_isspace(*p))
3554 p++;
3555 if (*p == '\0')
3556 return NULL;
3557 pstart = p;
3558 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3559 p++;
3560 len = p - pstart;
3561 if (len > nlen - 1)
3562 len = nlen - 1;
3563 memcpy(cmdname, pstart, len);
3564 cmdname[len] = '\0';
3565 return p;
3566}
3567
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003568/**
3569 * Read key of 'type' into 'key' and return the current
3570 * 'type' pointer.
3571 */
3572static char *key_get_info(const char *type, char **key)
3573{
3574 size_t len;
3575 char *p, *str;
3576
3577 if (*type == ',')
3578 type++;
3579
3580 p = strchr(type, ':');
3581 if (!p) {
3582 *key = NULL;
3583 return NULL;
3584 }
3585 len = p - type;
3586
Anthony Liguori7267c092011-08-20 22:09:37 -05003587 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003588 memcpy(str, type, len);
3589 str[len] = '\0';
3590
3591 *key = str;
3592 return ++p;
3593}
3594
bellard9307c4c2004-04-04 12:57:25 +00003595static int default_fmt_format = 'x';
3596static int default_fmt_size = 4;
3597
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003598static int is_valid_option(const char *c, const char *typestr)
3599{
3600 char option[3];
3601
3602 option[0] = '-';
3603 option[1] = *c;
3604 option[2] = '\0';
3605
3606 typestr = strstr(typestr, option);
3607 return (typestr != NULL);
3608}
3609
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003610static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3611 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003612{
3613 const mon_cmd_t *cmd;
3614
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003615 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003616 if (compare_cmd(cmdname, cmd->name)) {
3617 return cmd;
3618 }
3619 }
3620
3621 return NULL;
3622}
3623
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003624static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3625{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003626 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003627}
3628
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003629/*
Bandan Dasae502122015-06-03 18:38:08 -04003630 * Parse command name from @cmdp according to command table @table.
3631 * If blank, return NULL.
3632 * Else, if no valid command can be found, report to @mon, and return
3633 * NULL.
3634 * Else, change @cmdp to point right behind the name, and return its
3635 * command table entry.
3636 * Do not assume the return value points into @table! It doesn't when
3637 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003638 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003639static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04003640 const char **cmdp,
3641 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003642{
Bandan Dasae502122015-06-03 18:38:08 -04003643 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003644 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003645 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003646
bellard9307c4c2004-04-04 12:57:25 +00003647 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003648 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003649 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003650 return NULL;
ths3b46e622007-09-17 08:09:54 +00003651
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003652 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003653 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003654 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04003655 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003656 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003657 }
bellard9307c4c2004-04-04 12:57:25 +00003658
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003659 /* filter out following useless space */
3660 while (qemu_isspace(*p)) {
3661 p++;
3662 }
Bandan Dasae502122015-06-03 18:38:08 -04003663
3664 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003665 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003666 if (cmd->sub_table != NULL && *p != '\0') {
3667 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003668 }
3669
Bandan Dasae502122015-06-03 18:38:08 -04003670 return cmd;
3671}
3672
3673/*
3674 * Parse arguments for @cmd.
3675 * If it can't be parsed, report to @mon, and return NULL.
3676 * Else, insert command arguments into a QDict, and return it.
3677 * Note: On success, caller has to free the QDict structure.
3678 */
3679
3680static QDict *monitor_parse_arguments(Monitor *mon,
3681 const char **endp,
3682 const mon_cmd_t *cmd)
3683{
3684 const char *typestr;
3685 char *key;
3686 int c;
3687 const char *p = *endp;
3688 char buf[1024];
3689 QDict *qdict = qdict_new();
3690
bellard9307c4c2004-04-04 12:57:25 +00003691 /* parse the parameters */
3692 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003693 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003694 typestr = key_get_info(typestr, &key);
3695 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003696 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003697 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003698 typestr++;
3699 switch(c) {
3700 case 'F':
bellard81d09122004-07-14 17:21:37 +00003701 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003702 case 's':
3703 {
3704 int ret;
ths3b46e622007-09-17 08:09:54 +00003705
blueswir1cd390082008-11-16 13:53:32 +00003706 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003707 p++;
3708 if (*typestr == '?') {
3709 typestr++;
3710 if (*p == '\0') {
3711 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003712 break;
bellard9307c4c2004-04-04 12:57:25 +00003713 }
3714 }
3715 ret = get_str(buf, sizeof(buf), &p);
3716 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003717 switch(c) {
3718 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003719 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003720 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003721 break;
3722 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003723 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003724 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003725 break;
3726 default:
Bandan Dasae502122015-06-03 18:38:08 -04003727 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003728 break;
3729 }
bellard9307c4c2004-04-04 12:57:25 +00003730 goto fail;
3731 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003732 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003733 }
3734 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003735 case 'O':
3736 {
3737 QemuOptsList *opts_list;
3738 QemuOpts *opts;
3739
3740 opts_list = qemu_find_opts(key);
3741 if (!opts_list || opts_list->desc->name) {
3742 goto bad_type;
3743 }
3744 while (qemu_isspace(*p)) {
3745 p++;
3746 }
3747 if (!*p)
3748 break;
3749 if (get_str(buf, sizeof(buf), &p) < 0) {
3750 goto fail;
3751 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003752 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003753 if (!opts) {
3754 goto fail;
3755 }
3756 qemu_opts_to_qdict(opts, qdict);
3757 qemu_opts_del(opts);
3758 }
3759 break;
bellard9307c4c2004-04-04 12:57:25 +00003760 case '/':
3761 {
3762 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003763
blueswir1cd390082008-11-16 13:53:32 +00003764 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003765 p++;
3766 if (*p == '/') {
3767 /* format found */
3768 p++;
3769 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003770 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003771 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003772 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003773 count = count * 10 + (*p - '0');
3774 p++;
3775 }
3776 }
3777 size = -1;
3778 format = -1;
3779 for(;;) {
3780 switch(*p) {
3781 case 'o':
3782 case 'd':
3783 case 'u':
3784 case 'x':
3785 case 'i':
3786 case 'c':
3787 format = *p++;
3788 break;
3789 case 'b':
3790 size = 1;
3791 p++;
3792 break;
3793 case 'h':
3794 size = 2;
3795 p++;
3796 break;
3797 case 'w':
3798 size = 4;
3799 p++;
3800 break;
3801 case 'g':
3802 case 'L':
3803 size = 8;
3804 p++;
3805 break;
3806 default:
3807 goto next;
3808 }
3809 }
3810 next:
blueswir1cd390082008-11-16 13:53:32 +00003811 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003812 monitor_printf(mon, "invalid char in format: '%c'\n",
3813 *p);
bellard9307c4c2004-04-04 12:57:25 +00003814 goto fail;
3815 }
bellard9307c4c2004-04-04 12:57:25 +00003816 if (format < 0)
3817 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003818 if (format != 'i') {
3819 /* for 'i', not specifying a size gives -1 as size */
3820 if (size < 0)
3821 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003822 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003823 }
bellard9307c4c2004-04-04 12:57:25 +00003824 default_fmt_format = format;
3825 } else {
3826 count = 1;
3827 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003828 if (format != 'i') {
3829 size = default_fmt_size;
3830 } else {
3831 size = -1;
3832 }
bellard9307c4c2004-04-04 12:57:25 +00003833 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003834 qdict_put(qdict, "count", qint_from_int(count));
3835 qdict_put(qdict, "format", qint_from_int(format));
3836 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003837 }
3838 break;
3839 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003840 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003841 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003842 {
blueswir1c2efc952007-09-25 17:28:42 +00003843 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003844
blueswir1cd390082008-11-16 13:53:32 +00003845 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003846 p++;
bellard34405572004-06-08 00:55:58 +00003847 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003848 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003849 if (*p == '\0') {
3850 typestr++;
3851 break;
3852 }
bellard34405572004-06-08 00:55:58 +00003853 } else {
3854 if (*p == '.') {
3855 p++;
blueswir1cd390082008-11-16 13:53:32 +00003856 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003857 p++;
bellard34405572004-06-08 00:55:58 +00003858 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003859 typestr++;
3860 break;
bellard34405572004-06-08 00:55:58 +00003861 }
3862 }
bellard13224a82006-07-14 22:03:35 +00003863 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003864 }
aliguori376253e2009-03-05 23:01:23 +00003865 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003866 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003867 /* Check if 'i' is greater than 32-bit */
3868 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003869 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003870 monitor_printf(mon, "integer is for 32-bit values\n");
3871 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003872 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003873 if (val < 0) {
3874 monitor_printf(mon, "enter a positive value\n");
3875 goto fail;
3876 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003877 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003878 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003879 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003880 }
3881 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003882 case 'o':
3883 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003884 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003885 char *end;
3886
3887 while (qemu_isspace(*p)) {
3888 p++;
3889 }
3890 if (*typestr == '?') {
3891 typestr++;
3892 if (*p == '\0') {
3893 break;
3894 }
3895 }
3896 val = strtosz(p, &end);
3897 if (val < 0) {
3898 monitor_printf(mon, "invalid size\n");
3899 goto fail;
3900 }
3901 qdict_put(qdict, key, qint_from_int(val));
3902 p = end;
3903 }
3904 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003905 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003906 {
3907 double val;
3908
3909 while (qemu_isspace(*p))
3910 p++;
3911 if (*typestr == '?') {
3912 typestr++;
3913 if (*p == '\0') {
3914 break;
3915 }
3916 }
3917 if (get_double(mon, &val, &p) < 0) {
3918 goto fail;
3919 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003920 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003921 switch (*p) {
3922 case 'm':
3923 val /= 1e3; p += 2; break;
3924 case 'u':
3925 val /= 1e6; p += 2; break;
3926 case 'n':
3927 val /= 1e9; p += 2; break;
3928 }
3929 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003930 if (*p && !qemu_isspace(*p)) {
3931 monitor_printf(mon, "Unknown unit suffix\n");
3932 goto fail;
3933 }
3934 qdict_put(qdict, key, qfloat_from_double(val));
3935 }
3936 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003937 case 'b':
3938 {
3939 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003940 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003941
3942 while (qemu_isspace(*p)) {
3943 p++;
3944 }
3945 beg = p;
3946 while (qemu_isgraph(*p)) {
3947 p++;
3948 }
3949 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003950 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003951 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003952 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003953 } else {
3954 monitor_printf(mon, "Expected 'on' or 'off'\n");
3955 goto fail;
3956 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06003957 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003958 }
3959 break;
bellard9307c4c2004-04-04 12:57:25 +00003960 case '-':
3961 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003962 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003963 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003964 /* option */
ths3b46e622007-09-17 08:09:54 +00003965
bellard9307c4c2004-04-04 12:57:25 +00003966 c = *typestr++;
3967 if (c == '\0')
3968 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003969 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003970 p++;
bellard9307c4c2004-04-04 12:57:25 +00003971 if (*p == '-') {
3972 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003973 if(c != *p) {
3974 if(!is_valid_option(p, typestr)) {
3975
3976 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003977 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003978 goto fail;
3979 } else {
3980 skip_key = 1;
3981 }
bellard9307c4c2004-04-04 12:57:25 +00003982 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003983 if(skip_key) {
3984 p = tmp;
3985 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003986 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003987 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003988 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003989 }
bellard9307c4c2004-04-04 12:57:25 +00003990 }
bellard9307c4c2004-04-04 12:57:25 +00003991 }
3992 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003993 case 'S':
3994 {
3995 /* package all remaining string */
3996 int len;
3997
3998 while (qemu_isspace(*p)) {
3999 p++;
4000 }
4001 if (*typestr == '?') {
4002 typestr++;
4003 if (*p == '\0') {
4004 /* no remaining string: NULL argument */
4005 break;
4006 }
4007 }
4008 len = strlen(p);
4009 if (len <= 0) {
4010 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04004011 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04004012 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08004013 }
4014 qdict_put(qdict, key, qstring_from_str(p));
4015 p += len;
4016 }
4017 break;
bellard9307c4c2004-04-04 12:57:25 +00004018 default:
4019 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04004020 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00004021 goto fail;
4022 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004023 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004024 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004025 }
4026 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004027 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004028 p++;
4029 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004030 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04004031 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00004032 goto fail;
4033 }
4034
Bandan Dasae502122015-06-03 18:38:08 -04004035 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004036
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004037fail:
Bandan Dasae502122015-06-03 18:38:08 -04004038 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05004039 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004040 return NULL;
4041}
4042
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004043void monitor_set_error(Monitor *mon, QError *qerror)
4044{
4045 /* report only the first error */
4046 if (!mon->error) {
4047 mon->error = qerror;
4048 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004049 QDECREF(qerror);
4050 }
4051}
4052
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004053static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004054{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004055 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004056 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004057
Bandan Dasae502122015-06-03 18:38:08 -04004058 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4059 if (!cmd) {
4060 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004061 }
4062
Bandan Dasae502122015-06-03 18:38:08 -04004063 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4064 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04004065 monitor_printf(mon, "Try \"help %s\" for more information\n",
4066 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04004067 return;
4068 }
4069
4070 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004071 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004072}
4073
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004074static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004075{
4076 const char *p, *pstart;
4077 char cmd[128];
4078 int len;
4079
4080 p = list;
4081 for(;;) {
4082 pstart = p;
4083 p = strchr(p, '|');
4084 if (!p)
4085 p = pstart + strlen(pstart);
4086 len = p - pstart;
4087 if (len > sizeof(cmd) - 2)
4088 len = sizeof(cmd) - 2;
4089 memcpy(cmd, pstart, len);
4090 cmd[len] = '\0';
4091 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004092 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004093 }
4094 if (*p == '\0')
4095 break;
4096 p++;
4097 }
4098}
4099
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004100static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004101{
4102 DIR *ffs;
4103 struct dirent *d;
4104 char path[1024];
4105 char file[1024], file_prefix[1024];
4106 int input_path_len;
4107 const char *p;
4108
ths5fafdf22007-09-16 21:08:06 +00004109 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004110 if (!p) {
4111 input_path_len = 0;
4112 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004113 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004114 } else {
4115 input_path_len = p - input + 1;
4116 memcpy(path, input, input_path_len);
4117 if (input_path_len > sizeof(path) - 1)
4118 input_path_len = sizeof(path) - 1;
4119 path[input_path_len] = '\0';
4120 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4121 }
Bandan Das19f2db52015-06-03 18:38:07 -04004122
bellard81d09122004-07-14 17:21:37 +00004123 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 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004700
4701 /* if the line ends with a space, it means we want to complete the
4702 next arg */
4703 len = strlen(cmdline);
4704 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4705 if (nb_args >= MAX_ARGS) {
4706 goto cleanup;
4707 }
4708 args[nb_args++] = g_strdup("");
4709 }
4710
4711 /* 2. auto complete according to args */
4712 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004713
4714cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004715 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004716}
4717
aliguori731b0362009-03-05 23:01:42 +00004718static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004719{
aliguori731b0362009-03-05 23:01:42 +00004720 Monitor *mon = opaque;
4721
Jan Kiszkac62313b2009-12-04 14:05:29 +01004722 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004723}
4724
Markus Armbruster40861822015-05-29 10:27:16 +02004725static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4726 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004727{
Eric Blake2d5a8342015-04-15 09:19:23 -06004728 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004729
4730 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004731 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4732 "Capabilities negotiation is already complete, command "
4733 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004734 return true;
4735 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004736 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004737 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4738 "Expecting capabilities negotiation with "
4739 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004740 return true;
4741 }
4742 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004743}
4744
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004745/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004746 * Argument validation rules:
4747 *
4748 * 1. The argument must exist in cmd_args qdict
4749 * 2. The argument type must be the expected one
4750 *
4751 * Special case: If the argument doesn't exist in cmd_args and
4752 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4753 * checking is skipped for it.
4754 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004755static void check_client_args_type(const QDict *client_args,
4756 const QDict *cmd_args, int flags,
4757 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004758{
4759 const QDictEntry *ent;
4760
4761 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4762 QObject *obj;
4763 QString *arg_type;
4764 const QObject *client_arg = qdict_entry_value(ent);
4765 const char *client_arg_name = qdict_entry_key(ent);
4766
4767 obj = qdict_get(cmd_args, client_arg_name);
4768 if (!obj) {
4769 if (flags & QMP_ACCEPT_UNKNOWNS) {
4770 /* handler accepts unknowns */
4771 continue;
4772 }
4773 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004774 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004775 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004776 }
4777
4778 arg_type = qobject_to_qstring(obj);
4779 assert(arg_type != NULL);
4780
4781 /* check if argument's type is correct */
4782 switch (qstring_get_str(arg_type)[0]) {
4783 case 'F':
4784 case 'B':
4785 case 's':
4786 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004787 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4788 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01004789 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004790 }
4791 break;
4792 case 'i':
4793 case 'l':
4794 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004795 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004796 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004797 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4798 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01004799 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004800 }
4801 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004802 case 'T':
4803 if (qobject_type(client_arg) != QTYPE_QINT &&
4804 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004805 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4806 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01004807 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004808 }
4809 break;
4810 case 'b':
4811 case '-':
4812 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004813 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4814 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01004815 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004816 }
4817 break;
4818 case 'O':
4819 assert(flags & QMP_ACCEPT_UNKNOWNS);
4820 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004821 case 'q':
4822 /* Any QObject can be passed. */
4823 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004824 case '/':
4825 case '.':
4826 /*
4827 * These types are not supported by QMP and thus are not
4828 * handled here. Fall through.
4829 */
4830 default:
4831 abort();
4832 }
4833 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004834}
4835
4836/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004837 * - Check if the client has passed all mandatory args
4838 * - Set special flags for argument validation
4839 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004840static void check_mandatory_args(const QDict *cmd_args,
4841 const QDict *client_args, int *flags,
4842 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004843{
4844 const QDictEntry *ent;
4845
4846 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4847 const char *cmd_arg_name = qdict_entry_key(ent);
4848 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4849 assert(type != NULL);
4850
4851 if (qstring_get_str(type)[0] == 'O') {
4852 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4853 *flags |= QMP_ACCEPT_UNKNOWNS;
4854 } else if (qstring_get_str(type)[0] != '-' &&
4855 qstring_get_str(type)[1] != '?' &&
4856 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004857 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004858 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004859 }
4860 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004861}
4862
4863static QDict *qdict_from_args_type(const char *args_type)
4864{
4865 int i;
4866 QDict *qdict;
4867 QString *key, *type, *cur_qs;
4868
4869 assert(args_type != NULL);
4870
4871 qdict = qdict_new();
4872
4873 if (args_type == NULL || args_type[0] == '\0') {
4874 /* no args, empty qdict */
4875 goto out;
4876 }
4877
4878 key = qstring_new();
4879 type = qstring_new();
4880
4881 cur_qs = key;
4882
4883 for (i = 0;; i++) {
4884 switch (args_type[i]) {
4885 case ',':
4886 case '\0':
4887 qdict_put(qdict, qstring_get_str(key), type);
4888 QDECREF(key);
4889 if (args_type[i] == '\0') {
4890 goto out;
4891 }
4892 type = qstring_new(); /* qdict has ref */
4893 cur_qs = key = qstring_new();
4894 break;
4895 case ':':
4896 cur_qs = type;
4897 break;
4898 default:
4899 qstring_append_chr(cur_qs, args_type[i]);
4900 break;
4901 }
4902 }
4903
4904out:
4905 return qdict;
4906}
4907
4908/*
4909 * Client argument checking rules:
4910 *
4911 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004912 * 2. Each argument provided by the client must be expected
4913 * 3. Each argument provided by the client must have the type expected
4914 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004915 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004916static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4917 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004918{
Markus Armbruster326283a2015-03-02 18:39:09 +01004919 Error *err = NULL;
4920 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004921 QDict *cmd_args;
4922
4923 cmd_args = qdict_from_args_type(cmd->args_type);
4924
4925 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004926 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004927 if (err) {
4928 goto out;
4929 }
4930
Markus Armbruster326283a2015-03-02 18:39:09 +01004931 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004932
4933out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004934 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004935 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004936}
4937
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004938/*
4939 * Input object checking rules
4940 *
4941 * 1. Input object must be a dict
4942 * 2. The "execute" key must exist
4943 * 3. The "execute" key must be a string
4944 * 4. If the "arguments" key exists, it must be a dict
4945 * 5. If the "id" key exists, it can be anything (ie. json-value)
4946 * 6. Any argument not listed above is considered invalid
4947 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004948static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004949{
4950 const QDictEntry *ent;
4951 int has_exec_key = 0;
4952 QDict *input_dict;
4953
4954 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004955 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004956 return NULL;
4957 }
4958
4959 input_dict = qobject_to_qdict(input_obj);
4960
4961 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4962 const char *arg_name = qdict_entry_key(ent);
4963 const QObject *arg_obj = qdict_entry_value(ent);
4964
4965 if (!strcmp(arg_name, "execute")) {
4966 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004967 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4968 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004969 return NULL;
4970 }
4971 has_exec_key = 1;
4972 } else if (!strcmp(arg_name, "arguments")) {
4973 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004974 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4975 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004976 return NULL;
4977 }
Markus Armbruster779cec42015-06-08 10:44:30 +02004978 } else if (!strcmp(arg_name, "id")) {
4979 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004980 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004981 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004982 return NULL;
4983 }
4984 }
4985
4986 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004987 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004988 return NULL;
4989 }
4990
4991 return input_dict;
4992}
4993
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004994static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4995{
Markus Armbruster326283a2015-03-02 18:39:09 +01004996 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004997 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004998 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004999 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005000 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005001 Monitor *mon = cur_mon;
5002
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005003 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01005004 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005005
5006 obj = json_parser_parse(tokens, NULL);
5007 if (!obj) {
5008 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005009 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005010 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005011 }
5012
Markus Armbrusterba0510a2015-03-02 18:41:43 +01005013 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005014 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005015 qobject_decref(obj);
5016 goto err_out;
5017 }
5018
Markus Armbruster74358f22015-03-06 19:35:59 +01005019 mon->qmp.id = qdict_get(input, "id");
5020 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005021
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005022 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005023 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005024 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005025 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005026 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5027 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005028 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005029 }
Markus Armbruster40861822015-05-29 10:27:16 +02005030 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005031 goto err_out;
5032 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005033
5034 obj = qdict_get(input, "arguments");
5035 if (!obj) {
5036 args = qdict_new();
5037 } else {
5038 args = qobject_to_qdict(obj);
5039 QINCREF(args);
5040 }
5041
Markus Armbruster326283a2015-03-02 18:39:09 +01005042 qmp_check_client_args(cmd, args, &local_err);
5043 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005044 goto err_out;
5045 }
5046
Markus Armbruster84add862015-03-05 16:45:15 +01005047 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5048 /* Command failed... */
Markus Armbruster452e0302015-03-06 19:11:13 +01005049 if (!mon->error) {
Markus Armbruster84add862015-03-05 16:45:15 +01005050 /* ... without setting an error, so make one up */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005051 error_setg(&local_err, QERR_UNDEFINED_ERROR);
Markus Armbruster84add862015-03-05 16:45:15 +01005052 }
5053 }
Markus Armbruster710aec92015-03-06 11:28:00 +01005054 if (mon->error) {
5055 error_set(&local_err, mon->error->err_class, "%s",
5056 mon->error->err_msg);
5057 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005058
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005059err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005060 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005061 qobject_decref(data);
Markus Armbruster70ea0c52015-03-06 10:47:08 +01005062 QDECREF(mon->error);
5063 mon->error = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005064 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005065 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005066}
5067
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005068static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005069{
5070 Monitor *old_mon = cur_mon;
5071
5072 cur_mon = opaque;
5073
Markus Armbruster74358f22015-03-06 19:35:59 +01005074 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005075
5076 cur_mon = old_mon;
5077}
5078
aliguori731b0362009-03-05 23:01:42 +00005079static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005080{
aliguori731b0362009-03-05 23:01:42 +00005081 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005082 int i;
aliguori376253e2009-03-05 23:01:23 +00005083
aliguori731b0362009-03-05 23:01:42 +00005084 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005085
aliguoricde76ee2009-03-05 23:01:51 +00005086 if (cur_mon->rs) {
5087 for (i = 0; i < size; i++)
5088 readline_handle_byte(cur_mon->rs, buf[i]);
5089 } else {
5090 if (size == 0 || buf[size - 1] != 0)
5091 monitor_printf(cur_mon, "corrupted command\n");
5092 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005093 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005094 }
aliguori731b0362009-03-05 23:01:42 +00005095
5096 cur_mon = old_mon;
5097}
aliguorid8f44602008-10-06 13:52:44 +00005098
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005099static void monitor_command_cb(void *opaque, const char *cmdline,
5100 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005101{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005102 Monitor *mon = opaque;
5103
aliguori731b0362009-03-05 23:01:42 +00005104 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005105 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005106 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005107}
5108
aliguoricde76ee2009-03-05 23:01:51 +00005109int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005110{
aliguoricde76ee2009-03-05 23:01:51 +00005111 if (!mon->rs)
5112 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005113 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005114 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005115}
5116
aliguori376253e2009-03-05 23:01:23 +00005117void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005118{
aliguoricde76ee2009-03-05 23:01:51 +00005119 if (!mon->rs)
5120 return;
aliguori731b0362009-03-05 23:01:42 +00005121 if (--mon->suspend_cnt == 0)
5122 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005123}
5124
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005125static QObject *get_qmp_greeting(void)
5126{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005127 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005128
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005129 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005130 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5131}
5132
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005133static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005134{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005135 QObject *data;
5136 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005137
Luiz Capitulino47116d12010-02-08 17:01:30 -02005138 switch (event) {
5139 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005140 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005141 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005142 monitor_json_emitter(mon, data);
5143 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005144 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005145 break;
5146 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005147 json_message_parser_destroy(&mon->qmp.parser);
5148 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005149 mon_refcount--;
5150 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005151 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005152 }
5153}
5154
aliguori731b0362009-03-05 23:01:42 +00005155static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005156{
aliguori376253e2009-03-05 23:01:23 +00005157 Monitor *mon = opaque;
5158
aliguori2724b182009-03-05 23:01:47 +00005159 switch (event) {
5160 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005161 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005162 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005163 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005164 if (mon->reset_seen) {
5165 readline_restart(mon->rs);
5166 monitor_resume(mon);
5167 monitor_flush(mon);
5168 } else {
5169 mon->suspend_cnt = 0;
5170 }
aliguori2724b182009-03-05 23:01:47 +00005171 break;
ths86e94de2007-01-05 22:01:59 +00005172
aliguori2724b182009-03-05 23:01:47 +00005173 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005174 if (mon->reset_seen) {
5175 if (mon->suspend_cnt == 0) {
5176 monitor_printf(mon, "\n");
5177 }
5178 monitor_flush(mon);
5179 monitor_suspend(mon);
5180 } else {
5181 mon->suspend_cnt++;
5182 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005183 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005184 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005185 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005186 break;
5187
Amit Shahb6b8df52009-10-07 18:31:16 +05305188 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005189 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5190 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005191 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005192 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005193 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005194 }
5195 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005196 mon_refcount++;
5197 break;
5198
5199 case CHR_EVENT_CLOSED:
5200 mon_refcount--;
5201 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005202 break;
5203 }
ths86e94de2007-01-05 22:01:59 +00005204}
5205
Wayne Xia816f8922011-10-12 11:32:41 +08005206static int
5207compare_mon_cmd(const void *a, const void *b)
5208{
5209 return strcmp(((const mon_cmd_t *)a)->name,
5210 ((const mon_cmd_t *)b)->name);
5211}
5212
5213static void sortcmdlist(void)
5214{
5215 int array_num;
5216 int elem_size = sizeof(mon_cmd_t);
5217
5218 array_num = sizeof(mon_cmds)/elem_size-1;
5219 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5220
5221 array_num = sizeof(info_cmds)/elem_size-1;
5222 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5223}
5224
aliguori76655d62009-03-06 20:27:37 +00005225
5226/*
5227 * Local variables:
5228 * c-indent-level: 4
5229 * c-basic-offset: 4
5230 * tab-width: 8
5231 * End:
5232 */
5233
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005234/* These functions just adapt the readline interface in a typesafe way. We
5235 * could cast function pointers but that discards compiler checks.
5236 */
Stefan Weild5d15072014-01-25 18:18:23 +01005237static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5238 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005239{
5240 va_list ap;
5241 va_start(ap, fmt);
5242 monitor_vprintf(opaque, fmt, ap);
5243 va_end(ap);
5244}
5245
5246static void monitor_readline_flush(void *opaque)
5247{
5248 monitor_flush(opaque);
5249}
5250
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005251static void __attribute__((constructor)) monitor_lock_init(void)
5252{
5253 qemu_mutex_init(&monitor_lock);
5254}
5255
aliguori731b0362009-03-05 23:01:42 +00005256void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005257{
aliguori731b0362009-03-05 23:01:42 +00005258 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005259 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005260
5261 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005262 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005263 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005264 is_first_init = 0;
5265 }
aliguori87127162009-03-05 23:01:29 +00005266
Wenchao Xiab01fe892013-08-27 20:38:19 +08005267 mon = g_malloc(sizeof(*mon));
5268 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005269
aliguori87127162009-03-05 23:01:29 +00005270 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005271 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005272 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005273 mon->rs = readline_init(monitor_readline_printf,
5274 monitor_readline_flush,
5275 mon,
5276 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005277 monitor_read_command(mon, 0);
5278 }
aliguori87127162009-03-05 23:01:29 +00005279
Markus Armbruster9f3982f2015-03-06 19:56:38 +01005280 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005281 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5282 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005283 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005284 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005285 } else {
5286 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5287 monitor_event, mon);
5288 }
aliguori87127162009-03-05 23:01:29 +00005289
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005290 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005291 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005292 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00005293}
5294
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005295static void bdrv_password_cb(void *opaque, const char *password,
5296 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005297{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005298 Monitor *mon = opaque;
5299 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005300 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005301 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005302
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005303 bdrv_add_key(bs, password, &local_err);
5304 if (local_err) {
5305 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5306 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005307 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005308 }
aliguori731b0362009-03-05 23:01:42 +00005309 if (mon->password_completion_cb)
5310 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005311
aliguori731b0362009-03-05 23:01:42 +00005312 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005313}
aliguoric0f4ce72009-03-05 23:01:01 +00005314
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005315int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005316 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005317 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005318{
aliguoricde76ee2009-03-05 23:01:51 +00005319 int err;
5320
aliguori376253e2009-03-05 23:01:23 +00005321 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5322 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005323
aliguori731b0362009-03-05 23:01:42 +00005324 mon->password_completion_cb = completion_cb;
5325 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005326
aliguoricde76ee2009-03-05 23:01:51 +00005327 err = monitor_read_password(mon, bdrv_password_cb, bs);
5328
5329 if (err && completion_cb)
5330 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005331
5332 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005333}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005334
5335int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005336 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005337 void *opaque)
5338{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005339 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005340 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005341
Fam Zheng55606252015-03-02 19:36:46 +08005342 blk = blk_by_name(device);
5343 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005344 monitor_printf(mon, "Device not found %s\n", device);
5345 return -1;
5346 }
5347
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005348 bdrv_add_key(blk_bs(blk), NULL, &err);
5349 if (err) {
5350 error_free(err);
5351 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5352 }
5353
5354 if (completion_cb) {
5355 completion_cb(opaque, 0);
5356 }
5357 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005358}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005359
5360QemuOptsList qemu_mon_opts = {
5361 .name = "mon",
5362 .implied_opt_name = "chardev",
5363 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5364 .desc = {
5365 {
5366 .name = "mode",
5367 .type = QEMU_OPT_STRING,
5368 },{
5369 .name = "chardev",
5370 .type = QEMU_OPT_STRING,
5371 },{
5372 .name = "default",
5373 .type = QEMU_OPT_BOOL,
5374 },{
5375 .name = "pretty",
5376 .type = QEMU_OPT_BOOL,
5377 },
5378 { /* end of list */ }
5379 },
5380};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005381
5382#ifndef TARGET_I386
5383void qmp_rtc_reset_reinjection(Error **errp)
5384{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005385 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005386}
5387#endif