blob: a1999e396ca04b75c316843f1ae8e82d285ee765 [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.
Markus Armbruster774a6b62018-07-03 10:53:57 +0200172 * @commands is &qmp_cap_negotiation_commands then. When command
173 * qmp_capabilities succeeds, we go into command mode, and
174 * @command becomes &qmp_commands.
Markus Armbrusterf994b252015-03-06 19:51:51 +0100175 */
Markus Armbruster635db182017-03-03 13:32:27 +0100176 QmpCommandList *commands;
Markus Armbruster279f9e02018-07-03 10:53:56 +0200177 bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
178 bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */
Peter Xu71da4662018-03-09 16:59:57 +0800179 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200180 * Protects qmp request/response queue.
181 * Take monitor_lock first when you need both.
Peter Xu71da4662018-03-09 16:59:57 +0800182 */
183 QemuMutex qmp_queue_lock;
184 /* Input queue that holds all the parsed QMP requests */
185 GQueue *qmp_requests;
Peter Xuabe3cd02018-03-09 17:00:02 +0800186 /* Output queue contains all the QMP responses in order */
187 GQueue *qmp_responses;
Markus Armbruster74358f22015-03-06 19:35:59 +0100188} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200189
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100190/*
191 * To prevent flooding clients, events can be throttled. The
192 * throttling is calculated globally, rather than per-Monitor
193 * instance.
194 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200195typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200196 QAPIEvent event; /* Throttling state for this event type and... */
197 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100198 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200199 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200200} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100201
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200202typedef struct {
203 int64_t rate; /* Minimum time (in ns) between two events */
204} MonitorQAPIEventConf;
205
aliguori87127162009-03-05 23:01:29 +0000206struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300207 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200208 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000209 int flags;
Peter Xudf152fb2018-03-09 16:59:55 +0800210 int suspend_cnt; /* Needs to be accessed atomically */
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400211 bool skip_flush;
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200212 bool use_io_thread;
Peter Xud9f25282018-06-08 11:55:07 +0800213
214 /*
215 * State used only in the thread "owning" the monitor.
Markus Armbrustercab5ad82018-07-03 10:53:46 +0200216 * If @use_io_thread, this is @mon_iothread.
Peter Xud9f25282018-06-08 11:55:07 +0800217 * Else, it's the main thread.
218 * These members can be safely accessed without locks.
219 */
aliguori731b0362009-03-05 23:01:42 +0000220 ReadLineState *rs;
Peter Xud9f25282018-06-08 11:55:07 +0800221
Markus Armbruster74358f22015-03-06 19:35:59 +0100222 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200223 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200224 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000225 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800226 mon_cmd_t *cmd_table;
Peter Xu238d9f32018-03-09 16:59:51 +0800227 QTAILQ_ENTRY(Monitor) entry;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800228
229 /*
230 * The per-monitor lock. We can't access guest memory when holding
231 * the lock.
232 */
233 QemuMutex mon_lock;
234
235 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200236 * Members that are protected by the per-monitor lock
Peter Xudc7cbcd2018-06-08 11:55:05 +0800237 */
Peter Xu9409fc02018-06-08 11:55:06 +0800238 QLIST_HEAD(, mon_fd_t) fds;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800239 QString *outbuf;
240 guint out_watch;
241 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
242 int mux_out;
aliguori87127162009-03-05 23:01:29 +0000243};
244
Markus Armbruster774a6b62018-07-03 10:53:57 +0200245/* Shared monitor I/O thread */
Markus Armbrustercab5ad82018-07-03 10:53:46 +0200246IOThread *mon_iothread;
247
248/* Bottom half to dispatch the requests received from I/O thread */
249QEMUBH *qmp_dispatcher_bh;
250
251/* Bottom half to deliver the responses back to clients */
252QEMUBH *qmp_respond_bh;
Peter Xua5ed3522018-03-09 16:59:52 +0800253
Peter Xu6d2d5632018-03-26 14:38:55 +0800254struct QMPRequest {
255 /* Owner of the request */
256 Monitor *mon;
257 /* "id" field of the request */
258 QObject *id;
Markus Armbruster1cc37472018-07-03 10:53:44 +0200259 /*
260 * Request object to be handled or Error to be reported
261 * (exactly one of them is non-null)
262 */
Peter Xu6d2d5632018-03-26 14:38:55 +0800263 QObject *req;
Markus Armbruster1cc37472018-07-03 10:53:44 +0200264 Error *err;
Peter Xu6d2d5632018-03-26 14:38:55 +0800265 /*
266 * Whether we need to resume the monitor afterward. This flag is
267 * used to emulate the old QMP server behavior that the current
268 * command must be completed before execution of the next one.
269 */
270 bool need_resume;
271};
272typedef struct QMPRequest QMPRequest;
273
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300274/* QMP checker flags */
275#define QMP_ACCEPT_UNKNOWNS 1
276
Peter Xu095cb1b2018-06-08 11:55:08 +0800277/* Protects mon_list, monitor_qapi_event_state. */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200278static QemuMutex monitor_lock;
Peter Xu095cb1b2018-06-08 11:55:08 +0800279static GHashTable *monitor_qapi_event_state;
Peter Xu238d9f32018-03-09 16:59:51 +0800280static QTAILQ_HEAD(mon_list, Monitor) mon_list;
Peter Xu095cb1b2018-06-08 11:55:08 +0800281
Peter Xu47451462018-06-08 11:55:11 +0800282/* Protects mon_fdsets */
283static QemuMutex mon_fdsets_lock;
Corey Bryantba1c0482012-08-14 16:43:43 -0400284static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800285
Corey Bryantefb87c12012-08-14 16:43:48 -0400286static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000287
Wayne Xia816f8922011-10-12 11:32:41 +0800288static mon_cmd_t mon_cmds[];
289static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000290
Markus Armbruster635db182017-03-03 13:32:27 +0100291QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100292
Peter Xu62aa1d82018-07-20 11:34:51 +0800293__thread Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000294
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100295static void monitor_command_cb(void *opaque, const char *cmdline,
296 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000297
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100298/**
299 * Is @mon a QMP monitor?
300 */
301static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200302{
303 return (mon->flags & MONITOR_USE_CONTROL);
304}
305
Markus Armbruster489653b2015-03-06 20:01:05 +0100306/**
Markus Armbruster774a6b62018-07-03 10:53:57 +0200307 * Is @mon is using readline?
308 * Note: not all HMP monitors use readline, e.g., gdbserver has a
309 * non-interactive HMP monitor, so readline is not used there.
Peter Xue3e977d2018-03-09 16:59:56 +0800310 */
311static inline bool monitor_uses_readline(const Monitor *mon)
312{
313 return mon->flags & MONITOR_USE_READLINE;
314}
315
316static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
317{
318 return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
319}
320
Peter Xu6e8c5f42018-06-08 11:55:09 +0800321/*
322 * Return the clock to use for recording an event's time.
Markus Armbruster774a6b62018-07-03 10:53:57 +0200323 * It's QEMU_CLOCK_REALTIME, except for qtests it's
324 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
Peter Xu6e8c5f42018-06-08 11:55:09 +0800325 * Beware: result is invalid before configure_accelerator().
326 */
327static inline QEMUClockType monitor_get_event_clock(void)
328{
Peter Xu6e8c5f42018-06-08 11:55:09 +0800329 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
Markus Armbruster774a6b62018-07-03 10:53:57 +0200371/* Caller must hold mon->qmp.qmp_queue_lock */
Peter Xu6d2d5632018-03-26 14:38:55 +0800372static 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
Markus Armbruster774a6b62018-07-03 10:53:57 +0200379/* Caller must hold the mon->qmp.qmp_queue_lock */
Peter Xu6d2d5632018-03-26 14:38:55 +0800380static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon)
381{
382 while (!g_queue_is_empty(mon->qmp.qmp_responses)) {
Markus Armbrusterd43b1692018-07-03 10:53:49 +0200383 qobject_unref((QDict *)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
Markus Armbruster774a6b62018-07-03 10:53:57 +0200410/* Caller must hold mon->mon_lock */
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
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200507static void qmp_send_response(Monitor *mon, QDict *rsp)
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200508{
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200509 QObject *data = QOBJECT(rsp);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200510 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
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200522static void qmp_queue_response(Monitor *mon, QDict *rsp)
Peter Xuabe3cd02018-03-09 17:00:02 +0800523{
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200524 if (mon->use_io_thread) {
Peter Xuabe3cd02018-03-09 17:00:02 +0800525 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200526 * Push a reference to the response queue. The I/O thread
527 * drains that queue and emits.
Peter Xuabe3cd02018-03-09 17:00:02 +0800528 */
Peter Xuabe3cd02018-03-09 17:00:02 +0800529 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200530 g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(rsp));
Peter Xuabe3cd02018-03-09 17:00:02 +0800531 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
Markus Armbrustercab5ad82018-07-03 10:53:46 +0200532 qemu_bh_schedule(qmp_respond_bh);
Peter Xuabe3cd02018-03-09 17:00:02 +0800533 } else {
534 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200535 * Not using monitor I/O thread, i.e. we are in the main thread.
536 * Emit right away.
Peter Xuabe3cd02018-03-09 17:00:02 +0800537 */
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200538 qmp_send_response(mon, rsp);
Peter Xuabe3cd02018-03-09 17:00:02 +0800539 }
540}
541
542struct QMPResponse {
543 Monitor *mon;
Markus Armbrusterd43b1692018-07-03 10:53:49 +0200544 QDict *data;
Peter Xuabe3cd02018-03-09 17:00:02 +0800545};
546typedef struct QMPResponse QMPResponse;
547
Markus Armbrusterd43b1692018-07-03 10:53:49 +0200548static QDict *monitor_qmp_response_pop_one(Monitor *mon)
Peter Xuc73a8432018-06-20 15:32:19 +0800549{
Markus Armbrusterd43b1692018-07-03 10:53:49 +0200550 QDict *data;
Peter Xuc73a8432018-06-20 15:32:19 +0800551
552 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
553 data = g_queue_pop_head(mon->qmp.qmp_responses);
554 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
555
556 return data;
557}
558
559static void monitor_qmp_response_flush(Monitor *mon)
560{
Markus Armbrusterd43b1692018-07-03 10:53:49 +0200561 QDict *data;
Peter Xuc73a8432018-06-20 15:32:19 +0800562
563 while ((data = monitor_qmp_response_pop_one(mon))) {
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200564 qmp_send_response(mon, data);
Peter Xuc73a8432018-06-20 15:32:19 +0800565 qobject_unref(data);
566 }
567}
568
Peter Xuabe3cd02018-03-09 17:00:02 +0800569/*
Peter Xu40687eb2018-06-20 15:32:18 +0800570 * Pop a QMPResponse from any monitor's response queue into @response.
571 * Return false if all the queues are empty; else true.
Peter Xuabe3cd02018-03-09 17:00:02 +0800572 */
Peter Xu40687eb2018-06-20 15:32:18 +0800573static bool monitor_qmp_response_pop_any(QMPResponse *response)
Peter Xuabe3cd02018-03-09 17:00:02 +0800574{
575 Monitor *mon;
Markus Armbrusterd43b1692018-07-03 10:53:49 +0200576 QDict *data = NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +0800577
578 qemu_mutex_lock(&monitor_lock);
579 QTAILQ_FOREACH(mon, &mon_list, entry) {
Peter Xuc73a8432018-06-20 15:32:19 +0800580 data = monitor_qmp_response_pop_one(mon);
Peter Xuabe3cd02018-03-09 17:00:02 +0800581 if (data) {
Peter Xu40687eb2018-06-20 15:32:18 +0800582 response->mon = mon;
583 response->data = data;
Peter Xuabe3cd02018-03-09 17:00:02 +0800584 break;
585 }
586 }
587 qemu_mutex_unlock(&monitor_lock);
Peter Xu40687eb2018-06-20 15:32:18 +0800588 return data != NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +0800589}
590
591static void monitor_qmp_bh_responder(void *opaque)
592{
593 QMPResponse response;
594
Peter Xu40687eb2018-06-20 15:32:18 +0800595 while (monitor_qmp_response_pop_any(&response)) {
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200596 qmp_send_response(response.mon, response.data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200597 qobject_unref(response.data);
Peter Xuabe3cd02018-03-09 17:00:02 +0800598 }
599}
600
Eric Blake7fb1cf12015-11-18 01:52:57 -0700601static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200602 /* Limit guest-triggerable events to 1 per second */
603 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
604 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
605 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
606 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
607 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
608 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
609};
610
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100611/*
Markus Armbruster774a6b62018-07-03 10:53:57 +0200612 * Broadcast an event to all monitors.
613 * @qdict is the event object. Its member "event" must match @event.
614 * Caller must hold monitor_lock.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100615 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200616static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100617{
618 Monitor *mon;
619
Markus Armbruster688b4b72015-10-15 17:08:30 +0200620 trace_monitor_protocol_event_emit(event, qdict);
Peter Xu238d9f32018-03-09 16:59:51 +0800621 QTAILQ_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100622 if (monitor_is_qmp(mon)
623 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster65e3fe62018-07-03 10:53:50 +0200624 qmp_queue_response(mon, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100625 }
626 }
627}
628
Markus Armbrustera24712a2015-10-15 17:08:34 +0200629static void monitor_qapi_event_handler(void *opaque);
630
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100631/*
632 * Queue a new event for emission to Monitor instances,
633 * applying any rate limiting if required.
634 */
635static void
Marc-André Lureau9a105402018-07-31 17:01:44 +0200636monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100637{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200638 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200639 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200640
Eric Blake7fb1cf12015-11-18 01:52:57 -0700641 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200642 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200643 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100644
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200645 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200646
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200647 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200648 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200649 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100650 } else {
Max Reitz7dc847e2018-02-24 16:40:29 +0100651 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
Markus Armbruster7de0be62015-10-15 17:08:35 +0200652 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200653
654 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
655 assert(!evstate || timer_pending(evstate->timer));
656
657 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200658 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200659 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200660 * last send. Store event for sending when timer fires,
661 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100662 */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200663 qobject_unref(evstate->qdict);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200664 evstate->qdict = qobject_ref(qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100665 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200666 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200667 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200668 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200669 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200670 * events arriving before then will be delayed until then.
671 */
Peter Xu6e8c5f42018-06-08 11:55:09 +0800672 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200673
Markus Armbruster688b4b72015-10-15 17:08:30 +0200674 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200675
676 evstate = g_new(MonitorQAPIEventState, 1);
677 evstate->event = event;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200678 evstate->data = qobject_ref(data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200679 evstate->qdict = NULL;
Peter Xu6e8c5f42018-06-08 11:55:09 +0800680 evstate->timer = timer_new_ns(monitor_get_event_clock(),
Markus Armbrustera24712a2015-10-15 17:08:34 +0200681 monitor_qapi_event_handler,
682 evstate);
683 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200684 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100685 }
686 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200687
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200688 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100689}
690
Marc-André Lureau9a105402018-07-31 17:01:44 +0200691static void
692monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
693{
694 /*
695 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
696 * would deadlock on monitor_lock. Work around by queueing
697 * events in thread-local storage.
698 * TODO: remove this, make it re-enter safe.
699 */
700 typedef struct MonitorQapiEvent {
701 QAPIEvent event;
702 QDict *qdict;
703 QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
704 } MonitorQapiEvent;
705 static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
706 static __thread bool reentered;
707 MonitorQapiEvent *ev;
708
709 if (!reentered) {
710 QSIMPLEQ_INIT(&event_queue);
711 }
712
713 ev = g_new(MonitorQapiEvent, 1);
714 ev->qdict = qobject_ref(qdict);
715 ev->event = event;
716 QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
717 if (reentered) {
718 return;
719 }
720
721 reentered = true;
722
723 while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
724 QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
725 monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
726 qobject_unref(ev->qdict);
727 g_free(ev);
728 }
729
730 reentered = false;
731}
732
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100733/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200734 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200735 * event.
736 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100737 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200738static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100739{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200740 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200741 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100742
Markus Armbruster93f8f982015-10-15 17:08:31 +0200743 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200744 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200745
Markus Armbruster688b4b72015-10-15 17:08:30 +0200746 if (evstate->qdict) {
Peter Xu6e8c5f42018-06-08 11:55:09 +0800747 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
Markus Armbruster93f8f982015-10-15 17:08:31 +0200748
Markus Armbruster688b4b72015-10-15 17:08:30 +0200749 monitor_qapi_event_emit(evstate->event, evstate->qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200750 qobject_unref(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200751 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200752 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200753 } else {
754 g_hash_table_remove(monitor_qapi_event_state, evstate);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200755 qobject_unref(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200756 timer_free(evstate->timer);
757 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100758 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200759
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200760 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100761}
762
Markus Armbrustera24712a2015-10-15 17:08:34 +0200763static unsigned int qapi_event_throttle_hash(const void *key)
764{
765 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200766 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200767
Markus Armbruster7de0be62015-10-15 17:08:35 +0200768 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
769 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
770 }
771
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200772 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
773 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
774 }
775
Markus Armbruster7de0be62015-10-15 17:08:35 +0200776 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200777}
778
779static gboolean qapi_event_throttle_equal(const void *a, const void *b)
780{
781 const MonitorQAPIEventState *eva = a;
782 const MonitorQAPIEventState *evb = b;
783
Markus Armbruster7de0be62015-10-15 17:08:35 +0200784 if (eva->event != evb->event) {
785 return FALSE;
786 }
787
788 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
789 return !strcmp(qdict_get_str(eva->data, "id"),
790 qdict_get_str(evb->data, "id"));
791 }
792
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200793 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
794 return !strcmp(qdict_get_str(eva->data, "node-name"),
795 qdict_get_str(evb->data, "node-name"));
796 }
797
Markus Armbruster7de0be62015-10-15 17:08:35 +0200798 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200799}
800
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200801static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100802{
Markus Armbrustera24712a2015-10-15 17:08:34 +0200803 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
804 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200805 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200806}
807
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100808static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200809
Peter Xua5ed3522018-03-09 16:59:52 +0800810static void monitor_data_init(Monitor *mon, bool skip_flush,
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200811 bool use_io_thread)
Wenchao Xiab01fe892013-08-27 20:38:19 +0800812{
813 memset(mon, 0, sizeof(Monitor));
Peter Xudc7cbcd2018-06-08 11:55:05 +0800814 qemu_mutex_init(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800815 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800816 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800817 /* Use *mon_cmds by default. */
818 mon->cmd_table = mon_cmds;
Peter Xu88a95d12018-03-09 16:59:48 +0800819 mon->skip_flush = skip_flush;
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +0200820 mon->use_io_thread = use_io_thread;
Peter Xu71da4662018-03-09 16:59:57 +0800821 mon->qmp.qmp_requests = g_queue_new();
Peter Xuabe3cd02018-03-09 17:00:02 +0800822 mon->qmp.qmp_responses = g_queue_new();
Wenchao Xiab01fe892013-08-27 20:38:19 +0800823}
824
825static void monitor_data_destroy(Monitor *mon)
826{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200827 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400828 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400829 if (monitor_is_qmp(mon)) {
830 json_message_parser_destroy(&mon->qmp.parser);
831 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100832 readline_free(mon->rs);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200833 qobject_unref(mon->outbuf);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800834 qemu_mutex_destroy(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800835 qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
Peter Xu6d2d5632018-03-26 14:38:55 +0800836 monitor_qmp_cleanup_req_queue_locked(mon);
837 monitor_qmp_cleanup_resp_queue_locked(mon);
Peter Xu71da4662018-03-09 16:59:57 +0800838 g_queue_free(mon->qmp.qmp_requests);
Peter Xuabe3cd02018-03-09 17:00:02 +0800839 g_queue_free(mon->qmp.qmp_responses);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800840}
841
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200842char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
843 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200844{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200845 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200846 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200847
Peter Xua5ed3522018-03-09 16:59:52 +0800848 monitor_data_init(&hmp, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200849
850 old_mon = cur_mon;
851 cur_mon = &hmp;
852
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200853 if (has_cpu_index) {
854 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200855 if (ret < 0) {
856 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100857 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
858 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200859 goto out;
860 }
861 }
862
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100863 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200864 cur_mon = old_mon;
865
Peter Xudc7cbcd2018-06-08 11:55:05 +0800866 qemu_mutex_lock(&hmp.mon_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400867 if (qstring_get_length(hmp.outbuf) > 0) {
868 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200869 } else {
870 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200871 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800872 qemu_mutex_unlock(&hmp.mon_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200873
874out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800875 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200876 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200877}
878
bellard9dc39cb2004-03-14 21:38:27 +0000879static int compare_cmd(const char *name, const char *list)
880{
881 const char *p, *pstart;
882 int len;
883 len = strlen(name);
884 p = list;
885 for(;;) {
886 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200887 p = qemu_strchrnul(p, '|');
bellard9dc39cb2004-03-14 21:38:27 +0000888 if ((p - pstart) == len && !memcmp(pstart, name, len))
889 return 1;
890 if (*p == '\0')
891 break;
892 p++;
893 }
894 return 0;
895}
896
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800897static int get_str(char *buf, int buf_size, const char **pp)
898{
899 const char *p;
900 char *q;
901 int c;
902
903 q = buf;
904 p = *pp;
905 while (qemu_isspace(*p)) {
906 p++;
907 }
908 if (*p == '\0') {
909 fail:
910 *q = '\0';
911 *pp = p;
912 return -1;
913 }
914 if (*p == '\"') {
915 p++;
916 while (*p != '\0' && *p != '\"') {
917 if (*p == '\\') {
918 p++;
919 c = *p++;
920 switch (c) {
921 case 'n':
922 c = '\n';
923 break;
924 case 'r':
925 c = '\r';
926 break;
927 case '\\':
928 case '\'':
929 case '\"':
930 break;
931 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100932 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800933 goto fail;
934 }
935 if ((q - buf) < buf_size - 1) {
936 *q++ = c;
937 }
938 } else {
939 if ((q - buf) < buf_size - 1) {
940 *q++ = *p;
941 }
942 p++;
943 }
944 }
945 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100946 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800947 goto fail;
948 }
949 p++;
950 } else {
951 while (*p != '\0' && !qemu_isspace(*p)) {
952 if ((q - buf) < buf_size - 1) {
953 *q++ = *p;
954 }
955 p++;
956 }
957 }
958 *q = '\0';
959 *pp = p;
960 return 0;
961}
962
963#define MAX_ARGS 16
964
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800965static void free_cmdline_args(char **args, int nb_args)
966{
967 int i;
968
969 assert(nb_args <= MAX_ARGS);
970
971 for (i = 0; i < nb_args; i++) {
972 g_free(args[i]);
973 }
974
975}
976
977/*
978 * Parse the command line to get valid args.
979 * @cmdline: command line to be parsed.
980 * @pnb_args: location to store the number of args, must NOT be NULL.
981 * @args: location to store the args, which should be freed by caller, must
982 * NOT be NULL.
983 *
984 * Returns 0 on success, negative on failure.
985 *
986 * NOTE: this parser is an approximate form of the real command parser. Number
987 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
988 * return with failure.
989 */
990static int parse_cmdline(const char *cmdline,
991 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800992{
993 const char *p;
994 int nb_args, ret;
995 char buf[1024];
996
997 p = cmdline;
998 nb_args = 0;
999 for (;;) {
1000 while (qemu_isspace(*p)) {
1001 p++;
1002 }
1003 if (*p == '\0') {
1004 break;
1005 }
1006 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08001007 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +08001008 }
1009 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08001010 if (ret < 0) {
1011 goto fail;
1012 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +08001013 args[nb_args] = g_strdup(buf);
1014 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +08001015 }
1016 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08001017 return 0;
1018
1019 fail:
1020 free_cmdline_args(args, nb_args);
1021 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +08001022}
1023
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +01001024/*
Markus Armbruster774a6b62018-07-03 10:53:57 +02001025 * Can command @cmd be executed in preconfig state?
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +01001026 */
1027static bool cmd_can_preconfig(const mon_cmd_t *cmd)
1028{
1029 if (!cmd->flags) {
1030 return false;
1031 }
1032
1033 return strchr(cmd->flags, 'p');
1034}
1035
Wenchao Xia66855492013-08-27 20:38:23 +08001036static void help_cmd_dump_one(Monitor *mon,
1037 const mon_cmd_t *cmd,
1038 char **prefix_args,
1039 int prefix_args_nb)
1040{
1041 int i;
1042
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +01001043 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
1044 return;
1045 }
1046
Wenchao Xia66855492013-08-27 20:38:23 +08001047 for (i = 0; i < prefix_args_nb; i++) {
1048 monitor_printf(mon, "%s ", prefix_args[i]);
1049 }
1050 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
1051}
1052
1053/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -05001054static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +08001055 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +00001056{
Anthony Liguoric227f092009-10-01 16:12:16 -05001057 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +00001058
Wenchao Xia66855492013-08-27 20:38:23 +08001059 /* No valid arg need to compare with, dump all in *cmds */
1060 if (arg_index >= nb_args) {
1061 for (cmd = cmds; cmd->name != NULL; cmd++) {
1062 help_cmd_dump_one(mon, cmd, args, arg_index);
1063 }
1064 return;
1065 }
1066
1067 /* Find one entry to dump */
1068 for (cmd = cmds; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert31785f12018-06-20 16:39:42 +01001069 if (compare_cmd(args[arg_index], cmd->name) &&
1070 ((!runstate_check(RUN_STATE_PRECONFIG) ||
1071 cmd_can_preconfig(cmd)))) {
Wenchao Xia66855492013-08-27 20:38:23 +08001072 if (cmd->sub_table) {
1073 /* continue with next arg */
1074 help_cmd_dump(mon, cmd->sub_table,
1075 args, nb_args, arg_index + 1);
1076 } else {
1077 help_cmd_dump_one(mon, cmd, args, arg_index);
1078 }
1079 break;
1080 }
bellard9dc39cb2004-03-14 21:38:27 +00001081 }
1082}
1083
aliguori376253e2009-03-05 23:01:23 +00001084static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +00001085{
Wenchao Xia66855492013-08-27 20:38:23 +08001086 char *args[MAX_ARGS];
1087 int nb_args = 0;
1088
1089 /* 1. parse user input */
1090 if (name) {
1091 /* special case for log, directly dump and return */
1092 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +00001093 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +00001094 monitor_printf(mon, "Log items (comma separated):\n");
1095 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +00001096 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +00001097 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +00001098 }
Wenchao Xia66855492013-08-27 20:38:23 +08001099 return;
1100 }
1101
1102 if (parse_cmdline(name, &nb_args, args) < 0) {
1103 return;
bellardf193c792004-03-21 17:06:25 +00001104 }
bellard9dc39cb2004-03-14 21:38:27 +00001105 }
Wenchao Xia66855492013-08-27 20:38:23 +08001106
1107 /* 2. dump the contents according to parsed args */
1108 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
1109
1110 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +00001111}
1112
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001113static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -03001114{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001115 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -03001116}
1117
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001118static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301119{
1120 const char *tp_name = qdict_get_str(qdict, "name");
1121 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001122 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1123 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001124 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +00001125
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001126 if (vcpu < 0) {
1127 monitor_printf(mon, "argument vcpu must be positive");
1128 return;
1129 }
1130
1131 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001132 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +01001133 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +00001134 }
Prerna Saxena22890ab2010-06-24 17:04:53 +05301135}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001136
Michael Rothc45a8162011-10-02 08:44:37 -05001137#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001138static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001139{
1140 const char *op = qdict_get_try_str(qdict, "op");
1141 const char *arg = qdict_get_try_str(qdict, "arg");
1142
1143 if (!op) {
1144 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
1145 } else if (!strcmp(op, "on")) {
1146 st_set_trace_file_enabled(true);
1147 } else if (!strcmp(op, "off")) {
1148 st_set_trace_file_enabled(false);
1149 } else if (!strcmp(op, "flush")) {
1150 st_flush_trace_buffer();
1151 } else if (!strcmp(op, "set")) {
1152 if (arg) {
1153 st_set_trace_file(arg);
1154 }
1155 } else {
1156 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1157 help_cmd(mon, "trace-file");
1158 }
1159}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301160#endif
1161
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001162static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001163{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001164 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001165}
1166
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001167static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +00001168{
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001169 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001170
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001171 if (!cmd->enabled) {
1172 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001173 }
1174
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001175 info = g_malloc0(sizeof(*info));
1176 info->value = g_malloc0(sizeof(*info->value));
1177 info->value->name = g_strdup(cmd->name);
1178 info->next = *list;
1179 *list = info;
1180}
1181
1182CommandInfoList *qmp_query_commands(Error **errp)
1183{
1184 CommandInfoList *list = NULL;
1185
Markus Armbruster635db182017-03-03 13:32:27 +01001186 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001187
1188 return list;
thsa36e69d2007-12-02 05:18:19 +00001189}
1190
Daniel P. Berrange48608532012-05-21 17:59:51 +01001191EventInfoList *qmp_query_events(Error **errp)
1192{
1193 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +02001194 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +01001195
Eric Blake7fb1cf12015-11-18 01:52:57 -07001196 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001197 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +01001198 assert(event_name != NULL);
1199 info = g_malloc0(sizeof(*info));
1200 info->value = g_malloc0(sizeof(*info->value));
1201 info->value->name = g_strdup(event_name);
1202
1203 info->next = ev_list;
1204 ev_list = info;
1205 }
1206
1207 return ev_list;
1208}
1209
Markus Armbruster39a18152015-09-16 13:06:28 +02001210/*
1211 * Minor hack: generated marshalling suppressed for this command
1212 * ('gen': false in the schema) so we can parse the JSON string
1213 * directly into QObject instead of first parsing it with
1214 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1215 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01001216 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -06001217 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +02001218 */
1219static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1220 Error **errp)
1221{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001222 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
Markus Armbruster39a18152015-09-16 13:06:28 +02001223}
1224
Marc-André Lureau5032a162016-09-12 13:19:02 +04001225/*
Marc-André Lureau5032a162016-09-12 13:19:02 +04001226 * We used to define commands in qmp-commands.hx in addition to the
1227 * QAPI schema. This permitted defining some of them only in certain
1228 * configurations. query-commands has always reflected that (good,
1229 * because it lets QMP clients figure out what's actually available),
1230 * while query-qmp-schema never did (not so good). This function is a
1231 * hack to keep the configuration-specific commands defined exactly as
1232 * before, even though qmp-commands.hx is gone.
1233 *
1234 * FIXME Educate the QAPI schema on configuration-specific commands,
1235 * and drop this hack.
1236 */
1237static void qmp_unregister_commands_hack(void)
1238{
Markus Armbruster38bb54f2017-04-27 15:00:53 +02001239#ifndef CONFIG_REPLICATION
1240 qmp_unregister_command(&qmp_commands, "xen-set-replication");
1241 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
1242 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
1243#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001244#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +01001245 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Brijesh Singh08a161f2018-03-08 06:48:42 -06001246 qmp_unregister_command(&qmp_commands, "query-sev");
Brijesh Singh1b6a0342018-03-08 06:48:56 -06001247 qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
Brijesh Singh31dd67f2018-03-08 06:48:59 -06001248 qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001249#endif
1250#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +01001251 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001252#endif
1253#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +01001254 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001255#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001256#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001257 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001258#endif
1259#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001260 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
1261 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001262#endif
1263#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1264 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001265 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001266#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001267}
1268
Peter Xu6adf08d2018-03-09 16:59:50 +08001269static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001270{
Markus Armbruster635db182017-03-03 13:32:27 +01001271 /*
1272 * Two command lists:
1273 * - qmp_commands contains all QMP commands
1274 * - qmp_cap_negotiation_commands contains just
1275 * "qmp_capabilities", to enforce capability negotiation
1276 */
1277
Markus Armbruster1527bad2017-03-03 13:32:25 +01001278 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001279
Markus Armbruster1527bad2017-03-03 13:32:25 +01001280 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001281 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001282 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001283 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001284 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001285 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001286
Markus Armbruster05875682017-03-03 13:32:24 +01001287 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001288
1289 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1290 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001291 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +01001292}
1293
Peter Xu71da4662018-03-09 16:59:57 +08001294static bool qmp_oob_enabled(Monitor *mon)
1295{
Markus Armbruster279f9e02018-07-03 10:53:56 +02001296 return mon->qmp.capab[QMP_CAPABILITY_OOB];
Peter Xu71da4662018-03-09 16:59:57 +08001297}
1298
Markus Armbruster279f9e02018-07-03 10:53:56 +02001299static void monitor_qmp_caps_reset(Monitor *mon)
Markus Armbruster635db182017-03-03 13:32:27 +01001300{
Markus Armbruster279f9e02018-07-03 10:53:56 +02001301 memset(mon->qmp.capab_offered, 0, sizeof(mon->qmp.capab_offered));
1302 memset(mon->qmp.capab, 0, sizeof(mon->qmp.capab));
1303 mon->qmp.capab_offered[QMP_CAPABILITY_OOB] = mon->use_io_thread;
Peter Xu02130312018-03-09 16:59:53 +08001304}
1305
Peter Xucf869d52018-03-10 20:38:05 -06001306/*
Markus Armbruster279f9e02018-07-03 10:53:56 +02001307 * Accept QMP capabilities in @list for @mon.
1308 * On success, set mon->qmp.capab[], and return true.
1309 * On error, set @errp, and return false.
Peter Xucf869d52018-03-10 20:38:05 -06001310 */
Markus Armbruster279f9e02018-07-03 10:53:56 +02001311static bool qmp_caps_accept(Monitor *mon, QMPCapabilityList *list,
1312 Error **errp)
Peter Xucf869d52018-03-10 20:38:05 -06001313{
Markus Armbruster279f9e02018-07-03 10:53:56 +02001314 GString *unavailable = NULL;
1315 bool capab[QMP_CAPABILITY__MAX];
Peter Xucf869d52018-03-10 20:38:05 -06001316
Markus Armbruster279f9e02018-07-03 10:53:56 +02001317 memset(capab, 0, sizeof(capab));
1318
Peter Xu02130312018-03-09 16:59:53 +08001319 for (; list; list = list->next) {
Markus Armbruster279f9e02018-07-03 10:53:56 +02001320 if (!mon->qmp.capab_offered[list->value]) {
1321 if (!unavailable) {
1322 unavailable = g_string_new(QMPCapability_str(list->value));
1323 } else {
1324 g_string_append_printf(unavailable, ", %s",
1325 QMPCapability_str(list->value));
Peter Xu02130312018-03-09 16:59:53 +08001326 }
Peter Xu02130312018-03-09 16:59:53 +08001327 }
Markus Armbruster279f9e02018-07-03 10:53:56 +02001328 capab[list->value] = true;
Peter Xu02130312018-03-09 16:59:53 +08001329 }
Peter Xu02130312018-03-09 16:59:53 +08001330
Markus Armbruster279f9e02018-07-03 10:53:56 +02001331 if (unavailable) {
1332 error_setg(errp, "Capability %s not available", unavailable->str);
1333 g_string_free(unavailable, true);
Peter Xucf869d52018-03-10 20:38:05 -06001334 return false;
1335 }
1336
Markus Armbruster279f9e02018-07-03 10:53:56 +02001337 memcpy(mon->qmp.capab, capab, sizeof(capab));
Peter Xucf869d52018-03-10 20:38:05 -06001338 return true;
1339}
1340
Peter Xu02130312018-03-09 16:59:53 +08001341void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
1342 Error **errp)
1343{
Markus Armbruster635db182017-03-03 13:32:27 +01001344 if (cur_mon->qmp.commands == &qmp_commands) {
1345 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1346 "Capabilities negotiation is already complete, command "
1347 "ignored");
1348 return;
1349 }
1350
Markus Armbruster279f9e02018-07-03 10:53:56 +02001351 if (!qmp_caps_accept(cur_mon, enable, errp)) {
1352 return;
Peter Xu02130312018-03-09 16:59:53 +08001353 }
1354
Markus Armbruster635db182017-03-03 13:32:27 +01001355 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001356}
1357
Peter Xud9f25282018-06-08 11:55:07 +08001358/* Set the current CPU defined by the user. Callers must hold BQL. */
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001359int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001360{
Andreas Färber55e5c282012-12-17 06:18:02 +01001361 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001362
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001363 cpu = qemu_get_cpu(cpu_index);
1364 if (cpu == NULL) {
1365 return -1;
bellard6a00d602005-11-21 23:25:50 +00001366 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001367 g_free(cur_mon->mon_cpu_path);
1368 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001369 return 0;
bellard6a00d602005-11-21 23:25:50 +00001370}
1371
Peter Xud9f25282018-06-08 11:55:07 +08001372/* Callers must hold BQL. */
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001373static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001374{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001375 CPUState *cpu;
1376
1377 if (cur_mon->mon_cpu_path) {
1378 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1379 TYPE_CPU, NULL);
1380 if (!cpu) {
1381 g_free(cur_mon->mon_cpu_path);
1382 cur_mon->mon_cpu_path = NULL;
1383 }
1384 }
1385 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001386 if (!first_cpu) {
1387 return NULL;
1388 }
David Gibson27a83f82016-09-21 15:29:26 +10001389 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001390 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001391 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001392 if (synchronize) {
1393 cpu_synchronize_state(cpu);
1394 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001395 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001396}
1397
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001398CPUState *mon_get_cpu(void)
1399{
1400 return mon_get_cpu_sync(true);
1401}
1402
Pavel Butsykinbf957282015-09-10 18:38:59 +03001403CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001404{
Thomas Huth854e67f2017-01-13 13:12:35 +01001405 CPUState *cs = mon_get_cpu();
1406
1407 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001408}
1409
Luiz Capitulino99b77962011-10-24 10:53:44 -02001410int monitor_get_cpu_index(void)
1411{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001412 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001413
1414 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001415}
1416
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001417static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001418{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001419 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1420 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001421
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001422 if (all_cpus) {
1423 CPU_FOREACH(cs) {
1424 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1425 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1426 }
1427 } else {
1428 cs = mon_get_cpu();
1429
1430 if (!cs) {
1431 monitor_printf(mon, "No CPU available\n");
1432 return;
1433 }
1434
1435 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001436 }
bellard9307c4c2004-04-04 12:57:25 +00001437}
1438
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001439#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001440static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001441{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001442 if (!tcg_enabled()) {
1443 error_report("JIT information is only available with accel=tcg");
1444 return;
1445 }
1446
aliguori376253e2009-03-05 23:01:23 +00001447 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001448 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001449}
1450
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001451static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001452{
1453 dump_opcount_info((FILE *)mon, monitor_fprintf);
1454}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001455#endif
Max Filippov246ae242014-11-02 11:04:18 +03001456
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001457static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001458{
1459 int i;
bellard7e2515e2004-08-01 21:52:19 +00001460 const char *str;
ths3b46e622007-09-17 08:09:54 +00001461
aliguoricde76ee2009-03-05 23:01:51 +00001462 if (!mon->rs)
1463 return;
bellard7e2515e2004-08-01 21:52:19 +00001464 i = 0;
1465 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001466 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001467 if (!str)
1468 break;
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001470 i++;
bellardaa455482004-04-04 13:07:25 +00001471 }
1472}
1473
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001474static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001475{
Thomas Huth854e67f2017-01-13 13:12:35 +01001476 CPUState *cs = mon_get_cpu();
1477
1478 if (!cs) {
1479 monitor_printf(mon, "No CPU available\n");
1480 return;
1481 }
1482 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001483}
j_mayer76a66252007-03-07 08:32:30 +00001484
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001485static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301486{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001487 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001488 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1489 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001490 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001491 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001492 Error *local_err = NULL;
1493
1494 if (name == NULL) {
1495 name = "*";
1496 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001497 if (vcpu < 0) {
1498 monitor_printf(mon, "argument vcpu must be positive");
1499 return;
1500 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001501
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001502 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001503 if (local_err) {
1504 error_report_err(local_err);
1505 return;
1506 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001507
1508 for (elem = events; elem != NULL; elem = elem->next) {
1509 monitor_printf(mon, "%s : state %u\n",
1510 elem->value->name,
1511 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1512 }
1513 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301514}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301515
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001516void qmp_client_migrate_info(const char *protocol, const char *hostname,
1517 bool has_port, int64_t port,
1518 bool has_tls_port, int64_t tls_port,
1519 bool has_cert_subject, const char *cert_subject,
1520 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001521{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001522 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001523 if (!qemu_using_spice(errp)) {
1524 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001525 }
1526
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001527 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001528 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001529 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001530 }
1531
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001532 if (qemu_spice_migrate_info(hostname,
1533 has_port ? port : -1,
1534 has_tls_port ? tls_port : -1,
1535 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001536 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001537 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001538 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001539 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001540 }
1541
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001542 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001543}
1544
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001545static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001546{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001547 Error *err = NULL;
1548
1549 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1550 if (err) {
1551 error_report_err(err);
1552 }
pbrooke735b912007-06-30 13:53:24 +00001553}
1554
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001555static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001556{
1557 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001558 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001559
bellard9307c4c2004-04-04 12:57:25 +00001560 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001561 mask = 0;
1562 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001563 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001564 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001565 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001566 return;
1567 }
1568 }
Peter Maydell24537a02013-02-11 16:41:23 +00001569 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001570}
1571
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001572static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001573{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001574 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001575 if (!option || !strcmp(option, "on")) {
1576 singlestep = 1;
1577 } else if (!strcmp(option, "off")) {
1578 singlestep = 0;
1579 } else {
1580 monitor_printf(mon, "unexpected option %s\n", option);
1581 }
1582}
1583
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001584static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001585{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001586 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001587 if (!device)
1588 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1589 if (gdbserver_start(device) < 0) {
1590 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1591 device);
1592 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001593 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001594 } else {
aliguori59030a82009-04-05 18:43:41 +00001595 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1596 device);
bellard8a7ddc32004-03-31 19:00:16 +00001597 }
1598}
1599
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001600static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001601{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001602 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001603 if (select_watchdog_action(action) == -1) {
1604 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1605 }
1606}
1607
aliguori376253e2009-03-05 23:01:23 +00001608static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001609{
aliguori376253e2009-03-05 23:01:23 +00001610 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001611 switch(c) {
1612 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001613 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001614 break;
1615 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001616 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001617 break;
1618 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001619 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001620 break;
1621 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001622 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001623 break;
1624 default:
1625 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001626 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001627 } else {
aliguori376253e2009-03-05 23:01:23 +00001628 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001629 }
1630 break;
1631 }
aliguori376253e2009-03-05 23:01:23 +00001632 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001633}
1634
aliguori376253e2009-03-05 23:01:23 +00001635static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001636 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001637{
Blue Swirl23842aa2010-01-12 20:27:43 +00001638 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001639 uint8_t buf[16];
1640 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001641 CPUState *cs = mon_get_cpu();
1642
1643 if (!cs && (format == 'i' || !is_physical)) {
1644 monitor_printf(mon, "Can not dump without CPU\n");
1645 return;
1646 }
bellard9307c4c2004-04-04 12:57:25 +00001647
1648 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001649 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001650 return;
1651 }
1652
1653 len = wsize * count;
1654 if (wsize == 1)
1655 line_size = 8;
1656 else
1657 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001658 max_digits = 0;
1659
1660 switch(format) {
1661 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001662 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001663 break;
1664 default:
1665 case 'x':
1666 max_digits = (wsize * 8) / 4;
1667 break;
1668 case 'u':
1669 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001670 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001671 break;
1672 case 'c':
1673 wsize = 1;
1674 break;
1675 }
1676
1677 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001678 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001679 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001680 else
aliguori376253e2009-03-05 23:01:23 +00001681 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001682 l = len;
1683 if (l > line_size)
1684 l = line_size;
1685 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001686 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001687 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001688 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001689 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001690 break;
1691 }
bellard9307c4c2004-04-04 12:57:25 +00001692 }
ths5fafdf22007-09-16 21:08:06 +00001693 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001694 while (i < l) {
1695 switch(wsize) {
1696 default:
1697 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001698 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001699 break;
1700 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001701 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001702 break;
1703 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001704 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001705 break;
1706 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001707 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001708 break;
1709 }
aliguori376253e2009-03-05 23:01:23 +00001710 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001711 switch(format) {
1712 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001713 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001714 break;
1715 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001716 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001717 break;
1718 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001719 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001720 break;
1721 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001722 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001723 break;
1724 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001725 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001726 break;
1727 }
1728 i += wsize;
1729 }
aliguori376253e2009-03-05 23:01:23 +00001730 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001731 addr += l;
1732 len -= l;
1733 }
1734}
1735
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001736static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001737{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001738 int count = qdict_get_int(qdict, "count");
1739 int format = qdict_get_int(qdict, "format");
1740 int size = qdict_get_int(qdict, "size");
1741 target_long addr = qdict_get_int(qdict, "addr");
1742
aliguori376253e2009-03-05 23:01:23 +00001743 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001744}
1745
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001746static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001747{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001748 int count = qdict_get_int(qdict, "count");
1749 int format = qdict_get_int(qdict, "format");
1750 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001751 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001752
aliguori376253e2009-03-05 23:01:23 +00001753 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001754}
1755
Paolo Bonzinie9628442017-04-20 15:30:58 +02001756static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1757{
1758 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1759 addr, 1);
1760
1761 if (!mrs.mr) {
1762 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1763 return NULL;
1764 }
1765
1766 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1767 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1768 memory_region_unref(mrs.mr);
1769 return NULL;
1770 }
1771
1772 *p_mr = mrs.mr;
1773 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1774}
1775
1776static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1777{
1778 hwaddr addr = qdict_get_int(qdict, "addr");
1779 Error *local_err = NULL;
1780 MemoryRegion *mr = NULL;
1781 void *ptr;
1782
1783 ptr = gpa2hva(&mr, addr, &local_err);
1784 if (local_err) {
1785 error_report_err(local_err);
1786 return;
1787 }
1788
1789 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1790 " (%s) is %p\n",
1791 addr, mr->name, ptr);
1792
1793 memory_region_unref(mr);
1794}
1795
1796#ifdef CONFIG_LINUX
1797static uint64_t vtop(void *ptr, Error **errp)
1798{
1799 uint64_t pinfo;
1800 uint64_t ret = -1;
1801 uintptr_t addr = (uintptr_t) ptr;
1802 uintptr_t pagesize = getpagesize();
1803 off_t offset = addr / pagesize * sizeof(pinfo);
1804 int fd;
1805
1806 fd = open("/proc/self/pagemap", O_RDONLY);
1807 if (fd == -1) {
1808 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1809 return -1;
1810 }
1811
1812 /* Force copy-on-write if necessary. */
1813 atomic_add((uint8_t *)ptr, 0);
1814
1815 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1816 error_setg_errno(errp, errno, "Cannot read pagemap");
1817 goto out;
1818 }
1819 if ((pinfo & (1ull << 63)) == 0) {
1820 error_setg(errp, "Page not present");
1821 goto out;
1822 }
1823 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1824
1825out:
1826 close(fd);
1827 return ret;
1828}
1829
1830static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1831{
1832 hwaddr addr = qdict_get_int(qdict, "addr");
1833 Error *local_err = NULL;
1834 MemoryRegion *mr = NULL;
1835 void *ptr;
1836 uint64_t physaddr;
1837
1838 ptr = gpa2hva(&mr, addr, &local_err);
1839 if (local_err) {
1840 error_report_err(local_err);
1841 return;
1842 }
1843
1844 physaddr = vtop(ptr, &local_err);
1845 if (local_err) {
1846 error_report_err(local_err);
1847 } else {
1848 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1849 " (%s) is 0x%" PRIx64 "\n",
1850 addr, mr->name, (uint64_t) physaddr);
1851 }
1852
1853 memory_region_unref(mr);
1854}
1855#endif
1856
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001857static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001858{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001859 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001860 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001861
bellard9307c4c2004-04-04 12:57:25 +00001862 switch(format) {
1863 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001864 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001865 break;
1866 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001867 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001868 break;
1869 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001870 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001871 break;
1872 default:
1873 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001874 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001875 break;
1876 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001877 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001878 break;
1879 }
aliguori376253e2009-03-05 23:01:23 +00001880 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001881}
1882
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001883static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001884{
1885 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001886 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001887 uint32_t start = qdict_get_int(qdict, "start");
1888 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001889
1890 sum = 0;
1891 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001892 uint8_t val = address_space_ldub(&address_space_memory, addr,
1893 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001894 /* BSD sum algorithm ('sum' Unix command) */
1895 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001896 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001897 }
aliguori376253e2009-03-05 23:01:23 +00001898 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001899}
1900
bellard13224a82006-07-14 22:03:35 +00001901static int mouse_button_state;
1902
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001903static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001904{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001905 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001906 const char *dx_str = qdict_get_str(qdict, "dx_str");
1907 const char *dy_str = qdict_get_str(qdict, "dy_str");
1908 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001909
bellard13224a82006-07-14 22:03:35 +00001910 dx = strtol(dx_str, NULL, 0);
1911 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001912 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1913 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1914
1915 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001916 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001917 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001918 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001919 qemu_input_queue_btn(NULL, button, true);
1920 qemu_input_event_sync();
1921 qemu_input_queue_btn(NULL, button, false);
1922 }
1923 }
1924 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001925}
1926
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001927static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001928{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001929 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001930 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1931 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1932 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1933 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001934 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001935
1936 if (mouse_button_state == button_state) {
1937 return;
1938 }
1939 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1940 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001941 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001942}
1943
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001944static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001945{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001946 int size = qdict_get_int(qdict, "size");
1947 int addr = qdict_get_int(qdict, "addr");
1948 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001949 uint32_t val;
1950 int suffix;
1951
1952 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001953 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001954 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001955 addr++;
1956 }
1957 addr &= 0xffff;
1958
1959 switch(size) {
1960 default:
1961 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001962 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001963 suffix = 'b';
1964 break;
1965 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001966 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001967 suffix = 'w';
1968 break;
1969 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001970 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001971 suffix = 'l';
1972 break;
1973 }
aliguori376253e2009-03-05 23:01:23 +00001974 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1975 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001976}
bellarda3a91a32004-06-04 11:06:21 +00001977
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001978static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001979{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001980 int size = qdict_get_int(qdict, "size");
1981 int addr = qdict_get_int(qdict, "addr");
1982 int val = qdict_get_int(qdict, "val");
1983
Jan Kiszkaf1147842009-07-14 10:20:11 +02001984 addr &= IOPORTS_MASK;
1985
1986 switch (size) {
1987 default:
1988 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001989 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001990 break;
1991 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001992 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001993 break;
1994 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001995 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001996 break;
1997 }
1998}
1999
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002000static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00002001{
Gongleif1839932014-12-03 18:20:58 +00002002 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002003 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00002004
Gongleif1839932014-12-03 18:20:58 +00002005 qemu_boot_set(bootdevice, &local_err);
2006 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01002007 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00002008 } else {
Gongleif1839932014-12-03 18:20:58 +00002009 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00002010 }
2011}
2012
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002013static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00002014{
Peter Xu57bb40c2017-01-16 16:40:05 +08002015 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10002016 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10002017 bool owner = qdict_get_try_bool(qdict, "owner", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08002018
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +10002019 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree,
2020 owner);
Blue Swirl314e2982011-09-11 20:22:05 +00002021}
2022
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002023static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00002024{
aliguorib28b6232009-04-22 20:20:29 +00002025 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002026 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02002027 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00002028
Igor Mammedovf75cd442017-05-30 18:23:59 +02002029 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002030 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
2031
zhanghailiang5b009e42014-11-04 19:49:30 +08002032 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002033 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2034 for (i = 0; i < nb_numa_nodes; i++) {
2035 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02002036 for (cpu = cpu_list; cpu; cpu = cpu->next) {
2037 if (cpu->value->has_props && cpu->value->props->has_node_id &&
2038 cpu->value->props->node_id == i) {
2039 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00002040 }
2041 }
2042 monitor_printf(mon, "\n");
2043 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002044 node_mem[i].node_mem >> 20);
2045 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
2046 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00002047 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02002048 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08002049 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002050}
2051
bellard5f1ce942006-02-08 22:40:15 +00002052#ifdef CONFIG_PROFILER
2053
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002054int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002055int64_t dev_time;
2056
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002057static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002058{
aliguori376253e2009-03-05 23:01:23 +00002059 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302060 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00002061 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302062 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002063 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002064 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002065}
2066#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002067static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002068{
aliguori376253e2009-03-05 23:01:23 +00002069 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002070}
2071#endif
2072
bellardec36b692006-07-16 18:57:03 +00002073/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002074static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002075
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002076static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002077{
2078 int i;
2079 CaptureState *s;
2080
2081 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002082 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002083 s->ops.info (s->opaque);
2084 }
2085}
2086
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002087static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002088{
2089 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002090 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002091 CaptureState *s;
2092
2093 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2094 if (i == n) {
2095 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002096 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002097 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002098 return;
2099 }
2100 }
2101}
2102
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002103static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002104{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002105 const char *path = qdict_get_str(qdict, "path");
2106 int has_freq = qdict_haskey(qdict, "freq");
2107 int freq = qdict_get_try_int(qdict, "freq", -1);
2108 int has_bits = qdict_haskey(qdict, "bits");
2109 int bits = qdict_get_try_int(qdict, "bits", -1);
2110 int has_channels = qdict_haskey(qdict, "nchannels");
2111 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002112 CaptureState *s;
2113
Anthony Liguori7267c092011-08-20 22:09:37 -05002114 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002115
2116 freq = has_freq ? freq : 44100;
2117 bits = has_bits ? bits : 16;
2118 nchannels = has_channels ? nchannels : 2;
2119
2120 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002121 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002122 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002123 return;
bellardec36b692006-07-16 18:57:03 +00002124 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002125 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002126}
bellardec36b692006-07-16 18:57:03 +00002127
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002129{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002130 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002131
aliguori76655d62009-03-06 20:27:37 +00002132 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002133 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002134 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002135 return acl;
2136}
aliguori76655d62009-03-06 20:27:37 +00002137
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002138static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002140 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002141 qemu_acl *acl = find_acl(mon, aclname);
2142 qemu_acl_entry *entry;
2143 int i = 0;
2144
2145 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002146 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002147 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002148 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002149 i++;
2150 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002151 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002152 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002153 }
2154}
2155
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002156static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002158 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002159 qemu_acl *acl = find_acl(mon, aclname);
2160
2161 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002162 qemu_acl_reset(acl);
2163 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002164 }
2165}
aliguori76655d62009-03-06 20:27:37 +00002166
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002167static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002168{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002169 const char *aclname = qdict_get_str(qdict, "aclname");
2170 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002171 qemu_acl *acl = find_acl(mon, aclname);
2172
2173 if (acl) {
2174 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002175 acl->defaultDeny = 0;
2176 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002177 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002178 acl->defaultDeny = 1;
2179 monitor_printf(mon, "acl: policy set to 'deny'\n");
2180 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002181 monitor_printf(mon, "acl: unknown policy '%s', "
2182 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002183 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002184 }
2185}
aliguori76655d62009-03-06 20:27:37 +00002186
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002187static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002188{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002189 const char *aclname = qdict_get_str(qdict, "aclname");
2190 const char *match = qdict_get_str(qdict, "match");
2191 const char *policy = qdict_get_str(qdict, "policy");
2192 int has_index = qdict_haskey(qdict, "index");
2193 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002194 qemu_acl *acl = find_acl(mon, aclname);
2195 int deny, ret;
2196
2197 if (acl) {
2198 if (strcmp(policy, "allow") == 0) {
2199 deny = 0;
2200 } else if (strcmp(policy, "deny") == 0) {
2201 deny = 1;
2202 } else {
2203 monitor_printf(mon, "acl: unknown policy '%s', "
2204 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002205 return;
2206 }
aliguori28a76be2009-03-06 20:27:40 +00002207 if (has_index)
2208 ret = qemu_acl_insert(acl, deny, match, index);
2209 else
2210 ret = qemu_acl_append(acl, deny, match);
2211 if (ret < 0)
2212 monitor_printf(mon, "acl: unable to add acl entry\n");
2213 else
2214 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002215 }
2216}
aliguori76655d62009-03-06 20:27:37 +00002217
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002218static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002219{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002220 const char *aclname = qdict_get_str(qdict, "aclname");
2221 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002222 qemu_acl *acl = find_acl(mon, aclname);
2223 int ret;
aliguori76655d62009-03-06 20:27:37 +00002224
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002225 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002226 ret = qemu_acl_remove(acl, match);
2227 if (ret < 0)
2228 monitor_printf(mon, "acl: no matching acl entry\n");
2229 else
2230 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002231 }
2232}
2233
Corey Bryant208c9d12012-06-22 14:36:09 -04002234void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002235{
Anthony Liguoric227f092009-10-01 16:12:16 -05002236 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002237 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002238
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002239 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002240 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002241 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002242 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002243 }
2244
2245 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002246 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2248 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002249 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250 }
2251
Peter Xu9409fc02018-06-08 11:55:06 +08002252 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002253 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002254 if (strcmp(monfd->name, fdname) != 0) {
2255 continue;
2256 }
2257
Peter Xu9409fc02018-06-08 11:55:06 +08002258 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002259 monfd->fd = fd;
Peter Xu9409fc02018-06-08 11:55:06 +08002260 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02002261 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08002262 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002263 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002264 }
2265
Anthony Liguori7267c092011-08-20 22:09:37 -05002266 monfd = g_malloc0(sizeof(mon_fd_t));
2267 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002268 monfd->fd = fd;
2269
Corey Bryant208c9d12012-06-22 14:36:09 -04002270 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002271 qemu_mutex_unlock(&cur_mon->mon_lock);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002272}
2273
Corey Bryant208c9d12012-06-22 14:36:09 -04002274void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002275{
Anthony Liguoric227f092009-10-01 16:12:16 -05002276 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002277 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002278
Peter Xu9409fc02018-06-08 11:55:06 +08002279 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002280 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002281 if (strcmp(monfd->name, fdname) != 0) {
2282 continue;
2283 }
2284
Blue Swirl72cf2d42009-09-12 07:36:22 +00002285 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002286 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05002287 g_free(monfd->name);
2288 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002289 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02002290 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08002291 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002292 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002293 }
2294
Peter Xu9409fc02018-06-08 11:55:06 +08002295 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002296 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002297}
2298
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002299int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002300{
Anthony Liguoric227f092009-10-01 16:12:16 -05002301 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002302
Peter Xu9409fc02018-06-08 11:55:06 +08002303 qemu_mutex_lock(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002304 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002305 int fd;
2306
2307 if (strcmp(monfd->name, fdname) != 0) {
2308 continue;
2309 }
2310
2311 fd = monfd->fd;
2312
2313 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002314 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002315 g_free(monfd->name);
2316 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002317 qemu_mutex_unlock(&mon->mon_lock);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002318
2319 return fd;
2320 }
2321
Peter Xu9409fc02018-06-08 11:55:06 +08002322 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002323 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002324 return -1;
2325}
2326
Corey Bryantba1c0482012-08-14 16:43:43 -04002327static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2328{
2329 MonFdsetFd *mon_fdset_fd;
2330 MonFdsetFd *mon_fdset_fd_next;
2331
2332 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002333 if ((mon_fdset_fd->removed ||
2334 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2335 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002336 close(mon_fdset_fd->fd);
2337 g_free(mon_fdset_fd->opaque);
2338 QLIST_REMOVE(mon_fdset_fd, next);
2339 g_free(mon_fdset_fd);
2340 }
2341 }
2342
Corey Bryantadb696f2012-08-14 16:43:47 -04002343 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002344 QLIST_REMOVE(mon_fdset, next);
2345 g_free(mon_fdset);
2346 }
2347}
2348
Corey Bryantefb87c12012-08-14 16:43:48 -04002349static void monitor_fdsets_cleanup(void)
2350{
2351 MonFdset *mon_fdset;
2352 MonFdset *mon_fdset_next;
2353
Peter Xu47451462018-06-08 11:55:11 +08002354 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002355 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2356 monitor_fdset_cleanup(mon_fdset);
2357 }
Peter Xu47451462018-06-08 11:55:11 +08002358 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04002359}
2360
Corey Bryantba1c0482012-08-14 16:43:43 -04002361AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2362 const char *opaque, Error **errp)
2363{
2364 int fd;
2365 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002366 AddfdInfo *fdinfo;
2367
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002368 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002369 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002370 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002371 goto error;
2372 }
2373
Corey Bryante446f702012-10-18 15:19:32 -04002374 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2375 has_opaque, opaque, errp);
2376 if (fdinfo) {
2377 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002378 }
2379
Corey Bryantba1c0482012-08-14 16:43:43 -04002380error:
2381 if (fd != -1) {
2382 close(fd);
2383 }
2384 return NULL;
2385}
2386
2387void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2388{
2389 MonFdset *mon_fdset;
2390 MonFdsetFd *mon_fdset_fd;
2391 char fd_str[60];
2392
Peter Xu47451462018-06-08 11:55:11 +08002393 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002394 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2395 if (mon_fdset->id != fdset_id) {
2396 continue;
2397 }
2398 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2399 if (has_fd) {
2400 if (mon_fdset_fd->fd != fd) {
2401 continue;
2402 }
2403 mon_fdset_fd->removed = true;
2404 break;
2405 } else {
2406 mon_fdset_fd->removed = true;
2407 }
2408 }
2409 if (has_fd && !mon_fdset_fd) {
2410 goto error;
2411 }
2412 monitor_fdset_cleanup(mon_fdset);
Peter Xu47451462018-06-08 11:55:11 +08002413 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002414 return;
2415 }
2416
2417error:
Peter Xu47451462018-06-08 11:55:11 +08002418 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002419 if (has_fd) {
2420 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2421 fdset_id, fd);
2422 } else {
2423 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2424 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002425 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002426}
2427
2428FdsetInfoList *qmp_query_fdsets(Error **errp)
2429{
2430 MonFdset *mon_fdset;
2431 MonFdsetFd *mon_fdset_fd;
2432 FdsetInfoList *fdset_list = NULL;
2433
Peter Xu47451462018-06-08 11:55:11 +08002434 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002435 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2436 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2437 FdsetFdInfoList *fdsetfd_list = NULL;
2438
2439 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2440 fdset_info->value->fdset_id = mon_fdset->id;
2441
2442 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2443 FdsetFdInfoList *fdsetfd_info;
2444
2445 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2446 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2447 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2448 if (mon_fdset_fd->opaque) {
2449 fdsetfd_info->value->has_opaque = true;
2450 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2451 } else {
2452 fdsetfd_info->value->has_opaque = false;
2453 }
2454
2455 fdsetfd_info->next = fdsetfd_list;
2456 fdsetfd_list = fdsetfd_info;
2457 }
2458
2459 fdset_info->value->fds = fdsetfd_list;
2460
2461 fdset_info->next = fdset_list;
2462 fdset_list = fdset_info;
2463 }
Peter Xu47451462018-06-08 11:55:11 +08002464 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04002465
2466 return fdset_list;
2467}
2468
Corey Bryante446f702012-10-18 15:19:32 -04002469AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2470 bool has_opaque, const char *opaque,
2471 Error **errp)
2472{
2473 MonFdset *mon_fdset = NULL;
2474 MonFdsetFd *mon_fdset_fd;
2475 AddfdInfo *fdinfo;
2476
Peter Xu47451462018-06-08 11:55:11 +08002477 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002478 if (has_fdset_id) {
2479 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2480 /* Break if match found or match impossible due to ordering by ID */
2481 if (fdset_id <= mon_fdset->id) {
2482 if (fdset_id < mon_fdset->id) {
2483 mon_fdset = NULL;
2484 }
2485 break;
2486 }
2487 }
2488 }
2489
2490 if (mon_fdset == NULL) {
2491 int64_t fdset_id_prev = -1;
2492 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2493
2494 if (has_fdset_id) {
2495 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002496 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2497 "a non-negative value");
Peter Xu47451462018-06-08 11:55:11 +08002498 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002499 return NULL;
2500 }
2501 /* Use specified fdset ID */
2502 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2503 mon_fdset_cur = mon_fdset;
2504 if (fdset_id < mon_fdset_cur->id) {
2505 break;
2506 }
2507 }
2508 } else {
2509 /* Use first available fdset ID */
2510 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2511 mon_fdset_cur = mon_fdset;
2512 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2513 fdset_id_prev = mon_fdset_cur->id;
2514 continue;
2515 }
2516 break;
2517 }
2518 }
2519
2520 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2521 if (has_fdset_id) {
2522 mon_fdset->id = fdset_id;
2523 } else {
2524 mon_fdset->id = fdset_id_prev + 1;
2525 }
2526
2527 /* The fdset list is ordered by fdset ID */
2528 if (!mon_fdset_cur) {
2529 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2530 } else if (mon_fdset->id < mon_fdset_cur->id) {
2531 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2532 } else {
2533 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2534 }
2535 }
2536
2537 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2538 mon_fdset_fd->fd = fd;
2539 mon_fdset_fd->removed = false;
2540 if (has_opaque) {
2541 mon_fdset_fd->opaque = g_strdup(opaque);
2542 }
2543 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2544
2545 fdinfo = g_malloc0(sizeof(*fdinfo));
2546 fdinfo->fdset_id = mon_fdset->id;
2547 fdinfo->fd = mon_fdset_fd->fd;
2548
Peter Xu47451462018-06-08 11:55:11 +08002549 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04002550 return fdinfo;
2551}
2552
Corey Bryantadb696f2012-08-14 16:43:47 -04002553int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2554{
Peter Xu47451462018-06-08 11:55:11 +08002555#ifdef _WIN32
2556 return -ENOENT;
2557#else
Corey Bryantadb696f2012-08-14 16:43:47 -04002558 MonFdset *mon_fdset;
2559 MonFdsetFd *mon_fdset_fd;
2560 int mon_fd_flags;
Peter Xu47451462018-06-08 11:55:11 +08002561 int ret;
Corey Bryantadb696f2012-08-14 16:43:47 -04002562
Peter Xu47451462018-06-08 11:55:11 +08002563 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002564 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2565 if (mon_fdset->id != fdset_id) {
2566 continue;
2567 }
2568 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2569 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2570 if (mon_fd_flags == -1) {
Peter Xu47451462018-06-08 11:55:11 +08002571 ret = -errno;
2572 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002573 }
2574
2575 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Peter Xu47451462018-06-08 11:55:11 +08002576 ret = mon_fdset_fd->fd;
2577 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002578 }
2579 }
Peter Xu47451462018-06-08 11:55:11 +08002580 ret = -EACCES;
2581 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04002582 }
Peter Xu47451462018-06-08 11:55:11 +08002583 ret = -ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04002584
Peter Xu47451462018-06-08 11:55:11 +08002585out:
2586 qemu_mutex_unlock(&mon_fdsets_lock);
2587 return ret;
2588#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04002589}
2590
2591int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2592{
2593 MonFdset *mon_fdset;
2594 MonFdsetFd *mon_fdset_fd_dup;
2595
Peter Xu47451462018-06-08 11:55:11 +08002596 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002597 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2598 if (mon_fdset->id != fdset_id) {
2599 continue;
2600 }
2601 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2602 if (mon_fdset_fd_dup->fd == dup_fd) {
Peter Xu47451462018-06-08 11:55:11 +08002603 goto err;
Corey Bryantadb696f2012-08-14 16:43:47 -04002604 }
2605 }
2606 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2607 mon_fdset_fd_dup->fd = dup_fd;
2608 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Peter Xu47451462018-06-08 11:55:11 +08002609 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002610 return 0;
2611 }
Peter Xu47451462018-06-08 11:55:11 +08002612
2613err:
2614 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002615 return -1;
2616}
2617
2618static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2619{
2620 MonFdset *mon_fdset;
2621 MonFdsetFd *mon_fdset_fd_dup;
2622
Peter Xu47451462018-06-08 11:55:11 +08002623 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002624 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2625 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2626 if (mon_fdset_fd_dup->fd == dup_fd) {
2627 if (remove) {
2628 QLIST_REMOVE(mon_fdset_fd_dup, next);
2629 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2630 monitor_fdset_cleanup(mon_fdset);
2631 }
Peter Xu47451462018-06-08 11:55:11 +08002632 goto err;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002633 } else {
Peter Xu47451462018-06-08 11:55:11 +08002634 qemu_mutex_unlock(&mon_fdsets_lock);
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002635 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002636 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002637 }
2638 }
2639 }
Peter Xu47451462018-06-08 11:55:11 +08002640
2641err:
2642 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04002643 return -1;
2644}
2645
2646int monitor_fdset_dup_fd_find(int dup_fd)
2647{
2648 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2649}
2650
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002651void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002652{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002653 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002654}
2655
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002656int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002657{
2658 int fd;
2659 Error *local_err = NULL;
2660
2661 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002662 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002663 } else {
2664 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002665 if (fd == -1) {
2666 error_setg(&local_err, "Invalid file descriptor number '%s'",
2667 fdname);
2668 }
2669 }
2670 if (local_err) {
2671 error_propagate(errp, local_err);
2672 assert(fd == -1);
2673 } else {
2674 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002675 }
2676
2677 return fd;
2678}
2679
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002680/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002681static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002682#include "hmp-commands-info.h"
2683 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002684};
2685
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002686/* mon_cmds and info_cmds would be sorted at runtime */
2687static mon_cmd_t mon_cmds[] = {
2688#include "hmp-commands.h"
2689 { NULL, NULL, },
2690};
2691
bellard9307c4c2004-04-04 12:57:25 +00002692/*******************************************************************/
2693
2694static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002695static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002696
bellard9307c4c2004-04-04 12:57:25 +00002697
Stefan Weil9c3175c2013-08-22 21:30:09 +02002698static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2699expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002700{
Fam Zheng277acfe2013-08-20 10:58:21 +08002701 va_list ap;
2702 va_start(ap, fmt);
2703 monitor_vprintf(mon, fmt, ap);
2704 monitor_printf(mon, "\n");
2705 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002706 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002707}
2708
Markus Armbruster09b94182010-01-20 13:07:30 +01002709/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002710static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002711{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002712 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002713 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002714 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002715 uint64_t tmp = 0;
2716 int ret;
bellard92a31b12005-02-10 22:00:52 +00002717
Thomas Huth854e67f2017-01-13 13:12:35 +01002718 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002719 return -1;
2720 }
2721
2722 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002723 if (compare_cmd(name, md->name)) {
2724 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002725 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002726 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002727 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002728 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002729 switch(md->type) {
2730 case MD_I32:
2731 *pval = *(int32_t *)ptr;
2732 break;
2733 case MD_TLONG:
2734 *pval = *(target_long *)ptr;
2735 break;
2736 default:
2737 *pval = 0;
2738 break;
2739 }
bellard9307c4c2004-04-04 12:57:25 +00002740 }
2741 return 0;
2742 }
2743 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002744
Thomas Huth854e67f2017-01-13 13:12:35 +01002745 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002746 if (!ret) {
2747 *pval = (target_long) tmp;
2748 }
2749
2750 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002751}
2752
2753static void next(void)
2754{
Blue Swirl660f11b2009-07-31 21:16:51 +00002755 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002756 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002757 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002758 pch++;
2759 }
2760}
2761
aliguori376253e2009-03-05 23:01:23 +00002762static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002763
aliguori376253e2009-03-05 23:01:23 +00002764static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002765{
blueswir1c2efc952007-09-25 17:28:42 +00002766 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002767 char *p;
bellard6a00d602005-11-21 23:25:50 +00002768 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002769
2770 switch(*pch) {
2771 case '+':
2772 next();
aliguori376253e2009-03-05 23:01:23 +00002773 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002774 break;
2775 case '-':
2776 next();
aliguori376253e2009-03-05 23:01:23 +00002777 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002778 break;
2779 case '~':
2780 next();
aliguori376253e2009-03-05 23:01:23 +00002781 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002782 break;
2783 case '(':
2784 next();
aliguori376253e2009-03-05 23:01:23 +00002785 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002786 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002787 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002788 }
2789 next();
2790 break;
bellard81d09122004-07-14 17:21:37 +00002791 case '\'':
2792 pch++;
2793 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002794 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002795 n = *pch;
2796 pch++;
2797 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002798 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002799 next();
2800 break;
bellard9307c4c2004-04-04 12:57:25 +00002801 case '$':
2802 {
2803 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002804 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002805
bellard9307c4c2004-04-04 12:57:25 +00002806 pch++;
2807 q = buf;
2808 while ((*pch >= 'a' && *pch <= 'z') ||
2809 (*pch >= 'A' && *pch <= 'Z') ||
2810 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002811 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002812 if ((q - buf) < sizeof(buf) - 1)
2813 *q++ = *pch;
2814 pch++;
2815 }
blueswir1cd390082008-11-16 13:53:32 +00002816 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002817 pch++;
2818 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002819 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002820 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002821 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002822 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002823 }
2824 break;
2825 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002826 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002827 n = 0;
2828 break;
2829 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002830 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002831 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002832 if (errno == ERANGE) {
2833 expr_error(mon, "number too large");
2834 }
bellard9307c4c2004-04-04 12:57:25 +00002835 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002836 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002837 }
2838 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002839 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002840 pch++;
2841 break;
2842 }
2843 return n;
2844}
2845
2846
aliguori376253e2009-03-05 23:01:23 +00002847static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002848{
blueswir1c2efc952007-09-25 17:28:42 +00002849 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002850 int op;
ths3b46e622007-09-17 08:09:54 +00002851
aliguori376253e2009-03-05 23:01:23 +00002852 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002853 for(;;) {
2854 op = *pch;
2855 if (op != '*' && op != '/' && op != '%')
2856 break;
2857 next();
aliguori376253e2009-03-05 23:01:23 +00002858 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002859 switch(op) {
2860 default:
2861 case '*':
2862 val *= val2;
2863 break;
2864 case '/':
2865 case '%':
ths5fafdf22007-09-16 21:08:06 +00002866 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002867 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002868 if (op == '/')
2869 val /= val2;
2870 else
2871 val %= val2;
2872 break;
2873 }
2874 }
2875 return val;
2876}
2877
aliguori376253e2009-03-05 23:01:23 +00002878static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002879{
blueswir1c2efc952007-09-25 17:28:42 +00002880 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002881 int op;
bellard9307c4c2004-04-04 12:57:25 +00002882
aliguori376253e2009-03-05 23:01:23 +00002883 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002884 for(;;) {
2885 op = *pch;
2886 if (op != '&' && op != '|' && op != '^')
2887 break;
2888 next();
aliguori376253e2009-03-05 23:01:23 +00002889 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002890 switch(op) {
2891 default:
2892 case '&':
2893 val &= val2;
2894 break;
2895 case '|':
2896 val |= val2;
2897 break;
2898 case '^':
2899 val ^= val2;
2900 break;
2901 }
2902 }
2903 return val;
2904}
2905
aliguori376253e2009-03-05 23:01:23 +00002906static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002907{
blueswir1c2efc952007-09-25 17:28:42 +00002908 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002909 int op;
bellard9307c4c2004-04-04 12:57:25 +00002910
aliguori376253e2009-03-05 23:01:23 +00002911 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002912 for(;;) {
2913 op = *pch;
2914 if (op != '+' && op != '-')
2915 break;
2916 next();
aliguori376253e2009-03-05 23:01:23 +00002917 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002918 if (op == '+')
2919 val += val2;
2920 else
2921 val -= val2;
2922 }
2923 return val;
2924}
2925
aliguori376253e2009-03-05 23:01:23 +00002926static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002927{
2928 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002929 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002930 *pp = pch;
2931 return -1;
2932 }
blueswir1cd390082008-11-16 13:53:32 +00002933 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002934 pch++;
aliguori376253e2009-03-05 23:01:23 +00002935 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002936 *pp = pch;
2937 return 0;
2938}
2939
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002940static int get_double(Monitor *mon, double *pval, const char **pp)
2941{
2942 const char *p = *pp;
2943 char *tailp;
2944 double d;
2945
2946 d = strtod(p, &tailp);
2947 if (tailp == p) {
2948 monitor_printf(mon, "Number expected\n");
2949 return -1;
2950 }
2951 if (d != d || d - d != 0) {
2952 /* NaN or infinity */
2953 monitor_printf(mon, "Bad number\n");
2954 return -1;
2955 }
2956 *pval = d;
2957 *pp = tailp;
2958 return 0;
2959}
2960
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002961/*
2962 * Store the command-name in cmdname, and return a pointer to
2963 * the remaining of the command string.
2964 */
2965static const char *get_command_name(const char *cmdline,
2966 char *cmdname, size_t nlen)
2967{
2968 size_t len;
2969 const char *p, *pstart;
2970
2971 p = cmdline;
2972 while (qemu_isspace(*p))
2973 p++;
2974 if (*p == '\0')
2975 return NULL;
2976 pstart = p;
2977 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2978 p++;
2979 len = p - pstart;
2980 if (len > nlen - 1)
2981 len = nlen - 1;
2982 memcpy(cmdname, pstart, len);
2983 cmdname[len] = '\0';
2984 return p;
2985}
2986
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002987/**
2988 * Read key of 'type' into 'key' and return the current
2989 * 'type' pointer.
2990 */
2991static char *key_get_info(const char *type, char **key)
2992{
2993 size_t len;
2994 char *p, *str;
2995
2996 if (*type == ',')
2997 type++;
2998
2999 p = strchr(type, ':');
3000 if (!p) {
3001 *key = NULL;
3002 return NULL;
3003 }
3004 len = p - type;
3005
Anthony Liguori7267c092011-08-20 22:09:37 -05003006 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003007 memcpy(str, type, len);
3008 str[len] = '\0';
3009
3010 *key = str;
3011 return ++p;
3012}
3013
bellard9307c4c2004-04-04 12:57:25 +00003014static int default_fmt_format = 'x';
3015static int default_fmt_size = 4;
3016
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003017static int is_valid_option(const char *c, const char *typestr)
3018{
3019 char option[3];
3020
3021 option[0] = '-';
3022 option[1] = *c;
3023 option[2] = '\0';
3024
3025 typestr = strstr(typestr, option);
3026 return (typestr != NULL);
3027}
3028
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003029static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3030 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003031{
3032 const mon_cmd_t *cmd;
3033
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003034 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003035 if (compare_cmd(cmdname, cmd->name)) {
3036 return cmd;
3037 }
3038 }
3039
3040 return NULL;
3041}
3042
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003043/*
Bandan Dasae502122015-06-03 18:38:08 -04003044 * Parse command name from @cmdp according to command table @table.
3045 * If blank, return NULL.
3046 * Else, if no valid command can be found, report to @mon, and return
3047 * NULL.
3048 * Else, change @cmdp to point right behind the name, and return its
3049 * command table entry.
3050 * Do not assume the return value points into @table! It doesn't when
3051 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003052 */
Anthony Liguoric227f092009-10-01 16:12:16 -05003053static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003054 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04003055 const char **cmdp,
3056 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00003057{
Bandan Dasae502122015-06-03 18:38:08 -04003058 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05003059 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003060 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00003061
bellard9307c4c2004-04-04 12:57:25 +00003062 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04003063 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003064 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003065 return NULL;
ths3b46e622007-09-17 08:09:54 +00003066
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003067 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003068 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003069 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003070 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003071 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003072 }
Dr. David Alan Gilbertc3120f72018-06-20 16:39:41 +01003073 if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
3074 monitor_printf(mon, "Command '%.*s' not available with -preconfig "
3075 "until after exit_preconfig.\n",
3076 (int)(p - cmdp_start), cmdp_start);
3077 return NULL;
3078 }
bellard9307c4c2004-04-04 12:57:25 +00003079
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003080 /* filter out following useless space */
3081 while (qemu_isspace(*p)) {
3082 p++;
3083 }
Bandan Dasae502122015-06-03 18:38:08 -04003084
3085 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003086 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003087 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003088 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003089 }
3090
Bandan Dasae502122015-06-03 18:38:08 -04003091 return cmd;
3092}
3093
3094/*
3095 * Parse arguments for @cmd.
3096 * If it can't be parsed, report to @mon, and return NULL.
3097 * Else, insert command arguments into a QDict, and return it.
3098 * Note: On success, caller has to free the QDict structure.
3099 */
3100
3101static QDict *monitor_parse_arguments(Monitor *mon,
3102 const char **endp,
3103 const mon_cmd_t *cmd)
3104{
3105 const char *typestr;
3106 char *key;
3107 int c;
3108 const char *p = *endp;
3109 char buf[1024];
3110 QDict *qdict = qdict_new();
3111
bellard9307c4c2004-04-04 12:57:25 +00003112 /* parse the parameters */
3113 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003114 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003115 typestr = key_get_info(typestr, &key);
3116 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003117 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003118 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003119 typestr++;
3120 switch(c) {
3121 case 'F':
bellard81d09122004-07-14 17:21:37 +00003122 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003123 case 's':
3124 {
3125 int ret;
ths3b46e622007-09-17 08:09:54 +00003126
blueswir1cd390082008-11-16 13:53:32 +00003127 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003128 p++;
3129 if (*typestr == '?') {
3130 typestr++;
3131 if (*p == '\0') {
3132 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003133 break;
bellard9307c4c2004-04-04 12:57:25 +00003134 }
3135 }
3136 ret = get_str(buf, sizeof(buf), &p);
3137 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003138 switch(c) {
3139 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003140 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003141 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003142 break;
3143 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003144 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003145 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003146 break;
3147 default:
Bandan Dasae502122015-06-03 18:38:08 -04003148 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003149 break;
3150 }
bellard9307c4c2004-04-04 12:57:25 +00003151 goto fail;
3152 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003153 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00003154 }
3155 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003156 case 'O':
3157 {
3158 QemuOptsList *opts_list;
3159 QemuOpts *opts;
3160
3161 opts_list = qemu_find_opts(key);
3162 if (!opts_list || opts_list->desc->name) {
3163 goto bad_type;
3164 }
3165 while (qemu_isspace(*p)) {
3166 p++;
3167 }
3168 if (!*p)
3169 break;
3170 if (get_str(buf, sizeof(buf), &p) < 0) {
3171 goto fail;
3172 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003173 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003174 if (!opts) {
3175 goto fail;
3176 }
3177 qemu_opts_to_qdict(opts, qdict);
3178 qemu_opts_del(opts);
3179 }
3180 break;
bellard9307c4c2004-04-04 12:57:25 +00003181 case '/':
3182 {
3183 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003184
blueswir1cd390082008-11-16 13:53:32 +00003185 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003186 p++;
3187 if (*p == '/') {
3188 /* format found */
3189 p++;
3190 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003191 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003192 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003193 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003194 count = count * 10 + (*p - '0');
3195 p++;
3196 }
3197 }
3198 size = -1;
3199 format = -1;
3200 for(;;) {
3201 switch(*p) {
3202 case 'o':
3203 case 'd':
3204 case 'u':
3205 case 'x':
3206 case 'i':
3207 case 'c':
3208 format = *p++;
3209 break;
3210 case 'b':
3211 size = 1;
3212 p++;
3213 break;
3214 case 'h':
3215 size = 2;
3216 p++;
3217 break;
3218 case 'w':
3219 size = 4;
3220 p++;
3221 break;
3222 case 'g':
3223 case 'L':
3224 size = 8;
3225 p++;
3226 break;
3227 default:
3228 goto next;
3229 }
3230 }
3231 next:
blueswir1cd390082008-11-16 13:53:32 +00003232 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003233 monitor_printf(mon, "invalid char in format: '%c'\n",
3234 *p);
bellard9307c4c2004-04-04 12:57:25 +00003235 goto fail;
3236 }
bellard9307c4c2004-04-04 12:57:25 +00003237 if (format < 0)
3238 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003239 if (format != 'i') {
3240 /* for 'i', not specifying a size gives -1 as size */
3241 if (size < 0)
3242 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003243 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003244 }
bellard9307c4c2004-04-04 12:57:25 +00003245 default_fmt_format = format;
3246 } else {
3247 count = 1;
3248 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003249 if (format != 'i') {
3250 size = default_fmt_size;
3251 } else {
3252 size = -1;
3253 }
bellard9307c4c2004-04-04 12:57:25 +00003254 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003255 qdict_put_int(qdict, "count", count);
3256 qdict_put_int(qdict, "format", format);
3257 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00003258 }
3259 break;
3260 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003261 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003262 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003263 {
blueswir1c2efc952007-09-25 17:28:42 +00003264 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003265
blueswir1cd390082008-11-16 13:53:32 +00003266 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003267 p++;
bellard34405572004-06-08 00:55:58 +00003268 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003269 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003270 if (*p == '\0') {
3271 typestr++;
3272 break;
3273 }
bellard34405572004-06-08 00:55:58 +00003274 } else {
3275 if (*p == '.') {
3276 p++;
blueswir1cd390082008-11-16 13:53:32 +00003277 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003278 p++;
bellard34405572004-06-08 00:55:58 +00003279 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003280 typestr++;
3281 break;
bellard34405572004-06-08 00:55:58 +00003282 }
3283 }
bellard13224a82006-07-14 22:03:35 +00003284 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003285 }
aliguori376253e2009-03-05 23:01:23 +00003286 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003287 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003288 /* Check if 'i' is greater than 32-bit */
3289 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003290 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003291 monitor_printf(mon, "integer is for 32-bit values\n");
3292 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003293 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003294 if (val < 0) {
3295 monitor_printf(mon, "enter a positive value\n");
3296 goto fail;
3297 }
Philippe Mathieu-Daudé8ec338a2018-06-25 09:42:34 -03003298 val *= MiB;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003299 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003300 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00003301 }
3302 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003303 case 'o':
3304 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003305 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003306 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003307 char *end;
3308
3309 while (qemu_isspace(*p)) {
3310 p++;
3311 }
3312 if (*typestr == '?') {
3313 typestr++;
3314 if (*p == '\0') {
3315 break;
3316 }
3317 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003318 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003319 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02003320 monitor_printf(mon, "invalid size\n");
3321 goto fail;
3322 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003323 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02003324 p = end;
3325 }
3326 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003327 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003328 {
3329 double val;
3330
3331 while (qemu_isspace(*p))
3332 p++;
3333 if (*typestr == '?') {
3334 typestr++;
3335 if (*p == '\0') {
3336 break;
3337 }
3338 }
3339 if (get_double(mon, &val, &p) < 0) {
3340 goto fail;
3341 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003342 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003343 switch (*p) {
3344 case 'm':
3345 val /= 1e3; p += 2; break;
3346 case 'u':
3347 val /= 1e6; p += 2; break;
3348 case 'n':
3349 val /= 1e9; p += 2; break;
3350 }
3351 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003352 if (*p && !qemu_isspace(*p)) {
3353 monitor_printf(mon, "Unknown unit suffix\n");
3354 goto fail;
3355 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04003356 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003357 }
3358 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003359 case 'b':
3360 {
3361 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003362 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003363
3364 while (qemu_isspace(*p)) {
3365 p++;
3366 }
3367 beg = p;
3368 while (qemu_isgraph(*p)) {
3369 p++;
3370 }
3371 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003372 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003373 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003374 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003375 } else {
3376 monitor_printf(mon, "Expected 'on' or 'off'\n");
3377 goto fail;
3378 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003379 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003380 }
3381 break;
bellard9307c4c2004-04-04 12:57:25 +00003382 case '-':
3383 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003384 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003385 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003386 /* option */
ths3b46e622007-09-17 08:09:54 +00003387
bellard9307c4c2004-04-04 12:57:25 +00003388 c = *typestr++;
3389 if (c == '\0')
3390 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003391 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003392 p++;
bellard9307c4c2004-04-04 12:57:25 +00003393 if (*p == '-') {
3394 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003395 if(c != *p) {
3396 if(!is_valid_option(p, typestr)) {
3397
3398 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003399 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003400 goto fail;
3401 } else {
3402 skip_key = 1;
3403 }
bellard9307c4c2004-04-04 12:57:25 +00003404 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003405 if(skip_key) {
3406 p = tmp;
3407 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003408 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003409 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003410 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003411 }
bellard9307c4c2004-04-04 12:57:25 +00003412 }
bellard9307c4c2004-04-04 12:57:25 +00003413 }
3414 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003415 case 'S':
3416 {
3417 /* package all remaining string */
3418 int len;
3419
3420 while (qemu_isspace(*p)) {
3421 p++;
3422 }
3423 if (*typestr == '?') {
3424 typestr++;
3425 if (*p == '\0') {
3426 /* no remaining string: NULL argument */
3427 break;
3428 }
3429 }
3430 len = strlen(p);
3431 if (len <= 0) {
3432 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003433 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003434 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003435 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003436 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003437 p += len;
3438 }
3439 break;
bellard9307c4c2004-04-04 12:57:25 +00003440 default:
3441 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003442 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003443 goto fail;
3444 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003445 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003446 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003447 }
3448 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003449 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003450 p++;
3451 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003452 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003453 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003454 goto fail;
3455 }
3456
Bandan Dasae502122015-06-03 18:38:08 -04003457 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003458
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003459fail:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003460 qobject_unref(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003461 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003462 return NULL;
3463}
3464
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003465static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003466{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003467 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003468 const mon_cmd_t *cmd;
Collin Walling317c52c2018-05-07 10:30:54 -04003469 const char *cmd_start = cmdline;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003470
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003471 trace_handle_hmp_command(mon, cmdline);
3472
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003473 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003474 if (!cmd) {
3475 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003476 }
3477
Bandan Dasae502122015-06-03 18:38:08 -04003478 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3479 if (!qdict) {
Collin Walling317c52c2018-05-07 10:30:54 -04003480 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
3481 cmdline--;
3482 }
3483 monitor_printf(mon, "Try \"help %.*s\" for more information\n",
3484 (int)(cmdline - cmd_start), cmd_start);
Bandan Dasae502122015-06-03 18:38:08 -04003485 return;
3486 }
3487
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003488 cmd->cmd(mon, qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003489 qobject_unref(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003490}
3491
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003492static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003493{
3494 const char *p, *pstart;
3495 char cmd[128];
3496 int len;
3497
3498 p = list;
3499 for(;;) {
3500 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +02003501 p = qemu_strchrnul(p, '|');
bellard81d09122004-07-14 17:21:37 +00003502 len = p - pstart;
3503 if (len > sizeof(cmd) - 2)
3504 len = sizeof(cmd) - 2;
3505 memcpy(cmd, pstart, len);
3506 cmd[len] = '\0';
3507 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003508 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003509 }
3510 if (*p == '\0')
3511 break;
3512 p++;
3513 }
3514}
3515
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003516static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003517{
3518 DIR *ffs;
3519 struct dirent *d;
3520 char path[1024];
3521 char file[1024], file_prefix[1024];
3522 int input_path_len;
3523 const char *p;
3524
ths5fafdf22007-09-16 21:08:06 +00003525 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003526 if (!p) {
3527 input_path_len = 0;
3528 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003529 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003530 } else {
3531 input_path_len = p - input + 1;
3532 memcpy(path, input, input_path_len);
3533 if (input_path_len > sizeof(path) - 1)
3534 input_path_len = sizeof(path) - 1;
3535 path[input_path_len] = '\0';
3536 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3537 }
Bandan Das19f2db52015-06-03 18:38:07 -04003538
bellard81d09122004-07-14 17:21:37 +00003539 ffs = opendir(path);
3540 if (!ffs)
3541 return;
3542 for(;;) {
3543 struct stat sb;
3544 d = readdir(ffs);
3545 if (!d)
3546 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003547
3548 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3549 continue;
3550 }
3551
bellard81d09122004-07-14 17:21:37 +00003552 if (strstart(d->d_name, file_prefix, NULL)) {
3553 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003554 if (input_path_len < sizeof(file))
3555 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3556 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003557 /* stat the file to find out if it's a directory.
3558 * In that case add a slash to speed up typing long paths
3559 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003560 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003561 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003562 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003563 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003564 }
3565 }
3566 closedir(ffs);
3567}
3568
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003569static const char *next_arg_type(const char *typestr)
3570{
3571 const char *p = strchr(typestr, ':');
3572 return (p != NULL ? ++p : typestr);
3573}
3574
Hani Benhabiles40d19392014-05-07 23:41:30 +01003575static void add_completion_option(ReadLineState *rs, const char *str,
3576 const char *option)
3577{
3578 if (!str || !option) {
3579 return;
3580 }
3581 if (!strncmp(option, str, strlen(str))) {
3582 readline_add_completion(rs, option);
3583 }
3584}
3585
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003586void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3587{
3588 size_t len;
3589 ChardevBackendInfoList *list, *start;
3590
3591 if (nb_args != 2) {
3592 return;
3593 }
3594 len = strlen(str);
3595 readline_set_completion_index(rs, len);
3596
3597 start = list = qmp_query_chardev_backends(NULL);
3598 while (list) {
3599 const char *chr_name = list->value->name;
3600
3601 if (!strncmp(chr_name, str, len)) {
3602 readline_add_completion(rs, chr_name);
3603 }
3604 list = list->next;
3605 }
3606 qapi_free_ChardevBackendInfoList(start);
3607}
3608
Hani Benhabilesb162b492014-05-07 23:41:31 +01003609void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3610{
3611 size_t len;
3612 int i;
3613
3614 if (nb_args != 2) {
3615 return;
3616 }
3617 len = strlen(str);
3618 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003619 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003620 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003621 }
3622}
3623
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003624void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003625{
3626 GSList *list, *elt;
3627 size_t len;
3628
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003629 if (nb_args != 2) {
3630 return;
3631 }
3632
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003633 len = strlen(str);
3634 readline_set_completion_index(rs, len);
3635 list = elt = object_class_get_list(TYPE_DEVICE, false);
3636 while (elt) {
3637 const char *name;
3638 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3639 TYPE_DEVICE);
3640 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003641
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003642 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003643 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003644 readline_add_completion(rs, name);
3645 }
3646 elt = elt->next;
3647 }
3648 g_slist_free(list);
3649}
3650
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003651void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003652{
3653 GSList *list, *elt;
3654 size_t len;
3655
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003656 if (nb_args != 2) {
3657 return;
3658 }
3659
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003660 len = strlen(str);
3661 readline_set_completion_index(rs, len);
3662 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3663 while (elt) {
3664 const char *name;
3665
3666 name = object_class_get_name(OBJECT_CLASS(elt->data));
3667 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3668 readline_add_completion(rs, name);
3669 }
3670 elt = elt->next;
3671 }
3672 g_slist_free(list);
3673}
3674
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003675static void peripheral_device_del_completion(ReadLineState *rs,
3676 const char *str, size_t len)
3677{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003678 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3679 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003680
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003681 list = qdev_build_hotpluggable_device_list(peripheral);
3682 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003683 return;
3684 }
3685
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003686 for (item = list; item; item = g_slist_next(item)) {
3687 DeviceState *dev = item->data;
3688
3689 if (dev->id && !strncmp(str, dev->id, len)) {
3690 readline_add_completion(rs, dev->id);
3691 }
3692 }
3693
3694 g_slist_free(list);
3695}
3696
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003697void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3698{
3699 size_t len;
3700 ChardevInfoList *list, *start;
3701
3702 if (nb_args != 2) {
3703 return;
3704 }
3705 len = strlen(str);
3706 readline_set_completion_index(rs, len);
3707
3708 start = list = qmp_query_chardev(NULL);
3709 while (list) {
3710 ChardevInfo *chr = list->value;
3711
3712 if (!strncmp(chr->label, str, len)) {
3713 readline_add_completion(rs, chr->label);
3714 }
3715 list = list->next;
3716 }
3717 qapi_free_ChardevInfoList(start);
3718}
3719
Hani Benhabiles8e597772014-05-27 23:39:30 +01003720static void ringbuf_completion(ReadLineState *rs, const char *str)
3721{
3722 size_t len;
3723 ChardevInfoList *list, *start;
3724
3725 len = strlen(str);
3726 readline_set_completion_index(rs, len);
3727
3728 start = list = qmp_query_chardev(NULL);
3729 while (list) {
3730 ChardevInfo *chr_info = list->value;
3731
3732 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003733 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003734 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003735 readline_add_completion(rs, chr_info->label);
3736 }
3737 }
3738 list = list->next;
3739 }
3740 qapi_free_ChardevInfoList(start);
3741}
3742
Hani Benhabiles8e597772014-05-27 23:39:30 +01003743void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3744{
3745 if (nb_args != 2) {
3746 return;
3747 }
3748 ringbuf_completion(rs, str);
3749}
3750
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003751void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3752{
3753 size_t len;
3754
3755 if (nb_args != 2) {
3756 return;
3757 }
3758
3759 len = strlen(str);
3760 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003761 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003762}
3763
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003764void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003765{
3766 ObjectPropertyInfoList *list, *start;
3767 size_t len;
3768
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003769 if (nb_args != 2) {
3770 return;
3771 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003772 len = strlen(str);
3773 readline_set_completion_index(rs, len);
3774
3775 start = list = qmp_qom_list("/objects", NULL);
3776 while (list) {
3777 ObjectPropertyInfo *info = list->value;
3778
3779 if (!strncmp(info->type, "child<", 5)
3780 && !strncmp(info->name, str, len)) {
3781 readline_add_completion(rs, info->name);
3782 }
3783 list = list->next;
3784 }
3785 qapi_free_ObjectPropertyInfoList(start);
3786}
3787
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003788void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3789{
3790 int i;
3791 char *sep;
3792 size_t len;
3793
3794 if (nb_args != 2) {
3795 return;
3796 }
3797 sep = strrchr(str, '-');
3798 if (sep) {
3799 str = sep + 1;
3800 }
3801 len = strlen(str);
3802 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003803 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003804 if (!strncmp(str, QKeyCode_str(i), len)) {
3805 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003806 }
3807 }
3808}
3809
Hani Benhabiles40d19392014-05-07 23:41:30 +01003810void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3811{
3812 size_t len;
3813
3814 len = strlen(str);
3815 readline_set_completion_index(rs, len);
3816 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003817 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003818 int count, i;
3819 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003820 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003821 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003822 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003823 const char *name = ncs[i]->name;
3824 if (!strncmp(str, name, len)) {
3825 readline_add_completion(rs, name);
3826 }
3827 }
3828 } else if (nb_args == 3) {
3829 add_completion_option(rs, str, "on");
3830 add_completion_option(rs, str, "off");
3831 }
3832}
3833
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003834void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3835{
3836 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003837 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003838
3839 if (nb_args != 2) {
3840 return;
3841 }
3842
3843 len = strlen(str);
3844 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003845 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003846 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003847 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003848 QemuOpts *opts;
3849 const char *name = ncs[i]->name;
3850 if (strncmp(str, name, len)) {
3851 continue;
3852 }
3853 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3854 if (opts) {
3855 readline_add_completion(rs, name);
3856 }
3857 }
3858}
3859
Lluís Vilanovabd712112016-07-11 12:53:51 +02003860void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3861{
3862 size_t len;
3863
3864 len = strlen(str);
3865 readline_set_completion_index(rs, len);
3866 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003867 TraceEventIter iter;
3868 TraceEvent *ev;
3869 char *pattern = g_strdup_printf("%s*", str);
3870 trace_event_iter_init(&iter, pattern);
3871 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3872 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003873 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003874 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003875 }
3876}
3877
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003878void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3879{
3880 size_t len;
3881
3882 len = strlen(str);
3883 readline_set_completion_index(rs, len);
3884 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003885 TraceEventIter iter;
3886 TraceEvent *ev;
3887 char *pattern = g_strdup_printf("%s*", str);
3888 trace_event_iter_init(&iter, pattern);
3889 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3890 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003891 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003892 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003893 } else if (nb_args == 3) {
3894 add_completion_option(rs, str, "on");
3895 add_completion_option(rs, str, "off");
3896 }
3897}
3898
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003899void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3900{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003901 int i;
3902
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003903 if (nb_args != 2) {
3904 return;
3905 }
3906 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003907 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3908 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003909 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003910}
3911
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003912void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3913 const char *str)
3914{
3915 size_t len;
3916
3917 len = strlen(str);
3918 readline_set_completion_index(rs, len);
3919 if (nb_args == 2) {
3920 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003921 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003922 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003923 if (!strncmp(str, name, len)) {
3924 readline_add_completion(rs, name);
3925 }
3926 }
3927 } else if (nb_args == 3) {
3928 add_completion_option(rs, str, "on");
3929 add_completion_option(rs, str, "off");
3930 }
3931}
3932
Liang Li50e9a622015-03-23 16:32:29 +08003933void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3934 const char *str)
3935{
3936 size_t len;
3937
3938 len = strlen(str);
3939 readline_set_completion_index(rs, len);
3940 if (nb_args == 2) {
3941 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003942 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003943 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003944 if (!strncmp(str, name, len)) {
3945 readline_add_completion(rs, name);
3946 }
3947 }
3948 }
3949}
3950
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003951static void vm_completion(ReadLineState *rs, const char *str)
3952{
3953 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003954 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003955 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003956
3957 len = strlen(str);
3958 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003959
Kevin Wolf88be7b42016-05-20 18:49:07 +02003960 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003961 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003962 AioContext *ctx = bdrv_get_aio_context(bs);
3963 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003964
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003965 aio_context_acquire(ctx);
3966 if (bdrv_can_snapshot(bs)) {
3967 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3968 }
3969 aio_context_release(ctx);
3970 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003971 continue;
3972 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003973
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003974 snapshot = snapshots;
3975 while (snapshot) {
3976 char *completion = snapshot->value->name;
3977 if (!strncmp(str, completion, len)) {
3978 readline_add_completion(rs, completion);
3979 }
3980 completion = snapshot->value->id;
3981 if (!strncmp(str, completion, len)) {
3982 readline_add_completion(rs, completion);
3983 }
3984 snapshot = snapshot->next;
3985 }
3986 qapi_free_SnapshotInfoList(snapshots);
3987 }
3988
3989}
3990
3991void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3992{
3993 if (nb_args == 2) {
3994 vm_completion(rs, str);
3995 }
3996}
3997
3998void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3999{
4000 if (nb_args == 2) {
4001 vm_completion(rs, str);
4002 }
4003}
4004
Wenchao Xiac35b6402013-08-27 20:38:24 +08004005static void monitor_find_completion_by_table(Monitor *mon,
4006 const mon_cmd_t *cmd_table,
4007 char **args,
4008 int nb_args)
bellard81d09122004-07-14 17:21:37 +00004009{
4010 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004011 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00004012 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05004013 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01004014 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00004015
bellard81d09122004-07-14 17:21:37 +00004016 if (nb_args <= 1) {
4017 /* command completion */
4018 if (nb_args == 0)
4019 cmdname = "";
4020 else
4021 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08004022 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08004023 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004024 if (!runstate_check(RUN_STATE_PRECONFIG) ||
4025 cmd_can_preconfig(cmd)) {
4026 cmd_completion(mon, cmdname, cmd->name);
4027 }
bellard81d09122004-07-14 17:21:37 +00004028 }
4029 } else {
4030 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08004031 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Dr. David Alan Gilbert6d9f7832018-06-20 16:39:43 +01004032 if (compare_cmd(args[0], cmd->name) &&
4033 (!runstate_check(RUN_STATE_PRECONFIG) ||
4034 cmd_can_preconfig(cmd))) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004035 break;
4036 }
bellard81d09122004-07-14 17:21:37 +00004037 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004038 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08004039 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02004040 }
4041
Wenchao Xiad903a772013-08-27 20:38:25 +08004042 if (cmd->sub_table) {
4043 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01004044 monitor_find_completion_by_table(mon, cmd->sub_table,
4045 &args[1], nb_args - 1);
4046 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08004047 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004048 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01004049 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4050 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01004051 }
Wenchao Xiad903a772013-08-27 20:38:25 +08004052
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004053 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004054 for(i = 0; i < nb_args - 2; i++) {
4055 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004056 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004057 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004058 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004059 }
4060 }
4061 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00004062 old_ptype = NULL;
4063 while (*ptype == '-' && old_ptype != ptype) {
4064 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004065 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004066 }
bellard81d09122004-07-14 17:21:37 +00004067 switch(*ptype) {
4068 case 'F':
4069 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08004070 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08004071 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00004072 break;
4073 case 'B':
4074 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08004075 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01004076 while ((blk = blk_next(blk)) != NULL) {
4077 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004078 if (str[0] == '\0' ||
4079 !strncmp(name, str, strlen(str))) {
4080 readline_add_completion(mon->rs, name);
4081 }
4082 }
bellard81d09122004-07-14 17:21:37 +00004083 break;
bellard7fe48482004-10-09 18:08:01 +00004084 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004085 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004086 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004087 monitor_find_completion_by_table(mon, cmd_table,
4088 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004089 }
4090 break;
bellard81d09122004-07-14 17:21:37 +00004091 default:
4092 break;
4093 }
4094 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004095}
4096
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004097static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004098 const char *cmdline)
4099{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004100 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004101 char *args[MAX_ARGS];
4102 int nb_args, len;
4103
4104 /* 1. parse the cmdline */
4105 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4106 return;
4107 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004108
4109 /* if the line ends with a space, it means we want to complete the
4110 next arg */
4111 len = strlen(cmdline);
4112 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4113 if (nb_args >= MAX_ARGS) {
4114 goto cleanup;
4115 }
4116 args[nb_args++] = g_strdup("");
4117 }
4118
4119 /* 2. auto complete according to args */
4120 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004121
4122cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004123 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004124}
4125
aliguori731b0362009-03-05 23:01:42 +00004126static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004127{
aliguori731b0362009-03-05 23:01:42 +00004128 Monitor *mon = opaque;
4129
Peter Xudf152fb2018-03-09 16:59:55 +08004130 return !atomic_mb_read(&mon->suspend_cnt);
bellard9dc39cb2004-03-14 21:38:27 +00004131}
4132
Peter Xu546aa562018-03-09 16:59:54 +08004133/*
Markus Armbruster7cb21232018-07-03 10:53:52 +02004134 * Emit QMP response @rsp with ID @id to @mon.
4135 * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
4136 * Nothing is emitted then.
Peter Xu546aa562018-03-09 16:59:54 +08004137 */
Markus Armbruster7cb21232018-07-03 10:53:52 +02004138static void monitor_qmp_respond(Monitor *mon, QDict *rsp, QObject *id)
Peter Xu546aa562018-03-09 16:59:54 +08004139{
Peter Xu546aa562018-03-09 16:59:54 +08004140 if (rsp) {
4141 if (id) {
Markus Armbrusterd43b1692018-07-03 10:53:49 +02004142 qdict_put_obj(rsp, "id", qobject_ref(id));
Peter Xu546aa562018-03-09 16:59:54 +08004143 }
4144
Markus Armbruster65e3fe62018-07-03 10:53:50 +02004145 qmp_queue_response(mon, rsp);
Peter Xu546aa562018-03-09 16:59:54 +08004146 }
Peter Xu546aa562018-03-09 16:59:54 +08004147}
4148
Markus Armbrusterb2731452018-07-03 10:53:41 +02004149static void monitor_qmp_dispatch(Monitor *mon, QObject *req, QObject *id)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004150{
Markus Armbrusterb2731452018-07-03 10:53:41 +02004151 Monitor *old_mon;
Markus Armbrusterd43b1692018-07-03 10:53:49 +02004152 QDict *rsp;
Markus Armbruster69240fe2018-07-03 10:53:43 +02004153 QDict *error;
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01004154
Peter Xu227a0752018-03-09 16:59:49 +08004155 old_mon = cur_mon;
4156 cur_mon = mon;
4157
Markus Armbruster674ed722018-07-03 10:53:37 +02004158 rsp = qmp_dispatch(mon->qmp.commands, req, qmp_oob_enabled(mon));
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004159
Peter Xu227a0752018-03-09 16:59:49 +08004160 cur_mon = old_mon;
4161
Markus Armbruster69240fe2018-07-03 10:53:43 +02004162 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
Markus Armbrusterd43b1692018-07-03 10:53:49 +02004163 error = qdict_get_qdict(rsp, "error");
Markus Armbruster69240fe2018-07-03 10:53:43 +02004164 if (error
4165 && !g_strcmp0(qdict_get_try_str(error, "class"),
4166 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
4167 /* Provide a more useful error message */
4168 qdict_del(error, "desc");
4169 qdict_put_str(error, "desc", "Expecting capabilities negotiation"
4170 " with 'qmp_capabilities'");
4171 }
Peter Xu71da4662018-03-09 16:59:57 +08004172 }
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004173
Markus Armbruster7cb21232018-07-03 10:53:52 +02004174 monitor_qmp_respond(mon, rsp, id);
4175 qobject_unref(rsp);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004176}
4177
Peter Xu71da4662018-03-09 16:59:57 +08004178/*
Markus Armbruster774a6b62018-07-03 10:53:57 +02004179 * Pop a QMP request from a monitor request queue.
4180 * Return the request, or NULL all request queues are empty.
Peter Xu71da4662018-03-09 16:59:57 +08004181 * We are using round-robin fashion to pop the request, to avoid
4182 * processing commands only on a very busy monitor. To achieve that,
4183 * when we process one request on a specific monitor, we put that
4184 * monitor to the end of mon_list queue.
4185 */
Peter Xu40687eb2018-06-20 15:32:18 +08004186static QMPRequest *monitor_qmp_requests_pop_any(void)
Peter Xu71da4662018-03-09 16:59:57 +08004187{
4188 QMPRequest *req_obj = NULL;
4189 Monitor *mon;
4190
4191 qemu_mutex_lock(&monitor_lock);
4192
4193 QTAILQ_FOREACH(mon, &mon_list, entry) {
4194 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4195 req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
4196 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4197 if (req_obj) {
4198 break;
4199 }
4200 }
4201
4202 if (req_obj) {
4203 /*
4204 * We found one request on the monitor. Degrade this monitor's
4205 * priority to lowest by re-inserting it to end of queue.
4206 */
4207 QTAILQ_REMOVE(&mon_list, mon, entry);
4208 QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
4209 }
4210
4211 qemu_mutex_unlock(&monitor_lock);
4212
4213 return req_obj;
4214}
4215
4216static void monitor_qmp_bh_dispatcher(void *data)
4217{
Peter Xu40687eb2018-06-20 15:32:18 +08004218 QMPRequest *req_obj = monitor_qmp_requests_pop_any();
Markus Armbruster7cb21232018-07-03 10:53:52 +02004219 QDict *rsp;
Peter Xu71da4662018-03-09 16:59:57 +08004220
Markus Armbrusterb2731452018-07-03 10:53:41 +02004221 if (!req_obj) {
4222 return;
Peter Xu71da4662018-03-09 16:59:57 +08004223 }
Markus Armbrusterb2731452018-07-03 10:53:41 +02004224
Markus Armbruster1cc37472018-07-03 10:53:44 +02004225 if (req_obj->req) {
4226 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
4227 monitor_qmp_dispatch(req_obj->mon, req_obj->req, req_obj->id);
4228 } else {
4229 assert(req_obj->err);
Markus Armbruster7cb21232018-07-03 10:53:52 +02004230 rsp = qmp_error_response(req_obj->err);
Marc-André Lureau42eab8d2018-07-05 18:42:01 +02004231 req_obj->err = NULL;
Markus Armbruster7cb21232018-07-03 10:53:52 +02004232 monitor_qmp_respond(req_obj->mon, rsp, NULL);
4233 qobject_unref(rsp);
Markus Armbruster1cc37472018-07-03 10:53:44 +02004234 }
4235
Markus Armbrusterb2731452018-07-03 10:53:41 +02004236 if (req_obj->need_resume) {
4237 /* Pairs with the monitor_suspend() in handle_qmp_command() */
4238 monitor_resume(req_obj->mon);
4239 }
4240 qmp_request_free(req_obj);
4241
4242 /* Reschedule instead of looping so the main loop stays responsive */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004243 qemu_bh_schedule(qmp_dispatcher_bh);
Peter Xu71da4662018-03-09 16:59:57 +08004244}
4245
Peter Xubf1e7302018-03-09 16:59:59 +08004246#define QMP_REQ_QUEUE_LEN_MAX (8)
4247
Peter Xu71da4662018-03-09 16:59:57 +08004248static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
4249{
4250 QObject *req, *id = NULL;
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004251 QDict *qdict;
Peter Xu71da4662018-03-09 16:59:57 +08004252 MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser);
4253 Monitor *mon = container_of(mon_qmp, Monitor, qmp);
4254 Error *err = NULL;
4255 QMPRequest *req_obj;
4256
4257 req = json_parser_parse_err(tokens, NULL, &err);
4258 if (!req && !err) {
4259 /* json_parser_parse_err() sucks: can fail without setting @err */
4260 error_setg(&err, QERR_JSON_PARSING);
4261 }
Peter Xu71da4662018-03-09 16:59:57 +08004262
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004263 qdict = qobject_to(QDict, req);
4264 if (qdict) {
4265 id = qobject_ref(qdict_get(qdict, "id"));
4266 qdict_del(qdict, "id");
4267 } /* else will fail qmp_dispatch() */
4268
Markus Armbruster8720e632018-07-16 11:10:12 +02004269 if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
Markus Armbruster45434ba2018-07-03 10:53:39 +02004270 QString *req_json = qobject_to_json(req);
4271 trace_handle_qmp_command(mon, qstring_get_str(req_json));
4272 qobject_unref(req_json);
Peter Xu71da4662018-03-09 16:59:57 +08004273 }
4274
Markus Armbruster69240fe2018-07-03 10:53:43 +02004275 if (qdict && qmp_is_oob(qdict)) {
Markus Armbruster774a6b62018-07-03 10:53:57 +02004276 /* OOB commands are executed immediately */
Markus Armbrusterb2731452018-07-03 10:53:41 +02004277 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id)
4278 ?: "");
4279 monitor_qmp_dispatch(mon, req, id);
Marc-André Lureaucb9ec422018-08-09 13:44:16 +02004280 qobject_unref(req);
4281 qobject_unref(id);
Markus Armbrusterb2731452018-07-03 10:53:41 +02004282 return;
Peter Xucf869d52018-03-10 20:38:05 -06004283 }
4284
Peter Xu71da4662018-03-09 16:59:57 +08004285 req_obj = g_new0(QMPRequest, 1);
4286 req_obj->mon = mon;
Markus Armbruster0fa39d02018-07-03 10:53:35 +02004287 req_obj->id = id;
Peter Xu71da4662018-03-09 16:59:57 +08004288 req_obj->req = req;
Markus Armbruster1cc37472018-07-03 10:53:44 +02004289 req_obj->err = err;
Peter Xu71da4662018-03-09 16:59:57 +08004290 req_obj->need_resume = false;
4291
Peter Xubf1e7302018-03-09 16:59:59 +08004292 /* Protect qmp_requests and fetching its length. */
4293 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4294
Peter Xu71da4662018-03-09 16:59:57 +08004295 /*
4296 * If OOB is not enabled on the current monitor, we'll emulate the
4297 * old behavior that we won't process the current monitor any more
4298 * until it has responded. This helps make sure that as long as
4299 * OOB is not enabled, the server will never drop any command.
4300 */
4301 if (!qmp_oob_enabled(mon)) {
4302 monitor_suspend(mon);
4303 req_obj->need_resume = true;
Peter Xubf1e7302018-03-09 16:59:59 +08004304 } else {
4305 /* Drop the request if queue is full. */
4306 if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) {
4307 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
Markus Armbrusterd621cfe2018-07-03 10:53:30 +02004308 /*
4309 * FIXME @id's scope is just @mon, and broadcasting it is
4310 * wrong. If another monitor's client has a command with
4311 * the same ID in flight, the event will incorrectly claim
4312 * that command was dropped.
4313 */
Peter Xubf1e7302018-03-09 16:59:59 +08004314 qapi_event_send_command_dropped(id,
4315 COMMAND_DROP_REASON_QUEUE_FULL,
4316 &error_abort);
Peter Xu6d2d5632018-03-26 14:38:55 +08004317 qmp_request_free(req_obj);
Peter Xubf1e7302018-03-09 16:59:59 +08004318 return;
4319 }
Peter Xu71da4662018-03-09 16:59:57 +08004320 }
4321
4322 /*
4323 * Put the request to the end of queue so that requests will be
4324 * handled in time order. Ownership for req_obj, req, id,
4325 * etc. will be delivered to the handler side.
4326 */
Peter Xu71da4662018-03-09 16:59:57 +08004327 g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
4328 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4329
4330 /* Kick the dispatcher routine */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004331 qemu_bh_schedule(qmp_dispatcher_bh);
Peter Xu71da4662018-03-09 16:59:57 +08004332}
4333
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004334static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004335{
Peter Xu227a0752018-03-09 16:59:49 +08004336 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004337
Peter Xu227a0752018-03-09 16:59:49 +08004338 json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004339}
4340
aliguori731b0362009-03-05 23:01:42 +00004341static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004342{
aliguori731b0362009-03-05 23:01:42 +00004343 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004344 int i;
aliguori376253e2009-03-05 23:01:23 +00004345
aliguori731b0362009-03-05 23:01:42 +00004346 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004347
aliguoricde76ee2009-03-05 23:01:51 +00004348 if (cur_mon->rs) {
4349 for (i = 0; i < size; i++)
4350 readline_handle_byte(cur_mon->rs, buf[i]);
4351 } else {
4352 if (size == 0 || buf[size - 1] != 0)
4353 monitor_printf(cur_mon, "corrupted command\n");
4354 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004355 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004356 }
aliguori731b0362009-03-05 23:01:42 +00004357
4358 cur_mon = old_mon;
4359}
aliguorid8f44602008-10-06 13:52:44 +00004360
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004361static void monitor_command_cb(void *opaque, const char *cmdline,
4362 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004363{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004364 Monitor *mon = opaque;
4365
aliguori731b0362009-03-05 23:01:42 +00004366 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004367 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004368 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004369}
4370
aliguoricde76ee2009-03-05 23:01:51 +00004371int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004372{
Peter Xue3e977d2018-03-09 16:59:56 +08004373 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004374 return -ENOTTY;
Peter Xue3e977d2018-03-09 16:59:56 +08004375 }
4376
Peter Xudf152fb2018-03-09 16:59:55 +08004377 atomic_inc(&mon->suspend_cnt);
Peter Xue3e977d2018-03-09 16:59:56 +08004378
4379 if (monitor_is_qmp(mon)) {
4380 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004381 * Kick I/O thread to make sure this takes effect. It'll be
Peter Xue3e977d2018-03-09 16:59:56 +08004382 * evaluated again in prepare() of the watch object.
4383 */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004384 aio_notify(iothread_get_aio_context(mon_iothread));
Peter Xue3e977d2018-03-09 16:59:56 +08004385 }
4386
4387 trace_monitor_suspend(mon, 1);
aliguoricde76ee2009-03-05 23:01:51 +00004388 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004389}
4390
aliguori376253e2009-03-05 23:01:23 +00004391void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004392{
Peter Xue3e977d2018-03-09 16:59:56 +08004393 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004394 return;
Peter Xudf152fb2018-03-09 16:59:55 +08004395 }
Peter Xue3e977d2018-03-09 16:59:56 +08004396
4397 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
4398 if (monitor_is_qmp(mon)) {
4399 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +02004400 * For QMP monitors that are running in the I/O thread,
4401 * let's kick the thread in case it's sleeping.
Peter Xue3e977d2018-03-09 16:59:56 +08004402 */
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004403 if (mon->use_io_thread) {
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004404 aio_notify(iothread_get_aio_context(mon_iothread));
Peter Xue3e977d2018-03-09 16:59:56 +08004405 }
4406 } else {
4407 assert(mon->rs);
4408 readline_show_prompt(mon->rs);
4409 }
4410 }
4411 trace_monitor_suspend(mon, -1);
bellard7e2515e2004-08-01 21:52:19 +00004412}
4413
Markus Armbruster18166042018-07-03 10:53:51 +02004414static QDict *qmp_greeting(Monitor *mon)
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004415{
Peter Xu02130312018-03-09 16:59:53 +08004416 QList *cap_list = qlist_new();
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004417 QObject *ver = NULL;
Peter Xu02130312018-03-09 16:59:53 +08004418 QMPCapability cap;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004419
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004420 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004421
Peter Xu02130312018-03-09 16:59:53 +08004422 for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
Markus Armbruster279f9e02018-07-03 10:53:56 +02004423 if (mon->qmp.capab_offered[cap]) {
4424 qlist_append_str(cap_list, QMPCapability_str(cap));
Peter Xu02130312018-03-09 16:59:53 +08004425 }
Peter Xu02130312018-03-09 16:59:53 +08004426 }
4427
Markus Armbruster18166042018-07-03 10:53:51 +02004428 return qdict_from_jsonf_nofail(
4429 "{'QMP': {'version': %p, 'capabilities': %p}}",
4430 ver, cap_list);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004431}
4432
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004433static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004434{
Markus Armbruster18166042018-07-03 10:53:51 +02004435 QDict *data;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004436 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004437
Luiz Capitulino47116d12010-02-08 17:01:30 -02004438 switch (event) {
4439 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01004440 mon->qmp.commands = &qmp_cap_negotiation_commands;
Peter Xu02130312018-03-09 16:59:53 +08004441 monitor_qmp_caps_reset(mon);
Markus Armbruster18166042018-07-03 10:53:51 +02004442 data = qmp_greeting(mon);
4443 qmp_queue_response(mon, data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004444 qobject_unref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004445 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004446 break;
4447 case CHR_EVENT_CLOSED:
Peter Xuc73a8432018-06-20 15:32:19 +08004448 /*
4449 * Note: this is only useful when the output of the chardev
4450 * backend is still open. For example, when the backend is
4451 * stdio, it's possible that stdout is still open when stdin
4452 * is closed.
4453 */
4454 monitor_qmp_response_flush(mon);
Peter Xu6d2d5632018-03-26 14:38:55 +08004455 monitor_qmp_cleanup_queues(mon);
Markus Armbruster74358f22015-03-06 19:35:59 +01004456 json_message_parser_destroy(&mon->qmp.parser);
4457 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004458 mon_refcount--;
4459 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004460 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004461 }
4462}
4463
aliguori731b0362009-03-05 23:01:42 +00004464static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004465{
aliguori376253e2009-03-05 23:01:23 +00004466 Monitor *mon = opaque;
4467
aliguori2724b182009-03-05 23:01:47 +00004468 switch (event) {
4469 case CHR_EVENT_MUX_IN:
Peter Xudc7cbcd2018-06-08 11:55:05 +08004470 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004471 mon->mux_out = 0;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004472 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004473 if (mon->reset_seen) {
4474 readline_restart(mon->rs);
4475 monitor_resume(mon);
4476 monitor_flush(mon);
4477 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004478 atomic_mb_set(&mon->suspend_cnt, 0);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004479 }
aliguori2724b182009-03-05 23:01:47 +00004480 break;
ths86e94de2007-01-05 22:01:59 +00004481
aliguori2724b182009-03-05 23:01:47 +00004482 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004483 if (mon->reset_seen) {
Peter Xudf152fb2018-03-09 16:59:55 +08004484 if (atomic_mb_read(&mon->suspend_cnt) == 0) {
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004485 monitor_printf(mon, "\n");
4486 }
4487 monitor_flush(mon);
4488 monitor_suspend(mon);
4489 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004490 atomic_inc(&mon->suspend_cnt);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004491 }
Peter Xudc7cbcd2018-06-08 11:55:05 +08004492 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004493 mon->mux_out = 1;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004494 qemu_mutex_unlock(&mon->mon_lock);
aliguori2724b182009-03-05 23:01:47 +00004495 break;
4496
Amit Shahb6b8df52009-10-07 18:31:16 +05304497 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004498 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4499 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004500 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004501 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004502 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004503 }
4504 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004505 mon_refcount++;
4506 break;
4507
4508 case CHR_EVENT_CLOSED:
4509 mon_refcount--;
4510 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004511 break;
4512 }
ths86e94de2007-01-05 22:01:59 +00004513}
4514
Wayne Xia816f8922011-10-12 11:32:41 +08004515static int
4516compare_mon_cmd(const void *a, const void *b)
4517{
4518 return strcmp(((const mon_cmd_t *)a)->name,
4519 ((const mon_cmd_t *)b)->name);
4520}
4521
4522static void sortcmdlist(void)
4523{
4524 int array_num;
4525 int elem_size = sizeof(mon_cmd_t);
4526
4527 array_num = sizeof(mon_cmds)/elem_size-1;
4528 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4529
4530 array_num = sizeof(info_cmds)/elem_size-1;
4531 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4532}
4533
Peter Xua5ed3522018-03-09 16:59:52 +08004534static GMainContext *monitor_get_io_context(void)
4535{
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004536 return iothread_get_g_main_context(mon_iothread);
Peter Xua5ed3522018-03-09 16:59:52 +08004537}
4538
4539static AioContext *monitor_get_aio_context(void)
4540{
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004541 return iothread_get_aio_context(mon_iothread);
Peter Xua5ed3522018-03-09 16:59:52 +08004542}
4543
4544static void monitor_iothread_init(void)
4545{
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004546 mon_iothread = iothread_create("mon_iothread", &error_abort);
Peter Xu71da4662018-03-09 16:59:57 +08004547
4548 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +02004549 * The dispatcher BH must run in the main loop thread, since we
4550 * have commands assuming that context. It would be nice to get
4551 * rid of those assumptions.
Peter Xu71da4662018-03-09 16:59:57 +08004552 */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004553 qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
4554 monitor_qmp_bh_dispatcher,
4555 NULL);
Peter Xuabe3cd02018-03-09 17:00:02 +08004556
4557 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +02004558 * The responder BH must be run in the monitor I/O thread, so that
4559 * monitors that are using the I/O thread have their output
4560 * written by the I/O thread.
Peter Xuabe3cd02018-03-09 17:00:02 +08004561 */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004562 qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
4563 monitor_qmp_bh_responder,
4564 NULL);
Peter Xua5ed3522018-03-09 16:59:52 +08004565}
4566
Peter Xu6adf08d2018-03-09 16:59:50 +08004567void monitor_init_globals(void)
4568{
4569 monitor_init_qmp_commands();
4570 monitor_qapi_event_init();
4571 sortcmdlist();
4572 qemu_mutex_init(&monitor_lock);
Peter Xu47451462018-06-08 11:55:11 +08004573 qemu_mutex_init(&mon_fdsets_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004574 monitor_iothread_init();
Peter Xu6adf08d2018-03-09 16:59:50 +08004575}
4576
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004577/* These functions just adapt the readline interface in a typesafe way. We
4578 * could cast function pointers but that discards compiler checks.
4579 */
Stefan Weild5d15072014-01-25 18:18:23 +01004580static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4581 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004582{
4583 va_list ap;
4584 va_start(ap, fmt);
4585 monitor_vprintf(opaque, fmt, ap);
4586 va_end(ap);
4587}
4588
4589static void monitor_readline_flush(void *opaque)
4590{
4591 monitor_flush(opaque);
4592}
4593
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004594/*
4595 * Print to current monitor if we have one, else to stderr.
4596 * TODO should return int, so callers can calculate width, but that
4597 * requires surgery to monitor_vprintf(). Left for another day.
4598 */
4599void error_vprintf(const char *fmt, va_list ap)
4600{
4601 if (cur_mon && !monitor_cur_is_qmp()) {
4602 monitor_vprintf(cur_mon, fmt, ap);
4603 } else {
4604 vfprintf(stderr, fmt, ap);
4605 }
4606}
4607
4608void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4609{
4610 if (cur_mon && !monitor_cur_is_qmp()) {
4611 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004612 } else if (!cur_mon) {
4613 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004614 }
4615}
4616
Peter Xua5ed3522018-03-09 16:59:52 +08004617static void monitor_list_append(Monitor *mon)
4618{
4619 qemu_mutex_lock(&monitor_lock);
4620 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4621 qemu_mutex_unlock(&monitor_lock);
4622}
4623
4624static void monitor_qmp_setup_handlers_bh(void *opaque)
4625{
4626 Monitor *mon = opaque;
4627 GMainContext *context;
4628
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004629 if (mon->use_io_thread) {
Markus Armbruster774a6b62018-07-03 10:53:57 +02004630 /* Use @mon_iothread context */
Peter Xua5ed3522018-03-09 16:59:52 +08004631 context = monitor_get_io_context();
Peter Xua5ed3522018-03-09 16:59:52 +08004632 assert(context);
4633 } else {
Markus Armbruster774a6b62018-07-03 10:53:57 +02004634 /* Use default main loop context */
Peter Xua5ed3522018-03-09 16:59:52 +08004635 context = NULL;
4636 }
4637
4638 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4639 monitor_qmp_event, NULL, mon, context, true);
4640 monitor_list_append(mon);
4641}
4642
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004643void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004644{
Peter Xu6adf08d2018-03-09 16:59:50 +08004645 Monitor *mon = g_malloc(sizeof(*mon));
Peter Xube933ff2018-03-26 14:38:56 +08004646 bool use_readline = flags & MONITOR_USE_READLINE;
4647 bool use_oob = flags & MONITOR_USE_OOB;
ths20d8a3e2007-02-18 17:04:49 +00004648
Peter Xube933ff2018-03-26 14:38:56 +08004649 if (use_oob) {
4650 if (CHARDEV_IS_MUX(chr)) {
Markus Armbrusterc0698212018-07-03 10:53:27 +02004651 error_report("Monitor out-of-band is not supported with "
Peter Xube933ff2018-03-26 14:38:56 +08004652 "MUX typed chardev backend");
4653 exit(1);
4654 }
4655 if (use_readline) {
Markus Armbrusterc0698212018-07-03 10:53:27 +02004656 error_report("Monitor out-of-band is only supported by QMP");
Peter Xube933ff2018-03-26 14:38:56 +08004657 exit(1);
4658 }
4659 }
4660
4661 monitor_data_init(mon, false, use_oob);
ths20d8a3e2007-02-18 17:04:49 +00004662
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004663 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004664 mon->flags = flags;
Peter Xube933ff2018-03-26 14:38:56 +08004665 if (use_readline) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004666 mon->rs = readline_init(monitor_readline_printf,
4667 monitor_readline_flush,
4668 mon,
4669 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004670 monitor_read_command(mon, 0);
4671 }
aliguori87127162009-03-05 23:01:29 +00004672
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004673 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004674 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004675 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Markus Armbrusterf91dc2a2018-07-03 10:53:45 +02004676 if (mon->use_io_thread) {
Peter Xua5ed3522018-03-09 16:59:52 +08004677 /*
4678 * Make sure the old iowatch is gone. It's possible when
4679 * e.g. the chardev is in client mode, with wait=on.
4680 */
4681 remove_fd_in_watch(chr);
4682 /*
4683 * We can't call qemu_chr_fe_set_handlers() directly here
Markus Armbruster774a6b62018-07-03 10:53:57 +02004684 * since chardev might be running in the monitor I/O
4685 * thread. Schedule a bottom half.
Peter Xua5ed3522018-03-09 16:59:52 +08004686 */
4687 aio_bh_schedule_oneshot(monitor_get_aio_context(),
4688 monitor_qmp_setup_handlers_bh, mon);
Markus Armbruster774a6b62018-07-03 10:53:57 +02004689 /* The bottom half will add @mon to @mon_list */
Peter Xua5ed3522018-03-09 16:59:52 +08004690 return;
4691 } else {
4692 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
4693 monitor_qmp_read, monitor_qmp_event,
4694 NULL, mon, NULL, true);
4695 }
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004696 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004697 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004698 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004699 }
aliguori87127162009-03-05 23:01:29 +00004700
Peter Xua5ed3522018-03-09 16:59:52 +08004701 monitor_list_append(mon);
bellard7e2515e2004-08-01 21:52:19 +00004702}
4703
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004704void monitor_cleanup(void)
4705{
4706 Monitor *mon, *next;
4707
Peter Xua5ed3522018-03-09 16:59:52 +08004708 /*
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004709 * We need to explicitly stop the I/O thread (but not destroy it),
Markus Armbruster774a6b62018-07-03 10:53:57 +02004710 * clean up the monitor resources, then destroy the I/O thread since
Peter Xua5ed3522018-03-09 16:59:52 +08004711 * we need to unregister from chardev below in
4712 * monitor_data_destroy(), and chardev is not thread-safe yet
4713 */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004714 iothread_stop(mon_iothread);
Peter Xua5ed3522018-03-09 16:59:52 +08004715
Peter Xuabe3cd02018-03-09 17:00:02 +08004716 /*
Markus Armbruster774a6b62018-07-03 10:53:57 +02004717 * Flush all response queues. Note that even after this flush,
4718 * data may remain in output buffers.
Peter Xuabe3cd02018-03-09 17:00:02 +08004719 */
4720 monitor_qmp_bh_responder(NULL);
4721
Markus Armbruster774a6b62018-07-03 10:53:57 +02004722 /* Flush output buffers and destroy monitors */
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004723 qemu_mutex_lock(&monitor_lock);
Peter Xu238d9f32018-03-09 16:59:51 +08004724 QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
4725 QTAILQ_REMOVE(&mon_list, mon, entry);
Peter Xuabe3cd02018-03-09 17:00:02 +08004726 monitor_flush(mon);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004727 monitor_data_destroy(mon);
4728 g_free(mon);
4729 }
4730 qemu_mutex_unlock(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004731
Markus Armbrusterc5f57ed2018-07-03 10:53:28 +02004732 /* QEMUBHs needs to be deleted before destroying the I/O thread */
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004733 qemu_bh_delete(qmp_dispatcher_bh);
4734 qmp_dispatcher_bh = NULL;
4735 qemu_bh_delete(qmp_respond_bh);
4736 qmp_respond_bh = NULL;
Peter Xu71da4662018-03-09 16:59:57 +08004737
Markus Armbrustercab5ad82018-07-03 10:53:46 +02004738 iothread_destroy(mon_iothread);
4739 mon_iothread = NULL;
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004740}
4741
Paolo Bonzini4d454572012-11-26 16:03:42 +01004742QemuOptsList qemu_mon_opts = {
4743 .name = "mon",
4744 .implied_opt_name = "chardev",
4745 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4746 .desc = {
4747 {
4748 .name = "mode",
4749 .type = QEMU_OPT_STRING,
4750 },{
4751 .name = "chardev",
4752 .type = QEMU_OPT_STRING,
4753 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004754 .name = "pretty",
4755 .type = QEMU_OPT_BOOL,
Peter Xube933ff2018-03-26 14:38:56 +08004756 },{
4757 .name = "x-oob",
4758 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004759 },
4760 { /* end of list */ }
4761 },
4762};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004763
4764#ifndef TARGET_I386
4765void qmp_rtc_reset_reinjection(Error **errp)
4766{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004767 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004768}
Brijesh Singh08a161f2018-03-08 06:48:42 -06004769
4770SevInfo *qmp_query_sev(Error **errp)
4771{
4772 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
4773 return NULL;
4774}
Brijesh Singh1b6a0342018-03-08 06:48:56 -06004775
4776SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
4777{
4778 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
4779 return NULL;
4780}
Brijesh Singh31dd67f2018-03-08 06:48:59 -06004781
4782SevCapability *qmp_query_sev_capabilities(Error **errp)
4783{
4784 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
4785 return NULL;
4786}
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004787#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004788
4789#ifndef TARGET_S390X
4790void qmp_dump_skeys(const char *filename, Error **errp)
4791{
4792 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4793}
4794#endif
Peter Xuae50a772016-03-30 17:27:24 +01004795
4796#ifndef TARGET_ARM
4797GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4798{
4799 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4800 return NULL;
4801}
4802#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304803
4804HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4805{
4806 MachineState *ms = MACHINE(qdev_get_machine());
4807 MachineClass *mc = MACHINE_GET_CLASS(ms);
4808
Igor Mammedovc5514d02017-02-10 11:20:57 +01004809 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304810 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4811 return NULL;
4812 }
4813
Igor Mammedovc5514d02017-02-10 11:20:57 +01004814 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304815}