blob: 9afee7b946e1163400acdeba35814eaa1684db48 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080076#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000077
Markus Armbruster1ce6be22015-02-06 14:18:24 +010078/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020079#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010080#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020081#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083
bellard9dc39cb2004-03-14 21:38:27 +000084//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000085//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000086
bellard9307c4c2004-04-04 12:57:25 +000087/*
88 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000089 *
bellard9307c4c2004-04-04 12:57:25 +000090 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000091 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000092 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080093 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010094 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000099 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200103 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000111 * '/' optional gdb-like print format (like "/10x")
112 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100115 * 'b' boolean
116 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000118 *
119 */
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000123 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *params;
125 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800130 /* @sub_table is a list of 2nd level of commands. If it do not exist,
131 * mhandler should be used. If it exist, sub_table[?].mhandler should be
132 * used, and mhandler of 1st level plays the role of help function.
133 */
134 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100135 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500136} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000137
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500139typedef struct mon_fd_t mon_fd_t;
140struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141 char *name;
142 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144};
145
Corey Bryantba1c0482012-08-14 16:43:43 -0400146/* file descriptor associated with a file descriptor set */
147typedef struct MonFdsetFd MonFdsetFd;
148struct MonFdsetFd {
149 int fd;
150 bool removed;
151 char *opaque;
152 QLIST_ENTRY(MonFdsetFd) next;
153};
154
155/* file descriptor set containing fds passed via SCM_RIGHTS */
156typedef struct MonFdset MonFdset;
157struct MonFdset {
158 int64_t id;
159 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400160 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400161 QLIST_ENTRY(MonFdset) next;
162};
163
Markus Armbruster74358f22015-03-06 19:35:59 +0100164typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165 QObject *id;
166 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100167 /*
168 * When a client connects, we're in capabilities negotiation mode.
169 * When command qmp_capabilities succeeds, we go into command
170 * mode.
171 */
172 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100173} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200174
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100175/*
176 * To prevent flooding clients, events can be throttled. The
177 * throttling is calculated globally, rather than per-Monitor
178 * instance.
179 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200180typedef struct MonitorQAPIEventState {
181 QAPIEvent event; /* Event being tracked */
182 int64_t rate; /* Minimum time (in ns) between two events */
183 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100184 QEMUTimer *timer; /* Timer for handling delayed events */
185 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200186} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187
aliguori87127162009-03-05 23:01:29 +0000188struct Monitor {
189 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200190 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000191 int flags;
192 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400193 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200194
195 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400196 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200197 guint out_watch;
198
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
200 int mux_out;
201
aliguori731b0362009-03-05 23:01:42 +0000202 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100203 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200204 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200205 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000206 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800207 mon_cmd_t *cmd_table;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200208 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500209 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000210 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000211};
212
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300213/* QMP checker flags */
214#define QMP_ACCEPT_UNKNOWNS 1
215
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200216/* Protects mon_list, monitor_event_state. */
217static QemuMutex monitor_lock;
218
Blue Swirl72cf2d42009-09-12 07:36:22 +0000219static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400220static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400221static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000222
Wayne Xia816f8922011-10-12 11:32:41 +0800223static mon_cmd_t mon_cmds[];
224static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000225
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300226static const mon_cmd_t qmp_cmds[];
227
Markus Armbruster8631b602010-02-18 11:41:55 +0100228Monitor *cur_mon;
229Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000230
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100231static void monitor_command_cb(void *opaque, const char *cmdline,
232 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000233
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100234/**
235 * Is @mon a QMP monitor?
236 */
237static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200238{
239 return (mon->flags & MONITOR_USE_CONTROL);
240}
241
Markus Armbruster489653b2015-03-06 20:01:05 +0100242/**
243 * Is the current monitor, if any, a QMP monitor?
244 */
245bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100246{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100247 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100248}
249
Anthony Liguori7060b472011-09-02 12:34:50 -0500250void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000251{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200252 if (!mon->rs)
253 return;
254
aliguori731b0362009-03-05 23:01:42 +0000255 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
256 if (show_prompt)
257 readline_show_prompt(mon->rs);
258}
bellard6a00d602005-11-21 23:25:50 +0000259
Anthony Liguori7060b472011-09-02 12:34:50 -0500260int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
261 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000262{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100263 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000264 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
265 /* prompt is printed on return from the command handler */
266 return 0;
267 } else {
268 monitor_printf(mon, "terminal does not support password prompting\n");
269 return -ENOTTY;
270 }
aliguoribb5fc202009-03-05 23:01:15 +0000271}
272
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200273static void monitor_flush_locked(Monitor *mon);
274
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100275static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
276 void *opaque)
277{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200278 Monitor *mon = opaque;
279
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200280 qemu_mutex_lock(&mon->out_lock);
281 mon->out_watch = 0;
282 monitor_flush_locked(mon);
283 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100284 return FALSE;
285}
286
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200287/* Called with mon->out_lock held. */
288static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000289{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400291 size_t len;
292 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100293
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400294 if (mon->skip_flush) {
295 return;
296 }
297
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 buf = qstring_get_str(mon->outbuf);
299 len = qstring_get_length(mon->outbuf);
300
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200301 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200303 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
304 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400305 QDECREF(mon->outbuf);
306 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100307 return;
308 }
309 if (rc > 0) {
310 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400311 QString *tmp = qstring_from_str(buf + rc);
312 QDECREF(mon->outbuf);
313 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100314 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200315 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200316 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200317 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200318 }
bellard7e2515e2004-08-01 21:52:19 +0000319 }
320}
321
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200322void monitor_flush(Monitor *mon)
323{
324 qemu_mutex_lock(&mon->out_lock);
325 monitor_flush_locked(mon);
326 qemu_mutex_unlock(&mon->out_lock);
327}
328
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400329/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000330static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000331{
ths60fe76f2007-12-16 03:02:09 +0000332 char c;
aliguori731b0362009-03-05 23:01:42 +0000333
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200334 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000335 for(;;) {
336 c = *str++;
337 if (c == '\0')
338 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400339 if (c == '\n') {
340 qstring_append_chr(mon->outbuf, '\r');
341 }
342 qstring_append_chr(mon->outbuf, c);
343 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200344 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400345 }
bellard7e2515e2004-08-01 21:52:19 +0000346 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200347 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000348}
349
aliguori376253e2009-03-05 23:01:23 +0000350void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000351{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400352 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200354 if (!mon)
355 return;
356
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100357 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200359 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400361 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400363 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000364}
365
aliguori376253e2009-03-05 23:01:23 +0000366void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000367{
368 va_list ap;
369 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000370 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000371 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000372}
373
Stefan Weil8b7968f2010-09-23 21:28:05 +0200374static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
375 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000376{
377 va_list ap;
378 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000380 va_end(ap);
381 return 0;
382}
383
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200384static void monitor_json_emitter(Monitor *mon, const QObject *data)
385{
386 QString *json;
387
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200388 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
389 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390 assert(json != NULL);
391
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200392 qstring_append_chr(json, '\n');
393 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200394
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200395 QDECREF(json);
396}
397
Markus Armbruster710aec92015-03-06 11:28:00 +0100398static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300399{
400 QObject *obj;
401
Markus Armbruster710aec92015-03-06 11:28:00 +0100402 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
403 ErrorClass_lookup[error_get_class(err)],
404 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300405
406 return qobject_to_qdict(obj);
407}
408
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100409static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100410 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200411{
412 QDict *qmp;
413
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100414 trace_monitor_protocol_emitter(mon);
415
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100416 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200417 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300418 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200419 if (data) {
420 qobject_incref(data);
421 qdict_put_obj(qmp, "return", data);
422 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200423 /* return an empty QDict by default */
424 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426 } else {
427 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100428 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200429 }
430
Markus Armbruster74358f22015-03-06 19:35:59 +0100431 if (mon->qmp.id) {
432 qdict_put_obj(qmp, "id", mon->qmp.id);
433 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200434 }
435
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200436 monitor_json_emitter(mon, QOBJECT(qmp));
437 QDECREF(qmp);
438}
439
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200440
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200441static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442
443/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200444 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200445 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100446 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200447static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100448{
449 Monitor *mon;
450
451 trace_monitor_protocol_event_emit(event, data);
452 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100453 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100454 monitor_json_emitter(mon, data);
455 }
456 }
457}
458
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459/*
460 * Queue a new event for emission to Monitor instances,
461 * applying any rate limiting if required.
462 */
463static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200464monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 MonitorQAPIEventState *evstate;
467 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100468 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200470 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471 trace_monitor_protocol_event_queue(event,
472 data,
473 evstate->rate,
474 evstate->last,
475 now);
476
477 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200478 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200480 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 evstate->last = now;
482 } else {
483 int64_t delta = now - evstate->last;
484 if (evstate->data ||
485 delta < evstate->rate) {
486 /* If there's an existing event pending, replace
487 * it with the new event, otherwise schedule a
488 * timer for delayed emission
489 */
490 if (evstate->data) {
491 qobject_decref(evstate->data);
492 } else {
493 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100494 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200496 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 qobject_incref(evstate->data);
498 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200499 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500 evstate->last = now;
501 }
502 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200503 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100504}
505
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100506/*
507 * The callback invoked by QemuTimer when a delayed
508 * event is ready to be emitted
509 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200510static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200512 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100513 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515 trace_monitor_protocol_event_handler(evstate->event,
516 evstate->data,
517 evstate->last,
518 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200519 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100520 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200521 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 qobject_decref(evstate->data);
523 evstate->data = NULL;
524 }
525 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200526 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100527}
528
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100529/*
530 * @event: the event ID to be limited
531 * @rate: the rate limit in milliseconds
532 *
533 * Sets a rate limit on a particular event, so no
534 * more than 1 event will be emitted within @rate
535 * milliseconds
536 */
537static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200538monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200540 MonitorQAPIEventState *evstate;
541 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100542
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200543 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544
545 trace_monitor_protocol_event_throttle(event, rate);
546 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700547 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 evstate->last = 0;
550 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200551 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
552 SCALE_MS,
553 monitor_qapi_event_handler,
554 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100555}
556
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200557static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200559 /* Limit guest-triggerable events to 1 per second */
560 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200565 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100566
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200567 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200568}
569
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200570static int do_qmp_capabilities(Monitor *mon, const QDict *params,
571 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200572{
Markus Armbrusterf994b252015-03-06 19:51:51 +0100573 mon->qmp.in_command_mode = true;
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200574 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200575}
576
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100577static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200578
Wenchao Xiab01fe892013-08-27 20:38:19 +0800579static void monitor_data_init(Monitor *mon)
580{
581 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200582 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800583 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800584 /* Use *mon_cmds by default. */
585 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800586}
587
588static void monitor_data_destroy(Monitor *mon)
589{
590 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200591 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800592}
593
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200594char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
595 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200597 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200599
Wenchao Xiab01fe892013-08-27 20:38:19 +0800600 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400601 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200602
603 old_mon = cur_mon;
604 cur_mon = &hmp;
605
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200606 if (has_cpu_index) {
607 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608 if (ret < 0) {
609 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200610 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
611 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200612 goto out;
613 }
614 }
615
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100616 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200617 cur_mon = old_mon;
618
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200619 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400620 if (qstring_get_length(hmp.outbuf) > 0) {
621 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200622 } else {
623 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200625 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626
627out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800628 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200629 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200630}
631
bellard9dc39cb2004-03-14 21:38:27 +0000632static int compare_cmd(const char *name, const char *list)
633{
634 const char *p, *pstart;
635 int len;
636 len = strlen(name);
637 p = list;
638 for(;;) {
639 pstart = p;
640 p = strchr(p, '|');
641 if (!p)
642 p = pstart + strlen(pstart);
643 if ((p - pstart) == len && !memcmp(pstart, name, len))
644 return 1;
645 if (*p == '\0')
646 break;
647 p++;
648 }
649 return 0;
650}
651
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800652static int get_str(char *buf, int buf_size, const char **pp)
653{
654 const char *p;
655 char *q;
656 int c;
657
658 q = buf;
659 p = *pp;
660 while (qemu_isspace(*p)) {
661 p++;
662 }
663 if (*p == '\0') {
664 fail:
665 *q = '\0';
666 *pp = p;
667 return -1;
668 }
669 if (*p == '\"') {
670 p++;
671 while (*p != '\0' && *p != '\"') {
672 if (*p == '\\') {
673 p++;
674 c = *p++;
675 switch (c) {
676 case 'n':
677 c = '\n';
678 break;
679 case 'r':
680 c = '\r';
681 break;
682 case '\\':
683 case '\'':
684 case '\"':
685 break;
686 default:
687 qemu_printf("unsupported escape code: '\\%c'\n", c);
688 goto fail;
689 }
690 if ((q - buf) < buf_size - 1) {
691 *q++ = c;
692 }
693 } else {
694 if ((q - buf) < buf_size - 1) {
695 *q++ = *p;
696 }
697 p++;
698 }
699 }
700 if (*p != '\"') {
701 qemu_printf("unterminated string\n");
702 goto fail;
703 }
704 p++;
705 } else {
706 while (*p != '\0' && !qemu_isspace(*p)) {
707 if ((q - buf) < buf_size - 1) {
708 *q++ = *p;
709 }
710 p++;
711 }
712 }
713 *q = '\0';
714 *pp = p;
715 return 0;
716}
717
718#define MAX_ARGS 16
719
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800720static void free_cmdline_args(char **args, int nb_args)
721{
722 int i;
723
724 assert(nb_args <= MAX_ARGS);
725
726 for (i = 0; i < nb_args; i++) {
727 g_free(args[i]);
728 }
729
730}
731
732/*
733 * Parse the command line to get valid args.
734 * @cmdline: command line to be parsed.
735 * @pnb_args: location to store the number of args, must NOT be NULL.
736 * @args: location to store the args, which should be freed by caller, must
737 * NOT be NULL.
738 *
739 * Returns 0 on success, negative on failure.
740 *
741 * NOTE: this parser is an approximate form of the real command parser. Number
742 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
743 * return with failure.
744 */
745static int parse_cmdline(const char *cmdline,
746 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800747{
748 const char *p;
749 int nb_args, ret;
750 char buf[1024];
751
752 p = cmdline;
753 nb_args = 0;
754 for (;;) {
755 while (qemu_isspace(*p)) {
756 p++;
757 }
758 if (*p == '\0') {
759 break;
760 }
761 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800762 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800763 }
764 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800765 if (ret < 0) {
766 goto fail;
767 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 args[nb_args] = g_strdup(buf);
769 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800770 }
771 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800772 return 0;
773
774 fail:
775 free_cmdline_args(args, nb_args);
776 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800777}
778
Wenchao Xia66855492013-08-27 20:38:23 +0800779static void help_cmd_dump_one(Monitor *mon,
780 const mon_cmd_t *cmd,
781 char **prefix_args,
782 int prefix_args_nb)
783{
784 int i;
785
786 for (i = 0; i < prefix_args_nb; i++) {
787 monitor_printf(mon, "%s ", prefix_args[i]);
788 }
789 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
790}
791
792/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500793static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800794 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000795{
Anthony Liguoric227f092009-10-01 16:12:16 -0500796 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000797
Wenchao Xia66855492013-08-27 20:38:23 +0800798 /* No valid arg need to compare with, dump all in *cmds */
799 if (arg_index >= nb_args) {
800 for (cmd = cmds; cmd->name != NULL; cmd++) {
801 help_cmd_dump_one(mon, cmd, args, arg_index);
802 }
803 return;
804 }
805
806 /* Find one entry to dump */
807 for (cmd = cmds; cmd->name != NULL; cmd++) {
808 if (compare_cmd(args[arg_index], cmd->name)) {
809 if (cmd->sub_table) {
810 /* continue with next arg */
811 help_cmd_dump(mon, cmd->sub_table,
812 args, nb_args, arg_index + 1);
813 } else {
814 help_cmd_dump_one(mon, cmd, args, arg_index);
815 }
816 break;
817 }
bellard9dc39cb2004-03-14 21:38:27 +0000818 }
819}
820
aliguori376253e2009-03-05 23:01:23 +0000821static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000822{
Wenchao Xia66855492013-08-27 20:38:23 +0800823 char *args[MAX_ARGS];
824 int nb_args = 0;
825
826 /* 1. parse user input */
827 if (name) {
828 /* special case for log, directly dump and return */
829 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000830 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "Log items (comma separated):\n");
832 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000833 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000835 }
Wenchao Xia66855492013-08-27 20:38:23 +0800836 return;
837 }
838
839 if (parse_cmdline(name, &nb_args, args) < 0) {
840 return;
bellardf193c792004-03-21 17:06:25 +0000841 }
bellard9dc39cb2004-03-14 21:38:27 +0000842 }
Wenchao Xia66855492013-08-27 20:38:23 +0800843
844 /* 2. dump the contents according to parsed args */
845 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
846
847 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000848}
849
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300851{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300852 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300853}
854
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100855static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530856{
857 const char *tp_name = qdict_get_str(qdict, "name");
858 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000860
Lluís Vilanova14101d02014-08-25 13:20:03 +0200861 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
862 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100863 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000864 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530865}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100866
Michael Rothc45a8162011-10-02 08:44:37 -0500867#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100868static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100869{
870 const char *op = qdict_get_try_str(qdict, "op");
871 const char *arg = qdict_get_try_str(qdict, "arg");
872
873 if (!op) {
874 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
875 } else if (!strcmp(op, "on")) {
876 st_set_trace_file_enabled(true);
877 } else if (!strcmp(op, "off")) {
878 st_set_trace_file_enabled(false);
879 } else if (!strcmp(op, "flush")) {
880 st_flush_trace_buffer();
881 } else if (!strcmp(op, "set")) {
882 if (arg) {
883 st_set_trace_file(arg);
884 }
885 } else {
886 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
887 help_cmd(mon, "trace-file");
888 }
889}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530890#endif
891
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100892static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000893{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300894 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000895}
896
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300897CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000898{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300899 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200900 const mon_cmd_t *cmd;
901
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300902 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200903 info = g_malloc0(sizeof(*info));
904 info->value = g_malloc0(sizeof(*info->value));
905 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200906
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300907 info->next = cmd_list;
908 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200909 }
910
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300911 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000912}
913
Daniel P. Berrange48608532012-05-21 17:59:51 +0100914EventInfoList *qmp_query_events(Error **errp)
915{
916 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200917 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100918
Wenchao Xia75175172014-06-18 08:43:54 +0200919 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
920 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100921 assert(event_name != NULL);
922 info = g_malloc0(sizeof(*info));
923 info->value = g_malloc0(sizeof(*info->value));
924 info->value->name = g_strdup(event_name);
925
926 info->next = ev_list;
927 ev_list = info;
928 }
929
930 return ev_list;
931}
932
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300933/* set the current CPU defined by the user */
934int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000935{
Andreas Färber55e5c282012-12-17 06:18:02 +0100936 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000937
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100938 cpu = qemu_get_cpu(cpu_index);
939 if (cpu == NULL) {
940 return -1;
bellard6a00d602005-11-21 23:25:50 +0000941 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200942 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100943 return 0;
bellard6a00d602005-11-21 23:25:50 +0000944}
945
Andreas Färber9349b4f2012-03-14 01:38:32 +0100946static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000947{
aliguori731b0362009-03-05 23:01:42 +0000948 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300949 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000950 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300951 cpu_synchronize_state(cur_mon->mon_cpu);
Andreas Färbercb446ec2013-05-01 14:24:52 +0200952 return cur_mon->mon_cpu->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000953}
954
Luiz Capitulino99b77962011-10-24 10:53:44 -0200955int monitor_get_cpu_index(void)
956{
Andreas Färber55e5c282012-12-17 06:18:02 +0100957 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
958 return cpu->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200959}
960
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100961static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000962{
Andreas Färber878096e2013-05-27 01:33:50 +0200963 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100964 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000965 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +0200966 cpu = ENV_GET_CPU(env);
967 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000968}
969
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100970static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000971{
aliguori376253e2009-03-05 23:01:23 +0000972 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200973 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000974}
975
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100976static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300977{
978 dump_opcount_info((FILE *)mon, monitor_fprintf);
979}
980
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100981static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000982{
983 int i;
bellard7e2515e2004-08-01 21:52:19 +0000984 const char *str;
ths3b46e622007-09-17 08:09:54 +0000985
aliguoricde76ee2009-03-05 23:01:51 +0000986 if (!mon->rs)
987 return;
bellard7e2515e2004-08-01 21:52:19 +0000988 i = 0;
989 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000990 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000991 if (!str)
992 break;
aliguori376253e2009-03-05 23:01:23 +0000993 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000994 i++;
bellardaa455482004-04-04 13:07:25 +0000995 }
996}
997
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100998static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000999{
Andreas Färber878096e2013-05-27 01:33:50 +02001000 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01001001 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +00001002
1003 env = mon_get_cpu();
Andreas Färber878096e2013-05-27 01:33:50 +02001004 cpu = ENV_GET_CPU(env);
1005 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001006}
j_mayer76a66252007-03-07 08:32:30 +00001007
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001008static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301009{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001010 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1011 TraceEventInfoList *elem;
1012
1013 for (elem = events; elem != NULL; elem = elem->next) {
1014 monitor_printf(mon, "%s : state %u\n",
1015 elem->value->name,
1016 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1017 }
1018 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301019}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301020
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001021void qmp_client_migrate_info(const char *protocol, const char *hostname,
1022 bool has_port, int64_t port,
1023 bool has_tls_port, int64_t tls_port,
1024 bool has_cert_subject, const char *cert_subject,
1025 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001026{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001027 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001028 if (!qemu_using_spice(errp)) {
1029 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001030 }
1031
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001032 if (!has_port && !has_tls_port) {
1033 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1034 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001035 }
1036
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001037 if (qemu_spice_migrate_info(hostname,
1038 has_port ? port : -1,
1039 has_tls_port ? tls_port : -1,
1040 cert_subject)) {
1041 error_set(errp, QERR_UNDEFINED_ERROR);
1042 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001043 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001044 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001045 }
1046
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001047 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001048}
1049
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001050static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001051{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001052 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001053}
1054
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001055static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001056{
1057 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001058 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001059
bellard9307c4c2004-04-04 12:57:25 +00001060 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001061 mask = 0;
1062 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001063 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001064 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001065 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001066 return;
1067 }
1068 }
Peter Maydell24537a02013-02-11 16:41:23 +00001069 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001070}
1071
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001072static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001073{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001075 if (!option || !strcmp(option, "on")) {
1076 singlestep = 1;
1077 } else if (!strcmp(option, "off")) {
1078 singlestep = 0;
1079 } else {
1080 monitor_printf(mon, "unexpected option %s\n", option);
1081 }
1082}
1083
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001084static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001085{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001086 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001087 if (!device)
1088 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1089 if (gdbserver_start(device) < 0) {
1090 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1091 device);
1092 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001093 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001094 } else {
aliguori59030a82009-04-05 18:43:41 +00001095 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1096 device);
bellard8a7ddc32004-03-31 19:00:16 +00001097 }
1098}
1099
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001100static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001101{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001102 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001103 if (select_watchdog_action(action) == -1) {
1104 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1105 }
1106}
1107
aliguori376253e2009-03-05 23:01:23 +00001108static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001109{
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001111 switch(c) {
1112 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001114 break;
1115 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001117 break;
1118 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001120 break;
1121 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001123 break;
1124 default:
1125 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001126 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001127 } else {
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001129 }
1130 break;
1131 }
aliguori376253e2009-03-05 23:01:23 +00001132 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001133}
1134
aliguori376253e2009-03-05 23:01:23 +00001135static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001136 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001137{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001138 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001139 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001140 uint8_t buf[16];
1141 uint64_t v;
1142
1143 if (format == 'i') {
1144 int flags;
1145 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001146 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001147#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001148 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001149 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001150 } else if (wsize == 4) {
1151 flags = 0;
1152 } else {
bellard6a15fd12006-04-12 21:07:07 +00001153 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001154 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001155 if (env) {
1156#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001157 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001158 (env->segs[R_CS].flags & DESC_L_MASK))
1159 flags = 2;
1160 else
1161#endif
1162 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1163 flags = 1;
1164 }
bellard4c27ba22004-04-25 18:05:08 +00001165 }
1166#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001167#ifdef TARGET_PPC
1168 flags = msr_le << 16;
1169 flags |= env->bfd_mach;
1170#endif
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001172 return;
1173 }
1174
1175 len = wsize * count;
1176 if (wsize == 1)
1177 line_size = 8;
1178 else
1179 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001180 max_digits = 0;
1181
1182 switch(format) {
1183 case 'o':
1184 max_digits = (wsize * 8 + 2) / 3;
1185 break;
1186 default:
1187 case 'x':
1188 max_digits = (wsize * 8) / 4;
1189 break;
1190 case 'u':
1191 case 'd':
1192 max_digits = (wsize * 8 * 10 + 32) / 33;
1193 break;
1194 case 'c':
1195 wsize = 1;
1196 break;
1197 }
1198
1199 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001200 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001202 else
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001204 l = len;
1205 if (l > line_size)
1206 l = line_size;
1207 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001208 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001209 } else {
bellard6a00d602005-11-21 23:25:50 +00001210 env = mon_get_cpu();
Andreas Färberf17ec442013-06-29 19:40:58 +02001211 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001212 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001213 break;
1214 }
bellard9307c4c2004-04-04 12:57:25 +00001215 }
ths5fafdf22007-09-16 21:08:06 +00001216 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001217 while (i < l) {
1218 switch(wsize) {
1219 default:
1220 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001221 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001222 break;
1223 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001224 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001225 break;
1226 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001227 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001230 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 }
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001234 switch(format) {
1235 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001240 break;
1241 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001243 break;
1244 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001246 break;
1247 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001249 break;
1250 }
1251 i += wsize;
1252 }
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001254 addr += l;
1255 len -= l;
1256 }
1257}
1258
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001259static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001260{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001261 int count = qdict_get_int(qdict, "count");
1262 int format = qdict_get_int(qdict, "format");
1263 int size = qdict_get_int(qdict, "size");
1264 target_long addr = qdict_get_int(qdict, "addr");
1265
aliguori376253e2009-03-05 23:01:23 +00001266 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001267}
1268
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001269static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001270{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001271 int count = qdict_get_int(qdict, "count");
1272 int format = qdict_get_int(qdict, "format");
1273 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001274 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001275
aliguori376253e2009-03-05 23:01:23 +00001276 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001277}
1278
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001279static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001280{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001282 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001283
bellard9307c4c2004-04-04 12:57:25 +00001284 switch(format) {
1285 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001286 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001289 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001290 break;
1291 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001292 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 default:
1295 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001296 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001297 break;
1298 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001299 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001300 break;
1301 }
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001303}
1304
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001305static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001306{
1307 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001308 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001309 uint32_t start = qdict_get_int(qdict, "start");
1310 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001311
1312 sum = 0;
1313 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001314 uint8_t val = address_space_ldub(&address_space_memory, addr,
1315 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001316 /* BSD sum algorithm ('sum' Unix command) */
1317 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001318 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001319 }
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001321}
1322
bellard13224a82006-07-14 22:03:35 +00001323static int mouse_button_state;
1324
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001325static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001326{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001327 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001328 const char *dx_str = qdict_get_str(qdict, "dx_str");
1329 const char *dy_str = qdict_get_str(qdict, "dy_str");
1330 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001331
bellard13224a82006-07-14 22:03:35 +00001332 dx = strtol(dx_str, NULL, 0);
1333 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001334 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1335 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1336
1337 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001338 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001339 if (dz != 0) {
1340 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1341 qemu_input_queue_btn(NULL, button, true);
1342 qemu_input_event_sync();
1343 qemu_input_queue_btn(NULL, button, false);
1344 }
1345 }
1346 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001347}
1348
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001349static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001350{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001351 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1352 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1353 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1354 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1355 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001356 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001357
1358 if (mouse_button_state == button_state) {
1359 return;
1360 }
1361 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1362 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001363 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001364}
1365
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001366static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001367{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001368 int size = qdict_get_int(qdict, "size");
1369 int addr = qdict_get_int(qdict, "addr");
1370 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001371 uint32_t val;
1372 int suffix;
1373
1374 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001375 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001376 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001377 addr++;
1378 }
1379 addr &= 0xffff;
1380
1381 switch(size) {
1382 default:
1383 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001384 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001385 suffix = 'b';
1386 break;
1387 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001388 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001389 suffix = 'w';
1390 break;
1391 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001392 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001393 suffix = 'l';
1394 break;
1395 }
aliguori376253e2009-03-05 23:01:23 +00001396 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1397 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001398}
bellarda3a91a32004-06-04 11:06:21 +00001399
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001400static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001401{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001402 int size = qdict_get_int(qdict, "size");
1403 int addr = qdict_get_int(qdict, "addr");
1404 int val = qdict_get_int(qdict, "val");
1405
Jan Kiszkaf1147842009-07-14 10:20:11 +02001406 addr &= IOPORTS_MASK;
1407
1408 switch (size) {
1409 default:
1410 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001411 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001412 break;
1413 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001414 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001415 break;
1416 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001417 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001418 break;
1419 }
1420}
1421
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001422static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001423{
Gongleif1839932014-12-03 18:20:58 +00001424 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001425 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001426
Gongleif1839932014-12-03 18:20:58 +00001427 qemu_boot_set(bootdevice, &local_err);
1428 if (local_err) {
1429 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1430 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001431 } else {
Gongleif1839932014-12-03 18:20:58 +00001432 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001433 }
1434}
1435
bellardb86bda52004-09-18 19:32:46 +00001436#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001437static void print_pte(Monitor *mon, hwaddr addr,
1438 hwaddr pte,
1439 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001440{
Blue Swirld65aaf32010-12-11 18:56:24 +00001441#ifdef TARGET_X86_64
1442 if (addr & (1ULL << 47)) {
1443 addr |= -1LL << 48;
1444 }
1445#endif
1446 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1447 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001448 addr,
1449 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001450 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001451 pte & PG_GLOBAL_MASK ? 'G' : '-',
1452 pte & PG_PSE_MASK ? 'P' : '-',
1453 pte & PG_DIRTY_MASK ? 'D' : '-',
1454 pte & PG_ACCESSED_MASK ? 'A' : '-',
1455 pte & PG_PCD_MASK ? 'C' : '-',
1456 pte & PG_PWT_MASK ? 'T' : '-',
1457 pte & PG_USER_MASK ? 'U' : '-',
1458 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001459}
1460
Andreas Färber9349b4f2012-03-14 01:38:32 +01001461static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001462{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001463 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001464 uint32_t pgd, pde, pte;
1465
bellardb86bda52004-09-18 19:32:46 +00001466 pgd = env->cr[3] & ~0xfff;
1467 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001468 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001469 pde = le32_to_cpu(pde);
1470 if (pde & PG_PRESENT_MASK) {
1471 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001472 /* 4M pages */
1473 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001474 } else {
1475 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001476 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001477 pte = le32_to_cpu(pte);
1478 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001479 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001480 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001481 ~0xfff);
1482 }
1483 }
1484 }
1485 }
1486 }
1487}
1488
Andreas Färber9349b4f2012-03-14 01:38:32 +01001489static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001490{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001491 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001492 uint64_t pdpe, pde, pte;
1493 uint64_t pdp_addr, pd_addr, pt_addr;
1494
1495 pdp_addr = env->cr[3] & ~0x1f;
1496 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001497 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001498 pdpe = le64_to_cpu(pdpe);
1499 if (pdpe & PG_PRESENT_MASK) {
1500 pd_addr = pdpe & 0x3fffffffff000ULL;
1501 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001502 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001503 pde = le64_to_cpu(pde);
1504 if (pde & PG_PRESENT_MASK) {
1505 if (pde & PG_PSE_MASK) {
1506 /* 2M pages with PAE, CR4.PSE is ignored */
1507 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001508 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001509 } else {
1510 pt_addr = pde & 0x3fffffffff000ULL;
1511 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001512 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001513 pte = le64_to_cpu(pte);
1514 if (pte & PG_PRESENT_MASK) {
1515 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1516 + (l3 << 12),
1517 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001518 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001519 }
1520 }
1521 }
1522 }
1523 }
1524 }
1525 }
1526}
1527
1528#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001529static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001530{
1531 uint64_t l1, l2, l3, l4;
1532 uint64_t pml4e, pdpe, pde, pte;
1533 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1534
1535 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1536 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001537 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001538 pml4e = le64_to_cpu(pml4e);
1539 if (pml4e & PG_PRESENT_MASK) {
1540 pdp_addr = pml4e & 0x3fffffffff000ULL;
1541 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001542 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001543 pdpe = le64_to_cpu(pdpe);
1544 if (pdpe & PG_PRESENT_MASK) {
1545 if (pdpe & PG_PSE_MASK) {
1546 /* 1G pages, CR4.PSE is ignored */
1547 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1548 0x3ffffc0000000ULL);
1549 } else {
1550 pd_addr = pdpe & 0x3fffffffff000ULL;
1551 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001552 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001553 pde = le64_to_cpu(pde);
1554 if (pde & PG_PRESENT_MASK) {
1555 if (pde & PG_PSE_MASK) {
1556 /* 2M pages, CR4.PSE is ignored */
1557 print_pte(mon, (l1 << 39) + (l2 << 30) +
1558 (l3 << 21), pde,
1559 0x3ffffffe00000ULL);
1560 } else {
1561 pt_addr = pde & 0x3fffffffff000ULL;
1562 for (l4 = 0; l4 < 512; l4++) {
1563 cpu_physical_memory_read(pt_addr
1564 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001565 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001566 pte = le64_to_cpu(pte);
1567 if (pte & PG_PRESENT_MASK) {
1568 print_pte(mon, (l1 << 39) +
1569 (l2 << 30) +
1570 (l3 << 21) + (l4 << 12),
1571 pte & ~PG_PSE_MASK,
1572 0x3fffffffff000ULL);
1573 }
1574 }
1575 }
1576 }
1577 }
1578 }
1579 }
1580 }
1581 }
1582 }
1583}
1584#endif
1585
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001586static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001587{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001588 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001589
1590 env = mon_get_cpu();
1591
1592 if (!(env->cr[0] & CR0_PG_MASK)) {
1593 monitor_printf(mon, "PG disabled\n");
1594 return;
1595 }
1596 if (env->cr[4] & CR4_PAE_MASK) {
1597#ifdef TARGET_X86_64
1598 if (env->hflags & HF_LMA_MASK) {
1599 tlb_info_64(mon, env);
1600 } else
1601#endif
1602 {
1603 tlb_info_pae32(mon, env);
1604 }
1605 } else {
1606 tlb_info_32(mon, env);
1607 }
1608}
1609
Avi Kivitya8170e52012-10-23 12:30:10 +02001610static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001611 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001612 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001613{
bellard9746b152004-11-11 18:30:24 +00001614 int prot1;
1615 prot1 = *plast_prot;
1616 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001617 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001618 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1619 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001620 *pstart, end, end - *pstart,
1621 prot1 & PG_USER_MASK ? 'u' : '-',
1622 'r',
1623 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001624 }
1625 if (prot != 0)
1626 *pstart = end;
1627 else
1628 *pstart = -1;
1629 *plast_prot = prot;
1630 }
1631}
1632
Andreas Färber9349b4f2012-03-14 01:38:32 +01001633static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001634{
Austin Clementsb49ca722011-08-14 23:19:21 -04001635 unsigned int l1, l2;
1636 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001637 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001638 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001639
bellardb86bda52004-09-18 19:32:46 +00001640 pgd = env->cr[3] & ~0xfff;
1641 last_prot = 0;
1642 start = -1;
1643 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001644 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001645 pde = le32_to_cpu(pde);
1646 end = l1 << 22;
1647 if (pde & PG_PRESENT_MASK) {
1648 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1649 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001650 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001651 } else {
1652 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001653 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001654 pte = le32_to_cpu(pte);
1655 end = (l1 << 22) + (l2 << 12);
1656 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001657 prot = pte & pde &
1658 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001659 } else {
1660 prot = 0;
1661 }
aliguori376253e2009-03-05 23:01:23 +00001662 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001663 }
1664 }
1665 } else {
1666 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001667 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001668 }
1669 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001670 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001671 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001672}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001673
Andreas Färber9349b4f2012-03-14 01:38:32 +01001674static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001675{
Austin Clementsb49ca722011-08-14 23:19:21 -04001676 unsigned int l1, l2, l3;
1677 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001678 uint64_t pdpe, pde, pte;
1679 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001680 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001681
1682 pdp_addr = env->cr[3] & ~0x1f;
1683 last_prot = 0;
1684 start = -1;
1685 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001686 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001687 pdpe = le64_to_cpu(pdpe);
1688 end = l1 << 30;
1689 if (pdpe & PG_PRESENT_MASK) {
1690 pd_addr = pdpe & 0x3fffffffff000ULL;
1691 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001692 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001693 pde = le64_to_cpu(pde);
1694 end = (l1 << 30) + (l2 << 21);
1695 if (pde & PG_PRESENT_MASK) {
1696 if (pde & PG_PSE_MASK) {
1697 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1698 PG_PRESENT_MASK);
1699 mem_print(mon, &start, &last_prot, end, prot);
1700 } else {
1701 pt_addr = pde & 0x3fffffffff000ULL;
1702 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001703 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001704 pte = le64_to_cpu(pte);
1705 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1706 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001707 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1708 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001709 } else {
1710 prot = 0;
1711 }
1712 mem_print(mon, &start, &last_prot, end, prot);
1713 }
1714 }
1715 } else {
1716 prot = 0;
1717 mem_print(mon, &start, &last_prot, end, prot);
1718 }
1719 }
1720 } else {
1721 prot = 0;
1722 mem_print(mon, &start, &last_prot, end, prot);
1723 }
1724 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001725 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001726 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001727}
1728
1729
1730#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001731static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001732{
1733 int prot, last_prot;
1734 uint64_t l1, l2, l3, l4;
1735 uint64_t pml4e, pdpe, pde, pte;
1736 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1737
1738 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1739 last_prot = 0;
1740 start = -1;
1741 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001742 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001743 pml4e = le64_to_cpu(pml4e);
1744 end = l1 << 39;
1745 if (pml4e & PG_PRESENT_MASK) {
1746 pdp_addr = pml4e & 0x3fffffffff000ULL;
1747 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001748 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001749 pdpe = le64_to_cpu(pdpe);
1750 end = (l1 << 39) + (l2 << 30);
1751 if (pdpe & PG_PRESENT_MASK) {
1752 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001753 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1754 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001755 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001756 mem_print(mon, &start, &last_prot, end, prot);
1757 } else {
1758 pd_addr = pdpe & 0x3fffffffff000ULL;
1759 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001760 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001761 pde = le64_to_cpu(pde);
1762 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1763 if (pde & PG_PRESENT_MASK) {
1764 if (pde & PG_PSE_MASK) {
1765 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1766 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001767 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001768 mem_print(mon, &start, &last_prot, end, prot);
1769 } else {
1770 pt_addr = pde & 0x3fffffffff000ULL;
1771 for (l4 = 0; l4 < 512; l4++) {
1772 cpu_physical_memory_read(pt_addr
1773 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001774 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001775 pte = le64_to_cpu(pte);
1776 end = (l1 << 39) + (l2 << 30) +
1777 (l3 << 21) + (l4 << 12);
1778 if (pte & PG_PRESENT_MASK) {
1779 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1780 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001781 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001782 } else {
1783 prot = 0;
1784 }
1785 mem_print(mon, &start, &last_prot, end, prot);
1786 }
1787 }
1788 } else {
1789 prot = 0;
1790 mem_print(mon, &start, &last_prot, end, prot);
1791 }
1792 }
1793 }
1794 } else {
1795 prot = 0;
1796 mem_print(mon, &start, &last_prot, end, prot);
1797 }
1798 }
1799 } else {
1800 prot = 0;
1801 mem_print(mon, &start, &last_prot, end, prot);
1802 }
1803 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001804 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001805 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001806}
1807#endif
1808
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001809static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001810{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001811 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001812
1813 env = mon_get_cpu();
1814
1815 if (!(env->cr[0] & CR0_PG_MASK)) {
1816 monitor_printf(mon, "PG disabled\n");
1817 return;
1818 }
1819 if (env->cr[4] & CR4_PAE_MASK) {
1820#ifdef TARGET_X86_64
1821 if (env->hflags & HF_LMA_MASK) {
1822 mem_info_64(mon, env);
1823 } else
1824#endif
1825 {
1826 mem_info_pae32(mon, env);
1827 }
1828 } else {
1829 mem_info_32(mon, env);
1830 }
1831}
bellardb86bda52004-09-18 19:32:46 +00001832#endif
1833
aurel327c664e22009-03-03 06:12:22 +00001834#if defined(TARGET_SH4)
1835
aliguori376253e2009-03-05 23:01:23 +00001836static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001837{
aliguori376253e2009-03-05 23:01:23 +00001838 monitor_printf(mon, " tlb%i:\t"
1839 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1840 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1841 "dirty=%hhu writethrough=%hhu\n",
1842 idx,
1843 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1844 tlb->v, tlb->sh, tlb->c, tlb->pr,
1845 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001846}
1847
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001848static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001849{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001850 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00001851 int i;
1852
aliguori376253e2009-03-05 23:01:23 +00001853 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001854 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001855 print_tlb (mon, i, &env->itlb[i]);
1856 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001857 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001858 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001859}
1860
1861#endif
1862
Max Filippov692f7372012-01-07 20:02:40 +04001863#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001864static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001865{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001866 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00001867
1868 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1869}
1870#endif
1871
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001872static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001873{
1874 mtree_info((fprintf_function)monitor_printf, mon);
1875}
1876
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001877static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001878{
aliguorib28b6232009-04-22 20:20:29 +00001879 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001880 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001881 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001882
zhanghailiang5b009e42014-11-04 19:49:30 +08001883 node_mem = g_new0(uint64_t, nb_numa_nodes);
1884 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001885 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1886 for (i = 0; i < nb_numa_nodes; i++) {
1887 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001888 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001889 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001890 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001891 }
1892 }
1893 monitor_printf(mon, "\n");
1894 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001895 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001896 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001897 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001898}
1899
bellard5f1ce942006-02-08 22:40:15 +00001900#ifdef CONFIG_PROFILER
1901
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001902int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001903int64_t dev_time;
1904
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001905static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001906{
aliguori376253e2009-03-05 23:01:23 +00001907 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001908 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001909 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001910 tcg_time, tcg_time / (double)get_ticks_per_sec());
1911 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001912 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001913}
1914#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001915static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001916{
aliguori376253e2009-03-05 23:01:23 +00001917 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001918}
1919#endif
1920
bellardec36b692006-07-16 18:57:03 +00001921/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001922static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001923
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001924static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001925{
1926 int i;
1927 CaptureState *s;
1928
1929 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001930 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001931 s->ops.info (s->opaque);
1932 }
1933}
1934
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001935static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001936{
1937 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001938 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001939 CaptureState *s;
1940
1941 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1942 if (i == n) {
1943 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001944 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001945 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001946 return;
1947 }
1948 }
1949}
1950
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001951static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001952{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001953 const char *path = qdict_get_str(qdict, "path");
1954 int has_freq = qdict_haskey(qdict, "freq");
1955 int freq = qdict_get_try_int(qdict, "freq", -1);
1956 int has_bits = qdict_haskey(qdict, "bits");
1957 int bits = qdict_get_try_int(qdict, "bits", -1);
1958 int has_channels = qdict_haskey(qdict, "nchannels");
1959 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001960 CaptureState *s;
1961
Anthony Liguori7267c092011-08-20 22:09:37 -05001962 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001963
1964 freq = has_freq ? freq : 44100;
1965 bits = has_bits ? bits : 16;
1966 nchannels = has_channels ? nchannels : 2;
1967
1968 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001969 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001970 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001971 return;
bellardec36b692006-07-16 18:57:03 +00001972 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001973 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001974}
bellardec36b692006-07-16 18:57:03 +00001975
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001976static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001977{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001978 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001979
aliguori76655d62009-03-06 20:27:37 +00001980 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001981 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001982 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001983 return acl;
1984}
aliguori76655d62009-03-06 20:27:37 +00001985
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001986static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001987{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001988 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001989 qemu_acl *acl = find_acl(mon, aclname);
1990 qemu_acl_entry *entry;
1991 int i = 0;
1992
1993 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001994 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001995 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001996 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001997 i++;
1998 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001999 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002000 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002001 }
2002}
2003
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002004static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002005{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002006 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002007 qemu_acl *acl = find_acl(mon, aclname);
2008
2009 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002010 qemu_acl_reset(acl);
2011 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002012 }
2013}
aliguori76655d62009-03-06 20:27:37 +00002014
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002015static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002016{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002017 const char *aclname = qdict_get_str(qdict, "aclname");
2018 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002019 qemu_acl *acl = find_acl(mon, aclname);
2020
2021 if (acl) {
2022 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002023 acl->defaultDeny = 0;
2024 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002025 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002026 acl->defaultDeny = 1;
2027 monitor_printf(mon, "acl: policy set to 'deny'\n");
2028 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002029 monitor_printf(mon, "acl: unknown policy '%s', "
2030 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002031 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002032 }
2033}
aliguori76655d62009-03-06 20:27:37 +00002034
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002035static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002036{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002037 const char *aclname = qdict_get_str(qdict, "aclname");
2038 const char *match = qdict_get_str(qdict, "match");
2039 const char *policy = qdict_get_str(qdict, "policy");
2040 int has_index = qdict_haskey(qdict, "index");
2041 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002042 qemu_acl *acl = find_acl(mon, aclname);
2043 int deny, ret;
2044
2045 if (acl) {
2046 if (strcmp(policy, "allow") == 0) {
2047 deny = 0;
2048 } else if (strcmp(policy, "deny") == 0) {
2049 deny = 1;
2050 } else {
2051 monitor_printf(mon, "acl: unknown policy '%s', "
2052 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002053 return;
2054 }
aliguori28a76be2009-03-06 20:27:40 +00002055 if (has_index)
2056 ret = qemu_acl_insert(acl, deny, match, index);
2057 else
2058 ret = qemu_acl_append(acl, deny, match);
2059 if (ret < 0)
2060 monitor_printf(mon, "acl: unable to add acl entry\n");
2061 else
2062 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002063 }
2064}
aliguori76655d62009-03-06 20:27:37 +00002065
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002066static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002067{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002068 const char *aclname = qdict_get_str(qdict, "aclname");
2069 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002070 qemu_acl *acl = find_acl(mon, aclname);
2071 int ret;
aliguori76655d62009-03-06 20:27:37 +00002072
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002073 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002074 ret = qemu_acl_remove(acl, match);
2075 if (ret < 0)
2076 monitor_printf(mon, "acl: no matching acl entry\n");
2077 else
2078 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002079 }
2080}
2081
Huang Ying79c4f6b2009-06-23 10:05:14 +08002082#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002083static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002084{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002085 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002086 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002087 int cpu_index = qdict_get_int(qdict, "cpu_index");
2088 int bank = qdict_get_int(qdict, "bank");
2089 uint64_t status = qdict_get_int(qdict, "status");
2090 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2091 uint64_t addr = qdict_get_int(qdict, "addr");
2092 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002093 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002094
Jan Kiszka747461c2011-03-02 08:56:10 +01002095 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2096 flags |= MCE_INJECT_BROADCAST;
2097 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002098 cs = qemu_get_cpu(cpu_index);
2099 if (cs != NULL) {
2100 cpu = X86_CPU(cs);
2101 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2102 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002103 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002104}
2105#endif
2106
Corey Bryant208c9d12012-06-22 14:36:09 -04002107void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002108{
Anthony Liguoric227f092009-10-01 16:12:16 -05002109 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002110 int fd;
2111
Corey Bryant208c9d12012-06-22 14:36:09 -04002112 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002113 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002114 error_set(errp, QERR_FD_NOT_SUPPLIED);
2115 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002116 }
2117
2118 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002119 close(fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002120 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2121 "a name not starting with a digit");
2122 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002123 }
2124
Corey Bryant208c9d12012-06-22 14:36:09 -04002125 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002126 if (strcmp(monfd->name, fdname) != 0) {
2127 continue;
2128 }
2129
2130 close(monfd->fd);
2131 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002132 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002133 }
2134
Anthony Liguori7267c092011-08-20 22:09:37 -05002135 monfd = g_malloc0(sizeof(mon_fd_t));
2136 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002137 monfd->fd = fd;
2138
Corey Bryant208c9d12012-06-22 14:36:09 -04002139 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002140}
2141
Corey Bryant208c9d12012-06-22 14:36:09 -04002142void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143{
Anthony Liguoric227f092009-10-01 16:12:16 -05002144 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002145
Corey Bryant208c9d12012-06-22 14:36:09 -04002146 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002147 if (strcmp(monfd->name, fdname) != 0) {
2148 continue;
2149 }
2150
Blue Swirl72cf2d42009-09-12 07:36:22 +00002151 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002152 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002153 g_free(monfd->name);
2154 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002155 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002156 }
2157
Corey Bryant208c9d12012-06-22 14:36:09 -04002158 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002159}
2160
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002161static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002162{
Luiz Capitulino13548692011-07-29 15:36:43 -03002163 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002164 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002165
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002166 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002167
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002168 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002169 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002170 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002171}
2172
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002173int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002174{
Anthony Liguoric227f092009-10-01 16:12:16 -05002175 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002176
Blue Swirl72cf2d42009-09-12 07:36:22 +00002177 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002178 int fd;
2179
2180 if (strcmp(monfd->name, fdname) != 0) {
2181 continue;
2182 }
2183
2184 fd = monfd->fd;
2185
2186 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002187 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002188 g_free(monfd->name);
2189 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002190
2191 return fd;
2192 }
2193
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002194 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002195 return -1;
2196}
2197
Corey Bryantba1c0482012-08-14 16:43:43 -04002198static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2199{
2200 MonFdsetFd *mon_fdset_fd;
2201 MonFdsetFd *mon_fdset_fd_next;
2202
2203 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002204 if ((mon_fdset_fd->removed ||
2205 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2206 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002207 close(mon_fdset_fd->fd);
2208 g_free(mon_fdset_fd->opaque);
2209 QLIST_REMOVE(mon_fdset_fd, next);
2210 g_free(mon_fdset_fd);
2211 }
2212 }
2213
Corey Bryantadb696f2012-08-14 16:43:47 -04002214 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002215 QLIST_REMOVE(mon_fdset, next);
2216 g_free(mon_fdset);
2217 }
2218}
2219
Corey Bryantefb87c12012-08-14 16:43:48 -04002220static void monitor_fdsets_cleanup(void)
2221{
2222 MonFdset *mon_fdset;
2223 MonFdset *mon_fdset_next;
2224
2225 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2226 monitor_fdset_cleanup(mon_fdset);
2227 }
2228}
2229
Corey Bryantba1c0482012-08-14 16:43:43 -04002230AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2231 const char *opaque, Error **errp)
2232{
2233 int fd;
2234 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002235 AddfdInfo *fdinfo;
2236
2237 fd = qemu_chr_fe_get_msgfd(mon->chr);
2238 if (fd == -1) {
2239 error_set(errp, QERR_FD_NOT_SUPPLIED);
2240 goto error;
2241 }
2242
Corey Bryante446f702012-10-18 15:19:32 -04002243 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2244 has_opaque, opaque, errp);
2245 if (fdinfo) {
2246 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002247 }
2248
Corey Bryantba1c0482012-08-14 16:43:43 -04002249error:
2250 if (fd != -1) {
2251 close(fd);
2252 }
2253 return NULL;
2254}
2255
2256void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2257{
2258 MonFdset *mon_fdset;
2259 MonFdsetFd *mon_fdset_fd;
2260 char fd_str[60];
2261
2262 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2263 if (mon_fdset->id != fdset_id) {
2264 continue;
2265 }
2266 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2267 if (has_fd) {
2268 if (mon_fdset_fd->fd != fd) {
2269 continue;
2270 }
2271 mon_fdset_fd->removed = true;
2272 break;
2273 } else {
2274 mon_fdset_fd->removed = true;
2275 }
2276 }
2277 if (has_fd && !mon_fdset_fd) {
2278 goto error;
2279 }
2280 monitor_fdset_cleanup(mon_fdset);
2281 return;
2282 }
2283
2284error:
2285 if (has_fd) {
2286 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2287 fdset_id, fd);
2288 } else {
2289 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2290 }
2291 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2292}
2293
2294FdsetInfoList *qmp_query_fdsets(Error **errp)
2295{
2296 MonFdset *mon_fdset;
2297 MonFdsetFd *mon_fdset_fd;
2298 FdsetInfoList *fdset_list = NULL;
2299
2300 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2301 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2302 FdsetFdInfoList *fdsetfd_list = NULL;
2303
2304 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2305 fdset_info->value->fdset_id = mon_fdset->id;
2306
2307 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2308 FdsetFdInfoList *fdsetfd_info;
2309
2310 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2311 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2312 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2313 if (mon_fdset_fd->opaque) {
2314 fdsetfd_info->value->has_opaque = true;
2315 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2316 } else {
2317 fdsetfd_info->value->has_opaque = false;
2318 }
2319
2320 fdsetfd_info->next = fdsetfd_list;
2321 fdsetfd_list = fdsetfd_info;
2322 }
2323
2324 fdset_info->value->fds = fdsetfd_list;
2325
2326 fdset_info->next = fdset_list;
2327 fdset_list = fdset_info;
2328 }
2329
2330 return fdset_list;
2331}
2332
Corey Bryante446f702012-10-18 15:19:32 -04002333AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2334 bool has_opaque, const char *opaque,
2335 Error **errp)
2336{
2337 MonFdset *mon_fdset = NULL;
2338 MonFdsetFd *mon_fdset_fd;
2339 AddfdInfo *fdinfo;
2340
2341 if (has_fdset_id) {
2342 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2343 /* Break if match found or match impossible due to ordering by ID */
2344 if (fdset_id <= mon_fdset->id) {
2345 if (fdset_id < mon_fdset->id) {
2346 mon_fdset = NULL;
2347 }
2348 break;
2349 }
2350 }
2351 }
2352
2353 if (mon_fdset == NULL) {
2354 int64_t fdset_id_prev = -1;
2355 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2356
2357 if (has_fdset_id) {
2358 if (fdset_id < 0) {
2359 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2360 "a non-negative value");
2361 return NULL;
2362 }
2363 /* Use specified fdset ID */
2364 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2365 mon_fdset_cur = mon_fdset;
2366 if (fdset_id < mon_fdset_cur->id) {
2367 break;
2368 }
2369 }
2370 } else {
2371 /* Use first available fdset ID */
2372 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2373 mon_fdset_cur = mon_fdset;
2374 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2375 fdset_id_prev = mon_fdset_cur->id;
2376 continue;
2377 }
2378 break;
2379 }
2380 }
2381
2382 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2383 if (has_fdset_id) {
2384 mon_fdset->id = fdset_id;
2385 } else {
2386 mon_fdset->id = fdset_id_prev + 1;
2387 }
2388
2389 /* The fdset list is ordered by fdset ID */
2390 if (!mon_fdset_cur) {
2391 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2392 } else if (mon_fdset->id < mon_fdset_cur->id) {
2393 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2394 } else {
2395 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2396 }
2397 }
2398
2399 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2400 mon_fdset_fd->fd = fd;
2401 mon_fdset_fd->removed = false;
2402 if (has_opaque) {
2403 mon_fdset_fd->opaque = g_strdup(opaque);
2404 }
2405 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2406
2407 fdinfo = g_malloc0(sizeof(*fdinfo));
2408 fdinfo->fdset_id = mon_fdset->id;
2409 fdinfo->fd = mon_fdset_fd->fd;
2410
2411 return fdinfo;
2412}
2413
Corey Bryantadb696f2012-08-14 16:43:47 -04002414int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2415{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002416#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002417 MonFdset *mon_fdset;
2418 MonFdsetFd *mon_fdset_fd;
2419 int mon_fd_flags;
2420
Corey Bryantadb696f2012-08-14 16:43:47 -04002421 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2422 if (mon_fdset->id != fdset_id) {
2423 continue;
2424 }
2425 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2426 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2427 if (mon_fd_flags == -1) {
2428 return -1;
2429 }
2430
2431 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2432 return mon_fdset_fd->fd;
2433 }
2434 }
2435 errno = EACCES;
2436 return -1;
2437 }
2438#endif
2439
2440 errno = ENOENT;
2441 return -1;
2442}
2443
2444int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2445{
2446 MonFdset *mon_fdset;
2447 MonFdsetFd *mon_fdset_fd_dup;
2448
2449 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2450 if (mon_fdset->id != fdset_id) {
2451 continue;
2452 }
2453 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2454 if (mon_fdset_fd_dup->fd == dup_fd) {
2455 return -1;
2456 }
2457 }
2458 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2459 mon_fdset_fd_dup->fd = dup_fd;
2460 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2461 return 0;
2462 }
2463 return -1;
2464}
2465
2466static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2467{
2468 MonFdset *mon_fdset;
2469 MonFdsetFd *mon_fdset_fd_dup;
2470
2471 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2472 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2473 if (mon_fdset_fd_dup->fd == dup_fd) {
2474 if (remove) {
2475 QLIST_REMOVE(mon_fdset_fd_dup, next);
2476 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2477 monitor_fdset_cleanup(mon_fdset);
2478 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002479 return -1;
2480 } else {
2481 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002482 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002483 }
2484 }
2485 }
2486 return -1;
2487}
2488
2489int monitor_fdset_dup_fd_find(int dup_fd)
2490{
2491 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2492}
2493
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002494void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002495{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002496 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002497}
2498
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002499int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002500{
2501 int fd;
2502 Error *local_err = NULL;
2503
2504 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002505 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002506 } else {
2507 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002508 if (fd == -1) {
2509 error_setg(&local_err, "Invalid file descriptor number '%s'",
2510 fdname);
2511 }
2512 }
2513 if (local_err) {
2514 error_propagate(errp, local_err);
2515 assert(fd == -1);
2516 } else {
2517 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002518 }
2519
2520 return fd;
2521}
2522
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002523/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002524static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 {
2526 .name = "version",
2527 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 .params = "",
2529 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002530 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 },
2532 {
2533 .name = "network",
2534 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002535 .params = "",
2536 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002537 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 },
2539 {
2540 .name = "chardev",
2541 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002542 .params = "",
2543 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002544 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 },
2546 {
2547 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002548 .args_type = "nodes:-n,verbose:-v,device:B?",
2549 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002550 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002551 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002552 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 },
2554 {
2555 .name = "blockstats",
2556 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 .params = "",
2558 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002559 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 },
2561 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002562 .name = "block-jobs",
2563 .args_type = "",
2564 .params = "",
2565 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002566 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002567 },
2568 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 .name = "registers",
2570 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 .params = "",
2572 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002573 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 },
2575 {
2576 .name = "cpus",
2577 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002578 .params = "",
2579 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002580 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002581 },
2582 {
2583 .name = "history",
2584 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 .params = "",
2586 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002587 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002589#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2590 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 {
2592 .name = "irq",
2593 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 .params = "",
2595 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002596#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002597 .mhandler.cmd = sun4m_hmp_info_irq,
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_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002600#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002601 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002602#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002603 },
2604 {
2605 .name = "pic",
2606 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 .params = "",
2608 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002609#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002610 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002611#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002612 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002613#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002614 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002615#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002617#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 {
2619 .name = "pci",
2620 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 .params = "",
2622 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002623 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002625#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002626 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 {
2628 .name = "tlb",
2629 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 .params = "",
2631 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002632 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 },
aurel327c664e22009-03-03 06:12:22 +00002634#endif
2635#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 {
2637 .name = "mem",
2638 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 .params = "",
2640 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002641 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 },
bellardb86bda52004-09-18 19:32:46 +00002643#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 {
Blue Swirl314e2982011-09-11 20:22:05 +00002645 .name = "mtree",
2646 .args_type = "",
2647 .params = "",
2648 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002649 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002650 },
2651 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 .name = "jit",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002656 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
Max Filippov246ae242014-11-02 11:04:18 +03002659 .name = "opcount",
2660 .args_type = "",
2661 .params = "",
2662 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002663 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002664 },
2665 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .name = "kvm",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002670 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
2673 .name = "numa",
2674 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 .params = "",
2676 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002677 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 },
2679 {
2680 .name = "usb",
2681 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .params = "",
2683 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002684 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 },
2686 {
2687 .name = "usbhost",
2688 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 .params = "",
2690 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002691 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 },
2693 {
2694 .name = "profile",
2695 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .params = "",
2697 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002698 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
2700 {
2701 .name = "capture",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002705 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
2707 {
2708 .name = "snapshots",
2709 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 .params = "",
2711 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002712 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 },
2714 {
2715 .name = "status",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002719 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 },
2721 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 .name = "mice",
2723 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002724 .params = "",
2725 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002726 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 },
2728 {
2729 .name = "vnc",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002733 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002735#if defined(CONFIG_SPICE)
2736 {
2737 .name = "spice",
2738 .args_type = "",
2739 .params = "",
2740 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002741 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002742 },
2743#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 {
2745 .name = "name",
2746 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 .params = "",
2748 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002749 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 },
2751 {
2752 .name = "uuid",
2753 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 .params = "",
2755 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002756 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 {
2759 .name = "cpustats",
2760 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002761 .params = "",
2762 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002763 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 },
blueswir131a60e22007-10-26 18:42:59 +00002765#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 {
2767 .name = "usernet",
2768 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002769 .params = "",
2770 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002771 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 },
blueswir131a60e22007-10-26 18:42:59 +00002773#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 {
2775 .name = "migrate",
2776 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002777 .params = "",
2778 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002779 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 },
2781 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002782 .name = "migrate_capabilities",
2783 .args_type = "",
2784 .params = "",
2785 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002786 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002787 },
2788 {
Liang Li50e9a622015-03-23 16:32:29 +08002789 .name = "migrate_parameters",
2790 .args_type = "",
2791 .params = "",
2792 .help = "show current migration parameters",
2793 .mhandler.cmd = hmp_info_migrate_parameters,
2794 },
2795 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002796 .name = "migrate_cache_size",
2797 .args_type = "",
2798 .params = "",
2799 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002800 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002801 },
2802 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 .name = "balloon",
2804 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 .params = "",
2806 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002807 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 },
2809 {
2810 .name = "qtree",
2811 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002812 .params = "",
2813 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002814 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002815 },
2816 {
2817 .name = "qdm",
2818 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002819 .params = "",
2820 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002821 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 },
2823 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002824 .name = "qom-tree",
2825 .args_type = "path:s?",
2826 .params = "[path]",
2827 .help = "show QOM composition tree",
2828 .mhandler.cmd = hmp_info_qom_tree,
2829 },
2830 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002831 .name = "roms",
2832 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 .params = "",
2834 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002835 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002836 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302837 {
2838 .name = "trace-events",
2839 .args_type = "",
2840 .params = "",
2841 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002842 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302843 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002844 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002845 .name = "tpm",
2846 .args_type = "",
2847 .params = "",
2848 .help = "show the TPM device",
2849 .mhandler.cmd = hmp_info_tpm,
2850 },
2851 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002852 .name = "memdev",
2853 .args_type = "",
2854 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002855 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002856 .mhandler.cmd = hmp_info_memdev,
2857 },
2858 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002859 .name = "memory-devices",
2860 .args_type = "",
2861 .params = "",
2862 .help = "show memory devices",
2863 .mhandler.cmd = hmp_info_memory_devices,
2864 },
2865 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002866 .name = NULL,
2867 },
bellard9dc39cb2004-03-14 21:38:27 +00002868};
2869
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002870/* mon_cmds and info_cmds would be sorted at runtime */
2871static mon_cmd_t mon_cmds[] = {
2872#include "hmp-commands.h"
2873 { NULL, NULL, },
2874};
2875
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002876static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002877#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002878 { /* NULL */ },
2879};
2880
bellard9307c4c2004-04-04 12:57:25 +00002881/*******************************************************************/
2882
2883static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002884static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002885
bellard92a31b12005-02-10 22:00:52 +00002886#define MD_TLONG 0
2887#define MD_I32 1
2888
bellard9307c4c2004-04-04 12:57:25 +00002889typedef struct MonitorDef {
2890 const char *name;
2891 int offset;
blueswir18662d652008-10-02 18:32:44 +00002892 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002893 int type;
bellard9307c4c2004-04-04 12:57:25 +00002894} MonitorDef;
2895
bellard57206fd2004-04-25 18:54:52 +00002896#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002897static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002898{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002899 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002900 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002901}
2902#endif
2903
bellarda541f292004-04-12 20:39:29 +00002904#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002905static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002906{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002907 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002908 unsigned int u;
2909 int i;
2910
2911 u = 0;
2912 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002913 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002914
2915 return u;
2916}
2917
blueswir18662d652008-10-02 18:32:44 +00002918static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002919{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002920 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002921 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002922}
2923
blueswir18662d652008-10-02 18:32:44 +00002924static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002925{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002926 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002927 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002928}
bellard9fddaa02004-05-21 12:59:32 +00002929
blueswir18662d652008-10-02 18:32:44 +00002930static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002931{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002932 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002933 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002934}
2935
blueswir18662d652008-10-02 18:32:44 +00002936static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002937{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002938 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002939 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002940}
2941
blueswir18662d652008-10-02 18:32:44 +00002942static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002943{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002944 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002945 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002946}
bellarda541f292004-04-12 20:39:29 +00002947#endif
2948
bellarde95c8d52004-09-30 22:22:08 +00002949#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002950#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002951static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002952{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002953 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002954
2955 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002956}
bellard7b936c02005-10-30 17:05:13 +00002957#endif
bellarde95c8d52004-09-30 22:22:08 +00002958
blueswir18662d652008-10-02 18:32:44 +00002959static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002960{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002961 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002962 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002963}
2964#endif
2965
blueswir18662d652008-10-02 18:32:44 +00002966static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002967#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002968
2969#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002970 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2971 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2972 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002973
Andreas Färbere59d1672012-02-16 00:40:47 +01002974 { "eax", offsetof(CPUX86State, regs[0]) },
2975 { "ecx", offsetof(CPUX86State, regs[1]) },
2976 { "edx", offsetof(CPUX86State, regs[2]) },
2977 { "ebx", offsetof(CPUX86State, regs[3]) },
2978 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2979 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2980 { "esi", offsetof(CPUX86State, regs[6]) },
2981 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002982#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002983 { "r8", offsetof(CPUX86State, regs[8]) },
2984 { "r9", offsetof(CPUX86State, regs[9]) },
2985 { "r10", offsetof(CPUX86State, regs[10]) },
2986 { "r11", offsetof(CPUX86State, regs[11]) },
2987 { "r12", offsetof(CPUX86State, regs[12]) },
2988 { "r13", offsetof(CPUX86State, regs[13]) },
2989 { "r14", offsetof(CPUX86State, regs[14]) },
2990 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00002991#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01002992 { "eflags", offsetof(CPUX86State, eflags) },
2993 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00002994 SEG("cs", R_CS)
2995 SEG("ds", R_DS)
2996 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002997 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002998 SEG("fs", R_FS)
2999 SEG("gs", R_GS)
3000 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003001#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003002 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003003 { "r0", offsetof(CPUPPCState, gpr[0]) },
3004 { "r1", offsetof(CPUPPCState, gpr[1]) },
3005 { "r2", offsetof(CPUPPCState, gpr[2]) },
3006 { "r3", offsetof(CPUPPCState, gpr[3]) },
3007 { "r4", offsetof(CPUPPCState, gpr[4]) },
3008 { "r5", offsetof(CPUPPCState, gpr[5]) },
3009 { "r6", offsetof(CPUPPCState, gpr[6]) },
3010 { "r7", offsetof(CPUPPCState, gpr[7]) },
3011 { "r8", offsetof(CPUPPCState, gpr[8]) },
3012 { "r9", offsetof(CPUPPCState, gpr[9]) },
3013 { "r10", offsetof(CPUPPCState, gpr[10]) },
3014 { "r11", offsetof(CPUPPCState, gpr[11]) },
3015 { "r12", offsetof(CPUPPCState, gpr[12]) },
3016 { "r13", offsetof(CPUPPCState, gpr[13]) },
3017 { "r14", offsetof(CPUPPCState, gpr[14]) },
3018 { "r15", offsetof(CPUPPCState, gpr[15]) },
3019 { "r16", offsetof(CPUPPCState, gpr[16]) },
3020 { "r17", offsetof(CPUPPCState, gpr[17]) },
3021 { "r18", offsetof(CPUPPCState, gpr[18]) },
3022 { "r19", offsetof(CPUPPCState, gpr[19]) },
3023 { "r20", offsetof(CPUPPCState, gpr[20]) },
3024 { "r21", offsetof(CPUPPCState, gpr[21]) },
3025 { "r22", offsetof(CPUPPCState, gpr[22]) },
3026 { "r23", offsetof(CPUPPCState, gpr[23]) },
3027 { "r24", offsetof(CPUPPCState, gpr[24]) },
3028 { "r25", offsetof(CPUPPCState, gpr[25]) },
3029 { "r26", offsetof(CPUPPCState, gpr[26]) },
3030 { "r27", offsetof(CPUPPCState, gpr[27]) },
3031 { "r28", offsetof(CPUPPCState, gpr[28]) },
3032 { "r29", offsetof(CPUPPCState, gpr[29]) },
3033 { "r30", offsetof(CPUPPCState, gpr[30]) },
3034 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003035 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003036 { "f0", offsetof(CPUPPCState, fpr[0]) },
3037 { "f1", offsetof(CPUPPCState, fpr[1]) },
3038 { "f2", offsetof(CPUPPCState, fpr[2]) },
3039 { "f3", offsetof(CPUPPCState, fpr[3]) },
3040 { "f4", offsetof(CPUPPCState, fpr[4]) },
3041 { "f5", offsetof(CPUPPCState, fpr[5]) },
3042 { "f6", offsetof(CPUPPCState, fpr[6]) },
3043 { "f7", offsetof(CPUPPCState, fpr[7]) },
3044 { "f8", offsetof(CPUPPCState, fpr[8]) },
3045 { "f9", offsetof(CPUPPCState, fpr[9]) },
3046 { "f10", offsetof(CPUPPCState, fpr[10]) },
3047 { "f11", offsetof(CPUPPCState, fpr[11]) },
3048 { "f12", offsetof(CPUPPCState, fpr[12]) },
3049 { "f13", offsetof(CPUPPCState, fpr[13]) },
3050 { "f14", offsetof(CPUPPCState, fpr[14]) },
3051 { "f15", offsetof(CPUPPCState, fpr[15]) },
3052 { "f16", offsetof(CPUPPCState, fpr[16]) },
3053 { "f17", offsetof(CPUPPCState, fpr[17]) },
3054 { "f18", offsetof(CPUPPCState, fpr[18]) },
3055 { "f19", offsetof(CPUPPCState, fpr[19]) },
3056 { "f20", offsetof(CPUPPCState, fpr[20]) },
3057 { "f21", offsetof(CPUPPCState, fpr[21]) },
3058 { "f22", offsetof(CPUPPCState, fpr[22]) },
3059 { "f23", offsetof(CPUPPCState, fpr[23]) },
3060 { "f24", offsetof(CPUPPCState, fpr[24]) },
3061 { "f25", offsetof(CPUPPCState, fpr[25]) },
3062 { "f26", offsetof(CPUPPCState, fpr[26]) },
3063 { "f27", offsetof(CPUPPCState, fpr[27]) },
3064 { "f28", offsetof(CPUPPCState, fpr[28]) },
3065 { "f29", offsetof(CPUPPCState, fpr[29]) },
3066 { "f30", offsetof(CPUPPCState, fpr[30]) },
3067 { "f31", offsetof(CPUPPCState, fpr[31]) },
3068 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003069 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003070 { "nip|pc", offsetof(CPUPPCState, nip) },
3071 { "lr", offsetof(CPUPPCState, lr) },
3072 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003073 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003074 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003075 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003076 { "msr", 0, &monitor_get_msr, },
3077 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003078 { "tbu", 0, &monitor_get_tbu, },
3079 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003080 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003081 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3082 { "sr0", offsetof(CPUPPCState, sr[0]) },
3083 { "sr1", offsetof(CPUPPCState, sr[1]) },
3084 { "sr2", offsetof(CPUPPCState, sr[2]) },
3085 { "sr3", offsetof(CPUPPCState, sr[3]) },
3086 { "sr4", offsetof(CPUPPCState, sr[4]) },
3087 { "sr5", offsetof(CPUPPCState, sr[5]) },
3088 { "sr6", offsetof(CPUPPCState, sr[6]) },
3089 { "sr7", offsetof(CPUPPCState, sr[7]) },
3090 { "sr8", offsetof(CPUPPCState, sr[8]) },
3091 { "sr9", offsetof(CPUPPCState, sr[9]) },
3092 { "sr10", offsetof(CPUPPCState, sr[10]) },
3093 { "sr11", offsetof(CPUPPCState, sr[11]) },
3094 { "sr12", offsetof(CPUPPCState, sr[12]) },
3095 { "sr13", offsetof(CPUPPCState, sr[13]) },
3096 { "sr14", offsetof(CPUPPCState, sr[14]) },
3097 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003098 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003099 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003100
Andreas Färbere59d1672012-02-16 00:40:47 +01003101 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3102 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003103 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3104 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3105 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003106 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3107 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3108 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3109 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3110 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3111 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3112 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3113 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3114 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3115 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3116 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3117 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3118 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3119 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3120 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3121 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3122 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3123 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3124 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3125 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3126 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3127 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3128 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3129 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3130 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3131 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3132 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3133 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3134 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3135 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3136 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3137 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3138 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3139 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3140 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3141 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3142 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3143 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3144 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3145 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3146 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3147 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3148 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3149 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3150 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3151 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3152 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3153 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3154 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3155 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3156 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3157 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3158 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3159 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3160 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3161 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3162 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3163 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3164 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3165 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3166 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3167 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3168 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3169 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003170
bellarde95c8d52004-09-30 22:22:08 +00003171#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003172 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3173 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3174 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3175 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3176 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3177 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3178 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3179 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003180 { "o0", 0, monitor_get_reg },
3181 { "o1", 1, monitor_get_reg },
3182 { "o2", 2, monitor_get_reg },
3183 { "o3", 3, monitor_get_reg },
3184 { "o4", 4, monitor_get_reg },
3185 { "o5", 5, monitor_get_reg },
3186 { "o6", 6, monitor_get_reg },
3187 { "o7", 7, monitor_get_reg },
3188 { "l0", 8, monitor_get_reg },
3189 { "l1", 9, monitor_get_reg },
3190 { "l2", 10, monitor_get_reg },
3191 { "l3", 11, monitor_get_reg },
3192 { "l4", 12, monitor_get_reg },
3193 { "l5", 13, monitor_get_reg },
3194 { "l6", 14, monitor_get_reg },
3195 { "l7", 15, monitor_get_reg },
3196 { "i0", 16, monitor_get_reg },
3197 { "i1", 17, monitor_get_reg },
3198 { "i2", 18, monitor_get_reg },
3199 { "i3", 19, monitor_get_reg },
3200 { "i4", 20, monitor_get_reg },
3201 { "i5", 21, monitor_get_reg },
3202 { "i6", 22, monitor_get_reg },
3203 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003204 { "pc", offsetof(CPUSPARCState, pc) },
3205 { "npc", offsetof(CPUSPARCState, npc) },
3206 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003207#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003208 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003209 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003210#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003211 { "tbr", offsetof(CPUSPARCState, tbr) },
3212 { "fsr", offsetof(CPUSPARCState, fsr) },
3213 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3214 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3215 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3216 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3217 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3218 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3219 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3220 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3221 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3222 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3223 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3224 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3225 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3226 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3227 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3228 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3229 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3230 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3231 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3232 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3233 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3234 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3235 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3236 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3237 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3238 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3239 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3240 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3241 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3242 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3243 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3244 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003245#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003246 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3247 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3248 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3249 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3250 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3251 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3252 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3253 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3254 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3255 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3256 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3257 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3258 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3259 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3260 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3261 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3262 { "asi", offsetof(CPUSPARCState, asi) },
3263 { "pstate", offsetof(CPUSPARCState, pstate) },
3264 { "cansave", offsetof(CPUSPARCState, cansave) },
3265 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3266 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3267 { "wstate", offsetof(CPUSPARCState, wstate) },
3268 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3269 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003270#endif
bellard9307c4c2004-04-04 12:57:25 +00003271#endif
3272 { NULL },
3273};
3274
Stefan Weil9c3175c2013-08-22 21:30:09 +02003275static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3276expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003277{
Fam Zheng277acfe2013-08-20 10:58:21 +08003278 va_list ap;
3279 va_start(ap, fmt);
3280 monitor_vprintf(mon, fmt, ap);
3281 monitor_printf(mon, "\n");
3282 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003283 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003284}
3285
Markus Armbruster09b94182010-01-20 13:07:30 +01003286/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003287static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003288{
blueswir18662d652008-10-02 18:32:44 +00003289 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003290 void *ptr;
3291
bellard9307c4c2004-04-04 12:57:25 +00003292 for(md = monitor_defs; md->name != NULL; md++) {
3293 if (compare_cmd(name, md->name)) {
3294 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003295 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003296 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003297 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003298 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003299 switch(md->type) {
3300 case MD_I32:
3301 *pval = *(int32_t *)ptr;
3302 break;
3303 case MD_TLONG:
3304 *pval = *(target_long *)ptr;
3305 break;
3306 default:
3307 *pval = 0;
3308 break;
3309 }
bellard9307c4c2004-04-04 12:57:25 +00003310 }
3311 return 0;
3312 }
3313 }
3314 return -1;
3315}
3316
3317static void next(void)
3318{
Blue Swirl660f11b2009-07-31 21:16:51 +00003319 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003320 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003321 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003322 pch++;
3323 }
3324}
3325
aliguori376253e2009-03-05 23:01:23 +00003326static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003327
aliguori376253e2009-03-05 23:01:23 +00003328static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003329{
blueswir1c2efc952007-09-25 17:28:42 +00003330 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003331 char *p;
bellard6a00d602005-11-21 23:25:50 +00003332 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003333
3334 switch(*pch) {
3335 case '+':
3336 next();
aliguori376253e2009-03-05 23:01:23 +00003337 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003338 break;
3339 case '-':
3340 next();
aliguori376253e2009-03-05 23:01:23 +00003341 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003342 break;
3343 case '~':
3344 next();
aliguori376253e2009-03-05 23:01:23 +00003345 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003346 break;
3347 case '(':
3348 next();
aliguori376253e2009-03-05 23:01:23 +00003349 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003350 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003351 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003352 }
3353 next();
3354 break;
bellard81d09122004-07-14 17:21:37 +00003355 case '\'':
3356 pch++;
3357 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003358 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003359 n = *pch;
3360 pch++;
3361 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003362 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003363 next();
3364 break;
bellard9307c4c2004-04-04 12:57:25 +00003365 case '$':
3366 {
3367 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003368 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003369
bellard9307c4c2004-04-04 12:57:25 +00003370 pch++;
3371 q = buf;
3372 while ((*pch >= 'a' && *pch <= 'z') ||
3373 (*pch >= 'A' && *pch <= 'Z') ||
3374 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003375 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003376 if ((q - buf) < sizeof(buf) - 1)
3377 *q++ = *pch;
3378 pch++;
3379 }
blueswir1cd390082008-11-16 13:53:32 +00003380 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003381 pch++;
3382 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003383 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003384 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003385 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003386 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003387 }
3388 break;
3389 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003390 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003391 n = 0;
3392 break;
3393 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003394 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003395 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003396 if (errno == ERANGE) {
3397 expr_error(mon, "number too large");
3398 }
bellard9307c4c2004-04-04 12:57:25 +00003399 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003400 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003401 }
3402 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003403 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003404 pch++;
3405 break;
3406 }
3407 return n;
3408}
3409
3410
aliguori376253e2009-03-05 23:01:23 +00003411static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003412{
blueswir1c2efc952007-09-25 17:28:42 +00003413 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003414 int op;
ths3b46e622007-09-17 08:09:54 +00003415
aliguori376253e2009-03-05 23:01:23 +00003416 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003417 for(;;) {
3418 op = *pch;
3419 if (op != '*' && op != '/' && op != '%')
3420 break;
3421 next();
aliguori376253e2009-03-05 23:01:23 +00003422 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003423 switch(op) {
3424 default:
3425 case '*':
3426 val *= val2;
3427 break;
3428 case '/':
3429 case '%':
ths5fafdf22007-09-16 21:08:06 +00003430 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003431 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003432 if (op == '/')
3433 val /= val2;
3434 else
3435 val %= val2;
3436 break;
3437 }
3438 }
3439 return val;
3440}
3441
aliguori376253e2009-03-05 23:01:23 +00003442static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003443{
blueswir1c2efc952007-09-25 17:28:42 +00003444 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003445 int op;
bellard9307c4c2004-04-04 12:57:25 +00003446
aliguori376253e2009-03-05 23:01:23 +00003447 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003448 for(;;) {
3449 op = *pch;
3450 if (op != '&' && op != '|' && op != '^')
3451 break;
3452 next();
aliguori376253e2009-03-05 23:01:23 +00003453 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003454 switch(op) {
3455 default:
3456 case '&':
3457 val &= val2;
3458 break;
3459 case '|':
3460 val |= val2;
3461 break;
3462 case '^':
3463 val ^= val2;
3464 break;
3465 }
3466 }
3467 return val;
3468}
3469
aliguori376253e2009-03-05 23:01:23 +00003470static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003471{
blueswir1c2efc952007-09-25 17:28:42 +00003472 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003473 int op;
bellard9307c4c2004-04-04 12:57:25 +00003474
aliguori376253e2009-03-05 23:01:23 +00003475 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003476 for(;;) {
3477 op = *pch;
3478 if (op != '+' && op != '-')
3479 break;
3480 next();
aliguori376253e2009-03-05 23:01:23 +00003481 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003482 if (op == '+')
3483 val += val2;
3484 else
3485 val -= val2;
3486 }
3487 return val;
3488}
3489
aliguori376253e2009-03-05 23:01:23 +00003490static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003491{
3492 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003493 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003494 *pp = pch;
3495 return -1;
3496 }
blueswir1cd390082008-11-16 13:53:32 +00003497 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003498 pch++;
aliguori376253e2009-03-05 23:01:23 +00003499 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003500 *pp = pch;
3501 return 0;
3502}
3503
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003504static int get_double(Monitor *mon, double *pval, const char **pp)
3505{
3506 const char *p = *pp;
3507 char *tailp;
3508 double d;
3509
3510 d = strtod(p, &tailp);
3511 if (tailp == p) {
3512 monitor_printf(mon, "Number expected\n");
3513 return -1;
3514 }
3515 if (d != d || d - d != 0) {
3516 /* NaN or infinity */
3517 monitor_printf(mon, "Bad number\n");
3518 return -1;
3519 }
3520 *pval = d;
3521 *pp = tailp;
3522 return 0;
3523}
3524
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003525/*
3526 * Store the command-name in cmdname, and return a pointer to
3527 * the remaining of the command string.
3528 */
3529static const char *get_command_name(const char *cmdline,
3530 char *cmdname, size_t nlen)
3531{
3532 size_t len;
3533 const char *p, *pstart;
3534
3535 p = cmdline;
3536 while (qemu_isspace(*p))
3537 p++;
3538 if (*p == '\0')
3539 return NULL;
3540 pstart = p;
3541 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3542 p++;
3543 len = p - pstart;
3544 if (len > nlen - 1)
3545 len = nlen - 1;
3546 memcpy(cmdname, pstart, len);
3547 cmdname[len] = '\0';
3548 return p;
3549}
3550
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003551/**
3552 * Read key of 'type' into 'key' and return the current
3553 * 'type' pointer.
3554 */
3555static char *key_get_info(const char *type, char **key)
3556{
3557 size_t len;
3558 char *p, *str;
3559
3560 if (*type == ',')
3561 type++;
3562
3563 p = strchr(type, ':');
3564 if (!p) {
3565 *key = NULL;
3566 return NULL;
3567 }
3568 len = p - type;
3569
Anthony Liguori7267c092011-08-20 22:09:37 -05003570 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003571 memcpy(str, type, len);
3572 str[len] = '\0';
3573
3574 *key = str;
3575 return ++p;
3576}
3577
bellard9307c4c2004-04-04 12:57:25 +00003578static int default_fmt_format = 'x';
3579static int default_fmt_size = 4;
3580
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003581static int is_valid_option(const char *c, const char *typestr)
3582{
3583 char option[3];
3584
3585 option[0] = '-';
3586 option[1] = *c;
3587 option[2] = '\0';
3588
3589 typestr = strstr(typestr, option);
3590 return (typestr != NULL);
3591}
3592
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003593static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3594 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003595{
3596 const mon_cmd_t *cmd;
3597
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003598 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003599 if (compare_cmd(cmdname, cmd->name)) {
3600 return cmd;
3601 }
3602 }
3603
3604 return NULL;
3605}
3606
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003607static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3608{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003609 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003610}
3611
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003612/*
3613 * Parse @cmdline according to command table @table.
3614 * If @cmdline is blank, return NULL.
3615 * If it can't be parsed, report to @mon, and return NULL.
3616 * Else, insert command arguments into @qdict, and return the command.
Peter Maydell085d8132013-03-18 17:20:07 +00003617 * If a sub-command table exists, and if @cmdline contains an additional string
3618 * for a sub-command, this function will try to search the sub-command table.
3619 * If no additional string for a sub-command is present, this function will
3620 * return the command found in @table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003621 * Do not assume the returned command points into @table! It doesn't
3622 * when the command is a sub-command.
3623 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003624static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003625 const char *cmdline,
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003626 int start,
3627 mon_cmd_t *table,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003628 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003629{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003630 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003631 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003632 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003633 char cmdname[256];
3634 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003635 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003636
3637#ifdef DEBUG
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003638 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
bellard9dc39cb2004-03-14 21:38:27 +00003639#endif
ths3b46e622007-09-17 08:09:54 +00003640
bellard9307c4c2004-04-04 12:57:25 +00003641 /* extract the command name */
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003642 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003643 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003644 return NULL;
ths3b46e622007-09-17 08:09:54 +00003645
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003646 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003647 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003648 monitor_printf(mon, "unknown command: '%.*s'\n",
3649 (int)(p - cmdline), cmdline);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003650 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003651 }
bellard9307c4c2004-04-04 12:57:25 +00003652
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003653 /* filter out following useless space */
3654 while (qemu_isspace(*p)) {
3655 p++;
3656 }
3657 /* search sub command */
3658 if (cmd->sub_table != NULL) {
3659 /* check if user set additional command */
3660 if (*p == '\0') {
3661 return cmd;
3662 }
3663 return monitor_parse_command(mon, cmdline, p - cmdline,
3664 cmd->sub_table, qdict);
3665 }
3666
bellard9307c4c2004-04-04 12:57:25 +00003667 /* parse the parameters */
3668 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003669 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003670 typestr = key_get_info(typestr, &key);
3671 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003672 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003673 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003674 typestr++;
3675 switch(c) {
3676 case 'F':
bellard81d09122004-07-14 17:21:37 +00003677 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003678 case 's':
3679 {
3680 int ret;
ths3b46e622007-09-17 08:09:54 +00003681
blueswir1cd390082008-11-16 13:53:32 +00003682 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003683 p++;
3684 if (*typestr == '?') {
3685 typestr++;
3686 if (*p == '\0') {
3687 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003688 break;
bellard9307c4c2004-04-04 12:57:25 +00003689 }
3690 }
3691 ret = get_str(buf, sizeof(buf), &p);
3692 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003693 switch(c) {
3694 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003695 monitor_printf(mon, "%s: filename expected\n",
3696 cmdname);
bellard81d09122004-07-14 17:21:37 +00003697 break;
3698 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003699 monitor_printf(mon, "%s: block device name expected\n",
3700 cmdname);
bellard81d09122004-07-14 17:21:37 +00003701 break;
3702 default:
aliguori376253e2009-03-05 23:01:23 +00003703 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003704 break;
3705 }
bellard9307c4c2004-04-04 12:57:25 +00003706 goto fail;
3707 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003708 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003709 }
3710 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003711 case 'O':
3712 {
3713 QemuOptsList *opts_list;
3714 QemuOpts *opts;
3715
3716 opts_list = qemu_find_opts(key);
3717 if (!opts_list || opts_list->desc->name) {
3718 goto bad_type;
3719 }
3720 while (qemu_isspace(*p)) {
3721 p++;
3722 }
3723 if (!*p)
3724 break;
3725 if (get_str(buf, sizeof(buf), &p) < 0) {
3726 goto fail;
3727 }
3728 opts = qemu_opts_parse(opts_list, buf, 1);
3729 if (!opts) {
3730 goto fail;
3731 }
3732 qemu_opts_to_qdict(opts, qdict);
3733 qemu_opts_del(opts);
3734 }
3735 break;
bellard9307c4c2004-04-04 12:57:25 +00003736 case '/':
3737 {
3738 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003739
blueswir1cd390082008-11-16 13:53:32 +00003740 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003741 p++;
3742 if (*p == '/') {
3743 /* format found */
3744 p++;
3745 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003746 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003747 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003748 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003749 count = count * 10 + (*p - '0');
3750 p++;
3751 }
3752 }
3753 size = -1;
3754 format = -1;
3755 for(;;) {
3756 switch(*p) {
3757 case 'o':
3758 case 'd':
3759 case 'u':
3760 case 'x':
3761 case 'i':
3762 case 'c':
3763 format = *p++;
3764 break;
3765 case 'b':
3766 size = 1;
3767 p++;
3768 break;
3769 case 'h':
3770 size = 2;
3771 p++;
3772 break;
3773 case 'w':
3774 size = 4;
3775 p++;
3776 break;
3777 case 'g':
3778 case 'L':
3779 size = 8;
3780 p++;
3781 break;
3782 default:
3783 goto next;
3784 }
3785 }
3786 next:
blueswir1cd390082008-11-16 13:53:32 +00003787 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003788 monitor_printf(mon, "invalid char in format: '%c'\n",
3789 *p);
bellard9307c4c2004-04-04 12:57:25 +00003790 goto fail;
3791 }
bellard9307c4c2004-04-04 12:57:25 +00003792 if (format < 0)
3793 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003794 if (format != 'i') {
3795 /* for 'i', not specifying a size gives -1 as size */
3796 if (size < 0)
3797 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003798 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003799 }
bellard9307c4c2004-04-04 12:57:25 +00003800 default_fmt_format = format;
3801 } else {
3802 count = 1;
3803 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003804 if (format != 'i') {
3805 size = default_fmt_size;
3806 } else {
3807 size = -1;
3808 }
bellard9307c4c2004-04-04 12:57:25 +00003809 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003810 qdict_put(qdict, "count", qint_from_int(count));
3811 qdict_put(qdict, "format", qint_from_int(format));
3812 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003813 }
3814 break;
3815 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003816 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003817 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003818 {
blueswir1c2efc952007-09-25 17:28:42 +00003819 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003820
blueswir1cd390082008-11-16 13:53:32 +00003821 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003822 p++;
bellard34405572004-06-08 00:55:58 +00003823 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003824 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003825 if (*p == '\0') {
3826 typestr++;
3827 break;
3828 }
bellard34405572004-06-08 00:55:58 +00003829 } else {
3830 if (*p == '.') {
3831 p++;
blueswir1cd390082008-11-16 13:53:32 +00003832 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003833 p++;
bellard34405572004-06-08 00:55:58 +00003834 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003835 typestr++;
3836 break;
bellard34405572004-06-08 00:55:58 +00003837 }
3838 }
bellard13224a82006-07-14 22:03:35 +00003839 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003840 }
aliguori376253e2009-03-05 23:01:23 +00003841 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003842 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003843 /* Check if 'i' is greater than 32-bit */
3844 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3845 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3846 monitor_printf(mon, "integer is for 32-bit values\n");
3847 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003848 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003849 if (val < 0) {
3850 monitor_printf(mon, "enter a positive value\n");
3851 goto fail;
3852 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003853 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003854 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003855 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003856 }
3857 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003858 case 'o':
3859 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003860 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003861 char *end;
3862
3863 while (qemu_isspace(*p)) {
3864 p++;
3865 }
3866 if (*typestr == '?') {
3867 typestr++;
3868 if (*p == '\0') {
3869 break;
3870 }
3871 }
3872 val = strtosz(p, &end);
3873 if (val < 0) {
3874 monitor_printf(mon, "invalid size\n");
3875 goto fail;
3876 }
3877 qdict_put(qdict, key, qint_from_int(val));
3878 p = end;
3879 }
3880 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003881 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003882 {
3883 double val;
3884
3885 while (qemu_isspace(*p))
3886 p++;
3887 if (*typestr == '?') {
3888 typestr++;
3889 if (*p == '\0') {
3890 break;
3891 }
3892 }
3893 if (get_double(mon, &val, &p) < 0) {
3894 goto fail;
3895 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003896 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003897 switch (*p) {
3898 case 'm':
3899 val /= 1e3; p += 2; break;
3900 case 'u':
3901 val /= 1e6; p += 2; break;
3902 case 'n':
3903 val /= 1e9; p += 2; break;
3904 }
3905 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003906 if (*p && !qemu_isspace(*p)) {
3907 monitor_printf(mon, "Unknown unit suffix\n");
3908 goto fail;
3909 }
3910 qdict_put(qdict, key, qfloat_from_double(val));
3911 }
3912 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003913 case 'b':
3914 {
3915 const char *beg;
3916 int val;
3917
3918 while (qemu_isspace(*p)) {
3919 p++;
3920 }
3921 beg = p;
3922 while (qemu_isgraph(*p)) {
3923 p++;
3924 }
3925 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3926 val = 1;
3927 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3928 val = 0;
3929 } else {
3930 monitor_printf(mon, "Expected 'on' or 'off'\n");
3931 goto fail;
3932 }
3933 qdict_put(qdict, key, qbool_from_int(val));
3934 }
3935 break;
bellard9307c4c2004-04-04 12:57:25 +00003936 case '-':
3937 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003938 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003939 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003940 /* option */
ths3b46e622007-09-17 08:09:54 +00003941
bellard9307c4c2004-04-04 12:57:25 +00003942 c = *typestr++;
3943 if (c == '\0')
3944 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003945 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003946 p++;
bellard9307c4c2004-04-04 12:57:25 +00003947 if (*p == '-') {
3948 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003949 if(c != *p) {
3950 if(!is_valid_option(p, typestr)) {
3951
3952 monitor_printf(mon, "%s: unsupported option -%c\n",
3953 cmdname, *p);
3954 goto fail;
3955 } else {
3956 skip_key = 1;
3957 }
bellard9307c4c2004-04-04 12:57:25 +00003958 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003959 if(skip_key) {
3960 p = tmp;
3961 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003962 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003963 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003964 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003965 }
bellard9307c4c2004-04-04 12:57:25 +00003966 }
bellard9307c4c2004-04-04 12:57:25 +00003967 }
3968 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003969 case 'S':
3970 {
3971 /* package all remaining string */
3972 int len;
3973
3974 while (qemu_isspace(*p)) {
3975 p++;
3976 }
3977 if (*typestr == '?') {
3978 typestr++;
3979 if (*p == '\0') {
3980 /* no remaining string: NULL argument */
3981 break;
3982 }
3983 }
3984 len = strlen(p);
3985 if (len <= 0) {
3986 monitor_printf(mon, "%s: string expected\n",
3987 cmdname);
3988 break;
3989 }
3990 qdict_put(qdict, key, qstring_from_str(p));
3991 p += len;
3992 }
3993 break;
bellard9307c4c2004-04-04 12:57:25 +00003994 default:
3995 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003996 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003997 goto fail;
3998 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003999 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004000 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004001 }
4002 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004003 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004004 p++;
4005 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004006 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4007 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004008 goto fail;
4009 }
4010
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004011 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004012
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004013fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004014 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004015 return NULL;
4016}
4017
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004018void monitor_set_error(Monitor *mon, QError *qerror)
4019{
4020 /* report only the first error */
4021 if (!mon->error) {
4022 mon->error = qerror;
4023 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004024 QDECREF(qerror);
4025 }
4026}
4027
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004028static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004029{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004030 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004031 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004032
4033 qdict = qdict_new();
4034
Wenchao Xia77172392013-08-27 20:38:20 +08004035 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
Markus Armbruster8a4f5012015-03-05 18:50:05 +01004036 if (cmd) {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004037 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004038 }
4039
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004040 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004041}
4042
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004043static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004044{
4045 const char *p, *pstart;
4046 char cmd[128];
4047 int len;
4048
4049 p = list;
4050 for(;;) {
4051 pstart = p;
4052 p = strchr(p, '|');
4053 if (!p)
4054 p = pstart + strlen(pstart);
4055 len = p - pstart;
4056 if (len > sizeof(cmd) - 2)
4057 len = sizeof(cmd) - 2;
4058 memcpy(cmd, pstart, len);
4059 cmd[len] = '\0';
4060 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004061 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004062 }
4063 if (*p == '\0')
4064 break;
4065 p++;
4066 }
4067}
4068
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004069static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004070{
4071 DIR *ffs;
4072 struct dirent *d;
4073 char path[1024];
4074 char file[1024], file_prefix[1024];
4075 int input_path_len;
4076 const char *p;
4077
ths5fafdf22007-09-16 21:08:06 +00004078 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004079 if (!p) {
4080 input_path_len = 0;
4081 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004082 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004083 } else {
4084 input_path_len = p - input + 1;
4085 memcpy(path, input, input_path_len);
4086 if (input_path_len > sizeof(path) - 1)
4087 input_path_len = sizeof(path) - 1;
4088 path[input_path_len] = '\0';
4089 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4090 }
4091#ifdef DEBUG_COMPLETION
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004092 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
aliguori376253e2009-03-05 23:01:23 +00004093 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004094#endif
4095 ffs = opendir(path);
4096 if (!ffs)
4097 return;
4098 for(;;) {
4099 struct stat sb;
4100 d = readdir(ffs);
4101 if (!d)
4102 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004103
4104 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4105 continue;
4106 }
4107
bellard81d09122004-07-14 17:21:37 +00004108 if (strstart(d->d_name, file_prefix, NULL)) {
4109 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004110 if (input_path_len < sizeof(file))
4111 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4112 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004113 /* stat the file to find out if it's a directory.
4114 * In that case add a slash to speed up typing long paths
4115 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004116 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004117 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004118 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004119 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004120 }
4121 }
4122 closedir(ffs);
4123}
4124
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004125static const char *next_arg_type(const char *typestr)
4126{
4127 const char *p = strchr(typestr, ':');
4128 return (p != NULL ? ++p : typestr);
4129}
4130
Hani Benhabiles40d19392014-05-07 23:41:30 +01004131static void add_completion_option(ReadLineState *rs, const char *str,
4132 const char *option)
4133{
4134 if (!str || !option) {
4135 return;
4136 }
4137 if (!strncmp(option, str, strlen(str))) {
4138 readline_add_completion(rs, option);
4139 }
4140}
4141
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004142void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4143{
4144 size_t len;
4145 ChardevBackendInfoList *list, *start;
4146
4147 if (nb_args != 2) {
4148 return;
4149 }
4150 len = strlen(str);
4151 readline_set_completion_index(rs, len);
4152
4153 start = list = qmp_query_chardev_backends(NULL);
4154 while (list) {
4155 const char *chr_name = list->value->name;
4156
4157 if (!strncmp(chr_name, str, len)) {
4158 readline_add_completion(rs, chr_name);
4159 }
4160 list = list->next;
4161 }
4162 qapi_free_ChardevBackendInfoList(start);
4163}
4164
Hani Benhabilesb162b492014-05-07 23:41:31 +01004165void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4166{
4167 size_t len;
4168 int i;
4169
4170 if (nb_args != 2) {
4171 return;
4172 }
4173 len = strlen(str);
4174 readline_set_completion_index(rs, len);
4175 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4176 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4177 }
4178}
4179
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004180void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004181{
4182 GSList *list, *elt;
4183 size_t len;
4184
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004185 if (nb_args != 2) {
4186 return;
4187 }
4188
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004189 len = strlen(str);
4190 readline_set_completion_index(rs, len);
4191 list = elt = object_class_get_list(TYPE_DEVICE, false);
4192 while (elt) {
4193 const char *name;
4194 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4195 TYPE_DEVICE);
4196 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004197
4198 if (!dc->cannot_instantiate_with_device_add_yet
4199 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004200 readline_add_completion(rs, name);
4201 }
4202 elt = elt->next;
4203 }
4204 g_slist_free(list);
4205}
4206
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004207void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004208{
4209 GSList *list, *elt;
4210 size_t len;
4211
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004212 if (nb_args != 2) {
4213 return;
4214 }
4215
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004216 len = strlen(str);
4217 readline_set_completion_index(rs, len);
4218 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4219 while (elt) {
4220 const char *name;
4221
4222 name = object_class_get_name(OBJECT_CLASS(elt->data));
4223 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4224 readline_add_completion(rs, name);
4225 }
4226 elt = elt->next;
4227 }
4228 g_slist_free(list);
4229}
4230
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004231static void peripheral_device_del_completion(ReadLineState *rs,
4232 const char *str, size_t len)
4233{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004234 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4235 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004236
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004237 list = qdev_build_hotpluggable_device_list(peripheral);
4238 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004239 return;
4240 }
4241
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004242 for (item = list; item; item = g_slist_next(item)) {
4243 DeviceState *dev = item->data;
4244
4245 if (dev->id && !strncmp(str, dev->id, len)) {
4246 readline_add_completion(rs, dev->id);
4247 }
4248 }
4249
4250 g_slist_free(list);
4251}
4252
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004253void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4254{
4255 size_t len;
4256 ChardevInfoList *list, *start;
4257
4258 if (nb_args != 2) {
4259 return;
4260 }
4261 len = strlen(str);
4262 readline_set_completion_index(rs, len);
4263
4264 start = list = qmp_query_chardev(NULL);
4265 while (list) {
4266 ChardevInfo *chr = list->value;
4267
4268 if (!strncmp(chr->label, str, len)) {
4269 readline_add_completion(rs, chr->label);
4270 }
4271 list = list->next;
4272 }
4273 qapi_free_ChardevInfoList(start);
4274}
4275
Hani Benhabiles8e597772014-05-27 23:39:30 +01004276static void ringbuf_completion(ReadLineState *rs, const char *str)
4277{
4278 size_t len;
4279 ChardevInfoList *list, *start;
4280
4281 len = strlen(str);
4282 readline_set_completion_index(rs, len);
4283
4284 start = list = qmp_query_chardev(NULL);
4285 while (list) {
4286 ChardevInfo *chr_info = list->value;
4287
4288 if (!strncmp(chr_info->label, str, len)) {
4289 CharDriverState *chr = qemu_chr_find(chr_info->label);
4290 if (chr && chr_is_ringbuf(chr)) {
4291 readline_add_completion(rs, chr_info->label);
4292 }
4293 }
4294 list = list->next;
4295 }
4296 qapi_free_ChardevInfoList(start);
4297}
4298
Hani Benhabiles8e597772014-05-27 23:39:30 +01004299void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4300{
4301 if (nb_args != 2) {
4302 return;
4303 }
4304 ringbuf_completion(rs, str);
4305}
4306
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004307void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4308{
4309 size_t len;
4310
4311 if (nb_args != 2) {
4312 return;
4313 }
4314
4315 len = strlen(str);
4316 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004317 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004318}
4319
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004320void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004321{
4322 ObjectPropertyInfoList *list, *start;
4323 size_t len;
4324
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004325 if (nb_args != 2) {
4326 return;
4327 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004328 len = strlen(str);
4329 readline_set_completion_index(rs, len);
4330
4331 start = list = qmp_qom_list("/objects", NULL);
4332 while (list) {
4333 ObjectPropertyInfo *info = list->value;
4334
4335 if (!strncmp(info->type, "child<", 5)
4336 && !strncmp(info->name, str, len)) {
4337 readline_add_completion(rs, info->name);
4338 }
4339 list = list->next;
4340 }
4341 qapi_free_ObjectPropertyInfoList(start);
4342}
4343
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004344void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4345{
4346 int i;
4347 char *sep;
4348 size_t len;
4349
4350 if (nb_args != 2) {
4351 return;
4352 }
4353 sep = strrchr(str, '-');
4354 if (sep) {
4355 str = sep + 1;
4356 }
4357 len = strlen(str);
4358 readline_set_completion_index(rs, len);
4359 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4360 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4361 readline_add_completion(rs, QKeyCode_lookup[i]);
4362 }
4363 }
4364}
4365
Hani Benhabiles40d19392014-05-07 23:41:30 +01004366void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4367{
4368 size_t len;
4369
4370 len = strlen(str);
4371 readline_set_completion_index(rs, len);
4372 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004373 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004374 int count, i;
4375 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004376 NET_CLIENT_OPTIONS_KIND_NONE,
4377 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004378 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004379 const char *name = ncs[i]->name;
4380 if (!strncmp(str, name, len)) {
4381 readline_add_completion(rs, name);
4382 }
4383 }
4384 } else if (nb_args == 3) {
4385 add_completion_option(rs, str, "on");
4386 add_completion_option(rs, str, "off");
4387 }
4388}
4389
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004390void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4391{
4392 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004393 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004394
4395 if (nb_args != 2) {
4396 return;
4397 }
4398
4399 len = strlen(str);
4400 readline_set_completion_index(rs, len);
4401 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004402 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004403 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004404 QemuOpts *opts;
4405 const char *name = ncs[i]->name;
4406 if (strncmp(str, name, len)) {
4407 continue;
4408 }
4409 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4410 if (opts) {
4411 readline_add_completion(rs, name);
4412 }
4413 }
4414}
4415
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004416void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4417{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004418 int i;
4419
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004420 if (nb_args != 2) {
4421 return;
4422 }
4423 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004424 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4425 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4426 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004427}
4428
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004429void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4430 const char *str)
4431{
4432 size_t len;
4433
4434 len = strlen(str);
4435 readline_set_completion_index(rs, len);
4436 if (nb_args == 2) {
4437 int i;
4438 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4439 const char *name = MigrationCapability_lookup[i];
4440 if (!strncmp(str, name, len)) {
4441 readline_add_completion(rs, name);
4442 }
4443 }
4444 } else if (nb_args == 3) {
4445 add_completion_option(rs, str, "on");
4446 add_completion_option(rs, str, "off");
4447 }
4448}
4449
Liang Li50e9a622015-03-23 16:32:29 +08004450void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4451 const char *str)
4452{
4453 size_t len;
4454
4455 len = strlen(str);
4456 readline_set_completion_index(rs, len);
4457 if (nb_args == 2) {
4458 int i;
4459 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4460 const char *name = MigrationParameter_lookup[i];
4461 if (!strncmp(str, name, len)) {
4462 readline_add_completion(rs, name);
4463 }
4464 }
4465 }
4466}
4467
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004468void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4469{
4470 int i;
4471 size_t len;
4472 if (nb_args != 2) {
4473 return;
4474 }
4475 len = strlen(str);
4476 readline_set_completion_index(rs, len);
4477 for (i = 0; host_net_devices[i]; i++) {
4478 if (!strncmp(host_net_devices[i], str, len)) {
4479 readline_add_completion(rs, host_net_devices[i]);
4480 }
4481 }
4482}
4483
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004484void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4485{
Jason Wangeaed4832015-04-23 14:21:38 +08004486 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004487 int count, i, len;
4488
4489 len = strlen(str);
4490 readline_set_completion_index(rs, len);
4491 if (nb_args == 2) {
4492 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004493 NET_CLIENT_OPTIONS_KIND_NONE,
4494 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004495 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004496 int id;
4497 char name[16];
4498
4499 if (net_hub_id_for_client(ncs[i], &id)) {
4500 continue;
4501 }
4502 snprintf(name, sizeof(name), "%d", id);
4503 if (!strncmp(str, name, len)) {
4504 readline_add_completion(rs, name);
4505 }
4506 }
4507 return;
4508 } else if (nb_args == 3) {
4509 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004510 NET_CLIENT_OPTIONS_KIND_NIC,
4511 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004512 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004513 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004514 const char *name;
4515
Jason Wang2c4681f2015-02-02 15:06:38 +08004516 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4517 net_hub_id_for_client(ncs[i], &id)) {
4518 continue;
4519 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004520 name = ncs[i]->name;
4521 if (!strncmp(str, name, len)) {
4522 readline_add_completion(rs, name);
4523 }
4524 }
4525 return;
4526 }
4527}
4528
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004529static void vm_completion(ReadLineState *rs, const char *str)
4530{
4531 size_t len;
4532 BlockDriverState *bs = NULL;
4533
4534 len = strlen(str);
4535 readline_set_completion_index(rs, len);
4536 while ((bs = bdrv_next(bs))) {
4537 SnapshotInfoList *snapshots, *snapshot;
4538
4539 if (!bdrv_can_snapshot(bs)) {
4540 continue;
4541 }
4542 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4543 continue;
4544 }
4545 snapshot = snapshots;
4546 while (snapshot) {
4547 char *completion = snapshot->value->name;
4548 if (!strncmp(str, completion, len)) {
4549 readline_add_completion(rs, completion);
4550 }
4551 completion = snapshot->value->id;
4552 if (!strncmp(str, completion, len)) {
4553 readline_add_completion(rs, completion);
4554 }
4555 snapshot = snapshot->next;
4556 }
4557 qapi_free_SnapshotInfoList(snapshots);
4558 }
4559
4560}
4561
4562void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4563{
4564 if (nb_args == 2) {
4565 vm_completion(rs, str);
4566 }
4567}
4568
4569void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4570{
4571 if (nb_args == 2) {
4572 vm_completion(rs, str);
4573 }
4574}
4575
Wenchao Xiac35b6402013-08-27 20:38:24 +08004576static void monitor_find_completion_by_table(Monitor *mon,
4577 const mon_cmd_t *cmd_table,
4578 char **args,
4579 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004580{
4581 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004582 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004583 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004584 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004585 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004586
bellard81d09122004-07-14 17:21:37 +00004587 if (nb_args <= 1) {
4588 /* command completion */
4589 if (nb_args == 0)
4590 cmdname = "";
4591 else
4592 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004593 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004594 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004595 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004596 }
4597 } else {
4598 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004599 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004600 if (compare_cmd(args[0], cmd->name)) {
4601 break;
4602 }
bellard81d09122004-07-14 17:21:37 +00004603 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004604 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004605 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004606 }
4607
Wenchao Xiad903a772013-08-27 20:38:25 +08004608 if (cmd->sub_table) {
4609 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004610 monitor_find_completion_by_table(mon, cmd->sub_table,
4611 &args[1], nb_args - 1);
4612 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004613 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004614 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004615 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4616 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004617 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004618
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004619 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004620 for(i = 0; i < nb_args - 2; i++) {
4621 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004622 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004623 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004624 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004625 }
4626 }
4627 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004628 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004629 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004630 }
bellard81d09122004-07-14 17:21:37 +00004631 switch(*ptype) {
4632 case 'F':
4633 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004634 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004635 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004636 break;
4637 case 'B':
4638 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004639 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004640 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4641 name = bdrv_get_device_name(bs);
4642 if (str[0] == '\0' ||
4643 !strncmp(name, str, strlen(str))) {
4644 readline_add_completion(mon->rs, name);
4645 }
4646 }
bellard81d09122004-07-14 17:21:37 +00004647 break;
bellard7fe48482004-10-09 18:08:01 +00004648 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004649 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004650 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004651 monitor_find_completion_by_table(mon, cmd_table,
4652 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004653 }
4654 break;
bellard81d09122004-07-14 17:21:37 +00004655 default:
4656 break;
4657 }
4658 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004659}
4660
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004661static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004662 const char *cmdline)
4663{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004664 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004665 char *args[MAX_ARGS];
4666 int nb_args, len;
4667
4668 /* 1. parse the cmdline */
4669 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4670 return;
4671 }
4672#ifdef DEBUG_COMPLETION
Gonglei5fb9b5b2014-08-21 21:03:09 +08004673 {
4674 int i;
4675 for (i = 0; i < nb_args; i++) {
4676 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
4677 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004678 }
4679#endif
4680
4681 /* if the line ends with a space, it means we want to complete the
4682 next arg */
4683 len = strlen(cmdline);
4684 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4685 if (nb_args >= MAX_ARGS) {
4686 goto cleanup;
4687 }
4688 args[nb_args++] = g_strdup("");
4689 }
4690
4691 /* 2. auto complete according to args */
4692 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004693
4694cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004695 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004696}
4697
aliguori731b0362009-03-05 23:01:42 +00004698static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004699{
aliguori731b0362009-03-05 23:01:42 +00004700 Monitor *mon = opaque;
4701
Jan Kiszkac62313b2009-12-04 14:05:29 +01004702 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004703}
4704
Markus Armbruster40861822015-05-29 10:27:16 +02004705static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4706 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004707{
Eric Blake2d5a8342015-04-15 09:19:23 -06004708 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004709
4710 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004711 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4712 "Capabilities negotiation is already complete, command "
4713 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004714 return true;
4715 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004716 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004717 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4718 "Expecting capabilities negotiation with "
4719 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004720 return true;
4721 }
4722 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004723}
4724
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004725/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004726 * Argument validation rules:
4727 *
4728 * 1. The argument must exist in cmd_args qdict
4729 * 2. The argument type must be the expected one
4730 *
4731 * Special case: If the argument doesn't exist in cmd_args and
4732 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4733 * checking is skipped for it.
4734 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004735static void check_client_args_type(const QDict *client_args,
4736 const QDict *cmd_args, int flags,
4737 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004738{
4739 const QDictEntry *ent;
4740
4741 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4742 QObject *obj;
4743 QString *arg_type;
4744 const QObject *client_arg = qdict_entry_value(ent);
4745 const char *client_arg_name = qdict_entry_key(ent);
4746
4747 obj = qdict_get(cmd_args, client_arg_name);
4748 if (!obj) {
4749 if (flags & QMP_ACCEPT_UNKNOWNS) {
4750 /* handler accepts unknowns */
4751 continue;
4752 }
4753 /* client arg doesn't exist */
Markus Armbruster326283a2015-03-02 18:39:09 +01004754 error_set(errp, QERR_INVALID_PARAMETER, client_arg_name);
4755 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004756 }
4757
4758 arg_type = qobject_to_qstring(obj);
4759 assert(arg_type != NULL);
4760
4761 /* check if argument's type is correct */
4762 switch (qstring_get_str(arg_type)[0]) {
4763 case 'F':
4764 case 'B':
4765 case 's':
4766 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004767 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4768 client_arg_name, "string");
4769 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004770 }
4771 break;
4772 case 'i':
4773 case 'l':
4774 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004775 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004776 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004777 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4778 client_arg_name, "int");
4779 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004780 }
4781 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004782 case 'T':
4783 if (qobject_type(client_arg) != QTYPE_QINT &&
4784 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004785 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4786 client_arg_name, "number");
4787 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004788 }
4789 break;
4790 case 'b':
4791 case '-':
4792 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004793 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4794 client_arg_name, "bool");
4795 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004796 }
4797 break;
4798 case 'O':
4799 assert(flags & QMP_ACCEPT_UNKNOWNS);
4800 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004801 case 'q':
4802 /* Any QObject can be passed. */
4803 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004804 case '/':
4805 case '.':
4806 /*
4807 * These types are not supported by QMP and thus are not
4808 * handled here. Fall through.
4809 */
4810 default:
4811 abort();
4812 }
4813 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004814}
4815
4816/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004817 * - Check if the client has passed all mandatory args
4818 * - Set special flags for argument validation
4819 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004820static void check_mandatory_args(const QDict *cmd_args,
4821 const QDict *client_args, int *flags,
4822 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004823{
4824 const QDictEntry *ent;
4825
4826 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4827 const char *cmd_arg_name = qdict_entry_key(ent);
4828 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4829 assert(type != NULL);
4830
4831 if (qstring_get_str(type)[0] == 'O') {
4832 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4833 *flags |= QMP_ACCEPT_UNKNOWNS;
4834 } else if (qstring_get_str(type)[0] != '-' &&
4835 qstring_get_str(type)[1] != '?' &&
4836 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbruster326283a2015-03-02 18:39:09 +01004837 error_set(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4838 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004839 }
4840 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004841}
4842
4843static QDict *qdict_from_args_type(const char *args_type)
4844{
4845 int i;
4846 QDict *qdict;
4847 QString *key, *type, *cur_qs;
4848
4849 assert(args_type != NULL);
4850
4851 qdict = qdict_new();
4852
4853 if (args_type == NULL || args_type[0] == '\0') {
4854 /* no args, empty qdict */
4855 goto out;
4856 }
4857
4858 key = qstring_new();
4859 type = qstring_new();
4860
4861 cur_qs = key;
4862
4863 for (i = 0;; i++) {
4864 switch (args_type[i]) {
4865 case ',':
4866 case '\0':
4867 qdict_put(qdict, qstring_get_str(key), type);
4868 QDECREF(key);
4869 if (args_type[i] == '\0') {
4870 goto out;
4871 }
4872 type = qstring_new(); /* qdict has ref */
4873 cur_qs = key = qstring_new();
4874 break;
4875 case ':':
4876 cur_qs = type;
4877 break;
4878 default:
4879 qstring_append_chr(cur_qs, args_type[i]);
4880 break;
4881 }
4882 }
4883
4884out:
4885 return qdict;
4886}
4887
4888/*
4889 * Client argument checking rules:
4890 *
4891 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004892 * 2. Each argument provided by the client must be expected
4893 * 3. Each argument provided by the client must have the type expected
4894 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004895 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004896static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4897 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004898{
Markus Armbruster326283a2015-03-02 18:39:09 +01004899 Error *err = NULL;
4900 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004901 QDict *cmd_args;
4902
4903 cmd_args = qdict_from_args_type(cmd->args_type);
4904
4905 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004906 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004907 if (err) {
4908 goto out;
4909 }
4910
Markus Armbruster326283a2015-03-02 18:39:09 +01004911 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004912
4913out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004914 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004915 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004916}
4917
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004918/*
4919 * Input object checking rules
4920 *
4921 * 1. Input object must be a dict
4922 * 2. The "execute" key must exist
4923 * 3. The "execute" key must be a string
4924 * 4. If the "arguments" key exists, it must be a dict
4925 * 5. If the "id" key exists, it can be anything (ie. json-value)
4926 * 6. Any argument not listed above is considered invalid
4927 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004928static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004929{
4930 const QDictEntry *ent;
4931 int has_exec_key = 0;
4932 QDict *input_dict;
4933
4934 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004935 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004936 return NULL;
4937 }
4938
4939 input_dict = qobject_to_qdict(input_obj);
4940
4941 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4942 const char *arg_name = qdict_entry_key(ent);
4943 const QObject *arg_obj = qdict_entry_value(ent);
4944
4945 if (!strcmp(arg_name, "execute")) {
4946 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004947 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4948 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004949 return NULL;
4950 }
4951 has_exec_key = 1;
4952 } else if (!strcmp(arg_name, "arguments")) {
4953 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004954 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4955 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004956 return NULL;
4957 }
Markus Armbruster779cec42015-06-08 10:44:30 +02004958 } else if (!strcmp(arg_name, "id")) {
4959 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004960 } else {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004961 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004962 return NULL;
4963 }
4964 }
4965
4966 if (!has_exec_key) {
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004967 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004968 return NULL;
4969 }
4970
4971 return input_dict;
4972}
4973
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004974static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4975{
Markus Armbruster326283a2015-03-02 18:39:09 +01004976 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004977 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004978 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004979 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004980 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004981 Monitor *mon = cur_mon;
4982
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004983 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004984 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004985
4986 obj = json_parser_parse(tokens, NULL);
4987 if (!obj) {
4988 // FIXME: should be triggered in json_parser_parse()
Markus Armbruster710aec92015-03-06 11:28:00 +01004989 error_set(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004990 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004991 }
4992
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004993 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004994 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004995 qobject_decref(obj);
4996 goto err_out;
4997 }
4998
Markus Armbruster74358f22015-03-06 19:35:59 +01004999 mon->qmp.id = qdict_get(input, "id");
5000 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005001
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005002 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005003 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005004 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005005 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005006 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5007 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005008 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005009 }
Markus Armbruster40861822015-05-29 10:27:16 +02005010 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005011 goto err_out;
5012 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005013
5014 obj = qdict_get(input, "arguments");
5015 if (!obj) {
5016 args = qdict_new();
5017 } else {
5018 args = qobject_to_qdict(obj);
5019 QINCREF(args);
5020 }
5021
Markus Armbruster326283a2015-03-02 18:39:09 +01005022 qmp_check_client_args(cmd, args, &local_err);
5023 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005024 goto err_out;
5025 }
5026
Markus Armbruster84add862015-03-05 16:45:15 +01005027 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5028 /* Command failed... */
Markus Armbruster452e0302015-03-06 19:11:13 +01005029 if (!mon->error) {
Markus Armbruster84add862015-03-05 16:45:15 +01005030 /* ... without setting an error, so make one up */
Markus Armbruster710aec92015-03-06 11:28:00 +01005031 error_set(&local_err, QERR_UNDEFINED_ERROR);
Markus Armbruster84add862015-03-05 16:45:15 +01005032 }
5033 }
Markus Armbruster710aec92015-03-06 11:28:00 +01005034 if (mon->error) {
5035 error_set(&local_err, mon->error->err_class, "%s",
5036 mon->error->err_msg);
5037 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005038
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005039err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005040 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005041 qobject_decref(data);
Markus Armbruster70ea0c52015-03-06 10:47:08 +01005042 QDECREF(mon->error);
5043 mon->error = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005044 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005045 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005046}
5047
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005048static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005049{
5050 Monitor *old_mon = cur_mon;
5051
5052 cur_mon = opaque;
5053
Markus Armbruster74358f22015-03-06 19:35:59 +01005054 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005055
5056 cur_mon = old_mon;
5057}
5058
aliguori731b0362009-03-05 23:01:42 +00005059static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005060{
aliguori731b0362009-03-05 23:01:42 +00005061 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005062 int i;
aliguori376253e2009-03-05 23:01:23 +00005063
aliguori731b0362009-03-05 23:01:42 +00005064 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005065
aliguoricde76ee2009-03-05 23:01:51 +00005066 if (cur_mon->rs) {
5067 for (i = 0; i < size; i++)
5068 readline_handle_byte(cur_mon->rs, buf[i]);
5069 } else {
5070 if (size == 0 || buf[size - 1] != 0)
5071 monitor_printf(cur_mon, "corrupted command\n");
5072 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005073 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005074 }
aliguori731b0362009-03-05 23:01:42 +00005075
5076 cur_mon = old_mon;
5077}
aliguorid8f44602008-10-06 13:52:44 +00005078
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005079static void monitor_command_cb(void *opaque, const char *cmdline,
5080 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005081{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005082 Monitor *mon = opaque;
5083
aliguori731b0362009-03-05 23:01:42 +00005084 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005085 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005086 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005087}
5088
aliguoricde76ee2009-03-05 23:01:51 +00005089int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005090{
aliguoricde76ee2009-03-05 23:01:51 +00005091 if (!mon->rs)
5092 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005093 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005094 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005095}
5096
aliguori376253e2009-03-05 23:01:23 +00005097void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005098{
aliguoricde76ee2009-03-05 23:01:51 +00005099 if (!mon->rs)
5100 return;
aliguori731b0362009-03-05 23:01:42 +00005101 if (--mon->suspend_cnt == 0)
5102 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005103}
5104
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005105static QObject *get_qmp_greeting(void)
5106{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005107 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005108
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005109 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005110 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5111}
5112
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005113static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005114{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005115 QObject *data;
5116 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005117
Luiz Capitulino47116d12010-02-08 17:01:30 -02005118 switch (event) {
5119 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005120 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005121 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005122 monitor_json_emitter(mon, data);
5123 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005124 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005125 break;
5126 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005127 json_message_parser_destroy(&mon->qmp.parser);
5128 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005129 mon_refcount--;
5130 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005131 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005132 }
5133}
5134
aliguori731b0362009-03-05 23:01:42 +00005135static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005136{
aliguori376253e2009-03-05 23:01:23 +00005137 Monitor *mon = opaque;
5138
aliguori2724b182009-03-05 23:01:47 +00005139 switch (event) {
5140 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005141 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005142 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005143 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005144 if (mon->reset_seen) {
5145 readline_restart(mon->rs);
5146 monitor_resume(mon);
5147 monitor_flush(mon);
5148 } else {
5149 mon->suspend_cnt = 0;
5150 }
aliguori2724b182009-03-05 23:01:47 +00005151 break;
ths86e94de2007-01-05 22:01:59 +00005152
aliguori2724b182009-03-05 23:01:47 +00005153 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005154 if (mon->reset_seen) {
5155 if (mon->suspend_cnt == 0) {
5156 monitor_printf(mon, "\n");
5157 }
5158 monitor_flush(mon);
5159 monitor_suspend(mon);
5160 } else {
5161 mon->suspend_cnt++;
5162 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005163 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005164 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005165 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005166 break;
5167
Amit Shahb6b8df52009-10-07 18:31:16 +05305168 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005169 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5170 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005171 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005172 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005173 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005174 }
5175 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005176 mon_refcount++;
5177 break;
5178
5179 case CHR_EVENT_CLOSED:
5180 mon_refcount--;
5181 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005182 break;
5183 }
ths86e94de2007-01-05 22:01:59 +00005184}
5185
Wayne Xia816f8922011-10-12 11:32:41 +08005186static int
5187compare_mon_cmd(const void *a, const void *b)
5188{
5189 return strcmp(((const mon_cmd_t *)a)->name,
5190 ((const mon_cmd_t *)b)->name);
5191}
5192
5193static void sortcmdlist(void)
5194{
5195 int array_num;
5196 int elem_size = sizeof(mon_cmd_t);
5197
5198 array_num = sizeof(mon_cmds)/elem_size-1;
5199 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5200
5201 array_num = sizeof(info_cmds)/elem_size-1;
5202 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5203}
5204
aliguori76655d62009-03-06 20:27:37 +00005205
5206/*
5207 * Local variables:
5208 * c-indent-level: 4
5209 * c-basic-offset: 4
5210 * tab-width: 8
5211 * End:
5212 */
5213
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005214/* These functions just adapt the readline interface in a typesafe way. We
5215 * could cast function pointers but that discards compiler checks.
5216 */
Stefan Weild5d15072014-01-25 18:18:23 +01005217static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5218 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005219{
5220 va_list ap;
5221 va_start(ap, fmt);
5222 monitor_vprintf(opaque, fmt, ap);
5223 va_end(ap);
5224}
5225
5226static void monitor_readline_flush(void *opaque)
5227{
5228 monitor_flush(opaque);
5229}
5230
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005231static void __attribute__((constructor)) monitor_lock_init(void)
5232{
5233 qemu_mutex_init(&monitor_lock);
5234}
5235
aliguori731b0362009-03-05 23:01:42 +00005236void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005237{
aliguori731b0362009-03-05 23:01:42 +00005238 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005239 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005240
5241 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005242 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005243 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005244 is_first_init = 0;
5245 }
aliguori87127162009-03-05 23:01:29 +00005246
Wenchao Xiab01fe892013-08-27 20:38:19 +08005247 mon = g_malloc(sizeof(*mon));
5248 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005249
aliguori87127162009-03-05 23:01:29 +00005250 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005251 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005252 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005253 mon->rs = readline_init(monitor_readline_printf,
5254 monitor_readline_flush,
5255 mon,
5256 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005257 monitor_read_command(mon, 0);
5258 }
aliguori87127162009-03-05 23:01:29 +00005259
Markus Armbruster9f3982f2015-03-06 19:56:38 +01005260 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005261 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5262 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005263 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005264 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005265 } else {
5266 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5267 monitor_event, mon);
5268 }
aliguori87127162009-03-05 23:01:29 +00005269
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005270 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005271 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005272 qemu_mutex_unlock(&monitor_lock);
5273
Markus Armbruster8631b602010-02-18 11:41:55 +01005274 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5275 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00005276}
5277
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005278static void bdrv_password_cb(void *opaque, const char *password,
5279 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005280{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005281 Monitor *mon = opaque;
5282 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005283 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005284 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005285
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005286 bdrv_add_key(bs, password, &local_err);
5287 if (local_err) {
5288 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5289 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005290 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005291 }
aliguori731b0362009-03-05 23:01:42 +00005292 if (mon->password_completion_cb)
5293 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005294
aliguori731b0362009-03-05 23:01:42 +00005295 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005296}
aliguoric0f4ce72009-03-05 23:01:01 +00005297
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005298int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005299 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005300 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005301{
aliguoricde76ee2009-03-05 23:01:51 +00005302 int err;
5303
aliguori376253e2009-03-05 23:01:23 +00005304 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5305 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005306
aliguori731b0362009-03-05 23:01:42 +00005307 mon->password_completion_cb = completion_cb;
5308 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005309
aliguoricde76ee2009-03-05 23:01:51 +00005310 err = monitor_read_password(mon, bdrv_password_cb, bs);
5311
5312 if (err && completion_cb)
5313 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005314
5315 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005316}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005317
5318int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005319 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005320 void *opaque)
5321{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005322 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005323 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005324
Fam Zheng55606252015-03-02 19:36:46 +08005325 blk = blk_by_name(device);
5326 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005327 monitor_printf(mon, "Device not found %s\n", device);
5328 return -1;
5329 }
5330
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005331 bdrv_add_key(blk_bs(blk), NULL, &err);
5332 if (err) {
5333 error_free(err);
5334 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5335 }
5336
5337 if (completion_cb) {
5338 completion_cb(opaque, 0);
5339 }
5340 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005341}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005342
5343QemuOptsList qemu_mon_opts = {
5344 .name = "mon",
5345 .implied_opt_name = "chardev",
5346 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5347 .desc = {
5348 {
5349 .name = "mode",
5350 .type = QEMU_OPT_STRING,
5351 },{
5352 .name = "chardev",
5353 .type = QEMU_OPT_STRING,
5354 },{
5355 .name = "default",
5356 .type = QEMU_OPT_BOOL,
5357 },{
5358 .name = "pretty",
5359 .type = QEMU_OPT_BOOL,
5360 },
5361 { /* end of list */ }
5362 },
5363};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005364
5365#ifndef TARGET_I386
5366void qmp_rtc_reset_reinjection(Error **errp)
5367{
5368 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5369}
5370#endif