blob: 79de805e160d11eaf977f0921a0f3d4f1ccbd5c6 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080076#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000077
Markus Armbruster1ce6be22015-02-06 14:18:24 +010078/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020079#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010080#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020081#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083
bellard9dc39cb2004-03-14 21:38:27 +000084//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000085//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000086
bellard9307c4c2004-04-04 12:57:25 +000087/*
88 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000089 *
bellard9307c4c2004-04-04 12:57:25 +000090 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000091 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000092 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080093 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010094 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000099 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200103 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000111 * '/' optional gdb-like print format (like "/10x")
112 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100115 * 'b' boolean
116 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000118 *
119 */
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000123 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *params;
125 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800130 /* @sub_table is a list of 2nd level of commands. If it do not exist,
131 * mhandler should be used. If it exist, sub_table[?].mhandler should be
132 * used, and mhandler of 1st level plays the role of help function.
133 */
134 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100135 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500136} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000137
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500139typedef struct mon_fd_t mon_fd_t;
140struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141 char *name;
142 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144};
145
Corey Bryantba1c0482012-08-14 16:43:43 -0400146/* file descriptor associated with a file descriptor set */
147typedef struct MonFdsetFd MonFdsetFd;
148struct MonFdsetFd {
149 int fd;
150 bool removed;
151 char *opaque;
152 QLIST_ENTRY(MonFdsetFd) next;
153};
154
155/* file descriptor set containing fds passed via SCM_RIGHTS */
156typedef struct MonFdset MonFdset;
157struct MonFdset {
158 int64_t id;
159 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400160 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400161 QLIST_ENTRY(MonFdset) next;
162};
163
Markus Armbruster74358f22015-03-06 19:35:59 +0100164typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165 QObject *id;
166 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100167 /*
168 * When a client connects, we're in capabilities negotiation mode.
169 * When command qmp_capabilities succeeds, we go into command
170 * mode.
171 */
172 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100173} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200174
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100175/*
176 * To prevent flooding clients, events can be throttled. The
177 * throttling is calculated globally, rather than per-Monitor
178 * instance.
179 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200180typedef struct MonitorQAPIEventState {
181 QAPIEvent event; /* Event being tracked */
182 int64_t rate; /* Minimum time (in ns) between two events */
183 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100184 QEMUTimer *timer; /* Timer for handling delayed events */
185 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200186} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187
aliguori87127162009-03-05 23:01:29 +0000188struct Monitor {
189 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200190 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000191 int flags;
192 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400193 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200194
195 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400196 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200197 guint out_watch;
198
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
200 int mux_out;
201
aliguori731b0362009-03-05 23:01:42 +0000202 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100203 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200204 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200205 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000206 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800207 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200208 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500209 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000210 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000211};
212
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300213/* QMP checker flags */
214#define QMP_ACCEPT_UNKNOWNS 1
215
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200216/* Protects mon_list, monitor_event_state. */
217static QemuMutex monitor_lock;
218
Blue Swirl72cf2d42009-09-12 07:36:22 +0000219static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400220static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400221static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000222
Wayne Xia816f8922011-10-12 11:32:41 +0800223static mon_cmd_t mon_cmds[];
224static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000225
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300226static const mon_cmd_t qmp_cmds[];
227
Markus Armbruster8631b602010-02-18 11:41:55 +0100228Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000229
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100230static void monitor_command_cb(void *opaque, const char *cmdline,
231 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000232
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100233/**
234 * Is @mon a QMP monitor?
235 */
236static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200237{
238 return (mon->flags & MONITOR_USE_CONTROL);
239}
240
Markus Armbruster489653b2015-03-06 20:01:05 +0100241/**
242 * Is the current monitor, if any, a QMP monitor?
243 */
244bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100245{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100246 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100247}
248
Anthony Liguori7060b472011-09-02 12:34:50 -0500249void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000250{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200251 if (!mon->rs)
252 return;
253
aliguori731b0362009-03-05 23:01:42 +0000254 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
255 if (show_prompt)
256 readline_show_prompt(mon->rs);
257}
bellard6a00d602005-11-21 23:25:50 +0000258
Anthony Liguori7060b472011-09-02 12:34:50 -0500259int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
260 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000261{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100262 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000263 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
264 /* prompt is printed on return from the command handler */
265 return 0;
266 } else {
267 monitor_printf(mon, "terminal does not support password prompting\n");
268 return -ENOTTY;
269 }
aliguoribb5fc202009-03-05 23:01:15 +0000270}
271
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200272static void monitor_flush_locked(Monitor *mon);
273
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100274static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
275 void *opaque)
276{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200277 Monitor *mon = opaque;
278
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200279 qemu_mutex_lock(&mon->out_lock);
280 mon->out_watch = 0;
281 monitor_flush_locked(mon);
282 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100283 return FALSE;
284}
285
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200286/* Called with mon->out_lock held. */
287static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000288{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400290 size_t len;
291 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100292
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400293 if (mon->skip_flush) {
294 return;
295 }
296
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400297 buf = qstring_get_str(mon->outbuf);
298 len = qstring_get_length(mon->outbuf);
299
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200300 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200302 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
303 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400304 QDECREF(mon->outbuf);
305 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100306 return;
307 }
308 if (rc > 0) {
309 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400310 QString *tmp = qstring_from_str(buf + rc);
311 QDECREF(mon->outbuf);
312 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100313 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200314 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200315 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200316 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200317 }
bellard7e2515e2004-08-01 21:52:19 +0000318 }
319}
320
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200321void monitor_flush(Monitor *mon)
322{
323 qemu_mutex_lock(&mon->out_lock);
324 monitor_flush_locked(mon);
325 qemu_mutex_unlock(&mon->out_lock);
326}
327
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400328/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000329static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000330{
ths60fe76f2007-12-16 03:02:09 +0000331 char c;
aliguori731b0362009-03-05 23:01:42 +0000332
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200333 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000334 for(;;) {
335 c = *str++;
336 if (c == '\0')
337 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400338 if (c == '\n') {
339 qstring_append_chr(mon->outbuf, '\r');
340 }
341 qstring_append_chr(mon->outbuf, c);
342 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400344 }
bellard7e2515e2004-08-01 21:52:19 +0000345 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200346 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000347}
348
aliguori376253e2009-03-05 23:01:23 +0000349void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000350{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400351 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200352
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200353 if (!mon)
354 return;
355
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100356 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200358 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400360 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400362 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000363}
364
aliguori376253e2009-03-05 23:01:23 +0000365void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000366{
367 va_list ap;
368 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000369 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000370 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000371}
372
Stefan Weil8b7968f2010-09-23 21:28:05 +0200373static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
374 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000375{
376 va_list ap;
377 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000378 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000379 va_end(ap);
380 return 0;
381}
382
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200383static void monitor_json_emitter(Monitor *mon, const QObject *data)
384{
385 QString *json;
386
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200387 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
388 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200389 assert(json != NULL);
390
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200391 qstring_append_chr(json, '\n');
392 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200393
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200394 QDECREF(json);
395}
396
Markus Armbruster710aec92015-03-06 11:28:00 +0100397static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300398{
399 QObject *obj;
400
Markus Armbruster710aec92015-03-06 11:28:00 +0100401 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
402 ErrorClass_lookup[error_get_class(err)],
403 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300404
405 return qobject_to_qdict(obj);
406}
407
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100408static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100409 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200410{
411 QDict *qmp;
412
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100413 trace_monitor_protocol_emitter(mon);
414
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100415 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200416 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300417 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418 if (data) {
419 qobject_incref(data);
420 qdict_put_obj(qmp, "return", data);
421 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200422 /* return an empty QDict by default */
423 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 }
425 } else {
426 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100427 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200428 }
429
Markus Armbruster74358f22015-03-06 19:35:59 +0100430 if (mon->qmp.id) {
431 qdict_put_obj(qmp, "id", mon->qmp.id);
432 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200433 }
434
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200435 monitor_json_emitter(mon, QOBJECT(qmp));
436 QDECREF(qmp);
437}
438
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200440static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100441
442/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200443 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200444 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100445 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200446static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100447{
448 Monitor *mon;
449
450 trace_monitor_protocol_event_emit(event, data);
451 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100452 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100453 monitor_json_emitter(mon, data);
454 }
455 }
456}
457
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458/*
459 * Queue a new event for emission to Monitor instances,
460 * applying any rate limiting if required.
461 */
462static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200463monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200465 MonitorQAPIEventState *evstate;
466 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100467 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100468
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200469 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470 trace_monitor_protocol_event_queue(event,
471 data,
472 evstate->rate,
473 evstate->last,
474 now);
475
476 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200477 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100478 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200479 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100480 evstate->last = now;
481 } else {
482 int64_t delta = now - evstate->last;
483 if (evstate->data ||
484 delta < evstate->rate) {
485 /* If there's an existing event pending, replace
486 * it with the new event, otherwise schedule a
487 * timer for delayed emission
488 */
489 if (evstate->data) {
490 qobject_decref(evstate->data);
491 } else {
492 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100493 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100494 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200495 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 qobject_incref(evstate->data);
497 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200498 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100499 evstate->last = now;
500 }
501 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200502 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100503}
504
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100505/*
506 * The callback invoked by QemuTimer when a delayed
507 * event is ready to be emitted
508 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200509static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200511 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100512 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100513
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514 trace_monitor_protocol_event_handler(evstate->event,
515 evstate->data,
516 evstate->last,
517 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200518 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100519 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200520 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100521 qobject_decref(evstate->data);
522 evstate->data = NULL;
523 }
524 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200525 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100526}
527
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100528/*
529 * @event: the event ID to be limited
530 * @rate: the rate limit in milliseconds
531 *
532 * Sets a rate limit on a particular event, so no
533 * more than 1 event will be emitted within @rate
534 * milliseconds
535 */
536static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200537monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100538{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200539 MonitorQAPIEventState *evstate;
540 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100541
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200542 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543
544 trace_monitor_protocol_event_throttle(event, rate);
545 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700546 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100547 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548 evstate->last = 0;
549 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200550 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
551 SCALE_MS,
552 monitor_qapi_event_handler,
553 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554}
555
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200556static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100557{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200558 /* Limit guest-triggerable events to 1 per second */
559 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200560 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200564 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100565
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200566 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200567}
568
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200569static int do_qmp_capabilities(Monitor *mon, const QDict *params,
570 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200571{
Markus Armbrusterf994b252015-03-06 19:51:51 +0100572 mon->qmp.in_command_mode = true;
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200573 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200574}
575
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100576static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200577
Wenchao Xiab01fe892013-08-27 20:38:19 +0800578static void monitor_data_init(Monitor *mon)
579{
580 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200581 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800582 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800583 /* Use *mon_cmds by default. */
584 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800585}
586
587static void monitor_data_destroy(Monitor *mon)
588{
589 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200590 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800591}
592
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
594 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200596 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
Wenchao Xiab01fe892013-08-27 20:38:19 +0800599 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400600 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200601
602 old_mon = cur_mon;
603 cur_mon = &hmp;
604
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200605 if (has_cpu_index) {
606 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200607 if (ret < 0) {
608 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200609 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
610 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200611 goto out;
612 }
613 }
614
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100615 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616 cur_mon = old_mon;
617
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200618 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400619 if (qstring_get_length(hmp.outbuf) > 0) {
620 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200621 } else {
622 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200624 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625
626out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800627 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200628 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200629}
630
bellard9dc39cb2004-03-14 21:38:27 +0000631static int compare_cmd(const char *name, const char *list)
632{
633 const char *p, *pstart;
634 int len;
635 len = strlen(name);
636 p = list;
637 for(;;) {
638 pstart = p;
639 p = strchr(p, '|');
640 if (!p)
641 p = pstart + strlen(pstart);
642 if ((p - pstart) == len && !memcmp(pstart, name, len))
643 return 1;
644 if (*p == '\0')
645 break;
646 p++;
647 }
648 return 0;
649}
650
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800651static int get_str(char *buf, int buf_size, const char **pp)
652{
653 const char *p;
654 char *q;
655 int c;
656
657 q = buf;
658 p = *pp;
659 while (qemu_isspace(*p)) {
660 p++;
661 }
662 if (*p == '\0') {
663 fail:
664 *q = '\0';
665 *pp = p;
666 return -1;
667 }
668 if (*p == '\"') {
669 p++;
670 while (*p != '\0' && *p != '\"') {
671 if (*p == '\\') {
672 p++;
673 c = *p++;
674 switch (c) {
675 case 'n':
676 c = '\n';
677 break;
678 case 'r':
679 c = '\r';
680 break;
681 case '\\':
682 case '\'':
683 case '\"':
684 break;
685 default:
686 qemu_printf("unsupported escape code: '\\%c'\n", c);
687 goto fail;
688 }
689 if ((q - buf) < buf_size - 1) {
690 *q++ = c;
691 }
692 } else {
693 if ((q - buf) < buf_size - 1) {
694 *q++ = *p;
695 }
696 p++;
697 }
698 }
699 if (*p != '\"') {
700 qemu_printf("unterminated string\n");
701 goto fail;
702 }
703 p++;
704 } else {
705 while (*p != '\0' && !qemu_isspace(*p)) {
706 if ((q - buf) < buf_size - 1) {
707 *q++ = *p;
708 }
709 p++;
710 }
711 }
712 *q = '\0';
713 *pp = p;
714 return 0;
715}
716
717#define MAX_ARGS 16
718
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800719static void free_cmdline_args(char **args, int nb_args)
720{
721 int i;
722
723 assert(nb_args <= MAX_ARGS);
724
725 for (i = 0; i < nb_args; i++) {
726 g_free(args[i]);
727 }
728
729}
730
731/*
732 * Parse the command line to get valid args.
733 * @cmdline: command line to be parsed.
734 * @pnb_args: location to store the number of args, must NOT be NULL.
735 * @args: location to store the args, which should be freed by caller, must
736 * NOT be NULL.
737 *
738 * Returns 0 on success, negative on failure.
739 *
740 * NOTE: this parser is an approximate form of the real command parser. Number
741 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
742 * return with failure.
743 */
744static int parse_cmdline(const char *cmdline,
745 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800746{
747 const char *p;
748 int nb_args, ret;
749 char buf[1024];
750
751 p = cmdline;
752 nb_args = 0;
753 for (;;) {
754 while (qemu_isspace(*p)) {
755 p++;
756 }
757 if (*p == '\0') {
758 break;
759 }
760 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800762 }
763 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800764 if (ret < 0) {
765 goto fail;
766 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767 args[nb_args] = g_strdup(buf);
768 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800769 }
770 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800771 return 0;
772
773 fail:
774 free_cmdline_args(args, nb_args);
775 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800776}
777
Wenchao Xia66855492013-08-27 20:38:23 +0800778static void help_cmd_dump_one(Monitor *mon,
779 const mon_cmd_t *cmd,
780 char **prefix_args,
781 int prefix_args_nb)
782{
783 int i;
784
785 for (i = 0; i < prefix_args_nb; i++) {
786 monitor_printf(mon, "%s ", prefix_args[i]);
787 }
788 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
789}
790
791/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500792static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800793 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000794{
Anthony Liguoric227f092009-10-01 16:12:16 -0500795 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000796
Wenchao Xia66855492013-08-27 20:38:23 +0800797 /* No valid arg need to compare with, dump all in *cmds */
798 if (arg_index >= nb_args) {
799 for (cmd = cmds; cmd->name != NULL; cmd++) {
800 help_cmd_dump_one(mon, cmd, args, arg_index);
801 }
802 return;
803 }
804
805 /* Find one entry to dump */
806 for (cmd = cmds; cmd->name != NULL; cmd++) {
807 if (compare_cmd(args[arg_index], cmd->name)) {
808 if (cmd->sub_table) {
809 /* continue with next arg */
810 help_cmd_dump(mon, cmd->sub_table,
811 args, nb_args, arg_index + 1);
812 } else {
813 help_cmd_dump_one(mon, cmd, args, arg_index);
814 }
815 break;
816 }
bellard9dc39cb2004-03-14 21:38:27 +0000817 }
818}
819
aliguori376253e2009-03-05 23:01:23 +0000820static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000821{
Wenchao Xia66855492013-08-27 20:38:23 +0800822 char *args[MAX_ARGS];
823 int nb_args = 0;
824
825 /* 1. parse user input */
826 if (name) {
827 /* special case for log, directly dump and return */
828 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "Log items (comma separated):\n");
831 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000832 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000834 }
Wenchao Xia66855492013-08-27 20:38:23 +0800835 return;
836 }
837
838 if (parse_cmdline(name, &nb_args, args) < 0) {
839 return;
bellardf193c792004-03-21 17:06:25 +0000840 }
bellard9dc39cb2004-03-14 21:38:27 +0000841 }
Wenchao Xia66855492013-08-27 20:38:23 +0800842
843 /* 2. dump the contents according to parsed args */
844 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
845
846 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000847}
848
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300849static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300850{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300851 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300852}
853
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100854static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530855{
856 const char *tp_name = qdict_get_str(qdict, "name");
857 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200858 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000859
Lluís Vilanova14101d02014-08-25 13:20:03 +0200860 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
861 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100862 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000863 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530864}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100865
Michael Rothc45a8162011-10-02 08:44:37 -0500866#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100867static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100868{
869 const char *op = qdict_get_try_str(qdict, "op");
870 const char *arg = qdict_get_try_str(qdict, "arg");
871
872 if (!op) {
873 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
874 } else if (!strcmp(op, "on")) {
875 st_set_trace_file_enabled(true);
876 } else if (!strcmp(op, "off")) {
877 st_set_trace_file_enabled(false);
878 } else if (!strcmp(op, "flush")) {
879 st_flush_trace_buffer();
880 } else if (!strcmp(op, "set")) {
881 if (arg) {
882 st_set_trace_file(arg);
883 }
884 } else {
885 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
886 help_cmd(mon, "trace-file");
887 }
888}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530889#endif
890
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100891static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000892{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300893 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000894}
895
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300896CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000897{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300898 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200899 const mon_cmd_t *cmd;
900
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300901 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200902 info = g_malloc0(sizeof(*info));
903 info->value = g_malloc0(sizeof(*info->value));
904 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300906 info->next = cmd_list;
907 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200908 }
909
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300910 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000911}
912
Daniel P. Berrange48608532012-05-21 17:59:51 +0100913EventInfoList *qmp_query_events(Error **errp)
914{
915 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200916 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100917
Wenchao Xia75175172014-06-18 08:43:54 +0200918 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
919 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100920 assert(event_name != NULL);
921 info = g_malloc0(sizeof(*info));
922 info->value = g_malloc0(sizeof(*info->value));
923 info->value->name = g_strdup(event_name);
924
925 info->next = ev_list;
926 ev_list = info;
927 }
928
929 return ev_list;
930}
931
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300932/* set the current CPU defined by the user */
933int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000934{
Andreas Färber55e5c282012-12-17 06:18:02 +0100935 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000936
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100937 cpu = qemu_get_cpu(cpu_index);
938 if (cpu == NULL) {
939 return -1;
bellard6a00d602005-11-21 23:25:50 +0000940 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200941 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100942 return 0;
bellard6a00d602005-11-21 23:25:50 +0000943}
944
Andreas Färber9349b4f2012-03-14 01:38:32 +0100945static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000946{
aliguori731b0362009-03-05 23:01:42 +0000947 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300948 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000949 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300950 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200951 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000952}
953
Luiz Capitulino99b77962011-10-24 10:53:44 -0200954int monitor_get_cpu_index(void)
955{
Andreas Färber55e5c282012-12-17 06:18:02 +0100956 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
957 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200958}
959
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100960static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000961{
Andreas Färber878096e2013-05-27 01:33:50 +0200962 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100963 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000964 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200965 cpu = ENV_GET_CPU(env);
966 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000967}
968
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100969static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000970{
aliguori376253e2009-03-05 23:01:23 +0000971 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200972 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300976{
977 dump_opcount_info((FILE *)mon, monitor_fprintf);
978}
979
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100980static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000981{
982 int i;
bellard7e2515e2004-08-01 21:52:19 +0000983 const char *str;
ths3b46e622007-09-17 08:09:54 +0000984
aliguoricde76ee2009-03-05 23:01:51 +0000985 if (!mon->rs)
986 return;
bellard7e2515e2004-08-01 21:52:19 +0000987 i = 0;
988 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000989 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000990 if (!str)
991 break;
aliguori376253e2009-03-05 23:01:23 +0000992 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000993 i++;
bellardaa455482004-04-04 13:07:25 +0000994 }
995}
996
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100997static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000998{
Andreas Färber878096e2013-05-27 01:33:50 +0200999 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001000 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001001
1002 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001003 cpu = ENV_GET_CPU(env);
1004 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001005}
j_mayer76a66252007-03-07 08:32:30 +00001006
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001007static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301008{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001009 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1010 TraceEventInfoList *elem;
1011
1012 for (elem = events; elem != NULL; elem = elem->next) {
1013 monitor_printf(mon, "%s : state %u\n",
1014 elem->value->name,
1015 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1016 }
1017 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301018}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301019
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001020void qmp_client_migrate_info(const char *protocol, const char *hostname,
1021 bool has_port, int64_t port,
1022 bool has_tls_port, int64_t tls_port,
1023 bool has_cert_subject, const char *cert_subject,
1024 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001025{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001026 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001027 if (!qemu_using_spice(errp)) {
1028 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001029 }
1030
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001031 if (!has_port && !has_tls_port) {
1032 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1033 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001034 }
1035
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001036 if (qemu_spice_migrate_info(hostname,
1037 has_port ? port : -1,
1038 has_tls_port ? tls_port : -1,
1039 cert_subject)) {
1040 error_set(errp, QERR_UNDEFINED_ERROR);
1041 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001042 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001043 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001044 }
1045
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001046 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001047}
1048
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001049static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001050{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001051 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001052}
1053
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001054static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001055{
1056 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001057 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001058
bellard9307c4c2004-04-04 12:57:25 +00001059 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001060 mask = 0;
1061 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001062 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001063 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001064 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001065 return;
1066 }
1067 }
Peter Maydell24537a02013-02-11 16:41:23 +00001068 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001069}
1070
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001071static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001072{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001073 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001074 if (!option || !strcmp(option, "on")) {
1075 singlestep = 1;
1076 } else if (!strcmp(option, "off")) {
1077 singlestep = 0;
1078 } else {
1079 monitor_printf(mon, "unexpected option %s\n", option);
1080 }
1081}
1082
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001083static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001084{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001085 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001086 if (!device)
1087 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1088 if (gdbserver_start(device) < 0) {
1089 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1090 device);
1091 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001092 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001093 } else {
aliguori59030a82009-04-05 18:43:41 +00001094 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1095 device);
bellard8a7ddc32004-03-31 19:00:16 +00001096 }
1097}
1098
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001099static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001100{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001101 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001102 if (select_watchdog_action(action) == -1) {
1103 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1104 }
1105}
1106
aliguori376253e2009-03-05 23:01:23 +00001107static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001108{
aliguori376253e2009-03-05 23:01:23 +00001109 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001110 switch(c) {
1111 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001112 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001113 break;
1114 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001116 break;
1117 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001118 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001119 break;
1120 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001122 break;
1123 default:
1124 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001126 } else {
aliguori376253e2009-03-05 23:01:23 +00001127 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001128 }
1129 break;
1130 }
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001132}
1133
aliguori376253e2009-03-05 23:01:23 +00001134static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001135 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001136{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001137 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001138 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001139 uint8_t buf[16];
1140 uint64_t v;
1141
1142 if (format == 'i') {
1143 int flags;
1144 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001145 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001146#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001147 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001148 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001149 } else if (wsize == 4) {
1150 flags = 0;
1151 } else {
bellard6a15fd12006-04-12 21:07:07 +00001152 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001153 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001154 if (env) {
1155#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001156 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001157 (env->segs[R_CS].flags & DESC_L_MASK))
1158 flags = 2;
1159 else
1160#endif
1161 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1162 flags = 1;
1163 }
bellard4c27ba22004-04-25 18:05:08 +00001164 }
1165#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001166#ifdef TARGET_PPC
1167 flags = msr_le << 16;
1168 flags |= env->bfd_mach;
1169#endif
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001171 return;
1172 }
1173
1174 len = wsize * count;
1175 if (wsize == 1)
1176 line_size = 8;
1177 else
1178 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001179 max_digits = 0;
1180
1181 switch(format) {
1182 case 'o':
1183 max_digits = (wsize * 8 + 2) / 3;
1184 break;
1185 default:
1186 case 'x':
1187 max_digits = (wsize * 8) / 4;
1188 break;
1189 case 'u':
1190 case 'd':
1191 max_digits = (wsize * 8 * 10 + 32) / 33;
1192 break;
1193 case 'c':
1194 wsize = 1;
1195 break;
1196 }
1197
1198 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001199 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001200 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001201 else
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001203 l = len;
1204 if (l > line_size)
1205 l = line_size;
1206 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001207 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001208 } else {
bellard6a00d602005-11-21 23:25:50 +00001209 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001210 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001212 break;
1213 }
bellard9307c4c2004-04-04 12:57:25 +00001214 }
ths5fafdf22007-09-16 21:08:06 +00001215 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001216 while (i < l) {
1217 switch(wsize) {
1218 default:
1219 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001220 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001221 break;
1222 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001223 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001226 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001227 break;
1228 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001229 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001230 break;
1231 }
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001233 switch(format) {
1234 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001236 break;
1237 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001238 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001239 break;
1240 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001242 break;
1243 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001245 break;
1246 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001247 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001248 break;
1249 }
1250 i += wsize;
1251 }
aliguori376253e2009-03-05 23:01:23 +00001252 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001253 addr += l;
1254 len -= l;
1255 }
1256}
1257
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001258static void hmp_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");
1263 target_long addr = qdict_get_int(qdict, "addr");
1264
aliguori376253e2009-03-05 23:01:23 +00001265 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001266}
1267
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001268static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001269{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001270 int count = qdict_get_int(qdict, "count");
1271 int format = qdict_get_int(qdict, "format");
1272 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001273 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001274
aliguori376253e2009-03-05 23:01:23 +00001275 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001276}
1277
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001278static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001279{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001280 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001281 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001282
bellard9307c4c2004-04-04 12:57:25 +00001283 switch(format) {
1284 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001285 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001288 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001291 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001292 break;
1293 default:
1294 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001295 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 }
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001302}
1303
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001304static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001305{
1306 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001307 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001308 uint32_t start = qdict_get_int(qdict, "start");
1309 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001310
1311 sum = 0;
1312 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001313 uint8_t val = address_space_ldub(&address_space_memory, addr,
1314 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001315 /* BSD sum algorithm ('sum' Unix command) */
1316 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001317 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001318 }
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001320}
1321
bellard13224a82006-07-14 22:03:35 +00001322static int mouse_button_state;
1323
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001324static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001325{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001326 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001327 const char *dx_str = qdict_get_str(qdict, "dx_str");
1328 const char *dy_str = qdict_get_str(qdict, "dy_str");
1329 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001330
bellard13224a82006-07-14 22:03:35 +00001331 dx = strtol(dx_str, NULL, 0);
1332 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001333 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1334 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1335
1336 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001337 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001338 if (dz != 0) {
1339 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1340 qemu_input_queue_btn(NULL, button, true);
1341 qemu_input_event_sync();
1342 qemu_input_queue_btn(NULL, button, false);
1343 }
1344 }
1345 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001346}
1347
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001348static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001349{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001350 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1351 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1352 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1353 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1354 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001355 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001356
1357 if (mouse_button_state == button_state) {
1358 return;
1359 }
1360 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1361 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001362 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001363}
1364
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001365static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001366{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001367 int size = qdict_get_int(qdict, "size");
1368 int addr = qdict_get_int(qdict, "addr");
1369 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001370 uint32_t val;
1371 int suffix;
1372
1373 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001374 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001375 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001376 addr++;
1377 }
1378 addr &= 0xffff;
1379
1380 switch(size) {
1381 default:
1382 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001383 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001384 suffix = 'b';
1385 break;
1386 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001387 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001388 suffix = 'w';
1389 break;
1390 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001391 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001392 suffix = 'l';
1393 break;
1394 }
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1396 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001397}
bellarda3a91a32004-06-04 11:06:21 +00001398
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001399static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001400{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001401 int size = qdict_get_int(qdict, "size");
1402 int addr = qdict_get_int(qdict, "addr");
1403 int val = qdict_get_int(qdict, "val");
1404
Jan Kiszkaf1147842009-07-14 10:20:11 +02001405 addr &= IOPORTS_MASK;
1406
1407 switch (size) {
1408 default:
1409 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001410 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001411 break;
1412 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001413 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001414 break;
1415 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001416 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001417 break;
1418 }
1419}
1420
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001421static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001422{
Gongleif1839932014-12-03 18:20:58 +00001423 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001424 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001425
Gongleif1839932014-12-03 18:20:58 +00001426 qemu_boot_set(bootdevice, &local_err);
1427 if (local_err) {
1428 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1429 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001430 } else {
Gongleif1839932014-12-03 18:20:58 +00001431 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001432 }
1433}
1434
bellardb86bda52004-09-18 19:32:46 +00001435#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001436static void print_pte(Monitor *mon, hwaddr addr,
1437 hwaddr pte,
1438 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001439{
Blue Swirld65aaf32010-12-11 18:56:24 +00001440#ifdef TARGET_X86_64
1441 if (addr & (1ULL << 47)) {
1442 addr |= -1LL << 48;
1443 }
1444#endif
1445 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1446 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001447 addr,
1448 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001449 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001450 pte & PG_GLOBAL_MASK ? 'G' : '-',
1451 pte & PG_PSE_MASK ? 'P' : '-',
1452 pte & PG_DIRTY_MASK ? 'D' : '-',
1453 pte & PG_ACCESSED_MASK ? 'A' : '-',
1454 pte & PG_PCD_MASK ? 'C' : '-',
1455 pte & PG_PWT_MASK ? 'T' : '-',
1456 pte & PG_USER_MASK ? 'U' : '-',
1457 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001458}
1459
Andreas Färber9349b4f2012-03-14 01:38:32 +01001460static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001461{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001462 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001463 uint32_t pgd, pde, pte;
1464
bellardb86bda52004-09-18 19:32:46 +00001465 pgd = env->cr[3] & ~0xfff;
1466 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001467 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001468 pde = le32_to_cpu(pde);
1469 if (pde & PG_PRESENT_MASK) {
1470 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001471 /* 4M pages */
1472 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001473 } else {
1474 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001475 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001476 pte = le32_to_cpu(pte);
1477 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001478 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001479 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001480 ~0xfff);
1481 }
1482 }
1483 }
1484 }
1485 }
1486}
1487
Andreas Färber9349b4f2012-03-14 01:38:32 +01001488static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001489{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001490 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001491 uint64_t pdpe, pde, pte;
1492 uint64_t pdp_addr, pd_addr, pt_addr;
1493
1494 pdp_addr = env->cr[3] & ~0x1f;
1495 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001496 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001497 pdpe = le64_to_cpu(pdpe);
1498 if (pdpe & PG_PRESENT_MASK) {
1499 pd_addr = pdpe & 0x3fffffffff000ULL;
1500 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001501 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001502 pde = le64_to_cpu(pde);
1503 if (pde & PG_PRESENT_MASK) {
1504 if (pde & PG_PSE_MASK) {
1505 /* 2M pages with PAE, CR4.PSE is ignored */
1506 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001507 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001508 } else {
1509 pt_addr = pde & 0x3fffffffff000ULL;
1510 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001511 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001512 pte = le64_to_cpu(pte);
1513 if (pte & PG_PRESENT_MASK) {
1514 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1515 + (l3 << 12),
1516 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001517 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001518 }
1519 }
1520 }
1521 }
1522 }
1523 }
1524 }
1525}
1526
1527#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001528static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001529{
1530 uint64_t l1, l2, l3, l4;
1531 uint64_t pml4e, pdpe, pde, pte;
1532 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1533
1534 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1535 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001536 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001537 pml4e = le64_to_cpu(pml4e);
1538 if (pml4e & PG_PRESENT_MASK) {
1539 pdp_addr = pml4e & 0x3fffffffff000ULL;
1540 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001541 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001542 pdpe = le64_to_cpu(pdpe);
1543 if (pdpe & PG_PRESENT_MASK) {
1544 if (pdpe & PG_PSE_MASK) {
1545 /* 1G pages, CR4.PSE is ignored */
1546 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1547 0x3ffffc0000000ULL);
1548 } else {
1549 pd_addr = pdpe & 0x3fffffffff000ULL;
1550 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001551 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001552 pde = le64_to_cpu(pde);
1553 if (pde & PG_PRESENT_MASK) {
1554 if (pde & PG_PSE_MASK) {
1555 /* 2M pages, CR4.PSE is ignored */
1556 print_pte(mon, (l1 << 39) + (l2 << 30) +
1557 (l3 << 21), pde,
1558 0x3ffffffe00000ULL);
1559 } else {
1560 pt_addr = pde & 0x3fffffffff000ULL;
1561 for (l4 = 0; l4 < 512; l4++) {
1562 cpu_physical_memory_read(pt_addr
1563 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001564 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001565 pte = le64_to_cpu(pte);
1566 if (pte & PG_PRESENT_MASK) {
1567 print_pte(mon, (l1 << 39) +
1568 (l2 << 30) +
1569 (l3 << 21) + (l4 << 12),
1570 pte & ~PG_PSE_MASK,
1571 0x3fffffffff000ULL);
1572 }
1573 }
1574 }
1575 }
1576 }
1577 }
1578 }
1579 }
1580 }
1581 }
1582}
1583#endif
1584
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001585static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001586{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001587 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001588
1589 env = mon_get_cpu();
1590
1591 if (!(env->cr[0] & CR0_PG_MASK)) {
1592 monitor_printf(mon, "PG disabled\n");
1593 return;
1594 }
1595 if (env->cr[4] & CR4_PAE_MASK) {
1596#ifdef TARGET_X86_64
1597 if (env->hflags & HF_LMA_MASK) {
1598 tlb_info_64(mon, env);
1599 } else
1600#endif
1601 {
1602 tlb_info_pae32(mon, env);
1603 }
1604 } else {
1605 tlb_info_32(mon, env);
1606 }
1607}
1608
Avi Kivitya8170e52012-10-23 12:30:10 +02001609static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001610 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001611 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001612{
bellard9746b152004-11-11 18:30:24 +00001613 int prot1;
1614 prot1 = *plast_prot;
1615 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001616 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001617 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1618 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001619 *pstart, end, end - *pstart,
1620 prot1 & PG_USER_MASK ? 'u' : '-',
1621 'r',
1622 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001623 }
1624 if (prot != 0)
1625 *pstart = end;
1626 else
1627 *pstart = -1;
1628 *plast_prot = prot;
1629 }
1630}
1631
Andreas Färber9349b4f2012-03-14 01:38:32 +01001632static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001633{
Austin Clementsb49ca722011-08-14 23:19:21 -04001634 unsigned int l1, l2;
1635 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001636 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001637 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001638
bellardb86bda52004-09-18 19:32:46 +00001639 pgd = env->cr[3] & ~0xfff;
1640 last_prot = 0;
1641 start = -1;
1642 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001643 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001644 pde = le32_to_cpu(pde);
1645 end = l1 << 22;
1646 if (pde & PG_PRESENT_MASK) {
1647 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1648 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001649 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001650 } else {
1651 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001652 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001653 pte = le32_to_cpu(pte);
1654 end = (l1 << 22) + (l2 << 12);
1655 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001656 prot = pte & pde &
1657 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001658 } else {
1659 prot = 0;
1660 }
aliguori376253e2009-03-05 23:01:23 +00001661 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001662 }
1663 }
1664 } else {
1665 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001666 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001667 }
1668 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001669 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001670 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001671}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001672
Andreas Färber9349b4f2012-03-14 01:38:32 +01001673static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001674{
Austin Clementsb49ca722011-08-14 23:19:21 -04001675 unsigned int l1, l2, l3;
1676 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001677 uint64_t pdpe, pde, pte;
1678 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001679 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001680
1681 pdp_addr = env->cr[3] & ~0x1f;
1682 last_prot = 0;
1683 start = -1;
1684 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001685 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001686 pdpe = le64_to_cpu(pdpe);
1687 end = l1 << 30;
1688 if (pdpe & PG_PRESENT_MASK) {
1689 pd_addr = pdpe & 0x3fffffffff000ULL;
1690 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001691 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001692 pde = le64_to_cpu(pde);
1693 end = (l1 << 30) + (l2 << 21);
1694 if (pde & PG_PRESENT_MASK) {
1695 if (pde & PG_PSE_MASK) {
1696 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1697 PG_PRESENT_MASK);
1698 mem_print(mon, &start, &last_prot, end, prot);
1699 } else {
1700 pt_addr = pde & 0x3fffffffff000ULL;
1701 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001702 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001703 pte = le64_to_cpu(pte);
1704 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1705 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001706 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1707 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001708 } else {
1709 prot = 0;
1710 }
1711 mem_print(mon, &start, &last_prot, end, prot);
1712 }
1713 }
1714 } else {
1715 prot = 0;
1716 mem_print(mon, &start, &last_prot, end, prot);
1717 }
1718 }
1719 } else {
1720 prot = 0;
1721 mem_print(mon, &start, &last_prot, end, prot);
1722 }
1723 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001724 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001725 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001726}
1727
1728
1729#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001730static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001731{
1732 int prot, last_prot;
1733 uint64_t l1, l2, l3, l4;
1734 uint64_t pml4e, pdpe, pde, pte;
1735 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1736
1737 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1738 last_prot = 0;
1739 start = -1;
1740 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001741 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001742 pml4e = le64_to_cpu(pml4e);
1743 end = l1 << 39;
1744 if (pml4e & PG_PRESENT_MASK) {
1745 pdp_addr = pml4e & 0x3fffffffff000ULL;
1746 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001747 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001748 pdpe = le64_to_cpu(pdpe);
1749 end = (l1 << 39) + (l2 << 30);
1750 if (pdpe & PG_PRESENT_MASK) {
1751 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001752 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1753 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001754 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001755 mem_print(mon, &start, &last_prot, end, prot);
1756 } else {
1757 pd_addr = pdpe & 0x3fffffffff000ULL;
1758 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001759 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001760 pde = le64_to_cpu(pde);
1761 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1762 if (pde & PG_PRESENT_MASK) {
1763 if (pde & PG_PSE_MASK) {
1764 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1765 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001766 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001767 mem_print(mon, &start, &last_prot, end, prot);
1768 } else {
1769 pt_addr = pde & 0x3fffffffff000ULL;
1770 for (l4 = 0; l4 < 512; l4++) {
1771 cpu_physical_memory_read(pt_addr
1772 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001773 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001774 pte = le64_to_cpu(pte);
1775 end = (l1 << 39) + (l2 << 30) +
1776 (l3 << 21) + (l4 << 12);
1777 if (pte & PG_PRESENT_MASK) {
1778 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1779 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001780 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001781 } else {
1782 prot = 0;
1783 }
1784 mem_print(mon, &start, &last_prot, end, prot);
1785 }
1786 }
1787 } else {
1788 prot = 0;
1789 mem_print(mon, &start, &last_prot, end, prot);
1790 }
1791 }
1792 }
1793 } else {
1794 prot = 0;
1795 mem_print(mon, &start, &last_prot, end, prot);
1796 }
1797 }
1798 } else {
1799 prot = 0;
1800 mem_print(mon, &start, &last_prot, end, prot);
1801 }
1802 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001803 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001804 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001805}
1806#endif
1807
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001808static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001809{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001810 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001811
1812 env = mon_get_cpu();
1813
1814 if (!(env->cr[0] & CR0_PG_MASK)) {
1815 monitor_printf(mon, "PG disabled\n");
1816 return;
1817 }
1818 if (env->cr[4] & CR4_PAE_MASK) {
1819#ifdef TARGET_X86_64
1820 if (env->hflags & HF_LMA_MASK) {
1821 mem_info_64(mon, env);
1822 } else
1823#endif
1824 {
1825 mem_info_pae32(mon, env);
1826 }
1827 } else {
1828 mem_info_32(mon, env);
1829 }
1830}
bellardb86bda52004-09-18 19:32:46 +00001831#endif
1832
aurel327c664e22009-03-03 06:12:22 +00001833#if defined(TARGET_SH4)
1834
aliguori376253e2009-03-05 23:01:23 +00001835static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001836{
aliguori376253e2009-03-05 23:01:23 +00001837 monitor_printf(mon, " tlb%i:\t"
1838 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1839 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1840 "dirty=%hhu writethrough=%hhu\n",
1841 idx,
1842 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1843 tlb->v, tlb->sh, tlb->c, tlb->pr,
1844 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001845}
1846
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001847static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001848{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001849 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001850 int i;
1851
aliguori376253e2009-03-05 23:01:23 +00001852 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001853 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001854 print_tlb (mon, i, &env->itlb[i]);
1855 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001856 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001857 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001858}
1859
1860#endif
1861
Max Filippov692f7372012-01-07 20:02:40 +04001862#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001863static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001864{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001865 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001866
1867 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1868}
1869#endif
1870
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001871static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001872{
1873 mtree_info((fprintf_function)monitor_printf, mon);
1874}
1875
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001876static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001877{
aliguorib28b6232009-04-22 20:20:29 +00001878 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001879 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001880 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001881
zhanghailiang5b009e42014-11-04 19:49:30 +08001882 node_mem = g_new0(uint64_t, nb_numa_nodes);
1883 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001884 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1885 for (i = 0; i < nb_numa_nodes; i++) {
1886 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001887 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001888 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001889 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001890 }
1891 }
1892 monitor_printf(mon, "\n");
1893 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001894 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001895 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001896 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001897}
1898
bellard5f1ce942006-02-08 22:40:15 +00001899#ifdef CONFIG_PROFILER
1900
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001901int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001902int64_t dev_time;
1903
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, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001907 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001908 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001909 tcg_time, tcg_time / (double)get_ticks_per_sec());
1910 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001911 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001912}
1913#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001914static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001915{
aliguori376253e2009-03-05 23:01:23 +00001916 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001917}
1918#endif
1919
bellardec36b692006-07-16 18:57:03 +00001920/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001921static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001922
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001923static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001924{
1925 int i;
1926 CaptureState *s;
1927
1928 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001929 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001930 s->ops.info (s->opaque);
1931 }
1932}
1933
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001934static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001935{
1936 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001937 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001938 CaptureState *s;
1939
1940 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1941 if (i == n) {
1942 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001943 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001944 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001945 return;
1946 }
1947 }
1948}
1949
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001950static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001951{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001952 const char *path = qdict_get_str(qdict, "path");
1953 int has_freq = qdict_haskey(qdict, "freq");
1954 int freq = qdict_get_try_int(qdict, "freq", -1);
1955 int has_bits = qdict_haskey(qdict, "bits");
1956 int bits = qdict_get_try_int(qdict, "bits", -1);
1957 int has_channels = qdict_haskey(qdict, "nchannels");
1958 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001959 CaptureState *s;
1960
Anthony Liguori7267c092011-08-20 22:09:37 -05001961 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001962
1963 freq = has_freq ? freq : 44100;
1964 bits = has_bits ? bits : 16;
1965 nchannels = has_channels ? nchannels : 2;
1966
1967 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001968 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001969 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001970 return;
bellardec36b692006-07-16 18:57:03 +00001971 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001972 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001973}
bellardec36b692006-07-16 18:57:03 +00001974
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001975static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001976{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001977 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001978
aliguori76655d62009-03-06 20:27:37 +00001979 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001980 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001981 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001982 return acl;
1983}
aliguori76655d62009-03-06 20:27:37 +00001984
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001985static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001986{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001987 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001988 qemu_acl *acl = find_acl(mon, aclname);
1989 qemu_acl_entry *entry;
1990 int i = 0;
1991
1992 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001993 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001994 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001995 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001996 i++;
1997 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001998 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001999 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002000 }
2001}
2002
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002003static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002004{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002005 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002006 qemu_acl *acl = find_acl(mon, aclname);
2007
2008 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002009 qemu_acl_reset(acl);
2010 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002011 }
2012}
aliguori76655d62009-03-06 20:27:37 +00002013
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002014static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002015{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002016 const char *aclname = qdict_get_str(qdict, "aclname");
2017 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002018 qemu_acl *acl = find_acl(mon, aclname);
2019
2020 if (acl) {
2021 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002022 acl->defaultDeny = 0;
2023 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002024 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002025 acl->defaultDeny = 1;
2026 monitor_printf(mon, "acl: policy set to 'deny'\n");
2027 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002028 monitor_printf(mon, "acl: unknown policy '%s', "
2029 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002030 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002031 }
2032}
aliguori76655d62009-03-06 20:27:37 +00002033
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002034static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002035{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002036 const char *aclname = qdict_get_str(qdict, "aclname");
2037 const char *match = qdict_get_str(qdict, "match");
2038 const char *policy = qdict_get_str(qdict, "policy");
2039 int has_index = qdict_haskey(qdict, "index");
2040 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002041 qemu_acl *acl = find_acl(mon, aclname);
2042 int deny, ret;
2043
2044 if (acl) {
2045 if (strcmp(policy, "allow") == 0) {
2046 deny = 0;
2047 } else if (strcmp(policy, "deny") == 0) {
2048 deny = 1;
2049 } else {
2050 monitor_printf(mon, "acl: unknown policy '%s', "
2051 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002052 return;
2053 }
aliguori28a76be2009-03-06 20:27:40 +00002054 if (has_index)
2055 ret = qemu_acl_insert(acl, deny, match, index);
2056 else
2057 ret = qemu_acl_append(acl, deny, match);
2058 if (ret < 0)
2059 monitor_printf(mon, "acl: unable to add acl entry\n");
2060 else
2061 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002062 }
2063}
aliguori76655d62009-03-06 20:27:37 +00002064
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002065static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002066{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002067 const char *aclname = qdict_get_str(qdict, "aclname");
2068 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002069 qemu_acl *acl = find_acl(mon, aclname);
2070 int ret;
aliguori76655d62009-03-06 20:27:37 +00002071
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002072 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002073 ret = qemu_acl_remove(acl, match);
2074 if (ret < 0)
2075 monitor_printf(mon, "acl: no matching acl entry\n");
2076 else
2077 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002078 }
2079}
2080
Huang Ying79c4f6b2009-06-23 10:05:14 +08002081#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002082static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002083{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002084 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002085 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002086 int cpu_index = qdict_get_int(qdict, "cpu_index");
2087 int bank = qdict_get_int(qdict, "bank");
2088 uint64_t status = qdict_get_int(qdict, "status");
2089 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2090 uint64_t addr = qdict_get_int(qdict, "addr");
2091 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002092 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002093
Eric Blake34acbc92015-05-15 16:25:00 -06002094 if (qdict_get_try_bool(qdict, "broadcast", false)) {
Jan Kiszka747461c2011-03-02 08:56:10 +01002095 flags |= MCE_INJECT_BROADCAST;
2096 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002097 cs = qemu_get_cpu(cpu_index);
2098 if (cs != NULL) {
2099 cpu = X86_CPU(cs);
2100 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2101 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002102 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002103}
2104#endif
2105
Corey Bryant208c9d12012-06-22 14:36:09 -04002106void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002107{
Anthony Liguoric227f092009-10-01 16:12:16 -05002108 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002109 int fd;
2110
Corey Bryant208c9d12012-06-22 14:36:09 -04002111 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002112 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002113 error_set(errp, QERR_FD_NOT_SUPPLIED);
2114 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002115 }
2116
2117 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002118 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002119 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2120 "a name not starting with a digit");
2121 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002122 }
2123
Corey Bryant208c9d12012-06-22 14:36:09 -04002124 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002125 if (strcmp(monfd->name, fdname) != 0) {
2126 continue;
2127 }
2128
2129 close(monfd->fd);
2130 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002131 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002132 }
2133
Anthony Liguori7267c092011-08-20 22:09:37 -05002134 monfd = g_malloc0(sizeof(mon_fd_t));
2135 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002136 monfd->fd = fd;
2137
Corey Bryant208c9d12012-06-22 14:36:09 -04002138 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002139}
2140
Corey Bryant208c9d12012-06-22 14:36:09 -04002141void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002142{
Anthony Liguoric227f092009-10-01 16:12:16 -05002143 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002144
Corey Bryant208c9d12012-06-22 14:36:09 -04002145 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002146 if (strcmp(monfd->name, fdname) != 0) {
2147 continue;
2148 }
2149
Blue Swirl72cf2d42009-09-12 07:36:22 +00002150 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002151 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002152 g_free(monfd->name);
2153 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002154 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002155 }
2156
Corey Bryant208c9d12012-06-22 14:36:09 -04002157 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002158}
2159
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002160static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002161{
Luiz Capitulino13548692011-07-29 15:36:43 -03002162 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002163 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002164
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002165 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002166
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002167 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002168 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002169 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002170}
2171
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002172int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002173{
Anthony Liguoric227f092009-10-01 16:12:16 -05002174 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002175
Blue Swirl72cf2d42009-09-12 07:36:22 +00002176 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002177 int fd;
2178
2179 if (strcmp(monfd->name, fdname) != 0) {
2180 continue;
2181 }
2182
2183 fd = monfd->fd;
2184
2185 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002186 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002187 g_free(monfd->name);
2188 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002189
2190 return fd;
2191 }
2192
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002193 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002194 return -1;
2195}
2196
Corey Bryantba1c0482012-08-14 16:43:43 -04002197static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2198{
2199 MonFdsetFd *mon_fdset_fd;
2200 MonFdsetFd *mon_fdset_fd_next;
2201
2202 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002203 if ((mon_fdset_fd->removed ||
2204 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2205 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002206 close(mon_fdset_fd->fd);
2207 g_free(mon_fdset_fd->opaque);
2208 QLIST_REMOVE(mon_fdset_fd, next);
2209 g_free(mon_fdset_fd);
2210 }
2211 }
2212
Corey Bryantadb696f2012-08-14 16:43:47 -04002213 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002214 QLIST_REMOVE(mon_fdset, next);
2215 g_free(mon_fdset);
2216 }
2217}
2218
Corey Bryantefb87c12012-08-14 16:43:48 -04002219static void monitor_fdsets_cleanup(void)
2220{
2221 MonFdset *mon_fdset;
2222 MonFdset *mon_fdset_next;
2223
2224 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2225 monitor_fdset_cleanup(mon_fdset);
2226 }
2227}
2228
Corey Bryantba1c0482012-08-14 16:43:43 -04002229AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2230 const char *opaque, Error **errp)
2231{
2232 int fd;
2233 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002234 AddfdInfo *fdinfo;
2235
2236 fd = qemu_chr_fe_get_msgfd(mon->chr);
2237 if (fd == -1) {
2238 error_set(errp, QERR_FD_NOT_SUPPLIED);
2239 goto error;
2240 }
2241
Corey Bryante446f702012-10-18 15:19:32 -04002242 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2243 has_opaque, opaque, errp);
2244 if (fdinfo) {
2245 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002246 }
2247
Corey Bryantba1c0482012-08-14 16:43:43 -04002248error:
2249 if (fd != -1) {
2250 close(fd);
2251 }
2252 return NULL;
2253}
2254
2255void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2256{
2257 MonFdset *mon_fdset;
2258 MonFdsetFd *mon_fdset_fd;
2259 char fd_str[60];
2260
2261 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2262 if (mon_fdset->id != fdset_id) {
2263 continue;
2264 }
2265 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2266 if (has_fd) {
2267 if (mon_fdset_fd->fd != fd) {
2268 continue;
2269 }
2270 mon_fdset_fd->removed = true;
2271 break;
2272 } else {
2273 mon_fdset_fd->removed = true;
2274 }
2275 }
2276 if (has_fd && !mon_fdset_fd) {
2277 goto error;
2278 }
2279 monitor_fdset_cleanup(mon_fdset);
2280 return;
2281 }
2282
2283error:
2284 if (has_fd) {
2285 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2286 fdset_id, fd);
2287 } else {
2288 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2289 }
2290 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2291}
2292
2293FdsetInfoList *qmp_query_fdsets(Error **errp)
2294{
2295 MonFdset *mon_fdset;
2296 MonFdsetFd *mon_fdset_fd;
2297 FdsetInfoList *fdset_list = NULL;
2298
2299 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2300 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2301 FdsetFdInfoList *fdsetfd_list = NULL;
2302
2303 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2304 fdset_info->value->fdset_id = mon_fdset->id;
2305
2306 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2307 FdsetFdInfoList *fdsetfd_info;
2308
2309 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2310 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2311 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2312 if (mon_fdset_fd->opaque) {
2313 fdsetfd_info->value->has_opaque = true;
2314 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2315 } else {
2316 fdsetfd_info->value->has_opaque = false;
2317 }
2318
2319 fdsetfd_info->next = fdsetfd_list;
2320 fdsetfd_list = fdsetfd_info;
2321 }
2322
2323 fdset_info->value->fds = fdsetfd_list;
2324
2325 fdset_info->next = fdset_list;
2326 fdset_list = fdset_info;
2327 }
2328
2329 return fdset_list;
2330}
2331
Corey Bryante446f702012-10-18 15:19:32 -04002332AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2333 bool has_opaque, const char *opaque,
2334 Error **errp)
2335{
2336 MonFdset *mon_fdset = NULL;
2337 MonFdsetFd *mon_fdset_fd;
2338 AddfdInfo *fdinfo;
2339
2340 if (has_fdset_id) {
2341 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2342 /* Break if match found or match impossible due to ordering by ID */
2343 if (fdset_id <= mon_fdset->id) {
2344 if (fdset_id < mon_fdset->id) {
2345 mon_fdset = NULL;
2346 }
2347 break;
2348 }
2349 }
2350 }
2351
2352 if (mon_fdset == NULL) {
2353 int64_t fdset_id_prev = -1;
2354 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2355
2356 if (has_fdset_id) {
2357 if (fdset_id < 0) {
2358 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2359 "a non-negative value");
2360 return NULL;
2361 }
2362 /* Use specified fdset ID */
2363 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2364 mon_fdset_cur = mon_fdset;
2365 if (fdset_id < mon_fdset_cur->id) {
2366 break;
2367 }
2368 }
2369 } else {
2370 /* Use first available fdset ID */
2371 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2372 mon_fdset_cur = mon_fdset;
2373 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2374 fdset_id_prev = mon_fdset_cur->id;
2375 continue;
2376 }
2377 break;
2378 }
2379 }
2380
2381 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2382 if (has_fdset_id) {
2383 mon_fdset->id = fdset_id;
2384 } else {
2385 mon_fdset->id = fdset_id_prev + 1;
2386 }
2387
2388 /* The fdset list is ordered by fdset ID */
2389 if (!mon_fdset_cur) {
2390 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2391 } else if (mon_fdset->id < mon_fdset_cur->id) {
2392 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2393 } else {
2394 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2395 }
2396 }
2397
2398 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2399 mon_fdset_fd->fd = fd;
2400 mon_fdset_fd->removed = false;
2401 if (has_opaque) {
2402 mon_fdset_fd->opaque = g_strdup(opaque);
2403 }
2404 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2405
2406 fdinfo = g_malloc0(sizeof(*fdinfo));
2407 fdinfo->fdset_id = mon_fdset->id;
2408 fdinfo->fd = mon_fdset_fd->fd;
2409
2410 return fdinfo;
2411}
2412
Corey Bryantadb696f2012-08-14 16:43:47 -04002413int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2414{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002415#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002416 MonFdset *mon_fdset;
2417 MonFdsetFd *mon_fdset_fd;
2418 int mon_fd_flags;
2419
Corey Bryantadb696f2012-08-14 16:43:47 -04002420 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2421 if (mon_fdset->id != fdset_id) {
2422 continue;
2423 }
2424 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2425 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2426 if (mon_fd_flags == -1) {
2427 return -1;
2428 }
2429
2430 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2431 return mon_fdset_fd->fd;
2432 }
2433 }
2434 errno = EACCES;
2435 return -1;
2436 }
2437#endif
2438
2439 errno = ENOENT;
2440 return -1;
2441}
2442
2443int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2444{
2445 MonFdset *mon_fdset;
2446 MonFdsetFd *mon_fdset_fd_dup;
2447
2448 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2449 if (mon_fdset->id != fdset_id) {
2450 continue;
2451 }
2452 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2453 if (mon_fdset_fd_dup->fd == dup_fd) {
2454 return -1;
2455 }
2456 }
2457 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2458 mon_fdset_fd_dup->fd = dup_fd;
2459 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2460 return 0;
2461 }
2462 return -1;
2463}
2464
2465static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2466{
2467 MonFdset *mon_fdset;
2468 MonFdsetFd *mon_fdset_fd_dup;
2469
2470 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2471 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2472 if (mon_fdset_fd_dup->fd == dup_fd) {
2473 if (remove) {
2474 QLIST_REMOVE(mon_fdset_fd_dup, next);
2475 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2476 monitor_fdset_cleanup(mon_fdset);
2477 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002478 return -1;
2479 } else {
2480 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002481 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002482 }
2483 }
2484 }
2485 return -1;
2486}
2487
2488int monitor_fdset_dup_fd_find(int dup_fd)
2489{
2490 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2491}
2492
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002493void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002494{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002495 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002496}
2497
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002498int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002499{
2500 int fd;
2501 Error *local_err = NULL;
2502
2503 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002504 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002505 } else {
2506 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002507 if (fd == -1) {
2508 error_setg(&local_err, "Invalid file descriptor number '%s'",
2509 fdname);
2510 }
2511 }
2512 if (local_err) {
2513 error_propagate(errp, local_err);
2514 assert(fd == -1);
2515 } else {
2516 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002517 }
2518
2519 return fd;
2520}
2521
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002522/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002523static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 {
2525 .name = "version",
2526 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 .params = "",
2528 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002529 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 },
2531 {
2532 .name = "network",
2533 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 .params = "",
2535 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002536 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 },
2538 {
2539 .name = "chardev",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002543 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002547 .args_type = "nodes:-n,verbose:-v,device:B?",
2548 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002549 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002550 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002551 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 },
2553 {
2554 .name = "blockstats",
2555 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 .params = "",
2557 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002558 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 },
2560 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002561 .name = "block-jobs",
2562 .args_type = "",
2563 .params = "",
2564 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002565 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002566 },
2567 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 .name = "registers",
2569 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002570 .params = "",
2571 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002572 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002573 },
2574 {
2575 .name = "cpus",
2576 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 .params = "",
2578 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002579 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = "history",
2583 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 .params = "",
2585 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002586 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002588#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2589 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 {
2591 .name = "irq",
2592 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 .params = "",
2594 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002595#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002596 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002597#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002598 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002599#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002600 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002601#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
2603 {
2604 .name = "pic",
2605 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 .params = "",
2607 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002608#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002609 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002610#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002611 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002612#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002613 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002614#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002616#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 {
2618 .name = "pci",
2619 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 .params = "",
2621 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002622 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002624#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002625 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 {
2627 .name = "tlb",
2628 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 .params = "",
2630 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002631 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 },
aurel327c664e22009-03-03 06:12:22 +00002633#endif
2634#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 {
2636 .name = "mem",
2637 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 .params = "",
2639 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002640 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 },
bellardb86bda52004-09-18 19:32:46 +00002642#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 {
Blue Swirl314e2982011-09-11 20:22:05 +00002644 .name = "mtree",
2645 .args_type = "",
2646 .params = "",
2647 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002648 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002649 },
2650 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 .name = "jit",
2652 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 .params = "",
2654 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002655 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 },
2657 {
Max Filippov246ae242014-11-02 11:04:18 +03002658 .name = "opcount",
2659 .args_type = "",
2660 .params = "",
2661 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002662 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002663 },
2664 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 .name = "kvm",
2666 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 .params = "",
2668 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002669 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 },
2671 {
2672 .name = "numa",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002676 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
2679 .name = "usb",
2680 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 .params = "",
2682 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002683 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
2685 {
2686 .name = "usbhost",
2687 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002688 .params = "",
2689 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002690 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
2693 .name = "profile",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002697 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "capture",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002704 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "snapshots",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002711 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
2713 {
2714 .name = "status",
2715 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 .params = "",
2717 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002718 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002719 },
2720 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 .name = "mice",
2722 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 .params = "",
2724 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002725 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 },
2727 {
2728 .name = "vnc",
2729 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 .params = "",
2731 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002732 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002734#if defined(CONFIG_SPICE)
2735 {
2736 .name = "spice",
2737 .args_type = "",
2738 .params = "",
2739 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002740 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002741 },
2742#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 {
2744 .name = "name",
2745 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 .params = "",
2747 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002748 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 },
2750 {
2751 .name = "uuid",
2752 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 .params = "",
2754 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002755 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 {
2758 .name = "cpustats",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002762 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
blueswir131a60e22007-10-26 18:42:59 +00002764#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 {
2766 .name = "usernet",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002770 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
blueswir131a60e22007-10-26 18:42:59 +00002772#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002773 {
2774 .name = "migrate",
2775 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 .params = "",
2777 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002778 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002779 },
2780 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002781 .name = "migrate_capabilities",
2782 .args_type = "",
2783 .params = "",
2784 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002785 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002786 },
2787 {
Liang Li50e9a622015-03-23 16:32:29 +08002788 .name = "migrate_parameters",
2789 .args_type = "",
2790 .params = "",
2791 .help = "show current migration parameters",
2792 .mhandler.cmd = hmp_info_migrate_parameters,
2793 },
2794 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002795 .name = "migrate_cache_size",
2796 .args_type = "",
2797 .params = "",
2798 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002799 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002800 },
2801 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 .name = "balloon",
2803 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002804 .params = "",
2805 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002806 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002807 },
2808 {
2809 .name = "qtree",
2810 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 .params = "",
2812 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002813 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 },
2815 {
2816 .name = "qdm",
2817 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 .params = "",
2819 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002820 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002821 },
2822 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002823 .name = "qom-tree",
2824 .args_type = "path:s?",
2825 .params = "[path]",
2826 .help = "show QOM composition tree",
2827 .mhandler.cmd = hmp_info_qom_tree,
2828 },
2829 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 .name = "roms",
2831 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002832 .params = "",
2833 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002834 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002835 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302836 {
2837 .name = "trace-events",
2838 .args_type = "",
2839 .params = "",
2840 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002841 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302842 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002844 .name = "tpm",
2845 .args_type = "",
2846 .params = "",
2847 .help = "show the TPM device",
2848 .mhandler.cmd = hmp_info_tpm,
2849 },
2850 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002851 .name = "memdev",
2852 .args_type = "",
2853 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002854 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002855 .mhandler.cmd = hmp_info_memdev,
2856 },
2857 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002858 .name = "memory-devices",
2859 .args_type = "",
2860 .params = "",
2861 .help = "show memory devices",
2862 .mhandler.cmd = hmp_info_memory_devices,
2863 },
2864 {
Scott Feldmanfafa4d52015-06-10 18:21:21 -07002865 .name = "rocker",
2866 .args_type = "name:s",
2867 .params = "name",
2868 .help = "Show rocker switch",
2869 .mhandler.cmd = hmp_rocker,
2870 },
2871 {
2872 .name = "rocker-ports",
2873 .args_type = "name:s",
2874 .params = "name",
2875 .help = "Show rocker ports",
2876 .mhandler.cmd = hmp_rocker_ports,
2877 },
2878 {
2879 .name = "rocker-of-dpa-flows",
2880 .args_type = "name:s,tbl_id:i?",
2881 .params = "name [tbl_id]",
2882 .help = "Show rocker OF-DPA flow tables",
2883 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2884 },
2885 {
2886 .name = "rocker-of-dpa-groups",
2887 .args_type = "name:s,type:i?",
2888 .params = "name [type]",
2889 .help = "Show rocker OF-DPA groups",
2890 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2891 },
2892 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002893 .name = NULL,
2894 },
bellard9dc39cb2004-03-14 21:38:27 +00002895};
2896
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002897/* mon_cmds and info_cmds would be sorted at runtime */
2898static mon_cmd_t mon_cmds[] = {
2899#include "hmp-commands.h"
2900 { NULL, NULL, },
2901};
2902
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002903static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002904#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002905 { /* NULL */ },
2906};
2907
bellard9307c4c2004-04-04 12:57:25 +00002908/*******************************************************************/
2909
2910static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002911static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002912
bellard92a31b12005-02-10 22:00:52 +00002913#define MD_TLONG 0
2914#define MD_I32 1
2915
bellard9307c4c2004-04-04 12:57:25 +00002916typedef struct MonitorDef {
2917 const char *name;
2918 int offset;
blueswir18662d652008-10-02 18:32:44 +00002919 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002920 int type;
bellard9307c4c2004-04-04 12:57:25 +00002921} MonitorDef;
2922
bellard57206fd2004-04-25 18:54:52 +00002923#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002924static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002925{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002926 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002927 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002928}
2929#endif
2930
bellarda541f292004-04-12 20:39:29 +00002931#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002932static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002933{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002934 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002935 unsigned int u;
2936 int i;
2937
2938 u = 0;
2939 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002940 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002941
2942 return u;
2943}
2944
blueswir18662d652008-10-02 18:32:44 +00002945static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002946{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002947 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002948 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002949}
2950
blueswir18662d652008-10-02 18:32:44 +00002951static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002952{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002953 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002954 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002955}
bellard9fddaa02004-05-21 12:59:32 +00002956
blueswir18662d652008-10-02 18:32:44 +00002957static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002958{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002959 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002960 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002961}
2962
blueswir18662d652008-10-02 18:32:44 +00002963static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002964{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002965 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002966 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002967}
2968
blueswir18662d652008-10-02 18:32:44 +00002969static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002970{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002971 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002972 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002973}
bellarda541f292004-04-12 20:39:29 +00002974#endif
2975
bellarde95c8d52004-09-30 22:22:08 +00002976#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002977#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002978static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002979{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002980 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002981
2982 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002983}
bellard7b936c02005-10-30 17:05:13 +00002984#endif
bellarde95c8d52004-09-30 22:22:08 +00002985
blueswir18662d652008-10-02 18:32:44 +00002986static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002987{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002988 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002989 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002990}
2991#endif
2992
blueswir18662d652008-10-02 18:32:44 +00002993static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002994#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002995
2996#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002997 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2998 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2999 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003000
Andreas Färbere59d1672012-02-16 00:40:47 +01003001 { "eax", offsetof(CPUX86State, regs[0]) },
3002 { "ecx", offsetof(CPUX86State, regs[1]) },
3003 { "edx", offsetof(CPUX86State, regs[2]) },
3004 { "ebx", offsetof(CPUX86State, regs[3]) },
3005 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3006 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3007 { "esi", offsetof(CPUX86State, regs[6]) },
3008 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003009#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003010 { "r8", offsetof(CPUX86State, regs[8]) },
3011 { "r9", offsetof(CPUX86State, regs[9]) },
3012 { "r10", offsetof(CPUX86State, regs[10]) },
3013 { "r11", offsetof(CPUX86State, regs[11]) },
3014 { "r12", offsetof(CPUX86State, regs[12]) },
3015 { "r13", offsetof(CPUX86State, regs[13]) },
3016 { "r14", offsetof(CPUX86State, regs[14]) },
3017 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003018#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003019 { "eflags", offsetof(CPUX86State, eflags) },
3020 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003021 SEG("cs", R_CS)
3022 SEG("ds", R_DS)
3023 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003024 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003025 SEG("fs", R_FS)
3026 SEG("gs", R_GS)
3027 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003028#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003029 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003030 { "r0", offsetof(CPUPPCState, gpr[0]) },
3031 { "r1", offsetof(CPUPPCState, gpr[1]) },
3032 { "r2", offsetof(CPUPPCState, gpr[2]) },
3033 { "r3", offsetof(CPUPPCState, gpr[3]) },
3034 { "r4", offsetof(CPUPPCState, gpr[4]) },
3035 { "r5", offsetof(CPUPPCState, gpr[5]) },
3036 { "r6", offsetof(CPUPPCState, gpr[6]) },
3037 { "r7", offsetof(CPUPPCState, gpr[7]) },
3038 { "r8", offsetof(CPUPPCState, gpr[8]) },
3039 { "r9", offsetof(CPUPPCState, gpr[9]) },
3040 { "r10", offsetof(CPUPPCState, gpr[10]) },
3041 { "r11", offsetof(CPUPPCState, gpr[11]) },
3042 { "r12", offsetof(CPUPPCState, gpr[12]) },
3043 { "r13", offsetof(CPUPPCState, gpr[13]) },
3044 { "r14", offsetof(CPUPPCState, gpr[14]) },
3045 { "r15", offsetof(CPUPPCState, gpr[15]) },
3046 { "r16", offsetof(CPUPPCState, gpr[16]) },
3047 { "r17", offsetof(CPUPPCState, gpr[17]) },
3048 { "r18", offsetof(CPUPPCState, gpr[18]) },
3049 { "r19", offsetof(CPUPPCState, gpr[19]) },
3050 { "r20", offsetof(CPUPPCState, gpr[20]) },
3051 { "r21", offsetof(CPUPPCState, gpr[21]) },
3052 { "r22", offsetof(CPUPPCState, gpr[22]) },
3053 { "r23", offsetof(CPUPPCState, gpr[23]) },
3054 { "r24", offsetof(CPUPPCState, gpr[24]) },
3055 { "r25", offsetof(CPUPPCState, gpr[25]) },
3056 { "r26", offsetof(CPUPPCState, gpr[26]) },
3057 { "r27", offsetof(CPUPPCState, gpr[27]) },
3058 { "r28", offsetof(CPUPPCState, gpr[28]) },
3059 { "r29", offsetof(CPUPPCState, gpr[29]) },
3060 { "r30", offsetof(CPUPPCState, gpr[30]) },
3061 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003062 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003063 { "f0", offsetof(CPUPPCState, fpr[0]) },
3064 { "f1", offsetof(CPUPPCState, fpr[1]) },
3065 { "f2", offsetof(CPUPPCState, fpr[2]) },
3066 { "f3", offsetof(CPUPPCState, fpr[3]) },
3067 { "f4", offsetof(CPUPPCState, fpr[4]) },
3068 { "f5", offsetof(CPUPPCState, fpr[5]) },
3069 { "f6", offsetof(CPUPPCState, fpr[6]) },
3070 { "f7", offsetof(CPUPPCState, fpr[7]) },
3071 { "f8", offsetof(CPUPPCState, fpr[8]) },
3072 { "f9", offsetof(CPUPPCState, fpr[9]) },
3073 { "f10", offsetof(CPUPPCState, fpr[10]) },
3074 { "f11", offsetof(CPUPPCState, fpr[11]) },
3075 { "f12", offsetof(CPUPPCState, fpr[12]) },
3076 { "f13", offsetof(CPUPPCState, fpr[13]) },
3077 { "f14", offsetof(CPUPPCState, fpr[14]) },
3078 { "f15", offsetof(CPUPPCState, fpr[15]) },
3079 { "f16", offsetof(CPUPPCState, fpr[16]) },
3080 { "f17", offsetof(CPUPPCState, fpr[17]) },
3081 { "f18", offsetof(CPUPPCState, fpr[18]) },
3082 { "f19", offsetof(CPUPPCState, fpr[19]) },
3083 { "f20", offsetof(CPUPPCState, fpr[20]) },
3084 { "f21", offsetof(CPUPPCState, fpr[21]) },
3085 { "f22", offsetof(CPUPPCState, fpr[22]) },
3086 { "f23", offsetof(CPUPPCState, fpr[23]) },
3087 { "f24", offsetof(CPUPPCState, fpr[24]) },
3088 { "f25", offsetof(CPUPPCState, fpr[25]) },
3089 { "f26", offsetof(CPUPPCState, fpr[26]) },
3090 { "f27", offsetof(CPUPPCState, fpr[27]) },
3091 { "f28", offsetof(CPUPPCState, fpr[28]) },
3092 { "f29", offsetof(CPUPPCState, fpr[29]) },
3093 { "f30", offsetof(CPUPPCState, fpr[30]) },
3094 { "f31", offsetof(CPUPPCState, fpr[31]) },
3095 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003096 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003097 { "nip|pc", offsetof(CPUPPCState, nip) },
3098 { "lr", offsetof(CPUPPCState, lr) },
3099 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003100 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003101 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003102 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003103 { "msr", 0, &monitor_get_msr, },
3104 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003105 { "tbu", 0, &monitor_get_tbu, },
3106 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003107 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003108 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3109 { "sr0", offsetof(CPUPPCState, sr[0]) },
3110 { "sr1", offsetof(CPUPPCState, sr[1]) },
3111 { "sr2", offsetof(CPUPPCState, sr[2]) },
3112 { "sr3", offsetof(CPUPPCState, sr[3]) },
3113 { "sr4", offsetof(CPUPPCState, sr[4]) },
3114 { "sr5", offsetof(CPUPPCState, sr[5]) },
3115 { "sr6", offsetof(CPUPPCState, sr[6]) },
3116 { "sr7", offsetof(CPUPPCState, sr[7]) },
3117 { "sr8", offsetof(CPUPPCState, sr[8]) },
3118 { "sr9", offsetof(CPUPPCState, sr[9]) },
3119 { "sr10", offsetof(CPUPPCState, sr[10]) },
3120 { "sr11", offsetof(CPUPPCState, sr[11]) },
3121 { "sr12", offsetof(CPUPPCState, sr[12]) },
3122 { "sr13", offsetof(CPUPPCState, sr[13]) },
3123 { "sr14", offsetof(CPUPPCState, sr[14]) },
3124 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003125 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003126 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003127
Andreas Färbere59d1672012-02-16 00:40:47 +01003128 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3129 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003130 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3131 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3132 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003133 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3134 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3135 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3136 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3137 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3138 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3139 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3140 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3141 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3142 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3143 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3144 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3145 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3146 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3147 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3148 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3149 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3150 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3151 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3152 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3153 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3154 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3155 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3156 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3157 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3158 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3159 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3160 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3161 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3162 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3163 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3164 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3165 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3166 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3167 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3168 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3169 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3170 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3171 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3172 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3173 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3174 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3175 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3176 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3177 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3178 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3179 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3180 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3181 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3182 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3183 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3184 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3185 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3186 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3187 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3188 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3189 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3190 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3191 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3192 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3193 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3194 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3195 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3196 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003197
bellarde95c8d52004-09-30 22:22:08 +00003198#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003199 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3200 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3201 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3202 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3203 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3204 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3205 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3206 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003207 { "o0", 0, monitor_get_reg },
3208 { "o1", 1, monitor_get_reg },
3209 { "o2", 2, monitor_get_reg },
3210 { "o3", 3, monitor_get_reg },
3211 { "o4", 4, monitor_get_reg },
3212 { "o5", 5, monitor_get_reg },
3213 { "o6", 6, monitor_get_reg },
3214 { "o7", 7, monitor_get_reg },
3215 { "l0", 8, monitor_get_reg },
3216 { "l1", 9, monitor_get_reg },
3217 { "l2", 10, monitor_get_reg },
3218 { "l3", 11, monitor_get_reg },
3219 { "l4", 12, monitor_get_reg },
3220 { "l5", 13, monitor_get_reg },
3221 { "l6", 14, monitor_get_reg },
3222 { "l7", 15, monitor_get_reg },
3223 { "i0", 16, monitor_get_reg },
3224 { "i1", 17, monitor_get_reg },
3225 { "i2", 18, monitor_get_reg },
3226 { "i3", 19, monitor_get_reg },
3227 { "i4", 20, monitor_get_reg },
3228 { "i5", 21, monitor_get_reg },
3229 { "i6", 22, monitor_get_reg },
3230 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003231 { "pc", offsetof(CPUSPARCState, pc) },
3232 { "npc", offsetof(CPUSPARCState, npc) },
3233 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003234#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003235 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003236 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003237#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003238 { "tbr", offsetof(CPUSPARCState, tbr) },
3239 { "fsr", offsetof(CPUSPARCState, fsr) },
3240 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3241 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3242 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3243 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3244 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3245 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3246 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3247 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3248 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3249 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3250 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3251 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3252 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3253 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3254 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3255 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3256 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3257 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3258 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3259 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3260 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3261 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3262 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3263 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3264 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3265 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3266 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3267 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3268 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3269 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3270 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3271 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003272#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003273 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3274 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3275 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3276 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3277 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3278 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3279 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3280 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3281 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3282 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3283 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3284 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3285 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3286 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3287 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3288 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3289 { "asi", offsetof(CPUSPARCState, asi) },
3290 { "pstate", offsetof(CPUSPARCState, pstate) },
3291 { "cansave", offsetof(CPUSPARCState, cansave) },
3292 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3293 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3294 { "wstate", offsetof(CPUSPARCState, wstate) },
3295 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3296 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003297#endif
bellard9307c4c2004-04-04 12:57:25 +00003298#endif
3299 { NULL },
3300};
3301
Stefan Weil9c3175c2013-08-22 21:30:09 +02003302static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3303expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003304{
Fam Zheng277acfe2013-08-20 10:58:21 +08003305 va_list ap;
3306 va_start(ap, fmt);
3307 monitor_vprintf(mon, fmt, ap);
3308 monitor_printf(mon, "\n");
3309 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003310 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003311}
3312
Markus Armbruster09b94182010-01-20 13:07:30 +01003313/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003314static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003315{
blueswir18662d652008-10-02 18:32:44 +00003316 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003317 void *ptr;
3318
bellard9307c4c2004-04-04 12:57:25 +00003319 for(md = monitor_defs; md->name != NULL; md++) {
3320 if (compare_cmd(name, md->name)) {
3321 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003322 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003323 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003324 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003325 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003326 switch(md->type) {
3327 case MD_I32:
3328 *pval = *(int32_t *)ptr;
3329 break;
3330 case MD_TLONG:
3331 *pval = *(target_long *)ptr;
3332 break;
3333 default:
3334 *pval = 0;
3335 break;
3336 }
bellard9307c4c2004-04-04 12:57:25 +00003337 }
3338 return 0;
3339 }
3340 }
3341 return -1;
3342}
3343
3344static void next(void)
3345{
Blue Swirl660f11b2009-07-31 21:16:51 +00003346 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003347 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003348 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003349 pch++;
3350 }
3351}
3352
aliguori376253e2009-03-05 23:01:23 +00003353static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003354
aliguori376253e2009-03-05 23:01:23 +00003355static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003356{
blueswir1c2efc952007-09-25 17:28:42 +00003357 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003358 char *p;
bellard6a00d602005-11-21 23:25:50 +00003359 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003360
3361 switch(*pch) {
3362 case '+':
3363 next();
aliguori376253e2009-03-05 23:01:23 +00003364 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003365 break;
3366 case '-':
3367 next();
aliguori376253e2009-03-05 23:01:23 +00003368 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003369 break;
3370 case '~':
3371 next();
aliguori376253e2009-03-05 23:01:23 +00003372 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003373 break;
3374 case '(':
3375 next();
aliguori376253e2009-03-05 23:01:23 +00003376 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003377 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003378 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003379 }
3380 next();
3381 break;
bellard81d09122004-07-14 17:21:37 +00003382 case '\'':
3383 pch++;
3384 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003385 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003386 n = *pch;
3387 pch++;
3388 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003389 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003390 next();
3391 break;
bellard9307c4c2004-04-04 12:57:25 +00003392 case '$':
3393 {
3394 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003395 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003396
bellard9307c4c2004-04-04 12:57:25 +00003397 pch++;
3398 q = buf;
3399 while ((*pch >= 'a' && *pch <= 'z') ||
3400 (*pch >= 'A' && *pch <= 'Z') ||
3401 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003402 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003403 if ((q - buf) < sizeof(buf) - 1)
3404 *q++ = *pch;
3405 pch++;
3406 }
blueswir1cd390082008-11-16 13:53:32 +00003407 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003408 pch++;
3409 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003410 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003411 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003412 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003413 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003414 }
3415 break;
3416 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003417 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003418 n = 0;
3419 break;
3420 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003421 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003422 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003423 if (errno == ERANGE) {
3424 expr_error(mon, "number too large");
3425 }
bellard9307c4c2004-04-04 12:57:25 +00003426 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003427 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003428 }
3429 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003430 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003431 pch++;
3432 break;
3433 }
3434 return n;
3435}
3436
3437
aliguori376253e2009-03-05 23:01:23 +00003438static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003439{
blueswir1c2efc952007-09-25 17:28:42 +00003440 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003441 int op;
ths3b46e622007-09-17 08:09:54 +00003442
aliguori376253e2009-03-05 23:01:23 +00003443 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003444 for(;;) {
3445 op = *pch;
3446 if (op != '*' && op != '/' && op != '%')
3447 break;
3448 next();
aliguori376253e2009-03-05 23:01:23 +00003449 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003450 switch(op) {
3451 default:
3452 case '*':
3453 val *= val2;
3454 break;
3455 case '/':
3456 case '%':
ths5fafdf22007-09-16 21:08:06 +00003457 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003458 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003459 if (op == '/')
3460 val /= val2;
3461 else
3462 val %= val2;
3463 break;
3464 }
3465 }
3466 return val;
3467}
3468
aliguori376253e2009-03-05 23:01:23 +00003469static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003470{
blueswir1c2efc952007-09-25 17:28:42 +00003471 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003472 int op;
bellard9307c4c2004-04-04 12:57:25 +00003473
aliguori376253e2009-03-05 23:01:23 +00003474 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003475 for(;;) {
3476 op = *pch;
3477 if (op != '&' && op != '|' && op != '^')
3478 break;
3479 next();
aliguori376253e2009-03-05 23:01:23 +00003480 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003481 switch(op) {
3482 default:
3483 case '&':
3484 val &= val2;
3485 break;
3486 case '|':
3487 val |= val2;
3488 break;
3489 case '^':
3490 val ^= val2;
3491 break;
3492 }
3493 }
3494 return val;
3495}
3496
aliguori376253e2009-03-05 23:01:23 +00003497static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003498{
blueswir1c2efc952007-09-25 17:28:42 +00003499 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003500 int op;
bellard9307c4c2004-04-04 12:57:25 +00003501
aliguori376253e2009-03-05 23:01:23 +00003502 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003503 for(;;) {
3504 op = *pch;
3505 if (op != '+' && op != '-')
3506 break;
3507 next();
aliguori376253e2009-03-05 23:01:23 +00003508 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003509 if (op == '+')
3510 val += val2;
3511 else
3512 val -= val2;
3513 }
3514 return val;
3515}
3516
aliguori376253e2009-03-05 23:01:23 +00003517static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003518{
3519 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003520 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003521 *pp = pch;
3522 return -1;
3523 }
blueswir1cd390082008-11-16 13:53:32 +00003524 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003525 pch++;
aliguori376253e2009-03-05 23:01:23 +00003526 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003527 *pp = pch;
3528 return 0;
3529}
3530
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003531static int get_double(Monitor *mon, double *pval, const char **pp)
3532{
3533 const char *p = *pp;
3534 char *tailp;
3535 double d;
3536
3537 d = strtod(p, &tailp);
3538 if (tailp == p) {
3539 monitor_printf(mon, "Number expected\n");
3540 return -1;
3541 }
3542 if (d != d || d - d != 0) {
3543 /* NaN or infinity */
3544 monitor_printf(mon, "Bad number\n");
3545 return -1;
3546 }
3547 *pval = d;
3548 *pp = tailp;
3549 return 0;
3550}
3551
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003552/*
3553 * Store the command-name in cmdname, and return a pointer to
3554 * the remaining of the command string.
3555 */
3556static const char *get_command_name(const char *cmdline,
3557 char *cmdname, size_t nlen)
3558{
3559 size_t len;
3560 const char *p, *pstart;
3561
3562 p = cmdline;
3563 while (qemu_isspace(*p))
3564 p++;
3565 if (*p == '\0')
3566 return NULL;
3567 pstart = p;
3568 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3569 p++;
3570 len = p - pstart;
3571 if (len > nlen - 1)
3572 len = nlen - 1;
3573 memcpy(cmdname, pstart, len);
3574 cmdname[len] = '\0';
3575 return p;
3576}
3577
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003578/**
3579 * Read key of 'type' into 'key' and return the current
3580 * 'type' pointer.
3581 */
3582static char *key_get_info(const char *type, char **key)
3583{
3584 size_t len;
3585 char *p, *str;
3586
3587 if (*type == ',')
3588 type++;
3589
3590 p = strchr(type, ':');
3591 if (!p) {
3592 *key = NULL;
3593 return NULL;
3594 }
3595 len = p - type;
3596
Anthony Liguori7267c092011-08-20 22:09:37 -05003597 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003598 memcpy(str, type, len);
3599 str[len] = '\0';
3600
3601 *key = str;
3602 return ++p;
3603}
3604
bellard9307c4c2004-04-04 12:57:25 +00003605static int default_fmt_format = 'x';
3606static int default_fmt_size = 4;
3607
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003608static int is_valid_option(const char *c, const char *typestr)
3609{
3610 char option[3];
3611
3612 option[0] = '-';
3613 option[1] = *c;
3614 option[2] = '\0';
3615
3616 typestr = strstr(typestr, option);
3617 return (typestr != NULL);
3618}
3619
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003620static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3621 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003622{
3623 const mon_cmd_t *cmd;
3624
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003625 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003626 if (compare_cmd(cmdname, cmd->name)) {
3627 return cmd;
3628 }
3629 }
3630
3631 return NULL;
3632}
3633
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003634static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3635{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003636 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003637}
3638
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003639/*
3640 * Parse @cmdline according to command table @table.
3641 * If @cmdline is blank, return NULL.
3642 * If it can't be parsed, report to @mon, and return NULL.
3643 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003644 * If a sub-command table exists, and if @cmdline contains an additional string
3645 * for a sub-command, this function will try to search the sub-command table.
3646 * If no additional string for a sub-command is present, this function will
3647 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003648 * Do not assume the returned command points into @table! It doesn't
3649 * when the command is a sub-command.
3650 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003651static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003652 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003653 int start,
3654 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003655 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003656{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003657 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003658 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003659 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003660 char cmdname[256];
3661 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003662 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003663
3664#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003665 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003666#endif
ths3b46e622007-09-17 08:09:54 +00003667
bellard9307c4c2004-04-04 12:57:25 +00003668 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003669 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003670 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003671 return NULL;
ths3b46e622007-09-17 08:09:54 +00003672
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003673 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003674 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003675 monitor_printf(mon, "unknown command: '%.*s'\n",
3676 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003677 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003678 }
bellard9307c4c2004-04-04 12:57:25 +00003679
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003680 /* filter out following useless space */
3681 while (qemu_isspace(*p)) {
3682 p++;
3683 }
3684 /* search sub command */
3685 if (cmd->sub_table != NULL) {
3686 /* check if user set additional command */
3687 if (*p == '\0') {
3688 return cmd;
3689 }
3690 return monitor_parse_command(mon, cmdline, p - cmdline,
3691 cmd->sub_table, qdict);
3692 }
3693
bellard9307c4c2004-04-04 12:57:25 +00003694 /* parse the parameters */
3695 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003696 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003697 typestr = key_get_info(typestr, &key);
3698 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003699 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003700 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003701 typestr++;
3702 switch(c) {
3703 case 'F':
bellard81d09122004-07-14 17:21:37 +00003704 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003705 case 's':
3706 {
3707 int ret;
ths3b46e622007-09-17 08:09:54 +00003708
blueswir1cd390082008-11-16 13:53:32 +00003709 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003710 p++;
3711 if (*typestr == '?') {
3712 typestr++;
3713 if (*p == '\0') {
3714 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003715 break;
bellard9307c4c2004-04-04 12:57:25 +00003716 }
3717 }
3718 ret = get_str(buf, sizeof(buf), &p);
3719 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003720 switch(c) {
3721 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003722 monitor_printf(mon, "%s: filename expected\n",
3723 cmdname);
bellard81d09122004-07-14 17:21:37 +00003724 break;
3725 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003726 monitor_printf(mon, "%s: block device name expected\n",
3727 cmdname);
bellard81d09122004-07-14 17:21:37 +00003728 break;
3729 default:
aliguori376253e2009-03-05 23:01:23 +00003730 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003731 break;
3732 }
bellard9307c4c2004-04-04 12:57:25 +00003733 goto fail;
3734 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003735 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003736 }
3737 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003738 case 'O':
3739 {
3740 QemuOptsList *opts_list;
3741 QemuOpts *opts;
3742
3743 opts_list = qemu_find_opts(key);
3744 if (!opts_list || opts_list->desc->name) {
3745 goto bad_type;
3746 }
3747 while (qemu_isspace(*p)) {
3748 p++;
3749 }
3750 if (!*p)
3751 break;
3752 if (get_str(buf, sizeof(buf), &p) < 0) {
3753 goto fail;
3754 }
3755 opts = qemu_opts_parse(opts_list, buf, 1);
3756 if (!opts) {
3757 goto fail;
3758 }
3759 qemu_opts_to_qdict(opts, qdict);
3760 qemu_opts_del(opts);
3761 }
3762 break;
bellard9307c4c2004-04-04 12:57:25 +00003763 case '/':
3764 {
3765 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003766
blueswir1cd390082008-11-16 13:53:32 +00003767 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003768 p++;
3769 if (*p == '/') {
3770 /* format found */
3771 p++;
3772 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003773 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003774 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003775 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003776 count = count * 10 + (*p - '0');
3777 p++;
3778 }
3779 }
3780 size = -1;
3781 format = -1;
3782 for(;;) {
3783 switch(*p) {
3784 case 'o':
3785 case 'd':
3786 case 'u':
3787 case 'x':
3788 case 'i':
3789 case 'c':
3790 format = *p++;
3791 break;
3792 case 'b':
3793 size = 1;
3794 p++;
3795 break;
3796 case 'h':
3797 size = 2;
3798 p++;
3799 break;
3800 case 'w':
3801 size = 4;
3802 p++;
3803 break;
3804 case 'g':
3805 case 'L':
3806 size = 8;
3807 p++;
3808 break;
3809 default:
3810 goto next;
3811 }
3812 }
3813 next:
blueswir1cd390082008-11-16 13:53:32 +00003814 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003815 monitor_printf(mon, "invalid char in format: '%c'\n",
3816 *p);
bellard9307c4c2004-04-04 12:57:25 +00003817 goto fail;
3818 }
bellard9307c4c2004-04-04 12:57:25 +00003819 if (format < 0)
3820 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003821 if (format != 'i') {
3822 /* for 'i', not specifying a size gives -1 as size */
3823 if (size < 0)
3824 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003825 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003826 }
bellard9307c4c2004-04-04 12:57:25 +00003827 default_fmt_format = format;
3828 } else {
3829 count = 1;
3830 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003831 if (format != 'i') {
3832 size = default_fmt_size;
3833 } else {
3834 size = -1;
3835 }
bellard9307c4c2004-04-04 12:57:25 +00003836 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003837 qdict_put(qdict, "count", qint_from_int(count));
3838 qdict_put(qdict, "format", qint_from_int(format));
3839 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003840 }
3841 break;
3842 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003843 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003844 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003845 {
blueswir1c2efc952007-09-25 17:28:42 +00003846 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003847
blueswir1cd390082008-11-16 13:53:32 +00003848 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003849 p++;
bellard34405572004-06-08 00:55:58 +00003850 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003851 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003852 if (*p == '\0') {
3853 typestr++;
3854 break;
3855 }
bellard34405572004-06-08 00:55:58 +00003856 } else {
3857 if (*p == '.') {
3858 p++;
blueswir1cd390082008-11-16 13:53:32 +00003859 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003860 p++;
bellard34405572004-06-08 00:55:58 +00003861 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003862 typestr++;
3863 break;
bellard34405572004-06-08 00:55:58 +00003864 }
3865 }
bellard13224a82006-07-14 22:03:35 +00003866 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003867 }
aliguori376253e2009-03-05 23:01:23 +00003868 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003869 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003870 /* Check if 'i' is greater than 32-bit */
3871 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3872 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3873 monitor_printf(mon, "integer is for 32-bit values\n");
3874 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003875 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003876 if (val < 0) {
3877 monitor_printf(mon, "enter a positive value\n");
3878 goto fail;
3879 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003880 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003881 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003882 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003883 }
3884 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003885 case 'o':
3886 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003887 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003888 char *end;
3889
3890 while (qemu_isspace(*p)) {
3891 p++;
3892 }
3893 if (*typestr == '?') {
3894 typestr++;
3895 if (*p == '\0') {
3896 break;
3897 }
3898 }
3899 val = strtosz(p, &end);
3900 if (val < 0) {
3901 monitor_printf(mon, "invalid size\n");
3902 goto fail;
3903 }
3904 qdict_put(qdict, key, qint_from_int(val));
3905 p = end;
3906 }
3907 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003908 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003909 {
3910 double val;
3911
3912 while (qemu_isspace(*p))
3913 p++;
3914 if (*typestr == '?') {
3915 typestr++;
3916 if (*p == '\0') {
3917 break;
3918 }
3919 }
3920 if (get_double(mon, &val, &p) < 0) {
3921 goto fail;
3922 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003923 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003924 switch (*p) {
3925 case 'm':
3926 val /= 1e3; p += 2; break;
3927 case 'u':
3928 val /= 1e6; p += 2; break;
3929 case 'n':
3930 val /= 1e9; p += 2; break;
3931 }
3932 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003933 if (*p && !qemu_isspace(*p)) {
3934 monitor_printf(mon, "Unknown unit suffix\n");
3935 goto fail;
3936 }
3937 qdict_put(qdict, key, qfloat_from_double(val));
3938 }
3939 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003940 case 'b':
3941 {
3942 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003943 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003944
3945 while (qemu_isspace(*p)) {
3946 p++;
3947 }
3948 beg = p;
3949 while (qemu_isgraph(*p)) {
3950 p++;
3951 }
3952 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003953 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003954 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003955 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003956 } else {
3957 monitor_printf(mon, "Expected 'on' or 'off'\n");
3958 goto fail;
3959 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06003960 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003961 }
3962 break;
bellard9307c4c2004-04-04 12:57:25 +00003963 case '-':
3964 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003965 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003966 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003967 /* option */
ths3b46e622007-09-17 08:09:54 +00003968
bellard9307c4c2004-04-04 12:57:25 +00003969 c = *typestr++;
3970 if (c == '\0')
3971 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003972 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003973 p++;
bellard9307c4c2004-04-04 12:57:25 +00003974 if (*p == '-') {
3975 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003976 if(c != *p) {
3977 if(!is_valid_option(p, typestr)) {
3978
3979 monitor_printf(mon, "%s: unsupported option -%c\n",
3980 cmdname, *p);
3981 goto fail;
3982 } else {
3983 skip_key = 1;
3984 }
bellard9307c4c2004-04-04 12:57:25 +00003985 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003986 if(skip_key) {
3987 p = tmp;
3988 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003989 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003990 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003991 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003992 }
bellard9307c4c2004-04-04 12:57:25 +00003993 }
bellard9307c4c2004-04-04 12:57:25 +00003994 }
3995 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003996 case 'S':
3997 {
3998 /* package all remaining string */
3999 int len;
4000
4001 while (qemu_isspace(*p)) {
4002 p++;
4003 }
4004 if (*typestr == '?') {
4005 typestr++;
4006 if (*p == '\0') {
4007 /* no remaining string: NULL argument */
4008 break;
4009 }
4010 }
4011 len = strlen(p);
4012 if (len <= 0) {
4013 monitor_printf(mon, "%s: string expected\n",
4014 cmdname);
4015 break;
4016 }
4017 qdict_put(qdict, key, qstring_from_str(p));
4018 p += len;
4019 }
4020 break;
bellard9307c4c2004-04-04 12:57:25 +00004021 default:
4022 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004023 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004024 goto fail;
4025 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004026 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004027 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004028 }
4029 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004030 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004031 p++;
4032 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004033 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4034 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004035 goto fail;
4036 }
4037
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004038 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004039
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004040fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004041 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004042 return NULL;
4043}
4044
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004045void monitor_set_error(Monitor *mon, QError *qerror)
4046{
4047 /* report only the first error */
4048 if (!mon->error) {
4049 mon->error = qerror;
4050 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004051 QDECREF(qerror);
4052 }
4053}
4054
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004055static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004056{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004057 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004058 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004059
4060 qdict = qdict_new();
4061
Wenchao Xia77172392013-08-27 20:38:20 +08004062 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Markus Armbruster8a4f5012015-03-05 18:50:05 +01004063 if (cmd) {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004064 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004065 }
4066
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004067 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004068}
4069
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004070static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004071{
4072 const char *p, *pstart;
4073 char cmd[128];
4074 int len;
4075
4076 p = list;
4077 for(;;) {
4078 pstart = p;
4079 p = strchr(p, '|');
4080 if (!p)
4081 p = pstart + strlen(pstart);
4082 len = p - pstart;
4083 if (len > sizeof(cmd) - 2)
4084 len = sizeof(cmd) - 2;
4085 memcpy(cmd, pstart, len);
4086 cmd[len] = '\0';
4087 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004088 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004089 }
4090 if (*p == '\0')
4091 break;
4092 p++;
4093 }
4094}
4095
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004096static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004097{
4098 DIR *ffs;
4099 struct dirent *d;
4100 char path[1024];
4101 char file[1024], file_prefix[1024];
4102 int input_path_len;
4103 const char *p;
4104
ths5fafdf22007-09-16 21:08:06 +00004105 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004106 if (!p) {
4107 input_path_len = 0;
4108 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004109 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004110 } else {
4111 input_path_len = p - input + 1;
4112 memcpy(path, input, input_path_len);
4113 if (input_path_len > sizeof(path) - 1)
4114 input_path_len = sizeof(path) - 1;
4115 path[input_path_len] = '\0';
4116 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4117 }
4118#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004119 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004120 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004121#endif
4122 ffs = opendir(path);
4123 if (!ffs)
4124 return;
4125 for(;;) {
4126 struct stat sb;
4127 d = readdir(ffs);
4128 if (!d)
4129 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004130
4131 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4132 continue;
4133 }
4134
bellard81d09122004-07-14 17:21:37 +00004135 if (strstart(d->d_name, file_prefix, NULL)) {
4136 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004137 if (input_path_len < sizeof(file))
4138 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4139 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004140 /* stat the file to find out if it's a directory.
4141 * In that case add a slash to speed up typing long paths
4142 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004143 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004144 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004145 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004146 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004147 }
4148 }
4149 closedir(ffs);
4150}
4151
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004152static const char *next_arg_type(const char *typestr)
4153{
4154 const char *p = strchr(typestr, ':');
4155 return (p != NULL ? ++p : typestr);
4156}
4157
Hani Benhabiles40d19392014-05-07 23:41:30 +01004158static void add_completion_option(ReadLineState *rs, const char *str,
4159 const char *option)
4160{
4161 if (!str || !option) {
4162 return;
4163 }
4164 if (!strncmp(option, str, strlen(str))) {
4165 readline_add_completion(rs, option);
4166 }
4167}
4168
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004169void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4170{
4171 size_t len;
4172 ChardevBackendInfoList *list, *start;
4173
4174 if (nb_args != 2) {
4175 return;
4176 }
4177 len = strlen(str);
4178 readline_set_completion_index(rs, len);
4179
4180 start = list = qmp_query_chardev_backends(NULL);
4181 while (list) {
4182 const char *chr_name = list->value->name;
4183
4184 if (!strncmp(chr_name, str, len)) {
4185 readline_add_completion(rs, chr_name);
4186 }
4187 list = list->next;
4188 }
4189 qapi_free_ChardevBackendInfoList(start);
4190}
4191
Hani Benhabilesb162b492014-05-07 23:41:31 +01004192void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4193{
4194 size_t len;
4195 int i;
4196
4197 if (nb_args != 2) {
4198 return;
4199 }
4200 len = strlen(str);
4201 readline_set_completion_index(rs, len);
4202 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4203 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4204 }
4205}
4206
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004207void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004208{
4209 GSList *list, *elt;
4210 size_t len;
4211
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004212 if (nb_args != 2) {
4213 return;
4214 }
4215
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004216 len = strlen(str);
4217 readline_set_completion_index(rs, len);
4218 list = elt = object_class_get_list(TYPE_DEVICE, false);
4219 while (elt) {
4220 const char *name;
4221 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4222 TYPE_DEVICE);
4223 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004224
4225 if (!dc->cannot_instantiate_with_device_add_yet
4226 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004227 readline_add_completion(rs, name);
4228 }
4229 elt = elt->next;
4230 }
4231 g_slist_free(list);
4232}
4233
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004234void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004235{
4236 GSList *list, *elt;
4237 size_t len;
4238
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004239 if (nb_args != 2) {
4240 return;
4241 }
4242
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004243 len = strlen(str);
4244 readline_set_completion_index(rs, len);
4245 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4246 while (elt) {
4247 const char *name;
4248
4249 name = object_class_get_name(OBJECT_CLASS(elt->data));
4250 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4251 readline_add_completion(rs, name);
4252 }
4253 elt = elt->next;
4254 }
4255 g_slist_free(list);
4256}
4257
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004258static void peripheral_device_del_completion(ReadLineState *rs,
4259 const char *str, size_t len)
4260{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004261 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4262 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004263
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004264 list = qdev_build_hotpluggable_device_list(peripheral);
4265 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004266 return;
4267 }
4268
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004269 for (item = list; item; item = g_slist_next(item)) {
4270 DeviceState *dev = item->data;
4271
4272 if (dev->id && !strncmp(str, dev->id, len)) {
4273 readline_add_completion(rs, dev->id);
4274 }
4275 }
4276
4277 g_slist_free(list);
4278}
4279
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004280void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4281{
4282 size_t len;
4283 ChardevInfoList *list, *start;
4284
4285 if (nb_args != 2) {
4286 return;
4287 }
4288 len = strlen(str);
4289 readline_set_completion_index(rs, len);
4290
4291 start = list = qmp_query_chardev(NULL);
4292 while (list) {
4293 ChardevInfo *chr = list->value;
4294
4295 if (!strncmp(chr->label, str, len)) {
4296 readline_add_completion(rs, chr->label);
4297 }
4298 list = list->next;
4299 }
4300 qapi_free_ChardevInfoList(start);
4301}
4302
Hani Benhabiles8e597772014-05-27 23:39:30 +01004303static void ringbuf_completion(ReadLineState *rs, const char *str)
4304{
4305 size_t len;
4306 ChardevInfoList *list, *start;
4307
4308 len = strlen(str);
4309 readline_set_completion_index(rs, len);
4310
4311 start = list = qmp_query_chardev(NULL);
4312 while (list) {
4313 ChardevInfo *chr_info = list->value;
4314
4315 if (!strncmp(chr_info->label, str, len)) {
4316 CharDriverState *chr = qemu_chr_find(chr_info->label);
4317 if (chr && chr_is_ringbuf(chr)) {
4318 readline_add_completion(rs, chr_info->label);
4319 }
4320 }
4321 list = list->next;
4322 }
4323 qapi_free_ChardevInfoList(start);
4324}
4325
Hani Benhabiles8e597772014-05-27 23:39:30 +01004326void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4327{
4328 if (nb_args != 2) {
4329 return;
4330 }
4331 ringbuf_completion(rs, str);
4332}
4333
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004334void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4335{
4336 size_t len;
4337
4338 if (nb_args != 2) {
4339 return;
4340 }
4341
4342 len = strlen(str);
4343 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004344 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004345}
4346
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004347void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004348{
4349 ObjectPropertyInfoList *list, *start;
4350 size_t len;
4351
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004352 if (nb_args != 2) {
4353 return;
4354 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004355 len = strlen(str);
4356 readline_set_completion_index(rs, len);
4357
4358 start = list = qmp_qom_list("/objects", NULL);
4359 while (list) {
4360 ObjectPropertyInfo *info = list->value;
4361
4362 if (!strncmp(info->type, "child<", 5)
4363 && !strncmp(info->name, str, len)) {
4364 readline_add_completion(rs, info->name);
4365 }
4366 list = list->next;
4367 }
4368 qapi_free_ObjectPropertyInfoList(start);
4369}
4370
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004371void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4372{
4373 int i;
4374 char *sep;
4375 size_t len;
4376
4377 if (nb_args != 2) {
4378 return;
4379 }
4380 sep = strrchr(str, '-');
4381 if (sep) {
4382 str = sep + 1;
4383 }
4384 len = strlen(str);
4385 readline_set_completion_index(rs, len);
4386 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4387 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4388 readline_add_completion(rs, QKeyCode_lookup[i]);
4389 }
4390 }
4391}
4392
Hani Benhabiles40d19392014-05-07 23:41:30 +01004393void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4394{
4395 size_t len;
4396
4397 len = strlen(str);
4398 readline_set_completion_index(rs, len);
4399 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004400 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004401 int count, i;
4402 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004403 NET_CLIENT_OPTIONS_KIND_NONE,
4404 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004405 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004406 const char *name = ncs[i]->name;
4407 if (!strncmp(str, name, len)) {
4408 readline_add_completion(rs, name);
4409 }
4410 }
4411 } else if (nb_args == 3) {
4412 add_completion_option(rs, str, "on");
4413 add_completion_option(rs, str, "off");
4414 }
4415}
4416
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004417void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4418{
4419 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004420 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004421
4422 if (nb_args != 2) {
4423 return;
4424 }
4425
4426 len = strlen(str);
4427 readline_set_completion_index(rs, len);
4428 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004429 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004430 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004431 QemuOpts *opts;
4432 const char *name = ncs[i]->name;
4433 if (strncmp(str, name, len)) {
4434 continue;
4435 }
4436 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4437 if (opts) {
4438 readline_add_completion(rs, name);
4439 }
4440 }
4441}
4442
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004443void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4444{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004445 int i;
4446
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004447 if (nb_args != 2) {
4448 return;
4449 }
4450 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004451 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4452 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4453 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004454}
4455
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004456void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4457 const char *str)
4458{
4459 size_t len;
4460
4461 len = strlen(str);
4462 readline_set_completion_index(rs, len);
4463 if (nb_args == 2) {
4464 int i;
4465 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4466 const char *name = MigrationCapability_lookup[i];
4467 if (!strncmp(str, name, len)) {
4468 readline_add_completion(rs, name);
4469 }
4470 }
4471 } else if (nb_args == 3) {
4472 add_completion_option(rs, str, "on");
4473 add_completion_option(rs, str, "off");
4474 }
4475}
4476
Liang Li50e9a622015-03-23 16:32:29 +08004477void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4478 const char *str)
4479{
4480 size_t len;
4481
4482 len = strlen(str);
4483 readline_set_completion_index(rs, len);
4484 if (nb_args == 2) {
4485 int i;
4486 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4487 const char *name = MigrationParameter_lookup[i];
4488 if (!strncmp(str, name, len)) {
4489 readline_add_completion(rs, name);
4490 }
4491 }
4492 }
4493}
4494
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004495void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4496{
4497 int i;
4498 size_t len;
4499 if (nb_args != 2) {
4500 return;
4501 }
4502 len = strlen(str);
4503 readline_set_completion_index(rs, len);
4504 for (i = 0; host_net_devices[i]; i++) {
4505 if (!strncmp(host_net_devices[i], str, len)) {
4506 readline_add_completion(rs, host_net_devices[i]);
4507 }
4508 }
4509}
4510
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004511void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4512{
Jason Wangeaed4832015-04-23 14:21:38 +08004513 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004514 int count, i, len;
4515
4516 len = strlen(str);
4517 readline_set_completion_index(rs, len);
4518 if (nb_args == 2) {
4519 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004520 NET_CLIENT_OPTIONS_KIND_NONE,
4521 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004522 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004523 int id;
4524 char name[16];
4525
4526 if (net_hub_id_for_client(ncs[i], &id)) {
4527 continue;
4528 }
4529 snprintf(name, sizeof(name), "%d", id);
4530 if (!strncmp(str, name, len)) {
4531 readline_add_completion(rs, name);
4532 }
4533 }
4534 return;
4535 } else if (nb_args == 3) {
4536 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004537 NET_CLIENT_OPTIONS_KIND_NIC,
4538 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004539 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004540 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004541 const char *name;
4542
Jason Wang2c4681f2015-02-02 15:06:38 +08004543 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4544 net_hub_id_for_client(ncs[i], &id)) {
4545 continue;
4546 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004547 name = ncs[i]->name;
4548 if (!strncmp(str, name, len)) {
4549 readline_add_completion(rs, name);
4550 }
4551 }
4552 return;
4553 }
4554}
4555
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004556static void vm_completion(ReadLineState *rs, const char *str)
4557{
4558 size_t len;
4559 BlockDriverState *bs = NULL;
4560
4561 len = strlen(str);
4562 readline_set_completion_index(rs, len);
4563 while ((bs = bdrv_next(bs))) {
4564 SnapshotInfoList *snapshots, *snapshot;
4565
4566 if (!bdrv_can_snapshot(bs)) {
4567 continue;
4568 }
4569 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4570 continue;
4571 }
4572 snapshot = snapshots;
4573 while (snapshot) {
4574 char *completion = snapshot->value->name;
4575 if (!strncmp(str, completion, len)) {
4576 readline_add_completion(rs, completion);
4577 }
4578 completion = snapshot->value->id;
4579 if (!strncmp(str, completion, len)) {
4580 readline_add_completion(rs, completion);
4581 }
4582 snapshot = snapshot->next;
4583 }
4584 qapi_free_SnapshotInfoList(snapshots);
4585 }
4586
4587}
4588
4589void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4590{
4591 if (nb_args == 2) {
4592 vm_completion(rs, str);
4593 }
4594}
4595
4596void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4597{
4598 if (nb_args == 2) {
4599 vm_completion(rs, str);
4600 }
4601}
4602
Wenchao Xiac35b6402013-08-27 20:38:24 +08004603static void monitor_find_completion_by_table(Monitor *mon,
4604 const mon_cmd_t *cmd_table,
4605 char **args,
4606 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004607{
4608 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004609 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004610 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004611 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004612 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004613
bellard81d09122004-07-14 17:21:37 +00004614 if (nb_args <= 1) {
4615 /* command completion */
4616 if (nb_args == 0)
4617 cmdname = "";
4618 else
4619 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004620 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004621 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004622 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004623 }
4624 } else {
4625 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004626 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004627 if (compare_cmd(args[0], cmd->name)) {
4628 break;
4629 }
bellard81d09122004-07-14 17:21:37 +00004630 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004631 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004632 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004633 }
4634
Wenchao Xiad903a772013-08-27 20:38:25 +08004635 if (cmd->sub_table) {
4636 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004637 monitor_find_completion_by_table(mon, cmd->sub_table,
4638 &args[1], nb_args - 1);
4639 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004640 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004641 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004642 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4643 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004644 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004645
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004646 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004647 for(i = 0; i < nb_args - 2; i++) {
4648 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004649 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004650 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004651 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004652 }
4653 }
4654 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004655 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004656 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004657 }
bellard81d09122004-07-14 17:21:37 +00004658 switch(*ptype) {
4659 case 'F':
4660 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004661 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004662 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004663 break;
4664 case 'B':
4665 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004666 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004667 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4668 name = bdrv_get_device_name(bs);
4669 if (str[0] == '\0' ||
4670 !strncmp(name, str, strlen(str))) {
4671 readline_add_completion(mon->rs, name);
4672 }
4673 }
bellard81d09122004-07-14 17:21:37 +00004674 break;
bellard7fe48482004-10-09 18:08:01 +00004675 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004676 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004677 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004678 monitor_find_completion_by_table(mon, cmd_table,
4679 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004680 }
4681 break;
bellard81d09122004-07-14 17:21:37 +00004682 default:
4683 break;
4684 }
4685 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004686}
4687
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004688static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004689 const char *cmdline)
4690{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004691 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004692 char *args[MAX_ARGS];
4693 int nb_args, len;
4694
4695 /* 1. parse the cmdline */
4696 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4697 return;
4698 }
4699#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004700 {
4701 int i;
4702 for (i = 0; i < nb_args; i++) {
4703 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4704 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004705 }
4706#endif
4707
4708 /* if the line ends with a space, it means we want to complete the
4709 next arg */
4710 len = strlen(cmdline);
4711 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4712 if (nb_args >= MAX_ARGS) {
4713 goto cleanup;
4714 }
4715 args[nb_args++] = g_strdup("");
4716 }
4717
4718 /* 2. auto complete according to args */
4719 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004720
4721cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004722 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004723}
4724
aliguori731b0362009-03-05 23:01:42 +00004725static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004726{
aliguori731b0362009-03-05 23:01:42 +00004727 Monitor *mon = opaque;
4728
Jan Kiszkac62313b2009-12-04 14:05:29 +01004729 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004730}
4731
Markus Armbruster40861822015-05-29 10:27:16 +02004732static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4733 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004734{
Eric Blake2d5a8342015-04-15 09:19:23 -06004735 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004736
4737 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004738 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4739 "Capabilities negotiation is already complete, command "
4740 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004741 return true;
4742 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004743 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004744 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4745 "Expecting capabilities negotiation with "
4746 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004747 return true;
4748 }
4749 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004750}
4751
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004752/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004753 * Argument validation rules:
4754 *
4755 * 1. The argument must exist in cmd_args qdict
4756 * 2. The argument type must be the expected one
4757 *
4758 * Special case: If the argument doesn't exist in cmd_args and
4759 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4760 * checking is skipped for it.
4761 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004762static void check_client_args_type(const QDict *client_args,
4763 const QDict *cmd_args, int flags,
4764 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004765{
4766 const QDictEntry *ent;
4767
4768 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4769 QObject *obj;
4770 QString *arg_type;
4771 const QObject *client_arg = qdict_entry_value(ent);
4772 const char *client_arg_name = qdict_entry_key(ent);
4773
4774 obj = qdict_get(cmd_args, client_arg_name);
4775 if (!obj) {
4776 if (flags & QMP_ACCEPT_UNKNOWNS) {
4777 /* handler accepts unknowns */
4778 continue;
4779 }
4780 /* client arg doesn't exist */
Markus Armbruster326283a2015-03-02 18:39:09 +01004781 error_set(errp, QERR_INVALID_PARAMETER, client_arg_name);
4782 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004783 }
4784
4785 arg_type = qobject_to_qstring(obj);
4786 assert(arg_type != NULL);
4787
4788 /* check if argument's type is correct */
4789 switch (qstring_get_str(arg_type)[0]) {
4790 case 'F':
4791 case 'B':
4792 case 's':
4793 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004794 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4795 client_arg_name, "string");
4796 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004797 }
4798 break;
4799 case 'i':
4800 case 'l':
4801 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004802 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004803 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004804 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4805 client_arg_name, "int");
4806 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004807 }
4808 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004809 case 'T':
4810 if (qobject_type(client_arg) != QTYPE_QINT &&
4811 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004812 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4813 client_arg_name, "number");
4814 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004815 }
4816 break;
4817 case 'b':
4818 case '-':
4819 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004820 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4821 client_arg_name, "bool");
4822 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004823 }
4824 break;
4825 case 'O':
4826 assert(flags & QMP_ACCEPT_UNKNOWNS);
4827 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004828 case 'q':
4829 /* Any QObject can be passed. */
4830 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004831 case '/':
4832 case '.':
4833 /*
4834 * These types are not supported by QMP and thus are not
4835 * handled here. Fall through.
4836 */
4837 default:
4838 abort();
4839 }
4840 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004841}
4842
4843/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004844 * - Check if the client has passed all mandatory args
4845 * - Set special flags for argument validation
4846 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004847static void check_mandatory_args(const QDict *cmd_args,
4848 const QDict *client_args, int *flags,
4849 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004850{
4851 const QDictEntry *ent;
4852
4853 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4854 const char *cmd_arg_name = qdict_entry_key(ent);
4855 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4856 assert(type != NULL);
4857
4858 if (qstring_get_str(type)[0] == 'O') {
4859 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4860 *flags |= QMP_ACCEPT_UNKNOWNS;
4861 } else if (qstring_get_str(type)[0] != '-' &&
4862 qstring_get_str(type)[1] != '?' &&
4863 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004864 error_set(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4865 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004866 }
4867 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004868}
4869
4870static QDict *qdict_from_args_type(const char *args_type)
4871{
4872 int i;
4873 QDict *qdict;
4874 QString *key, *type, *cur_qs;
4875
4876 assert(args_type != NULL);
4877
4878 qdict = qdict_new();
4879
4880 if (args_type == NULL || args_type[0] == '\0') {
4881 /* no args, empty qdict */
4882 goto out;
4883 }
4884
4885 key = qstring_new();
4886 type = qstring_new();
4887
4888 cur_qs = key;
4889
4890 for (i = 0;; i++) {
4891 switch (args_type[i]) {
4892 case ',':
4893 case '\0':
4894 qdict_put(qdict, qstring_get_str(key), type);
4895 QDECREF(key);
4896 if (args_type[i] == '\0') {
4897 goto out;
4898 }
4899 type = qstring_new(); /* qdict has ref */
4900 cur_qs = key = qstring_new();
4901 break;
4902 case ':':
4903 cur_qs = type;
4904 break;
4905 default:
4906 qstring_append_chr(cur_qs, args_type[i]);
4907 break;
4908 }
4909 }
4910
4911out:
4912 return qdict;
4913}
4914
4915/*
4916 * Client argument checking rules:
4917 *
4918 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004919 * 2. Each argument provided by the client must be expected
4920 * 3. Each argument provided by the client must have the type expected
4921 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004922 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004923static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4924 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004925{
Markus Armbruster326283a2015-03-02 18:39:09 +01004926 Error *err = NULL;
4927 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004928 QDict *cmd_args;
4929
4930 cmd_args = qdict_from_args_type(cmd->args_type);
4931
4932 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004933 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004934 if (err) {
4935 goto out;
4936 }
4937
Markus Armbruster326283a2015-03-02 18:39:09 +01004938 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004939
4940out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004941 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004942 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004943}
4944
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004945/*
4946 * Input object checking rules
4947 *
4948 * 1. Input object must be a dict
4949 * 2. The "execute" key must exist
4950 * 3. The "execute" key must be a string
4951 * 4. If the "arguments" key exists, it must be a dict
4952 * 5. If the "id" key exists, it can be anything (ie. json-value)
4953 * 6. Any argument not listed above is considered invalid
4954 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004955static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004956{
4957 const QDictEntry *ent;
4958 int has_exec_key = 0;
4959 QDict *input_dict;
4960
4961 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004962 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004963 return NULL;
4964 }
4965
4966 input_dict = qobject_to_qdict(input_obj);
4967
4968 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4969 const char *arg_name = qdict_entry_key(ent);
4970 const QObject *arg_obj = qdict_entry_value(ent);
4971
4972 if (!strcmp(arg_name, "execute")) {
4973 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004974 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4975 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004976 return NULL;
4977 }
4978 has_exec_key = 1;
4979 } else if (!strcmp(arg_name, "arguments")) {
4980 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004981 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4982 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004983 return NULL;
4984 }
Markus Armbruster779cec42015-06-08 10:44:30 +02004985 } else if (!strcmp(arg_name, "id")) {
4986 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004987 } else {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004988 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004989 return NULL;
4990 }
4991 }
4992
4993 if (!has_exec_key) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004994 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004995 return NULL;
4996 }
4997
4998 return input_dict;
4999}
5000
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005001static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5002{
Markus Armbruster326283a2015-03-02 18:39:09 +01005003 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01005004 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005005 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005006 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02005007 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005008 Monitor *mon = cur_mon;
5009
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005010 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01005011 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005012
5013 obj = json_parser_parse(tokens, NULL);
5014 if (!obj) {
5015 // FIXME: should be triggered in json_parser_parse()
Markus Armbruster710aec92015-03-06 11:28:00 +01005016 error_set(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005017 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005018 }
5019
Markus Armbrusterba0510a2015-03-02 18:41:43 +01005020 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005021 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005022 qobject_decref(obj);
5023 goto err_out;
5024 }
5025
Markus Armbruster74358f22015-03-06 19:35:59 +01005026 mon->qmp.id = qdict_get(input, "id");
5027 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005028
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005029 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005030 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005031 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005032 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005033 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5034 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005035 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005036 }
Markus Armbruster40861822015-05-29 10:27:16 +02005037 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005038 goto err_out;
5039 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005040
5041 obj = qdict_get(input, "arguments");
5042 if (!obj) {
5043 args = qdict_new();
5044 } else {
5045 args = qobject_to_qdict(obj);
5046 QINCREF(args);
5047 }
5048
Markus Armbruster326283a2015-03-02 18:39:09 +01005049 qmp_check_client_args(cmd, args, &local_err);
5050 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005051 goto err_out;
5052 }
5053
Markus Armbruster84add862015-03-05 16:45:15 +01005054 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5055 /* Command failed... */
Markus Armbruster452e0302015-03-06 19:11:13 +01005056 if (!mon->error) {
Markus Armbruster84add862015-03-05 16:45:15 +01005057 /* ... without setting an error, so make one up */
Markus Armbruster710aec92015-03-06 11:28:00 +01005058 error_set(&local_err, QERR_UNDEFINED_ERROR);
Markus Armbruster84add862015-03-05 16:45:15 +01005059 }
5060 }
Markus Armbruster710aec92015-03-06 11:28:00 +01005061 if (mon->error) {
5062 error_set(&local_err, mon->error->err_class, "%s",
5063 mon->error->err_msg);
5064 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005065
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005066err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005067 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005068 qobject_decref(data);
Markus Armbruster70ea0c52015-03-06 10:47:08 +01005069 QDECREF(mon->error);
5070 mon->error = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005071 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005072 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005073}
5074
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005075static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005076{
5077 Monitor *old_mon = cur_mon;
5078
5079 cur_mon = opaque;
5080
Markus Armbruster74358f22015-03-06 19:35:59 +01005081 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005082
5083 cur_mon = old_mon;
5084}
5085
aliguori731b0362009-03-05 23:01:42 +00005086static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005087{
aliguori731b0362009-03-05 23:01:42 +00005088 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005089 int i;
aliguori376253e2009-03-05 23:01:23 +00005090
aliguori731b0362009-03-05 23:01:42 +00005091 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005092
aliguoricde76ee2009-03-05 23:01:51 +00005093 if (cur_mon->rs) {
5094 for (i = 0; i < size; i++)
5095 readline_handle_byte(cur_mon->rs, buf[i]);
5096 } else {
5097 if (size == 0 || buf[size - 1] != 0)
5098 monitor_printf(cur_mon, "corrupted command\n");
5099 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005100 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005101 }
aliguori731b0362009-03-05 23:01:42 +00005102
5103 cur_mon = old_mon;
5104}
aliguorid8f44602008-10-06 13:52:44 +00005105
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005106static void monitor_command_cb(void *opaque, const char *cmdline,
5107 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005108{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005109 Monitor *mon = opaque;
5110
aliguori731b0362009-03-05 23:01:42 +00005111 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005112 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005113 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005114}
5115
aliguoricde76ee2009-03-05 23:01:51 +00005116int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005117{
aliguoricde76ee2009-03-05 23:01:51 +00005118 if (!mon->rs)
5119 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005120 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005121 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005122}
5123
aliguori376253e2009-03-05 23:01:23 +00005124void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005125{
aliguoricde76ee2009-03-05 23:01:51 +00005126 if (!mon->rs)
5127 return;
aliguori731b0362009-03-05 23:01:42 +00005128 if (--mon->suspend_cnt == 0)
5129 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005130}
5131
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005132static QObject *get_qmp_greeting(void)
5133{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005134 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005135
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005136 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005137 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5138}
5139
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005140static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005141{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005142 QObject *data;
5143 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005144
Luiz Capitulino47116d12010-02-08 17:01:30 -02005145 switch (event) {
5146 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005147 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005148 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005149 monitor_json_emitter(mon, data);
5150 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005151 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005152 break;
5153 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005154 json_message_parser_destroy(&mon->qmp.parser);
5155 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005156 mon_refcount--;
5157 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005158 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005159 }
5160}
5161
aliguori731b0362009-03-05 23:01:42 +00005162static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005163{
aliguori376253e2009-03-05 23:01:23 +00005164 Monitor *mon = opaque;
5165
aliguori2724b182009-03-05 23:01:47 +00005166 switch (event) {
5167 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005168 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005169 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005170 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005171 if (mon->reset_seen) {
5172 readline_restart(mon->rs);
5173 monitor_resume(mon);
5174 monitor_flush(mon);
5175 } else {
5176 mon->suspend_cnt = 0;
5177 }
aliguori2724b182009-03-05 23:01:47 +00005178 break;
ths86e94de2007-01-05 22:01:59 +00005179
aliguori2724b182009-03-05 23:01:47 +00005180 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005181 if (mon->reset_seen) {
5182 if (mon->suspend_cnt == 0) {
5183 monitor_printf(mon, "\n");
5184 }
5185 monitor_flush(mon);
5186 monitor_suspend(mon);
5187 } else {
5188 mon->suspend_cnt++;
5189 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005190 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005191 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005192 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005193 break;
5194
Amit Shahb6b8df52009-10-07 18:31:16 +05305195 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005196 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5197 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005198 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005199 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005200 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005201 }
5202 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005203 mon_refcount++;
5204 break;
5205
5206 case CHR_EVENT_CLOSED:
5207 mon_refcount--;
5208 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005209 break;
5210 }
ths86e94de2007-01-05 22:01:59 +00005211}
5212
Wayne Xia816f8922011-10-12 11:32:41 +08005213static int
5214compare_mon_cmd(const void *a, const void *b)
5215{
5216 return strcmp(((const mon_cmd_t *)a)->name,
5217 ((const mon_cmd_t *)b)->name);
5218}
5219
5220static void sortcmdlist(void)
5221{
5222 int array_num;
5223 int elem_size = sizeof(mon_cmd_t);
5224
5225 array_num = sizeof(mon_cmds)/elem_size-1;
5226 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5227
5228 array_num = sizeof(info_cmds)/elem_size-1;
5229 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5230}
5231
aliguori76655d62009-03-06 20:27:37 +00005232
5233/*
5234 * Local variables:
5235 * c-indent-level: 4
5236 * c-basic-offset: 4
5237 * tab-width: 8
5238 * End:
5239 */
5240
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005241/* These functions just adapt the readline interface in a typesafe way. We
5242 * could cast function pointers but that discards compiler checks.
5243 */
Stefan Weild5d15072014-01-25 18:18:23 +01005244static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5245 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005246{
5247 va_list ap;
5248 va_start(ap, fmt);
5249 monitor_vprintf(opaque, fmt, ap);
5250 va_end(ap);
5251}
5252
5253static void monitor_readline_flush(void *opaque)
5254{
5255 monitor_flush(opaque);
5256}
5257
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005258static void __attribute__((constructor)) monitor_lock_init(void)
5259{
5260 qemu_mutex_init(&monitor_lock);
5261}
5262
aliguori731b0362009-03-05 23:01:42 +00005263void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005264{
aliguori731b0362009-03-05 23:01:42 +00005265 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005266 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005267
5268 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005269 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005270 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005271 is_first_init = 0;
5272 }
aliguori87127162009-03-05 23:01:29 +00005273
Wenchao Xiab01fe892013-08-27 20:38:19 +08005274 mon = g_malloc(sizeof(*mon));
5275 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005276
aliguori87127162009-03-05 23:01:29 +00005277 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005278 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005279 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005280 mon->rs = readline_init(monitor_readline_printf,
5281 monitor_readline_flush,
5282 mon,
5283 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005284 monitor_read_command(mon, 0);
5285 }
aliguori87127162009-03-05 23:01:29 +00005286
Markus Armbruster9f3982f2015-03-06 19:56:38 +01005287 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005288 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5289 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005290 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005291 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005292 } else {
5293 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5294 monitor_event, mon);
5295 }
aliguori87127162009-03-05 23:01:29 +00005296
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005297 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005298 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005299 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00005300}
5301
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005302static void bdrv_password_cb(void *opaque, const char *password,
5303 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005304{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005305 Monitor *mon = opaque;
5306 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005307 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005308 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005309
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005310 bdrv_add_key(bs, password, &local_err);
5311 if (local_err) {
5312 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5313 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005314 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005315 }
aliguori731b0362009-03-05 23:01:42 +00005316 if (mon->password_completion_cb)
5317 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005318
aliguori731b0362009-03-05 23:01:42 +00005319 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005320}
aliguoric0f4ce72009-03-05 23:01:01 +00005321
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005322int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005323 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005324 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005325{
aliguoricde76ee2009-03-05 23:01:51 +00005326 int err;
5327
aliguori376253e2009-03-05 23:01:23 +00005328 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5329 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005330
aliguori731b0362009-03-05 23:01:42 +00005331 mon->password_completion_cb = completion_cb;
5332 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005333
aliguoricde76ee2009-03-05 23:01:51 +00005334 err = monitor_read_password(mon, bdrv_password_cb, bs);
5335
5336 if (err && completion_cb)
5337 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005338
5339 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005340}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005341
5342int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005343 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005344 void *opaque)
5345{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005346 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005347 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005348
Fam Zheng55606252015-03-02 19:36:46 +08005349 blk = blk_by_name(device);
5350 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005351 monitor_printf(mon, "Device not found %s\n", device);
5352 return -1;
5353 }
5354
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005355 bdrv_add_key(blk_bs(blk), NULL, &err);
5356 if (err) {
5357 error_free(err);
5358 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5359 }
5360
5361 if (completion_cb) {
5362 completion_cb(opaque, 0);
5363 }
5364 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005365}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005366
5367QemuOptsList qemu_mon_opts = {
5368 .name = "mon",
5369 .implied_opt_name = "chardev",
5370 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5371 .desc = {
5372 {
5373 .name = "mode",
5374 .type = QEMU_OPT_STRING,
5375 },{
5376 .name = "chardev",
5377 .type = QEMU_OPT_STRING,
5378 },{
5379 .name = "default",
5380 .type = QEMU_OPT_BOOL,
5381 },{
5382 .name = "pretty",
5383 .type = QEMU_OPT_BOOL,
5384 },
5385 { /* end of list */ }
5386 },
5387};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005388
5389#ifndef TARGET_I386
5390void qmp_rtc_reset_reinjection(Error **errp)
5391{
5392 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5393}
5394#endif