blob: 10be8bdb8641893b7e766f4aa0e8e4de76ff89d7 [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"
Philippe Mathieu-Daudé8ec338a2018-06-25 09:42:34 -030026#include "qemu/units.h"
blueswir1511d2b12009-03-07 15:32:56 +000027#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010028#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010030#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000031#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040038#include "chardev/char-fe.h"
Peter Xua5ed3522018-03-09 16:59:52 +080039#include "chardev/char-io.h"
Peter Xube933ff2018-03-26 14:38:56 +080040#include "chardev/char-mux.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020041#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020042#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010043#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030044#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010045#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010046#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010047#include "ui/input.h"
Max Reitzda27a002016-03-16 19:54:29 +010048#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000049#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020050#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010051#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010053#include "sysemu/hw_accel.h"
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +000054#include "authz/list.h"
55#include "qapi/util.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020056#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010057#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010058#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010059#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010060#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010061#include "qapi/qmp/qjson.h"
Markus Armbruster86cdf9e2018-08-23 18:40:20 +020062#include "qapi/qmp/json-parser.h"
Peter Xu02130312018-03-09 16:59:53 +080063#include "qapi/qmp/qlist.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020064#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000065#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030067#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020068#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020069#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053070#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010071#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010072#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010073#include "qemu/log.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010074#include "qemu/option.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050075#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010076#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010077#include "block/qapi.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010078#include "qapi/qapi-commands.h"
Markus Armbruster5d756482019-02-14 16:22:38 +010079#include "qapi/qapi-emit-events.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010080#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020081#include "qapi/qmp-event.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010082#include "qapi/qapi-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020083#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010084#include "sysemu/cpus.h"
Peter Xua5ed3522018-03-09 16:59:52 +080085#include "sysemu/iothread.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020086#include "qemu/cutils.h"
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -040087#include "tcg/tcg.h"
ths6a5bd302007-12-03 17:05:38 +000088
Jason J. Hernea4538a52015-06-26 14:07:21 -040089#if defined(TARGET_S390X)
90#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020091#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040092#endif
93
bellard9307c4c2004-04-04 12:57:25 +000094/*
95 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000096 *
bellard9307c4c2004-04-04 12:57:25 +000097 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000098 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000099 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +0800100 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100101 * 'O' option string of the form NAME=VALUE,...
102 * parsed according to QemuOptsList given by its name
103 * Example: 'device:O' uses qemu_device_opts.
104 * Restriction: only lists with empty desc are supported
105 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000106 * 'i' 32 bit integer
107 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300108 * 'M' Non-negative target long (32 or 64 bit), in user mode the
109 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200110 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200111 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
112 * K, k suffix, which multiplies the value by 2^60 for suffixes E
113 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
114 * 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 +0100115 * 'T' double
116 * user mode accepts an optional ms, us, ns suffix,
117 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000118 * '/' optional gdb-like print format (like "/10x")
119 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300120 * '?' optional type (for all types, except '/')
121 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100122 * 'b' boolean
123 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300124 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000125 *
126 */
127
Anthony Liguoric227f092009-10-01 16:12:16 -0500128typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000129 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000130 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000131 const char *params;
132 const char *help;
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100133 const char *flags; /* p=preconfig */
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400134 void (*cmd)(Monitor *mon, const QDict *qdict);
135 /* @sub_table is a list of 2nd level of commands. If it does not exist,
136 * cmd should be used. If it exists, sub_table[?].cmd should be
137 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800138 */
139 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100140 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500141} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000142
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500144typedef struct mon_fd_t mon_fd_t;
145struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146 char *name;
147 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500148 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100149};
150
Corey Bryantba1c0482012-08-14 16:43:43 -0400151/* file descriptor associated with a file descriptor set */
152typedef struct MonFdsetFd MonFdsetFd;
153struct MonFdsetFd {
154 int fd;
155 bool removed;
156 char *opaque;
157 QLIST_ENTRY(MonFdsetFd) next;
158};
159
160/* file descriptor set containing fds passed via SCM_RIGHTS */
161typedef struct MonFdset MonFdset;
162struct MonFdset {
163 int64_t id;
164 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400165 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400166 QLIST_ENTRY(MonFdset) next;
167};
168
Markus Armbruster74358f22015-03-06 19:35:59 +0100169typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200170 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100171 /*
172 * When a client connects, we're in capabilities negotiation mode.
Markus Armbruster774a6b62018-07-03 10:53:57 +0200173 * @commands is &qmp_cap_negotiation_commands then. When command
174 * qmp_capabilities succeeds, we go into command mode, and
175 * @command becomes &qmp_commands.
Markus Armbrusterf994b252015-03-06 19:51:51 +0100176 */
Markus Armbruster635db182017-03-03 13:32:27 +0100177 QmpCommandList *commands;
Markus Armbruster279f9e02018-07-03 10:53:56 +0200178 bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
179 bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */
Peter Xu71da4662018-03-09 16:59:57 +0800180 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200181 * Protects qmp request/response queue.
182 * Take monitor_lock first when you need both.
Peter Xu71da4662018-03-09 16:59:57 +0800183 */
184 QemuMutex qmp_queue_lock;
185 /* Input queue that holds all the parsed QMP requests */
186 GQueue *qmp_requests;
Markus Armbruster74358f22015-03-06 19:35:59 +0100187} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200188
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100189/*
190 * To prevent flooding clients, events can be throttled. The
191 * throttling is calculated globally, rather than per-Monitor
192 * instance.
193 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200194typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200195 QAPIEvent event; /* Throttling state for this event type and... */
196 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100197 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200198 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200199} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100200
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200201typedef struct {
202 int64_t rate; /* Minimum time (in ns) between two events */
203} MonitorQAPIEventConf;
204
aliguori87127162009-03-05 23:01:29 +0000205struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300206 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200207 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000208 int flags;
Peter Xudf152fb2018-03-09 16:59:55 +0800209 int suspend_cnt; /* Needs to be accessed atomically */
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400210 bool skip_flush;
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200211 bool use_io_thread;
Peter Xud9f25282018-06-08 11:55:07 +0800212
213 /*
214 * State used only in the thread "owning" the monitor.
Markus Armbrustercab5ad82018-07-03 10:53:46 +0200215 * If @use_io_thread, this is @mon_iothread.
Peter Xud9f25282018-06-08 11:55:07 +0800216 * Else, it's the main thread.
217 * These members can be safely accessed without locks.
218 */
aliguori731b0362009-03-05 23:01:42 +0000219 ReadLineState *rs;
Peter Xud9f25282018-06-08 11:55:07 +0800220
Markus Armbruster74358f22015-03-06 19:35:59 +0100221 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200222 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200223 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000224 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800225 mon_cmd_t *cmd_table;
Peter Xu238d9f32018-03-09 16:59:51 +0800226 QTAILQ_ENTRY(Monitor) entry;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800227
228 /*
229 * The per-monitor lock. We can't access guest memory when holding
230 * the lock.
231 */
232 QemuMutex mon_lock;
233
234 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200235 * Members that are protected by the per-monitor lock
Peter Xudc7cbcd2018-06-08 11:55:05 +0800236 */
Peter Xu9409fc02018-06-08 11:55:06 +0800237 QLIST_HEAD(, mon_fd_t) fds;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800238 QString *outbuf;
239 guint out_watch;
240 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
241 int mux_out;
aliguori87127162009-03-05 23:01:29 +0000242};
243
Markus Armbruster774a6b62018-07-03 10:53:57 +0200244/* Shared monitor I/O thread */
Markus Armbrustercab5ad82018-07-03 10:53:46 +0200245IOThread *mon_iothread;
246
247/* Bottom half to dispatch the requests received from I/O thread */
248QEMUBH *qmp_dispatcher_bh;
249
Peter Xu6d2d5632018-03-26 14:38:55 +0800250struct QMPRequest {
251 /* Owner of the request */
252 Monitor *mon;
Markus Armbruster1cc37472018-07-03 10:53:44 +0200253 /*
254 * Request object to be handled or Error to be reported
255 * (exactly one of them is non-null)
256 */
Peter Xu6d2d5632018-03-26 14:38:55 +0800257 QObject *req;
Markus Armbruster1cc37472018-07-03 10:53:44 +0200258 Error *err;
Peter Xu6d2d5632018-03-26 14:38:55 +0800259};
260typedef struct QMPRequest QMPRequest;
261
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300262/* QMP checker flags */
263#define QMP_ACCEPT_UNKNOWNS 1
264
Marc-André Lureau8dac00b2018-12-06 00:37:36 +0400265/* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200266static QemuMutex monitor_lock;
Peter Xu095cb1b2018-06-08 11:55:08 +0800267static GHashTable *monitor_qapi_event_state;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100268static QTAILQ_HEAD(, Monitor) mon_list;
Marc-André Lureau8dac00b2018-12-06 00:37:36 +0400269static bool monitor_destroyed;
Peter Xu095cb1b2018-06-08 11:55:08 +0800270
Peter Xu47451462018-06-08 11:55:11 +0800271/* Protects mon_fdsets */
272static QemuMutex mon_fdsets_lock;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100273static QLIST_HEAD(, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800274
Corey Bryantefb87c12012-08-14 16:43:48 -0400275static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000276
Wayne Xia816f8922011-10-12 11:32:41 +0800277static mon_cmd_t mon_cmds[];
278static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000279
Markus Armbruster635db182017-03-03 13:32:27 +0100280QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100281
Peter Xu62aa1d82018-07-20 11:34:51 +0800282__thread Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000283
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100284static void monitor_command_cb(void *opaque, const char *cmdline,
285 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000286
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100287/**
288 * Is @mon a QMP monitor?
289 */
290static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200291{
292 return (mon->flags & MONITOR_USE_CONTROL);
293}
294
Markus Armbruster489653b2015-03-06 20:01:05 +0100295/**
Markus Armbruster774a6b62018-07-03 10:53:57 +0200296 * Is @mon is using readline?
297 * Note: not all HMP monitors use readline, e.g., gdbserver has a
298 * non-interactive HMP monitor, so readline is not used there.
Peter Xue3e977d2018-03-09 16:59:56 +0800299 */
300static inline bool monitor_uses_readline(const Monitor *mon)
301{
302 return mon->flags & MONITOR_USE_READLINE;
303}
304
305static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
306{
307 return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
308}
309
Peter Xu6e8c5f42018-06-08 11:55:09 +0800310/*
311 * Return the clock to use for recording an event's time.
Markus Armbruster774a6b62018-07-03 10:53:57 +0200312 * It's QEMU_CLOCK_REALTIME, except for qtests it's
313 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
Peter Xu6e8c5f42018-06-08 11:55:09 +0800314 * Beware: result is invalid before configure_accelerator().
315 */
316static inline QEMUClockType monitor_get_event_clock(void)
317{
Peter Xu6e8c5f42018-06-08 11:55:09 +0800318 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
319}
320
Peter Xue3e977d2018-03-09 16:59:56 +0800321/**
Markus Armbruster489653b2015-03-06 20:01:05 +0100322 * Is the current monitor, if any, a QMP monitor?
323 */
324bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100325{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100326 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100327}
328
Anthony Liguori7060b472011-09-02 12:34:50 -0500329void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000330{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200331 if (!mon->rs)
332 return;
333
aliguori731b0362009-03-05 23:01:42 +0000334 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
335 if (show_prompt)
336 readline_show_prompt(mon->rs);
337}
bellard6a00d602005-11-21 23:25:50 +0000338
Anthony Liguori7060b472011-09-02 12:34:50 -0500339int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
340 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000341{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100342 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000343 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
344 /* prompt is printed on return from the command handler */
345 return 0;
346 } else {
347 monitor_printf(mon, "terminal does not support password prompting\n");
348 return -ENOTTY;
349 }
aliguoribb5fc202009-03-05 23:01:15 +0000350}
351
Peter Xu6d2d5632018-03-26 14:38:55 +0800352static void qmp_request_free(QMPRequest *req)
353{
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200354 qobject_unref(req->req);
Markus Armbruster1cc37472018-07-03 10:53:44 +0200355 error_free(req->err);
Peter Xu6d2d5632018-03-26 14:38:55 +0800356 g_free(req);
357}
358
Markus Armbruster774a6b62018-07-03 10:53:57 +0200359/* Caller must hold mon->qmp.qmp_queue_lock */
Peter Xu6d2d5632018-03-26 14:38:55 +0800360static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon)
361{
362 while (!g_queue_is_empty(mon->qmp.qmp_requests)) {
363 qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests));
364 }
365}
366
Peter Xu6d2d5632018-03-26 14:38:55 +0800367static void monitor_qmp_cleanup_queues(Monitor *mon)
368{
369 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
370 monitor_qmp_cleanup_req_queue_locked(mon);
Peter Xu6d2d5632018-03-26 14:38:55 +0800371 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
372}
373
374
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200375static void monitor_flush_locked(Monitor *mon);
376
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100377static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
378 void *opaque)
379{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200380 Monitor *mon = opaque;
381
Peter Xudc7cbcd2018-06-08 11:55:05 +0800382 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200383 mon->out_watch = 0;
384 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800385 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100386 return FALSE;
387}
388
Markus Armbruster774a6b62018-07-03 10:53:57 +0200389/* Caller must hold mon->mon_lock */
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200390static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000391{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100392 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400393 size_t len;
394 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100395
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400396 if (mon->skip_flush) {
397 return;
398 }
399
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400400 buf = qstring_get_str(mon->outbuf);
401 len = qstring_get_length(mon->outbuf);
402
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200403 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300404 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200405 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
406 /* all flushed or error */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200407 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400408 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100409 return;
410 }
411 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600412 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400413 QString *tmp = qstring_from_str(buf + rc);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200414 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400415 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100416 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200417 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300418 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300419 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300420 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200421 }
bellard7e2515e2004-08-01 21:52:19 +0000422 }
423}
424
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200425void monitor_flush(Monitor *mon)
426{
Peter Xudc7cbcd2018-06-08 11:55:05 +0800427 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200428 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800429 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200430}
431
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400432/* flush at every end of line */
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200433static int monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000434{
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200435 int i;
ths60fe76f2007-12-16 03:02:09 +0000436 char c;
aliguori731b0362009-03-05 23:01:42 +0000437
Peter Xudc7cbcd2018-06-08 11:55:05 +0800438 qemu_mutex_lock(&mon->mon_lock);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200439 for (i = 0; str[i]; i++) {
440 c = str[i];
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400441 if (c == '\n') {
442 qstring_append_chr(mon->outbuf, '\r');
443 }
444 qstring_append_chr(mon->outbuf, c);
445 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200446 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400447 }
bellard7e2515e2004-08-01 21:52:19 +0000448 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800449 qemu_mutex_unlock(&mon->mon_lock);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200450
451 return i;
bellard7e2515e2004-08-01 21:52:19 +0000452}
453
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200454int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000455{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400456 char *buf;
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200457 int n;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200458
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200459 if (!mon)
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200460 return -1;
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200461
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100462 if (monitor_is_qmp(mon)) {
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200463 return -1;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200464 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200465
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400466 buf = g_strdup_vprintf(fmt, ap);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200467 n = monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400468 g_free(buf);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200469 return n;
bellard7e2515e2004-08-01 21:52:19 +0000470}
471
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200472int monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000473{
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200474 int ret;
475
bellard7e2515e2004-08-01 21:52:19 +0000476 va_list ap;
477 va_start(ap, fmt);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200478 ret = monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000479 va_end(ap);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200480 return ret;
bellard9dc39cb2004-03-14 21:38:27 +0000481}
482
Pavel Butsykincaf15312015-09-22 16:18:17 +0300483int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000484{
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200485 int ret;
486
bellard7fe48482004-10-09 18:08:01 +0000487 va_list ap;
488 va_start(ap, fmt);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200489 ret = monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000490 va_end(ap);
Markus Armbruster679cb8e2019-04-17 21:06:36 +0200491 return ret;
bellard7fe48482004-10-09 18:08:01 +0000492}
493
Marc-André Lureau6cd112e2018-08-29 15:40:34 +0200494static void qmp_send_response(Monitor *mon, const QDict *rsp)
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200495{
Marc-André Lureau6cd112e2018-08-29 15:40:34 +0200496 const QObject *data = QOBJECT(rsp);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200497 QString *json;
498
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200499 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
500 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200501 assert(json != NULL);
502
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200503 qstring_append_chr(json, '\n');
504 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200505
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200506 qobject_unref(json);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200507}
508
Eric Blake7fb1cf12015-11-18 01:52:57 -0700509static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200510 /* Limit guest-triggerable events to 1 per second */
511 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
512 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
513 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
514 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
515 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
516 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
517};
518
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100519/*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200520 * Broadcast an event to all monitors.
521 * @qdict is the event object. Its member "event" must match @event.
522 * Caller must hold monitor_lock.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100523 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200524static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525{
526 Monitor *mon;
527
Markus Armbruster688b4b72015-10-15 17:08:30 +0200528 trace_monitor_protocol_event_emit(event, qdict);
Peter Xu238d9f32018-03-09 16:59:51 +0800529 QTAILQ_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100530 if (monitor_is_qmp(mon)
531 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Marc-André Lureau27656012018-08-29 15:40:36 +0200532 qmp_send_response(mon, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100533 }
534 }
535}
536
Markus Armbrustera24712a2015-10-15 17:08:34 +0200537static void monitor_qapi_event_handler(void *opaque);
538
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539/*
540 * Queue a new event for emission to Monitor instances,
541 * applying any rate limiting if required.
542 */
543static void
Marc-André Lureau9a105402018-07-31 17:01:44 +0200544monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200546 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200547 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200548
Eric Blake7fb1cf12015-11-18 01:52:57 -0700549 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200550 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200551 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100552
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200553 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200554
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200555 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200556 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200557 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558 } else {
Max Reitz7dc847e2018-02-24 16:40:29 +0100559 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
Markus Armbruster7de0be62015-10-15 17:08:35 +0200560 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200561
562 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
563 assert(!evstate || timer_pending(evstate->timer));
564
565 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200566 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200567 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200568 * last send. Store event for sending when timer fires,
569 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100570 */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200571 qobject_unref(evstate->qdict);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200572 evstate->qdict = qobject_ref(qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100573 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200574 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200575 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200576 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200577 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200578 * events arriving before then will be delayed until then.
579 */
Peter Xu6e8c5f42018-06-08 11:55:09 +0800580 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200581
Markus Armbruster688b4b72015-10-15 17:08:30 +0200582 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200583
584 evstate = g_new(MonitorQAPIEventState, 1);
585 evstate->event = event;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200586 evstate->data = qobject_ref(data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200587 evstate->qdict = NULL;
Peter Xu6e8c5f42018-06-08 11:55:09 +0800588 evstate->timer = timer_new_ns(monitor_get_event_clock(),
Markus Armbrustera24712a2015-10-15 17:08:34 +0200589 monitor_qapi_event_handler,
590 evstate);
591 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200592 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100593 }
594 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200595
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200596 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100597}
598
Markus Armbrustera9529102018-12-18 19:22:21 +0100599void qapi_event_emit(QAPIEvent event, QDict *qdict)
Marc-André Lureau9a105402018-07-31 17:01:44 +0200600{
601 /*
602 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
603 * would deadlock on monitor_lock. Work around by queueing
604 * events in thread-local storage.
605 * TODO: remove this, make it re-enter safe.
606 */
607 typedef struct MonitorQapiEvent {
608 QAPIEvent event;
609 QDict *qdict;
610 QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
611 } MonitorQapiEvent;
612 static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
613 static __thread bool reentered;
614 MonitorQapiEvent *ev;
615
616 if (!reentered) {
617 QSIMPLEQ_INIT(&event_queue);
618 }
619
620 ev = g_new(MonitorQapiEvent, 1);
621 ev->qdict = qobject_ref(qdict);
622 ev->event = event;
623 QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
624 if (reentered) {
625 return;
626 }
627
628 reentered = true;
629
630 while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
631 QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
632 monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
633 qobject_unref(ev->qdict);
634 g_free(ev);
635 }
636
637 reentered = false;
638}
639
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100640/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200641 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200642 * event.
643 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100644 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200645static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100646{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200647 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200648 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100649
Markus Armbruster93f8f982015-10-15 17:08:31 +0200650 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200651 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200652
Markus Armbruster688b4b72015-10-15 17:08:30 +0200653 if (evstate->qdict) {
Peter Xu6e8c5f42018-06-08 11:55:09 +0800654 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200655
Markus Armbruster688b4b72015-10-15 17:08:30 +0200656 monitor_qapi_event_emit(evstate->event, evstate->qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200657 qobject_unref(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200658 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200659 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200660 } else {
661 g_hash_table_remove(monitor_qapi_event_state, evstate);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200662 qobject_unref(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200663 timer_free(evstate->timer);
664 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100665 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200666
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200667 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100668}
669
Markus Armbrustera24712a2015-10-15 17:08:34 +0200670static unsigned int qapi_event_throttle_hash(const void *key)
671{
672 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200673 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200674
Markus Armbruster7de0be62015-10-15 17:08:35 +0200675 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
676 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
677 }
678
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200679 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
680 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
681 }
682
Markus Armbruster7de0be62015-10-15 17:08:35 +0200683 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200684}
685
686static gboolean qapi_event_throttle_equal(const void *a, const void *b)
687{
688 const MonitorQAPIEventState *eva = a;
689 const MonitorQAPIEventState *evb = b;
690
Markus Armbruster7de0be62015-10-15 17:08:35 +0200691 if (eva->event != evb->event) {
692 return FALSE;
693 }
694
695 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
696 return !strcmp(qdict_get_str(eva->data, "id"),
697 qdict_get_str(evb->data, "id"));
698 }
699
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200700 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
701 return !strcmp(qdict_get_str(eva->data, "node-name"),
702 qdict_get_str(evb->data, "node-name"));
703 }
704
Markus Armbruster7de0be62015-10-15 17:08:35 +0200705 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200706}
707
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200708static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100709{
Markus Armbrustera24712a2015-10-15 17:08:34 +0200710 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
711 qapi_event_throttle_equal);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200712}
713
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100714static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200715
Wolfgang Bumiller85117702018-09-25 10:15:07 +0200716static void monitor_iothread_init(void);
717
Peter Xua5ed3522018-03-09 16:59:52 +0800718static void monitor_data_init(Monitor *mon, bool skip_flush,
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200719 bool use_io_thread)
Wenchao Xiab01fe892013-08-27 20:38:19 +0800720{
Wolfgang Bumiller85117702018-09-25 10:15:07 +0200721 if (use_io_thread && !mon_iothread) {
722 monitor_iothread_init();
723 }
Wenchao Xiab01fe892013-08-27 20:38:19 +0800724 memset(mon, 0, sizeof(Monitor));
Peter Xudc7cbcd2018-06-08 11:55:05 +0800725 qemu_mutex_init(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800726 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800727 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800728 /* Use *mon_cmds by default. */
729 mon->cmd_table = mon_cmds;
Peter Xu88a95d12018-03-09 16:59:48 +0800730 mon->skip_flush = skip_flush;
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200731 mon->use_io_thread = use_io_thread;
Peter Xu71da4662018-03-09 16:59:57 +0800732 mon->qmp.qmp_requests = g_queue_new();
Wenchao Xiab01fe892013-08-27 20:38:19 +0800733}
734
735static void monitor_data_destroy(Monitor *mon)
736{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200737 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400738 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400739 if (monitor_is_qmp(mon)) {
740 json_message_parser_destroy(&mon->qmp.parser);
741 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100742 readline_free(mon->rs);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200743 qobject_unref(mon->outbuf);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800744 qemu_mutex_destroy(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800745 qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
Peter Xu6d2d5632018-03-26 14:38:55 +0800746 monitor_qmp_cleanup_req_queue_locked(mon);
Peter Xu71da4662018-03-09 16:59:57 +0800747 g_queue_free(mon->qmp.qmp_requests);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800748}
749
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200750char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
751 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200752{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200753 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200754 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200755
Peter Xua5ed3522018-03-09 16:59:52 +0800756 monitor_data_init(&hmp, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200757
758 old_mon = cur_mon;
759 cur_mon = &hmp;
760
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200761 if (has_cpu_index) {
762 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200763 if (ret < 0) {
764 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100765 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
766 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200767 goto out;
768 }
769 }
770
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100771 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200772 cur_mon = old_mon;
773
Peter Xudc7cbcd2018-06-08 11:55:05 +0800774 qemu_mutex_lock(&hmp.mon_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400775 if (qstring_get_length(hmp.outbuf) > 0) {
776 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200777 } else {
778 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200779 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800780 qemu_mutex_unlock(&hmp.mon_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200781
782out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800783 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200784 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200785}
786
bellard9dc39cb2004-03-14 21:38:27 +0000787static int compare_cmd(const char *name, const char *list)
788{
789 const char *p, *pstart;
790 int len;
791 len = strlen(name);
792 p = list;
793 for(;;) {
794 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200795 p = qemu_strchrnul(p, '|');
bellard9dc39cb2004-03-14 21:38:27 +0000796 if ((p - pstart) == len && !memcmp(pstart, name, len))
797 return 1;
798 if (*p == '\0')
799 break;
800 p++;
801 }
802 return 0;
803}
804
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800805static int get_str(char *buf, int buf_size, const char **pp)
806{
807 const char *p;
808 char *q;
809 int c;
810
811 q = buf;
812 p = *pp;
813 while (qemu_isspace(*p)) {
814 p++;
815 }
816 if (*p == '\0') {
817 fail:
818 *q = '\0';
819 *pp = p;
820 return -1;
821 }
822 if (*p == '\"') {
823 p++;
824 while (*p != '\0' && *p != '\"') {
825 if (*p == '\\') {
826 p++;
827 c = *p++;
828 switch (c) {
829 case 'n':
830 c = '\n';
831 break;
832 case 'r':
833 c = '\r';
834 break;
835 case '\\':
836 case '\'':
837 case '\"':
838 break;
839 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100840 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800841 goto fail;
842 }
843 if ((q - buf) < buf_size - 1) {
844 *q++ = c;
845 }
846 } else {
847 if ((q - buf) < buf_size - 1) {
848 *q++ = *p;
849 }
850 p++;
851 }
852 }
853 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100854 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800855 goto fail;
856 }
857 p++;
858 } else {
859 while (*p != '\0' && !qemu_isspace(*p)) {
860 if ((q - buf) < buf_size - 1) {
861 *q++ = *p;
862 }
863 p++;
864 }
865 }
866 *q = '\0';
867 *pp = p;
868 return 0;
869}
870
871#define MAX_ARGS 16
872
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800873static void free_cmdline_args(char **args, int nb_args)
874{
875 int i;
876
877 assert(nb_args <= MAX_ARGS);
878
879 for (i = 0; i < nb_args; i++) {
880 g_free(args[i]);
881 }
882
883}
884
885/*
886 * Parse the command line to get valid args.
887 * @cmdline: command line to be parsed.
888 * @pnb_args: location to store the number of args, must NOT be NULL.
889 * @args: location to store the args, which should be freed by caller, must
890 * NOT be NULL.
891 *
892 * Returns 0 on success, negative on failure.
893 *
894 * NOTE: this parser is an approximate form of the real command parser. Number
895 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
896 * return with failure.
897 */
898static int parse_cmdline(const char *cmdline,
899 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800900{
901 const char *p;
902 int nb_args, ret;
903 char buf[1024];
904
905 p = cmdline;
906 nb_args = 0;
907 for (;;) {
908 while (qemu_isspace(*p)) {
909 p++;
910 }
911 if (*p == '\0') {
912 break;
913 }
914 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800915 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800916 }
917 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800918 if (ret < 0) {
919 goto fail;
920 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800921 args[nb_args] = g_strdup(buf);
922 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800923 }
924 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800925 return 0;
926
927 fail:
928 free_cmdline_args(args, nb_args);
929 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800930}
931
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100932/*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200933 * Can command @cmd be executed in preconfig state?
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100934 */
935static bool cmd_can_preconfig(const mon_cmd_t *cmd)
936{
937 if (!cmd->flags) {
938 return false;
939 }
940
941 return strchr(cmd->flags, 'p');
942}
943
Wenchao Xia66855492013-08-27 20:38:23 +0800944static void help_cmd_dump_one(Monitor *mon,
945 const mon_cmd_t *cmd,
946 char **prefix_args,
947 int prefix_args_nb)
948{
949 int i;
950
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +0100951 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
952 return;
953 }
954
Wenchao Xia66855492013-08-27 20:38:23 +0800955 for (i = 0; i < prefix_args_nb; i++) {
956 monitor_printf(mon, "%s ", prefix_args[i]);
957 }
958 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
959}
960
961/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500962static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800963 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000964{
Anthony Liguoric227f092009-10-01 16:12:16 -0500965 const mon_cmd_t *cmd;
Collin Walling43e87b32018-07-20 15:40:24 -0400966 size_t i;
bellard9dc39cb2004-03-14 21:38:27 +0000967
Wenchao Xia66855492013-08-27 20:38:23 +0800968 /* No valid arg need to compare with, dump all in *cmds */
969 if (arg_index >= nb_args) {
970 for (cmd = cmds; cmd->name != NULL; cmd++) {
971 help_cmd_dump_one(mon, cmd, args, arg_index);
972 }
973 return;
974 }
975
976 /* Find one entry to dump */
977 for (cmd = cmds; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +0100978 if (compare_cmd(args[arg_index], cmd->name) &&
979 ((!runstate_check(RUN_STATE_PRECONFIG) ||
980 cmd_can_preconfig(cmd)))) {
Wenchao Xia66855492013-08-27 20:38:23 +0800981 if (cmd->sub_table) {
982 /* continue with next arg */
983 help_cmd_dump(mon, cmd->sub_table,
984 args, nb_args, arg_index + 1);
985 } else {
986 help_cmd_dump_one(mon, cmd, args, arg_index);
987 }
Collin Walling43e87b32018-07-20 15:40:24 -0400988 return;
Wenchao Xia66855492013-08-27 20:38:23 +0800989 }
bellard9dc39cb2004-03-14 21:38:27 +0000990 }
Collin Walling43e87b32018-07-20 15:40:24 -0400991
992 /* Command not found */
993 monitor_printf(mon, "unknown command: '");
994 for (i = 0; i <= arg_index; i++) {
995 monitor_printf(mon, "%s%s", args[i], i == arg_index ? "'\n" : " ");
996 }
bellard9dc39cb2004-03-14 21:38:27 +0000997}
998
aliguori376253e2009-03-05 23:01:23 +0000999static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +00001000{
Wenchao Xia66855492013-08-27 20:38:23 +08001001 char *args[MAX_ARGS];
1002 int nb_args = 0;
1003
1004 /* 1. parse user input */
1005 if (name) {
1006 /* special case for log, directly dump and return */
1007 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +00001008 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +00001009 monitor_printf(mon, "Log items (comma separated):\n");
1010 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +00001011 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +00001012 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +00001013 }
Wenchao Xia66855492013-08-27 20:38:23 +08001014 return;
1015 }
1016
1017 if (parse_cmdline(name, &nb_args, args) < 0) {
1018 return;
bellardf193c792004-03-21 17:06:25 +00001019 }
bellard9dc39cb2004-03-14 21:38:27 +00001020 }
Wenchao Xia66855492013-08-27 20:38:23 +08001021
1022 /* 2. dump the contents according to parsed args */
1023 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
1024
1025 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +00001026}
1027
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001028static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -03001029{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001030 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -03001031}
1032
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001033static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301034{
1035 const char *tp_name = qdict_get_str(qdict, "name");
1036 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001037 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1038 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001039 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +00001040
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001041 if (vcpu < 0) {
1042 monitor_printf(mon, "argument vcpu must be positive");
1043 return;
1044 }
1045
1046 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001047 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +01001048 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +00001049 }
Prerna Saxena22890ab2010-06-24 17:04:53 +05301050}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001051
Michael Rothc45a8162011-10-02 08:44:37 -05001052#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001053static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001054{
1055 const char *op = qdict_get_try_str(qdict, "op");
1056 const char *arg = qdict_get_try_str(qdict, "arg");
1057
1058 if (!op) {
1059 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
1060 } else if (!strcmp(op, "on")) {
1061 st_set_trace_file_enabled(true);
1062 } else if (!strcmp(op, "off")) {
1063 st_set_trace_file_enabled(false);
1064 } else if (!strcmp(op, "flush")) {
1065 st_flush_trace_buffer();
1066 } else if (!strcmp(op, "set")) {
1067 if (arg) {
1068 st_set_trace_file(arg);
1069 }
1070 } else {
1071 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1072 help_cmd(mon, "trace-file");
1073 }
1074}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301075#endif
1076
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001077static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001078{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001079 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001080}
1081
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001082static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +00001083{
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001084 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001085
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001086 if (!cmd->enabled) {
1087 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001088 }
1089
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001090 info = g_malloc0(sizeof(*info));
1091 info->value = g_malloc0(sizeof(*info->value));
1092 info->value->name = g_strdup(cmd->name);
1093 info->next = *list;
1094 *list = info;
1095}
1096
1097CommandInfoList *qmp_query_commands(Error **errp)
1098{
1099 CommandInfoList *list = NULL;
1100
Markus Armbruster635db182017-03-03 13:32:27 +01001101 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001102
1103 return list;
thsa36e69d2007-12-02 05:18:19 +00001104}
1105
Daniel P. Berrange48608532012-05-21 17:59:51 +01001106EventInfoList *qmp_query_events(Error **errp)
1107{
Markus Armbruster9d7b7082019-02-14 16:22:50 +01001108 /*
1109 * TODO This deprecated command is the only user of
1110 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
1111 * they should go, too.
1112 */
Daniel P. Berrange48608532012-05-21 17:59:51 +01001113 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +02001114 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +01001115
Eric Blake7fb1cf12015-11-18 01:52:57 -07001116 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001117 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +01001118 assert(event_name != NULL);
1119 info = g_malloc0(sizeof(*info));
1120 info->value = g_malloc0(sizeof(*info->value));
1121 info->value->name = g_strdup(event_name);
1122
1123 info->next = ev_list;
1124 ev_list = info;
1125 }
1126
1127 return ev_list;
1128}
1129
Markus Armbruster39a18152015-09-16 13:06:28 +02001130/*
1131 * Minor hack: generated marshalling suppressed for this command
1132 * ('gen': false in the schema) so we can parse the JSON string
1133 * directly into QObject instead of first parsing it with
1134 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1135 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01001136 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -06001137 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +02001138 */
1139static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1140 Error **errp)
1141{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001142 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
Markus Armbruster39a18152015-09-16 13:06:28 +02001143}
1144
Peter Xu6adf08d2018-03-09 16:59:50 +08001145static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001146{
Markus Armbruster635db182017-03-03 13:32:27 +01001147 /*
1148 * Two command lists:
1149 * - qmp_commands contains all QMP commands
1150 * - qmp_cap_negotiation_commands contains just
1151 * "qmp_capabilities", to enforce capability negotiation
1152 */
1153
Markus Armbruster1527bad2017-03-03 13:32:25 +01001154 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001155
Markus Armbruster1527bad2017-03-03 13:32:25 +01001156 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001157 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001158 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001159 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001160 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001161 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001162
Markus Armbruster635db182017-03-03 13:32:27 +01001163 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1164 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001165 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +01001166}
1167
Peter Xu71da4662018-03-09 16:59:57 +08001168static bool qmp_oob_enabled(Monitor *mon)
1169{
Markus Armbruster279f9e02018-07-03 10:53:56 +02001170 return mon->qmp.capab[QMP_CAPABILITY_OOB];
Peter Xu71da4662018-03-09 16:59:57 +08001171}
1172
Markus Armbruster279f9e02018-07-03 10:53:56 +02001173static void monitor_qmp_caps_reset(Monitor *mon)
Markus Armbruster635db182017-03-03 13:32:27 +01001174{
Markus Armbruster279f9e02018-07-03 10:53:56 +02001175 memset(mon->qmp.capab_offered, 0, sizeof(mon->qmp.capab_offered));
1176 memset(mon->qmp.capab, 0, sizeof(mon->qmp.capab));
1177 mon->qmp.capab_offered[QMP_CAPABILITY_OOB] = mon->use_io_thread;
Peter Xu02130312018-03-09 16:59:53 +08001178}
1179
Peter Xucf869d52018-03-10 20:38:05 -06001180/*
Markus Armbruster279f9e02018-07-03 10:53:56 +02001181 * Accept QMP capabilities in @list for @mon.
1182 * On success, set mon->qmp.capab[], and return true.
1183 * On error, set @errp, and return false.
Peter Xucf869d52018-03-10 20:38:05 -06001184 */
Markus Armbruster279f9e02018-07-03 10:53:56 +02001185static bool qmp_caps_accept(Monitor *mon, QMPCapabilityList *list,
1186 Error **errp)
Peter Xucf869d52018-03-10 20:38:05 -06001187{
Markus Armbruster279f9e02018-07-03 10:53:56 +02001188 GString *unavailable = NULL;
1189 bool capab[QMP_CAPABILITY__MAX];
Peter Xucf869d52018-03-10 20:38:05 -06001190
Markus Armbruster279f9e02018-07-03 10:53:56 +02001191 memset(capab, 0, sizeof(capab));
1192
Peter Xu02130312018-03-09 16:59:53 +08001193 for (; list; list = list->next) {
Markus Armbruster279f9e02018-07-03 10:53:56 +02001194 if (!mon->qmp.capab_offered[list->value]) {
1195 if (!unavailable) {
1196 unavailable = g_string_new(QMPCapability_str(list->value));
1197 } else {
1198 g_string_append_printf(unavailable, ", %s",
1199 QMPCapability_str(list->value));
Peter Xu02130312018-03-09 16:59:53 +08001200 }
Peter Xu02130312018-03-09 16:59:53 +08001201 }
Markus Armbruster279f9e02018-07-03 10:53:56 +02001202 capab[list->value] = true;
Peter Xu02130312018-03-09 16:59:53 +08001203 }
Peter Xu02130312018-03-09 16:59:53 +08001204
Markus Armbruster279f9e02018-07-03 10:53:56 +02001205 if (unavailable) {
1206 error_setg(errp, "Capability %s not available", unavailable->str);
1207 g_string_free(unavailable, true);
Peter Xucf869d52018-03-10 20:38:05 -06001208 return false;
1209 }
1210
Markus Armbruster279f9e02018-07-03 10:53:56 +02001211 memcpy(mon->qmp.capab, capab, sizeof(capab));
Peter Xucf869d52018-03-10 20:38:05 -06001212 return true;
1213}
1214
Peter Xu02130312018-03-09 16:59:53 +08001215void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
1216 Error **errp)
1217{
Markus Armbruster635db182017-03-03 13:32:27 +01001218 if (cur_mon->qmp.commands == &qmp_commands) {
1219 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1220 "Capabilities negotiation is already complete, command "
1221 "ignored");
1222 return;
1223 }
1224
Markus Armbruster279f9e02018-07-03 10:53:56 +02001225 if (!qmp_caps_accept(cur_mon, enable, errp)) {
1226 return;
Peter Xu02130312018-03-09 16:59:53 +08001227 }
1228
Markus Armbruster635db182017-03-03 13:32:27 +01001229 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001230}
1231
Peter Xud9f25282018-06-08 11:55:07 +08001232/* Set the current CPU defined by the user. Callers must hold BQL. */
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001233int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001234{
Andreas Färber55e5c282012-12-17 06:18:02 +01001235 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001236
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001237 cpu = qemu_get_cpu(cpu_index);
1238 if (cpu == NULL) {
1239 return -1;
bellard6a00d602005-11-21 23:25:50 +00001240 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001241 g_free(cur_mon->mon_cpu_path);
1242 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001243 return 0;
bellard6a00d602005-11-21 23:25:50 +00001244}
1245
Peter Xud9f25282018-06-08 11:55:07 +08001246/* Callers must hold BQL. */
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001247static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001248{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001249 CPUState *cpu;
1250
1251 if (cur_mon->mon_cpu_path) {
1252 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1253 TYPE_CPU, NULL);
1254 if (!cpu) {
1255 g_free(cur_mon->mon_cpu_path);
1256 cur_mon->mon_cpu_path = NULL;
1257 }
1258 }
1259 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001260 if (!first_cpu) {
1261 return NULL;
1262 }
David Gibson27a83f82016-09-21 15:29:26 +10001263 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001264 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001265 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001266 if (synchronize) {
1267 cpu_synchronize_state(cpu);
1268 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001269 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001270}
1271
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001272CPUState *mon_get_cpu(void)
1273{
1274 return mon_get_cpu_sync(true);
1275}
1276
Pavel Butsykinbf957282015-09-10 18:38:59 +03001277CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001278{
Thomas Huth854e67f2017-01-13 13:12:35 +01001279 CPUState *cs = mon_get_cpu();
1280
1281 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001282}
1283
Luiz Capitulino99b77962011-10-24 10:53:44 -02001284int monitor_get_cpu_index(void)
1285{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001286 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001287
1288 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001289}
1290
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001291static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001292{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001293 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1294 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001295
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001296 if (all_cpus) {
1297 CPU_FOREACH(cs) {
1298 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1299 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1300 }
1301 } else {
1302 cs = mon_get_cpu();
1303
1304 if (!cs) {
1305 monitor_printf(mon, "No CPU available\n");
1306 return;
1307 }
1308
1309 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001310 }
bellard9307c4c2004-04-04 12:57:25 +00001311}
1312
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001313#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001314static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001315{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001316 if (!tcg_enabled()) {
1317 error_report("JIT information is only available with accel=tcg");
1318 return;
1319 }
1320
aliguori376253e2009-03-05 23:01:23 +00001321 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001322 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001323}
1324
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001325static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001326{
1327 dump_opcount_info((FILE *)mon, monitor_fprintf);
1328}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001329#endif
Max Filippov246ae242014-11-02 11:04:18 +03001330
Emilio G. Cota97bfafe2017-08-08 13:54:42 -04001331static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
1332{
1333 int64_t max = qdict_get_try_int(qdict, "max", 10);
1334 bool mean = qdict_get_try_bool(qdict, "mean", false);
1335 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
1336 enum QSPSortBy sort_by;
1337
1338 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
1339 qsp_report((FILE *)mon, monitor_fprintf, max, sort_by, coalesce);
1340}
1341
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001342static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001343{
1344 int i;
bellard7e2515e2004-08-01 21:52:19 +00001345 const char *str;
ths3b46e622007-09-17 08:09:54 +00001346
aliguoricde76ee2009-03-05 23:01:51 +00001347 if (!mon->rs)
1348 return;
bellard7e2515e2004-08-01 21:52:19 +00001349 i = 0;
1350 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001351 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001352 if (!str)
1353 break;
aliguori376253e2009-03-05 23:01:23 +00001354 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001355 i++;
bellardaa455482004-04-04 13:07:25 +00001356 }
1357}
1358
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001359static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001360{
Thomas Huth854e67f2017-01-13 13:12:35 +01001361 CPUState *cs = mon_get_cpu();
1362
1363 if (!cs) {
1364 monitor_printf(mon, "No CPU available\n");
1365 return;
1366 }
1367 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001368}
j_mayer76a66252007-03-07 08:32:30 +00001369
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001370static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301371{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001372 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001373 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1374 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001375 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001376 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001377 Error *local_err = NULL;
1378
1379 if (name == NULL) {
1380 name = "*";
1381 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001382 if (vcpu < 0) {
1383 monitor_printf(mon, "argument vcpu must be positive");
1384 return;
1385 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001386
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001387 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001388 if (local_err) {
1389 error_report_err(local_err);
1390 return;
1391 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001392
1393 for (elem = events; elem != NULL; elem = elem->next) {
1394 monitor_printf(mon, "%s : state %u\n",
1395 elem->value->name,
1396 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1397 }
1398 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301399}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301400
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001401void qmp_client_migrate_info(const char *protocol, const char *hostname,
1402 bool has_port, int64_t port,
1403 bool has_tls_port, int64_t tls_port,
1404 bool has_cert_subject, const char *cert_subject,
1405 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001406{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001407 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001408 if (!qemu_using_spice(errp)) {
1409 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001410 }
1411
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001412 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001413 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001414 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001415 }
1416
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001417 if (qemu_spice_migrate_info(hostname,
1418 has_port ? port : -1,
1419 has_tls_port ? tls_port : -1,
1420 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001421 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001422 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001423 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001424 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001425 }
1426
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001427 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001428}
1429
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001430static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001431{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001432 Error *err = NULL;
1433
1434 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1435 if (err) {
1436 error_report_err(err);
1437 }
pbrooke735b912007-06-30 13:53:24 +00001438}
1439
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001440static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001441{
1442 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001443 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001444
bellard9307c4c2004-04-04 12:57:25 +00001445 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001446 mask = 0;
1447 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001448 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001449 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001450 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001451 return;
1452 }
1453 }
Peter Maydell24537a02013-02-11 16:41:23 +00001454 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001455}
1456
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001457static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001458{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001459 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001460 if (!option || !strcmp(option, "on")) {
1461 singlestep = 1;
1462 } else if (!strcmp(option, "off")) {
1463 singlestep = 0;
1464 } else {
1465 monitor_printf(mon, "unexpected option %s\n", option);
1466 }
1467}
1468
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001469static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001470{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001471 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001472 if (!device)
1473 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1474 if (gdbserver_start(device) < 0) {
1475 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1476 device);
1477 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001478 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001479 } else {
aliguori59030a82009-04-05 18:43:41 +00001480 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1481 device);
bellard8a7ddc32004-03-31 19:00:16 +00001482 }
1483}
1484
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001485static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001486{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001487 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001488 if (select_watchdog_action(action) == -1) {
1489 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1490 }
1491}
1492
aliguori376253e2009-03-05 23:01:23 +00001493static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001494{
aliguori376253e2009-03-05 23:01:23 +00001495 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001496 switch(c) {
1497 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001498 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001499 break;
1500 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001502 break;
1503 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001504 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001505 break;
1506 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001507 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001508 break;
1509 default:
1510 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001512 } else {
aliguori376253e2009-03-05 23:01:23 +00001513 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001514 }
1515 break;
1516 }
aliguori376253e2009-03-05 23:01:23 +00001517 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001518}
1519
aliguori376253e2009-03-05 23:01:23 +00001520static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001521 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001522{
Blue Swirl23842aa2010-01-12 20:27:43 +00001523 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001524 uint8_t buf[16];
1525 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001526 CPUState *cs = mon_get_cpu();
1527
1528 if (!cs && (format == 'i' || !is_physical)) {
1529 monitor_printf(mon, "Can not dump without CPU\n");
1530 return;
1531 }
bellard9307c4c2004-04-04 12:57:25 +00001532
1533 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001534 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001535 return;
1536 }
1537
1538 len = wsize * count;
1539 if (wsize == 1)
1540 line_size = 8;
1541 else
1542 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001543 max_digits = 0;
1544
1545 switch(format) {
1546 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001547 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001548 break;
1549 default:
1550 case 'x':
1551 max_digits = (wsize * 8) / 4;
1552 break;
1553 case 'u':
1554 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001555 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001556 break;
1557 case 'c':
1558 wsize = 1;
1559 break;
1560 }
1561
1562 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001563 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001564 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001565 else
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001567 l = len;
1568 if (l > line_size)
1569 l = line_size;
1570 if (is_physical) {
Peter Maydell6f89ae52018-12-14 13:30:49 +00001571 AddressSpace *as = cs ? cs->as : &address_space_memory;
1572 MemTxResult r = address_space_read(as, addr,
1573 MEMTXATTRS_UNSPECIFIED, buf, l);
1574 if (r != MEMTX_OK) {
1575 monitor_printf(mon, " Cannot access memory\n");
1576 break;
1577 }
bellard9307c4c2004-04-04 12:57:25 +00001578 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001579 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001580 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001581 break;
1582 }
bellard9307c4c2004-04-04 12:57:25 +00001583 }
ths5fafdf22007-09-16 21:08:06 +00001584 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001585 while (i < l) {
1586 switch(wsize) {
1587 default:
1588 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001589 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001590 break;
1591 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001592 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001593 break;
1594 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001595 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001596 break;
1597 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001598 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001599 break;
1600 }
aliguori376253e2009-03-05 23:01:23 +00001601 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001602 switch(format) {
1603 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001604 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001605 break;
1606 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001607 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001608 break;
1609 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001610 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001611 break;
1612 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001613 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001614 break;
1615 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001616 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001617 break;
1618 }
1619 i += wsize;
1620 }
aliguori376253e2009-03-05 23:01:23 +00001621 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001622 addr += l;
1623 len -= l;
1624 }
1625}
1626
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001627static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001628{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001629 int count = qdict_get_int(qdict, "count");
1630 int format = qdict_get_int(qdict, "format");
1631 int size = qdict_get_int(qdict, "size");
1632 target_long addr = qdict_get_int(qdict, "addr");
1633
aliguori376253e2009-03-05 23:01:23 +00001634 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001635}
1636
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001637static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001638{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001639 int count = qdict_get_int(qdict, "count");
1640 int format = qdict_get_int(qdict, "format");
1641 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001642 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001643
aliguori376253e2009-03-05 23:01:23 +00001644 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001645}
1646
Paolo Bonzinie9628442017-04-20 15:30:58 +02001647static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1648{
1649 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1650 addr, 1);
1651
1652 if (!mrs.mr) {
1653 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1654 return NULL;
1655 }
1656
1657 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1658 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1659 memory_region_unref(mrs.mr);
1660 return NULL;
1661 }
1662
1663 *p_mr = mrs.mr;
1664 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1665}
1666
1667static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1668{
1669 hwaddr addr = qdict_get_int(qdict, "addr");
1670 Error *local_err = NULL;
1671 MemoryRegion *mr = NULL;
1672 void *ptr;
1673
1674 ptr = gpa2hva(&mr, addr, &local_err);
1675 if (local_err) {
1676 error_report_err(local_err);
1677 return;
1678 }
1679
1680 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1681 " (%s) is %p\n",
1682 addr, mr->name, ptr);
1683
1684 memory_region_unref(mr);
1685}
1686
1687#ifdef CONFIG_LINUX
1688static uint64_t vtop(void *ptr, Error **errp)
1689{
1690 uint64_t pinfo;
1691 uint64_t ret = -1;
1692 uintptr_t addr = (uintptr_t) ptr;
1693 uintptr_t pagesize = getpagesize();
1694 off_t offset = addr / pagesize * sizeof(pinfo);
1695 int fd;
1696
1697 fd = open("/proc/self/pagemap", O_RDONLY);
1698 if (fd == -1) {
1699 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1700 return -1;
1701 }
1702
1703 /* Force copy-on-write if necessary. */
1704 atomic_add((uint8_t *)ptr, 0);
1705
1706 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1707 error_setg_errno(errp, errno, "Cannot read pagemap");
1708 goto out;
1709 }
1710 if ((pinfo & (1ull << 63)) == 0) {
1711 error_setg(errp, "Page not present");
1712 goto out;
1713 }
1714 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1715
1716out:
1717 close(fd);
1718 return ret;
1719}
1720
1721static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1722{
1723 hwaddr addr = qdict_get_int(qdict, "addr");
1724 Error *local_err = NULL;
1725 MemoryRegion *mr = NULL;
1726 void *ptr;
1727 uint64_t physaddr;
1728
1729 ptr = gpa2hva(&mr, addr, &local_err);
1730 if (local_err) {
1731 error_report_err(local_err);
1732 return;
1733 }
1734
1735 physaddr = vtop(ptr, &local_err);
1736 if (local_err) {
1737 error_report_err(local_err);
1738 } else {
1739 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1740 " (%s) is 0x%" PRIx64 "\n",
1741 addr, mr->name, (uint64_t) physaddr);
1742 }
1743
1744 memory_region_unref(mr);
1745}
1746#endif
1747
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001748static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001749{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001750 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001751 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001752
bellard9307c4c2004-04-04 12:57:25 +00001753 switch(format) {
1754 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001755 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001756 break;
1757 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001758 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001759 break;
1760 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001761 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001762 break;
1763 default:
1764 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001765 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001766 break;
1767 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001768 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001769 break;
1770 }
aliguori376253e2009-03-05 23:01:23 +00001771 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001772}
1773
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001774static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001775{
1776 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001777 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001778 uint32_t start = qdict_get_int(qdict, "start");
1779 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001780
1781 sum = 0;
1782 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001783 uint8_t val = address_space_ldub(&address_space_memory, addr,
1784 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001785 /* BSD sum algorithm ('sum' Unix command) */
1786 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001787 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001788 }
aliguori376253e2009-03-05 23:01:23 +00001789 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001790}
1791
bellard13224a82006-07-14 22:03:35 +00001792static int mouse_button_state;
1793
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001794static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001795{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001796 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001797 const char *dx_str = qdict_get_str(qdict, "dx_str");
1798 const char *dy_str = qdict_get_str(qdict, "dy_str");
1799 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001800
bellard13224a82006-07-14 22:03:35 +00001801 dx = strtol(dx_str, NULL, 0);
1802 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001803 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1804 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1805
1806 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001807 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001808 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001809 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001810 qemu_input_queue_btn(NULL, button, true);
1811 qemu_input_event_sync();
1812 qemu_input_queue_btn(NULL, button, false);
1813 }
1814 }
1815 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001816}
1817
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001818static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001819{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001820 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001821 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1822 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1823 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1824 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001825 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001826
1827 if (mouse_button_state == button_state) {
1828 return;
1829 }
1830 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1831 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001832 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001833}
1834
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001835static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001836{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001837 int size = qdict_get_int(qdict, "size");
1838 int addr = qdict_get_int(qdict, "addr");
1839 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001840 uint32_t val;
1841 int suffix;
1842
1843 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001844 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001845 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001846 addr++;
1847 }
1848 addr &= 0xffff;
1849
1850 switch(size) {
1851 default:
1852 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001853 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001854 suffix = 'b';
1855 break;
1856 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001857 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001858 suffix = 'w';
1859 break;
1860 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001861 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001862 suffix = 'l';
1863 break;
1864 }
aliguori376253e2009-03-05 23:01:23 +00001865 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1866 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001867}
bellarda3a91a32004-06-04 11:06:21 +00001868
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001869static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001870{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001871 int size = qdict_get_int(qdict, "size");
1872 int addr = qdict_get_int(qdict, "addr");
1873 int val = qdict_get_int(qdict, "val");
1874
Jan Kiszkaf1147842009-07-14 10:20:11 +02001875 addr &= IOPORTS_MASK;
1876
1877 switch (size) {
1878 default:
1879 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001880 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001881 break;
1882 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001883 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001884 break;
1885 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001886 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001887 break;
1888 }
1889}
1890
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001891static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001892{
Gongleif1839932014-12-03 18:20:58 +00001893 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001894 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001895
Gongleif1839932014-12-03 18:20:58 +00001896 qemu_boot_set(bootdevice, &local_err);
1897 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001898 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001899 } else {
Gongleif1839932014-12-03 18:20:58 +00001900 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001901 }
1902}
1903
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001904static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001905{
Peter Xu57bb40c2017-01-16 16:40:05 +08001906 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001907 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10001908 bool owner = qdict_get_try_bool(qdict, "owner", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001909
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10001910 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree,
1911 owner);
Blue Swirl314e2982011-09-11 20:22:05 +00001912}
1913
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001914static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001915{
aliguorib28b6232009-04-22 20:20:29 +00001916 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001917 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001918 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001919
Igor Mammedovf75cd442017-05-30 18:23:59 +02001920 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001921 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1922
zhanghailiang5b009e42014-11-04 19:49:30 +08001923 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001924 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1925 for (i = 0; i < nb_numa_nodes; i++) {
1926 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001927 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1928 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1929 cpu->value->props->node_id == i) {
1930 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001931 }
1932 }
1933 monitor_printf(mon, "\n");
1934 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001935 node_mem[i].node_mem >> 20);
1936 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1937 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00001938 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001939 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001940 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001941}
1942
bellard5f1ce942006-02-08 22:40:15 +00001943#ifdef CONFIG_PROFILER
1944
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001945int64_t dev_time;
1946
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001947static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001948{
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001949 static int64_t last_cpu_exec_time;
1950 int64_t cpu_exec_time;
1951 int64_t delta;
1952
1953 cpu_exec_time = tcg_cpu_exec_time();
1954 delta = cpu_exec_time - last_cpu_exec_time;
1955
aliguori376253e2009-03-05 23:01:23 +00001956 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301957 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001958 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -04001959 delta, delta / (double)NANOSECONDS_PER_SECOND);
1960 last_cpu_exec_time = cpu_exec_time;
bellard5f1ce942006-02-08 22:40:15 +00001961 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001962}
1963#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001964static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001965{
aliguori376253e2009-03-05 23:01:23 +00001966 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001967}
1968#endif
1969
bellardec36b692006-07-16 18:57:03 +00001970/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001971static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001972
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001973static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001974{
1975 int i;
1976 CaptureState *s;
1977
1978 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001979 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001980 s->ops.info (s->opaque);
1981 }
1982}
1983
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001984static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001985{
1986 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001987 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001988 CaptureState *s;
1989
1990 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1991 if (i == n) {
1992 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001993 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001994 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001995 return;
1996 }
1997 }
1998}
1999
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002000static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002001{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002002 const char *path = qdict_get_str(qdict, "path");
2003 int has_freq = qdict_haskey(qdict, "freq");
2004 int freq = qdict_get_try_int(qdict, "freq", -1);
2005 int has_bits = qdict_haskey(qdict, "bits");
2006 int bits = qdict_get_try_int(qdict, "bits", -1);
2007 int has_channels = qdict_haskey(qdict, "nchannels");
2008 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002009 CaptureState *s;
2010
Anthony Liguori7267c092011-08-20 22:09:37 -05002011 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002012
2013 freq = has_freq ? freq : 44100;
2014 bits = has_bits ? bits : 16;
2015 nchannels = has_channels ? nchannels : 2;
2016
2017 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002018 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002019 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002020 return;
bellardec36b692006-07-16 18:57:03 +00002021 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002022 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002023}
bellardec36b692006-07-16 18:57:03 +00002024
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002025static QAuthZList *find_auth(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002026{
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002027 Object *obj;
2028 Object *container;
aliguori76655d62009-03-06 20:27:37 +00002029
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002030 container = object_get_objects_root();
2031 obj = object_resolve_path_component(container, name);
2032 if (!obj) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002033 monitor_printf(mon, "acl: unknown list '%s'\n", name);
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002034 return NULL;
aliguori76655d62009-03-06 20:27:37 +00002035 }
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002036
2037 return QAUTHZ_LIST(obj);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002038}
aliguori76655d62009-03-06 20:27:37 +00002039
Daniel P. Berrangé01438402019-02-27 14:57:55 +00002040static bool warn_acl;
2041static void hmp_warn_acl(void)
2042{
2043 if (warn_acl) {
2044 return;
2045 }
2046 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
2047 "commands are deprecated with no replacement. Authorization "
2048 "for VNC should be performed using the pluggable QAuthZ "
2049 "objects");
2050 warn_acl = true;
2051}
2052
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002053static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002054{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002055 const char *aclname = qdict_get_str(qdict, "aclname");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002056 QAuthZList *auth = find_auth(mon, aclname);
2057 QAuthZListRuleList *rules;
2058 size_t i = 0;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002059
Daniel P. Berrangé01438402019-02-27 14:57:55 +00002060 hmp_warn_acl();
2061
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002062 if (!auth) {
2063 return;
2064 }
2065
2066 monitor_printf(mon, "policy: %s\n",
2067 QAuthZListPolicy_str(auth->policy));
2068
2069 rules = auth->rules;
2070 while (rules) {
2071 QAuthZListRule *rule = rules->value;
2072 i++;
2073 monitor_printf(mon, "%zu: %s %s\n", i,
2074 QAuthZListPolicy_str(rule->policy),
2075 rule->match);
2076 rules = rules->next;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002077 }
2078}
2079
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002080static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002081{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002082 const char *aclname = qdict_get_str(qdict, "aclname");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002083 QAuthZList *auth = find_auth(mon, aclname);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002084
Daniel P. Berrangé01438402019-02-27 14:57:55 +00002085 hmp_warn_acl();
2086
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002087 if (!auth) {
2088 return;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002089 }
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002090
2091 auth->policy = QAUTHZ_LIST_POLICY_DENY;
2092 qapi_free_QAuthZListRuleList(auth->rules);
2093 auth->rules = NULL;
2094 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002095}
aliguori76655d62009-03-06 20:27:37 +00002096
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002097static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002098{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002099 const char *aclname = qdict_get_str(qdict, "aclname");
2100 const char *policy = qdict_get_str(qdict, "policy");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002101 QAuthZList *auth = find_auth(mon, aclname);
2102 int val;
2103 Error *err = NULL;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002104
Daniel P. Berrangé01438402019-02-27 14:57:55 +00002105 hmp_warn_acl();
2106
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002107 if (!auth) {
2108 return;
2109 }
2110
2111 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
2112 policy,
2113 QAUTHZ_LIST_POLICY_DENY,
2114 &err);
2115 if (err) {
2116 error_free(err);
2117 monitor_printf(mon, "acl: unknown policy '%s', "
2118 "expected 'deny' or 'allow'\n", policy);
2119 } else {
2120 auth->policy = val;
2121 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
aliguori28a76be2009-03-06 20:27:40 +00002122 monitor_printf(mon, "acl: policy set to 'allow'\n");
aliguori28a76be2009-03-06 20:27:40 +00002123 } else {
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002124 monitor_printf(mon, "acl: policy set to 'deny'\n");
aliguori28a76be2009-03-06 20:27:40 +00002125 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126 }
2127}
aliguori76655d62009-03-06 20:27:37 +00002128
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002129static QAuthZListFormat hmp_acl_get_format(const char *match)
2130{
2131 if (strchr(match, '*')) {
2132 return QAUTHZ_LIST_FORMAT_GLOB;
2133 } else {
2134 return QAUTHZ_LIST_FORMAT_EXACT;
2135 }
2136}
2137
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002138static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002140 const char *aclname = qdict_get_str(qdict, "aclname");
2141 const char *match = qdict_get_str(qdict, "match");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002142 const char *policystr = qdict_get_str(qdict, "policy");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002143 int has_index = qdict_haskey(qdict, "index");
2144 int index = qdict_get_try_int(qdict, "index", -1);
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002145 QAuthZList *auth = find_auth(mon, aclname);
2146 Error *err = NULL;
2147 QAuthZListPolicy policy;
2148 QAuthZListFormat format;
2149 size_t i = 0;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002150
Daniel P. Berrangé01438402019-02-27 14:57:55 +00002151 hmp_warn_acl();
2152
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002153 if (!auth) {
2154 return;
2155 }
2156
2157 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
2158 policystr,
2159 QAUTHZ_LIST_POLICY_DENY,
2160 &err);
2161 if (err) {
2162 error_free(err);
2163 monitor_printf(mon, "acl: unknown policy '%s', "
2164 "expected 'deny' or 'allow'\n", policystr);
2165 return;
2166 }
2167
2168 format = hmp_acl_get_format(match);
2169
2170 if (has_index && index == 0) {
2171 monitor_printf(mon, "acl: unable to add acl entry\n");
2172 return;
2173 }
2174
2175 if (has_index) {
2176 i = qauthz_list_insert_rule(auth, match, policy,
2177 format, index - 1, &err);
2178 } else {
2179 i = qauthz_list_append_rule(auth, match, policy,
2180 format, &err);
2181 }
2182 if (err) {
2183 monitor_printf(mon, "acl: unable to add rule: %s",
2184 error_get_pretty(err));
2185 error_free(err);
2186 } else {
2187 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002188 }
2189}
aliguori76655d62009-03-06 20:27:37 +00002190
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002191static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002192{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002193 const char *aclname = qdict_get_str(qdict, "aclname");
2194 const char *match = qdict_get_str(qdict, "match");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002195 QAuthZList *auth = find_auth(mon, aclname);
2196 ssize_t i = 0;
aliguori76655d62009-03-06 20:27:37 +00002197
Daniel P. Berrangé01438402019-02-27 14:57:55 +00002198 hmp_warn_acl();
2199
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00002200 if (!auth) {
2201 return;
2202 }
2203
2204 i = qauthz_list_delete_rule(auth, match);
2205 if (i >= 0) {
2206 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
2207 } else {
2208 monitor_printf(mon, "acl: no matching acl entry\n");
aliguori76655d62009-03-06 20:27:37 +00002209 }
2210}
2211
Corey Bryant208c9d12012-06-22 14:36:09 -04002212void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002213{
Anthony Liguoric227f092009-10-01 16:12:16 -05002214 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002215 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002216
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002217 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002218 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002219 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002220 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002221 }
2222
2223 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002224 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002225 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2226 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002227 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002228 }
2229
Peter Xu9409fc02018-06-08 11:55:06 +08002230 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002231 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002232 if (strcmp(monfd->name, fdname) != 0) {
2233 continue;
2234 }
2235
Peter Xu9409fc02018-06-08 11:55:06 +08002236 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002237 monfd->fd = fd;
Peter Xu9409fc02018-06-08 11:55:06 +08002238 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02002239 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08002240 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002241 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242 }
2243
Anthony Liguori7267c092011-08-20 22:09:37 -05002244 monfd = g_malloc0(sizeof(mon_fd_t));
2245 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002246 monfd->fd = fd;
2247
Corey Bryant208c9d12012-06-22 14:36:09 -04002248 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002249 qemu_mutex_unlock(&cur_mon->mon_lock);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250}
2251
Corey Bryant208c9d12012-06-22 14:36:09 -04002252void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253{
Anthony Liguoric227f092009-10-01 16:12:16 -05002254 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002255 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002256
Peter Xu9409fc02018-06-08 11:55:06 +08002257 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002258 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002259 if (strcmp(monfd->name, fdname) != 0) {
2260 continue;
2261 }
2262
Blue Swirl72cf2d42009-09-12 07:36:22 +00002263 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002264 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05002265 g_free(monfd->name);
2266 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002267 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02002268 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08002269 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002270 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002271 }
2272
Peter Xu9409fc02018-06-08 11:55:06 +08002273 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002274 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002275}
2276
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002277int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002278{
Anthony Liguoric227f092009-10-01 16:12:16 -05002279 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002280
Peter Xu9409fc02018-06-08 11:55:06 +08002281 qemu_mutex_lock(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002282 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002283 int fd;
2284
2285 if (strcmp(monfd->name, fdname) != 0) {
2286 continue;
2287 }
2288
2289 fd = monfd->fd;
2290
2291 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002292 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002293 g_free(monfd->name);
2294 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002295 qemu_mutex_unlock(&mon->mon_lock);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002296
2297 return fd;
2298 }
2299
Peter Xu9409fc02018-06-08 11:55:06 +08002300 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002301 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002302 return -1;
2303}
2304
Corey Bryantba1c0482012-08-14 16:43:43 -04002305static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2306{
2307 MonFdsetFd *mon_fdset_fd;
2308 MonFdsetFd *mon_fdset_fd_next;
2309
2310 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002311 if ((mon_fdset_fd->removed ||
2312 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2313 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002314 close(mon_fdset_fd->fd);
2315 g_free(mon_fdset_fd->opaque);
2316 QLIST_REMOVE(mon_fdset_fd, next);
2317 g_free(mon_fdset_fd);
2318 }
2319 }
2320
Corey Bryantadb696f2012-08-14 16:43:47 -04002321 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002322 QLIST_REMOVE(mon_fdset, next);
2323 g_free(mon_fdset);
2324 }
2325}
2326
Corey Bryantefb87c12012-08-14 16:43:48 -04002327static void monitor_fdsets_cleanup(void)
2328{
2329 MonFdset *mon_fdset;
2330 MonFdset *mon_fdset_next;
2331
Peter Xu47451462018-06-08 11:55:11 +08002332 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002333 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2334 monitor_fdset_cleanup(mon_fdset);
2335 }
Peter Xu47451462018-06-08 11:55:11 +08002336 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002337}
2338
Corey Bryantba1c0482012-08-14 16:43:43 -04002339AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2340 const char *opaque, Error **errp)
2341{
2342 int fd;
2343 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002344 AddfdInfo *fdinfo;
2345
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002346 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002347 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002348 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002349 goto error;
2350 }
2351
Corey Bryante446f702012-10-18 15:19:32 -04002352 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2353 has_opaque, opaque, errp);
2354 if (fdinfo) {
2355 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002356 }
2357
Corey Bryantba1c0482012-08-14 16:43:43 -04002358error:
2359 if (fd != -1) {
2360 close(fd);
2361 }
2362 return NULL;
2363}
2364
2365void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2366{
2367 MonFdset *mon_fdset;
2368 MonFdsetFd *mon_fdset_fd;
2369 char fd_str[60];
2370
Peter Xu47451462018-06-08 11:55:11 +08002371 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002372 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2373 if (mon_fdset->id != fdset_id) {
2374 continue;
2375 }
2376 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2377 if (has_fd) {
2378 if (mon_fdset_fd->fd != fd) {
2379 continue;
2380 }
2381 mon_fdset_fd->removed = true;
2382 break;
2383 } else {
2384 mon_fdset_fd->removed = true;
2385 }
2386 }
2387 if (has_fd && !mon_fdset_fd) {
2388 goto error;
2389 }
2390 monitor_fdset_cleanup(mon_fdset);
Peter Xu47451462018-06-08 11:55:11 +08002391 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002392 return;
2393 }
2394
2395error:
Peter Xu47451462018-06-08 11:55:11 +08002396 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002397 if (has_fd) {
2398 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2399 fdset_id, fd);
2400 } else {
2401 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2402 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002403 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002404}
2405
2406FdsetInfoList *qmp_query_fdsets(Error **errp)
2407{
2408 MonFdset *mon_fdset;
2409 MonFdsetFd *mon_fdset_fd;
2410 FdsetInfoList *fdset_list = NULL;
2411
Peter Xu47451462018-06-08 11:55:11 +08002412 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002413 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2414 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2415 FdsetFdInfoList *fdsetfd_list = NULL;
2416
2417 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2418 fdset_info->value->fdset_id = mon_fdset->id;
2419
2420 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2421 FdsetFdInfoList *fdsetfd_info;
2422
2423 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2424 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2425 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2426 if (mon_fdset_fd->opaque) {
2427 fdsetfd_info->value->has_opaque = true;
2428 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2429 } else {
2430 fdsetfd_info->value->has_opaque = false;
2431 }
2432
2433 fdsetfd_info->next = fdsetfd_list;
2434 fdsetfd_list = fdsetfd_info;
2435 }
2436
2437 fdset_info->value->fds = fdsetfd_list;
2438
2439 fdset_info->next = fdset_list;
2440 fdset_list = fdset_info;
2441 }
Peter Xu47451462018-06-08 11:55:11 +08002442 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002443
2444 return fdset_list;
2445}
2446
Corey Bryante446f702012-10-18 15:19:32 -04002447AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2448 bool has_opaque, const char *opaque,
2449 Error **errp)
2450{
2451 MonFdset *mon_fdset = NULL;
2452 MonFdsetFd *mon_fdset_fd;
2453 AddfdInfo *fdinfo;
2454
Peter Xu47451462018-06-08 11:55:11 +08002455 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002456 if (has_fdset_id) {
2457 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2458 /* Break if match found or match impossible due to ordering by ID */
2459 if (fdset_id <= mon_fdset->id) {
2460 if (fdset_id < mon_fdset->id) {
2461 mon_fdset = NULL;
2462 }
2463 break;
2464 }
2465 }
2466 }
2467
2468 if (mon_fdset == NULL) {
2469 int64_t fdset_id_prev = -1;
2470 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2471
2472 if (has_fdset_id) {
2473 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002474 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2475 "a non-negative value");
Peter Xu47451462018-06-08 11:55:11 +08002476 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002477 return NULL;
2478 }
2479 /* Use specified fdset ID */
2480 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2481 mon_fdset_cur = mon_fdset;
2482 if (fdset_id < mon_fdset_cur->id) {
2483 break;
2484 }
2485 }
2486 } else {
2487 /* Use first available fdset ID */
2488 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2489 mon_fdset_cur = mon_fdset;
2490 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2491 fdset_id_prev = mon_fdset_cur->id;
2492 continue;
2493 }
2494 break;
2495 }
2496 }
2497
2498 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2499 if (has_fdset_id) {
2500 mon_fdset->id = fdset_id;
2501 } else {
2502 mon_fdset->id = fdset_id_prev + 1;
2503 }
2504
2505 /* The fdset list is ordered by fdset ID */
2506 if (!mon_fdset_cur) {
2507 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2508 } else if (mon_fdset->id < mon_fdset_cur->id) {
2509 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2510 } else {
2511 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2512 }
2513 }
2514
2515 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2516 mon_fdset_fd->fd = fd;
2517 mon_fdset_fd->removed = false;
2518 if (has_opaque) {
2519 mon_fdset_fd->opaque = g_strdup(opaque);
2520 }
2521 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2522
2523 fdinfo = g_malloc0(sizeof(*fdinfo));
2524 fdinfo->fdset_id = mon_fdset->id;
2525 fdinfo->fd = mon_fdset_fd->fd;
2526
Peter Xu47451462018-06-08 11:55:11 +08002527 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002528 return fdinfo;
2529}
2530
Corey Bryantadb696f2012-08-14 16:43:47 -04002531int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2532{
Peter Xu47451462018-06-08 11:55:11 +08002533#ifdef _WIN32
2534 return -ENOENT;
2535#else
Corey Bryantadb696f2012-08-14 16:43:47 -04002536 MonFdset *mon_fdset;
2537 MonFdsetFd *mon_fdset_fd;
2538 int mon_fd_flags;
Peter Xu47451462018-06-08 11:55:11 +08002539 int ret;
Corey Bryantadb696f2012-08-14 16:43:47 -04002540
Peter Xu47451462018-06-08 11:55:11 +08002541 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002542 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2543 if (mon_fdset->id != fdset_id) {
2544 continue;
2545 }
2546 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2547 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2548 if (mon_fd_flags == -1) {
Peter Xu47451462018-06-08 11:55:11 +08002549 ret = -errno;
2550 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002551 }
2552
2553 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Peter Xu47451462018-06-08 11:55:11 +08002554 ret = mon_fdset_fd->fd;
2555 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002556 }
2557 }
Peter Xu47451462018-06-08 11:55:11 +08002558 ret = -EACCES;
2559 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002560 }
Peter Xu47451462018-06-08 11:55:11 +08002561 ret = -ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04002562
Peter Xu47451462018-06-08 11:55:11 +08002563out:
2564 qemu_mutex_unlock(&mon_fdsets_lock);
2565 return ret;
2566#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04002567}
2568
2569int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2570{
2571 MonFdset *mon_fdset;
2572 MonFdsetFd *mon_fdset_fd_dup;
2573
Peter Xu47451462018-06-08 11:55:11 +08002574 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002575 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2576 if (mon_fdset->id != fdset_id) {
2577 continue;
2578 }
2579 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2580 if (mon_fdset_fd_dup->fd == dup_fd) {
Peter Xu47451462018-06-08 11:55:11 +08002581 goto err;
Corey Bryantadb696f2012-08-14 16:43:47 -04002582 }
2583 }
2584 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2585 mon_fdset_fd_dup->fd = dup_fd;
2586 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Peter Xu47451462018-06-08 11:55:11 +08002587 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002588 return 0;
2589 }
Peter Xu47451462018-06-08 11:55:11 +08002590
2591err:
2592 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002593 return -1;
2594}
2595
2596static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2597{
2598 MonFdset *mon_fdset;
2599 MonFdsetFd *mon_fdset_fd_dup;
2600
Peter Xu47451462018-06-08 11:55:11 +08002601 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002602 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2603 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2604 if (mon_fdset_fd_dup->fd == dup_fd) {
2605 if (remove) {
2606 QLIST_REMOVE(mon_fdset_fd_dup, next);
2607 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2608 monitor_fdset_cleanup(mon_fdset);
2609 }
Peter Xu47451462018-06-08 11:55:11 +08002610 goto err;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002611 } else {
Peter Xu47451462018-06-08 11:55:11 +08002612 qemu_mutex_unlock(&mon_fdsets_lock);
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002613 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002614 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002615 }
2616 }
2617 }
Peter Xu47451462018-06-08 11:55:11 +08002618
2619err:
2620 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002621 return -1;
2622}
2623
2624int monitor_fdset_dup_fd_find(int dup_fd)
2625{
2626 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2627}
2628
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002629void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002630{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002631 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002632}
2633
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002634int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002635{
2636 int fd;
2637 Error *local_err = NULL;
2638
2639 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002640 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002641 } else {
2642 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002643 if (fd == -1) {
2644 error_setg(&local_err, "Invalid file descriptor number '%s'",
2645 fdname);
2646 }
2647 }
2648 if (local_err) {
2649 error_propagate(errp, local_err);
2650 assert(fd == -1);
2651 } else {
2652 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002653 }
2654
2655 return fd;
2656}
2657
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002658/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002659static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002660#include "hmp-commands-info.h"
2661 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002662};
2663
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002664/* mon_cmds and info_cmds would be sorted at runtime */
2665static mon_cmd_t mon_cmds[] = {
2666#include "hmp-commands.h"
2667 { NULL, NULL, },
2668};
2669
bellard9307c4c2004-04-04 12:57:25 +00002670/*******************************************************************/
2671
2672static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002673static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002674
bellard9307c4c2004-04-04 12:57:25 +00002675
Stefan Weil9c3175c2013-08-22 21:30:09 +02002676static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2677expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002678{
Fam Zheng277acfe2013-08-20 10:58:21 +08002679 va_list ap;
2680 va_start(ap, fmt);
2681 monitor_vprintf(mon, fmt, ap);
2682 monitor_printf(mon, "\n");
2683 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002684 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002685}
2686
Markus Armbruster09b94182010-01-20 13:07:30 +01002687/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002688static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002689{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002690 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002691 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002692 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002693 uint64_t tmp = 0;
2694 int ret;
bellard92a31b12005-02-10 22:00:52 +00002695
Thomas Huth854e67f2017-01-13 13:12:35 +01002696 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002697 return -1;
2698 }
2699
2700 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002701 if (compare_cmd(name, md->name)) {
2702 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002703 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002704 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002705 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002706 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002707 switch(md->type) {
2708 case MD_I32:
2709 *pval = *(int32_t *)ptr;
2710 break;
2711 case MD_TLONG:
2712 *pval = *(target_long *)ptr;
2713 break;
2714 default:
2715 *pval = 0;
2716 break;
2717 }
bellard9307c4c2004-04-04 12:57:25 +00002718 }
2719 return 0;
2720 }
2721 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002722
Thomas Huth854e67f2017-01-13 13:12:35 +01002723 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002724 if (!ret) {
2725 *pval = (target_long) tmp;
2726 }
2727
2728 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002729}
2730
2731static void next(void)
2732{
Blue Swirl660f11b2009-07-31 21:16:51 +00002733 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002734 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002735 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002736 pch++;
2737 }
2738}
2739
aliguori376253e2009-03-05 23:01:23 +00002740static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002741
aliguori376253e2009-03-05 23:01:23 +00002742static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002743{
blueswir1c2efc952007-09-25 17:28:42 +00002744 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002745 char *p;
bellard6a00d602005-11-21 23:25:50 +00002746 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002747
2748 switch(*pch) {
2749 case '+':
2750 next();
aliguori376253e2009-03-05 23:01:23 +00002751 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002752 break;
2753 case '-':
2754 next();
aliguori376253e2009-03-05 23:01:23 +00002755 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002756 break;
2757 case '~':
2758 next();
aliguori376253e2009-03-05 23:01:23 +00002759 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002760 break;
2761 case '(':
2762 next();
aliguori376253e2009-03-05 23:01:23 +00002763 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002764 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002765 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002766 }
2767 next();
2768 break;
bellard81d09122004-07-14 17:21:37 +00002769 case '\'':
2770 pch++;
2771 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002772 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002773 n = *pch;
2774 pch++;
2775 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002776 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002777 next();
2778 break;
bellard9307c4c2004-04-04 12:57:25 +00002779 case '$':
2780 {
2781 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002782 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002783
bellard9307c4c2004-04-04 12:57:25 +00002784 pch++;
2785 q = buf;
2786 while ((*pch >= 'a' && *pch <= 'z') ||
2787 (*pch >= 'A' && *pch <= 'Z') ||
2788 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002789 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002790 if ((q - buf) < sizeof(buf) - 1)
2791 *q++ = *pch;
2792 pch++;
2793 }
blueswir1cd390082008-11-16 13:53:32 +00002794 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002795 pch++;
2796 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002797 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002798 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002799 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002800 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002801 }
2802 break;
2803 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002804 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002805 n = 0;
2806 break;
2807 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002808 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002809 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002810 if (errno == ERANGE) {
2811 expr_error(mon, "number too large");
2812 }
bellard9307c4c2004-04-04 12:57:25 +00002813 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002814 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002815 }
2816 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002817 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002818 pch++;
2819 break;
2820 }
2821 return n;
2822}
2823
2824
aliguori376253e2009-03-05 23:01:23 +00002825static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002826{
blueswir1c2efc952007-09-25 17:28:42 +00002827 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002828 int op;
ths3b46e622007-09-17 08:09:54 +00002829
aliguori376253e2009-03-05 23:01:23 +00002830 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002831 for(;;) {
2832 op = *pch;
2833 if (op != '*' && op != '/' && op != '%')
2834 break;
2835 next();
aliguori376253e2009-03-05 23:01:23 +00002836 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002837 switch(op) {
2838 default:
2839 case '*':
2840 val *= val2;
2841 break;
2842 case '/':
2843 case '%':
ths5fafdf22007-09-16 21:08:06 +00002844 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002845 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002846 if (op == '/')
2847 val /= val2;
2848 else
2849 val %= val2;
2850 break;
2851 }
2852 }
2853 return val;
2854}
2855
aliguori376253e2009-03-05 23:01:23 +00002856static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002857{
blueswir1c2efc952007-09-25 17:28:42 +00002858 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002859 int op;
bellard9307c4c2004-04-04 12:57:25 +00002860
aliguori376253e2009-03-05 23:01:23 +00002861 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002862 for(;;) {
2863 op = *pch;
2864 if (op != '&' && op != '|' && op != '^')
2865 break;
2866 next();
aliguori376253e2009-03-05 23:01:23 +00002867 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002868 switch(op) {
2869 default:
2870 case '&':
2871 val &= val2;
2872 break;
2873 case '|':
2874 val |= val2;
2875 break;
2876 case '^':
2877 val ^= val2;
2878 break;
2879 }
2880 }
2881 return val;
2882}
2883
aliguori376253e2009-03-05 23:01:23 +00002884static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002885{
blueswir1c2efc952007-09-25 17:28:42 +00002886 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002887 int op;
bellard9307c4c2004-04-04 12:57:25 +00002888
aliguori376253e2009-03-05 23:01:23 +00002889 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002890 for(;;) {
2891 op = *pch;
2892 if (op != '+' && op != '-')
2893 break;
2894 next();
aliguori376253e2009-03-05 23:01:23 +00002895 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002896 if (op == '+')
2897 val += val2;
2898 else
2899 val -= val2;
2900 }
2901 return val;
2902}
2903
aliguori376253e2009-03-05 23:01:23 +00002904static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002905{
2906 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002907 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002908 *pp = pch;
2909 return -1;
2910 }
blueswir1cd390082008-11-16 13:53:32 +00002911 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002912 pch++;
aliguori376253e2009-03-05 23:01:23 +00002913 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002914 *pp = pch;
2915 return 0;
2916}
2917
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002918static int get_double(Monitor *mon, double *pval, const char **pp)
2919{
2920 const char *p = *pp;
2921 char *tailp;
2922 double d;
2923
2924 d = strtod(p, &tailp);
2925 if (tailp == p) {
2926 monitor_printf(mon, "Number expected\n");
2927 return -1;
2928 }
2929 if (d != d || d - d != 0) {
2930 /* NaN or infinity */
2931 monitor_printf(mon, "Bad number\n");
2932 return -1;
2933 }
2934 *pval = d;
2935 *pp = tailp;
2936 return 0;
2937}
2938
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002939/*
2940 * Store the command-name in cmdname, and return a pointer to
2941 * the remaining of the command string.
2942 */
2943static const char *get_command_name(const char *cmdline,
2944 char *cmdname, size_t nlen)
2945{
2946 size_t len;
2947 const char *p, *pstart;
2948
2949 p = cmdline;
2950 while (qemu_isspace(*p))
2951 p++;
2952 if (*p == '\0')
2953 return NULL;
2954 pstart = p;
2955 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2956 p++;
2957 len = p - pstart;
2958 if (len > nlen - 1)
2959 len = nlen - 1;
2960 memcpy(cmdname, pstart, len);
2961 cmdname[len] = '\0';
2962 return p;
2963}
2964
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002965/**
2966 * Read key of 'type' into 'key' and return the current
2967 * 'type' pointer.
2968 */
2969static char *key_get_info(const char *type, char **key)
2970{
2971 size_t len;
2972 char *p, *str;
2973
2974 if (*type == ',')
2975 type++;
2976
2977 p = strchr(type, ':');
2978 if (!p) {
2979 *key = NULL;
2980 return NULL;
2981 }
2982 len = p - type;
2983
Anthony Liguori7267c092011-08-20 22:09:37 -05002984 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002985 memcpy(str, type, len);
2986 str[len] = '\0';
2987
2988 *key = str;
2989 return ++p;
2990}
2991
bellard9307c4c2004-04-04 12:57:25 +00002992static int default_fmt_format = 'x';
2993static int default_fmt_size = 4;
2994
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002995static int is_valid_option(const char *c, const char *typestr)
2996{
2997 char option[3];
2998
2999 option[0] = '-';
3000 option[1] = *c;
3001 option[2] = '\0';
3002
3003 typestr = strstr(typestr, option);
3004 return (typestr != NULL);
3005}
3006
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003007static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3008 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003009{
3010 const mon_cmd_t *cmd;
3011
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003012 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003013 if (compare_cmd(cmdname, cmd->name)) {
3014 return cmd;
3015 }
3016 }
3017
3018 return NULL;
3019}
3020
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003021/*
Bandan Dasae502122015-06-03 18:38:08 -04003022 * Parse command name from @cmdp according to command table @table.
3023 * If blank, return NULL.
3024 * Else, if no valid command can be found, report to @mon, and return
3025 * NULL.
3026 * Else, change @cmdp to point right behind the name, and return its
3027 * command table entry.
3028 * Do not assume the return value points into @table! It doesn't when
3029 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003030 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003031static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003032 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04003033 const char **cmdp,
3034 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003035{
Bandan Dasae502122015-06-03 18:38:08 -04003036 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003037 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003038 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003039
bellard9307c4c2004-04-04 12:57:25 +00003040 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003041 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003042 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003043 return NULL;
ths3b46e622007-09-17 08:09:54 +00003044
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003045 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003046 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003047 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003048 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003049 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003050 }
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +01003051 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
3052 monitor_printf(mon, "Command '%.*s' not available with -preconfig "
3053 "until after exit_preconfig.\n",
3054 (int)(p - cmdp_start), cmdp_start);
3055 return NULL;
3056 }
bellard9307c4c2004-04-04 12:57:25 +00003057
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003058 /* filter out following useless space */
3059 while (qemu_isspace(*p)) {
3060 p++;
3061 }
Bandan Dasae502122015-06-03 18:38:08 -04003062
3063 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003064 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003065 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003066 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003067 }
3068
Bandan Dasae502122015-06-03 18:38:08 -04003069 return cmd;
3070}
3071
3072/*
3073 * Parse arguments for @cmd.
3074 * If it can't be parsed, report to @mon, and return NULL.
3075 * Else, insert command arguments into a QDict, and return it.
3076 * Note: On success, caller has to free the QDict structure.
3077 */
3078
3079static QDict *monitor_parse_arguments(Monitor *mon,
3080 const char **endp,
3081 const mon_cmd_t *cmd)
3082{
3083 const char *typestr;
3084 char *key;
3085 int c;
3086 const char *p = *endp;
3087 char buf[1024];
3088 QDict *qdict = qdict_new();
3089
bellard9307c4c2004-04-04 12:57:25 +00003090 /* parse the parameters */
3091 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003092 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003093 typestr = key_get_info(typestr, &key);
3094 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003095 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003096 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003097 typestr++;
3098 switch(c) {
3099 case 'F':
bellard81d09122004-07-14 17:21:37 +00003100 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003101 case 's':
3102 {
3103 int ret;
ths3b46e622007-09-17 08:09:54 +00003104
blueswir1cd390082008-11-16 13:53:32 +00003105 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003106 p++;
3107 if (*typestr == '?') {
3108 typestr++;
3109 if (*p == '\0') {
3110 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003111 break;
bellard9307c4c2004-04-04 12:57:25 +00003112 }
3113 }
3114 ret = get_str(buf, sizeof(buf), &p);
3115 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003116 switch(c) {
3117 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003118 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003119 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003120 break;
3121 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003122 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003123 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003124 break;
3125 default:
Bandan Dasae502122015-06-03 18:38:08 -04003126 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003127 break;
3128 }
bellard9307c4c2004-04-04 12:57:25 +00003129 goto fail;
3130 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003131 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00003132 }
3133 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003134 case 'O':
3135 {
3136 QemuOptsList *opts_list;
3137 QemuOpts *opts;
3138
3139 opts_list = qemu_find_opts(key);
3140 if (!opts_list || opts_list->desc->name) {
3141 goto bad_type;
3142 }
3143 while (qemu_isspace(*p)) {
3144 p++;
3145 }
3146 if (!*p)
3147 break;
3148 if (get_str(buf, sizeof(buf), &p) < 0) {
3149 goto fail;
3150 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003151 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003152 if (!opts) {
3153 goto fail;
3154 }
3155 qemu_opts_to_qdict(opts, qdict);
3156 qemu_opts_del(opts);
3157 }
3158 break;
bellard9307c4c2004-04-04 12:57:25 +00003159 case '/':
3160 {
3161 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003162
blueswir1cd390082008-11-16 13:53:32 +00003163 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003164 p++;
3165 if (*p == '/') {
3166 /* format found */
3167 p++;
3168 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003169 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003170 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003171 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003172 count = count * 10 + (*p - '0');
3173 p++;
3174 }
3175 }
3176 size = -1;
3177 format = -1;
3178 for(;;) {
3179 switch(*p) {
3180 case 'o':
3181 case 'd':
3182 case 'u':
3183 case 'x':
3184 case 'i':
3185 case 'c':
3186 format = *p++;
3187 break;
3188 case 'b':
3189 size = 1;
3190 p++;
3191 break;
3192 case 'h':
3193 size = 2;
3194 p++;
3195 break;
3196 case 'w':
3197 size = 4;
3198 p++;
3199 break;
3200 case 'g':
3201 case 'L':
3202 size = 8;
3203 p++;
3204 break;
3205 default:
3206 goto next;
3207 }
3208 }
3209 next:
blueswir1cd390082008-11-16 13:53:32 +00003210 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003211 monitor_printf(mon, "invalid char in format: '%c'\n",
3212 *p);
bellard9307c4c2004-04-04 12:57:25 +00003213 goto fail;
3214 }
bellard9307c4c2004-04-04 12:57:25 +00003215 if (format < 0)
3216 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003217 if (format != 'i') {
3218 /* for 'i', not specifying a size gives -1 as size */
3219 if (size < 0)
3220 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003221 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003222 }
bellard9307c4c2004-04-04 12:57:25 +00003223 default_fmt_format = format;
3224 } else {
3225 count = 1;
3226 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003227 if (format != 'i') {
3228 size = default_fmt_size;
3229 } else {
3230 size = -1;
3231 }
bellard9307c4c2004-04-04 12:57:25 +00003232 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003233 qdict_put_int(qdict, "count", count);
3234 qdict_put_int(qdict, "format", format);
3235 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00003236 }
3237 break;
3238 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003239 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003240 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003241 {
blueswir1c2efc952007-09-25 17:28:42 +00003242 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003243
blueswir1cd390082008-11-16 13:53:32 +00003244 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003245 p++;
bellard34405572004-06-08 00:55:58 +00003246 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003247 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003248 if (*p == '\0') {
3249 typestr++;
3250 break;
3251 }
bellard34405572004-06-08 00:55:58 +00003252 } else {
3253 if (*p == '.') {
3254 p++;
blueswir1cd390082008-11-16 13:53:32 +00003255 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003256 p++;
bellard34405572004-06-08 00:55:58 +00003257 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003258 typestr++;
3259 break;
bellard34405572004-06-08 00:55:58 +00003260 }
3261 }
bellard13224a82006-07-14 22:03:35 +00003262 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003263 }
aliguori376253e2009-03-05 23:01:23 +00003264 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003265 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003266 /* Check if 'i' is greater than 32-bit */
3267 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003268 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003269 monitor_printf(mon, "integer is for 32-bit values\n");
3270 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003271 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003272 if (val < 0) {
3273 monitor_printf(mon, "enter a positive value\n");
3274 goto fail;
3275 }
Philippe Mathieu-Daudé8ec338a2018-06-25 09:42:34 -03003276 val *= MiB;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003277 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003278 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00003279 }
3280 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003281 case 'o':
3282 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003283 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003284 uint64_t val;
David Hildenbrandaf02f4c2018-11-21 17:44:14 +01003285 const char *end;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003286
3287 while (qemu_isspace(*p)) {
3288 p++;
3289 }
3290 if (*typestr == '?') {
3291 typestr++;
3292 if (*p == '\0') {
3293 break;
3294 }
3295 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003296 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003297 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02003298 monitor_printf(mon, "invalid size\n");
3299 goto fail;
3300 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003301 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02003302 p = end;
3303 }
3304 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003305 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003306 {
3307 double val;
3308
3309 while (qemu_isspace(*p))
3310 p++;
3311 if (*typestr == '?') {
3312 typestr++;
3313 if (*p == '\0') {
3314 break;
3315 }
3316 }
3317 if (get_double(mon, &val, &p) < 0) {
3318 goto fail;
3319 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003320 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003321 switch (*p) {
3322 case 'm':
3323 val /= 1e3; p += 2; break;
3324 case 'u':
3325 val /= 1e6; p += 2; break;
3326 case 'n':
3327 val /= 1e9; p += 2; break;
3328 }
3329 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003330 if (*p && !qemu_isspace(*p)) {
3331 monitor_printf(mon, "Unknown unit suffix\n");
3332 goto fail;
3333 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04003334 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003335 }
3336 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003337 case 'b':
3338 {
3339 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003340 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003341
3342 while (qemu_isspace(*p)) {
3343 p++;
3344 }
3345 beg = p;
3346 while (qemu_isgraph(*p)) {
3347 p++;
3348 }
3349 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003350 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003351 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003352 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003353 } else {
3354 monitor_printf(mon, "Expected 'on' or 'off'\n");
3355 goto fail;
3356 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003357 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003358 }
3359 break;
bellard9307c4c2004-04-04 12:57:25 +00003360 case '-':
3361 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003362 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003363 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003364 /* option */
ths3b46e622007-09-17 08:09:54 +00003365
bellard9307c4c2004-04-04 12:57:25 +00003366 c = *typestr++;
3367 if (c == '\0')
3368 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003369 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003370 p++;
bellard9307c4c2004-04-04 12:57:25 +00003371 if (*p == '-') {
3372 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003373 if(c != *p) {
3374 if(!is_valid_option(p, typestr)) {
3375
3376 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003377 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003378 goto fail;
3379 } else {
3380 skip_key = 1;
3381 }
bellard9307c4c2004-04-04 12:57:25 +00003382 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003383 if(skip_key) {
3384 p = tmp;
3385 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003386 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003387 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003388 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003389 }
bellard9307c4c2004-04-04 12:57:25 +00003390 }
bellard9307c4c2004-04-04 12:57:25 +00003391 }
3392 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003393 case 'S':
3394 {
3395 /* package all remaining string */
3396 int len;
3397
3398 while (qemu_isspace(*p)) {
3399 p++;
3400 }
3401 if (*typestr == '?') {
3402 typestr++;
3403 if (*p == '\0') {
3404 /* no remaining string: NULL argument */
3405 break;
3406 }
3407 }
3408 len = strlen(p);
3409 if (len <= 0) {
3410 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003411 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003412 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003413 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003414 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003415 p += len;
3416 }
3417 break;
bellard9307c4c2004-04-04 12:57:25 +00003418 default:
3419 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003420 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003421 goto fail;
3422 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003423 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003424 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003425 }
3426 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003427 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003428 p++;
3429 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003430 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003431 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003432 goto fail;
3433 }
3434
Bandan Dasae502122015-06-03 18:38:08 -04003435 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003436
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003437fail:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003438 qobject_unref(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003439 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003440 return NULL;
3441}
3442
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003443static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003444{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003445 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003446 const mon_cmd_t *cmd;
Collin Walling317c52c2018-05-07 10:30:54 -04003447 const char *cmd_start = cmdline;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003448
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003449 trace_handle_hmp_command(mon, cmdline);
3450
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003451 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003452 if (!cmd) {
3453 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003454 }
3455
Bandan Dasae502122015-06-03 18:38:08 -04003456 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3457 if (!qdict) {
Collin Walling317c52c2018-05-07 10:30:54 -04003458 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
3459 cmdline--;
3460 }
3461 monitor_printf(mon, "Try \"help %.*s\" for more information\n",
3462 (int)(cmdline - cmd_start), cmd_start);
Bandan Dasae502122015-06-03 18:38:08 -04003463 return;
3464 }
3465
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003466 cmd->cmd(mon, qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003467 qobject_unref(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003468}
3469
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003470static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003471{
3472 const char *p, *pstart;
3473 char cmd[128];
3474 int len;
3475
3476 p = list;
3477 for(;;) {
3478 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +02003479 p = qemu_strchrnul(p, '|');
bellard81d09122004-07-14 17:21:37 +00003480 len = p - pstart;
3481 if (len > sizeof(cmd) - 2)
3482 len = sizeof(cmd) - 2;
3483 memcpy(cmd, pstart, len);
3484 cmd[len] = '\0';
3485 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003486 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003487 }
3488 if (*p == '\0')
3489 break;
3490 p++;
3491 }
3492}
3493
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003494static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003495{
3496 DIR *ffs;
3497 struct dirent *d;
3498 char path[1024];
3499 char file[1024], file_prefix[1024];
3500 int input_path_len;
3501 const char *p;
3502
ths5fafdf22007-09-16 21:08:06 +00003503 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003504 if (!p) {
3505 input_path_len = 0;
3506 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003507 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003508 } else {
3509 input_path_len = p - input + 1;
3510 memcpy(path, input, input_path_len);
3511 if (input_path_len > sizeof(path) - 1)
3512 input_path_len = sizeof(path) - 1;
3513 path[input_path_len] = '\0';
3514 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3515 }
Bandan Das19f2db52015-06-03 18:38:07 -04003516
bellard81d09122004-07-14 17:21:37 +00003517 ffs = opendir(path);
3518 if (!ffs)
3519 return;
3520 for(;;) {
3521 struct stat sb;
3522 d = readdir(ffs);
3523 if (!d)
3524 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003525
3526 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3527 continue;
3528 }
3529
bellard81d09122004-07-14 17:21:37 +00003530 if (strstart(d->d_name, file_prefix, NULL)) {
3531 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003532 if (input_path_len < sizeof(file))
3533 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3534 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003535 /* stat the file to find out if it's a directory.
3536 * In that case add a slash to speed up typing long paths
3537 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003538 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003539 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003540 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003541 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003542 }
3543 }
3544 closedir(ffs);
3545}
3546
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003547static const char *next_arg_type(const char *typestr)
3548{
3549 const char *p = strchr(typestr, ':');
3550 return (p != NULL ? ++p : typestr);
3551}
3552
Hani Benhabiles40d19392014-05-07 23:41:30 +01003553static void add_completion_option(ReadLineState *rs, const char *str,
3554 const char *option)
3555{
3556 if (!str || !option) {
3557 return;
3558 }
3559 if (!strncmp(option, str, strlen(str))) {
3560 readline_add_completion(rs, option);
3561 }
3562}
3563
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003564void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3565{
3566 size_t len;
3567 ChardevBackendInfoList *list, *start;
3568
3569 if (nb_args != 2) {
3570 return;
3571 }
3572 len = strlen(str);
3573 readline_set_completion_index(rs, len);
3574
3575 start = list = qmp_query_chardev_backends(NULL);
3576 while (list) {
3577 const char *chr_name = list->value->name;
3578
3579 if (!strncmp(chr_name, str, len)) {
3580 readline_add_completion(rs, chr_name);
3581 }
3582 list = list->next;
3583 }
3584 qapi_free_ChardevBackendInfoList(start);
3585}
3586
Hani Benhabilesb162b492014-05-07 23:41:31 +01003587void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3588{
3589 size_t len;
3590 int i;
3591
3592 if (nb_args != 2) {
3593 return;
3594 }
3595 len = strlen(str);
3596 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003597 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003598 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003599 }
3600}
3601
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003602void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003603{
3604 GSList *list, *elt;
3605 size_t len;
3606
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003607 if (nb_args != 2) {
3608 return;
3609 }
3610
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003611 len = strlen(str);
3612 readline_set_completion_index(rs, len);
3613 list = elt = object_class_get_list(TYPE_DEVICE, false);
3614 while (elt) {
3615 const char *name;
3616 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3617 TYPE_DEVICE);
3618 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003619
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003620 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003621 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003622 readline_add_completion(rs, name);
3623 }
3624 elt = elt->next;
3625 }
3626 g_slist_free(list);
3627}
3628
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003629void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003630{
3631 GSList *list, *elt;
3632 size_t len;
3633
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003634 if (nb_args != 2) {
3635 return;
3636 }
3637
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003638 len = strlen(str);
3639 readline_set_completion_index(rs, len);
3640 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3641 while (elt) {
3642 const char *name;
3643
3644 name = object_class_get_name(OBJECT_CLASS(elt->data));
3645 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3646 readline_add_completion(rs, name);
3647 }
3648 elt = elt->next;
3649 }
3650 g_slist_free(list);
3651}
3652
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003653static void peripheral_device_del_completion(ReadLineState *rs,
3654 const char *str, size_t len)
3655{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003656 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3657 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003658
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003659 list = qdev_build_hotpluggable_device_list(peripheral);
3660 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003661 return;
3662 }
3663
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003664 for (item = list; item; item = g_slist_next(item)) {
3665 DeviceState *dev = item->data;
3666
3667 if (dev->id && !strncmp(str, dev->id, len)) {
3668 readline_add_completion(rs, dev->id);
3669 }
3670 }
3671
3672 g_slist_free(list);
3673}
3674
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003675void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3676{
3677 size_t len;
3678 ChardevInfoList *list, *start;
3679
3680 if (nb_args != 2) {
3681 return;
3682 }
3683 len = strlen(str);
3684 readline_set_completion_index(rs, len);
3685
3686 start = list = qmp_query_chardev(NULL);
3687 while (list) {
3688 ChardevInfo *chr = list->value;
3689
3690 if (!strncmp(chr->label, str, len)) {
3691 readline_add_completion(rs, chr->label);
3692 }
3693 list = list->next;
3694 }
3695 qapi_free_ChardevInfoList(start);
3696}
3697
Hani Benhabiles8e597772014-05-27 23:39:30 +01003698static void ringbuf_completion(ReadLineState *rs, const char *str)
3699{
3700 size_t len;
3701 ChardevInfoList *list, *start;
3702
3703 len = strlen(str);
3704 readline_set_completion_index(rs, len);
3705
3706 start = list = qmp_query_chardev(NULL);
3707 while (list) {
3708 ChardevInfo *chr_info = list->value;
3709
3710 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003711 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003712 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003713 readline_add_completion(rs, chr_info->label);
3714 }
3715 }
3716 list = list->next;
3717 }
3718 qapi_free_ChardevInfoList(start);
3719}
3720
Hani Benhabiles8e597772014-05-27 23:39:30 +01003721void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3722{
3723 if (nb_args != 2) {
3724 return;
3725 }
3726 ringbuf_completion(rs, str);
3727}
3728
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003729void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3730{
3731 size_t len;
3732
3733 if (nb_args != 2) {
3734 return;
3735 }
3736
3737 len = strlen(str);
3738 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003739 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003740}
3741
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003742void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003743{
3744 ObjectPropertyInfoList *list, *start;
3745 size_t len;
3746
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003747 if (nb_args != 2) {
3748 return;
3749 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003750 len = strlen(str);
3751 readline_set_completion_index(rs, len);
3752
3753 start = list = qmp_qom_list("/objects", NULL);
3754 while (list) {
3755 ObjectPropertyInfo *info = list->value;
3756
3757 if (!strncmp(info->type, "child<", 5)
3758 && !strncmp(info->name, str, len)) {
3759 readline_add_completion(rs, info->name);
3760 }
3761 list = list->next;
3762 }
3763 qapi_free_ObjectPropertyInfoList(start);
3764}
3765
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003766void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3767{
3768 int i;
3769 char *sep;
3770 size_t len;
3771
3772 if (nb_args != 2) {
3773 return;
3774 }
3775 sep = strrchr(str, '-');
3776 if (sep) {
3777 str = sep + 1;
3778 }
3779 len = strlen(str);
3780 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003781 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003782 if (!strncmp(str, QKeyCode_str(i), len)) {
3783 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003784 }
3785 }
3786}
3787
Hani Benhabiles40d19392014-05-07 23:41:30 +01003788void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3789{
3790 size_t len;
3791
3792 len = strlen(str);
3793 readline_set_completion_index(rs, len);
3794 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003795 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003796 int count, i;
3797 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003798 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003799 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003800 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003801 const char *name = ncs[i]->name;
3802 if (!strncmp(str, name, len)) {
3803 readline_add_completion(rs, name);
3804 }
3805 }
3806 } else if (nb_args == 3) {
3807 add_completion_option(rs, str, "on");
3808 add_completion_option(rs, str, "off");
3809 }
3810}
3811
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003812void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3813{
3814 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003815 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003816
3817 if (nb_args != 2) {
3818 return;
3819 }
3820
3821 len = strlen(str);
3822 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003823 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003824 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003825 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003826 QemuOpts *opts;
3827 const char *name = ncs[i]->name;
3828 if (strncmp(str, name, len)) {
3829 continue;
3830 }
3831 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3832 if (opts) {
3833 readline_add_completion(rs, name);
3834 }
3835 }
3836}
3837
Lluís Vilanovabd712112016-07-11 12:53:51 +02003838void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3839{
3840 size_t len;
3841
3842 len = strlen(str);
3843 readline_set_completion_index(rs, len);
3844 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003845 TraceEventIter iter;
3846 TraceEvent *ev;
3847 char *pattern = g_strdup_printf("%s*", str);
3848 trace_event_iter_init(&iter, pattern);
3849 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3850 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003851 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003852 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003853 }
3854}
3855
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003856void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3857{
3858 size_t len;
3859
3860 len = strlen(str);
3861 readline_set_completion_index(rs, len);
3862 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003863 TraceEventIter iter;
3864 TraceEvent *ev;
3865 char *pattern = g_strdup_printf("%s*", str);
3866 trace_event_iter_init(&iter, pattern);
3867 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3868 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003869 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003870 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003871 } else if (nb_args == 3) {
3872 add_completion_option(rs, str, "on");
3873 add_completion_option(rs, str, "off");
3874 }
3875}
3876
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003877void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3878{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003879 int i;
3880
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003881 if (nb_args != 2) {
3882 return;
3883 }
3884 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003885 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3886 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003887 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003888}
3889
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003890void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3891 const char *str)
3892{
3893 size_t len;
3894
3895 len = strlen(str);
3896 readline_set_completion_index(rs, len);
3897 if (nb_args == 2) {
3898 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003899 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003900 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003901 if (!strncmp(str, name, len)) {
3902 readline_add_completion(rs, name);
3903 }
3904 }
3905 } else if (nb_args == 3) {
3906 add_completion_option(rs, str, "on");
3907 add_completion_option(rs, str, "off");
3908 }
3909}
3910
Liang Li50e9a622015-03-23 16:32:29 +08003911void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3912 const char *str)
3913{
3914 size_t len;
3915
3916 len = strlen(str);
3917 readline_set_completion_index(rs, len);
3918 if (nb_args == 2) {
3919 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003920 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003921 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003922 if (!strncmp(str, name, len)) {
3923 readline_add_completion(rs, name);
3924 }
3925 }
3926 }
3927}
3928
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003929static void vm_completion(ReadLineState *rs, const char *str)
3930{
3931 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003932 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003933 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003934
3935 len = strlen(str);
3936 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003937
Kevin Wolf88be7b42016-05-20 18:49:07 +02003938 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003939 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003940 AioContext *ctx = bdrv_get_aio_context(bs);
3941 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003942
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003943 aio_context_acquire(ctx);
3944 if (bdrv_can_snapshot(bs)) {
3945 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3946 }
3947 aio_context_release(ctx);
3948 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003949 continue;
3950 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003951
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003952 snapshot = snapshots;
3953 while (snapshot) {
3954 char *completion = snapshot->value->name;
3955 if (!strncmp(str, completion, len)) {
3956 readline_add_completion(rs, completion);
3957 }
3958 completion = snapshot->value->id;
3959 if (!strncmp(str, completion, len)) {
3960 readline_add_completion(rs, completion);
3961 }
3962 snapshot = snapshot->next;
3963 }
3964 qapi_free_SnapshotInfoList(snapshots);
3965 }
3966
3967}
3968
3969void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3970{
3971 if (nb_args == 2) {
3972 vm_completion(rs, str);
3973 }
3974}
3975
3976void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3977{
3978 if (nb_args == 2) {
3979 vm_completion(rs, str);
3980 }
3981}
3982
Wenchao Xiac35b6402013-08-27 20:38:24 +08003983static void monitor_find_completion_by_table(Monitor *mon,
3984 const mon_cmd_t *cmd_table,
3985 char **args,
3986 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003987{
3988 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003989 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003990 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003991 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003992 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003993
bellard81d09122004-07-14 17:21:37 +00003994 if (nb_args <= 1) {
3995 /* command completion */
3996 if (nb_args == 0)
3997 cmdname = "";
3998 else
3999 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004000 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004001 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004002 if (!runstate_check(RUN_STATE_PRECONFIG) ||
4003 cmd_can_preconfig(cmd)) {
4004 cmd_completion(mon, cmdname, cmd->name);
4005 }
bellard81d09122004-07-14 17:21:37 +00004006 }
4007 } else {
4008 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004009 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004010 if (compare_cmd(args[0], cmd->name) &&
4011 (!runstate_check(RUN_STATE_PRECONFIG) ||
4012 cmd_can_preconfig(cmd))) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004013 break;
4014 }
bellard81d09122004-07-14 17:21:37 +00004015 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004016 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004017 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004018 }
4019
Wenchao Xiad903a772013-08-27 20:38:25 +08004020 if (cmd->sub_table) {
4021 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004022 monitor_find_completion_by_table(mon, cmd->sub_table,
4023 &args[1], nb_args - 1);
4024 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004025 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004026 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004027 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4028 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004029 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004030
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004031 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004032 for(i = 0; i < nb_args - 2; i++) {
4033 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004034 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004035 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004036 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004037 }
4038 }
4039 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00004040 old_ptype = NULL;
4041 while (*ptype == '-' && old_ptype != ptype) {
4042 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004043 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004044 }
bellard81d09122004-07-14 17:21:37 +00004045 switch(*ptype) {
4046 case 'F':
4047 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004048 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004049 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004050 break;
4051 case 'B':
4052 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004053 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01004054 while ((blk = blk_next(blk)) != NULL) {
4055 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004056 if (str[0] == '\0' ||
4057 !strncmp(name, str, strlen(str))) {
4058 readline_add_completion(mon->rs, name);
4059 }
4060 }
bellard81d09122004-07-14 17:21:37 +00004061 break;
bellard7fe48482004-10-09 18:08:01 +00004062 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004063 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004064 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004065 monitor_find_completion_by_table(mon, cmd_table,
4066 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004067 }
4068 break;
bellard81d09122004-07-14 17:21:37 +00004069 default:
4070 break;
4071 }
4072 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004073}
4074
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004075static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004076 const char *cmdline)
4077{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004078 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004079 char *args[MAX_ARGS];
4080 int nb_args, len;
4081
4082 /* 1. parse the cmdline */
4083 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4084 return;
4085 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004086
4087 /* if the line ends with a space, it means we want to complete the
4088 next arg */
4089 len = strlen(cmdline);
4090 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4091 if (nb_args >= MAX_ARGS) {
4092 goto cleanup;
4093 }
4094 args[nb_args++] = g_strdup("");
4095 }
4096
4097 /* 2. auto complete according to args */
4098 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004099
4100cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004101 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004102}
4103
aliguori731b0362009-03-05 23:01:42 +00004104static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004105{
aliguori731b0362009-03-05 23:01:42 +00004106 Monitor *mon = opaque;
4107
Peter Xudf152fb2018-03-09 16:59:55 +08004108 return !atomic_mb_read(&mon->suspend_cnt);
bellard9dc39cb2004-03-14 21:38:27 +00004109}
4110
Peter Xu546aa562018-03-09 16:59:54 +08004111/*
Markus Armbruster7cb21232018-07-03 10:53:52 +02004112 * Emit QMP response @rsp with ID @id to @mon.
4113 * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
4114 * Nothing is emitted then.
Peter Xu546aa562018-03-09 16:59:54 +08004115 */
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004116static void monitor_qmp_respond(Monitor *mon, QDict *rsp)
Peter Xu546aa562018-03-09 16:59:54 +08004117{
Peter Xu546aa562018-03-09 16:59:54 +08004118 if (rsp) {
Marc-André Lureau27656012018-08-29 15:40:36 +02004119 qmp_send_response(mon, rsp);
Peter Xu546aa562018-03-09 16:59:54 +08004120 }
Peter Xu546aa562018-03-09 16:59:54 +08004121}
4122
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004123static void monitor_qmp_dispatch(Monitor *mon, QObject *req)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004124{
Markus Armbrusterb2731452018-07-03 10:53:41 +02004125 Monitor *old_mon;
Markus Armbrusterd43b1692018-07-03 10:53:49 +02004126 QDict *rsp;
Markus Armbruster69240fe2018-07-03 10:53:43 +02004127 QDict *error;
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01004128
Peter Xu227a0752018-03-09 16:59:49 +08004129 old_mon = cur_mon;
4130 cur_mon = mon;
4131
Markus Armbruster674ed722018-07-03 10:53:37 +02004132 rsp = qmp_dispatch(mon->qmp.commands, req, qmp_oob_enabled(mon));
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004133
Peter Xu227a0752018-03-09 16:59:49 +08004134 cur_mon = old_mon;
4135
Markus Armbruster69240fe2018-07-03 10:53:43 +02004136 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
Markus Armbrusterd43b1692018-07-03 10:53:49 +02004137 error = qdict_get_qdict(rsp, "error");
Markus Armbruster69240fe2018-07-03 10:53:43 +02004138 if (error
4139 && !g_strcmp0(qdict_get_try_str(error, "class"),
4140 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
4141 /* Provide a more useful error message */
4142 qdict_del(error, "desc");
4143 qdict_put_str(error, "desc", "Expecting capabilities negotiation"
4144 " with 'qmp_capabilities'");
4145 }
Peter Xu71da4662018-03-09 16:59:57 +08004146 }
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004147
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004148 monitor_qmp_respond(mon, rsp);
Markus Armbruster7cb21232018-07-03 10:53:52 +02004149 qobject_unref(rsp);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004150}
4151
Peter Xu71da4662018-03-09 16:59:57 +08004152/*
Markus Armbruster774a6b62018-07-03 10:53:57 +02004153 * Pop a QMP request from a monitor request queue.
4154 * Return the request, or NULL all request queues are empty.
Peter Xu71da4662018-03-09 16:59:57 +08004155 * We are using round-robin fashion to pop the request, to avoid
4156 * processing commands only on a very busy monitor. To achieve that,
4157 * when we process one request on a specific monitor, we put that
4158 * monitor to the end of mon_list queue.
Peter Xu9ab84472018-10-09 14:27:13 +08004159 *
4160 * Note: if the function returned with non-NULL, then the caller will
4161 * be with mon->qmp.qmp_queue_lock held, and the caller is responsible
4162 * to release it.
Peter Xu71da4662018-03-09 16:59:57 +08004163 */
Peter Xu9ab84472018-10-09 14:27:13 +08004164static QMPRequest *monitor_qmp_requests_pop_any_with_lock(void)
Peter Xu71da4662018-03-09 16:59:57 +08004165{
4166 QMPRequest *req_obj = NULL;
4167 Monitor *mon;
4168
4169 qemu_mutex_lock(&monitor_lock);
4170
4171 QTAILQ_FOREACH(mon, &mon_list, entry) {
4172 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4173 req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
Peter Xu71da4662018-03-09 16:59:57 +08004174 if (req_obj) {
Peter Xu9ab84472018-10-09 14:27:13 +08004175 /* With the lock of corresponding queue held */
Peter Xu71da4662018-03-09 16:59:57 +08004176 break;
4177 }
Peter Xu9ab84472018-10-09 14:27:13 +08004178 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
Peter Xu71da4662018-03-09 16:59:57 +08004179 }
4180
4181 if (req_obj) {
4182 /*
4183 * We found one request on the monitor. Degrade this monitor's
4184 * priority to lowest by re-inserting it to end of queue.
4185 */
4186 QTAILQ_REMOVE(&mon_list, mon, entry);
4187 QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
4188 }
4189
4190 qemu_mutex_unlock(&monitor_lock);
4191
4192 return req_obj;
4193}
4194
4195static void monitor_qmp_bh_dispatcher(void *data)
4196{
Peter Xu9ab84472018-10-09 14:27:13 +08004197 QMPRequest *req_obj = monitor_qmp_requests_pop_any_with_lock();
Markus Armbruster7cb21232018-07-03 10:53:52 +02004198 QDict *rsp;
Marc-André Lureau176160c2018-08-29 15:40:37 +02004199 bool need_resume;
Peter Xu9ab84472018-10-09 14:27:13 +08004200 Monitor *mon;
Peter Xu71da4662018-03-09 16:59:57 +08004201
Markus Armbrusterb2731452018-07-03 10:53:41 +02004202 if (!req_obj) {
4203 return;
Peter Xu71da4662018-03-09 16:59:57 +08004204 }
Markus Armbrusterb2731452018-07-03 10:53:41 +02004205
Peter Xu9ab84472018-10-09 14:27:13 +08004206 mon = req_obj->mon;
Marc-André Lureau176160c2018-08-29 15:40:37 +02004207 /* qmp_oob_enabled() might change after "qmp_capabilities" */
Peter Xu9ab84472018-10-09 14:27:13 +08004208 need_resume = !qmp_oob_enabled(mon) ||
4209 mon->qmp.qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1;
4210 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
Markus Armbruster1cc37472018-07-03 10:53:44 +02004211 if (req_obj->req) {
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004212 QDict *qdict = qobject_to(QDict, req_obj->req);
4213 QObject *id = qdict ? qdict_get(qdict, "id") : NULL;
4214 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id) ?: "");
4215 monitor_qmp_dispatch(mon, req_obj->req);
Markus Armbruster1cc37472018-07-03 10:53:44 +02004216 } else {
4217 assert(req_obj->err);
Markus Armbruster7cb21232018-07-03 10:53:52 +02004218 rsp = qmp_error_response(req_obj->err);
Marc-André Lureau42eab8d2018-07-05 18:42:01 +02004219 req_obj->err = NULL;
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004220 monitor_qmp_respond(mon, rsp);
Markus Armbruster7cb21232018-07-03 10:53:52 +02004221 qobject_unref(rsp);
Markus Armbruster1cc37472018-07-03 10:53:44 +02004222 }
4223
Marc-André Lureau176160c2018-08-29 15:40:37 +02004224 if (need_resume) {
Markus Armbrusterb2731452018-07-03 10:53:41 +02004225 /* Pairs with the monitor_suspend() in handle_qmp_command() */
Peter Xu9ab84472018-10-09 14:27:13 +08004226 monitor_resume(mon);
Markus Armbrusterb2731452018-07-03 10:53:41 +02004227 }
4228 qmp_request_free(req_obj);
4229
4230 /* Reschedule instead of looping so the main loop stays responsive */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004231 qemu_bh_schedule(qmp_dispatcher_bh);
Peter Xu71da4662018-03-09 16:59:57 +08004232}
4233
Markus Armbruster62815d82018-08-23 18:40:01 +02004234static void handle_qmp_command(void *opaque, QObject *req, Error *err)
Peter Xu71da4662018-03-09 16:59:57 +08004235{
Markus Armbruster62815d82018-08-23 18:40:01 +02004236 Monitor *mon = opaque;
4237 QObject *id = NULL;
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004238 QDict *qdict;
Peter Xu71da4662018-03-09 16:59:57 +08004239 QMPRequest *req_obj;
4240
Markus Armbruster84a56f32018-08-23 18:40:06 +02004241 assert(!req != !err);
Peter Xu71da4662018-03-09 16:59:57 +08004242
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004243 qdict = qobject_to(QDict, req);
4244 if (qdict) {
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004245 id = qdict_get(qdict, "id");
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004246 } /* else will fail qmp_dispatch() */
4247
Markus Armbruster8720e632018-07-16 11:10:12 +02004248 if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
Markus Armbruster45434ba2018-07-03 10:53:39 +02004249 QString *req_json = qobject_to_json(req);
4250 trace_handle_qmp_command(mon, qstring_get_str(req_json));
4251 qobject_unref(req_json);
Peter Xu71da4662018-03-09 16:59:57 +08004252 }
4253
Markus Armbruster69240fe2018-07-03 10:53:43 +02004254 if (qdict && qmp_is_oob(qdict)) {
Markus Armbruster774a6b62018-07-03 10:53:57 +02004255 /* OOB commands are executed immediately */
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004256 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) ?: "");
4257 monitor_qmp_dispatch(mon, req);
Marc-André Lureaucb9ec422018-08-09 13:44:16 +02004258 qobject_unref(req);
Markus Armbrusterb2731452018-07-03 10:53:41 +02004259 return;
Peter Xucf869d52018-03-10 20:38:05 -06004260 }
4261
Peter Xu71da4662018-03-09 16:59:57 +08004262 req_obj = g_new0(QMPRequest, 1);
4263 req_obj->mon = mon;
Peter Xu71da4662018-03-09 16:59:57 +08004264 req_obj->req = req;
Markus Armbruster1cc37472018-07-03 10:53:44 +02004265 req_obj->err = err;
Peter Xu71da4662018-03-09 16:59:57 +08004266
Peter Xubf1e7302018-03-09 16:59:59 +08004267 /* Protect qmp_requests and fetching its length. */
4268 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4269
Peter Xu71da4662018-03-09 16:59:57 +08004270 /*
Peter Xu9ab84472018-10-09 14:27:13 +08004271 * Suspend the monitor when we can't queue more requests after
4272 * this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume
4273 * it. Note that when OOB is disabled, we queue at most one
4274 * command, for backward compatibility.
Peter Xu71da4662018-03-09 16:59:57 +08004275 */
Peter Xu9ab84472018-10-09 14:27:13 +08004276 if (!qmp_oob_enabled(mon) ||
4277 mon->qmp.qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) {
Peter Xu71da4662018-03-09 16:59:57 +08004278 monitor_suspend(mon);
Peter Xu71da4662018-03-09 16:59:57 +08004279 }
4280
4281 /*
4282 * Put the request to the end of queue so that requests will be
Marc-André Lureau4eaca8d2019-02-20 16:42:53 +01004283 * handled in time order. Ownership for req_obj, req,
Peter Xu71da4662018-03-09 16:59:57 +08004284 * etc. will be delivered to the handler side.
4285 */
Peter Xu9ab84472018-10-09 14:27:13 +08004286 assert(mon->qmp.qmp_requests->length < QMP_REQ_QUEUE_LEN_MAX);
Peter Xu71da4662018-03-09 16:59:57 +08004287 g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
4288 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4289
4290 /* Kick the dispatcher routine */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004291 qemu_bh_schedule(qmp_dispatcher_bh);
Peter Xu71da4662018-03-09 16:59:57 +08004292}
4293
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004294static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004295{
Peter Xu227a0752018-03-09 16:59:49 +08004296 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004297
Peter Xu227a0752018-03-09 16:59:49 +08004298 json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004299}
4300
aliguori731b0362009-03-05 23:01:42 +00004301static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004302{
aliguori731b0362009-03-05 23:01:42 +00004303 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004304 int i;
aliguori376253e2009-03-05 23:01:23 +00004305
aliguori731b0362009-03-05 23:01:42 +00004306 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004307
aliguoricde76ee2009-03-05 23:01:51 +00004308 if (cur_mon->rs) {
4309 for (i = 0; i < size; i++)
4310 readline_handle_byte(cur_mon->rs, buf[i]);
4311 } else {
4312 if (size == 0 || buf[size - 1] != 0)
4313 monitor_printf(cur_mon, "corrupted command\n");
4314 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004315 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004316 }
aliguori731b0362009-03-05 23:01:42 +00004317
4318 cur_mon = old_mon;
4319}
aliguorid8f44602008-10-06 13:52:44 +00004320
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004321static void monitor_command_cb(void *opaque, const char *cmdline,
4322 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004323{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004324 Monitor *mon = opaque;
4325
aliguori731b0362009-03-05 23:01:42 +00004326 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004327 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004328 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004329}
4330
aliguoricde76ee2009-03-05 23:01:51 +00004331int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004332{
Peter Xue3e977d2018-03-09 16:59:56 +08004333 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004334 return -ENOTTY;
Peter Xue3e977d2018-03-09 16:59:56 +08004335 }
4336
Peter Xudf152fb2018-03-09 16:59:55 +08004337 atomic_inc(&mon->suspend_cnt);
Peter Xue3e977d2018-03-09 16:59:56 +08004338
Marc-André Lureauef12a702018-12-06 00:37:32 +04004339 if (mon->use_io_thread) {
Peter Xue3e977d2018-03-09 16:59:56 +08004340 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004341 * Kick I/O thread to make sure this takes effect. It'll be
Peter Xue3e977d2018-03-09 16:59:56 +08004342 * evaluated again in prepare() of the watch object.
4343 */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004344 aio_notify(iothread_get_aio_context(mon_iothread));
Peter Xue3e977d2018-03-09 16:59:56 +08004345 }
4346
4347 trace_monitor_suspend(mon, 1);
aliguoricde76ee2009-03-05 23:01:51 +00004348 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004349}
4350
Marc-André Lureauef12a702018-12-06 00:37:32 +04004351static void monitor_accept_input(void *opaque)
4352{
4353 Monitor *mon = opaque;
4354
4355 qemu_chr_fe_accept_input(&mon->chr);
4356}
4357
aliguori376253e2009-03-05 23:01:23 +00004358void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004359{
Peter Xue3e977d2018-03-09 16:59:56 +08004360 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004361 return;
Peter Xudf152fb2018-03-09 16:59:55 +08004362 }
Peter Xue3e977d2018-03-09 16:59:56 +08004363
4364 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
Marc-André Lureauef12a702018-12-06 00:37:32 +04004365 AioContext *ctx;
4366
4367 if (mon->use_io_thread) {
4368 ctx = iothread_get_aio_context(mon_iothread);
Peter Xue3e977d2018-03-09 16:59:56 +08004369 } else {
Marc-André Lureauef12a702018-12-06 00:37:32 +04004370 ctx = qemu_get_aio_context();
4371 }
4372
4373 if (!monitor_is_qmp(mon)) {
Peter Xue3e977d2018-03-09 16:59:56 +08004374 assert(mon->rs);
4375 readline_show_prompt(mon->rs);
4376 }
Marc-André Lureauef12a702018-12-06 00:37:32 +04004377
4378 aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
Peter Xue3e977d2018-03-09 16:59:56 +08004379 }
Marc-André Lureauef12a702018-12-06 00:37:32 +04004380
Peter Xue3e977d2018-03-09 16:59:56 +08004381 trace_monitor_suspend(mon, -1);
bellard7e2515e2004-08-01 21:52:19 +00004382}
4383
Markus Armbruster18166042018-07-03 10:53:51 +02004384static QDict *qmp_greeting(Monitor *mon)
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004385{
Peter Xu02130312018-03-09 16:59:53 +08004386 QList *cap_list = qlist_new();
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004387 QObject *ver = NULL;
Peter Xu02130312018-03-09 16:59:53 +08004388 QMPCapability cap;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004389
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004390 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004391
Peter Xu02130312018-03-09 16:59:53 +08004392 for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
Markus Armbruster279f9e02018-07-03 10:53:56 +02004393 if (mon->qmp.capab_offered[cap]) {
4394 qlist_append_str(cap_list, QMPCapability_str(cap));
Peter Xu02130312018-03-09 16:59:53 +08004395 }
Peter Xu02130312018-03-09 16:59:53 +08004396 }
4397
Markus Armbruster18166042018-07-03 10:53:51 +02004398 return qdict_from_jsonf_nofail(
4399 "{'QMP': {'version': %p, 'capabilities': %p}}",
4400 ver, cap_list);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004401}
4402
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004403static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004404{
Markus Armbruster18166042018-07-03 10:53:51 +02004405 QDict *data;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004406 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004407
Luiz Capitulino47116d12010-02-08 17:01:30 -02004408 switch (event) {
4409 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01004410 mon->qmp.commands = &qmp_cap_negotiation_commands;
Peter Xu02130312018-03-09 16:59:53 +08004411 monitor_qmp_caps_reset(mon);
Markus Armbruster18166042018-07-03 10:53:51 +02004412 data = qmp_greeting(mon);
Marc-André Lureau27656012018-08-29 15:40:36 +02004413 qmp_send_response(mon, data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004414 qobject_unref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004415 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004416 break;
4417 case CHR_EVENT_CLOSED:
Peter Xuc73a8432018-06-20 15:32:19 +08004418 /*
4419 * Note: this is only useful when the output of the chardev
4420 * backend is still open. For example, when the backend is
4421 * stdio, it's possible that stdout is still open when stdin
4422 * is closed.
4423 */
Peter Xu6d2d5632018-03-26 14:38:55 +08004424 monitor_qmp_cleanup_queues(mon);
Markus Armbruster74358f22015-03-06 19:35:59 +01004425 json_message_parser_destroy(&mon->qmp.parser);
Markus Armbruster62815d82018-08-23 18:40:01 +02004426 json_message_parser_init(&mon->qmp.parser, handle_qmp_command,
4427 mon, NULL);
Corey Bryantefb87c12012-08-14 16:43:48 -04004428 mon_refcount--;
4429 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004430 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004431 }
4432}
4433
aliguori731b0362009-03-05 23:01:42 +00004434static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004435{
aliguori376253e2009-03-05 23:01:23 +00004436 Monitor *mon = opaque;
4437
aliguori2724b182009-03-05 23:01:47 +00004438 switch (event) {
4439 case CHR_EVENT_MUX_IN:
Peter Xudc7cbcd2018-06-08 11:55:05 +08004440 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004441 mon->mux_out = 0;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004442 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004443 if (mon->reset_seen) {
4444 readline_restart(mon->rs);
4445 monitor_resume(mon);
4446 monitor_flush(mon);
4447 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004448 atomic_mb_set(&mon->suspend_cnt, 0);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004449 }
aliguori2724b182009-03-05 23:01:47 +00004450 break;
ths86e94de2007-01-05 22:01:59 +00004451
aliguori2724b182009-03-05 23:01:47 +00004452 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004453 if (mon->reset_seen) {
Peter Xudf152fb2018-03-09 16:59:55 +08004454 if (atomic_mb_read(&mon->suspend_cnt) == 0) {
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004455 monitor_printf(mon, "\n");
4456 }
4457 monitor_flush(mon);
4458 monitor_suspend(mon);
4459 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004460 atomic_inc(&mon->suspend_cnt);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004461 }
Peter Xudc7cbcd2018-06-08 11:55:05 +08004462 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004463 mon->mux_out = 1;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004464 qemu_mutex_unlock(&mon->mon_lock);
aliguori2724b182009-03-05 23:01:47 +00004465 break;
4466
Amit Shahb6b8df52009-10-07 18:31:16 +05304467 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004468 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4469 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004470 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004471 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004472 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004473 }
4474 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004475 mon_refcount++;
4476 break;
4477
4478 case CHR_EVENT_CLOSED:
4479 mon_refcount--;
4480 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004481 break;
4482 }
ths86e94de2007-01-05 22:01:59 +00004483}
4484
Wayne Xia816f8922011-10-12 11:32:41 +08004485static int
4486compare_mon_cmd(const void *a, const void *b)
4487{
4488 return strcmp(((const mon_cmd_t *)a)->name,
4489 ((const mon_cmd_t *)b)->name);
4490}
4491
4492static void sortcmdlist(void)
4493{
4494 int array_num;
4495 int elem_size = sizeof(mon_cmd_t);
4496
4497 array_num = sizeof(mon_cmds)/elem_size-1;
4498 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4499
4500 array_num = sizeof(info_cmds)/elem_size-1;
4501 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4502}
4503
Peter Xua5ed3522018-03-09 16:59:52 +08004504static void monitor_iothread_init(void)
4505{
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004506 mon_iothread = iothread_create("mon_iothread", &error_abort);
Peter Xua5ed3522018-03-09 16:59:52 +08004507}
4508
Peter Xu6adf08d2018-03-09 16:59:50 +08004509void monitor_init_globals(void)
4510{
4511 monitor_init_qmp_commands();
4512 monitor_qapi_event_init();
4513 sortcmdlist();
4514 qemu_mutex_init(&monitor_lock);
Peter Xu47451462018-06-08 11:55:11 +08004515 qemu_mutex_init(&mon_fdsets_lock);
Wolfgang Bumiller85117702018-09-25 10:15:07 +02004516
4517 /*
4518 * The dispatcher BH must run in the main loop thread, since we
4519 * have commands assuming that context. It would be nice to get
4520 * rid of those assumptions.
4521 */
4522 qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
4523 monitor_qmp_bh_dispatcher,
4524 NULL);
Peter Xu6adf08d2018-03-09 16:59:50 +08004525}
4526
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004527/* These functions just adapt the readline interface in a typesafe way. We
4528 * could cast function pointers but that discards compiler checks.
4529 */
Stefan Weild5d15072014-01-25 18:18:23 +01004530static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4531 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004532{
4533 va_list ap;
4534 va_start(ap, fmt);
4535 monitor_vprintf(opaque, fmt, ap);
4536 va_end(ap);
4537}
4538
4539static void monitor_readline_flush(void *opaque)
4540{
4541 monitor_flush(opaque);
4542}
4543
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004544/*
4545 * Print to current monitor if we have one, else to stderr.
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004546 */
Markus Armbruster679cb8e2019-04-17 21:06:36 +02004547int error_vprintf(const char *fmt, va_list ap)
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004548{
Markus Armbruster8acb2a72019-04-17 21:06:41 +02004549 if (cur_mon && !monitor_cur_is_qmp()) {
4550 return monitor_vprintf(cur_mon, fmt, ap);
4551 }
4552 return vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004553}
4554
Markus Armbruster679cb8e2019-04-17 21:06:36 +02004555int error_vprintf_unless_qmp(const char *fmt, va_list ap)
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004556{
Markus Armbruster679cb8e2019-04-17 21:06:36 +02004557 if (!cur_mon) {
4558 return vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004559 }
Markus Armbruster679cb8e2019-04-17 21:06:36 +02004560 if (!monitor_cur_is_qmp()) {
4561 return monitor_vprintf(cur_mon, fmt, ap);
4562 }
4563 return -1;
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004564}
4565
Peter Xua5ed3522018-03-09 16:59:52 +08004566static void monitor_list_append(Monitor *mon)
4567{
4568 qemu_mutex_lock(&monitor_lock);
Marc-André Lureau8dac00b2018-12-06 00:37:36 +04004569 /*
4570 * This prevents inserting new monitors during monitor_cleanup().
4571 * A cleaner solution would involve the main thread telling other
4572 * threads to terminate, waiting for their termination.
4573 */
4574 if (!monitor_destroyed) {
4575 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4576 mon = NULL;
4577 }
Peter Xua5ed3522018-03-09 16:59:52 +08004578 qemu_mutex_unlock(&monitor_lock);
Marc-André Lureau8dac00b2018-12-06 00:37:36 +04004579
4580 if (mon) {
4581 monitor_data_destroy(mon);
4582 g_free(mon);
4583 }
Peter Xua5ed3522018-03-09 16:59:52 +08004584}
4585
4586static void monitor_qmp_setup_handlers_bh(void *opaque)
4587{
4588 Monitor *mon = opaque;
4589 GMainContext *context;
4590
Peter Xu3d7a1c42018-08-15 21:37:35 +08004591 assert(mon->use_io_thread);
Marc-André Lureau88e40e42018-12-06 00:37:31 +04004592 context = iothread_get_g_main_context(mon_iothread);
Peter Xu3d7a1c42018-08-15 21:37:35 +08004593 assert(context);
Peter Xua5ed3522018-03-09 16:59:52 +08004594 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4595 monitor_qmp_event, NULL, mon, context, true);
4596 monitor_list_append(mon);
4597}
4598
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004599void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004600{
Peter Xu6adf08d2018-03-09 16:59:50 +08004601 Monitor *mon = g_malloc(sizeof(*mon));
Peter Xube933ff2018-03-26 14:38:56 +08004602 bool use_readline = flags & MONITOR_USE_READLINE;
ths20d8a3e2007-02-18 17:04:49 +00004603
Peter Xu82582922018-10-09 14:27:15 +08004604 /* Note: we run QMP monitor in I/O thread when @chr supports that */
4605 monitor_data_init(mon, false,
4606 (flags & MONITOR_USE_CONTROL)
4607 && qemu_chr_has_feature(chr,
4608 QEMU_CHAR_FEATURE_GCONTEXT));
ths20d8a3e2007-02-18 17:04:49 +00004609
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004610 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004611 mon->flags = flags;
Peter Xube933ff2018-03-26 14:38:56 +08004612 if (use_readline) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004613 mon->rs = readline_init(monitor_readline_printf,
4614 monitor_readline_flush,
4615 mon,
4616 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004617 monitor_read_command(mon, 0);
4618 }
aliguori87127162009-03-05 23:01:29 +00004619
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004620 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004621 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster62815d82018-08-23 18:40:01 +02004622 json_message_parser_init(&mon->qmp.parser, handle_qmp_command,
4623 mon, NULL);
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004624 if (mon->use_io_thread) {
Peter Xua5ed3522018-03-09 16:59:52 +08004625 /*
4626 * Make sure the old iowatch is gone. It's possible when
4627 * e.g. the chardev is in client mode, with wait=on.
4628 */
4629 remove_fd_in_watch(chr);
4630 /*
4631 * We can't call qemu_chr_fe_set_handlers() directly here
Markus Armbruster774a6b62018-07-03 10:53:57 +02004632 * since chardev might be running in the monitor I/O
4633 * thread. Schedule a bottom half.
Peter Xua5ed3522018-03-09 16:59:52 +08004634 */
Marc-André Lureau88e40e42018-12-06 00:37:31 +04004635 aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread),
Peter Xua5ed3522018-03-09 16:59:52 +08004636 monitor_qmp_setup_handlers_bh, mon);
Markus Armbruster774a6b62018-07-03 10:53:57 +02004637 /* The bottom half will add @mon to @mon_list */
Peter Xua5ed3522018-03-09 16:59:52 +08004638 return;
4639 } else {
4640 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
4641 monitor_qmp_read, monitor_qmp_event,
4642 NULL, mon, NULL, true);
4643 }
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004644 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004645 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004646 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004647 }
aliguori87127162009-03-05 23:01:29 +00004648
Peter Xua5ed3522018-03-09 16:59:52 +08004649 monitor_list_append(mon);
bellard7e2515e2004-08-01 21:52:19 +00004650}
4651
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004652void monitor_cleanup(void)
4653{
Peter Xua5ed3522018-03-09 16:59:52 +08004654 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004655 * We need to explicitly stop the I/O thread (but not destroy it),
Markus Armbruster774a6b62018-07-03 10:53:57 +02004656 * clean up the monitor resources, then destroy the I/O thread since
Peter Xua5ed3522018-03-09 16:59:52 +08004657 * we need to unregister from chardev below in
4658 * monitor_data_destroy(), and chardev is not thread-safe yet
4659 */
Wolfgang Bumiller85117702018-09-25 10:15:07 +02004660 if (mon_iothread) {
4661 iothread_stop(mon_iothread);
4662 }
Peter Xua5ed3522018-03-09 16:59:52 +08004663
Markus Armbruster774a6b62018-07-03 10:53:57 +02004664 /* Flush output buffers and destroy monitors */
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004665 qemu_mutex_lock(&monitor_lock);
Marc-André Lureau8dac00b2018-12-06 00:37:36 +04004666 monitor_destroyed = true;
Paolo Bonzini82e870b2018-12-28 14:40:41 +01004667 while (!QTAILQ_EMPTY(&mon_list)) {
4668 Monitor *mon = QTAILQ_FIRST(&mon_list);
Peter Xu238d9f32018-03-09 16:59:51 +08004669 QTAILQ_REMOVE(&mon_list, mon, entry);
Marc-André Lureau34f1f3e2018-12-06 00:37:37 +04004670 /* Permit QAPI event emission from character frontend release */
4671 qemu_mutex_unlock(&monitor_lock);
Peter Xuabe3cd02018-03-09 17:00:02 +08004672 monitor_flush(mon);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004673 monitor_data_destroy(mon);
Marc-André Lureau34f1f3e2018-12-06 00:37:37 +04004674 qemu_mutex_lock(&monitor_lock);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004675 g_free(mon);
4676 }
4677 qemu_mutex_unlock(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004678
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004679 /* QEMUBHs needs to be deleted before destroying the I/O thread */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004680 qemu_bh_delete(qmp_dispatcher_bh);
4681 qmp_dispatcher_bh = NULL;
Wolfgang Bumiller85117702018-09-25 10:15:07 +02004682 if (mon_iothread) {
4683 iothread_destroy(mon_iothread);
4684 mon_iothread = NULL;
4685 }
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004686}
4687
Paolo Bonzini4d454572012-11-26 16:03:42 +01004688QemuOptsList qemu_mon_opts = {
4689 .name = "mon",
4690 .implied_opt_name = "chardev",
4691 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4692 .desc = {
4693 {
4694 .name = "mode",
4695 .type = QEMU_OPT_STRING,
4696 },{
4697 .name = "chardev",
4698 .type = QEMU_OPT_STRING,
4699 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004700 .name = "pretty",
4701 .type = QEMU_OPT_BOOL,
4702 },
4703 { /* end of list */ }
4704 },
4705};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004706
Igor Mammedovd4633542016-06-10 06:29:06 +05304707HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4708{
4709 MachineState *ms = MACHINE(qdev_get_machine());
4710 MachineClass *mc = MACHINE_GET_CLASS(ms);
4711
Igor Mammedovc5514d02017-02-10 11:20:57 +01004712 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304713 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4714 return NULL;
4715 }
4716
Igor Mammedovc5514d02017-02-10 11:20:57 +01004717 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304718}