blob: 373bb8d1c371f27e1f72acc70861cd123d0fac15 [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 "qmp-commands.h"
73#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010074#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010075#include "block/qapi.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010076#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020077#include "qapi/qmp-event.h"
78#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020079#include "qmp-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 Armbruster39a18152015-09-16 13:06:28 +0200954 * qapi-introspect.py's output actually conforms to the schema.
955 */
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
Pavel Butsykincaf15312015-09-22 16:18:17 +03001058CPUState *mon_get_cpu(void)
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 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001077 cpu_synchronize_state(cpu);
1078 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001079}
1080
Pavel Butsykinbf957282015-09-10 18:38:59 +03001081CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001082{
Thomas Huth854e67f2017-01-13 13:12:35 +01001083 CPUState *cs = mon_get_cpu();
1084
1085 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001086}
1087
Luiz Capitulino99b77962011-10-24 10:53:44 -02001088int monitor_get_cpu_index(void)
1089{
Thomas Huth854e67f2017-01-13 13:12:35 +01001090 CPUState *cs = mon_get_cpu();
1091
1092 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001093}
1094
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001095static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001096{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001097 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1098 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001099
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001100 if (all_cpus) {
1101 CPU_FOREACH(cs) {
1102 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1103 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1104 }
1105 } else {
1106 cs = mon_get_cpu();
1107
1108 if (!cs) {
1109 monitor_printf(mon, "No CPU available\n");
1110 return;
1111 }
1112
1113 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001114 }
bellard9307c4c2004-04-04 12:57:25 +00001115}
1116
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001117#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001118static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001119{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001120 if (!tcg_enabled()) {
1121 error_report("JIT information is only available with accel=tcg");
1122 return;
1123 }
1124
aliguori376253e2009-03-05 23:01:23 +00001125 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001126 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001127}
1128
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001129static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001130{
1131 dump_opcount_info((FILE *)mon, monitor_fprintf);
1132}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001133#endif
Max Filippov246ae242014-11-02 11:04:18 +03001134
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001135static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001136{
1137 int i;
bellard7e2515e2004-08-01 21:52:19 +00001138 const char *str;
ths3b46e622007-09-17 08:09:54 +00001139
aliguoricde76ee2009-03-05 23:01:51 +00001140 if (!mon->rs)
1141 return;
bellard7e2515e2004-08-01 21:52:19 +00001142 i = 0;
1143 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001144 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001145 if (!str)
1146 break;
aliguori376253e2009-03-05 23:01:23 +00001147 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001148 i++;
bellardaa455482004-04-04 13:07:25 +00001149 }
1150}
1151
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001152static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001153{
Thomas Huth854e67f2017-01-13 13:12:35 +01001154 CPUState *cs = mon_get_cpu();
1155
1156 if (!cs) {
1157 monitor_printf(mon, "No CPU available\n");
1158 return;
1159 }
1160 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001161}
j_mayer76a66252007-03-07 08:32:30 +00001162
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001163static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301164{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001165 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001166 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1167 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001168 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001169 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001170 Error *local_err = NULL;
1171
1172 if (name == NULL) {
1173 name = "*";
1174 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001175 if (vcpu < 0) {
1176 monitor_printf(mon, "argument vcpu must be positive");
1177 return;
1178 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001179
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001180 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001181 if (local_err) {
1182 error_report_err(local_err);
1183 return;
1184 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001185
1186 for (elem = events; elem != NULL; elem = elem->next) {
1187 monitor_printf(mon, "%s : state %u\n",
1188 elem->value->name,
1189 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1190 }
1191 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301192}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301193
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001194void qmp_client_migrate_info(const char *protocol, const char *hostname,
1195 bool has_port, int64_t port,
1196 bool has_tls_port, int64_t tls_port,
1197 bool has_cert_subject, const char *cert_subject,
1198 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001199{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001200 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001201 if (!qemu_using_spice(errp)) {
1202 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001203 }
1204
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001205 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001206 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001207 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001208 }
1209
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001210 if (qemu_spice_migrate_info(hostname,
1211 has_port ? port : -1,
1212 has_tls_port ? tls_port : -1,
1213 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001214 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001215 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001216 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001217 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001218 }
1219
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001220 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001221}
1222
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001223static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001224{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001225 Error *err = NULL;
1226
1227 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1228 if (err) {
1229 error_report_err(err);
1230 }
pbrooke735b912007-06-30 13:53:24 +00001231}
1232
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001233static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001234{
1235 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001236 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001237
bellard9307c4c2004-04-04 12:57:25 +00001238 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001239 mask = 0;
1240 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001241 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001242 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001243 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001244 return;
1245 }
1246 }
Peter Maydell24537a02013-02-11 16:41:23 +00001247 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001248}
1249
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001250static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001251{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001252 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001253 if (!option || !strcmp(option, "on")) {
1254 singlestep = 1;
1255 } else if (!strcmp(option, "off")) {
1256 singlestep = 0;
1257 } else {
1258 monitor_printf(mon, "unexpected option %s\n", option);
1259 }
1260}
1261
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001262static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001263{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001264 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001265 if (!device)
1266 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1267 if (gdbserver_start(device) < 0) {
1268 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1269 device);
1270 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001271 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001272 } else {
aliguori59030a82009-04-05 18:43:41 +00001273 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1274 device);
bellard8a7ddc32004-03-31 19:00:16 +00001275 }
1276}
1277
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001278static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001279{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001280 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001281 if (select_watchdog_action(action) == -1) {
1282 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1283 }
1284}
1285
aliguori376253e2009-03-05 23:01:23 +00001286static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001287{
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001289 switch(c) {
1290 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001292 break;
1293 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001301 break;
1302 default:
1303 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001305 } else {
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001307 }
1308 break;
1309 }
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001311}
1312
aliguori376253e2009-03-05 23:01:23 +00001313static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001314 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001315{
Blue Swirl23842aa2010-01-12 20:27:43 +00001316 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001317 uint8_t buf[16];
1318 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001319 CPUState *cs = mon_get_cpu();
1320
1321 if (!cs && (format == 'i' || !is_physical)) {
1322 monitor_printf(mon, "Can not dump without CPU\n");
1323 return;
1324 }
bellard9307c4c2004-04-04 12:57:25 +00001325
1326 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001327 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001328 return;
1329 }
1330
1331 len = wsize * count;
1332 if (wsize == 1)
1333 line_size = 8;
1334 else
1335 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001336 max_digits = 0;
1337
1338 switch(format) {
1339 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001340 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001341 break;
1342 default:
1343 case 'x':
1344 max_digits = (wsize * 8) / 4;
1345 break;
1346 case 'u':
1347 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001348 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001349 break;
1350 case 'c':
1351 wsize = 1;
1352 break;
1353 }
1354
1355 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001356 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001357 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001358 else
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001360 l = len;
1361 if (l > line_size)
1362 l = line_size;
1363 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001364 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001365 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001366 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001368 break;
1369 }
bellard9307c4c2004-04-04 12:57:25 +00001370 }
ths5fafdf22007-09-16 21:08:06 +00001371 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001372 while (i < l) {
1373 switch(wsize) {
1374 default:
1375 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001376 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001377 break;
1378 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001379 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001380 break;
1381 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001382 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001383 break;
1384 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001385 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001386 break;
1387 }
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001389 switch(format) {
1390 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001398 break;
1399 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001401 break;
1402 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001404 break;
1405 }
1406 i += wsize;
1407 }
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001409 addr += l;
1410 len -= l;
1411 }
1412}
1413
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001414static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001415{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001416 int count = qdict_get_int(qdict, "count");
1417 int format = qdict_get_int(qdict, "format");
1418 int size = qdict_get_int(qdict, "size");
1419 target_long addr = qdict_get_int(qdict, "addr");
1420
aliguori376253e2009-03-05 23:01:23 +00001421 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001422}
1423
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001424static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001425{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001426 int count = qdict_get_int(qdict, "count");
1427 int format = qdict_get_int(qdict, "format");
1428 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001429 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001430
aliguori376253e2009-03-05 23:01:23 +00001431 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001432}
1433
Paolo Bonzinie9628442017-04-20 15:30:58 +02001434static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1435{
1436 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1437 addr, 1);
1438
1439 if (!mrs.mr) {
1440 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1441 return NULL;
1442 }
1443
1444 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1445 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1446 memory_region_unref(mrs.mr);
1447 return NULL;
1448 }
1449
1450 *p_mr = mrs.mr;
1451 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1452}
1453
1454static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1455{
1456 hwaddr addr = qdict_get_int(qdict, "addr");
1457 Error *local_err = NULL;
1458 MemoryRegion *mr = NULL;
1459 void *ptr;
1460
1461 ptr = gpa2hva(&mr, addr, &local_err);
1462 if (local_err) {
1463 error_report_err(local_err);
1464 return;
1465 }
1466
1467 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1468 " (%s) is %p\n",
1469 addr, mr->name, ptr);
1470
1471 memory_region_unref(mr);
1472}
1473
1474#ifdef CONFIG_LINUX
1475static uint64_t vtop(void *ptr, Error **errp)
1476{
1477 uint64_t pinfo;
1478 uint64_t ret = -1;
1479 uintptr_t addr = (uintptr_t) ptr;
1480 uintptr_t pagesize = getpagesize();
1481 off_t offset = addr / pagesize * sizeof(pinfo);
1482 int fd;
1483
1484 fd = open("/proc/self/pagemap", O_RDONLY);
1485 if (fd == -1) {
1486 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1487 return -1;
1488 }
1489
1490 /* Force copy-on-write if necessary. */
1491 atomic_add((uint8_t *)ptr, 0);
1492
1493 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1494 error_setg_errno(errp, errno, "Cannot read pagemap");
1495 goto out;
1496 }
1497 if ((pinfo & (1ull << 63)) == 0) {
1498 error_setg(errp, "Page not present");
1499 goto out;
1500 }
1501 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1502
1503out:
1504 close(fd);
1505 return ret;
1506}
1507
1508static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1509{
1510 hwaddr addr = qdict_get_int(qdict, "addr");
1511 Error *local_err = NULL;
1512 MemoryRegion *mr = NULL;
1513 void *ptr;
1514 uint64_t physaddr;
1515
1516 ptr = gpa2hva(&mr, addr, &local_err);
1517 if (local_err) {
1518 error_report_err(local_err);
1519 return;
1520 }
1521
1522 physaddr = vtop(ptr, &local_err);
1523 if (local_err) {
1524 error_report_err(local_err);
1525 } else {
1526 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1527 " (%s) is 0x%" PRIx64 "\n",
1528 addr, mr->name, (uint64_t) physaddr);
1529 }
1530
1531 memory_region_unref(mr);
1532}
1533#endif
1534
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001535static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001536{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001537 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001538 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001539
bellard9307c4c2004-04-04 12:57:25 +00001540 switch(format) {
1541 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001542 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001543 break;
1544 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001545 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001546 break;
1547 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001548 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001549 break;
1550 default:
1551 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001552 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001553 break;
1554 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001555 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001556 break;
1557 }
aliguori376253e2009-03-05 23:01:23 +00001558 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001559}
1560
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001561static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001562{
1563 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001564 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001565 uint32_t start = qdict_get_int(qdict, "start");
1566 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001567
1568 sum = 0;
1569 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001570 uint8_t val = address_space_ldub(&address_space_memory, addr,
1571 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001572 /* BSD sum algorithm ('sum' Unix command) */
1573 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001574 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001575 }
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001577}
1578
bellard13224a82006-07-14 22:03:35 +00001579static int mouse_button_state;
1580
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001581static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001582{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001583 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001584 const char *dx_str = qdict_get_str(qdict, "dx_str");
1585 const char *dy_str = qdict_get_str(qdict, "dy_str");
1586 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001587
bellard13224a82006-07-14 22:03:35 +00001588 dx = strtol(dx_str, NULL, 0);
1589 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001590 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1591 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1592
1593 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001594 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001595 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001596 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001597 qemu_input_queue_btn(NULL, button, true);
1598 qemu_input_event_sync();
1599 qemu_input_queue_btn(NULL, button, false);
1600 }
1601 }
1602 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001603}
1604
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001605static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001606{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001607 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001608 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1609 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1610 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1611 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001612 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001613
1614 if (mouse_button_state == button_state) {
1615 return;
1616 }
1617 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1618 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001619 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001620}
1621
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001622static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001623{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001624 int size = qdict_get_int(qdict, "size");
1625 int addr = qdict_get_int(qdict, "addr");
1626 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001627 uint32_t val;
1628 int suffix;
1629
1630 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001631 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001632 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001633 addr++;
1634 }
1635 addr &= 0xffff;
1636
1637 switch(size) {
1638 default:
1639 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001640 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001641 suffix = 'b';
1642 break;
1643 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001644 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001645 suffix = 'w';
1646 break;
1647 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001648 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001649 suffix = 'l';
1650 break;
1651 }
aliguori376253e2009-03-05 23:01:23 +00001652 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1653 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001654}
bellarda3a91a32004-06-04 11:06:21 +00001655
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001656static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001657{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001658 int size = qdict_get_int(qdict, "size");
1659 int addr = qdict_get_int(qdict, "addr");
1660 int val = qdict_get_int(qdict, "val");
1661
Jan Kiszkaf1147842009-07-14 10:20:11 +02001662 addr &= IOPORTS_MASK;
1663
1664 switch (size) {
1665 default:
1666 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001667 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001668 break;
1669 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001670 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001671 break;
1672 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001673 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001674 break;
1675 }
1676}
1677
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001678static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001679{
Gongleif1839932014-12-03 18:20:58 +00001680 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001681 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001682
Gongleif1839932014-12-03 18:20:58 +00001683 qemu_boot_set(bootdevice, &local_err);
1684 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001685 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001686 } else {
Gongleif1839932014-12-03 18:20:58 +00001687 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001688 }
1689}
1690
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001691static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001692{
Peter Xu57bb40c2017-01-16 16:40:05 +08001693 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001694 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001695
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001696 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001697}
1698
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001699static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001700{
aliguorib28b6232009-04-22 20:20:29 +00001701 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001702 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001703 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001704
Igor Mammedovf75cd442017-05-30 18:23:59 +02001705 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001706 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1707
zhanghailiang5b009e42014-11-04 19:49:30 +08001708 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001709 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1710 for (i = 0; i < nb_numa_nodes; i++) {
1711 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001712 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1713 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1714 cpu->value->props->node_id == i) {
1715 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001716 }
1717 }
1718 monitor_printf(mon, "\n");
1719 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001720 node_mem[i].node_mem >> 20);
1721 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1722 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00001723 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001724 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001725 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001726}
1727
bellard5f1ce942006-02-08 22:40:15 +00001728#ifdef CONFIG_PROFILER
1729
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001730int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001731int64_t dev_time;
1732
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001733static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001734{
aliguori376253e2009-03-05 23:01:23 +00001735 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301736 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001737 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301738 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001739 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001740 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001741}
1742#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001743static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001744{
aliguori376253e2009-03-05 23:01:23 +00001745 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001746}
1747#endif
1748
bellardec36b692006-07-16 18:57:03 +00001749/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001750static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001751
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001752static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001753{
1754 int i;
1755 CaptureState *s;
1756
1757 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001758 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001759 s->ops.info (s->opaque);
1760 }
1761}
1762
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001763static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001764{
1765 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001766 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001767 CaptureState *s;
1768
1769 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1770 if (i == n) {
1771 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001772 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001773 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001774 return;
1775 }
1776 }
1777}
1778
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001779static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001780{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001781 const char *path = qdict_get_str(qdict, "path");
1782 int has_freq = qdict_haskey(qdict, "freq");
1783 int freq = qdict_get_try_int(qdict, "freq", -1);
1784 int has_bits = qdict_haskey(qdict, "bits");
1785 int bits = qdict_get_try_int(qdict, "bits", -1);
1786 int has_channels = qdict_haskey(qdict, "nchannels");
1787 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001788 CaptureState *s;
1789
Anthony Liguori7267c092011-08-20 22:09:37 -05001790 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001791
1792 freq = has_freq ? freq : 44100;
1793 bits = has_bits ? bits : 16;
1794 nchannels = has_channels ? nchannels : 2;
1795
1796 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001797 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001798 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001799 return;
bellardec36b692006-07-16 18:57:03 +00001800 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001801 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001802}
bellardec36b692006-07-16 18:57:03 +00001803
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001804static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001805{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001806 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001807
aliguori76655d62009-03-06 20:27:37 +00001808 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001809 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001810 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001811 return acl;
1812}
aliguori76655d62009-03-06 20:27:37 +00001813
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001814static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001815{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001816 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001817 qemu_acl *acl = find_acl(mon, aclname);
1818 qemu_acl_entry *entry;
1819 int i = 0;
1820
1821 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001822 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001823 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001824 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001825 i++;
1826 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001827 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001828 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001829 }
1830}
1831
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001832static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001833{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001834 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001835 qemu_acl *acl = find_acl(mon, aclname);
1836
1837 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001838 qemu_acl_reset(acl);
1839 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001840 }
1841}
aliguori76655d62009-03-06 20:27:37 +00001842
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001843static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001844{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001845 const char *aclname = qdict_get_str(qdict, "aclname");
1846 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001847 qemu_acl *acl = find_acl(mon, aclname);
1848
1849 if (acl) {
1850 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001851 acl->defaultDeny = 0;
1852 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001853 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001854 acl->defaultDeny = 1;
1855 monitor_printf(mon, "acl: policy set to 'deny'\n");
1856 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001857 monitor_printf(mon, "acl: unknown policy '%s', "
1858 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001859 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001860 }
1861}
aliguori76655d62009-03-06 20:27:37 +00001862
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001863static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001864{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001865 const char *aclname = qdict_get_str(qdict, "aclname");
1866 const char *match = qdict_get_str(qdict, "match");
1867 const char *policy = qdict_get_str(qdict, "policy");
1868 int has_index = qdict_haskey(qdict, "index");
1869 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001870 qemu_acl *acl = find_acl(mon, aclname);
1871 int deny, ret;
1872
1873 if (acl) {
1874 if (strcmp(policy, "allow") == 0) {
1875 deny = 0;
1876 } else if (strcmp(policy, "deny") == 0) {
1877 deny = 1;
1878 } else {
1879 monitor_printf(mon, "acl: unknown policy '%s', "
1880 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001881 return;
1882 }
aliguori28a76be2009-03-06 20:27:40 +00001883 if (has_index)
1884 ret = qemu_acl_insert(acl, deny, match, index);
1885 else
1886 ret = qemu_acl_append(acl, deny, match);
1887 if (ret < 0)
1888 monitor_printf(mon, "acl: unable to add acl entry\n");
1889 else
1890 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001891 }
1892}
aliguori76655d62009-03-06 20:27:37 +00001893
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001894static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001895{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001896 const char *aclname = qdict_get_str(qdict, "aclname");
1897 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001898 qemu_acl *acl = find_acl(mon, aclname);
1899 int ret;
aliguori76655d62009-03-06 20:27:37 +00001900
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001901 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001902 ret = qemu_acl_remove(acl, match);
1903 if (ret < 0)
1904 monitor_printf(mon, "acl: no matching acl entry\n");
1905 else
1906 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001907 }
1908}
1909
Corey Bryant208c9d12012-06-22 14:36:09 -04001910void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001911{
Anthony Liguoric227f092009-10-01 16:12:16 -05001912 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001913 int fd;
1914
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001915 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001916 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001917 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001918 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001919 }
1920
1921 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001922 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001923 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1924 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001925 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001926 }
1927
Corey Bryant208c9d12012-06-22 14:36:09 -04001928 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001929 if (strcmp(monfd->name, fdname) != 0) {
1930 continue;
1931 }
1932
1933 close(monfd->fd);
1934 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001935 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001936 }
1937
Anthony Liguori7267c092011-08-20 22:09:37 -05001938 monfd = g_malloc0(sizeof(mon_fd_t));
1939 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001940 monfd->fd = fd;
1941
Corey Bryant208c9d12012-06-22 14:36:09 -04001942 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001943}
1944
Corey Bryant208c9d12012-06-22 14:36:09 -04001945void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001946{
Anthony Liguoric227f092009-10-01 16:12:16 -05001947 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001948
Corey Bryant208c9d12012-06-22 14:36:09 -04001949 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001950 if (strcmp(monfd->name, fdname) != 0) {
1951 continue;
1952 }
1953
Blue Swirl72cf2d42009-09-12 07:36:22 +00001954 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001955 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001956 g_free(monfd->name);
1957 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001958 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001959 }
1960
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001961 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001962}
1963
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001964int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001965{
Anthony Liguoric227f092009-10-01 16:12:16 -05001966 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001967
Blue Swirl72cf2d42009-09-12 07:36:22 +00001968 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001969 int fd;
1970
1971 if (strcmp(monfd->name, fdname) != 0) {
1972 continue;
1973 }
1974
1975 fd = monfd->fd;
1976
1977 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001978 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001979 g_free(monfd->name);
1980 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001981
1982 return fd;
1983 }
1984
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001985 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001986 return -1;
1987}
1988
Corey Bryantba1c0482012-08-14 16:43:43 -04001989static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1990{
1991 MonFdsetFd *mon_fdset_fd;
1992 MonFdsetFd *mon_fdset_fd_next;
1993
1994 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001995 if ((mon_fdset_fd->removed ||
1996 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1997 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001998 close(mon_fdset_fd->fd);
1999 g_free(mon_fdset_fd->opaque);
2000 QLIST_REMOVE(mon_fdset_fd, next);
2001 g_free(mon_fdset_fd);
2002 }
2003 }
2004
Corey Bryantadb696f2012-08-14 16:43:47 -04002005 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002006 QLIST_REMOVE(mon_fdset, next);
2007 g_free(mon_fdset);
2008 }
2009}
2010
Corey Bryantefb87c12012-08-14 16:43:48 -04002011static void monitor_fdsets_cleanup(void)
2012{
2013 MonFdset *mon_fdset;
2014 MonFdset *mon_fdset_next;
2015
2016 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2017 monitor_fdset_cleanup(mon_fdset);
2018 }
2019}
2020
Corey Bryantba1c0482012-08-14 16:43:43 -04002021AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2022 const char *opaque, Error **errp)
2023{
2024 int fd;
2025 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002026 AddfdInfo *fdinfo;
2027
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002028 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002029 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002030 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002031 goto error;
2032 }
2033
Corey Bryante446f702012-10-18 15:19:32 -04002034 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2035 has_opaque, opaque, errp);
2036 if (fdinfo) {
2037 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002038 }
2039
Corey Bryantba1c0482012-08-14 16:43:43 -04002040error:
2041 if (fd != -1) {
2042 close(fd);
2043 }
2044 return NULL;
2045}
2046
2047void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2048{
2049 MonFdset *mon_fdset;
2050 MonFdsetFd *mon_fdset_fd;
2051 char fd_str[60];
2052
2053 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2054 if (mon_fdset->id != fdset_id) {
2055 continue;
2056 }
2057 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2058 if (has_fd) {
2059 if (mon_fdset_fd->fd != fd) {
2060 continue;
2061 }
2062 mon_fdset_fd->removed = true;
2063 break;
2064 } else {
2065 mon_fdset_fd->removed = true;
2066 }
2067 }
2068 if (has_fd && !mon_fdset_fd) {
2069 goto error;
2070 }
2071 monitor_fdset_cleanup(mon_fdset);
2072 return;
2073 }
2074
2075error:
2076 if (has_fd) {
2077 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2078 fdset_id, fd);
2079 } else {
2080 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2081 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002082 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002083}
2084
2085FdsetInfoList *qmp_query_fdsets(Error **errp)
2086{
2087 MonFdset *mon_fdset;
2088 MonFdsetFd *mon_fdset_fd;
2089 FdsetInfoList *fdset_list = NULL;
2090
2091 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2092 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2093 FdsetFdInfoList *fdsetfd_list = NULL;
2094
2095 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2096 fdset_info->value->fdset_id = mon_fdset->id;
2097
2098 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2099 FdsetFdInfoList *fdsetfd_info;
2100
2101 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2102 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2103 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2104 if (mon_fdset_fd->opaque) {
2105 fdsetfd_info->value->has_opaque = true;
2106 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2107 } else {
2108 fdsetfd_info->value->has_opaque = false;
2109 }
2110
2111 fdsetfd_info->next = fdsetfd_list;
2112 fdsetfd_list = fdsetfd_info;
2113 }
2114
2115 fdset_info->value->fds = fdsetfd_list;
2116
2117 fdset_info->next = fdset_list;
2118 fdset_list = fdset_info;
2119 }
2120
2121 return fdset_list;
2122}
2123
Corey Bryante446f702012-10-18 15:19:32 -04002124AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2125 bool has_opaque, const char *opaque,
2126 Error **errp)
2127{
2128 MonFdset *mon_fdset = NULL;
2129 MonFdsetFd *mon_fdset_fd;
2130 AddfdInfo *fdinfo;
2131
2132 if (has_fdset_id) {
2133 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2134 /* Break if match found or match impossible due to ordering by ID */
2135 if (fdset_id <= mon_fdset->id) {
2136 if (fdset_id < mon_fdset->id) {
2137 mon_fdset = NULL;
2138 }
2139 break;
2140 }
2141 }
2142 }
2143
2144 if (mon_fdset == NULL) {
2145 int64_t fdset_id_prev = -1;
2146 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2147
2148 if (has_fdset_id) {
2149 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002150 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2151 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002152 return NULL;
2153 }
2154 /* Use specified fdset ID */
2155 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2156 mon_fdset_cur = mon_fdset;
2157 if (fdset_id < mon_fdset_cur->id) {
2158 break;
2159 }
2160 }
2161 } else {
2162 /* Use first available fdset ID */
2163 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2164 mon_fdset_cur = mon_fdset;
2165 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2166 fdset_id_prev = mon_fdset_cur->id;
2167 continue;
2168 }
2169 break;
2170 }
2171 }
2172
2173 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2174 if (has_fdset_id) {
2175 mon_fdset->id = fdset_id;
2176 } else {
2177 mon_fdset->id = fdset_id_prev + 1;
2178 }
2179
2180 /* The fdset list is ordered by fdset ID */
2181 if (!mon_fdset_cur) {
2182 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2183 } else if (mon_fdset->id < mon_fdset_cur->id) {
2184 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2185 } else {
2186 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2187 }
2188 }
2189
2190 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2191 mon_fdset_fd->fd = fd;
2192 mon_fdset_fd->removed = false;
2193 if (has_opaque) {
2194 mon_fdset_fd->opaque = g_strdup(opaque);
2195 }
2196 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2197
2198 fdinfo = g_malloc0(sizeof(*fdinfo));
2199 fdinfo->fdset_id = mon_fdset->id;
2200 fdinfo->fd = mon_fdset_fd->fd;
2201
2202 return fdinfo;
2203}
2204
Corey Bryantadb696f2012-08-14 16:43:47 -04002205int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2206{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002207#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002208 MonFdset *mon_fdset;
2209 MonFdsetFd *mon_fdset_fd;
2210 int mon_fd_flags;
2211
Corey Bryantadb696f2012-08-14 16:43:47 -04002212 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2213 if (mon_fdset->id != fdset_id) {
2214 continue;
2215 }
2216 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2217 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2218 if (mon_fd_flags == -1) {
2219 return -1;
2220 }
2221
2222 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2223 return mon_fdset_fd->fd;
2224 }
2225 }
2226 errno = EACCES;
2227 return -1;
2228 }
2229#endif
2230
2231 errno = ENOENT;
2232 return -1;
2233}
2234
2235int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2236{
2237 MonFdset *mon_fdset;
2238 MonFdsetFd *mon_fdset_fd_dup;
2239
2240 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2241 if (mon_fdset->id != fdset_id) {
2242 continue;
2243 }
2244 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2245 if (mon_fdset_fd_dup->fd == dup_fd) {
2246 return -1;
2247 }
2248 }
2249 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2250 mon_fdset_fd_dup->fd = dup_fd;
2251 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2252 return 0;
2253 }
2254 return -1;
2255}
2256
2257static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2258{
2259 MonFdset *mon_fdset;
2260 MonFdsetFd *mon_fdset_fd_dup;
2261
2262 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2263 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2264 if (mon_fdset_fd_dup->fd == dup_fd) {
2265 if (remove) {
2266 QLIST_REMOVE(mon_fdset_fd_dup, next);
2267 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2268 monitor_fdset_cleanup(mon_fdset);
2269 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002270 return -1;
2271 } else {
2272 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002273 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002274 }
2275 }
2276 }
2277 return -1;
2278}
2279
2280int monitor_fdset_dup_fd_find(int dup_fd)
2281{
2282 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2283}
2284
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002285void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002286{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002287 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002288}
2289
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002290int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002291{
2292 int fd;
2293 Error *local_err = NULL;
2294
2295 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002296 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002297 } else {
2298 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002299 if (fd == -1) {
2300 error_setg(&local_err, "Invalid file descriptor number '%s'",
2301 fdname);
2302 }
2303 }
2304 if (local_err) {
2305 error_propagate(errp, local_err);
2306 assert(fd == -1);
2307 } else {
2308 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002309 }
2310
2311 return fd;
2312}
2313
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002314/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002315static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002316#include "hmp-commands-info.h"
2317 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002318};
2319
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002320/* mon_cmds and info_cmds would be sorted at runtime */
2321static mon_cmd_t mon_cmds[] = {
2322#include "hmp-commands.h"
2323 { NULL, NULL, },
2324};
2325
bellard9307c4c2004-04-04 12:57:25 +00002326/*******************************************************************/
2327
2328static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002329static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002330
bellard9307c4c2004-04-04 12:57:25 +00002331
Stefan Weil9c3175c2013-08-22 21:30:09 +02002332static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2333expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002334{
Fam Zheng277acfe2013-08-20 10:58:21 +08002335 va_list ap;
2336 va_start(ap, fmt);
2337 monitor_vprintf(mon, fmt, ap);
2338 monitor_printf(mon, "\n");
2339 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002340 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002341}
2342
Markus Armbruster09b94182010-01-20 13:07:30 +01002343/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002344static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002345{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002346 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002347 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002348 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002349 uint64_t tmp = 0;
2350 int ret;
bellard92a31b12005-02-10 22:00:52 +00002351
Thomas Huth854e67f2017-01-13 13:12:35 +01002352 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002353 return -1;
2354 }
2355
2356 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002357 if (compare_cmd(name, md->name)) {
2358 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002359 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002360 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002361 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002362 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002363 switch(md->type) {
2364 case MD_I32:
2365 *pval = *(int32_t *)ptr;
2366 break;
2367 case MD_TLONG:
2368 *pval = *(target_long *)ptr;
2369 break;
2370 default:
2371 *pval = 0;
2372 break;
2373 }
bellard9307c4c2004-04-04 12:57:25 +00002374 }
2375 return 0;
2376 }
2377 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002378
Thomas Huth854e67f2017-01-13 13:12:35 +01002379 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002380 if (!ret) {
2381 *pval = (target_long) tmp;
2382 }
2383
2384 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002385}
2386
2387static void next(void)
2388{
Blue Swirl660f11b2009-07-31 21:16:51 +00002389 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002390 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002391 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002392 pch++;
2393 }
2394}
2395
aliguori376253e2009-03-05 23:01:23 +00002396static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002397
aliguori376253e2009-03-05 23:01:23 +00002398static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002399{
blueswir1c2efc952007-09-25 17:28:42 +00002400 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002401 char *p;
bellard6a00d602005-11-21 23:25:50 +00002402 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002403
2404 switch(*pch) {
2405 case '+':
2406 next();
aliguori376253e2009-03-05 23:01:23 +00002407 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002408 break;
2409 case '-':
2410 next();
aliguori376253e2009-03-05 23:01:23 +00002411 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002412 break;
2413 case '~':
2414 next();
aliguori376253e2009-03-05 23:01:23 +00002415 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002416 break;
2417 case '(':
2418 next();
aliguori376253e2009-03-05 23:01:23 +00002419 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002420 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002421 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002422 }
2423 next();
2424 break;
bellard81d09122004-07-14 17:21:37 +00002425 case '\'':
2426 pch++;
2427 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002428 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002429 n = *pch;
2430 pch++;
2431 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002432 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002433 next();
2434 break;
bellard9307c4c2004-04-04 12:57:25 +00002435 case '$':
2436 {
2437 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002438 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002439
bellard9307c4c2004-04-04 12:57:25 +00002440 pch++;
2441 q = buf;
2442 while ((*pch >= 'a' && *pch <= 'z') ||
2443 (*pch >= 'A' && *pch <= 'Z') ||
2444 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002445 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002446 if ((q - buf) < sizeof(buf) - 1)
2447 *q++ = *pch;
2448 pch++;
2449 }
blueswir1cd390082008-11-16 13:53:32 +00002450 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002451 pch++;
2452 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002453 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002454 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002455 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002456 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002457 }
2458 break;
2459 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002460 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002461 n = 0;
2462 break;
2463 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002464 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002465 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002466 if (errno == ERANGE) {
2467 expr_error(mon, "number too large");
2468 }
bellard9307c4c2004-04-04 12:57:25 +00002469 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002470 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002471 }
2472 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002473 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002474 pch++;
2475 break;
2476 }
2477 return n;
2478}
2479
2480
aliguori376253e2009-03-05 23:01:23 +00002481static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002482{
blueswir1c2efc952007-09-25 17:28:42 +00002483 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002484 int op;
ths3b46e622007-09-17 08:09:54 +00002485
aliguori376253e2009-03-05 23:01:23 +00002486 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002487 for(;;) {
2488 op = *pch;
2489 if (op != '*' && op != '/' && op != '%')
2490 break;
2491 next();
aliguori376253e2009-03-05 23:01:23 +00002492 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002493 switch(op) {
2494 default:
2495 case '*':
2496 val *= val2;
2497 break;
2498 case '/':
2499 case '%':
ths5fafdf22007-09-16 21:08:06 +00002500 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002501 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002502 if (op == '/')
2503 val /= val2;
2504 else
2505 val %= val2;
2506 break;
2507 }
2508 }
2509 return val;
2510}
2511
aliguori376253e2009-03-05 23:01:23 +00002512static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002513{
blueswir1c2efc952007-09-25 17:28:42 +00002514 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002515 int op;
bellard9307c4c2004-04-04 12:57:25 +00002516
aliguori376253e2009-03-05 23:01:23 +00002517 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002518 for(;;) {
2519 op = *pch;
2520 if (op != '&' && op != '|' && op != '^')
2521 break;
2522 next();
aliguori376253e2009-03-05 23:01:23 +00002523 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002524 switch(op) {
2525 default:
2526 case '&':
2527 val &= val2;
2528 break;
2529 case '|':
2530 val |= val2;
2531 break;
2532 case '^':
2533 val ^= val2;
2534 break;
2535 }
2536 }
2537 return val;
2538}
2539
aliguori376253e2009-03-05 23:01:23 +00002540static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002541{
blueswir1c2efc952007-09-25 17:28:42 +00002542 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002543 int op;
bellard9307c4c2004-04-04 12:57:25 +00002544
aliguori376253e2009-03-05 23:01:23 +00002545 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002546 for(;;) {
2547 op = *pch;
2548 if (op != '+' && op != '-')
2549 break;
2550 next();
aliguori376253e2009-03-05 23:01:23 +00002551 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002552 if (op == '+')
2553 val += val2;
2554 else
2555 val -= val2;
2556 }
2557 return val;
2558}
2559
aliguori376253e2009-03-05 23:01:23 +00002560static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002561{
2562 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002563 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002564 *pp = pch;
2565 return -1;
2566 }
blueswir1cd390082008-11-16 13:53:32 +00002567 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002568 pch++;
aliguori376253e2009-03-05 23:01:23 +00002569 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002570 *pp = pch;
2571 return 0;
2572}
2573
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002574static int get_double(Monitor *mon, double *pval, const char **pp)
2575{
2576 const char *p = *pp;
2577 char *tailp;
2578 double d;
2579
2580 d = strtod(p, &tailp);
2581 if (tailp == p) {
2582 monitor_printf(mon, "Number expected\n");
2583 return -1;
2584 }
2585 if (d != d || d - d != 0) {
2586 /* NaN or infinity */
2587 monitor_printf(mon, "Bad number\n");
2588 return -1;
2589 }
2590 *pval = d;
2591 *pp = tailp;
2592 return 0;
2593}
2594
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002595/*
2596 * Store the command-name in cmdname, and return a pointer to
2597 * the remaining of the command string.
2598 */
2599static const char *get_command_name(const char *cmdline,
2600 char *cmdname, size_t nlen)
2601{
2602 size_t len;
2603 const char *p, *pstart;
2604
2605 p = cmdline;
2606 while (qemu_isspace(*p))
2607 p++;
2608 if (*p == '\0')
2609 return NULL;
2610 pstart = p;
2611 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2612 p++;
2613 len = p - pstart;
2614 if (len > nlen - 1)
2615 len = nlen - 1;
2616 memcpy(cmdname, pstart, len);
2617 cmdname[len] = '\0';
2618 return p;
2619}
2620
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002621/**
2622 * Read key of 'type' into 'key' and return the current
2623 * 'type' pointer.
2624 */
2625static char *key_get_info(const char *type, char **key)
2626{
2627 size_t len;
2628 char *p, *str;
2629
2630 if (*type == ',')
2631 type++;
2632
2633 p = strchr(type, ':');
2634 if (!p) {
2635 *key = NULL;
2636 return NULL;
2637 }
2638 len = p - type;
2639
Anthony Liguori7267c092011-08-20 22:09:37 -05002640 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002641 memcpy(str, type, len);
2642 str[len] = '\0';
2643
2644 *key = str;
2645 return ++p;
2646}
2647
bellard9307c4c2004-04-04 12:57:25 +00002648static int default_fmt_format = 'x';
2649static int default_fmt_size = 4;
2650
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002651static int is_valid_option(const char *c, const char *typestr)
2652{
2653 char option[3];
2654
2655 option[0] = '-';
2656 option[1] = *c;
2657 option[2] = '\0';
2658
2659 typestr = strstr(typestr, option);
2660 return (typestr != NULL);
2661}
2662
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002663static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2664 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002665{
2666 const mon_cmd_t *cmd;
2667
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002668 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002669 if (compare_cmd(cmdname, cmd->name)) {
2670 return cmd;
2671 }
2672 }
2673
2674 return NULL;
2675}
2676
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002677/*
Bandan Dasae502122015-06-03 18:38:08 -04002678 * Parse command name from @cmdp according to command table @table.
2679 * If blank, return NULL.
2680 * Else, if no valid command can be found, report to @mon, and return
2681 * NULL.
2682 * Else, change @cmdp to point right behind the name, and return its
2683 * command table entry.
2684 * Do not assume the return value points into @table! It doesn't when
2685 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002686 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002687static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002688 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04002689 const char **cmdp,
2690 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002691{
Bandan Dasae502122015-06-03 18:38:08 -04002692 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002693 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002694 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002695
bellard9307c4c2004-04-04 12:57:25 +00002696 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002697 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002698 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002699 return NULL;
ths3b46e622007-09-17 08:09:54 +00002700
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002701 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002702 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002703 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002704 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002705 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002706 }
bellard9307c4c2004-04-04 12:57:25 +00002707
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002708 /* filter out following useless space */
2709 while (qemu_isspace(*p)) {
2710 p++;
2711 }
Bandan Dasae502122015-06-03 18:38:08 -04002712
2713 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002714 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002715 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002716 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002717 }
2718
Bandan Dasae502122015-06-03 18:38:08 -04002719 return cmd;
2720}
2721
2722/*
2723 * Parse arguments for @cmd.
2724 * If it can't be parsed, report to @mon, and return NULL.
2725 * Else, insert command arguments into a QDict, and return it.
2726 * Note: On success, caller has to free the QDict structure.
2727 */
2728
2729static QDict *monitor_parse_arguments(Monitor *mon,
2730 const char **endp,
2731 const mon_cmd_t *cmd)
2732{
2733 const char *typestr;
2734 char *key;
2735 int c;
2736 const char *p = *endp;
2737 char buf[1024];
2738 QDict *qdict = qdict_new();
2739
bellard9307c4c2004-04-04 12:57:25 +00002740 /* parse the parameters */
2741 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002742 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002743 typestr = key_get_info(typestr, &key);
2744 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002745 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002746 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002747 typestr++;
2748 switch(c) {
2749 case 'F':
bellard81d09122004-07-14 17:21:37 +00002750 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002751 case 's':
2752 {
2753 int ret;
ths3b46e622007-09-17 08:09:54 +00002754
blueswir1cd390082008-11-16 13:53:32 +00002755 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002756 p++;
2757 if (*typestr == '?') {
2758 typestr++;
2759 if (*p == '\0') {
2760 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002761 break;
bellard9307c4c2004-04-04 12:57:25 +00002762 }
2763 }
2764 ret = get_str(buf, sizeof(buf), &p);
2765 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002766 switch(c) {
2767 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002768 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002769 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002770 break;
2771 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002772 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002773 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002774 break;
2775 default:
Bandan Dasae502122015-06-03 18:38:08 -04002776 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002777 break;
2778 }
bellard9307c4c2004-04-04 12:57:25 +00002779 goto fail;
2780 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002781 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00002782 }
2783 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002784 case 'O':
2785 {
2786 QemuOptsList *opts_list;
2787 QemuOpts *opts;
2788
2789 opts_list = qemu_find_opts(key);
2790 if (!opts_list || opts_list->desc->name) {
2791 goto bad_type;
2792 }
2793 while (qemu_isspace(*p)) {
2794 p++;
2795 }
2796 if (!*p)
2797 break;
2798 if (get_str(buf, sizeof(buf), &p) < 0) {
2799 goto fail;
2800 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002801 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002802 if (!opts) {
2803 goto fail;
2804 }
2805 qemu_opts_to_qdict(opts, qdict);
2806 qemu_opts_del(opts);
2807 }
2808 break;
bellard9307c4c2004-04-04 12:57:25 +00002809 case '/':
2810 {
2811 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002812
blueswir1cd390082008-11-16 13:53:32 +00002813 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002814 p++;
2815 if (*p == '/') {
2816 /* format found */
2817 p++;
2818 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002819 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002820 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002821 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002822 count = count * 10 + (*p - '0');
2823 p++;
2824 }
2825 }
2826 size = -1;
2827 format = -1;
2828 for(;;) {
2829 switch(*p) {
2830 case 'o':
2831 case 'd':
2832 case 'u':
2833 case 'x':
2834 case 'i':
2835 case 'c':
2836 format = *p++;
2837 break;
2838 case 'b':
2839 size = 1;
2840 p++;
2841 break;
2842 case 'h':
2843 size = 2;
2844 p++;
2845 break;
2846 case 'w':
2847 size = 4;
2848 p++;
2849 break;
2850 case 'g':
2851 case 'L':
2852 size = 8;
2853 p++;
2854 break;
2855 default:
2856 goto next;
2857 }
2858 }
2859 next:
blueswir1cd390082008-11-16 13:53:32 +00002860 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002861 monitor_printf(mon, "invalid char in format: '%c'\n",
2862 *p);
bellard9307c4c2004-04-04 12:57:25 +00002863 goto fail;
2864 }
bellard9307c4c2004-04-04 12:57:25 +00002865 if (format < 0)
2866 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002867 if (format != 'i') {
2868 /* for 'i', not specifying a size gives -1 as size */
2869 if (size < 0)
2870 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002871 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002872 }
bellard9307c4c2004-04-04 12:57:25 +00002873 default_fmt_format = format;
2874 } else {
2875 count = 1;
2876 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002877 if (format != 'i') {
2878 size = default_fmt_size;
2879 } else {
2880 size = -1;
2881 }
bellard9307c4c2004-04-04 12:57:25 +00002882 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002883 qdict_put_int(qdict, "count", count);
2884 qdict_put_int(qdict, "format", format);
2885 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00002886 }
2887 break;
2888 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002889 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002890 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002891 {
blueswir1c2efc952007-09-25 17:28:42 +00002892 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002893
blueswir1cd390082008-11-16 13:53:32 +00002894 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002895 p++;
bellard34405572004-06-08 00:55:58 +00002896 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002897 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002898 if (*p == '\0') {
2899 typestr++;
2900 break;
2901 }
bellard34405572004-06-08 00:55:58 +00002902 } else {
2903 if (*p == '.') {
2904 p++;
blueswir1cd390082008-11-16 13:53:32 +00002905 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002906 p++;
bellard34405572004-06-08 00:55:58 +00002907 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002908 typestr++;
2909 break;
bellard34405572004-06-08 00:55:58 +00002910 }
2911 }
bellard13224a82006-07-14 22:03:35 +00002912 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002913 }
aliguori376253e2009-03-05 23:01:23 +00002914 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002915 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002916 /* Check if 'i' is greater than 32-bit */
2917 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002918 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002919 monitor_printf(mon, "integer is for 32-bit values\n");
2920 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002921 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002922 if (val < 0) {
2923 monitor_printf(mon, "enter a positive value\n");
2924 goto fail;
2925 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002926 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002927 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002928 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00002929 }
2930 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002931 case 'o':
2932 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002933 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002934 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002935 char *end;
2936
2937 while (qemu_isspace(*p)) {
2938 p++;
2939 }
2940 if (*typestr == '?') {
2941 typestr++;
2942 if (*p == '\0') {
2943 break;
2944 }
2945 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002946 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002947 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002948 monitor_printf(mon, "invalid size\n");
2949 goto fail;
2950 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002951 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002952 p = end;
2953 }
2954 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002955 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002956 {
2957 double val;
2958
2959 while (qemu_isspace(*p))
2960 p++;
2961 if (*typestr == '?') {
2962 typestr++;
2963 if (*p == '\0') {
2964 break;
2965 }
2966 }
2967 if (get_double(mon, &val, &p) < 0) {
2968 goto fail;
2969 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002970 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002971 switch (*p) {
2972 case 'm':
2973 val /= 1e3; p += 2; break;
2974 case 'u':
2975 val /= 1e6; p += 2; break;
2976 case 'n':
2977 val /= 1e9; p += 2; break;
2978 }
2979 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002980 if (*p && !qemu_isspace(*p)) {
2981 monitor_printf(mon, "Unknown unit suffix\n");
2982 goto fail;
2983 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04002984 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002985 }
2986 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002987 case 'b':
2988 {
2989 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002990 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002991
2992 while (qemu_isspace(*p)) {
2993 p++;
2994 }
2995 beg = p;
2996 while (qemu_isgraph(*p)) {
2997 p++;
2998 }
2999 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003000 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003001 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003002 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003003 } else {
3004 monitor_printf(mon, "Expected 'on' or 'off'\n");
3005 goto fail;
3006 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003007 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003008 }
3009 break;
bellard9307c4c2004-04-04 12:57:25 +00003010 case '-':
3011 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003012 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003013 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003014 /* option */
ths3b46e622007-09-17 08:09:54 +00003015
bellard9307c4c2004-04-04 12:57:25 +00003016 c = *typestr++;
3017 if (c == '\0')
3018 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003019 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003020 p++;
bellard9307c4c2004-04-04 12:57:25 +00003021 if (*p == '-') {
3022 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003023 if(c != *p) {
3024 if(!is_valid_option(p, typestr)) {
3025
3026 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003027 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003028 goto fail;
3029 } else {
3030 skip_key = 1;
3031 }
bellard9307c4c2004-04-04 12:57:25 +00003032 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003033 if(skip_key) {
3034 p = tmp;
3035 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003036 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003037 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003038 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003039 }
bellard9307c4c2004-04-04 12:57:25 +00003040 }
bellard9307c4c2004-04-04 12:57:25 +00003041 }
3042 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003043 case 'S':
3044 {
3045 /* package all remaining string */
3046 int len;
3047
3048 while (qemu_isspace(*p)) {
3049 p++;
3050 }
3051 if (*typestr == '?') {
3052 typestr++;
3053 if (*p == '\0') {
3054 /* no remaining string: NULL argument */
3055 break;
3056 }
3057 }
3058 len = strlen(p);
3059 if (len <= 0) {
3060 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003061 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003062 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003063 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003064 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003065 p += len;
3066 }
3067 break;
bellard9307c4c2004-04-04 12:57:25 +00003068 default:
3069 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003070 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003071 goto fail;
3072 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003073 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003074 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003075 }
3076 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003077 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003078 p++;
3079 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003080 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003081 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003082 goto fail;
3083 }
3084
Bandan Dasae502122015-06-03 18:38:08 -04003085 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003086
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003087fail:
Bandan Dasae502122015-06-03 18:38:08 -04003088 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003089 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003090 return NULL;
3091}
3092
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003093static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003094{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003095 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003096 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003097
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003098 trace_handle_hmp_command(mon, cmdline);
3099
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003100 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003101 if (!cmd) {
3102 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003103 }
3104
Bandan Dasae502122015-06-03 18:38:08 -04003105 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3106 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003107 monitor_printf(mon, "Try \"help %s\" for more information\n",
3108 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003109 return;
3110 }
3111
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003112 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003113 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003114}
3115
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003116static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003117{
3118 const char *p, *pstart;
3119 char cmd[128];
3120 int len;
3121
3122 p = list;
3123 for(;;) {
3124 pstart = p;
3125 p = strchr(p, '|');
3126 if (!p)
3127 p = pstart + strlen(pstart);
3128 len = p - pstart;
3129 if (len > sizeof(cmd) - 2)
3130 len = sizeof(cmd) - 2;
3131 memcpy(cmd, pstart, len);
3132 cmd[len] = '\0';
3133 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003134 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003135 }
3136 if (*p == '\0')
3137 break;
3138 p++;
3139 }
3140}
3141
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003142static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003143{
3144 DIR *ffs;
3145 struct dirent *d;
3146 char path[1024];
3147 char file[1024], file_prefix[1024];
3148 int input_path_len;
3149 const char *p;
3150
ths5fafdf22007-09-16 21:08:06 +00003151 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003152 if (!p) {
3153 input_path_len = 0;
3154 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003155 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003156 } else {
3157 input_path_len = p - input + 1;
3158 memcpy(path, input, input_path_len);
3159 if (input_path_len > sizeof(path) - 1)
3160 input_path_len = sizeof(path) - 1;
3161 path[input_path_len] = '\0';
3162 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3163 }
Bandan Das19f2db52015-06-03 18:38:07 -04003164
bellard81d09122004-07-14 17:21:37 +00003165 ffs = opendir(path);
3166 if (!ffs)
3167 return;
3168 for(;;) {
3169 struct stat sb;
3170 d = readdir(ffs);
3171 if (!d)
3172 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003173
3174 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3175 continue;
3176 }
3177
bellard81d09122004-07-14 17:21:37 +00003178 if (strstart(d->d_name, file_prefix, NULL)) {
3179 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003180 if (input_path_len < sizeof(file))
3181 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3182 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003183 /* stat the file to find out if it's a directory.
3184 * In that case add a slash to speed up typing long paths
3185 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003186 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003187 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003188 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003189 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003190 }
3191 }
3192 closedir(ffs);
3193}
3194
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003195static const char *next_arg_type(const char *typestr)
3196{
3197 const char *p = strchr(typestr, ':');
3198 return (p != NULL ? ++p : typestr);
3199}
3200
Hani Benhabiles40d19392014-05-07 23:41:30 +01003201static void add_completion_option(ReadLineState *rs, const char *str,
3202 const char *option)
3203{
3204 if (!str || !option) {
3205 return;
3206 }
3207 if (!strncmp(option, str, strlen(str))) {
3208 readline_add_completion(rs, option);
3209 }
3210}
3211
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003212void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3213{
3214 size_t len;
3215 ChardevBackendInfoList *list, *start;
3216
3217 if (nb_args != 2) {
3218 return;
3219 }
3220 len = strlen(str);
3221 readline_set_completion_index(rs, len);
3222
3223 start = list = qmp_query_chardev_backends(NULL);
3224 while (list) {
3225 const char *chr_name = list->value->name;
3226
3227 if (!strncmp(chr_name, str, len)) {
3228 readline_add_completion(rs, chr_name);
3229 }
3230 list = list->next;
3231 }
3232 qapi_free_ChardevBackendInfoList(start);
3233}
3234
Hani Benhabilesb162b492014-05-07 23:41:31 +01003235void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3236{
3237 size_t len;
3238 int i;
3239
3240 if (nb_args != 2) {
3241 return;
3242 }
3243 len = strlen(str);
3244 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003245 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003246 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003247 }
3248}
3249
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003250void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003251{
3252 GSList *list, *elt;
3253 size_t len;
3254
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003255 if (nb_args != 2) {
3256 return;
3257 }
3258
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003259 len = strlen(str);
3260 readline_set_completion_index(rs, len);
3261 list = elt = object_class_get_list(TYPE_DEVICE, false);
3262 while (elt) {
3263 const char *name;
3264 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3265 TYPE_DEVICE);
3266 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003267
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003268 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003269 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003270 readline_add_completion(rs, name);
3271 }
3272 elt = elt->next;
3273 }
3274 g_slist_free(list);
3275}
3276
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003277void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003278{
3279 GSList *list, *elt;
3280 size_t len;
3281
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003282 if (nb_args != 2) {
3283 return;
3284 }
3285
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003286 len = strlen(str);
3287 readline_set_completion_index(rs, len);
3288 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3289 while (elt) {
3290 const char *name;
3291
3292 name = object_class_get_name(OBJECT_CLASS(elt->data));
3293 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3294 readline_add_completion(rs, name);
3295 }
3296 elt = elt->next;
3297 }
3298 g_slist_free(list);
3299}
3300
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003301static void peripheral_device_del_completion(ReadLineState *rs,
3302 const char *str, size_t len)
3303{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003304 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3305 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003306
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003307 list = qdev_build_hotpluggable_device_list(peripheral);
3308 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003309 return;
3310 }
3311
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003312 for (item = list; item; item = g_slist_next(item)) {
3313 DeviceState *dev = item->data;
3314
3315 if (dev->id && !strncmp(str, dev->id, len)) {
3316 readline_add_completion(rs, dev->id);
3317 }
3318 }
3319
3320 g_slist_free(list);
3321}
3322
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003323void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3324{
3325 size_t len;
3326 ChardevInfoList *list, *start;
3327
3328 if (nb_args != 2) {
3329 return;
3330 }
3331 len = strlen(str);
3332 readline_set_completion_index(rs, len);
3333
3334 start = list = qmp_query_chardev(NULL);
3335 while (list) {
3336 ChardevInfo *chr = list->value;
3337
3338 if (!strncmp(chr->label, str, len)) {
3339 readline_add_completion(rs, chr->label);
3340 }
3341 list = list->next;
3342 }
3343 qapi_free_ChardevInfoList(start);
3344}
3345
Hani Benhabiles8e597772014-05-27 23:39:30 +01003346static void ringbuf_completion(ReadLineState *rs, const char *str)
3347{
3348 size_t len;
3349 ChardevInfoList *list, *start;
3350
3351 len = strlen(str);
3352 readline_set_completion_index(rs, len);
3353
3354 start = list = qmp_query_chardev(NULL);
3355 while (list) {
3356 ChardevInfo *chr_info = list->value;
3357
3358 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003359 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003360 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003361 readline_add_completion(rs, chr_info->label);
3362 }
3363 }
3364 list = list->next;
3365 }
3366 qapi_free_ChardevInfoList(start);
3367}
3368
Hani Benhabiles8e597772014-05-27 23:39:30 +01003369void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3370{
3371 if (nb_args != 2) {
3372 return;
3373 }
3374 ringbuf_completion(rs, str);
3375}
3376
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003377void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3378{
3379 size_t len;
3380
3381 if (nb_args != 2) {
3382 return;
3383 }
3384
3385 len = strlen(str);
3386 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003387 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003388}
3389
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003390void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003391{
3392 ObjectPropertyInfoList *list, *start;
3393 size_t len;
3394
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003395 if (nb_args != 2) {
3396 return;
3397 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003398 len = strlen(str);
3399 readline_set_completion_index(rs, len);
3400
3401 start = list = qmp_qom_list("/objects", NULL);
3402 while (list) {
3403 ObjectPropertyInfo *info = list->value;
3404
3405 if (!strncmp(info->type, "child<", 5)
3406 && !strncmp(info->name, str, len)) {
3407 readline_add_completion(rs, info->name);
3408 }
3409 list = list->next;
3410 }
3411 qapi_free_ObjectPropertyInfoList(start);
3412}
3413
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003414void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3415{
3416 int i;
3417 char *sep;
3418 size_t len;
3419
3420 if (nb_args != 2) {
3421 return;
3422 }
3423 sep = strrchr(str, '-');
3424 if (sep) {
3425 str = sep + 1;
3426 }
3427 len = strlen(str);
3428 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003429 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003430 if (!strncmp(str, QKeyCode_str(i), len)) {
3431 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003432 }
3433 }
3434}
3435
Hani Benhabiles40d19392014-05-07 23:41:30 +01003436void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3437{
3438 size_t len;
3439
3440 len = strlen(str);
3441 readline_set_completion_index(rs, len);
3442 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003443 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003444 int count, i;
3445 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003446 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003447 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003448 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003449 const char *name = ncs[i]->name;
3450 if (!strncmp(str, name, len)) {
3451 readline_add_completion(rs, name);
3452 }
3453 }
3454 } else if (nb_args == 3) {
3455 add_completion_option(rs, str, "on");
3456 add_completion_option(rs, str, "off");
3457 }
3458}
3459
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003460void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3461{
3462 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003463 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003464
3465 if (nb_args != 2) {
3466 return;
3467 }
3468
3469 len = strlen(str);
3470 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003471 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003472 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003473 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003474 QemuOpts *opts;
3475 const char *name = ncs[i]->name;
3476 if (strncmp(str, name, len)) {
3477 continue;
3478 }
3479 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3480 if (opts) {
3481 readline_add_completion(rs, name);
3482 }
3483 }
3484}
3485
Lluís Vilanovabd712112016-07-11 12:53:51 +02003486void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3487{
3488 size_t len;
3489
3490 len = strlen(str);
3491 readline_set_completion_index(rs, len);
3492 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003493 TraceEventIter iter;
3494 TraceEvent *ev;
3495 char *pattern = g_strdup_printf("%s*", str);
3496 trace_event_iter_init(&iter, pattern);
3497 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3498 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003499 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003500 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003501 }
3502}
3503
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003504void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3505{
3506 size_t len;
3507
3508 len = strlen(str);
3509 readline_set_completion_index(rs, len);
3510 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003511 TraceEventIter iter;
3512 TraceEvent *ev;
3513 char *pattern = g_strdup_printf("%s*", str);
3514 trace_event_iter_init(&iter, pattern);
3515 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3516 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003517 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003518 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003519 } else if (nb_args == 3) {
3520 add_completion_option(rs, str, "on");
3521 add_completion_option(rs, str, "off");
3522 }
3523}
3524
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003525void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3526{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003527 int i;
3528
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003529 if (nb_args != 2) {
3530 return;
3531 }
3532 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003533 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3534 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003535 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003536}
3537
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003538void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3539 const char *str)
3540{
3541 size_t len;
3542
3543 len = strlen(str);
3544 readline_set_completion_index(rs, len);
3545 if (nb_args == 2) {
3546 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003547 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003548 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003549 if (!strncmp(str, name, len)) {
3550 readline_add_completion(rs, name);
3551 }
3552 }
3553 } else if (nb_args == 3) {
3554 add_completion_option(rs, str, "on");
3555 add_completion_option(rs, str, "off");
3556 }
3557}
3558
Liang Li50e9a622015-03-23 16:32:29 +08003559void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3560 const char *str)
3561{
3562 size_t len;
3563
3564 len = strlen(str);
3565 readline_set_completion_index(rs, len);
3566 if (nb_args == 2) {
3567 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003568 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003569 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003570 if (!strncmp(str, name, len)) {
3571 readline_add_completion(rs, name);
3572 }
3573 }
3574 }
3575}
3576
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003577void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3578{
3579 int i;
3580 size_t len;
3581 if (nb_args != 2) {
3582 return;
3583 }
3584 len = strlen(str);
3585 readline_set_completion_index(rs, len);
3586 for (i = 0; host_net_devices[i]; i++) {
3587 if (!strncmp(host_net_devices[i], str, len)) {
3588 readline_add_completion(rs, host_net_devices[i]);
3589 }
3590 }
3591}
3592
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003593void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3594{
Jason Wangeaed4832015-04-23 14:21:38 +08003595 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003596 int count, i, len;
3597
3598 len = strlen(str);
3599 readline_set_completion_index(rs, len);
3600 if (nb_args == 2) {
3601 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003602 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003603 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003604 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003605 int id;
3606 char name[16];
3607
3608 if (net_hub_id_for_client(ncs[i], &id)) {
3609 continue;
3610 }
3611 snprintf(name, sizeof(name), "%d", id);
3612 if (!strncmp(str, name, len)) {
3613 readline_add_completion(rs, name);
3614 }
3615 }
3616 return;
3617 } else if (nb_args == 3) {
3618 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003619 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003620 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003621 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003622 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003623 const char *name;
3624
Eric Blakef394b2e2016-07-13 21:50:23 -06003625 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003626 net_hub_id_for_client(ncs[i], &id)) {
3627 continue;
3628 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003629 name = ncs[i]->name;
3630 if (!strncmp(str, name, len)) {
3631 readline_add_completion(rs, name);
3632 }
3633 }
3634 return;
3635 }
3636}
3637
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003638static void vm_completion(ReadLineState *rs, const char *str)
3639{
3640 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003641 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003642 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003643
3644 len = strlen(str);
3645 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003646
Kevin Wolf88be7b42016-05-20 18:49:07 +02003647 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003648 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003649 AioContext *ctx = bdrv_get_aio_context(bs);
3650 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003651
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003652 aio_context_acquire(ctx);
3653 if (bdrv_can_snapshot(bs)) {
3654 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3655 }
3656 aio_context_release(ctx);
3657 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003658 continue;
3659 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003660
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003661 snapshot = snapshots;
3662 while (snapshot) {
3663 char *completion = snapshot->value->name;
3664 if (!strncmp(str, completion, len)) {
3665 readline_add_completion(rs, completion);
3666 }
3667 completion = snapshot->value->id;
3668 if (!strncmp(str, completion, len)) {
3669 readline_add_completion(rs, completion);
3670 }
3671 snapshot = snapshot->next;
3672 }
3673 qapi_free_SnapshotInfoList(snapshots);
3674 }
3675
3676}
3677
3678void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3679{
3680 if (nb_args == 2) {
3681 vm_completion(rs, str);
3682 }
3683}
3684
3685void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3686{
3687 if (nb_args == 2) {
3688 vm_completion(rs, str);
3689 }
3690}
3691
Wenchao Xiac35b6402013-08-27 20:38:24 +08003692static void monitor_find_completion_by_table(Monitor *mon,
3693 const mon_cmd_t *cmd_table,
3694 char **args,
3695 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003696{
3697 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003698 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003699 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003700 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003701 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003702
bellard81d09122004-07-14 17:21:37 +00003703 if (nb_args <= 1) {
3704 /* command completion */
3705 if (nb_args == 0)
3706 cmdname = "";
3707 else
3708 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003709 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003710 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003711 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003712 }
3713 } else {
3714 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003715 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003716 if (compare_cmd(args[0], cmd->name)) {
3717 break;
3718 }
bellard81d09122004-07-14 17:21:37 +00003719 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003720 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003721 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003722 }
3723
Wenchao Xiad903a772013-08-27 20:38:25 +08003724 if (cmd->sub_table) {
3725 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003726 monitor_find_completion_by_table(mon, cmd->sub_table,
3727 &args[1], nb_args - 1);
3728 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003729 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003730 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003731 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3732 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003733 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003734
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003735 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003736 for(i = 0; i < nb_args - 2; i++) {
3737 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003738 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003739 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003740 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003741 }
3742 }
3743 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003744 old_ptype = NULL;
3745 while (*ptype == '-' && old_ptype != ptype) {
3746 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003747 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003748 }
bellard81d09122004-07-14 17:21:37 +00003749 switch(*ptype) {
3750 case 'F':
3751 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003752 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003753 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003754 break;
3755 case 'B':
3756 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003757 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003758 while ((blk = blk_next(blk)) != NULL) {
3759 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003760 if (str[0] == '\0' ||
3761 !strncmp(name, str, strlen(str))) {
3762 readline_add_completion(mon->rs, name);
3763 }
3764 }
bellard81d09122004-07-14 17:21:37 +00003765 break;
bellard7fe48482004-10-09 18:08:01 +00003766 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003767 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003768 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003769 monitor_find_completion_by_table(mon, cmd_table,
3770 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003771 }
3772 break;
bellard81d09122004-07-14 17:21:37 +00003773 default:
3774 break;
3775 }
3776 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003777}
3778
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003779static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003780 const char *cmdline)
3781{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003782 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003783 char *args[MAX_ARGS];
3784 int nb_args, len;
3785
3786 /* 1. parse the cmdline */
3787 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3788 return;
3789 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003790
3791 /* if the line ends with a space, it means we want to complete the
3792 next arg */
3793 len = strlen(cmdline);
3794 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3795 if (nb_args >= MAX_ARGS) {
3796 goto cleanup;
3797 }
3798 args[nb_args++] = g_strdup("");
3799 }
3800
3801 /* 2. auto complete according to args */
3802 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003803
3804cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003805 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003806}
3807
aliguori731b0362009-03-05 23:01:42 +00003808static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003809{
aliguori731b0362009-03-05 23:01:42 +00003810 Monitor *mon = opaque;
3811
Jan Kiszkac62313b2009-12-04 14:05:29 +01003812 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003813}
3814
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003815static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003816{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003817 QObject *req, *rsp = NULL, *id = NULL;
3818 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003819 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003820 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003821
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003822 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003823 if (!req && !err) {
3824 /* json_parser_parse_err() sucks: can fail without setting @err */
3825 error_setg(&err, QERR_JSON_PARSING);
3826 }
3827 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003828 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003829 }
3830
Markus Armbruster104fc302017-03-03 13:32:28 +01003831 qdict = qobject_to_qdict(req);
3832 if (qdict) {
3833 id = qdict_get(qdict, "id");
3834 qobject_incref(id);
3835 qdict_del(qdict, "id");
3836 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003837
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03003838 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
3839 QString *req_json = qobject_to_json(req);
3840 trace_handle_qmp_command(mon, qstring_get_str(req_json));
3841 QDECREF(req_json);
3842 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003843
Markus Armbruster635db182017-03-03 13:32:27 +01003844 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003845
Markus Armbruster635db182017-03-03 13:32:27 +01003846 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3847 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3848 if (qdict
3849 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
Markus Armbruster977c7362017-08-24 10:46:08 +02003850 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
Markus Armbruster635db182017-03-03 13:32:27 +01003851 /* Provide a more useful error message */
3852 qdict_del(qdict, "desc");
Eric Blake46f5ac22017-04-27 16:58:17 -05003853 qdict_put_str(qdict, "desc", "Expecting capabilities negotiation"
3854 " with 'qmp_capabilities'");
Markus Armbruster635db182017-03-03 13:32:27 +01003855 }
3856 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003857
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003858err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003859 if (err) {
3860 qdict = qdict_new();
3861 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3862 error_free(err);
3863 rsp = QOBJECT(qdict);
3864 }
3865
3866 if (rsp) {
3867 if (id) {
3868 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3869 id = NULL;
3870 }
3871
3872 monitor_json_emitter(mon, rsp);
3873 }
3874
3875 qobject_decref(id);
3876 qobject_decref(rsp);
3877 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003878}
3879
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003880static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003881{
3882 Monitor *old_mon = cur_mon;
3883
3884 cur_mon = opaque;
3885
Markus Armbruster74358f22015-03-06 19:35:59 +01003886 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003887
3888 cur_mon = old_mon;
3889}
3890
aliguori731b0362009-03-05 23:01:42 +00003891static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003892{
aliguori731b0362009-03-05 23:01:42 +00003893 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003894 int i;
aliguori376253e2009-03-05 23:01:23 +00003895
aliguori731b0362009-03-05 23:01:42 +00003896 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003897
aliguoricde76ee2009-03-05 23:01:51 +00003898 if (cur_mon->rs) {
3899 for (i = 0; i < size; i++)
3900 readline_handle_byte(cur_mon->rs, buf[i]);
3901 } else {
3902 if (size == 0 || buf[size - 1] != 0)
3903 monitor_printf(cur_mon, "corrupted command\n");
3904 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003905 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003906 }
aliguori731b0362009-03-05 23:01:42 +00003907
3908 cur_mon = old_mon;
3909}
aliguorid8f44602008-10-06 13:52:44 +00003910
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003911static void monitor_command_cb(void *opaque, const char *cmdline,
3912 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003913{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003914 Monitor *mon = opaque;
3915
aliguori731b0362009-03-05 23:01:42 +00003916 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003917 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003918 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003919}
3920
aliguoricde76ee2009-03-05 23:01:51 +00003921int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003922{
aliguoricde76ee2009-03-05 23:01:51 +00003923 if (!mon->rs)
3924 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003925 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003926 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003927}
3928
aliguori376253e2009-03-05 23:01:23 +00003929void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003930{
aliguoricde76ee2009-03-05 23:01:51 +00003931 if (!mon->rs)
3932 return;
aliguori731b0362009-03-05 23:01:42 +00003933 if (--mon->suspend_cnt == 0)
3934 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003935}
3936
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003937static QObject *get_qmp_greeting(void)
3938{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003939 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003940
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003941 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003942
3943 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3944 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003945}
3946
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003947static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003948{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003949 QObject *data;
3950 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003951
Luiz Capitulino47116d12010-02-08 17:01:30 -02003952 switch (event) {
3953 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003954 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003955 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003956 monitor_json_emitter(mon, data);
3957 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003958 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003959 break;
3960 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003961 json_message_parser_destroy(&mon->qmp.parser);
3962 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003963 mon_refcount--;
3964 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003965 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003966 }
3967}
3968
aliguori731b0362009-03-05 23:01:42 +00003969static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003970{
aliguori376253e2009-03-05 23:01:23 +00003971 Monitor *mon = opaque;
3972
aliguori2724b182009-03-05 23:01:47 +00003973 switch (event) {
3974 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003975 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003976 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003977 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003978 if (mon->reset_seen) {
3979 readline_restart(mon->rs);
3980 monitor_resume(mon);
3981 monitor_flush(mon);
3982 } else {
3983 mon->suspend_cnt = 0;
3984 }
aliguori2724b182009-03-05 23:01:47 +00003985 break;
ths86e94de2007-01-05 22:01:59 +00003986
aliguori2724b182009-03-05 23:01:47 +00003987 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003988 if (mon->reset_seen) {
3989 if (mon->suspend_cnt == 0) {
3990 monitor_printf(mon, "\n");
3991 }
3992 monitor_flush(mon);
3993 monitor_suspend(mon);
3994 } else {
3995 mon->suspend_cnt++;
3996 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003997 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003998 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003999 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004000 break;
4001
Amit Shahb6b8df52009-10-07 18:31:16 +05304002 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004003 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4004 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004005 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004006 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004007 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004008 }
4009 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004010 mon_refcount++;
4011 break;
4012
4013 case CHR_EVENT_CLOSED:
4014 mon_refcount--;
4015 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004016 break;
4017 }
ths86e94de2007-01-05 22:01:59 +00004018}
4019
Wayne Xia816f8922011-10-12 11:32:41 +08004020static int
4021compare_mon_cmd(const void *a, const void *b)
4022{
4023 return strcmp(((const mon_cmd_t *)a)->name,
4024 ((const mon_cmd_t *)b)->name);
4025}
4026
4027static void sortcmdlist(void)
4028{
4029 int array_num;
4030 int elem_size = sizeof(mon_cmd_t);
4031
4032 array_num = sizeof(mon_cmds)/elem_size-1;
4033 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4034
4035 array_num = sizeof(info_cmds)/elem_size-1;
4036 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4037}
4038
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004039/* These functions just adapt the readline interface in a typesafe way. We
4040 * could cast function pointers but that discards compiler checks.
4041 */
Stefan Weild5d15072014-01-25 18:18:23 +01004042static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4043 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004044{
4045 va_list ap;
4046 va_start(ap, fmt);
4047 monitor_vprintf(opaque, fmt, ap);
4048 va_end(ap);
4049}
4050
4051static void monitor_readline_flush(void *opaque)
4052{
4053 monitor_flush(opaque);
4054}
4055
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004056/*
4057 * Print to current monitor if we have one, else to stderr.
4058 * TODO should return int, so callers can calculate width, but that
4059 * requires surgery to monitor_vprintf(). Left for another day.
4060 */
4061void error_vprintf(const char *fmt, va_list ap)
4062{
4063 if (cur_mon && !monitor_cur_is_qmp()) {
4064 monitor_vprintf(cur_mon, fmt, ap);
4065 } else {
4066 vfprintf(stderr, fmt, ap);
4067 }
4068}
4069
4070void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4071{
4072 if (cur_mon && !monitor_cur_is_qmp()) {
4073 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004074 } else if (!cur_mon) {
4075 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004076 }
4077}
4078
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004079static void __attribute__((constructor)) monitor_lock_init(void)
4080{
4081 qemu_mutex_init(&monitor_lock);
4082}
4083
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004084void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004085{
aliguori731b0362009-03-05 23:01:42 +00004086 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004087 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004088
4089 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004090 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004091 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004092 is_first_init = 0;
4093 }
aliguori87127162009-03-05 23:01:29 +00004094
Wenchao Xiab01fe892013-08-27 20:38:19 +08004095 mon = g_malloc(sizeof(*mon));
4096 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004097
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004098 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004099 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004100 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004101 mon->rs = readline_init(monitor_readline_printf,
4102 monitor_readline_flush,
4103 mon,
4104 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004105 monitor_read_command(mon, 0);
4106 }
aliguori87127162009-03-05 23:01:29 +00004107
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004108 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004109 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004110 monitor_qmp_event, NULL, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004111 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004112 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004113 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004114 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004115 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004116 }
aliguori87127162009-03-05 23:01:29 +00004117
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004118 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004119 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004120 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004121}
4122
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004123void monitor_cleanup(void)
4124{
4125 Monitor *mon, *next;
4126
4127 qemu_mutex_lock(&monitor_lock);
4128 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4129 QLIST_REMOVE(mon, entry);
4130 monitor_data_destroy(mon);
4131 g_free(mon);
4132 }
4133 qemu_mutex_unlock(&monitor_lock);
4134}
4135
Paolo Bonzini4d454572012-11-26 16:03:42 +01004136QemuOptsList qemu_mon_opts = {
4137 .name = "mon",
4138 .implied_opt_name = "chardev",
4139 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4140 .desc = {
4141 {
4142 .name = "mode",
4143 .type = QEMU_OPT_STRING,
4144 },{
4145 .name = "chardev",
4146 .type = QEMU_OPT_STRING,
4147 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004148 .name = "pretty",
4149 .type = QEMU_OPT_BOOL,
4150 },
4151 { /* end of list */ }
4152 },
4153};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004154
4155#ifndef TARGET_I386
4156void qmp_rtc_reset_reinjection(Error **errp)
4157{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004158 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004159}
4160#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004161
4162#ifndef TARGET_S390X
4163void qmp_dump_skeys(const char *filename, Error **errp)
4164{
4165 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4166}
4167#endif
Peter Xuae50a772016-03-30 17:27:24 +01004168
4169#ifndef TARGET_ARM
4170GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4171{
4172 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4173 return NULL;
4174}
4175#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304176
4177HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4178{
4179 MachineState *ms = MACHINE(qdev_get_machine());
4180 MachineClass *mc = MACHINE_GET_CLASS(ms);
4181
Igor Mammedovc5514d02017-02-10 11:20:57 +01004182 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304183 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4184 return NULL;
4185 }
4186
Igor Mammedovc5514d02017-02-10 11:20:57 +01004187 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304188}