blob: b3c5dcc6855e2b5041499a3ea39166444f917020 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Philippe Mathieu-Daudé8ec338a2018-06-25 09:42:34 -030026#include "qemu/units.h"
blueswir1511d2b12009-03-07 15:32:56 +000027#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010028#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010030#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000031#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040038#include "chardev/char-fe.h"
Peter Xua5ed3522018-03-09 16:59:52 +080039#include "chardev/char-io.h"
Peter Xube933ff2018-03-26 14:38:56 +080040#include "chardev/char-mux.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020041#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020042#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010043#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030044#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010045#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010046#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010047#include "ui/input.h"
Max Reitzda27a002016-03-16 19:54:29 +010048#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000049#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020050#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010051#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010053#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010054#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020055#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010056#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010057#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010058#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010059#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010060#include "qapi/qmp/qjson.h"
61#include "qapi/qmp/json-streamer.h"
62#include "qapi/qmp/json-parser.h"
Peter Xu02130312018-03-09 16:59:53 +080063#include "qapi/qmp/qlist.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020064#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000065#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030067#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020068#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020069#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053070#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010071#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010072#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010073#include "qemu/log.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010074#include "qemu/option.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050075#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010076#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010077#include "block/qapi.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010078#include "qapi/qapi-commands.h"
79#include "qapi/qapi-events.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010080#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020081#include "qapi/qmp-event.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010082#include "qapi/qapi-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020083#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010084#include "sysemu/cpus.h"
Peter Xua5ed3522018-03-09 16:59:52 +080085#include "sysemu/iothread.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020086#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000087
Jason J. Hernea4538a52015-06-26 14:07:21 -040088#if defined(TARGET_S390X)
89#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020090#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040091#endif
92
bellard9307c4c2004-04-04 12:57:25 +000093/*
94 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000095 *
bellard9307c4c2004-04-04 12:57:25 +000096 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000097 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000098 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080099 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100100 * 'O' option string of the form NAME=VALUE,...
101 * parsed according to QemuOptsList given by its name
102 * Example: 'device:O' uses qemu_device_opts.
103 * Restriction: only lists with empty desc are supported
104 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000105 * 'i' 32 bit integer
106 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300107 * 'M' Non-negative target long (32 or 64 bit), in user mode the
108 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200109 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200110 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
111 * K, k suffix, which multiplies the value by 2^60 for suffixes E
112 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
113 * 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 +0100114 * 'T' double
115 * user mode accepts an optional ms, us, ns suffix,
116 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000117 * '/' optional gdb-like print format (like "/10x")
118 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300119 * '?' optional type (for all types, except '/')
120 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100121 * 'b' boolean
122 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300123 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000124 *
125 */
126
Anthony Liguoric227f092009-10-01 16:12:16 -0500127typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000128 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000129 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000130 const char *params;
131 const char *help;
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100132 const char *flags; /* p=preconfig */
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400133 void (*cmd)(Monitor *mon, const QDict *qdict);
134 /* @sub_table is a list of 2nd level of commands. If it does not exist,
135 * cmd should be used. If it exists, sub_table[?].cmd should be
136 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800137 */
138 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100139 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500140} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000141
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500143typedef struct mon_fd_t mon_fd_t;
144struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145 char *name;
146 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500147 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100148};
149
Corey Bryantba1c0482012-08-14 16:43:43 -0400150/* file descriptor associated with a file descriptor set */
151typedef struct MonFdsetFd MonFdsetFd;
152struct MonFdsetFd {
153 int fd;
154 bool removed;
155 char *opaque;
156 QLIST_ENTRY(MonFdsetFd) next;
157};
158
159/* file descriptor set containing fds passed via SCM_RIGHTS */
160typedef struct MonFdset MonFdset;
161struct MonFdset {
162 int64_t id;
163 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400164 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400165 QLIST_ENTRY(MonFdset) next;
166};
167
Markus Armbruster74358f22015-03-06 19:35:59 +0100168typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100170 /*
171 * When a client connects, we're in capabilities negotiation mode.
172 * When command qmp_capabilities succeeds, we go into command
173 * mode.
174 */
Markus Armbruster635db182017-03-03 13:32:27 +0100175 QmpCommandList *commands;
Peter Xu02130312018-03-09 16:59:53 +0800176 bool qmp_caps[QMP_CAPABILITY__MAX];
Peter Xu71da4662018-03-09 16:59:57 +0800177 /*
178 * Protects qmp request/response queue. Please take monitor_lock
179 * first when used together.
180 */
181 QemuMutex qmp_queue_lock;
182 /* Input queue that holds all the parsed QMP requests */
183 GQueue *qmp_requests;
Peter Xuabe3cd02018-03-09 17:00:02 +0800184 /* Output queue contains all the QMP responses in order */
185 GQueue *qmp_responses;
Markus Armbruster74358f22015-03-06 19:35:59 +0100186} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200187
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100188/*
189 * To prevent flooding clients, events can be throttled. The
190 * throttling is calculated globally, rather than per-Monitor
191 * instance.
192 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200193typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200194 QAPIEvent event; /* Throttling state for this event type and... */
195 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100196 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200197 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200198} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100199
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200200typedef struct {
201 int64_t rate; /* Minimum time (in ns) between two events */
202} MonitorQAPIEventConf;
203
aliguori87127162009-03-05 23:01:29 +0000204struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300205 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200206 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000207 int flags;
Peter Xudf152fb2018-03-09 16:59:55 +0800208 int suspend_cnt; /* Needs to be accessed atomically */
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400209 bool skip_flush;
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200210 bool use_io_thread;
Peter Xud9f25282018-06-08 11:55:07 +0800211
212 /*
213 * State used only in the thread "owning" the monitor.
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200214 * If @use_io_thread, this is mon_global.mon_iothread.
Peter Xud9f25282018-06-08 11:55:07 +0800215 * Else, it's the main thread.
216 * These members can be safely accessed without locks.
217 */
aliguori731b0362009-03-05 23:01:42 +0000218 ReadLineState *rs;
Peter Xud9f25282018-06-08 11:55:07 +0800219
Markus Armbruster74358f22015-03-06 19:35:59 +0100220 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200221 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200222 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000223 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800224 mon_cmd_t *cmd_table;
Peter Xu238d9f32018-03-09 16:59:51 +0800225 QTAILQ_ENTRY(Monitor) entry;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800226
227 /*
228 * The per-monitor lock. We can't access guest memory when holding
229 * the lock.
230 */
231 QemuMutex mon_lock;
232
233 /*
234 * Fields that are protected by the per-monitor lock.
235 */
Peter Xu9409fc02018-06-08 11:55:06 +0800236 QLIST_HEAD(, mon_fd_t) fds;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800237 QString *outbuf;
238 guint out_watch;
239 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
240 int mux_out;
aliguori87127162009-03-05 23:01:29 +0000241};
242
Peter Xua5ed3522018-03-09 16:59:52 +0800243/* Let's add monitor global variables to this struct. */
244static struct {
245 IOThread *mon_iothread;
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +0200246 /* Bottom half to dispatch the requests received from I/O thread */
Peter Xu71da4662018-03-09 16:59:57 +0800247 QEMUBH *qmp_dispatcher_bh;
Peter Xuabe3cd02018-03-09 17:00:02 +0800248 /* Bottom half to deliver the responses back to clients */
249 QEMUBH *qmp_respond_bh;
Peter Xua5ed3522018-03-09 16:59:52 +0800250} mon_global;
251
Peter Xu6d2d5632018-03-26 14:38:55 +0800252struct QMPRequest {
253 /* Owner of the request */
254 Monitor *mon;
255 /* "id" field of the request */
256 QObject *id;
Markus Armbruster1cc37472018-07-03 10:53:44 +0200257 /*
258 * Request object to be handled or Error to be reported
259 * (exactly one of them is non-null)
260 */
Peter Xu6d2d5632018-03-26 14:38:55 +0800261 QObject *req;
Markus Armbruster1cc37472018-07-03 10:53:44 +0200262 Error *err;
Peter Xu6d2d5632018-03-26 14:38:55 +0800263 /*
264 * Whether we need to resume the monitor afterward. This flag is
265 * used to emulate the old QMP server behavior that the current
266 * command must be completed before execution of the next one.
267 */
268 bool need_resume;
269};
270typedef struct QMPRequest QMPRequest;
271
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300272/* QMP checker flags */
273#define QMP_ACCEPT_UNKNOWNS 1
274
Peter Xu095cb1b2018-06-08 11:55:08 +0800275/* Protects mon_list, monitor_qapi_event_state. */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200276static QemuMutex monitor_lock;
Peter Xu095cb1b2018-06-08 11:55:08 +0800277static GHashTable *monitor_qapi_event_state;
Peter Xu238d9f32018-03-09 16:59:51 +0800278static QTAILQ_HEAD(mon_list, Monitor) mon_list;
Peter Xu095cb1b2018-06-08 11:55:08 +0800279
Peter Xu47451462018-06-08 11:55:11 +0800280/* Protects mon_fdsets */
281static QemuMutex mon_fdsets_lock;
Corey Bryantba1c0482012-08-14 16:43:43 -0400282static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800283
Corey Bryantefb87c12012-08-14 16:43:48 -0400284static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000285
Wayne Xia816f8922011-10-12 11:32:41 +0800286static mon_cmd_t mon_cmds[];
287static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000288
Markus Armbruster635db182017-03-03 13:32:27 +0100289QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100290
Markus Armbruster8631b602010-02-18 11:41:55 +0100291Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000292
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100293static void monitor_command_cb(void *opaque, const char *cmdline,
294 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000295
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100296/**
297 * Is @mon a QMP monitor?
298 */
299static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200300{
301 return (mon->flags & MONITOR_USE_CONTROL);
302}
303
Markus Armbruster489653b2015-03-06 20:01:05 +0100304/**
Peter Xue3e977d2018-03-09 16:59:56 +0800305 * Whether @mon is using readline? Note: not all HMP monitors use
306 * readline, e.g., gdbserver has a non-interactive HMP monitor, so
307 * readline is not used there.
308 */
309static inline bool monitor_uses_readline(const Monitor *mon)
310{
311 return mon->flags & MONITOR_USE_READLINE;
312}
313
314static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
315{
316 return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
317}
318
Peter Xu6e8c5f42018-06-08 11:55:09 +0800319/*
320 * Return the clock to use for recording an event's time.
321 * Beware: result is invalid before configure_accelerator().
322 */
323static inline QEMUClockType monitor_get_event_clock(void)
324{
325 /*
326 * This allows us to perform tests on the monitor queues to verify
327 * that the rate limits are enforced.
328 */
329 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
330}
331
Peter Xue3e977d2018-03-09 16:59:56 +0800332/**
Markus Armbruster489653b2015-03-06 20:01:05 +0100333 * Is the current monitor, if any, a QMP monitor?
334 */
335bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100336{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100337 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100338}
339
Anthony Liguori7060b472011-09-02 12:34:50 -0500340void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000341{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200342 if (!mon->rs)
343 return;
344
aliguori731b0362009-03-05 23:01:42 +0000345 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
346 if (show_prompt)
347 readline_show_prompt(mon->rs);
348}
bellard6a00d602005-11-21 23:25:50 +0000349
Anthony Liguori7060b472011-09-02 12:34:50 -0500350int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
351 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000352{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100353 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000354 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
355 /* prompt is printed on return from the command handler */
356 return 0;
357 } else {
358 monitor_printf(mon, "terminal does not support password prompting\n");
359 return -ENOTTY;
360 }
aliguoribb5fc202009-03-05 23:01:15 +0000361}
362
Peter Xu6d2d5632018-03-26 14:38:55 +0800363static void qmp_request_free(QMPRequest *req)
364{
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200365 qobject_unref(req->id);
366 qobject_unref(req->req);
Markus Armbruster1cc37472018-07-03 10:53:44 +0200367 error_free(req->err);
Peter Xu6d2d5632018-03-26 14:38:55 +0800368 g_free(req);
369}
370
371/* Must with the mon->qmp.qmp_queue_lock held */
372static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon)
373{
374 while (!g_queue_is_empty(mon->qmp.qmp_requests)) {
375 qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests));
376 }
377}
378
379/* Must with the mon->qmp.qmp_queue_lock held */
380static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon)
381{
382 while (!g_queue_is_empty(mon->qmp.qmp_responses)) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200383 qobject_unref((QObject *)g_queue_pop_head(mon->qmp.qmp_responses));
Peter Xu6d2d5632018-03-26 14:38:55 +0800384 }
385}
386
387static void monitor_qmp_cleanup_queues(Monitor *mon)
388{
389 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
390 monitor_qmp_cleanup_req_queue_locked(mon);
391 monitor_qmp_cleanup_resp_queue_locked(mon);
392 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
393}
394
395
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200396static void monitor_flush_locked(Monitor *mon);
397
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100398static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
399 void *opaque)
400{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200401 Monitor *mon = opaque;
402
Peter Xudc7cbcd2018-06-08 11:55:05 +0800403 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200404 mon->out_watch = 0;
405 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800406 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100407 return FALSE;
408}
409
Peter Xudc7cbcd2018-06-08 11:55:05 +0800410/* Called with mon->mon_lock held. */
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200411static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000412{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100413 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400414 size_t len;
415 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100416
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400417 if (mon->skip_flush) {
418 return;
419 }
420
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400421 buf = qstring_get_str(mon->outbuf);
422 len = qstring_get_length(mon->outbuf);
423
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200424 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300425 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200426 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
427 /* all flushed or error */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200428 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400429 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100430 return;
431 }
432 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600433 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400434 QString *tmp = qstring_from_str(buf + rc);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200435 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400436 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100437 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200438 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300439 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300440 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300441 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200442 }
bellard7e2515e2004-08-01 21:52:19 +0000443 }
444}
445
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200446void monitor_flush(Monitor *mon)
447{
Peter Xudc7cbcd2018-06-08 11:55:05 +0800448 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200449 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800450 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200451}
452
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400453/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000454static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000455{
ths60fe76f2007-12-16 03:02:09 +0000456 char c;
aliguori731b0362009-03-05 23:01:42 +0000457
Peter Xudc7cbcd2018-06-08 11:55:05 +0800458 qemu_mutex_lock(&mon->mon_lock);
bellard7e2515e2004-08-01 21:52:19 +0000459 for(;;) {
460 c = *str++;
461 if (c == '\0')
462 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400463 if (c == '\n') {
464 qstring_append_chr(mon->outbuf, '\r');
465 }
466 qstring_append_chr(mon->outbuf, c);
467 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200468 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400469 }
bellard7e2515e2004-08-01 21:52:19 +0000470 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800471 qemu_mutex_unlock(&mon->mon_lock);
bellard7e2515e2004-08-01 21:52:19 +0000472}
473
aliguori376253e2009-03-05 23:01:23 +0000474void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000475{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400476 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200477
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200478 if (!mon)
479 return;
480
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100481 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200482 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200483 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200484
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400485 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200486 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400487 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000488}
489
aliguori376253e2009-03-05 23:01:23 +0000490void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000491{
492 va_list ap;
493 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000494 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000495 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000496}
497
Pavel Butsykincaf15312015-09-22 16:18:17 +0300498int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000499{
500 va_list ap;
501 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000502 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000503 va_end(ap);
504 return 0;
505}
506
Peter Xuabe3cd02018-03-09 17:00:02 +0800507static void monitor_json_emitter_raw(Monitor *mon,
508 QObject *data)
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200509{
510 QString *json;
511
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200512 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
513 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200514 assert(json != NULL);
515
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200516 qstring_append_chr(json, '\n');
517 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200518
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200519 qobject_unref(json);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200520}
521
Peter Xuabe3cd02018-03-09 17:00:02 +0800522static void monitor_json_emitter(Monitor *mon, QObject *data)
523{
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200524 if (mon->use_io_thread) {
Peter Xuabe3cd02018-03-09 17:00:02 +0800525 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +0200526 * If using I/O thread, we need to queue the item so that I/O
Peter Xuabe3cd02018-03-09 17:00:02 +0800527 * thread will do the rest for us. Take refcount so that
528 * caller won't free the data (which will be finally freed in
529 * responder thread).
530 */
Peter Xuabe3cd02018-03-09 17:00:02 +0800531 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200532 g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(data));
Peter Xuabe3cd02018-03-09 17:00:02 +0800533 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
534 qemu_bh_schedule(mon_global.qmp_respond_bh);
535 } else {
536 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +0200537 * If not using monitor I/O thread, then we are in main thread.
Peter Xuabe3cd02018-03-09 17:00:02 +0800538 * Do the emission right away.
539 */
540 monitor_json_emitter_raw(mon, data);
541 }
542}
543
544struct QMPResponse {
545 Monitor *mon;
546 QObject *data;
547};
548typedef struct QMPResponse QMPResponse;
549
Peter Xuc73a8432018-06-20 15:32:19 +0800550static QObject *monitor_qmp_response_pop_one(Monitor *mon)
551{
552 QObject *data;
553
554 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
555 data = g_queue_pop_head(mon->qmp.qmp_responses);
556 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
557
558 return data;
559}
560
561static void monitor_qmp_response_flush(Monitor *mon)
562{
563 QObject *data;
564
565 while ((data = monitor_qmp_response_pop_one(mon))) {
566 monitor_json_emitter_raw(mon, data);
567 qobject_unref(data);
568 }
569}
570
Peter Xuabe3cd02018-03-09 17:00:02 +0800571/*
Peter Xu40687eb2018-06-20 15:32:18 +0800572 * Pop a QMPResponse from any monitor's response queue into @response.
573 * Return false if all the queues are empty; else true.
Peter Xuabe3cd02018-03-09 17:00:02 +0800574 */
Peter Xu40687eb2018-06-20 15:32:18 +0800575static bool monitor_qmp_response_pop_any(QMPResponse *response)
Peter Xuabe3cd02018-03-09 17:00:02 +0800576{
577 Monitor *mon;
578 QObject *data = NULL;
579
580 qemu_mutex_lock(&monitor_lock);
581 QTAILQ_FOREACH(mon, &mon_list, entry) {
Peter Xuc73a8432018-06-20 15:32:19 +0800582 data = monitor_qmp_response_pop_one(mon);
Peter Xuabe3cd02018-03-09 17:00:02 +0800583 if (data) {
Peter Xu40687eb2018-06-20 15:32:18 +0800584 response->mon = mon;
585 response->data = data;
Peter Xuabe3cd02018-03-09 17:00:02 +0800586 break;
587 }
588 }
589 qemu_mutex_unlock(&monitor_lock);
Peter Xu40687eb2018-06-20 15:32:18 +0800590 return data != NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +0800591}
592
593static void monitor_qmp_bh_responder(void *opaque)
594{
595 QMPResponse response;
596
Peter Xu40687eb2018-06-20 15:32:18 +0800597 while (monitor_qmp_response_pop_any(&response)) {
Peter Xuabe3cd02018-03-09 17:00:02 +0800598 monitor_json_emitter_raw(response.mon, response.data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200599 qobject_unref(response.data);
Peter Xuabe3cd02018-03-09 17:00:02 +0800600 }
601}
602
Eric Blake7fb1cf12015-11-18 01:52:57 -0700603static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200604 /* Limit guest-triggerable events to 1 per second */
605 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
606 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
607 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
608 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
609 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
610 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
611};
612
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100613/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200614 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200615 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100616 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200617static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100618{
619 Monitor *mon;
620
Markus Armbruster688b4b72015-10-15 17:08:30 +0200621 trace_monitor_protocol_event_emit(event, qdict);
Peter Xu238d9f32018-03-09 16:59:51 +0800622 QTAILQ_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100623 if (monitor_is_qmp(mon)
624 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200625 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100626 }
627 }
628}
629
Markus Armbrustera24712a2015-10-15 17:08:34 +0200630static void monitor_qapi_event_handler(void *opaque);
631
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100632/*
633 * Queue a new event for emission to Monitor instances,
634 * applying any rate limiting if required.
635 */
636static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200637monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100638{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200639 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200640 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200641
Eric Blake7fb1cf12015-11-18 01:52:57 -0700642 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200643 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200644 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100645
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200646 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200647
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200648 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200649 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200650 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100651 } else {
Max Reitz7dc847e2018-02-24 16:40:29 +0100652 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
Markus Armbruster7de0be62015-10-15 17:08:35 +0200653 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200654
655 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
656 assert(!evstate || timer_pending(evstate->timer));
657
658 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200659 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200660 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200661 * last send. Store event for sending when timer fires,
662 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100663 */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200664 qobject_unref(evstate->qdict);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200665 evstate->qdict = qobject_ref(qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100666 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200667 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200668 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200669 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200670 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200671 * events arriving before then will be delayed until then.
672 */
Peter Xu6e8c5f42018-06-08 11:55:09 +0800673 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200674
Markus Armbruster688b4b72015-10-15 17:08:30 +0200675 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200676
677 evstate = g_new(MonitorQAPIEventState, 1);
678 evstate->event = event;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200679 evstate->data = qobject_ref(data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200680 evstate->qdict = NULL;
Peter Xu6e8c5f42018-06-08 11:55:09 +0800681 evstate->timer = timer_new_ns(monitor_get_event_clock(),
Markus Armbrustera24712a2015-10-15 17:08:34 +0200682 monitor_qapi_event_handler,
683 evstate);
684 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200685 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100686 }
687 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200688
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200689 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100690}
691
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100692/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200693 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200694 * event.
695 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100696 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200697static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100698{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200699 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200700 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100701
Markus Armbruster93f8f982015-10-15 17:08:31 +0200702 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200703 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200704
Markus Armbruster688b4b72015-10-15 17:08:30 +0200705 if (evstate->qdict) {
Peter Xu6e8c5f42018-06-08 11:55:09 +0800706 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200707
Markus Armbruster688b4b72015-10-15 17:08:30 +0200708 monitor_qapi_event_emit(evstate->event, evstate->qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200709 qobject_unref(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200710 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200711 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200712 } else {
713 g_hash_table_remove(monitor_qapi_event_state, evstate);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200714 qobject_unref(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200715 timer_free(evstate->timer);
716 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100717 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200718
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200719 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100720}
721
Markus Armbrustera24712a2015-10-15 17:08:34 +0200722static unsigned int qapi_event_throttle_hash(const void *key)
723{
724 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200725 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200726
Markus Armbruster7de0be62015-10-15 17:08:35 +0200727 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
728 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
729 }
730
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200731 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
732 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
733 }
734
Markus Armbruster7de0be62015-10-15 17:08:35 +0200735 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200736}
737
738static gboolean qapi_event_throttle_equal(const void *a, const void *b)
739{
740 const MonitorQAPIEventState *eva = a;
741 const MonitorQAPIEventState *evb = b;
742
Markus Armbruster7de0be62015-10-15 17:08:35 +0200743 if (eva->event != evb->event) {
744 return FALSE;
745 }
746
747 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
748 return !strcmp(qdict_get_str(eva->data, "id"),
749 qdict_get_str(evb->data, "id"));
750 }
751
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200752 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
753 return !strcmp(qdict_get_str(eva->data, "node-name"),
754 qdict_get_str(evb->data, "node-name"));
755 }
756
Markus Armbruster7de0be62015-10-15 17:08:35 +0200757 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200758}
759
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200760static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100761{
Markus Armbrustera24712a2015-10-15 17:08:34 +0200762 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
763 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200764 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200765}
766
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100767static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200768
Peter Xua5ed3522018-03-09 16:59:52 +0800769static void monitor_data_init(Monitor *mon, bool skip_flush,
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200770 bool use_io_thread)
Wenchao Xiab01fe892013-08-27 20:38:19 +0800771{
772 memset(mon, 0, sizeof(Monitor));
Peter Xudc7cbcd2018-06-08 11:55:05 +0800773 qemu_mutex_init(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800774 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800775 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800776 /* Use *mon_cmds by default. */
777 mon->cmd_table = mon_cmds;
Peter Xu88a95d12018-03-09 16:59:48 +0800778 mon->skip_flush = skip_flush;
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200779 mon->use_io_thread = use_io_thread;
Peter Xu71da4662018-03-09 16:59:57 +0800780 mon->qmp.qmp_requests = g_queue_new();
Peter Xuabe3cd02018-03-09 17:00:02 +0800781 mon->qmp.qmp_responses = g_queue_new();
Wenchao Xiab01fe892013-08-27 20:38:19 +0800782}
783
784static void monitor_data_destroy(Monitor *mon)
785{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200786 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400787 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400788 if (monitor_is_qmp(mon)) {
789 json_message_parser_destroy(&mon->qmp.parser);
790 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100791 readline_free(mon->rs);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200792 qobject_unref(mon->outbuf);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800793 qemu_mutex_destroy(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800794 qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
Peter Xu6d2d5632018-03-26 14:38:55 +0800795 monitor_qmp_cleanup_req_queue_locked(mon);
796 monitor_qmp_cleanup_resp_queue_locked(mon);
Peter Xu71da4662018-03-09 16:59:57 +0800797 g_queue_free(mon->qmp.qmp_requests);
Peter Xuabe3cd02018-03-09 17:00:02 +0800798 g_queue_free(mon->qmp.qmp_responses);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800799}
800
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200801char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
802 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200803{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200804 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200805 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200806
Peter Xua5ed3522018-03-09 16:59:52 +0800807 monitor_data_init(&hmp, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200808
809 old_mon = cur_mon;
810 cur_mon = &hmp;
811
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200812 if (has_cpu_index) {
813 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200814 if (ret < 0) {
815 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100816 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
817 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200818 goto out;
819 }
820 }
821
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100822 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200823 cur_mon = old_mon;
824
Peter Xudc7cbcd2018-06-08 11:55:05 +0800825 qemu_mutex_lock(&hmp.mon_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400826 if (qstring_get_length(hmp.outbuf) > 0) {
827 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200828 } else {
829 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200830 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800831 qemu_mutex_unlock(&hmp.mon_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200832
833out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800834 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200835 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200836}
837
bellard9dc39cb2004-03-14 21:38:27 +0000838static int compare_cmd(const char *name, const char *list)
839{
840 const char *p, *pstart;
841 int len;
842 len = strlen(name);
843 p = list;
844 for(;;) {
845 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200846 p = qemu_strchrnul(p, '|');
bellard9dc39cb2004-03-14 21:38:27 +0000847 if ((p - pstart) == len && !memcmp(pstart, name, len))
848 return 1;
849 if (*p == '\0')
850 break;
851 p++;
852 }
853 return 0;
854}
855
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800856static int get_str(char *buf, int buf_size, const char **pp)
857{
858 const char *p;
859 char *q;
860 int c;
861
862 q = buf;
863 p = *pp;
864 while (qemu_isspace(*p)) {
865 p++;
866 }
867 if (*p == '\0') {
868 fail:
869 *q = '\0';
870 *pp = p;
871 return -1;
872 }
873 if (*p == '\"') {
874 p++;
875 while (*p != '\0' && *p != '\"') {
876 if (*p == '\\') {
877 p++;
878 c = *p++;
879 switch (c) {
880 case 'n':
881 c = '\n';
882 break;
883 case 'r':
884 c = '\r';
885 break;
886 case '\\':
887 case '\'':
888 case '\"':
889 break;
890 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100891 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800892 goto fail;
893 }
894 if ((q - buf) < buf_size - 1) {
895 *q++ = c;
896 }
897 } else {
898 if ((q - buf) < buf_size - 1) {
899 *q++ = *p;
900 }
901 p++;
902 }
903 }
904 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100905 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800906 goto fail;
907 }
908 p++;
909 } else {
910 while (*p != '\0' && !qemu_isspace(*p)) {
911 if ((q - buf) < buf_size - 1) {
912 *q++ = *p;
913 }
914 p++;
915 }
916 }
917 *q = '\0';
918 *pp = p;
919 return 0;
920}
921
922#define MAX_ARGS 16
923
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800924static void free_cmdline_args(char **args, int nb_args)
925{
926 int i;
927
928 assert(nb_args <= MAX_ARGS);
929
930 for (i = 0; i < nb_args; i++) {
931 g_free(args[i]);
932 }
933
934}
935
936/*
937 * Parse the command line to get valid args.
938 * @cmdline: command line to be parsed.
939 * @pnb_args: location to store the number of args, must NOT be NULL.
940 * @args: location to store the args, which should be freed by caller, must
941 * NOT be NULL.
942 *
943 * Returns 0 on success, negative on failure.
944 *
945 * NOTE: this parser is an approximate form of the real command parser. Number
946 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
947 * return with failure.
948 */
949static int parse_cmdline(const char *cmdline,
950 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800951{
952 const char *p;
953 int nb_args, ret;
954 char buf[1024];
955
956 p = cmdline;
957 nb_args = 0;
958 for (;;) {
959 while (qemu_isspace(*p)) {
960 p++;
961 }
962 if (*p == '\0') {
963 break;
964 }
965 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800966 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800967 }
968 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800969 if (ret < 0) {
970 goto fail;
971 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800972 args[nb_args] = g_strdup(buf);
973 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800974 }
975 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800976 return 0;
977
978 fail:
979 free_cmdline_args(args, nb_args);
980 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800981}
982
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +0100983/*
984 * Returns true if the command can be executed in preconfig mode
985 * i.e. it has the 'p' flag.
986 */
987static bool cmd_can_preconfig(const mon_cmd_t *cmd)
988{
989 if (!cmd->flags) {
990 return false;
991 }
992
993 return strchr(cmd->flags, 'p');
994}
995
Wenchao Xia66855492013-08-27 20:38:23 +0800996static void help_cmd_dump_one(Monitor *mon,
997 const mon_cmd_t *cmd,
998 char **prefix_args,
999 int prefix_args_nb)
1000{
1001 int i;
1002
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +01001003 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
1004 return;
1005 }
1006
Wenchao Xia66855492013-08-27 20:38:23 +08001007 for (i = 0; i < prefix_args_nb; i++) {
1008 monitor_printf(mon, "%s ", prefix_args[i]);
1009 }
1010 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
1011}
1012
1013/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -05001014static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +08001015 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +00001016{
Anthony Liguoric227f092009-10-01 16:12:16 -05001017 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +00001018
Wenchao Xia66855492013-08-27 20:38:23 +08001019 /* No valid arg need to compare with, dump all in *cmds */
1020 if (arg_index >= nb_args) {
1021 for (cmd = cmds; cmd->name != NULL; cmd++) {
1022 help_cmd_dump_one(mon, cmd, args, arg_index);
1023 }
1024 return;
1025 }
1026
1027 /* Find one entry to dump */
1028 for (cmd = cmds; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +01001029 if (compare_cmd(args[arg_index], cmd->name) &&
1030 ((!runstate_check(RUN_STATE_PRECONFIG) ||
1031 cmd_can_preconfig(cmd)))) {
Wenchao Xia66855492013-08-27 20:38:23 +08001032 if (cmd->sub_table) {
1033 /* continue with next arg */
1034 help_cmd_dump(mon, cmd->sub_table,
1035 args, nb_args, arg_index + 1);
1036 } else {
1037 help_cmd_dump_one(mon, cmd, args, arg_index);
1038 }
1039 break;
1040 }
bellard9dc39cb2004-03-14 21:38:27 +00001041 }
1042}
1043
aliguori376253e2009-03-05 23:01:23 +00001044static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +00001045{
Wenchao Xia66855492013-08-27 20:38:23 +08001046 char *args[MAX_ARGS];
1047 int nb_args = 0;
1048
1049 /* 1. parse user input */
1050 if (name) {
1051 /* special case for log, directly dump and return */
1052 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +00001053 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +00001054 monitor_printf(mon, "Log items (comma separated):\n");
1055 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +00001056 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +00001057 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +00001058 }
Wenchao Xia66855492013-08-27 20:38:23 +08001059 return;
1060 }
1061
1062 if (parse_cmdline(name, &nb_args, args) < 0) {
1063 return;
bellardf193c792004-03-21 17:06:25 +00001064 }
bellard9dc39cb2004-03-14 21:38:27 +00001065 }
Wenchao Xia66855492013-08-27 20:38:23 +08001066
1067 /* 2. dump the contents according to parsed args */
1068 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
1069
1070 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +00001071}
1072
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001073static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -03001074{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001075 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -03001076}
1077
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001078static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301079{
1080 const char *tp_name = qdict_get_str(qdict, "name");
1081 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001082 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1083 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001084 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +00001085
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001086 if (vcpu < 0) {
1087 monitor_printf(mon, "argument vcpu must be positive");
1088 return;
1089 }
1090
1091 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001092 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +01001093 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +00001094 }
Prerna Saxena22890ab2010-06-24 17:04:53 +05301095}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001096
Michael Rothc45a8162011-10-02 08:44:37 -05001097#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001098static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001099{
1100 const char *op = qdict_get_try_str(qdict, "op");
1101 const char *arg = qdict_get_try_str(qdict, "arg");
1102
1103 if (!op) {
1104 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
1105 } else if (!strcmp(op, "on")) {
1106 st_set_trace_file_enabled(true);
1107 } else if (!strcmp(op, "off")) {
1108 st_set_trace_file_enabled(false);
1109 } else if (!strcmp(op, "flush")) {
1110 st_flush_trace_buffer();
1111 } else if (!strcmp(op, "set")) {
1112 if (arg) {
1113 st_set_trace_file(arg);
1114 }
1115 } else {
1116 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1117 help_cmd(mon, "trace-file");
1118 }
1119}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301120#endif
1121
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001122static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001123{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001124 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001125}
1126
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001127static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +00001128{
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001129 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001130
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001131 if (!cmd->enabled) {
1132 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001133 }
1134
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001135 info = g_malloc0(sizeof(*info));
1136 info->value = g_malloc0(sizeof(*info->value));
1137 info->value->name = g_strdup(cmd->name);
1138 info->next = *list;
1139 *list = info;
1140}
1141
1142CommandInfoList *qmp_query_commands(Error **errp)
1143{
1144 CommandInfoList *list = NULL;
1145
Markus Armbruster635db182017-03-03 13:32:27 +01001146 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001147
1148 return list;
thsa36e69d2007-12-02 05:18:19 +00001149}
1150
Daniel P. Berrange48608532012-05-21 17:59:51 +01001151EventInfoList *qmp_query_events(Error **errp)
1152{
1153 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +02001154 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +01001155
Eric Blake7fb1cf12015-11-18 01:52:57 -07001156 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001157 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +01001158 assert(event_name != NULL);
1159 info = g_malloc0(sizeof(*info));
1160 info->value = g_malloc0(sizeof(*info->value));
1161 info->value->name = g_strdup(event_name);
1162
1163 info->next = ev_list;
1164 ev_list = info;
1165 }
1166
1167 return ev_list;
1168}
1169
Markus Armbruster39a18152015-09-16 13:06:28 +02001170/*
1171 * Minor hack: generated marshalling suppressed for this command
1172 * ('gen': false in the schema) so we can parse the JSON string
1173 * directly into QObject instead of first parsing it with
1174 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1175 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01001176 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -06001177 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +02001178 */
1179static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1180 Error **errp)
1181{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001182 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
Markus Armbruster39a18152015-09-16 13:06:28 +02001183}
1184
Marc-André Lureau5032a162016-09-12 13:19:02 +04001185/*
Marc-André Lureau5032a162016-09-12 13:19:02 +04001186 * We used to define commands in qmp-commands.hx in addition to the
1187 * QAPI schema. This permitted defining some of them only in certain
1188 * configurations. query-commands has always reflected that (good,
1189 * because it lets QMP clients figure out what's actually available),
1190 * while query-qmp-schema never did (not so good). This function is a
1191 * hack to keep the configuration-specific commands defined exactly as
1192 * before, even though qmp-commands.hx is gone.
1193 *
1194 * FIXME Educate the QAPI schema on configuration-specific commands,
1195 * and drop this hack.
1196 */
1197static void qmp_unregister_commands_hack(void)
1198{
1199#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +01001200 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001201#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +02001202#ifndef CONFIG_REPLICATION
1203 qmp_unregister_command(&qmp_commands, "xen-set-replication");
1204 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
1205 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
1206#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001207#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +01001208 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Brijesh Singh08a161f2018-03-08 06:48:42 -06001209 qmp_unregister_command(&qmp_commands, "query-sev");
Brijesh Singh1b6a0342018-03-08 06:48:56 -06001210 qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
Brijesh Singh31dd67f2018-03-08 06:48:59 -06001211 qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001212#endif
1213#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +01001214 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001215#endif
1216#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +01001217 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001218#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001219#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001220 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001221#endif
1222#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001223 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
1224 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001225#endif
1226#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1227 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001228 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001229#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001230}
1231
Peter Xu6adf08d2018-03-09 16:59:50 +08001232static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001233{
Markus Armbruster635db182017-03-03 13:32:27 +01001234 /*
1235 * Two command lists:
1236 * - qmp_commands contains all QMP commands
1237 * - qmp_cap_negotiation_commands contains just
1238 * "qmp_capabilities", to enforce capability negotiation
1239 */
1240
Markus Armbruster1527bad2017-03-03 13:32:25 +01001241 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001242
Markus Armbruster1527bad2017-03-03 13:32:25 +01001243 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001244 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001245 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001246 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001247 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001248 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001249
Markus Armbruster05875682017-03-03 13:32:24 +01001250 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001251
1252 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1253 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001254 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +01001255}
1256
Peter Xu71da4662018-03-09 16:59:57 +08001257static bool qmp_cap_enabled(Monitor *mon, QMPCapability cap)
1258{
1259 return mon->qmp.qmp_caps[cap];
1260}
1261
1262static bool qmp_oob_enabled(Monitor *mon)
1263{
1264 return qmp_cap_enabled(mon, QMP_CAPABILITY_OOB);
1265}
1266
Peter Xu02130312018-03-09 16:59:53 +08001267static void qmp_caps_check(Monitor *mon, QMPCapabilityList *list,
1268 Error **errp)
Markus Armbruster635db182017-03-03 13:32:27 +01001269{
Peter Xu02130312018-03-09 16:59:53 +08001270 for (; list; list = list->next) {
1271 assert(list->value < QMP_CAPABILITY__MAX);
1272 switch (list->value) {
1273 case QMP_CAPABILITY_OOB:
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02001274 if (!mon->use_io_thread) {
Peter Xu02130312018-03-09 16:59:53 +08001275 /*
Markus Armbrusterc0698212018-07-03 10:53:27 +02001276 * Out-of-band only works with monitors that are
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02001277 * running on dedicated I/O thread.
Peter Xu02130312018-03-09 16:59:53 +08001278 */
1279 error_setg(errp, "This monitor does not support "
Markus Armbrusterc0698212018-07-03 10:53:27 +02001280 "out-of-band (OOB)");
Peter Xu02130312018-03-09 16:59:53 +08001281 return;
1282 }
1283 break;
1284 default:
1285 break;
1286 }
1287 }
1288}
1289
1290/* This function should only be called after capabilities are checked. */
1291static void qmp_caps_apply(Monitor *mon, QMPCapabilityList *list)
1292{
1293 for (; list; list = list->next) {
1294 mon->qmp.qmp_caps[list->value] = true;
1295 }
1296}
1297
1298void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
1299 Error **errp)
1300{
1301 Error *local_err = NULL;
1302
Markus Armbruster635db182017-03-03 13:32:27 +01001303 if (cur_mon->qmp.commands == &qmp_commands) {
1304 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1305 "Capabilities negotiation is already complete, command "
1306 "ignored");
1307 return;
1308 }
1309
Peter Xu02130312018-03-09 16:59:53 +08001310 /* Enable QMP capabilities provided by the client if applicable. */
1311 if (has_enable) {
1312 qmp_caps_check(cur_mon, enable, &local_err);
1313 if (local_err) {
1314 /*
1315 * Failed check on any of the capabilities will fail the
1316 * entire command (and thus not apply any of the other
1317 * capabilities that were also requested).
1318 */
1319 error_propagate(errp, local_err);
1320 return;
1321 }
1322 qmp_caps_apply(cur_mon, enable);
1323 }
1324
Markus Armbruster635db182017-03-03 13:32:27 +01001325 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001326}
1327
Peter Xud9f25282018-06-08 11:55:07 +08001328/* Set the current CPU defined by the user. Callers must hold BQL. */
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001329int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001330{
Andreas Färber55e5c282012-12-17 06:18:02 +01001331 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001332
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001333 cpu = qemu_get_cpu(cpu_index);
1334 if (cpu == NULL) {
1335 return -1;
bellard6a00d602005-11-21 23:25:50 +00001336 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001337 g_free(cur_mon->mon_cpu_path);
1338 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001339 return 0;
bellard6a00d602005-11-21 23:25:50 +00001340}
1341
Peter Xud9f25282018-06-08 11:55:07 +08001342/* Callers must hold BQL. */
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001343static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001344{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001345 CPUState *cpu;
1346
1347 if (cur_mon->mon_cpu_path) {
1348 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1349 TYPE_CPU, NULL);
1350 if (!cpu) {
1351 g_free(cur_mon->mon_cpu_path);
1352 cur_mon->mon_cpu_path = NULL;
1353 }
1354 }
1355 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001356 if (!first_cpu) {
1357 return NULL;
1358 }
David Gibson27a83f82016-09-21 15:29:26 +10001359 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001360 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001361 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001362 if (synchronize) {
1363 cpu_synchronize_state(cpu);
1364 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001365 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001366}
1367
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001368CPUState *mon_get_cpu(void)
1369{
1370 return mon_get_cpu_sync(true);
1371}
1372
Pavel Butsykinbf957282015-09-10 18:38:59 +03001373CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001374{
Thomas Huth854e67f2017-01-13 13:12:35 +01001375 CPUState *cs = mon_get_cpu();
1376
1377 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001378}
1379
Luiz Capitulino99b77962011-10-24 10:53:44 -02001380int monitor_get_cpu_index(void)
1381{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001382 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001383
1384 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001385}
1386
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001387static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001388{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001389 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1390 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001391
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001392 if (all_cpus) {
1393 CPU_FOREACH(cs) {
1394 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1395 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1396 }
1397 } else {
1398 cs = mon_get_cpu();
1399
1400 if (!cs) {
1401 monitor_printf(mon, "No CPU available\n");
1402 return;
1403 }
1404
1405 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001406 }
bellard9307c4c2004-04-04 12:57:25 +00001407}
1408
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001409#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001410static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001411{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001412 if (!tcg_enabled()) {
1413 error_report("JIT information is only available with accel=tcg");
1414 return;
1415 }
1416
aliguori376253e2009-03-05 23:01:23 +00001417 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001418 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001419}
1420
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001421static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001422{
1423 dump_opcount_info((FILE *)mon, monitor_fprintf);
1424}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001425#endif
Max Filippov246ae242014-11-02 11:04:18 +03001426
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001427static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001428{
1429 int i;
bellard7e2515e2004-08-01 21:52:19 +00001430 const char *str;
ths3b46e622007-09-17 08:09:54 +00001431
aliguoricde76ee2009-03-05 23:01:51 +00001432 if (!mon->rs)
1433 return;
bellard7e2515e2004-08-01 21:52:19 +00001434 i = 0;
1435 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001436 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001437 if (!str)
1438 break;
aliguori376253e2009-03-05 23:01:23 +00001439 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001440 i++;
bellardaa455482004-04-04 13:07:25 +00001441 }
1442}
1443
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001444static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001445{
Thomas Huth854e67f2017-01-13 13:12:35 +01001446 CPUState *cs = mon_get_cpu();
1447
1448 if (!cs) {
1449 monitor_printf(mon, "No CPU available\n");
1450 return;
1451 }
1452 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001453}
j_mayer76a66252007-03-07 08:32:30 +00001454
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001455static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301456{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001457 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001458 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1459 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001460 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001461 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001462 Error *local_err = NULL;
1463
1464 if (name == NULL) {
1465 name = "*";
1466 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001467 if (vcpu < 0) {
1468 monitor_printf(mon, "argument vcpu must be positive");
1469 return;
1470 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001471
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001472 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001473 if (local_err) {
1474 error_report_err(local_err);
1475 return;
1476 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001477
1478 for (elem = events; elem != NULL; elem = elem->next) {
1479 monitor_printf(mon, "%s : state %u\n",
1480 elem->value->name,
1481 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1482 }
1483 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301484}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301485
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001486void qmp_client_migrate_info(const char *protocol, const char *hostname,
1487 bool has_port, int64_t port,
1488 bool has_tls_port, int64_t tls_port,
1489 bool has_cert_subject, const char *cert_subject,
1490 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001491{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001492 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001493 if (!qemu_using_spice(errp)) {
1494 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001495 }
1496
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001497 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001498 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001499 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001500 }
1501
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001502 if (qemu_spice_migrate_info(hostname,
1503 has_port ? port : -1,
1504 has_tls_port ? tls_port : -1,
1505 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001506 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001507 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001508 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001509 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001510 }
1511
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001512 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001513}
1514
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001515static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001516{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001517 Error *err = NULL;
1518
1519 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1520 if (err) {
1521 error_report_err(err);
1522 }
pbrooke735b912007-06-30 13:53:24 +00001523}
1524
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001525static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001526{
1527 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001528 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001529
bellard9307c4c2004-04-04 12:57:25 +00001530 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001531 mask = 0;
1532 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001533 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001534 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001535 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001536 return;
1537 }
1538 }
Peter Maydell24537a02013-02-11 16:41:23 +00001539 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001540}
1541
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001542static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001543{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001544 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001545 if (!option || !strcmp(option, "on")) {
1546 singlestep = 1;
1547 } else if (!strcmp(option, "off")) {
1548 singlestep = 0;
1549 } else {
1550 monitor_printf(mon, "unexpected option %s\n", option);
1551 }
1552}
1553
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001554static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001555{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001556 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001557 if (!device)
1558 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1559 if (gdbserver_start(device) < 0) {
1560 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1561 device);
1562 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001563 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001564 } else {
aliguori59030a82009-04-05 18:43:41 +00001565 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1566 device);
bellard8a7ddc32004-03-31 19:00:16 +00001567 }
1568}
1569
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001570static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001571{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001572 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001573 if (select_watchdog_action(action) == -1) {
1574 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1575 }
1576}
1577
aliguori376253e2009-03-05 23:01:23 +00001578static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001579{
aliguori376253e2009-03-05 23:01:23 +00001580 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001581 switch(c) {
1582 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001584 break;
1585 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001586 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001587 break;
1588 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001589 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001590 break;
1591 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001592 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001593 break;
1594 default:
1595 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001596 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001597 } else {
aliguori376253e2009-03-05 23:01:23 +00001598 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001599 }
1600 break;
1601 }
aliguori376253e2009-03-05 23:01:23 +00001602 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001603}
1604
aliguori376253e2009-03-05 23:01:23 +00001605static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001606 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001607{
Blue Swirl23842aa2010-01-12 20:27:43 +00001608 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001609 uint8_t buf[16];
1610 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001611 CPUState *cs = mon_get_cpu();
1612
1613 if (!cs && (format == 'i' || !is_physical)) {
1614 monitor_printf(mon, "Can not dump without CPU\n");
1615 return;
1616 }
bellard9307c4c2004-04-04 12:57:25 +00001617
1618 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001619 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001620 return;
1621 }
1622
1623 len = wsize * count;
1624 if (wsize == 1)
1625 line_size = 8;
1626 else
1627 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001628 max_digits = 0;
1629
1630 switch(format) {
1631 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001632 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001633 break;
1634 default:
1635 case 'x':
1636 max_digits = (wsize * 8) / 4;
1637 break;
1638 case 'u':
1639 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001640 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001641 break;
1642 case 'c':
1643 wsize = 1;
1644 break;
1645 }
1646
1647 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001648 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001649 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001650 else
aliguori376253e2009-03-05 23:01:23 +00001651 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001652 l = len;
1653 if (l > line_size)
1654 l = line_size;
1655 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001656 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001657 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001658 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001659 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001660 break;
1661 }
bellard9307c4c2004-04-04 12:57:25 +00001662 }
ths5fafdf22007-09-16 21:08:06 +00001663 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001664 while (i < l) {
1665 switch(wsize) {
1666 default:
1667 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001668 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001669 break;
1670 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001671 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001672 break;
1673 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001674 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001675 break;
1676 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001677 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001678 break;
1679 }
aliguori376253e2009-03-05 23:01:23 +00001680 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001681 switch(format) {
1682 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001683 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001684 break;
1685 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001686 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001687 break;
1688 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001689 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001690 break;
1691 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001692 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001693 break;
1694 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001695 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001696 break;
1697 }
1698 i += wsize;
1699 }
aliguori376253e2009-03-05 23:01:23 +00001700 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001701 addr += l;
1702 len -= l;
1703 }
1704}
1705
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001706static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001707{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001708 int count = qdict_get_int(qdict, "count");
1709 int format = qdict_get_int(qdict, "format");
1710 int size = qdict_get_int(qdict, "size");
1711 target_long addr = qdict_get_int(qdict, "addr");
1712
aliguori376253e2009-03-05 23:01:23 +00001713 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001714}
1715
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001716static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001717{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001718 int count = qdict_get_int(qdict, "count");
1719 int format = qdict_get_int(qdict, "format");
1720 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001721 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001722
aliguori376253e2009-03-05 23:01:23 +00001723 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001724}
1725
Paolo Bonzinie9628442017-04-20 15:30:58 +02001726static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1727{
1728 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1729 addr, 1);
1730
1731 if (!mrs.mr) {
1732 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1733 return NULL;
1734 }
1735
1736 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1737 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1738 memory_region_unref(mrs.mr);
1739 return NULL;
1740 }
1741
1742 *p_mr = mrs.mr;
1743 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1744}
1745
1746static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1747{
1748 hwaddr addr = qdict_get_int(qdict, "addr");
1749 Error *local_err = NULL;
1750 MemoryRegion *mr = NULL;
1751 void *ptr;
1752
1753 ptr = gpa2hva(&mr, addr, &local_err);
1754 if (local_err) {
1755 error_report_err(local_err);
1756 return;
1757 }
1758
1759 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1760 " (%s) is %p\n",
1761 addr, mr->name, ptr);
1762
1763 memory_region_unref(mr);
1764}
1765
1766#ifdef CONFIG_LINUX
1767static uint64_t vtop(void *ptr, Error **errp)
1768{
1769 uint64_t pinfo;
1770 uint64_t ret = -1;
1771 uintptr_t addr = (uintptr_t) ptr;
1772 uintptr_t pagesize = getpagesize();
1773 off_t offset = addr / pagesize * sizeof(pinfo);
1774 int fd;
1775
1776 fd = open("/proc/self/pagemap", O_RDONLY);
1777 if (fd == -1) {
1778 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1779 return -1;
1780 }
1781
1782 /* Force copy-on-write if necessary. */
1783 atomic_add((uint8_t *)ptr, 0);
1784
1785 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1786 error_setg_errno(errp, errno, "Cannot read pagemap");
1787 goto out;
1788 }
1789 if ((pinfo & (1ull << 63)) == 0) {
1790 error_setg(errp, "Page not present");
1791 goto out;
1792 }
1793 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1794
1795out:
1796 close(fd);
1797 return ret;
1798}
1799
1800static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1801{
1802 hwaddr addr = qdict_get_int(qdict, "addr");
1803 Error *local_err = NULL;
1804 MemoryRegion *mr = NULL;
1805 void *ptr;
1806 uint64_t physaddr;
1807
1808 ptr = gpa2hva(&mr, addr, &local_err);
1809 if (local_err) {
1810 error_report_err(local_err);
1811 return;
1812 }
1813
1814 physaddr = vtop(ptr, &local_err);
1815 if (local_err) {
1816 error_report_err(local_err);
1817 } else {
1818 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1819 " (%s) is 0x%" PRIx64 "\n",
1820 addr, mr->name, (uint64_t) physaddr);
1821 }
1822
1823 memory_region_unref(mr);
1824}
1825#endif
1826
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001827static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001828{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001829 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001830 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001831
bellard9307c4c2004-04-04 12:57:25 +00001832 switch(format) {
1833 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001834 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001835 break;
1836 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001837 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001838 break;
1839 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001840 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001841 break;
1842 default:
1843 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001844 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001845 break;
1846 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001847 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001848 break;
1849 }
aliguori376253e2009-03-05 23:01:23 +00001850 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001851}
1852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001853static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001854{
1855 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001856 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001857 uint32_t start = qdict_get_int(qdict, "start");
1858 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001859
1860 sum = 0;
1861 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001862 uint8_t val = address_space_ldub(&address_space_memory, addr,
1863 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001864 /* BSD sum algorithm ('sum' Unix command) */
1865 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001866 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001867 }
aliguori376253e2009-03-05 23:01:23 +00001868 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001869}
1870
bellard13224a82006-07-14 22:03:35 +00001871static int mouse_button_state;
1872
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001873static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001874{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001875 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001876 const char *dx_str = qdict_get_str(qdict, "dx_str");
1877 const char *dy_str = qdict_get_str(qdict, "dy_str");
1878 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001879
bellard13224a82006-07-14 22:03:35 +00001880 dx = strtol(dx_str, NULL, 0);
1881 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001882 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1883 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1884
1885 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001886 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001887 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001888 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001889 qemu_input_queue_btn(NULL, button, true);
1890 qemu_input_event_sync();
1891 qemu_input_queue_btn(NULL, button, false);
1892 }
1893 }
1894 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001895}
1896
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001897static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001898{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001899 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001900 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1901 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1902 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1903 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001904 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001905
1906 if (mouse_button_state == button_state) {
1907 return;
1908 }
1909 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1910 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001911 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001912}
1913
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001914static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001915{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001916 int size = qdict_get_int(qdict, "size");
1917 int addr = qdict_get_int(qdict, "addr");
1918 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001919 uint32_t val;
1920 int suffix;
1921
1922 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001923 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001924 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001925 addr++;
1926 }
1927 addr &= 0xffff;
1928
1929 switch(size) {
1930 default:
1931 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001932 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001933 suffix = 'b';
1934 break;
1935 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001936 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001937 suffix = 'w';
1938 break;
1939 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001940 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001941 suffix = 'l';
1942 break;
1943 }
aliguori376253e2009-03-05 23:01:23 +00001944 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1945 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001946}
bellarda3a91a32004-06-04 11:06:21 +00001947
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001948static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001949{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001950 int size = qdict_get_int(qdict, "size");
1951 int addr = qdict_get_int(qdict, "addr");
1952 int val = qdict_get_int(qdict, "val");
1953
Jan Kiszkaf1147842009-07-14 10:20:11 +02001954 addr &= IOPORTS_MASK;
1955
1956 switch (size) {
1957 default:
1958 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001959 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001960 break;
1961 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001962 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001963 break;
1964 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001965 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001966 break;
1967 }
1968}
1969
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001970static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001971{
Gongleif1839932014-12-03 18:20:58 +00001972 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001973 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001974
Gongleif1839932014-12-03 18:20:58 +00001975 qemu_boot_set(bootdevice, &local_err);
1976 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001977 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001978 } else {
Gongleif1839932014-12-03 18:20:58 +00001979 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001980 }
1981}
1982
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001983static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001984{
Peter Xu57bb40c2017-01-16 16:40:05 +08001985 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001986 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10001987 bool owner = qdict_get_try_bool(qdict, "owner", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001988
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10001989 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree,
1990 owner);
Blue Swirl314e2982011-09-11 20:22:05 +00001991}
1992
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001993static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001994{
aliguorib28b6232009-04-22 20:20:29 +00001995 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001996 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001997 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001998
Igor Mammedovf75cd442017-05-30 18:23:59 +02001999 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002000 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
2001
zhanghailiang5b009e42014-11-04 19:49:30 +08002002 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002003 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2004 for (i = 0; i < nb_numa_nodes; i++) {
2005 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02002006 for (cpu = cpu_list; cpu; cpu = cpu->next) {
2007 if (cpu->value->has_props && cpu->value->props->has_node_id &&
2008 cpu->value->props->node_id == i) {
2009 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00002010 }
2011 }
2012 monitor_printf(mon, "\n");
2013 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002014 node_mem[i].node_mem >> 20);
2015 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
2016 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00002017 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02002018 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08002019 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002020}
2021
bellard5f1ce942006-02-08 22:40:15 +00002022#ifdef CONFIG_PROFILER
2023
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002024int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002025int64_t dev_time;
2026
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002027static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002028{
aliguori376253e2009-03-05 23:01:23 +00002029 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302030 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00002031 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302032 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002033 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002034 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002035}
2036#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002037static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002038{
aliguori376253e2009-03-05 23:01:23 +00002039 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002040}
2041#endif
2042
bellardec36b692006-07-16 18:57:03 +00002043/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002044static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002045
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002046static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002047{
2048 int i;
2049 CaptureState *s;
2050
2051 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002052 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002053 s->ops.info (s->opaque);
2054 }
2055}
2056
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002057static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002058{
2059 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002060 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002061 CaptureState *s;
2062
2063 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2064 if (i == n) {
2065 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002066 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002067 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002068 return;
2069 }
2070 }
2071}
2072
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002073static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002074{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002075 const char *path = qdict_get_str(qdict, "path");
2076 int has_freq = qdict_haskey(qdict, "freq");
2077 int freq = qdict_get_try_int(qdict, "freq", -1);
2078 int has_bits = qdict_haskey(qdict, "bits");
2079 int bits = qdict_get_try_int(qdict, "bits", -1);
2080 int has_channels = qdict_haskey(qdict, "nchannels");
2081 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002082 CaptureState *s;
2083
Anthony Liguori7267c092011-08-20 22:09:37 -05002084 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002085
2086 freq = has_freq ? freq : 44100;
2087 bits = has_bits ? bits : 16;
2088 nchannels = has_channels ? nchannels : 2;
2089
2090 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002091 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002092 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002093 return;
bellardec36b692006-07-16 18:57:03 +00002094 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002095 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002096}
bellardec36b692006-07-16 18:57:03 +00002097
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002098static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002099{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002100 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002101
aliguori76655d62009-03-06 20:27:37 +00002102 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002103 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002104 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002105 return acl;
2106}
aliguori76655d62009-03-06 20:27:37 +00002107
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002108static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002109{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002110 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002111 qemu_acl *acl = find_acl(mon, aclname);
2112 qemu_acl_entry *entry;
2113 int i = 0;
2114
2115 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002116 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002117 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002118 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002119 i++;
2120 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002122 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002123 }
2124}
2125
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002126static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002127{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002128 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002129 qemu_acl *acl = find_acl(mon, aclname);
2130
2131 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002132 qemu_acl_reset(acl);
2133 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002134 }
2135}
aliguori76655d62009-03-06 20:27:37 +00002136
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002137static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002138{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002139 const char *aclname = qdict_get_str(qdict, "aclname");
2140 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002141 qemu_acl *acl = find_acl(mon, aclname);
2142
2143 if (acl) {
2144 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002145 acl->defaultDeny = 0;
2146 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002147 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002148 acl->defaultDeny = 1;
2149 monitor_printf(mon, "acl: policy set to 'deny'\n");
2150 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002151 monitor_printf(mon, "acl: unknown policy '%s', "
2152 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002153 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002154 }
2155}
aliguori76655d62009-03-06 20:27:37 +00002156
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002157static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002158{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002159 const char *aclname = qdict_get_str(qdict, "aclname");
2160 const char *match = qdict_get_str(qdict, "match");
2161 const char *policy = qdict_get_str(qdict, "policy");
2162 int has_index = qdict_haskey(qdict, "index");
2163 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002164 qemu_acl *acl = find_acl(mon, aclname);
2165 int deny, ret;
2166
2167 if (acl) {
2168 if (strcmp(policy, "allow") == 0) {
2169 deny = 0;
2170 } else if (strcmp(policy, "deny") == 0) {
2171 deny = 1;
2172 } else {
2173 monitor_printf(mon, "acl: unknown policy '%s', "
2174 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002175 return;
2176 }
aliguori28a76be2009-03-06 20:27:40 +00002177 if (has_index)
2178 ret = qemu_acl_insert(acl, deny, match, index);
2179 else
2180 ret = qemu_acl_append(acl, deny, match);
2181 if (ret < 0)
2182 monitor_printf(mon, "acl: unable to add acl entry\n");
2183 else
2184 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002185 }
2186}
aliguori76655d62009-03-06 20:27:37 +00002187
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002188static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002189{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002190 const char *aclname = qdict_get_str(qdict, "aclname");
2191 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002192 qemu_acl *acl = find_acl(mon, aclname);
2193 int ret;
aliguori76655d62009-03-06 20:27:37 +00002194
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002195 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002196 ret = qemu_acl_remove(acl, match);
2197 if (ret < 0)
2198 monitor_printf(mon, "acl: no matching acl entry\n");
2199 else
2200 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002201 }
2202}
2203
Corey Bryant208c9d12012-06-22 14:36:09 -04002204void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002205{
Anthony Liguoric227f092009-10-01 16:12:16 -05002206 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002207 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002208
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002209 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002210 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002211 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002212 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002213 }
2214
2215 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002216 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002217 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2218 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002219 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002220 }
2221
Peter Xu9409fc02018-06-08 11:55:06 +08002222 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002223 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002224 if (strcmp(monfd->name, fdname) != 0) {
2225 continue;
2226 }
2227
Peter Xu9409fc02018-06-08 11:55:06 +08002228 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002229 monfd->fd = fd;
Peter Xu9409fc02018-06-08 11:55:06 +08002230 qemu_mutex_unlock(&cur_mon->mon_lock);
2231 /* Make sure close() is out of critical section */
2232 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002233 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002234 }
2235
Anthony Liguori7267c092011-08-20 22:09:37 -05002236 monfd = g_malloc0(sizeof(mon_fd_t));
2237 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002238 monfd->fd = fd;
2239
Corey Bryant208c9d12012-06-22 14:36:09 -04002240 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002241 qemu_mutex_unlock(&cur_mon->mon_lock);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242}
2243
Corey Bryant208c9d12012-06-22 14:36:09 -04002244void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002245{
Anthony Liguoric227f092009-10-01 16:12:16 -05002246 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002247 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002248
Peter Xu9409fc02018-06-08 11:55:06 +08002249 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002250 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002251 if (strcmp(monfd->name, fdname) != 0) {
2252 continue;
2253 }
2254
Blue Swirl72cf2d42009-09-12 07:36:22 +00002255 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002256 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05002257 g_free(monfd->name);
2258 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002259 qemu_mutex_unlock(&cur_mon->mon_lock);
2260 /* Make sure close() is out of critical section */
2261 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002262 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002263 }
2264
Peter Xu9409fc02018-06-08 11:55:06 +08002265 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002266 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002267}
2268
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002269int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002270{
Anthony Liguoric227f092009-10-01 16:12:16 -05002271 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002272
Peter Xu9409fc02018-06-08 11:55:06 +08002273 qemu_mutex_lock(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002274 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002275 int fd;
2276
2277 if (strcmp(monfd->name, fdname) != 0) {
2278 continue;
2279 }
2280
2281 fd = monfd->fd;
2282
2283 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002284 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002285 g_free(monfd->name);
2286 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002287 qemu_mutex_unlock(&mon->mon_lock);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002288
2289 return fd;
2290 }
2291
Peter Xu9409fc02018-06-08 11:55:06 +08002292 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002293 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002294 return -1;
2295}
2296
Corey Bryantba1c0482012-08-14 16:43:43 -04002297static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2298{
2299 MonFdsetFd *mon_fdset_fd;
2300 MonFdsetFd *mon_fdset_fd_next;
2301
2302 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002303 if ((mon_fdset_fd->removed ||
2304 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2305 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002306 close(mon_fdset_fd->fd);
2307 g_free(mon_fdset_fd->opaque);
2308 QLIST_REMOVE(mon_fdset_fd, next);
2309 g_free(mon_fdset_fd);
2310 }
2311 }
2312
Corey Bryantadb696f2012-08-14 16:43:47 -04002313 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002314 QLIST_REMOVE(mon_fdset, next);
2315 g_free(mon_fdset);
2316 }
2317}
2318
Corey Bryantefb87c12012-08-14 16:43:48 -04002319static void monitor_fdsets_cleanup(void)
2320{
2321 MonFdset *mon_fdset;
2322 MonFdset *mon_fdset_next;
2323
Peter Xu47451462018-06-08 11:55:11 +08002324 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002325 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2326 monitor_fdset_cleanup(mon_fdset);
2327 }
Peter Xu47451462018-06-08 11:55:11 +08002328 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002329}
2330
Corey Bryantba1c0482012-08-14 16:43:43 -04002331AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2332 const char *opaque, Error **errp)
2333{
2334 int fd;
2335 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002336 AddfdInfo *fdinfo;
2337
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002338 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002339 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002340 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002341 goto error;
2342 }
2343
Corey Bryante446f702012-10-18 15:19:32 -04002344 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2345 has_opaque, opaque, errp);
2346 if (fdinfo) {
2347 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002348 }
2349
Corey Bryantba1c0482012-08-14 16:43:43 -04002350error:
2351 if (fd != -1) {
2352 close(fd);
2353 }
2354 return NULL;
2355}
2356
2357void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2358{
2359 MonFdset *mon_fdset;
2360 MonFdsetFd *mon_fdset_fd;
2361 char fd_str[60];
2362
Peter Xu47451462018-06-08 11:55:11 +08002363 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002364 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2365 if (mon_fdset->id != fdset_id) {
2366 continue;
2367 }
2368 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2369 if (has_fd) {
2370 if (mon_fdset_fd->fd != fd) {
2371 continue;
2372 }
2373 mon_fdset_fd->removed = true;
2374 break;
2375 } else {
2376 mon_fdset_fd->removed = true;
2377 }
2378 }
2379 if (has_fd && !mon_fdset_fd) {
2380 goto error;
2381 }
2382 monitor_fdset_cleanup(mon_fdset);
Peter Xu47451462018-06-08 11:55:11 +08002383 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002384 return;
2385 }
2386
2387error:
Peter Xu47451462018-06-08 11:55:11 +08002388 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002389 if (has_fd) {
2390 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2391 fdset_id, fd);
2392 } else {
2393 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2394 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002395 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002396}
2397
2398FdsetInfoList *qmp_query_fdsets(Error **errp)
2399{
2400 MonFdset *mon_fdset;
2401 MonFdsetFd *mon_fdset_fd;
2402 FdsetInfoList *fdset_list = NULL;
2403
Peter Xu47451462018-06-08 11:55:11 +08002404 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002405 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2406 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2407 FdsetFdInfoList *fdsetfd_list = NULL;
2408
2409 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2410 fdset_info->value->fdset_id = mon_fdset->id;
2411
2412 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2413 FdsetFdInfoList *fdsetfd_info;
2414
2415 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2416 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2417 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2418 if (mon_fdset_fd->opaque) {
2419 fdsetfd_info->value->has_opaque = true;
2420 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2421 } else {
2422 fdsetfd_info->value->has_opaque = false;
2423 }
2424
2425 fdsetfd_info->next = fdsetfd_list;
2426 fdsetfd_list = fdsetfd_info;
2427 }
2428
2429 fdset_info->value->fds = fdsetfd_list;
2430
2431 fdset_info->next = fdset_list;
2432 fdset_list = fdset_info;
2433 }
Peter Xu47451462018-06-08 11:55:11 +08002434 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002435
2436 return fdset_list;
2437}
2438
Corey Bryante446f702012-10-18 15:19:32 -04002439AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2440 bool has_opaque, const char *opaque,
2441 Error **errp)
2442{
2443 MonFdset *mon_fdset = NULL;
2444 MonFdsetFd *mon_fdset_fd;
2445 AddfdInfo *fdinfo;
2446
Peter Xu47451462018-06-08 11:55:11 +08002447 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002448 if (has_fdset_id) {
2449 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2450 /* Break if match found or match impossible due to ordering by ID */
2451 if (fdset_id <= mon_fdset->id) {
2452 if (fdset_id < mon_fdset->id) {
2453 mon_fdset = NULL;
2454 }
2455 break;
2456 }
2457 }
2458 }
2459
2460 if (mon_fdset == NULL) {
2461 int64_t fdset_id_prev = -1;
2462 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2463
2464 if (has_fdset_id) {
2465 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002466 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2467 "a non-negative value");
Peter Xu47451462018-06-08 11:55:11 +08002468 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002469 return NULL;
2470 }
2471 /* Use specified fdset ID */
2472 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2473 mon_fdset_cur = mon_fdset;
2474 if (fdset_id < mon_fdset_cur->id) {
2475 break;
2476 }
2477 }
2478 } else {
2479 /* Use first available fdset ID */
2480 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2481 mon_fdset_cur = mon_fdset;
2482 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2483 fdset_id_prev = mon_fdset_cur->id;
2484 continue;
2485 }
2486 break;
2487 }
2488 }
2489
2490 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2491 if (has_fdset_id) {
2492 mon_fdset->id = fdset_id;
2493 } else {
2494 mon_fdset->id = fdset_id_prev + 1;
2495 }
2496
2497 /* The fdset list is ordered by fdset ID */
2498 if (!mon_fdset_cur) {
2499 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2500 } else if (mon_fdset->id < mon_fdset_cur->id) {
2501 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2502 } else {
2503 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2504 }
2505 }
2506
2507 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2508 mon_fdset_fd->fd = fd;
2509 mon_fdset_fd->removed = false;
2510 if (has_opaque) {
2511 mon_fdset_fd->opaque = g_strdup(opaque);
2512 }
2513 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2514
2515 fdinfo = g_malloc0(sizeof(*fdinfo));
2516 fdinfo->fdset_id = mon_fdset->id;
2517 fdinfo->fd = mon_fdset_fd->fd;
2518
Peter Xu47451462018-06-08 11:55:11 +08002519 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002520 return fdinfo;
2521}
2522
Corey Bryantadb696f2012-08-14 16:43:47 -04002523int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2524{
Peter Xu47451462018-06-08 11:55:11 +08002525#ifdef _WIN32
2526 return -ENOENT;
2527#else
Corey Bryantadb696f2012-08-14 16:43:47 -04002528 MonFdset *mon_fdset;
2529 MonFdsetFd *mon_fdset_fd;
2530 int mon_fd_flags;
Peter Xu47451462018-06-08 11:55:11 +08002531 int ret;
Corey Bryantadb696f2012-08-14 16:43:47 -04002532
Peter Xu47451462018-06-08 11:55:11 +08002533 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002534 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2535 if (mon_fdset->id != fdset_id) {
2536 continue;
2537 }
2538 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2539 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2540 if (mon_fd_flags == -1) {
Peter Xu47451462018-06-08 11:55:11 +08002541 ret = -errno;
2542 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002543 }
2544
2545 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Peter Xu47451462018-06-08 11:55:11 +08002546 ret = mon_fdset_fd->fd;
2547 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002548 }
2549 }
Peter Xu47451462018-06-08 11:55:11 +08002550 ret = -EACCES;
2551 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002552 }
Peter Xu47451462018-06-08 11:55:11 +08002553 ret = -ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04002554
Peter Xu47451462018-06-08 11:55:11 +08002555out:
2556 qemu_mutex_unlock(&mon_fdsets_lock);
2557 return ret;
2558#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04002559}
2560
2561int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2562{
2563 MonFdset *mon_fdset;
2564 MonFdsetFd *mon_fdset_fd_dup;
2565
Peter Xu47451462018-06-08 11:55:11 +08002566 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002567 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2568 if (mon_fdset->id != fdset_id) {
2569 continue;
2570 }
2571 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2572 if (mon_fdset_fd_dup->fd == dup_fd) {
Peter Xu47451462018-06-08 11:55:11 +08002573 goto err;
Corey Bryantadb696f2012-08-14 16:43:47 -04002574 }
2575 }
2576 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2577 mon_fdset_fd_dup->fd = dup_fd;
2578 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Peter Xu47451462018-06-08 11:55:11 +08002579 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002580 return 0;
2581 }
Peter Xu47451462018-06-08 11:55:11 +08002582
2583err:
2584 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002585 return -1;
2586}
2587
2588static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2589{
2590 MonFdset *mon_fdset;
2591 MonFdsetFd *mon_fdset_fd_dup;
2592
Peter Xu47451462018-06-08 11:55:11 +08002593 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002594 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2595 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2596 if (mon_fdset_fd_dup->fd == dup_fd) {
2597 if (remove) {
2598 QLIST_REMOVE(mon_fdset_fd_dup, next);
2599 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2600 monitor_fdset_cleanup(mon_fdset);
2601 }
Peter Xu47451462018-06-08 11:55:11 +08002602 goto err;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002603 } else {
Peter Xu47451462018-06-08 11:55:11 +08002604 qemu_mutex_unlock(&mon_fdsets_lock);
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002605 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002606 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002607 }
2608 }
2609 }
Peter Xu47451462018-06-08 11:55:11 +08002610
2611err:
2612 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002613 return -1;
2614}
2615
2616int monitor_fdset_dup_fd_find(int dup_fd)
2617{
2618 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2619}
2620
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002621void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002622{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002623 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002624}
2625
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002626int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002627{
2628 int fd;
2629 Error *local_err = NULL;
2630
2631 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002632 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002633 } else {
2634 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002635 if (fd == -1) {
2636 error_setg(&local_err, "Invalid file descriptor number '%s'",
2637 fdname);
2638 }
2639 }
2640 if (local_err) {
2641 error_propagate(errp, local_err);
2642 assert(fd == -1);
2643 } else {
2644 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002645 }
2646
2647 return fd;
2648}
2649
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002650/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002651static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002652#include "hmp-commands-info.h"
2653 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002654};
2655
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002656/* mon_cmds and info_cmds would be sorted at runtime */
2657static mon_cmd_t mon_cmds[] = {
2658#include "hmp-commands.h"
2659 { NULL, NULL, },
2660};
2661
bellard9307c4c2004-04-04 12:57:25 +00002662/*******************************************************************/
2663
2664static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002665static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002666
bellard9307c4c2004-04-04 12:57:25 +00002667
Stefan Weil9c3175c2013-08-22 21:30:09 +02002668static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2669expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002670{
Fam Zheng277acfe2013-08-20 10:58:21 +08002671 va_list ap;
2672 va_start(ap, fmt);
2673 monitor_vprintf(mon, fmt, ap);
2674 monitor_printf(mon, "\n");
2675 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002676 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002677}
2678
Markus Armbruster09b94182010-01-20 13:07:30 +01002679/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002680static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002681{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002682 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002683 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002684 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002685 uint64_t tmp = 0;
2686 int ret;
bellard92a31b12005-02-10 22:00:52 +00002687
Thomas Huth854e67f2017-01-13 13:12:35 +01002688 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002689 return -1;
2690 }
2691
2692 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002693 if (compare_cmd(name, md->name)) {
2694 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002695 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002696 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002697 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002698 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002699 switch(md->type) {
2700 case MD_I32:
2701 *pval = *(int32_t *)ptr;
2702 break;
2703 case MD_TLONG:
2704 *pval = *(target_long *)ptr;
2705 break;
2706 default:
2707 *pval = 0;
2708 break;
2709 }
bellard9307c4c2004-04-04 12:57:25 +00002710 }
2711 return 0;
2712 }
2713 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002714
Thomas Huth854e67f2017-01-13 13:12:35 +01002715 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002716 if (!ret) {
2717 *pval = (target_long) tmp;
2718 }
2719
2720 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002721}
2722
2723static void next(void)
2724{
Blue Swirl660f11b2009-07-31 21:16:51 +00002725 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002726 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002727 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002728 pch++;
2729 }
2730}
2731
aliguori376253e2009-03-05 23:01:23 +00002732static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002733
aliguori376253e2009-03-05 23:01:23 +00002734static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002735{
blueswir1c2efc952007-09-25 17:28:42 +00002736 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002737 char *p;
bellard6a00d602005-11-21 23:25:50 +00002738 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002739
2740 switch(*pch) {
2741 case '+':
2742 next();
aliguori376253e2009-03-05 23:01:23 +00002743 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002744 break;
2745 case '-':
2746 next();
aliguori376253e2009-03-05 23:01:23 +00002747 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002748 break;
2749 case '~':
2750 next();
aliguori376253e2009-03-05 23:01:23 +00002751 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002752 break;
2753 case '(':
2754 next();
aliguori376253e2009-03-05 23:01:23 +00002755 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002756 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002757 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002758 }
2759 next();
2760 break;
bellard81d09122004-07-14 17:21:37 +00002761 case '\'':
2762 pch++;
2763 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002764 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002765 n = *pch;
2766 pch++;
2767 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002768 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002769 next();
2770 break;
bellard9307c4c2004-04-04 12:57:25 +00002771 case '$':
2772 {
2773 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002774 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002775
bellard9307c4c2004-04-04 12:57:25 +00002776 pch++;
2777 q = buf;
2778 while ((*pch >= 'a' && *pch <= 'z') ||
2779 (*pch >= 'A' && *pch <= 'Z') ||
2780 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002781 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002782 if ((q - buf) < sizeof(buf) - 1)
2783 *q++ = *pch;
2784 pch++;
2785 }
blueswir1cd390082008-11-16 13:53:32 +00002786 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002787 pch++;
2788 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002789 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002790 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002791 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002792 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002793 }
2794 break;
2795 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002796 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002797 n = 0;
2798 break;
2799 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002800 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002801 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002802 if (errno == ERANGE) {
2803 expr_error(mon, "number too large");
2804 }
bellard9307c4c2004-04-04 12:57:25 +00002805 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002806 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002807 }
2808 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002809 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002810 pch++;
2811 break;
2812 }
2813 return n;
2814}
2815
2816
aliguori376253e2009-03-05 23:01:23 +00002817static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002818{
blueswir1c2efc952007-09-25 17:28:42 +00002819 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002820 int op;
ths3b46e622007-09-17 08:09:54 +00002821
aliguori376253e2009-03-05 23:01:23 +00002822 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002823 for(;;) {
2824 op = *pch;
2825 if (op != '*' && op != '/' && op != '%')
2826 break;
2827 next();
aliguori376253e2009-03-05 23:01:23 +00002828 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002829 switch(op) {
2830 default:
2831 case '*':
2832 val *= val2;
2833 break;
2834 case '/':
2835 case '%':
ths5fafdf22007-09-16 21:08:06 +00002836 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002837 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002838 if (op == '/')
2839 val /= val2;
2840 else
2841 val %= val2;
2842 break;
2843 }
2844 }
2845 return val;
2846}
2847
aliguori376253e2009-03-05 23:01:23 +00002848static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002849{
blueswir1c2efc952007-09-25 17:28:42 +00002850 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002851 int op;
bellard9307c4c2004-04-04 12:57:25 +00002852
aliguori376253e2009-03-05 23:01:23 +00002853 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002854 for(;;) {
2855 op = *pch;
2856 if (op != '&' && op != '|' && op != '^')
2857 break;
2858 next();
aliguori376253e2009-03-05 23:01:23 +00002859 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002860 switch(op) {
2861 default:
2862 case '&':
2863 val &= val2;
2864 break;
2865 case '|':
2866 val |= val2;
2867 break;
2868 case '^':
2869 val ^= val2;
2870 break;
2871 }
2872 }
2873 return val;
2874}
2875
aliguori376253e2009-03-05 23:01:23 +00002876static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002877{
blueswir1c2efc952007-09-25 17:28:42 +00002878 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002879 int op;
bellard9307c4c2004-04-04 12:57:25 +00002880
aliguori376253e2009-03-05 23:01:23 +00002881 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002882 for(;;) {
2883 op = *pch;
2884 if (op != '+' && op != '-')
2885 break;
2886 next();
aliguori376253e2009-03-05 23:01:23 +00002887 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002888 if (op == '+')
2889 val += val2;
2890 else
2891 val -= val2;
2892 }
2893 return val;
2894}
2895
aliguori376253e2009-03-05 23:01:23 +00002896static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002897{
2898 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002899 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002900 *pp = pch;
2901 return -1;
2902 }
blueswir1cd390082008-11-16 13:53:32 +00002903 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002904 pch++;
aliguori376253e2009-03-05 23:01:23 +00002905 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002906 *pp = pch;
2907 return 0;
2908}
2909
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002910static int get_double(Monitor *mon, double *pval, const char **pp)
2911{
2912 const char *p = *pp;
2913 char *tailp;
2914 double d;
2915
2916 d = strtod(p, &tailp);
2917 if (tailp == p) {
2918 monitor_printf(mon, "Number expected\n");
2919 return -1;
2920 }
2921 if (d != d || d - d != 0) {
2922 /* NaN or infinity */
2923 monitor_printf(mon, "Bad number\n");
2924 return -1;
2925 }
2926 *pval = d;
2927 *pp = tailp;
2928 return 0;
2929}
2930
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002931/*
2932 * Store the command-name in cmdname, and return a pointer to
2933 * the remaining of the command string.
2934 */
2935static const char *get_command_name(const char *cmdline,
2936 char *cmdname, size_t nlen)
2937{
2938 size_t len;
2939 const char *p, *pstart;
2940
2941 p = cmdline;
2942 while (qemu_isspace(*p))
2943 p++;
2944 if (*p == '\0')
2945 return NULL;
2946 pstart = p;
2947 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2948 p++;
2949 len = p - pstart;
2950 if (len > nlen - 1)
2951 len = nlen - 1;
2952 memcpy(cmdname, pstart, len);
2953 cmdname[len] = '\0';
2954 return p;
2955}
2956
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002957/**
2958 * Read key of 'type' into 'key' and return the current
2959 * 'type' pointer.
2960 */
2961static char *key_get_info(const char *type, char **key)
2962{
2963 size_t len;
2964 char *p, *str;
2965
2966 if (*type == ',')
2967 type++;
2968
2969 p = strchr(type, ':');
2970 if (!p) {
2971 *key = NULL;
2972 return NULL;
2973 }
2974 len = p - type;
2975
Anthony Liguori7267c092011-08-20 22:09:37 -05002976 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002977 memcpy(str, type, len);
2978 str[len] = '\0';
2979
2980 *key = str;
2981 return ++p;
2982}
2983
bellard9307c4c2004-04-04 12:57:25 +00002984static int default_fmt_format = 'x';
2985static int default_fmt_size = 4;
2986
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002987static int is_valid_option(const char *c, const char *typestr)
2988{
2989 char option[3];
2990
2991 option[0] = '-';
2992 option[1] = *c;
2993 option[2] = '\0';
2994
2995 typestr = strstr(typestr, option);
2996 return (typestr != NULL);
2997}
2998
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002999static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3000 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003001{
3002 const mon_cmd_t *cmd;
3003
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003004 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003005 if (compare_cmd(cmdname, cmd->name)) {
3006 return cmd;
3007 }
3008 }
3009
3010 return NULL;
3011}
3012
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003013/*
Bandan Dasae502122015-06-03 18:38:08 -04003014 * Parse command name from @cmdp according to command table @table.
3015 * If blank, return NULL.
3016 * Else, if no valid command can be found, report to @mon, and return
3017 * NULL.
3018 * Else, change @cmdp to point right behind the name, and return its
3019 * command table entry.
3020 * Do not assume the return value points into @table! It doesn't when
3021 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003022 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003023static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003024 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04003025 const char **cmdp,
3026 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003027{
Bandan Dasae502122015-06-03 18:38:08 -04003028 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003029 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003030 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003031
bellard9307c4c2004-04-04 12:57:25 +00003032 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003033 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003034 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003035 return NULL;
ths3b46e622007-09-17 08:09:54 +00003036
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003037 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003038 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003039 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003040 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003041 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003042 }
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +01003043 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
3044 monitor_printf(mon, "Command '%.*s' not available with -preconfig "
3045 "until after exit_preconfig.\n",
3046 (int)(p - cmdp_start), cmdp_start);
3047 return NULL;
3048 }
bellard9307c4c2004-04-04 12:57:25 +00003049
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003050 /* filter out following useless space */
3051 while (qemu_isspace(*p)) {
3052 p++;
3053 }
Bandan Dasae502122015-06-03 18:38:08 -04003054
3055 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003056 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003057 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003058 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003059 }
3060
Bandan Dasae502122015-06-03 18:38:08 -04003061 return cmd;
3062}
3063
3064/*
3065 * Parse arguments for @cmd.
3066 * If it can't be parsed, report to @mon, and return NULL.
3067 * Else, insert command arguments into a QDict, and return it.
3068 * Note: On success, caller has to free the QDict structure.
3069 */
3070
3071static QDict *monitor_parse_arguments(Monitor *mon,
3072 const char **endp,
3073 const mon_cmd_t *cmd)
3074{
3075 const char *typestr;
3076 char *key;
3077 int c;
3078 const char *p = *endp;
3079 char buf[1024];
3080 QDict *qdict = qdict_new();
3081
bellard9307c4c2004-04-04 12:57:25 +00003082 /* parse the parameters */
3083 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003084 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003085 typestr = key_get_info(typestr, &key);
3086 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003087 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003088 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003089 typestr++;
3090 switch(c) {
3091 case 'F':
bellard81d09122004-07-14 17:21:37 +00003092 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003093 case 's':
3094 {
3095 int ret;
ths3b46e622007-09-17 08:09:54 +00003096
blueswir1cd390082008-11-16 13:53:32 +00003097 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003098 p++;
3099 if (*typestr == '?') {
3100 typestr++;
3101 if (*p == '\0') {
3102 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003103 break;
bellard9307c4c2004-04-04 12:57:25 +00003104 }
3105 }
3106 ret = get_str(buf, sizeof(buf), &p);
3107 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003108 switch(c) {
3109 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003110 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003111 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003112 break;
3113 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003114 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003115 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003116 break;
3117 default:
Bandan Dasae502122015-06-03 18:38:08 -04003118 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003119 break;
3120 }
bellard9307c4c2004-04-04 12:57:25 +00003121 goto fail;
3122 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003123 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00003124 }
3125 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003126 case 'O':
3127 {
3128 QemuOptsList *opts_list;
3129 QemuOpts *opts;
3130
3131 opts_list = qemu_find_opts(key);
3132 if (!opts_list || opts_list->desc->name) {
3133 goto bad_type;
3134 }
3135 while (qemu_isspace(*p)) {
3136 p++;
3137 }
3138 if (!*p)
3139 break;
3140 if (get_str(buf, sizeof(buf), &p) < 0) {
3141 goto fail;
3142 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003143 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003144 if (!opts) {
3145 goto fail;
3146 }
3147 qemu_opts_to_qdict(opts, qdict);
3148 qemu_opts_del(opts);
3149 }
3150 break;
bellard9307c4c2004-04-04 12:57:25 +00003151 case '/':
3152 {
3153 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003154
blueswir1cd390082008-11-16 13:53:32 +00003155 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003156 p++;
3157 if (*p == '/') {
3158 /* format found */
3159 p++;
3160 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003161 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003162 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003163 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003164 count = count * 10 + (*p - '0');
3165 p++;
3166 }
3167 }
3168 size = -1;
3169 format = -1;
3170 for(;;) {
3171 switch(*p) {
3172 case 'o':
3173 case 'd':
3174 case 'u':
3175 case 'x':
3176 case 'i':
3177 case 'c':
3178 format = *p++;
3179 break;
3180 case 'b':
3181 size = 1;
3182 p++;
3183 break;
3184 case 'h':
3185 size = 2;
3186 p++;
3187 break;
3188 case 'w':
3189 size = 4;
3190 p++;
3191 break;
3192 case 'g':
3193 case 'L':
3194 size = 8;
3195 p++;
3196 break;
3197 default:
3198 goto next;
3199 }
3200 }
3201 next:
blueswir1cd390082008-11-16 13:53:32 +00003202 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003203 monitor_printf(mon, "invalid char in format: '%c'\n",
3204 *p);
bellard9307c4c2004-04-04 12:57:25 +00003205 goto fail;
3206 }
bellard9307c4c2004-04-04 12:57:25 +00003207 if (format < 0)
3208 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003209 if (format != 'i') {
3210 /* for 'i', not specifying a size gives -1 as size */
3211 if (size < 0)
3212 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003213 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003214 }
bellard9307c4c2004-04-04 12:57:25 +00003215 default_fmt_format = format;
3216 } else {
3217 count = 1;
3218 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003219 if (format != 'i') {
3220 size = default_fmt_size;
3221 } else {
3222 size = -1;
3223 }
bellard9307c4c2004-04-04 12:57:25 +00003224 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003225 qdict_put_int(qdict, "count", count);
3226 qdict_put_int(qdict, "format", format);
3227 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00003228 }
3229 break;
3230 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003231 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003232 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003233 {
blueswir1c2efc952007-09-25 17:28:42 +00003234 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003235
blueswir1cd390082008-11-16 13:53:32 +00003236 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003237 p++;
bellard34405572004-06-08 00:55:58 +00003238 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003239 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003240 if (*p == '\0') {
3241 typestr++;
3242 break;
3243 }
bellard34405572004-06-08 00:55:58 +00003244 } else {
3245 if (*p == '.') {
3246 p++;
blueswir1cd390082008-11-16 13:53:32 +00003247 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003248 p++;
bellard34405572004-06-08 00:55:58 +00003249 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003250 typestr++;
3251 break;
bellard34405572004-06-08 00:55:58 +00003252 }
3253 }
bellard13224a82006-07-14 22:03:35 +00003254 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003255 }
aliguori376253e2009-03-05 23:01:23 +00003256 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003257 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003258 /* Check if 'i' is greater than 32-bit */
3259 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003260 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003261 monitor_printf(mon, "integer is for 32-bit values\n");
3262 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003263 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003264 if (val < 0) {
3265 monitor_printf(mon, "enter a positive value\n");
3266 goto fail;
3267 }
Philippe Mathieu-Daudé8ec338a2018-06-25 09:42:34 -03003268 val *= MiB;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003269 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003270 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00003271 }
3272 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003273 case 'o':
3274 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003275 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003276 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003277 char *end;
3278
3279 while (qemu_isspace(*p)) {
3280 p++;
3281 }
3282 if (*typestr == '?') {
3283 typestr++;
3284 if (*p == '\0') {
3285 break;
3286 }
3287 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003288 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003289 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02003290 monitor_printf(mon, "invalid size\n");
3291 goto fail;
3292 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003293 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02003294 p = end;
3295 }
3296 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003297 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003298 {
3299 double val;
3300
3301 while (qemu_isspace(*p))
3302 p++;
3303 if (*typestr == '?') {
3304 typestr++;
3305 if (*p == '\0') {
3306 break;
3307 }
3308 }
3309 if (get_double(mon, &val, &p) < 0) {
3310 goto fail;
3311 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003312 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003313 switch (*p) {
3314 case 'm':
3315 val /= 1e3; p += 2; break;
3316 case 'u':
3317 val /= 1e6; p += 2; break;
3318 case 'n':
3319 val /= 1e9; p += 2; break;
3320 }
3321 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003322 if (*p && !qemu_isspace(*p)) {
3323 monitor_printf(mon, "Unknown unit suffix\n");
3324 goto fail;
3325 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04003326 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003327 }
3328 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003329 case 'b':
3330 {
3331 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003332 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003333
3334 while (qemu_isspace(*p)) {
3335 p++;
3336 }
3337 beg = p;
3338 while (qemu_isgraph(*p)) {
3339 p++;
3340 }
3341 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003342 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003343 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003344 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003345 } else {
3346 monitor_printf(mon, "Expected 'on' or 'off'\n");
3347 goto fail;
3348 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003349 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003350 }
3351 break;
bellard9307c4c2004-04-04 12:57:25 +00003352 case '-':
3353 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003354 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003355 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003356 /* option */
ths3b46e622007-09-17 08:09:54 +00003357
bellard9307c4c2004-04-04 12:57:25 +00003358 c = *typestr++;
3359 if (c == '\0')
3360 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003361 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003362 p++;
bellard9307c4c2004-04-04 12:57:25 +00003363 if (*p == '-') {
3364 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003365 if(c != *p) {
3366 if(!is_valid_option(p, typestr)) {
3367
3368 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003369 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003370 goto fail;
3371 } else {
3372 skip_key = 1;
3373 }
bellard9307c4c2004-04-04 12:57:25 +00003374 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003375 if(skip_key) {
3376 p = tmp;
3377 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003378 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003379 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003380 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003381 }
bellard9307c4c2004-04-04 12:57:25 +00003382 }
bellard9307c4c2004-04-04 12:57:25 +00003383 }
3384 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003385 case 'S':
3386 {
3387 /* package all remaining string */
3388 int len;
3389
3390 while (qemu_isspace(*p)) {
3391 p++;
3392 }
3393 if (*typestr == '?') {
3394 typestr++;
3395 if (*p == '\0') {
3396 /* no remaining string: NULL argument */
3397 break;
3398 }
3399 }
3400 len = strlen(p);
3401 if (len <= 0) {
3402 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003403 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003404 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003405 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003406 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003407 p += len;
3408 }
3409 break;
bellard9307c4c2004-04-04 12:57:25 +00003410 default:
3411 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003412 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003413 goto fail;
3414 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003415 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003416 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003417 }
3418 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003419 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003420 p++;
3421 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003422 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003423 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003424 goto fail;
3425 }
3426
Bandan Dasae502122015-06-03 18:38:08 -04003427 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003428
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003429fail:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003430 qobject_unref(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003431 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003432 return NULL;
3433}
3434
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003435static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003436{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003437 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003438 const mon_cmd_t *cmd;
Collin Walling317c52c2018-05-07 10:30:54 -04003439 const char *cmd_start = cmdline;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003440
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003441 trace_handle_hmp_command(mon, cmdline);
3442
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003443 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003444 if (!cmd) {
3445 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003446 }
3447
Bandan Dasae502122015-06-03 18:38:08 -04003448 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3449 if (!qdict) {
Collin Walling317c52c2018-05-07 10:30:54 -04003450 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
3451 cmdline--;
3452 }
3453 monitor_printf(mon, "Try \"help %.*s\" for more information\n",
3454 (int)(cmdline - cmd_start), cmd_start);
Bandan Dasae502122015-06-03 18:38:08 -04003455 return;
3456 }
3457
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003458 cmd->cmd(mon, qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003459 qobject_unref(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003460}
3461
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003462static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003463{
3464 const char *p, *pstart;
3465 char cmd[128];
3466 int len;
3467
3468 p = list;
3469 for(;;) {
3470 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +02003471 p = qemu_strchrnul(p, '|');
bellard81d09122004-07-14 17:21:37 +00003472 len = p - pstart;
3473 if (len > sizeof(cmd) - 2)
3474 len = sizeof(cmd) - 2;
3475 memcpy(cmd, pstart, len);
3476 cmd[len] = '\0';
3477 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003478 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003479 }
3480 if (*p == '\0')
3481 break;
3482 p++;
3483 }
3484}
3485
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003486static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003487{
3488 DIR *ffs;
3489 struct dirent *d;
3490 char path[1024];
3491 char file[1024], file_prefix[1024];
3492 int input_path_len;
3493 const char *p;
3494
ths5fafdf22007-09-16 21:08:06 +00003495 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003496 if (!p) {
3497 input_path_len = 0;
3498 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003499 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003500 } else {
3501 input_path_len = p - input + 1;
3502 memcpy(path, input, input_path_len);
3503 if (input_path_len > sizeof(path) - 1)
3504 input_path_len = sizeof(path) - 1;
3505 path[input_path_len] = '\0';
3506 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3507 }
Bandan Das19f2db52015-06-03 18:38:07 -04003508
bellard81d09122004-07-14 17:21:37 +00003509 ffs = opendir(path);
3510 if (!ffs)
3511 return;
3512 for(;;) {
3513 struct stat sb;
3514 d = readdir(ffs);
3515 if (!d)
3516 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003517
3518 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3519 continue;
3520 }
3521
bellard81d09122004-07-14 17:21:37 +00003522 if (strstart(d->d_name, file_prefix, NULL)) {
3523 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003524 if (input_path_len < sizeof(file))
3525 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3526 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003527 /* stat the file to find out if it's a directory.
3528 * In that case add a slash to speed up typing long paths
3529 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003530 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003531 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003532 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003533 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003534 }
3535 }
3536 closedir(ffs);
3537}
3538
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003539static const char *next_arg_type(const char *typestr)
3540{
3541 const char *p = strchr(typestr, ':');
3542 return (p != NULL ? ++p : typestr);
3543}
3544
Hani Benhabiles40d19392014-05-07 23:41:30 +01003545static void add_completion_option(ReadLineState *rs, const char *str,
3546 const char *option)
3547{
3548 if (!str || !option) {
3549 return;
3550 }
3551 if (!strncmp(option, str, strlen(str))) {
3552 readline_add_completion(rs, option);
3553 }
3554}
3555
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003556void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3557{
3558 size_t len;
3559 ChardevBackendInfoList *list, *start;
3560
3561 if (nb_args != 2) {
3562 return;
3563 }
3564 len = strlen(str);
3565 readline_set_completion_index(rs, len);
3566
3567 start = list = qmp_query_chardev_backends(NULL);
3568 while (list) {
3569 const char *chr_name = list->value->name;
3570
3571 if (!strncmp(chr_name, str, len)) {
3572 readline_add_completion(rs, chr_name);
3573 }
3574 list = list->next;
3575 }
3576 qapi_free_ChardevBackendInfoList(start);
3577}
3578
Hani Benhabilesb162b492014-05-07 23:41:31 +01003579void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3580{
3581 size_t len;
3582 int i;
3583
3584 if (nb_args != 2) {
3585 return;
3586 }
3587 len = strlen(str);
3588 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003589 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003590 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003591 }
3592}
3593
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003594void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003595{
3596 GSList *list, *elt;
3597 size_t len;
3598
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003599 if (nb_args != 2) {
3600 return;
3601 }
3602
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003603 len = strlen(str);
3604 readline_set_completion_index(rs, len);
3605 list = elt = object_class_get_list(TYPE_DEVICE, false);
3606 while (elt) {
3607 const char *name;
3608 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3609 TYPE_DEVICE);
3610 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003611
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003612 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003613 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003614 readline_add_completion(rs, name);
3615 }
3616 elt = elt->next;
3617 }
3618 g_slist_free(list);
3619}
3620
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003621void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003622{
3623 GSList *list, *elt;
3624 size_t len;
3625
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003626 if (nb_args != 2) {
3627 return;
3628 }
3629
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003630 len = strlen(str);
3631 readline_set_completion_index(rs, len);
3632 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3633 while (elt) {
3634 const char *name;
3635
3636 name = object_class_get_name(OBJECT_CLASS(elt->data));
3637 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3638 readline_add_completion(rs, name);
3639 }
3640 elt = elt->next;
3641 }
3642 g_slist_free(list);
3643}
3644
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003645static void peripheral_device_del_completion(ReadLineState *rs,
3646 const char *str, size_t len)
3647{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003648 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3649 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003650
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003651 list = qdev_build_hotpluggable_device_list(peripheral);
3652 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003653 return;
3654 }
3655
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003656 for (item = list; item; item = g_slist_next(item)) {
3657 DeviceState *dev = item->data;
3658
3659 if (dev->id && !strncmp(str, dev->id, len)) {
3660 readline_add_completion(rs, dev->id);
3661 }
3662 }
3663
3664 g_slist_free(list);
3665}
3666
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003667void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3668{
3669 size_t len;
3670 ChardevInfoList *list, *start;
3671
3672 if (nb_args != 2) {
3673 return;
3674 }
3675 len = strlen(str);
3676 readline_set_completion_index(rs, len);
3677
3678 start = list = qmp_query_chardev(NULL);
3679 while (list) {
3680 ChardevInfo *chr = list->value;
3681
3682 if (!strncmp(chr->label, str, len)) {
3683 readline_add_completion(rs, chr->label);
3684 }
3685 list = list->next;
3686 }
3687 qapi_free_ChardevInfoList(start);
3688}
3689
Hani Benhabiles8e597772014-05-27 23:39:30 +01003690static void ringbuf_completion(ReadLineState *rs, const char *str)
3691{
3692 size_t len;
3693 ChardevInfoList *list, *start;
3694
3695 len = strlen(str);
3696 readline_set_completion_index(rs, len);
3697
3698 start = list = qmp_query_chardev(NULL);
3699 while (list) {
3700 ChardevInfo *chr_info = list->value;
3701
3702 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003703 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003704 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003705 readline_add_completion(rs, chr_info->label);
3706 }
3707 }
3708 list = list->next;
3709 }
3710 qapi_free_ChardevInfoList(start);
3711}
3712
Hani Benhabiles8e597772014-05-27 23:39:30 +01003713void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3714{
3715 if (nb_args != 2) {
3716 return;
3717 }
3718 ringbuf_completion(rs, str);
3719}
3720
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003721void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3722{
3723 size_t len;
3724
3725 if (nb_args != 2) {
3726 return;
3727 }
3728
3729 len = strlen(str);
3730 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003731 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003732}
3733
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003734void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003735{
3736 ObjectPropertyInfoList *list, *start;
3737 size_t len;
3738
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003739 if (nb_args != 2) {
3740 return;
3741 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003742 len = strlen(str);
3743 readline_set_completion_index(rs, len);
3744
3745 start = list = qmp_qom_list("/objects", NULL);
3746 while (list) {
3747 ObjectPropertyInfo *info = list->value;
3748
3749 if (!strncmp(info->type, "child<", 5)
3750 && !strncmp(info->name, str, len)) {
3751 readline_add_completion(rs, info->name);
3752 }
3753 list = list->next;
3754 }
3755 qapi_free_ObjectPropertyInfoList(start);
3756}
3757
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003758void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3759{
3760 int i;
3761 char *sep;
3762 size_t len;
3763
3764 if (nb_args != 2) {
3765 return;
3766 }
3767 sep = strrchr(str, '-');
3768 if (sep) {
3769 str = sep + 1;
3770 }
3771 len = strlen(str);
3772 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003773 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003774 if (!strncmp(str, QKeyCode_str(i), len)) {
3775 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003776 }
3777 }
3778}
3779
Hani Benhabiles40d19392014-05-07 23:41:30 +01003780void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3781{
3782 size_t len;
3783
3784 len = strlen(str);
3785 readline_set_completion_index(rs, len);
3786 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003787 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003788 int count, i;
3789 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003790 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003791 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003792 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003793 const char *name = ncs[i]->name;
3794 if (!strncmp(str, name, len)) {
3795 readline_add_completion(rs, name);
3796 }
3797 }
3798 } else if (nb_args == 3) {
3799 add_completion_option(rs, str, "on");
3800 add_completion_option(rs, str, "off");
3801 }
3802}
3803
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003804void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3805{
3806 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003807 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003808
3809 if (nb_args != 2) {
3810 return;
3811 }
3812
3813 len = strlen(str);
3814 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003815 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003816 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003817 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003818 QemuOpts *opts;
3819 const char *name = ncs[i]->name;
3820 if (strncmp(str, name, len)) {
3821 continue;
3822 }
3823 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3824 if (opts) {
3825 readline_add_completion(rs, name);
3826 }
3827 }
3828}
3829
Lluís Vilanovabd712112016-07-11 12:53:51 +02003830void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3831{
3832 size_t len;
3833
3834 len = strlen(str);
3835 readline_set_completion_index(rs, len);
3836 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003837 TraceEventIter iter;
3838 TraceEvent *ev;
3839 char *pattern = g_strdup_printf("%s*", str);
3840 trace_event_iter_init(&iter, pattern);
3841 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3842 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003843 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003844 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003845 }
3846}
3847
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003848void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3849{
3850 size_t len;
3851
3852 len = strlen(str);
3853 readline_set_completion_index(rs, len);
3854 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003855 TraceEventIter iter;
3856 TraceEvent *ev;
3857 char *pattern = g_strdup_printf("%s*", str);
3858 trace_event_iter_init(&iter, pattern);
3859 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3860 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003861 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003862 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003863 } else if (nb_args == 3) {
3864 add_completion_option(rs, str, "on");
3865 add_completion_option(rs, str, "off");
3866 }
3867}
3868
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003869void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3870{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003871 int i;
3872
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003873 if (nb_args != 2) {
3874 return;
3875 }
3876 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003877 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3878 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003879 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003880}
3881
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003882void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3883 const char *str)
3884{
3885 size_t len;
3886
3887 len = strlen(str);
3888 readline_set_completion_index(rs, len);
3889 if (nb_args == 2) {
3890 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003891 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003892 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003893 if (!strncmp(str, name, len)) {
3894 readline_add_completion(rs, name);
3895 }
3896 }
3897 } else if (nb_args == 3) {
3898 add_completion_option(rs, str, "on");
3899 add_completion_option(rs, str, "off");
3900 }
3901}
3902
Liang Li50e9a622015-03-23 16:32:29 +08003903void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3904 const char *str)
3905{
3906 size_t len;
3907
3908 len = strlen(str);
3909 readline_set_completion_index(rs, len);
3910 if (nb_args == 2) {
3911 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003912 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003913 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003914 if (!strncmp(str, name, len)) {
3915 readline_add_completion(rs, name);
3916 }
3917 }
3918 }
3919}
3920
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003921static void vm_completion(ReadLineState *rs, const char *str)
3922{
3923 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003924 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003925 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003926
3927 len = strlen(str);
3928 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003929
Kevin Wolf88be7b42016-05-20 18:49:07 +02003930 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003931 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003932 AioContext *ctx = bdrv_get_aio_context(bs);
3933 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003934
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003935 aio_context_acquire(ctx);
3936 if (bdrv_can_snapshot(bs)) {
3937 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3938 }
3939 aio_context_release(ctx);
3940 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003941 continue;
3942 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003943
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003944 snapshot = snapshots;
3945 while (snapshot) {
3946 char *completion = snapshot->value->name;
3947 if (!strncmp(str, completion, len)) {
3948 readline_add_completion(rs, completion);
3949 }
3950 completion = snapshot->value->id;
3951 if (!strncmp(str, completion, len)) {
3952 readline_add_completion(rs, completion);
3953 }
3954 snapshot = snapshot->next;
3955 }
3956 qapi_free_SnapshotInfoList(snapshots);
3957 }
3958
3959}
3960
3961void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3962{
3963 if (nb_args == 2) {
3964 vm_completion(rs, str);
3965 }
3966}
3967
3968void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3969{
3970 if (nb_args == 2) {
3971 vm_completion(rs, str);
3972 }
3973}
3974
Wenchao Xiac35b6402013-08-27 20:38:24 +08003975static void monitor_find_completion_by_table(Monitor *mon,
3976 const mon_cmd_t *cmd_table,
3977 char **args,
3978 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003979{
3980 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003981 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003982 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003983 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003984 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003985
bellard81d09122004-07-14 17:21:37 +00003986 if (nb_args <= 1) {
3987 /* command completion */
3988 if (nb_args == 0)
3989 cmdname = "";
3990 else
3991 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003992 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003993 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01003994 if (!runstate_check(RUN_STATE_PRECONFIG) ||
3995 cmd_can_preconfig(cmd)) {
3996 cmd_completion(mon, cmdname, cmd->name);
3997 }
bellard81d09122004-07-14 17:21:37 +00003998 }
3999 } else {
4000 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004001 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004002 if (compare_cmd(args[0], cmd->name) &&
4003 (!runstate_check(RUN_STATE_PRECONFIG) ||
4004 cmd_can_preconfig(cmd))) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004005 break;
4006 }
bellard81d09122004-07-14 17:21:37 +00004007 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004008 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004009 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004010 }
4011
Wenchao Xiad903a772013-08-27 20:38:25 +08004012 if (cmd->sub_table) {
4013 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004014 monitor_find_completion_by_table(mon, cmd->sub_table,
4015 &args[1], nb_args - 1);
4016 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004017 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004018 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004019 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4020 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004021 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004022
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004023 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004024 for(i = 0; i < nb_args - 2; i++) {
4025 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004026 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004027 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004028 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004029 }
4030 }
4031 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00004032 old_ptype = NULL;
4033 while (*ptype == '-' && old_ptype != ptype) {
4034 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004035 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004036 }
bellard81d09122004-07-14 17:21:37 +00004037 switch(*ptype) {
4038 case 'F':
4039 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004040 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004041 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004042 break;
4043 case 'B':
4044 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004045 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01004046 while ((blk = blk_next(blk)) != NULL) {
4047 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004048 if (str[0] == '\0' ||
4049 !strncmp(name, str, strlen(str))) {
4050 readline_add_completion(mon->rs, name);
4051 }
4052 }
bellard81d09122004-07-14 17:21:37 +00004053 break;
bellard7fe48482004-10-09 18:08:01 +00004054 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004055 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004056 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004057 monitor_find_completion_by_table(mon, cmd_table,
4058 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004059 }
4060 break;
bellard81d09122004-07-14 17:21:37 +00004061 default:
4062 break;
4063 }
4064 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004065}
4066
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004067static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004068 const char *cmdline)
4069{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004070 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004071 char *args[MAX_ARGS];
4072 int nb_args, len;
4073
4074 /* 1. parse the cmdline */
4075 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4076 return;
4077 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004078
4079 /* if the line ends with a space, it means we want to complete the
4080 next arg */
4081 len = strlen(cmdline);
4082 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4083 if (nb_args >= MAX_ARGS) {
4084 goto cleanup;
4085 }
4086 args[nb_args++] = g_strdup("");
4087 }
4088
4089 /* 2. auto complete according to args */
4090 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004091
4092cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004093 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004094}
4095
aliguori731b0362009-03-05 23:01:42 +00004096static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004097{
aliguori731b0362009-03-05 23:01:42 +00004098 Monitor *mon = opaque;
4099
Peter Xudf152fb2018-03-09 16:59:55 +08004100 return !atomic_mb_read(&mon->suspend_cnt);
bellard9dc39cb2004-03-14 21:38:27 +00004101}
4102
Peter Xu546aa562018-03-09 16:59:54 +08004103/*
4104 * 1. This function takes ownership of rsp, err, and id.
4105 * 2. rsp, err, and id may be NULL.
4106 * 3. If err != NULL then rsp must be NULL.
4107 */
4108static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
4109 Error *err, QObject *id)
4110{
4111 QDict *qdict = NULL;
4112
4113 if (err) {
4114 assert(!rsp);
4115 qdict = qdict_new();
4116 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
4117 error_free(err);
4118 rsp = QOBJECT(qdict);
4119 }
4120
4121 if (rsp) {
4122 if (id) {
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004123 qdict_put_obj(qobject_to(QDict, rsp), "id", qobject_ref(id));
Peter Xu546aa562018-03-09 16:59:54 +08004124 }
4125
4126 monitor_json_emitter(mon, rsp);
4127 }
4128
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004129 qobject_unref(id);
4130 qobject_unref(rsp);
Peter Xu546aa562018-03-09 16:59:54 +08004131}
4132
Markus Armbrusterb2731452018-07-03 10:53:41 +02004133static void monitor_qmp_dispatch(Monitor *mon, QObject *req, QObject *id)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004134{
Markus Armbrusterb2731452018-07-03 10:53:41 +02004135 Monitor *old_mon;
4136 QObject *rsp;
Markus Armbruster69240fe2018-07-03 10:53:43 +02004137 QDict *error;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004138
Peter Xu227a0752018-03-09 16:59:49 +08004139 old_mon = cur_mon;
4140 cur_mon = mon;
4141
Markus Armbruster674ed722018-07-03 10:53:37 +02004142 rsp = qmp_dispatch(mon->qmp.commands, req, qmp_oob_enabled(mon));
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004143
Peter Xu227a0752018-03-09 16:59:49 +08004144 cur_mon = old_mon;
4145
Markus Armbruster69240fe2018-07-03 10:53:43 +02004146 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
4147 error = qdict_get_qdict(qobject_to(QDict, rsp), "error");
4148 if (error
4149 && !g_strcmp0(qdict_get_try_str(error, "class"),
4150 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
4151 /* Provide a more useful error message */
4152 qdict_del(error, "desc");
4153 qdict_put_str(error, "desc", "Expecting capabilities negotiation"
4154 " with 'qmp_capabilities'");
4155 }
4156 }
4157
4158 /* Respond if necessary */
Markus Armbruster05f72742018-07-03 10:53:40 +02004159 monitor_qmp_respond(mon, rsp, NULL, qobject_ref(id));
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004160}
4161
Peter Xu71da4662018-03-09 16:59:57 +08004162/*
4163 * Pop one QMP request from monitor queues, return NULL if not found.
4164 * We are using round-robin fashion to pop the request, to avoid
4165 * processing commands only on a very busy monitor. To achieve that,
4166 * when we process one request on a specific monitor, we put that
4167 * monitor to the end of mon_list queue.
4168 */
Peter Xu40687eb2018-06-20 15:32:18 +08004169static QMPRequest *monitor_qmp_requests_pop_any(void)
Peter Xu71da4662018-03-09 16:59:57 +08004170{
4171 QMPRequest *req_obj = NULL;
4172 Monitor *mon;
4173
4174 qemu_mutex_lock(&monitor_lock);
4175
4176 QTAILQ_FOREACH(mon, &mon_list, entry) {
4177 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4178 req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
4179 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4180 if (req_obj) {
4181 break;
4182 }
4183 }
4184
4185 if (req_obj) {
4186 /*
4187 * We found one request on the monitor. Degrade this monitor's
4188 * priority to lowest by re-inserting it to end of queue.
4189 */
4190 QTAILQ_REMOVE(&mon_list, mon, entry);
4191 QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
4192 }
4193
4194 qemu_mutex_unlock(&monitor_lock);
4195
4196 return req_obj;
4197}
4198
4199static void monitor_qmp_bh_dispatcher(void *data)
4200{
Peter Xu40687eb2018-06-20 15:32:18 +08004201 QMPRequest *req_obj = monitor_qmp_requests_pop_any();
Peter Xu71da4662018-03-09 16:59:57 +08004202
Markus Armbrusterb2731452018-07-03 10:53:41 +02004203 if (!req_obj) {
4204 return;
Peter Xu71da4662018-03-09 16:59:57 +08004205 }
Markus Armbrusterb2731452018-07-03 10:53:41 +02004206
Markus Armbruster1cc37472018-07-03 10:53:44 +02004207 if (req_obj->req) {
4208 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
4209 monitor_qmp_dispatch(req_obj->mon, req_obj->req, req_obj->id);
4210 } else {
4211 assert(req_obj->err);
4212 monitor_qmp_respond(req_obj->mon, NULL, req_obj->err, NULL);
4213 }
4214
Markus Armbrusterb2731452018-07-03 10:53:41 +02004215 if (req_obj->need_resume) {
4216 /* Pairs with the monitor_suspend() in handle_qmp_command() */
4217 monitor_resume(req_obj->mon);
4218 }
4219 qmp_request_free(req_obj);
4220
4221 /* Reschedule instead of looping so the main loop stays responsive */
4222 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
Peter Xu71da4662018-03-09 16:59:57 +08004223}
4224
Peter Xubf1e7302018-03-09 16:59:59 +08004225#define QMP_REQ_QUEUE_LEN_MAX (8)
4226
Peter Xu71da4662018-03-09 16:59:57 +08004227static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
4228{
4229 QObject *req, *id = NULL;
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004230 QDict *qdict;
Peter Xu71da4662018-03-09 16:59:57 +08004231 MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser);
4232 Monitor *mon = container_of(mon_qmp, Monitor, qmp);
4233 Error *err = NULL;
4234 QMPRequest *req_obj;
4235
4236 req = json_parser_parse_err(tokens, NULL, &err);
4237 if (!req && !err) {
4238 /* json_parser_parse_err() sucks: can fail without setting @err */
4239 error_setg(&err, QERR_JSON_PARSING);
4240 }
Peter Xu71da4662018-03-09 16:59:57 +08004241
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004242 qdict = qobject_to(QDict, req);
4243 if (qdict) {
4244 id = qobject_ref(qdict_get(qdict, "id"));
4245 qdict_del(qdict, "id");
4246 } /* else will fail qmp_dispatch() */
4247
Markus Armbruster45434ba2018-07-03 10:53:39 +02004248 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
4249 QString *req_json = qobject_to_json(req);
4250 trace_handle_qmp_command(mon, qstring_get_str(req_json));
4251 qobject_unref(req_json);
4252 }
4253
Markus Armbruster69240fe2018-07-03 10:53:43 +02004254 if (qdict && qmp_is_oob(qdict)) {
Markus Armbrusterb2731452018-07-03 10:53:41 +02004255 /* Out-of-band (OOB) requests are executed directly in parser. */
4256 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id)
4257 ?: "");
4258 monitor_qmp_dispatch(mon, req, id);
4259 return;
4260 }
4261
Peter Xu71da4662018-03-09 16:59:57 +08004262 req_obj = g_new0(QMPRequest, 1);
4263 req_obj->mon = mon;
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004264 req_obj->id = id;
Peter Xu71da4662018-03-09 16:59:57 +08004265 req_obj->req = req;
Markus Armbruster1cc37472018-07-03 10:53:44 +02004266 req_obj->err = err;
Peter Xu71da4662018-03-09 16:59:57 +08004267 req_obj->need_resume = false;
4268
Peter Xubf1e7302018-03-09 16:59:59 +08004269 /* Protect qmp_requests and fetching its length. */
4270 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4271
Peter Xu71da4662018-03-09 16:59:57 +08004272 /*
4273 * If OOB is not enabled on the current monitor, we'll emulate the
4274 * old behavior that we won't process the current monitor any more
4275 * until it has responded. This helps make sure that as long as
4276 * OOB is not enabled, the server will never drop any command.
4277 */
4278 if (!qmp_oob_enabled(mon)) {
4279 monitor_suspend(mon);
4280 req_obj->need_resume = true;
Peter Xubf1e7302018-03-09 16:59:59 +08004281 } else {
4282 /* Drop the request if queue is full. */
4283 if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) {
4284 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
Markus Armbrusterd621cfe2018-07-03 10:53:30 +02004285 /*
4286 * FIXME @id's scope is just @mon, and broadcasting it is
4287 * wrong. If another monitor's client has a command with
4288 * the same ID in flight, the event will incorrectly claim
4289 * that command was dropped.
4290 */
Peter Xubf1e7302018-03-09 16:59:59 +08004291 qapi_event_send_command_dropped(id,
4292 COMMAND_DROP_REASON_QUEUE_FULL,
4293 &error_abort);
Peter Xu6d2d5632018-03-26 14:38:55 +08004294 qmp_request_free(req_obj);
Peter Xubf1e7302018-03-09 16:59:59 +08004295 return;
4296 }
Peter Xu71da4662018-03-09 16:59:57 +08004297 }
4298
4299 /*
4300 * Put the request to the end of queue so that requests will be
4301 * handled in time order. Ownership for req_obj, req, id,
4302 * etc. will be delivered to the handler side.
4303 */
Peter Xu71da4662018-03-09 16:59:57 +08004304 g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
4305 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4306
4307 /* Kick the dispatcher routine */
4308 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
4309}
4310
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004311static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004312{
Peter Xu227a0752018-03-09 16:59:49 +08004313 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004314
Peter Xu227a0752018-03-09 16:59:49 +08004315 json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004316}
4317
aliguori731b0362009-03-05 23:01:42 +00004318static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004319{
aliguori731b0362009-03-05 23:01:42 +00004320 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004321 int i;
aliguori376253e2009-03-05 23:01:23 +00004322
aliguori731b0362009-03-05 23:01:42 +00004323 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004324
aliguoricde76ee2009-03-05 23:01:51 +00004325 if (cur_mon->rs) {
4326 for (i = 0; i < size; i++)
4327 readline_handle_byte(cur_mon->rs, buf[i]);
4328 } else {
4329 if (size == 0 || buf[size - 1] != 0)
4330 monitor_printf(cur_mon, "corrupted command\n");
4331 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004332 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004333 }
aliguori731b0362009-03-05 23:01:42 +00004334
4335 cur_mon = old_mon;
4336}
aliguorid8f44602008-10-06 13:52:44 +00004337
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004338static void monitor_command_cb(void *opaque, const char *cmdline,
4339 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004340{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004341 Monitor *mon = opaque;
4342
aliguori731b0362009-03-05 23:01:42 +00004343 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004344 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004345 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004346}
4347
aliguoricde76ee2009-03-05 23:01:51 +00004348int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004349{
Peter Xue3e977d2018-03-09 16:59:56 +08004350 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004351 return -ENOTTY;
Peter Xue3e977d2018-03-09 16:59:56 +08004352 }
4353
Peter Xudf152fb2018-03-09 16:59:55 +08004354 atomic_inc(&mon->suspend_cnt);
Peter Xue3e977d2018-03-09 16:59:56 +08004355
4356 if (monitor_is_qmp(mon)) {
4357 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004358 * Kick I/O thread to make sure this takes effect. It'll be
Peter Xue3e977d2018-03-09 16:59:56 +08004359 * evaluated again in prepare() of the watch object.
4360 */
4361 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4362 }
4363
4364 trace_monitor_suspend(mon, 1);
aliguoricde76ee2009-03-05 23:01:51 +00004365 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004366}
4367
aliguori376253e2009-03-05 23:01:23 +00004368void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004369{
Peter Xue3e977d2018-03-09 16:59:56 +08004370 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004371 return;
Peter Xudf152fb2018-03-09 16:59:55 +08004372 }
Peter Xue3e977d2018-03-09 16:59:56 +08004373
4374 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
4375 if (monitor_is_qmp(mon)) {
4376 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004377 * For QMP monitors that are running in I/O thread, let's
Peter Xue3e977d2018-03-09 16:59:56 +08004378 * kick the thread in case it's sleeping.
4379 */
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004380 if (mon->use_io_thread) {
Peter Xue3e977d2018-03-09 16:59:56 +08004381 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4382 }
4383 } else {
4384 assert(mon->rs);
4385 readline_show_prompt(mon->rs);
4386 }
4387 }
4388 trace_monitor_suspend(mon, -1);
bellard7e2515e2004-08-01 21:52:19 +00004389}
4390
Peter Xu02130312018-03-09 16:59:53 +08004391static QObject *get_qmp_greeting(Monitor *mon)
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004392{
Peter Xu02130312018-03-09 16:59:53 +08004393 QList *cap_list = qlist_new();
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004394 QObject *ver = NULL;
Peter Xu02130312018-03-09 16:59:53 +08004395 QMPCapability cap;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004396
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004397 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004398
Peter Xu02130312018-03-09 16:59:53 +08004399 for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004400 if (!mon->use_io_thread && cap == QMP_CAPABILITY_OOB) {
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004401 /* Monitors that are not using I/O thread won't support OOB */
Peter Xu02130312018-03-09 16:59:53 +08004402 continue;
4403 }
Laurent Vivier625eaca2018-03-23 15:32:01 +01004404 qlist_append_str(cap_list, QMPCapability_str(cap));
Peter Xu02130312018-03-09 16:59:53 +08004405 }
4406
4407 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': %p}}",
4408 ver, cap_list);
4409}
4410
4411static void monitor_qmp_caps_reset(Monitor *mon)
4412{
4413 memset(mon->qmp.qmp_caps, 0, sizeof(mon->qmp.qmp_caps));
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004414}
4415
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004416static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004417{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004418 QObject *data;
4419 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004420
Luiz Capitulino47116d12010-02-08 17:01:30 -02004421 switch (event) {
4422 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01004423 mon->qmp.commands = &qmp_cap_negotiation_commands;
Peter Xu02130312018-03-09 16:59:53 +08004424 monitor_qmp_caps_reset(mon);
4425 data = get_qmp_greeting(mon);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004426 monitor_json_emitter(mon, data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004427 qobject_unref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004428 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004429 break;
4430 case CHR_EVENT_CLOSED:
Peter Xuc73a8432018-06-20 15:32:19 +08004431 /*
4432 * Note: this is only useful when the output of the chardev
4433 * backend is still open. For example, when the backend is
4434 * stdio, it's possible that stdout is still open when stdin
4435 * is closed.
4436 */
4437 monitor_qmp_response_flush(mon);
Peter Xu6d2d5632018-03-26 14:38:55 +08004438 monitor_qmp_cleanup_queues(mon);
Markus Armbruster74358f22015-03-06 19:35:59 +01004439 json_message_parser_destroy(&mon->qmp.parser);
4440 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004441 mon_refcount--;
4442 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004443 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004444 }
4445}
4446
aliguori731b0362009-03-05 23:01:42 +00004447static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004448{
aliguori376253e2009-03-05 23:01:23 +00004449 Monitor *mon = opaque;
4450
aliguori2724b182009-03-05 23:01:47 +00004451 switch (event) {
4452 case CHR_EVENT_MUX_IN:
Peter Xudc7cbcd2018-06-08 11:55:05 +08004453 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004454 mon->mux_out = 0;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004455 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004456 if (mon->reset_seen) {
4457 readline_restart(mon->rs);
4458 monitor_resume(mon);
4459 monitor_flush(mon);
4460 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004461 atomic_mb_set(&mon->suspend_cnt, 0);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004462 }
aliguori2724b182009-03-05 23:01:47 +00004463 break;
ths86e94de2007-01-05 22:01:59 +00004464
aliguori2724b182009-03-05 23:01:47 +00004465 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004466 if (mon->reset_seen) {
Peter Xudf152fb2018-03-09 16:59:55 +08004467 if (atomic_mb_read(&mon->suspend_cnt) == 0) {
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004468 monitor_printf(mon, "\n");
4469 }
4470 monitor_flush(mon);
4471 monitor_suspend(mon);
4472 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004473 atomic_inc(&mon->suspend_cnt);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004474 }
Peter Xudc7cbcd2018-06-08 11:55:05 +08004475 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004476 mon->mux_out = 1;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004477 qemu_mutex_unlock(&mon->mon_lock);
aliguori2724b182009-03-05 23:01:47 +00004478 break;
4479
Amit Shahb6b8df52009-10-07 18:31:16 +05304480 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004481 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4482 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004483 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004484 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004485 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004486 }
4487 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004488 mon_refcount++;
4489 break;
4490
4491 case CHR_EVENT_CLOSED:
4492 mon_refcount--;
4493 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004494 break;
4495 }
ths86e94de2007-01-05 22:01:59 +00004496}
4497
Wayne Xia816f8922011-10-12 11:32:41 +08004498static int
4499compare_mon_cmd(const void *a, const void *b)
4500{
4501 return strcmp(((const mon_cmd_t *)a)->name,
4502 ((const mon_cmd_t *)b)->name);
4503}
4504
4505static void sortcmdlist(void)
4506{
4507 int array_num;
4508 int elem_size = sizeof(mon_cmd_t);
4509
4510 array_num = sizeof(mon_cmds)/elem_size-1;
4511 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4512
4513 array_num = sizeof(info_cmds)/elem_size-1;
4514 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4515}
4516
Peter Xua5ed3522018-03-09 16:59:52 +08004517static GMainContext *monitor_get_io_context(void)
4518{
4519 return iothread_get_g_main_context(mon_global.mon_iothread);
4520}
4521
4522static AioContext *monitor_get_aio_context(void)
4523{
4524 return iothread_get_aio_context(mon_global.mon_iothread);
4525}
4526
4527static void monitor_iothread_init(void)
4528{
4529 mon_global.mon_iothread = iothread_create("mon_iothread",
4530 &error_abort);
Peter Xu71da4662018-03-09 16:59:57 +08004531
4532 /*
4533 * This MUST be on main loop thread since we have commands that
4534 * have assumption to be run on main loop thread. It would be
4535 * nice that one day we can remove this assumption in the future.
4536 */
Peter Xu951702f2018-04-10 12:49:42 +08004537 mon_global.qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
Peter Xu71da4662018-03-09 16:59:57 +08004538 monitor_qmp_bh_dispatcher,
4539 NULL);
Peter Xuabe3cd02018-03-09 17:00:02 +08004540
4541 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004542 * Unlike the dispatcher BH, this must be run on the monitor I/O
4543 * thread, so that monitors that are using I/O thread will make
4544 * sure read/write operations are all done on the I/O thread.
Peter Xuabe3cd02018-03-09 17:00:02 +08004545 */
4546 mon_global.qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
4547 monitor_qmp_bh_responder,
4548 NULL);
Peter Xua5ed3522018-03-09 16:59:52 +08004549}
4550
Peter Xu6adf08d2018-03-09 16:59:50 +08004551void monitor_init_globals(void)
4552{
4553 monitor_init_qmp_commands();
4554 monitor_qapi_event_init();
4555 sortcmdlist();
4556 qemu_mutex_init(&monitor_lock);
Peter Xu47451462018-06-08 11:55:11 +08004557 qemu_mutex_init(&mon_fdsets_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004558 monitor_iothread_init();
Peter Xu6adf08d2018-03-09 16:59:50 +08004559}
4560
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004561/* These functions just adapt the readline interface in a typesafe way. We
4562 * could cast function pointers but that discards compiler checks.
4563 */
Stefan Weild5d15072014-01-25 18:18:23 +01004564static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4565 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004566{
4567 va_list ap;
4568 va_start(ap, fmt);
4569 monitor_vprintf(opaque, fmt, ap);
4570 va_end(ap);
4571}
4572
4573static void monitor_readline_flush(void *opaque)
4574{
4575 monitor_flush(opaque);
4576}
4577
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004578/*
4579 * Print to current monitor if we have one, else to stderr.
4580 * TODO should return int, so callers can calculate width, but that
4581 * requires surgery to monitor_vprintf(). Left for another day.
4582 */
4583void error_vprintf(const char *fmt, va_list ap)
4584{
4585 if (cur_mon && !monitor_cur_is_qmp()) {
4586 monitor_vprintf(cur_mon, fmt, ap);
4587 } else {
4588 vfprintf(stderr, fmt, ap);
4589 }
4590}
4591
4592void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4593{
4594 if (cur_mon && !monitor_cur_is_qmp()) {
4595 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004596 } else if (!cur_mon) {
4597 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004598 }
4599}
4600
Peter Xua5ed3522018-03-09 16:59:52 +08004601static void monitor_list_append(Monitor *mon)
4602{
4603 qemu_mutex_lock(&monitor_lock);
4604 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4605 qemu_mutex_unlock(&monitor_lock);
4606}
4607
4608static void monitor_qmp_setup_handlers_bh(void *opaque)
4609{
4610 Monitor *mon = opaque;
4611 GMainContext *context;
4612
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004613 if (mon->use_io_thread) {
Peter Xua5ed3522018-03-09 16:59:52 +08004614 /*
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004615 * When @use_io_thread is set, we use the global shared dedicated
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004616 * I/O thread for this monitor to handle input/output.
Peter Xua5ed3522018-03-09 16:59:52 +08004617 */
4618 context = monitor_get_io_context();
4619 /* We should have inited globals before reaching here. */
4620 assert(context);
4621 } else {
4622 /* The default main loop, which is the main thread */
4623 context = NULL;
4624 }
4625
4626 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4627 monitor_qmp_event, NULL, mon, context, true);
4628 monitor_list_append(mon);
4629}
4630
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004631void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004632{
Peter Xu6adf08d2018-03-09 16:59:50 +08004633 Monitor *mon = g_malloc(sizeof(*mon));
Peter Xube933ff2018-03-26 14:38:56 +08004634 bool use_readline = flags & MONITOR_USE_READLINE;
4635 bool use_oob = flags & MONITOR_USE_OOB;
ths20d8a3e2007-02-18 17:04:49 +00004636
Peter Xube933ff2018-03-26 14:38:56 +08004637 if (use_oob) {
4638 if (CHARDEV_IS_MUX(chr)) {
Markus Armbrusterc0698212018-07-03 10:53:27 +02004639 error_report("Monitor out-of-band is not supported with "
Peter Xube933ff2018-03-26 14:38:56 +08004640 "MUX typed chardev backend");
4641 exit(1);
4642 }
4643 if (use_readline) {
Markus Armbrusterc0698212018-07-03 10:53:27 +02004644 error_report("Monitor out-of-band is only supported by QMP");
Peter Xube933ff2018-03-26 14:38:56 +08004645 exit(1);
4646 }
4647 }
4648
4649 monitor_data_init(mon, false, use_oob);
ths20d8a3e2007-02-18 17:04:49 +00004650
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004651 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004652 mon->flags = flags;
Peter Xube933ff2018-03-26 14:38:56 +08004653 if (use_readline) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004654 mon->rs = readline_init(monitor_readline_printf,
4655 monitor_readline_flush,
4656 mon,
4657 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004658 monitor_read_command(mon, 0);
4659 }
aliguori87127162009-03-05 23:01:29 +00004660
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004661 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004662 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004663 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004664 if (mon->use_io_thread) {
Peter Xua5ed3522018-03-09 16:59:52 +08004665 /*
4666 * Make sure the old iowatch is gone. It's possible when
4667 * e.g. the chardev is in client mode, with wait=on.
4668 */
4669 remove_fd_in_watch(chr);
4670 /*
4671 * We can't call qemu_chr_fe_set_handlers() directly here
4672 * since during the procedure the chardev will be active
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004673 * and running in monitor I/O thread, while we'll still do
Peter Xua5ed3522018-03-09 16:59:52 +08004674 * something before returning from it, which is a possible
4675 * race too. To avoid that, we just create a BH to setup
4676 * the handlers.
4677 */
4678 aio_bh_schedule_oneshot(monitor_get_aio_context(),
4679 monitor_qmp_setup_handlers_bh, mon);
4680 /* We'll add this to mon_list in the BH when setup done */
4681 return;
4682 } else {
4683 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
4684 monitor_qmp_read, monitor_qmp_event,
4685 NULL, mon, NULL, true);
4686 }
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004687 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004688 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004689 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004690 }
aliguori87127162009-03-05 23:01:29 +00004691
Peter Xua5ed3522018-03-09 16:59:52 +08004692 monitor_list_append(mon);
bellard7e2515e2004-08-01 21:52:19 +00004693}
4694
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004695void monitor_cleanup(void)
4696{
4697 Monitor *mon, *next;
4698
Peter Xua5ed3522018-03-09 16:59:52 +08004699 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004700 * We need to explicitly stop the I/O thread (but not destroy it),
4701 * cleanup the monitor resources, then destroy the I/O thread since
Peter Xua5ed3522018-03-09 16:59:52 +08004702 * we need to unregister from chardev below in
4703 * monitor_data_destroy(), and chardev is not thread-safe yet
4704 */
4705 iothread_stop(mon_global.mon_iothread);
4706
Peter Xuabe3cd02018-03-09 17:00:02 +08004707 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004708 * After we have I/O thread to send responses, it's possible that
4709 * when we stop the I/O thread there are still replies queued in the
Peter Xuabe3cd02018-03-09 17:00:02 +08004710 * responder queue. Flush all of them. Note that even after this
4711 * flush it's still possible that out buffer is not flushed.
4712 * It'll be done in below monitor_flush() as the last resort.
4713 */
4714 monitor_qmp_bh_responder(NULL);
4715
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004716 qemu_mutex_lock(&monitor_lock);
Peter Xu238d9f32018-03-09 16:59:51 +08004717 QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
4718 QTAILQ_REMOVE(&mon_list, mon, entry);
Peter Xuabe3cd02018-03-09 17:00:02 +08004719 monitor_flush(mon);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004720 monitor_data_destroy(mon);
4721 g_free(mon);
4722 }
4723 qemu_mutex_unlock(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004724
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004725 /* QEMUBHs needs to be deleted before destroying the I/O thread */
Peter Xu71da4662018-03-09 16:59:57 +08004726 qemu_bh_delete(mon_global.qmp_dispatcher_bh);
4727 mon_global.qmp_dispatcher_bh = NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +08004728 qemu_bh_delete(mon_global.qmp_respond_bh);
4729 mon_global.qmp_respond_bh = NULL;
Peter Xu71da4662018-03-09 16:59:57 +08004730
Peter Xua5ed3522018-03-09 16:59:52 +08004731 iothread_destroy(mon_global.mon_iothread);
4732 mon_global.mon_iothread = NULL;
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004733}
4734
Paolo Bonzini4d454572012-11-26 16:03:42 +01004735QemuOptsList qemu_mon_opts = {
4736 .name = "mon",
4737 .implied_opt_name = "chardev",
4738 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4739 .desc = {
4740 {
4741 .name = "mode",
4742 .type = QEMU_OPT_STRING,
4743 },{
4744 .name = "chardev",
4745 .type = QEMU_OPT_STRING,
4746 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004747 .name = "pretty",
4748 .type = QEMU_OPT_BOOL,
Peter Xube933ff2018-03-26 14:38:56 +08004749 },{
4750 .name = "x-oob",
4751 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004752 },
4753 { /* end of list */ }
4754 },
4755};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004756
4757#ifndef TARGET_I386
4758void qmp_rtc_reset_reinjection(Error **errp)
4759{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004760 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004761}
Brijesh Singh08a161f2018-03-08 06:48:42 -06004762
4763SevInfo *qmp_query_sev(Error **errp)
4764{
4765 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
4766 return NULL;
4767}
Brijesh Singh1b6a0342018-03-08 06:48:56 -06004768
4769SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
4770{
4771 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
4772 return NULL;
4773}
Brijesh Singh31dd67f2018-03-08 06:48:59 -06004774
4775SevCapability *qmp_query_sev_capabilities(Error **errp)
4776{
4777 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
4778 return NULL;
4779}
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004780#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004781
4782#ifndef TARGET_S390X
4783void qmp_dump_skeys(const char *filename, Error **errp)
4784{
4785 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4786}
4787#endif
Peter Xuae50a772016-03-30 17:27:24 +01004788
4789#ifndef TARGET_ARM
4790GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4791{
4792 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4793 return NULL;
4794}
4795#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304796
4797HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4798{
4799 MachineState *ms = MACHINE(qdev_get_machine());
4800 MachineClass *mc = MACHINE_GET_CLASS(ms);
4801
Igor Mammedovc5514d02017-02-10 11:20:57 +01004802 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304803 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4804 return NULL;
4805 }
4806
Igor Mammedovc5514d02017-02-10 11:20:57 +01004807 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304808}