blob: daa3d98cf386d02d9cc0a4c13b210b02cbe6353c [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"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010052#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010053#include "qapi/qmp/qint.h"
54#include "qapi/qmp/qfloat.h"
55#include "qapi/qmp/qlist.h"
56#include "qapi/qmp/qbool.h"
57#include "qapi/qmp/qstring.h"
58#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010061#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010062#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000063#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010064#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020066#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020067#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053068#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010069#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010070#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050071#include "qmp-commands.h"
72#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020075#include "qapi/qmp-event.h"
76#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080077#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000078
Markus Armbruster1ce6be22015-02-06 14:18:24 +010079/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020080#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010081#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020082#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010083#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020084
bellard9307c4c2004-04-04 12:57:25 +000085/*
86 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000087 *
bellard9307c4c2004-04-04 12:57:25 +000088 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000089 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000090 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080091 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010092 * 'O' option string of the form NAME=VALUE,...
93 * parsed according to QemuOptsList given by its name
94 * Example: 'device:O' uses qemu_device_opts.
95 * Restriction: only lists with empty desc are supported
96 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000097 * 'i' 32 bit integer
98 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030099 * 'M' Non-negative target long (32 or 64 bit), in user mode the
100 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200101 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200102 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
103 * K, k suffix, which multiplies the value by 2^60 for suffixes E
104 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
105 * 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 +0100106 * 'T' double
107 * user mode accepts an optional ms, us, ns suffix,
108 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000109 * '/' optional gdb-like print format (like "/10x")
110 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300111 * '?' optional type (for all types, except '/')
112 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100113 * 'b' boolean
114 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300115 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000116 *
117 */
118
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000120 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000121 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *params;
123 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300125 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100126 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300127 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800128 /* @sub_table is a list of 2nd level of commands. If it do not exist,
129 * mhandler should be used. If it exist, sub_table[?].mhandler should be
130 * used, and mhandler of 1st level plays the role of help function.
131 */
132 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100133 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500134} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000135
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100136/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500137typedef struct mon_fd_t mon_fd_t;
138struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100139 char *name;
140 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500141 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142};
143
Corey Bryantba1c0482012-08-14 16:43:43 -0400144/* file descriptor associated with a file descriptor set */
145typedef struct MonFdsetFd MonFdsetFd;
146struct MonFdsetFd {
147 int fd;
148 bool removed;
149 char *opaque;
150 QLIST_ENTRY(MonFdsetFd) next;
151};
152
153/* file descriptor set containing fds passed via SCM_RIGHTS */
154typedef struct MonFdset MonFdset;
155struct MonFdset {
156 int64_t id;
157 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400158 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400159 QLIST_ENTRY(MonFdset) next;
160};
161
Markus Armbruster74358f22015-03-06 19:35:59 +0100162typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200163 QObject *id;
164 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100165 /*
166 * When a client connects, we're in capabilities negotiation mode.
167 * When command qmp_capabilities succeeds, we go into command
168 * mode.
169 */
170 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100171} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200172
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100173/*
174 * To prevent flooding clients, events can be throttled. The
175 * throttling is calculated globally, rather than per-Monitor
176 * instance.
177 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200178typedef struct MonitorQAPIEventState {
179 QAPIEvent event; /* Event being tracked */
180 int64_t rate; /* Minimum time (in ns) between two events */
181 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100182 QEMUTimer *timer; /* Timer for handling delayed events */
183 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200184} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100185
aliguori87127162009-03-05 23:01:29 +0000186struct Monitor {
187 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200188 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000189 int flags;
190 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400191 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200192
193 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400194 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200195 guint out_watch;
196
197 /* Read under either BQL or out_lock, written with BQL+out_lock. */
198 int mux_out;
199
aliguori731b0362009-03-05 23:01:42 +0000200 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100201 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200202 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200203 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000204 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800205 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500206 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000207 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000208};
209
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300210/* QMP checker flags */
211#define QMP_ACCEPT_UNKNOWNS 1
212
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200213/* Protects mon_list, monitor_event_state. */
214static QemuMutex monitor_lock;
215
Blue Swirl72cf2d42009-09-12 07:36:22 +0000216static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400217static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400218static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000219
Wayne Xia816f8922011-10-12 11:32:41 +0800220static mon_cmd_t mon_cmds[];
221static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000222
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300223static const mon_cmd_t qmp_cmds[];
224
Markus Armbruster8631b602010-02-18 11:41:55 +0100225Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000226
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100227static void monitor_command_cb(void *opaque, const char *cmdline,
228 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000229
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100230/**
231 * Is @mon a QMP monitor?
232 */
233static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200234{
235 return (mon->flags & MONITOR_USE_CONTROL);
236}
237
Markus Armbruster489653b2015-03-06 20:01:05 +0100238/**
239 * Is the current monitor, if any, a QMP monitor?
240 */
241bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100242{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100243 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100244}
245
Anthony Liguori7060b472011-09-02 12:34:50 -0500246void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000247{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200248 if (!mon->rs)
249 return;
250
aliguori731b0362009-03-05 23:01:42 +0000251 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
252 if (show_prompt)
253 readline_show_prompt(mon->rs);
254}
bellard6a00d602005-11-21 23:25:50 +0000255
Anthony Liguori7060b472011-09-02 12:34:50 -0500256int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
257 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000258{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100259 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000260 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
261 /* prompt is printed on return from the command handler */
262 return 0;
263 } else {
264 monitor_printf(mon, "terminal does not support password prompting\n");
265 return -ENOTTY;
266 }
aliguoribb5fc202009-03-05 23:01:15 +0000267}
268
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200269static void monitor_flush_locked(Monitor *mon);
270
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100271static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
272 void *opaque)
273{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200274 Monitor *mon = opaque;
275
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200276 qemu_mutex_lock(&mon->out_lock);
277 mon->out_watch = 0;
278 monitor_flush_locked(mon);
279 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 return FALSE;
281}
282
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200283/* Called with mon->out_lock held. */
284static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000285{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400287 size_t len;
288 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400290 if (mon->skip_flush) {
291 return;
292 }
293
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 buf = qstring_get_str(mon->outbuf);
295 len = qstring_get_length(mon->outbuf);
296
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200297 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200299 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
300 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 QDECREF(mon->outbuf);
302 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100303 return;
304 }
305 if (rc > 0) {
306 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 QString *tmp = qstring_from_str(buf + rc);
308 QDECREF(mon->outbuf);
309 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100310 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200311 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200312 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200313 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200314 }
bellard7e2515e2004-08-01 21:52:19 +0000315 }
316}
317
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318void monitor_flush(Monitor *mon)
319{
320 qemu_mutex_lock(&mon->out_lock);
321 monitor_flush_locked(mon);
322 qemu_mutex_unlock(&mon->out_lock);
323}
324
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400325/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000326static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000327{
ths60fe76f2007-12-16 03:02:09 +0000328 char c;
aliguori731b0362009-03-05 23:01:42 +0000329
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200330 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000331 for(;;) {
332 c = *str++;
333 if (c == '\0')
334 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400335 if (c == '\n') {
336 qstring_append_chr(mon->outbuf, '\r');
337 }
338 qstring_append_chr(mon->outbuf, c);
339 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200340 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400341 }
bellard7e2515e2004-08-01 21:52:19 +0000342 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000344}
345
aliguori376253e2009-03-05 23:01:23 +0000346void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000347{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200349
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200350 if (!mon)
351 return;
352
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100353 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200355 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400357 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000360}
361
aliguori376253e2009-03-05 23:01:23 +0000362void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000363{
364 va_list ap;
365 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000366 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000367 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000368}
369
Stefan Weil8b7968f2010-09-23 21:28:05 +0200370static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
371 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000372{
373 va_list ap;
374 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000376 va_end(ap);
377 return 0;
378}
379
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200380static void monitor_json_emitter(Monitor *mon, const QObject *data)
381{
382 QString *json;
383
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200384 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
385 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200386 assert(json != NULL);
387
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200388 qstring_append_chr(json, '\n');
389 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200390
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 QDECREF(json);
392}
393
Markus Armbruster710aec92015-03-06 11:28:00 +0100394static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300395{
396 QObject *obj;
397
Markus Armbruster710aec92015-03-06 11:28:00 +0100398 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
399 ErrorClass_lookup[error_get_class(err)],
400 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300401
402 return qobject_to_qdict(obj);
403}
404
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100405static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100406 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200407{
408 QDict *qmp;
409
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100410 trace_monitor_protocol_emitter(mon);
411
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100412 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200413 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300414 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200415 if (data) {
416 qobject_incref(data);
417 qdict_put_obj(qmp, "return", data);
418 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200419 /* return an empty QDict by default */
420 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421 }
422 } else {
423 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100424 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426
Markus Armbruster74358f22015-03-06 19:35:59 +0100427 if (mon->qmp.id) {
428 qdict_put_obj(qmp, "id", mon->qmp.id);
429 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200430 }
431
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200432 monitor_json_emitter(mon, QOBJECT(qmp));
433 QDECREF(qmp);
434}
435
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200436
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200437static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100438
439/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200440 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200441 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200443static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444{
445 Monitor *mon;
446
447 trace_monitor_protocol_event_emit(event, data);
448 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100449 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100450 monitor_json_emitter(mon, data);
451 }
452 }
453}
454
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455/*
456 * Queue a new event for emission to Monitor instances,
457 * applying any rate limiting if required.
458 */
459static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200460monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100461{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200462 MonitorQAPIEventState *evstate;
463 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100464 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100467 trace_monitor_protocol_event_queue(event,
468 data,
469 evstate->rate,
470 evstate->last,
471 now);
472
473 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200474 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100475 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200476 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477 evstate->last = now;
478 } else {
479 int64_t delta = now - evstate->last;
480 if (evstate->data ||
481 delta < evstate->rate) {
482 /* If there's an existing event pending, replace
483 * it with the new event, otherwise schedule a
484 * timer for delayed emission
485 */
486 if (evstate->data) {
487 qobject_decref(evstate->data);
488 } else {
489 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100490 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200492 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493 qobject_incref(evstate->data);
494 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200495 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 evstate->last = now;
497 }
498 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200499 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500}
501
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502/*
503 * The callback invoked by QemuTimer when a delayed
504 * event is ready to be emitted
505 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200506static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200508 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100509 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511 trace_monitor_protocol_event_handler(evstate->event,
512 evstate->data,
513 evstate->last,
514 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200515 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200517 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518 qobject_decref(evstate->data);
519 evstate->data = NULL;
520 }
521 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200522 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100523}
524
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525/*
526 * @event: the event ID to be limited
527 * @rate: the rate limit in milliseconds
528 *
529 * Sets a rate limit on a particular event, so no
530 * more than 1 event will be emitted within @rate
531 * milliseconds
532 */
533static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200534monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100535{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200536 MonitorQAPIEventState *evstate;
537 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100538
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200539 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540
541 trace_monitor_protocol_event_throttle(event, rate);
542 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700543 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545 evstate->last = 0;
546 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200547 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
548 SCALE_MS,
549 monitor_qapi_event_handler,
550 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100551}
552
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200553static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200555 /* Limit guest-triggerable events to 1 per second */
556 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200557 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200558 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
560 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100562
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200563 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200564}
565
Markus Armbruster485febc2015-03-13 17:25:50 +0100566static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200567{
Markus Armbruster485febc2015-03-13 17:25:50 +0100568 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200569}
570
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100571static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200572
Wenchao Xiab01fe892013-08-27 20:38:19 +0800573static void monitor_data_init(Monitor *mon)
574{
575 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200576 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800578 /* Use *mon_cmds by default. */
579 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800580}
581
582static void monitor_data_destroy(Monitor *mon)
583{
584 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200585 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800586}
587
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200588char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
589 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200590{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200591 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200592 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200593
Wenchao Xiab01fe892013-08-27 20:38:19 +0800594 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400595 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596
597 old_mon = cur_mon;
598 cur_mon = &hmp;
599
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200600 if (has_cpu_index) {
601 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200602 if (ret < 0) {
603 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100604 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
605 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 goto out;
607 }
608 }
609
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100610 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200611 cur_mon = old_mon;
612
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200613 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400614 if (qstring_get_length(hmp.outbuf) > 0) {
615 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200616 } else {
617 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200618 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200619 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200620
621out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800622 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200623 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624}
625
bellard9dc39cb2004-03-14 21:38:27 +0000626static int compare_cmd(const char *name, const char *list)
627{
628 const char *p, *pstart;
629 int len;
630 len = strlen(name);
631 p = list;
632 for(;;) {
633 pstart = p;
634 p = strchr(p, '|');
635 if (!p)
636 p = pstart + strlen(pstart);
637 if ((p - pstart) == len && !memcmp(pstart, name, len))
638 return 1;
639 if (*p == '\0')
640 break;
641 p++;
642 }
643 return 0;
644}
645
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800646static int get_str(char *buf, int buf_size, const char **pp)
647{
648 const char *p;
649 char *q;
650 int c;
651
652 q = buf;
653 p = *pp;
654 while (qemu_isspace(*p)) {
655 p++;
656 }
657 if (*p == '\0') {
658 fail:
659 *q = '\0';
660 *pp = p;
661 return -1;
662 }
663 if (*p == '\"') {
664 p++;
665 while (*p != '\0' && *p != '\"') {
666 if (*p == '\\') {
667 p++;
668 c = *p++;
669 switch (c) {
670 case 'n':
671 c = '\n';
672 break;
673 case 'r':
674 c = '\r';
675 break;
676 case '\\':
677 case '\'':
678 case '\"':
679 break;
680 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100681 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800682 goto fail;
683 }
684 if ((q - buf) < buf_size - 1) {
685 *q++ = c;
686 }
687 } else {
688 if ((q - buf) < buf_size - 1) {
689 *q++ = *p;
690 }
691 p++;
692 }
693 }
694 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100695 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800696 goto fail;
697 }
698 p++;
699 } else {
700 while (*p != '\0' && !qemu_isspace(*p)) {
701 if ((q - buf) < buf_size - 1) {
702 *q++ = *p;
703 }
704 p++;
705 }
706 }
707 *q = '\0';
708 *pp = p;
709 return 0;
710}
711
712#define MAX_ARGS 16
713
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800714static void free_cmdline_args(char **args, int nb_args)
715{
716 int i;
717
718 assert(nb_args <= MAX_ARGS);
719
720 for (i = 0; i < nb_args; i++) {
721 g_free(args[i]);
722 }
723
724}
725
726/*
727 * Parse the command line to get valid args.
728 * @cmdline: command line to be parsed.
729 * @pnb_args: location to store the number of args, must NOT be NULL.
730 * @args: location to store the args, which should be freed by caller, must
731 * NOT be NULL.
732 *
733 * Returns 0 on success, negative on failure.
734 *
735 * NOTE: this parser is an approximate form of the real command parser. Number
736 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
737 * return with failure.
738 */
739static int parse_cmdline(const char *cmdline,
740 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800741{
742 const char *p;
743 int nb_args, ret;
744 char buf[1024];
745
746 p = cmdline;
747 nb_args = 0;
748 for (;;) {
749 while (qemu_isspace(*p)) {
750 p++;
751 }
752 if (*p == '\0') {
753 break;
754 }
755 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800756 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800757 }
758 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800759 if (ret < 0) {
760 goto fail;
761 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800762 args[nb_args] = g_strdup(buf);
763 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800764 }
765 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800766 return 0;
767
768 fail:
769 free_cmdline_args(args, nb_args);
770 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800771}
772
Wenchao Xia66855492013-08-27 20:38:23 +0800773static void help_cmd_dump_one(Monitor *mon,
774 const mon_cmd_t *cmd,
775 char **prefix_args,
776 int prefix_args_nb)
777{
778 int i;
779
780 for (i = 0; i < prefix_args_nb; i++) {
781 monitor_printf(mon, "%s ", prefix_args[i]);
782 }
783 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
784}
785
786/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500787static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800788 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000789{
Anthony Liguoric227f092009-10-01 16:12:16 -0500790 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000791
Wenchao Xia66855492013-08-27 20:38:23 +0800792 /* No valid arg need to compare with, dump all in *cmds */
793 if (arg_index >= nb_args) {
794 for (cmd = cmds; cmd->name != NULL; cmd++) {
795 help_cmd_dump_one(mon, cmd, args, arg_index);
796 }
797 return;
798 }
799
800 /* Find one entry to dump */
801 for (cmd = cmds; cmd->name != NULL; cmd++) {
802 if (compare_cmd(args[arg_index], cmd->name)) {
803 if (cmd->sub_table) {
804 /* continue with next arg */
805 help_cmd_dump(mon, cmd->sub_table,
806 args, nb_args, arg_index + 1);
807 } else {
808 help_cmd_dump_one(mon, cmd, args, arg_index);
809 }
810 break;
811 }
bellard9dc39cb2004-03-14 21:38:27 +0000812 }
813}
814
aliguori376253e2009-03-05 23:01:23 +0000815static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000816{
Wenchao Xia66855492013-08-27 20:38:23 +0800817 char *args[MAX_ARGS];
818 int nb_args = 0;
819
820 /* 1. parse user input */
821 if (name) {
822 /* special case for log, directly dump and return */
823 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000824 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000825 monitor_printf(mon, "Log items (comma separated):\n");
826 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000827 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000828 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000829 }
Wenchao Xia66855492013-08-27 20:38:23 +0800830 return;
831 }
832
833 if (parse_cmdline(name, &nb_args, args) < 0) {
834 return;
bellardf193c792004-03-21 17:06:25 +0000835 }
bellard9dc39cb2004-03-14 21:38:27 +0000836 }
Wenchao Xia66855492013-08-27 20:38:23 +0800837
838 /* 2. dump the contents according to parsed args */
839 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
840
841 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000842}
843
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300844static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300845{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300846 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300847}
848
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100849static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530850{
851 const char *tp_name = qdict_get_str(qdict, "name");
852 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200853 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000854
Lluís Vilanova14101d02014-08-25 13:20:03 +0200855 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
856 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100857 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000858 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530859}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100860
Michael Rothc45a8162011-10-02 08:44:37 -0500861#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100862static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100863{
864 const char *op = qdict_get_try_str(qdict, "op");
865 const char *arg = qdict_get_try_str(qdict, "arg");
866
867 if (!op) {
868 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
869 } else if (!strcmp(op, "on")) {
870 st_set_trace_file_enabled(true);
871 } else if (!strcmp(op, "off")) {
872 st_set_trace_file_enabled(false);
873 } else if (!strcmp(op, "flush")) {
874 st_flush_trace_buffer();
875 } else if (!strcmp(op, "set")) {
876 if (arg) {
877 st_set_trace_file(arg);
878 }
879 } else {
880 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
881 help_cmd(mon, "trace-file");
882 }
883}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530884#endif
885
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100886static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000887{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300888 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000889}
890
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300891CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000892{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300893 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200894 const mon_cmd_t *cmd;
895
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300896 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200897 info = g_malloc0(sizeof(*info));
898 info->value = g_malloc0(sizeof(*info->value));
899 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200900
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300901 info->next = cmd_list;
902 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200903 }
904
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300905 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000906}
907
Daniel P. Berrange48608532012-05-21 17:59:51 +0100908EventInfoList *qmp_query_events(Error **errp)
909{
910 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200911 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100912
Wenchao Xia75175172014-06-18 08:43:54 +0200913 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
914 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100915 assert(event_name != NULL);
916 info = g_malloc0(sizeof(*info));
917 info->value = g_malloc0(sizeof(*info->value));
918 info->value->name = g_strdup(event_name);
919
920 info->next = ev_list;
921 ev_list = info;
922 }
923
924 return ev_list;
925}
926
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300927/* set the current CPU defined by the user */
928int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000929{
Andreas Färber55e5c282012-12-17 06:18:02 +0100930 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000931
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100932 cpu = qemu_get_cpu(cpu_index);
933 if (cpu == NULL) {
934 return -1;
bellard6a00d602005-11-21 23:25:50 +0000935 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200936 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100937 return 0;
bellard6a00d602005-11-21 23:25:50 +0000938}
939
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700940static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000941{
aliguori731b0362009-03-05 23:01:42 +0000942 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300943 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000944 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300945 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700946 return cur_mon->mon_cpu;
947}
948
949static CPUArchState *mon_get_cpu_env(void)
950{
951 return mon_get_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{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700956 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200957}
958
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100959static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000960{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700961 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000962}
963
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100964static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000965{
aliguori376253e2009-03-05 23:01:23 +0000966 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200967 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000968}
969
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100970static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300971{
972 dump_opcount_info((FILE *)mon, monitor_fprintf);
973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000976{
977 int i;
bellard7e2515e2004-08-01 21:52:19 +0000978 const char *str;
ths3b46e622007-09-17 08:09:54 +0000979
aliguoricde76ee2009-03-05 23:01:51 +0000980 if (!mon->rs)
981 return;
bellard7e2515e2004-08-01 21:52:19 +0000982 i = 0;
983 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000984 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000985 if (!str)
986 break;
aliguori376253e2009-03-05 23:01:23 +0000987 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000988 i++;
bellardaa455482004-04-04 13:07:25 +0000989 }
990}
991
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100992static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000993{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700994 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000995}
j_mayer76a66252007-03-07 08:32:30 +0000996
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100997static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530998{
Lluís Vilanova14101d02014-08-25 13:20:03 +0200999 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1000 TraceEventInfoList *elem;
1001
1002 for (elem = events; elem != NULL; elem = elem->next) {
1003 monitor_printf(mon, "%s : state %u\n",
1004 elem->value->name,
1005 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1006 }
1007 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301008}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301009
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001010void qmp_client_migrate_info(const char *protocol, const char *hostname,
1011 bool has_port, int64_t port,
1012 bool has_tls_port, int64_t tls_port,
1013 bool has_cert_subject, const char *cert_subject,
1014 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001015{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001016 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001017 if (!qemu_using_spice(errp)) {
1018 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001019 }
1020
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001021 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001022 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001023 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001024 }
1025
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001026 if (qemu_spice_migrate_info(hostname,
1027 has_port ? port : -1,
1028 has_tls_port ? tls_port : -1,
1029 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001030 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001031 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001032 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001033 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001034 }
1035
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001036 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001037}
1038
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001039static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001040{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001041 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001042}
1043
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001044static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001045{
1046 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001047 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001048
bellard9307c4c2004-04-04 12:57:25 +00001049 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001050 mask = 0;
1051 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001052 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001053 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001054 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001055 return;
1056 }
1057 }
Peter Maydell24537a02013-02-11 16:41:23 +00001058 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001059}
1060
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001061static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001062{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001063 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001064 if (!option || !strcmp(option, "on")) {
1065 singlestep = 1;
1066 } else if (!strcmp(option, "off")) {
1067 singlestep = 0;
1068 } else {
1069 monitor_printf(mon, "unexpected option %s\n", option);
1070 }
1071}
1072
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001073static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001074{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001075 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001076 if (!device)
1077 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1078 if (gdbserver_start(device) < 0) {
1079 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1080 device);
1081 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001082 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001083 } else {
aliguori59030a82009-04-05 18:43:41 +00001084 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1085 device);
bellard8a7ddc32004-03-31 19:00:16 +00001086 }
1087}
1088
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001089static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001090{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001091 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001092 if (select_watchdog_action(action) == -1) {
1093 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1094 }
1095}
1096
aliguori376253e2009-03-05 23:01:23 +00001097static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001098{
aliguori376253e2009-03-05 23:01:23 +00001099 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001100 switch(c) {
1101 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001103 break;
1104 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001105 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001106 break;
1107 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001109 break;
1110 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001111 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001112 break;
1113 default:
1114 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001116 } else {
aliguori376253e2009-03-05 23:01:23 +00001117 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001118 }
1119 break;
1120 }
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001122}
1123
aliguori376253e2009-03-05 23:01:23 +00001124static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001125 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001126{
Blue Swirl23842aa2010-01-12 20:27:43 +00001127 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001128 uint8_t buf[16];
1129 uint64_t v;
1130
1131 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001132 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001133#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001134 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001135 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001136 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001137 } else if (wsize == 4) {
1138 flags = 0;
1139 } else {
bellard6a15fd12006-04-12 21:07:07 +00001140 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001141 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001142 if (env) {
1143#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001144 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001145 (env->segs[R_CS].flags & DESC_L_MASK))
1146 flags = 2;
1147 else
1148#endif
1149 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1150 flags = 1;
1151 }
bellard4c27ba22004-04-25 18:05:08 +00001152 }
1153#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001154#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001155 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001156 flags = msr_le << 16;
1157 flags |= env->bfd_mach;
1158#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001159 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001160 return;
1161 }
1162
1163 len = wsize * count;
1164 if (wsize == 1)
1165 line_size = 8;
1166 else
1167 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001168 max_digits = 0;
1169
1170 switch(format) {
1171 case 'o':
1172 max_digits = (wsize * 8 + 2) / 3;
1173 break;
1174 default:
1175 case 'x':
1176 max_digits = (wsize * 8) / 4;
1177 break;
1178 case 'u':
1179 case 'd':
1180 max_digits = (wsize * 8 * 10 + 32) / 33;
1181 break;
1182 case 'c':
1183 wsize = 1;
1184 break;
1185 }
1186
1187 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001188 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001190 else
aliguori376253e2009-03-05 23:01:23 +00001191 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001192 l = len;
1193 if (l > line_size)
1194 l = line_size;
1195 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001196 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001197 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001198 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001200 break;
1201 }
bellard9307c4c2004-04-04 12:57:25 +00001202 }
ths5fafdf22007-09-16 21:08:06 +00001203 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001204 while (i < l) {
1205 switch(wsize) {
1206 default:
1207 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001208 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001209 break;
1210 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001211 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001212 break;
1213 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001214 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001215 break;
1216 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001217 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001218 break;
1219 }
aliguori376253e2009-03-05 23:01:23 +00001220 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001221 switch(format) {
1222 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001224 break;
1225 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001227 break;
1228 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001230 break;
1231 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001233 break;
1234 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001235 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001236 break;
1237 }
1238 i += wsize;
1239 }
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001241 addr += l;
1242 len -= l;
1243 }
1244}
1245
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001246static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001247{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001248 int count = qdict_get_int(qdict, "count");
1249 int format = qdict_get_int(qdict, "format");
1250 int size = qdict_get_int(qdict, "size");
1251 target_long addr = qdict_get_int(qdict, "addr");
1252
aliguori376253e2009-03-05 23:01:23 +00001253 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001254}
1255
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001256static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001257{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001258 int count = qdict_get_int(qdict, "count");
1259 int format = qdict_get_int(qdict, "format");
1260 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001261 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001262
aliguori376253e2009-03-05 23:01:23 +00001263 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001264}
1265
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001266static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001267{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001268 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001269 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001270
bellard9307c4c2004-04-04 12:57:25 +00001271 switch(format) {
1272 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001273 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001274 break;
1275 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001276 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001277 break;
1278 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001279 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 default:
1282 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001283 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001286 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 }
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001290}
1291
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001292static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001293{
1294 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001295 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001296 uint32_t start = qdict_get_int(qdict, "start");
1297 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001298
1299 sum = 0;
1300 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001301 uint8_t val = address_space_ldub(&address_space_memory, addr,
1302 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001303 /* BSD sum algorithm ('sum' Unix command) */
1304 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001305 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001306 }
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001308}
1309
bellard13224a82006-07-14 22:03:35 +00001310static int mouse_button_state;
1311
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001312static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001313{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001314 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001315 const char *dx_str = qdict_get_str(qdict, "dx_str");
1316 const char *dy_str = qdict_get_str(qdict, "dy_str");
1317 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001318
bellard13224a82006-07-14 22:03:35 +00001319 dx = strtol(dx_str, NULL, 0);
1320 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001321 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1322 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1323
1324 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001325 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001326 if (dz != 0) {
1327 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1328 qemu_input_queue_btn(NULL, button, true);
1329 qemu_input_event_sync();
1330 qemu_input_queue_btn(NULL, button, false);
1331 }
1332 }
1333 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001334}
1335
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001336static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001337{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001338 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1339 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1340 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1341 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1342 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001343 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001344
1345 if (mouse_button_state == button_state) {
1346 return;
1347 }
1348 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1349 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001350 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001351}
1352
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001353static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001354{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001355 int size = qdict_get_int(qdict, "size");
1356 int addr = qdict_get_int(qdict, "addr");
1357 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001358 uint32_t val;
1359 int suffix;
1360
1361 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001362 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001363 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001364 addr++;
1365 }
1366 addr &= 0xffff;
1367
1368 switch(size) {
1369 default:
1370 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001371 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001372 suffix = 'b';
1373 break;
1374 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001375 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001376 suffix = 'w';
1377 break;
1378 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001379 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001380 suffix = 'l';
1381 break;
1382 }
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1384 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001385}
bellarda3a91a32004-06-04 11:06:21 +00001386
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001387static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001388{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001389 int size = qdict_get_int(qdict, "size");
1390 int addr = qdict_get_int(qdict, "addr");
1391 int val = qdict_get_int(qdict, "val");
1392
Jan Kiszkaf1147842009-07-14 10:20:11 +02001393 addr &= IOPORTS_MASK;
1394
1395 switch (size) {
1396 default:
1397 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001398 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001399 break;
1400 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001401 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001402 break;
1403 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001404 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001405 break;
1406 }
1407}
1408
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001409static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001410{
Gongleif1839932014-12-03 18:20:58 +00001411 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001412 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001413
Gongleif1839932014-12-03 18:20:58 +00001414 qemu_boot_set(bootdevice, &local_err);
1415 if (local_err) {
1416 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1417 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001418 } else {
Gongleif1839932014-12-03 18:20:58 +00001419 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001420 }
1421}
1422
bellardb86bda52004-09-18 19:32:46 +00001423#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001424static void print_pte(Monitor *mon, hwaddr addr,
1425 hwaddr pte,
1426 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001427{
Blue Swirld65aaf32010-12-11 18:56:24 +00001428#ifdef TARGET_X86_64
1429 if (addr & (1ULL << 47)) {
1430 addr |= -1LL << 48;
1431 }
1432#endif
1433 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1434 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001435 addr,
1436 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001437 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001438 pte & PG_GLOBAL_MASK ? 'G' : '-',
1439 pte & PG_PSE_MASK ? 'P' : '-',
1440 pte & PG_DIRTY_MASK ? 'D' : '-',
1441 pte & PG_ACCESSED_MASK ? 'A' : '-',
1442 pte & PG_PCD_MASK ? 'C' : '-',
1443 pte & PG_PWT_MASK ? 'T' : '-',
1444 pte & PG_USER_MASK ? 'U' : '-',
1445 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001446}
1447
Andreas Färber9349b4f2012-03-14 01:38:32 +01001448static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001449{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001450 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001451 uint32_t pgd, pde, pte;
1452
bellardb86bda52004-09-18 19:32:46 +00001453 pgd = env->cr[3] & ~0xfff;
1454 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001455 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001456 pde = le32_to_cpu(pde);
1457 if (pde & PG_PRESENT_MASK) {
1458 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001459 /* 4M pages */
1460 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001461 } else {
1462 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001463 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001464 pte = le32_to_cpu(pte);
1465 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001466 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001467 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001468 ~0xfff);
1469 }
1470 }
1471 }
1472 }
1473 }
1474}
1475
Andreas Färber9349b4f2012-03-14 01:38:32 +01001476static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001477{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001478 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001479 uint64_t pdpe, pde, pte;
1480 uint64_t pdp_addr, pd_addr, pt_addr;
1481
1482 pdp_addr = env->cr[3] & ~0x1f;
1483 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001484 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001485 pdpe = le64_to_cpu(pdpe);
1486 if (pdpe & PG_PRESENT_MASK) {
1487 pd_addr = pdpe & 0x3fffffffff000ULL;
1488 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001489 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001490 pde = le64_to_cpu(pde);
1491 if (pde & PG_PRESENT_MASK) {
1492 if (pde & PG_PSE_MASK) {
1493 /* 2M pages with PAE, CR4.PSE is ignored */
1494 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001495 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001496 } else {
1497 pt_addr = pde & 0x3fffffffff000ULL;
1498 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001499 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001500 pte = le64_to_cpu(pte);
1501 if (pte & PG_PRESENT_MASK) {
1502 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1503 + (l3 << 12),
1504 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001505 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001506 }
1507 }
1508 }
1509 }
1510 }
1511 }
1512 }
1513}
1514
1515#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001516static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001517{
1518 uint64_t l1, l2, l3, l4;
1519 uint64_t pml4e, pdpe, pde, pte;
1520 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1521
1522 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1523 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001524 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001525 pml4e = le64_to_cpu(pml4e);
1526 if (pml4e & PG_PRESENT_MASK) {
1527 pdp_addr = pml4e & 0x3fffffffff000ULL;
1528 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001529 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001530 pdpe = le64_to_cpu(pdpe);
1531 if (pdpe & PG_PRESENT_MASK) {
1532 if (pdpe & PG_PSE_MASK) {
1533 /* 1G pages, CR4.PSE is ignored */
1534 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1535 0x3ffffc0000000ULL);
1536 } else {
1537 pd_addr = pdpe & 0x3fffffffff000ULL;
1538 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001539 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001540 pde = le64_to_cpu(pde);
1541 if (pde & PG_PRESENT_MASK) {
1542 if (pde & PG_PSE_MASK) {
1543 /* 2M pages, CR4.PSE is ignored */
1544 print_pte(mon, (l1 << 39) + (l2 << 30) +
1545 (l3 << 21), pde,
1546 0x3ffffffe00000ULL);
1547 } else {
1548 pt_addr = pde & 0x3fffffffff000ULL;
1549 for (l4 = 0; l4 < 512; l4++) {
1550 cpu_physical_memory_read(pt_addr
1551 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001552 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001553 pte = le64_to_cpu(pte);
1554 if (pte & PG_PRESENT_MASK) {
1555 print_pte(mon, (l1 << 39) +
1556 (l2 << 30) +
1557 (l3 << 21) + (l4 << 12),
1558 pte & ~PG_PSE_MASK,
1559 0x3fffffffff000ULL);
1560 }
1561 }
1562 }
1563 }
1564 }
1565 }
1566 }
1567 }
1568 }
1569 }
1570}
1571#endif
1572
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001573static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001574{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001575 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001576
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001577 env = mon_get_cpu_env();
Blue Swirld65aaf32010-12-11 18:56:24 +00001578
1579 if (!(env->cr[0] & CR0_PG_MASK)) {
1580 monitor_printf(mon, "PG disabled\n");
1581 return;
1582 }
1583 if (env->cr[4] & CR4_PAE_MASK) {
1584#ifdef TARGET_X86_64
1585 if (env->hflags & HF_LMA_MASK) {
1586 tlb_info_64(mon, env);
1587 } else
1588#endif
1589 {
1590 tlb_info_pae32(mon, env);
1591 }
1592 } else {
1593 tlb_info_32(mon, env);
1594 }
1595}
1596
Avi Kivitya8170e52012-10-23 12:30:10 +02001597static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001598 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001599 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001600{
bellard9746b152004-11-11 18:30:24 +00001601 int prot1;
1602 prot1 = *plast_prot;
1603 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001604 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001605 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1606 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001607 *pstart, end, end - *pstart,
1608 prot1 & PG_USER_MASK ? 'u' : '-',
1609 'r',
1610 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001611 }
1612 if (prot != 0)
1613 *pstart = end;
1614 else
1615 *pstart = -1;
1616 *plast_prot = prot;
1617 }
1618}
1619
Andreas Färber9349b4f2012-03-14 01:38:32 +01001620static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001621{
Austin Clementsb49ca722011-08-14 23:19:21 -04001622 unsigned int l1, l2;
1623 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001624 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001625 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001626
bellardb86bda52004-09-18 19:32:46 +00001627 pgd = env->cr[3] & ~0xfff;
1628 last_prot = 0;
1629 start = -1;
1630 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001631 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001632 pde = le32_to_cpu(pde);
1633 end = l1 << 22;
1634 if (pde & PG_PRESENT_MASK) {
1635 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1636 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001637 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001638 } else {
1639 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001640 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001641 pte = le32_to_cpu(pte);
1642 end = (l1 << 22) + (l2 << 12);
1643 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001644 prot = pte & pde &
1645 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001646 } else {
1647 prot = 0;
1648 }
aliguori376253e2009-03-05 23:01:23 +00001649 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001650 }
1651 }
1652 } else {
1653 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001654 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001655 }
1656 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001657 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001658 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001659}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001660
Andreas Färber9349b4f2012-03-14 01:38:32 +01001661static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001662{
Austin Clementsb49ca722011-08-14 23:19:21 -04001663 unsigned int l1, l2, l3;
1664 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001665 uint64_t pdpe, pde, pte;
1666 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001667 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001668
1669 pdp_addr = env->cr[3] & ~0x1f;
1670 last_prot = 0;
1671 start = -1;
1672 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001673 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001674 pdpe = le64_to_cpu(pdpe);
1675 end = l1 << 30;
1676 if (pdpe & PG_PRESENT_MASK) {
1677 pd_addr = pdpe & 0x3fffffffff000ULL;
1678 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001679 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001680 pde = le64_to_cpu(pde);
1681 end = (l1 << 30) + (l2 << 21);
1682 if (pde & PG_PRESENT_MASK) {
1683 if (pde & PG_PSE_MASK) {
1684 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1685 PG_PRESENT_MASK);
1686 mem_print(mon, &start, &last_prot, end, prot);
1687 } else {
1688 pt_addr = pde & 0x3fffffffff000ULL;
1689 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001690 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001691 pte = le64_to_cpu(pte);
1692 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1693 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001694 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1695 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001696 } else {
1697 prot = 0;
1698 }
1699 mem_print(mon, &start, &last_prot, end, prot);
1700 }
1701 }
1702 } else {
1703 prot = 0;
1704 mem_print(mon, &start, &last_prot, end, prot);
1705 }
1706 }
1707 } else {
1708 prot = 0;
1709 mem_print(mon, &start, &last_prot, end, prot);
1710 }
1711 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001712 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001713 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001714}
1715
1716
1717#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001718static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001719{
1720 int prot, last_prot;
1721 uint64_t l1, l2, l3, l4;
1722 uint64_t pml4e, pdpe, pde, pte;
1723 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1724
1725 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1726 last_prot = 0;
1727 start = -1;
1728 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001729 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001730 pml4e = le64_to_cpu(pml4e);
1731 end = l1 << 39;
1732 if (pml4e & PG_PRESENT_MASK) {
1733 pdp_addr = pml4e & 0x3fffffffff000ULL;
1734 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001735 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001736 pdpe = le64_to_cpu(pdpe);
1737 end = (l1 << 39) + (l2 << 30);
1738 if (pdpe & PG_PRESENT_MASK) {
1739 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001740 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1741 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001742 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001743 mem_print(mon, &start, &last_prot, end, prot);
1744 } else {
1745 pd_addr = pdpe & 0x3fffffffff000ULL;
1746 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001747 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001748 pde = le64_to_cpu(pde);
1749 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1750 if (pde & PG_PRESENT_MASK) {
1751 if (pde & PG_PSE_MASK) {
1752 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1753 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001754 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001755 mem_print(mon, &start, &last_prot, end, prot);
1756 } else {
1757 pt_addr = pde & 0x3fffffffff000ULL;
1758 for (l4 = 0; l4 < 512; l4++) {
1759 cpu_physical_memory_read(pt_addr
1760 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001761 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001762 pte = le64_to_cpu(pte);
1763 end = (l1 << 39) + (l2 << 30) +
1764 (l3 << 21) + (l4 << 12);
1765 if (pte & PG_PRESENT_MASK) {
1766 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1767 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001768 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001769 } else {
1770 prot = 0;
1771 }
1772 mem_print(mon, &start, &last_prot, end, prot);
1773 }
1774 }
1775 } else {
1776 prot = 0;
1777 mem_print(mon, &start, &last_prot, end, prot);
1778 }
1779 }
1780 }
1781 } else {
1782 prot = 0;
1783 mem_print(mon, &start, &last_prot, end, prot);
1784 }
1785 }
1786 } else {
1787 prot = 0;
1788 mem_print(mon, &start, &last_prot, end, prot);
1789 }
1790 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001791 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001792 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001793}
1794#endif
1795
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001796static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001797{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001798 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001799
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001800 env = mon_get_cpu_env();
Blue Swirl1b3cba62010-12-11 18:56:27 +00001801
1802 if (!(env->cr[0] & CR0_PG_MASK)) {
1803 monitor_printf(mon, "PG disabled\n");
1804 return;
1805 }
1806 if (env->cr[4] & CR4_PAE_MASK) {
1807#ifdef TARGET_X86_64
1808 if (env->hflags & HF_LMA_MASK) {
1809 mem_info_64(mon, env);
1810 } else
1811#endif
1812 {
1813 mem_info_pae32(mon, env);
1814 }
1815 } else {
1816 mem_info_32(mon, env);
1817 }
1818}
bellardb86bda52004-09-18 19:32:46 +00001819#endif
1820
aurel327c664e22009-03-03 06:12:22 +00001821#if defined(TARGET_SH4)
1822
aliguori376253e2009-03-05 23:01:23 +00001823static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001824{
aliguori376253e2009-03-05 23:01:23 +00001825 monitor_printf(mon, " tlb%i:\t"
1826 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1827 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1828 "dirty=%hhu writethrough=%hhu\n",
1829 idx,
1830 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1831 tlb->v, tlb->sh, tlb->c, tlb->pr,
1832 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001833}
1834
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001835static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001836{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001837 CPUArchState *env = mon_get_cpu_env();
aurel327c664e22009-03-03 06:12:22 +00001838 int i;
1839
aliguori376253e2009-03-05 23:01:23 +00001840 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001841 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001842 print_tlb (mon, i, &env->itlb[i]);
1843 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001844 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001845 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001846}
1847
1848#endif
1849
Max Filippov692f7372012-01-07 20:02:40 +04001850#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001851static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001852{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001853 CPUArchState *env1 = mon_get_cpu_env();
Blue Swirld41160a2010-12-19 13:42:56 +00001854
1855 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1856}
1857#endif
1858
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001859static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001860{
1861 mtree_info((fprintf_function)monitor_printf, mon);
1862}
1863
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001864static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001865{
aliguorib28b6232009-04-22 20:20:29 +00001866 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001867 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001868 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001869
zhanghailiang5b009e42014-11-04 19:49:30 +08001870 node_mem = g_new0(uint64_t, nb_numa_nodes);
1871 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001872 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1873 for (i = 0; i < nb_numa_nodes; i++) {
1874 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001875 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001876 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001877 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001878 }
1879 }
1880 monitor_printf(mon, "\n");
1881 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001882 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001883 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001884 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001885}
1886
bellard5f1ce942006-02-08 22:40:15 +00001887#ifdef CONFIG_PROFILER
1888
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001889int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001890int64_t dev_time;
1891
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001892static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001893{
aliguori376253e2009-03-05 23:01:23 +00001894 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001895 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001896 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001897 tcg_time, tcg_time / (double)get_ticks_per_sec());
1898 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001899 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001900}
1901#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001902static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001903{
aliguori376253e2009-03-05 23:01:23 +00001904 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001905}
1906#endif
1907
bellardec36b692006-07-16 18:57:03 +00001908/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001909static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001910
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001911static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001912{
1913 int i;
1914 CaptureState *s;
1915
1916 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001917 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001918 s->ops.info (s->opaque);
1919 }
1920}
1921
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001922static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001923{
1924 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001925 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001926 CaptureState *s;
1927
1928 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1929 if (i == n) {
1930 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001931 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001932 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001933 return;
1934 }
1935 }
1936}
1937
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001938static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001939{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001940 const char *path = qdict_get_str(qdict, "path");
1941 int has_freq = qdict_haskey(qdict, "freq");
1942 int freq = qdict_get_try_int(qdict, "freq", -1);
1943 int has_bits = qdict_haskey(qdict, "bits");
1944 int bits = qdict_get_try_int(qdict, "bits", -1);
1945 int has_channels = qdict_haskey(qdict, "nchannels");
1946 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001947 CaptureState *s;
1948
Anthony Liguori7267c092011-08-20 22:09:37 -05001949 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001950
1951 freq = has_freq ? freq : 44100;
1952 bits = has_bits ? bits : 16;
1953 nchannels = has_channels ? nchannels : 2;
1954
1955 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001956 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001957 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001958 return;
bellardec36b692006-07-16 18:57:03 +00001959 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001960 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001961}
bellardec36b692006-07-16 18:57:03 +00001962
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001963static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001964{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001965 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001966
aliguori76655d62009-03-06 20:27:37 +00001967 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001968 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001969 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001970 return acl;
1971}
aliguori76655d62009-03-06 20:27:37 +00001972
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001973static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001974{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001975 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001976 qemu_acl *acl = find_acl(mon, aclname);
1977 qemu_acl_entry *entry;
1978 int i = 0;
1979
1980 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001981 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001982 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001983 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001984 i++;
1985 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001986 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001987 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001988 }
1989}
1990
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001991static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001992{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001993 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001994 qemu_acl *acl = find_acl(mon, aclname);
1995
1996 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001997 qemu_acl_reset(acl);
1998 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001999 }
2000}
aliguori76655d62009-03-06 20:27:37 +00002001
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002002static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002003{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002004 const char *aclname = qdict_get_str(qdict, "aclname");
2005 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002006 qemu_acl *acl = find_acl(mon, aclname);
2007
2008 if (acl) {
2009 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002010 acl->defaultDeny = 0;
2011 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002012 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002013 acl->defaultDeny = 1;
2014 monitor_printf(mon, "acl: policy set to 'deny'\n");
2015 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002016 monitor_printf(mon, "acl: unknown policy '%s', "
2017 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002018 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002019 }
2020}
aliguori76655d62009-03-06 20:27:37 +00002021
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002022static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002023{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002024 const char *aclname = qdict_get_str(qdict, "aclname");
2025 const char *match = qdict_get_str(qdict, "match");
2026 const char *policy = qdict_get_str(qdict, "policy");
2027 int has_index = qdict_haskey(qdict, "index");
2028 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002029 qemu_acl *acl = find_acl(mon, aclname);
2030 int deny, ret;
2031
2032 if (acl) {
2033 if (strcmp(policy, "allow") == 0) {
2034 deny = 0;
2035 } else if (strcmp(policy, "deny") == 0) {
2036 deny = 1;
2037 } else {
2038 monitor_printf(mon, "acl: unknown policy '%s', "
2039 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002040 return;
2041 }
aliguori28a76be2009-03-06 20:27:40 +00002042 if (has_index)
2043 ret = qemu_acl_insert(acl, deny, match, index);
2044 else
2045 ret = qemu_acl_append(acl, deny, match);
2046 if (ret < 0)
2047 monitor_printf(mon, "acl: unable to add acl entry\n");
2048 else
2049 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002050 }
2051}
aliguori76655d62009-03-06 20:27:37 +00002052
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002053static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002054{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002055 const char *aclname = qdict_get_str(qdict, "aclname");
2056 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002057 qemu_acl *acl = find_acl(mon, aclname);
2058 int ret;
aliguori76655d62009-03-06 20:27:37 +00002059
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002060 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002061 ret = qemu_acl_remove(acl, match);
2062 if (ret < 0)
2063 monitor_printf(mon, "acl: no matching acl entry\n");
2064 else
2065 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002066 }
2067}
2068
Huang Ying79c4f6b2009-06-23 10:05:14 +08002069#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002070static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002071{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002072 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002073 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002074 int cpu_index = qdict_get_int(qdict, "cpu_index");
2075 int bank = qdict_get_int(qdict, "bank");
2076 uint64_t status = qdict_get_int(qdict, "status");
2077 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2078 uint64_t addr = qdict_get_int(qdict, "addr");
2079 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002080 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002081
Eric Blake34acbc92015-05-15 16:25:00 -06002082 if (qdict_get_try_bool(qdict, "broadcast", false)) {
Jan Kiszka747461c2011-03-02 08:56:10 +01002083 flags |= MCE_INJECT_BROADCAST;
2084 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002085 cs = qemu_get_cpu(cpu_index);
2086 if (cs != NULL) {
2087 cpu = X86_CPU(cs);
2088 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2089 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002090 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002091}
2092#endif
2093
Corey Bryant208c9d12012-06-22 14:36:09 -04002094void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002095{
Anthony Liguoric227f092009-10-01 16:12:16 -05002096 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002097 int fd;
2098
Corey Bryant208c9d12012-06-22 14:36:09 -04002099 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002100 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002101 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002102 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002103 }
2104
2105 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002106 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002107 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2108 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002109 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002110 }
2111
Corey Bryant208c9d12012-06-22 14:36:09 -04002112 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002113 if (strcmp(monfd->name, fdname) != 0) {
2114 continue;
2115 }
2116
2117 close(monfd->fd);
2118 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002119 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002120 }
2121
Anthony Liguori7267c092011-08-20 22:09:37 -05002122 monfd = g_malloc0(sizeof(mon_fd_t));
2123 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002124 monfd->fd = fd;
2125
Corey Bryant208c9d12012-06-22 14:36:09 -04002126 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002127}
2128
Corey Bryant208c9d12012-06-22 14:36:09 -04002129void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002130{
Anthony Liguoric227f092009-10-01 16:12:16 -05002131 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002132
Corey Bryant208c9d12012-06-22 14:36:09 -04002133 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002134 if (strcmp(monfd->name, fdname) != 0) {
2135 continue;
2136 }
2137
Blue Swirl72cf2d42009-09-12 07:36:22 +00002138 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002139 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002140 g_free(monfd->name);
2141 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002142 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143 }
2144
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002145 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002146}
2147
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002148static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002149{
Luiz Capitulino13548692011-07-29 15:36:43 -03002150 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002151 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002152
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002153 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002154
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002155 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002156 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002157 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002158}
2159
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002160int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002161{
Anthony Liguoric227f092009-10-01 16:12:16 -05002162 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002163
Blue Swirl72cf2d42009-09-12 07:36:22 +00002164 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002165 int fd;
2166
2167 if (strcmp(monfd->name, fdname) != 0) {
2168 continue;
2169 }
2170
2171 fd = monfd->fd;
2172
2173 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002174 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002175 g_free(monfd->name);
2176 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002177
2178 return fd;
2179 }
2180
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002181 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002182 return -1;
2183}
2184
Corey Bryantba1c0482012-08-14 16:43:43 -04002185static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2186{
2187 MonFdsetFd *mon_fdset_fd;
2188 MonFdsetFd *mon_fdset_fd_next;
2189
2190 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002191 if ((mon_fdset_fd->removed ||
2192 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2193 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002194 close(mon_fdset_fd->fd);
2195 g_free(mon_fdset_fd->opaque);
2196 QLIST_REMOVE(mon_fdset_fd, next);
2197 g_free(mon_fdset_fd);
2198 }
2199 }
2200
Corey Bryantadb696f2012-08-14 16:43:47 -04002201 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002202 QLIST_REMOVE(mon_fdset, next);
2203 g_free(mon_fdset);
2204 }
2205}
2206
Corey Bryantefb87c12012-08-14 16:43:48 -04002207static void monitor_fdsets_cleanup(void)
2208{
2209 MonFdset *mon_fdset;
2210 MonFdset *mon_fdset_next;
2211
2212 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2213 monitor_fdset_cleanup(mon_fdset);
2214 }
2215}
2216
Corey Bryantba1c0482012-08-14 16:43:43 -04002217AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2218 const char *opaque, Error **errp)
2219{
2220 int fd;
2221 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002222 AddfdInfo *fdinfo;
2223
2224 fd = qemu_chr_fe_get_msgfd(mon->chr);
2225 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002226 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002227 goto error;
2228 }
2229
Corey Bryante446f702012-10-18 15:19:32 -04002230 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2231 has_opaque, opaque, errp);
2232 if (fdinfo) {
2233 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002234 }
2235
Corey Bryantba1c0482012-08-14 16:43:43 -04002236error:
2237 if (fd != -1) {
2238 close(fd);
2239 }
2240 return NULL;
2241}
2242
2243void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2244{
2245 MonFdset *mon_fdset;
2246 MonFdsetFd *mon_fdset_fd;
2247 char fd_str[60];
2248
2249 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2250 if (mon_fdset->id != fdset_id) {
2251 continue;
2252 }
2253 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2254 if (has_fd) {
2255 if (mon_fdset_fd->fd != fd) {
2256 continue;
2257 }
2258 mon_fdset_fd->removed = true;
2259 break;
2260 } else {
2261 mon_fdset_fd->removed = true;
2262 }
2263 }
2264 if (has_fd && !mon_fdset_fd) {
2265 goto error;
2266 }
2267 monitor_fdset_cleanup(mon_fdset);
2268 return;
2269 }
2270
2271error:
2272 if (has_fd) {
2273 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2274 fdset_id, fd);
2275 } else {
2276 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2277 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002278 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002279}
2280
2281FdsetInfoList *qmp_query_fdsets(Error **errp)
2282{
2283 MonFdset *mon_fdset;
2284 MonFdsetFd *mon_fdset_fd;
2285 FdsetInfoList *fdset_list = NULL;
2286
2287 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2288 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2289 FdsetFdInfoList *fdsetfd_list = NULL;
2290
2291 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2292 fdset_info->value->fdset_id = mon_fdset->id;
2293
2294 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2295 FdsetFdInfoList *fdsetfd_info;
2296
2297 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2298 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2299 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2300 if (mon_fdset_fd->opaque) {
2301 fdsetfd_info->value->has_opaque = true;
2302 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2303 } else {
2304 fdsetfd_info->value->has_opaque = false;
2305 }
2306
2307 fdsetfd_info->next = fdsetfd_list;
2308 fdsetfd_list = fdsetfd_info;
2309 }
2310
2311 fdset_info->value->fds = fdsetfd_list;
2312
2313 fdset_info->next = fdset_list;
2314 fdset_list = fdset_info;
2315 }
2316
2317 return fdset_list;
2318}
2319
Corey Bryante446f702012-10-18 15:19:32 -04002320AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2321 bool has_opaque, const char *opaque,
2322 Error **errp)
2323{
2324 MonFdset *mon_fdset = NULL;
2325 MonFdsetFd *mon_fdset_fd;
2326 AddfdInfo *fdinfo;
2327
2328 if (has_fdset_id) {
2329 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2330 /* Break if match found or match impossible due to ordering by ID */
2331 if (fdset_id <= mon_fdset->id) {
2332 if (fdset_id < mon_fdset->id) {
2333 mon_fdset = NULL;
2334 }
2335 break;
2336 }
2337 }
2338 }
2339
2340 if (mon_fdset == NULL) {
2341 int64_t fdset_id_prev = -1;
2342 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2343
2344 if (has_fdset_id) {
2345 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002346 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2347 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002348 return NULL;
2349 }
2350 /* Use specified fdset ID */
2351 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2352 mon_fdset_cur = mon_fdset;
2353 if (fdset_id < mon_fdset_cur->id) {
2354 break;
2355 }
2356 }
2357 } else {
2358 /* Use first available fdset ID */
2359 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2360 mon_fdset_cur = mon_fdset;
2361 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2362 fdset_id_prev = mon_fdset_cur->id;
2363 continue;
2364 }
2365 break;
2366 }
2367 }
2368
2369 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2370 if (has_fdset_id) {
2371 mon_fdset->id = fdset_id;
2372 } else {
2373 mon_fdset->id = fdset_id_prev + 1;
2374 }
2375
2376 /* The fdset list is ordered by fdset ID */
2377 if (!mon_fdset_cur) {
2378 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2379 } else if (mon_fdset->id < mon_fdset_cur->id) {
2380 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2381 } else {
2382 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2383 }
2384 }
2385
2386 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2387 mon_fdset_fd->fd = fd;
2388 mon_fdset_fd->removed = false;
2389 if (has_opaque) {
2390 mon_fdset_fd->opaque = g_strdup(opaque);
2391 }
2392 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2393
2394 fdinfo = g_malloc0(sizeof(*fdinfo));
2395 fdinfo->fdset_id = mon_fdset->id;
2396 fdinfo->fd = mon_fdset_fd->fd;
2397
2398 return fdinfo;
2399}
2400
Corey Bryantadb696f2012-08-14 16:43:47 -04002401int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2402{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002403#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002404 MonFdset *mon_fdset;
2405 MonFdsetFd *mon_fdset_fd;
2406 int mon_fd_flags;
2407
Corey Bryantadb696f2012-08-14 16:43:47 -04002408 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2409 if (mon_fdset->id != fdset_id) {
2410 continue;
2411 }
2412 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2413 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2414 if (mon_fd_flags == -1) {
2415 return -1;
2416 }
2417
2418 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2419 return mon_fdset_fd->fd;
2420 }
2421 }
2422 errno = EACCES;
2423 return -1;
2424 }
2425#endif
2426
2427 errno = ENOENT;
2428 return -1;
2429}
2430
2431int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2432{
2433 MonFdset *mon_fdset;
2434 MonFdsetFd *mon_fdset_fd_dup;
2435
2436 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2437 if (mon_fdset->id != fdset_id) {
2438 continue;
2439 }
2440 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2441 if (mon_fdset_fd_dup->fd == dup_fd) {
2442 return -1;
2443 }
2444 }
2445 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2446 mon_fdset_fd_dup->fd = dup_fd;
2447 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2448 return 0;
2449 }
2450 return -1;
2451}
2452
2453static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2454{
2455 MonFdset *mon_fdset;
2456 MonFdsetFd *mon_fdset_fd_dup;
2457
2458 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2459 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2460 if (mon_fdset_fd_dup->fd == dup_fd) {
2461 if (remove) {
2462 QLIST_REMOVE(mon_fdset_fd_dup, next);
2463 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2464 monitor_fdset_cleanup(mon_fdset);
2465 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002466 return -1;
2467 } else {
2468 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002469 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002470 }
2471 }
2472 }
2473 return -1;
2474}
2475
2476int monitor_fdset_dup_fd_find(int dup_fd)
2477{
2478 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2479}
2480
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002481void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002482{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002483 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002484}
2485
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002486int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002487{
2488 int fd;
2489 Error *local_err = NULL;
2490
2491 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002492 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002493 } else {
2494 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002495 if (fd == -1) {
2496 error_setg(&local_err, "Invalid file descriptor number '%s'",
2497 fdname);
2498 }
2499 }
2500 if (local_err) {
2501 error_propagate(errp, local_err);
2502 assert(fd == -1);
2503 } else {
2504 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002505 }
2506
2507 return fd;
2508}
2509
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002510/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002511static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 {
2513 .name = "version",
2514 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002515 .params = "",
2516 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002517 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002518 },
2519 {
2520 .name = "network",
2521 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002522 .params = "",
2523 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002524 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 },
2526 {
2527 .name = "chardev",
2528 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 .params = "",
2530 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002531 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002535 .args_type = "nodes:-n,verbose:-v,device:B?",
2536 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002537 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002538 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002539 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "blockstats",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002546 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002549 .name = "block-jobs",
2550 .args_type = "",
2551 .params = "",
2552 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002553 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002554 },
2555 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 .name = "registers",
2557 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .params = "",
2559 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002560 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
2562 {
2563 .name = "cpus",
2564 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .params = "",
2566 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002567 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 },
2569 {
2570 .name = "history",
2571 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 .params = "",
2573 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002574 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002576#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2577 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 {
2579 .name = "irq",
2580 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002581 .params = "",
2582 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002583#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002584 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002585#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002586 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002587#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002588 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002589#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 },
2591 {
2592 .name = "pic",
2593 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 .params = "",
2595 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002596#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002597 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002598#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002599 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002600#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002601 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002602#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002604#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 {
2606 .name = "pci",
2607 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002608 .params = "",
2609 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002610 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002611 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002612#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002613 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 {
2615 .name = "tlb",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002619 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002620 },
aurel327c664e22009-03-03 06:12:22 +00002621#endif
2622#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 {
2624 .name = "mem",
2625 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 .params = "",
2627 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002628 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 },
bellardb86bda52004-09-18 19:32:46 +00002630#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 {
Blue Swirl314e2982011-09-11 20:22:05 +00002632 .name = "mtree",
2633 .args_type = "",
2634 .params = "",
2635 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002636 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002637 },
2638 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 .name = "jit",
2640 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 .params = "",
2642 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002643 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 },
2645 {
Max Filippov246ae242014-11-02 11:04:18 +03002646 .name = "opcount",
2647 .args_type = "",
2648 .params = "",
2649 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002650 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002651 },
2652 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 .name = "kvm",
2654 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 .params = "",
2656 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002657 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 },
2659 {
2660 .name = "numa",
2661 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 .params = "",
2663 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002664 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 },
2666 {
2667 .name = "usb",
2668 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 .params = "",
2670 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002671 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002672 },
2673 {
2674 .name = "usbhost",
2675 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 .params = "",
2677 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002678 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 },
2680 {
2681 .name = "profile",
2682 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 .params = "",
2684 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002685 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002686 },
2687 {
2688 .name = "capture",
2689 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 .params = "",
2691 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002692 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 },
2694 {
2695 .name = "snapshots",
2696 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 .params = "",
2698 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002699 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 },
2701 {
2702 .name = "status",
2703 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 .params = "",
2705 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002706 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 },
2708 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .name = "mice",
2710 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 .params = "",
2712 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002713 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 },
2715 {
2716 .name = "vnc",
2717 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 .params = "",
2719 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002720 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002722#if defined(CONFIG_SPICE)
2723 {
2724 .name = "spice",
2725 .args_type = "",
2726 .params = "",
2727 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002728 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002729 },
2730#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 {
2732 .name = "name",
2733 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 .params = "",
2735 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002736 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002737 },
2738 {
2739 .name = "uuid",
2740 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 .params = "",
2742 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002743 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 {
2746 .name = "cpustats",
2747 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 .params = "",
2749 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002750 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 },
blueswir131a60e22007-10-26 18:42:59 +00002752#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 {
2754 .name = "usernet",
2755 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 .params = "",
2757 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002758 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002759 },
blueswir131a60e22007-10-26 18:42:59 +00002760#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 {
2762 .name = "migrate",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002766 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 },
2768 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002769 .name = "migrate_capabilities",
2770 .args_type = "",
2771 .params = "",
2772 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002773 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002774 },
2775 {
Liang Li50e9a622015-03-23 16:32:29 +08002776 .name = "migrate_parameters",
2777 .args_type = "",
2778 .params = "",
2779 .help = "show current migration parameters",
2780 .mhandler.cmd = hmp_info_migrate_parameters,
2781 },
2782 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002783 .name = "migrate_cache_size",
2784 .args_type = "",
2785 .params = "",
2786 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002787 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002788 },
2789 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002790 .name = "balloon",
2791 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .params = "",
2793 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002794 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "qtree",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002801 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 },
2803 {
2804 .name = "qdm",
2805 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 .params = "",
2807 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002808 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 },
2810 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002811 .name = "qom-tree",
2812 .args_type = "path:s?",
2813 .params = "[path]",
2814 .help = "show QOM composition tree",
2815 .mhandler.cmd = hmp_info_qom_tree,
2816 },
2817 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 .name = "roms",
2819 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .params = "",
2821 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002822 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302824 {
2825 .name = "trace-events",
2826 .args_type = "",
2827 .params = "",
2828 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002829 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302830 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002831 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002832 .name = "tpm",
2833 .args_type = "",
2834 .params = "",
2835 .help = "show the TPM device",
2836 .mhandler.cmd = hmp_info_tpm,
2837 },
2838 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002839 .name = "memdev",
2840 .args_type = "",
2841 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002842 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002843 .mhandler.cmd = hmp_info_memdev,
2844 },
2845 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002846 .name = "memory-devices",
2847 .args_type = "",
2848 .params = "",
2849 .help = "show memory devices",
2850 .mhandler.cmd = hmp_info_memory_devices,
2851 },
2852 {
Scott Feldmanfafa4d52015-06-10 18:21:21 -07002853 .name = "rocker",
2854 .args_type = "name:s",
2855 .params = "name",
2856 .help = "Show rocker switch",
2857 .mhandler.cmd = hmp_rocker,
2858 },
2859 {
2860 .name = "rocker-ports",
2861 .args_type = "name:s",
2862 .params = "name",
2863 .help = "Show rocker ports",
2864 .mhandler.cmd = hmp_rocker_ports,
2865 },
2866 {
2867 .name = "rocker-of-dpa-flows",
2868 .args_type = "name:s,tbl_id:i?",
2869 .params = "name [tbl_id]",
2870 .help = "Show rocker OF-DPA flow tables",
2871 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2872 },
2873 {
2874 .name = "rocker-of-dpa-groups",
2875 .args_type = "name:s,type:i?",
2876 .params = "name [type]",
2877 .help = "Show rocker OF-DPA groups",
2878 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2879 },
2880 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 .name = NULL,
2882 },
bellard9dc39cb2004-03-14 21:38:27 +00002883};
2884
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002885/* mon_cmds and info_cmds would be sorted at runtime */
2886static mon_cmd_t mon_cmds[] = {
2887#include "hmp-commands.h"
2888 { NULL, NULL, },
2889};
2890
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002891static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002892#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002893 { /* NULL */ },
2894};
2895
bellard9307c4c2004-04-04 12:57:25 +00002896/*******************************************************************/
2897
2898static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002899static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002900
bellard92a31b12005-02-10 22:00:52 +00002901#define MD_TLONG 0
2902#define MD_I32 1
2903
bellard9307c4c2004-04-04 12:57:25 +00002904typedef struct MonitorDef {
2905 const char *name;
2906 int offset;
blueswir18662d652008-10-02 18:32:44 +00002907 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002908 int type;
bellard9307c4c2004-04-04 12:57:25 +00002909} MonitorDef;
2910
bellard57206fd2004-04-25 18:54:52 +00002911#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002912static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002913{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002914 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002915 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002916}
2917#endif
2918
bellarda541f292004-04-12 20:39:29 +00002919#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002920static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002921{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002922 CPUArchState *env = mon_get_cpu_env();
bellarda541f292004-04-12 20:39:29 +00002923 unsigned int u;
2924 int i;
2925
2926 u = 0;
2927 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002928 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002929
2930 return u;
2931}
2932
blueswir18662d652008-10-02 18:32:44 +00002933static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002934{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002935 CPUArchState *env = mon_get_cpu_env();
j_mayer0411a972007-10-25 21:35:50 +00002936 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002937}
2938
blueswir18662d652008-10-02 18:32:44 +00002939static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002940{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002941 CPUArchState *env = mon_get_cpu_env();
aurel323d7b4172008-10-21 11:28:46 +00002942 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002943}
bellard9fddaa02004-05-21 12:59:32 +00002944
blueswir18662d652008-10-02 18:32:44 +00002945static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002946{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002947 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002948 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002949}
2950
blueswir18662d652008-10-02 18:32:44 +00002951static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002952{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002953 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002954 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002955}
2956
blueswir18662d652008-10-02 18:32:44 +00002957static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002958{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002959 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002960 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002961}
bellarda541f292004-04-12 20:39:29 +00002962#endif
2963
bellarde95c8d52004-09-30 22:22:08 +00002964#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002965#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002966static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002967{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002968 CPUArchState *env = mon_get_cpu_env();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002969
2970 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002971}
bellard7b936c02005-10-30 17:05:13 +00002972#endif
bellarde95c8d52004-09-30 22:22:08 +00002973
blueswir18662d652008-10-02 18:32:44 +00002974static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002975{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002976 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002977 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002978}
2979#endif
2980
blueswir18662d652008-10-02 18:32:44 +00002981static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002982#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002983
2984#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002985 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2986 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2987 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002988
Andreas Färbere59d1672012-02-16 00:40:47 +01002989 { "eax", offsetof(CPUX86State, regs[0]) },
2990 { "ecx", offsetof(CPUX86State, regs[1]) },
2991 { "edx", offsetof(CPUX86State, regs[2]) },
2992 { "ebx", offsetof(CPUX86State, regs[3]) },
2993 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2994 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2995 { "esi", offsetof(CPUX86State, regs[6]) },
2996 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002997#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002998 { "r8", offsetof(CPUX86State, regs[8]) },
2999 { "r9", offsetof(CPUX86State, regs[9]) },
3000 { "r10", offsetof(CPUX86State, regs[10]) },
3001 { "r11", offsetof(CPUX86State, regs[11]) },
3002 { "r12", offsetof(CPUX86State, regs[12]) },
3003 { "r13", offsetof(CPUX86State, regs[13]) },
3004 { "r14", offsetof(CPUX86State, regs[14]) },
3005 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003006#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003007 { "eflags", offsetof(CPUX86State, eflags) },
3008 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003009 SEG("cs", R_CS)
3010 SEG("ds", R_DS)
3011 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003012 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003013 SEG("fs", R_FS)
3014 SEG("gs", R_GS)
3015 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003016#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003017 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003018 { "r0", offsetof(CPUPPCState, gpr[0]) },
3019 { "r1", offsetof(CPUPPCState, gpr[1]) },
3020 { "r2", offsetof(CPUPPCState, gpr[2]) },
3021 { "r3", offsetof(CPUPPCState, gpr[3]) },
3022 { "r4", offsetof(CPUPPCState, gpr[4]) },
3023 { "r5", offsetof(CPUPPCState, gpr[5]) },
3024 { "r6", offsetof(CPUPPCState, gpr[6]) },
3025 { "r7", offsetof(CPUPPCState, gpr[7]) },
3026 { "r8", offsetof(CPUPPCState, gpr[8]) },
3027 { "r9", offsetof(CPUPPCState, gpr[9]) },
3028 { "r10", offsetof(CPUPPCState, gpr[10]) },
3029 { "r11", offsetof(CPUPPCState, gpr[11]) },
3030 { "r12", offsetof(CPUPPCState, gpr[12]) },
3031 { "r13", offsetof(CPUPPCState, gpr[13]) },
3032 { "r14", offsetof(CPUPPCState, gpr[14]) },
3033 { "r15", offsetof(CPUPPCState, gpr[15]) },
3034 { "r16", offsetof(CPUPPCState, gpr[16]) },
3035 { "r17", offsetof(CPUPPCState, gpr[17]) },
3036 { "r18", offsetof(CPUPPCState, gpr[18]) },
3037 { "r19", offsetof(CPUPPCState, gpr[19]) },
3038 { "r20", offsetof(CPUPPCState, gpr[20]) },
3039 { "r21", offsetof(CPUPPCState, gpr[21]) },
3040 { "r22", offsetof(CPUPPCState, gpr[22]) },
3041 { "r23", offsetof(CPUPPCState, gpr[23]) },
3042 { "r24", offsetof(CPUPPCState, gpr[24]) },
3043 { "r25", offsetof(CPUPPCState, gpr[25]) },
3044 { "r26", offsetof(CPUPPCState, gpr[26]) },
3045 { "r27", offsetof(CPUPPCState, gpr[27]) },
3046 { "r28", offsetof(CPUPPCState, gpr[28]) },
3047 { "r29", offsetof(CPUPPCState, gpr[29]) },
3048 { "r30", offsetof(CPUPPCState, gpr[30]) },
3049 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003050 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003051 { "f0", offsetof(CPUPPCState, fpr[0]) },
3052 { "f1", offsetof(CPUPPCState, fpr[1]) },
3053 { "f2", offsetof(CPUPPCState, fpr[2]) },
3054 { "f3", offsetof(CPUPPCState, fpr[3]) },
3055 { "f4", offsetof(CPUPPCState, fpr[4]) },
3056 { "f5", offsetof(CPUPPCState, fpr[5]) },
3057 { "f6", offsetof(CPUPPCState, fpr[6]) },
3058 { "f7", offsetof(CPUPPCState, fpr[7]) },
3059 { "f8", offsetof(CPUPPCState, fpr[8]) },
3060 { "f9", offsetof(CPUPPCState, fpr[9]) },
3061 { "f10", offsetof(CPUPPCState, fpr[10]) },
3062 { "f11", offsetof(CPUPPCState, fpr[11]) },
3063 { "f12", offsetof(CPUPPCState, fpr[12]) },
3064 { "f13", offsetof(CPUPPCState, fpr[13]) },
3065 { "f14", offsetof(CPUPPCState, fpr[14]) },
3066 { "f15", offsetof(CPUPPCState, fpr[15]) },
3067 { "f16", offsetof(CPUPPCState, fpr[16]) },
3068 { "f17", offsetof(CPUPPCState, fpr[17]) },
3069 { "f18", offsetof(CPUPPCState, fpr[18]) },
3070 { "f19", offsetof(CPUPPCState, fpr[19]) },
3071 { "f20", offsetof(CPUPPCState, fpr[20]) },
3072 { "f21", offsetof(CPUPPCState, fpr[21]) },
3073 { "f22", offsetof(CPUPPCState, fpr[22]) },
3074 { "f23", offsetof(CPUPPCState, fpr[23]) },
3075 { "f24", offsetof(CPUPPCState, fpr[24]) },
3076 { "f25", offsetof(CPUPPCState, fpr[25]) },
3077 { "f26", offsetof(CPUPPCState, fpr[26]) },
3078 { "f27", offsetof(CPUPPCState, fpr[27]) },
3079 { "f28", offsetof(CPUPPCState, fpr[28]) },
3080 { "f29", offsetof(CPUPPCState, fpr[29]) },
3081 { "f30", offsetof(CPUPPCState, fpr[30]) },
3082 { "f31", offsetof(CPUPPCState, fpr[31]) },
3083 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003084 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003085 { "nip|pc", offsetof(CPUPPCState, nip) },
3086 { "lr", offsetof(CPUPPCState, lr) },
3087 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003088 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003089 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003090 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003091 { "msr", 0, &monitor_get_msr, },
3092 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003093 { "tbu", 0, &monitor_get_tbu, },
3094 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003095 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003096 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3097 { "sr0", offsetof(CPUPPCState, sr[0]) },
3098 { "sr1", offsetof(CPUPPCState, sr[1]) },
3099 { "sr2", offsetof(CPUPPCState, sr[2]) },
3100 { "sr3", offsetof(CPUPPCState, sr[3]) },
3101 { "sr4", offsetof(CPUPPCState, sr[4]) },
3102 { "sr5", offsetof(CPUPPCState, sr[5]) },
3103 { "sr6", offsetof(CPUPPCState, sr[6]) },
3104 { "sr7", offsetof(CPUPPCState, sr[7]) },
3105 { "sr8", offsetof(CPUPPCState, sr[8]) },
3106 { "sr9", offsetof(CPUPPCState, sr[9]) },
3107 { "sr10", offsetof(CPUPPCState, sr[10]) },
3108 { "sr11", offsetof(CPUPPCState, sr[11]) },
3109 { "sr12", offsetof(CPUPPCState, sr[12]) },
3110 { "sr13", offsetof(CPUPPCState, sr[13]) },
3111 { "sr14", offsetof(CPUPPCState, sr[14]) },
3112 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003113 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003114 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003115
Andreas Färbere59d1672012-02-16 00:40:47 +01003116 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3117 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003118 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3119 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3120 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003121 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3122 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3123 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3124 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3125 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3126 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3127 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3128 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3129 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3130 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3131 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3132 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3133 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3134 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3135 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3136 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3137 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3138 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3139 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3140 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3141 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3142 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3143 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3144 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3145 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3146 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3147 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3148 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3149 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3150 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3151 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3152 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3153 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3154 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3155 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3156 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3157 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3158 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3159 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3160 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3161 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3162 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3163 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3164 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3165 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3166 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3167 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3168 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3169 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3170 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3171 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3172 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3173 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3174 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3175 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3176 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3177 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3178 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3179 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3180 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3181 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3182 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3183 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3184 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003185
bellarde95c8d52004-09-30 22:22:08 +00003186#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003187 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3188 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3189 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3190 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3191 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3192 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3193 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3194 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003195 { "o0", 0, monitor_get_reg },
3196 { "o1", 1, monitor_get_reg },
3197 { "o2", 2, monitor_get_reg },
3198 { "o3", 3, monitor_get_reg },
3199 { "o4", 4, monitor_get_reg },
3200 { "o5", 5, monitor_get_reg },
3201 { "o6", 6, monitor_get_reg },
3202 { "o7", 7, monitor_get_reg },
3203 { "l0", 8, monitor_get_reg },
3204 { "l1", 9, monitor_get_reg },
3205 { "l2", 10, monitor_get_reg },
3206 { "l3", 11, monitor_get_reg },
3207 { "l4", 12, monitor_get_reg },
3208 { "l5", 13, monitor_get_reg },
3209 { "l6", 14, monitor_get_reg },
3210 { "l7", 15, monitor_get_reg },
3211 { "i0", 16, monitor_get_reg },
3212 { "i1", 17, monitor_get_reg },
3213 { "i2", 18, monitor_get_reg },
3214 { "i3", 19, monitor_get_reg },
3215 { "i4", 20, monitor_get_reg },
3216 { "i5", 21, monitor_get_reg },
3217 { "i6", 22, monitor_get_reg },
3218 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003219 { "pc", offsetof(CPUSPARCState, pc) },
3220 { "npc", offsetof(CPUSPARCState, npc) },
3221 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003222#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003223 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003224 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003225#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003226 { "tbr", offsetof(CPUSPARCState, tbr) },
3227 { "fsr", offsetof(CPUSPARCState, fsr) },
3228 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3229 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3230 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3231 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3232 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3233 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3234 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3235 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3236 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3237 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3238 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3239 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3240 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3241 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3242 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3243 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3244 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3245 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3246 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3247 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3248 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3249 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3250 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3251 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3252 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3253 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3254 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3255 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3256 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3257 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3258 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3259 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003260#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003261 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3262 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3263 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3264 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3265 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3266 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3267 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3268 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3269 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3270 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3271 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3272 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3273 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3274 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3275 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3276 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3277 { "asi", offsetof(CPUSPARCState, asi) },
3278 { "pstate", offsetof(CPUSPARCState, pstate) },
3279 { "cansave", offsetof(CPUSPARCState, cansave) },
3280 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3281 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3282 { "wstate", offsetof(CPUSPARCState, wstate) },
3283 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3284 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003285#endif
bellard9307c4c2004-04-04 12:57:25 +00003286#endif
3287 { NULL },
3288};
3289
Stefan Weil9c3175c2013-08-22 21:30:09 +02003290static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3291expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003292{
Fam Zheng277acfe2013-08-20 10:58:21 +08003293 va_list ap;
3294 va_start(ap, fmt);
3295 monitor_vprintf(mon, fmt, ap);
3296 monitor_printf(mon, "\n");
3297 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003298 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003299}
3300
Markus Armbruster09b94182010-01-20 13:07:30 +01003301/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003302static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003303{
blueswir18662d652008-10-02 18:32:44 +00003304 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003305 void *ptr;
3306
bellard9307c4c2004-04-04 12:57:25 +00003307 for(md = monitor_defs; md->name != NULL; md++) {
3308 if (compare_cmd(name, md->name)) {
3309 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003310 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003311 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07003312 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00003313 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003314 switch(md->type) {
3315 case MD_I32:
3316 *pval = *(int32_t *)ptr;
3317 break;
3318 case MD_TLONG:
3319 *pval = *(target_long *)ptr;
3320 break;
3321 default:
3322 *pval = 0;
3323 break;
3324 }
bellard9307c4c2004-04-04 12:57:25 +00003325 }
3326 return 0;
3327 }
3328 }
3329 return -1;
3330}
3331
3332static void next(void)
3333{
Blue Swirl660f11b2009-07-31 21:16:51 +00003334 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003335 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003336 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003337 pch++;
3338 }
3339}
3340
aliguori376253e2009-03-05 23:01:23 +00003341static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003342
aliguori376253e2009-03-05 23:01:23 +00003343static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003344{
blueswir1c2efc952007-09-25 17:28:42 +00003345 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003346 char *p;
bellard6a00d602005-11-21 23:25:50 +00003347 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003348
3349 switch(*pch) {
3350 case '+':
3351 next();
aliguori376253e2009-03-05 23:01:23 +00003352 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003353 break;
3354 case '-':
3355 next();
aliguori376253e2009-03-05 23:01:23 +00003356 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003357 break;
3358 case '~':
3359 next();
aliguori376253e2009-03-05 23:01:23 +00003360 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003361 break;
3362 case '(':
3363 next();
aliguori376253e2009-03-05 23:01:23 +00003364 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003365 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003366 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003367 }
3368 next();
3369 break;
bellard81d09122004-07-14 17:21:37 +00003370 case '\'':
3371 pch++;
3372 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003373 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003374 n = *pch;
3375 pch++;
3376 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003377 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003378 next();
3379 break;
bellard9307c4c2004-04-04 12:57:25 +00003380 case '$':
3381 {
3382 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003383 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003384
bellard9307c4c2004-04-04 12:57:25 +00003385 pch++;
3386 q = buf;
3387 while ((*pch >= 'a' && *pch <= 'z') ||
3388 (*pch >= 'A' && *pch <= 'Z') ||
3389 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003390 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003391 if ((q - buf) < sizeof(buf) - 1)
3392 *q++ = *pch;
3393 pch++;
3394 }
blueswir1cd390082008-11-16 13:53:32 +00003395 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003396 pch++;
3397 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003398 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003399 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003400 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003401 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003402 }
3403 break;
3404 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003405 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003406 n = 0;
3407 break;
3408 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003409 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003410 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003411 if (errno == ERANGE) {
3412 expr_error(mon, "number too large");
3413 }
bellard9307c4c2004-04-04 12:57:25 +00003414 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003415 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003416 }
3417 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003418 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003419 pch++;
3420 break;
3421 }
3422 return n;
3423}
3424
3425
aliguori376253e2009-03-05 23:01:23 +00003426static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003427{
blueswir1c2efc952007-09-25 17:28:42 +00003428 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003429 int op;
ths3b46e622007-09-17 08:09:54 +00003430
aliguori376253e2009-03-05 23:01:23 +00003431 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003432 for(;;) {
3433 op = *pch;
3434 if (op != '*' && op != '/' && op != '%')
3435 break;
3436 next();
aliguori376253e2009-03-05 23:01:23 +00003437 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003438 switch(op) {
3439 default:
3440 case '*':
3441 val *= val2;
3442 break;
3443 case '/':
3444 case '%':
ths5fafdf22007-09-16 21:08:06 +00003445 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003446 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003447 if (op == '/')
3448 val /= val2;
3449 else
3450 val %= val2;
3451 break;
3452 }
3453 }
3454 return val;
3455}
3456
aliguori376253e2009-03-05 23:01:23 +00003457static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003458{
blueswir1c2efc952007-09-25 17:28:42 +00003459 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003460 int op;
bellard9307c4c2004-04-04 12:57:25 +00003461
aliguori376253e2009-03-05 23:01:23 +00003462 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003463 for(;;) {
3464 op = *pch;
3465 if (op != '&' && op != '|' && op != '^')
3466 break;
3467 next();
aliguori376253e2009-03-05 23:01:23 +00003468 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003469 switch(op) {
3470 default:
3471 case '&':
3472 val &= val2;
3473 break;
3474 case '|':
3475 val |= val2;
3476 break;
3477 case '^':
3478 val ^= val2;
3479 break;
3480 }
3481 }
3482 return val;
3483}
3484
aliguori376253e2009-03-05 23:01:23 +00003485static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003486{
blueswir1c2efc952007-09-25 17:28:42 +00003487 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003488 int op;
bellard9307c4c2004-04-04 12:57:25 +00003489
aliguori376253e2009-03-05 23:01:23 +00003490 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003491 for(;;) {
3492 op = *pch;
3493 if (op != '+' && op != '-')
3494 break;
3495 next();
aliguori376253e2009-03-05 23:01:23 +00003496 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003497 if (op == '+')
3498 val += val2;
3499 else
3500 val -= val2;
3501 }
3502 return val;
3503}
3504
aliguori376253e2009-03-05 23:01:23 +00003505static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003506{
3507 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003508 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003509 *pp = pch;
3510 return -1;
3511 }
blueswir1cd390082008-11-16 13:53:32 +00003512 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003513 pch++;
aliguori376253e2009-03-05 23:01:23 +00003514 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003515 *pp = pch;
3516 return 0;
3517}
3518
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003519static int get_double(Monitor *mon, double *pval, const char **pp)
3520{
3521 const char *p = *pp;
3522 char *tailp;
3523 double d;
3524
3525 d = strtod(p, &tailp);
3526 if (tailp == p) {
3527 monitor_printf(mon, "Number expected\n");
3528 return -1;
3529 }
3530 if (d != d || d - d != 0) {
3531 /* NaN or infinity */
3532 monitor_printf(mon, "Bad number\n");
3533 return -1;
3534 }
3535 *pval = d;
3536 *pp = tailp;
3537 return 0;
3538}
3539
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003540/*
3541 * Store the command-name in cmdname, and return a pointer to
3542 * the remaining of the command string.
3543 */
3544static const char *get_command_name(const char *cmdline,
3545 char *cmdname, size_t nlen)
3546{
3547 size_t len;
3548 const char *p, *pstart;
3549
3550 p = cmdline;
3551 while (qemu_isspace(*p))
3552 p++;
3553 if (*p == '\0')
3554 return NULL;
3555 pstart = p;
3556 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3557 p++;
3558 len = p - pstart;
3559 if (len > nlen - 1)
3560 len = nlen - 1;
3561 memcpy(cmdname, pstart, len);
3562 cmdname[len] = '\0';
3563 return p;
3564}
3565
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003566/**
3567 * Read key of 'type' into 'key' and return the current
3568 * 'type' pointer.
3569 */
3570static char *key_get_info(const char *type, char **key)
3571{
3572 size_t len;
3573 char *p, *str;
3574
3575 if (*type == ',')
3576 type++;
3577
3578 p = strchr(type, ':');
3579 if (!p) {
3580 *key = NULL;
3581 return NULL;
3582 }
3583 len = p - type;
3584
Anthony Liguori7267c092011-08-20 22:09:37 -05003585 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003586 memcpy(str, type, len);
3587 str[len] = '\0';
3588
3589 *key = str;
3590 return ++p;
3591}
3592
bellard9307c4c2004-04-04 12:57:25 +00003593static int default_fmt_format = 'x';
3594static int default_fmt_size = 4;
3595
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003596static int is_valid_option(const char *c, const char *typestr)
3597{
3598 char option[3];
3599
3600 option[0] = '-';
3601 option[1] = *c;
3602 option[2] = '\0';
3603
3604 typestr = strstr(typestr, option);
3605 return (typestr != NULL);
3606}
3607
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003608static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3609 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003610{
3611 const mon_cmd_t *cmd;
3612
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003613 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003614 if (compare_cmd(cmdname, cmd->name)) {
3615 return cmd;
3616 }
3617 }
3618
3619 return NULL;
3620}
3621
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003622static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3623{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003624 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003625}
3626
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003627/*
Bandan Dasae502122015-06-03 18:38:08 -04003628 * Parse command name from @cmdp according to command table @table.
3629 * If blank, return NULL.
3630 * Else, if no valid command can be found, report to @mon, and return
3631 * NULL.
3632 * Else, change @cmdp to point right behind the name, and return its
3633 * command table entry.
3634 * Do not assume the return value points into @table! It doesn't when
3635 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003636 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003637static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04003638 const char **cmdp,
3639 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003640{
Bandan Dasae502122015-06-03 18:38:08 -04003641 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003642 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003643 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003644
bellard9307c4c2004-04-04 12:57:25 +00003645 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003646 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003647 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003648 return NULL;
ths3b46e622007-09-17 08:09:54 +00003649
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003650 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003651 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003652 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04003653 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003654 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003655 }
bellard9307c4c2004-04-04 12:57:25 +00003656
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003657 /* filter out following useless space */
3658 while (qemu_isspace(*p)) {
3659 p++;
3660 }
Bandan Dasae502122015-06-03 18:38:08 -04003661
3662 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003663 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003664 if (cmd->sub_table != NULL && *p != '\0') {
3665 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003666 }
3667
Bandan Dasae502122015-06-03 18:38:08 -04003668 return cmd;
3669}
3670
3671/*
3672 * Parse arguments for @cmd.
3673 * If it can't be parsed, report to @mon, and return NULL.
3674 * Else, insert command arguments into a QDict, and return it.
3675 * Note: On success, caller has to free the QDict structure.
3676 */
3677
3678static QDict *monitor_parse_arguments(Monitor *mon,
3679 const char **endp,
3680 const mon_cmd_t *cmd)
3681{
3682 const char *typestr;
3683 char *key;
3684 int c;
3685 const char *p = *endp;
3686 char buf[1024];
3687 QDict *qdict = qdict_new();
3688
bellard9307c4c2004-04-04 12:57:25 +00003689 /* parse the parameters */
3690 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003691 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003692 typestr = key_get_info(typestr, &key);
3693 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003694 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003695 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003696 typestr++;
3697 switch(c) {
3698 case 'F':
bellard81d09122004-07-14 17:21:37 +00003699 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003700 case 's':
3701 {
3702 int ret;
ths3b46e622007-09-17 08:09:54 +00003703
blueswir1cd390082008-11-16 13:53:32 +00003704 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003705 p++;
3706 if (*typestr == '?') {
3707 typestr++;
3708 if (*p == '\0') {
3709 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003710 break;
bellard9307c4c2004-04-04 12:57:25 +00003711 }
3712 }
3713 ret = get_str(buf, sizeof(buf), &p);
3714 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003715 switch(c) {
3716 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003717 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003718 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003719 break;
3720 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003721 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003722 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003723 break;
3724 default:
Bandan Dasae502122015-06-03 18:38:08 -04003725 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003726 break;
3727 }
bellard9307c4c2004-04-04 12:57:25 +00003728 goto fail;
3729 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003730 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003731 }
3732 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003733 case 'O':
3734 {
3735 QemuOptsList *opts_list;
3736 QemuOpts *opts;
3737
3738 opts_list = qemu_find_opts(key);
3739 if (!opts_list || opts_list->desc->name) {
3740 goto bad_type;
3741 }
3742 while (qemu_isspace(*p)) {
3743 p++;
3744 }
3745 if (!*p)
3746 break;
3747 if (get_str(buf, sizeof(buf), &p) < 0) {
3748 goto fail;
3749 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003750 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003751 if (!opts) {
3752 goto fail;
3753 }
3754 qemu_opts_to_qdict(opts, qdict);
3755 qemu_opts_del(opts);
3756 }
3757 break;
bellard9307c4c2004-04-04 12:57:25 +00003758 case '/':
3759 {
3760 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003761
blueswir1cd390082008-11-16 13:53:32 +00003762 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003763 p++;
3764 if (*p == '/') {
3765 /* format found */
3766 p++;
3767 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003768 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003769 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003770 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003771 count = count * 10 + (*p - '0');
3772 p++;
3773 }
3774 }
3775 size = -1;
3776 format = -1;
3777 for(;;) {
3778 switch(*p) {
3779 case 'o':
3780 case 'd':
3781 case 'u':
3782 case 'x':
3783 case 'i':
3784 case 'c':
3785 format = *p++;
3786 break;
3787 case 'b':
3788 size = 1;
3789 p++;
3790 break;
3791 case 'h':
3792 size = 2;
3793 p++;
3794 break;
3795 case 'w':
3796 size = 4;
3797 p++;
3798 break;
3799 case 'g':
3800 case 'L':
3801 size = 8;
3802 p++;
3803 break;
3804 default:
3805 goto next;
3806 }
3807 }
3808 next:
blueswir1cd390082008-11-16 13:53:32 +00003809 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003810 monitor_printf(mon, "invalid char in format: '%c'\n",
3811 *p);
bellard9307c4c2004-04-04 12:57:25 +00003812 goto fail;
3813 }
bellard9307c4c2004-04-04 12:57:25 +00003814 if (format < 0)
3815 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003816 if (format != 'i') {
3817 /* for 'i', not specifying a size gives -1 as size */
3818 if (size < 0)
3819 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003820 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003821 }
bellard9307c4c2004-04-04 12:57:25 +00003822 default_fmt_format = format;
3823 } else {
3824 count = 1;
3825 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003826 if (format != 'i') {
3827 size = default_fmt_size;
3828 } else {
3829 size = -1;
3830 }
bellard9307c4c2004-04-04 12:57:25 +00003831 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003832 qdict_put(qdict, "count", qint_from_int(count));
3833 qdict_put(qdict, "format", qint_from_int(format));
3834 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003835 }
3836 break;
3837 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003838 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003839 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003840 {
blueswir1c2efc952007-09-25 17:28:42 +00003841 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003842
blueswir1cd390082008-11-16 13:53:32 +00003843 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003844 p++;
bellard34405572004-06-08 00:55:58 +00003845 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003846 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003847 if (*p == '\0') {
3848 typestr++;
3849 break;
3850 }
bellard34405572004-06-08 00:55:58 +00003851 } else {
3852 if (*p == '.') {
3853 p++;
blueswir1cd390082008-11-16 13:53:32 +00003854 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003855 p++;
bellard34405572004-06-08 00:55:58 +00003856 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003857 typestr++;
3858 break;
bellard34405572004-06-08 00:55:58 +00003859 }
3860 }
bellard13224a82006-07-14 22:03:35 +00003861 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003862 }
aliguori376253e2009-03-05 23:01:23 +00003863 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003864 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003865 /* Check if 'i' is greater than 32-bit */
3866 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003867 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003868 monitor_printf(mon, "integer is for 32-bit values\n");
3869 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003870 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003871 if (val < 0) {
3872 monitor_printf(mon, "enter a positive value\n");
3873 goto fail;
3874 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003875 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003876 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003877 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003878 }
3879 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003880 case 'o':
3881 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003882 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003883 char *end;
3884
3885 while (qemu_isspace(*p)) {
3886 p++;
3887 }
3888 if (*typestr == '?') {
3889 typestr++;
3890 if (*p == '\0') {
3891 break;
3892 }
3893 }
3894 val = strtosz(p, &end);
3895 if (val < 0) {
3896 monitor_printf(mon, "invalid size\n");
3897 goto fail;
3898 }
3899 qdict_put(qdict, key, qint_from_int(val));
3900 p = end;
3901 }
3902 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003903 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003904 {
3905 double val;
3906
3907 while (qemu_isspace(*p))
3908 p++;
3909 if (*typestr == '?') {
3910 typestr++;
3911 if (*p == '\0') {
3912 break;
3913 }
3914 }
3915 if (get_double(mon, &val, &p) < 0) {
3916 goto fail;
3917 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003918 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003919 switch (*p) {
3920 case 'm':
3921 val /= 1e3; p += 2; break;
3922 case 'u':
3923 val /= 1e6; p += 2; break;
3924 case 'n':
3925 val /= 1e9; p += 2; break;
3926 }
3927 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003928 if (*p && !qemu_isspace(*p)) {
3929 monitor_printf(mon, "Unknown unit suffix\n");
3930 goto fail;
3931 }
3932 qdict_put(qdict, key, qfloat_from_double(val));
3933 }
3934 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003935 case 'b':
3936 {
3937 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003938 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003939
3940 while (qemu_isspace(*p)) {
3941 p++;
3942 }
3943 beg = p;
3944 while (qemu_isgraph(*p)) {
3945 p++;
3946 }
3947 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003948 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003949 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003950 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003951 } else {
3952 monitor_printf(mon, "Expected 'on' or 'off'\n");
3953 goto fail;
3954 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06003955 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003956 }
3957 break;
bellard9307c4c2004-04-04 12:57:25 +00003958 case '-':
3959 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003960 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003961 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003962 /* option */
ths3b46e622007-09-17 08:09:54 +00003963
bellard9307c4c2004-04-04 12:57:25 +00003964 c = *typestr++;
3965 if (c == '\0')
3966 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003967 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003968 p++;
bellard9307c4c2004-04-04 12:57:25 +00003969 if (*p == '-') {
3970 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003971 if(c != *p) {
3972 if(!is_valid_option(p, typestr)) {
3973
3974 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003975 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003976 goto fail;
3977 } else {
3978 skip_key = 1;
3979 }
bellard9307c4c2004-04-04 12:57:25 +00003980 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003981 if(skip_key) {
3982 p = tmp;
3983 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003984 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003985 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003986 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003987 }
bellard9307c4c2004-04-04 12:57:25 +00003988 }
bellard9307c4c2004-04-04 12:57:25 +00003989 }
3990 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003991 case 'S':
3992 {
3993 /* package all remaining string */
3994 int len;
3995
3996 while (qemu_isspace(*p)) {
3997 p++;
3998 }
3999 if (*typestr == '?') {
4000 typestr++;
4001 if (*p == '\0') {
4002 /* no remaining string: NULL argument */
4003 break;
4004 }
4005 }
4006 len = strlen(p);
4007 if (len <= 0) {
4008 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04004009 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04004010 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08004011 }
4012 qdict_put(qdict, key, qstring_from_str(p));
4013 p += len;
4014 }
4015 break;
bellard9307c4c2004-04-04 12:57:25 +00004016 default:
4017 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04004018 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00004019 goto fail;
4020 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004021 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004022 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004023 }
4024 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004025 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004026 p++;
4027 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004028 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04004029 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00004030 goto fail;
4031 }
4032
Bandan Dasae502122015-06-03 18:38:08 -04004033 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004034
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004035fail:
Bandan Dasae502122015-06-03 18:38:08 -04004036 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05004037 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004038 return NULL;
4039}
4040
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004041static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004042{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004043 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004044 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004045
Bandan Dasae502122015-06-03 18:38:08 -04004046 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4047 if (!cmd) {
4048 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004049 }
4050
Bandan Dasae502122015-06-03 18:38:08 -04004051 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4052 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04004053 monitor_printf(mon, "Try \"help %s\" for more information\n",
4054 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04004055 return;
4056 }
4057
4058 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004059 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004060}
4061
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004062static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004063{
4064 const char *p, *pstart;
4065 char cmd[128];
4066 int len;
4067
4068 p = list;
4069 for(;;) {
4070 pstart = p;
4071 p = strchr(p, '|');
4072 if (!p)
4073 p = pstart + strlen(pstart);
4074 len = p - pstart;
4075 if (len > sizeof(cmd) - 2)
4076 len = sizeof(cmd) - 2;
4077 memcpy(cmd, pstart, len);
4078 cmd[len] = '\0';
4079 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004080 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004081 }
4082 if (*p == '\0')
4083 break;
4084 p++;
4085 }
4086}
4087
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004088static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004089{
4090 DIR *ffs;
4091 struct dirent *d;
4092 char path[1024];
4093 char file[1024], file_prefix[1024];
4094 int input_path_len;
4095 const char *p;
4096
ths5fafdf22007-09-16 21:08:06 +00004097 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004098 if (!p) {
4099 input_path_len = 0;
4100 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004101 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004102 } else {
4103 input_path_len = p - input + 1;
4104 memcpy(path, input, input_path_len);
4105 if (input_path_len > sizeof(path) - 1)
4106 input_path_len = sizeof(path) - 1;
4107 path[input_path_len] = '\0';
4108 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4109 }
Bandan Das19f2db52015-06-03 18:38:07 -04004110
bellard81d09122004-07-14 17:21:37 +00004111 ffs = opendir(path);
4112 if (!ffs)
4113 return;
4114 for(;;) {
4115 struct stat sb;
4116 d = readdir(ffs);
4117 if (!d)
4118 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004119
4120 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4121 continue;
4122 }
4123
bellard81d09122004-07-14 17:21:37 +00004124 if (strstart(d->d_name, file_prefix, NULL)) {
4125 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004126 if (input_path_len < sizeof(file))
4127 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4128 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004129 /* stat the file to find out if it's a directory.
4130 * In that case add a slash to speed up typing long paths
4131 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004132 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004133 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004134 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004135 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004136 }
4137 }
4138 closedir(ffs);
4139}
4140
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004141static const char *next_arg_type(const char *typestr)
4142{
4143 const char *p = strchr(typestr, ':');
4144 return (p != NULL ? ++p : typestr);
4145}
4146
Hani Benhabiles40d19392014-05-07 23:41:30 +01004147static void add_completion_option(ReadLineState *rs, const char *str,
4148 const char *option)
4149{
4150 if (!str || !option) {
4151 return;
4152 }
4153 if (!strncmp(option, str, strlen(str))) {
4154 readline_add_completion(rs, option);
4155 }
4156}
4157
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004158void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4159{
4160 size_t len;
4161 ChardevBackendInfoList *list, *start;
4162
4163 if (nb_args != 2) {
4164 return;
4165 }
4166 len = strlen(str);
4167 readline_set_completion_index(rs, len);
4168
4169 start = list = qmp_query_chardev_backends(NULL);
4170 while (list) {
4171 const char *chr_name = list->value->name;
4172
4173 if (!strncmp(chr_name, str, len)) {
4174 readline_add_completion(rs, chr_name);
4175 }
4176 list = list->next;
4177 }
4178 qapi_free_ChardevBackendInfoList(start);
4179}
4180
Hani Benhabilesb162b492014-05-07 23:41:31 +01004181void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4182{
4183 size_t len;
4184 int i;
4185
4186 if (nb_args != 2) {
4187 return;
4188 }
4189 len = strlen(str);
4190 readline_set_completion_index(rs, len);
4191 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4192 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4193 }
4194}
4195
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004196void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004197{
4198 GSList *list, *elt;
4199 size_t len;
4200
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004201 if (nb_args != 2) {
4202 return;
4203 }
4204
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004205 len = strlen(str);
4206 readline_set_completion_index(rs, len);
4207 list = elt = object_class_get_list(TYPE_DEVICE, false);
4208 while (elt) {
4209 const char *name;
4210 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4211 TYPE_DEVICE);
4212 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004213
4214 if (!dc->cannot_instantiate_with_device_add_yet
4215 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004216 readline_add_completion(rs, name);
4217 }
4218 elt = elt->next;
4219 }
4220 g_slist_free(list);
4221}
4222
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004223void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004224{
4225 GSList *list, *elt;
4226 size_t len;
4227
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004228 if (nb_args != 2) {
4229 return;
4230 }
4231
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004232 len = strlen(str);
4233 readline_set_completion_index(rs, len);
4234 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4235 while (elt) {
4236 const char *name;
4237
4238 name = object_class_get_name(OBJECT_CLASS(elt->data));
4239 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4240 readline_add_completion(rs, name);
4241 }
4242 elt = elt->next;
4243 }
4244 g_slist_free(list);
4245}
4246
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004247static void peripheral_device_del_completion(ReadLineState *rs,
4248 const char *str, size_t len)
4249{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004250 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4251 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004252
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004253 list = qdev_build_hotpluggable_device_list(peripheral);
4254 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004255 return;
4256 }
4257
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004258 for (item = list; item; item = g_slist_next(item)) {
4259 DeviceState *dev = item->data;
4260
4261 if (dev->id && !strncmp(str, dev->id, len)) {
4262 readline_add_completion(rs, dev->id);
4263 }
4264 }
4265
4266 g_slist_free(list);
4267}
4268
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004269void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4270{
4271 size_t len;
4272 ChardevInfoList *list, *start;
4273
4274 if (nb_args != 2) {
4275 return;
4276 }
4277 len = strlen(str);
4278 readline_set_completion_index(rs, len);
4279
4280 start = list = qmp_query_chardev(NULL);
4281 while (list) {
4282 ChardevInfo *chr = list->value;
4283
4284 if (!strncmp(chr->label, str, len)) {
4285 readline_add_completion(rs, chr->label);
4286 }
4287 list = list->next;
4288 }
4289 qapi_free_ChardevInfoList(start);
4290}
4291
Hani Benhabiles8e597772014-05-27 23:39:30 +01004292static void ringbuf_completion(ReadLineState *rs, const char *str)
4293{
4294 size_t len;
4295 ChardevInfoList *list, *start;
4296
4297 len = strlen(str);
4298 readline_set_completion_index(rs, len);
4299
4300 start = list = qmp_query_chardev(NULL);
4301 while (list) {
4302 ChardevInfo *chr_info = list->value;
4303
4304 if (!strncmp(chr_info->label, str, len)) {
4305 CharDriverState *chr = qemu_chr_find(chr_info->label);
4306 if (chr && chr_is_ringbuf(chr)) {
4307 readline_add_completion(rs, chr_info->label);
4308 }
4309 }
4310 list = list->next;
4311 }
4312 qapi_free_ChardevInfoList(start);
4313}
4314
Hani Benhabiles8e597772014-05-27 23:39:30 +01004315void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4316{
4317 if (nb_args != 2) {
4318 return;
4319 }
4320 ringbuf_completion(rs, str);
4321}
4322
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004323void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4324{
4325 size_t len;
4326
4327 if (nb_args != 2) {
4328 return;
4329 }
4330
4331 len = strlen(str);
4332 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004333 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004334}
4335
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004336void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004337{
4338 ObjectPropertyInfoList *list, *start;
4339 size_t len;
4340
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004341 if (nb_args != 2) {
4342 return;
4343 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004344 len = strlen(str);
4345 readline_set_completion_index(rs, len);
4346
4347 start = list = qmp_qom_list("/objects", NULL);
4348 while (list) {
4349 ObjectPropertyInfo *info = list->value;
4350
4351 if (!strncmp(info->type, "child<", 5)
4352 && !strncmp(info->name, str, len)) {
4353 readline_add_completion(rs, info->name);
4354 }
4355 list = list->next;
4356 }
4357 qapi_free_ObjectPropertyInfoList(start);
4358}
4359
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004360void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4361{
4362 int i;
4363 char *sep;
4364 size_t len;
4365
4366 if (nb_args != 2) {
4367 return;
4368 }
4369 sep = strrchr(str, '-');
4370 if (sep) {
4371 str = sep + 1;
4372 }
4373 len = strlen(str);
4374 readline_set_completion_index(rs, len);
4375 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4376 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4377 readline_add_completion(rs, QKeyCode_lookup[i]);
4378 }
4379 }
4380}
4381
Hani Benhabiles40d19392014-05-07 23:41:30 +01004382void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4383{
4384 size_t len;
4385
4386 len = strlen(str);
4387 readline_set_completion_index(rs, len);
4388 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004389 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004390 int count, i;
4391 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004392 NET_CLIENT_OPTIONS_KIND_NONE,
4393 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004394 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004395 const char *name = ncs[i]->name;
4396 if (!strncmp(str, name, len)) {
4397 readline_add_completion(rs, name);
4398 }
4399 }
4400 } else if (nb_args == 3) {
4401 add_completion_option(rs, str, "on");
4402 add_completion_option(rs, str, "off");
4403 }
4404}
4405
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004406void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4407{
4408 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004409 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004410
4411 if (nb_args != 2) {
4412 return;
4413 }
4414
4415 len = strlen(str);
4416 readline_set_completion_index(rs, len);
4417 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004418 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004419 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004420 QemuOpts *opts;
4421 const char *name = ncs[i]->name;
4422 if (strncmp(str, name, len)) {
4423 continue;
4424 }
4425 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4426 if (opts) {
4427 readline_add_completion(rs, name);
4428 }
4429 }
4430}
4431
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004432void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4433{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004434 int i;
4435
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004436 if (nb_args != 2) {
4437 return;
4438 }
4439 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004440 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4441 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4442 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004443}
4444
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004445void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4446 const char *str)
4447{
4448 size_t len;
4449
4450 len = strlen(str);
4451 readline_set_completion_index(rs, len);
4452 if (nb_args == 2) {
4453 int i;
4454 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4455 const char *name = MigrationCapability_lookup[i];
4456 if (!strncmp(str, name, len)) {
4457 readline_add_completion(rs, name);
4458 }
4459 }
4460 } else if (nb_args == 3) {
4461 add_completion_option(rs, str, "on");
4462 add_completion_option(rs, str, "off");
4463 }
4464}
4465
Liang Li50e9a622015-03-23 16:32:29 +08004466void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4467 const char *str)
4468{
4469 size_t len;
4470
4471 len = strlen(str);
4472 readline_set_completion_index(rs, len);
4473 if (nb_args == 2) {
4474 int i;
4475 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4476 const char *name = MigrationParameter_lookup[i];
4477 if (!strncmp(str, name, len)) {
4478 readline_add_completion(rs, name);
4479 }
4480 }
4481 }
4482}
4483
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004484void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4485{
4486 int i;
4487 size_t len;
4488 if (nb_args != 2) {
4489 return;
4490 }
4491 len = strlen(str);
4492 readline_set_completion_index(rs, len);
4493 for (i = 0; host_net_devices[i]; i++) {
4494 if (!strncmp(host_net_devices[i], str, len)) {
4495 readline_add_completion(rs, host_net_devices[i]);
4496 }
4497 }
4498}
4499
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004500void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4501{
Jason Wangeaed4832015-04-23 14:21:38 +08004502 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004503 int count, i, len;
4504
4505 len = strlen(str);
4506 readline_set_completion_index(rs, len);
4507 if (nb_args == 2) {
4508 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004509 NET_CLIENT_OPTIONS_KIND_NONE,
4510 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004511 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004512 int id;
4513 char name[16];
4514
4515 if (net_hub_id_for_client(ncs[i], &id)) {
4516 continue;
4517 }
4518 snprintf(name, sizeof(name), "%d", id);
4519 if (!strncmp(str, name, len)) {
4520 readline_add_completion(rs, name);
4521 }
4522 }
4523 return;
4524 } else if (nb_args == 3) {
4525 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004526 NET_CLIENT_OPTIONS_KIND_NIC,
4527 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004528 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004529 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004530 const char *name;
4531
Jason Wang2c4681f2015-02-02 15:06:38 +08004532 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4533 net_hub_id_for_client(ncs[i], &id)) {
4534 continue;
4535 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004536 name = ncs[i]->name;
4537 if (!strncmp(str, name, len)) {
4538 readline_add_completion(rs, name);
4539 }
4540 }
4541 return;
4542 }
4543}
4544
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004545static void vm_completion(ReadLineState *rs, const char *str)
4546{
4547 size_t len;
4548 BlockDriverState *bs = NULL;
4549
4550 len = strlen(str);
4551 readline_set_completion_index(rs, len);
4552 while ((bs = bdrv_next(bs))) {
4553 SnapshotInfoList *snapshots, *snapshot;
4554
4555 if (!bdrv_can_snapshot(bs)) {
4556 continue;
4557 }
4558 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4559 continue;
4560 }
4561 snapshot = snapshots;
4562 while (snapshot) {
4563 char *completion = snapshot->value->name;
4564 if (!strncmp(str, completion, len)) {
4565 readline_add_completion(rs, completion);
4566 }
4567 completion = snapshot->value->id;
4568 if (!strncmp(str, completion, len)) {
4569 readline_add_completion(rs, completion);
4570 }
4571 snapshot = snapshot->next;
4572 }
4573 qapi_free_SnapshotInfoList(snapshots);
4574 }
4575
4576}
4577
4578void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4579{
4580 if (nb_args == 2) {
4581 vm_completion(rs, str);
4582 }
4583}
4584
4585void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4586{
4587 if (nb_args == 2) {
4588 vm_completion(rs, str);
4589 }
4590}
4591
Wenchao Xiac35b6402013-08-27 20:38:24 +08004592static void monitor_find_completion_by_table(Monitor *mon,
4593 const mon_cmd_t *cmd_table,
4594 char **args,
4595 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004596{
4597 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004598 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004599 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004600 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004601 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004602
bellard81d09122004-07-14 17:21:37 +00004603 if (nb_args <= 1) {
4604 /* command completion */
4605 if (nb_args == 0)
4606 cmdname = "";
4607 else
4608 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004609 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004610 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004611 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004612 }
4613 } else {
4614 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004615 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004616 if (compare_cmd(args[0], cmd->name)) {
4617 break;
4618 }
bellard81d09122004-07-14 17:21:37 +00004619 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004620 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004621 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004622 }
4623
Wenchao Xiad903a772013-08-27 20:38:25 +08004624 if (cmd->sub_table) {
4625 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004626 monitor_find_completion_by_table(mon, cmd->sub_table,
4627 &args[1], nb_args - 1);
4628 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004629 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004630 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004631 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4632 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004633 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004634
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004635 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004636 for(i = 0; i < nb_args - 2; i++) {
4637 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004638 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004639 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004640 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004641 }
4642 }
4643 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004644 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004645 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004646 }
bellard81d09122004-07-14 17:21:37 +00004647 switch(*ptype) {
4648 case 'F':
4649 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004650 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004651 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004652 break;
4653 case 'B':
4654 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004655 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004656 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4657 name = bdrv_get_device_name(bs);
4658 if (str[0] == '\0' ||
4659 !strncmp(name, str, strlen(str))) {
4660 readline_add_completion(mon->rs, name);
4661 }
4662 }
bellard81d09122004-07-14 17:21:37 +00004663 break;
bellard7fe48482004-10-09 18:08:01 +00004664 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004665 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004666 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004667 monitor_find_completion_by_table(mon, cmd_table,
4668 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004669 }
4670 break;
bellard81d09122004-07-14 17:21:37 +00004671 default:
4672 break;
4673 }
4674 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004675}
4676
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004677static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004678 const char *cmdline)
4679{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004680 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004681 char *args[MAX_ARGS];
4682 int nb_args, len;
4683
4684 /* 1. parse the cmdline */
4685 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4686 return;
4687 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004688
4689 /* if the line ends with a space, it means we want to complete the
4690 next arg */
4691 len = strlen(cmdline);
4692 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4693 if (nb_args >= MAX_ARGS) {
4694 goto cleanup;
4695 }
4696 args[nb_args++] = g_strdup("");
4697 }
4698
4699 /* 2. auto complete according to args */
4700 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004701
4702cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004703 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004704}
4705
aliguori731b0362009-03-05 23:01:42 +00004706static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004707{
aliguori731b0362009-03-05 23:01:42 +00004708 Monitor *mon = opaque;
4709
Jan Kiszkac62313b2009-12-04 14:05:29 +01004710 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004711}
4712
Markus Armbruster40861822015-05-29 10:27:16 +02004713static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4714 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004715{
Markus Armbruster485febc2015-03-13 17:25:50 +01004716 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004717
4718 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004719 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4720 "Capabilities negotiation is already complete, command "
4721 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004722 return true;
4723 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004724 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004725 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4726 "Expecting capabilities negotiation with "
4727 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004728 return true;
4729 }
4730 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004731}
4732
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004733/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004734 * Argument validation rules:
4735 *
4736 * 1. The argument must exist in cmd_args qdict
4737 * 2. The argument type must be the expected one
4738 *
4739 * Special case: If the argument doesn't exist in cmd_args and
4740 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4741 * checking is skipped for it.
4742 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004743static void check_client_args_type(const QDict *client_args,
4744 const QDict *cmd_args, int flags,
4745 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004746{
4747 const QDictEntry *ent;
4748
4749 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4750 QObject *obj;
4751 QString *arg_type;
4752 const QObject *client_arg = qdict_entry_value(ent);
4753 const char *client_arg_name = qdict_entry_key(ent);
4754
4755 obj = qdict_get(cmd_args, client_arg_name);
4756 if (!obj) {
4757 if (flags & QMP_ACCEPT_UNKNOWNS) {
4758 /* handler accepts unknowns */
4759 continue;
4760 }
4761 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004762 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004763 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004764 }
4765
4766 arg_type = qobject_to_qstring(obj);
4767 assert(arg_type != NULL);
4768
4769 /* check if argument's type is correct */
4770 switch (qstring_get_str(arg_type)[0]) {
4771 case 'F':
4772 case 'B':
4773 case 's':
4774 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004775 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4776 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01004777 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004778 }
4779 break;
4780 case 'i':
4781 case 'l':
4782 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004783 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004784 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004785 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4786 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01004787 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004788 }
4789 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004790 case 'T':
4791 if (qobject_type(client_arg) != QTYPE_QINT &&
4792 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004793 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4794 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01004795 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004796 }
4797 break;
4798 case 'b':
4799 case '-':
4800 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004801 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4802 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01004803 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004804 }
4805 break;
4806 case 'O':
4807 assert(flags & QMP_ACCEPT_UNKNOWNS);
4808 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004809 case 'q':
4810 /* Any QObject can be passed. */
4811 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004812 case '/':
4813 case '.':
4814 /*
4815 * These types are not supported by QMP and thus are not
4816 * handled here. Fall through.
4817 */
4818 default:
4819 abort();
4820 }
4821 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004822}
4823
4824/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004825 * - Check if the client has passed all mandatory args
4826 * - Set special flags for argument validation
4827 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004828static void check_mandatory_args(const QDict *cmd_args,
4829 const QDict *client_args, int *flags,
4830 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004831{
4832 const QDictEntry *ent;
4833
4834 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4835 const char *cmd_arg_name = qdict_entry_key(ent);
4836 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4837 assert(type != NULL);
4838
4839 if (qstring_get_str(type)[0] == 'O') {
4840 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4841 *flags |= QMP_ACCEPT_UNKNOWNS;
4842 } else if (qstring_get_str(type)[0] != '-' &&
4843 qstring_get_str(type)[1] != '?' &&
4844 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004845 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004846 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004847 }
4848 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004849}
4850
4851static QDict *qdict_from_args_type(const char *args_type)
4852{
4853 int i;
4854 QDict *qdict;
4855 QString *key, *type, *cur_qs;
4856
4857 assert(args_type != NULL);
4858
4859 qdict = qdict_new();
4860
4861 if (args_type == NULL || args_type[0] == '\0') {
4862 /* no args, empty qdict */
4863 goto out;
4864 }
4865
4866 key = qstring_new();
4867 type = qstring_new();
4868
4869 cur_qs = key;
4870
4871 for (i = 0;; i++) {
4872 switch (args_type[i]) {
4873 case ',':
4874 case '\0':
4875 qdict_put(qdict, qstring_get_str(key), type);
4876 QDECREF(key);
4877 if (args_type[i] == '\0') {
4878 goto out;
4879 }
4880 type = qstring_new(); /* qdict has ref */
4881 cur_qs = key = qstring_new();
4882 break;
4883 case ':':
4884 cur_qs = type;
4885 break;
4886 default:
4887 qstring_append_chr(cur_qs, args_type[i]);
4888 break;
4889 }
4890 }
4891
4892out:
4893 return qdict;
4894}
4895
4896/*
4897 * Client argument checking rules:
4898 *
4899 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004900 * 2. Each argument provided by the client must be expected
4901 * 3. Each argument provided by the client must have the type expected
4902 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004903 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004904static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4905 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004906{
Markus Armbruster326283a2015-03-02 18:39:09 +01004907 Error *err = NULL;
4908 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004909 QDict *cmd_args;
4910
4911 cmd_args = qdict_from_args_type(cmd->args_type);
4912
4913 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004914 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004915 if (err) {
4916 goto out;
4917 }
4918
Markus Armbruster326283a2015-03-02 18:39:09 +01004919 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004920
4921out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004922 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004923 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004924}
4925
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004926/*
4927 * Input object checking rules
4928 *
4929 * 1. Input object must be a dict
4930 * 2. The "execute" key must exist
4931 * 3. The "execute" key must be a string
4932 * 4. If the "arguments" key exists, it must be a dict
4933 * 5. If the "id" key exists, it can be anything (ie. json-value)
4934 * 6. Any argument not listed above is considered invalid
4935 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004936static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004937{
4938 const QDictEntry *ent;
4939 int has_exec_key = 0;
4940 QDict *input_dict;
4941
4942 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004943 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004944 return NULL;
4945 }
4946
4947 input_dict = qobject_to_qdict(input_obj);
4948
4949 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4950 const char *arg_name = qdict_entry_key(ent);
4951 const QObject *arg_obj = qdict_entry_value(ent);
4952
4953 if (!strcmp(arg_name, "execute")) {
4954 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004955 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4956 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004957 return NULL;
4958 }
4959 has_exec_key = 1;
4960 } else if (!strcmp(arg_name, "arguments")) {
4961 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004962 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4963 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004964 return NULL;
4965 }
Markus Armbruster779cec42015-06-08 10:44:30 +02004966 } else if (!strcmp(arg_name, "id")) {
4967 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004968 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004969 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004970 return NULL;
4971 }
4972 }
4973
4974 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004975 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004976 return NULL;
4977 }
4978
4979 return input_dict;
4980}
4981
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004982static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4983{
Markus Armbruster326283a2015-03-02 18:39:09 +01004984 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004985 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004986 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004987 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004988 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004989 Monitor *mon = cur_mon;
4990
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004991 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004992 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004993
4994 obj = json_parser_parse(tokens, NULL);
4995 if (!obj) {
4996 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004997 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004998 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004999 }
5000
Markus Armbrusterba0510a2015-03-02 18:41:43 +01005001 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005002 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005003 qobject_decref(obj);
5004 goto err_out;
5005 }
5006
Markus Armbruster74358f22015-03-06 19:35:59 +01005007 mon->qmp.id = qdict_get(input, "id");
5008 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005009
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005010 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005011 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005012 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005013 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005014 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5015 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005016 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005017 }
Markus Armbruster40861822015-05-29 10:27:16 +02005018 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005019 goto err_out;
5020 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005021
5022 obj = qdict_get(input, "arguments");
5023 if (!obj) {
5024 args = qdict_new();
5025 } else {
5026 args = qobject_to_qdict(obj);
5027 QINCREF(args);
5028 }
5029
Markus Armbruster326283a2015-03-02 18:39:09 +01005030 qmp_check_client_args(cmd, args, &local_err);
5031 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005032 goto err_out;
5033 }
5034
Markus Armbruster485febc2015-03-13 17:25:50 +01005035 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005036
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005037err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005038 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005039 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005040 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005041 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005042}
5043
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005044static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005045{
5046 Monitor *old_mon = cur_mon;
5047
5048 cur_mon = opaque;
5049
Markus Armbruster74358f22015-03-06 19:35:59 +01005050 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005051
5052 cur_mon = old_mon;
5053}
5054
aliguori731b0362009-03-05 23:01:42 +00005055static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005056{
aliguori731b0362009-03-05 23:01:42 +00005057 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005058 int i;
aliguori376253e2009-03-05 23:01:23 +00005059
aliguori731b0362009-03-05 23:01:42 +00005060 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005061
aliguoricde76ee2009-03-05 23:01:51 +00005062 if (cur_mon->rs) {
5063 for (i = 0; i < size; i++)
5064 readline_handle_byte(cur_mon->rs, buf[i]);
5065 } else {
5066 if (size == 0 || buf[size - 1] != 0)
5067 monitor_printf(cur_mon, "corrupted command\n");
5068 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005069 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005070 }
aliguori731b0362009-03-05 23:01:42 +00005071
5072 cur_mon = old_mon;
5073}
aliguorid8f44602008-10-06 13:52:44 +00005074
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005075static void monitor_command_cb(void *opaque, const char *cmdline,
5076 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005077{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005078 Monitor *mon = opaque;
5079
aliguori731b0362009-03-05 23:01:42 +00005080 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005081 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005082 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005083}
5084
aliguoricde76ee2009-03-05 23:01:51 +00005085int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005086{
aliguoricde76ee2009-03-05 23:01:51 +00005087 if (!mon->rs)
5088 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005089 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005090 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005091}
5092
aliguori376253e2009-03-05 23:01:23 +00005093void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005094{
aliguoricde76ee2009-03-05 23:01:51 +00005095 if (!mon->rs)
5096 return;
aliguori731b0362009-03-05 23:01:42 +00005097 if (--mon->suspend_cnt == 0)
5098 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005099}
5100
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005101static QObject *get_qmp_greeting(void)
5102{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005103 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005104
Markus Armbruster485febc2015-03-13 17:25:50 +01005105 qmp_marshal_input_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005106 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5107}
5108
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005109static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005110{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005111 QObject *data;
5112 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005113
Luiz Capitulino47116d12010-02-08 17:01:30 -02005114 switch (event) {
5115 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005116 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005117 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005118 monitor_json_emitter(mon, data);
5119 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005120 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005121 break;
5122 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005123 json_message_parser_destroy(&mon->qmp.parser);
5124 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005125 mon_refcount--;
5126 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005127 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005128 }
5129}
5130
aliguori731b0362009-03-05 23:01:42 +00005131static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005132{
aliguori376253e2009-03-05 23:01:23 +00005133 Monitor *mon = opaque;
5134
aliguori2724b182009-03-05 23:01:47 +00005135 switch (event) {
5136 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005137 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005138 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005139 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005140 if (mon->reset_seen) {
5141 readline_restart(mon->rs);
5142 monitor_resume(mon);
5143 monitor_flush(mon);
5144 } else {
5145 mon->suspend_cnt = 0;
5146 }
aliguori2724b182009-03-05 23:01:47 +00005147 break;
ths86e94de2007-01-05 22:01:59 +00005148
aliguori2724b182009-03-05 23:01:47 +00005149 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005150 if (mon->reset_seen) {
5151 if (mon->suspend_cnt == 0) {
5152 monitor_printf(mon, "\n");
5153 }
5154 monitor_flush(mon);
5155 monitor_suspend(mon);
5156 } else {
5157 mon->suspend_cnt++;
5158 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005159 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005160 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005161 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005162 break;
5163
Amit Shahb6b8df52009-10-07 18:31:16 +05305164 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005165 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5166 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005167 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005168 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005169 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005170 }
5171 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005172 mon_refcount++;
5173 break;
5174
5175 case CHR_EVENT_CLOSED:
5176 mon_refcount--;
5177 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005178 break;
5179 }
ths86e94de2007-01-05 22:01:59 +00005180}
5181
Wayne Xia816f8922011-10-12 11:32:41 +08005182static int
5183compare_mon_cmd(const void *a, const void *b)
5184{
5185 return strcmp(((const mon_cmd_t *)a)->name,
5186 ((const mon_cmd_t *)b)->name);
5187}
5188
5189static void sortcmdlist(void)
5190{
5191 int array_num;
5192 int elem_size = sizeof(mon_cmd_t);
5193
5194 array_num = sizeof(mon_cmds)/elem_size-1;
5195 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5196
5197 array_num = sizeof(info_cmds)/elem_size-1;
5198 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5199}
5200
aliguori76655d62009-03-06 20:27:37 +00005201
5202/*
5203 * Local variables:
5204 * c-indent-level: 4
5205 * c-basic-offset: 4
5206 * tab-width: 8
5207 * End:
5208 */
5209
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005210/* These functions just adapt the readline interface in a typesafe way. We
5211 * could cast function pointers but that discards compiler checks.
5212 */
Stefan Weild5d15072014-01-25 18:18:23 +01005213static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5214 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005215{
5216 va_list ap;
5217 va_start(ap, fmt);
5218 monitor_vprintf(opaque, fmt, ap);
5219 va_end(ap);
5220}
5221
5222static void monitor_readline_flush(void *opaque)
5223{
5224 monitor_flush(opaque);
5225}
5226
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005227static void __attribute__((constructor)) monitor_lock_init(void)
5228{
5229 qemu_mutex_init(&monitor_lock);
5230}
5231
aliguori731b0362009-03-05 23:01:42 +00005232void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005233{
aliguori731b0362009-03-05 23:01:42 +00005234 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005235 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005236
5237 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005238 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005239 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005240 is_first_init = 0;
5241 }
aliguori87127162009-03-05 23:01:29 +00005242
Wenchao Xiab01fe892013-08-27 20:38:19 +08005243 mon = g_malloc(sizeof(*mon));
5244 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005245
aliguori87127162009-03-05 23:01:29 +00005246 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005247 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005248 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005249 mon->rs = readline_init(monitor_readline_printf,
5250 monitor_readline_flush,
5251 mon,
5252 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005253 monitor_read_command(mon, 0);
5254 }
aliguori87127162009-03-05 23:01:29 +00005255
Markus Armbruster9f3982f2015-03-06 19:56:38 +01005256 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005257 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5258 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005259 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005260 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005261 } else {
5262 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5263 monitor_event, mon);
5264 }
aliguori87127162009-03-05 23:01:29 +00005265
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005266 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005267 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005268 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00005269}
5270
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005271static void bdrv_password_cb(void *opaque, const char *password,
5272 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005273{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005274 Monitor *mon = opaque;
5275 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005276 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005277 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005278
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005279 bdrv_add_key(bs, password, &local_err);
5280 if (local_err) {
5281 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5282 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005283 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005284 }
aliguori731b0362009-03-05 23:01:42 +00005285 if (mon->password_completion_cb)
5286 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005287
aliguori731b0362009-03-05 23:01:42 +00005288 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005289}
aliguoric0f4ce72009-03-05 23:01:01 +00005290
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005291int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005292 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005293 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005294{
aliguoricde76ee2009-03-05 23:01:51 +00005295 int err;
5296
aliguori376253e2009-03-05 23:01:23 +00005297 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5298 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005299
aliguori731b0362009-03-05 23:01:42 +00005300 mon->password_completion_cb = completion_cb;
5301 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005302
aliguoricde76ee2009-03-05 23:01:51 +00005303 err = monitor_read_password(mon, bdrv_password_cb, bs);
5304
5305 if (err && completion_cb)
5306 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005307
5308 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005309}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005310
5311int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005312 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005313 void *opaque)
5314{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005315 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005316 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005317
Fam Zheng55606252015-03-02 19:36:46 +08005318 blk = blk_by_name(device);
5319 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005320 monitor_printf(mon, "Device not found %s\n", device);
5321 return -1;
5322 }
5323
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005324 bdrv_add_key(blk_bs(blk), NULL, &err);
5325 if (err) {
5326 error_free(err);
5327 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5328 }
5329
5330 if (completion_cb) {
5331 completion_cb(opaque, 0);
5332 }
5333 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005334}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005335
5336QemuOptsList qemu_mon_opts = {
5337 .name = "mon",
5338 .implied_opt_name = "chardev",
5339 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5340 .desc = {
5341 {
5342 .name = "mode",
5343 .type = QEMU_OPT_STRING,
5344 },{
5345 .name = "chardev",
5346 .type = QEMU_OPT_STRING,
5347 },{
5348 .name = "default",
5349 .type = QEMU_OPT_BOOL,
5350 },{
5351 .name = "pretty",
5352 .type = QEMU_OPT_BOOL,
5353 },
5354 { /* end of list */ }
5355 },
5356};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005357
5358#ifndef TARGET_I386
5359void qmp_rtc_reset_reinjection(Error **errp)
5360{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005361 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005362}
5363#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04005364
5365#ifndef TARGET_S390X
5366void qmp_dump_skeys(const char *filename, Error **errp)
5367{
5368 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
5369}
5370#endif