blob: 3deba38105838aa3deb8631f145f2e80433f538d [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010052#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010053#include "qapi/qmp/qint.h"
54#include "qapi/qmp/qfloat.h"
55#include "qapi/qmp/qlist.h"
56#include "qapi/qmp/qbool.h"
57#include "qapi/qmp/qstring.h"
58#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010061#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010062#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000063#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010064#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020066#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020067#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053068#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010069#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010070#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050071#include "qmp-commands.h"
72#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020075#include "qapi/qmp-event.h"
76#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080077#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000078
Markus Armbruster1ce6be22015-02-06 14:18:24 +010079/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020080#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010081#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020082#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010083#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020084
Jason J. Hernea4538a52015-06-26 14:07:21 -040085#if defined(TARGET_S390X)
86#include "hw/s390x/storage-keys.h"
87#endif
88
bellard9307c4c2004-04-04 12:57:25 +000089/*
90 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000091 *
bellard9307c4c2004-04-04 12:57:25 +000092 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000093 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000094 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080095 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010096 * 'O' option string of the form NAME=VALUE,...
97 * parsed according to QemuOptsList given by its name
98 * Example: 'device:O' uses qemu_device_opts.
99 * Restriction: only lists with empty desc are supported
100 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000101 * 'i' 32 bit integer
102 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300103 * 'M' Non-negative target long (32 or 64 bit), in user mode the
104 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200105 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200106 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
107 * K, k suffix, which multiplies the value by 2^60 for suffixes E
108 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
109 * 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 +0100110 * 'T' double
111 * user mode accepts an optional ms, us, ns suffix,
112 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000113 * '/' optional gdb-like print format (like "/10x")
114 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300115 * '?' optional type (for all types, except '/')
116 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100117 * 'b' boolean
118 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300119 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000120 *
121 */
122
Anthony Liguoric227f092009-10-01 16:12:16 -0500123typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000125 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000126 const char *params;
127 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300128 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300129 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100130 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800132 /* @sub_table is a list of 2nd level of commands. If it do not exist,
133 * mhandler should be used. If it exist, sub_table[?].mhandler should be
134 * used, and mhandler of 1st level plays the role of help function.
135 */
136 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100137 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500138} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000139
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500141typedef struct mon_fd_t mon_fd_t;
142struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143 char *name;
144 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500145 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146};
147
Corey Bryantba1c0482012-08-14 16:43:43 -0400148/* file descriptor associated with a file descriptor set */
149typedef struct MonFdsetFd MonFdsetFd;
150struct MonFdsetFd {
151 int fd;
152 bool removed;
153 char *opaque;
154 QLIST_ENTRY(MonFdsetFd) next;
155};
156
157/* file descriptor set containing fds passed via SCM_RIGHTS */
158typedef struct MonFdset MonFdset;
159struct MonFdset {
160 int64_t id;
161 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400162 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400163 QLIST_ENTRY(MonFdset) next;
164};
165
Markus Armbruster74358f22015-03-06 19:35:59 +0100166typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200167 QObject *id;
168 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100169 /*
170 * When a client connects, we're in capabilities negotiation mode.
171 * When command qmp_capabilities succeeds, we go into command
172 * mode.
173 */
174 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100175} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200176
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100177/*
178 * To prevent flooding clients, events can be throttled. The
179 * throttling is calculated globally, rather than per-Monitor
180 * instance.
181 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200182typedef struct MonitorQAPIEventState {
183 QAPIEvent event; /* Event being tracked */
184 int64_t rate; /* Minimum time (in ns) between two events */
185 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100186 QEMUTimer *timer; /* Timer for handling delayed events */
187 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200188} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100189
aliguori87127162009-03-05 23:01:29 +0000190struct Monitor {
191 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200192 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000193 int flags;
194 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400195 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200196
197 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400198 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200199 guint out_watch;
200
201 /* Read under either BQL or out_lock, written with BQL+out_lock. */
202 int mux_out;
203
aliguori731b0362009-03-05 23:01:42 +0000204 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100205 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200206 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200207 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000208 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800209 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500210 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000211 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000212};
213
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300214/* QMP checker flags */
215#define QMP_ACCEPT_UNKNOWNS 1
216
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200217/* Protects mon_list, monitor_event_state. */
218static QemuMutex monitor_lock;
219
Blue Swirl72cf2d42009-09-12 07:36:22 +0000220static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400221static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400222static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000223
Wayne Xia816f8922011-10-12 11:32:41 +0800224static mon_cmd_t mon_cmds[];
225static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000226
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300227static const mon_cmd_t qmp_cmds[];
228
Markus Armbruster8631b602010-02-18 11:41:55 +0100229Monitor *cur_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
Markus Armbruster485febc2015-03-13 17:25:50 +0100570static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200571{
Markus Armbruster485febc2015-03-13 17:25:50 +0100572 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200573}
574
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100575static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200576
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577static void monitor_data_init(Monitor *mon)
578{
579 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200580 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800582 /* Use *mon_cmds by default. */
583 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800584}
585
586static void monitor_data_destroy(Monitor *mon)
587{
588 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200589 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590}
591
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400599 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
601 old_mon = cur_mon;
602 cur_mon = &hmp;
603
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 if (ret < 0) {
607 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 goto out;
611 }
612 }
613
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100614 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200615 cur_mon = old_mon;
616
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200617 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 } else {
621 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200623 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
625out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628}
629
bellard9dc39cb2004-03-14 21:38:27 +0000630static int compare_cmd(const char *name, const char *list)
631{
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 return 0;
648}
649
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800650static int get_str(char *buf, int buf_size, const char **pp)
651{
652 const char *p;
653 char *q;
654 int c;
655
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
660 }
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
666 }
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100685 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800686 goto fail;
687 }
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
690 }
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
694 }
695 p++;
696 }
697 }
698 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100699 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800700 goto fail;
701 }
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
707 }
708 p++;
709 }
710 }
711 *q = '\0';
712 *pp = p;
713 return 0;
714}
715
716#define MAX_ARGS 16
717
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800718static void free_cmdline_args(char **args, int nb_args)
719{
720 int i;
721
722 assert(nb_args <= MAX_ARGS);
723
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
726 }
727
728}
729
730/*
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
736 *
737 * Returns 0 on success, negative on failure.
738 *
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
742 */
743static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800745{
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
749
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
755 }
756 if (*p == '\0') {
757 break;
758 }
759 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 }
762 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 if (ret < 0) {
764 goto fail;
765 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 args[nb_args] = g_strdup(buf);
767 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 }
769 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770 return 0;
771
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800775}
776
Wenchao Xia66855492013-08-27 20:38:23 +0800777static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
781{
782 int i;
783
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
786 }
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788}
789
790/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500791static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800792 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000793{
Anthony Liguoric227f092009-10-01 16:12:16 -0500794 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000795
Wenchao Xia66855492013-08-27 20:38:23 +0800796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
800 }
801 return;
802 }
803
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
813 }
814 break;
815 }
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
817}
818
aliguori376253e2009-03-05 23:01:23 +0000819static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000820{
Wenchao Xia66855492013-08-27 20:38:23 +0800821 char *args[MAX_ARGS];
822 int nb_args = 0;
823
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000833 }
Wenchao Xia66855492013-08-27 20:38:23 +0800834 return;
835 }
836
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
bellardf193c792004-03-21 17:06:25 +0000839 }
bellard9dc39cb2004-03-14 21:38:27 +0000840 }
Wenchao Xia66855492013-08-27 20:38:23 +0800841
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
844
845 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000846}
847
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300849{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854{
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000858
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
860 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100861 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000862 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530863}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100864
Michael Rothc45a8162011-10-02 08:44:37 -0500865#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100866static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100867{
868 const char *op = qdict_get_try_str(qdict, "op");
869 const char *arg = qdict_get_try_str(qdict, "arg");
870
871 if (!op) {
872 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
873 } else if (!strcmp(op, "on")) {
874 st_set_trace_file_enabled(true);
875 } else if (!strcmp(op, "off")) {
876 st_set_trace_file_enabled(false);
877 } else if (!strcmp(op, "flush")) {
878 st_flush_trace_buffer();
879 } else if (!strcmp(op, "set")) {
880 if (arg) {
881 st_set_trace_file(arg);
882 }
883 } else {
884 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
885 help_cmd(mon, "trace-file");
886 }
887}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530888#endif
889
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100890static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000891{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300892 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000893}
894
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300895CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000896{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300897 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200898 const mon_cmd_t *cmd;
899
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300900 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200901 info = g_malloc0(sizeof(*info));
902 info->value = g_malloc0(sizeof(*info->value));
903 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300905 info->next = cmd_list;
906 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907 }
908
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300909 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000910}
911
Daniel P. Berrange48608532012-05-21 17:59:51 +0100912EventInfoList *qmp_query_events(Error **errp)
913{
914 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200915 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100916
Wenchao Xia75175172014-06-18 08:43:54 +0200917 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
918 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100919 assert(event_name != NULL);
920 info = g_malloc0(sizeof(*info));
921 info->value = g_malloc0(sizeof(*info->value));
922 info->value->name = g_strdup(event_name);
923
924 info->next = ev_list;
925 ev_list = info;
926 }
927
928 return ev_list;
929}
930
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300931/* set the current CPU defined by the user */
932int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000933{
Andreas Färber55e5c282012-12-17 06:18:02 +0100934 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000935
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100936 cpu = qemu_get_cpu(cpu_index);
937 if (cpu == NULL) {
938 return -1;
bellard6a00d602005-11-21 23:25:50 +0000939 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200940 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100941 return 0;
bellard6a00d602005-11-21 23:25:50 +0000942}
943
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700944static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000945{
aliguori731b0362009-03-05 23:01:42 +0000946 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300947 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000948 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300949 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700950 return cur_mon->mon_cpu;
951}
952
953static CPUArchState *mon_get_cpu_env(void)
954{
955 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000956}
957
Luiz Capitulino99b77962011-10-24 10:53:44 -0200958int monitor_get_cpu_index(void)
959{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700960 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200961}
962
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100963static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000964{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700965 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000966}
967
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100968static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000969{
aliguori376253e2009-03-05 23:01:23 +0000970 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200971 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000972}
973
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100974static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300975{
976 dump_opcount_info((FILE *)mon, monitor_fprintf);
977}
978
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100979static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000980{
981 int i;
bellard7e2515e2004-08-01 21:52:19 +0000982 const char *str;
ths3b46e622007-09-17 08:09:54 +0000983
aliguoricde76ee2009-03-05 23:01:51 +0000984 if (!mon->rs)
985 return;
bellard7e2515e2004-08-01 21:52:19 +0000986 i = 0;
987 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000988 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000989 if (!str)
990 break;
aliguori376253e2009-03-05 23:01:23 +0000991 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000992 i++;
bellardaa455482004-04-04 13:07:25 +0000993 }
994}
995
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100996static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000997{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700998 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000999}
j_mayer76a66252007-03-07 08:32:30 +00001000
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001001static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301002{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001003 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1004 TraceEventInfoList *elem;
1005
1006 for (elem = events; elem != NULL; elem = elem->next) {
1007 monitor_printf(mon, "%s : state %u\n",
1008 elem->value->name,
1009 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1010 }
1011 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301012}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301013
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001014void qmp_client_migrate_info(const char *protocol, const char *hostname,
1015 bool has_port, int64_t port,
1016 bool has_tls_port, int64_t tls_port,
1017 bool has_cert_subject, const char *cert_subject,
1018 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001019{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001020 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001021 if (!qemu_using_spice(errp)) {
1022 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001023 }
1024
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001025 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001026 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001027 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001028 }
1029
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001030 if (qemu_spice_migrate_info(hostname,
1031 has_port ? port : -1,
1032 has_tls_port ? tls_port : -1,
1033 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001034 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001035 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001036 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001037 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001038 }
1039
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001040 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001041}
1042
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001043static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001044{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001045 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001046}
1047
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001048static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001049{
1050 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001051 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001052
bellard9307c4c2004-04-04 12:57:25 +00001053 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001054 mask = 0;
1055 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001056 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001057 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001058 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001059 return;
1060 }
1061 }
Peter Maydell24537a02013-02-11 16:41:23 +00001062 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001063}
1064
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001065static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001066{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001067 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001068 if (!option || !strcmp(option, "on")) {
1069 singlestep = 1;
1070 } else if (!strcmp(option, "off")) {
1071 singlestep = 0;
1072 } else {
1073 monitor_printf(mon, "unexpected option %s\n", option);
1074 }
1075}
1076
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001077static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001078{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001079 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001080 if (!device)
1081 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1082 if (gdbserver_start(device) < 0) {
1083 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1084 device);
1085 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001086 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001087 } else {
aliguori59030a82009-04-05 18:43:41 +00001088 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1089 device);
bellard8a7ddc32004-03-31 19:00:16 +00001090 }
1091}
1092
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001093static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001095 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001096 if (select_watchdog_action(action) == -1) {
1097 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1098 }
1099}
1100
aliguori376253e2009-03-05 23:01:23 +00001101static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001102{
aliguori376253e2009-03-05 23:01:23 +00001103 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001104 switch(c) {
1105 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001107 break;
1108 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001109 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001110 break;
1111 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001112 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001113 break;
1114 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001116 break;
1117 default:
1118 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001120 } else {
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001122 }
1123 break;
1124 }
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001126}
1127
aliguori376253e2009-03-05 23:01:23 +00001128static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001129 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001130{
Blue Swirl23842aa2010-01-12 20:27:43 +00001131 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001132 uint8_t buf[16];
1133 uint64_t v;
1134
1135 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001136 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001137#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001138 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001139 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001140 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001141 } else if (wsize == 4) {
1142 flags = 0;
1143 } else {
bellard6a15fd12006-04-12 21:07:07 +00001144 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001145 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001146 if (env) {
1147#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001148 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001149 (env->segs[R_CS].flags & DESC_L_MASK))
1150 flags = 2;
1151 else
1152#endif
1153 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1154 flags = 1;
1155 }
bellard4c27ba22004-04-25 18:05:08 +00001156 }
1157#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001158#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001159 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001160 flags = msr_le << 16;
1161 flags |= env->bfd_mach;
1162#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001163 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001164 return;
1165 }
1166
1167 len = wsize * count;
1168 if (wsize == 1)
1169 line_size = 8;
1170 else
1171 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001172 max_digits = 0;
1173
1174 switch(format) {
1175 case 'o':
1176 max_digits = (wsize * 8 + 2) / 3;
1177 break;
1178 default:
1179 case 'x':
1180 max_digits = (wsize * 8) / 4;
1181 break;
1182 case 'u':
1183 case 'd':
1184 max_digits = (wsize * 8 * 10 + 32) / 33;
1185 break;
1186 case 'c':
1187 wsize = 1;
1188 break;
1189 }
1190
1191 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001192 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001194 else
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001196 l = len;
1197 if (l > line_size)
1198 l = line_size;
1199 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001200 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001201 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001202 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001204 break;
1205 }
bellard9307c4c2004-04-04 12:57:25 +00001206 }
ths5fafdf22007-09-16 21:08:06 +00001207 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001208 while (i < l) {
1209 switch(wsize) {
1210 default:
1211 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001212 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001213 break;
1214 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001215 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001216 break;
1217 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001218 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001219 break;
1220 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001221 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001222 break;
1223 }
aliguori376253e2009-03-05 23:01:23 +00001224 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001225 switch(format) {
1226 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001234 break;
1235 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001240 break;
1241 }
1242 i += wsize;
1243 }
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001245 addr += l;
1246 len -= l;
1247 }
1248}
1249
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001250static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001251{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001252 int count = qdict_get_int(qdict, "count");
1253 int format = qdict_get_int(qdict, "format");
1254 int size = qdict_get_int(qdict, "size");
1255 target_long addr = qdict_get_int(qdict, "addr");
1256
aliguori376253e2009-03-05 23:01:23 +00001257 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001258}
1259
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001260static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001261{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001262 int count = qdict_get_int(qdict, "count");
1263 int format = qdict_get_int(qdict, "format");
1264 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001265 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001266
aliguori376253e2009-03-05 23:01:23 +00001267 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001268}
1269
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001270static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001271{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001272 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001273 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001274
bellard9307c4c2004-04-04 12:57:25 +00001275 switch(format) {
1276 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001277 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001278 break;
1279 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001281 break;
1282 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001283 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 default:
1286 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001287 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 }
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001294}
1295
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001296static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001297{
1298 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001299 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001300 uint32_t start = qdict_get_int(qdict, "start");
1301 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001302
1303 sum = 0;
1304 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001305 uint8_t val = address_space_ldub(&address_space_memory, addr,
1306 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001307 /* BSD sum algorithm ('sum' Unix command) */
1308 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001309 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001310 }
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001312}
1313
bellard13224a82006-07-14 22:03:35 +00001314static int mouse_button_state;
1315
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001316static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001317{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001318 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001319 const char *dx_str = qdict_get_str(qdict, "dx_str");
1320 const char *dy_str = qdict_get_str(qdict, "dy_str");
1321 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001322
bellard13224a82006-07-14 22:03:35 +00001323 dx = strtol(dx_str, NULL, 0);
1324 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001325 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1326 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1327
1328 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001329 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001330 if (dz != 0) {
1331 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1332 qemu_input_queue_btn(NULL, button, true);
1333 qemu_input_event_sync();
1334 qemu_input_queue_btn(NULL, button, false);
1335 }
1336 }
1337 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001338}
1339
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001340static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001341{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001342 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1343 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1344 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1345 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1346 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001347 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001348
1349 if (mouse_button_state == button_state) {
1350 return;
1351 }
1352 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1353 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001354 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001355}
1356
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001357static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001358{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001359 int size = qdict_get_int(qdict, "size");
1360 int addr = qdict_get_int(qdict, "addr");
1361 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001362 uint32_t val;
1363 int suffix;
1364
1365 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001366 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001367 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001368 addr++;
1369 }
1370 addr &= 0xffff;
1371
1372 switch(size) {
1373 default:
1374 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001375 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001376 suffix = 'b';
1377 break;
1378 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001379 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001380 suffix = 'w';
1381 break;
1382 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001383 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001384 suffix = 'l';
1385 break;
1386 }
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1388 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001389}
bellarda3a91a32004-06-04 11:06:21 +00001390
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001391static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001392{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001393 int size = qdict_get_int(qdict, "size");
1394 int addr = qdict_get_int(qdict, "addr");
1395 int val = qdict_get_int(qdict, "val");
1396
Jan Kiszkaf1147842009-07-14 10:20:11 +02001397 addr &= IOPORTS_MASK;
1398
1399 switch (size) {
1400 default:
1401 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001402 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001403 break;
1404 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001405 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001406 break;
1407 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001408 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001409 break;
1410 }
1411}
1412
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001413static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001414{
Gongleif1839932014-12-03 18:20:58 +00001415 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001416 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001417
Gongleif1839932014-12-03 18:20:58 +00001418 qemu_boot_set(bootdevice, &local_err);
1419 if (local_err) {
1420 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1421 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001422 } else {
Gongleif1839932014-12-03 18:20:58 +00001423 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001424 }
1425}
1426
bellardb86bda52004-09-18 19:32:46 +00001427#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001428static void print_pte(Monitor *mon, hwaddr addr,
1429 hwaddr pte,
1430 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001431{
Blue Swirld65aaf32010-12-11 18:56:24 +00001432#ifdef TARGET_X86_64
1433 if (addr & (1ULL << 47)) {
1434 addr |= -1LL << 48;
1435 }
1436#endif
1437 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1438 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001439 addr,
1440 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001441 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001442 pte & PG_GLOBAL_MASK ? 'G' : '-',
1443 pte & PG_PSE_MASK ? 'P' : '-',
1444 pte & PG_DIRTY_MASK ? 'D' : '-',
1445 pte & PG_ACCESSED_MASK ? 'A' : '-',
1446 pte & PG_PCD_MASK ? 'C' : '-',
1447 pte & PG_PWT_MASK ? 'T' : '-',
1448 pte & PG_USER_MASK ? 'U' : '-',
1449 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001450}
1451
Andreas Färber9349b4f2012-03-14 01:38:32 +01001452static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001453{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001454 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001455 uint32_t pgd, pde, pte;
1456
bellardb86bda52004-09-18 19:32:46 +00001457 pgd = env->cr[3] & ~0xfff;
1458 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001459 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001460 pde = le32_to_cpu(pde);
1461 if (pde & PG_PRESENT_MASK) {
1462 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001463 /* 4M pages */
1464 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001465 } else {
1466 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001467 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001468 pte = le32_to_cpu(pte);
1469 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001470 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001471 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001472 ~0xfff);
1473 }
1474 }
1475 }
1476 }
1477 }
1478}
1479
Andreas Färber9349b4f2012-03-14 01:38:32 +01001480static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001481{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001482 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001483 uint64_t pdpe, pde, pte;
1484 uint64_t pdp_addr, pd_addr, pt_addr;
1485
1486 pdp_addr = env->cr[3] & ~0x1f;
1487 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001488 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001489 pdpe = le64_to_cpu(pdpe);
1490 if (pdpe & PG_PRESENT_MASK) {
1491 pd_addr = pdpe & 0x3fffffffff000ULL;
1492 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001493 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001494 pde = le64_to_cpu(pde);
1495 if (pde & PG_PRESENT_MASK) {
1496 if (pde & PG_PSE_MASK) {
1497 /* 2M pages with PAE, CR4.PSE is ignored */
1498 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001499 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001500 } else {
1501 pt_addr = pde & 0x3fffffffff000ULL;
1502 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001503 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001504 pte = le64_to_cpu(pte);
1505 if (pte & PG_PRESENT_MASK) {
1506 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1507 + (l3 << 12),
1508 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001509 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001510 }
1511 }
1512 }
1513 }
1514 }
1515 }
1516 }
1517}
1518
1519#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001520static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001521{
1522 uint64_t l1, l2, l3, l4;
1523 uint64_t pml4e, pdpe, pde, pte;
1524 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1525
1526 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1527 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001528 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001529 pml4e = le64_to_cpu(pml4e);
1530 if (pml4e & PG_PRESENT_MASK) {
1531 pdp_addr = pml4e & 0x3fffffffff000ULL;
1532 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001533 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001534 pdpe = le64_to_cpu(pdpe);
1535 if (pdpe & PG_PRESENT_MASK) {
1536 if (pdpe & PG_PSE_MASK) {
1537 /* 1G pages, CR4.PSE is ignored */
1538 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1539 0x3ffffc0000000ULL);
1540 } else {
1541 pd_addr = pdpe & 0x3fffffffff000ULL;
1542 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001543 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001544 pde = le64_to_cpu(pde);
1545 if (pde & PG_PRESENT_MASK) {
1546 if (pde & PG_PSE_MASK) {
1547 /* 2M pages, CR4.PSE is ignored */
1548 print_pte(mon, (l1 << 39) + (l2 << 30) +
1549 (l3 << 21), pde,
1550 0x3ffffffe00000ULL);
1551 } else {
1552 pt_addr = pde & 0x3fffffffff000ULL;
1553 for (l4 = 0; l4 < 512; l4++) {
1554 cpu_physical_memory_read(pt_addr
1555 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001556 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001557 pte = le64_to_cpu(pte);
1558 if (pte & PG_PRESENT_MASK) {
1559 print_pte(mon, (l1 << 39) +
1560 (l2 << 30) +
1561 (l3 << 21) + (l4 << 12),
1562 pte & ~PG_PSE_MASK,
1563 0x3fffffffff000ULL);
1564 }
1565 }
1566 }
1567 }
1568 }
1569 }
1570 }
1571 }
1572 }
1573 }
1574}
1575#endif
1576
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001577static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001578{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001579 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001580
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001581 env = mon_get_cpu_env();
Blue Swirld65aaf32010-12-11 18:56:24 +00001582
1583 if (!(env->cr[0] & CR0_PG_MASK)) {
1584 monitor_printf(mon, "PG disabled\n");
1585 return;
1586 }
1587 if (env->cr[4] & CR4_PAE_MASK) {
1588#ifdef TARGET_X86_64
1589 if (env->hflags & HF_LMA_MASK) {
1590 tlb_info_64(mon, env);
1591 } else
1592#endif
1593 {
1594 tlb_info_pae32(mon, env);
1595 }
1596 } else {
1597 tlb_info_32(mon, env);
1598 }
1599}
1600
Avi Kivitya8170e52012-10-23 12:30:10 +02001601static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001602 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001603 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001604{
bellard9746b152004-11-11 18:30:24 +00001605 int prot1;
1606 prot1 = *plast_prot;
1607 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001608 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001609 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1610 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001611 *pstart, end, end - *pstart,
1612 prot1 & PG_USER_MASK ? 'u' : '-',
1613 'r',
1614 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001615 }
1616 if (prot != 0)
1617 *pstart = end;
1618 else
1619 *pstart = -1;
1620 *plast_prot = prot;
1621 }
1622}
1623
Andreas Färber9349b4f2012-03-14 01:38:32 +01001624static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001625{
Austin Clementsb49ca722011-08-14 23:19:21 -04001626 unsigned int l1, l2;
1627 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001628 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001629 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001630
bellardb86bda52004-09-18 19:32:46 +00001631 pgd = env->cr[3] & ~0xfff;
1632 last_prot = 0;
1633 start = -1;
1634 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001635 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001636 pde = le32_to_cpu(pde);
1637 end = l1 << 22;
1638 if (pde & PG_PRESENT_MASK) {
1639 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1640 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001641 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001642 } else {
1643 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001644 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001645 pte = le32_to_cpu(pte);
1646 end = (l1 << 22) + (l2 << 12);
1647 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001648 prot = pte & pde &
1649 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001650 } else {
1651 prot = 0;
1652 }
aliguori376253e2009-03-05 23:01:23 +00001653 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001654 }
1655 }
1656 } else {
1657 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001658 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001659 }
1660 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001661 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001662 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001663}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001664
Andreas Färber9349b4f2012-03-14 01:38:32 +01001665static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001666{
Austin Clementsb49ca722011-08-14 23:19:21 -04001667 unsigned int l1, l2, l3;
1668 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001669 uint64_t pdpe, pde, pte;
1670 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001671 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001672
1673 pdp_addr = env->cr[3] & ~0x1f;
1674 last_prot = 0;
1675 start = -1;
1676 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001677 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001678 pdpe = le64_to_cpu(pdpe);
1679 end = l1 << 30;
1680 if (pdpe & PG_PRESENT_MASK) {
1681 pd_addr = pdpe & 0x3fffffffff000ULL;
1682 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001683 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001684 pde = le64_to_cpu(pde);
1685 end = (l1 << 30) + (l2 << 21);
1686 if (pde & PG_PRESENT_MASK) {
1687 if (pde & PG_PSE_MASK) {
1688 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1689 PG_PRESENT_MASK);
1690 mem_print(mon, &start, &last_prot, end, prot);
1691 } else {
1692 pt_addr = pde & 0x3fffffffff000ULL;
1693 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001694 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001695 pte = le64_to_cpu(pte);
1696 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1697 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001698 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1699 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001700 } else {
1701 prot = 0;
1702 }
1703 mem_print(mon, &start, &last_prot, end, prot);
1704 }
1705 }
1706 } else {
1707 prot = 0;
1708 mem_print(mon, &start, &last_prot, end, prot);
1709 }
1710 }
1711 } else {
1712 prot = 0;
1713 mem_print(mon, &start, &last_prot, end, prot);
1714 }
1715 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001716 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001717 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001718}
1719
1720
1721#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001722static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001723{
1724 int prot, last_prot;
1725 uint64_t l1, l2, l3, l4;
1726 uint64_t pml4e, pdpe, pde, pte;
1727 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1728
1729 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1730 last_prot = 0;
1731 start = -1;
1732 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001733 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001734 pml4e = le64_to_cpu(pml4e);
1735 end = l1 << 39;
1736 if (pml4e & PG_PRESENT_MASK) {
1737 pdp_addr = pml4e & 0x3fffffffff000ULL;
1738 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001739 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001740 pdpe = le64_to_cpu(pdpe);
1741 end = (l1 << 39) + (l2 << 30);
1742 if (pdpe & PG_PRESENT_MASK) {
1743 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001744 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1745 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001746 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001747 mem_print(mon, &start, &last_prot, end, prot);
1748 } else {
1749 pd_addr = pdpe & 0x3fffffffff000ULL;
1750 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001751 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001752 pde = le64_to_cpu(pde);
1753 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1754 if (pde & PG_PRESENT_MASK) {
1755 if (pde & PG_PSE_MASK) {
1756 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1757 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001758 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001759 mem_print(mon, &start, &last_prot, end, prot);
1760 } else {
1761 pt_addr = pde & 0x3fffffffff000ULL;
1762 for (l4 = 0; l4 < 512; l4++) {
1763 cpu_physical_memory_read(pt_addr
1764 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001765 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001766 pte = le64_to_cpu(pte);
1767 end = (l1 << 39) + (l2 << 30) +
1768 (l3 << 21) + (l4 << 12);
1769 if (pte & PG_PRESENT_MASK) {
1770 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1771 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001772 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001773 } else {
1774 prot = 0;
1775 }
1776 mem_print(mon, &start, &last_prot, end, prot);
1777 }
1778 }
1779 } else {
1780 prot = 0;
1781 mem_print(mon, &start, &last_prot, end, prot);
1782 }
1783 }
1784 }
1785 } else {
1786 prot = 0;
1787 mem_print(mon, &start, &last_prot, end, prot);
1788 }
1789 }
1790 } else {
1791 prot = 0;
1792 mem_print(mon, &start, &last_prot, end, prot);
1793 }
1794 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001795 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001796 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001797}
1798#endif
1799
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001800static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001801{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001802 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001803
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001804 env = mon_get_cpu_env();
Blue Swirl1b3cba62010-12-11 18:56:27 +00001805
1806 if (!(env->cr[0] & CR0_PG_MASK)) {
1807 monitor_printf(mon, "PG disabled\n");
1808 return;
1809 }
1810 if (env->cr[4] & CR4_PAE_MASK) {
1811#ifdef TARGET_X86_64
1812 if (env->hflags & HF_LMA_MASK) {
1813 mem_info_64(mon, env);
1814 } else
1815#endif
1816 {
1817 mem_info_pae32(mon, env);
1818 }
1819 } else {
1820 mem_info_32(mon, env);
1821 }
1822}
bellardb86bda52004-09-18 19:32:46 +00001823#endif
1824
aurel327c664e22009-03-03 06:12:22 +00001825#if defined(TARGET_SH4)
1826
aliguori376253e2009-03-05 23:01:23 +00001827static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001828{
aliguori376253e2009-03-05 23:01:23 +00001829 monitor_printf(mon, " tlb%i:\t"
1830 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1831 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1832 "dirty=%hhu writethrough=%hhu\n",
1833 idx,
1834 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1835 tlb->v, tlb->sh, tlb->c, tlb->pr,
1836 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001837}
1838
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001839static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001840{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001841 CPUArchState *env = mon_get_cpu_env();
aurel327c664e22009-03-03 06:12:22 +00001842 int i;
1843
aliguori376253e2009-03-05 23:01:23 +00001844 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001845 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001846 print_tlb (mon, i, &env->itlb[i]);
1847 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001848 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001849 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001850}
1851
1852#endif
1853
Max Filippov692f7372012-01-07 20:02:40 +04001854#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001855static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001856{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001857 CPUArchState *env1 = mon_get_cpu_env();
Blue Swirld41160a2010-12-19 13:42:56 +00001858
1859 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1860}
1861#endif
1862
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001863static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001864{
1865 mtree_info((fprintf_function)monitor_printf, mon);
1866}
1867
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001868static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001869{
aliguorib28b6232009-04-22 20:20:29 +00001870 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001871 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001872 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001873
zhanghailiang5b009e42014-11-04 19:49:30 +08001874 node_mem = g_new0(uint64_t, nb_numa_nodes);
1875 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001876 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1877 for (i = 0; i < nb_numa_nodes; i++) {
1878 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001879 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001880 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001881 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001882 }
1883 }
1884 monitor_printf(mon, "\n");
1885 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001886 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001887 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001888 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001889}
1890
bellard5f1ce942006-02-08 22:40:15 +00001891#ifdef CONFIG_PROFILER
1892
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001893int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001894int64_t dev_time;
1895
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001896static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001897{
aliguori376253e2009-03-05 23:01:23 +00001898 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001899 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001900 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001901 tcg_time, tcg_time / (double)get_ticks_per_sec());
1902 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001903 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001904}
1905#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001906static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001907{
aliguori376253e2009-03-05 23:01:23 +00001908 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001909}
1910#endif
1911
bellardec36b692006-07-16 18:57:03 +00001912/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001913static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001914
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001915static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001916{
1917 int i;
1918 CaptureState *s;
1919
1920 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001921 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001922 s->ops.info (s->opaque);
1923 }
1924}
1925
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001926static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001927{
1928 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001929 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001930 CaptureState *s;
1931
1932 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1933 if (i == n) {
1934 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001935 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001936 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001937 return;
1938 }
1939 }
1940}
1941
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001942static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001943{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001944 const char *path = qdict_get_str(qdict, "path");
1945 int has_freq = qdict_haskey(qdict, "freq");
1946 int freq = qdict_get_try_int(qdict, "freq", -1);
1947 int has_bits = qdict_haskey(qdict, "bits");
1948 int bits = qdict_get_try_int(qdict, "bits", -1);
1949 int has_channels = qdict_haskey(qdict, "nchannels");
1950 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001951 CaptureState *s;
1952
Anthony Liguori7267c092011-08-20 22:09:37 -05001953 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001954
1955 freq = has_freq ? freq : 44100;
1956 bits = has_bits ? bits : 16;
1957 nchannels = has_channels ? nchannels : 2;
1958
1959 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001960 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001961 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001962 return;
bellardec36b692006-07-16 18:57:03 +00001963 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001964 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001965}
bellardec36b692006-07-16 18:57:03 +00001966
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001967static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001968{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001969 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001970
aliguori76655d62009-03-06 20:27:37 +00001971 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001972 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001973 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001974 return acl;
1975}
aliguori76655d62009-03-06 20:27:37 +00001976
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001977static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001978{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001979 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001980 qemu_acl *acl = find_acl(mon, aclname);
1981 qemu_acl_entry *entry;
1982 int i = 0;
1983
1984 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001985 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001986 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001987 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001988 i++;
1989 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001990 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001991 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001992 }
1993}
1994
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001995static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001996{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001997 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001998 qemu_acl *acl = find_acl(mon, aclname);
1999
2000 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002001 qemu_acl_reset(acl);
2002 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002003 }
2004}
aliguori76655d62009-03-06 20:27:37 +00002005
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002006static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002007{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002008 const char *aclname = qdict_get_str(qdict, "aclname");
2009 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002010 qemu_acl *acl = find_acl(mon, aclname);
2011
2012 if (acl) {
2013 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002014 acl->defaultDeny = 0;
2015 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002016 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002017 acl->defaultDeny = 1;
2018 monitor_printf(mon, "acl: policy set to 'deny'\n");
2019 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002020 monitor_printf(mon, "acl: unknown policy '%s', "
2021 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002022 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002023 }
2024}
aliguori76655d62009-03-06 20:27:37 +00002025
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002026static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002027{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002028 const char *aclname = qdict_get_str(qdict, "aclname");
2029 const char *match = qdict_get_str(qdict, "match");
2030 const char *policy = qdict_get_str(qdict, "policy");
2031 int has_index = qdict_haskey(qdict, "index");
2032 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002033 qemu_acl *acl = find_acl(mon, aclname);
2034 int deny, ret;
2035
2036 if (acl) {
2037 if (strcmp(policy, "allow") == 0) {
2038 deny = 0;
2039 } else if (strcmp(policy, "deny") == 0) {
2040 deny = 1;
2041 } else {
2042 monitor_printf(mon, "acl: unknown policy '%s', "
2043 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002044 return;
2045 }
aliguori28a76be2009-03-06 20:27:40 +00002046 if (has_index)
2047 ret = qemu_acl_insert(acl, deny, match, index);
2048 else
2049 ret = qemu_acl_append(acl, deny, match);
2050 if (ret < 0)
2051 monitor_printf(mon, "acl: unable to add acl entry\n");
2052 else
2053 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002054 }
2055}
aliguori76655d62009-03-06 20:27:37 +00002056
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002057static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002058{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002059 const char *aclname = qdict_get_str(qdict, "aclname");
2060 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002061 qemu_acl *acl = find_acl(mon, aclname);
2062 int ret;
aliguori76655d62009-03-06 20:27:37 +00002063
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002064 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002065 ret = qemu_acl_remove(acl, match);
2066 if (ret < 0)
2067 monitor_printf(mon, "acl: no matching acl entry\n");
2068 else
2069 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002070 }
2071}
2072
Huang Ying79c4f6b2009-06-23 10:05:14 +08002073#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002074static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002075{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002076 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002077 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002078 int cpu_index = qdict_get_int(qdict, "cpu_index");
2079 int bank = qdict_get_int(qdict, "bank");
2080 uint64_t status = qdict_get_int(qdict, "status");
2081 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2082 uint64_t addr = qdict_get_int(qdict, "addr");
2083 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002084 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002085
Eric Blake34acbc92015-05-15 16:25:00 -06002086 if (qdict_get_try_bool(qdict, "broadcast", false)) {
Jan Kiszka747461c2011-03-02 08:56:10 +01002087 flags |= MCE_INJECT_BROADCAST;
2088 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002089 cs = qemu_get_cpu(cpu_index);
2090 if (cs != NULL) {
2091 cpu = X86_CPU(cs);
2092 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2093 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002094 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002095}
2096#endif
2097
Corey Bryant208c9d12012-06-22 14:36:09 -04002098void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002099{
Anthony Liguoric227f092009-10-01 16:12:16 -05002100 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002101 int fd;
2102
Corey Bryant208c9d12012-06-22 14:36:09 -04002103 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002104 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002105 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002106 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002107 }
2108
2109 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002110 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2112 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002113 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002114 }
2115
Corey Bryant208c9d12012-06-22 14:36:09 -04002116 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002117 if (strcmp(monfd->name, fdname) != 0) {
2118 continue;
2119 }
2120
2121 close(monfd->fd);
2122 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002123 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002124 }
2125
Anthony Liguori7267c092011-08-20 22:09:37 -05002126 monfd = g_malloc0(sizeof(mon_fd_t));
2127 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002128 monfd->fd = fd;
2129
Corey Bryant208c9d12012-06-22 14:36:09 -04002130 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002131}
2132
Corey Bryant208c9d12012-06-22 14:36:09 -04002133void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002134{
Anthony Liguoric227f092009-10-01 16:12:16 -05002135 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002136
Corey Bryant208c9d12012-06-22 14:36:09 -04002137 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002138 if (strcmp(monfd->name, fdname) != 0) {
2139 continue;
2140 }
2141
Blue Swirl72cf2d42009-09-12 07:36:22 +00002142 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002144 g_free(monfd->name);
2145 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002146 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002147 }
2148
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002149 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002150}
2151
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002152static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002153{
Luiz Capitulino13548692011-07-29 15:36:43 -03002154 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002155 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002156
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002157 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002158
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002159 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002160 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002161 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002162}
2163
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002164int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002165{
Anthony Liguoric227f092009-10-01 16:12:16 -05002166 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002167
Blue Swirl72cf2d42009-09-12 07:36:22 +00002168 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002169 int fd;
2170
2171 if (strcmp(monfd->name, fdname) != 0) {
2172 continue;
2173 }
2174
2175 fd = monfd->fd;
2176
2177 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002178 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002179 g_free(monfd->name);
2180 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002181
2182 return fd;
2183 }
2184
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002185 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002186 return -1;
2187}
2188
Corey Bryantba1c0482012-08-14 16:43:43 -04002189static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2190{
2191 MonFdsetFd *mon_fdset_fd;
2192 MonFdsetFd *mon_fdset_fd_next;
2193
2194 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002195 if ((mon_fdset_fd->removed ||
2196 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2197 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002198 close(mon_fdset_fd->fd);
2199 g_free(mon_fdset_fd->opaque);
2200 QLIST_REMOVE(mon_fdset_fd, next);
2201 g_free(mon_fdset_fd);
2202 }
2203 }
2204
Corey Bryantadb696f2012-08-14 16:43:47 -04002205 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002206 QLIST_REMOVE(mon_fdset, next);
2207 g_free(mon_fdset);
2208 }
2209}
2210
Corey Bryantefb87c12012-08-14 16:43:48 -04002211static void monitor_fdsets_cleanup(void)
2212{
2213 MonFdset *mon_fdset;
2214 MonFdset *mon_fdset_next;
2215
2216 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2217 monitor_fdset_cleanup(mon_fdset);
2218 }
2219}
2220
Corey Bryantba1c0482012-08-14 16:43:43 -04002221AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2222 const char *opaque, Error **errp)
2223{
2224 int fd;
2225 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002226 AddfdInfo *fdinfo;
2227
2228 fd = qemu_chr_fe_get_msgfd(mon->chr);
2229 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002230 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002231 goto error;
2232 }
2233
Corey Bryante446f702012-10-18 15:19:32 -04002234 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2235 has_opaque, opaque, errp);
2236 if (fdinfo) {
2237 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002238 }
2239
Corey Bryantba1c0482012-08-14 16:43:43 -04002240error:
2241 if (fd != -1) {
2242 close(fd);
2243 }
2244 return NULL;
2245}
2246
2247void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2248{
2249 MonFdset *mon_fdset;
2250 MonFdsetFd *mon_fdset_fd;
2251 char fd_str[60];
2252
2253 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2254 if (mon_fdset->id != fdset_id) {
2255 continue;
2256 }
2257 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2258 if (has_fd) {
2259 if (mon_fdset_fd->fd != fd) {
2260 continue;
2261 }
2262 mon_fdset_fd->removed = true;
2263 break;
2264 } else {
2265 mon_fdset_fd->removed = true;
2266 }
2267 }
2268 if (has_fd && !mon_fdset_fd) {
2269 goto error;
2270 }
2271 monitor_fdset_cleanup(mon_fdset);
2272 return;
2273 }
2274
2275error:
2276 if (has_fd) {
2277 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2278 fdset_id, fd);
2279 } else {
2280 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2281 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002282 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002283}
2284
2285FdsetInfoList *qmp_query_fdsets(Error **errp)
2286{
2287 MonFdset *mon_fdset;
2288 MonFdsetFd *mon_fdset_fd;
2289 FdsetInfoList *fdset_list = NULL;
2290
2291 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2292 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2293 FdsetFdInfoList *fdsetfd_list = NULL;
2294
2295 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2296 fdset_info->value->fdset_id = mon_fdset->id;
2297
2298 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2299 FdsetFdInfoList *fdsetfd_info;
2300
2301 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2302 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2303 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2304 if (mon_fdset_fd->opaque) {
2305 fdsetfd_info->value->has_opaque = true;
2306 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2307 } else {
2308 fdsetfd_info->value->has_opaque = false;
2309 }
2310
2311 fdsetfd_info->next = fdsetfd_list;
2312 fdsetfd_list = fdsetfd_info;
2313 }
2314
2315 fdset_info->value->fds = fdsetfd_list;
2316
2317 fdset_info->next = fdset_list;
2318 fdset_list = fdset_info;
2319 }
2320
2321 return fdset_list;
2322}
2323
Corey Bryante446f702012-10-18 15:19:32 -04002324AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2325 bool has_opaque, const char *opaque,
2326 Error **errp)
2327{
2328 MonFdset *mon_fdset = NULL;
2329 MonFdsetFd *mon_fdset_fd;
2330 AddfdInfo *fdinfo;
2331
2332 if (has_fdset_id) {
2333 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2334 /* Break if match found or match impossible due to ordering by ID */
2335 if (fdset_id <= mon_fdset->id) {
2336 if (fdset_id < mon_fdset->id) {
2337 mon_fdset = NULL;
2338 }
2339 break;
2340 }
2341 }
2342 }
2343
2344 if (mon_fdset == NULL) {
2345 int64_t fdset_id_prev = -1;
2346 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2347
2348 if (has_fdset_id) {
2349 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002350 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2351 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002352 return NULL;
2353 }
2354 /* Use specified fdset ID */
2355 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2356 mon_fdset_cur = mon_fdset;
2357 if (fdset_id < mon_fdset_cur->id) {
2358 break;
2359 }
2360 }
2361 } else {
2362 /* Use first available fdset ID */
2363 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2364 mon_fdset_cur = mon_fdset;
2365 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2366 fdset_id_prev = mon_fdset_cur->id;
2367 continue;
2368 }
2369 break;
2370 }
2371 }
2372
2373 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2374 if (has_fdset_id) {
2375 mon_fdset->id = fdset_id;
2376 } else {
2377 mon_fdset->id = fdset_id_prev + 1;
2378 }
2379
2380 /* The fdset list is ordered by fdset ID */
2381 if (!mon_fdset_cur) {
2382 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2383 } else if (mon_fdset->id < mon_fdset_cur->id) {
2384 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2385 } else {
2386 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2387 }
2388 }
2389
2390 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2391 mon_fdset_fd->fd = fd;
2392 mon_fdset_fd->removed = false;
2393 if (has_opaque) {
2394 mon_fdset_fd->opaque = g_strdup(opaque);
2395 }
2396 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2397
2398 fdinfo = g_malloc0(sizeof(*fdinfo));
2399 fdinfo->fdset_id = mon_fdset->id;
2400 fdinfo->fd = mon_fdset_fd->fd;
2401
2402 return fdinfo;
2403}
2404
Corey Bryantadb696f2012-08-14 16:43:47 -04002405int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2406{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002407#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002408 MonFdset *mon_fdset;
2409 MonFdsetFd *mon_fdset_fd;
2410 int mon_fd_flags;
2411
Corey Bryantadb696f2012-08-14 16:43:47 -04002412 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2413 if (mon_fdset->id != fdset_id) {
2414 continue;
2415 }
2416 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2417 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2418 if (mon_fd_flags == -1) {
2419 return -1;
2420 }
2421
2422 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2423 return mon_fdset_fd->fd;
2424 }
2425 }
2426 errno = EACCES;
2427 return -1;
2428 }
2429#endif
2430
2431 errno = ENOENT;
2432 return -1;
2433}
2434
2435int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2436{
2437 MonFdset *mon_fdset;
2438 MonFdsetFd *mon_fdset_fd_dup;
2439
2440 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2441 if (mon_fdset->id != fdset_id) {
2442 continue;
2443 }
2444 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2445 if (mon_fdset_fd_dup->fd == dup_fd) {
2446 return -1;
2447 }
2448 }
2449 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2450 mon_fdset_fd_dup->fd = dup_fd;
2451 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2452 return 0;
2453 }
2454 return -1;
2455}
2456
2457static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2458{
2459 MonFdset *mon_fdset;
2460 MonFdsetFd *mon_fdset_fd_dup;
2461
2462 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2463 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2464 if (mon_fdset_fd_dup->fd == dup_fd) {
2465 if (remove) {
2466 QLIST_REMOVE(mon_fdset_fd_dup, next);
2467 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2468 monitor_fdset_cleanup(mon_fdset);
2469 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002470 return -1;
2471 } else {
2472 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002473 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002474 }
2475 }
2476 }
2477 return -1;
2478}
2479
2480int monitor_fdset_dup_fd_find(int dup_fd)
2481{
2482 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2483}
2484
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002485void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002486{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002487 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002488}
2489
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002490int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002491{
2492 int fd;
2493 Error *local_err = NULL;
2494
2495 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002496 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002497 } else {
2498 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002499 if (fd == -1) {
2500 error_setg(&local_err, "Invalid file descriptor number '%s'",
2501 fdname);
2502 }
2503 }
2504 if (local_err) {
2505 error_propagate(errp, local_err);
2506 assert(fd == -1);
2507 } else {
2508 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002509 }
2510
2511 return fd;
2512}
2513
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002514/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002515static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 {
2517 .name = "version",
2518 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002519 .params = "",
2520 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002521 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002522 },
2523 {
2524 .name = "network",
2525 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 .params = "",
2527 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002528 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 },
2530 {
2531 .name = "chardev",
2532 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 .params = "",
2534 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002535 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 },
2537 {
2538 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002539 .args_type = "nodes:-n,verbose:-v,device:B?",
2540 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002541 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002542 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002543 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "blockstats",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002550 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 },
2552 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002553 .name = "block-jobs",
2554 .args_type = "",
2555 .params = "",
2556 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002557 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002558 },
2559 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 .name = "registers",
2561 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 .params = "",
2563 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002564 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 },
2566 {
2567 .name = "cpus",
2568 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 .params = "",
2570 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002571 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
2573 {
2574 .name = "history",
2575 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 .params = "",
2577 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002578 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002580#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2581 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 {
2583 .name = "irq",
2584 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002585 .params = "",
2586 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002587#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002588 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002589#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002590 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002591#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002592 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002593#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
2595 {
2596 .name = "pic",
2597 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 .params = "",
2599 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002600#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002601 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002602#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002603 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002604#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002605 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002606#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002608#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 {
2610 .name = "pci",
2611 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002612 .params = "",
2613 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002614 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002615 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002616#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002617 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 {
2619 .name = "tlb",
2620 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 .params = "",
2622 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002623 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 },
aurel327c664e22009-03-03 06:12:22 +00002625#endif
2626#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 {
2628 .name = "mem",
2629 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 .params = "",
2631 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002632 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 },
bellardb86bda52004-09-18 19:32:46 +00002634#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 {
Blue Swirl314e2982011-09-11 20:22:05 +00002636 .name = "mtree",
2637 .args_type = "",
2638 .params = "",
2639 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002640 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002641 },
2642 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 .name = "jit",
2644 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 .params = "",
2646 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002647 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 },
2649 {
Max Filippov246ae242014-11-02 11:04:18 +03002650 .name = "opcount",
2651 .args_type = "",
2652 .params = "",
2653 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002654 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002655 },
2656 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 .name = "kvm",
2658 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002659 .params = "",
2660 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002661 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 },
2663 {
2664 .name = "numa",
2665 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002666 .params = "",
2667 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002668 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 },
2670 {
2671 .name = "usb",
2672 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 .params = "",
2674 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002675 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 },
2677 {
2678 .name = "usbhost",
2679 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002680 .params = "",
2681 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002682 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
2684 {
2685 .name = "profile",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002689 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "capture",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002696 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002697 },
2698 {
2699 .name = "snapshots",
2700 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 .params = "",
2702 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002703 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 },
2705 {
2706 .name = "status",
2707 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .params = "",
2709 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002710 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002711 },
2712 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 .name = "mice",
2714 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 .params = "",
2716 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002717 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 },
2719 {
2720 .name = "vnc",
2721 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002722 .params = "",
2723 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002724 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002726#if defined(CONFIG_SPICE)
2727 {
2728 .name = "spice",
2729 .args_type = "",
2730 .params = "",
2731 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002732 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002733 },
2734#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 {
2736 .name = "name",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002740 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 },
2742 {
2743 .name = "uuid",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002747 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 {
2750 .name = "cpustats",
2751 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 .params = "",
2753 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002754 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 },
blueswir131a60e22007-10-26 18:42:59 +00002756#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 {
2758 .name = "usernet",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002762 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 },
blueswir131a60e22007-10-26 18:42:59 +00002764#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002765 {
2766 .name = "migrate",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002770 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
2772 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002773 .name = "migrate_capabilities",
2774 .args_type = "",
2775 .params = "",
2776 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002777 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002778 },
2779 {
Liang Li50e9a622015-03-23 16:32:29 +08002780 .name = "migrate_parameters",
2781 .args_type = "",
2782 .params = "",
2783 .help = "show current migration parameters",
2784 .mhandler.cmd = hmp_info_migrate_parameters,
2785 },
2786 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002787 .name = "migrate_cache_size",
2788 .args_type = "",
2789 .params = "",
2790 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002791 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002792 },
2793 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 .name = "balloon",
2795 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002796 .params = "",
2797 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002798 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 },
2800 {
2801 .name = "qtree",
2802 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 .params = "",
2804 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002805 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 },
2807 {
2808 .name = "qdm",
2809 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002810 .params = "",
2811 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002812 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 },
2814 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002815 .name = "qom-tree",
2816 .args_type = "path:s?",
2817 .params = "[path]",
2818 .help = "show QOM composition tree",
2819 .mhandler.cmd = hmp_info_qom_tree,
2820 },
2821 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 .name = "roms",
2823 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002824 .params = "",
2825 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002826 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302828 {
2829 .name = "trace-events",
2830 .args_type = "",
2831 .params = "",
2832 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002833 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302834 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002835 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002836 .name = "tpm",
2837 .args_type = "",
2838 .params = "",
2839 .help = "show the TPM device",
2840 .mhandler.cmd = hmp_info_tpm,
2841 },
2842 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002843 .name = "memdev",
2844 .args_type = "",
2845 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002846 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002847 .mhandler.cmd = hmp_info_memdev,
2848 },
2849 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002850 .name = "memory-devices",
2851 .args_type = "",
2852 .params = "",
2853 .help = "show memory devices",
2854 .mhandler.cmd = hmp_info_memory_devices,
2855 },
2856 {
Scott Feldmanfafa4d52015-06-10 18:21:21 -07002857 .name = "rocker",
2858 .args_type = "name:s",
2859 .params = "name",
2860 .help = "Show rocker switch",
2861 .mhandler.cmd = hmp_rocker,
2862 },
2863 {
2864 .name = "rocker-ports",
2865 .args_type = "name:s",
2866 .params = "name",
2867 .help = "Show rocker ports",
2868 .mhandler.cmd = hmp_rocker_ports,
2869 },
2870 {
2871 .name = "rocker-of-dpa-flows",
2872 .args_type = "name:s,tbl_id:i?",
2873 .params = "name [tbl_id]",
2874 .help = "Show rocker OF-DPA flow tables",
2875 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2876 },
2877 {
2878 .name = "rocker-of-dpa-groups",
2879 .args_type = "name:s,type:i?",
2880 .params = "name [type]",
2881 .help = "Show rocker OF-DPA groups",
2882 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2883 },
2884 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 .name = NULL,
2886 },
bellard9dc39cb2004-03-14 21:38:27 +00002887};
2888
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002889/* mon_cmds and info_cmds would be sorted at runtime */
2890static mon_cmd_t mon_cmds[] = {
2891#include "hmp-commands.h"
2892 { NULL, NULL, },
2893};
2894
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002895static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002896#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002897 { /* NULL */ },
2898};
2899
bellard9307c4c2004-04-04 12:57:25 +00002900/*******************************************************************/
2901
2902static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002903static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002904
bellard92a31b12005-02-10 22:00:52 +00002905#define MD_TLONG 0
2906#define MD_I32 1
2907
bellard9307c4c2004-04-04 12:57:25 +00002908typedef struct MonitorDef {
2909 const char *name;
2910 int offset;
blueswir18662d652008-10-02 18:32:44 +00002911 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002912 int type;
bellard9307c4c2004-04-04 12:57:25 +00002913} MonitorDef;
2914
bellard57206fd2004-04-25 18:54:52 +00002915#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002916static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002917{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002918 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002919 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002920}
2921#endif
2922
bellarda541f292004-04-12 20:39:29 +00002923#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002924static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002925{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002926 CPUArchState *env = mon_get_cpu_env();
bellarda541f292004-04-12 20:39:29 +00002927 unsigned int u;
2928 int i;
2929
2930 u = 0;
2931 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002932 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002933
2934 return u;
2935}
2936
blueswir18662d652008-10-02 18:32:44 +00002937static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002938{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002939 CPUArchState *env = mon_get_cpu_env();
j_mayer0411a972007-10-25 21:35:50 +00002940 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002941}
2942
blueswir18662d652008-10-02 18:32:44 +00002943static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002944{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002945 CPUArchState *env = mon_get_cpu_env();
aurel323d7b4172008-10-21 11:28:46 +00002946 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002947}
bellard9fddaa02004-05-21 12:59:32 +00002948
blueswir18662d652008-10-02 18:32:44 +00002949static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002950{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002951 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002952 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002953}
2954
blueswir18662d652008-10-02 18:32:44 +00002955static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002956{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002957 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002958 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002959}
2960
blueswir18662d652008-10-02 18:32:44 +00002961static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002962{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002963 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002964 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002965}
bellarda541f292004-04-12 20:39:29 +00002966#endif
2967
bellarde95c8d52004-09-30 22:22:08 +00002968#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002969#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002970static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002971{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002972 CPUArchState *env = mon_get_cpu_env();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002973
2974 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002975}
bellard7b936c02005-10-30 17:05:13 +00002976#endif
bellarde95c8d52004-09-30 22:22:08 +00002977
blueswir18662d652008-10-02 18:32:44 +00002978static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002979{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002980 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002981 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002982}
2983#endif
2984
blueswir18662d652008-10-02 18:32:44 +00002985static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002986#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002987
2988#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002989 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2990 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2991 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002992
Andreas Färbere59d1672012-02-16 00:40:47 +01002993 { "eax", offsetof(CPUX86State, regs[0]) },
2994 { "ecx", offsetof(CPUX86State, regs[1]) },
2995 { "edx", offsetof(CPUX86State, regs[2]) },
2996 { "ebx", offsetof(CPUX86State, regs[3]) },
2997 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2998 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2999 { "esi", offsetof(CPUX86State, regs[6]) },
3000 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003001#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003002 { "r8", offsetof(CPUX86State, regs[8]) },
3003 { "r9", offsetof(CPUX86State, regs[9]) },
3004 { "r10", offsetof(CPUX86State, regs[10]) },
3005 { "r11", offsetof(CPUX86State, regs[11]) },
3006 { "r12", offsetof(CPUX86State, regs[12]) },
3007 { "r13", offsetof(CPUX86State, regs[13]) },
3008 { "r14", offsetof(CPUX86State, regs[14]) },
3009 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003010#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003011 { "eflags", offsetof(CPUX86State, eflags) },
3012 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003013 SEG("cs", R_CS)
3014 SEG("ds", R_DS)
3015 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003016 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003017 SEG("fs", R_FS)
3018 SEG("gs", R_GS)
3019 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003020#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003021 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003022 { "r0", offsetof(CPUPPCState, gpr[0]) },
3023 { "r1", offsetof(CPUPPCState, gpr[1]) },
3024 { "r2", offsetof(CPUPPCState, gpr[2]) },
3025 { "r3", offsetof(CPUPPCState, gpr[3]) },
3026 { "r4", offsetof(CPUPPCState, gpr[4]) },
3027 { "r5", offsetof(CPUPPCState, gpr[5]) },
3028 { "r6", offsetof(CPUPPCState, gpr[6]) },
3029 { "r7", offsetof(CPUPPCState, gpr[7]) },
3030 { "r8", offsetof(CPUPPCState, gpr[8]) },
3031 { "r9", offsetof(CPUPPCState, gpr[9]) },
3032 { "r10", offsetof(CPUPPCState, gpr[10]) },
3033 { "r11", offsetof(CPUPPCState, gpr[11]) },
3034 { "r12", offsetof(CPUPPCState, gpr[12]) },
3035 { "r13", offsetof(CPUPPCState, gpr[13]) },
3036 { "r14", offsetof(CPUPPCState, gpr[14]) },
3037 { "r15", offsetof(CPUPPCState, gpr[15]) },
3038 { "r16", offsetof(CPUPPCState, gpr[16]) },
3039 { "r17", offsetof(CPUPPCState, gpr[17]) },
3040 { "r18", offsetof(CPUPPCState, gpr[18]) },
3041 { "r19", offsetof(CPUPPCState, gpr[19]) },
3042 { "r20", offsetof(CPUPPCState, gpr[20]) },
3043 { "r21", offsetof(CPUPPCState, gpr[21]) },
3044 { "r22", offsetof(CPUPPCState, gpr[22]) },
3045 { "r23", offsetof(CPUPPCState, gpr[23]) },
3046 { "r24", offsetof(CPUPPCState, gpr[24]) },
3047 { "r25", offsetof(CPUPPCState, gpr[25]) },
3048 { "r26", offsetof(CPUPPCState, gpr[26]) },
3049 { "r27", offsetof(CPUPPCState, gpr[27]) },
3050 { "r28", offsetof(CPUPPCState, gpr[28]) },
3051 { "r29", offsetof(CPUPPCState, gpr[29]) },
3052 { "r30", offsetof(CPUPPCState, gpr[30]) },
3053 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003054 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003055 { "f0", offsetof(CPUPPCState, fpr[0]) },
3056 { "f1", offsetof(CPUPPCState, fpr[1]) },
3057 { "f2", offsetof(CPUPPCState, fpr[2]) },
3058 { "f3", offsetof(CPUPPCState, fpr[3]) },
3059 { "f4", offsetof(CPUPPCState, fpr[4]) },
3060 { "f5", offsetof(CPUPPCState, fpr[5]) },
3061 { "f6", offsetof(CPUPPCState, fpr[6]) },
3062 { "f7", offsetof(CPUPPCState, fpr[7]) },
3063 { "f8", offsetof(CPUPPCState, fpr[8]) },
3064 { "f9", offsetof(CPUPPCState, fpr[9]) },
3065 { "f10", offsetof(CPUPPCState, fpr[10]) },
3066 { "f11", offsetof(CPUPPCState, fpr[11]) },
3067 { "f12", offsetof(CPUPPCState, fpr[12]) },
3068 { "f13", offsetof(CPUPPCState, fpr[13]) },
3069 { "f14", offsetof(CPUPPCState, fpr[14]) },
3070 { "f15", offsetof(CPUPPCState, fpr[15]) },
3071 { "f16", offsetof(CPUPPCState, fpr[16]) },
3072 { "f17", offsetof(CPUPPCState, fpr[17]) },
3073 { "f18", offsetof(CPUPPCState, fpr[18]) },
3074 { "f19", offsetof(CPUPPCState, fpr[19]) },
3075 { "f20", offsetof(CPUPPCState, fpr[20]) },
3076 { "f21", offsetof(CPUPPCState, fpr[21]) },
3077 { "f22", offsetof(CPUPPCState, fpr[22]) },
3078 { "f23", offsetof(CPUPPCState, fpr[23]) },
3079 { "f24", offsetof(CPUPPCState, fpr[24]) },
3080 { "f25", offsetof(CPUPPCState, fpr[25]) },
3081 { "f26", offsetof(CPUPPCState, fpr[26]) },
3082 { "f27", offsetof(CPUPPCState, fpr[27]) },
3083 { "f28", offsetof(CPUPPCState, fpr[28]) },
3084 { "f29", offsetof(CPUPPCState, fpr[29]) },
3085 { "f30", offsetof(CPUPPCState, fpr[30]) },
3086 { "f31", offsetof(CPUPPCState, fpr[31]) },
3087 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003088 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003089 { "nip|pc", offsetof(CPUPPCState, nip) },
3090 { "lr", offsetof(CPUPPCState, lr) },
3091 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003092 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003093 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003094 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003095 { "msr", 0, &monitor_get_msr, },
3096 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003097 { "tbu", 0, &monitor_get_tbu, },
3098 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003099 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003100 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3101 { "sr0", offsetof(CPUPPCState, sr[0]) },
3102 { "sr1", offsetof(CPUPPCState, sr[1]) },
3103 { "sr2", offsetof(CPUPPCState, sr[2]) },
3104 { "sr3", offsetof(CPUPPCState, sr[3]) },
3105 { "sr4", offsetof(CPUPPCState, sr[4]) },
3106 { "sr5", offsetof(CPUPPCState, sr[5]) },
3107 { "sr6", offsetof(CPUPPCState, sr[6]) },
3108 { "sr7", offsetof(CPUPPCState, sr[7]) },
3109 { "sr8", offsetof(CPUPPCState, sr[8]) },
3110 { "sr9", offsetof(CPUPPCState, sr[9]) },
3111 { "sr10", offsetof(CPUPPCState, sr[10]) },
3112 { "sr11", offsetof(CPUPPCState, sr[11]) },
3113 { "sr12", offsetof(CPUPPCState, sr[12]) },
3114 { "sr13", offsetof(CPUPPCState, sr[13]) },
3115 { "sr14", offsetof(CPUPPCState, sr[14]) },
3116 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003117 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003118 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003119
Andreas Färbere59d1672012-02-16 00:40:47 +01003120 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3121 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003122 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3123 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3124 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003125 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3126 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3127 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3128 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3129 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3130 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3131 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3132 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3133 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3134 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3135 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3136 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3137 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3138 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3139 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3140 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3141 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3142 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3143 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3144 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3145 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3146 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3147 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3148 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3149 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3150 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3151 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3152 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3153 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3154 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3155 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3156 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3157 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3158 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3159 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3160 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3161 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3162 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3163 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3164 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3165 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3166 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3167 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3168 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3169 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3170 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3171 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3172 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3173 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3174 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3175 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3176 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3177 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3178 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3179 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3180 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3181 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3182 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3183 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3184 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3185 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3186 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3187 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3188 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003189
bellarde95c8d52004-09-30 22:22:08 +00003190#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003191 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3192 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3193 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3194 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3195 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3196 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3197 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3198 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003199 { "o0", 0, monitor_get_reg },
3200 { "o1", 1, monitor_get_reg },
3201 { "o2", 2, monitor_get_reg },
3202 { "o3", 3, monitor_get_reg },
3203 { "o4", 4, monitor_get_reg },
3204 { "o5", 5, monitor_get_reg },
3205 { "o6", 6, monitor_get_reg },
3206 { "o7", 7, monitor_get_reg },
3207 { "l0", 8, monitor_get_reg },
3208 { "l1", 9, monitor_get_reg },
3209 { "l2", 10, monitor_get_reg },
3210 { "l3", 11, monitor_get_reg },
3211 { "l4", 12, monitor_get_reg },
3212 { "l5", 13, monitor_get_reg },
3213 { "l6", 14, monitor_get_reg },
3214 { "l7", 15, monitor_get_reg },
3215 { "i0", 16, monitor_get_reg },
3216 { "i1", 17, monitor_get_reg },
3217 { "i2", 18, monitor_get_reg },
3218 { "i3", 19, monitor_get_reg },
3219 { "i4", 20, monitor_get_reg },
3220 { "i5", 21, monitor_get_reg },
3221 { "i6", 22, monitor_get_reg },
3222 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003223 { "pc", offsetof(CPUSPARCState, pc) },
3224 { "npc", offsetof(CPUSPARCState, npc) },
3225 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003226#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003227 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003228 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003229#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003230 { "tbr", offsetof(CPUSPARCState, tbr) },
3231 { "fsr", offsetof(CPUSPARCState, fsr) },
3232 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3233 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3234 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3235 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3236 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3237 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3238 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3239 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3240 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3241 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3242 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3243 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3244 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3245 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3246 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3247 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3248 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3249 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3250 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3251 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3252 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3253 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3254 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3255 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3256 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3257 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3258 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3259 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3260 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3261 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3262 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3263 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003264#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003265 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3266 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3267 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3268 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3269 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3270 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3271 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3272 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3273 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3274 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3275 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3276 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3277 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3278 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3279 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3280 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3281 { "asi", offsetof(CPUSPARCState, asi) },
3282 { "pstate", offsetof(CPUSPARCState, pstate) },
3283 { "cansave", offsetof(CPUSPARCState, cansave) },
3284 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3285 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3286 { "wstate", offsetof(CPUSPARCState, wstate) },
3287 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3288 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003289#endif
bellard9307c4c2004-04-04 12:57:25 +00003290#endif
3291 { NULL },
3292};
3293
Stefan Weil9c3175c2013-08-22 21:30:09 +02003294static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3295expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003296{
Fam Zheng277acfe2013-08-20 10:58:21 +08003297 va_list ap;
3298 va_start(ap, fmt);
3299 monitor_vprintf(mon, fmt, ap);
3300 monitor_printf(mon, "\n");
3301 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003302 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003303}
3304
Markus Armbruster09b94182010-01-20 13:07:30 +01003305/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003306static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003307{
blueswir18662d652008-10-02 18:32:44 +00003308 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003309 void *ptr;
3310
bellard9307c4c2004-04-04 12:57:25 +00003311 for(md = monitor_defs; md->name != NULL; md++) {
3312 if (compare_cmd(name, md->name)) {
3313 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003314 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003315 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07003316 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00003317 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003318 switch(md->type) {
3319 case MD_I32:
3320 *pval = *(int32_t *)ptr;
3321 break;
3322 case MD_TLONG:
3323 *pval = *(target_long *)ptr;
3324 break;
3325 default:
3326 *pval = 0;
3327 break;
3328 }
bellard9307c4c2004-04-04 12:57:25 +00003329 }
3330 return 0;
3331 }
3332 }
3333 return -1;
3334}
3335
3336static void next(void)
3337{
Blue Swirl660f11b2009-07-31 21:16:51 +00003338 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003339 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003340 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003341 pch++;
3342 }
3343}
3344
aliguori376253e2009-03-05 23:01:23 +00003345static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003346
aliguori376253e2009-03-05 23:01:23 +00003347static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003348{
blueswir1c2efc952007-09-25 17:28:42 +00003349 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003350 char *p;
bellard6a00d602005-11-21 23:25:50 +00003351 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003352
3353 switch(*pch) {
3354 case '+':
3355 next();
aliguori376253e2009-03-05 23:01:23 +00003356 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003357 break;
3358 case '-':
3359 next();
aliguori376253e2009-03-05 23:01:23 +00003360 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003361 break;
3362 case '~':
3363 next();
aliguori376253e2009-03-05 23:01:23 +00003364 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003365 break;
3366 case '(':
3367 next();
aliguori376253e2009-03-05 23:01:23 +00003368 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003369 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003370 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003371 }
3372 next();
3373 break;
bellard81d09122004-07-14 17:21:37 +00003374 case '\'':
3375 pch++;
3376 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003377 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003378 n = *pch;
3379 pch++;
3380 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003381 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003382 next();
3383 break;
bellard9307c4c2004-04-04 12:57:25 +00003384 case '$':
3385 {
3386 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003387 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003388
bellard9307c4c2004-04-04 12:57:25 +00003389 pch++;
3390 q = buf;
3391 while ((*pch >= 'a' && *pch <= 'z') ||
3392 (*pch >= 'A' && *pch <= 'Z') ||
3393 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003394 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003395 if ((q - buf) < sizeof(buf) - 1)
3396 *q++ = *pch;
3397 pch++;
3398 }
blueswir1cd390082008-11-16 13:53:32 +00003399 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003400 pch++;
3401 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003402 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003403 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003404 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003405 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003406 }
3407 break;
3408 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003409 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003410 n = 0;
3411 break;
3412 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003413 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003414 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003415 if (errno == ERANGE) {
3416 expr_error(mon, "number too large");
3417 }
bellard9307c4c2004-04-04 12:57:25 +00003418 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003419 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003420 }
3421 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003422 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003423 pch++;
3424 break;
3425 }
3426 return n;
3427}
3428
3429
aliguori376253e2009-03-05 23:01:23 +00003430static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003431{
blueswir1c2efc952007-09-25 17:28:42 +00003432 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003433 int op;
ths3b46e622007-09-17 08:09:54 +00003434
aliguori376253e2009-03-05 23:01:23 +00003435 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003436 for(;;) {
3437 op = *pch;
3438 if (op != '*' && op != '/' && op != '%')
3439 break;
3440 next();
aliguori376253e2009-03-05 23:01:23 +00003441 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003442 switch(op) {
3443 default:
3444 case '*':
3445 val *= val2;
3446 break;
3447 case '/':
3448 case '%':
ths5fafdf22007-09-16 21:08:06 +00003449 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003450 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003451 if (op == '/')
3452 val /= val2;
3453 else
3454 val %= val2;
3455 break;
3456 }
3457 }
3458 return val;
3459}
3460
aliguori376253e2009-03-05 23:01:23 +00003461static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003462{
blueswir1c2efc952007-09-25 17:28:42 +00003463 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003464 int op;
bellard9307c4c2004-04-04 12:57:25 +00003465
aliguori376253e2009-03-05 23:01:23 +00003466 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003467 for(;;) {
3468 op = *pch;
3469 if (op != '&' && op != '|' && op != '^')
3470 break;
3471 next();
aliguori376253e2009-03-05 23:01:23 +00003472 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003473 switch(op) {
3474 default:
3475 case '&':
3476 val &= val2;
3477 break;
3478 case '|':
3479 val |= val2;
3480 break;
3481 case '^':
3482 val ^= val2;
3483 break;
3484 }
3485 }
3486 return val;
3487}
3488
aliguori376253e2009-03-05 23:01:23 +00003489static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003490{
blueswir1c2efc952007-09-25 17:28:42 +00003491 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003492 int op;
bellard9307c4c2004-04-04 12:57:25 +00003493
aliguori376253e2009-03-05 23:01:23 +00003494 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003495 for(;;) {
3496 op = *pch;
3497 if (op != '+' && op != '-')
3498 break;
3499 next();
aliguori376253e2009-03-05 23:01:23 +00003500 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003501 if (op == '+')
3502 val += val2;
3503 else
3504 val -= val2;
3505 }
3506 return val;
3507}
3508
aliguori376253e2009-03-05 23:01:23 +00003509static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003510{
3511 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003512 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003513 *pp = pch;
3514 return -1;
3515 }
blueswir1cd390082008-11-16 13:53:32 +00003516 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003517 pch++;
aliguori376253e2009-03-05 23:01:23 +00003518 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003519 *pp = pch;
3520 return 0;
3521}
3522
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003523static int get_double(Monitor *mon, double *pval, const char **pp)
3524{
3525 const char *p = *pp;
3526 char *tailp;
3527 double d;
3528
3529 d = strtod(p, &tailp);
3530 if (tailp == p) {
3531 monitor_printf(mon, "Number expected\n");
3532 return -1;
3533 }
3534 if (d != d || d - d != 0) {
3535 /* NaN or infinity */
3536 monitor_printf(mon, "Bad number\n");
3537 return -1;
3538 }
3539 *pval = d;
3540 *pp = tailp;
3541 return 0;
3542}
3543
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003544/*
3545 * Store the command-name in cmdname, and return a pointer to
3546 * the remaining of the command string.
3547 */
3548static const char *get_command_name(const char *cmdline,
3549 char *cmdname, size_t nlen)
3550{
3551 size_t len;
3552 const char *p, *pstart;
3553
3554 p = cmdline;
3555 while (qemu_isspace(*p))
3556 p++;
3557 if (*p == '\0')
3558 return NULL;
3559 pstart = p;
3560 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3561 p++;
3562 len = p - pstart;
3563 if (len > nlen - 1)
3564 len = nlen - 1;
3565 memcpy(cmdname, pstart, len);
3566 cmdname[len] = '\0';
3567 return p;
3568}
3569
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003570/**
3571 * Read key of 'type' into 'key' and return the current
3572 * 'type' pointer.
3573 */
3574static char *key_get_info(const char *type, char **key)
3575{
3576 size_t len;
3577 char *p, *str;
3578
3579 if (*type == ',')
3580 type++;
3581
3582 p = strchr(type, ':');
3583 if (!p) {
3584 *key = NULL;
3585 return NULL;
3586 }
3587 len = p - type;
3588
Anthony Liguori7267c092011-08-20 22:09:37 -05003589 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003590 memcpy(str, type, len);
3591 str[len] = '\0';
3592
3593 *key = str;
3594 return ++p;
3595}
3596
bellard9307c4c2004-04-04 12:57:25 +00003597static int default_fmt_format = 'x';
3598static int default_fmt_size = 4;
3599
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003600static int is_valid_option(const char *c, const char *typestr)
3601{
3602 char option[3];
3603
3604 option[0] = '-';
3605 option[1] = *c;
3606 option[2] = '\0';
3607
3608 typestr = strstr(typestr, option);
3609 return (typestr != NULL);
3610}
3611
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003612static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3613 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003614{
3615 const mon_cmd_t *cmd;
3616
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003617 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003618 if (compare_cmd(cmdname, cmd->name)) {
3619 return cmd;
3620 }
3621 }
3622
3623 return NULL;
3624}
3625
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003626static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3627{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003628 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003629}
3630
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003631/*
Bandan Dasae502122015-06-03 18:38:08 -04003632 * Parse command name from @cmdp according to command table @table.
3633 * If blank, return NULL.
3634 * Else, if no valid command can be found, report to @mon, and return
3635 * NULL.
3636 * Else, change @cmdp to point right behind the name, and return its
3637 * command table entry.
3638 * Do not assume the return value points into @table! It doesn't when
3639 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003640 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003641static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04003642 const char **cmdp,
3643 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003644{
Bandan Dasae502122015-06-03 18:38:08 -04003645 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003646 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003647 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003648
bellard9307c4c2004-04-04 12:57:25 +00003649 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003650 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003651 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003652 return NULL;
ths3b46e622007-09-17 08:09:54 +00003653
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003654 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003655 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003656 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04003657 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003658 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003659 }
bellard9307c4c2004-04-04 12:57:25 +00003660
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003661 /* filter out following useless space */
3662 while (qemu_isspace(*p)) {
3663 p++;
3664 }
Bandan Dasae502122015-06-03 18:38:08 -04003665
3666 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003667 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003668 if (cmd->sub_table != NULL && *p != '\0') {
3669 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003670 }
3671
Bandan Dasae502122015-06-03 18:38:08 -04003672 return cmd;
3673}
3674
3675/*
3676 * Parse arguments for @cmd.
3677 * If it can't be parsed, report to @mon, and return NULL.
3678 * Else, insert command arguments into a QDict, and return it.
3679 * Note: On success, caller has to free the QDict structure.
3680 */
3681
3682static QDict *monitor_parse_arguments(Monitor *mon,
3683 const char **endp,
3684 const mon_cmd_t *cmd)
3685{
3686 const char *typestr;
3687 char *key;
3688 int c;
3689 const char *p = *endp;
3690 char buf[1024];
3691 QDict *qdict = qdict_new();
3692
bellard9307c4c2004-04-04 12:57:25 +00003693 /* parse the parameters */
3694 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003695 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003696 typestr = key_get_info(typestr, &key);
3697 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003698 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003699 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003700 typestr++;
3701 switch(c) {
3702 case 'F':
bellard81d09122004-07-14 17:21:37 +00003703 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003704 case 's':
3705 {
3706 int ret;
ths3b46e622007-09-17 08:09:54 +00003707
blueswir1cd390082008-11-16 13:53:32 +00003708 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003709 p++;
3710 if (*typestr == '?') {
3711 typestr++;
3712 if (*p == '\0') {
3713 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003714 break;
bellard9307c4c2004-04-04 12:57:25 +00003715 }
3716 }
3717 ret = get_str(buf, sizeof(buf), &p);
3718 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003719 switch(c) {
3720 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003721 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003722 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003723 break;
3724 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003725 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003726 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003727 break;
3728 default:
Bandan Dasae502122015-06-03 18:38:08 -04003729 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003730 break;
3731 }
bellard9307c4c2004-04-04 12:57:25 +00003732 goto fail;
3733 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003734 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003735 }
3736 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003737 case 'O':
3738 {
3739 QemuOptsList *opts_list;
3740 QemuOpts *opts;
3741
3742 opts_list = qemu_find_opts(key);
3743 if (!opts_list || opts_list->desc->name) {
3744 goto bad_type;
3745 }
3746 while (qemu_isspace(*p)) {
3747 p++;
3748 }
3749 if (!*p)
3750 break;
3751 if (get_str(buf, sizeof(buf), &p) < 0) {
3752 goto fail;
3753 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003754 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003755 if (!opts) {
3756 goto fail;
3757 }
3758 qemu_opts_to_qdict(opts, qdict);
3759 qemu_opts_del(opts);
3760 }
3761 break;
bellard9307c4c2004-04-04 12:57:25 +00003762 case '/':
3763 {
3764 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003765
blueswir1cd390082008-11-16 13:53:32 +00003766 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003767 p++;
3768 if (*p == '/') {
3769 /* format found */
3770 p++;
3771 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003772 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003773 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003774 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003775 count = count * 10 + (*p - '0');
3776 p++;
3777 }
3778 }
3779 size = -1;
3780 format = -1;
3781 for(;;) {
3782 switch(*p) {
3783 case 'o':
3784 case 'd':
3785 case 'u':
3786 case 'x':
3787 case 'i':
3788 case 'c':
3789 format = *p++;
3790 break;
3791 case 'b':
3792 size = 1;
3793 p++;
3794 break;
3795 case 'h':
3796 size = 2;
3797 p++;
3798 break;
3799 case 'w':
3800 size = 4;
3801 p++;
3802 break;
3803 case 'g':
3804 case 'L':
3805 size = 8;
3806 p++;
3807 break;
3808 default:
3809 goto next;
3810 }
3811 }
3812 next:
blueswir1cd390082008-11-16 13:53:32 +00003813 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003814 monitor_printf(mon, "invalid char in format: '%c'\n",
3815 *p);
bellard9307c4c2004-04-04 12:57:25 +00003816 goto fail;
3817 }
bellard9307c4c2004-04-04 12:57:25 +00003818 if (format < 0)
3819 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003820 if (format != 'i') {
3821 /* for 'i', not specifying a size gives -1 as size */
3822 if (size < 0)
3823 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003824 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003825 }
bellard9307c4c2004-04-04 12:57:25 +00003826 default_fmt_format = format;
3827 } else {
3828 count = 1;
3829 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003830 if (format != 'i') {
3831 size = default_fmt_size;
3832 } else {
3833 size = -1;
3834 }
bellard9307c4c2004-04-04 12:57:25 +00003835 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003836 qdict_put(qdict, "count", qint_from_int(count));
3837 qdict_put(qdict, "format", qint_from_int(format));
3838 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003839 }
3840 break;
3841 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003842 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003843 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003844 {
blueswir1c2efc952007-09-25 17:28:42 +00003845 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003846
blueswir1cd390082008-11-16 13:53:32 +00003847 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003848 p++;
bellard34405572004-06-08 00:55:58 +00003849 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003850 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003851 if (*p == '\0') {
3852 typestr++;
3853 break;
3854 }
bellard34405572004-06-08 00:55:58 +00003855 } else {
3856 if (*p == '.') {
3857 p++;
blueswir1cd390082008-11-16 13:53:32 +00003858 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003859 p++;
bellard34405572004-06-08 00:55:58 +00003860 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003861 typestr++;
3862 break;
bellard34405572004-06-08 00:55:58 +00003863 }
3864 }
bellard13224a82006-07-14 22:03:35 +00003865 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003866 }
aliguori376253e2009-03-05 23:01:23 +00003867 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003868 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003869 /* Check if 'i' is greater than 32-bit */
3870 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003871 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003872 monitor_printf(mon, "integer is for 32-bit values\n");
3873 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003874 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003875 if (val < 0) {
3876 monitor_printf(mon, "enter a positive value\n");
3877 goto fail;
3878 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003879 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003880 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003881 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003882 }
3883 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003884 case 'o':
3885 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003886 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003887 char *end;
3888
3889 while (qemu_isspace(*p)) {
3890 p++;
3891 }
3892 if (*typestr == '?') {
3893 typestr++;
3894 if (*p == '\0') {
3895 break;
3896 }
3897 }
3898 val = strtosz(p, &end);
3899 if (val < 0) {
3900 monitor_printf(mon, "invalid size\n");
3901 goto fail;
3902 }
3903 qdict_put(qdict, key, qint_from_int(val));
3904 p = end;
3905 }
3906 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003907 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003908 {
3909 double val;
3910
3911 while (qemu_isspace(*p))
3912 p++;
3913 if (*typestr == '?') {
3914 typestr++;
3915 if (*p == '\0') {
3916 break;
3917 }
3918 }
3919 if (get_double(mon, &val, &p) < 0) {
3920 goto fail;
3921 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003922 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003923 switch (*p) {
3924 case 'm':
3925 val /= 1e3; p += 2; break;
3926 case 'u':
3927 val /= 1e6; p += 2; break;
3928 case 'n':
3929 val /= 1e9; p += 2; break;
3930 }
3931 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003932 if (*p && !qemu_isspace(*p)) {
3933 monitor_printf(mon, "Unknown unit suffix\n");
3934 goto fail;
3935 }
3936 qdict_put(qdict, key, qfloat_from_double(val));
3937 }
3938 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003939 case 'b':
3940 {
3941 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003942 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003943
3944 while (qemu_isspace(*p)) {
3945 p++;
3946 }
3947 beg = p;
3948 while (qemu_isgraph(*p)) {
3949 p++;
3950 }
3951 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003952 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003953 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003954 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003955 } else {
3956 monitor_printf(mon, "Expected 'on' or 'off'\n");
3957 goto fail;
3958 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06003959 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003960 }
3961 break;
bellard9307c4c2004-04-04 12:57:25 +00003962 case '-':
3963 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003964 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003965 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003966 /* option */
ths3b46e622007-09-17 08:09:54 +00003967
bellard9307c4c2004-04-04 12:57:25 +00003968 c = *typestr++;
3969 if (c == '\0')
3970 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003971 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003972 p++;
bellard9307c4c2004-04-04 12:57:25 +00003973 if (*p == '-') {
3974 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003975 if(c != *p) {
3976 if(!is_valid_option(p, typestr)) {
3977
3978 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003979 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003980 goto fail;
3981 } else {
3982 skip_key = 1;
3983 }
bellard9307c4c2004-04-04 12:57:25 +00003984 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003985 if(skip_key) {
3986 p = tmp;
3987 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003988 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003989 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003990 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003991 }
bellard9307c4c2004-04-04 12:57:25 +00003992 }
bellard9307c4c2004-04-04 12:57:25 +00003993 }
3994 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003995 case 'S':
3996 {
3997 /* package all remaining string */
3998 int len;
3999
4000 while (qemu_isspace(*p)) {
4001 p++;
4002 }
4003 if (*typestr == '?') {
4004 typestr++;
4005 if (*p == '\0') {
4006 /* no remaining string: NULL argument */
4007 break;
4008 }
4009 }
4010 len = strlen(p);
4011 if (len <= 0) {
4012 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04004013 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04004014 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08004015 }
4016 qdict_put(qdict, key, qstring_from_str(p));
4017 p += len;
4018 }
4019 break;
bellard9307c4c2004-04-04 12:57:25 +00004020 default:
4021 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04004022 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00004023 goto fail;
4024 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004025 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004026 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004027 }
4028 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004029 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004030 p++;
4031 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004032 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04004033 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00004034 goto fail;
4035 }
4036
Bandan Dasae502122015-06-03 18:38:08 -04004037 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004038
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004039fail:
Bandan Dasae502122015-06-03 18:38:08 -04004040 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05004041 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004042 return NULL;
4043}
4044
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004045static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004046{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004047 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004048 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004049
Bandan Dasae502122015-06-03 18:38:08 -04004050 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4051 if (!cmd) {
4052 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004053 }
4054
Bandan Dasae502122015-06-03 18:38:08 -04004055 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4056 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04004057 monitor_printf(mon, "Try \"help %s\" for more information\n",
4058 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04004059 return;
4060 }
4061
4062 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004063 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004064}
4065
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004066static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004067{
4068 const char *p, *pstart;
4069 char cmd[128];
4070 int len;
4071
4072 p = list;
4073 for(;;) {
4074 pstart = p;
4075 p = strchr(p, '|');
4076 if (!p)
4077 p = pstart + strlen(pstart);
4078 len = p - pstart;
4079 if (len > sizeof(cmd) - 2)
4080 len = sizeof(cmd) - 2;
4081 memcpy(cmd, pstart, len);
4082 cmd[len] = '\0';
4083 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004084 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004085 }
4086 if (*p == '\0')
4087 break;
4088 p++;
4089 }
4090}
4091
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004092static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004093{
4094 DIR *ffs;
4095 struct dirent *d;
4096 char path[1024];
4097 char file[1024], file_prefix[1024];
4098 int input_path_len;
4099 const char *p;
4100
ths5fafdf22007-09-16 21:08:06 +00004101 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004102 if (!p) {
4103 input_path_len = 0;
4104 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004105 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004106 } else {
4107 input_path_len = p - input + 1;
4108 memcpy(path, input, input_path_len);
4109 if (input_path_len > sizeof(path) - 1)
4110 input_path_len = sizeof(path) - 1;
4111 path[input_path_len] = '\0';
4112 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4113 }
Bandan Das19f2db52015-06-03 18:38:07 -04004114
bellard81d09122004-07-14 17:21:37 +00004115 ffs = opendir(path);
4116 if (!ffs)
4117 return;
4118 for(;;) {
4119 struct stat sb;
4120 d = readdir(ffs);
4121 if (!d)
4122 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004123
4124 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4125 continue;
4126 }
4127
bellard81d09122004-07-14 17:21:37 +00004128 if (strstart(d->d_name, file_prefix, NULL)) {
4129 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004130 if (input_path_len < sizeof(file))
4131 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4132 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004133 /* stat the file to find out if it's a directory.
4134 * In that case add a slash to speed up typing long paths
4135 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004136 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004137 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004138 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004139 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004140 }
4141 }
4142 closedir(ffs);
4143}
4144
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004145static const char *next_arg_type(const char *typestr)
4146{
4147 const char *p = strchr(typestr, ':');
4148 return (p != NULL ? ++p : typestr);
4149}
4150
Hani Benhabiles40d19392014-05-07 23:41:30 +01004151static void add_completion_option(ReadLineState *rs, const char *str,
4152 const char *option)
4153{
4154 if (!str || !option) {
4155 return;
4156 }
4157 if (!strncmp(option, str, strlen(str))) {
4158 readline_add_completion(rs, option);
4159 }
4160}
4161
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004162void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4163{
4164 size_t len;
4165 ChardevBackendInfoList *list, *start;
4166
4167 if (nb_args != 2) {
4168 return;
4169 }
4170 len = strlen(str);
4171 readline_set_completion_index(rs, len);
4172
4173 start = list = qmp_query_chardev_backends(NULL);
4174 while (list) {
4175 const char *chr_name = list->value->name;
4176
4177 if (!strncmp(chr_name, str, len)) {
4178 readline_add_completion(rs, chr_name);
4179 }
4180 list = list->next;
4181 }
4182 qapi_free_ChardevBackendInfoList(start);
4183}
4184
Hani Benhabilesb162b492014-05-07 23:41:31 +01004185void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4186{
4187 size_t len;
4188 int i;
4189
4190 if (nb_args != 2) {
4191 return;
4192 }
4193 len = strlen(str);
4194 readline_set_completion_index(rs, len);
4195 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4196 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4197 }
4198}
4199
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004200void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004201{
4202 GSList *list, *elt;
4203 size_t len;
4204
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004205 if (nb_args != 2) {
4206 return;
4207 }
4208
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004209 len = strlen(str);
4210 readline_set_completion_index(rs, len);
4211 list = elt = object_class_get_list(TYPE_DEVICE, false);
4212 while (elt) {
4213 const char *name;
4214 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4215 TYPE_DEVICE);
4216 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004217
4218 if (!dc->cannot_instantiate_with_device_add_yet
4219 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004220 readline_add_completion(rs, name);
4221 }
4222 elt = elt->next;
4223 }
4224 g_slist_free(list);
4225}
4226
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004227void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004228{
4229 GSList *list, *elt;
4230 size_t len;
4231
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004232 if (nb_args != 2) {
4233 return;
4234 }
4235
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004236 len = strlen(str);
4237 readline_set_completion_index(rs, len);
4238 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4239 while (elt) {
4240 const char *name;
4241
4242 name = object_class_get_name(OBJECT_CLASS(elt->data));
4243 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4244 readline_add_completion(rs, name);
4245 }
4246 elt = elt->next;
4247 }
4248 g_slist_free(list);
4249}
4250
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004251static void peripheral_device_del_completion(ReadLineState *rs,
4252 const char *str, size_t len)
4253{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004254 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4255 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004256
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004257 list = qdev_build_hotpluggable_device_list(peripheral);
4258 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004259 return;
4260 }
4261
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004262 for (item = list; item; item = g_slist_next(item)) {
4263 DeviceState *dev = item->data;
4264
4265 if (dev->id && !strncmp(str, dev->id, len)) {
4266 readline_add_completion(rs, dev->id);
4267 }
4268 }
4269
4270 g_slist_free(list);
4271}
4272
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004273void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4274{
4275 size_t len;
4276 ChardevInfoList *list, *start;
4277
4278 if (nb_args != 2) {
4279 return;
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 = list->value;
4287
4288 if (!strncmp(chr->label, str, len)) {
4289 readline_add_completion(rs, chr->label);
4290 }
4291 list = list->next;
4292 }
4293 qapi_free_ChardevInfoList(start);
4294}
4295
Hani Benhabiles8e597772014-05-27 23:39:30 +01004296static void ringbuf_completion(ReadLineState *rs, const char *str)
4297{
4298 size_t len;
4299 ChardevInfoList *list, *start;
4300
4301 len = strlen(str);
4302 readline_set_completion_index(rs, len);
4303
4304 start = list = qmp_query_chardev(NULL);
4305 while (list) {
4306 ChardevInfo *chr_info = list->value;
4307
4308 if (!strncmp(chr_info->label, str, len)) {
4309 CharDriverState *chr = qemu_chr_find(chr_info->label);
4310 if (chr && chr_is_ringbuf(chr)) {
4311 readline_add_completion(rs, chr_info->label);
4312 }
4313 }
4314 list = list->next;
4315 }
4316 qapi_free_ChardevInfoList(start);
4317}
4318
Hani Benhabiles8e597772014-05-27 23:39:30 +01004319void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4320{
4321 if (nb_args != 2) {
4322 return;
4323 }
4324 ringbuf_completion(rs, str);
4325}
4326
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004327void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4328{
4329 size_t len;
4330
4331 if (nb_args != 2) {
4332 return;
4333 }
4334
4335 len = strlen(str);
4336 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004337 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004338}
4339
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004340void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004341{
4342 ObjectPropertyInfoList *list, *start;
4343 size_t len;
4344
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004345 if (nb_args != 2) {
4346 return;
4347 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004348 len = strlen(str);
4349 readline_set_completion_index(rs, len);
4350
4351 start = list = qmp_qom_list("/objects", NULL);
4352 while (list) {
4353 ObjectPropertyInfo *info = list->value;
4354
4355 if (!strncmp(info->type, "child<", 5)
4356 && !strncmp(info->name, str, len)) {
4357 readline_add_completion(rs, info->name);
4358 }
4359 list = list->next;
4360 }
4361 qapi_free_ObjectPropertyInfoList(start);
4362}
4363
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004364void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4365{
4366 int i;
4367 char *sep;
4368 size_t len;
4369
4370 if (nb_args != 2) {
4371 return;
4372 }
4373 sep = strrchr(str, '-');
4374 if (sep) {
4375 str = sep + 1;
4376 }
4377 len = strlen(str);
4378 readline_set_completion_index(rs, len);
4379 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4380 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4381 readline_add_completion(rs, QKeyCode_lookup[i]);
4382 }
4383 }
4384}
4385
Hani Benhabiles40d19392014-05-07 23:41:30 +01004386void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4387{
4388 size_t len;
4389
4390 len = strlen(str);
4391 readline_set_completion_index(rs, len);
4392 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004393 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004394 int count, i;
4395 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004396 NET_CLIENT_OPTIONS_KIND_NONE,
4397 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004398 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004399 const char *name = ncs[i]->name;
4400 if (!strncmp(str, name, len)) {
4401 readline_add_completion(rs, name);
4402 }
4403 }
4404 } else if (nb_args == 3) {
4405 add_completion_option(rs, str, "on");
4406 add_completion_option(rs, str, "off");
4407 }
4408}
4409
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004410void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4411{
4412 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004413 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004414
4415 if (nb_args != 2) {
4416 return;
4417 }
4418
4419 len = strlen(str);
4420 readline_set_completion_index(rs, len);
4421 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004422 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004423 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004424 QemuOpts *opts;
4425 const char *name = ncs[i]->name;
4426 if (strncmp(str, name, len)) {
4427 continue;
4428 }
4429 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4430 if (opts) {
4431 readline_add_completion(rs, name);
4432 }
4433 }
4434}
4435
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004436void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4437{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004438 int i;
4439
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004440 if (nb_args != 2) {
4441 return;
4442 }
4443 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004444 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4445 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4446 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004447}
4448
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004449void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4450 const char *str)
4451{
4452 size_t len;
4453
4454 len = strlen(str);
4455 readline_set_completion_index(rs, len);
4456 if (nb_args == 2) {
4457 int i;
4458 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4459 const char *name = MigrationCapability_lookup[i];
4460 if (!strncmp(str, name, len)) {
4461 readline_add_completion(rs, name);
4462 }
4463 }
4464 } else if (nb_args == 3) {
4465 add_completion_option(rs, str, "on");
4466 add_completion_option(rs, str, "off");
4467 }
4468}
4469
Liang Li50e9a622015-03-23 16:32:29 +08004470void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4471 const char *str)
4472{
4473 size_t len;
4474
4475 len = strlen(str);
4476 readline_set_completion_index(rs, len);
4477 if (nb_args == 2) {
4478 int i;
4479 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4480 const char *name = MigrationParameter_lookup[i];
4481 if (!strncmp(str, name, len)) {
4482 readline_add_completion(rs, name);
4483 }
4484 }
4485 }
4486}
4487
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004488void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4489{
4490 int i;
4491 size_t len;
4492 if (nb_args != 2) {
4493 return;
4494 }
4495 len = strlen(str);
4496 readline_set_completion_index(rs, len);
4497 for (i = 0; host_net_devices[i]; i++) {
4498 if (!strncmp(host_net_devices[i], str, len)) {
4499 readline_add_completion(rs, host_net_devices[i]);
4500 }
4501 }
4502}
4503
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004504void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4505{
Jason Wangeaed4832015-04-23 14:21:38 +08004506 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004507 int count, i, len;
4508
4509 len = strlen(str);
4510 readline_set_completion_index(rs, len);
4511 if (nb_args == 2) {
4512 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004513 NET_CLIENT_OPTIONS_KIND_NONE,
4514 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004515 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004516 int id;
4517 char name[16];
4518
4519 if (net_hub_id_for_client(ncs[i], &id)) {
4520 continue;
4521 }
4522 snprintf(name, sizeof(name), "%d", id);
4523 if (!strncmp(str, name, len)) {
4524 readline_add_completion(rs, name);
4525 }
4526 }
4527 return;
4528 } else if (nb_args == 3) {
4529 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004530 NET_CLIENT_OPTIONS_KIND_NIC,
4531 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004532 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004533 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004534 const char *name;
4535
Jason Wang2c4681f2015-02-02 15:06:38 +08004536 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4537 net_hub_id_for_client(ncs[i], &id)) {
4538 continue;
4539 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004540 name = ncs[i]->name;
4541 if (!strncmp(str, name, len)) {
4542 readline_add_completion(rs, name);
4543 }
4544 }
4545 return;
4546 }
4547}
4548
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004549static void vm_completion(ReadLineState *rs, const char *str)
4550{
4551 size_t len;
4552 BlockDriverState *bs = NULL;
4553
4554 len = strlen(str);
4555 readline_set_completion_index(rs, len);
4556 while ((bs = bdrv_next(bs))) {
4557 SnapshotInfoList *snapshots, *snapshot;
4558
4559 if (!bdrv_can_snapshot(bs)) {
4560 continue;
4561 }
4562 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4563 continue;
4564 }
4565 snapshot = snapshots;
4566 while (snapshot) {
4567 char *completion = snapshot->value->name;
4568 if (!strncmp(str, completion, len)) {
4569 readline_add_completion(rs, completion);
4570 }
4571 completion = snapshot->value->id;
4572 if (!strncmp(str, completion, len)) {
4573 readline_add_completion(rs, completion);
4574 }
4575 snapshot = snapshot->next;
4576 }
4577 qapi_free_SnapshotInfoList(snapshots);
4578 }
4579
4580}
4581
4582void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4583{
4584 if (nb_args == 2) {
4585 vm_completion(rs, str);
4586 }
4587}
4588
4589void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4590{
4591 if (nb_args == 2) {
4592 vm_completion(rs, str);
4593 }
4594}
4595
Wenchao Xiac35b6402013-08-27 20:38:24 +08004596static void monitor_find_completion_by_table(Monitor *mon,
4597 const mon_cmd_t *cmd_table,
4598 char **args,
4599 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004600{
4601 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004602 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004603 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004604 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004605 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004606
bellard81d09122004-07-14 17:21:37 +00004607 if (nb_args <= 1) {
4608 /* command completion */
4609 if (nb_args == 0)
4610 cmdname = "";
4611 else
4612 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004613 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004614 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004615 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004616 }
4617 } else {
4618 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004619 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004620 if (compare_cmd(args[0], cmd->name)) {
4621 break;
4622 }
bellard81d09122004-07-14 17:21:37 +00004623 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004624 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004625 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004626 }
4627
Wenchao Xiad903a772013-08-27 20:38:25 +08004628 if (cmd->sub_table) {
4629 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004630 monitor_find_completion_by_table(mon, cmd->sub_table,
4631 &args[1], nb_args - 1);
4632 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004633 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004634 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004635 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4636 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004637 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004638
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004639 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004640 for(i = 0; i < nb_args - 2; i++) {
4641 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004642 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004643 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004644 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004645 }
4646 }
4647 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004648 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004649 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004650 }
bellard81d09122004-07-14 17:21:37 +00004651 switch(*ptype) {
4652 case 'F':
4653 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004654 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004655 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004656 break;
4657 case 'B':
4658 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004659 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004660 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4661 name = bdrv_get_device_name(bs);
4662 if (str[0] == '\0' ||
4663 !strncmp(name, str, strlen(str))) {
4664 readline_add_completion(mon->rs, name);
4665 }
4666 }
bellard81d09122004-07-14 17:21:37 +00004667 break;
bellard7fe48482004-10-09 18:08:01 +00004668 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004669 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004670 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004671 monitor_find_completion_by_table(mon, cmd_table,
4672 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004673 }
4674 break;
bellard81d09122004-07-14 17:21:37 +00004675 default:
4676 break;
4677 }
4678 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004679}
4680
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004681static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004682 const char *cmdline)
4683{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004684 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004685 char *args[MAX_ARGS];
4686 int nb_args, len;
4687
4688 /* 1. parse the cmdline */
4689 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4690 return;
4691 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004692
4693 /* if the line ends with a space, it means we want to complete the
4694 next arg */
4695 len = strlen(cmdline);
4696 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4697 if (nb_args >= MAX_ARGS) {
4698 goto cleanup;
4699 }
4700 args[nb_args++] = g_strdup("");
4701 }
4702
4703 /* 2. auto complete according to args */
4704 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004705
4706cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004707 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004708}
4709
aliguori731b0362009-03-05 23:01:42 +00004710static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004711{
aliguori731b0362009-03-05 23:01:42 +00004712 Monitor *mon = opaque;
4713
Jan Kiszkac62313b2009-12-04 14:05:29 +01004714 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004715}
4716
Markus Armbruster40861822015-05-29 10:27:16 +02004717static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4718 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004719{
Markus Armbruster485febc2015-03-13 17:25:50 +01004720 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004721
4722 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004723 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4724 "Capabilities negotiation is already complete, command "
4725 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004726 return true;
4727 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004728 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004729 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4730 "Expecting capabilities negotiation with "
4731 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004732 return true;
4733 }
4734 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004735}
4736
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004737/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004738 * Argument validation rules:
4739 *
4740 * 1. The argument must exist in cmd_args qdict
4741 * 2. The argument type must be the expected one
4742 *
4743 * Special case: If the argument doesn't exist in cmd_args and
4744 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4745 * checking is skipped for it.
4746 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004747static void check_client_args_type(const QDict *client_args,
4748 const QDict *cmd_args, int flags,
4749 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004750{
4751 const QDictEntry *ent;
4752
4753 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4754 QObject *obj;
4755 QString *arg_type;
4756 const QObject *client_arg = qdict_entry_value(ent);
4757 const char *client_arg_name = qdict_entry_key(ent);
4758
4759 obj = qdict_get(cmd_args, client_arg_name);
4760 if (!obj) {
4761 if (flags & QMP_ACCEPT_UNKNOWNS) {
4762 /* handler accepts unknowns */
4763 continue;
4764 }
4765 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004766 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004767 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004768 }
4769
4770 arg_type = qobject_to_qstring(obj);
4771 assert(arg_type != NULL);
4772
4773 /* check if argument's type is correct */
4774 switch (qstring_get_str(arg_type)[0]) {
4775 case 'F':
4776 case 'B':
4777 case 's':
4778 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004779 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4780 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01004781 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004782 }
4783 break;
4784 case 'i':
4785 case 'l':
4786 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004787 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004788 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004789 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4790 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01004791 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004792 }
4793 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004794 case 'T':
4795 if (qobject_type(client_arg) != QTYPE_QINT &&
4796 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004797 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4798 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01004799 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004800 }
4801 break;
4802 case 'b':
4803 case '-':
4804 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004805 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4806 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01004807 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004808 }
4809 break;
4810 case 'O':
4811 assert(flags & QMP_ACCEPT_UNKNOWNS);
4812 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004813 case 'q':
4814 /* Any QObject can be passed. */
4815 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004816 case '/':
4817 case '.':
4818 /*
4819 * These types are not supported by QMP and thus are not
4820 * handled here. Fall through.
4821 */
4822 default:
4823 abort();
4824 }
4825 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004826}
4827
4828/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004829 * - Check if the client has passed all mandatory args
4830 * - Set special flags for argument validation
4831 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004832static void check_mandatory_args(const QDict *cmd_args,
4833 const QDict *client_args, int *flags,
4834 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004835{
4836 const QDictEntry *ent;
4837
4838 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4839 const char *cmd_arg_name = qdict_entry_key(ent);
4840 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4841 assert(type != NULL);
4842
4843 if (qstring_get_str(type)[0] == 'O') {
4844 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4845 *flags |= QMP_ACCEPT_UNKNOWNS;
4846 } else if (qstring_get_str(type)[0] != '-' &&
4847 qstring_get_str(type)[1] != '?' &&
4848 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004849 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004850 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004851 }
4852 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004853}
4854
4855static QDict *qdict_from_args_type(const char *args_type)
4856{
4857 int i;
4858 QDict *qdict;
4859 QString *key, *type, *cur_qs;
4860
4861 assert(args_type != NULL);
4862
4863 qdict = qdict_new();
4864
4865 if (args_type == NULL || args_type[0] == '\0') {
4866 /* no args, empty qdict */
4867 goto out;
4868 }
4869
4870 key = qstring_new();
4871 type = qstring_new();
4872
4873 cur_qs = key;
4874
4875 for (i = 0;; i++) {
4876 switch (args_type[i]) {
4877 case ',':
4878 case '\0':
4879 qdict_put(qdict, qstring_get_str(key), type);
4880 QDECREF(key);
4881 if (args_type[i] == '\0') {
4882 goto out;
4883 }
4884 type = qstring_new(); /* qdict has ref */
4885 cur_qs = key = qstring_new();
4886 break;
4887 case ':':
4888 cur_qs = type;
4889 break;
4890 default:
4891 qstring_append_chr(cur_qs, args_type[i]);
4892 break;
4893 }
4894 }
4895
4896out:
4897 return qdict;
4898}
4899
4900/*
4901 * Client argument checking rules:
4902 *
4903 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004904 * 2. Each argument provided by the client must be expected
4905 * 3. Each argument provided by the client must have the type expected
4906 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004907 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004908static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4909 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004910{
Markus Armbruster326283a2015-03-02 18:39:09 +01004911 Error *err = NULL;
4912 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004913 QDict *cmd_args;
4914
4915 cmd_args = qdict_from_args_type(cmd->args_type);
4916
4917 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004918 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004919 if (err) {
4920 goto out;
4921 }
4922
Markus Armbruster326283a2015-03-02 18:39:09 +01004923 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004924
4925out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004926 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004927 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004928}
4929
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004930/*
4931 * Input object checking rules
4932 *
4933 * 1. Input object must be a dict
4934 * 2. The "execute" key must exist
4935 * 3. The "execute" key must be a string
4936 * 4. If the "arguments" key exists, it must be a dict
4937 * 5. If the "id" key exists, it can be anything (ie. json-value)
4938 * 6. Any argument not listed above is considered invalid
4939 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004940static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004941{
4942 const QDictEntry *ent;
4943 int has_exec_key = 0;
4944 QDict *input_dict;
4945
4946 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004947 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004948 return NULL;
4949 }
4950
4951 input_dict = qobject_to_qdict(input_obj);
4952
4953 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4954 const char *arg_name = qdict_entry_key(ent);
4955 const QObject *arg_obj = qdict_entry_value(ent);
4956
4957 if (!strcmp(arg_name, "execute")) {
4958 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004959 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4960 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004961 return NULL;
4962 }
4963 has_exec_key = 1;
4964 } else if (!strcmp(arg_name, "arguments")) {
4965 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004966 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4967 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004968 return NULL;
4969 }
Markus Armbruster779cec42015-06-08 10:44:30 +02004970 } else if (!strcmp(arg_name, "id")) {
4971 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004972 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004973 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004974 return NULL;
4975 }
4976 }
4977
4978 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004979 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004980 return NULL;
4981 }
4982
4983 return input_dict;
4984}
4985
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004986static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4987{
Markus Armbruster326283a2015-03-02 18:39:09 +01004988 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004989 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004990 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004991 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004992 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004993 Monitor *mon = cur_mon;
4994
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004995 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004996 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004997
4998 obj = json_parser_parse(tokens, NULL);
4999 if (!obj) {
5000 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005001 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005002 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005003 }
5004
Markus Armbrusterba0510a2015-03-02 18:41:43 +01005005 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005006 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005007 qobject_decref(obj);
5008 goto err_out;
5009 }
5010
Markus Armbruster74358f22015-03-06 19:35:59 +01005011 mon->qmp.id = qdict_get(input, "id");
5012 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005013
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005014 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005015 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005016 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005017 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005018 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5019 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005020 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005021 }
Markus Armbruster40861822015-05-29 10:27:16 +02005022 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005023 goto err_out;
5024 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005025
5026 obj = qdict_get(input, "arguments");
5027 if (!obj) {
5028 args = qdict_new();
5029 } else {
5030 args = qobject_to_qdict(obj);
5031 QINCREF(args);
5032 }
5033
Markus Armbruster326283a2015-03-02 18:39:09 +01005034 qmp_check_client_args(cmd, args, &local_err);
5035 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005036 goto err_out;
5037 }
5038
Markus Armbruster485febc2015-03-13 17:25:50 +01005039 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005040
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005041err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005042 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005043 qobject_decref(data);
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
Markus Armbruster485febc2015-03-13 17:25:50 +01005109 qmp_marshal_input_query_version(NULL, &ver, NULL);
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);
bellard7e2515e2004-08-01 21:52:19 +00005273}
5274
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005275static void bdrv_password_cb(void *opaque, const char *password,
5276 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005277{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005278 Monitor *mon = opaque;
5279 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005280 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005281 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005282
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005283 bdrv_add_key(bs, password, &local_err);
5284 if (local_err) {
5285 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5286 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005287 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005288 }
aliguori731b0362009-03-05 23:01:42 +00005289 if (mon->password_completion_cb)
5290 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005291
aliguori731b0362009-03-05 23:01:42 +00005292 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005293}
aliguoric0f4ce72009-03-05 23:01:01 +00005294
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005295int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005296 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005297 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005298{
aliguoricde76ee2009-03-05 23:01:51 +00005299 int err;
5300
aliguori376253e2009-03-05 23:01:23 +00005301 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5302 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005303
aliguori731b0362009-03-05 23:01:42 +00005304 mon->password_completion_cb = completion_cb;
5305 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005306
aliguoricde76ee2009-03-05 23:01:51 +00005307 err = monitor_read_password(mon, bdrv_password_cb, bs);
5308
5309 if (err && completion_cb)
5310 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005311
5312 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005313}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005314
5315int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005316 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005317 void *opaque)
5318{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005319 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005320 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005321
Fam Zheng55606252015-03-02 19:36:46 +08005322 blk = blk_by_name(device);
5323 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005324 monitor_printf(mon, "Device not found %s\n", device);
5325 return -1;
5326 }
5327
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005328 bdrv_add_key(blk_bs(blk), NULL, &err);
5329 if (err) {
5330 error_free(err);
5331 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5332 }
5333
5334 if (completion_cb) {
5335 completion_cb(opaque, 0);
5336 }
5337 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005338}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005339
5340QemuOptsList qemu_mon_opts = {
5341 .name = "mon",
5342 .implied_opt_name = "chardev",
5343 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5344 .desc = {
5345 {
5346 .name = "mode",
5347 .type = QEMU_OPT_STRING,
5348 },{
5349 .name = "chardev",
5350 .type = QEMU_OPT_STRING,
5351 },{
5352 .name = "default",
5353 .type = QEMU_OPT_BOOL,
5354 },{
5355 .name = "pretty",
5356 .type = QEMU_OPT_BOOL,
5357 },
5358 { /* end of list */ }
5359 },
5360};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005361
5362#ifndef TARGET_I386
5363void qmp_rtc_reset_reinjection(Error **errp)
5364{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005365 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005366}
5367#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04005368
5369#ifndef TARGET_S390X
5370void qmp_dump_skeys(const char *filename, Error **errp)
5371{
5372 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
5373}
5374#endif