blob: 2881345758e368bb8ac102f8422d167fae8b4cbc [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020031#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020033#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020035#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000036#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040037#include "chardev/char-fe.h"
Peter Xua5ed3522018-03-09 16:59:52 +080038#include "chardev/char-io.h"
Peter Xube933ff2018-03-26 14:38:56 +080039#include "chardev/char-mux.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020040#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020041#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010042#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030043#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010044#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010045#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010046#include "ui/input.h"
Max Reitzda27a002016-03-16 19:54:29 +010047#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000048#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020049#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010052#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020054#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010055#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010056#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010057#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010058#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010059#include "qapi/qmp/qjson.h"
60#include "qapi/qmp/json-streamer.h"
61#include "qapi/qmp/json-parser.h"
Peter Xu02130312018-03-09 16:59:53 +080062#include "qapi/qmp/qlist.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020063#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000064#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030066#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020067#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020068#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053069#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010070#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010071#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010072#include "qemu/log.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010073#include "qemu/option.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050074#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010075#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010076#include "block/qapi.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010077#include "qapi/qapi-commands.h"
78#include "qapi/qapi-events.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010079#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020080#include "qapi/qmp-event.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010081#include "qapi/qapi-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020082#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010083#include "sysemu/cpus.h"
Peter Xua5ed3522018-03-09 16:59:52 +080084#include "sysemu/iothread.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020085#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000086
Jason J. Hernea4538a52015-06-26 14:07:21 -040087#if defined(TARGET_S390X)
88#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020089#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040090#endif
91
bellard9307c4c2004-04-04 12:57:25 +000092/*
93 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000094 *
bellard9307c4c2004-04-04 12:57:25 +000095 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000096 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000097 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080098 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010099 * 'O' option string of the form NAME=VALUE,...
100 * parsed according to QemuOptsList given by its name
101 * Example: 'device:O' uses qemu_device_opts.
102 * Restriction: only lists with empty desc are supported
103 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000104 * 'i' 32 bit integer
105 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300106 * 'M' Non-negative target long (32 or 64 bit), in user mode the
107 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200108 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200109 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
110 * K, k suffix, which multiplies the value by 2^60 for suffixes E
111 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
112 * 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 +0100113 * 'T' double
114 * user mode accepts an optional ms, us, ns suffix,
115 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000116 * '/' optional gdb-like print format (like "/10x")
117 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300118 * '?' optional type (for all types, except '/')
119 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100120 * 'b' boolean
121 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300122 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000123 *
124 */
125
Anthony Liguoric227f092009-10-01 16:12:16 -0500126typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000128 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000129 const char *params;
130 const char *help;
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100131 const char *flags; /* p=preconfig */
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400132 void (*cmd)(Monitor *mon, const QDict *qdict);
133 /* @sub_table is a list of 2nd level of commands. If it does not exist,
134 * cmd should be used. If it exists, sub_table[?].cmd should be
135 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800136 */
137 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100138 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500139} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000140
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500142typedef struct mon_fd_t mon_fd_t;
143struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144 char *name;
145 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500146 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147};
148
Corey Bryantba1c0482012-08-14 16:43:43 -0400149/* file descriptor associated with a file descriptor set */
150typedef struct MonFdsetFd MonFdsetFd;
151struct MonFdsetFd {
152 int fd;
153 bool removed;
154 char *opaque;
155 QLIST_ENTRY(MonFdsetFd) next;
156};
157
158/* file descriptor set containing fds passed via SCM_RIGHTS */
159typedef struct MonFdset MonFdset;
160struct MonFdset {
161 int64_t id;
162 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400163 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400164 QLIST_ENTRY(MonFdset) next;
165};
166
Markus Armbruster74358f22015-03-06 19:35:59 +0100167typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200168 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100169 /*
170 * When a client connects, we're in capabilities negotiation mode.
171 * When command qmp_capabilities succeeds, we go into command
172 * mode.
173 */
Markus Armbruster635db182017-03-03 13:32:27 +0100174 QmpCommandList *commands;
Peter Xu02130312018-03-09 16:59:53 +0800175 bool qmp_caps[QMP_CAPABILITY__MAX];
Peter Xu71da4662018-03-09 16:59:57 +0800176 /*
177 * Protects qmp request/response queue. Please take monitor_lock
178 * first when used together.
179 */
180 QemuMutex qmp_queue_lock;
181 /* Input queue that holds all the parsed QMP requests */
182 GQueue *qmp_requests;
Peter Xuabe3cd02018-03-09 17:00:02 +0800183 /* Output queue contains all the QMP responses in order */
184 GQueue *qmp_responses;
Markus Armbruster74358f22015-03-06 19:35:59 +0100185} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200186
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187/*
188 * To prevent flooding clients, events can be throttled. The
189 * throttling is calculated globally, rather than per-Monitor
190 * instance.
191 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200192typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200193 QAPIEvent event; /* Throttling state for this event type and... */
194 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100195 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200196 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200197} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100198
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200199typedef struct {
200 int64_t rate; /* Minimum time (in ns) between two events */
201} MonitorQAPIEventConf;
202
aliguori87127162009-03-05 23:01:29 +0000203struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300204 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200205 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000206 int flags;
Peter Xudf152fb2018-03-09 16:59:55 +0800207 int suspend_cnt; /* Needs to be accessed atomically */
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400208 bool skip_flush;
Peter Xua5ed3522018-03-09 16:59:52 +0800209 bool use_io_thr;
Peter Xud9f25282018-06-08 11:55:07 +0800210
211 /*
212 * State used only in the thread "owning" the monitor.
213 * If @use_io_thr, this is mon_global.mon_iothread.
214 * Else, it's the main thread.
215 * These members can be safely accessed without locks.
216 */
aliguori731b0362009-03-05 23:01:42 +0000217 ReadLineState *rs;
Peter Xud9f25282018-06-08 11:55:07 +0800218
Markus Armbruster74358f22015-03-06 19:35:59 +0100219 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200220 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200221 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000222 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800223 mon_cmd_t *cmd_table;
Peter Xu238d9f32018-03-09 16:59:51 +0800224 QTAILQ_ENTRY(Monitor) entry;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800225
226 /*
227 * The per-monitor lock. We can't access guest memory when holding
228 * the lock.
229 */
230 QemuMutex mon_lock;
231
232 /*
233 * Fields that are protected by the per-monitor lock.
234 */
Peter Xu9409fc02018-06-08 11:55:06 +0800235 QLIST_HEAD(, mon_fd_t) fds;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800236 QString *outbuf;
237 guint out_watch;
238 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
239 int mux_out;
aliguori87127162009-03-05 23:01:29 +0000240};
241
Peter Xua5ed3522018-03-09 16:59:52 +0800242/* Let's add monitor global variables to this struct. */
243static struct {
244 IOThread *mon_iothread;
Peter Xu71da4662018-03-09 16:59:57 +0800245 /* Bottom half to dispatch the requests received from IO thread */
246 QEMUBH *qmp_dispatcher_bh;
Peter Xuabe3cd02018-03-09 17:00:02 +0800247 /* Bottom half to deliver the responses back to clients */
248 QEMUBH *qmp_respond_bh;
Peter Xua5ed3522018-03-09 16:59:52 +0800249} mon_global;
250
Peter Xu6d2d5632018-03-26 14:38:55 +0800251struct QMPRequest {
252 /* Owner of the request */
253 Monitor *mon;
254 /* "id" field of the request */
255 QObject *id;
256 /* Request object to be handled */
257 QObject *req;
258 /*
259 * Whether we need to resume the monitor afterward. This flag is
260 * used to emulate the old QMP server behavior that the current
261 * command must be completed before execution of the next one.
262 */
263 bool need_resume;
264};
265typedef struct QMPRequest QMPRequest;
266
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300267/* QMP checker flags */
268#define QMP_ACCEPT_UNKNOWNS 1
269
Peter Xu095cb1b2018-06-08 11:55:08 +0800270/* Protects mon_list, monitor_qapi_event_state. */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200271static QemuMutex monitor_lock;
Peter Xu095cb1b2018-06-08 11:55:08 +0800272static GHashTable *monitor_qapi_event_state;
Peter Xu238d9f32018-03-09 16:59:51 +0800273static QTAILQ_HEAD(mon_list, Monitor) mon_list;
Peter Xu095cb1b2018-06-08 11:55:08 +0800274
Peter Xu47451462018-06-08 11:55:11 +0800275/* Protects mon_fdsets */
276static QemuMutex mon_fdsets_lock;
Corey Bryantba1c0482012-08-14 16:43:43 -0400277static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800278
Corey Bryantefb87c12012-08-14 16:43:48 -0400279static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000280
Wayne Xia816f8922011-10-12 11:32:41 +0800281static mon_cmd_t mon_cmds[];
282static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000283
Markus Armbruster635db182017-03-03 13:32:27 +0100284QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100285
Markus Armbruster8631b602010-02-18 11:41:55 +0100286Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000287
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100288static void monitor_command_cb(void *opaque, const char *cmdline,
289 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000290
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100291/**
292 * Is @mon a QMP monitor?
293 */
294static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200295{
296 return (mon->flags & MONITOR_USE_CONTROL);
297}
298
Markus Armbruster489653b2015-03-06 20:01:05 +0100299/**
Peter Xue3e977d2018-03-09 16:59:56 +0800300 * Whether @mon is using readline? Note: not all HMP monitors use
301 * readline, e.g., gdbserver has a non-interactive HMP monitor, so
302 * readline is not used there.
303 */
304static inline bool monitor_uses_readline(const Monitor *mon)
305{
306 return mon->flags & MONITOR_USE_READLINE;
307}
308
309static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
310{
311 return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
312}
313
Peter Xu6e8c5f42018-06-08 11:55:09 +0800314/*
315 * Return the clock to use for recording an event's time.
316 * Beware: result is invalid before configure_accelerator().
317 */
318static inline QEMUClockType monitor_get_event_clock(void)
319{
320 /*
321 * This allows us to perform tests on the monitor queues to verify
322 * that the rate limits are enforced.
323 */
324 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
325}
326
Peter Xue3e977d2018-03-09 16:59:56 +0800327/**
Markus Armbruster489653b2015-03-06 20:01:05 +0100328 * Is the current monitor, if any, a QMP monitor?
329 */
330bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100331{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100332 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100333}
334
Anthony Liguori7060b472011-09-02 12:34:50 -0500335void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000336{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200337 if (!mon->rs)
338 return;
339
aliguori731b0362009-03-05 23:01:42 +0000340 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
341 if (show_prompt)
342 readline_show_prompt(mon->rs);
343}
bellard6a00d602005-11-21 23:25:50 +0000344
Anthony Liguori7060b472011-09-02 12:34:50 -0500345int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
346 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000347{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100348 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000349 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
350 /* prompt is printed on return from the command handler */
351 return 0;
352 } else {
353 monitor_printf(mon, "terminal does not support password prompting\n");
354 return -ENOTTY;
355 }
aliguoribb5fc202009-03-05 23:01:15 +0000356}
357
Peter Xu6d2d5632018-03-26 14:38:55 +0800358static void qmp_request_free(QMPRequest *req)
359{
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200360 qobject_unref(req->id);
361 qobject_unref(req->req);
Peter Xu6d2d5632018-03-26 14:38:55 +0800362 g_free(req);
363}
364
365/* Must with the mon->qmp.qmp_queue_lock held */
366static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon)
367{
368 while (!g_queue_is_empty(mon->qmp.qmp_requests)) {
369 qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests));
370 }
371}
372
373/* Must with the mon->qmp.qmp_queue_lock held */
374static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon)
375{
376 while (!g_queue_is_empty(mon->qmp.qmp_responses)) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200377 qobject_unref((QObject *)g_queue_pop_head(mon->qmp.qmp_responses));
Peter Xu6d2d5632018-03-26 14:38:55 +0800378 }
379}
380
381static void monitor_qmp_cleanup_queues(Monitor *mon)
382{
383 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
384 monitor_qmp_cleanup_req_queue_locked(mon);
385 monitor_qmp_cleanup_resp_queue_locked(mon);
386 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
387}
388
389
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200390static void monitor_flush_locked(Monitor *mon);
391
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100392static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
393 void *opaque)
394{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200395 Monitor *mon = opaque;
396
Peter Xudc7cbcd2018-06-08 11:55:05 +0800397 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200398 mon->out_watch = 0;
399 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800400 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100401 return FALSE;
402}
403
Peter Xudc7cbcd2018-06-08 11:55:05 +0800404/* Called with mon->mon_lock held. */
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200405static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000406{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100407 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400408 size_t len;
409 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100410
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400411 if (mon->skip_flush) {
412 return;
413 }
414
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400415 buf = qstring_get_str(mon->outbuf);
416 len = qstring_get_length(mon->outbuf);
417
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200418 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300419 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200420 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
421 /* all flushed or error */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200422 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400423 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100424 return;
425 }
426 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600427 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400428 QString *tmp = qstring_from_str(buf + rc);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200429 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400430 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100431 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200432 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300433 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300434 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300435 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200436 }
bellard7e2515e2004-08-01 21:52:19 +0000437 }
438}
439
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200440void monitor_flush(Monitor *mon)
441{
Peter Xudc7cbcd2018-06-08 11:55:05 +0800442 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200443 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800444 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200445}
446
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400447/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000448static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000449{
ths60fe76f2007-12-16 03:02:09 +0000450 char c;
aliguori731b0362009-03-05 23:01:42 +0000451
Peter Xudc7cbcd2018-06-08 11:55:05 +0800452 qemu_mutex_lock(&mon->mon_lock);
bellard7e2515e2004-08-01 21:52:19 +0000453 for(;;) {
454 c = *str++;
455 if (c == '\0')
456 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400457 if (c == '\n') {
458 qstring_append_chr(mon->outbuf, '\r');
459 }
460 qstring_append_chr(mon->outbuf, c);
461 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200462 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400463 }
bellard7e2515e2004-08-01 21:52:19 +0000464 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800465 qemu_mutex_unlock(&mon->mon_lock);
bellard7e2515e2004-08-01 21:52:19 +0000466}
467
aliguori376253e2009-03-05 23:01:23 +0000468void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000469{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400470 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200471
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200472 if (!mon)
473 return;
474
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100475 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200476 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200477 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200478
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400479 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200480 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400481 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000482}
483
aliguori376253e2009-03-05 23:01:23 +0000484void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000485{
486 va_list ap;
487 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000488 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000489 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000490}
491
Pavel Butsykincaf15312015-09-22 16:18:17 +0300492int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000493{
494 va_list ap;
495 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000496 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000497 va_end(ap);
498 return 0;
499}
500
Peter Xuabe3cd02018-03-09 17:00:02 +0800501static void monitor_json_emitter_raw(Monitor *mon,
502 QObject *data)
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200503{
504 QString *json;
505
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200506 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
507 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200508 assert(json != NULL);
509
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200510 qstring_append_chr(json, '\n');
511 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200512
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200513 qobject_unref(json);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200514}
515
Peter Xuabe3cd02018-03-09 17:00:02 +0800516static void monitor_json_emitter(Monitor *mon, QObject *data)
517{
518 if (mon->use_io_thr) {
519 /*
520 * If using IO thread, we need to queue the item so that IO
521 * thread will do the rest for us. Take refcount so that
522 * caller won't free the data (which will be finally freed in
523 * responder thread).
524 */
Peter Xuabe3cd02018-03-09 17:00:02 +0800525 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200526 g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(data));
Peter Xuabe3cd02018-03-09 17:00:02 +0800527 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
528 qemu_bh_schedule(mon_global.qmp_respond_bh);
529 } else {
530 /*
531 * If not using monitor IO thread, then we are in main thread.
532 * Do the emission right away.
533 */
534 monitor_json_emitter_raw(mon, data);
535 }
536}
537
538struct QMPResponse {
539 Monitor *mon;
540 QObject *data;
541};
542typedef struct QMPResponse QMPResponse;
543
544/*
Peter Xu40687eb2018-06-20 15:32:18 +0800545 * Pop a QMPResponse from any monitor's response queue into @response.
546 * Return false if all the queues are empty; else true.
Peter Xuabe3cd02018-03-09 17:00:02 +0800547 */
Peter Xu40687eb2018-06-20 15:32:18 +0800548static bool monitor_qmp_response_pop_any(QMPResponse *response)
Peter Xuabe3cd02018-03-09 17:00:02 +0800549{
550 Monitor *mon;
551 QObject *data = NULL;
552
553 qemu_mutex_lock(&monitor_lock);
554 QTAILQ_FOREACH(mon, &mon_list, entry) {
555 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
556 data = g_queue_pop_head(mon->qmp.qmp_responses);
557 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
558 if (data) {
Peter Xu40687eb2018-06-20 15:32:18 +0800559 response->mon = mon;
560 response->data = data;
Peter Xuabe3cd02018-03-09 17:00:02 +0800561 break;
562 }
563 }
564 qemu_mutex_unlock(&monitor_lock);
Peter Xu40687eb2018-06-20 15:32:18 +0800565 return data != NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +0800566}
567
568static void monitor_qmp_bh_responder(void *opaque)
569{
570 QMPResponse response;
571
Peter Xu40687eb2018-06-20 15:32:18 +0800572 while (monitor_qmp_response_pop_any(&response)) {
Peter Xuabe3cd02018-03-09 17:00:02 +0800573 monitor_json_emitter_raw(response.mon, response.data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200574 qobject_unref(response.data);
Peter Xuabe3cd02018-03-09 17:00:02 +0800575 }
576}
577
Eric Blake7fb1cf12015-11-18 01:52:57 -0700578static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200579 /* Limit guest-triggerable events to 1 per second */
580 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
581 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
582 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
583 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
584 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
585 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
586};
587
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100588/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200589 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200590 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100591 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200592static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100593{
594 Monitor *mon;
595
Markus Armbruster688b4b72015-10-15 17:08:30 +0200596 trace_monitor_protocol_event_emit(event, qdict);
Peter Xu238d9f32018-03-09 16:59:51 +0800597 QTAILQ_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100598 if (monitor_is_qmp(mon)
599 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200600 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100601 }
602 }
603}
604
Markus Armbrustera24712a2015-10-15 17:08:34 +0200605static void monitor_qapi_event_handler(void *opaque);
606
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100607/*
608 * Queue a new event for emission to Monitor instances,
609 * applying any rate limiting if required.
610 */
611static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200612monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100613{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200614 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200615 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200616
Eric Blake7fb1cf12015-11-18 01:52:57 -0700617 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200618 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200619 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100620
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200621 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200622
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200623 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200624 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200625 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100626 } else {
Max Reitz7dc847e2018-02-24 16:40:29 +0100627 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
Markus Armbruster7de0be62015-10-15 17:08:35 +0200628 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200629
630 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
631 assert(!evstate || timer_pending(evstate->timer));
632
633 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200634 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200635 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200636 * last send. Store event for sending when timer fires,
637 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100638 */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200639 qobject_unref(evstate->qdict);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200640 evstate->qdict = qobject_ref(qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100641 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200642 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200643 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200644 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200645 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200646 * events arriving before then will be delayed until then.
647 */
Peter Xu6e8c5f42018-06-08 11:55:09 +0800648 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200649
Markus Armbruster688b4b72015-10-15 17:08:30 +0200650 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200651
652 evstate = g_new(MonitorQAPIEventState, 1);
653 evstate->event = event;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200654 evstate->data = qobject_ref(data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200655 evstate->qdict = NULL;
Peter Xu6e8c5f42018-06-08 11:55:09 +0800656 evstate->timer = timer_new_ns(monitor_get_event_clock(),
Markus Armbrustera24712a2015-10-15 17:08:34 +0200657 monitor_qapi_event_handler,
658 evstate);
659 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200660 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100661 }
662 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200663
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200664 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100665}
666
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100667/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200668 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200669 * event.
670 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100671 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200672static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100673{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200674 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200675 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100676
Markus Armbruster93f8f982015-10-15 17:08:31 +0200677 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200678 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200679
Markus Armbruster688b4b72015-10-15 17:08:30 +0200680 if (evstate->qdict) {
Peter Xu6e8c5f42018-06-08 11:55:09 +0800681 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200682
Markus Armbruster688b4b72015-10-15 17:08:30 +0200683 monitor_qapi_event_emit(evstate->event, evstate->qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200684 qobject_unref(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200685 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200686 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200687 } else {
688 g_hash_table_remove(monitor_qapi_event_state, evstate);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200689 qobject_unref(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200690 timer_free(evstate->timer);
691 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100692 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200693
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200694 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100695}
696
Markus Armbrustera24712a2015-10-15 17:08:34 +0200697static unsigned int qapi_event_throttle_hash(const void *key)
698{
699 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200700 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200701
Markus Armbruster7de0be62015-10-15 17:08:35 +0200702 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
703 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
704 }
705
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200706 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
707 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
708 }
709
Markus Armbruster7de0be62015-10-15 17:08:35 +0200710 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200711}
712
713static gboolean qapi_event_throttle_equal(const void *a, const void *b)
714{
715 const MonitorQAPIEventState *eva = a;
716 const MonitorQAPIEventState *evb = b;
717
Markus Armbruster7de0be62015-10-15 17:08:35 +0200718 if (eva->event != evb->event) {
719 return FALSE;
720 }
721
722 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
723 return !strcmp(qdict_get_str(eva->data, "id"),
724 qdict_get_str(evb->data, "id"));
725 }
726
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200727 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
728 return !strcmp(qdict_get_str(eva->data, "node-name"),
729 qdict_get_str(evb->data, "node-name"));
730 }
731
Markus Armbruster7de0be62015-10-15 17:08:35 +0200732 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200733}
734
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200735static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100736{
Markus Armbrustera24712a2015-10-15 17:08:34 +0200737 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
738 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200739 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200740}
741
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100742static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200743
Peter Xua5ed3522018-03-09 16:59:52 +0800744static void monitor_data_init(Monitor *mon, bool skip_flush,
745 bool use_io_thr)
Wenchao Xiab01fe892013-08-27 20:38:19 +0800746{
747 memset(mon, 0, sizeof(Monitor));
Peter Xudc7cbcd2018-06-08 11:55:05 +0800748 qemu_mutex_init(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800749 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800750 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800751 /* Use *mon_cmds by default. */
752 mon->cmd_table = mon_cmds;
Peter Xu88a95d12018-03-09 16:59:48 +0800753 mon->skip_flush = skip_flush;
Peter Xua5ed3522018-03-09 16:59:52 +0800754 mon->use_io_thr = use_io_thr;
Peter Xu71da4662018-03-09 16:59:57 +0800755 mon->qmp.qmp_requests = g_queue_new();
Peter Xuabe3cd02018-03-09 17:00:02 +0800756 mon->qmp.qmp_responses = g_queue_new();
Wenchao Xiab01fe892013-08-27 20:38:19 +0800757}
758
759static void monitor_data_destroy(Monitor *mon)
760{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200761 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400762 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400763 if (monitor_is_qmp(mon)) {
764 json_message_parser_destroy(&mon->qmp.parser);
765 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100766 readline_free(mon->rs);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200767 qobject_unref(mon->outbuf);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800768 qemu_mutex_destroy(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800769 qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
Peter Xu6d2d5632018-03-26 14:38:55 +0800770 monitor_qmp_cleanup_req_queue_locked(mon);
771 monitor_qmp_cleanup_resp_queue_locked(mon);
Peter Xu71da4662018-03-09 16:59:57 +0800772 g_queue_free(mon->qmp.qmp_requests);
Peter Xuabe3cd02018-03-09 17:00:02 +0800773 g_queue_free(mon->qmp.qmp_responses);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800774}
775
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200776char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
777 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200778{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200779 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200780 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200781
Peter Xua5ed3522018-03-09 16:59:52 +0800782 monitor_data_init(&hmp, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200783
784 old_mon = cur_mon;
785 cur_mon = &hmp;
786
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200787 if (has_cpu_index) {
788 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200789 if (ret < 0) {
790 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100791 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
792 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200793 goto out;
794 }
795 }
796
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100797 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200798 cur_mon = old_mon;
799
Peter Xudc7cbcd2018-06-08 11:55:05 +0800800 qemu_mutex_lock(&hmp.mon_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400801 if (qstring_get_length(hmp.outbuf) > 0) {
802 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200803 } else {
804 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200805 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800806 qemu_mutex_unlock(&hmp.mon_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200807
808out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800809 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200810 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200811}
812
bellard9dc39cb2004-03-14 21:38:27 +0000813static int compare_cmd(const char *name, const char *list)
814{
815 const char *p, *pstart;
816 int len;
817 len = strlen(name);
818 p = list;
819 for(;;) {
820 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200821 p = qemu_strchrnul(p, '|');
bellard9dc39cb2004-03-14 21:38:27 +0000822 if ((p - pstart) == len && !memcmp(pstart, name, len))
823 return 1;
824 if (*p == '\0')
825 break;
826 p++;
827 }
828 return 0;
829}
830
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800831static int get_str(char *buf, int buf_size, const char **pp)
832{
833 const char *p;
834 char *q;
835 int c;
836
837 q = buf;
838 p = *pp;
839 while (qemu_isspace(*p)) {
840 p++;
841 }
842 if (*p == '\0') {
843 fail:
844 *q = '\0';
845 *pp = p;
846 return -1;
847 }
848 if (*p == '\"') {
849 p++;
850 while (*p != '\0' && *p != '\"') {
851 if (*p == '\\') {
852 p++;
853 c = *p++;
854 switch (c) {
855 case 'n':
856 c = '\n';
857 break;
858 case 'r':
859 c = '\r';
860 break;
861 case '\\':
862 case '\'':
863 case '\"':
864 break;
865 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100866 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800867 goto fail;
868 }
869 if ((q - buf) < buf_size - 1) {
870 *q++ = c;
871 }
872 } else {
873 if ((q - buf) < buf_size - 1) {
874 *q++ = *p;
875 }
876 p++;
877 }
878 }
879 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100880 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800881 goto fail;
882 }
883 p++;
884 } else {
885 while (*p != '\0' && !qemu_isspace(*p)) {
886 if ((q - buf) < buf_size - 1) {
887 *q++ = *p;
888 }
889 p++;
890 }
891 }
892 *q = '\0';
893 *pp = p;
894 return 0;
895}
896
897#define MAX_ARGS 16
898
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800899static void free_cmdline_args(char **args, int nb_args)
900{
901 int i;
902
903 assert(nb_args <= MAX_ARGS);
904
905 for (i = 0; i < nb_args; i++) {
906 g_free(args[i]);
907 }
908
909}
910
911/*
912 * Parse the command line to get valid args.
913 * @cmdline: command line to be parsed.
914 * @pnb_args: location to store the number of args, must NOT be NULL.
915 * @args: location to store the args, which should be freed by caller, must
916 * NOT be NULL.
917 *
918 * Returns 0 on success, negative on failure.
919 *
920 * NOTE: this parser is an approximate form of the real command parser. Number
921 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
922 * return with failure.
923 */
924static int parse_cmdline(const char *cmdline,
925 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800926{
927 const char *p;
928 int nb_args, ret;
929 char buf[1024];
930
931 p = cmdline;
932 nb_args = 0;
933 for (;;) {
934 while (qemu_isspace(*p)) {
935 p++;
936 }
937 if (*p == '\0') {
938 break;
939 }
940 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800941 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800942 }
943 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800944 if (ret < 0) {
945 goto fail;
946 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800947 args[nb_args] = g_strdup(buf);
948 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800949 }
950 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800951 return 0;
952
953 fail:
954 free_cmdline_args(args, nb_args);
955 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800956}
957
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100958/*
959 * Returns true if the command can be executed in preconfig mode
960 * i.e. it has the 'p' flag.
961 */
962static bool cmd_can_preconfig(const mon_cmd_t *cmd)
963{
964 if (!cmd->flags) {
965 return false;
966 }
967
968 return strchr(cmd->flags, 'p');
969}
970
Wenchao Xia66855492013-08-27 20:38:23 +0800971static void help_cmd_dump_one(Monitor *mon,
972 const mon_cmd_t *cmd,
973 char **prefix_args,
974 int prefix_args_nb)
975{
976 int i;
977
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +0100978 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
979 return;
980 }
981
Wenchao Xia66855492013-08-27 20:38:23 +0800982 for (i = 0; i < prefix_args_nb; i++) {
983 monitor_printf(mon, "%s ", prefix_args[i]);
984 }
985 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
986}
987
988/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500989static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800990 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000991{
Anthony Liguoric227f092009-10-01 16:12:16 -0500992 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000993
Wenchao Xia66855492013-08-27 20:38:23 +0800994 /* No valid arg need to compare with, dump all in *cmds */
995 if (arg_index >= nb_args) {
996 for (cmd = cmds; cmd->name != NULL; cmd++) {
997 help_cmd_dump_one(mon, cmd, args, arg_index);
998 }
999 return;
1000 }
1001
1002 /* Find one entry to dump */
1003 for (cmd = cmds; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +01001004 if (compare_cmd(args[arg_index], cmd->name) &&
1005 ((!runstate_check(RUN_STATE_PRECONFIG) ||
1006 cmd_can_preconfig(cmd)))) {
Wenchao Xia66855492013-08-27 20:38:23 +08001007 if (cmd->sub_table) {
1008 /* continue with next arg */
1009 help_cmd_dump(mon, cmd->sub_table,
1010 args, nb_args, arg_index + 1);
1011 } else {
1012 help_cmd_dump_one(mon, cmd, args, arg_index);
1013 }
1014 break;
1015 }
bellard9dc39cb2004-03-14 21:38:27 +00001016 }
1017}
1018
aliguori376253e2009-03-05 23:01:23 +00001019static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +00001020{
Wenchao Xia66855492013-08-27 20:38:23 +08001021 char *args[MAX_ARGS];
1022 int nb_args = 0;
1023
1024 /* 1. parse user input */
1025 if (name) {
1026 /* special case for log, directly dump and return */
1027 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +00001028 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +00001029 monitor_printf(mon, "Log items (comma separated):\n");
1030 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +00001031 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +00001032 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +00001033 }
Wenchao Xia66855492013-08-27 20:38:23 +08001034 return;
1035 }
1036
1037 if (parse_cmdline(name, &nb_args, args) < 0) {
1038 return;
bellardf193c792004-03-21 17:06:25 +00001039 }
bellard9dc39cb2004-03-14 21:38:27 +00001040 }
Wenchao Xia66855492013-08-27 20:38:23 +08001041
1042 /* 2. dump the contents according to parsed args */
1043 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
1044
1045 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +00001046}
1047
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001048static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -03001049{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001050 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -03001051}
1052
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001053static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301054{
1055 const char *tp_name = qdict_get_str(qdict, "name");
1056 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001057 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1058 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001059 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +00001060
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001061 if (vcpu < 0) {
1062 monitor_printf(mon, "argument vcpu must be positive");
1063 return;
1064 }
1065
1066 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001067 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +01001068 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +00001069 }
Prerna Saxena22890ab2010-06-24 17:04:53 +05301070}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001071
Michael Rothc45a8162011-10-02 08:44:37 -05001072#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001073static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001074{
1075 const char *op = qdict_get_try_str(qdict, "op");
1076 const char *arg = qdict_get_try_str(qdict, "arg");
1077
1078 if (!op) {
1079 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
1080 } else if (!strcmp(op, "on")) {
1081 st_set_trace_file_enabled(true);
1082 } else if (!strcmp(op, "off")) {
1083 st_set_trace_file_enabled(false);
1084 } else if (!strcmp(op, "flush")) {
1085 st_flush_trace_buffer();
1086 } else if (!strcmp(op, "set")) {
1087 if (arg) {
1088 st_set_trace_file(arg);
1089 }
1090 } else {
1091 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1092 help_cmd(mon, "trace-file");
1093 }
1094}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301095#endif
1096
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001097static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001098{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001099 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001100}
1101
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001102static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +00001103{
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001104 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001105
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001106 if (!cmd->enabled) {
1107 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001108 }
1109
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001110 info = g_malloc0(sizeof(*info));
1111 info->value = g_malloc0(sizeof(*info->value));
1112 info->value->name = g_strdup(cmd->name);
1113 info->next = *list;
1114 *list = info;
1115}
1116
1117CommandInfoList *qmp_query_commands(Error **errp)
1118{
1119 CommandInfoList *list = NULL;
1120
Markus Armbruster635db182017-03-03 13:32:27 +01001121 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001122
1123 return list;
thsa36e69d2007-12-02 05:18:19 +00001124}
1125
Daniel P. Berrange48608532012-05-21 17:59:51 +01001126EventInfoList *qmp_query_events(Error **errp)
1127{
1128 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +02001129 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +01001130
Eric Blake7fb1cf12015-11-18 01:52:57 -07001131 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001132 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +01001133 assert(event_name != NULL);
1134 info = g_malloc0(sizeof(*info));
1135 info->value = g_malloc0(sizeof(*info->value));
1136 info->value->name = g_strdup(event_name);
1137
1138 info->next = ev_list;
1139 ev_list = info;
1140 }
1141
1142 return ev_list;
1143}
1144
Markus Armbruster39a18152015-09-16 13:06:28 +02001145/*
1146 * Minor hack: generated marshalling suppressed for this command
1147 * ('gen': false in the schema) so we can parse the JSON string
1148 * directly into QObject instead of first parsing it with
1149 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1150 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01001151 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -06001152 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +02001153 */
1154static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1155 Error **errp)
1156{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001157 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
Markus Armbruster39a18152015-09-16 13:06:28 +02001158}
1159
Marc-André Lureau5032a162016-09-12 13:19:02 +04001160/*
Marc-André Lureau5032a162016-09-12 13:19:02 +04001161 * We used to define commands in qmp-commands.hx in addition to the
1162 * QAPI schema. This permitted defining some of them only in certain
1163 * configurations. query-commands has always reflected that (good,
1164 * because it lets QMP clients figure out what's actually available),
1165 * while query-qmp-schema never did (not so good). This function is a
1166 * hack to keep the configuration-specific commands defined exactly as
1167 * before, even though qmp-commands.hx is gone.
1168 *
1169 * FIXME Educate the QAPI schema on configuration-specific commands,
1170 * and drop this hack.
1171 */
1172static void qmp_unregister_commands_hack(void)
1173{
1174#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +01001175 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001176#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +02001177#ifndef CONFIG_REPLICATION
1178 qmp_unregister_command(&qmp_commands, "xen-set-replication");
1179 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
1180 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
1181#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001182#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +01001183 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Brijesh Singh08a161f2018-03-08 06:48:42 -06001184 qmp_unregister_command(&qmp_commands, "query-sev");
Brijesh Singh1b6a0342018-03-08 06:48:56 -06001185 qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
Brijesh Singh31dd67f2018-03-08 06:48:59 -06001186 qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001187#endif
1188#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +01001189 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001190#endif
1191#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +01001192 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001193#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001194#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001195 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001196#endif
1197#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001198 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
1199 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001200#endif
1201#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1202 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001203 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001204#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001205}
1206
Peter Xu6adf08d2018-03-09 16:59:50 +08001207static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001208{
Markus Armbruster635db182017-03-03 13:32:27 +01001209 /*
1210 * Two command lists:
1211 * - qmp_commands contains all QMP commands
1212 * - qmp_cap_negotiation_commands contains just
1213 * "qmp_capabilities", to enforce capability negotiation
1214 */
1215
Markus Armbruster1527bad2017-03-03 13:32:25 +01001216 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001217
Markus Armbruster1527bad2017-03-03 13:32:25 +01001218 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001219 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001220 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001221 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001222 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001223 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001224
Markus Armbruster05875682017-03-03 13:32:24 +01001225 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001226
1227 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1228 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001229 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +01001230}
1231
Peter Xu71da4662018-03-09 16:59:57 +08001232static bool qmp_cap_enabled(Monitor *mon, QMPCapability cap)
1233{
1234 return mon->qmp.qmp_caps[cap];
1235}
1236
1237static bool qmp_oob_enabled(Monitor *mon)
1238{
1239 return qmp_cap_enabled(mon, QMP_CAPABILITY_OOB);
1240}
1241
Peter Xu02130312018-03-09 16:59:53 +08001242static void qmp_caps_check(Monitor *mon, QMPCapabilityList *list,
1243 Error **errp)
Markus Armbruster635db182017-03-03 13:32:27 +01001244{
Peter Xu02130312018-03-09 16:59:53 +08001245 for (; list; list = list->next) {
1246 assert(list->value < QMP_CAPABILITY__MAX);
1247 switch (list->value) {
1248 case QMP_CAPABILITY_OOB:
1249 if (!mon->use_io_thr) {
1250 /*
1251 * Out-Of-Band only works with monitors that are
1252 * running on dedicated IOThread.
1253 */
1254 error_setg(errp, "This monitor does not support "
1255 "Out-Of-Band (OOB)");
1256 return;
1257 }
1258 break;
1259 default:
1260 break;
1261 }
1262 }
1263}
1264
1265/* This function should only be called after capabilities are checked. */
1266static void qmp_caps_apply(Monitor *mon, QMPCapabilityList *list)
1267{
1268 for (; list; list = list->next) {
1269 mon->qmp.qmp_caps[list->value] = true;
1270 }
1271}
1272
Peter Xucf869d52018-03-10 20:38:05 -06001273/*
1274 * Return true if check successful, or false otherwise. When false is
1275 * returned, detailed error will be in errp if provided.
1276 */
1277static bool qmp_cmd_oob_check(Monitor *mon, QDict *req, Error **errp)
1278{
1279 const char *command;
1280 QmpCommand *cmd;
1281
1282 command = qdict_get_try_str(req, "execute");
1283 if (!command) {
1284 error_setg(errp, "Command field 'execute' missing");
1285 return false;
1286 }
1287
1288 cmd = qmp_find_command(mon->qmp.commands, command);
1289 if (!cmd) {
Peter Xu9ddb7452018-03-26 14:38:54 +08001290 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
1291 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1292 "Expecting capabilities negotiation "
1293 "with 'qmp_capabilities'");
1294 } else {
1295 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1296 "The command %s has not been found", command);
1297 }
Peter Xucf869d52018-03-10 20:38:05 -06001298 return false;
1299 }
1300
1301 if (qmp_is_oob(req)) {
1302 if (!qmp_oob_enabled(mon)) {
1303 error_setg(errp, "Please enable Out-Of-Band first "
1304 "for the session during capabilities negotiation");
1305 return false;
1306 }
1307 if (!(cmd->options & QCO_ALLOW_OOB)) {
1308 error_setg(errp, "The command %s does not support OOB",
1309 command);
1310 return false;
1311 }
1312 }
1313
1314 return true;
1315}
1316
Peter Xu02130312018-03-09 16:59:53 +08001317void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
1318 Error **errp)
1319{
1320 Error *local_err = NULL;
1321
Markus Armbruster635db182017-03-03 13:32:27 +01001322 if (cur_mon->qmp.commands == &qmp_commands) {
1323 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1324 "Capabilities negotiation is already complete, command "
1325 "ignored");
1326 return;
1327 }
1328
Peter Xu02130312018-03-09 16:59:53 +08001329 /* Enable QMP capabilities provided by the client if applicable. */
1330 if (has_enable) {
1331 qmp_caps_check(cur_mon, enable, &local_err);
1332 if (local_err) {
1333 /*
1334 * Failed check on any of the capabilities will fail the
1335 * entire command (and thus not apply any of the other
1336 * capabilities that were also requested).
1337 */
1338 error_propagate(errp, local_err);
1339 return;
1340 }
1341 qmp_caps_apply(cur_mon, enable);
1342 }
1343
Markus Armbruster635db182017-03-03 13:32:27 +01001344 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001345}
1346
Peter Xud9f25282018-06-08 11:55:07 +08001347/* Set the current CPU defined by the user. Callers must hold BQL. */
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001348int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001349{
Andreas Färber55e5c282012-12-17 06:18:02 +01001350 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001351
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001352 cpu = qemu_get_cpu(cpu_index);
1353 if (cpu == NULL) {
1354 return -1;
bellard6a00d602005-11-21 23:25:50 +00001355 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001356 g_free(cur_mon->mon_cpu_path);
1357 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001358 return 0;
bellard6a00d602005-11-21 23:25:50 +00001359}
1360
Peter Xud9f25282018-06-08 11:55:07 +08001361/* Callers must hold BQL. */
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001362static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001363{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001364 CPUState *cpu;
1365
1366 if (cur_mon->mon_cpu_path) {
1367 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1368 TYPE_CPU, NULL);
1369 if (!cpu) {
1370 g_free(cur_mon->mon_cpu_path);
1371 cur_mon->mon_cpu_path = NULL;
1372 }
1373 }
1374 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001375 if (!first_cpu) {
1376 return NULL;
1377 }
David Gibson27a83f82016-09-21 15:29:26 +10001378 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001379 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001380 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001381 if (synchronize) {
1382 cpu_synchronize_state(cpu);
1383 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001384 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001385}
1386
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001387CPUState *mon_get_cpu(void)
1388{
1389 return mon_get_cpu_sync(true);
1390}
1391
Pavel Butsykinbf957282015-09-10 18:38:59 +03001392CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001393{
Thomas Huth854e67f2017-01-13 13:12:35 +01001394 CPUState *cs = mon_get_cpu();
1395
1396 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001397}
1398
Luiz Capitulino99b77962011-10-24 10:53:44 -02001399int monitor_get_cpu_index(void)
1400{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001401 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001402
1403 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001404}
1405
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001406static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001407{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001408 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1409 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001410
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001411 if (all_cpus) {
1412 CPU_FOREACH(cs) {
1413 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1414 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1415 }
1416 } else {
1417 cs = mon_get_cpu();
1418
1419 if (!cs) {
1420 monitor_printf(mon, "No CPU available\n");
1421 return;
1422 }
1423
1424 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001425 }
bellard9307c4c2004-04-04 12:57:25 +00001426}
1427
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001428#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001429static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001430{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001431 if (!tcg_enabled()) {
1432 error_report("JIT information is only available with accel=tcg");
1433 return;
1434 }
1435
aliguori376253e2009-03-05 23:01:23 +00001436 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001437 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001438}
1439
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001440static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001441{
1442 dump_opcount_info((FILE *)mon, monitor_fprintf);
1443}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001444#endif
Max Filippov246ae242014-11-02 11:04:18 +03001445
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001446static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001447{
1448 int i;
bellard7e2515e2004-08-01 21:52:19 +00001449 const char *str;
ths3b46e622007-09-17 08:09:54 +00001450
aliguoricde76ee2009-03-05 23:01:51 +00001451 if (!mon->rs)
1452 return;
bellard7e2515e2004-08-01 21:52:19 +00001453 i = 0;
1454 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001455 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001456 if (!str)
1457 break;
aliguori376253e2009-03-05 23:01:23 +00001458 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001459 i++;
bellardaa455482004-04-04 13:07:25 +00001460 }
1461}
1462
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001463static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001464{
Thomas Huth854e67f2017-01-13 13:12:35 +01001465 CPUState *cs = mon_get_cpu();
1466
1467 if (!cs) {
1468 monitor_printf(mon, "No CPU available\n");
1469 return;
1470 }
1471 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001472}
j_mayer76a66252007-03-07 08:32:30 +00001473
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001474static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301475{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001476 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001477 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1478 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001479 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001480 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001481 Error *local_err = NULL;
1482
1483 if (name == NULL) {
1484 name = "*";
1485 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001486 if (vcpu < 0) {
1487 monitor_printf(mon, "argument vcpu must be positive");
1488 return;
1489 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001490
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001491 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001492 if (local_err) {
1493 error_report_err(local_err);
1494 return;
1495 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001496
1497 for (elem = events; elem != NULL; elem = elem->next) {
1498 monitor_printf(mon, "%s : state %u\n",
1499 elem->value->name,
1500 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1501 }
1502 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301503}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301504
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001505void qmp_client_migrate_info(const char *protocol, const char *hostname,
1506 bool has_port, int64_t port,
1507 bool has_tls_port, int64_t tls_port,
1508 bool has_cert_subject, const char *cert_subject,
1509 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001510{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001511 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001512 if (!qemu_using_spice(errp)) {
1513 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001514 }
1515
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001516 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001517 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001518 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001519 }
1520
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001521 if (qemu_spice_migrate_info(hostname,
1522 has_port ? port : -1,
1523 has_tls_port ? tls_port : -1,
1524 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001525 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001526 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001527 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001528 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001529 }
1530
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001531 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001532}
1533
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001534static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001535{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001536 Error *err = NULL;
1537
1538 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1539 if (err) {
1540 error_report_err(err);
1541 }
pbrooke735b912007-06-30 13:53:24 +00001542}
1543
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001544static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001545{
1546 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001547 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001548
bellard9307c4c2004-04-04 12:57:25 +00001549 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001550 mask = 0;
1551 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001552 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001553 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001554 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001555 return;
1556 }
1557 }
Peter Maydell24537a02013-02-11 16:41:23 +00001558 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001559}
1560
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001561static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001562{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001563 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001564 if (!option || !strcmp(option, "on")) {
1565 singlestep = 1;
1566 } else if (!strcmp(option, "off")) {
1567 singlestep = 0;
1568 } else {
1569 monitor_printf(mon, "unexpected option %s\n", option);
1570 }
1571}
1572
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001573static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001574{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001575 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001576 if (!device)
1577 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1578 if (gdbserver_start(device) < 0) {
1579 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1580 device);
1581 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001582 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001583 } else {
aliguori59030a82009-04-05 18:43:41 +00001584 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1585 device);
bellard8a7ddc32004-03-31 19:00:16 +00001586 }
1587}
1588
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001589static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001590{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001591 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001592 if (select_watchdog_action(action) == -1) {
1593 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1594 }
1595}
1596
aliguori376253e2009-03-05 23:01:23 +00001597static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001598{
aliguori376253e2009-03-05 23:01:23 +00001599 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001600 switch(c) {
1601 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001602 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001603 break;
1604 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001605 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001606 break;
1607 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001608 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001609 break;
1610 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001611 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001612 break;
1613 default:
1614 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001615 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001616 } else {
aliguori376253e2009-03-05 23:01:23 +00001617 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001618 }
1619 break;
1620 }
aliguori376253e2009-03-05 23:01:23 +00001621 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001622}
1623
aliguori376253e2009-03-05 23:01:23 +00001624static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001625 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001626{
Blue Swirl23842aa2010-01-12 20:27:43 +00001627 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001628 uint8_t buf[16];
1629 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001630 CPUState *cs = mon_get_cpu();
1631
1632 if (!cs && (format == 'i' || !is_physical)) {
1633 monitor_printf(mon, "Can not dump without CPU\n");
1634 return;
1635 }
bellard9307c4c2004-04-04 12:57:25 +00001636
1637 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001638 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001639 return;
1640 }
1641
1642 len = wsize * count;
1643 if (wsize == 1)
1644 line_size = 8;
1645 else
1646 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001647 max_digits = 0;
1648
1649 switch(format) {
1650 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001651 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001652 break;
1653 default:
1654 case 'x':
1655 max_digits = (wsize * 8) / 4;
1656 break;
1657 case 'u':
1658 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001659 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001660 break;
1661 case 'c':
1662 wsize = 1;
1663 break;
1664 }
1665
1666 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001667 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001668 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001669 else
aliguori376253e2009-03-05 23:01:23 +00001670 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001671 l = len;
1672 if (l > line_size)
1673 l = line_size;
1674 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001675 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001676 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001677 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001678 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001679 break;
1680 }
bellard9307c4c2004-04-04 12:57:25 +00001681 }
ths5fafdf22007-09-16 21:08:06 +00001682 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001683 while (i < l) {
1684 switch(wsize) {
1685 default:
1686 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001687 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001688 break;
1689 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001690 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001691 break;
1692 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001693 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001694 break;
1695 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001696 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001697 break;
1698 }
aliguori376253e2009-03-05 23:01:23 +00001699 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001700 switch(format) {
1701 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001702 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001703 break;
1704 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001705 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001706 break;
1707 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001708 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001709 break;
1710 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001711 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001712 break;
1713 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001714 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001715 break;
1716 }
1717 i += wsize;
1718 }
aliguori376253e2009-03-05 23:01:23 +00001719 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001720 addr += l;
1721 len -= l;
1722 }
1723}
1724
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001725static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001726{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001727 int count = qdict_get_int(qdict, "count");
1728 int format = qdict_get_int(qdict, "format");
1729 int size = qdict_get_int(qdict, "size");
1730 target_long addr = qdict_get_int(qdict, "addr");
1731
aliguori376253e2009-03-05 23:01:23 +00001732 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001733}
1734
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001735static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001736{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001737 int count = qdict_get_int(qdict, "count");
1738 int format = qdict_get_int(qdict, "format");
1739 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001740 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001741
aliguori376253e2009-03-05 23:01:23 +00001742 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001743}
1744
Paolo Bonzinie9628442017-04-20 15:30:58 +02001745static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1746{
1747 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1748 addr, 1);
1749
1750 if (!mrs.mr) {
1751 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1752 return NULL;
1753 }
1754
1755 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1756 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1757 memory_region_unref(mrs.mr);
1758 return NULL;
1759 }
1760
1761 *p_mr = mrs.mr;
1762 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1763}
1764
1765static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1766{
1767 hwaddr addr = qdict_get_int(qdict, "addr");
1768 Error *local_err = NULL;
1769 MemoryRegion *mr = NULL;
1770 void *ptr;
1771
1772 ptr = gpa2hva(&mr, addr, &local_err);
1773 if (local_err) {
1774 error_report_err(local_err);
1775 return;
1776 }
1777
1778 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1779 " (%s) is %p\n",
1780 addr, mr->name, ptr);
1781
1782 memory_region_unref(mr);
1783}
1784
1785#ifdef CONFIG_LINUX
1786static uint64_t vtop(void *ptr, Error **errp)
1787{
1788 uint64_t pinfo;
1789 uint64_t ret = -1;
1790 uintptr_t addr = (uintptr_t) ptr;
1791 uintptr_t pagesize = getpagesize();
1792 off_t offset = addr / pagesize * sizeof(pinfo);
1793 int fd;
1794
1795 fd = open("/proc/self/pagemap", O_RDONLY);
1796 if (fd == -1) {
1797 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1798 return -1;
1799 }
1800
1801 /* Force copy-on-write if necessary. */
1802 atomic_add((uint8_t *)ptr, 0);
1803
1804 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1805 error_setg_errno(errp, errno, "Cannot read pagemap");
1806 goto out;
1807 }
1808 if ((pinfo & (1ull << 63)) == 0) {
1809 error_setg(errp, "Page not present");
1810 goto out;
1811 }
1812 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1813
1814out:
1815 close(fd);
1816 return ret;
1817}
1818
1819static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1820{
1821 hwaddr addr = qdict_get_int(qdict, "addr");
1822 Error *local_err = NULL;
1823 MemoryRegion *mr = NULL;
1824 void *ptr;
1825 uint64_t physaddr;
1826
1827 ptr = gpa2hva(&mr, addr, &local_err);
1828 if (local_err) {
1829 error_report_err(local_err);
1830 return;
1831 }
1832
1833 physaddr = vtop(ptr, &local_err);
1834 if (local_err) {
1835 error_report_err(local_err);
1836 } else {
1837 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1838 " (%s) is 0x%" PRIx64 "\n",
1839 addr, mr->name, (uint64_t) physaddr);
1840 }
1841
1842 memory_region_unref(mr);
1843}
1844#endif
1845
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001846static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001847{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001848 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001849 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001850
bellard9307c4c2004-04-04 12:57:25 +00001851 switch(format) {
1852 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001853 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001854 break;
1855 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001856 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001857 break;
1858 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001859 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001860 break;
1861 default:
1862 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001863 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001864 break;
1865 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001866 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001867 break;
1868 }
aliguori376253e2009-03-05 23:01:23 +00001869 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001870}
1871
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001872static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001873{
1874 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001875 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001876 uint32_t start = qdict_get_int(qdict, "start");
1877 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001878
1879 sum = 0;
1880 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001881 uint8_t val = address_space_ldub(&address_space_memory, addr,
1882 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001883 /* BSD sum algorithm ('sum' Unix command) */
1884 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001885 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001886 }
aliguori376253e2009-03-05 23:01:23 +00001887 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001888}
1889
bellard13224a82006-07-14 22:03:35 +00001890static int mouse_button_state;
1891
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001892static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001893{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001894 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001895 const char *dx_str = qdict_get_str(qdict, "dx_str");
1896 const char *dy_str = qdict_get_str(qdict, "dy_str");
1897 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001898
bellard13224a82006-07-14 22:03:35 +00001899 dx = strtol(dx_str, NULL, 0);
1900 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001901 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1902 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1903
1904 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001905 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001906 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001907 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001908 qemu_input_queue_btn(NULL, button, true);
1909 qemu_input_event_sync();
1910 qemu_input_queue_btn(NULL, button, false);
1911 }
1912 }
1913 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001914}
1915
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001916static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001917{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001918 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001919 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1920 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1921 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1922 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001923 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001924
1925 if (mouse_button_state == button_state) {
1926 return;
1927 }
1928 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1929 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001930 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001931}
1932
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001933static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001934{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001935 int size = qdict_get_int(qdict, "size");
1936 int addr = qdict_get_int(qdict, "addr");
1937 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001938 uint32_t val;
1939 int suffix;
1940
1941 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001942 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001943 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001944 addr++;
1945 }
1946 addr &= 0xffff;
1947
1948 switch(size) {
1949 default:
1950 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001951 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001952 suffix = 'b';
1953 break;
1954 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001955 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001956 suffix = 'w';
1957 break;
1958 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001959 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001960 suffix = 'l';
1961 break;
1962 }
aliguori376253e2009-03-05 23:01:23 +00001963 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1964 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001965}
bellarda3a91a32004-06-04 11:06:21 +00001966
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001967static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001968{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001969 int size = qdict_get_int(qdict, "size");
1970 int addr = qdict_get_int(qdict, "addr");
1971 int val = qdict_get_int(qdict, "val");
1972
Jan Kiszkaf1147842009-07-14 10:20:11 +02001973 addr &= IOPORTS_MASK;
1974
1975 switch (size) {
1976 default:
1977 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001978 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001979 break;
1980 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001981 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001982 break;
1983 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001984 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001985 break;
1986 }
1987}
1988
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001989static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001990{
Gongleif1839932014-12-03 18:20:58 +00001991 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001992 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001993
Gongleif1839932014-12-03 18:20:58 +00001994 qemu_boot_set(bootdevice, &local_err);
1995 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001996 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001997 } else {
Gongleif1839932014-12-03 18:20:58 +00001998 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001999 }
2000}
2001
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002002static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00002003{
Peter Xu57bb40c2017-01-16 16:40:05 +08002004 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10002005 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10002006 bool owner = qdict_get_try_bool(qdict, "owner", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08002007
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10002008 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree,
2009 owner);
Blue Swirl314e2982011-09-11 20:22:05 +00002010}
2011
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002012static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00002013{
aliguorib28b6232009-04-22 20:20:29 +00002014 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002015 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02002016 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00002017
Igor Mammedovf75cd442017-05-30 18:23:59 +02002018 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002019 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
2020
zhanghailiang5b009e42014-11-04 19:49:30 +08002021 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002022 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2023 for (i = 0; i < nb_numa_nodes; i++) {
2024 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02002025 for (cpu = cpu_list; cpu; cpu = cpu->next) {
2026 if (cpu->value->has_props && cpu->value->props->has_node_id &&
2027 cpu->value->props->node_id == i) {
2028 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00002029 }
2030 }
2031 monitor_printf(mon, "\n");
2032 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002033 node_mem[i].node_mem >> 20);
2034 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
2035 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00002036 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02002037 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08002038 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002039}
2040
bellard5f1ce942006-02-08 22:40:15 +00002041#ifdef CONFIG_PROFILER
2042
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002043int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002044int64_t dev_time;
2045
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002046static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002047{
aliguori376253e2009-03-05 23:01:23 +00002048 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302049 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00002050 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302051 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002052 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002053 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002054}
2055#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002056static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002057{
aliguori376253e2009-03-05 23:01:23 +00002058 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002059}
2060#endif
2061
bellardec36b692006-07-16 18:57:03 +00002062/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002063static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002064
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002065static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002066{
2067 int i;
2068 CaptureState *s;
2069
2070 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002071 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002072 s->ops.info (s->opaque);
2073 }
2074}
2075
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002076static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002077{
2078 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002079 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002080 CaptureState *s;
2081
2082 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2083 if (i == n) {
2084 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002085 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002086 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002087 return;
2088 }
2089 }
2090}
2091
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002092static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002093{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002094 const char *path = qdict_get_str(qdict, "path");
2095 int has_freq = qdict_haskey(qdict, "freq");
2096 int freq = qdict_get_try_int(qdict, "freq", -1);
2097 int has_bits = qdict_haskey(qdict, "bits");
2098 int bits = qdict_get_try_int(qdict, "bits", -1);
2099 int has_channels = qdict_haskey(qdict, "nchannels");
2100 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002101 CaptureState *s;
2102
Anthony Liguori7267c092011-08-20 22:09:37 -05002103 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002104
2105 freq = has_freq ? freq : 44100;
2106 bits = has_bits ? bits : 16;
2107 nchannels = has_channels ? nchannels : 2;
2108
2109 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002110 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002111 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002112 return;
bellardec36b692006-07-16 18:57:03 +00002113 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002114 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002115}
bellardec36b692006-07-16 18:57:03 +00002116
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002117static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002118{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002119 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002120
aliguori76655d62009-03-06 20:27:37 +00002121 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002122 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002123 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002124 return acl;
2125}
aliguori76655d62009-03-06 20:27:37 +00002126
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002127static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002129 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002130 qemu_acl *acl = find_acl(mon, aclname);
2131 qemu_acl_entry *entry;
2132 int i = 0;
2133
2134 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002135 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002136 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002137 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002138 i++;
2139 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002140 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002141 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002142 }
2143}
2144
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002145static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002146{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002147 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002148 qemu_acl *acl = find_acl(mon, aclname);
2149
2150 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002151 qemu_acl_reset(acl);
2152 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002153 }
2154}
aliguori76655d62009-03-06 20:27:37 +00002155
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002156static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002158 const char *aclname = qdict_get_str(qdict, "aclname");
2159 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002160 qemu_acl *acl = find_acl(mon, aclname);
2161
2162 if (acl) {
2163 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002164 acl->defaultDeny = 0;
2165 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002166 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002167 acl->defaultDeny = 1;
2168 monitor_printf(mon, "acl: policy set to 'deny'\n");
2169 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170 monitor_printf(mon, "acl: unknown policy '%s', "
2171 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002172 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002173 }
2174}
aliguori76655d62009-03-06 20:27:37 +00002175
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002176static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002177{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002178 const char *aclname = qdict_get_str(qdict, "aclname");
2179 const char *match = qdict_get_str(qdict, "match");
2180 const char *policy = qdict_get_str(qdict, "policy");
2181 int has_index = qdict_haskey(qdict, "index");
2182 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183 qemu_acl *acl = find_acl(mon, aclname);
2184 int deny, ret;
2185
2186 if (acl) {
2187 if (strcmp(policy, "allow") == 0) {
2188 deny = 0;
2189 } else if (strcmp(policy, "deny") == 0) {
2190 deny = 1;
2191 } else {
2192 monitor_printf(mon, "acl: unknown policy '%s', "
2193 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002194 return;
2195 }
aliguori28a76be2009-03-06 20:27:40 +00002196 if (has_index)
2197 ret = qemu_acl_insert(acl, deny, match, index);
2198 else
2199 ret = qemu_acl_append(acl, deny, match);
2200 if (ret < 0)
2201 monitor_printf(mon, "acl: unable to add acl entry\n");
2202 else
2203 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002204 }
2205}
aliguori76655d62009-03-06 20:27:37 +00002206
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002207static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002208{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002209 const char *aclname = qdict_get_str(qdict, "aclname");
2210 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002211 qemu_acl *acl = find_acl(mon, aclname);
2212 int ret;
aliguori76655d62009-03-06 20:27:37 +00002213
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002214 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002215 ret = qemu_acl_remove(acl, match);
2216 if (ret < 0)
2217 monitor_printf(mon, "acl: no matching acl entry\n");
2218 else
2219 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002220 }
2221}
2222
Corey Bryant208c9d12012-06-22 14:36:09 -04002223void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002224{
Anthony Liguoric227f092009-10-01 16:12:16 -05002225 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002226 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002227
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002228 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002230 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002231 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002232 }
2233
2234 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002235 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002236 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2237 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002238 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002239 }
2240
Peter Xu9409fc02018-06-08 11:55:06 +08002241 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002242 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002243 if (strcmp(monfd->name, fdname) != 0) {
2244 continue;
2245 }
2246
Peter Xu9409fc02018-06-08 11:55:06 +08002247 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002248 monfd->fd = fd;
Peter Xu9409fc02018-06-08 11:55:06 +08002249 qemu_mutex_unlock(&cur_mon->mon_lock);
2250 /* Make sure close() is out of critical section */
2251 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002252 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002253 }
2254
Anthony Liguori7267c092011-08-20 22:09:37 -05002255 monfd = g_malloc0(sizeof(mon_fd_t));
2256 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002257 monfd->fd = fd;
2258
Corey Bryant208c9d12012-06-22 14:36:09 -04002259 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002260 qemu_mutex_unlock(&cur_mon->mon_lock);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002261}
2262
Corey Bryant208c9d12012-06-22 14:36:09 -04002263void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002264{
Anthony Liguoric227f092009-10-01 16:12:16 -05002265 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002266 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002267
Peter Xu9409fc02018-06-08 11:55:06 +08002268 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002269 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002270 if (strcmp(monfd->name, fdname) != 0) {
2271 continue;
2272 }
2273
Blue Swirl72cf2d42009-09-12 07:36:22 +00002274 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002275 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05002276 g_free(monfd->name);
2277 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002278 qemu_mutex_unlock(&cur_mon->mon_lock);
2279 /* Make sure close() is out of critical section */
2280 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002281 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002282 }
2283
Peter Xu9409fc02018-06-08 11:55:06 +08002284 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002285 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002286}
2287
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002288int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002289{
Anthony Liguoric227f092009-10-01 16:12:16 -05002290 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002291
Peter Xu9409fc02018-06-08 11:55:06 +08002292 qemu_mutex_lock(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002293 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002294 int fd;
2295
2296 if (strcmp(monfd->name, fdname) != 0) {
2297 continue;
2298 }
2299
2300 fd = monfd->fd;
2301
2302 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002303 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002304 g_free(monfd->name);
2305 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002306 qemu_mutex_unlock(&mon->mon_lock);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002307
2308 return fd;
2309 }
2310
Peter Xu9409fc02018-06-08 11:55:06 +08002311 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002312 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002313 return -1;
2314}
2315
Corey Bryantba1c0482012-08-14 16:43:43 -04002316static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2317{
2318 MonFdsetFd *mon_fdset_fd;
2319 MonFdsetFd *mon_fdset_fd_next;
2320
2321 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002322 if ((mon_fdset_fd->removed ||
2323 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2324 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002325 close(mon_fdset_fd->fd);
2326 g_free(mon_fdset_fd->opaque);
2327 QLIST_REMOVE(mon_fdset_fd, next);
2328 g_free(mon_fdset_fd);
2329 }
2330 }
2331
Corey Bryantadb696f2012-08-14 16:43:47 -04002332 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002333 QLIST_REMOVE(mon_fdset, next);
2334 g_free(mon_fdset);
2335 }
2336}
2337
Corey Bryantefb87c12012-08-14 16:43:48 -04002338static void monitor_fdsets_cleanup(void)
2339{
2340 MonFdset *mon_fdset;
2341 MonFdset *mon_fdset_next;
2342
Peter Xu47451462018-06-08 11:55:11 +08002343 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002344 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2345 monitor_fdset_cleanup(mon_fdset);
2346 }
Peter Xu47451462018-06-08 11:55:11 +08002347 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002348}
2349
Corey Bryantba1c0482012-08-14 16:43:43 -04002350AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2351 const char *opaque, Error **errp)
2352{
2353 int fd;
2354 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002355 AddfdInfo *fdinfo;
2356
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002357 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002358 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002359 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002360 goto error;
2361 }
2362
Corey Bryante446f702012-10-18 15:19:32 -04002363 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2364 has_opaque, opaque, errp);
2365 if (fdinfo) {
2366 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002367 }
2368
Corey Bryantba1c0482012-08-14 16:43:43 -04002369error:
2370 if (fd != -1) {
2371 close(fd);
2372 }
2373 return NULL;
2374}
2375
2376void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2377{
2378 MonFdset *mon_fdset;
2379 MonFdsetFd *mon_fdset_fd;
2380 char fd_str[60];
2381
Peter Xu47451462018-06-08 11:55:11 +08002382 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002383 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2384 if (mon_fdset->id != fdset_id) {
2385 continue;
2386 }
2387 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2388 if (has_fd) {
2389 if (mon_fdset_fd->fd != fd) {
2390 continue;
2391 }
2392 mon_fdset_fd->removed = true;
2393 break;
2394 } else {
2395 mon_fdset_fd->removed = true;
2396 }
2397 }
2398 if (has_fd && !mon_fdset_fd) {
2399 goto error;
2400 }
2401 monitor_fdset_cleanup(mon_fdset);
Peter Xu47451462018-06-08 11:55:11 +08002402 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002403 return;
2404 }
2405
2406error:
Peter Xu47451462018-06-08 11:55:11 +08002407 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002408 if (has_fd) {
2409 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2410 fdset_id, fd);
2411 } else {
2412 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2413 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002414 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002415}
2416
2417FdsetInfoList *qmp_query_fdsets(Error **errp)
2418{
2419 MonFdset *mon_fdset;
2420 MonFdsetFd *mon_fdset_fd;
2421 FdsetInfoList *fdset_list = NULL;
2422
Peter Xu47451462018-06-08 11:55:11 +08002423 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002424 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2425 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2426 FdsetFdInfoList *fdsetfd_list = NULL;
2427
2428 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2429 fdset_info->value->fdset_id = mon_fdset->id;
2430
2431 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2432 FdsetFdInfoList *fdsetfd_info;
2433
2434 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2435 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2436 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2437 if (mon_fdset_fd->opaque) {
2438 fdsetfd_info->value->has_opaque = true;
2439 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2440 } else {
2441 fdsetfd_info->value->has_opaque = false;
2442 }
2443
2444 fdsetfd_info->next = fdsetfd_list;
2445 fdsetfd_list = fdsetfd_info;
2446 }
2447
2448 fdset_info->value->fds = fdsetfd_list;
2449
2450 fdset_info->next = fdset_list;
2451 fdset_list = fdset_info;
2452 }
Peter Xu47451462018-06-08 11:55:11 +08002453 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002454
2455 return fdset_list;
2456}
2457
Corey Bryante446f702012-10-18 15:19:32 -04002458AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2459 bool has_opaque, const char *opaque,
2460 Error **errp)
2461{
2462 MonFdset *mon_fdset = NULL;
2463 MonFdsetFd *mon_fdset_fd;
2464 AddfdInfo *fdinfo;
2465
Peter Xu47451462018-06-08 11:55:11 +08002466 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002467 if (has_fdset_id) {
2468 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2469 /* Break if match found or match impossible due to ordering by ID */
2470 if (fdset_id <= mon_fdset->id) {
2471 if (fdset_id < mon_fdset->id) {
2472 mon_fdset = NULL;
2473 }
2474 break;
2475 }
2476 }
2477 }
2478
2479 if (mon_fdset == NULL) {
2480 int64_t fdset_id_prev = -1;
2481 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2482
2483 if (has_fdset_id) {
2484 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002485 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2486 "a non-negative value");
Peter Xu47451462018-06-08 11:55:11 +08002487 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002488 return NULL;
2489 }
2490 /* Use specified fdset ID */
2491 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2492 mon_fdset_cur = mon_fdset;
2493 if (fdset_id < mon_fdset_cur->id) {
2494 break;
2495 }
2496 }
2497 } else {
2498 /* Use first available fdset ID */
2499 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2500 mon_fdset_cur = mon_fdset;
2501 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2502 fdset_id_prev = mon_fdset_cur->id;
2503 continue;
2504 }
2505 break;
2506 }
2507 }
2508
2509 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2510 if (has_fdset_id) {
2511 mon_fdset->id = fdset_id;
2512 } else {
2513 mon_fdset->id = fdset_id_prev + 1;
2514 }
2515
2516 /* The fdset list is ordered by fdset ID */
2517 if (!mon_fdset_cur) {
2518 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2519 } else if (mon_fdset->id < mon_fdset_cur->id) {
2520 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2521 } else {
2522 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2523 }
2524 }
2525
2526 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2527 mon_fdset_fd->fd = fd;
2528 mon_fdset_fd->removed = false;
2529 if (has_opaque) {
2530 mon_fdset_fd->opaque = g_strdup(opaque);
2531 }
2532 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2533
2534 fdinfo = g_malloc0(sizeof(*fdinfo));
2535 fdinfo->fdset_id = mon_fdset->id;
2536 fdinfo->fd = mon_fdset_fd->fd;
2537
Peter Xu47451462018-06-08 11:55:11 +08002538 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002539 return fdinfo;
2540}
2541
Corey Bryantadb696f2012-08-14 16:43:47 -04002542int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2543{
Peter Xu47451462018-06-08 11:55:11 +08002544#ifdef _WIN32
2545 return -ENOENT;
2546#else
Corey Bryantadb696f2012-08-14 16:43:47 -04002547 MonFdset *mon_fdset;
2548 MonFdsetFd *mon_fdset_fd;
2549 int mon_fd_flags;
Peter Xu47451462018-06-08 11:55:11 +08002550 int ret;
Corey Bryantadb696f2012-08-14 16:43:47 -04002551
Peter Xu47451462018-06-08 11:55:11 +08002552 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002553 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2554 if (mon_fdset->id != fdset_id) {
2555 continue;
2556 }
2557 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2558 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2559 if (mon_fd_flags == -1) {
Peter Xu47451462018-06-08 11:55:11 +08002560 ret = -errno;
2561 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002562 }
2563
2564 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Peter Xu47451462018-06-08 11:55:11 +08002565 ret = mon_fdset_fd->fd;
2566 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002567 }
2568 }
Peter Xu47451462018-06-08 11:55:11 +08002569 ret = -EACCES;
2570 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002571 }
Peter Xu47451462018-06-08 11:55:11 +08002572 ret = -ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04002573
Peter Xu47451462018-06-08 11:55:11 +08002574out:
2575 qemu_mutex_unlock(&mon_fdsets_lock);
2576 return ret;
2577#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04002578}
2579
2580int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2581{
2582 MonFdset *mon_fdset;
2583 MonFdsetFd *mon_fdset_fd_dup;
2584
Peter Xu47451462018-06-08 11:55:11 +08002585 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002586 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2587 if (mon_fdset->id != fdset_id) {
2588 continue;
2589 }
2590 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2591 if (mon_fdset_fd_dup->fd == dup_fd) {
Peter Xu47451462018-06-08 11:55:11 +08002592 goto err;
Corey Bryantadb696f2012-08-14 16:43:47 -04002593 }
2594 }
2595 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2596 mon_fdset_fd_dup->fd = dup_fd;
2597 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Peter Xu47451462018-06-08 11:55:11 +08002598 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002599 return 0;
2600 }
Peter Xu47451462018-06-08 11:55:11 +08002601
2602err:
2603 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002604 return -1;
2605}
2606
2607static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2608{
2609 MonFdset *mon_fdset;
2610 MonFdsetFd *mon_fdset_fd_dup;
2611
Peter Xu47451462018-06-08 11:55:11 +08002612 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002613 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2614 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2615 if (mon_fdset_fd_dup->fd == dup_fd) {
2616 if (remove) {
2617 QLIST_REMOVE(mon_fdset_fd_dup, next);
2618 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2619 monitor_fdset_cleanup(mon_fdset);
2620 }
Peter Xu47451462018-06-08 11:55:11 +08002621 goto err;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002622 } else {
Peter Xu47451462018-06-08 11:55:11 +08002623 qemu_mutex_unlock(&mon_fdsets_lock);
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002624 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002625 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002626 }
2627 }
2628 }
Peter Xu47451462018-06-08 11:55:11 +08002629
2630err:
2631 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002632 return -1;
2633}
2634
2635int monitor_fdset_dup_fd_find(int dup_fd)
2636{
2637 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2638}
2639
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002640void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002641{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002642 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002643}
2644
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002645int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002646{
2647 int fd;
2648 Error *local_err = NULL;
2649
2650 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002651 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002652 } else {
2653 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002654 if (fd == -1) {
2655 error_setg(&local_err, "Invalid file descriptor number '%s'",
2656 fdname);
2657 }
2658 }
2659 if (local_err) {
2660 error_propagate(errp, local_err);
2661 assert(fd == -1);
2662 } else {
2663 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002664 }
2665
2666 return fd;
2667}
2668
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002669/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002670static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002671#include "hmp-commands-info.h"
2672 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002673};
2674
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002675/* mon_cmds and info_cmds would be sorted at runtime */
2676static mon_cmd_t mon_cmds[] = {
2677#include "hmp-commands.h"
2678 { NULL, NULL, },
2679};
2680
bellard9307c4c2004-04-04 12:57:25 +00002681/*******************************************************************/
2682
2683static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002684static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002685
bellard9307c4c2004-04-04 12:57:25 +00002686
Stefan Weil9c3175c2013-08-22 21:30:09 +02002687static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2688expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002689{
Fam Zheng277acfe2013-08-20 10:58:21 +08002690 va_list ap;
2691 va_start(ap, fmt);
2692 monitor_vprintf(mon, fmt, ap);
2693 monitor_printf(mon, "\n");
2694 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002695 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002696}
2697
Markus Armbruster09b94182010-01-20 13:07:30 +01002698/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002699static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002700{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002701 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002702 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002703 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002704 uint64_t tmp = 0;
2705 int ret;
bellard92a31b12005-02-10 22:00:52 +00002706
Thomas Huth854e67f2017-01-13 13:12:35 +01002707 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002708 return -1;
2709 }
2710
2711 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002712 if (compare_cmd(name, md->name)) {
2713 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002714 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002715 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002716 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002717 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002718 switch(md->type) {
2719 case MD_I32:
2720 *pval = *(int32_t *)ptr;
2721 break;
2722 case MD_TLONG:
2723 *pval = *(target_long *)ptr;
2724 break;
2725 default:
2726 *pval = 0;
2727 break;
2728 }
bellard9307c4c2004-04-04 12:57:25 +00002729 }
2730 return 0;
2731 }
2732 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002733
Thomas Huth854e67f2017-01-13 13:12:35 +01002734 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002735 if (!ret) {
2736 *pval = (target_long) tmp;
2737 }
2738
2739 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002740}
2741
2742static void next(void)
2743{
Blue Swirl660f11b2009-07-31 21:16:51 +00002744 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002745 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002746 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002747 pch++;
2748 }
2749}
2750
aliguori376253e2009-03-05 23:01:23 +00002751static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002752
aliguori376253e2009-03-05 23:01:23 +00002753static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002754{
blueswir1c2efc952007-09-25 17:28:42 +00002755 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002756 char *p;
bellard6a00d602005-11-21 23:25:50 +00002757 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002758
2759 switch(*pch) {
2760 case '+':
2761 next();
aliguori376253e2009-03-05 23:01:23 +00002762 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002763 break;
2764 case '-':
2765 next();
aliguori376253e2009-03-05 23:01:23 +00002766 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002767 break;
2768 case '~':
2769 next();
aliguori376253e2009-03-05 23:01:23 +00002770 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002771 break;
2772 case '(':
2773 next();
aliguori376253e2009-03-05 23:01:23 +00002774 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002775 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002776 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002777 }
2778 next();
2779 break;
bellard81d09122004-07-14 17:21:37 +00002780 case '\'':
2781 pch++;
2782 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002783 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002784 n = *pch;
2785 pch++;
2786 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002787 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002788 next();
2789 break;
bellard9307c4c2004-04-04 12:57:25 +00002790 case '$':
2791 {
2792 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002793 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002794
bellard9307c4c2004-04-04 12:57:25 +00002795 pch++;
2796 q = buf;
2797 while ((*pch >= 'a' && *pch <= 'z') ||
2798 (*pch >= 'A' && *pch <= 'Z') ||
2799 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002800 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002801 if ((q - buf) < sizeof(buf) - 1)
2802 *q++ = *pch;
2803 pch++;
2804 }
blueswir1cd390082008-11-16 13:53:32 +00002805 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002806 pch++;
2807 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002808 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002809 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002810 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002811 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002812 }
2813 break;
2814 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002815 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002816 n = 0;
2817 break;
2818 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002819 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002820 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002821 if (errno == ERANGE) {
2822 expr_error(mon, "number too large");
2823 }
bellard9307c4c2004-04-04 12:57:25 +00002824 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002825 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002826 }
2827 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002828 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002829 pch++;
2830 break;
2831 }
2832 return n;
2833}
2834
2835
aliguori376253e2009-03-05 23:01:23 +00002836static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002837{
blueswir1c2efc952007-09-25 17:28:42 +00002838 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002839 int op;
ths3b46e622007-09-17 08:09:54 +00002840
aliguori376253e2009-03-05 23:01:23 +00002841 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002842 for(;;) {
2843 op = *pch;
2844 if (op != '*' && op != '/' && op != '%')
2845 break;
2846 next();
aliguori376253e2009-03-05 23:01:23 +00002847 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002848 switch(op) {
2849 default:
2850 case '*':
2851 val *= val2;
2852 break;
2853 case '/':
2854 case '%':
ths5fafdf22007-09-16 21:08:06 +00002855 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002856 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002857 if (op == '/')
2858 val /= val2;
2859 else
2860 val %= val2;
2861 break;
2862 }
2863 }
2864 return val;
2865}
2866
aliguori376253e2009-03-05 23:01:23 +00002867static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002868{
blueswir1c2efc952007-09-25 17:28:42 +00002869 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002870 int op;
bellard9307c4c2004-04-04 12:57:25 +00002871
aliguori376253e2009-03-05 23:01:23 +00002872 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002873 for(;;) {
2874 op = *pch;
2875 if (op != '&' && op != '|' && op != '^')
2876 break;
2877 next();
aliguori376253e2009-03-05 23:01:23 +00002878 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002879 switch(op) {
2880 default:
2881 case '&':
2882 val &= val2;
2883 break;
2884 case '|':
2885 val |= val2;
2886 break;
2887 case '^':
2888 val ^= val2;
2889 break;
2890 }
2891 }
2892 return val;
2893}
2894
aliguori376253e2009-03-05 23:01:23 +00002895static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002896{
blueswir1c2efc952007-09-25 17:28:42 +00002897 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002898 int op;
bellard9307c4c2004-04-04 12:57:25 +00002899
aliguori376253e2009-03-05 23:01:23 +00002900 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002901 for(;;) {
2902 op = *pch;
2903 if (op != '+' && op != '-')
2904 break;
2905 next();
aliguori376253e2009-03-05 23:01:23 +00002906 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002907 if (op == '+')
2908 val += val2;
2909 else
2910 val -= val2;
2911 }
2912 return val;
2913}
2914
aliguori376253e2009-03-05 23:01:23 +00002915static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002916{
2917 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002918 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002919 *pp = pch;
2920 return -1;
2921 }
blueswir1cd390082008-11-16 13:53:32 +00002922 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002923 pch++;
aliguori376253e2009-03-05 23:01:23 +00002924 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002925 *pp = pch;
2926 return 0;
2927}
2928
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002929static int get_double(Monitor *mon, double *pval, const char **pp)
2930{
2931 const char *p = *pp;
2932 char *tailp;
2933 double d;
2934
2935 d = strtod(p, &tailp);
2936 if (tailp == p) {
2937 monitor_printf(mon, "Number expected\n");
2938 return -1;
2939 }
2940 if (d != d || d - d != 0) {
2941 /* NaN or infinity */
2942 monitor_printf(mon, "Bad number\n");
2943 return -1;
2944 }
2945 *pval = d;
2946 *pp = tailp;
2947 return 0;
2948}
2949
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002950/*
2951 * Store the command-name in cmdname, and return a pointer to
2952 * the remaining of the command string.
2953 */
2954static const char *get_command_name(const char *cmdline,
2955 char *cmdname, size_t nlen)
2956{
2957 size_t len;
2958 const char *p, *pstart;
2959
2960 p = cmdline;
2961 while (qemu_isspace(*p))
2962 p++;
2963 if (*p == '\0')
2964 return NULL;
2965 pstart = p;
2966 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2967 p++;
2968 len = p - pstart;
2969 if (len > nlen - 1)
2970 len = nlen - 1;
2971 memcpy(cmdname, pstart, len);
2972 cmdname[len] = '\0';
2973 return p;
2974}
2975
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002976/**
2977 * Read key of 'type' into 'key' and return the current
2978 * 'type' pointer.
2979 */
2980static char *key_get_info(const char *type, char **key)
2981{
2982 size_t len;
2983 char *p, *str;
2984
2985 if (*type == ',')
2986 type++;
2987
2988 p = strchr(type, ':');
2989 if (!p) {
2990 *key = NULL;
2991 return NULL;
2992 }
2993 len = p - type;
2994
Anthony Liguori7267c092011-08-20 22:09:37 -05002995 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002996 memcpy(str, type, len);
2997 str[len] = '\0';
2998
2999 *key = str;
3000 return ++p;
3001}
3002
bellard9307c4c2004-04-04 12:57:25 +00003003static int default_fmt_format = 'x';
3004static int default_fmt_size = 4;
3005
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003006static int is_valid_option(const char *c, const char *typestr)
3007{
3008 char option[3];
3009
3010 option[0] = '-';
3011 option[1] = *c;
3012 option[2] = '\0';
3013
3014 typestr = strstr(typestr, option);
3015 return (typestr != NULL);
3016}
3017
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003018static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3019 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003020{
3021 const mon_cmd_t *cmd;
3022
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003023 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003024 if (compare_cmd(cmdname, cmd->name)) {
3025 return cmd;
3026 }
3027 }
3028
3029 return NULL;
3030}
3031
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003032/*
Bandan Dasae502122015-06-03 18:38:08 -04003033 * Parse command name from @cmdp according to command table @table.
3034 * If blank, return NULL.
3035 * Else, if no valid command can be found, report to @mon, and return
3036 * NULL.
3037 * Else, change @cmdp to point right behind the name, and return its
3038 * command table entry.
3039 * Do not assume the return value points into @table! It doesn't when
3040 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003041 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003042static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003043 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04003044 const char **cmdp,
3045 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003046{
Bandan Dasae502122015-06-03 18:38:08 -04003047 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003048 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003049 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003050
bellard9307c4c2004-04-04 12:57:25 +00003051 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003052 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003053 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003054 return NULL;
ths3b46e622007-09-17 08:09:54 +00003055
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003056 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003057 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003058 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003059 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003060 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003061 }
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +01003062 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
3063 monitor_printf(mon, "Command '%.*s' not available with -preconfig "
3064 "until after exit_preconfig.\n",
3065 (int)(p - cmdp_start), cmdp_start);
3066 return NULL;
3067 }
bellard9307c4c2004-04-04 12:57:25 +00003068
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003069 /* filter out following useless space */
3070 while (qemu_isspace(*p)) {
3071 p++;
3072 }
Bandan Dasae502122015-06-03 18:38:08 -04003073
3074 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003075 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003076 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003077 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003078 }
3079
Bandan Dasae502122015-06-03 18:38:08 -04003080 return cmd;
3081}
3082
3083/*
3084 * Parse arguments for @cmd.
3085 * If it can't be parsed, report to @mon, and return NULL.
3086 * Else, insert command arguments into a QDict, and return it.
3087 * Note: On success, caller has to free the QDict structure.
3088 */
3089
3090static QDict *monitor_parse_arguments(Monitor *mon,
3091 const char **endp,
3092 const mon_cmd_t *cmd)
3093{
3094 const char *typestr;
3095 char *key;
3096 int c;
3097 const char *p = *endp;
3098 char buf[1024];
3099 QDict *qdict = qdict_new();
3100
bellard9307c4c2004-04-04 12:57:25 +00003101 /* parse the parameters */
3102 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003103 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003104 typestr = key_get_info(typestr, &key);
3105 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003106 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003107 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003108 typestr++;
3109 switch(c) {
3110 case 'F':
bellard81d09122004-07-14 17:21:37 +00003111 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003112 case 's':
3113 {
3114 int ret;
ths3b46e622007-09-17 08:09:54 +00003115
blueswir1cd390082008-11-16 13:53:32 +00003116 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003117 p++;
3118 if (*typestr == '?') {
3119 typestr++;
3120 if (*p == '\0') {
3121 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003122 break;
bellard9307c4c2004-04-04 12:57:25 +00003123 }
3124 }
3125 ret = get_str(buf, sizeof(buf), &p);
3126 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003127 switch(c) {
3128 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003129 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003130 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003131 break;
3132 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003133 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003134 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003135 break;
3136 default:
Bandan Dasae502122015-06-03 18:38:08 -04003137 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003138 break;
3139 }
bellard9307c4c2004-04-04 12:57:25 +00003140 goto fail;
3141 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003142 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00003143 }
3144 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003145 case 'O':
3146 {
3147 QemuOptsList *opts_list;
3148 QemuOpts *opts;
3149
3150 opts_list = qemu_find_opts(key);
3151 if (!opts_list || opts_list->desc->name) {
3152 goto bad_type;
3153 }
3154 while (qemu_isspace(*p)) {
3155 p++;
3156 }
3157 if (!*p)
3158 break;
3159 if (get_str(buf, sizeof(buf), &p) < 0) {
3160 goto fail;
3161 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003162 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003163 if (!opts) {
3164 goto fail;
3165 }
3166 qemu_opts_to_qdict(opts, qdict);
3167 qemu_opts_del(opts);
3168 }
3169 break;
bellard9307c4c2004-04-04 12:57:25 +00003170 case '/':
3171 {
3172 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003173
blueswir1cd390082008-11-16 13:53:32 +00003174 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003175 p++;
3176 if (*p == '/') {
3177 /* format found */
3178 p++;
3179 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003180 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003181 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003182 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003183 count = count * 10 + (*p - '0');
3184 p++;
3185 }
3186 }
3187 size = -1;
3188 format = -1;
3189 for(;;) {
3190 switch(*p) {
3191 case 'o':
3192 case 'd':
3193 case 'u':
3194 case 'x':
3195 case 'i':
3196 case 'c':
3197 format = *p++;
3198 break;
3199 case 'b':
3200 size = 1;
3201 p++;
3202 break;
3203 case 'h':
3204 size = 2;
3205 p++;
3206 break;
3207 case 'w':
3208 size = 4;
3209 p++;
3210 break;
3211 case 'g':
3212 case 'L':
3213 size = 8;
3214 p++;
3215 break;
3216 default:
3217 goto next;
3218 }
3219 }
3220 next:
blueswir1cd390082008-11-16 13:53:32 +00003221 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003222 monitor_printf(mon, "invalid char in format: '%c'\n",
3223 *p);
bellard9307c4c2004-04-04 12:57:25 +00003224 goto fail;
3225 }
bellard9307c4c2004-04-04 12:57:25 +00003226 if (format < 0)
3227 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003228 if (format != 'i') {
3229 /* for 'i', not specifying a size gives -1 as size */
3230 if (size < 0)
3231 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003232 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003233 }
bellard9307c4c2004-04-04 12:57:25 +00003234 default_fmt_format = format;
3235 } else {
3236 count = 1;
3237 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003238 if (format != 'i') {
3239 size = default_fmt_size;
3240 } else {
3241 size = -1;
3242 }
bellard9307c4c2004-04-04 12:57:25 +00003243 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003244 qdict_put_int(qdict, "count", count);
3245 qdict_put_int(qdict, "format", format);
3246 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00003247 }
3248 break;
3249 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003250 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003251 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003252 {
blueswir1c2efc952007-09-25 17:28:42 +00003253 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003254
blueswir1cd390082008-11-16 13:53:32 +00003255 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003256 p++;
bellard34405572004-06-08 00:55:58 +00003257 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003258 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003259 if (*p == '\0') {
3260 typestr++;
3261 break;
3262 }
bellard34405572004-06-08 00:55:58 +00003263 } else {
3264 if (*p == '.') {
3265 p++;
blueswir1cd390082008-11-16 13:53:32 +00003266 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003267 p++;
bellard34405572004-06-08 00:55:58 +00003268 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003269 typestr++;
3270 break;
bellard34405572004-06-08 00:55:58 +00003271 }
3272 }
bellard13224a82006-07-14 22:03:35 +00003273 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003274 }
aliguori376253e2009-03-05 23:01:23 +00003275 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003276 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003277 /* Check if 'i' is greater than 32-bit */
3278 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003279 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003280 monitor_printf(mon, "integer is for 32-bit values\n");
3281 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003282 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003283 if (val < 0) {
3284 monitor_printf(mon, "enter a positive value\n");
3285 goto fail;
3286 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003287 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003288 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003289 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00003290 }
3291 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003292 case 'o':
3293 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003294 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003295 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003296 char *end;
3297
3298 while (qemu_isspace(*p)) {
3299 p++;
3300 }
3301 if (*typestr == '?') {
3302 typestr++;
3303 if (*p == '\0') {
3304 break;
3305 }
3306 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003307 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003308 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02003309 monitor_printf(mon, "invalid size\n");
3310 goto fail;
3311 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003312 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02003313 p = end;
3314 }
3315 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003316 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003317 {
3318 double val;
3319
3320 while (qemu_isspace(*p))
3321 p++;
3322 if (*typestr == '?') {
3323 typestr++;
3324 if (*p == '\0') {
3325 break;
3326 }
3327 }
3328 if (get_double(mon, &val, &p) < 0) {
3329 goto fail;
3330 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003331 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003332 switch (*p) {
3333 case 'm':
3334 val /= 1e3; p += 2; break;
3335 case 'u':
3336 val /= 1e6; p += 2; break;
3337 case 'n':
3338 val /= 1e9; p += 2; break;
3339 }
3340 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003341 if (*p && !qemu_isspace(*p)) {
3342 monitor_printf(mon, "Unknown unit suffix\n");
3343 goto fail;
3344 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04003345 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003346 }
3347 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003348 case 'b':
3349 {
3350 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003351 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003352
3353 while (qemu_isspace(*p)) {
3354 p++;
3355 }
3356 beg = p;
3357 while (qemu_isgraph(*p)) {
3358 p++;
3359 }
3360 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003361 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003362 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003363 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003364 } else {
3365 monitor_printf(mon, "Expected 'on' or 'off'\n");
3366 goto fail;
3367 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003368 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003369 }
3370 break;
bellard9307c4c2004-04-04 12:57:25 +00003371 case '-':
3372 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003373 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003374 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003375 /* option */
ths3b46e622007-09-17 08:09:54 +00003376
bellard9307c4c2004-04-04 12:57:25 +00003377 c = *typestr++;
3378 if (c == '\0')
3379 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003380 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003381 p++;
bellard9307c4c2004-04-04 12:57:25 +00003382 if (*p == '-') {
3383 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003384 if(c != *p) {
3385 if(!is_valid_option(p, typestr)) {
3386
3387 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003388 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003389 goto fail;
3390 } else {
3391 skip_key = 1;
3392 }
bellard9307c4c2004-04-04 12:57:25 +00003393 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003394 if(skip_key) {
3395 p = tmp;
3396 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003397 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003398 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003399 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003400 }
bellard9307c4c2004-04-04 12:57:25 +00003401 }
bellard9307c4c2004-04-04 12:57:25 +00003402 }
3403 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003404 case 'S':
3405 {
3406 /* package all remaining string */
3407 int len;
3408
3409 while (qemu_isspace(*p)) {
3410 p++;
3411 }
3412 if (*typestr == '?') {
3413 typestr++;
3414 if (*p == '\0') {
3415 /* no remaining string: NULL argument */
3416 break;
3417 }
3418 }
3419 len = strlen(p);
3420 if (len <= 0) {
3421 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003422 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003423 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003424 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003425 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003426 p += len;
3427 }
3428 break;
bellard9307c4c2004-04-04 12:57:25 +00003429 default:
3430 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003431 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003432 goto fail;
3433 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003434 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003435 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003436 }
3437 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003438 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003439 p++;
3440 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003441 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003442 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003443 goto fail;
3444 }
3445
Bandan Dasae502122015-06-03 18:38:08 -04003446 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003447
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003448fail:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003449 qobject_unref(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003450 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003451 return NULL;
3452}
3453
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003454static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003455{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003456 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003457 const mon_cmd_t *cmd;
Collin Walling317c52c2018-05-07 10:30:54 -04003458 const char *cmd_start = cmdline;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003459
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003460 trace_handle_hmp_command(mon, cmdline);
3461
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003462 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003463 if (!cmd) {
3464 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003465 }
3466
Bandan Dasae502122015-06-03 18:38:08 -04003467 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3468 if (!qdict) {
Collin Walling317c52c2018-05-07 10:30:54 -04003469 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
3470 cmdline--;
3471 }
3472 monitor_printf(mon, "Try \"help %.*s\" for more information\n",
3473 (int)(cmdline - cmd_start), cmd_start);
Bandan Dasae502122015-06-03 18:38:08 -04003474 return;
3475 }
3476
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003477 cmd->cmd(mon, qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003478 qobject_unref(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003479}
3480
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003481static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003482{
3483 const char *p, *pstart;
3484 char cmd[128];
3485 int len;
3486
3487 p = list;
3488 for(;;) {
3489 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +02003490 p = qemu_strchrnul(p, '|');
bellard81d09122004-07-14 17:21:37 +00003491 len = p - pstart;
3492 if (len > sizeof(cmd) - 2)
3493 len = sizeof(cmd) - 2;
3494 memcpy(cmd, pstart, len);
3495 cmd[len] = '\0';
3496 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003497 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003498 }
3499 if (*p == '\0')
3500 break;
3501 p++;
3502 }
3503}
3504
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003505static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003506{
3507 DIR *ffs;
3508 struct dirent *d;
3509 char path[1024];
3510 char file[1024], file_prefix[1024];
3511 int input_path_len;
3512 const char *p;
3513
ths5fafdf22007-09-16 21:08:06 +00003514 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003515 if (!p) {
3516 input_path_len = 0;
3517 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003518 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003519 } else {
3520 input_path_len = p - input + 1;
3521 memcpy(path, input, input_path_len);
3522 if (input_path_len > sizeof(path) - 1)
3523 input_path_len = sizeof(path) - 1;
3524 path[input_path_len] = '\0';
3525 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3526 }
Bandan Das19f2db52015-06-03 18:38:07 -04003527
bellard81d09122004-07-14 17:21:37 +00003528 ffs = opendir(path);
3529 if (!ffs)
3530 return;
3531 for(;;) {
3532 struct stat sb;
3533 d = readdir(ffs);
3534 if (!d)
3535 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003536
3537 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3538 continue;
3539 }
3540
bellard81d09122004-07-14 17:21:37 +00003541 if (strstart(d->d_name, file_prefix, NULL)) {
3542 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003543 if (input_path_len < sizeof(file))
3544 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3545 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003546 /* stat the file to find out if it's a directory.
3547 * In that case add a slash to speed up typing long paths
3548 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003549 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003550 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003551 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003552 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003553 }
3554 }
3555 closedir(ffs);
3556}
3557
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003558static const char *next_arg_type(const char *typestr)
3559{
3560 const char *p = strchr(typestr, ':');
3561 return (p != NULL ? ++p : typestr);
3562}
3563
Hani Benhabiles40d19392014-05-07 23:41:30 +01003564static void add_completion_option(ReadLineState *rs, const char *str,
3565 const char *option)
3566{
3567 if (!str || !option) {
3568 return;
3569 }
3570 if (!strncmp(option, str, strlen(str))) {
3571 readline_add_completion(rs, option);
3572 }
3573}
3574
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003575void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3576{
3577 size_t len;
3578 ChardevBackendInfoList *list, *start;
3579
3580 if (nb_args != 2) {
3581 return;
3582 }
3583 len = strlen(str);
3584 readline_set_completion_index(rs, len);
3585
3586 start = list = qmp_query_chardev_backends(NULL);
3587 while (list) {
3588 const char *chr_name = list->value->name;
3589
3590 if (!strncmp(chr_name, str, len)) {
3591 readline_add_completion(rs, chr_name);
3592 }
3593 list = list->next;
3594 }
3595 qapi_free_ChardevBackendInfoList(start);
3596}
3597
Hani Benhabilesb162b492014-05-07 23:41:31 +01003598void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3599{
3600 size_t len;
3601 int i;
3602
3603 if (nb_args != 2) {
3604 return;
3605 }
3606 len = strlen(str);
3607 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003608 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003609 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003610 }
3611}
3612
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003613void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003614{
3615 GSList *list, *elt;
3616 size_t len;
3617
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003618 if (nb_args != 2) {
3619 return;
3620 }
3621
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003622 len = strlen(str);
3623 readline_set_completion_index(rs, len);
3624 list = elt = object_class_get_list(TYPE_DEVICE, false);
3625 while (elt) {
3626 const char *name;
3627 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3628 TYPE_DEVICE);
3629 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003630
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003631 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003632 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003633 readline_add_completion(rs, name);
3634 }
3635 elt = elt->next;
3636 }
3637 g_slist_free(list);
3638}
3639
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003640void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003641{
3642 GSList *list, *elt;
3643 size_t len;
3644
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003645 if (nb_args != 2) {
3646 return;
3647 }
3648
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003649 len = strlen(str);
3650 readline_set_completion_index(rs, len);
3651 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3652 while (elt) {
3653 const char *name;
3654
3655 name = object_class_get_name(OBJECT_CLASS(elt->data));
3656 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3657 readline_add_completion(rs, name);
3658 }
3659 elt = elt->next;
3660 }
3661 g_slist_free(list);
3662}
3663
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003664static void peripheral_device_del_completion(ReadLineState *rs,
3665 const char *str, size_t len)
3666{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003667 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3668 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003669
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003670 list = qdev_build_hotpluggable_device_list(peripheral);
3671 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003672 return;
3673 }
3674
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003675 for (item = list; item; item = g_slist_next(item)) {
3676 DeviceState *dev = item->data;
3677
3678 if (dev->id && !strncmp(str, dev->id, len)) {
3679 readline_add_completion(rs, dev->id);
3680 }
3681 }
3682
3683 g_slist_free(list);
3684}
3685
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003686void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3687{
3688 size_t len;
3689 ChardevInfoList *list, *start;
3690
3691 if (nb_args != 2) {
3692 return;
3693 }
3694 len = strlen(str);
3695 readline_set_completion_index(rs, len);
3696
3697 start = list = qmp_query_chardev(NULL);
3698 while (list) {
3699 ChardevInfo *chr = list->value;
3700
3701 if (!strncmp(chr->label, str, len)) {
3702 readline_add_completion(rs, chr->label);
3703 }
3704 list = list->next;
3705 }
3706 qapi_free_ChardevInfoList(start);
3707}
3708
Hani Benhabiles8e597772014-05-27 23:39:30 +01003709static void ringbuf_completion(ReadLineState *rs, const char *str)
3710{
3711 size_t len;
3712 ChardevInfoList *list, *start;
3713
3714 len = strlen(str);
3715 readline_set_completion_index(rs, len);
3716
3717 start = list = qmp_query_chardev(NULL);
3718 while (list) {
3719 ChardevInfo *chr_info = list->value;
3720
3721 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003722 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003723 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003724 readline_add_completion(rs, chr_info->label);
3725 }
3726 }
3727 list = list->next;
3728 }
3729 qapi_free_ChardevInfoList(start);
3730}
3731
Hani Benhabiles8e597772014-05-27 23:39:30 +01003732void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3733{
3734 if (nb_args != 2) {
3735 return;
3736 }
3737 ringbuf_completion(rs, str);
3738}
3739
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003740void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3741{
3742 size_t len;
3743
3744 if (nb_args != 2) {
3745 return;
3746 }
3747
3748 len = strlen(str);
3749 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003750 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003751}
3752
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003753void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003754{
3755 ObjectPropertyInfoList *list, *start;
3756 size_t len;
3757
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003758 if (nb_args != 2) {
3759 return;
3760 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003761 len = strlen(str);
3762 readline_set_completion_index(rs, len);
3763
3764 start = list = qmp_qom_list("/objects", NULL);
3765 while (list) {
3766 ObjectPropertyInfo *info = list->value;
3767
3768 if (!strncmp(info->type, "child<", 5)
3769 && !strncmp(info->name, str, len)) {
3770 readline_add_completion(rs, info->name);
3771 }
3772 list = list->next;
3773 }
3774 qapi_free_ObjectPropertyInfoList(start);
3775}
3776
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003777void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3778{
3779 int i;
3780 char *sep;
3781 size_t len;
3782
3783 if (nb_args != 2) {
3784 return;
3785 }
3786 sep = strrchr(str, '-');
3787 if (sep) {
3788 str = sep + 1;
3789 }
3790 len = strlen(str);
3791 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003792 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003793 if (!strncmp(str, QKeyCode_str(i), len)) {
3794 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003795 }
3796 }
3797}
3798
Hani Benhabiles40d19392014-05-07 23:41:30 +01003799void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3800{
3801 size_t len;
3802
3803 len = strlen(str);
3804 readline_set_completion_index(rs, len);
3805 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003806 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003807 int count, i;
3808 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003809 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003810 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003811 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003812 const char *name = ncs[i]->name;
3813 if (!strncmp(str, name, len)) {
3814 readline_add_completion(rs, name);
3815 }
3816 }
3817 } else if (nb_args == 3) {
3818 add_completion_option(rs, str, "on");
3819 add_completion_option(rs, str, "off");
3820 }
3821}
3822
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003823void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3824{
3825 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003826 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003827
3828 if (nb_args != 2) {
3829 return;
3830 }
3831
3832 len = strlen(str);
3833 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003834 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003835 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003836 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003837 QemuOpts *opts;
3838 const char *name = ncs[i]->name;
3839 if (strncmp(str, name, len)) {
3840 continue;
3841 }
3842 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3843 if (opts) {
3844 readline_add_completion(rs, name);
3845 }
3846 }
3847}
3848
Lluís Vilanovabd712112016-07-11 12:53:51 +02003849void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3850{
3851 size_t len;
3852
3853 len = strlen(str);
3854 readline_set_completion_index(rs, len);
3855 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003856 TraceEventIter iter;
3857 TraceEvent *ev;
3858 char *pattern = g_strdup_printf("%s*", str);
3859 trace_event_iter_init(&iter, pattern);
3860 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3861 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003862 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003863 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003864 }
3865}
3866
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003867void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3868{
3869 size_t len;
3870
3871 len = strlen(str);
3872 readline_set_completion_index(rs, len);
3873 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003874 TraceEventIter iter;
3875 TraceEvent *ev;
3876 char *pattern = g_strdup_printf("%s*", str);
3877 trace_event_iter_init(&iter, pattern);
3878 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3879 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003880 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003881 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003882 } else if (nb_args == 3) {
3883 add_completion_option(rs, str, "on");
3884 add_completion_option(rs, str, "off");
3885 }
3886}
3887
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003888void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3889{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003890 int i;
3891
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003892 if (nb_args != 2) {
3893 return;
3894 }
3895 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003896 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3897 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003898 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003899}
3900
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003901void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3902 const char *str)
3903{
3904 size_t len;
3905
3906 len = strlen(str);
3907 readline_set_completion_index(rs, len);
3908 if (nb_args == 2) {
3909 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003910 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003911 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003912 if (!strncmp(str, name, len)) {
3913 readline_add_completion(rs, name);
3914 }
3915 }
3916 } else if (nb_args == 3) {
3917 add_completion_option(rs, str, "on");
3918 add_completion_option(rs, str, "off");
3919 }
3920}
3921
Liang Li50e9a622015-03-23 16:32:29 +08003922void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3923 const char *str)
3924{
3925 size_t len;
3926
3927 len = strlen(str);
3928 readline_set_completion_index(rs, len);
3929 if (nb_args == 2) {
3930 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003931 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003932 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003933 if (!strncmp(str, name, len)) {
3934 readline_add_completion(rs, name);
3935 }
3936 }
3937 }
3938}
3939
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003940static void vm_completion(ReadLineState *rs, const char *str)
3941{
3942 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003943 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003944 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003945
3946 len = strlen(str);
3947 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003948
Kevin Wolf88be7b42016-05-20 18:49:07 +02003949 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003950 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003951 AioContext *ctx = bdrv_get_aio_context(bs);
3952 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003953
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003954 aio_context_acquire(ctx);
3955 if (bdrv_can_snapshot(bs)) {
3956 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3957 }
3958 aio_context_release(ctx);
3959 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003960 continue;
3961 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003962
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003963 snapshot = snapshots;
3964 while (snapshot) {
3965 char *completion = snapshot->value->name;
3966 if (!strncmp(str, completion, len)) {
3967 readline_add_completion(rs, completion);
3968 }
3969 completion = snapshot->value->id;
3970 if (!strncmp(str, completion, len)) {
3971 readline_add_completion(rs, completion);
3972 }
3973 snapshot = snapshot->next;
3974 }
3975 qapi_free_SnapshotInfoList(snapshots);
3976 }
3977
3978}
3979
3980void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3981{
3982 if (nb_args == 2) {
3983 vm_completion(rs, str);
3984 }
3985}
3986
3987void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3988{
3989 if (nb_args == 2) {
3990 vm_completion(rs, str);
3991 }
3992}
3993
Wenchao Xiac35b6402013-08-27 20:38:24 +08003994static void monitor_find_completion_by_table(Monitor *mon,
3995 const mon_cmd_t *cmd_table,
3996 char **args,
3997 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003998{
3999 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004000 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00004001 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004002 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01004003 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00004004
bellard81d09122004-07-14 17:21:37 +00004005 if (nb_args <= 1) {
4006 /* command completion */
4007 if (nb_args == 0)
4008 cmdname = "";
4009 else
4010 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004011 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004012 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004013 if (!runstate_check(RUN_STATE_PRECONFIG) ||
4014 cmd_can_preconfig(cmd)) {
4015 cmd_completion(mon, cmdname, cmd->name);
4016 }
bellard81d09122004-07-14 17:21:37 +00004017 }
4018 } else {
4019 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004020 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004021 if (compare_cmd(args[0], cmd->name) &&
4022 (!runstate_check(RUN_STATE_PRECONFIG) ||
4023 cmd_can_preconfig(cmd))) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004024 break;
4025 }
bellard81d09122004-07-14 17:21:37 +00004026 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004027 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004028 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004029 }
4030
Wenchao Xiad903a772013-08-27 20:38:25 +08004031 if (cmd->sub_table) {
4032 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004033 monitor_find_completion_by_table(mon, cmd->sub_table,
4034 &args[1], nb_args - 1);
4035 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004036 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004037 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004038 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4039 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004040 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004041
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004042 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004043 for(i = 0; i < nb_args - 2; i++) {
4044 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004045 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004046 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004047 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004048 }
4049 }
4050 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00004051 old_ptype = NULL;
4052 while (*ptype == '-' && old_ptype != ptype) {
4053 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004054 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004055 }
bellard81d09122004-07-14 17:21:37 +00004056 switch(*ptype) {
4057 case 'F':
4058 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004059 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004060 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004061 break;
4062 case 'B':
4063 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004064 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01004065 while ((blk = blk_next(blk)) != NULL) {
4066 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004067 if (str[0] == '\0' ||
4068 !strncmp(name, str, strlen(str))) {
4069 readline_add_completion(mon->rs, name);
4070 }
4071 }
bellard81d09122004-07-14 17:21:37 +00004072 break;
bellard7fe48482004-10-09 18:08:01 +00004073 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004074 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004075 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004076 monitor_find_completion_by_table(mon, cmd_table,
4077 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004078 }
4079 break;
bellard81d09122004-07-14 17:21:37 +00004080 default:
4081 break;
4082 }
4083 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004084}
4085
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004086static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004087 const char *cmdline)
4088{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004089 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004090 char *args[MAX_ARGS];
4091 int nb_args, len;
4092
4093 /* 1. parse the cmdline */
4094 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4095 return;
4096 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004097
4098 /* if the line ends with a space, it means we want to complete the
4099 next arg */
4100 len = strlen(cmdline);
4101 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4102 if (nb_args >= MAX_ARGS) {
4103 goto cleanup;
4104 }
4105 args[nb_args++] = g_strdup("");
4106 }
4107
4108 /* 2. auto complete according to args */
4109 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004110
4111cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004112 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004113}
4114
aliguori731b0362009-03-05 23:01:42 +00004115static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004116{
aliguori731b0362009-03-05 23:01:42 +00004117 Monitor *mon = opaque;
4118
Peter Xudf152fb2018-03-09 16:59:55 +08004119 return !atomic_mb_read(&mon->suspend_cnt);
bellard9dc39cb2004-03-14 21:38:27 +00004120}
4121
Peter Xu546aa562018-03-09 16:59:54 +08004122/*
4123 * 1. This function takes ownership of rsp, err, and id.
4124 * 2. rsp, err, and id may be NULL.
4125 * 3. If err != NULL then rsp must be NULL.
4126 */
4127static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
4128 Error *err, QObject *id)
4129{
4130 QDict *qdict = NULL;
4131
4132 if (err) {
4133 assert(!rsp);
4134 qdict = qdict_new();
4135 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
4136 error_free(err);
4137 rsp = QOBJECT(qdict);
4138 }
4139
4140 if (rsp) {
4141 if (id) {
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004142 qdict_put_obj(qobject_to(QDict, rsp), "id", qobject_ref(id));
Peter Xu546aa562018-03-09 16:59:54 +08004143 }
4144
4145 monitor_json_emitter(mon, rsp);
4146 }
4147
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004148 qobject_unref(id);
4149 qobject_unref(rsp);
Peter Xu546aa562018-03-09 16:59:54 +08004150}
4151
Peter Xu71da4662018-03-09 16:59:57 +08004152/*
4153 * Dispatch one single QMP request. The function will free the req_obj
4154 * and objects inside it before return.
4155 */
4156static void monitor_qmp_dispatch_one(QMPRequest *req_obj)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004157{
Peter Xu71da4662018-03-09 16:59:57 +08004158 Monitor *mon, *old_mon;
4159 QObject *req, *rsp = NULL, *id;
Peter Xu71da4662018-03-09 16:59:57 +08004160 bool need_resume;
Peter Xu227a0752018-03-09 16:59:49 +08004161
Peter Xu71da4662018-03-09 16:59:57 +08004162 req = req_obj->req;
4163 mon = req_obj->mon;
4164 id = req_obj->id;
4165 need_resume = req_obj->need_resume;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004166
Peter Xu71da4662018-03-09 16:59:57 +08004167 g_free(req_obj);
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004168
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03004169 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
4170 QString *req_json = qobject_to_json(req);
4171 trace_handle_qmp_command(mon, qstring_get_str(req_json));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004172 qobject_unref(req_json);
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03004173 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01004174
Peter Xu227a0752018-03-09 16:59:49 +08004175 old_mon = cur_mon;
4176 cur_mon = mon;
4177
Peter Xu71da4662018-03-09 16:59:57 +08004178 rsp = qmp_dispatch(mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004179
Peter Xu227a0752018-03-09 16:59:49 +08004180 cur_mon = old_mon;
4181
Peter Xu71da4662018-03-09 16:59:57 +08004182 /* Respond if necessary */
4183 monitor_qmp_respond(mon, rsp, NULL, id);
4184
4185 /* This pairs with the monitor_suspend() in handle_qmp_command(). */
4186 if (need_resume) {
4187 monitor_resume(mon);
4188 }
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004189
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004190 qobject_unref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004191}
4192
Peter Xu71da4662018-03-09 16:59:57 +08004193/*
4194 * Pop one QMP request from monitor queues, return NULL if not found.
4195 * We are using round-robin fashion to pop the request, to avoid
4196 * processing commands only on a very busy monitor. To achieve that,
4197 * when we process one request on a specific monitor, we put that
4198 * monitor to the end of mon_list queue.
4199 */
Peter Xu40687eb2018-06-20 15:32:18 +08004200static QMPRequest *monitor_qmp_requests_pop_any(void)
Peter Xu71da4662018-03-09 16:59:57 +08004201{
4202 QMPRequest *req_obj = NULL;
4203 Monitor *mon;
4204
4205 qemu_mutex_lock(&monitor_lock);
4206
4207 QTAILQ_FOREACH(mon, &mon_list, entry) {
4208 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4209 req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
4210 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4211 if (req_obj) {
4212 break;
4213 }
4214 }
4215
4216 if (req_obj) {
4217 /*
4218 * We found one request on the monitor. Degrade this monitor's
4219 * priority to lowest by re-inserting it to end of queue.
4220 */
4221 QTAILQ_REMOVE(&mon_list, mon, entry);
4222 QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
4223 }
4224
4225 qemu_mutex_unlock(&monitor_lock);
4226
4227 return req_obj;
4228}
4229
4230static void monitor_qmp_bh_dispatcher(void *data)
4231{
Peter Xu40687eb2018-06-20 15:32:18 +08004232 QMPRequest *req_obj = monitor_qmp_requests_pop_any();
Peter Xu71da4662018-03-09 16:59:57 +08004233
4234 if (req_obj) {
Peter Xucf869d52018-03-10 20:38:05 -06004235 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
Peter Xu71da4662018-03-09 16:59:57 +08004236 monitor_qmp_dispatch_one(req_obj);
4237 /* Reschedule instead of looping so the main loop stays responsive */
4238 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
4239 }
4240}
4241
Peter Xubf1e7302018-03-09 16:59:59 +08004242#define QMP_REQ_QUEUE_LEN_MAX (8)
4243
Peter Xu71da4662018-03-09 16:59:57 +08004244static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
4245{
4246 QObject *req, *id = NULL;
4247 QDict *qdict = NULL;
4248 MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser);
4249 Monitor *mon = container_of(mon_qmp, Monitor, qmp);
4250 Error *err = NULL;
4251 QMPRequest *req_obj;
4252
4253 req = json_parser_parse_err(tokens, NULL, &err);
4254 if (!req && !err) {
4255 /* json_parser_parse_err() sucks: can fail without setting @err */
4256 error_setg(&err, QERR_JSON_PARSING);
4257 }
4258 if (err) {
Peter Xucf869d52018-03-10 20:38:05 -06004259 goto err;
Peter Xu71da4662018-03-09 16:59:57 +08004260 }
4261
Peter Xucf869d52018-03-10 20:38:05 -06004262 /* Check against the request in general layout */
4263 qdict = qmp_dispatch_check_obj(req, &err);
4264 if (!qdict) {
4265 goto err;
4266 }
4267
4268 /* Check against OOB specific */
4269 if (!qmp_cmd_oob_check(mon, qdict, &err)) {
4270 goto err;
4271 }
4272
4273 id = qdict_get(qdict, "id");
4274
4275 /* When OOB is enabled, the "id" field is mandatory. */
4276 if (qmp_oob_enabled(mon) && !id) {
4277 error_setg(&err, "Out-Of-Band capability requires that "
4278 "every command contains an 'id' field");
4279 goto err;
4280 }
4281
Peter Xu71da4662018-03-09 16:59:57 +08004282 req_obj = g_new0(QMPRequest, 1);
4283 req_obj->mon = mon;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004284 req_obj->id = qobject_ref(id);
Peter Xu71da4662018-03-09 16:59:57 +08004285 req_obj->req = req;
4286 req_obj->need_resume = false;
4287
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004288 qdict_del(qdict, "id");
4289
Peter Xucf869d52018-03-10 20:38:05 -06004290 if (qmp_is_oob(qdict)) {
4291 /* Out-Of-Band (OOB) requests are executed directly in parser. */
4292 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(req_obj->id)
4293 ?: "");
4294 monitor_qmp_dispatch_one(req_obj);
4295 return;
4296 }
4297
Peter Xubf1e7302018-03-09 16:59:59 +08004298 /* Protect qmp_requests and fetching its length. */
4299 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4300
Peter Xu71da4662018-03-09 16:59:57 +08004301 /*
4302 * If OOB is not enabled on the current monitor, we'll emulate the
4303 * old behavior that we won't process the current monitor any more
4304 * until it has responded. This helps make sure that as long as
4305 * OOB is not enabled, the server will never drop any command.
4306 */
4307 if (!qmp_oob_enabled(mon)) {
4308 monitor_suspend(mon);
4309 req_obj->need_resume = true;
Peter Xubf1e7302018-03-09 16:59:59 +08004310 } else {
4311 /* Drop the request if queue is full. */
4312 if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) {
4313 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4314 qapi_event_send_command_dropped(id,
4315 COMMAND_DROP_REASON_QUEUE_FULL,
4316 &error_abort);
Peter Xu6d2d5632018-03-26 14:38:55 +08004317 qmp_request_free(req_obj);
Peter Xubf1e7302018-03-09 16:59:59 +08004318 return;
4319 }
Peter Xu71da4662018-03-09 16:59:57 +08004320 }
4321
4322 /*
4323 * Put the request to the end of queue so that requests will be
4324 * handled in time order. Ownership for req_obj, req, id,
4325 * etc. will be delivered to the handler side.
4326 */
Peter Xu71da4662018-03-09 16:59:57 +08004327 g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
4328 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4329
4330 /* Kick the dispatcher routine */
4331 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
Peter Xucf869d52018-03-10 20:38:05 -06004332 return;
4333
4334err:
4335 monitor_qmp_respond(mon, NULL, err, NULL);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004336 qobject_unref(req);
Peter Xu71da4662018-03-09 16:59:57 +08004337}
4338
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004339static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004340{
Peter Xu227a0752018-03-09 16:59:49 +08004341 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004342
Peter Xu227a0752018-03-09 16:59:49 +08004343 json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004344}
4345
aliguori731b0362009-03-05 23:01:42 +00004346static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004347{
aliguori731b0362009-03-05 23:01:42 +00004348 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004349 int i;
aliguori376253e2009-03-05 23:01:23 +00004350
aliguori731b0362009-03-05 23:01:42 +00004351 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004352
aliguoricde76ee2009-03-05 23:01:51 +00004353 if (cur_mon->rs) {
4354 for (i = 0; i < size; i++)
4355 readline_handle_byte(cur_mon->rs, buf[i]);
4356 } else {
4357 if (size == 0 || buf[size - 1] != 0)
4358 monitor_printf(cur_mon, "corrupted command\n");
4359 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004360 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004361 }
aliguori731b0362009-03-05 23:01:42 +00004362
4363 cur_mon = old_mon;
4364}
aliguorid8f44602008-10-06 13:52:44 +00004365
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004366static void monitor_command_cb(void *opaque, const char *cmdline,
4367 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004368{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004369 Monitor *mon = opaque;
4370
aliguori731b0362009-03-05 23:01:42 +00004371 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004372 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004373 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004374}
4375
aliguoricde76ee2009-03-05 23:01:51 +00004376int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004377{
Peter Xue3e977d2018-03-09 16:59:56 +08004378 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004379 return -ENOTTY;
Peter Xue3e977d2018-03-09 16:59:56 +08004380 }
4381
Peter Xudf152fb2018-03-09 16:59:55 +08004382 atomic_inc(&mon->suspend_cnt);
Peter Xue3e977d2018-03-09 16:59:56 +08004383
4384 if (monitor_is_qmp(mon)) {
4385 /*
4386 * Kick iothread to make sure this takes effect. It'll be
4387 * evaluated again in prepare() of the watch object.
4388 */
4389 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4390 }
4391
4392 trace_monitor_suspend(mon, 1);
aliguoricde76ee2009-03-05 23:01:51 +00004393 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004394}
4395
aliguori376253e2009-03-05 23:01:23 +00004396void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004397{
Peter Xue3e977d2018-03-09 16:59:56 +08004398 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004399 return;
Peter Xudf152fb2018-03-09 16:59:55 +08004400 }
Peter Xue3e977d2018-03-09 16:59:56 +08004401
4402 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
4403 if (monitor_is_qmp(mon)) {
4404 /*
4405 * For QMP monitors that are running in IOThread, let's
4406 * kick the thread in case it's sleeping.
4407 */
4408 if (mon->use_io_thr) {
4409 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4410 }
4411 } else {
4412 assert(mon->rs);
4413 readline_show_prompt(mon->rs);
4414 }
4415 }
4416 trace_monitor_suspend(mon, -1);
bellard7e2515e2004-08-01 21:52:19 +00004417}
4418
Peter Xu02130312018-03-09 16:59:53 +08004419static QObject *get_qmp_greeting(Monitor *mon)
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004420{
Peter Xu02130312018-03-09 16:59:53 +08004421 QList *cap_list = qlist_new();
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004422 QObject *ver = NULL;
Peter Xu02130312018-03-09 16:59:53 +08004423 QMPCapability cap;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004424
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004425 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004426
Peter Xu02130312018-03-09 16:59:53 +08004427 for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
4428 if (!mon->use_io_thr && cap == QMP_CAPABILITY_OOB) {
4429 /* Monitors that are not using IOThread won't support OOB */
4430 continue;
4431 }
Laurent Vivier625eaca2018-03-23 15:32:01 +01004432 qlist_append_str(cap_list, QMPCapability_str(cap));
Peter Xu02130312018-03-09 16:59:53 +08004433 }
4434
4435 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': %p}}",
4436 ver, cap_list);
4437}
4438
4439static void monitor_qmp_caps_reset(Monitor *mon)
4440{
4441 memset(mon->qmp.qmp_caps, 0, sizeof(mon->qmp.qmp_caps));
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004442}
4443
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004444static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004445{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004446 QObject *data;
4447 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004448
Luiz Capitulino47116d12010-02-08 17:01:30 -02004449 switch (event) {
4450 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01004451 mon->qmp.commands = &qmp_cap_negotiation_commands;
Peter Xu02130312018-03-09 16:59:53 +08004452 monitor_qmp_caps_reset(mon);
4453 data = get_qmp_greeting(mon);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004454 monitor_json_emitter(mon, data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004455 qobject_unref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004456 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004457 break;
4458 case CHR_EVENT_CLOSED:
Peter Xu6d2d5632018-03-26 14:38:55 +08004459 monitor_qmp_cleanup_queues(mon);
Markus Armbruster74358f22015-03-06 19:35:59 +01004460 json_message_parser_destroy(&mon->qmp.parser);
4461 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004462 mon_refcount--;
4463 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004464 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004465 }
4466}
4467
aliguori731b0362009-03-05 23:01:42 +00004468static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004469{
aliguori376253e2009-03-05 23:01:23 +00004470 Monitor *mon = opaque;
4471
aliguori2724b182009-03-05 23:01:47 +00004472 switch (event) {
4473 case CHR_EVENT_MUX_IN:
Peter Xudc7cbcd2018-06-08 11:55:05 +08004474 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004475 mon->mux_out = 0;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004476 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004477 if (mon->reset_seen) {
4478 readline_restart(mon->rs);
4479 monitor_resume(mon);
4480 monitor_flush(mon);
4481 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004482 atomic_mb_set(&mon->suspend_cnt, 0);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004483 }
aliguori2724b182009-03-05 23:01:47 +00004484 break;
ths86e94de2007-01-05 22:01:59 +00004485
aliguori2724b182009-03-05 23:01:47 +00004486 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004487 if (mon->reset_seen) {
Peter Xudf152fb2018-03-09 16:59:55 +08004488 if (atomic_mb_read(&mon->suspend_cnt) == 0) {
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004489 monitor_printf(mon, "\n");
4490 }
4491 monitor_flush(mon);
4492 monitor_suspend(mon);
4493 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004494 atomic_inc(&mon->suspend_cnt);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004495 }
Peter Xudc7cbcd2018-06-08 11:55:05 +08004496 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004497 mon->mux_out = 1;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004498 qemu_mutex_unlock(&mon->mon_lock);
aliguori2724b182009-03-05 23:01:47 +00004499 break;
4500
Amit Shahb6b8df52009-10-07 18:31:16 +05304501 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004502 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4503 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004504 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004505 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004506 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004507 }
4508 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004509 mon_refcount++;
4510 break;
4511
4512 case CHR_EVENT_CLOSED:
4513 mon_refcount--;
4514 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004515 break;
4516 }
ths86e94de2007-01-05 22:01:59 +00004517}
4518
Wayne Xia816f8922011-10-12 11:32:41 +08004519static int
4520compare_mon_cmd(const void *a, const void *b)
4521{
4522 return strcmp(((const mon_cmd_t *)a)->name,
4523 ((const mon_cmd_t *)b)->name);
4524}
4525
4526static void sortcmdlist(void)
4527{
4528 int array_num;
4529 int elem_size = sizeof(mon_cmd_t);
4530
4531 array_num = sizeof(mon_cmds)/elem_size-1;
4532 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4533
4534 array_num = sizeof(info_cmds)/elem_size-1;
4535 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4536}
4537
Peter Xua5ed3522018-03-09 16:59:52 +08004538static GMainContext *monitor_get_io_context(void)
4539{
4540 return iothread_get_g_main_context(mon_global.mon_iothread);
4541}
4542
4543static AioContext *monitor_get_aio_context(void)
4544{
4545 return iothread_get_aio_context(mon_global.mon_iothread);
4546}
4547
4548static void monitor_iothread_init(void)
4549{
4550 mon_global.mon_iothread = iothread_create("mon_iothread",
4551 &error_abort);
Peter Xu71da4662018-03-09 16:59:57 +08004552
4553 /*
4554 * This MUST be on main loop thread since we have commands that
4555 * have assumption to be run on main loop thread. It would be
4556 * nice that one day we can remove this assumption in the future.
4557 */
Peter Xu951702f2018-04-10 12:49:42 +08004558 mon_global.qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
Peter Xu71da4662018-03-09 16:59:57 +08004559 monitor_qmp_bh_dispatcher,
4560 NULL);
Peter Xuabe3cd02018-03-09 17:00:02 +08004561
4562 /*
4563 * Unlike the dispatcher BH, this must be run on the monitor IO
4564 * thread, so that monitors that are using IO thread will make
4565 * sure read/write operations are all done on the IO thread.
4566 */
4567 mon_global.qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
4568 monitor_qmp_bh_responder,
4569 NULL);
Peter Xua5ed3522018-03-09 16:59:52 +08004570}
4571
Peter Xu6adf08d2018-03-09 16:59:50 +08004572void monitor_init_globals(void)
4573{
4574 monitor_init_qmp_commands();
4575 monitor_qapi_event_init();
4576 sortcmdlist();
4577 qemu_mutex_init(&monitor_lock);
Peter Xu47451462018-06-08 11:55:11 +08004578 qemu_mutex_init(&mon_fdsets_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004579 monitor_iothread_init();
Peter Xu6adf08d2018-03-09 16:59:50 +08004580}
4581
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004582/* These functions just adapt the readline interface in a typesafe way. We
4583 * could cast function pointers but that discards compiler checks.
4584 */
Stefan Weild5d15072014-01-25 18:18:23 +01004585static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4586 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004587{
4588 va_list ap;
4589 va_start(ap, fmt);
4590 monitor_vprintf(opaque, fmt, ap);
4591 va_end(ap);
4592}
4593
4594static void monitor_readline_flush(void *opaque)
4595{
4596 monitor_flush(opaque);
4597}
4598
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004599/*
4600 * Print to current monitor if we have one, else to stderr.
4601 * TODO should return int, so callers can calculate width, but that
4602 * requires surgery to monitor_vprintf(). Left for another day.
4603 */
4604void error_vprintf(const char *fmt, va_list ap)
4605{
4606 if (cur_mon && !monitor_cur_is_qmp()) {
4607 monitor_vprintf(cur_mon, fmt, ap);
4608 } else {
4609 vfprintf(stderr, fmt, ap);
4610 }
4611}
4612
4613void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4614{
4615 if (cur_mon && !monitor_cur_is_qmp()) {
4616 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004617 } else if (!cur_mon) {
4618 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004619 }
4620}
4621
Peter Xua5ed3522018-03-09 16:59:52 +08004622static void monitor_list_append(Monitor *mon)
4623{
4624 qemu_mutex_lock(&monitor_lock);
4625 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4626 qemu_mutex_unlock(&monitor_lock);
4627}
4628
4629static void monitor_qmp_setup_handlers_bh(void *opaque)
4630{
4631 Monitor *mon = opaque;
4632 GMainContext *context;
4633
4634 if (mon->use_io_thr) {
4635 /*
4636 * When use_io_thr is set, we use the global shared dedicated
4637 * IO thread for this monitor to handle input/output.
4638 */
4639 context = monitor_get_io_context();
4640 /* We should have inited globals before reaching here. */
4641 assert(context);
4642 } else {
4643 /* The default main loop, which is the main thread */
4644 context = NULL;
4645 }
4646
4647 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4648 monitor_qmp_event, NULL, mon, context, true);
4649 monitor_list_append(mon);
4650}
4651
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004652void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004653{
Peter Xu6adf08d2018-03-09 16:59:50 +08004654 Monitor *mon = g_malloc(sizeof(*mon));
Peter Xube933ff2018-03-26 14:38:56 +08004655 bool use_readline = flags & MONITOR_USE_READLINE;
4656 bool use_oob = flags & MONITOR_USE_OOB;
ths20d8a3e2007-02-18 17:04:49 +00004657
Peter Xube933ff2018-03-26 14:38:56 +08004658 if (use_oob) {
4659 if (CHARDEV_IS_MUX(chr)) {
4660 error_report("Monitor Out-Of-Band is not supported with "
4661 "MUX typed chardev backend");
4662 exit(1);
4663 }
4664 if (use_readline) {
4665 error_report("Monitor Out-Of-band is only supported by QMP");
4666 exit(1);
4667 }
4668 }
4669
4670 monitor_data_init(mon, false, use_oob);
ths20d8a3e2007-02-18 17:04:49 +00004671
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004672 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004673 mon->flags = flags;
Peter Xube933ff2018-03-26 14:38:56 +08004674 if (use_readline) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004675 mon->rs = readline_init(monitor_readline_printf,
4676 monitor_readline_flush,
4677 mon,
4678 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004679 monitor_read_command(mon, 0);
4680 }
aliguori87127162009-03-05 23:01:29 +00004681
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004682 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004683 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004684 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Peter Xua5ed3522018-03-09 16:59:52 +08004685 if (mon->use_io_thr) {
4686 /*
4687 * Make sure the old iowatch is gone. It's possible when
4688 * e.g. the chardev is in client mode, with wait=on.
4689 */
4690 remove_fd_in_watch(chr);
4691 /*
4692 * We can't call qemu_chr_fe_set_handlers() directly here
4693 * since during the procedure the chardev will be active
4694 * and running in monitor iothread, while we'll still do
4695 * something before returning from it, which is a possible
4696 * race too. To avoid that, we just create a BH to setup
4697 * the handlers.
4698 */
4699 aio_bh_schedule_oneshot(monitor_get_aio_context(),
4700 monitor_qmp_setup_handlers_bh, mon);
4701 /* We'll add this to mon_list in the BH when setup done */
4702 return;
4703 } else {
4704 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
4705 monitor_qmp_read, monitor_qmp_event,
4706 NULL, mon, NULL, true);
4707 }
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004708 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004709 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004710 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004711 }
aliguori87127162009-03-05 23:01:29 +00004712
Peter Xua5ed3522018-03-09 16:59:52 +08004713 monitor_list_append(mon);
bellard7e2515e2004-08-01 21:52:19 +00004714}
4715
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004716void monitor_cleanup(void)
4717{
4718 Monitor *mon, *next;
4719
Peter Xua5ed3522018-03-09 16:59:52 +08004720 /*
4721 * We need to explicitly stop the iothread (but not destroy it),
4722 * cleanup the monitor resources, then destroy the iothread since
4723 * we need to unregister from chardev below in
4724 * monitor_data_destroy(), and chardev is not thread-safe yet
4725 */
4726 iothread_stop(mon_global.mon_iothread);
4727
Peter Xuabe3cd02018-03-09 17:00:02 +08004728 /*
4729 * After we have IOThread to send responses, it's possible that
4730 * when we stop the IOThread there are still replies queued in the
4731 * responder queue. Flush all of them. Note that even after this
4732 * flush it's still possible that out buffer is not flushed.
4733 * It'll be done in below monitor_flush() as the last resort.
4734 */
4735 monitor_qmp_bh_responder(NULL);
4736
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004737 qemu_mutex_lock(&monitor_lock);
Peter Xu238d9f32018-03-09 16:59:51 +08004738 QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
4739 QTAILQ_REMOVE(&mon_list, mon, entry);
Peter Xuabe3cd02018-03-09 17:00:02 +08004740 monitor_flush(mon);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004741 monitor_data_destroy(mon);
4742 g_free(mon);
4743 }
4744 qemu_mutex_unlock(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004745
Peter Xu71da4662018-03-09 16:59:57 +08004746 /* QEMUBHs needs to be deleted before destroying the IOThread. */
4747 qemu_bh_delete(mon_global.qmp_dispatcher_bh);
4748 mon_global.qmp_dispatcher_bh = NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +08004749 qemu_bh_delete(mon_global.qmp_respond_bh);
4750 mon_global.qmp_respond_bh = NULL;
Peter Xu71da4662018-03-09 16:59:57 +08004751
Peter Xua5ed3522018-03-09 16:59:52 +08004752 iothread_destroy(mon_global.mon_iothread);
4753 mon_global.mon_iothread = NULL;
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004754}
4755
Paolo Bonzini4d454572012-11-26 16:03:42 +01004756QemuOptsList qemu_mon_opts = {
4757 .name = "mon",
4758 .implied_opt_name = "chardev",
4759 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4760 .desc = {
4761 {
4762 .name = "mode",
4763 .type = QEMU_OPT_STRING,
4764 },{
4765 .name = "chardev",
4766 .type = QEMU_OPT_STRING,
4767 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004768 .name = "pretty",
4769 .type = QEMU_OPT_BOOL,
Peter Xube933ff2018-03-26 14:38:56 +08004770 },{
4771 .name = "x-oob",
4772 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004773 },
4774 { /* end of list */ }
4775 },
4776};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004777
4778#ifndef TARGET_I386
4779void qmp_rtc_reset_reinjection(Error **errp)
4780{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004781 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004782}
Brijesh Singh08a161f2018-03-08 06:48:42 -06004783
4784SevInfo *qmp_query_sev(Error **errp)
4785{
4786 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
4787 return NULL;
4788}
Brijesh Singh1b6a0342018-03-08 06:48:56 -06004789
4790SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
4791{
4792 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
4793 return NULL;
4794}
Brijesh Singh31dd67f2018-03-08 06:48:59 -06004795
4796SevCapability *qmp_query_sev_capabilities(Error **errp)
4797{
4798 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
4799 return NULL;
4800}
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004801#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004802
4803#ifndef TARGET_S390X
4804void qmp_dump_skeys(const char *filename, Error **errp)
4805{
4806 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4807}
4808#endif
Peter Xuae50a772016-03-30 17:27:24 +01004809
4810#ifndef TARGET_ARM
4811GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4812{
4813 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4814 return NULL;
4815}
4816#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304817
4818HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4819{
4820 MachineState *ms = MACHINE(qdev_get_machine());
4821 MachineClass *mc = MACHINE_GET_CLASS(ms);
4822
Igor Mammedovc5514d02017-02-10 11:20:57 +01004823 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304824 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4825 return NULL;
4826 }
4827
Igor Mammedovc5514d02017-02-10 11:20:57 +01004828 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304829}