blob: a4417f26cde3a66b31b2fc99ef6f6a27d12ac5b8 [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 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020031#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020033#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020035#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000036#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040037#include "chardev/char-fe.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020038#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020039#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010040#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030041#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010042#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010043#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010044#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010045#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010046#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000047#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020048#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010051#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020053#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010054#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010055#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010056#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010057#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010058#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020061#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000062#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030064#include "monitor/hmp-target.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 Bonzini63c91552016-03-15 13:18:37 +010069#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010070#include "qemu/log.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010071#include "qemu/option.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050072#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010075#include "qapi/qapi-commands.h"
76#include "qapi/qapi-events.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010077#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020078#include "qapi/qmp-event.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010079#include "qapi/qapi-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020080#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010081#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020082#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000083
Jason J. Hernea4538a52015-06-26 14:07:21 -040084#if defined(TARGET_S390X)
85#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020086#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040087#endif
88
bellard9307c4c2004-04-04 12:57:25 +000089/*
90 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000091 *
bellard9307c4c2004-04-04 12:57:25 +000092 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000093 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000094 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080095 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010096 * 'O' option string of the form NAME=VALUE,...
97 * parsed according to QemuOptsList given by its name
98 * Example: 'device:O' uses qemu_device_opts.
99 * Restriction: only lists with empty desc are supported
100 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000101 * 'i' 32 bit integer
102 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300103 * 'M' Non-negative target long (32 or 64 bit), in user mode the
104 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200105 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200106 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
107 * K, k suffix, which multiplies the value by 2^60 for suffixes E
108 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
109 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100110 * 'T' double
111 * user mode accepts an optional ms, us, ns suffix,
112 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000113 * '/' optional gdb-like print format (like "/10x")
114 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300115 * '?' optional type (for all types, except '/')
116 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100117 * 'b' boolean
118 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300119 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000120 *
121 */
122
Anthony Liguoric227f092009-10-01 16:12:16 -0500123typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000125 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000126 const char *params;
127 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400128 void (*cmd)(Monitor *mon, const QDict *qdict);
129 /* @sub_table is a list of 2nd level of commands. If it does not exist,
130 * cmd should be used. If it exists, sub_table[?].cmd should be
131 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800132 */
133 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100134 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500135} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000136
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500138typedef struct mon_fd_t mon_fd_t;
139struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140 char *name;
141 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143};
144
Corey Bryantba1c0482012-08-14 16:43:43 -0400145/* file descriptor associated with a file descriptor set */
146typedef struct MonFdsetFd MonFdsetFd;
147struct MonFdsetFd {
148 int fd;
149 bool removed;
150 char *opaque;
151 QLIST_ENTRY(MonFdsetFd) next;
152};
153
154/* file descriptor set containing fds passed via SCM_RIGHTS */
155typedef struct MonFdset MonFdset;
156struct MonFdset {
157 int64_t id;
158 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400159 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400160 QLIST_ENTRY(MonFdset) next;
161};
162
Markus Armbruster74358f22015-03-06 19:35:59 +0100163typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200164 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100165 /*
166 * When a client connects, we're in capabilities negotiation mode.
167 * When command qmp_capabilities succeeds, we go into command
168 * mode.
169 */
Markus Armbruster635db182017-03-03 13:32:27 +0100170 QmpCommandList *commands;
Markus Armbruster74358f22015-03-06 19:35:59 +0100171} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200172
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100173/*
174 * To prevent flooding clients, events can be throttled. The
175 * throttling is calculated globally, rather than per-Monitor
176 * instance.
177 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200178typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200179 QAPIEvent event; /* Throttling state for this event type and... */
180 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100181 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200182 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200183} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100184
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200185typedef struct {
186 int64_t rate; /* Minimum time (in ns) between two events */
187} MonitorQAPIEventConf;
188
aliguori87127162009-03-05 23:01:29 +0000189struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300190 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200191 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000192 int flags;
193 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400194 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200195
196 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400197 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200198 guint out_watch;
199
200 /* Read under either BQL or out_lock, written with BQL+out_lock. */
201 int mux_out;
202
aliguori731b0362009-03-05 23:01:42 +0000203 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100204 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200205 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200206 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000207 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800208 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500209 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000210 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000211};
212
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300213/* QMP checker flags */
214#define QMP_ACCEPT_UNKNOWNS 1
215
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200216/* Protects mon_list, monitor_event_state. */
217static QemuMutex monitor_lock;
218
Blue Swirl72cf2d42009-09-12 07:36:22 +0000219static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400220static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400221static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000222
Wayne Xia816f8922011-10-12 11:32:41 +0800223static mon_cmd_t mon_cmds[];
224static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000225
Markus Armbruster635db182017-03-03 13:32:27 +0100226QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100227
Markus Armbruster8631b602010-02-18 11:41:55 +0100228Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000229
Alberto Garciadc599972016-03-10 13:55:26 +0200230static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
231
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100232static void monitor_command_cb(void *opaque, const char *cmdline,
233 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000234
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100235/**
236 * Is @mon a QMP monitor?
237 */
238static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200239{
240 return (mon->flags & MONITOR_USE_CONTROL);
241}
242
Markus Armbruster489653b2015-03-06 20:01:05 +0100243/**
244 * Is the current monitor, if any, a QMP monitor?
245 */
246bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100247{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100248 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100249}
250
Anthony Liguori7060b472011-09-02 12:34:50 -0500251void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000252{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200253 if (!mon->rs)
254 return;
255
aliguori731b0362009-03-05 23:01:42 +0000256 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
257 if (show_prompt)
258 readline_show_prompt(mon->rs);
259}
bellard6a00d602005-11-21 23:25:50 +0000260
Anthony Liguori7060b472011-09-02 12:34:50 -0500261int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
262 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000263{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100264 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000265 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
266 /* prompt is printed on return from the command handler */
267 return 0;
268 } else {
269 monitor_printf(mon, "terminal does not support password prompting\n");
270 return -ENOTTY;
271 }
aliguoribb5fc202009-03-05 23:01:15 +0000272}
273
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200274static void monitor_flush_locked(Monitor *mon);
275
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100276static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
277 void *opaque)
278{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200279 Monitor *mon = opaque;
280
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200281 qemu_mutex_lock(&mon->out_lock);
282 mon->out_watch = 0;
283 monitor_flush_locked(mon);
284 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100285 return FALSE;
286}
287
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200288/* Called with mon->out_lock held. */
289static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000290{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400292 size_t len;
293 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100294
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400295 if (mon->skip_flush) {
296 return;
297 }
298
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 buf = qstring_get_str(mon->outbuf);
300 len = qstring_get_length(mon->outbuf);
301
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200302 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300303 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200304 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
305 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 QDECREF(mon->outbuf);
307 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100308 return;
309 }
310 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600311 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400312 QString *tmp = qstring_from_str(buf + rc);
313 QDECREF(mon->outbuf);
314 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100315 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200316 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300317 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300318 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300319 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200320 }
bellard7e2515e2004-08-01 21:52:19 +0000321 }
322}
323
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200324void monitor_flush(Monitor *mon)
325{
326 qemu_mutex_lock(&mon->out_lock);
327 monitor_flush_locked(mon);
328 qemu_mutex_unlock(&mon->out_lock);
329}
330
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400331/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000332static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000333{
ths60fe76f2007-12-16 03:02:09 +0000334 char c;
aliguori731b0362009-03-05 23:01:42 +0000335
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200336 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000337 for(;;) {
338 c = *str++;
339 if (c == '\0')
340 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400341 if (c == '\n') {
342 qstring_append_chr(mon->outbuf, '\r');
343 }
344 qstring_append_chr(mon->outbuf, c);
345 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200346 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400347 }
bellard7e2515e2004-08-01 21:52:19 +0000348 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200349 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000350}
351
aliguori376253e2009-03-05 23:01:23 +0000352void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000353{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400354 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200355
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200356 if (!mon)
357 return;
358
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100359 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200361 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400363 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200364 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400365 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000366}
367
aliguori376253e2009-03-05 23:01:23 +0000368void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000369{
370 va_list ap;
371 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000372 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000373 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000374}
375
Pavel Butsykincaf15312015-09-22 16:18:17 +0300376int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000377{
378 va_list ap;
379 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000380 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000381 va_end(ap);
382 return 0;
383}
384
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200385static void monitor_json_emitter(Monitor *mon, const QObject *data)
386{
387 QString *json;
388
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200389 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
390 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 assert(json != NULL);
392
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200393 qstring_append_chr(json, '\n');
394 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200395
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396 QDECREF(json);
397}
398
Eric Blake7fb1cf12015-11-18 01:52:57 -0700399static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200400 /* Limit guest-triggerable events to 1 per second */
401 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
404 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
405 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
406 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
407};
408
Markus Armbrustera24712a2015-10-15 17:08:34 +0200409GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100410
411/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200412 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200413 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100414 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200415static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100416{
417 Monitor *mon;
418
Markus Armbruster688b4b72015-10-15 17:08:30 +0200419 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100420 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100421 if (monitor_is_qmp(mon)
422 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200423 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100424 }
425 }
426}
427
Markus Armbrustera24712a2015-10-15 17:08:34 +0200428static void monitor_qapi_event_handler(void *opaque);
429
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100430/*
431 * Queue a new event for emission to Monitor instances,
432 * applying any rate limiting if required.
433 */
434static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200435monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100436{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200437 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200438 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200439
Eric Blake7fb1cf12015-11-18 01:52:57 -0700440 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200441 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200442 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100443
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200444 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200445
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200446 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200447 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200448 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100449 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200450 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
451 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200452
453 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
454 assert(!evstate || timer_pending(evstate->timer));
455
456 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200457 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200458 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200459 * last send. Store event for sending when timer fires,
460 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100461 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200462 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200463 evstate->qdict = qdict;
464 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200466 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200467 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200468 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200469 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200470 * events arriving before then will be delayed until then.
471 */
Alberto Garciadc599972016-03-10 13:55:26 +0200472 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200473
Markus Armbruster688b4b72015-10-15 17:08:30 +0200474 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200475
476 evstate = g_new(MonitorQAPIEventState, 1);
477 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200478 evstate->data = data;
479 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200480 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200481 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200482 monitor_qapi_event_handler,
483 evstate);
484 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200485 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100486 }
487 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200488
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200489 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100490}
491
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100492/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200493 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200494 * event.
495 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200497static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100498{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200499 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200500 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100501
Markus Armbruster93f8f982015-10-15 17:08:31 +0200502 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200503 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200504
Markus Armbruster688b4b72015-10-15 17:08:30 +0200505 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200506 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200507
Markus Armbruster688b4b72015-10-15 17:08:30 +0200508 monitor_qapi_event_emit(evstate->event, evstate->qdict);
509 QDECREF(evstate->qdict);
510 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200511 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200512 } else {
513 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200514 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200515 timer_free(evstate->timer);
516 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100517 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200518
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200519 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100520}
521
Markus Armbrustera24712a2015-10-15 17:08:34 +0200522static unsigned int qapi_event_throttle_hash(const void *key)
523{
524 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200525 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200526
Markus Armbruster7de0be62015-10-15 17:08:35 +0200527 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
528 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
529 }
530
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200531 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
532 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
533 }
534
Markus Armbruster7de0be62015-10-15 17:08:35 +0200535 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200536}
537
538static gboolean qapi_event_throttle_equal(const void *a, const void *b)
539{
540 const MonitorQAPIEventState *eva = a;
541 const MonitorQAPIEventState *evb = b;
542
Markus Armbruster7de0be62015-10-15 17:08:35 +0200543 if (eva->event != evb->event) {
544 return FALSE;
545 }
546
547 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
548 return !strcmp(qdict_get_str(eva->data, "id"),
549 qdict_get_str(evb->data, "id"));
550 }
551
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200552 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
553 return !strcmp(qdict_get_str(eva->data, "node-name"),
554 qdict_get_str(evb->data, "node-name"));
555 }
556
Markus Armbruster7de0be62015-10-15 17:08:35 +0200557 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200558}
559
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200560static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100561{
Alberto Garciadc599972016-03-10 13:55:26 +0200562 if (qtest_enabled()) {
563 event_clock_type = QEMU_CLOCK_VIRTUAL;
564 }
565
Markus Armbrustera24712a2015-10-15 17:08:34 +0200566 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
567 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200568 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200569}
570
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100571static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200572
Wenchao Xiab01fe892013-08-27 20:38:19 +0800573static void monitor_data_init(Monitor *mon)
574{
575 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200576 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800578 /* Use *mon_cmds by default. */
579 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800580}
581
582static void monitor_data_destroy(Monitor *mon)
583{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200584 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400585 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400586 if (monitor_is_qmp(mon)) {
587 json_message_parser_destroy(&mon->qmp.parser);
588 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100589 readline_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200591 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800592}
593
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200594char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
595 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200597 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200599
Wenchao Xiab01fe892013-08-27 20:38:19 +0800600 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400601 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200602
603 old_mon = cur_mon;
604 cur_mon = &hmp;
605
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200606 if (has_cpu_index) {
607 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608 if (ret < 0) {
609 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100610 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
611 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200612 goto out;
613 }
614 }
615
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100616 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200617 cur_mon = old_mon;
618
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200619 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400620 if (qstring_get_length(hmp.outbuf) > 0) {
621 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200622 } else {
623 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200625 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626
627out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800628 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200629 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200630}
631
bellard9dc39cb2004-03-14 21:38:27 +0000632static int compare_cmd(const char *name, const char *list)
633{
634 const char *p, *pstart;
635 int len;
636 len = strlen(name);
637 p = list;
638 for(;;) {
639 pstart = p;
640 p = strchr(p, '|');
641 if (!p)
642 p = pstart + strlen(pstart);
643 if ((p - pstart) == len && !memcmp(pstart, name, len))
644 return 1;
645 if (*p == '\0')
646 break;
647 p++;
648 }
649 return 0;
650}
651
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800652static int get_str(char *buf, int buf_size, const char **pp)
653{
654 const char *p;
655 char *q;
656 int c;
657
658 q = buf;
659 p = *pp;
660 while (qemu_isspace(*p)) {
661 p++;
662 }
663 if (*p == '\0') {
664 fail:
665 *q = '\0';
666 *pp = p;
667 return -1;
668 }
669 if (*p == '\"') {
670 p++;
671 while (*p != '\0' && *p != '\"') {
672 if (*p == '\\') {
673 p++;
674 c = *p++;
675 switch (c) {
676 case 'n':
677 c = '\n';
678 break;
679 case 'r':
680 c = '\r';
681 break;
682 case '\\':
683 case '\'':
684 case '\"':
685 break;
686 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100687 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800688 goto fail;
689 }
690 if ((q - buf) < buf_size - 1) {
691 *q++ = c;
692 }
693 } else {
694 if ((q - buf) < buf_size - 1) {
695 *q++ = *p;
696 }
697 p++;
698 }
699 }
700 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100701 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800702 goto fail;
703 }
704 p++;
705 } else {
706 while (*p != '\0' && !qemu_isspace(*p)) {
707 if ((q - buf) < buf_size - 1) {
708 *q++ = *p;
709 }
710 p++;
711 }
712 }
713 *q = '\0';
714 *pp = p;
715 return 0;
716}
717
718#define MAX_ARGS 16
719
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800720static void free_cmdline_args(char **args, int nb_args)
721{
722 int i;
723
724 assert(nb_args <= MAX_ARGS);
725
726 for (i = 0; i < nb_args; i++) {
727 g_free(args[i]);
728 }
729
730}
731
732/*
733 * Parse the command line to get valid args.
734 * @cmdline: command line to be parsed.
735 * @pnb_args: location to store the number of args, must NOT be NULL.
736 * @args: location to store the args, which should be freed by caller, must
737 * NOT be NULL.
738 *
739 * Returns 0 on success, negative on failure.
740 *
741 * NOTE: this parser is an approximate form of the real command parser. Number
742 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
743 * return with failure.
744 */
745static int parse_cmdline(const char *cmdline,
746 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800747{
748 const char *p;
749 int nb_args, ret;
750 char buf[1024];
751
752 p = cmdline;
753 nb_args = 0;
754 for (;;) {
755 while (qemu_isspace(*p)) {
756 p++;
757 }
758 if (*p == '\0') {
759 break;
760 }
761 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800762 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800763 }
764 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800765 if (ret < 0) {
766 goto fail;
767 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 args[nb_args] = g_strdup(buf);
769 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800770 }
771 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800772 return 0;
773
774 fail:
775 free_cmdline_args(args, nb_args);
776 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800777}
778
Wenchao Xia66855492013-08-27 20:38:23 +0800779static void help_cmd_dump_one(Monitor *mon,
780 const mon_cmd_t *cmd,
781 char **prefix_args,
782 int prefix_args_nb)
783{
784 int i;
785
786 for (i = 0; i < prefix_args_nb; i++) {
787 monitor_printf(mon, "%s ", prefix_args[i]);
788 }
789 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
790}
791
792/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500793static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800794 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000795{
Anthony Liguoric227f092009-10-01 16:12:16 -0500796 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000797
Wenchao Xia66855492013-08-27 20:38:23 +0800798 /* No valid arg need to compare with, dump all in *cmds */
799 if (arg_index >= nb_args) {
800 for (cmd = cmds; cmd->name != NULL; cmd++) {
801 help_cmd_dump_one(mon, cmd, args, arg_index);
802 }
803 return;
804 }
805
806 /* Find one entry to dump */
807 for (cmd = cmds; cmd->name != NULL; cmd++) {
808 if (compare_cmd(args[arg_index], cmd->name)) {
809 if (cmd->sub_table) {
810 /* continue with next arg */
811 help_cmd_dump(mon, cmd->sub_table,
812 args, nb_args, arg_index + 1);
813 } else {
814 help_cmd_dump_one(mon, cmd, args, arg_index);
815 }
816 break;
817 }
bellard9dc39cb2004-03-14 21:38:27 +0000818 }
819}
820
aliguori376253e2009-03-05 23:01:23 +0000821static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000822{
Wenchao Xia66855492013-08-27 20:38:23 +0800823 char *args[MAX_ARGS];
824 int nb_args = 0;
825
826 /* 1. parse user input */
827 if (name) {
828 /* special case for log, directly dump and return */
829 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000830 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "Log items (comma separated):\n");
832 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000833 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000834 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000835 }
Wenchao Xia66855492013-08-27 20:38:23 +0800836 return;
837 }
838
839 if (parse_cmdline(name, &nb_args, args) < 0) {
840 return;
bellardf193c792004-03-21 17:06:25 +0000841 }
bellard9dc39cb2004-03-14 21:38:27 +0000842 }
Wenchao Xia66855492013-08-27 20:38:23 +0800843
844 /* 2. dump the contents according to parsed args */
845 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
846
847 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000848}
849
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300851{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300852 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300853}
854
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100855static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530856{
857 const char *tp_name = qdict_get_str(qdict, "name");
858 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200859 bool has_vcpu = qdict_haskey(qdict, "vcpu");
860 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200861 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000862
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200863 if (vcpu < 0) {
864 monitor_printf(mon, "argument vcpu must be positive");
865 return;
866 }
867
868 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200869 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100870 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000871 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530872}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100873
Michael Rothc45a8162011-10-02 08:44:37 -0500874#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100875static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100876{
877 const char *op = qdict_get_try_str(qdict, "op");
878 const char *arg = qdict_get_try_str(qdict, "arg");
879
880 if (!op) {
881 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
882 } else if (!strcmp(op, "on")) {
883 st_set_trace_file_enabled(true);
884 } else if (!strcmp(op, "off")) {
885 st_set_trace_file_enabled(false);
886 } else if (!strcmp(op, "flush")) {
887 st_flush_trace_buffer();
888 } else if (!strcmp(op, "set")) {
889 if (arg) {
890 st_set_trace_file(arg);
891 }
892 } else {
893 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
894 help_cmd(mon, "trace-file");
895 }
896}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530897#endif
898
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100899static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000900{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300901 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000902}
903
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400904static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000905{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400906 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400908 if (!cmd->enabled) {
909 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200910 }
911
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400912 info = g_malloc0(sizeof(*info));
913 info->value = g_malloc0(sizeof(*info->value));
914 info->value->name = g_strdup(cmd->name);
915 info->next = *list;
916 *list = info;
917}
918
919CommandInfoList *qmp_query_commands(Error **errp)
920{
921 CommandInfoList *list = NULL;
922
Markus Armbruster635db182017-03-03 13:32:27 +0100923 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400924
925 return list;
thsa36e69d2007-12-02 05:18:19 +0000926}
927
Daniel P. Berrange48608532012-05-21 17:59:51 +0100928EventInfoList *qmp_query_events(Error **errp)
929{
930 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200931 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100932
Eric Blake7fb1cf12015-11-18 01:52:57 -0700933 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +0200934 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +0100935 assert(event_name != NULL);
936 info = g_malloc0(sizeof(*info));
937 info->value = g_malloc0(sizeof(*info->value));
938 info->value->name = g_strdup(event_name);
939
940 info->next = ev_list;
941 ev_list = info;
942 }
943
944 return ev_list;
945}
946
Markus Armbruster39a18152015-09-16 13:06:28 +0200947/*
948 * Minor hack: generated marshalling suppressed for this command
949 * ('gen': false in the schema) so we can parse the JSON string
950 * directly into QObject instead of first parsing it with
951 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
952 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100953 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -0600954 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +0200955 */
956static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
957 Error **errp)
958{
Markus Armbrusterb0f36b22017-02-28 22:27:02 +0100959 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200960}
961
Marc-André Lureau5032a162016-09-12 13:19:02 +0400962/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400963 * We used to define commands in qmp-commands.hx in addition to the
964 * QAPI schema. This permitted defining some of them only in certain
965 * configurations. query-commands has always reflected that (good,
966 * because it lets QMP clients figure out what's actually available),
967 * while query-qmp-schema never did (not so good). This function is a
968 * hack to keep the configuration-specific commands defined exactly as
969 * before, even though qmp-commands.hx is gone.
970 *
971 * FIXME Educate the QAPI schema on configuration-specific commands,
972 * and drop this hack.
973 */
974static void qmp_unregister_commands_hack(void)
975{
976#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +0100977 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400978#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +0200979#ifndef CONFIG_REPLICATION
980 qmp_unregister_command(&qmp_commands, "xen-set-replication");
981 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
982 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
983#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400984#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +0100985 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400986#endif
987#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +0100988 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400989#endif
990#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +0100991 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400992#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300993#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100994 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300995#endif
996#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100997 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
998 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300999#endif
1000#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1001 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001002 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001003#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001004}
1005
Markus Armbruster05875682017-03-03 13:32:24 +01001006void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001007{
Markus Armbruster635db182017-03-03 13:32:27 +01001008 /*
1009 * Two command lists:
1010 * - qmp_commands contains all QMP commands
1011 * - qmp_cap_negotiation_commands contains just
1012 * "qmp_capabilities", to enforce capability negotiation
1013 */
1014
Markus Armbruster1527bad2017-03-03 13:32:25 +01001015 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001016
Markus Armbruster1527bad2017-03-03 13:32:25 +01001017 qmp_register_command(&qmp_commands, "query-qmp-schema",
1018 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001019 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001020 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001021 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001022 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001023 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001024
Markus Armbruster05875682017-03-03 13:32:24 +01001025 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001026
1027 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1028 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1029 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1030}
1031
1032void qmp_qmp_capabilities(Error **errp)
1033{
1034 if (cur_mon->qmp.commands == &qmp_commands) {
1035 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1036 "Capabilities negotiation is already complete, command "
1037 "ignored");
1038 return;
1039 }
1040
1041 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001042}
1043
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001044/* set the current CPU defined by the user */
1045int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001046{
Andreas Färber55e5c282012-12-17 06:18:02 +01001047 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001048
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001049 cpu = qemu_get_cpu(cpu_index);
1050 if (cpu == NULL) {
1051 return -1;
bellard6a00d602005-11-21 23:25:50 +00001052 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001053 g_free(cur_mon->mon_cpu_path);
1054 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001055 return 0;
bellard6a00d602005-11-21 23:25:50 +00001056}
1057
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001058static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001059{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001060 CPUState *cpu;
1061
1062 if (cur_mon->mon_cpu_path) {
1063 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1064 TYPE_CPU, NULL);
1065 if (!cpu) {
1066 g_free(cur_mon->mon_cpu_path);
1067 cur_mon->mon_cpu_path = NULL;
1068 }
1069 }
1070 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001071 if (!first_cpu) {
1072 return NULL;
1073 }
David Gibson27a83f82016-09-21 15:29:26 +10001074 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001075 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001076 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001077 if (synchronize) {
1078 cpu_synchronize_state(cpu);
1079 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001080 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001081}
1082
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001083CPUState *mon_get_cpu(void)
1084{
1085 return mon_get_cpu_sync(true);
1086}
1087
Pavel Butsykinbf957282015-09-10 18:38:59 +03001088CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001089{
Thomas Huth854e67f2017-01-13 13:12:35 +01001090 CPUState *cs = mon_get_cpu();
1091
1092 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001093}
1094
Luiz Capitulino99b77962011-10-24 10:53:44 -02001095int monitor_get_cpu_index(void)
1096{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001097 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001098
1099 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001100}
1101
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001102static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001103{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001104 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1105 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001106
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001107 if (all_cpus) {
1108 CPU_FOREACH(cs) {
1109 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1110 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1111 }
1112 } else {
1113 cs = mon_get_cpu();
1114
1115 if (!cs) {
1116 monitor_printf(mon, "No CPU available\n");
1117 return;
1118 }
1119
1120 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001121 }
bellard9307c4c2004-04-04 12:57:25 +00001122}
1123
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001124#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001125static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001126{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001127 if (!tcg_enabled()) {
1128 error_report("JIT information is only available with accel=tcg");
1129 return;
1130 }
1131
aliguori376253e2009-03-05 23:01:23 +00001132 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001133 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001134}
1135
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001136static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001137{
1138 dump_opcount_info((FILE *)mon, monitor_fprintf);
1139}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001140#endif
Max Filippov246ae242014-11-02 11:04:18 +03001141
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001142static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001143{
1144 int i;
bellard7e2515e2004-08-01 21:52:19 +00001145 const char *str;
ths3b46e622007-09-17 08:09:54 +00001146
aliguoricde76ee2009-03-05 23:01:51 +00001147 if (!mon->rs)
1148 return;
bellard7e2515e2004-08-01 21:52:19 +00001149 i = 0;
1150 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001151 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001152 if (!str)
1153 break;
aliguori376253e2009-03-05 23:01:23 +00001154 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001155 i++;
bellardaa455482004-04-04 13:07:25 +00001156 }
1157}
1158
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001159static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001160{
Thomas Huth854e67f2017-01-13 13:12:35 +01001161 CPUState *cs = mon_get_cpu();
1162
1163 if (!cs) {
1164 monitor_printf(mon, "No CPU available\n");
1165 return;
1166 }
1167 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001168}
j_mayer76a66252007-03-07 08:32:30 +00001169
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001170static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301171{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001172 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001173 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1174 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001175 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001176 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001177 Error *local_err = NULL;
1178
1179 if (name == NULL) {
1180 name = "*";
1181 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001182 if (vcpu < 0) {
1183 monitor_printf(mon, "argument vcpu must be positive");
1184 return;
1185 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001186
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001187 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001188 if (local_err) {
1189 error_report_err(local_err);
1190 return;
1191 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001192
1193 for (elem = events; elem != NULL; elem = elem->next) {
1194 monitor_printf(mon, "%s : state %u\n",
1195 elem->value->name,
1196 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1197 }
1198 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301199}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301200
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001201void qmp_client_migrate_info(const char *protocol, const char *hostname,
1202 bool has_port, int64_t port,
1203 bool has_tls_port, int64_t tls_port,
1204 bool has_cert_subject, const char *cert_subject,
1205 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001206{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001207 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001208 if (!qemu_using_spice(errp)) {
1209 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001210 }
1211
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001212 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001213 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001214 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001215 }
1216
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001217 if (qemu_spice_migrate_info(hostname,
1218 has_port ? port : -1,
1219 has_tls_port ? tls_port : -1,
1220 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001221 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001222 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001223 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001224 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001225 }
1226
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001227 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001228}
1229
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001230static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001231{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001232 Error *err = NULL;
1233
1234 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1235 if (err) {
1236 error_report_err(err);
1237 }
pbrooke735b912007-06-30 13:53:24 +00001238}
1239
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001240static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001241{
1242 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001243 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001244
bellard9307c4c2004-04-04 12:57:25 +00001245 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001246 mask = 0;
1247 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001248 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001249 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001250 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001251 return;
1252 }
1253 }
Peter Maydell24537a02013-02-11 16:41:23 +00001254 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001255}
1256
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001257static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001258{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001259 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001260 if (!option || !strcmp(option, "on")) {
1261 singlestep = 1;
1262 } else if (!strcmp(option, "off")) {
1263 singlestep = 0;
1264 } else {
1265 monitor_printf(mon, "unexpected option %s\n", option);
1266 }
1267}
1268
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001269static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001270{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001271 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001272 if (!device)
1273 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1274 if (gdbserver_start(device) < 0) {
1275 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1276 device);
1277 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001278 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001279 } else {
aliguori59030a82009-04-05 18:43:41 +00001280 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1281 device);
bellard8a7ddc32004-03-31 19:00:16 +00001282 }
1283}
1284
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001285static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001286{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001287 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001288 if (select_watchdog_action(action) == -1) {
1289 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1290 }
1291}
1292
aliguori376253e2009-03-05 23:01:23 +00001293static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001294{
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001296 switch(c) {
1297 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001302 break;
1303 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001305 break;
1306 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001308 break;
1309 default:
1310 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001312 } else {
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001314 }
1315 break;
1316 }
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001318}
1319
aliguori376253e2009-03-05 23:01:23 +00001320static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001321 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001322{
Blue Swirl23842aa2010-01-12 20:27:43 +00001323 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001324 uint8_t buf[16];
1325 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001326 CPUState *cs = mon_get_cpu();
1327
1328 if (!cs && (format == 'i' || !is_physical)) {
1329 monitor_printf(mon, "Can not dump without CPU\n");
1330 return;
1331 }
bellard9307c4c2004-04-04 12:57:25 +00001332
1333 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001334 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001335 return;
1336 }
1337
1338 len = wsize * count;
1339 if (wsize == 1)
1340 line_size = 8;
1341 else
1342 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001343 max_digits = 0;
1344
1345 switch(format) {
1346 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001347 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 default:
1350 case 'x':
1351 max_digits = (wsize * 8) / 4;
1352 break;
1353 case 'u':
1354 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001355 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001356 break;
1357 case 'c':
1358 wsize = 1;
1359 break;
1360 }
1361
1362 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001363 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001365 else
aliguori376253e2009-03-05 23:01:23 +00001366 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001367 l = len;
1368 if (l > line_size)
1369 l = line_size;
1370 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001371 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001372 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001373 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001375 break;
1376 }
bellard9307c4c2004-04-04 12:57:25 +00001377 }
ths5fafdf22007-09-16 21:08:06 +00001378 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001379 while (i < l) {
1380 switch(wsize) {
1381 default:
1382 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001383 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001386 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001389 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001392 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 }
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001396 switch(format) {
1397 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001399 break;
1400 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001402 break;
1403 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001405 break;
1406 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001407 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001408 break;
1409 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001410 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001411 break;
1412 }
1413 i += wsize;
1414 }
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001416 addr += l;
1417 len -= l;
1418 }
1419}
1420
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001421static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001422{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001423 int count = qdict_get_int(qdict, "count");
1424 int format = qdict_get_int(qdict, "format");
1425 int size = qdict_get_int(qdict, "size");
1426 target_long addr = qdict_get_int(qdict, "addr");
1427
aliguori376253e2009-03-05 23:01:23 +00001428 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001429}
1430
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001431static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001432{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001433 int count = qdict_get_int(qdict, "count");
1434 int format = qdict_get_int(qdict, "format");
1435 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001436 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001437
aliguori376253e2009-03-05 23:01:23 +00001438 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001439}
1440
Paolo Bonzinie9628442017-04-20 15:30:58 +02001441static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1442{
1443 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1444 addr, 1);
1445
1446 if (!mrs.mr) {
1447 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1448 return NULL;
1449 }
1450
1451 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1452 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1453 memory_region_unref(mrs.mr);
1454 return NULL;
1455 }
1456
1457 *p_mr = mrs.mr;
1458 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1459}
1460
1461static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1462{
1463 hwaddr addr = qdict_get_int(qdict, "addr");
1464 Error *local_err = NULL;
1465 MemoryRegion *mr = NULL;
1466 void *ptr;
1467
1468 ptr = gpa2hva(&mr, addr, &local_err);
1469 if (local_err) {
1470 error_report_err(local_err);
1471 return;
1472 }
1473
1474 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1475 " (%s) is %p\n",
1476 addr, mr->name, ptr);
1477
1478 memory_region_unref(mr);
1479}
1480
1481#ifdef CONFIG_LINUX
1482static uint64_t vtop(void *ptr, Error **errp)
1483{
1484 uint64_t pinfo;
1485 uint64_t ret = -1;
1486 uintptr_t addr = (uintptr_t) ptr;
1487 uintptr_t pagesize = getpagesize();
1488 off_t offset = addr / pagesize * sizeof(pinfo);
1489 int fd;
1490
1491 fd = open("/proc/self/pagemap", O_RDONLY);
1492 if (fd == -1) {
1493 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1494 return -1;
1495 }
1496
1497 /* Force copy-on-write if necessary. */
1498 atomic_add((uint8_t *)ptr, 0);
1499
1500 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1501 error_setg_errno(errp, errno, "Cannot read pagemap");
1502 goto out;
1503 }
1504 if ((pinfo & (1ull << 63)) == 0) {
1505 error_setg(errp, "Page not present");
1506 goto out;
1507 }
1508 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1509
1510out:
1511 close(fd);
1512 return ret;
1513}
1514
1515static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1516{
1517 hwaddr addr = qdict_get_int(qdict, "addr");
1518 Error *local_err = NULL;
1519 MemoryRegion *mr = NULL;
1520 void *ptr;
1521 uint64_t physaddr;
1522
1523 ptr = gpa2hva(&mr, addr, &local_err);
1524 if (local_err) {
1525 error_report_err(local_err);
1526 return;
1527 }
1528
1529 physaddr = vtop(ptr, &local_err);
1530 if (local_err) {
1531 error_report_err(local_err);
1532 } else {
1533 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1534 " (%s) is 0x%" PRIx64 "\n",
1535 addr, mr->name, (uint64_t) physaddr);
1536 }
1537
1538 memory_region_unref(mr);
1539}
1540#endif
1541
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001542static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001543{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001544 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001545 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001546
bellard9307c4c2004-04-04 12:57:25 +00001547 switch(format) {
1548 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001549 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001550 break;
1551 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001552 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001553 break;
1554 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001555 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001556 break;
1557 default:
1558 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001559 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001560 break;
1561 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001562 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001563 break;
1564 }
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001566}
1567
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001568static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001569{
1570 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001571 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001572 uint32_t start = qdict_get_int(qdict, "start");
1573 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001574
1575 sum = 0;
1576 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001577 uint8_t val = address_space_ldub(&address_space_memory, addr,
1578 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001579 /* BSD sum algorithm ('sum' Unix command) */
1580 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001581 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001582 }
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001584}
1585
bellard13224a82006-07-14 22:03:35 +00001586static int mouse_button_state;
1587
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001588static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001589{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001590 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001591 const char *dx_str = qdict_get_str(qdict, "dx_str");
1592 const char *dy_str = qdict_get_str(qdict, "dy_str");
1593 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001594
bellard13224a82006-07-14 22:03:35 +00001595 dx = strtol(dx_str, NULL, 0);
1596 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001597 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1598 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1599
1600 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001601 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001602 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001603 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001604 qemu_input_queue_btn(NULL, button, true);
1605 qemu_input_event_sync();
1606 qemu_input_queue_btn(NULL, button, false);
1607 }
1608 }
1609 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001610}
1611
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001612static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001613{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001614 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001615 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1616 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1617 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1618 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001619 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001620
1621 if (mouse_button_state == button_state) {
1622 return;
1623 }
1624 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1625 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001626 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001627}
1628
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001629static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001630{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001631 int size = qdict_get_int(qdict, "size");
1632 int addr = qdict_get_int(qdict, "addr");
1633 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001634 uint32_t val;
1635 int suffix;
1636
1637 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001638 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001639 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001640 addr++;
1641 }
1642 addr &= 0xffff;
1643
1644 switch(size) {
1645 default:
1646 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001647 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001648 suffix = 'b';
1649 break;
1650 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001651 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001652 suffix = 'w';
1653 break;
1654 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001655 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001656 suffix = 'l';
1657 break;
1658 }
aliguori376253e2009-03-05 23:01:23 +00001659 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1660 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001661}
bellarda3a91a32004-06-04 11:06:21 +00001662
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001663static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001664{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001665 int size = qdict_get_int(qdict, "size");
1666 int addr = qdict_get_int(qdict, "addr");
1667 int val = qdict_get_int(qdict, "val");
1668
Jan Kiszkaf1147842009-07-14 10:20:11 +02001669 addr &= IOPORTS_MASK;
1670
1671 switch (size) {
1672 default:
1673 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001674 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001675 break;
1676 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001677 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001678 break;
1679 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001680 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001681 break;
1682 }
1683}
1684
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001685static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001686{
Gongleif1839932014-12-03 18:20:58 +00001687 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001688 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001689
Gongleif1839932014-12-03 18:20:58 +00001690 qemu_boot_set(bootdevice, &local_err);
1691 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001692 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001693 } else {
Gongleif1839932014-12-03 18:20:58 +00001694 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001695 }
1696}
1697
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001698static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001699{
Peter Xu57bb40c2017-01-16 16:40:05 +08001700 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001701 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001702
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001703 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001704}
1705
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001706static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001707{
aliguorib28b6232009-04-22 20:20:29 +00001708 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001709 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001710 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001711
Igor Mammedovf75cd442017-05-30 18:23:59 +02001712 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001713 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1714
zhanghailiang5b009e42014-11-04 19:49:30 +08001715 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001716 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1717 for (i = 0; i < nb_numa_nodes; i++) {
1718 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001719 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1720 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1721 cpu->value->props->node_id == i) {
1722 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001723 }
1724 }
1725 monitor_printf(mon, "\n");
1726 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001727 node_mem[i].node_mem >> 20);
1728 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1729 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00001730 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001731 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001732 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001733}
1734
bellard5f1ce942006-02-08 22:40:15 +00001735#ifdef CONFIG_PROFILER
1736
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001737int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001738int64_t dev_time;
1739
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001740static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001741{
aliguori376253e2009-03-05 23:01:23 +00001742 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301743 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001744 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301745 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001746 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001747 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001748}
1749#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001750static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001751{
aliguori376253e2009-03-05 23:01:23 +00001752 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001753}
1754#endif
1755
bellardec36b692006-07-16 18:57:03 +00001756/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001757static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001758
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001759static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001760{
1761 int i;
1762 CaptureState *s;
1763
1764 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001765 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001766 s->ops.info (s->opaque);
1767 }
1768}
1769
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001770static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001771{
1772 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001773 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001774 CaptureState *s;
1775
1776 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1777 if (i == n) {
1778 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001779 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001780 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001781 return;
1782 }
1783 }
1784}
1785
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001786static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001787{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001788 const char *path = qdict_get_str(qdict, "path");
1789 int has_freq = qdict_haskey(qdict, "freq");
1790 int freq = qdict_get_try_int(qdict, "freq", -1);
1791 int has_bits = qdict_haskey(qdict, "bits");
1792 int bits = qdict_get_try_int(qdict, "bits", -1);
1793 int has_channels = qdict_haskey(qdict, "nchannels");
1794 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001795 CaptureState *s;
1796
Anthony Liguori7267c092011-08-20 22:09:37 -05001797 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001798
1799 freq = has_freq ? freq : 44100;
1800 bits = has_bits ? bits : 16;
1801 nchannels = has_channels ? nchannels : 2;
1802
1803 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001804 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001805 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001806 return;
bellardec36b692006-07-16 18:57:03 +00001807 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001808 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001809}
bellardec36b692006-07-16 18:57:03 +00001810
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001811static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001812{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001813 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001814
aliguori76655d62009-03-06 20:27:37 +00001815 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001816 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001817 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001818 return acl;
1819}
aliguori76655d62009-03-06 20:27:37 +00001820
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001821static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001822{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001823 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001824 qemu_acl *acl = find_acl(mon, aclname);
1825 qemu_acl_entry *entry;
1826 int i = 0;
1827
1828 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001829 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001830 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001831 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001832 i++;
1833 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001834 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001835 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001836 }
1837}
1838
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001839static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001840{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001841 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001842 qemu_acl *acl = find_acl(mon, aclname);
1843
1844 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001845 qemu_acl_reset(acl);
1846 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001847 }
1848}
aliguori76655d62009-03-06 20:27:37 +00001849
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001850static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001851{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001852 const char *aclname = qdict_get_str(qdict, "aclname");
1853 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001854 qemu_acl *acl = find_acl(mon, aclname);
1855
1856 if (acl) {
1857 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001858 acl->defaultDeny = 0;
1859 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001860 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001861 acl->defaultDeny = 1;
1862 monitor_printf(mon, "acl: policy set to 'deny'\n");
1863 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001864 monitor_printf(mon, "acl: unknown policy '%s', "
1865 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001866 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001867 }
1868}
aliguori76655d62009-03-06 20:27:37 +00001869
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001870static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001871{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001872 const char *aclname = qdict_get_str(qdict, "aclname");
1873 const char *match = qdict_get_str(qdict, "match");
1874 const char *policy = qdict_get_str(qdict, "policy");
1875 int has_index = qdict_haskey(qdict, "index");
1876 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001877 qemu_acl *acl = find_acl(mon, aclname);
1878 int deny, ret;
1879
1880 if (acl) {
1881 if (strcmp(policy, "allow") == 0) {
1882 deny = 0;
1883 } else if (strcmp(policy, "deny") == 0) {
1884 deny = 1;
1885 } else {
1886 monitor_printf(mon, "acl: unknown policy '%s', "
1887 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001888 return;
1889 }
aliguori28a76be2009-03-06 20:27:40 +00001890 if (has_index)
1891 ret = qemu_acl_insert(acl, deny, match, index);
1892 else
1893 ret = qemu_acl_append(acl, deny, match);
1894 if (ret < 0)
1895 monitor_printf(mon, "acl: unable to add acl entry\n");
1896 else
1897 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001898 }
1899}
aliguori76655d62009-03-06 20:27:37 +00001900
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001901static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001902{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001903 const char *aclname = qdict_get_str(qdict, "aclname");
1904 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001905 qemu_acl *acl = find_acl(mon, aclname);
1906 int ret;
aliguori76655d62009-03-06 20:27:37 +00001907
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001908 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001909 ret = qemu_acl_remove(acl, match);
1910 if (ret < 0)
1911 monitor_printf(mon, "acl: no matching acl entry\n");
1912 else
1913 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001914 }
1915}
1916
Corey Bryant208c9d12012-06-22 14:36:09 -04001917void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001918{
Anthony Liguoric227f092009-10-01 16:12:16 -05001919 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001920 int fd;
1921
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001922 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001923 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001924 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001925 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001926 }
1927
1928 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001929 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001930 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1931 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001932 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001933 }
1934
Corey Bryant208c9d12012-06-22 14:36:09 -04001935 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001936 if (strcmp(monfd->name, fdname) != 0) {
1937 continue;
1938 }
1939
1940 close(monfd->fd);
1941 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001942 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001943 }
1944
Anthony Liguori7267c092011-08-20 22:09:37 -05001945 monfd = g_malloc0(sizeof(mon_fd_t));
1946 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001947 monfd->fd = fd;
1948
Corey Bryant208c9d12012-06-22 14:36:09 -04001949 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001950}
1951
Corey Bryant208c9d12012-06-22 14:36:09 -04001952void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001953{
Anthony Liguoric227f092009-10-01 16:12:16 -05001954 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001955
Corey Bryant208c9d12012-06-22 14:36:09 -04001956 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001957 if (strcmp(monfd->name, fdname) != 0) {
1958 continue;
1959 }
1960
Blue Swirl72cf2d42009-09-12 07:36:22 +00001961 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001962 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001963 g_free(monfd->name);
1964 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001965 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001966 }
1967
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001968 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001969}
1970
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001971int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001972{
Anthony Liguoric227f092009-10-01 16:12:16 -05001973 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001974
Blue Swirl72cf2d42009-09-12 07:36:22 +00001975 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001976 int fd;
1977
1978 if (strcmp(monfd->name, fdname) != 0) {
1979 continue;
1980 }
1981
1982 fd = monfd->fd;
1983
1984 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001985 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001986 g_free(monfd->name);
1987 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001988
1989 return fd;
1990 }
1991
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001992 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001993 return -1;
1994}
1995
Corey Bryantba1c0482012-08-14 16:43:43 -04001996static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1997{
1998 MonFdsetFd *mon_fdset_fd;
1999 MonFdsetFd *mon_fdset_fd_next;
2000
2001 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002002 if ((mon_fdset_fd->removed ||
2003 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2004 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002005 close(mon_fdset_fd->fd);
2006 g_free(mon_fdset_fd->opaque);
2007 QLIST_REMOVE(mon_fdset_fd, next);
2008 g_free(mon_fdset_fd);
2009 }
2010 }
2011
Corey Bryantadb696f2012-08-14 16:43:47 -04002012 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002013 QLIST_REMOVE(mon_fdset, next);
2014 g_free(mon_fdset);
2015 }
2016}
2017
Corey Bryantefb87c12012-08-14 16:43:48 -04002018static void monitor_fdsets_cleanup(void)
2019{
2020 MonFdset *mon_fdset;
2021 MonFdset *mon_fdset_next;
2022
2023 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2024 monitor_fdset_cleanup(mon_fdset);
2025 }
2026}
2027
Corey Bryantba1c0482012-08-14 16:43:43 -04002028AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2029 const char *opaque, Error **errp)
2030{
2031 int fd;
2032 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002033 AddfdInfo *fdinfo;
2034
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002035 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002036 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002037 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002038 goto error;
2039 }
2040
Corey Bryante446f702012-10-18 15:19:32 -04002041 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2042 has_opaque, opaque, errp);
2043 if (fdinfo) {
2044 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002045 }
2046
Corey Bryantba1c0482012-08-14 16:43:43 -04002047error:
2048 if (fd != -1) {
2049 close(fd);
2050 }
2051 return NULL;
2052}
2053
2054void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2055{
2056 MonFdset *mon_fdset;
2057 MonFdsetFd *mon_fdset_fd;
2058 char fd_str[60];
2059
2060 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2061 if (mon_fdset->id != fdset_id) {
2062 continue;
2063 }
2064 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2065 if (has_fd) {
2066 if (mon_fdset_fd->fd != fd) {
2067 continue;
2068 }
2069 mon_fdset_fd->removed = true;
2070 break;
2071 } else {
2072 mon_fdset_fd->removed = true;
2073 }
2074 }
2075 if (has_fd && !mon_fdset_fd) {
2076 goto error;
2077 }
2078 monitor_fdset_cleanup(mon_fdset);
2079 return;
2080 }
2081
2082error:
2083 if (has_fd) {
2084 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2085 fdset_id, fd);
2086 } else {
2087 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2088 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002089 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002090}
2091
2092FdsetInfoList *qmp_query_fdsets(Error **errp)
2093{
2094 MonFdset *mon_fdset;
2095 MonFdsetFd *mon_fdset_fd;
2096 FdsetInfoList *fdset_list = NULL;
2097
2098 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2099 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2100 FdsetFdInfoList *fdsetfd_list = NULL;
2101
2102 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2103 fdset_info->value->fdset_id = mon_fdset->id;
2104
2105 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2106 FdsetFdInfoList *fdsetfd_info;
2107
2108 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2109 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2110 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2111 if (mon_fdset_fd->opaque) {
2112 fdsetfd_info->value->has_opaque = true;
2113 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2114 } else {
2115 fdsetfd_info->value->has_opaque = false;
2116 }
2117
2118 fdsetfd_info->next = fdsetfd_list;
2119 fdsetfd_list = fdsetfd_info;
2120 }
2121
2122 fdset_info->value->fds = fdsetfd_list;
2123
2124 fdset_info->next = fdset_list;
2125 fdset_list = fdset_info;
2126 }
2127
2128 return fdset_list;
2129}
2130
Corey Bryante446f702012-10-18 15:19:32 -04002131AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2132 bool has_opaque, const char *opaque,
2133 Error **errp)
2134{
2135 MonFdset *mon_fdset = NULL;
2136 MonFdsetFd *mon_fdset_fd;
2137 AddfdInfo *fdinfo;
2138
2139 if (has_fdset_id) {
2140 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2141 /* Break if match found or match impossible due to ordering by ID */
2142 if (fdset_id <= mon_fdset->id) {
2143 if (fdset_id < mon_fdset->id) {
2144 mon_fdset = NULL;
2145 }
2146 break;
2147 }
2148 }
2149 }
2150
2151 if (mon_fdset == NULL) {
2152 int64_t fdset_id_prev = -1;
2153 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2154
2155 if (has_fdset_id) {
2156 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2158 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002159 return NULL;
2160 }
2161 /* Use specified fdset ID */
2162 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2163 mon_fdset_cur = mon_fdset;
2164 if (fdset_id < mon_fdset_cur->id) {
2165 break;
2166 }
2167 }
2168 } else {
2169 /* Use first available fdset ID */
2170 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2171 mon_fdset_cur = mon_fdset;
2172 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2173 fdset_id_prev = mon_fdset_cur->id;
2174 continue;
2175 }
2176 break;
2177 }
2178 }
2179
2180 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2181 if (has_fdset_id) {
2182 mon_fdset->id = fdset_id;
2183 } else {
2184 mon_fdset->id = fdset_id_prev + 1;
2185 }
2186
2187 /* The fdset list is ordered by fdset ID */
2188 if (!mon_fdset_cur) {
2189 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2190 } else if (mon_fdset->id < mon_fdset_cur->id) {
2191 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2192 } else {
2193 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2194 }
2195 }
2196
2197 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2198 mon_fdset_fd->fd = fd;
2199 mon_fdset_fd->removed = false;
2200 if (has_opaque) {
2201 mon_fdset_fd->opaque = g_strdup(opaque);
2202 }
2203 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2204
2205 fdinfo = g_malloc0(sizeof(*fdinfo));
2206 fdinfo->fdset_id = mon_fdset->id;
2207 fdinfo->fd = mon_fdset_fd->fd;
2208
2209 return fdinfo;
2210}
2211
Corey Bryantadb696f2012-08-14 16:43:47 -04002212int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2213{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002214#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002215 MonFdset *mon_fdset;
2216 MonFdsetFd *mon_fdset_fd;
2217 int mon_fd_flags;
2218
Corey Bryantadb696f2012-08-14 16:43:47 -04002219 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2220 if (mon_fdset->id != fdset_id) {
2221 continue;
2222 }
2223 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2224 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2225 if (mon_fd_flags == -1) {
2226 return -1;
2227 }
2228
2229 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2230 return mon_fdset_fd->fd;
2231 }
2232 }
2233 errno = EACCES;
2234 return -1;
2235 }
2236#endif
2237
2238 errno = ENOENT;
2239 return -1;
2240}
2241
2242int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2243{
2244 MonFdset *mon_fdset;
2245 MonFdsetFd *mon_fdset_fd_dup;
2246
2247 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2248 if (mon_fdset->id != fdset_id) {
2249 continue;
2250 }
2251 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2252 if (mon_fdset_fd_dup->fd == dup_fd) {
2253 return -1;
2254 }
2255 }
2256 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2257 mon_fdset_fd_dup->fd = dup_fd;
2258 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2259 return 0;
2260 }
2261 return -1;
2262}
2263
2264static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2265{
2266 MonFdset *mon_fdset;
2267 MonFdsetFd *mon_fdset_fd_dup;
2268
2269 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2270 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2271 if (mon_fdset_fd_dup->fd == dup_fd) {
2272 if (remove) {
2273 QLIST_REMOVE(mon_fdset_fd_dup, next);
2274 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2275 monitor_fdset_cleanup(mon_fdset);
2276 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002277 return -1;
2278 } else {
2279 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002280 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002281 }
2282 }
2283 }
2284 return -1;
2285}
2286
2287int monitor_fdset_dup_fd_find(int dup_fd)
2288{
2289 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2290}
2291
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002292void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002293{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002294 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002295}
2296
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002297int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002298{
2299 int fd;
2300 Error *local_err = NULL;
2301
2302 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002303 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002304 } else {
2305 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002306 if (fd == -1) {
2307 error_setg(&local_err, "Invalid file descriptor number '%s'",
2308 fdname);
2309 }
2310 }
2311 if (local_err) {
2312 error_propagate(errp, local_err);
2313 assert(fd == -1);
2314 } else {
2315 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002316 }
2317
2318 return fd;
2319}
2320
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002321/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002322static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002323#include "hmp-commands-info.h"
2324 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002325};
2326
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002327/* mon_cmds and info_cmds would be sorted at runtime */
2328static mon_cmd_t mon_cmds[] = {
2329#include "hmp-commands.h"
2330 { NULL, NULL, },
2331};
2332
bellard9307c4c2004-04-04 12:57:25 +00002333/*******************************************************************/
2334
2335static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002336static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002337
bellard9307c4c2004-04-04 12:57:25 +00002338
Stefan Weil9c3175c2013-08-22 21:30:09 +02002339static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2340expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002341{
Fam Zheng277acfe2013-08-20 10:58:21 +08002342 va_list ap;
2343 va_start(ap, fmt);
2344 monitor_vprintf(mon, fmt, ap);
2345 monitor_printf(mon, "\n");
2346 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002347 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002348}
2349
Markus Armbruster09b94182010-01-20 13:07:30 +01002350/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002351static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002352{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002353 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002354 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002355 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002356 uint64_t tmp = 0;
2357 int ret;
bellard92a31b12005-02-10 22:00:52 +00002358
Thomas Huth854e67f2017-01-13 13:12:35 +01002359 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002360 return -1;
2361 }
2362
2363 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002364 if (compare_cmd(name, md->name)) {
2365 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002366 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002367 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002368 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002369 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002370 switch(md->type) {
2371 case MD_I32:
2372 *pval = *(int32_t *)ptr;
2373 break;
2374 case MD_TLONG:
2375 *pval = *(target_long *)ptr;
2376 break;
2377 default:
2378 *pval = 0;
2379 break;
2380 }
bellard9307c4c2004-04-04 12:57:25 +00002381 }
2382 return 0;
2383 }
2384 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002385
Thomas Huth854e67f2017-01-13 13:12:35 +01002386 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002387 if (!ret) {
2388 *pval = (target_long) tmp;
2389 }
2390
2391 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002392}
2393
2394static void next(void)
2395{
Blue Swirl660f11b2009-07-31 21:16:51 +00002396 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002397 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002398 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002399 pch++;
2400 }
2401}
2402
aliguori376253e2009-03-05 23:01:23 +00002403static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002404
aliguori376253e2009-03-05 23:01:23 +00002405static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002406{
blueswir1c2efc952007-09-25 17:28:42 +00002407 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002408 char *p;
bellard6a00d602005-11-21 23:25:50 +00002409 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002410
2411 switch(*pch) {
2412 case '+':
2413 next();
aliguori376253e2009-03-05 23:01:23 +00002414 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002415 break;
2416 case '-':
2417 next();
aliguori376253e2009-03-05 23:01:23 +00002418 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002419 break;
2420 case '~':
2421 next();
aliguori376253e2009-03-05 23:01:23 +00002422 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002423 break;
2424 case '(':
2425 next();
aliguori376253e2009-03-05 23:01:23 +00002426 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002427 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002428 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002429 }
2430 next();
2431 break;
bellard81d09122004-07-14 17:21:37 +00002432 case '\'':
2433 pch++;
2434 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002435 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002436 n = *pch;
2437 pch++;
2438 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002439 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002440 next();
2441 break;
bellard9307c4c2004-04-04 12:57:25 +00002442 case '$':
2443 {
2444 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002445 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002446
bellard9307c4c2004-04-04 12:57:25 +00002447 pch++;
2448 q = buf;
2449 while ((*pch >= 'a' && *pch <= 'z') ||
2450 (*pch >= 'A' && *pch <= 'Z') ||
2451 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002452 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002453 if ((q - buf) < sizeof(buf) - 1)
2454 *q++ = *pch;
2455 pch++;
2456 }
blueswir1cd390082008-11-16 13:53:32 +00002457 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002458 pch++;
2459 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002460 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002461 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002462 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002463 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002464 }
2465 break;
2466 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002467 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002468 n = 0;
2469 break;
2470 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002471 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002472 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002473 if (errno == ERANGE) {
2474 expr_error(mon, "number too large");
2475 }
bellard9307c4c2004-04-04 12:57:25 +00002476 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002477 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002478 }
2479 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002480 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002481 pch++;
2482 break;
2483 }
2484 return n;
2485}
2486
2487
aliguori376253e2009-03-05 23:01:23 +00002488static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002489{
blueswir1c2efc952007-09-25 17:28:42 +00002490 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002491 int op;
ths3b46e622007-09-17 08:09:54 +00002492
aliguori376253e2009-03-05 23:01:23 +00002493 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002494 for(;;) {
2495 op = *pch;
2496 if (op != '*' && op != '/' && op != '%')
2497 break;
2498 next();
aliguori376253e2009-03-05 23:01:23 +00002499 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002500 switch(op) {
2501 default:
2502 case '*':
2503 val *= val2;
2504 break;
2505 case '/':
2506 case '%':
ths5fafdf22007-09-16 21:08:06 +00002507 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002508 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002509 if (op == '/')
2510 val /= val2;
2511 else
2512 val %= val2;
2513 break;
2514 }
2515 }
2516 return val;
2517}
2518
aliguori376253e2009-03-05 23:01:23 +00002519static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002520{
blueswir1c2efc952007-09-25 17:28:42 +00002521 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002522 int op;
bellard9307c4c2004-04-04 12:57:25 +00002523
aliguori376253e2009-03-05 23:01:23 +00002524 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002525 for(;;) {
2526 op = *pch;
2527 if (op != '&' && op != '|' && op != '^')
2528 break;
2529 next();
aliguori376253e2009-03-05 23:01:23 +00002530 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002531 switch(op) {
2532 default:
2533 case '&':
2534 val &= val2;
2535 break;
2536 case '|':
2537 val |= val2;
2538 break;
2539 case '^':
2540 val ^= val2;
2541 break;
2542 }
2543 }
2544 return val;
2545}
2546
aliguori376253e2009-03-05 23:01:23 +00002547static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002548{
blueswir1c2efc952007-09-25 17:28:42 +00002549 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002550 int op;
bellard9307c4c2004-04-04 12:57:25 +00002551
aliguori376253e2009-03-05 23:01:23 +00002552 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002553 for(;;) {
2554 op = *pch;
2555 if (op != '+' && op != '-')
2556 break;
2557 next();
aliguori376253e2009-03-05 23:01:23 +00002558 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002559 if (op == '+')
2560 val += val2;
2561 else
2562 val -= val2;
2563 }
2564 return val;
2565}
2566
aliguori376253e2009-03-05 23:01:23 +00002567static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002568{
2569 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002570 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002571 *pp = pch;
2572 return -1;
2573 }
blueswir1cd390082008-11-16 13:53:32 +00002574 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002575 pch++;
aliguori376253e2009-03-05 23:01:23 +00002576 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002577 *pp = pch;
2578 return 0;
2579}
2580
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002581static int get_double(Monitor *mon, double *pval, const char **pp)
2582{
2583 const char *p = *pp;
2584 char *tailp;
2585 double d;
2586
2587 d = strtod(p, &tailp);
2588 if (tailp == p) {
2589 monitor_printf(mon, "Number expected\n");
2590 return -1;
2591 }
2592 if (d != d || d - d != 0) {
2593 /* NaN or infinity */
2594 monitor_printf(mon, "Bad number\n");
2595 return -1;
2596 }
2597 *pval = d;
2598 *pp = tailp;
2599 return 0;
2600}
2601
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002602/*
2603 * Store the command-name in cmdname, and return a pointer to
2604 * the remaining of the command string.
2605 */
2606static const char *get_command_name(const char *cmdline,
2607 char *cmdname, size_t nlen)
2608{
2609 size_t len;
2610 const char *p, *pstart;
2611
2612 p = cmdline;
2613 while (qemu_isspace(*p))
2614 p++;
2615 if (*p == '\0')
2616 return NULL;
2617 pstart = p;
2618 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2619 p++;
2620 len = p - pstart;
2621 if (len > nlen - 1)
2622 len = nlen - 1;
2623 memcpy(cmdname, pstart, len);
2624 cmdname[len] = '\0';
2625 return p;
2626}
2627
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002628/**
2629 * Read key of 'type' into 'key' and return the current
2630 * 'type' pointer.
2631 */
2632static char *key_get_info(const char *type, char **key)
2633{
2634 size_t len;
2635 char *p, *str;
2636
2637 if (*type == ',')
2638 type++;
2639
2640 p = strchr(type, ':');
2641 if (!p) {
2642 *key = NULL;
2643 return NULL;
2644 }
2645 len = p - type;
2646
Anthony Liguori7267c092011-08-20 22:09:37 -05002647 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002648 memcpy(str, type, len);
2649 str[len] = '\0';
2650
2651 *key = str;
2652 return ++p;
2653}
2654
bellard9307c4c2004-04-04 12:57:25 +00002655static int default_fmt_format = 'x';
2656static int default_fmt_size = 4;
2657
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002658static int is_valid_option(const char *c, const char *typestr)
2659{
2660 char option[3];
2661
2662 option[0] = '-';
2663 option[1] = *c;
2664 option[2] = '\0';
2665
2666 typestr = strstr(typestr, option);
2667 return (typestr != NULL);
2668}
2669
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002670static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2671 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002672{
2673 const mon_cmd_t *cmd;
2674
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002675 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002676 if (compare_cmd(cmdname, cmd->name)) {
2677 return cmd;
2678 }
2679 }
2680
2681 return NULL;
2682}
2683
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002684/*
Bandan Dasae502122015-06-03 18:38:08 -04002685 * Parse command name from @cmdp according to command table @table.
2686 * If blank, return NULL.
2687 * Else, if no valid command can be found, report to @mon, and return
2688 * NULL.
2689 * Else, change @cmdp to point right behind the name, and return its
2690 * command table entry.
2691 * Do not assume the return value points into @table! It doesn't when
2692 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002693 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002694static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002695 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04002696 const char **cmdp,
2697 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002698{
Bandan Dasae502122015-06-03 18:38:08 -04002699 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002700 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002701 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002702
bellard9307c4c2004-04-04 12:57:25 +00002703 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002704 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002705 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002706 return NULL;
ths3b46e622007-09-17 08:09:54 +00002707
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002708 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002709 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002710 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002711 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002712 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002713 }
bellard9307c4c2004-04-04 12:57:25 +00002714
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002715 /* filter out following useless space */
2716 while (qemu_isspace(*p)) {
2717 p++;
2718 }
Bandan Dasae502122015-06-03 18:38:08 -04002719
2720 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002721 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002722 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002723 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002724 }
2725
Bandan Dasae502122015-06-03 18:38:08 -04002726 return cmd;
2727}
2728
2729/*
2730 * Parse arguments for @cmd.
2731 * If it can't be parsed, report to @mon, and return NULL.
2732 * Else, insert command arguments into a QDict, and return it.
2733 * Note: On success, caller has to free the QDict structure.
2734 */
2735
2736static QDict *monitor_parse_arguments(Monitor *mon,
2737 const char **endp,
2738 const mon_cmd_t *cmd)
2739{
2740 const char *typestr;
2741 char *key;
2742 int c;
2743 const char *p = *endp;
2744 char buf[1024];
2745 QDict *qdict = qdict_new();
2746
bellard9307c4c2004-04-04 12:57:25 +00002747 /* parse the parameters */
2748 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002749 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002750 typestr = key_get_info(typestr, &key);
2751 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002752 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002753 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002754 typestr++;
2755 switch(c) {
2756 case 'F':
bellard81d09122004-07-14 17:21:37 +00002757 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002758 case 's':
2759 {
2760 int ret;
ths3b46e622007-09-17 08:09:54 +00002761
blueswir1cd390082008-11-16 13:53:32 +00002762 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002763 p++;
2764 if (*typestr == '?') {
2765 typestr++;
2766 if (*p == '\0') {
2767 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002768 break;
bellard9307c4c2004-04-04 12:57:25 +00002769 }
2770 }
2771 ret = get_str(buf, sizeof(buf), &p);
2772 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002773 switch(c) {
2774 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002775 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002776 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002777 break;
2778 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002779 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002780 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002781 break;
2782 default:
Bandan Dasae502122015-06-03 18:38:08 -04002783 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002784 break;
2785 }
bellard9307c4c2004-04-04 12:57:25 +00002786 goto fail;
2787 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002788 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00002789 }
2790 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002791 case 'O':
2792 {
2793 QemuOptsList *opts_list;
2794 QemuOpts *opts;
2795
2796 opts_list = qemu_find_opts(key);
2797 if (!opts_list || opts_list->desc->name) {
2798 goto bad_type;
2799 }
2800 while (qemu_isspace(*p)) {
2801 p++;
2802 }
2803 if (!*p)
2804 break;
2805 if (get_str(buf, sizeof(buf), &p) < 0) {
2806 goto fail;
2807 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002808 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002809 if (!opts) {
2810 goto fail;
2811 }
2812 qemu_opts_to_qdict(opts, qdict);
2813 qemu_opts_del(opts);
2814 }
2815 break;
bellard9307c4c2004-04-04 12:57:25 +00002816 case '/':
2817 {
2818 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002819
blueswir1cd390082008-11-16 13:53:32 +00002820 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002821 p++;
2822 if (*p == '/') {
2823 /* format found */
2824 p++;
2825 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002826 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002827 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002828 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002829 count = count * 10 + (*p - '0');
2830 p++;
2831 }
2832 }
2833 size = -1;
2834 format = -1;
2835 for(;;) {
2836 switch(*p) {
2837 case 'o':
2838 case 'd':
2839 case 'u':
2840 case 'x':
2841 case 'i':
2842 case 'c':
2843 format = *p++;
2844 break;
2845 case 'b':
2846 size = 1;
2847 p++;
2848 break;
2849 case 'h':
2850 size = 2;
2851 p++;
2852 break;
2853 case 'w':
2854 size = 4;
2855 p++;
2856 break;
2857 case 'g':
2858 case 'L':
2859 size = 8;
2860 p++;
2861 break;
2862 default:
2863 goto next;
2864 }
2865 }
2866 next:
blueswir1cd390082008-11-16 13:53:32 +00002867 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002868 monitor_printf(mon, "invalid char in format: '%c'\n",
2869 *p);
bellard9307c4c2004-04-04 12:57:25 +00002870 goto fail;
2871 }
bellard9307c4c2004-04-04 12:57:25 +00002872 if (format < 0)
2873 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002874 if (format != 'i') {
2875 /* for 'i', not specifying a size gives -1 as size */
2876 if (size < 0)
2877 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002878 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002879 }
bellard9307c4c2004-04-04 12:57:25 +00002880 default_fmt_format = format;
2881 } else {
2882 count = 1;
2883 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002884 if (format != 'i') {
2885 size = default_fmt_size;
2886 } else {
2887 size = -1;
2888 }
bellard9307c4c2004-04-04 12:57:25 +00002889 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002890 qdict_put_int(qdict, "count", count);
2891 qdict_put_int(qdict, "format", format);
2892 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00002893 }
2894 break;
2895 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002896 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002897 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002898 {
blueswir1c2efc952007-09-25 17:28:42 +00002899 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002900
blueswir1cd390082008-11-16 13:53:32 +00002901 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002902 p++;
bellard34405572004-06-08 00:55:58 +00002903 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002904 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002905 if (*p == '\0') {
2906 typestr++;
2907 break;
2908 }
bellard34405572004-06-08 00:55:58 +00002909 } else {
2910 if (*p == '.') {
2911 p++;
blueswir1cd390082008-11-16 13:53:32 +00002912 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002913 p++;
bellard34405572004-06-08 00:55:58 +00002914 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002915 typestr++;
2916 break;
bellard34405572004-06-08 00:55:58 +00002917 }
2918 }
bellard13224a82006-07-14 22:03:35 +00002919 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002920 }
aliguori376253e2009-03-05 23:01:23 +00002921 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002922 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002923 /* Check if 'i' is greater than 32-bit */
2924 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002925 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002926 monitor_printf(mon, "integer is for 32-bit values\n");
2927 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002928 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002929 if (val < 0) {
2930 monitor_printf(mon, "enter a positive value\n");
2931 goto fail;
2932 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002933 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002934 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002935 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00002936 }
2937 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002938 case 'o':
2939 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002940 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002941 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002942 char *end;
2943
2944 while (qemu_isspace(*p)) {
2945 p++;
2946 }
2947 if (*typestr == '?') {
2948 typestr++;
2949 if (*p == '\0') {
2950 break;
2951 }
2952 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002953 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002954 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002955 monitor_printf(mon, "invalid size\n");
2956 goto fail;
2957 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002958 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002959 p = end;
2960 }
2961 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002962 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002963 {
2964 double val;
2965
2966 while (qemu_isspace(*p))
2967 p++;
2968 if (*typestr == '?') {
2969 typestr++;
2970 if (*p == '\0') {
2971 break;
2972 }
2973 }
2974 if (get_double(mon, &val, &p) < 0) {
2975 goto fail;
2976 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002977 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002978 switch (*p) {
2979 case 'm':
2980 val /= 1e3; p += 2; break;
2981 case 'u':
2982 val /= 1e6; p += 2; break;
2983 case 'n':
2984 val /= 1e9; p += 2; break;
2985 }
2986 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002987 if (*p && !qemu_isspace(*p)) {
2988 monitor_printf(mon, "Unknown unit suffix\n");
2989 goto fail;
2990 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04002991 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002992 }
2993 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002994 case 'b':
2995 {
2996 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002997 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002998
2999 while (qemu_isspace(*p)) {
3000 p++;
3001 }
3002 beg = p;
3003 while (qemu_isgraph(*p)) {
3004 p++;
3005 }
3006 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003007 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003008 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003009 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003010 } else {
3011 monitor_printf(mon, "Expected 'on' or 'off'\n");
3012 goto fail;
3013 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003014 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003015 }
3016 break;
bellard9307c4c2004-04-04 12:57:25 +00003017 case '-':
3018 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003019 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003020 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003021 /* option */
ths3b46e622007-09-17 08:09:54 +00003022
bellard9307c4c2004-04-04 12:57:25 +00003023 c = *typestr++;
3024 if (c == '\0')
3025 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003026 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003027 p++;
bellard9307c4c2004-04-04 12:57:25 +00003028 if (*p == '-') {
3029 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003030 if(c != *p) {
3031 if(!is_valid_option(p, typestr)) {
3032
3033 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003034 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003035 goto fail;
3036 } else {
3037 skip_key = 1;
3038 }
bellard9307c4c2004-04-04 12:57:25 +00003039 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003040 if(skip_key) {
3041 p = tmp;
3042 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003043 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003044 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003045 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003046 }
bellard9307c4c2004-04-04 12:57:25 +00003047 }
bellard9307c4c2004-04-04 12:57:25 +00003048 }
3049 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003050 case 'S':
3051 {
3052 /* package all remaining string */
3053 int len;
3054
3055 while (qemu_isspace(*p)) {
3056 p++;
3057 }
3058 if (*typestr == '?') {
3059 typestr++;
3060 if (*p == '\0') {
3061 /* no remaining string: NULL argument */
3062 break;
3063 }
3064 }
3065 len = strlen(p);
3066 if (len <= 0) {
3067 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003068 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003069 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003070 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003071 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003072 p += len;
3073 }
3074 break;
bellard9307c4c2004-04-04 12:57:25 +00003075 default:
3076 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003077 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003078 goto fail;
3079 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003080 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003081 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003082 }
3083 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003084 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003085 p++;
3086 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003087 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003088 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003089 goto fail;
3090 }
3091
Bandan Dasae502122015-06-03 18:38:08 -04003092 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003093
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003094fail:
Bandan Dasae502122015-06-03 18:38:08 -04003095 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003096 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003097 return NULL;
3098}
3099
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003100static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003101{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003102 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003103 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003104
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003105 trace_handle_hmp_command(mon, cmdline);
3106
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003107 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003108 if (!cmd) {
3109 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003110 }
3111
Bandan Dasae502122015-06-03 18:38:08 -04003112 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3113 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003114 monitor_printf(mon, "Try \"help %s\" for more information\n",
3115 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003116 return;
3117 }
3118
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003119 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003120 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003121}
3122
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003123static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003124{
3125 const char *p, *pstart;
3126 char cmd[128];
3127 int len;
3128
3129 p = list;
3130 for(;;) {
3131 pstart = p;
3132 p = strchr(p, '|');
3133 if (!p)
3134 p = pstart + strlen(pstart);
3135 len = p - pstart;
3136 if (len > sizeof(cmd) - 2)
3137 len = sizeof(cmd) - 2;
3138 memcpy(cmd, pstart, len);
3139 cmd[len] = '\0';
3140 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003141 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003142 }
3143 if (*p == '\0')
3144 break;
3145 p++;
3146 }
3147}
3148
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003149static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003150{
3151 DIR *ffs;
3152 struct dirent *d;
3153 char path[1024];
3154 char file[1024], file_prefix[1024];
3155 int input_path_len;
3156 const char *p;
3157
ths5fafdf22007-09-16 21:08:06 +00003158 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003159 if (!p) {
3160 input_path_len = 0;
3161 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003162 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003163 } else {
3164 input_path_len = p - input + 1;
3165 memcpy(path, input, input_path_len);
3166 if (input_path_len > sizeof(path) - 1)
3167 input_path_len = sizeof(path) - 1;
3168 path[input_path_len] = '\0';
3169 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3170 }
Bandan Das19f2db52015-06-03 18:38:07 -04003171
bellard81d09122004-07-14 17:21:37 +00003172 ffs = opendir(path);
3173 if (!ffs)
3174 return;
3175 for(;;) {
3176 struct stat sb;
3177 d = readdir(ffs);
3178 if (!d)
3179 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003180
3181 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3182 continue;
3183 }
3184
bellard81d09122004-07-14 17:21:37 +00003185 if (strstart(d->d_name, file_prefix, NULL)) {
3186 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003187 if (input_path_len < sizeof(file))
3188 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3189 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003190 /* stat the file to find out if it's a directory.
3191 * In that case add a slash to speed up typing long paths
3192 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003193 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003194 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003195 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003196 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003197 }
3198 }
3199 closedir(ffs);
3200}
3201
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003202static const char *next_arg_type(const char *typestr)
3203{
3204 const char *p = strchr(typestr, ':');
3205 return (p != NULL ? ++p : typestr);
3206}
3207
Hani Benhabiles40d19392014-05-07 23:41:30 +01003208static void add_completion_option(ReadLineState *rs, const char *str,
3209 const char *option)
3210{
3211 if (!str || !option) {
3212 return;
3213 }
3214 if (!strncmp(option, str, strlen(str))) {
3215 readline_add_completion(rs, option);
3216 }
3217}
3218
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003219void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3220{
3221 size_t len;
3222 ChardevBackendInfoList *list, *start;
3223
3224 if (nb_args != 2) {
3225 return;
3226 }
3227 len = strlen(str);
3228 readline_set_completion_index(rs, len);
3229
3230 start = list = qmp_query_chardev_backends(NULL);
3231 while (list) {
3232 const char *chr_name = list->value->name;
3233
3234 if (!strncmp(chr_name, str, len)) {
3235 readline_add_completion(rs, chr_name);
3236 }
3237 list = list->next;
3238 }
3239 qapi_free_ChardevBackendInfoList(start);
3240}
3241
Hani Benhabilesb162b492014-05-07 23:41:31 +01003242void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3243{
3244 size_t len;
3245 int i;
3246
3247 if (nb_args != 2) {
3248 return;
3249 }
3250 len = strlen(str);
3251 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003252 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003253 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003254 }
3255}
3256
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003257void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003258{
3259 GSList *list, *elt;
3260 size_t len;
3261
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003262 if (nb_args != 2) {
3263 return;
3264 }
3265
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003266 len = strlen(str);
3267 readline_set_completion_index(rs, len);
3268 list = elt = object_class_get_list(TYPE_DEVICE, false);
3269 while (elt) {
3270 const char *name;
3271 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3272 TYPE_DEVICE);
3273 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003274
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003275 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003276 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003277 readline_add_completion(rs, name);
3278 }
3279 elt = elt->next;
3280 }
3281 g_slist_free(list);
3282}
3283
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003284void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003285{
3286 GSList *list, *elt;
3287 size_t len;
3288
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003289 if (nb_args != 2) {
3290 return;
3291 }
3292
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003293 len = strlen(str);
3294 readline_set_completion_index(rs, len);
3295 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3296 while (elt) {
3297 const char *name;
3298
3299 name = object_class_get_name(OBJECT_CLASS(elt->data));
3300 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3301 readline_add_completion(rs, name);
3302 }
3303 elt = elt->next;
3304 }
3305 g_slist_free(list);
3306}
3307
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003308static void peripheral_device_del_completion(ReadLineState *rs,
3309 const char *str, size_t len)
3310{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003311 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3312 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003313
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003314 list = qdev_build_hotpluggable_device_list(peripheral);
3315 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003316 return;
3317 }
3318
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003319 for (item = list; item; item = g_slist_next(item)) {
3320 DeviceState *dev = item->data;
3321
3322 if (dev->id && !strncmp(str, dev->id, len)) {
3323 readline_add_completion(rs, dev->id);
3324 }
3325 }
3326
3327 g_slist_free(list);
3328}
3329
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003330void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3331{
3332 size_t len;
3333 ChardevInfoList *list, *start;
3334
3335 if (nb_args != 2) {
3336 return;
3337 }
3338 len = strlen(str);
3339 readline_set_completion_index(rs, len);
3340
3341 start = list = qmp_query_chardev(NULL);
3342 while (list) {
3343 ChardevInfo *chr = list->value;
3344
3345 if (!strncmp(chr->label, str, len)) {
3346 readline_add_completion(rs, chr->label);
3347 }
3348 list = list->next;
3349 }
3350 qapi_free_ChardevInfoList(start);
3351}
3352
Hani Benhabiles8e597772014-05-27 23:39:30 +01003353static void ringbuf_completion(ReadLineState *rs, const char *str)
3354{
3355 size_t len;
3356 ChardevInfoList *list, *start;
3357
3358 len = strlen(str);
3359 readline_set_completion_index(rs, len);
3360
3361 start = list = qmp_query_chardev(NULL);
3362 while (list) {
3363 ChardevInfo *chr_info = list->value;
3364
3365 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003366 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003367 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003368 readline_add_completion(rs, chr_info->label);
3369 }
3370 }
3371 list = list->next;
3372 }
3373 qapi_free_ChardevInfoList(start);
3374}
3375
Hani Benhabiles8e597772014-05-27 23:39:30 +01003376void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3377{
3378 if (nb_args != 2) {
3379 return;
3380 }
3381 ringbuf_completion(rs, str);
3382}
3383
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003384void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3385{
3386 size_t len;
3387
3388 if (nb_args != 2) {
3389 return;
3390 }
3391
3392 len = strlen(str);
3393 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003394 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003395}
3396
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003397void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003398{
3399 ObjectPropertyInfoList *list, *start;
3400 size_t len;
3401
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003402 if (nb_args != 2) {
3403 return;
3404 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003405 len = strlen(str);
3406 readline_set_completion_index(rs, len);
3407
3408 start = list = qmp_qom_list("/objects", NULL);
3409 while (list) {
3410 ObjectPropertyInfo *info = list->value;
3411
3412 if (!strncmp(info->type, "child<", 5)
3413 && !strncmp(info->name, str, len)) {
3414 readline_add_completion(rs, info->name);
3415 }
3416 list = list->next;
3417 }
3418 qapi_free_ObjectPropertyInfoList(start);
3419}
3420
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003421void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3422{
3423 int i;
3424 char *sep;
3425 size_t len;
3426
3427 if (nb_args != 2) {
3428 return;
3429 }
3430 sep = strrchr(str, '-');
3431 if (sep) {
3432 str = sep + 1;
3433 }
3434 len = strlen(str);
3435 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003436 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003437 if (!strncmp(str, QKeyCode_str(i), len)) {
3438 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003439 }
3440 }
3441}
3442
Hani Benhabiles40d19392014-05-07 23:41:30 +01003443void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3444{
3445 size_t len;
3446
3447 len = strlen(str);
3448 readline_set_completion_index(rs, len);
3449 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003450 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003451 int count, i;
3452 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003453 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003454 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003455 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003456 const char *name = ncs[i]->name;
3457 if (!strncmp(str, name, len)) {
3458 readline_add_completion(rs, name);
3459 }
3460 }
3461 } else if (nb_args == 3) {
3462 add_completion_option(rs, str, "on");
3463 add_completion_option(rs, str, "off");
3464 }
3465}
3466
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003467void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3468{
3469 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003470 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003471
3472 if (nb_args != 2) {
3473 return;
3474 }
3475
3476 len = strlen(str);
3477 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003478 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003479 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003480 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003481 QemuOpts *opts;
3482 const char *name = ncs[i]->name;
3483 if (strncmp(str, name, len)) {
3484 continue;
3485 }
3486 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3487 if (opts) {
3488 readline_add_completion(rs, name);
3489 }
3490 }
3491}
3492
Lluís Vilanovabd712112016-07-11 12:53:51 +02003493void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3494{
3495 size_t len;
3496
3497 len = strlen(str);
3498 readline_set_completion_index(rs, len);
3499 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003500 TraceEventIter iter;
3501 TraceEvent *ev;
3502 char *pattern = g_strdup_printf("%s*", str);
3503 trace_event_iter_init(&iter, pattern);
3504 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3505 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003506 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003507 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003508 }
3509}
3510
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003511void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3512{
3513 size_t len;
3514
3515 len = strlen(str);
3516 readline_set_completion_index(rs, len);
3517 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003518 TraceEventIter iter;
3519 TraceEvent *ev;
3520 char *pattern = g_strdup_printf("%s*", str);
3521 trace_event_iter_init(&iter, pattern);
3522 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3523 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003524 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003525 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003526 } else if (nb_args == 3) {
3527 add_completion_option(rs, str, "on");
3528 add_completion_option(rs, str, "off");
3529 }
3530}
3531
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003532void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3533{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003534 int i;
3535
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003536 if (nb_args != 2) {
3537 return;
3538 }
3539 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003540 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3541 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003542 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003543}
3544
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003545void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3546 const char *str)
3547{
3548 size_t len;
3549
3550 len = strlen(str);
3551 readline_set_completion_index(rs, len);
3552 if (nb_args == 2) {
3553 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003554 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003555 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003556 if (!strncmp(str, name, len)) {
3557 readline_add_completion(rs, name);
3558 }
3559 }
3560 } else if (nb_args == 3) {
3561 add_completion_option(rs, str, "on");
3562 add_completion_option(rs, str, "off");
3563 }
3564}
3565
Liang Li50e9a622015-03-23 16:32:29 +08003566void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3567 const char *str)
3568{
3569 size_t len;
3570
3571 len = strlen(str);
3572 readline_set_completion_index(rs, len);
3573 if (nb_args == 2) {
3574 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003575 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003576 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003577 if (!strncmp(str, name, len)) {
3578 readline_add_completion(rs, name);
3579 }
3580 }
3581 }
3582}
3583
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003584static void vm_completion(ReadLineState *rs, const char *str)
3585{
3586 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003587 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003588 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003589
3590 len = strlen(str);
3591 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003592
Kevin Wolf88be7b42016-05-20 18:49:07 +02003593 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003594 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003595 AioContext *ctx = bdrv_get_aio_context(bs);
3596 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003597
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003598 aio_context_acquire(ctx);
3599 if (bdrv_can_snapshot(bs)) {
3600 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3601 }
3602 aio_context_release(ctx);
3603 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003604 continue;
3605 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003606
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003607 snapshot = snapshots;
3608 while (snapshot) {
3609 char *completion = snapshot->value->name;
3610 if (!strncmp(str, completion, len)) {
3611 readline_add_completion(rs, completion);
3612 }
3613 completion = snapshot->value->id;
3614 if (!strncmp(str, completion, len)) {
3615 readline_add_completion(rs, completion);
3616 }
3617 snapshot = snapshot->next;
3618 }
3619 qapi_free_SnapshotInfoList(snapshots);
3620 }
3621
3622}
3623
3624void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3625{
3626 if (nb_args == 2) {
3627 vm_completion(rs, str);
3628 }
3629}
3630
3631void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3632{
3633 if (nb_args == 2) {
3634 vm_completion(rs, str);
3635 }
3636}
3637
Wenchao Xiac35b6402013-08-27 20:38:24 +08003638static void monitor_find_completion_by_table(Monitor *mon,
3639 const mon_cmd_t *cmd_table,
3640 char **args,
3641 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003642{
3643 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003644 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003645 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003646 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003647 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003648
bellard81d09122004-07-14 17:21:37 +00003649 if (nb_args <= 1) {
3650 /* command completion */
3651 if (nb_args == 0)
3652 cmdname = "";
3653 else
3654 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003655 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003656 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003657 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003658 }
3659 } else {
3660 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003661 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003662 if (compare_cmd(args[0], cmd->name)) {
3663 break;
3664 }
bellard81d09122004-07-14 17:21:37 +00003665 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003666 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003667 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003668 }
3669
Wenchao Xiad903a772013-08-27 20:38:25 +08003670 if (cmd->sub_table) {
3671 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003672 monitor_find_completion_by_table(mon, cmd->sub_table,
3673 &args[1], nb_args - 1);
3674 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003675 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003676 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003677 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3678 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003679 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003680
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003681 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003682 for(i = 0; i < nb_args - 2; i++) {
3683 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003684 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003685 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003686 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003687 }
3688 }
3689 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003690 old_ptype = NULL;
3691 while (*ptype == '-' && old_ptype != ptype) {
3692 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003693 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003694 }
bellard81d09122004-07-14 17:21:37 +00003695 switch(*ptype) {
3696 case 'F':
3697 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003698 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003699 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003700 break;
3701 case 'B':
3702 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003703 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003704 while ((blk = blk_next(blk)) != NULL) {
3705 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003706 if (str[0] == '\0' ||
3707 !strncmp(name, str, strlen(str))) {
3708 readline_add_completion(mon->rs, name);
3709 }
3710 }
bellard81d09122004-07-14 17:21:37 +00003711 break;
bellard7fe48482004-10-09 18:08:01 +00003712 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003713 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003714 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003715 monitor_find_completion_by_table(mon, cmd_table,
3716 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003717 }
3718 break;
bellard81d09122004-07-14 17:21:37 +00003719 default:
3720 break;
3721 }
3722 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003723}
3724
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003725static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003726 const char *cmdline)
3727{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003728 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003729 char *args[MAX_ARGS];
3730 int nb_args, len;
3731
3732 /* 1. parse the cmdline */
3733 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3734 return;
3735 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003736
3737 /* if the line ends with a space, it means we want to complete the
3738 next arg */
3739 len = strlen(cmdline);
3740 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3741 if (nb_args >= MAX_ARGS) {
3742 goto cleanup;
3743 }
3744 args[nb_args++] = g_strdup("");
3745 }
3746
3747 /* 2. auto complete according to args */
3748 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003749
3750cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003751 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003752}
3753
aliguori731b0362009-03-05 23:01:42 +00003754static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003755{
aliguori731b0362009-03-05 23:01:42 +00003756 Monitor *mon = opaque;
3757
Jan Kiszkac62313b2009-12-04 14:05:29 +01003758 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003759}
3760
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003761static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003762{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003763 QObject *req, *rsp = NULL, *id = NULL;
3764 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003765 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003766 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003767
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003768 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003769 if (!req && !err) {
3770 /* json_parser_parse_err() sucks: can fail without setting @err */
3771 error_setg(&err, QERR_JSON_PARSING);
3772 }
3773 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003774 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003775 }
3776
Markus Armbruster104fc302017-03-03 13:32:28 +01003777 qdict = qobject_to_qdict(req);
3778 if (qdict) {
3779 id = qdict_get(qdict, "id");
3780 qobject_incref(id);
3781 qdict_del(qdict, "id");
3782 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003783
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03003784 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
3785 QString *req_json = qobject_to_json(req);
3786 trace_handle_qmp_command(mon, qstring_get_str(req_json));
3787 QDECREF(req_json);
3788 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003789
Markus Armbruster635db182017-03-03 13:32:27 +01003790 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003791
Markus Armbruster635db182017-03-03 13:32:27 +01003792 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3793 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3794 if (qdict
3795 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
Markus Armbruster977c7362017-08-24 10:46:08 +02003796 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
Markus Armbruster635db182017-03-03 13:32:27 +01003797 /* Provide a more useful error message */
3798 qdict_del(qdict, "desc");
Eric Blake46f5ac22017-04-27 16:58:17 -05003799 qdict_put_str(qdict, "desc", "Expecting capabilities negotiation"
3800 " with 'qmp_capabilities'");
Markus Armbruster635db182017-03-03 13:32:27 +01003801 }
3802 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003803
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003804err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003805 if (err) {
3806 qdict = qdict_new();
3807 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3808 error_free(err);
3809 rsp = QOBJECT(qdict);
3810 }
3811
3812 if (rsp) {
3813 if (id) {
3814 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3815 id = NULL;
3816 }
3817
3818 monitor_json_emitter(mon, rsp);
3819 }
3820
3821 qobject_decref(id);
3822 qobject_decref(rsp);
3823 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003824}
3825
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003826static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003827{
3828 Monitor *old_mon = cur_mon;
3829
3830 cur_mon = opaque;
3831
Markus Armbruster74358f22015-03-06 19:35:59 +01003832 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003833
3834 cur_mon = old_mon;
3835}
3836
aliguori731b0362009-03-05 23:01:42 +00003837static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003838{
aliguori731b0362009-03-05 23:01:42 +00003839 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003840 int i;
aliguori376253e2009-03-05 23:01:23 +00003841
aliguori731b0362009-03-05 23:01:42 +00003842 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003843
aliguoricde76ee2009-03-05 23:01:51 +00003844 if (cur_mon->rs) {
3845 for (i = 0; i < size; i++)
3846 readline_handle_byte(cur_mon->rs, buf[i]);
3847 } else {
3848 if (size == 0 || buf[size - 1] != 0)
3849 monitor_printf(cur_mon, "corrupted command\n");
3850 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003851 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003852 }
aliguori731b0362009-03-05 23:01:42 +00003853
3854 cur_mon = old_mon;
3855}
aliguorid8f44602008-10-06 13:52:44 +00003856
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003857static void monitor_command_cb(void *opaque, const char *cmdline,
3858 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003859{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003860 Monitor *mon = opaque;
3861
aliguori731b0362009-03-05 23:01:42 +00003862 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003863 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003864 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003865}
3866
aliguoricde76ee2009-03-05 23:01:51 +00003867int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003868{
aliguoricde76ee2009-03-05 23:01:51 +00003869 if (!mon->rs)
3870 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003871 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003872 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003873}
3874
aliguori376253e2009-03-05 23:01:23 +00003875void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003876{
aliguoricde76ee2009-03-05 23:01:51 +00003877 if (!mon->rs)
3878 return;
aliguori731b0362009-03-05 23:01:42 +00003879 if (--mon->suspend_cnt == 0)
3880 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003881}
3882
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003883static QObject *get_qmp_greeting(void)
3884{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003885 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003886
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003887 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003888
3889 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3890 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003891}
3892
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003893static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003894{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003895 QObject *data;
3896 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003897
Luiz Capitulino47116d12010-02-08 17:01:30 -02003898 switch (event) {
3899 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003900 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003901 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003902 monitor_json_emitter(mon, data);
3903 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003904 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003905 break;
3906 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003907 json_message_parser_destroy(&mon->qmp.parser);
3908 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003909 mon_refcount--;
3910 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003911 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003912 }
3913}
3914
aliguori731b0362009-03-05 23:01:42 +00003915static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003916{
aliguori376253e2009-03-05 23:01:23 +00003917 Monitor *mon = opaque;
3918
aliguori2724b182009-03-05 23:01:47 +00003919 switch (event) {
3920 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003921 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003922 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003923 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003924 if (mon->reset_seen) {
3925 readline_restart(mon->rs);
3926 monitor_resume(mon);
3927 monitor_flush(mon);
3928 } else {
3929 mon->suspend_cnt = 0;
3930 }
aliguori2724b182009-03-05 23:01:47 +00003931 break;
ths86e94de2007-01-05 22:01:59 +00003932
aliguori2724b182009-03-05 23:01:47 +00003933 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003934 if (mon->reset_seen) {
3935 if (mon->suspend_cnt == 0) {
3936 monitor_printf(mon, "\n");
3937 }
3938 monitor_flush(mon);
3939 monitor_suspend(mon);
3940 } else {
3941 mon->suspend_cnt++;
3942 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003943 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003944 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003945 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003946 break;
3947
Amit Shahb6b8df52009-10-07 18:31:16 +05303948 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003949 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3950 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003951 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003952 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003953 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003954 }
3955 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003956 mon_refcount++;
3957 break;
3958
3959 case CHR_EVENT_CLOSED:
3960 mon_refcount--;
3961 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00003962 break;
3963 }
ths86e94de2007-01-05 22:01:59 +00003964}
3965
Wayne Xia816f8922011-10-12 11:32:41 +08003966static int
3967compare_mon_cmd(const void *a, const void *b)
3968{
3969 return strcmp(((const mon_cmd_t *)a)->name,
3970 ((const mon_cmd_t *)b)->name);
3971}
3972
3973static void sortcmdlist(void)
3974{
3975 int array_num;
3976 int elem_size = sizeof(mon_cmd_t);
3977
3978 array_num = sizeof(mon_cmds)/elem_size-1;
3979 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
3980
3981 array_num = sizeof(info_cmds)/elem_size-1;
3982 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
3983}
3984
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003985/* These functions just adapt the readline interface in a typesafe way. We
3986 * could cast function pointers but that discards compiler checks.
3987 */
Stefan Weild5d15072014-01-25 18:18:23 +01003988static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
3989 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003990{
3991 va_list ap;
3992 va_start(ap, fmt);
3993 monitor_vprintf(opaque, fmt, ap);
3994 va_end(ap);
3995}
3996
3997static void monitor_readline_flush(void *opaque)
3998{
3999 monitor_flush(opaque);
4000}
4001
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004002/*
4003 * Print to current monitor if we have one, else to stderr.
4004 * TODO should return int, so callers can calculate width, but that
4005 * requires surgery to monitor_vprintf(). Left for another day.
4006 */
4007void error_vprintf(const char *fmt, va_list ap)
4008{
4009 if (cur_mon && !monitor_cur_is_qmp()) {
4010 monitor_vprintf(cur_mon, fmt, ap);
4011 } else {
4012 vfprintf(stderr, fmt, ap);
4013 }
4014}
4015
4016void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4017{
4018 if (cur_mon && !monitor_cur_is_qmp()) {
4019 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004020 } else if (!cur_mon) {
4021 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004022 }
4023}
4024
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004025static void __attribute__((constructor)) monitor_lock_init(void)
4026{
4027 qemu_mutex_init(&monitor_lock);
4028}
4029
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004030void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004031{
aliguori731b0362009-03-05 23:01:42 +00004032 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004033 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004034
4035 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004036 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004037 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004038 is_first_init = 0;
4039 }
aliguori87127162009-03-05 23:01:29 +00004040
Wenchao Xiab01fe892013-08-27 20:38:19 +08004041 mon = g_malloc(sizeof(*mon));
4042 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004043
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004044 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004045 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004046 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004047 mon->rs = readline_init(monitor_readline_printf,
4048 monitor_readline_flush,
4049 mon,
4050 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004051 monitor_read_command(mon, 0);
4052 }
aliguori87127162009-03-05 23:01:29 +00004053
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004054 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004055 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004056 monitor_qmp_event, NULL, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004057 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004058 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004059 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004060 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004061 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004062 }
aliguori87127162009-03-05 23:01:29 +00004063
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004064 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004065 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004066 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004067}
4068
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004069void monitor_cleanup(void)
4070{
4071 Monitor *mon, *next;
4072
4073 qemu_mutex_lock(&monitor_lock);
4074 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4075 QLIST_REMOVE(mon, entry);
4076 monitor_data_destroy(mon);
4077 g_free(mon);
4078 }
4079 qemu_mutex_unlock(&monitor_lock);
4080}
4081
Paolo Bonzini4d454572012-11-26 16:03:42 +01004082QemuOptsList qemu_mon_opts = {
4083 .name = "mon",
4084 .implied_opt_name = "chardev",
4085 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4086 .desc = {
4087 {
4088 .name = "mode",
4089 .type = QEMU_OPT_STRING,
4090 },{
4091 .name = "chardev",
4092 .type = QEMU_OPT_STRING,
4093 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004094 .name = "pretty",
4095 .type = QEMU_OPT_BOOL,
4096 },
4097 { /* end of list */ }
4098 },
4099};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004100
4101#ifndef TARGET_I386
4102void qmp_rtc_reset_reinjection(Error **errp)
4103{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004104 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004105}
4106#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004107
4108#ifndef TARGET_S390X
4109void qmp_dump_skeys(const char *filename, Error **errp)
4110{
4111 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4112}
4113#endif
Peter Xuae50a772016-03-30 17:27:24 +01004114
4115#ifndef TARGET_ARM
4116GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4117{
4118 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4119 return NULL;
4120}
4121#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304122
4123HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4124{
4125 MachineState *ms = MACHINE(qdev_get_machine());
4126 MachineClass *mc = MACHINE_GET_CLASS(ms);
4127
Igor Mammedovc5514d02017-02-10 11:20:57 +01004128 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304129 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4130 return NULL;
4131 }
4132
Igor Mammedovc5514d02017-02-10 11:20:57 +01004133 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304134}