blob: 497fed289e1d68c6aad66c0e8d83beff42fabd22 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010052#include "qapi/qmp/qint.h"
53#include "qapi/qmp/qfloat.h"
54#include "qapi/qmp/qlist.h"
55#include "qapi/qmp/qbool.h"
56#include "qapi/qmp/qstring.h"
57#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010060#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010061#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010069#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080076#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000077
Markus Armbruster1ce6be22015-02-06 14:18:24 +010078/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020079#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010080#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020081#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083
bellard9307c4c2004-04-04 12:57:25 +000084/*
85 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000086 *
bellard9307c4c2004-04-04 12:57:25 +000087 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000088 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000089 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080090 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010091 * 'O' option string of the form NAME=VALUE,...
92 * parsed according to QemuOptsList given by its name
93 * Example: 'device:O' uses qemu_device_opts.
94 * Restriction: only lists with empty desc are supported
95 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000096 * 'i' 32 bit integer
97 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030098 * 'M' Non-negative target long (32 or 64 bit), in user mode the
99 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200100 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200101 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
102 * K, k suffix, which multiplies the value by 2^60 for suffixes E
103 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
104 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100105 * 'T' double
106 * user mode accepts an optional ms, us, ns suffix,
107 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000108 * '/' optional gdb-like print format (like "/10x")
109 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300110 * '?' optional type (for all types, except '/')
111 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100112 * 'b' boolean
113 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300114 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000115 *
116 */
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300123 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300124 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100125 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300126 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800127 /* @sub_table is a list of 2nd level of commands. If it do not exist,
128 * mhandler should be used. If it exist, sub_table[?].mhandler should be
129 * used, and mhandler of 1st level plays the role of help function.
130 */
131 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100132 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Corey Bryantba1c0482012-08-14 16:43:43 -0400143/* file descriptor associated with a file descriptor set */
144typedef struct MonFdsetFd MonFdsetFd;
145struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
150};
151
152/* file descriptor set containing fds passed via SCM_RIGHTS */
153typedef struct MonFdset MonFdset;
154struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400157 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400158 QLIST_ENTRY(MonFdset) next;
159};
160
Markus Armbruster74358f22015-03-06 19:35:59 +0100161typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200162 QObject *id;
163 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100164 /*
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
167 * mode.
168 */
169 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100170} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200171
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100172/*
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
176 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200177typedef struct MonitorQAPIEventState {
178 QAPIEvent event; /* Event being tracked */
179 int64_t rate; /* Minimum time (in ns) between two events */
180 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100181 QEMUTimer *timer; /* Timer for handling delayed events */
182 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200183} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100184
aliguori87127162009-03-05 23:01:29 +0000185struct Monitor {
186 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200187 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000188 int flags;
189 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400190 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200191
192 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400193 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200194 guint out_watch;
195
196 /* Read under either BQL or out_lock, written with BQL+out_lock. */
197 int mux_out;
198
aliguori731b0362009-03-05 23:01:42 +0000199 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100200 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200201 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200202 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000203 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800204 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500205 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000206 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000207};
208
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300209/* QMP checker flags */
210#define QMP_ACCEPT_UNKNOWNS 1
211
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200212/* Protects mon_list, monitor_event_state. */
213static QemuMutex monitor_lock;
214
Blue Swirl72cf2d42009-09-12 07:36:22 +0000215static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400216static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400217static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000218
Wayne Xia816f8922011-10-12 11:32:41 +0800219static mon_cmd_t mon_cmds[];
220static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000221
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300222static const mon_cmd_t qmp_cmds[];
223
Markus Armbruster8631b602010-02-18 11:41:55 +0100224Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000225
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100226static void monitor_command_cb(void *opaque, const char *cmdline,
227 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000228
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100229/**
230 * Is @mon a QMP monitor?
231 */
232static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200233{
234 return (mon->flags & MONITOR_USE_CONTROL);
235}
236
Markus Armbruster489653b2015-03-06 20:01:05 +0100237/**
238 * Is the current monitor, if any, a QMP monitor?
239 */
240bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100241{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100242 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100243}
244
Anthony Liguori7060b472011-09-02 12:34:50 -0500245void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000246{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200247 if (!mon->rs)
248 return;
249
aliguori731b0362009-03-05 23:01:42 +0000250 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
251 if (show_prompt)
252 readline_show_prompt(mon->rs);
253}
bellard6a00d602005-11-21 23:25:50 +0000254
Anthony Liguori7060b472011-09-02 12:34:50 -0500255int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
256 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000257{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100258 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000259 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
260 /* prompt is printed on return from the command handler */
261 return 0;
262 } else {
263 monitor_printf(mon, "terminal does not support password prompting\n");
264 return -ENOTTY;
265 }
aliguoribb5fc202009-03-05 23:01:15 +0000266}
267
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200268static void monitor_flush_locked(Monitor *mon);
269
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100270static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
271 void *opaque)
272{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200273 Monitor *mon = opaque;
274
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200275 qemu_mutex_lock(&mon->out_lock);
276 mon->out_watch = 0;
277 monitor_flush_locked(mon);
278 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100279 return FALSE;
280}
281
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200282/* Called with mon->out_lock held. */
283static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000284{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100285 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400286 size_t len;
287 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100288
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400289 if (mon->skip_flush) {
290 return;
291 }
292
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400293 buf = qstring_get_str(mon->outbuf);
294 len = qstring_get_length(mon->outbuf);
295
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200296 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400297 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200298 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
299 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400300 QDECREF(mon->outbuf);
301 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100302 return;
303 }
304 if (rc > 0) {
305 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 QString *tmp = qstring_from_str(buf + rc);
307 QDECREF(mon->outbuf);
308 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100309 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200310 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200311 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200312 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200313 }
bellard7e2515e2004-08-01 21:52:19 +0000314 }
315}
316
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200317void monitor_flush(Monitor *mon)
318{
319 qemu_mutex_lock(&mon->out_lock);
320 monitor_flush_locked(mon);
321 qemu_mutex_unlock(&mon->out_lock);
322}
323
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400324/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000325static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000326{
ths60fe76f2007-12-16 03:02:09 +0000327 char c;
aliguori731b0362009-03-05 23:01:42 +0000328
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200329 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000330 for(;;) {
331 c = *str++;
332 if (c == '\0')
333 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400334 if (c == '\n') {
335 qstring_append_chr(mon->outbuf, '\r');
336 }
337 qstring_append_chr(mon->outbuf, c);
338 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200339 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340 }
bellard7e2515e2004-08-01 21:52:19 +0000341 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200342 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000343}
344
aliguori376253e2009-03-05 23:01:23 +0000345void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000346{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400347 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200348
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200349 if (!mon)
350 return;
351
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100352 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200354 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200355
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400356 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400358 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000359}
360
aliguori376253e2009-03-05 23:01:23 +0000361void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000362{
363 va_list ap;
364 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000365 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000366 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000367}
368
Stefan Weil8b7968f2010-09-23 21:28:05 +0200369static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
370 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000371{
372 va_list ap;
373 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000375 va_end(ap);
376 return 0;
377}
378
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200379static void monitor_json_emitter(Monitor *mon, const QObject *data)
380{
381 QString *json;
382
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200383 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
384 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200385 assert(json != NULL);
386
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200387 qstring_append_chr(json, '\n');
388 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200389
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390 QDECREF(json);
391}
392
Markus Armbruster710aec92015-03-06 11:28:00 +0100393static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300394{
395 QObject *obj;
396
Markus Armbruster710aec92015-03-06 11:28:00 +0100397 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
398 ErrorClass_lookup[error_get_class(err)],
399 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300400
401 return qobject_to_qdict(obj);
402}
403
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100404static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100405 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200406{
407 QDict *qmp;
408
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100409 trace_monitor_protocol_emitter(mon);
410
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100411 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200412 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300413 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200414 if (data) {
415 qobject_incref(data);
416 qdict_put_obj(qmp, "return", data);
417 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200418 /* return an empty QDict by default */
419 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200420 }
421 } else {
422 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100423 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 }
425
Markus Armbruster74358f22015-03-06 19:35:59 +0100426 if (mon->qmp.id) {
427 qdict_put_obj(qmp, "id", mon->qmp.id);
428 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200429 }
430
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200431 monitor_json_emitter(mon, QOBJECT(qmp));
432 QDECREF(qmp);
433}
434
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200435
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200436static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100437
438/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200439 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200440 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100441 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200442static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100443{
444 Monitor *mon;
445
446 trace_monitor_protocol_event_emit(event, data);
447 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100448 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100449 monitor_json_emitter(mon, data);
450 }
451 }
452}
453
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100454/*
455 * Queue a new event for emission to Monitor instances,
456 * applying any rate limiting if required.
457 */
458static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200459monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200461 MonitorQAPIEventState *evstate;
462 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100463 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200465 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100466 trace_monitor_protocol_event_queue(event,
467 data,
468 evstate->rate,
469 evstate->last,
470 now);
471
472 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200473 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100474 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200475 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100476 evstate->last = now;
477 } else {
478 int64_t delta = now - evstate->last;
479 if (evstate->data ||
480 delta < evstate->rate) {
481 /* If there's an existing event pending, replace
482 * it with the new event, otherwise schedule a
483 * timer for delayed emission
484 */
485 if (evstate->data) {
486 qobject_decref(evstate->data);
487 } else {
488 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100489 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100490 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200491 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100492 qobject_incref(evstate->data);
493 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200494 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495 evstate->last = now;
496 }
497 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200498 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100499}
500
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100501/*
502 * The callback invoked by QemuTimer when a delayed
503 * event is ready to be emitted
504 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200505static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100506{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200507 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100508 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100509
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510 trace_monitor_protocol_event_handler(evstate->event,
511 evstate->data,
512 evstate->last,
513 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200514 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200516 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100517 qobject_decref(evstate->data);
518 evstate->data = NULL;
519 }
520 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200521 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522}
523
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100524/*
525 * @event: the event ID to be limited
526 * @rate: the rate limit in milliseconds
527 *
528 * Sets a rate limit on a particular event, so no
529 * more than 1 event will be emitted within @rate
530 * milliseconds
531 */
532static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200533monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100534{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200535 MonitorQAPIEventState *evstate;
536 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200538 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539
540 trace_monitor_protocol_event_throttle(event, rate);
541 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700542 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544 evstate->last = 0;
545 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200546 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
547 SCALE_MS,
548 monitor_qapi_event_handler,
549 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100550}
551
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200552static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100553{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200554 /* Limit guest-triggerable events to 1 per second */
555 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200556 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200557 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200558 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200560 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100561
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200562 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200563}
564
Markus Armbruster485febc2015-03-13 17:25:50 +0100565static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200566{
Markus Armbruster485febc2015-03-13 17:25:50 +0100567 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200568}
569
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100570static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200571
Wenchao Xiab01fe892013-08-27 20:38:19 +0800572static void monitor_data_init(Monitor *mon)
573{
574 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200575 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800576 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800577 /* Use *mon_cmds by default. */
578 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800579}
580
581static void monitor_data_destroy(Monitor *mon)
582{
583 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200584 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800585}
586
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200587char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
588 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200589{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200590 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200591 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200592
Wenchao Xiab01fe892013-08-27 20:38:19 +0800593 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400594 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595
596 old_mon = cur_mon;
597 cur_mon = &hmp;
598
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200599 if (has_cpu_index) {
600 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200601 if (ret < 0) {
602 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100603 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
604 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200605 goto out;
606 }
607 }
608
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100609 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 cur_mon = old_mon;
611
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200612 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400613 if (qstring_get_length(hmp.outbuf) > 0) {
614 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200615 } else {
616 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200617 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200618 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200619
620out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800621 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200622 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623}
624
bellard9dc39cb2004-03-14 21:38:27 +0000625static int compare_cmd(const char *name, const char *list)
626{
627 const char *p, *pstart;
628 int len;
629 len = strlen(name);
630 p = list;
631 for(;;) {
632 pstart = p;
633 p = strchr(p, '|');
634 if (!p)
635 p = pstart + strlen(pstart);
636 if ((p - pstart) == len && !memcmp(pstart, name, len))
637 return 1;
638 if (*p == '\0')
639 break;
640 p++;
641 }
642 return 0;
643}
644
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800645static int get_str(char *buf, int buf_size, const char **pp)
646{
647 const char *p;
648 char *q;
649 int c;
650
651 q = buf;
652 p = *pp;
653 while (qemu_isspace(*p)) {
654 p++;
655 }
656 if (*p == '\0') {
657 fail:
658 *q = '\0';
659 *pp = p;
660 return -1;
661 }
662 if (*p == '\"') {
663 p++;
664 while (*p != '\0' && *p != '\"') {
665 if (*p == '\\') {
666 p++;
667 c = *p++;
668 switch (c) {
669 case 'n':
670 c = '\n';
671 break;
672 case 'r':
673 c = '\r';
674 break;
675 case '\\':
676 case '\'':
677 case '\"':
678 break;
679 default:
680 qemu_printf("unsupported escape code: '\\%c'\n", c);
681 goto fail;
682 }
683 if ((q - buf) < buf_size - 1) {
684 *q++ = c;
685 }
686 } else {
687 if ((q - buf) < buf_size - 1) {
688 *q++ = *p;
689 }
690 p++;
691 }
692 }
693 if (*p != '\"') {
694 qemu_printf("unterminated string\n");
695 goto fail;
696 }
697 p++;
698 } else {
699 while (*p != '\0' && !qemu_isspace(*p)) {
700 if ((q - buf) < buf_size - 1) {
701 *q++ = *p;
702 }
703 p++;
704 }
705 }
706 *q = '\0';
707 *pp = p;
708 return 0;
709}
710
711#define MAX_ARGS 16
712
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800713static void free_cmdline_args(char **args, int nb_args)
714{
715 int i;
716
717 assert(nb_args <= MAX_ARGS);
718
719 for (i = 0; i < nb_args; i++) {
720 g_free(args[i]);
721 }
722
723}
724
725/*
726 * Parse the command line to get valid args.
727 * @cmdline: command line to be parsed.
728 * @pnb_args: location to store the number of args, must NOT be NULL.
729 * @args: location to store the args, which should be freed by caller, must
730 * NOT be NULL.
731 *
732 * Returns 0 on success, negative on failure.
733 *
734 * NOTE: this parser is an approximate form of the real command parser. Number
735 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
736 * return with failure.
737 */
738static int parse_cmdline(const char *cmdline,
739 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800740{
741 const char *p;
742 int nb_args, ret;
743 char buf[1024];
744
745 p = cmdline;
746 nb_args = 0;
747 for (;;) {
748 while (qemu_isspace(*p)) {
749 p++;
750 }
751 if (*p == '\0') {
752 break;
753 }
754 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800755 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800756 }
757 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800758 if (ret < 0) {
759 goto fail;
760 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 args[nb_args] = g_strdup(buf);
762 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800763 }
764 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800765 return 0;
766
767 fail:
768 free_cmdline_args(args, nb_args);
769 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800770}
771
Wenchao Xia66855492013-08-27 20:38:23 +0800772static void help_cmd_dump_one(Monitor *mon,
773 const mon_cmd_t *cmd,
774 char **prefix_args,
775 int prefix_args_nb)
776{
777 int i;
778
779 for (i = 0; i < prefix_args_nb; i++) {
780 monitor_printf(mon, "%s ", prefix_args[i]);
781 }
782 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
783}
784
785/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500786static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800787 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000788{
Anthony Liguoric227f092009-10-01 16:12:16 -0500789 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000790
Wenchao Xia66855492013-08-27 20:38:23 +0800791 /* No valid arg need to compare with, dump all in *cmds */
792 if (arg_index >= nb_args) {
793 for (cmd = cmds; cmd->name != NULL; cmd++) {
794 help_cmd_dump_one(mon, cmd, args, arg_index);
795 }
796 return;
797 }
798
799 /* Find one entry to dump */
800 for (cmd = cmds; cmd->name != NULL; cmd++) {
801 if (compare_cmd(args[arg_index], cmd->name)) {
802 if (cmd->sub_table) {
803 /* continue with next arg */
804 help_cmd_dump(mon, cmd->sub_table,
805 args, nb_args, arg_index + 1);
806 } else {
807 help_cmd_dump_one(mon, cmd, args, arg_index);
808 }
809 break;
810 }
bellard9dc39cb2004-03-14 21:38:27 +0000811 }
812}
813
aliguori376253e2009-03-05 23:01:23 +0000814static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000815{
Wenchao Xia66855492013-08-27 20:38:23 +0800816 char *args[MAX_ARGS];
817 int nb_args = 0;
818
819 /* 1. parse user input */
820 if (name) {
821 /* special case for log, directly dump and return */
822 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000823 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "Log items (comma separated):\n");
825 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000826 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000828 }
Wenchao Xia66855492013-08-27 20:38:23 +0800829 return;
830 }
831
832 if (parse_cmdline(name, &nb_args, args) < 0) {
833 return;
bellardf193c792004-03-21 17:06:25 +0000834 }
bellard9dc39cb2004-03-14 21:38:27 +0000835 }
Wenchao Xia66855492013-08-27 20:38:23 +0800836
837 /* 2. dump the contents according to parsed args */
838 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
839
840 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000841}
842
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300843static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300844{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300845 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300846}
847
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100848static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530849{
850 const char *tp_name = qdict_get_str(qdict, "name");
851 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200852 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000853
Lluís Vilanova14101d02014-08-25 13:20:03 +0200854 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
855 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100856 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000857 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530858}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100859
Michael Rothc45a8162011-10-02 08:44:37 -0500860#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100861static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100862{
863 const char *op = qdict_get_try_str(qdict, "op");
864 const char *arg = qdict_get_try_str(qdict, "arg");
865
866 if (!op) {
867 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
868 } else if (!strcmp(op, "on")) {
869 st_set_trace_file_enabled(true);
870 } else if (!strcmp(op, "off")) {
871 st_set_trace_file_enabled(false);
872 } else if (!strcmp(op, "flush")) {
873 st_flush_trace_buffer();
874 } else if (!strcmp(op, "set")) {
875 if (arg) {
876 st_set_trace_file(arg);
877 }
878 } else {
879 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
880 help_cmd(mon, "trace-file");
881 }
882}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530883#endif
884
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100885static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000886{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300887 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000888}
889
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300890CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000891{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300892 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200893 const mon_cmd_t *cmd;
894
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300895 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200896 info = g_malloc0(sizeof(*info));
897 info->value = g_malloc0(sizeof(*info->value));
898 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200899
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300900 info->next = cmd_list;
901 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200902 }
903
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300904 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000905}
906
Daniel P. Berrange48608532012-05-21 17:59:51 +0100907EventInfoList *qmp_query_events(Error **errp)
908{
909 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200910 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100911
Wenchao Xia75175172014-06-18 08:43:54 +0200912 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
913 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100914 assert(event_name != NULL);
915 info = g_malloc0(sizeof(*info));
916 info->value = g_malloc0(sizeof(*info->value));
917 info->value->name = g_strdup(event_name);
918
919 info->next = ev_list;
920 ev_list = info;
921 }
922
923 return ev_list;
924}
925
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300926/* set the current CPU defined by the user */
927int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000928{
Andreas Färber55e5c282012-12-17 06:18:02 +0100929 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000930
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100931 cpu = qemu_get_cpu(cpu_index);
932 if (cpu == NULL) {
933 return -1;
bellard6a00d602005-11-21 23:25:50 +0000934 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200935 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100936 return 0;
bellard6a00d602005-11-21 23:25:50 +0000937}
938
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700939static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000940{
aliguori731b0362009-03-05 23:01:42 +0000941 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300942 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000943 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300944 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700945 return cur_mon->mon_cpu;
946}
947
948static CPUArchState *mon_get_cpu_env(void)
949{
950 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000951}
952
Luiz Capitulino99b77962011-10-24 10:53:44 -0200953int monitor_get_cpu_index(void)
954{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700955 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200956}
957
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100958static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000959{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700960 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000961}
962
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100963static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000964{
aliguori376253e2009-03-05 23:01:23 +0000965 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200966 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000967}
968
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100969static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300970{
971 dump_opcount_info((FILE *)mon, monitor_fprintf);
972}
973
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100974static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000975{
976 int i;
bellard7e2515e2004-08-01 21:52:19 +0000977 const char *str;
ths3b46e622007-09-17 08:09:54 +0000978
aliguoricde76ee2009-03-05 23:01:51 +0000979 if (!mon->rs)
980 return;
bellard7e2515e2004-08-01 21:52:19 +0000981 i = 0;
982 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000983 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000984 if (!str)
985 break;
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000987 i++;
bellardaa455482004-04-04 13:07:25 +0000988 }
989}
990
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100991static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000992{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700993 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000994}
j_mayer76a66252007-03-07 08:32:30 +0000995
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100996static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530997{
Lluís Vilanova14101d02014-08-25 13:20:03 +0200998 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
999 TraceEventInfoList *elem;
1000
1001 for (elem = events; elem != NULL; elem = elem->next) {
1002 monitor_printf(mon, "%s : state %u\n",
1003 elem->value->name,
1004 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1005 }
1006 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301007}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301008
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001009void qmp_client_migrate_info(const char *protocol, const char *hostname,
1010 bool has_port, int64_t port,
1011 bool has_tls_port, int64_t tls_port,
1012 bool has_cert_subject, const char *cert_subject,
1013 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001014{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001015 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001016 if (!qemu_using_spice(errp)) {
1017 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001018 }
1019
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001020 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001021 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001022 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001023 }
1024
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001025 if (qemu_spice_migrate_info(hostname,
1026 has_port ? port : -1,
1027 has_tls_port ? tls_port : -1,
1028 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001029 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001030 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001031 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001032 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001033 }
1034
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001035 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001036}
1037
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001038static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001039{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001040 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001041}
1042
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001043static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001044{
1045 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001046 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001047
bellard9307c4c2004-04-04 12:57:25 +00001048 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001049 mask = 0;
1050 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001051 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001052 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001053 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001054 return;
1055 }
1056 }
Peter Maydell24537a02013-02-11 16:41:23 +00001057 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001058}
1059
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001060static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001061{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001062 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001063 if (!option || !strcmp(option, "on")) {
1064 singlestep = 1;
1065 } else if (!strcmp(option, "off")) {
1066 singlestep = 0;
1067 } else {
1068 monitor_printf(mon, "unexpected option %s\n", option);
1069 }
1070}
1071
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001072static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001073{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001075 if (!device)
1076 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1077 if (gdbserver_start(device) < 0) {
1078 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1079 device);
1080 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001081 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001082 } else {
aliguori59030a82009-04-05 18:43:41 +00001083 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1084 device);
bellard8a7ddc32004-03-31 19:00:16 +00001085 }
1086}
1087
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001088static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001089{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001090 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001091 if (select_watchdog_action(action) == -1) {
1092 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1093 }
1094}
1095
aliguori376253e2009-03-05 23:01:23 +00001096static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001097{
aliguori376253e2009-03-05 23:01:23 +00001098 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001099 switch(c) {
1100 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001102 break;
1103 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001105 break;
1106 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001107 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001108 break;
1109 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001111 break;
1112 default:
1113 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001115 } else {
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001117 }
1118 break;
1119 }
aliguori376253e2009-03-05 23:01:23 +00001120 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001121}
1122
aliguori376253e2009-03-05 23:01:23 +00001123static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001124 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001125{
Blue Swirl23842aa2010-01-12 20:27:43 +00001126 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001127 uint8_t buf[16];
1128 uint64_t v;
1129
1130 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001131 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001132#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001133 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001134 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001135 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001136 } else if (wsize == 4) {
1137 flags = 0;
1138 } else {
bellard6a15fd12006-04-12 21:07:07 +00001139 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001140 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001141 if (env) {
1142#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001143 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001144 (env->segs[R_CS].flags & DESC_L_MASK))
1145 flags = 2;
1146 else
1147#endif
1148 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1149 flags = 1;
1150 }
bellard4c27ba22004-04-25 18:05:08 +00001151 }
1152#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001153#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001154 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001155 flags = msr_le << 16;
1156 flags |= env->bfd_mach;
1157#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001158 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001159 return;
1160 }
1161
1162 len = wsize * count;
1163 if (wsize == 1)
1164 line_size = 8;
1165 else
1166 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001167 max_digits = 0;
1168
1169 switch(format) {
1170 case 'o':
1171 max_digits = (wsize * 8 + 2) / 3;
1172 break;
1173 default:
1174 case 'x':
1175 max_digits = (wsize * 8) / 4;
1176 break;
1177 case 'u':
1178 case 'd':
1179 max_digits = (wsize * 8 * 10 + 32) / 33;
1180 break;
1181 case 'c':
1182 wsize = 1;
1183 break;
1184 }
1185
1186 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001187 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001189 else
aliguori376253e2009-03-05 23:01:23 +00001190 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001191 l = len;
1192 if (l > line_size)
1193 l = line_size;
1194 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001195 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001196 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001197 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001198 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001199 break;
1200 }
bellard9307c4c2004-04-04 12:57:25 +00001201 }
ths5fafdf22007-09-16 21:08:06 +00001202 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001203 while (i < l) {
1204 switch(wsize) {
1205 default:
1206 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001207 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001208 break;
1209 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001210 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001211 break;
1212 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001213 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001214 break;
1215 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001216 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001217 break;
1218 }
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001220 switch(format) {
1221 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001222 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001223 break;
1224 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001226 break;
1227 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001228 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001232 break;
1233 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 }
1237 i += wsize;
1238 }
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001240 addr += l;
1241 len -= l;
1242 }
1243}
1244
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001245static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001246{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001247 int count = qdict_get_int(qdict, "count");
1248 int format = qdict_get_int(qdict, "format");
1249 int size = qdict_get_int(qdict, "size");
1250 target_long addr = qdict_get_int(qdict, "addr");
1251
aliguori376253e2009-03-05 23:01:23 +00001252 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001253}
1254
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001255static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001256{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001257 int count = qdict_get_int(qdict, "count");
1258 int format = qdict_get_int(qdict, "format");
1259 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001260 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001261
aliguori376253e2009-03-05 23:01:23 +00001262 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001263}
1264
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001265static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001266{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001267 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001268 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001269
bellard9307c4c2004-04-04 12:57:25 +00001270 switch(format) {
1271 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001272 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001273 break;
1274 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001275 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001278 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 default:
1281 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001282 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001285 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 }
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001289}
1290
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001291static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001292{
1293 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001294 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001295 uint32_t start = qdict_get_int(qdict, "start");
1296 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001297
1298 sum = 0;
1299 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001300 uint8_t val = address_space_ldub(&address_space_memory, addr,
1301 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001302 /* BSD sum algorithm ('sum' Unix command) */
1303 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001304 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001305 }
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001307}
1308
bellard13224a82006-07-14 22:03:35 +00001309static int mouse_button_state;
1310
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001311static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001312{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001313 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001314 const char *dx_str = qdict_get_str(qdict, "dx_str");
1315 const char *dy_str = qdict_get_str(qdict, "dy_str");
1316 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001317
bellard13224a82006-07-14 22:03:35 +00001318 dx = strtol(dx_str, NULL, 0);
1319 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001320 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1321 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1322
1323 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001324 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001325 if (dz != 0) {
1326 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1327 qemu_input_queue_btn(NULL, button, true);
1328 qemu_input_event_sync();
1329 qemu_input_queue_btn(NULL, button, false);
1330 }
1331 }
1332 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001333}
1334
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001335static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001336{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001337 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1338 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1339 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1340 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1341 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001342 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001343
1344 if (mouse_button_state == button_state) {
1345 return;
1346 }
1347 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1348 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001349 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001350}
1351
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001352static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001353{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001354 int size = qdict_get_int(qdict, "size");
1355 int addr = qdict_get_int(qdict, "addr");
1356 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001357 uint32_t val;
1358 int suffix;
1359
1360 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001361 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001362 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001363 addr++;
1364 }
1365 addr &= 0xffff;
1366
1367 switch(size) {
1368 default:
1369 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001370 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001371 suffix = 'b';
1372 break;
1373 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001374 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001375 suffix = 'w';
1376 break;
1377 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001378 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001379 suffix = 'l';
1380 break;
1381 }
aliguori376253e2009-03-05 23:01:23 +00001382 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1383 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001384}
bellarda3a91a32004-06-04 11:06:21 +00001385
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001386static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001387{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001388 int size = qdict_get_int(qdict, "size");
1389 int addr = qdict_get_int(qdict, "addr");
1390 int val = qdict_get_int(qdict, "val");
1391
Jan Kiszkaf1147842009-07-14 10:20:11 +02001392 addr &= IOPORTS_MASK;
1393
1394 switch (size) {
1395 default:
1396 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001397 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001398 break;
1399 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001400 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001401 break;
1402 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001403 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001404 break;
1405 }
1406}
1407
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001408static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001409{
Gongleif1839932014-12-03 18:20:58 +00001410 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001411 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001412
Gongleif1839932014-12-03 18:20:58 +00001413 qemu_boot_set(bootdevice, &local_err);
1414 if (local_err) {
1415 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1416 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001417 } else {
Gongleif1839932014-12-03 18:20:58 +00001418 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001419 }
1420}
1421
bellardb86bda52004-09-18 19:32:46 +00001422#if defined(TARGET_I386)
Avi Kivitya8170e52012-10-23 12:30:10 +02001423static void print_pte(Monitor *mon, hwaddr addr,
1424 hwaddr pte,
1425 hwaddr mask)
bellardb86bda52004-09-18 19:32:46 +00001426{
Blue Swirld65aaf32010-12-11 18:56:24 +00001427#ifdef TARGET_X86_64
1428 if (addr & (1ULL << 47)) {
1429 addr |= -1LL << 48;
1430 }
1431#endif
1432 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1433 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001434 addr,
1435 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001436 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001437 pte & PG_GLOBAL_MASK ? 'G' : '-',
1438 pte & PG_PSE_MASK ? 'P' : '-',
1439 pte & PG_DIRTY_MASK ? 'D' : '-',
1440 pte & PG_ACCESSED_MASK ? 'A' : '-',
1441 pte & PG_PCD_MASK ? 'C' : '-',
1442 pte & PG_PWT_MASK ? 'T' : '-',
1443 pte & PG_USER_MASK ? 'U' : '-',
1444 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001445}
1446
Andreas Färber9349b4f2012-03-14 01:38:32 +01001447static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001448{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001449 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001450 uint32_t pgd, pde, pte;
1451
bellardb86bda52004-09-18 19:32:46 +00001452 pgd = env->cr[3] & ~0xfff;
1453 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001454 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001455 pde = le32_to_cpu(pde);
1456 if (pde & PG_PRESENT_MASK) {
1457 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001458 /* 4M pages */
1459 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001460 } else {
1461 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001462 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001463 pte = le32_to_cpu(pte);
1464 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001465 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001466 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001467 ~0xfff);
1468 }
1469 }
1470 }
1471 }
1472 }
1473}
1474
Andreas Färber9349b4f2012-03-14 01:38:32 +01001475static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001476{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001477 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001478 uint64_t pdpe, pde, pte;
1479 uint64_t pdp_addr, pd_addr, pt_addr;
1480
1481 pdp_addr = env->cr[3] & ~0x1f;
1482 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001483 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001484 pdpe = le64_to_cpu(pdpe);
1485 if (pdpe & PG_PRESENT_MASK) {
1486 pd_addr = pdpe & 0x3fffffffff000ULL;
1487 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001488 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001489 pde = le64_to_cpu(pde);
1490 if (pde & PG_PRESENT_MASK) {
1491 if (pde & PG_PSE_MASK) {
1492 /* 2M pages with PAE, CR4.PSE is ignored */
1493 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
Avi Kivitya8170e52012-10-23 12:30:10 +02001494 ~((hwaddr)(1 << 20) - 1));
Blue Swirld65aaf32010-12-11 18:56:24 +00001495 } else {
1496 pt_addr = pde & 0x3fffffffff000ULL;
1497 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001498 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001499 pte = le64_to_cpu(pte);
1500 if (pte & PG_PRESENT_MASK) {
1501 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1502 + (l3 << 12),
1503 pte & ~PG_PSE_MASK,
Avi Kivitya8170e52012-10-23 12:30:10 +02001504 ~(hwaddr)0xfff);
Blue Swirld65aaf32010-12-11 18:56:24 +00001505 }
1506 }
1507 }
1508 }
1509 }
1510 }
1511 }
1512}
1513
1514#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001515static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001516{
1517 uint64_t l1, l2, l3, l4;
1518 uint64_t pml4e, pdpe, pde, pte;
1519 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1520
1521 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1522 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001523 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001524 pml4e = le64_to_cpu(pml4e);
1525 if (pml4e & PG_PRESENT_MASK) {
1526 pdp_addr = pml4e & 0x3fffffffff000ULL;
1527 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001528 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001529 pdpe = le64_to_cpu(pdpe);
1530 if (pdpe & PG_PRESENT_MASK) {
1531 if (pdpe & PG_PSE_MASK) {
1532 /* 1G pages, CR4.PSE is ignored */
1533 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1534 0x3ffffc0000000ULL);
1535 } else {
1536 pd_addr = pdpe & 0x3fffffffff000ULL;
1537 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001538 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001539 pde = le64_to_cpu(pde);
1540 if (pde & PG_PRESENT_MASK) {
1541 if (pde & PG_PSE_MASK) {
1542 /* 2M pages, CR4.PSE is ignored */
1543 print_pte(mon, (l1 << 39) + (l2 << 30) +
1544 (l3 << 21), pde,
1545 0x3ffffffe00000ULL);
1546 } else {
1547 pt_addr = pde & 0x3fffffffff000ULL;
1548 for (l4 = 0; l4 < 512; l4++) {
1549 cpu_physical_memory_read(pt_addr
1550 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001551 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001552 pte = le64_to_cpu(pte);
1553 if (pte & PG_PRESENT_MASK) {
1554 print_pte(mon, (l1 << 39) +
1555 (l2 << 30) +
1556 (l3 << 21) + (l4 << 12),
1557 pte & ~PG_PSE_MASK,
1558 0x3fffffffff000ULL);
1559 }
1560 }
1561 }
1562 }
1563 }
1564 }
1565 }
1566 }
1567 }
1568 }
1569}
1570#endif
1571
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001572static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld65aaf32010-12-11 18:56:24 +00001573{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001574 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001575
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001576 env = mon_get_cpu_env();
Blue Swirld65aaf32010-12-11 18:56:24 +00001577
1578 if (!(env->cr[0] & CR0_PG_MASK)) {
1579 monitor_printf(mon, "PG disabled\n");
1580 return;
1581 }
1582 if (env->cr[4] & CR4_PAE_MASK) {
1583#ifdef TARGET_X86_64
1584 if (env->hflags & HF_LMA_MASK) {
1585 tlb_info_64(mon, env);
1586 } else
1587#endif
1588 {
1589 tlb_info_pae32(mon, env);
1590 }
1591 } else {
1592 tlb_info_32(mon, env);
1593 }
1594}
1595
Avi Kivitya8170e52012-10-23 12:30:10 +02001596static void mem_print(Monitor *mon, hwaddr *pstart,
Blue Swirl1b3cba62010-12-11 18:56:27 +00001597 int *plast_prot,
Avi Kivitya8170e52012-10-23 12:30:10 +02001598 hwaddr end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001599{
bellard9746b152004-11-11 18:30:24 +00001600 int prot1;
1601 prot1 = *plast_prot;
1602 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001603 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001604 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1605 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001606 *pstart, end, end - *pstart,
1607 prot1 & PG_USER_MASK ? 'u' : '-',
1608 'r',
1609 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001610 }
1611 if (prot != 0)
1612 *pstart = end;
1613 else
1614 *pstart = -1;
1615 *plast_prot = prot;
1616 }
1617}
1618
Andreas Färber9349b4f2012-03-14 01:38:32 +01001619static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001620{
Austin Clementsb49ca722011-08-14 23:19:21 -04001621 unsigned int l1, l2;
1622 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001623 uint32_t pgd, pde, pte;
Avi Kivitya8170e52012-10-23 12:30:10 +02001624 hwaddr start, end;
bellardb86bda52004-09-18 19:32:46 +00001625
bellardb86bda52004-09-18 19:32:46 +00001626 pgd = env->cr[3] & ~0xfff;
1627 last_prot = 0;
1628 start = -1;
1629 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001630 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001631 pde = le32_to_cpu(pde);
1632 end = l1 << 22;
1633 if (pde & PG_PRESENT_MASK) {
1634 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1635 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001636 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001637 } else {
1638 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001639 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001640 pte = le32_to_cpu(pte);
1641 end = (l1 << 22) + (l2 << 12);
1642 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001643 prot = pte & pde &
1644 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001645 } else {
1646 prot = 0;
1647 }
aliguori376253e2009-03-05 23:01:23 +00001648 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001649 }
1650 }
1651 } else {
1652 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001653 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001654 }
1655 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001656 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001657 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001658}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001659
Andreas Färber9349b4f2012-03-14 01:38:32 +01001660static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001661{
Austin Clementsb49ca722011-08-14 23:19:21 -04001662 unsigned int l1, l2, l3;
1663 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001664 uint64_t pdpe, pde, pte;
1665 uint64_t pdp_addr, pd_addr, pt_addr;
Avi Kivitya8170e52012-10-23 12:30:10 +02001666 hwaddr start, end;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001667
1668 pdp_addr = env->cr[3] & ~0x1f;
1669 last_prot = 0;
1670 start = -1;
1671 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001672 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001673 pdpe = le64_to_cpu(pdpe);
1674 end = l1 << 30;
1675 if (pdpe & PG_PRESENT_MASK) {
1676 pd_addr = pdpe & 0x3fffffffff000ULL;
1677 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001678 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001679 pde = le64_to_cpu(pde);
1680 end = (l1 << 30) + (l2 << 21);
1681 if (pde & PG_PRESENT_MASK) {
1682 if (pde & PG_PSE_MASK) {
1683 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1684 PG_PRESENT_MASK);
1685 mem_print(mon, &start, &last_prot, end, prot);
1686 } else {
1687 pt_addr = pde & 0x3fffffffff000ULL;
1688 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001689 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001690 pte = le64_to_cpu(pte);
1691 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1692 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001693 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1694 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001695 } else {
1696 prot = 0;
1697 }
1698 mem_print(mon, &start, &last_prot, end, prot);
1699 }
1700 }
1701 } else {
1702 prot = 0;
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 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001711 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001712 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001713}
1714
1715
1716#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001717static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001718{
1719 int prot, last_prot;
1720 uint64_t l1, l2, l3, l4;
1721 uint64_t pml4e, pdpe, pde, pte;
1722 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1723
1724 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1725 last_prot = 0;
1726 start = -1;
1727 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001728 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001729 pml4e = le64_to_cpu(pml4e);
1730 end = l1 << 39;
1731 if (pml4e & PG_PRESENT_MASK) {
1732 pdp_addr = pml4e & 0x3fffffffff000ULL;
1733 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001734 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001735 pdpe = le64_to_cpu(pdpe);
1736 end = (l1 << 39) + (l2 << 30);
1737 if (pdpe & PG_PRESENT_MASK) {
1738 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001739 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1740 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001741 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001742 mem_print(mon, &start, &last_prot, end, prot);
1743 } else {
1744 pd_addr = pdpe & 0x3fffffffff000ULL;
1745 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001746 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001747 pde = le64_to_cpu(pde);
1748 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1749 if (pde & PG_PRESENT_MASK) {
1750 if (pde & PG_PSE_MASK) {
1751 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1752 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001753 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001754 mem_print(mon, &start, &last_prot, end, prot);
1755 } else {
1756 pt_addr = pde & 0x3fffffffff000ULL;
1757 for (l4 = 0; l4 < 512; l4++) {
1758 cpu_physical_memory_read(pt_addr
1759 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001760 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001761 pte = le64_to_cpu(pte);
1762 end = (l1 << 39) + (l2 << 30) +
1763 (l3 << 21) + (l4 << 12);
1764 if (pte & PG_PRESENT_MASK) {
1765 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1766 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001767 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001768 } else {
1769 prot = 0;
1770 }
1771 mem_print(mon, &start, &last_prot, end, prot);
1772 }
1773 }
1774 } else {
1775 prot = 0;
1776 mem_print(mon, &start, &last_prot, end, prot);
1777 }
1778 }
1779 }
1780 } else {
1781 prot = 0;
1782 mem_print(mon, &start, &last_prot, end, prot);
1783 }
1784 }
1785 } else {
1786 prot = 0;
1787 mem_print(mon, &start, &last_prot, end, prot);
1788 }
1789 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04001790 /* Flush last range */
Avi Kivitya8170e52012-10-23 12:30:10 +02001791 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001792}
1793#endif
1794
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001795static void hmp_info_mem(Monitor *mon, const QDict *qdict)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001796{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001797 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001798
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001799 env = mon_get_cpu_env();
Blue Swirl1b3cba62010-12-11 18:56:27 +00001800
1801 if (!(env->cr[0] & CR0_PG_MASK)) {
1802 monitor_printf(mon, "PG disabled\n");
1803 return;
1804 }
1805 if (env->cr[4] & CR4_PAE_MASK) {
1806#ifdef TARGET_X86_64
1807 if (env->hflags & HF_LMA_MASK) {
1808 mem_info_64(mon, env);
1809 } else
1810#endif
1811 {
1812 mem_info_pae32(mon, env);
1813 }
1814 } else {
1815 mem_info_32(mon, env);
1816 }
1817}
bellardb86bda52004-09-18 19:32:46 +00001818#endif
1819
aurel327c664e22009-03-03 06:12:22 +00001820#if defined(TARGET_SH4)
1821
aliguori376253e2009-03-05 23:01:23 +00001822static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001823{
aliguori376253e2009-03-05 23:01:23 +00001824 monitor_printf(mon, " tlb%i:\t"
1825 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1826 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1827 "dirty=%hhu writethrough=%hhu\n",
1828 idx,
1829 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1830 tlb->v, tlb->sh, tlb->c, tlb->pr,
1831 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001832}
1833
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001834static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
aurel327c664e22009-03-03 06:12:22 +00001835{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001836 CPUArchState *env = mon_get_cpu_env();
aurel327c664e22009-03-03 06:12:22 +00001837 int i;
1838
aliguori376253e2009-03-05 23:01:23 +00001839 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001840 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001841 print_tlb (mon, i, &env->itlb[i]);
1842 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001843 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001844 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001845}
1846
1847#endif
1848
Max Filippov692f7372012-01-07 20:02:40 +04001849#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001850static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
Blue Swirld41160a2010-12-19 13:42:56 +00001851{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001852 CPUArchState *env1 = mon_get_cpu_env();
Blue Swirld41160a2010-12-19 13:42:56 +00001853
1854 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1855}
1856#endif
1857
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001858static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001859{
1860 mtree_info((fprintf_function)monitor_printf, mon);
1861}
1862
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001863static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001864{
aliguorib28b6232009-04-22 20:20:29 +00001865 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001866 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001867 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001868
zhanghailiang5b009e42014-11-04 19:49:30 +08001869 node_mem = g_new0(uint64_t, nb_numa_nodes);
1870 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001871 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1872 for (i = 0; i < nb_numa_nodes; i++) {
1873 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001874 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001875 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001876 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001877 }
1878 }
1879 monitor_printf(mon, "\n");
1880 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001881 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001882 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001883 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001884}
1885
bellard5f1ce942006-02-08 22:40:15 +00001886#ifdef CONFIG_PROFILER
1887
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001888int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001889int64_t dev_time;
1890
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001891static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001892{
aliguori376253e2009-03-05 23:01:23 +00001893 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001894 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001895 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001896 tcg_time, tcg_time / (double)get_ticks_per_sec());
1897 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001898 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001899}
1900#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001901static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001902{
aliguori376253e2009-03-05 23:01:23 +00001903 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001904}
1905#endif
1906
bellardec36b692006-07-16 18:57:03 +00001907/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001908static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001909
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001910static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001911{
1912 int i;
1913 CaptureState *s;
1914
1915 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001916 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001917 s->ops.info (s->opaque);
1918 }
1919}
1920
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001921static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001922{
1923 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001924 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001925 CaptureState *s;
1926
1927 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1928 if (i == n) {
1929 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001930 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001931 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001932 return;
1933 }
1934 }
1935}
1936
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001937static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001938{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001939 const char *path = qdict_get_str(qdict, "path");
1940 int has_freq = qdict_haskey(qdict, "freq");
1941 int freq = qdict_get_try_int(qdict, "freq", -1);
1942 int has_bits = qdict_haskey(qdict, "bits");
1943 int bits = qdict_get_try_int(qdict, "bits", -1);
1944 int has_channels = qdict_haskey(qdict, "nchannels");
1945 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001946 CaptureState *s;
1947
Anthony Liguori7267c092011-08-20 22:09:37 -05001948 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001949
1950 freq = has_freq ? freq : 44100;
1951 bits = has_bits ? bits : 16;
1952 nchannels = has_channels ? nchannels : 2;
1953
1954 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001955 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001956 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001957 return;
bellardec36b692006-07-16 18:57:03 +00001958 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001959 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001960}
bellardec36b692006-07-16 18:57:03 +00001961
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001962static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001963{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001964 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001965
aliguori76655d62009-03-06 20:27:37 +00001966 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001967 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001968 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001969 return acl;
1970}
aliguori76655d62009-03-06 20:27:37 +00001971
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001972static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001973{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001974 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001975 qemu_acl *acl = find_acl(mon, aclname);
1976 qemu_acl_entry *entry;
1977 int i = 0;
1978
1979 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001980 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001981 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001982 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001983 i++;
1984 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001985 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001986 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001987 }
1988}
1989
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001990static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001991{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001992 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001993 qemu_acl *acl = find_acl(mon, aclname);
1994
1995 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001996 qemu_acl_reset(acl);
1997 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001998 }
1999}
aliguori76655d62009-03-06 20:27:37 +00002000
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002001static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002002{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002003 const char *aclname = qdict_get_str(qdict, "aclname");
2004 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002005 qemu_acl *acl = find_acl(mon, aclname);
2006
2007 if (acl) {
2008 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002009 acl->defaultDeny = 0;
2010 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002011 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002012 acl->defaultDeny = 1;
2013 monitor_printf(mon, "acl: policy set to 'deny'\n");
2014 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002015 monitor_printf(mon, "acl: unknown policy '%s', "
2016 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002017 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002018 }
2019}
aliguori76655d62009-03-06 20:27:37 +00002020
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002021static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002022{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002023 const char *aclname = qdict_get_str(qdict, "aclname");
2024 const char *match = qdict_get_str(qdict, "match");
2025 const char *policy = qdict_get_str(qdict, "policy");
2026 int has_index = qdict_haskey(qdict, "index");
2027 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002028 qemu_acl *acl = find_acl(mon, aclname);
2029 int deny, ret;
2030
2031 if (acl) {
2032 if (strcmp(policy, "allow") == 0) {
2033 deny = 0;
2034 } else if (strcmp(policy, "deny") == 0) {
2035 deny = 1;
2036 } else {
2037 monitor_printf(mon, "acl: unknown policy '%s', "
2038 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002039 return;
2040 }
aliguori28a76be2009-03-06 20:27:40 +00002041 if (has_index)
2042 ret = qemu_acl_insert(acl, deny, match, index);
2043 else
2044 ret = qemu_acl_append(acl, deny, match);
2045 if (ret < 0)
2046 monitor_printf(mon, "acl: unable to add acl entry\n");
2047 else
2048 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002049 }
2050}
aliguori76655d62009-03-06 20:27:37 +00002051
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002052static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002053{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002054 const char *aclname = qdict_get_str(qdict, "aclname");
2055 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002056 qemu_acl *acl = find_acl(mon, aclname);
2057 int ret;
aliguori76655d62009-03-06 20:27:37 +00002058
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002059 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002060 ret = qemu_acl_remove(acl, match);
2061 if (ret < 0)
2062 monitor_printf(mon, "acl: no matching acl entry\n");
2063 else
2064 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002065 }
2066}
2067
Huang Ying79c4f6b2009-06-23 10:05:14 +08002068#if defined(TARGET_I386)
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002069static void hmp_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002070{
Andreas Färber8c5cf3b2012-05-03 15:22:54 +02002071 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +01002072 CPUState *cs;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002073 int cpu_index = qdict_get_int(qdict, "cpu_index");
2074 int bank = qdict_get_int(qdict, "bank");
2075 uint64_t status = qdict_get_int(qdict, "status");
2076 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2077 uint64_t addr = qdict_get_int(qdict, "addr");
2078 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002079 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002080
Eric Blake34acbc92015-05-15 16:25:00 -06002081 if (qdict_get_try_bool(qdict, "broadcast", false)) {
Jan Kiszka747461c2011-03-02 08:56:10 +01002082 flags |= MCE_INJECT_BROADCAST;
2083 }
Andreas Färberc51a9442013-05-17 16:57:52 +02002084 cs = qemu_get_cpu(cpu_index);
2085 if (cs != NULL) {
2086 cpu = X86_CPU(cs);
2087 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2088 flags);
Jin Dongming31ce5e02010-12-10 17:21:02 +09002089 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002090}
2091#endif
2092
Corey Bryant208c9d12012-06-22 14:36:09 -04002093void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002094{
Anthony Liguoric227f092009-10-01 16:12:16 -05002095 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002096 int fd;
2097
Corey Bryant208c9d12012-06-22 14:36:09 -04002098 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002099 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002100 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002101 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002102 }
2103
2104 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002105 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002106 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2107 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002108 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002109 }
2110
Corey Bryant208c9d12012-06-22 14:36:09 -04002111 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002112 if (strcmp(monfd->name, fdname) != 0) {
2113 continue;
2114 }
2115
2116 close(monfd->fd);
2117 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002118 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002119 }
2120
Anthony Liguori7267c092011-08-20 22:09:37 -05002121 monfd = g_malloc0(sizeof(mon_fd_t));
2122 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002123 monfd->fd = fd;
2124
Corey Bryant208c9d12012-06-22 14:36:09 -04002125 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002126}
2127
Corey Bryant208c9d12012-06-22 14:36:09 -04002128void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002129{
Anthony Liguoric227f092009-10-01 16:12:16 -05002130 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002131
Corey Bryant208c9d12012-06-22 14:36:09 -04002132 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002133 if (strcmp(monfd->name, fdname) != 0) {
2134 continue;
2135 }
2136
Blue Swirl72cf2d42009-09-12 07:36:22 +00002137 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002138 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002139 g_free(monfd->name);
2140 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002141 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002142 }
2143
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002144 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002145}
2146
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002147static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002148{
Luiz Capitulino13548692011-07-29 15:36:43 -03002149 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002150 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002151
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002152 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002153
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002154 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002155 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002156 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002157}
2158
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002159int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002160{
Anthony Liguoric227f092009-10-01 16:12:16 -05002161 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002162
Blue Swirl72cf2d42009-09-12 07:36:22 +00002163 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002164 int fd;
2165
2166 if (strcmp(monfd->name, fdname) != 0) {
2167 continue;
2168 }
2169
2170 fd = monfd->fd;
2171
2172 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002173 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002174 g_free(monfd->name);
2175 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002176
2177 return fd;
2178 }
2179
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002180 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002181 return -1;
2182}
2183
Corey Bryantba1c0482012-08-14 16:43:43 -04002184static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2185{
2186 MonFdsetFd *mon_fdset_fd;
2187 MonFdsetFd *mon_fdset_fd_next;
2188
2189 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002190 if ((mon_fdset_fd->removed ||
2191 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2192 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002193 close(mon_fdset_fd->fd);
2194 g_free(mon_fdset_fd->opaque);
2195 QLIST_REMOVE(mon_fdset_fd, next);
2196 g_free(mon_fdset_fd);
2197 }
2198 }
2199
Corey Bryantadb696f2012-08-14 16:43:47 -04002200 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002201 QLIST_REMOVE(mon_fdset, next);
2202 g_free(mon_fdset);
2203 }
2204}
2205
Corey Bryantefb87c12012-08-14 16:43:48 -04002206static void monitor_fdsets_cleanup(void)
2207{
2208 MonFdset *mon_fdset;
2209 MonFdset *mon_fdset_next;
2210
2211 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2212 monitor_fdset_cleanup(mon_fdset);
2213 }
2214}
2215
Corey Bryantba1c0482012-08-14 16:43:43 -04002216AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2217 const char *opaque, Error **errp)
2218{
2219 int fd;
2220 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002221 AddfdInfo *fdinfo;
2222
2223 fd = qemu_chr_fe_get_msgfd(mon->chr);
2224 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002225 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002226 goto error;
2227 }
2228
Corey Bryante446f702012-10-18 15:19:32 -04002229 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2230 has_opaque, opaque, errp);
2231 if (fdinfo) {
2232 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002233 }
2234
Corey Bryantba1c0482012-08-14 16:43:43 -04002235error:
2236 if (fd != -1) {
2237 close(fd);
2238 }
2239 return NULL;
2240}
2241
2242void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2243{
2244 MonFdset *mon_fdset;
2245 MonFdsetFd *mon_fdset_fd;
2246 char fd_str[60];
2247
2248 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2249 if (mon_fdset->id != fdset_id) {
2250 continue;
2251 }
2252 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2253 if (has_fd) {
2254 if (mon_fdset_fd->fd != fd) {
2255 continue;
2256 }
2257 mon_fdset_fd->removed = true;
2258 break;
2259 } else {
2260 mon_fdset_fd->removed = true;
2261 }
2262 }
2263 if (has_fd && !mon_fdset_fd) {
2264 goto error;
2265 }
2266 monitor_fdset_cleanup(mon_fdset);
2267 return;
2268 }
2269
2270error:
2271 if (has_fd) {
2272 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2273 fdset_id, fd);
2274 } else {
2275 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2276 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002277 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002278}
2279
2280FdsetInfoList *qmp_query_fdsets(Error **errp)
2281{
2282 MonFdset *mon_fdset;
2283 MonFdsetFd *mon_fdset_fd;
2284 FdsetInfoList *fdset_list = NULL;
2285
2286 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2287 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2288 FdsetFdInfoList *fdsetfd_list = NULL;
2289
2290 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2291 fdset_info->value->fdset_id = mon_fdset->id;
2292
2293 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2294 FdsetFdInfoList *fdsetfd_info;
2295
2296 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2297 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2298 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2299 if (mon_fdset_fd->opaque) {
2300 fdsetfd_info->value->has_opaque = true;
2301 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2302 } else {
2303 fdsetfd_info->value->has_opaque = false;
2304 }
2305
2306 fdsetfd_info->next = fdsetfd_list;
2307 fdsetfd_list = fdsetfd_info;
2308 }
2309
2310 fdset_info->value->fds = fdsetfd_list;
2311
2312 fdset_info->next = fdset_list;
2313 fdset_list = fdset_info;
2314 }
2315
2316 return fdset_list;
2317}
2318
Corey Bryante446f702012-10-18 15:19:32 -04002319AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2320 bool has_opaque, const char *opaque,
2321 Error **errp)
2322{
2323 MonFdset *mon_fdset = NULL;
2324 MonFdsetFd *mon_fdset_fd;
2325 AddfdInfo *fdinfo;
2326
2327 if (has_fdset_id) {
2328 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2329 /* Break if match found or match impossible due to ordering by ID */
2330 if (fdset_id <= mon_fdset->id) {
2331 if (fdset_id < mon_fdset->id) {
2332 mon_fdset = NULL;
2333 }
2334 break;
2335 }
2336 }
2337 }
2338
2339 if (mon_fdset == NULL) {
2340 int64_t fdset_id_prev = -1;
2341 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2342
2343 if (has_fdset_id) {
2344 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002345 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2346 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002347 return NULL;
2348 }
2349 /* Use specified fdset ID */
2350 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2351 mon_fdset_cur = mon_fdset;
2352 if (fdset_id < mon_fdset_cur->id) {
2353 break;
2354 }
2355 }
2356 } else {
2357 /* Use first available fdset ID */
2358 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2359 mon_fdset_cur = mon_fdset;
2360 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2361 fdset_id_prev = mon_fdset_cur->id;
2362 continue;
2363 }
2364 break;
2365 }
2366 }
2367
2368 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2369 if (has_fdset_id) {
2370 mon_fdset->id = fdset_id;
2371 } else {
2372 mon_fdset->id = fdset_id_prev + 1;
2373 }
2374
2375 /* The fdset list is ordered by fdset ID */
2376 if (!mon_fdset_cur) {
2377 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2378 } else if (mon_fdset->id < mon_fdset_cur->id) {
2379 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2380 } else {
2381 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2382 }
2383 }
2384
2385 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2386 mon_fdset_fd->fd = fd;
2387 mon_fdset_fd->removed = false;
2388 if (has_opaque) {
2389 mon_fdset_fd->opaque = g_strdup(opaque);
2390 }
2391 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2392
2393 fdinfo = g_malloc0(sizeof(*fdinfo));
2394 fdinfo->fdset_id = mon_fdset->id;
2395 fdinfo->fd = mon_fdset_fd->fd;
2396
2397 return fdinfo;
2398}
2399
Corey Bryantadb696f2012-08-14 16:43:47 -04002400int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2401{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002402#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002403 MonFdset *mon_fdset;
2404 MonFdsetFd *mon_fdset_fd;
2405 int mon_fd_flags;
2406
Corey Bryantadb696f2012-08-14 16:43:47 -04002407 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2408 if (mon_fdset->id != fdset_id) {
2409 continue;
2410 }
2411 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2412 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2413 if (mon_fd_flags == -1) {
2414 return -1;
2415 }
2416
2417 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2418 return mon_fdset_fd->fd;
2419 }
2420 }
2421 errno = EACCES;
2422 return -1;
2423 }
2424#endif
2425
2426 errno = ENOENT;
2427 return -1;
2428}
2429
2430int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2431{
2432 MonFdset *mon_fdset;
2433 MonFdsetFd *mon_fdset_fd_dup;
2434
2435 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2436 if (mon_fdset->id != fdset_id) {
2437 continue;
2438 }
2439 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2440 if (mon_fdset_fd_dup->fd == dup_fd) {
2441 return -1;
2442 }
2443 }
2444 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2445 mon_fdset_fd_dup->fd = dup_fd;
2446 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2447 return 0;
2448 }
2449 return -1;
2450}
2451
2452static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2453{
2454 MonFdset *mon_fdset;
2455 MonFdsetFd *mon_fdset_fd_dup;
2456
2457 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2458 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2459 if (mon_fdset_fd_dup->fd == dup_fd) {
2460 if (remove) {
2461 QLIST_REMOVE(mon_fdset_fd_dup, next);
2462 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2463 monitor_fdset_cleanup(mon_fdset);
2464 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002465 return -1;
2466 } else {
2467 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002468 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002469 }
2470 }
2471 }
2472 return -1;
2473}
2474
2475int monitor_fdset_dup_fd_find(int dup_fd)
2476{
2477 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2478}
2479
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002480void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002481{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002482 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002483}
2484
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002485int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002486{
2487 int fd;
2488 Error *local_err = NULL;
2489
2490 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002491 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002492 } else {
2493 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002494 if (fd == -1) {
2495 error_setg(&local_err, "Invalid file descriptor number '%s'",
2496 fdname);
2497 }
2498 }
2499 if (local_err) {
2500 error_propagate(errp, local_err);
2501 assert(fd == -1);
2502 } else {
2503 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002504 }
2505
2506 return fd;
2507}
2508
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002509/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002510static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 {
2512 .name = "version",
2513 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002514 .params = "",
2515 .help = "show the version of QEMU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002516 .mhandler.cmd = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 },
2518 {
2519 .name = "network",
2520 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 .params = "",
2522 .help = "show the network state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002523 .mhandler.cmd = hmp_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 },
2525 {
2526 .name = "chardev",
2527 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 .params = "",
2529 .help = "show the character devices",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002530 .mhandler.cmd = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 },
2532 {
2533 .name = "block",
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002534 .args_type = "nodes:-n,verbose:-v,device:B?",
2535 .params = "[-n] [-v] [device]",
Wenchao Xiae73fe2b2013-06-06 12:28:01 +08002536 .help = "show info of one block device or all block devices "
Kevin Wolfe6bb31e2014-09-15 12:19:14 +02002537 "(-n: show named nodes; -v: show details)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002538 .mhandler.cmd = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002539 },
2540 {
2541 .name = "blockstats",
2542 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002543 .params = "",
2544 .help = "show block device statistics",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002545 .mhandler.cmd = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002546 },
2547 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002548 .name = "block-jobs",
2549 .args_type = "",
2550 .params = "",
2551 .help = "show progress of ongoing block device operations",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002552 .mhandler.cmd = hmp_info_block_jobs,
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002553 },
2554 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 .name = "registers",
2556 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002557 .params = "",
2558 .help = "show the cpu registers",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002559 .mhandler.cmd = hmp_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 },
2561 {
2562 .name = "cpus",
2563 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 .params = "",
2565 .help = "show infos for each CPU",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002566 .mhandler.cmd = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 },
2568 {
2569 .name = "history",
2570 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 .params = "",
2572 .help = "show the command line history",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002573 .mhandler.cmd = hmp_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002575#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2576 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 {
2578 .name = "irq",
2579 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 .params = "",
2581 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002582#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002583 .mhandler.cmd = sun4m_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002584#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002585 .mhandler.cmd = lm32_hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002586#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002587 .mhandler.cmd = hmp_info_irq,
Jan Kiszka661f1922011-10-16 11:53:13 +02002588#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 },
2590 {
2591 .name = "pic",
2592 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 .params = "",
2594 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002595#ifdef TARGET_SPARC
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002596 .mhandler.cmd = sun4m_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002597#elif defined(TARGET_LM32)
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002598 .mhandler.cmd = lm32_hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002599#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002600 .mhandler.cmd = hmp_info_pic,
Jan Kiszka661f1922011-10-16 11:53:13 +02002601#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002603#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 {
2605 .name = "pci",
2606 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 .params = "",
2608 .help = "show PCI info",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002609 .mhandler.cmd = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002611#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002612 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 {
2614 .name = "tlb",
2615 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 .params = "",
2617 .help = "show virtual to physical memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002618 .mhandler.cmd = hmp_info_tlb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002619 },
aurel327c664e22009-03-03 06:12:22 +00002620#endif
2621#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 {
2623 .name = "mem",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show the active virtual memory mappings",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002627 .mhandler.cmd = hmp_info_mem,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 },
bellardb86bda52004-09-18 19:32:46 +00002629#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 {
Blue Swirl314e2982011-09-11 20:22:05 +00002631 .name = "mtree",
2632 .args_type = "",
2633 .params = "",
2634 .help = "show memory tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002635 .mhandler.cmd = hmp_info_mtree,
Blue Swirl314e2982011-09-11 20:22:05 +00002636 },
2637 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 .name = "jit",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show dynamic compiler info",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002642 .mhandler.cmd = hmp_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
2644 {
Max Filippov246ae242014-11-02 11:04:18 +03002645 .name = "opcount",
2646 .args_type = "",
2647 .params = "",
2648 .help = "show dynamic compiler opcode counters",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002649 .mhandler.cmd = hmp_info_opcount,
Max Filippov246ae242014-11-02 11:04:18 +03002650 },
2651 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 .name = "kvm",
2653 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 .params = "",
2655 .help = "show KVM information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002656 .mhandler.cmd = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002657 },
2658 {
2659 .name = "numa",
2660 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 .params = "",
2662 .help = "show NUMA information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002663 .mhandler.cmd = hmp_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 },
2665 {
2666 .name = "usb",
2667 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002668 .params = "",
2669 .help = "show guest USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002670 .mhandler.cmd = hmp_info_usb,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 },
2672 {
2673 .name = "usbhost",
2674 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 .params = "",
2676 .help = "show host USB devices",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002677 .mhandler.cmd = hmp_info_usbhost,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 },
2679 {
2680 .name = "profile",
2681 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002682 .params = "",
2683 .help = "show profiling information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002684 .mhandler.cmd = hmp_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002685 },
2686 {
2687 .name = "capture",
2688 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 .params = "",
2690 .help = "show capture information",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002691 .mhandler.cmd = hmp_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 },
2693 {
2694 .name = "snapshots",
2695 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002696 .params = "",
2697 .help = "show the currently saved VM snapshots",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002698 .mhandler.cmd = hmp_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
2700 {
2701 .name = "status",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show the current VM status (running|paused)",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002705 .mhandler.cmd = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
2707 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002708 .name = "mice",
2709 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 .params = "",
2711 .help = "show which guest mouse is receiving events",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002712 .mhandler.cmd = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 },
2714 {
2715 .name = "vnc",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show the vnc server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002719 .mhandler.cmd = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002721#if defined(CONFIG_SPICE)
2722 {
2723 .name = "spice",
2724 .args_type = "",
2725 .params = "",
2726 .help = "show the spice server status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002727 .mhandler.cmd = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002728 },
2729#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002730 {
2731 .name = "name",
2732 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002733 .params = "",
2734 .help = "show the current VM name",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002735 .mhandler.cmd = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002736 },
2737 {
2738 .name = "uuid",
2739 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 .params = "",
2741 .help = "show the current VM UUID",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002742 .mhandler.cmd = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002743 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002744 {
2745 .name = "cpustats",
2746 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002747 .params = "",
2748 .help = "show CPU statistics",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002749 .mhandler.cmd = hmp_info_cpustats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 },
blueswir131a60e22007-10-26 18:42:59 +00002751#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 {
2753 .name = "usernet",
2754 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 .params = "",
2756 .help = "show user network stack connection states",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002757 .mhandler.cmd = hmp_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002758 },
blueswir131a60e22007-10-26 18:42:59 +00002759#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 {
2761 .name = "migrate",
2762 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 .params = "",
2764 .help = "show migration status",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002765 .mhandler.cmd = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 },
2767 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002768 .name = "migrate_capabilities",
2769 .args_type = "",
2770 .params = "",
2771 .help = "show current migration capabilities",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002772 .mhandler.cmd = hmp_info_migrate_capabilities,
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002773 },
2774 {
Liang Li50e9a622015-03-23 16:32:29 +08002775 .name = "migrate_parameters",
2776 .args_type = "",
2777 .params = "",
2778 .help = "show current migration parameters",
2779 .mhandler.cmd = hmp_info_migrate_parameters,
2780 },
2781 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002782 .name = "migrate_cache_size",
2783 .args_type = "",
2784 .params = "",
2785 .help = "show current migration xbzrle cache size",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002786 .mhandler.cmd = hmp_info_migrate_cache_size,
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002787 },
2788 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002789 .name = "balloon",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show balloon information",
Wenchao Xia5f11cb02013-01-14 14:06:26 +08002793 .mhandler.cmd = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 },
2795 {
2796 .name = "qtree",
2797 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002798 .params = "",
2799 .help = "show device tree",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002800 .mhandler.cmd = hmp_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002801 },
2802 {
2803 .name = "qdm",
2804 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 .params = "",
2806 .help = "show qdev device model list",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002807 .mhandler.cmd = hmp_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 },
2809 {
Andreas Färbera01ff752014-05-07 17:03:18 +02002810 .name = "qom-tree",
2811 .args_type = "path:s?",
2812 .params = "[path]",
2813 .help = "show QOM composition tree",
2814 .mhandler.cmd = hmp_info_qom_tree,
2815 },
2816 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002817 .name = "roms",
2818 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002819 .params = "",
2820 .help = "show roms",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002821 .mhandler.cmd = hmp_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302823 {
2824 .name = "trace-events",
2825 .args_type = "",
2826 .params = "",
2827 .help = "show available trace-events & their state",
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002828 .mhandler.cmd = hmp_info_trace_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302829 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05002831 .name = "tpm",
2832 .args_type = "",
2833 .params = "",
2834 .help = "show the TPM device",
2835 .mhandler.cmd = hmp_info_tpm,
2836 },
2837 {
Hu Taoeb1539b2014-05-14 17:43:35 +08002838 .name = "memdev",
2839 .args_type = "",
2840 .params = "",
Igor Mammedov8f4e5ac2014-06-19 16:14:43 +02002841 .help = "show memory backends",
Hu Taoeb1539b2014-05-14 17:43:35 +08002842 .mhandler.cmd = hmp_info_memdev,
2843 },
2844 {
Zhu Guihuaa6318922014-09-23 13:35:19 +08002845 .name = "memory-devices",
2846 .args_type = "",
2847 .params = "",
2848 .help = "show memory devices",
2849 .mhandler.cmd = hmp_info_memory_devices,
2850 },
2851 {
Scott Feldmanfafa4d52015-06-10 18:21:21 -07002852 .name = "rocker",
2853 .args_type = "name:s",
2854 .params = "name",
2855 .help = "Show rocker switch",
2856 .mhandler.cmd = hmp_rocker,
2857 },
2858 {
2859 .name = "rocker-ports",
2860 .args_type = "name:s",
2861 .params = "name",
2862 .help = "Show rocker ports",
2863 .mhandler.cmd = hmp_rocker_ports,
2864 },
2865 {
2866 .name = "rocker-of-dpa-flows",
2867 .args_type = "name:s,tbl_id:i?",
2868 .params = "name [tbl_id]",
2869 .help = "Show rocker OF-DPA flow tables",
2870 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2871 },
2872 {
2873 .name = "rocker-of-dpa-groups",
2874 .args_type = "name:s,type:i?",
2875 .params = "name [type]",
2876 .help = "Show rocker OF-DPA groups",
2877 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2878 },
2879 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002880 .name = NULL,
2881 },
bellard9dc39cb2004-03-14 21:38:27 +00002882};
2883
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002884/* mon_cmds and info_cmds would be sorted at runtime */
2885static mon_cmd_t mon_cmds[] = {
2886#include "hmp-commands.h"
2887 { NULL, NULL, },
2888};
2889
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002890static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002891#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002892 { /* NULL */ },
2893};
2894
bellard9307c4c2004-04-04 12:57:25 +00002895/*******************************************************************/
2896
2897static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002898static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002899
bellard92a31b12005-02-10 22:00:52 +00002900#define MD_TLONG 0
2901#define MD_I32 1
2902
bellard9307c4c2004-04-04 12:57:25 +00002903typedef struct MonitorDef {
2904 const char *name;
2905 int offset;
blueswir18662d652008-10-02 18:32:44 +00002906 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002907 int type;
bellard9307c4c2004-04-04 12:57:25 +00002908} MonitorDef;
2909
bellard57206fd2004-04-25 18:54:52 +00002910#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002911static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002912{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002913 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002914 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002915}
2916#endif
2917
bellarda541f292004-04-12 20:39:29 +00002918#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002919static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002920{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002921 CPUArchState *env = mon_get_cpu_env();
bellarda541f292004-04-12 20:39:29 +00002922 unsigned int u;
2923 int i;
2924
2925 u = 0;
2926 for (i = 0; i < 8; i++)
Paolo Bonzinid2981182014-08-28 19:14:59 +02002927 u |= env->crf[i] << (32 - (4 * (i + 1)));
bellarda541f292004-04-12 20:39:29 +00002928
2929 return u;
2930}
2931
blueswir18662d652008-10-02 18:32:44 +00002932static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002933{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002934 CPUArchState *env = mon_get_cpu_env();
j_mayer0411a972007-10-25 21:35:50 +00002935 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002936}
2937
blueswir18662d652008-10-02 18:32:44 +00002938static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002939{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002940 CPUArchState *env = mon_get_cpu_env();
aurel323d7b4172008-10-21 11:28:46 +00002941 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002942}
bellard9fddaa02004-05-21 12:59:32 +00002943
blueswir18662d652008-10-02 18:32:44 +00002944static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002945{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002946 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002947 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002948}
2949
blueswir18662d652008-10-02 18:32:44 +00002950static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002951{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002952 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002953 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002954}
2955
blueswir18662d652008-10-02 18:32:44 +00002956static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002957{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002958 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002959 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002960}
bellarda541f292004-04-12 20:39:29 +00002961#endif
2962
bellarde95c8d52004-09-30 22:22:08 +00002963#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002964#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002965static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002966{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002967 CPUArchState *env = mon_get_cpu_env();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002968
2969 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002970}
bellard7b936c02005-10-30 17:05:13 +00002971#endif
bellarde95c8d52004-09-30 22:22:08 +00002972
blueswir18662d652008-10-02 18:32:44 +00002973static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002974{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002975 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002976 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002977}
2978#endif
2979
blueswir18662d652008-10-02 18:32:44 +00002980static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002981#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002982
2983#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01002984 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2985 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2986 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002987
Andreas Färbere59d1672012-02-16 00:40:47 +01002988 { "eax", offsetof(CPUX86State, regs[0]) },
2989 { "ecx", offsetof(CPUX86State, regs[1]) },
2990 { "edx", offsetof(CPUX86State, regs[2]) },
2991 { "ebx", offsetof(CPUX86State, regs[3]) },
2992 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2993 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2994 { "esi", offsetof(CPUX86State, regs[6]) },
2995 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002996#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01002997 { "r8", offsetof(CPUX86State, regs[8]) },
2998 { "r9", offsetof(CPUX86State, regs[9]) },
2999 { "r10", offsetof(CPUX86State, regs[10]) },
3000 { "r11", offsetof(CPUX86State, regs[11]) },
3001 { "r12", offsetof(CPUX86State, regs[12]) },
3002 { "r13", offsetof(CPUX86State, regs[13]) },
3003 { "r14", offsetof(CPUX86State, regs[14]) },
3004 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003005#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003006 { "eflags", offsetof(CPUX86State, eflags) },
3007 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003008 SEG("cs", R_CS)
3009 SEG("ds", R_DS)
3010 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003011 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003012 SEG("fs", R_FS)
3013 SEG("gs", R_GS)
3014 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003015#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003016 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003017 { "r0", offsetof(CPUPPCState, gpr[0]) },
3018 { "r1", offsetof(CPUPPCState, gpr[1]) },
3019 { "r2", offsetof(CPUPPCState, gpr[2]) },
3020 { "r3", offsetof(CPUPPCState, gpr[3]) },
3021 { "r4", offsetof(CPUPPCState, gpr[4]) },
3022 { "r5", offsetof(CPUPPCState, gpr[5]) },
3023 { "r6", offsetof(CPUPPCState, gpr[6]) },
3024 { "r7", offsetof(CPUPPCState, gpr[7]) },
3025 { "r8", offsetof(CPUPPCState, gpr[8]) },
3026 { "r9", offsetof(CPUPPCState, gpr[9]) },
3027 { "r10", offsetof(CPUPPCState, gpr[10]) },
3028 { "r11", offsetof(CPUPPCState, gpr[11]) },
3029 { "r12", offsetof(CPUPPCState, gpr[12]) },
3030 { "r13", offsetof(CPUPPCState, gpr[13]) },
3031 { "r14", offsetof(CPUPPCState, gpr[14]) },
3032 { "r15", offsetof(CPUPPCState, gpr[15]) },
3033 { "r16", offsetof(CPUPPCState, gpr[16]) },
3034 { "r17", offsetof(CPUPPCState, gpr[17]) },
3035 { "r18", offsetof(CPUPPCState, gpr[18]) },
3036 { "r19", offsetof(CPUPPCState, gpr[19]) },
3037 { "r20", offsetof(CPUPPCState, gpr[20]) },
3038 { "r21", offsetof(CPUPPCState, gpr[21]) },
3039 { "r22", offsetof(CPUPPCState, gpr[22]) },
3040 { "r23", offsetof(CPUPPCState, gpr[23]) },
3041 { "r24", offsetof(CPUPPCState, gpr[24]) },
3042 { "r25", offsetof(CPUPPCState, gpr[25]) },
3043 { "r26", offsetof(CPUPPCState, gpr[26]) },
3044 { "r27", offsetof(CPUPPCState, gpr[27]) },
3045 { "r28", offsetof(CPUPPCState, gpr[28]) },
3046 { "r29", offsetof(CPUPPCState, gpr[29]) },
3047 { "r30", offsetof(CPUPPCState, gpr[30]) },
3048 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003049 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003050 { "f0", offsetof(CPUPPCState, fpr[0]) },
3051 { "f1", offsetof(CPUPPCState, fpr[1]) },
3052 { "f2", offsetof(CPUPPCState, fpr[2]) },
3053 { "f3", offsetof(CPUPPCState, fpr[3]) },
3054 { "f4", offsetof(CPUPPCState, fpr[4]) },
3055 { "f5", offsetof(CPUPPCState, fpr[5]) },
3056 { "f6", offsetof(CPUPPCState, fpr[6]) },
3057 { "f7", offsetof(CPUPPCState, fpr[7]) },
3058 { "f8", offsetof(CPUPPCState, fpr[8]) },
3059 { "f9", offsetof(CPUPPCState, fpr[9]) },
3060 { "f10", offsetof(CPUPPCState, fpr[10]) },
3061 { "f11", offsetof(CPUPPCState, fpr[11]) },
3062 { "f12", offsetof(CPUPPCState, fpr[12]) },
3063 { "f13", offsetof(CPUPPCState, fpr[13]) },
3064 { "f14", offsetof(CPUPPCState, fpr[14]) },
3065 { "f15", offsetof(CPUPPCState, fpr[15]) },
3066 { "f16", offsetof(CPUPPCState, fpr[16]) },
3067 { "f17", offsetof(CPUPPCState, fpr[17]) },
3068 { "f18", offsetof(CPUPPCState, fpr[18]) },
3069 { "f19", offsetof(CPUPPCState, fpr[19]) },
3070 { "f20", offsetof(CPUPPCState, fpr[20]) },
3071 { "f21", offsetof(CPUPPCState, fpr[21]) },
3072 { "f22", offsetof(CPUPPCState, fpr[22]) },
3073 { "f23", offsetof(CPUPPCState, fpr[23]) },
3074 { "f24", offsetof(CPUPPCState, fpr[24]) },
3075 { "f25", offsetof(CPUPPCState, fpr[25]) },
3076 { "f26", offsetof(CPUPPCState, fpr[26]) },
3077 { "f27", offsetof(CPUPPCState, fpr[27]) },
3078 { "f28", offsetof(CPUPPCState, fpr[28]) },
3079 { "f29", offsetof(CPUPPCState, fpr[29]) },
3080 { "f30", offsetof(CPUPPCState, fpr[30]) },
3081 { "f31", offsetof(CPUPPCState, fpr[31]) },
3082 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003083 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003084 { "nip|pc", offsetof(CPUPPCState, nip) },
3085 { "lr", offsetof(CPUPPCState, lr) },
3086 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003087 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003088 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003089 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003090 { "msr", 0, &monitor_get_msr, },
3091 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003092 { "tbu", 0, &monitor_get_tbu, },
3093 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003094 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003095 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3096 { "sr0", offsetof(CPUPPCState, sr[0]) },
3097 { "sr1", offsetof(CPUPPCState, sr[1]) },
3098 { "sr2", offsetof(CPUPPCState, sr[2]) },
3099 { "sr3", offsetof(CPUPPCState, sr[3]) },
3100 { "sr4", offsetof(CPUPPCState, sr[4]) },
3101 { "sr5", offsetof(CPUPPCState, sr[5]) },
3102 { "sr6", offsetof(CPUPPCState, sr[6]) },
3103 { "sr7", offsetof(CPUPPCState, sr[7]) },
3104 { "sr8", offsetof(CPUPPCState, sr[8]) },
3105 { "sr9", offsetof(CPUPPCState, sr[9]) },
3106 { "sr10", offsetof(CPUPPCState, sr[10]) },
3107 { "sr11", offsetof(CPUPPCState, sr[11]) },
3108 { "sr12", offsetof(CPUPPCState, sr[12]) },
3109 { "sr13", offsetof(CPUPPCState, sr[13]) },
3110 { "sr14", offsetof(CPUPPCState, sr[14]) },
3111 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003112 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003113 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003114
Andreas Färbere59d1672012-02-16 00:40:47 +01003115 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3116 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
Tom Musta04f1f782013-09-25 17:41:13 +10003117 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3118 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3119 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
Andreas Färbere59d1672012-02-16 00:40:47 +01003120 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3121 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3122 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3123 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3124 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3125 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3126 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3127 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3128 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3129 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3130 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3131 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3132 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3133 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3134 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3135 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3136 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3137 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3138 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3139 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3140 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3141 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3142 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3143 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3144 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3145 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3146 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3147 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3148 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3149 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3150 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3151 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3152 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3153 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3154 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3155 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3156 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3157 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3158 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3159 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3160 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3161 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3162 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3163 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3164 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3165 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3166 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3167 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3168 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3169 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3170 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3171 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3172 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3173 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3174 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3175 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3176 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3177 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3178 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3179 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3180 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3181 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3182 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3183 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003184
bellarde95c8d52004-09-30 22:22:08 +00003185#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003186 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3187 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3188 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3189 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3190 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3191 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3192 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3193 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003194 { "o0", 0, monitor_get_reg },
3195 { "o1", 1, monitor_get_reg },
3196 { "o2", 2, monitor_get_reg },
3197 { "o3", 3, monitor_get_reg },
3198 { "o4", 4, monitor_get_reg },
3199 { "o5", 5, monitor_get_reg },
3200 { "o6", 6, monitor_get_reg },
3201 { "o7", 7, monitor_get_reg },
3202 { "l0", 8, monitor_get_reg },
3203 { "l1", 9, monitor_get_reg },
3204 { "l2", 10, monitor_get_reg },
3205 { "l3", 11, monitor_get_reg },
3206 { "l4", 12, monitor_get_reg },
3207 { "l5", 13, monitor_get_reg },
3208 { "l6", 14, monitor_get_reg },
3209 { "l7", 15, monitor_get_reg },
3210 { "i0", 16, monitor_get_reg },
3211 { "i1", 17, monitor_get_reg },
3212 { "i2", 18, monitor_get_reg },
3213 { "i3", 19, monitor_get_reg },
3214 { "i4", 20, monitor_get_reg },
3215 { "i5", 21, monitor_get_reg },
3216 { "i6", 22, monitor_get_reg },
3217 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003218 { "pc", offsetof(CPUSPARCState, pc) },
3219 { "npc", offsetof(CPUSPARCState, npc) },
3220 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003221#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003222 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003223 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003224#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003225 { "tbr", offsetof(CPUSPARCState, tbr) },
3226 { "fsr", offsetof(CPUSPARCState, fsr) },
3227 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3228 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3229 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3230 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3231 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3232 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3233 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3234 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3235 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3236 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3237 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3238 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3239 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3240 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3241 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3242 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3243 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3244 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3245 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3246 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3247 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3248 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3249 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3250 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3251 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3252 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3253 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3254 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3255 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3256 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3257 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3258 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003259#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003260 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3261 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3262 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3263 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3264 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3265 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3266 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3267 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3268 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3269 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3270 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3271 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3272 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3273 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3274 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3275 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3276 { "asi", offsetof(CPUSPARCState, asi) },
3277 { "pstate", offsetof(CPUSPARCState, pstate) },
3278 { "cansave", offsetof(CPUSPARCState, cansave) },
3279 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3280 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3281 { "wstate", offsetof(CPUSPARCState, wstate) },
3282 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3283 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003284#endif
bellard9307c4c2004-04-04 12:57:25 +00003285#endif
3286 { NULL },
3287};
3288
Stefan Weil9c3175c2013-08-22 21:30:09 +02003289static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3290expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00003291{
Fam Zheng277acfe2013-08-20 10:58:21 +08003292 va_list ap;
3293 va_start(ap, fmt);
3294 monitor_vprintf(mon, fmt, ap);
3295 monitor_printf(mon, "\n");
3296 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00003297 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00003298}
3299
Markus Armbruster09b94182010-01-20 13:07:30 +01003300/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003301static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003302{
blueswir18662d652008-10-02 18:32:44 +00003303 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003304 void *ptr;
3305
bellard9307c4c2004-04-04 12:57:25 +00003306 for(md = monitor_defs; md->name != NULL; md++) {
3307 if (compare_cmd(name, md->name)) {
3308 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003309 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003310 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07003311 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00003312 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003313 switch(md->type) {
3314 case MD_I32:
3315 *pval = *(int32_t *)ptr;
3316 break;
3317 case MD_TLONG:
3318 *pval = *(target_long *)ptr;
3319 break;
3320 default:
3321 *pval = 0;
3322 break;
3323 }
bellard9307c4c2004-04-04 12:57:25 +00003324 }
3325 return 0;
3326 }
3327 }
3328 return -1;
3329}
3330
3331static void next(void)
3332{
Blue Swirl660f11b2009-07-31 21:16:51 +00003333 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003334 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003335 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003336 pch++;
3337 }
3338}
3339
aliguori376253e2009-03-05 23:01:23 +00003340static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003341
aliguori376253e2009-03-05 23:01:23 +00003342static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003343{
blueswir1c2efc952007-09-25 17:28:42 +00003344 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003345 char *p;
bellard6a00d602005-11-21 23:25:50 +00003346 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003347
3348 switch(*pch) {
3349 case '+':
3350 next();
aliguori376253e2009-03-05 23:01:23 +00003351 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003352 break;
3353 case '-':
3354 next();
aliguori376253e2009-03-05 23:01:23 +00003355 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003356 break;
3357 case '~':
3358 next();
aliguori376253e2009-03-05 23:01:23 +00003359 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003360 break;
3361 case '(':
3362 next();
aliguori376253e2009-03-05 23:01:23 +00003363 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003364 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003365 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003366 }
3367 next();
3368 break;
bellard81d09122004-07-14 17:21:37 +00003369 case '\'':
3370 pch++;
3371 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003372 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003373 n = *pch;
3374 pch++;
3375 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003376 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003377 next();
3378 break;
bellard9307c4c2004-04-04 12:57:25 +00003379 case '$':
3380 {
3381 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003382 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003383
bellard9307c4c2004-04-04 12:57:25 +00003384 pch++;
3385 q = buf;
3386 while ((*pch >= 'a' && *pch <= 'z') ||
3387 (*pch >= 'A' && *pch <= 'Z') ||
3388 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003389 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003390 if ((q - buf) < sizeof(buf) - 1)
3391 *q++ = *pch;
3392 pch++;
3393 }
blueswir1cd390082008-11-16 13:53:32 +00003394 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003395 pch++;
3396 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003397 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003398 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003399 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003400 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003401 }
3402 break;
3403 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003404 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003405 n = 0;
3406 break;
3407 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003408 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00003409 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003410 if (errno == ERANGE) {
3411 expr_error(mon, "number too large");
3412 }
bellard9307c4c2004-04-04 12:57:25 +00003413 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08003414 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00003415 }
3416 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003417 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003418 pch++;
3419 break;
3420 }
3421 return n;
3422}
3423
3424
aliguori376253e2009-03-05 23:01:23 +00003425static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003426{
blueswir1c2efc952007-09-25 17:28:42 +00003427 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003428 int op;
ths3b46e622007-09-17 08:09:54 +00003429
aliguori376253e2009-03-05 23:01:23 +00003430 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003431 for(;;) {
3432 op = *pch;
3433 if (op != '*' && op != '/' && op != '%')
3434 break;
3435 next();
aliguori376253e2009-03-05 23:01:23 +00003436 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003437 switch(op) {
3438 default:
3439 case '*':
3440 val *= val2;
3441 break;
3442 case '/':
3443 case '%':
ths5fafdf22007-09-16 21:08:06 +00003444 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003445 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003446 if (op == '/')
3447 val /= val2;
3448 else
3449 val %= val2;
3450 break;
3451 }
3452 }
3453 return val;
3454}
3455
aliguori376253e2009-03-05 23:01:23 +00003456static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003457{
blueswir1c2efc952007-09-25 17:28:42 +00003458 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003459 int op;
bellard9307c4c2004-04-04 12:57:25 +00003460
aliguori376253e2009-03-05 23:01:23 +00003461 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003462 for(;;) {
3463 op = *pch;
3464 if (op != '&' && op != '|' && op != '^')
3465 break;
3466 next();
aliguori376253e2009-03-05 23:01:23 +00003467 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003468 switch(op) {
3469 default:
3470 case '&':
3471 val &= val2;
3472 break;
3473 case '|':
3474 val |= val2;
3475 break;
3476 case '^':
3477 val ^= val2;
3478 break;
3479 }
3480 }
3481 return val;
3482}
3483
aliguori376253e2009-03-05 23:01:23 +00003484static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003485{
blueswir1c2efc952007-09-25 17:28:42 +00003486 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003487 int op;
bellard9307c4c2004-04-04 12:57:25 +00003488
aliguori376253e2009-03-05 23:01:23 +00003489 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003490 for(;;) {
3491 op = *pch;
3492 if (op != '+' && op != '-')
3493 break;
3494 next();
aliguori376253e2009-03-05 23:01:23 +00003495 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003496 if (op == '+')
3497 val += val2;
3498 else
3499 val -= val2;
3500 }
3501 return val;
3502}
3503
aliguori376253e2009-03-05 23:01:23 +00003504static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003505{
3506 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00003507 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00003508 *pp = pch;
3509 return -1;
3510 }
blueswir1cd390082008-11-16 13:53:32 +00003511 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003512 pch++;
aliguori376253e2009-03-05 23:01:23 +00003513 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003514 *pp = pch;
3515 return 0;
3516}
3517
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003518static int get_double(Monitor *mon, double *pval, const char **pp)
3519{
3520 const char *p = *pp;
3521 char *tailp;
3522 double d;
3523
3524 d = strtod(p, &tailp);
3525 if (tailp == p) {
3526 monitor_printf(mon, "Number expected\n");
3527 return -1;
3528 }
3529 if (d != d || d - d != 0) {
3530 /* NaN or infinity */
3531 monitor_printf(mon, "Bad number\n");
3532 return -1;
3533 }
3534 *pval = d;
3535 *pp = tailp;
3536 return 0;
3537}
3538
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003539/*
3540 * Store the command-name in cmdname, and return a pointer to
3541 * the remaining of the command string.
3542 */
3543static const char *get_command_name(const char *cmdline,
3544 char *cmdname, size_t nlen)
3545{
3546 size_t len;
3547 const char *p, *pstart;
3548
3549 p = cmdline;
3550 while (qemu_isspace(*p))
3551 p++;
3552 if (*p == '\0')
3553 return NULL;
3554 pstart = p;
3555 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3556 p++;
3557 len = p - pstart;
3558 if (len > nlen - 1)
3559 len = nlen - 1;
3560 memcpy(cmdname, pstart, len);
3561 cmdname[len] = '\0';
3562 return p;
3563}
3564
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003565/**
3566 * Read key of 'type' into 'key' and return the current
3567 * 'type' pointer.
3568 */
3569static char *key_get_info(const char *type, char **key)
3570{
3571 size_t len;
3572 char *p, *str;
3573
3574 if (*type == ',')
3575 type++;
3576
3577 p = strchr(type, ':');
3578 if (!p) {
3579 *key = NULL;
3580 return NULL;
3581 }
3582 len = p - type;
3583
Anthony Liguori7267c092011-08-20 22:09:37 -05003584 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003585 memcpy(str, type, len);
3586 str[len] = '\0';
3587
3588 *key = str;
3589 return ++p;
3590}
3591
bellard9307c4c2004-04-04 12:57:25 +00003592static int default_fmt_format = 'x';
3593static int default_fmt_size = 4;
3594
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003595static int is_valid_option(const char *c, const char *typestr)
3596{
3597 char option[3];
3598
3599 option[0] = '-';
3600 option[1] = *c;
3601 option[2] = '\0';
3602
3603 typestr = strstr(typestr, option);
3604 return (typestr != NULL);
3605}
3606
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003607static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3608 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003609{
3610 const mon_cmd_t *cmd;
3611
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003612 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003613 if (compare_cmd(cmdname, cmd->name)) {
3614 return cmd;
3615 }
3616 }
3617
3618 return NULL;
3619}
3620
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003621static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3622{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003623 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003624}
3625
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003626/*
Bandan Dasae502122015-06-03 18:38:08 -04003627 * Parse command name from @cmdp according to command table @table.
3628 * If blank, return NULL.
3629 * Else, if no valid command can be found, report to @mon, and return
3630 * NULL.
3631 * Else, change @cmdp to point right behind the name, and return its
3632 * command table entry.
3633 * Do not assume the return value points into @table! It doesn't when
3634 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003635 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003636static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04003637 const char **cmdp,
3638 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003639{
Bandan Dasae502122015-06-03 18:38:08 -04003640 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003641 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003642 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003643
bellard9307c4c2004-04-04 12:57:25 +00003644 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003645 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003646 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003647 return NULL;
ths3b46e622007-09-17 08:09:54 +00003648
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003649 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003650 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003651 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04003652 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003653 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003654 }
bellard9307c4c2004-04-04 12:57:25 +00003655
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003656 /* filter out following useless space */
3657 while (qemu_isspace(*p)) {
3658 p++;
3659 }
Bandan Dasae502122015-06-03 18:38:08 -04003660
3661 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003662 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003663 if (cmd->sub_table != NULL && *p != '\0') {
3664 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003665 }
3666
Bandan Dasae502122015-06-03 18:38:08 -04003667 return cmd;
3668}
3669
3670/*
3671 * Parse arguments for @cmd.
3672 * If it can't be parsed, report to @mon, and return NULL.
3673 * Else, insert command arguments into a QDict, and return it.
3674 * Note: On success, caller has to free the QDict structure.
3675 */
3676
3677static QDict *monitor_parse_arguments(Monitor *mon,
3678 const char **endp,
3679 const mon_cmd_t *cmd)
3680{
3681 const char *typestr;
3682 char *key;
3683 int c;
3684 const char *p = *endp;
3685 char buf[1024];
3686 QDict *qdict = qdict_new();
3687
bellard9307c4c2004-04-04 12:57:25 +00003688 /* parse the parameters */
3689 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003690 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003691 typestr = key_get_info(typestr, &key);
3692 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003693 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003694 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003695 typestr++;
3696 switch(c) {
3697 case 'F':
bellard81d09122004-07-14 17:21:37 +00003698 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003699 case 's':
3700 {
3701 int ret;
ths3b46e622007-09-17 08:09:54 +00003702
blueswir1cd390082008-11-16 13:53:32 +00003703 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003704 p++;
3705 if (*typestr == '?') {
3706 typestr++;
3707 if (*p == '\0') {
3708 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003709 break;
bellard9307c4c2004-04-04 12:57:25 +00003710 }
3711 }
3712 ret = get_str(buf, sizeof(buf), &p);
3713 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003714 switch(c) {
3715 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003716 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003717 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003718 break;
3719 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003720 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003721 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003722 break;
3723 default:
Bandan Dasae502122015-06-03 18:38:08 -04003724 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003725 break;
3726 }
bellard9307c4c2004-04-04 12:57:25 +00003727 goto fail;
3728 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003729 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003730 }
3731 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003732 case 'O':
3733 {
3734 QemuOptsList *opts_list;
3735 QemuOpts *opts;
3736
3737 opts_list = qemu_find_opts(key);
3738 if (!opts_list || opts_list->desc->name) {
3739 goto bad_type;
3740 }
3741 while (qemu_isspace(*p)) {
3742 p++;
3743 }
3744 if (!*p)
3745 break;
3746 if (get_str(buf, sizeof(buf), &p) < 0) {
3747 goto fail;
3748 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003749 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003750 if (!opts) {
3751 goto fail;
3752 }
3753 qemu_opts_to_qdict(opts, qdict);
3754 qemu_opts_del(opts);
3755 }
3756 break;
bellard9307c4c2004-04-04 12:57:25 +00003757 case '/':
3758 {
3759 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003760
blueswir1cd390082008-11-16 13:53:32 +00003761 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003762 p++;
3763 if (*p == '/') {
3764 /* format found */
3765 p++;
3766 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003767 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003768 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003769 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003770 count = count * 10 + (*p - '0');
3771 p++;
3772 }
3773 }
3774 size = -1;
3775 format = -1;
3776 for(;;) {
3777 switch(*p) {
3778 case 'o':
3779 case 'd':
3780 case 'u':
3781 case 'x':
3782 case 'i':
3783 case 'c':
3784 format = *p++;
3785 break;
3786 case 'b':
3787 size = 1;
3788 p++;
3789 break;
3790 case 'h':
3791 size = 2;
3792 p++;
3793 break;
3794 case 'w':
3795 size = 4;
3796 p++;
3797 break;
3798 case 'g':
3799 case 'L':
3800 size = 8;
3801 p++;
3802 break;
3803 default:
3804 goto next;
3805 }
3806 }
3807 next:
blueswir1cd390082008-11-16 13:53:32 +00003808 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003809 monitor_printf(mon, "invalid char in format: '%c'\n",
3810 *p);
bellard9307c4c2004-04-04 12:57:25 +00003811 goto fail;
3812 }
bellard9307c4c2004-04-04 12:57:25 +00003813 if (format < 0)
3814 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003815 if (format != 'i') {
3816 /* for 'i', not specifying a size gives -1 as size */
3817 if (size < 0)
3818 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003819 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003820 }
bellard9307c4c2004-04-04 12:57:25 +00003821 default_fmt_format = format;
3822 } else {
3823 count = 1;
3824 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003825 if (format != 'i') {
3826 size = default_fmt_size;
3827 } else {
3828 size = -1;
3829 }
bellard9307c4c2004-04-04 12:57:25 +00003830 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003831 qdict_put(qdict, "count", qint_from_int(count));
3832 qdict_put(qdict, "format", qint_from_int(format));
3833 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003834 }
3835 break;
3836 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003837 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003838 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003839 {
blueswir1c2efc952007-09-25 17:28:42 +00003840 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003841
blueswir1cd390082008-11-16 13:53:32 +00003842 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003843 p++;
bellard34405572004-06-08 00:55:58 +00003844 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003845 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003846 if (*p == '\0') {
3847 typestr++;
3848 break;
3849 }
bellard34405572004-06-08 00:55:58 +00003850 } else {
3851 if (*p == '.') {
3852 p++;
blueswir1cd390082008-11-16 13:53:32 +00003853 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003854 p++;
bellard34405572004-06-08 00:55:58 +00003855 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003856 typestr++;
3857 break;
bellard34405572004-06-08 00:55:58 +00003858 }
3859 }
bellard13224a82006-07-14 22:03:35 +00003860 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003861 }
aliguori376253e2009-03-05 23:01:23 +00003862 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003863 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003864 /* Check if 'i' is greater than 32-bit */
3865 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003866 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003867 monitor_printf(mon, "integer is for 32-bit values\n");
3868 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003869 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003870 if (val < 0) {
3871 monitor_printf(mon, "enter a positive value\n");
3872 goto fail;
3873 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003874 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003875 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003876 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003877 }
3878 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003879 case 'o':
3880 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003881 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003882 char *end;
3883
3884 while (qemu_isspace(*p)) {
3885 p++;
3886 }
3887 if (*typestr == '?') {
3888 typestr++;
3889 if (*p == '\0') {
3890 break;
3891 }
3892 }
3893 val = strtosz(p, &end);
3894 if (val < 0) {
3895 monitor_printf(mon, "invalid size\n");
3896 goto fail;
3897 }
3898 qdict_put(qdict, key, qint_from_int(val));
3899 p = end;
3900 }
3901 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003902 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003903 {
3904 double val;
3905
3906 while (qemu_isspace(*p))
3907 p++;
3908 if (*typestr == '?') {
3909 typestr++;
3910 if (*p == '\0') {
3911 break;
3912 }
3913 }
3914 if (get_double(mon, &val, &p) < 0) {
3915 goto fail;
3916 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003917 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003918 switch (*p) {
3919 case 'm':
3920 val /= 1e3; p += 2; break;
3921 case 'u':
3922 val /= 1e6; p += 2; break;
3923 case 'n':
3924 val /= 1e9; p += 2; break;
3925 }
3926 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003927 if (*p && !qemu_isspace(*p)) {
3928 monitor_printf(mon, "Unknown unit suffix\n");
3929 goto fail;
3930 }
3931 qdict_put(qdict, key, qfloat_from_double(val));
3932 }
3933 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003934 case 'b':
3935 {
3936 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003937 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003938
3939 while (qemu_isspace(*p)) {
3940 p++;
3941 }
3942 beg = p;
3943 while (qemu_isgraph(*p)) {
3944 p++;
3945 }
3946 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003947 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003948 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003949 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003950 } else {
3951 monitor_printf(mon, "Expected 'on' or 'off'\n");
3952 goto fail;
3953 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06003954 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003955 }
3956 break;
bellard9307c4c2004-04-04 12:57:25 +00003957 case '-':
3958 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003959 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003960 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003961 /* option */
ths3b46e622007-09-17 08:09:54 +00003962
bellard9307c4c2004-04-04 12:57:25 +00003963 c = *typestr++;
3964 if (c == '\0')
3965 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003966 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003967 p++;
bellard9307c4c2004-04-04 12:57:25 +00003968 if (*p == '-') {
3969 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003970 if(c != *p) {
3971 if(!is_valid_option(p, typestr)) {
3972
3973 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003974 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003975 goto fail;
3976 } else {
3977 skip_key = 1;
3978 }
bellard9307c4c2004-04-04 12:57:25 +00003979 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003980 if(skip_key) {
3981 p = tmp;
3982 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003983 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003984 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003985 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003986 }
bellard9307c4c2004-04-04 12:57:25 +00003987 }
bellard9307c4c2004-04-04 12:57:25 +00003988 }
3989 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003990 case 'S':
3991 {
3992 /* package all remaining string */
3993 int len;
3994
3995 while (qemu_isspace(*p)) {
3996 p++;
3997 }
3998 if (*typestr == '?') {
3999 typestr++;
4000 if (*p == '\0') {
4001 /* no remaining string: NULL argument */
4002 break;
4003 }
4004 }
4005 len = strlen(p);
4006 if (len <= 0) {
4007 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04004008 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04004009 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08004010 }
4011 qdict_put(qdict, key, qstring_from_str(p));
4012 p += len;
4013 }
4014 break;
bellard9307c4c2004-04-04 12:57:25 +00004015 default:
4016 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04004017 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00004018 goto fail;
4019 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004020 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004021 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004022 }
4023 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004024 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004025 p++;
4026 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004027 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04004028 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00004029 goto fail;
4030 }
4031
Bandan Dasae502122015-06-03 18:38:08 -04004032 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004033
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004034fail:
Bandan Dasae502122015-06-03 18:38:08 -04004035 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05004036 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004037 return NULL;
4038}
4039
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004040static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004041{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004042 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004043 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004044
Bandan Dasae502122015-06-03 18:38:08 -04004045 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4046 if (!cmd) {
4047 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004048 }
4049
Bandan Dasae502122015-06-03 18:38:08 -04004050 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4051 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04004052 monitor_printf(mon, "Try \"help %s\" for more information\n",
4053 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04004054 return;
4055 }
4056
4057 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004058 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004059}
4060
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004061static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00004062{
4063 const char *p, *pstart;
4064 char cmd[128];
4065 int len;
4066
4067 p = list;
4068 for(;;) {
4069 pstart = p;
4070 p = strchr(p, '|');
4071 if (!p)
4072 p = pstart + strlen(pstart);
4073 len = p - pstart;
4074 if (len > sizeof(cmd) - 2)
4075 len = sizeof(cmd) - 2;
4076 memcpy(cmd, pstart, len);
4077 cmd[len] = '\0';
4078 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004079 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004080 }
4081 if (*p == '\0')
4082 break;
4083 p++;
4084 }
4085}
4086
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004087static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00004088{
4089 DIR *ffs;
4090 struct dirent *d;
4091 char path[1024];
4092 char file[1024], file_prefix[1024];
4093 int input_path_len;
4094 const char *p;
4095
ths5fafdf22007-09-16 21:08:06 +00004096 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004097 if (!p) {
4098 input_path_len = 0;
4099 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004100 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004101 } else {
4102 input_path_len = p - input + 1;
4103 memcpy(path, input, input_path_len);
4104 if (input_path_len > sizeof(path) - 1)
4105 input_path_len = sizeof(path) - 1;
4106 path[input_path_len] = '\0';
4107 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4108 }
Bandan Das19f2db52015-06-03 18:38:07 -04004109
bellard81d09122004-07-14 17:21:37 +00004110 ffs = opendir(path);
4111 if (!ffs)
4112 return;
4113 for(;;) {
4114 struct stat sb;
4115 d = readdir(ffs);
4116 if (!d)
4117 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004118
4119 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4120 continue;
4121 }
4122
bellard81d09122004-07-14 17:21:37 +00004123 if (strstart(d->d_name, file_prefix, NULL)) {
4124 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004125 if (input_path_len < sizeof(file))
4126 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4127 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004128 /* stat the file to find out if it's a directory.
4129 * In that case add a slash to speed up typing long paths
4130 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004131 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004132 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004133 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004134 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004135 }
4136 }
4137 closedir(ffs);
4138}
4139
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004140static const char *next_arg_type(const char *typestr)
4141{
4142 const char *p = strchr(typestr, ':');
4143 return (p != NULL ? ++p : typestr);
4144}
4145
Hani Benhabiles40d19392014-05-07 23:41:30 +01004146static void add_completion_option(ReadLineState *rs, const char *str,
4147 const char *option)
4148{
4149 if (!str || !option) {
4150 return;
4151 }
4152 if (!strncmp(option, str, strlen(str))) {
4153 readline_add_completion(rs, option);
4154 }
4155}
4156
Hani Benhabiles13e315d2014-05-07 23:41:29 +01004157void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4158{
4159 size_t len;
4160 ChardevBackendInfoList *list, *start;
4161
4162 if (nb_args != 2) {
4163 return;
4164 }
4165 len = strlen(str);
4166 readline_set_completion_index(rs, len);
4167
4168 start = list = qmp_query_chardev_backends(NULL);
4169 while (list) {
4170 const char *chr_name = list->value->name;
4171
4172 if (!strncmp(chr_name, str, len)) {
4173 readline_add_completion(rs, chr_name);
4174 }
4175 list = list->next;
4176 }
4177 qapi_free_ChardevBackendInfoList(start);
4178}
4179
Hani Benhabilesb162b492014-05-07 23:41:31 +01004180void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4181{
4182 size_t len;
4183 int i;
4184
4185 if (nb_args != 2) {
4186 return;
4187 }
4188 len = strlen(str);
4189 readline_set_completion_index(rs, len);
4190 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4191 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4192 }
4193}
4194
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004195void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004196{
4197 GSList *list, *elt;
4198 size_t len;
4199
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004200 if (nb_args != 2) {
4201 return;
4202 }
4203
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004204 len = strlen(str);
4205 readline_set_completion_index(rs, len);
4206 list = elt = object_class_get_list(TYPE_DEVICE, false);
4207 while (elt) {
4208 const char *name;
4209 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4210 TYPE_DEVICE);
4211 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004212
4213 if (!dc->cannot_instantiate_with_device_add_yet
4214 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01004215 readline_add_completion(rs, name);
4216 }
4217 elt = elt->next;
4218 }
4219 g_slist_free(list);
4220}
4221
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004222void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004223{
4224 GSList *list, *elt;
4225 size_t len;
4226
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004227 if (nb_args != 2) {
4228 return;
4229 }
4230
Hani Benhabiles1094fd32014-02-06 23:30:13 +01004231 len = strlen(str);
4232 readline_set_completion_index(rs, len);
4233 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4234 while (elt) {
4235 const char *name;
4236
4237 name = object_class_get_name(OBJECT_CLASS(elt->data));
4238 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4239 readline_add_completion(rs, name);
4240 }
4241 elt = elt->next;
4242 }
4243 g_slist_free(list);
4244}
4245
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004246static void peripheral_device_del_completion(ReadLineState *rs,
4247 const char *str, size_t len)
4248{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004249 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4250 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004251
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02004252 list = qdev_build_hotpluggable_device_list(peripheral);
4253 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004254 return;
4255 }
4256
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004257 for (item = list; item; item = g_slist_next(item)) {
4258 DeviceState *dev = item->data;
4259
4260 if (dev->id && !strncmp(str, dev->id, len)) {
4261 readline_add_completion(rs, dev->id);
4262 }
4263 }
4264
4265 g_slist_free(list);
4266}
4267
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01004268void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4269{
4270 size_t len;
4271 ChardevInfoList *list, *start;
4272
4273 if (nb_args != 2) {
4274 return;
4275 }
4276 len = strlen(str);
4277 readline_set_completion_index(rs, len);
4278
4279 start = list = qmp_query_chardev(NULL);
4280 while (list) {
4281 ChardevInfo *chr = list->value;
4282
4283 if (!strncmp(chr->label, str, len)) {
4284 readline_add_completion(rs, chr->label);
4285 }
4286 list = list->next;
4287 }
4288 qapi_free_ChardevInfoList(start);
4289}
4290
Hani Benhabiles8e597772014-05-27 23:39:30 +01004291static void ringbuf_completion(ReadLineState *rs, const char *str)
4292{
4293 size_t len;
4294 ChardevInfoList *list, *start;
4295
4296 len = strlen(str);
4297 readline_set_completion_index(rs, len);
4298
4299 start = list = qmp_query_chardev(NULL);
4300 while (list) {
4301 ChardevInfo *chr_info = list->value;
4302
4303 if (!strncmp(chr_info->label, str, len)) {
4304 CharDriverState *chr = qemu_chr_find(chr_info->label);
4305 if (chr && chr_is_ringbuf(chr)) {
4306 readline_add_completion(rs, chr_info->label);
4307 }
4308 }
4309 list = list->next;
4310 }
4311 qapi_free_ChardevInfoList(start);
4312}
4313
Hani Benhabiles8e597772014-05-27 23:39:30 +01004314void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4315{
4316 if (nb_args != 2) {
4317 return;
4318 }
4319 ringbuf_completion(rs, str);
4320}
4321
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004322void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4323{
4324 size_t len;
4325
4326 if (nb_args != 2) {
4327 return;
4328 }
4329
4330 len = strlen(str);
4331 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08004332 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01004333}
4334
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004335void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004336{
4337 ObjectPropertyInfoList *list, *start;
4338 size_t len;
4339
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004340 if (nb_args != 2) {
4341 return;
4342 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01004343 len = strlen(str);
4344 readline_set_completion_index(rs, len);
4345
4346 start = list = qmp_qom_list("/objects", NULL);
4347 while (list) {
4348 ObjectPropertyInfo *info = list->value;
4349
4350 if (!strncmp(info->type, "child<", 5)
4351 && !strncmp(info->name, str, len)) {
4352 readline_add_completion(rs, info->name);
4353 }
4354 list = list->next;
4355 }
4356 qapi_free_ObjectPropertyInfoList(start);
4357}
4358
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004359void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4360{
4361 int i;
4362 char *sep;
4363 size_t len;
4364
4365 if (nb_args != 2) {
4366 return;
4367 }
4368 sep = strrchr(str, '-');
4369 if (sep) {
4370 str = sep + 1;
4371 }
4372 len = strlen(str);
4373 readline_set_completion_index(rs, len);
4374 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4375 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4376 readline_add_completion(rs, QKeyCode_lookup[i]);
4377 }
4378 }
4379}
4380
Hani Benhabiles40d19392014-05-07 23:41:30 +01004381void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4382{
4383 size_t len;
4384
4385 len = strlen(str);
4386 readline_set_completion_index(rs, len);
4387 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08004388 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01004389 int count, i;
4390 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004391 NET_CLIENT_OPTIONS_KIND_NONE,
4392 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004393 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01004394 const char *name = ncs[i]->name;
4395 if (!strncmp(str, name, len)) {
4396 readline_add_completion(rs, name);
4397 }
4398 }
4399 } else if (nb_args == 3) {
4400 add_completion_option(rs, str, "on");
4401 add_completion_option(rs, str, "off");
4402 }
4403}
4404
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004405void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4406{
4407 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08004408 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004409
4410 if (nb_args != 2) {
4411 return;
4412 }
4413
4414 len = strlen(str);
4415 readline_set_completion_index(rs, len);
4416 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08004417 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004418 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01004419 QemuOpts *opts;
4420 const char *name = ncs[i]->name;
4421 if (strncmp(str, name, len)) {
4422 continue;
4423 }
4424 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4425 if (opts) {
4426 readline_add_completion(rs, name);
4427 }
4428 }
4429}
4430
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004431void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4432{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004433 int i;
4434
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004435 if (nb_args != 2) {
4436 return;
4437 }
4438 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01004439 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4440 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4441 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01004442}
4443
Hani Benhabilesc68a0402014-05-27 23:39:32 +01004444void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4445 const char *str)
4446{
4447 size_t len;
4448
4449 len = strlen(str);
4450 readline_set_completion_index(rs, len);
4451 if (nb_args == 2) {
4452 int i;
4453 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4454 const char *name = MigrationCapability_lookup[i];
4455 if (!strncmp(str, name, len)) {
4456 readline_add_completion(rs, name);
4457 }
4458 }
4459 } else if (nb_args == 3) {
4460 add_completion_option(rs, str, "on");
4461 add_completion_option(rs, str, "off");
4462 }
4463}
4464
Liang Li50e9a622015-03-23 16:32:29 +08004465void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4466 const char *str)
4467{
4468 size_t len;
4469
4470 len = strlen(str);
4471 readline_set_completion_index(rs, len);
4472 if (nb_args == 2) {
4473 int i;
4474 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4475 const char *name = MigrationParameter_lookup[i];
4476 if (!strncmp(str, name, len)) {
4477 readline_add_completion(rs, name);
4478 }
4479 }
4480 }
4481}
4482
Hani Benhabilese3bb5322014-05-27 23:39:34 +01004483void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4484{
4485 int i;
4486 size_t len;
4487 if (nb_args != 2) {
4488 return;
4489 }
4490 len = strlen(str);
4491 readline_set_completion_index(rs, len);
4492 for (i = 0; host_net_devices[i]; i++) {
4493 if (!strncmp(host_net_devices[i], str, len)) {
4494 readline_add_completion(rs, host_net_devices[i]);
4495 }
4496 }
4497}
4498
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004499void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4500{
Jason Wangeaed4832015-04-23 14:21:38 +08004501 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004502 int count, i, len;
4503
4504 len = strlen(str);
4505 readline_set_completion_index(rs, len);
4506 if (nb_args == 2) {
4507 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004508 NET_CLIENT_OPTIONS_KIND_NONE,
4509 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004510 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004511 int id;
4512 char name[16];
4513
4514 if (net_hub_id_for_client(ncs[i], &id)) {
4515 continue;
4516 }
4517 snprintf(name, sizeof(name), "%d", id);
4518 if (!strncmp(str, name, len)) {
4519 readline_add_completion(rs, name);
4520 }
4521 }
4522 return;
4523 } else if (nb_args == 3) {
4524 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08004525 NET_CLIENT_OPTIONS_KIND_NIC,
4526 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08004527 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08004528 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004529 const char *name;
4530
Jason Wang2c4681f2015-02-02 15:06:38 +08004531 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4532 net_hub_id_for_client(ncs[i], &id)) {
4533 continue;
4534 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01004535 name = ncs[i]->name;
4536 if (!strncmp(str, name, len)) {
4537 readline_add_completion(rs, name);
4538 }
4539 }
4540 return;
4541 }
4542}
4543
Hani Benhabilesb21631f2014-05-27 23:39:37 +01004544static void vm_completion(ReadLineState *rs, const char *str)
4545{
4546 size_t len;
4547 BlockDriverState *bs = NULL;
4548
4549 len = strlen(str);
4550 readline_set_completion_index(rs, len);
4551 while ((bs = bdrv_next(bs))) {
4552 SnapshotInfoList *snapshots, *snapshot;
4553
4554 if (!bdrv_can_snapshot(bs)) {
4555 continue;
4556 }
4557 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4558 continue;
4559 }
4560 snapshot = snapshots;
4561 while (snapshot) {
4562 char *completion = snapshot->value->name;
4563 if (!strncmp(str, completion, len)) {
4564 readline_add_completion(rs, completion);
4565 }
4566 completion = snapshot->value->id;
4567 if (!strncmp(str, completion, len)) {
4568 readline_add_completion(rs, completion);
4569 }
4570 snapshot = snapshot->next;
4571 }
4572 qapi_free_SnapshotInfoList(snapshots);
4573 }
4574
4575}
4576
4577void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4578{
4579 if (nb_args == 2) {
4580 vm_completion(rs, str);
4581 }
4582}
4583
4584void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4585{
4586 if (nb_args == 2) {
4587 vm_completion(rs, str);
4588 }
4589}
4590
Wenchao Xiac35b6402013-08-27 20:38:24 +08004591static void monitor_find_completion_by_table(Monitor *mon,
4592 const mon_cmd_t *cmd_table,
4593 char **args,
4594 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004595{
4596 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004597 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004598 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004599 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004600 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00004601
bellard81d09122004-07-14 17:21:37 +00004602 if (nb_args <= 1) {
4603 /* command completion */
4604 if (nb_args == 0)
4605 cmdname = "";
4606 else
4607 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004608 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004609 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08004610 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00004611 }
4612 } else {
4613 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004614 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004615 if (compare_cmd(args[0], cmd->name)) {
4616 break;
4617 }
bellard81d09122004-07-14 17:21:37 +00004618 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004619 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004620 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004621 }
4622
Wenchao Xiad903a772013-08-27 20:38:25 +08004623 if (cmd->sub_table) {
4624 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004625 monitor_find_completion_by_table(mon, cmd->sub_table,
4626 &args[1], nb_args - 1);
4627 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004628 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004629 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004630 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4631 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004632 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004633
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004634 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004635 for(i = 0; i < nb_args - 2; i++) {
4636 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004637 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004638 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004639 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004640 }
4641 }
4642 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01004643 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004644 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004645 }
bellard81d09122004-07-14 17:21:37 +00004646 switch(*ptype) {
4647 case 'F':
4648 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004649 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004650 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004651 break;
4652 case 'B':
4653 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004654 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004655 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4656 name = bdrv_get_device_name(bs);
4657 if (str[0] == '\0' ||
4658 !strncmp(name, str, strlen(str))) {
4659 readline_add_completion(mon->rs, name);
4660 }
4661 }
bellard81d09122004-07-14 17:21:37 +00004662 break;
bellard7fe48482004-10-09 18:08:01 +00004663 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004664 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004665 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004666 monitor_find_completion_by_table(mon, cmd_table,
4667 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004668 }
4669 break;
bellard81d09122004-07-14 17:21:37 +00004670 default:
4671 break;
4672 }
4673 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004674}
4675
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004676static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004677 const char *cmdline)
4678{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004679 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004680 char *args[MAX_ARGS];
4681 int nb_args, len;
4682
4683 /* 1. parse the cmdline */
4684 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4685 return;
4686 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004687
4688 /* if the line ends with a space, it means we want to complete the
4689 next arg */
4690 len = strlen(cmdline);
4691 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4692 if (nb_args >= MAX_ARGS) {
4693 goto cleanup;
4694 }
4695 args[nb_args++] = g_strdup("");
4696 }
4697
4698 /* 2. auto complete according to args */
4699 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004700
4701cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004702 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004703}
4704
aliguori731b0362009-03-05 23:01:42 +00004705static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004706{
aliguori731b0362009-03-05 23:01:42 +00004707 Monitor *mon = opaque;
4708
Jan Kiszkac62313b2009-12-04 14:05:29 +01004709 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004710}
4711
Markus Armbruster40861822015-05-29 10:27:16 +02004712static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4713 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02004714{
Markus Armbruster485febc2015-03-13 17:25:50 +01004715 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01004716
4717 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004718 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4719 "Capabilities negotiation is already complete, command "
4720 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004721 return true;
4722 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01004723 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02004724 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4725 "Expecting capabilities negotiation with "
4726 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06004727 return true;
4728 }
4729 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004730}
4731
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004732/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004733 * Argument validation rules:
4734 *
4735 * 1. The argument must exist in cmd_args qdict
4736 * 2. The argument type must be the expected one
4737 *
4738 * Special case: If the argument doesn't exist in cmd_args and
4739 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4740 * checking is skipped for it.
4741 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004742static void check_client_args_type(const QDict *client_args,
4743 const QDict *cmd_args, int flags,
4744 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03004745{
4746 const QDictEntry *ent;
4747
4748 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4749 QObject *obj;
4750 QString *arg_type;
4751 const QObject *client_arg = qdict_entry_value(ent);
4752 const char *client_arg_name = qdict_entry_key(ent);
4753
4754 obj = qdict_get(cmd_args, client_arg_name);
4755 if (!obj) {
4756 if (flags & QMP_ACCEPT_UNKNOWNS) {
4757 /* handler accepts unknowns */
4758 continue;
4759 }
4760 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004761 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004762 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004763 }
4764
4765 arg_type = qobject_to_qstring(obj);
4766 assert(arg_type != NULL);
4767
4768 /* check if argument's type is correct */
4769 switch (qstring_get_str(arg_type)[0]) {
4770 case 'F':
4771 case 'B':
4772 case 's':
4773 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004774 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4775 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01004776 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004777 }
4778 break;
4779 case 'i':
4780 case 'l':
4781 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004782 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004783 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004784 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4785 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01004786 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004787 }
4788 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004789 case 'T':
4790 if (qobject_type(client_arg) != QTYPE_QINT &&
4791 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004792 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4793 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01004794 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004795 }
4796 break;
4797 case 'b':
4798 case '-':
4799 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004800 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4801 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01004802 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004803 }
4804 break;
4805 case 'O':
4806 assert(flags & QMP_ACCEPT_UNKNOWNS);
4807 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004808 case 'q':
4809 /* Any QObject can be passed. */
4810 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004811 case '/':
4812 case '.':
4813 /*
4814 * These types are not supported by QMP and thus are not
4815 * handled here. Fall through.
4816 */
4817 default:
4818 abort();
4819 }
4820 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03004821}
4822
4823/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004824 * - Check if the client has passed all mandatory args
4825 * - Set special flags for argument validation
4826 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004827static void check_mandatory_args(const QDict *cmd_args,
4828 const QDict *client_args, int *flags,
4829 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004830{
4831 const QDictEntry *ent;
4832
4833 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4834 const char *cmd_arg_name = qdict_entry_key(ent);
4835 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4836 assert(type != NULL);
4837
4838 if (qstring_get_str(type)[0] == 'O') {
4839 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4840 *flags |= QMP_ACCEPT_UNKNOWNS;
4841 } else if (qstring_get_str(type)[0] != '-' &&
4842 qstring_get_str(type)[1] != '?' &&
4843 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004844 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01004845 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004846 }
4847 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004848}
4849
4850static QDict *qdict_from_args_type(const char *args_type)
4851{
4852 int i;
4853 QDict *qdict;
4854 QString *key, *type, *cur_qs;
4855
4856 assert(args_type != NULL);
4857
4858 qdict = qdict_new();
4859
4860 if (args_type == NULL || args_type[0] == '\0') {
4861 /* no args, empty qdict */
4862 goto out;
4863 }
4864
4865 key = qstring_new();
4866 type = qstring_new();
4867
4868 cur_qs = key;
4869
4870 for (i = 0;; i++) {
4871 switch (args_type[i]) {
4872 case ',':
4873 case '\0':
4874 qdict_put(qdict, qstring_get_str(key), type);
4875 QDECREF(key);
4876 if (args_type[i] == '\0') {
4877 goto out;
4878 }
4879 type = qstring_new(); /* qdict has ref */
4880 cur_qs = key = qstring_new();
4881 break;
4882 case ':':
4883 cur_qs = type;
4884 break;
4885 default:
4886 qstring_append_chr(cur_qs, args_type[i]);
4887 break;
4888 }
4889 }
4890
4891out:
4892 return qdict;
4893}
4894
4895/*
4896 * Client argument checking rules:
4897 *
4898 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004899 * 2. Each argument provided by the client must be expected
4900 * 3. Each argument provided by the client must have the type expected
4901 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004902 */
Markus Armbruster326283a2015-03-02 18:39:09 +01004903static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4904 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004905{
Markus Armbruster326283a2015-03-02 18:39:09 +01004906 Error *err = NULL;
4907 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004908 QDict *cmd_args;
4909
4910 cmd_args = qdict_from_args_type(cmd->args_type);
4911
4912 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01004913 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004914 if (err) {
4915 goto out;
4916 }
4917
Markus Armbruster326283a2015-03-02 18:39:09 +01004918 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004919
4920out:
Markus Armbruster326283a2015-03-02 18:39:09 +01004921 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004922 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004923}
4924
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004925/*
4926 * Input object checking rules
4927 *
4928 * 1. Input object must be a dict
4929 * 2. The "execute" key must exist
4930 * 3. The "execute" key must be a string
4931 * 4. If the "arguments" key exists, it must be a dict
4932 * 5. If the "id" key exists, it can be anything (ie. json-value)
4933 * 6. Any argument not listed above is considered invalid
4934 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01004935static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004936{
4937 const QDictEntry *ent;
4938 int has_exec_key = 0;
4939 QDict *input_dict;
4940
4941 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004942 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004943 return NULL;
4944 }
4945
4946 input_dict = qobject_to_qdict(input_obj);
4947
4948 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4949 const char *arg_name = qdict_entry_key(ent);
4950 const QObject *arg_obj = qdict_entry_value(ent);
4951
4952 if (!strcmp(arg_name, "execute")) {
4953 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004954 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4955 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004956 return NULL;
4957 }
4958 has_exec_key = 1;
4959 } else if (!strcmp(arg_name, "arguments")) {
4960 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004961 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4962 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004963 return NULL;
4964 }
Markus Armbruster779cec42015-06-08 10:44:30 +02004965 } else if (!strcmp(arg_name, "id")) {
4966 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004967 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004968 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004969 return NULL;
4970 }
4971 }
4972
4973 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004974 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004975 return NULL;
4976 }
4977
4978 return input_dict;
4979}
4980
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004981static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4982{
Markus Armbruster326283a2015-03-02 18:39:09 +01004983 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004984 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004985 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004986 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004987 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004988 Monitor *mon = cur_mon;
4989
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004990 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01004991 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004992
4993 obj = json_parser_parse(tokens, NULL);
4994 if (!obj) {
4995 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004996 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004997 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004998 }
4999
Markus Armbrusterba0510a2015-03-02 18:41:43 +01005000 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03005001 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005002 qobject_decref(obj);
5003 goto err_out;
5004 }
5005
Markus Armbruster74358f22015-03-06 19:35:59 +01005006 mon->qmp.id = qdict_get(input, "id");
5007 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005008
Luiz Capitulino0bbab462010-05-31 17:32:50 -03005009 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01005010 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05005011 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06005012 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01005013 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5014 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005015 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005016 }
Markus Armbruster40861822015-05-29 10:27:16 +02005017 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06005018 goto err_out;
5019 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005020
5021 obj = qdict_get(input, "arguments");
5022 if (!obj) {
5023 args = qdict_new();
5024 } else {
5025 args = qobject_to_qdict(obj);
5026 QINCREF(args);
5027 }
5028
Markus Armbruster326283a2015-03-02 18:39:09 +01005029 qmp_check_client_args(cmd, args, &local_err);
5030 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005031 goto err_out;
5032 }
5033
Markus Armbruster485febc2015-03-13 17:25:50 +01005034 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005035
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005036err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01005037 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01005038 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03005039 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005040 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02005041}
5042
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005043static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005044{
5045 Monitor *old_mon = cur_mon;
5046
5047 cur_mon = opaque;
5048
Markus Armbruster74358f22015-03-06 19:35:59 +01005049 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005050
5051 cur_mon = old_mon;
5052}
5053
aliguori731b0362009-03-05 23:01:42 +00005054static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00005055{
aliguori731b0362009-03-05 23:01:42 +00005056 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00005057 int i;
aliguori376253e2009-03-05 23:01:23 +00005058
aliguori731b0362009-03-05 23:01:42 +00005059 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00005060
aliguoricde76ee2009-03-05 23:01:51 +00005061 if (cur_mon->rs) {
5062 for (i = 0; i < size; i++)
5063 readline_handle_byte(cur_mon->rs, buf[i]);
5064 } else {
5065 if (size == 0 || buf[size - 1] != 0)
5066 monitor_printf(cur_mon, "corrupted command\n");
5067 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005068 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00005069 }
aliguori731b0362009-03-05 23:01:42 +00005070
5071 cur_mon = old_mon;
5072}
aliguorid8f44602008-10-06 13:52:44 +00005073
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005074static void monitor_command_cb(void *opaque, const char *cmdline,
5075 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005076{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005077 Monitor *mon = opaque;
5078
aliguori731b0362009-03-05 23:01:42 +00005079 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01005080 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00005081 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00005082}
5083
aliguoricde76ee2009-03-05 23:01:51 +00005084int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005085{
aliguoricde76ee2009-03-05 23:01:51 +00005086 if (!mon->rs)
5087 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00005088 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00005089 return 0;
aliguorid8f44602008-10-06 13:52:44 +00005090}
5091
aliguori376253e2009-03-05 23:01:23 +00005092void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00005093{
aliguoricde76ee2009-03-05 23:01:51 +00005094 if (!mon->rs)
5095 return;
aliguori731b0362009-03-05 23:01:42 +00005096 if (--mon->suspend_cnt == 0)
5097 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00005098}
5099
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005100static QObject *get_qmp_greeting(void)
5101{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03005102 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005103
Markus Armbruster485febc2015-03-13 17:25:50 +01005104 qmp_marshal_input_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005105 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5106}
5107
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005108static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005109{
Luiz Capitulino47116d12010-02-08 17:01:30 -02005110 QObject *data;
5111 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005112
Luiz Capitulino47116d12010-02-08 17:01:30 -02005113 switch (event) {
5114 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01005115 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02005116 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005117 monitor_json_emitter(mon, data);
5118 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04005119 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02005120 break;
5121 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01005122 json_message_parser_destroy(&mon->qmp.parser);
5123 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04005124 mon_refcount--;
5125 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02005126 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005127 }
5128}
5129
aliguori731b0362009-03-05 23:01:42 +00005130static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00005131{
aliguori376253e2009-03-05 23:01:23 +00005132 Monitor *mon = opaque;
5133
aliguori2724b182009-03-05 23:01:47 +00005134 switch (event) {
5135 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005136 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005137 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005138 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005139 if (mon->reset_seen) {
5140 readline_restart(mon->rs);
5141 monitor_resume(mon);
5142 monitor_flush(mon);
5143 } else {
5144 mon->suspend_cnt = 0;
5145 }
aliguori2724b182009-03-05 23:01:47 +00005146 break;
ths86e94de2007-01-05 22:01:59 +00005147
aliguori2724b182009-03-05 23:01:47 +00005148 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005149 if (mon->reset_seen) {
5150 if (mon->suspend_cnt == 0) {
5151 monitor_printf(mon, "\n");
5152 }
5153 monitor_flush(mon);
5154 monitor_suspend(mon);
5155 } else {
5156 mon->suspend_cnt++;
5157 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005158 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005159 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02005160 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00005161 break;
5162
Amit Shahb6b8df52009-10-07 18:31:16 +05305163 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00005164 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5165 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005166 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03005167 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00005168 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02005169 }
5170 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04005171 mon_refcount++;
5172 break;
5173
5174 case CHR_EVENT_CLOSED:
5175 mon_refcount--;
5176 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00005177 break;
5178 }
ths86e94de2007-01-05 22:01:59 +00005179}
5180
Wayne Xia816f8922011-10-12 11:32:41 +08005181static int
5182compare_mon_cmd(const void *a, const void *b)
5183{
5184 return strcmp(((const mon_cmd_t *)a)->name,
5185 ((const mon_cmd_t *)b)->name);
5186}
5187
5188static void sortcmdlist(void)
5189{
5190 int array_num;
5191 int elem_size = sizeof(mon_cmd_t);
5192
5193 array_num = sizeof(mon_cmds)/elem_size-1;
5194 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5195
5196 array_num = sizeof(info_cmds)/elem_size-1;
5197 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5198}
5199
aliguori76655d62009-03-06 20:27:37 +00005200
5201/*
5202 * Local variables:
5203 * c-indent-level: 4
5204 * c-basic-offset: 4
5205 * tab-width: 8
5206 * End:
5207 */
5208
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005209/* These functions just adapt the readline interface in a typesafe way. We
5210 * could cast function pointers but that discards compiler checks.
5211 */
Stefan Weild5d15072014-01-25 18:18:23 +01005212static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5213 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005214{
5215 va_list ap;
5216 va_start(ap, fmt);
5217 monitor_vprintf(opaque, fmt, ap);
5218 va_end(ap);
5219}
5220
5221static void monitor_readline_flush(void *opaque)
5222{
5223 monitor_flush(opaque);
5224}
5225
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005226static void __attribute__((constructor)) monitor_lock_init(void)
5227{
5228 qemu_mutex_init(&monitor_lock);
5229}
5230
aliguori731b0362009-03-05 23:01:42 +00005231void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00005232{
aliguori731b0362009-03-05 23:01:42 +00005233 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00005234 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00005235
5236 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02005237 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08005238 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00005239 is_first_init = 0;
5240 }
aliguori87127162009-03-05 23:01:29 +00005241
Wenchao Xiab01fe892013-08-27 20:38:19 +08005242 mon = g_malloc(sizeof(*mon));
5243 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00005244
aliguori87127162009-03-05 23:01:29 +00005245 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00005246 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00005247 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005248 mon->rs = readline_init(monitor_readline_printf,
5249 monitor_readline_flush,
5250 mon,
5251 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00005252 monitor_read_command(mon, 0);
5253 }
aliguori87127162009-03-05 23:01:29 +00005254
Markus Armbruster9f3982f2015-03-06 19:56:38 +01005255 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01005256 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5257 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05005258 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01005259 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02005260 } else {
5261 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5262 monitor_event, mon);
5263 }
aliguori87127162009-03-05 23:01:29 +00005264
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005265 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00005266 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02005267 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00005268}
5269
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005270static void bdrv_password_cb(void *opaque, const char *password,
5271 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00005272{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01005273 Monitor *mon = opaque;
5274 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005275 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005276 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00005277
Markus Armbruster4d2855a2015-01-29 10:37:00 +01005278 bdrv_add_key(bs, password, &local_err);
5279 if (local_err) {
5280 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5281 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00005282 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00005283 }
aliguori731b0362009-03-05 23:01:42 +00005284 if (mon->password_completion_cb)
5285 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00005286
aliguori731b0362009-03-05 23:01:42 +00005287 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00005288}
aliguoric0f4ce72009-03-05 23:01:01 +00005289
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005290int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02005291 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005292 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00005293{
aliguoricde76ee2009-03-05 23:01:51 +00005294 int err;
5295
aliguori376253e2009-03-05 23:01:23 +00005296 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5297 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00005298
aliguori731b0362009-03-05 23:01:42 +00005299 mon->password_completion_cb = completion_cb;
5300 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00005301
aliguoricde76ee2009-03-05 23:01:51 +00005302 err = monitor_read_password(mon, bdrv_password_cb, bs);
5303
5304 if (err && completion_cb)
5305 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02005306
5307 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00005308}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005309
5310int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02005311 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005312 void *opaque)
5313{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005314 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08005315 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005316
Fam Zheng55606252015-03-02 19:36:46 +08005317 blk = blk_by_name(device);
5318 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005319 monitor_printf(mon, "Device not found %s\n", device);
5320 return -1;
5321 }
5322
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01005323 bdrv_add_key(blk_bs(blk), NULL, &err);
5324 if (err) {
5325 error_free(err);
5326 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5327 }
5328
5329 if (completion_cb) {
5330 completion_cb(opaque, 0);
5331 }
5332 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02005333}
Paolo Bonzini4d454572012-11-26 16:03:42 +01005334
5335QemuOptsList qemu_mon_opts = {
5336 .name = "mon",
5337 .implied_opt_name = "chardev",
5338 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5339 .desc = {
5340 {
5341 .name = "mode",
5342 .type = QEMU_OPT_STRING,
5343 },{
5344 .name = "chardev",
5345 .type = QEMU_OPT_STRING,
5346 },{
5347 .name = "default",
5348 .type = QEMU_OPT_BOOL,
5349 },{
5350 .name = "pretty",
5351 .type = QEMU_OPT_BOOL,
5352 },
5353 { /* end of list */ }
5354 },
5355};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005356
5357#ifndef TARGET_I386
5358void qmp_rtc_reset_reinjection(Error **errp)
5359{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01005360 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03005361}
5362#endif