blob: 0b6cb6aca32d4e051e16344cf6f642596c45776c [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
Joshua Duongc984de52018-09-09 22:10:37 -070025#ifdef _MSC_VER
26#define USE_QEMU_DIRENT
27#endif
Peter Maydelld38ea872016-01-29 17:50:05 +000028#include "qemu/osdep.h"
Joshua Duongc984de52018-09-09 22:10:37 -070029#ifndef _MSC_VER
blueswir1511d2b12009-03-07 15:32:56 +000030#include <dirent.h>
Joshua Duongc984de52018-09-09 22:10:37 -070031#endif
Paolo Bonzini33c11872016-03-15 16:58:45 +010032#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010034#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000035#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020036#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010037#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020038#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010039#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020040#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000041#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040042#include "chardev/char-fe.h"
Peter Xua5ed3522018-03-09 16:59:52 +080043#include "chardev/char-io.h"
Peter Xube933ff2018-03-26 14:38:56 +080044#include "chardev/char-mux.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020045#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020046#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010047#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030048#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010049#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010050#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010051#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010052#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010053#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000054#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020055#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010056#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010057#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010058#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010059#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020060#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010061#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010062#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010063#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010064#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010065#include "qapi/qmp/qjson.h"
66#include "qapi/qmp/json-streamer.h"
67#include "qapi/qmp/json-parser.h"
Peter Xu02130312018-03-09 16:59:53 +080068#include "qapi/qmp/qlist.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020069#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000070#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020071#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030072#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020073#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020074#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053075#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010076#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010077#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010078#include "qemu/log.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010079#include "qemu/option.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050080#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010081#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010082#include "block/qapi.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010083#include "qapi/qapi-commands.h"
84#include "qapi/qapi-events.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010085#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020086#include "qapi/qmp-event.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010087#include "qapi/qapi-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020088#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010089#include "sysemu/cpus.h"
Peter Xua5ed3522018-03-09 16:59:52 +080090#include "sysemu/iothread.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020091#include "qemu/cutils.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020092
Jason J. Hernea4538a52015-06-26 14:07:21 -040093#if defined(TARGET_S390X)
94#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020095#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040096#endif
97
bellard9307c4c2004-04-04 12:57:25 +000098/*
99 * Supported types:
ths5fafdf22007-09-16 21:08:06 +0000100 *
bellard9307c4c2004-04-04 12:57:25 +0000101 * 'F' filename
bellard81d09122004-07-14 17:21:37 +0000102 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +0000103 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +0800104 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100105 * 'O' option string of the form NAME=VALUE,...
106 * parsed according to QemuOptsList given by its name
107 * Example: 'device:O' uses qemu_device_opts.
108 * Restriction: only lists with empty desc are supported
109 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000110 * 'i' 32 bit integer
111 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300112 * 'M' Non-negative target long (32 or 64 bit), in user mode the
113 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200114 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200115 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
116 * K, k suffix, which multiplies the value by 2^60 for suffixes E
117 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
118 * 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 +0100119 * 'T' double
120 * user mode accepts an optional ms, us, ns suffix,
121 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000122 * '/' optional gdb-like print format (like "/10x")
123 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300124 * '?' optional type (for all types, except '/')
125 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100126 * 'b' boolean
127 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300128 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000129 *
130 */
131
Anthony Liguoric227f092009-10-01 16:12:16 -0500132typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000133 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000134 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000135 const char *params;
136 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400137 void (*cmd)(Monitor *mon, const QDict *qdict);
138 /* @sub_table is a list of 2nd level of commands. If it does not exist,
139 * cmd should be used. If it exists, sub_table[?].cmd should be
140 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800141 */
142 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100143 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500144} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000145
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500147typedef struct mon_fd_t mon_fd_t;
148struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100149 char *name;
150 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500151 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100152};
153
Corey Bryantba1c048a2012-08-14 16:43:43 -0400154/* file descriptor associated with a file descriptor set */
155typedef struct MonFdsetFd MonFdsetFd;
156struct MonFdsetFd {
157 int fd;
158 bool removed;
159 char *opaque;
160 QLIST_ENTRY(MonFdsetFd) next;
161};
162
163/* file descriptor set containing fds passed via SCM_RIGHTS */
164typedef struct MonFdset MonFdset;
165struct MonFdset {
166 int64_t id;
167 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400168 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c048a2012-08-14 16:43:43 -0400169 QLIST_ENTRY(MonFdset) next;
170};
171
Markus Armbruster74358f22015-03-06 19:35:59 +0100172typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200173 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100174 /*
175 * When a client connects, we're in capabilities negotiation mode.
176 * When command qmp_capabilities succeeds, we go into command
177 * mode.
178 */
Markus Armbruster635db182017-03-03 13:32:27 +0100179 QmpCommandList *commands;
Peter Xu02130312018-03-09 16:59:53 +0800180 bool qmp_caps[QMP_CAPABILITY__MAX];
Peter Xu71da4662018-03-09 16:59:57 +0800181 /*
182 * Protects qmp request/response queue. Please take monitor_lock
183 * first when used together.
184 */
185 QemuMutex qmp_queue_lock;
186 /* Input queue that holds all the parsed QMP requests */
187 GQueue *qmp_requests;
Peter Xuabe3cd02018-03-09 17:00:02 +0800188 /* Output queue contains all the QMP responses in order */
189 GQueue *qmp_responses;
Markus Armbruster74358f22015-03-06 19:35:59 +0100190} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200191
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100192/*
193 * To prevent flooding clients, events can be throttled. The
194 * throttling is calculated globally, rather than per-Monitor
195 * instance.
196 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200197typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200198 QAPIEvent event; /* Throttling state for this event type and... */
199 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100200 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200201 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200202} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100203
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200204typedef struct {
205 int64_t rate; /* Minimum time (in ns) between two events */
206} MonitorQAPIEventConf;
207
aliguori87127162009-03-05 23:01:29 +0000208struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300209 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200210 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000211 int flags;
Peter Xudf152fb2018-03-09 16:59:55 +0800212 int suspend_cnt; /* Needs to be accessed atomically */
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400213 bool skip_flush;
Peter Xua5ed3522018-03-09 16:59:52 +0800214 bool use_io_thr;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200215
Peter Xuabe3cd02018-03-09 17:00:02 +0800216 /* We can't access guest memory when holding the lock */
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200217 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400218 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200219 guint out_watch;
220
221 /* Read under either BQL or out_lock, written with BQL+out_lock. */
222 int mux_out;
223
aliguori731b0362009-03-05 23:01:42 +0000224 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100225 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200226 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200227 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000228 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800229 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500230 QLIST_HEAD(,mon_fd_t) fds;
Peter Xu238d9f32018-03-09 16:59:51 +0800231 QTAILQ_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000232};
233
Peter Xua5ed3522018-03-09 16:59:52 +0800234/* Let's add monitor global variables to this struct. */
235static struct {
236 IOThread *mon_iothread;
Peter Xu71da4662018-03-09 16:59:57 +0800237 /* Bottom half to dispatch the requests received from IO thread */
238 QEMUBH *qmp_dispatcher_bh;
Peter Xuabe3cd02018-03-09 17:00:02 +0800239 /* Bottom half to deliver the responses back to clients */
240 QEMUBH *qmp_respond_bh;
Peter Xua5ed3522018-03-09 16:59:52 +0800241} mon_global;
242
Peter Xu6d2d5632018-03-26 14:38:55 +0800243struct QMPRequest {
244 /* Owner of the request */
245 Monitor *mon;
246 /* "id" field of the request */
247 QObject *id;
248 /* Request object to be handled */
249 QObject *req;
250 /*
251 * Whether we need to resume the monitor afterward. This flag is
252 * used to emulate the old QMP server behavior that the current
253 * command must be completed before execution of the next one.
254 */
255 bool need_resume;
256};
257typedef struct QMPRequest QMPRequest;
258
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300259/* QMP checker flags */
260#define QMP_ACCEPT_UNKNOWNS 1
261
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200262/* Protects mon_list, monitor_event_state. */
263static QemuMutex monitor_lock;
264
Peter Xu238d9f32018-03-09 16:59:51 +0800265static QTAILQ_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c048a2012-08-14 16:43:43 -0400266static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400267static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000268
Wayne Xia816f8922011-10-12 11:32:41 +0800269static mon_cmd_t mon_cmds[];
270static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000271
Markus Armbruster635db182017-03-03 13:32:27 +0100272QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100273
Markus Armbruster8631b602010-02-18 11:41:55 +0100274Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000275
Alberto Garciadc599972016-03-10 13:55:26 +0200276static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
277
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100278static void monitor_command_cb(void *opaque, const char *cmdline,
279 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000280
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100281/**
282 * Is @mon a QMP monitor?
283 */
284static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200285{
286 return (mon->flags & MONITOR_USE_CONTROL);
287}
288
Markus Armbruster489653b2015-03-06 20:01:05 +0100289/**
Peter Xue3e977d2018-03-09 16:59:56 +0800290 * Whether @mon is using readline? Note: not all HMP monitors use
291 * readline, e.g., gdbserver has a non-interactive HMP monitor, so
292 * readline is not used there.
293 */
294static inline bool monitor_uses_readline(const Monitor *mon)
295{
296 return mon->flags & MONITOR_USE_READLINE;
297}
298
299static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
300{
301 return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
302}
303
304/**
Markus Armbruster489653b2015-03-06 20:01:05 +0100305 * Is the current monitor, if any, a QMP monitor?
306 */
307bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100308{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100309 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100310}
311
Anthony Liguori7060b472011-09-02 12:34:50 -0500312void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000313{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200314 if (!mon->rs)
315 return;
316
aliguori731b0362009-03-05 23:01:42 +0000317 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
318 if (show_prompt)
319 readline_show_prompt(mon->rs);
320}
bellard6a00d602005-11-21 23:25:50 +0000321
Anthony Liguori7060b472011-09-02 12:34:50 -0500322int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
323 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000324{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100325 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000326 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
327 /* prompt is printed on return from the command handler */
328 return 0;
329 } else {
330 monitor_printf(mon, "terminal does not support password prompting\n");
331 return -ENOTTY;
332 }
aliguoribb5fc202009-03-05 23:01:15 +0000333}
334
Peter Xu6d2d5632018-03-26 14:38:55 +0800335static void qmp_request_free(QMPRequest *req)
336{
337 qobject_decref(req->id);
338 qobject_decref(req->req);
339 g_free(req);
340}
341
342/* Must with the mon->qmp.qmp_queue_lock held */
343static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon)
344{
345 while (!g_queue_is_empty(mon->qmp.qmp_requests)) {
346 qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests));
347 }
348}
349
350/* Must with the mon->qmp.qmp_queue_lock held */
351static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon)
352{
353 while (!g_queue_is_empty(mon->qmp.qmp_responses)) {
354 qobject_decref(g_queue_pop_head(mon->qmp.qmp_responses));
355 }
356}
357
358static void monitor_qmp_cleanup_queues(Monitor *mon)
359{
360 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
361 monitor_qmp_cleanup_req_queue_locked(mon);
362 monitor_qmp_cleanup_resp_queue_locked(mon);
363 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
364}
365
366
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200367static void monitor_flush_locked(Monitor *mon);
368
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100369static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
370 void *opaque)
371{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200372 Monitor *mon = opaque;
373
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200374 qemu_mutex_lock(&mon->out_lock);
375 mon->out_watch = 0;
376 monitor_flush_locked(mon);
377 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100378 return FALSE;
379}
380
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200381/* Called with mon->out_lock held. */
382static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000383{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100384 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400385 size_t len;
386 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100387
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400388 if (mon->skip_flush) {
389 return;
390 }
391
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400392 buf = qstring_get_str(mon->outbuf);
393 len = qstring_get_length(mon->outbuf);
394
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200395 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300396 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200397 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
398 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400399 QDECREF(mon->outbuf);
400 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100401 return;
402 }
403 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600404 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400405 QString *tmp = qstring_from_str(buf + rc);
406 QDECREF(mon->outbuf);
407 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100408 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200409 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300410 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300411 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300412 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200413 }
bellard7e2515e2004-08-01 21:52:19 +0000414 }
415}
416
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200417void monitor_flush(Monitor *mon)
418{
419 qemu_mutex_lock(&mon->out_lock);
420 monitor_flush_locked(mon);
421 qemu_mutex_unlock(&mon->out_lock);
422}
423
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400424/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000425static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000426{
ths60fe76f2007-12-16 03:02:09 +0000427 char c;
aliguori731b0362009-03-05 23:01:42 +0000428
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200429 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000430 for(;;) {
431 c = *str++;
432 if (c == '\0')
433 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400434 if (c == '\n') {
435 qstring_append_chr(mon->outbuf, '\r');
436 }
437 qstring_append_chr(mon->outbuf, c);
438 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200439 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400440 }
bellard7e2515e2004-08-01 21:52:19 +0000441 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200442 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000443}
444
aliguori376253e2009-03-05 23:01:23 +0000445void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000446{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400447 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200448
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200449 if (!mon)
450 return;
451
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100452 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200453 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200454 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200455
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400456 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200457 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400458 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000459}
460
aliguori376253e2009-03-05 23:01:23 +0000461void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000462{
463 va_list ap;
464 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000465 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000466 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000467}
468
Pavel Butsykincaf15312015-09-22 16:18:17 +0300469int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000470{
471 va_list ap;
472 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000473 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000474 va_end(ap);
475 return 0;
476}
477
Peter Xuabe3cd02018-03-09 17:00:02 +0800478static void monitor_json_emitter_raw(Monitor *mon,
479 QObject *data)
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200480{
481 QString *json;
482
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200483 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
484 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200485 assert(json != NULL);
486
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200487 qstring_append_chr(json, '\n');
488 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200489
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200490 QDECREF(json);
491}
492
Peter Xuabe3cd02018-03-09 17:00:02 +0800493static void monitor_json_emitter(Monitor *mon, QObject *data)
494{
495 if (mon->use_io_thr) {
496 /*
497 * If using IO thread, we need to queue the item so that IO
498 * thread will do the rest for us. Take refcount so that
499 * caller won't free the data (which will be finally freed in
500 * responder thread).
501 */
502 qobject_incref(data);
503 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
504 g_queue_push_tail(mon->qmp.qmp_responses, (void *)data);
505 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
506 qemu_bh_schedule(mon_global.qmp_respond_bh);
507 } else {
508 /*
509 * If not using monitor IO thread, then we are in main thread.
510 * Do the emission right away.
511 */
512 monitor_json_emitter_raw(mon, data);
513 }
514}
515
516struct QMPResponse {
517 Monitor *mon;
518 QObject *data;
519};
520typedef struct QMPResponse QMPResponse;
521
522/*
523 * Return one QMPResponse. The response is only valid if
524 * response.data is not NULL.
525 */
526static QMPResponse monitor_qmp_response_pop_one(void)
527{
528 Monitor *mon;
529 QObject *data = NULL;
530
531 qemu_mutex_lock(&monitor_lock);
532 QTAILQ_FOREACH(mon, &mon_list, entry) {
533 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
534 data = g_queue_pop_head(mon->qmp.qmp_responses);
535 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
536 if (data) {
537 break;
538 }
539 }
540 qemu_mutex_unlock(&monitor_lock);
541 return (QMPResponse) { .mon = mon, .data = data };
542}
543
544static void monitor_qmp_bh_responder(void *opaque)
545{
546 QMPResponse response;
547
548 while (true) {
549 response = monitor_qmp_response_pop_one();
550 if (!response.data) {
551 break;
552 }
553 monitor_json_emitter_raw(response.mon, response.data);
554 qobject_decref(response.data);
555 }
556}
557
Eric Blake7fb1cf1602015-11-18 01:52:57 -0700558static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200559 /* Limit guest-triggerable events to 1 per second */
560 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
561 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
562 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
563 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
564 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
565 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
566};
567
Markus Armbrustera24712a2015-10-15 17:08:34 +0200568GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100569
570/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200571 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200572 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100573 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200574static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100575{
576 Monitor *mon;
577
Markus Armbruster688b4b72015-10-15 17:08:30 +0200578 trace_monitor_protocol_event_emit(event, qdict);
Peter Xu238d9f32018-03-09 16:59:51 +0800579 QTAILQ_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100580 if (monitor_is_qmp(mon)
581 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200582 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100583 }
584 }
585}
586
Markus Armbrustera24712a2015-10-15 17:08:34 +0200587static void monitor_qapi_event_handler(void *opaque);
588
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100589/*
590 * Queue a new event for emission to Monitor instances,
591 * applying any rate limiting if required.
592 */
593static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200594monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100595{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200596 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200597 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200598
Eric Blake7fb1cf1602015-11-18 01:52:57 -0700599 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200600 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200601 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100602
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200603 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200604
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200605 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200606 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200607 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100608 } else {
Max Reitz7dc847e2018-02-24 16:40:29 +0100609 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
Markus Armbruster7de0be62015-10-15 17:08:35 +0200610 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200611
612 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
613 assert(!evstate || timer_pending(evstate->timer));
614
615 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200616 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200617 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200618 * last send. Store event for sending when timer fires,
619 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100620 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200621 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200622 evstate->qdict = qdict;
623 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100624 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200625 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200626 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200627 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200628 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200629 * events arriving before then will be delayed until then.
630 */
Alberto Garciadc599972016-03-10 13:55:26 +0200631 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200632
Markus Armbruster688b4b72015-10-15 17:08:30 +0200633 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200634
635 evstate = g_new(MonitorQAPIEventState, 1);
636 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200637 evstate->data = data;
638 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200639 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200640 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200641 monitor_qapi_event_handler,
642 evstate);
643 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200644 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100645 }
646 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200647
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200648 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100649}
650
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100651/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200652 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200653 * event.
654 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100655 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200656static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100657{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200658 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200659 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100660
Markus Armbruster93f8f982015-10-15 17:08:31 +0200661 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200662 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200663
Markus Armbruster688b4b72015-10-15 17:08:30 +0200664 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200665 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200666
Markus Armbruster688b4b72015-10-15 17:08:30 +0200667 monitor_qapi_event_emit(evstate->event, evstate->qdict);
668 QDECREF(evstate->qdict);
669 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200670 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200671 } else {
672 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200673 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200674 timer_free(evstate->timer);
675 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100676 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200677
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200678 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100679}
680
Markus Armbrustera24712a2015-10-15 17:08:34 +0200681static unsigned int qapi_event_throttle_hash(const void *key)
682{
683 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200684 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200685
Markus Armbruster7de0be62015-10-15 17:08:35 +0200686 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
687 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
688 }
689
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200690 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
691 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
692 }
693
Markus Armbruster7de0be62015-10-15 17:08:35 +0200694 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200695}
696
697static gboolean qapi_event_throttle_equal(const void *a, const void *b)
698{
699 const MonitorQAPIEventState *eva = a;
700 const MonitorQAPIEventState *evb = b;
701
Markus Armbruster7de0be62015-10-15 17:08:35 +0200702 if (eva->event != evb->event) {
703 return FALSE;
704 }
705
706 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
707 return !strcmp(qdict_get_str(eva->data, "id"),
708 qdict_get_str(evb->data, "id"));
709 }
710
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200711 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
712 return !strcmp(qdict_get_str(eva->data, "node-name"),
713 qdict_get_str(evb->data, "node-name"));
714 }
715
Markus Armbruster7de0be62015-10-15 17:08:35 +0200716 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200717}
718
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200719static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100720{
Alberto Garciadc599972016-03-10 13:55:26 +0200721 if (qtest_enabled()) {
722 event_clock_type = QEMU_CLOCK_VIRTUAL;
723 }
724
Markus Armbrustera24712a2015-10-15 17:08:34 +0200725 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
726 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200727 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200728}
729
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100730static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200731
Peter Xua5ed3522018-03-09 16:59:52 +0800732static void monitor_data_init(Monitor *mon, bool skip_flush,
733 bool use_io_thr)
Wenchao Xiab01fe892013-08-27 20:38:19 +0800734{
735 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200736 qemu_mutex_init(&mon->out_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800737 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800738 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800739 /* Use *mon_cmds by default. */
740 mon->cmd_table = mon_cmds;
Peter Xu88a95d12018-03-09 16:59:48 +0800741 mon->skip_flush = skip_flush;
Peter Xua5ed3522018-03-09 16:59:52 +0800742 mon->use_io_thr = use_io_thr;
Peter Xu71da4662018-03-09 16:59:57 +0800743 mon->qmp.qmp_requests = g_queue_new();
Peter Xuabe3cd02018-03-09 17:00:02 +0800744 mon->qmp.qmp_responses = g_queue_new();
Wenchao Xiab01fe892013-08-27 20:38:19 +0800745}
746
747static void monitor_data_destroy(Monitor *mon)
748{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200749 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400750 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400751 if (monitor_is_qmp(mon)) {
752 json_message_parser_destroy(&mon->qmp.parser);
753 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100754 readline_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800755 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200756 qemu_mutex_destroy(&mon->out_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800757 qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
Peter Xu6d2d5632018-03-26 14:38:55 +0800758 monitor_qmp_cleanup_req_queue_locked(mon);
759 monitor_qmp_cleanup_resp_queue_locked(mon);
Peter Xu71da4662018-03-09 16:59:57 +0800760 g_queue_free(mon->qmp.qmp_requests);
Peter Xuabe3cd02018-03-09 17:00:02 +0800761 g_queue_free(mon->qmp.qmp_responses);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800762}
763
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200764char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
765 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200766{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200767 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200768 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200769
Peter Xua5ed3522018-03-09 16:59:52 +0800770 monitor_data_init(&hmp, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200771
772 old_mon = cur_mon;
773 cur_mon = &hmp;
774
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200775 if (has_cpu_index) {
776 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200777 if (ret < 0) {
778 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100779 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
780 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200781 goto out;
782 }
783 }
784
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100785 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200786 cur_mon = old_mon;
787
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200788 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400789 if (qstring_get_length(hmp.outbuf) > 0) {
790 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200791 } else {
792 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200793 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200794 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200795
796out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800797 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200798 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200799}
800
bellard9dc39cb2004-03-14 21:38:27 +0000801static int compare_cmd(const char *name, const char *list)
802{
803 const char *p, *pstart;
804 int len;
805 len = strlen(name);
806 p = list;
807 for(;;) {
808 pstart = p;
809 p = strchr(p, '|');
810 if (!p)
811 p = pstart + strlen(pstart);
812 if ((p - pstart) == len && !memcmp(pstart, name, len))
813 return 1;
814 if (*p == '\0')
815 break;
816 p++;
817 }
818 return 0;
819}
820
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800821static int get_str(char *buf, int buf_size, const char **pp)
822{
823 const char *p;
824 char *q;
825 int c;
826
827 q = buf;
828 p = *pp;
829 while (qemu_isspace(*p)) {
830 p++;
831 }
832 if (*p == '\0') {
833 fail:
834 *q = '\0';
835 *pp = p;
836 return -1;
837 }
838 if (*p == '\"') {
839 p++;
840 while (*p != '\0' && *p != '\"') {
841 if (*p == '\\') {
842 p++;
843 c = *p++;
844 switch (c) {
845 case 'n':
846 c = '\n';
847 break;
848 case 'r':
849 c = '\r';
850 break;
851 case '\\':
852 case '\'':
853 case '\"':
854 break;
855 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100856 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800857 goto fail;
858 }
859 if ((q - buf) < buf_size - 1) {
860 *q++ = c;
861 }
862 } else {
863 if ((q - buf) < buf_size - 1) {
864 *q++ = *p;
865 }
866 p++;
867 }
868 }
869 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100870 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800871 goto fail;
872 }
873 p++;
874 } else {
875 while (*p != '\0' && !qemu_isspace(*p)) {
876 if ((q - buf) < buf_size - 1) {
877 *q++ = *p;
878 }
879 p++;
880 }
881 }
882 *q = '\0';
883 *pp = p;
884 return 0;
885}
886
887#define MAX_ARGS 16
888
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800889static void free_cmdline_args(char **args, int nb_args)
890{
891 int i;
892
893 assert(nb_args <= MAX_ARGS);
894
895 for (i = 0; i < nb_args; i++) {
896 g_free(args[i]);
897 }
898
899}
900
901/*
902 * Parse the command line to get valid args.
903 * @cmdline: command line to be parsed.
904 * @pnb_args: location to store the number of args, must NOT be NULL.
905 * @args: location to store the args, which should be freed by caller, must
906 * NOT be NULL.
907 *
908 * Returns 0 on success, negative on failure.
909 *
910 * NOTE: this parser is an approximate form of the real command parser. Number
911 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
912 * return with failure.
913 */
914static int parse_cmdline(const char *cmdline,
915 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800916{
917 const char *p;
918 int nb_args, ret;
919 char buf[1024];
920
921 p = cmdline;
922 nb_args = 0;
923 for (;;) {
924 while (qemu_isspace(*p)) {
925 p++;
926 }
927 if (*p == '\0') {
928 break;
929 }
930 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800931 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800932 }
933 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800934 if (ret < 0) {
935 goto fail;
936 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800937 args[nb_args] = g_strdup(buf);
938 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800939 }
940 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800941 return 0;
942
943 fail:
944 free_cmdline_args(args, nb_args);
945 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800946}
947
Wenchao Xia66855492013-08-27 20:38:23 +0800948static void help_cmd_dump_one(Monitor *mon,
949 const mon_cmd_t *cmd,
950 char **prefix_args,
951 int prefix_args_nb)
952{
953 int i;
954
955 for (i = 0; i < prefix_args_nb; i++) {
956 monitor_printf(mon, "%s ", prefix_args[i]);
957 }
958 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
959}
960
961/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500962static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800963 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000964{
Anthony Liguoric227f092009-10-01 16:12:16 -0500965 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000966
Wenchao Xia66855492013-08-27 20:38:23 +0800967 /* No valid arg need to compare with, dump all in *cmds */
968 if (arg_index >= nb_args) {
969 for (cmd = cmds; cmd->name != NULL; cmd++) {
970 help_cmd_dump_one(mon, cmd, args, arg_index);
971 }
972 return;
973 }
974
975 /* Find one entry to dump */
976 for (cmd = cmds; cmd->name != NULL; cmd++) {
977 if (compare_cmd(args[arg_index], cmd->name)) {
978 if (cmd->sub_table) {
979 /* continue with next arg */
980 help_cmd_dump(mon, cmd->sub_table,
981 args, nb_args, arg_index + 1);
982 } else {
983 help_cmd_dump_one(mon, cmd, args, arg_index);
984 }
985 break;
986 }
bellard9dc39cb2004-03-14 21:38:27 +0000987 }
988}
989
aliguori376253e2009-03-05 23:01:23 +0000990static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000991{
Wenchao Xia66855492013-08-27 20:38:23 +0800992 char *args[MAX_ARGS];
993 int nb_args = 0;
994
995 /* 1. parse user input */
996 if (name) {
997 /* special case for log, directly dump and return */
998 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000999 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +00001000 monitor_printf(mon, "Log items (comma separated):\n");
1001 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +00001002 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +00001003 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +00001004 }
Wenchao Xia66855492013-08-27 20:38:23 +08001005 return;
1006 }
1007
1008 if (parse_cmdline(name, &nb_args, args) < 0) {
1009 return;
bellardf193c792004-03-21 17:06:25 +00001010 }
bellard9dc39cb2004-03-14 21:38:27 +00001011 }
Wenchao Xia66855492013-08-27 20:38:23 +08001012
1013 /* 2. dump the contents according to parsed args */
1014 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
1015
1016 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +00001017}
1018
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001019static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -03001020{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001021 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -03001022}
1023
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001024static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301025{
1026 const char *tp_name = qdict_get_str(qdict, "name");
1027 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001028 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1029 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001030 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +00001031
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001032 if (vcpu < 0) {
1033 monitor_printf(mon, "argument vcpu must be positive");
1034 return;
1035 }
1036
1037 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001038 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +01001039 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +00001040 }
Prerna Saxena22890ab2010-06-24 17:04:53 +05301041}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001042
Michael Rothc45a8162011-10-02 08:44:37 -05001043#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001044static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001045{
1046 const char *op = qdict_get_try_str(qdict, "op");
1047 const char *arg = qdict_get_try_str(qdict, "arg");
1048
1049 if (!op) {
1050 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
1051 } else if (!strcmp(op, "on")) {
1052 st_set_trace_file_enabled(true);
1053 } else if (!strcmp(op, "off")) {
1054 st_set_trace_file_enabled(false);
1055 } else if (!strcmp(op, "flush")) {
1056 st_flush_trace_buffer();
1057 } else if (!strcmp(op, "set")) {
1058 if (arg) {
1059 st_set_trace_file(arg);
1060 }
1061 } else {
1062 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1063 help_cmd(mon, "trace-file");
1064 }
1065}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301066#endif
1067
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001068static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001069{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001070 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001071}
1072
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001073static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +00001074{
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001075 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001076
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001077 if (!cmd->enabled) {
1078 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001079 }
1080
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001081 info = g_malloc0(sizeof(*info));
1082 info->value = g_malloc0(sizeof(*info->value));
1083 info->value->name = g_strdup(cmd->name);
1084 info->next = *list;
1085 *list = info;
1086}
1087
1088CommandInfoList *qmp_query_commands(Error **errp)
1089{
1090 CommandInfoList *list = NULL;
1091
Markus Armbruster635db182017-03-03 13:32:27 +01001092 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001093
1094 return list;
thsa36e69d2007-12-02 05:18:19 +00001095}
1096
Daniel P. Berrange48608532012-05-21 17:59:51 +01001097EventInfoList *qmp_query_events(Error **errp)
1098{
1099 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +02001100 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +01001101
Eric Blake7fb1cf1602015-11-18 01:52:57 -07001102 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001103 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +01001104 assert(event_name != NULL);
1105 info = g_malloc0(sizeof(*info));
1106 info->value = g_malloc0(sizeof(*info->value));
1107 info->value->name = g_strdup(event_name);
1108
1109 info->next = ev_list;
1110 ev_list = info;
1111 }
1112
1113 return ev_list;
1114}
1115
Markus Armbruster39a18152015-09-16 13:06:28 +02001116/*
1117 * Minor hack: generated marshalling suppressed for this command
1118 * ('gen': false in the schema) so we can parse the JSON string
1119 * directly into QObject instead of first parsing it with
1120 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1121 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01001122 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -06001123 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +02001124 */
1125static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1126 Error **errp)
1127{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001128 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
Markus Armbruster39a18152015-09-16 13:06:28 +02001129}
1130
Marc-André Lureau5032a162016-09-12 13:19:02 +04001131/*
Marc-André Lureau5032a162016-09-12 13:19:02 +04001132 * We used to define commands in qmp-commands.hx in addition to the
1133 * QAPI schema. This permitted defining some of them only in certain
1134 * configurations. query-commands has always reflected that (good,
1135 * because it lets QMP clients figure out what's actually available),
1136 * while query-qmp-schema never did (not so good). This function is a
1137 * hack to keep the configuration-specific commands defined exactly as
1138 * before, even though qmp-commands.hx is gone.
1139 *
1140 * FIXME Educate the QAPI schema on configuration-specific commands,
1141 * and drop this hack.
1142 */
1143static void qmp_unregister_commands_hack(void)
1144{
1145#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +01001146 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001147#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +02001148#ifndef CONFIG_REPLICATION
1149 qmp_unregister_command(&qmp_commands, "xen-set-replication");
1150 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
1151 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
1152#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001153#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +01001154 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Brijesh Singh08a161f2018-03-08 06:48:42 -06001155 qmp_unregister_command(&qmp_commands, "query-sev");
Brijesh Singh1b6a0342018-03-08 06:48:56 -06001156 qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
Brijesh Singh31dd67f2018-03-08 06:48:59 -06001157 qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001158#endif
1159#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +01001160 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001161#endif
1162#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +01001163 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001164#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001165#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001166 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001167#endif
Eduardo Habkost728b1422016-10-05 16:49:01 -03001168#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001169 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
1170 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001171#endif
1172#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1173 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001174 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001175#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001176}
1177
Peter Xu6adf08d2018-03-09 16:59:50 +08001178static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001179{
Markus Armbruster635db182017-03-03 13:32:27 +01001180 /*
1181 * Two command lists:
1182 * - qmp_commands contains all QMP commands
1183 * - qmp_cap_negotiation_commands contains just
1184 * "qmp_capabilities", to enforce capability negotiation
1185 */
1186
Markus Armbruster1527bad2017-03-03 13:32:25 +01001187 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001188
Markus Armbruster1527bad2017-03-03 13:32:25 +01001189 qmp_register_command(&qmp_commands, "query-qmp-schema",
1190 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001191 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001192 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001193 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001194 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001195 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001196
Markus Armbruster05875682017-03-03 13:32:24 +01001197 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001198
1199 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1200 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1201 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001202}
1203
Peter Xu71da4662018-03-09 16:59:57 +08001204static bool qmp_cap_enabled(Monitor *mon, QMPCapability cap)
Markus Armbruster635db182017-03-03 13:32:27 +01001205{
Peter Xu71da4662018-03-09 16:59:57 +08001206 return mon->qmp.qmp_caps[cap];
1207}
1208
1209static bool qmp_oob_enabled(Monitor *mon)
1210{
1211 return qmp_cap_enabled(mon, QMP_CAPABILITY_OOB);
1212}
1213
Peter Xu02130312018-03-09 16:59:53 +08001214static void qmp_caps_check(Monitor *mon, QMPCapabilityList *list,
1215 Error **errp)
Markus Armbruster635db182017-03-03 13:32:27 +01001216{
Peter Xu02130312018-03-09 16:59:53 +08001217 for (; list; list = list->next) {
1218 assert(list->value < QMP_CAPABILITY__MAX);
1219 switch (list->value) {
1220 case QMP_CAPABILITY_OOB:
1221 if (!mon->use_io_thr) {
1222 /*
1223 * Out-Of-Band only works with monitors that are
1224 * running on dedicated IOThread.
1225 */
1226 error_setg(errp, "This monitor does not support "
1227 "Out-Of-Band (OOB)");
1228 return;
1229 }
1230 break;
1231 default:
1232 break;
1233 }
1234 }
1235}
1236
1237/* This function should only be called after capabilities are checked. */
1238static void qmp_caps_apply(Monitor *mon, QMPCapabilityList *list)
1239{
1240 for (; list; list = list->next) {
1241 mon->qmp.qmp_caps[list->value] = true;
1242 }
1243}
1244
Peter Xucf869d52018-03-10 20:38:05 -06001245/*
1246 * Return true if check successful, or false otherwise. When false is
1247 * returned, detailed error will be in errp if provided.
1248 */
1249static bool qmp_cmd_oob_check(Monitor *mon, QDict *req, Error **errp)
1250{
1251 const char *command;
1252 QmpCommand *cmd;
1253
1254 command = qdict_get_try_str(req, "execute");
1255 if (!command) {
1256 error_setg(errp, "Command field 'execute' missing");
1257 return false;
1258 }
1259
1260 cmd = qmp_find_command(mon->qmp.commands, command);
1261 if (!cmd) {
Peter Xu9ddb7452018-03-26 14:38:54 +08001262 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
1263 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1264 "Expecting capabilities negotiation "
1265 "with 'qmp_capabilities'");
1266 } else {
1267 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1268 "The command %s has not been found", command);
1269 }
Peter Xucf869d52018-03-10 20:38:05 -06001270 return false;
1271 }
1272
1273 if (qmp_is_oob(req)) {
1274 if (!qmp_oob_enabled(mon)) {
1275 error_setg(errp, "Please enable Out-Of-Band first "
1276 "for the session during capabilities negotiation");
1277 return false;
1278 }
1279 if (!(cmd->options & QCO_ALLOW_OOB)) {
1280 error_setg(errp, "The command %s does not support OOB",
1281 command);
1282 return false;
1283 }
1284 }
1285
1286 return true;
1287}
1288
Peter Xu02130312018-03-09 16:59:53 +08001289void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
1290 Error **errp)
1291{
1292 Error *local_err = NULL;
1293
Markus Armbruster635db182017-03-03 13:32:27 +01001294 if (cur_mon->qmp.commands == &qmp_commands) {
1295 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1296 "Capabilities negotiation is already complete, command "
1297 "ignored");
1298 return;
1299 }
1300
Peter Xu02130312018-03-09 16:59:53 +08001301 /* Enable QMP capabilities provided by the client if applicable. */
1302 if (has_enable) {
1303 qmp_caps_check(cur_mon, enable, &local_err);
1304 if (local_err) {
1305 /*
1306 * Failed check on any of the capabilities will fail the
1307 * entire command (and thus not apply any of the other
1308 * capabilities that were also requested).
1309 */
1310 error_propagate(errp, local_err);
1311 return;
1312 }
1313 qmp_caps_apply(cur_mon, enable);
1314 }
1315
Markus Armbruster635db182017-03-03 13:32:27 +01001316 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001317}
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001318
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001319/* set the current CPU defined by the user */
1320int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001321{
Andreas Färber55e5c282012-12-17 06:18:02 +01001322 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001323
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001324 cpu = qemu_get_cpu(cpu_index);
1325 if (cpu == NULL) {
1326 return -1;
bellard6a00d602005-11-21 23:25:50 +00001327 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001328 g_free(cur_mon->mon_cpu_path);
1329 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001330 return 0;
bellard6a00d602005-11-21 23:25:50 +00001331}
1332
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001333static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001334{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001335 CPUState *cpu;
1336
1337 if (cur_mon->mon_cpu_path) {
1338 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1339 TYPE_CPU, NULL);
1340 if (!cpu) {
1341 g_free(cur_mon->mon_cpu_path);
1342 cur_mon->mon_cpu_path = NULL;
1343 }
1344 }
1345 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001346 if (!first_cpu) {
1347 return NULL;
1348 }
David Gibson27a83f82016-09-21 15:29:26 +10001349 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001350 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001351 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001352 if (synchronize) {
1353 cpu_synchronize_state(cpu);
1354 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001355 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001356}
1357
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001358CPUState *mon_get_cpu(void)
1359{
1360 return mon_get_cpu_sync(true);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001361}
1362
Pavel Butsykinbf957282015-09-10 18:38:59 +03001363CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001364{
Thomas Huth854e67f2017-01-13 13:12:35 +01001365 CPUState *cs = mon_get_cpu();
1366
1367 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001368}
1369
Luiz Capitulino99b77962011-10-24 10:53:44 -02001370int monitor_get_cpu_index(void)
1371{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001372 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001373
1374 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001375}
1376
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001377static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001378{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001379 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1380 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001381
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001382 if (all_cpus) {
1383 CPU_FOREACH(cs) {
1384 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1385 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1386 }
1387 } else {
1388 cs = mon_get_cpu();
1389
1390 if (!cs) {
1391 monitor_printf(mon, "No CPU available\n");
1392 return;
1393 }
1394
1395 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001396 }
bellard9307c4c2004-04-04 12:57:25 +00001397}
1398
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001399#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001400static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001401{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001402 if (!tcg_enabled()) {
1403 error_report("JIT information is only available with accel=tcg");
1404 return;
1405 }
1406
aliguori376253e2009-03-05 23:01:23 +00001407 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001408 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001409}
1410
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001411static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001412{
1413 dump_opcount_info((FILE *)mon, monitor_fprintf);
1414}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001415#endif
Max Filippov246ae242014-11-02 11:04:18 +03001416
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001417static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001418{
1419 int i;
bellard7e2515e2004-08-01 21:52:19 +00001420 const char *str;
ths3b46e622007-09-17 08:09:54 +00001421
aliguoricde76ee2009-03-05 23:01:51 +00001422 if (!mon->rs)
1423 return;
bellard7e2515e2004-08-01 21:52:19 +00001424 i = 0;
1425 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001426 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001427 if (!str)
1428 break;
aliguori376253e2009-03-05 23:01:23 +00001429 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001430 i++;
bellardaa455482004-04-04 13:07:25 +00001431 }
1432}
1433
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001434static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001435{
Thomas Huth854e67f2017-01-13 13:12:35 +01001436 CPUState *cs = mon_get_cpu();
1437
1438 if (!cs) {
1439 monitor_printf(mon, "No CPU available\n");
1440 return;
1441 }
1442 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001443}
j_mayer76a66252007-03-07 08:32:30 +00001444
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001445static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301446{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001447 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001448 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1449 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001450 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001451 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001452 Error *local_err = NULL;
1453
1454 if (name == NULL) {
1455 name = "*";
1456 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001457 if (vcpu < 0) {
1458 monitor_printf(mon, "argument vcpu must be positive");
1459 return;
1460 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001461
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001462 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001463 if (local_err) {
1464 error_report_err(local_err);
1465 return;
1466 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001467
1468 for (elem = events; elem != NULL; elem = elem->next) {
1469 monitor_printf(mon, "%s : state %u\n",
1470 elem->value->name,
1471 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1472 }
1473 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301474}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301475
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001476void qmp_client_migrate_info(const char *protocol, const char *hostname,
1477 bool has_port, int64_t port,
1478 bool has_tls_port, int64_t tls_port,
1479 bool has_cert_subject, const char *cert_subject,
1480 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001481{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001482 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001483 if (!qemu_using_spice(errp)) {
1484 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001485 }
1486
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001487 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001488 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001489 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001490 }
1491
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001492 if (qemu_spice_migrate_info(hostname,
1493 has_port ? port : -1,
1494 has_tls_port ? tls_port : -1,
1495 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001496 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001497 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001498 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001499 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001500 }
1501
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001502 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001503}
1504
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001505static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001506{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001507 Error *err = NULL;
1508
1509 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1510 if (err) {
1511 error_report_err(err);
1512 }
pbrooke735b912007-06-30 13:53:24 +00001513}
1514
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001515static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001516{
1517 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001518 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001519
bellard9307c4c2004-04-04 12:57:25 +00001520 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001521 mask = 0;
1522 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001523 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001524 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001525 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001526 return;
1527 }
1528 }
Peter Maydell24537a02013-02-11 16:41:23 +00001529 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001530}
1531
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001532static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001533{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001534 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001535 if (!option || !strcmp(option, "on")) {
1536 singlestep = 1;
1537 } else if (!strcmp(option, "off")) {
1538 singlestep = 0;
1539 } else {
1540 monitor_printf(mon, "unexpected option %s\n", option);
1541 }
1542}
1543
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001544static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001545{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001546 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001547 if (!device)
1548 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1549 if (gdbserver_start(device) < 0) {
1550 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1551 device);
1552 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001553 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001554 } else {
aliguori59030a82009-04-05 18:43:41 +00001555 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1556 device);
bellard8a7ddc32004-03-31 19:00:16 +00001557 }
1558}
1559
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001560static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001561{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001563 if (select_watchdog_action(action) == -1) {
1564 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1565 }
1566}
1567
aliguori376253e2009-03-05 23:01:23 +00001568static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001569{
aliguori376253e2009-03-05 23:01:23 +00001570 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001571 switch(c) {
1572 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001573 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001574 break;
1575 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001576 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001577 break;
1578 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001579 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001580 break;
1581 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001582 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001583 break;
1584 default:
1585 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001586 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001587 } else {
aliguori376253e2009-03-05 23:01:23 +00001588 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001589 }
1590 break;
1591 }
aliguori376253e2009-03-05 23:01:23 +00001592 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001593}
1594
aliguori376253e2009-03-05 23:01:23 +00001595static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001596 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001597{
Blue Swirl23842aa2010-01-12 20:27:43 +00001598 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001599 uint8_t buf[16];
1600 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001601 CPUState *cs = mon_get_cpu();
1602
1603 if (!cs && (format == 'i' || !is_physical)) {
1604 monitor_printf(mon, "Can not dump without CPU\n");
1605 return;
1606 }
bellard9307c4c2004-04-04 12:57:25 +00001607
1608 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001609 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001610 return;
1611 }
1612
1613 len = wsize * count;
1614 if (wsize == 1)
1615 line_size = 8;
1616 else
1617 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001618 max_digits = 0;
1619
1620 switch(format) {
1621 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001622 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001623 break;
1624 default:
1625 case 'x':
1626 max_digits = (wsize * 8) / 4;
1627 break;
1628 case 'u':
1629 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001630 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001631 break;
1632 case 'c':
1633 wsize = 1;
1634 break;
1635 }
1636
1637 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001638 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001639 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001640 else
aliguori376253e2009-03-05 23:01:23 +00001641 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001642 l = len;
1643 if (l > line_size)
1644 l = line_size;
1645 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001646 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001647 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001648 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001649 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001650 break;
1651 }
bellard9307c4c2004-04-04 12:57:25 +00001652 }
ths5fafdf22007-09-16 21:08:06 +00001653 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001654 while (i < l) {
1655 switch(wsize) {
1656 default:
1657 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001658 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001659 break;
1660 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001661 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001662 break;
1663 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001664 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001665 break;
1666 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001667 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001668 break;
1669 }
aliguori376253e2009-03-05 23:01:23 +00001670 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001671 switch(format) {
1672 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001673 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001674 break;
1675 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001676 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001677 break;
1678 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001679 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001680 break;
1681 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001682 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001683 break;
1684 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001685 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001686 break;
1687 }
1688 i += wsize;
1689 }
aliguori376253e2009-03-05 23:01:23 +00001690 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001691 addr += l;
1692 len -= l;
1693 }
1694}
1695
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001696static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001697{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001698 int count = qdict_get_int(qdict, "count");
1699 int format = qdict_get_int(qdict, "format");
1700 int size = qdict_get_int(qdict, "size");
1701 target_long addr = qdict_get_int(qdict, "addr");
1702
aliguori376253e2009-03-05 23:01:23 +00001703 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001704}
1705
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001706static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001707{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001708 int count = qdict_get_int(qdict, "count");
1709 int format = qdict_get_int(qdict, "format");
1710 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001711 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001712
aliguori376253e2009-03-05 23:01:23 +00001713 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001714}
1715
Paolo Bonzinie9628442017-04-20 15:30:58 +02001716static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1717{
1718 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1719 addr, 1);
1720
1721 if (!mrs.mr) {
1722 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1723 return NULL;
1724 }
1725
1726 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1727 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1728 memory_region_unref(mrs.mr);
1729 return NULL;
1730 }
1731
1732 *p_mr = mrs.mr;
1733 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1734}
1735
1736static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1737{
1738 hwaddr addr = qdict_get_int(qdict, "addr");
1739 Error *local_err = NULL;
1740 MemoryRegion *mr = NULL;
1741 void *ptr;
1742
1743 ptr = gpa2hva(&mr, addr, &local_err);
1744 if (local_err) {
1745 error_report_err(local_err);
1746 return;
1747 }
1748
1749 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1750 " (%s) is %p\n",
1751 addr, mr->name, ptr);
1752
1753 memory_region_unref(mr);
1754}
1755
1756#ifdef CONFIG_LINUX
1757static uint64_t vtop(void *ptr, Error **errp)
1758{
1759 uint64_t pinfo;
1760 uint64_t ret = -1;
1761 uintptr_t addr = (uintptr_t) ptr;
1762 uintptr_t pagesize = getpagesize();
1763 off_t offset = addr / pagesize * sizeof(pinfo);
1764 int fd;
1765
1766 fd = open("/proc/self/pagemap", O_RDONLY);
1767 if (fd == -1) {
1768 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1769 return -1;
1770 }
1771
1772 /* Force copy-on-write if necessary. */
1773 atomic_add((uint8_t *)ptr, 0);
1774
1775 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1776 error_setg_errno(errp, errno, "Cannot read pagemap");
1777 goto out;
1778 }
1779 if ((pinfo & (1ull << 63)) == 0) {
1780 error_setg(errp, "Page not present");
1781 goto out;
1782 }
1783 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1784
1785out:
1786 close(fd);
1787 return ret;
1788}
1789
1790static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1791{
1792 hwaddr addr = qdict_get_int(qdict, "addr");
1793 Error *local_err = NULL;
1794 MemoryRegion *mr = NULL;
1795 void *ptr;
1796 uint64_t physaddr;
1797
1798 ptr = gpa2hva(&mr, addr, &local_err);
1799 if (local_err) {
1800 error_report_err(local_err);
1801 return;
1802 }
1803
1804 physaddr = vtop(ptr, &local_err);
1805 if (local_err) {
1806 error_report_err(local_err);
1807 } else {
1808 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1809 " (%s) is 0x%" PRIx64 "\n",
1810 addr, mr->name, (uint64_t) physaddr);
1811 }
1812
1813 memory_region_unref(mr);
1814}
1815#endif
1816
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001817static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001818{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001819 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001820 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001821
bellard9307c4c2004-04-04 12:57:25 +00001822 switch(format) {
1823 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001824 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001825 break;
1826 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001827 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001828 break;
1829 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001830 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001831 break;
1832 default:
1833 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001834 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001835 break;
1836 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001837 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001838 break;
1839 }
aliguori376253e2009-03-05 23:01:23 +00001840 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001841}
1842
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001843static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001844{
1845 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001846 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001847 uint32_t start = qdict_get_int(qdict, "start");
1848 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001849
1850 sum = 0;
1851 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001852 uint8_t val = address_space_ldub(&address_space_memory, addr,
1853 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001854 /* BSD sum algorithm ('sum' Unix command) */
1855 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001856 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001857 }
aliguori376253e2009-03-05 23:01:23 +00001858 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001859}
1860
bellard13224a82006-07-14 22:03:35 +00001861static int mouse_button_state;
1862
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001863static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001864{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001865 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001866 const char *dx_str = qdict_get_str(qdict, "dx_str");
1867 const char *dy_str = qdict_get_str(qdict, "dy_str");
1868 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001869
bellard13224a82006-07-14 22:03:35 +00001870 dx = strtol(dx_str, NULL, 0);
1871 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001872 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1873 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1874
1875 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001876 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001877 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001878 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001879 qemu_input_queue_btn(NULL, button, true);
1880 qemu_input_event_sync();
1881 qemu_input_queue_btn(NULL, button, false);
1882 }
1883 }
1884 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001885}
1886
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001887static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001888{
Eric Blake7fb1cf1602015-11-18 01:52:57 -07001889 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001890 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1891 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1892 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1893 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001894 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001895
1896 if (mouse_button_state == button_state) {
1897 return;
1898 }
1899 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1900 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001901 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001902}
1903
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001904static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001905{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001906 int size = qdict_get_int(qdict, "size");
1907 int addr = qdict_get_int(qdict, "addr");
1908 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001909 uint32_t val;
1910 int suffix;
1911
1912 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001913 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001914 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001915 addr++;
1916 }
1917 addr &= 0xffff;
1918
1919 switch(size) {
1920 default:
1921 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001922 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001923 suffix = 'b';
1924 break;
1925 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001926 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001927 suffix = 'w';
1928 break;
1929 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001930 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001931 suffix = 'l';
1932 break;
1933 }
aliguori376253e2009-03-05 23:01:23 +00001934 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1935 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001936}
bellarda3a91a32004-06-04 11:06:21 +00001937
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001938static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001939{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001940 int size = qdict_get_int(qdict, "size");
1941 int addr = qdict_get_int(qdict, "addr");
1942 int val = qdict_get_int(qdict, "val");
1943
Jan Kiszkaf1147842009-07-14 10:20:11 +02001944 addr &= IOPORTS_MASK;
1945
1946 switch (size) {
1947 default:
1948 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001949 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001950 break;
1951 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001952 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001953 break;
1954 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001955 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001956 break;
1957 }
1958}
1959
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001960static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001961{
Gongleif1839932014-12-03 18:20:58 +00001962 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001963 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001964
Gongleif1839932014-12-03 18:20:58 +00001965 qemu_boot_set(bootdevice, &local_err);
1966 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001967 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001968 } else {
Gongleif1839932014-12-03 18:20:58 +00001969 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001970 }
1971}
1972
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001973static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001974{
Peter Xu57bb40c2017-01-16 16:40:05 +08001975 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001976 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001977
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001978 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001979}
1980
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001981static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001982{
aliguorib28b6232009-04-22 20:20:29 +00001983 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001984 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001985 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001986
Igor Mammedovf75cd442017-05-30 18:23:59 +02001987 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001988 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1989
zhanghailiang5b009e42014-11-04 19:49:30 +08001990 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001991 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1992 for (i = 0; i < nb_numa_nodes; i++) {
1993 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001994 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1995 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1996 cpu->value->props->node_id == i) {
1997 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001998 }
1999 }
2000 monitor_printf(mon, "\n");
2001 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02002002 node_mem[i].node_mem >> 20);
2003 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
2004 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00002005 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02002006 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08002007 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002008}
2009
bellard5f1ce942006-02-08 22:40:15 +00002010#ifdef CONFIG_PROFILER
2011
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002012int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002013int64_t dev_time;
2014
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002015static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002016{
aliguori376253e2009-03-05 23:01:23 +00002017 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302018 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00002019 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302020 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002021 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002022 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002023}
2024#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002025static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002026{
aliguori376253e2009-03-05 23:01:23 +00002027 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002028}
2029#endif
2030
bellardec36b692006-07-16 18:57:03 +00002031/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002032static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002033
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002034static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002035{
2036 int i;
2037 CaptureState *s;
2038
2039 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002040 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002041 s->ops.info (s->opaque);
2042 }
2043}
2044
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002045static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002046{
2047 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002048 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002049 CaptureState *s;
2050
2051 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2052 if (i == n) {
2053 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002054 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002055 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002056 return;
2057 }
2058 }
2059}
2060
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002061static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002062{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002063 const char *path = qdict_get_str(qdict, "path");
2064 int has_freq = qdict_haskey(qdict, "freq");
2065 int freq = qdict_get_try_int(qdict, "freq", -1);
2066 int has_bits = qdict_haskey(qdict, "bits");
2067 int bits = qdict_get_try_int(qdict, "bits", -1);
2068 int has_channels = qdict_haskey(qdict, "nchannels");
2069 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002070 CaptureState *s;
2071
Anthony Liguori7267c092011-08-20 22:09:37 -05002072 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002073
2074 freq = has_freq ? freq : 44100;
2075 bits = has_bits ? bits : 16;
2076 nchannels = has_channels ? nchannels : 2;
2077
2078 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002079 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002080 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002081 return;
bellardec36b692006-07-16 18:57:03 +00002082 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002083 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002084}
bellardec36b692006-07-16 18:57:03 +00002085
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002086static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002087{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002088 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002089
aliguori76655d62009-03-06 20:27:37 +00002090 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002091 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002092 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002093 return acl;
2094}
aliguori76655d62009-03-06 20:27:37 +00002095
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002096static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002097{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002098 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002099 qemu_acl *acl = find_acl(mon, aclname);
2100 qemu_acl_entry *entry;
2101 int i = 0;
2102
2103 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002104 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002105 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002106 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002107 i++;
2108 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002109 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002110 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002111 }
2112}
2113
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002114static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002115{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002116 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002117 qemu_acl *acl = find_acl(mon, aclname);
2118
2119 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002120 qemu_acl_reset(acl);
2121 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002122 }
2123}
aliguori76655d62009-03-06 20:27:37 +00002124
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002125static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002127 const char *aclname = qdict_get_str(qdict, "aclname");
2128 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002129 qemu_acl *acl = find_acl(mon, aclname);
2130
2131 if (acl) {
2132 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002133 acl->defaultDeny = 0;
2134 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002135 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002136 acl->defaultDeny = 1;
2137 monitor_printf(mon, "acl: policy set to 'deny'\n");
2138 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139 monitor_printf(mon, "acl: unknown policy '%s', "
2140 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002141 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002142 }
2143}
aliguori76655d62009-03-06 20:27:37 +00002144
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002145static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002146{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002147 const char *aclname = qdict_get_str(qdict, "aclname");
2148 const char *match = qdict_get_str(qdict, "match");
2149 const char *policy = qdict_get_str(qdict, "policy");
2150 int has_index = qdict_haskey(qdict, "index");
2151 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 qemu_acl *acl = find_acl(mon, aclname);
2153 int deny, ret;
2154
2155 if (acl) {
2156 if (strcmp(policy, "allow") == 0) {
2157 deny = 0;
2158 } else if (strcmp(policy, "deny") == 0) {
2159 deny = 1;
2160 } else {
2161 monitor_printf(mon, "acl: unknown policy '%s', "
2162 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002163 return;
2164 }
aliguori28a76be2009-03-06 20:27:40 +00002165 if (has_index)
2166 ret = qemu_acl_insert(acl, deny, match, index);
2167 else
2168 ret = qemu_acl_append(acl, deny, match);
2169 if (ret < 0)
2170 monitor_printf(mon, "acl: unable to add acl entry\n");
2171 else
2172 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002173 }
2174}
aliguori76655d62009-03-06 20:27:37 +00002175
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002176static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002177{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002178 const char *aclname = qdict_get_str(qdict, "aclname");
2179 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180 qemu_acl *acl = find_acl(mon, aclname);
2181 int ret;
aliguori76655d62009-03-06 20:27:37 +00002182
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002184 ret = qemu_acl_remove(acl, match);
2185 if (ret < 0)
2186 monitor_printf(mon, "acl: no matching acl entry\n");
2187 else
2188 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002189 }
2190}
2191
Corey Bryant208c9d12012-06-22 14:36:09 -04002192void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002193{
Anthony Liguoric227f092009-10-01 16:12:16 -05002194 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002195 int fd;
2196
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002197 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002198 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002199 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002200 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002201 }
2202
2203 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002204 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002205 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2206 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002207 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002208 }
2209
Corey Bryant208c9d12012-06-22 14:36:09 -04002210 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002211 if (strcmp(monfd->name, fdname) != 0) {
2212 continue;
2213 }
2214
2215 close(monfd->fd);
2216 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002217 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002218 }
2219
Anthony Liguori7267c092011-08-20 22:09:37 -05002220 monfd = g_malloc0(sizeof(mon_fd_t));
2221 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002222 monfd->fd = fd;
2223
Corey Bryant208c9d12012-06-22 14:36:09 -04002224 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002225}
2226
Corey Bryant208c9d12012-06-22 14:36:09 -04002227void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002228{
Anthony Liguoric227f092009-10-01 16:12:16 -05002229 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002230
Corey Bryant208c9d12012-06-22 14:36:09 -04002231 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002232 if (strcmp(monfd->name, fdname) != 0) {
2233 continue;
2234 }
2235
Blue Swirl72cf2d42009-09-12 07:36:22 +00002236 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002237 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002238 g_free(monfd->name);
2239 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002240 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002241 }
2242
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002243 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002244}
2245
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002246int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002247{
Anthony Liguoric227f092009-10-01 16:12:16 -05002248 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002249
Blue Swirl72cf2d42009-09-12 07:36:22 +00002250 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002251 int fd;
2252
2253 if (strcmp(monfd->name, fdname) != 0) {
2254 continue;
2255 }
2256
2257 fd = monfd->fd;
2258
2259 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002260 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002261 g_free(monfd->name);
2262 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002263
2264 return fd;
2265 }
2266
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002267 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002268 return -1;
2269}
2270
Corey Bryantba1c048a2012-08-14 16:43:43 -04002271static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2272{
2273 MonFdsetFd *mon_fdset_fd;
2274 MonFdsetFd *mon_fdset_fd_next;
2275
2276 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002277 if ((mon_fdset_fd->removed ||
2278 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2279 runstate_is_running()) {
Corey Bryantba1c048a2012-08-14 16:43:43 -04002280 close(mon_fdset_fd->fd);
2281 g_free(mon_fdset_fd->opaque);
2282 QLIST_REMOVE(mon_fdset_fd, next);
2283 g_free(mon_fdset_fd);
2284 }
2285 }
2286
Corey Bryantadb696f2012-08-14 16:43:47 -04002287 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c048a2012-08-14 16:43:43 -04002288 QLIST_REMOVE(mon_fdset, next);
2289 g_free(mon_fdset);
2290 }
2291}
2292
Corey Bryantefb87c12012-08-14 16:43:48 -04002293static void monitor_fdsets_cleanup(void)
2294{
2295 MonFdset *mon_fdset;
2296 MonFdset *mon_fdset_next;
2297
2298 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2299 monitor_fdset_cleanup(mon_fdset);
2300 }
2301}
2302
Corey Bryantba1c048a2012-08-14 16:43:43 -04002303AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2304 const char *opaque, Error **errp)
2305{
2306 int fd;
2307 Monitor *mon = cur_mon;
Corey Bryantba1c048a2012-08-14 16:43:43 -04002308 AddfdInfo *fdinfo;
2309
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002310 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c048a2012-08-14 16:43:43 -04002311 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002312 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c048a2012-08-14 16:43:43 -04002313 goto error;
2314 }
2315
Corey Bryante446f702012-10-18 15:19:32 -04002316 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2317 has_opaque, opaque, errp);
2318 if (fdinfo) {
2319 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002320 }
2321
Corey Bryantba1c048a2012-08-14 16:43:43 -04002322error:
2323 if (fd != -1) {
2324 close(fd);
2325 }
2326 return NULL;
2327}
2328
2329void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2330{
2331 MonFdset *mon_fdset;
2332 MonFdsetFd *mon_fdset_fd;
2333 char fd_str[60];
2334
2335 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2336 if (mon_fdset->id != fdset_id) {
2337 continue;
2338 }
2339 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2340 if (has_fd) {
2341 if (mon_fdset_fd->fd != fd) {
2342 continue;
2343 }
2344 mon_fdset_fd->removed = true;
2345 break;
2346 } else {
2347 mon_fdset_fd->removed = true;
2348 }
2349 }
2350 if (has_fd && !mon_fdset_fd) {
2351 goto error;
2352 }
2353 monitor_fdset_cleanup(mon_fdset);
2354 return;
2355 }
2356
2357error:
2358 if (has_fd) {
2359 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2360 fdset_id, fd);
2361 } else {
2362 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2363 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002364 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c048a2012-08-14 16:43:43 -04002365}
2366
2367FdsetInfoList *qmp_query_fdsets(Error **errp)
2368{
2369 MonFdset *mon_fdset;
2370 MonFdsetFd *mon_fdset_fd;
2371 FdsetInfoList *fdset_list = NULL;
2372
2373 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2374 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2375 FdsetFdInfoList *fdsetfd_list = NULL;
2376
2377 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2378 fdset_info->value->fdset_id = mon_fdset->id;
2379
2380 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2381 FdsetFdInfoList *fdsetfd_info;
2382
2383 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2384 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2385 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2386 if (mon_fdset_fd->opaque) {
2387 fdsetfd_info->value->has_opaque = true;
2388 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2389 } else {
2390 fdsetfd_info->value->has_opaque = false;
2391 }
2392
2393 fdsetfd_info->next = fdsetfd_list;
2394 fdsetfd_list = fdsetfd_info;
2395 }
2396
2397 fdset_info->value->fds = fdsetfd_list;
2398
2399 fdset_info->next = fdset_list;
2400 fdset_list = fdset_info;
2401 }
2402
2403 return fdset_list;
2404}
2405
Corey Bryante446f702012-10-18 15:19:32 -04002406AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2407 bool has_opaque, const char *opaque,
2408 Error **errp)
2409{
2410 MonFdset *mon_fdset = NULL;
2411 MonFdsetFd *mon_fdset_fd;
2412 AddfdInfo *fdinfo;
2413
2414 if (has_fdset_id) {
2415 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2416 /* Break if match found or match impossible due to ordering by ID */
2417 if (fdset_id <= mon_fdset->id) {
2418 if (fdset_id < mon_fdset->id) {
2419 mon_fdset = NULL;
2420 }
2421 break;
2422 }
2423 }
2424 }
2425
2426 if (mon_fdset == NULL) {
2427 int64_t fdset_id_prev = -1;
2428 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2429
2430 if (has_fdset_id) {
2431 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002432 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2433 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002434 return NULL;
2435 }
2436 /* Use specified fdset ID */
2437 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2438 mon_fdset_cur = mon_fdset;
2439 if (fdset_id < mon_fdset_cur->id) {
2440 break;
2441 }
2442 }
2443 } else {
2444 /* Use first available fdset ID */
2445 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2446 mon_fdset_cur = mon_fdset;
2447 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2448 fdset_id_prev = mon_fdset_cur->id;
2449 continue;
2450 }
2451 break;
2452 }
2453 }
2454
2455 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2456 if (has_fdset_id) {
2457 mon_fdset->id = fdset_id;
2458 } else {
2459 mon_fdset->id = fdset_id_prev + 1;
2460 }
2461
2462 /* The fdset list is ordered by fdset ID */
2463 if (!mon_fdset_cur) {
2464 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2465 } else if (mon_fdset->id < mon_fdset_cur->id) {
2466 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2467 } else {
2468 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2469 }
2470 }
2471
2472 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2473 mon_fdset_fd->fd = fd;
2474 mon_fdset_fd->removed = false;
2475 if (has_opaque) {
2476 mon_fdset_fd->opaque = g_strdup(opaque);
2477 }
2478 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2479
2480 fdinfo = g_malloc0(sizeof(*fdinfo));
2481 fdinfo->fdset_id = mon_fdset->id;
2482 fdinfo->fd = mon_fdset_fd->fd;
2483
2484 return fdinfo;
2485}
2486
Corey Bryantadb696f2012-08-14 16:43:47 -04002487int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2488{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002489#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002490 MonFdset *mon_fdset;
2491 MonFdsetFd *mon_fdset_fd;
2492 int mon_fd_flags;
2493
Corey Bryantadb696f2012-08-14 16:43:47 -04002494 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2495 if (mon_fdset->id != fdset_id) {
2496 continue;
2497 }
2498 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2499 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2500 if (mon_fd_flags == -1) {
2501 return -1;
2502 }
2503
2504 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2505 return mon_fdset_fd->fd;
2506 }
2507 }
2508 errno = EACCES;
2509 return -1;
2510 }
2511#endif
2512
2513 errno = ENOENT;
2514 return -1;
2515}
2516
2517int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2518{
2519 MonFdset *mon_fdset;
2520 MonFdsetFd *mon_fdset_fd_dup;
2521
2522 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2523 if (mon_fdset->id != fdset_id) {
2524 continue;
2525 }
2526 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2527 if (mon_fdset_fd_dup->fd == dup_fd) {
2528 return -1;
2529 }
2530 }
2531 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2532 mon_fdset_fd_dup->fd = dup_fd;
2533 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2534 return 0;
2535 }
2536 return -1;
2537}
2538
2539static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2540{
2541 MonFdset *mon_fdset;
2542 MonFdsetFd *mon_fdset_fd_dup;
2543
2544 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2545 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2546 if (mon_fdset_fd_dup->fd == dup_fd) {
2547 if (remove) {
2548 QLIST_REMOVE(mon_fdset_fd_dup, next);
2549 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2550 monitor_fdset_cleanup(mon_fdset);
2551 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002552 return -1;
2553 } else {
2554 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002555 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002556 }
2557 }
2558 }
2559 return -1;
2560}
2561
2562int monitor_fdset_dup_fd_find(int dup_fd)
2563{
2564 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2565}
2566
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002567void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002568{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002569 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002570}
2571
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002572int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002573{
2574 int fd;
2575 Error *local_err = NULL;
2576
2577 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002578 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002579 } else {
2580 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002581 if (fd == -1) {
2582 error_setg(&local_err, "Invalid file descriptor number '%s'",
2583 fdname);
2584 }
2585 }
2586 if (local_err) {
2587 error_propagate(errp, local_err);
2588 assert(fd == -1);
2589 } else {
2590 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002591 }
2592
2593 return fd;
2594}
2595
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002596/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002597static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002598#include "hmp-commands-info.h"
2599 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002600};
2601
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002602/* mon_cmds and info_cmds would be sorted at runtime */
2603static mon_cmd_t mon_cmds[] = {
2604#include "hmp-commands.h"
2605 { NULL, NULL, },
2606};
2607
bellard9307c4c2004-04-04 12:57:25 +00002608/*******************************************************************/
2609
2610static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002611static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002612
bellard9307c4c2004-04-04 12:57:25 +00002613
Stefan Weil9c3175c2013-08-22 21:30:09 +02002614static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2615expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002616{
Fam Zheng277acfe2013-08-20 10:58:21 +08002617 va_list ap;
2618 va_start(ap, fmt);
2619 monitor_vprintf(mon, fmt, ap);
2620 monitor_printf(mon, "\n");
2621 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002622 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002623}
2624
Markus Armbruster09b94182010-01-20 13:07:30 +01002625/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002626static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002627{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002628 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002629 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002630 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002631 uint64_t tmp = 0;
2632 int ret;
bellard92a31b12005-02-10 22:00:52 +00002633
Thomas Huth854e67f2017-01-13 13:12:35 +01002634 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002635 return -1;
2636 }
2637
2638 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002639 if (compare_cmd(name, md->name)) {
2640 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002641 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002642 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002643 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002644 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002645 switch(md->type) {
2646 case MD_I32:
2647 *pval = *(int32_t *)ptr;
2648 break;
2649 case MD_TLONG:
2650 *pval = *(target_long *)ptr;
2651 break;
2652 default:
2653 *pval = 0;
2654 break;
2655 }
bellard9307c4c2004-04-04 12:57:25 +00002656 }
2657 return 0;
2658 }
2659 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002660
Thomas Huth854e67f2017-01-13 13:12:35 +01002661 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002662 if (!ret) {
2663 *pval = (target_long) tmp;
2664 }
2665
2666 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002667}
2668
2669static void next(void)
2670{
Blue Swirl660f11b2009-07-31 21:16:51 +00002671 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002672 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002673 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002674 pch++;
2675 }
2676}
2677
aliguori376253e2009-03-05 23:01:23 +00002678static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002679
aliguori376253e2009-03-05 23:01:23 +00002680static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002681{
blueswir1c2efc952007-09-25 17:28:42 +00002682 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002683 char *p;
bellard6a00d602005-11-21 23:25:50 +00002684 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002685
2686 switch(*pch) {
2687 case '+':
2688 next();
aliguori376253e2009-03-05 23:01:23 +00002689 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002690 break;
2691 case '-':
2692 next();
aliguori376253e2009-03-05 23:01:23 +00002693 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002694 break;
2695 case '~':
2696 next();
aliguori376253e2009-03-05 23:01:23 +00002697 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002698 break;
2699 case '(':
2700 next();
aliguori376253e2009-03-05 23:01:23 +00002701 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002702 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002703 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002704 }
2705 next();
2706 break;
bellard81d09122004-07-14 17:21:37 +00002707 case '\'':
2708 pch++;
2709 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002710 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002711 n = *pch;
2712 pch++;
2713 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002714 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002715 next();
2716 break;
bellard9307c4c2004-04-04 12:57:25 +00002717 case '$':
2718 {
2719 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002720 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002721
bellard9307c4c2004-04-04 12:57:25 +00002722 pch++;
2723 q = buf;
2724 while ((*pch >= 'a' && *pch <= 'z') ||
2725 (*pch >= 'A' && *pch <= 'Z') ||
2726 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002727 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002728 if ((q - buf) < sizeof(buf) - 1)
2729 *q++ = *pch;
2730 pch++;
2731 }
blueswir1cd390082008-11-16 13:53:32 +00002732 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002733 pch++;
2734 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002735 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002736 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002737 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002738 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002739 }
2740 break;
2741 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002742 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002743 n = 0;
2744 break;
2745 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002746 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002747 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002748 if (errno == ERANGE) {
2749 expr_error(mon, "number too large");
2750 }
bellard9307c4c2004-04-04 12:57:25 +00002751 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002752 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002753 }
2754 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002755 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002756 pch++;
2757 break;
2758 }
2759 return n;
2760}
2761
2762
aliguori376253e2009-03-05 23:01:23 +00002763static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002764{
blueswir1c2efc952007-09-25 17:28:42 +00002765 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002766 int op;
ths3b46e622007-09-17 08:09:54 +00002767
aliguori376253e2009-03-05 23:01:23 +00002768 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002769 for(;;) {
2770 op = *pch;
2771 if (op != '*' && op != '/' && op != '%')
2772 break;
2773 next();
aliguori376253e2009-03-05 23:01:23 +00002774 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002775 switch(op) {
2776 default:
2777 case '*':
2778 val *= val2;
2779 break;
2780 case '/':
2781 case '%':
ths5fafdf22007-09-16 21:08:06 +00002782 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002783 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002784 if (op == '/')
2785 val /= val2;
2786 else
2787 val %= val2;
2788 break;
2789 }
2790 }
2791 return val;
2792}
2793
aliguori376253e2009-03-05 23:01:23 +00002794static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002795{
blueswir1c2efc952007-09-25 17:28:42 +00002796 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002797 int op;
bellard9307c4c2004-04-04 12:57:25 +00002798
aliguori376253e2009-03-05 23:01:23 +00002799 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002800 for(;;) {
2801 op = *pch;
2802 if (op != '&' && op != '|' && op != '^')
2803 break;
2804 next();
aliguori376253e2009-03-05 23:01:23 +00002805 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002806 switch(op) {
2807 default:
2808 case '&':
2809 val &= val2;
2810 break;
2811 case '|':
2812 val |= val2;
2813 break;
2814 case '^':
2815 val ^= val2;
2816 break;
2817 }
2818 }
2819 return val;
2820}
2821
aliguori376253e2009-03-05 23:01:23 +00002822static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002823{
blueswir1c2efc952007-09-25 17:28:42 +00002824 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002825 int op;
bellard9307c4c2004-04-04 12:57:25 +00002826
aliguori376253e2009-03-05 23:01:23 +00002827 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002828 for(;;) {
2829 op = *pch;
2830 if (op != '+' && op != '-')
2831 break;
2832 next();
aliguori376253e2009-03-05 23:01:23 +00002833 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002834 if (op == '+')
2835 val += val2;
2836 else
2837 val -= val2;
2838 }
2839 return val;
2840}
2841
aliguori376253e2009-03-05 23:01:23 +00002842static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002843{
2844 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002845 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002846 *pp = pch;
2847 return -1;
2848 }
blueswir1cd390082008-11-16 13:53:32 +00002849 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002850 pch++;
aliguori376253e2009-03-05 23:01:23 +00002851 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002852 *pp = pch;
2853 return 0;
2854}
2855
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002856static int get_double(Monitor *mon, double *pval, const char **pp)
2857{
2858 const char *p = *pp;
2859 char *tailp;
2860 double d;
2861
2862 d = strtod(p, &tailp);
2863 if (tailp == p) {
2864 monitor_printf(mon, "Number expected\n");
2865 return -1;
2866 }
2867 if (d != d || d - d != 0) {
2868 /* NaN or infinity */
2869 monitor_printf(mon, "Bad number\n");
2870 return -1;
2871 }
2872 *pval = d;
2873 *pp = tailp;
2874 return 0;
2875}
2876
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002877/*
2878 * Store the command-name in cmdname, and return a pointer to
2879 * the remaining of the command string.
2880 */
2881static const char *get_command_name(const char *cmdline,
2882 char *cmdname, size_t nlen)
2883{
2884 size_t len;
2885 const char *p, *pstart;
2886
2887 p = cmdline;
2888 while (qemu_isspace(*p))
2889 p++;
2890 if (*p == '\0')
2891 return NULL;
2892 pstart = p;
2893 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2894 p++;
2895 len = p - pstart;
2896 if (len > nlen - 1)
2897 len = nlen - 1;
2898 memcpy(cmdname, pstart, len);
2899 cmdname[len] = '\0';
2900 return p;
2901}
2902
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002903/**
2904 * Read key of 'type' into 'key' and return the current
2905 * 'type' pointer.
2906 */
2907static char *key_get_info(const char *type, char **key)
2908{
2909 size_t len;
2910 char *p, *str;
2911
2912 if (*type == ',')
2913 type++;
2914
2915 p = strchr(type, ':');
2916 if (!p) {
2917 *key = NULL;
2918 return NULL;
2919 }
2920 len = p - type;
2921
Anthony Liguori7267c092011-08-20 22:09:37 -05002922 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002923 memcpy(str, type, len);
2924 str[len] = '\0';
2925
2926 *key = str;
2927 return ++p;
2928}
2929
bellard9307c4c2004-04-04 12:57:25 +00002930static int default_fmt_format = 'x';
2931static int default_fmt_size = 4;
2932
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002933static int is_valid_option(const char *c, const char *typestr)
2934{
2935 char option[3];
2936
2937 option[0] = '-';
2938 option[1] = *c;
2939 option[2] = '\0';
2940
2941 typestr = strstr(typestr, option);
2942 return (typestr != NULL);
2943}
2944
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002945static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2946 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002947{
2948 const mon_cmd_t *cmd;
2949
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002950 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002951 if (compare_cmd(cmdname, cmd->name)) {
2952 return cmd;
2953 }
2954 }
2955
2956 return NULL;
2957}
2958
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002959/*
Bandan Dasae502122015-06-03 18:38:08 -04002960 * Parse command name from @cmdp according to command table @table.
2961 * If blank, return NULL.
2962 * Else, if no valid command can be found, report to @mon, and return
2963 * NULL.
2964 * Else, change @cmdp to point right behind the name, and return its
2965 * command table entry.
2966 * Do not assume the return value points into @table! It doesn't when
2967 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002968 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002969static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002970 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04002971 const char **cmdp,
2972 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002973{
Bandan Dasae502122015-06-03 18:38:08 -04002974 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002975 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002976 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002977
bellard9307c4c2004-04-04 12:57:25 +00002978 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002979 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002980 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002981 return NULL;
ths3b46e622007-09-17 08:09:54 +00002982
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002983 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002984 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002985 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002986 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002987 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002988 }
bellard9307c4c2004-04-04 12:57:25 +00002989
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002990 /* filter out following useless space */
2991 while (qemu_isspace(*p)) {
2992 p++;
2993 }
Bandan Dasae502122015-06-03 18:38:08 -04002994
2995 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002996 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002997 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002998 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002999 }
3000
Bandan Dasae502122015-06-03 18:38:08 -04003001 return cmd;
3002}
3003
3004/*
3005 * Parse arguments for @cmd.
3006 * If it can't be parsed, report to @mon, and return NULL.
3007 * Else, insert command arguments into a QDict, and return it.
3008 * Note: On success, caller has to free the QDict structure.
3009 */
3010
3011static QDict *monitor_parse_arguments(Monitor *mon,
3012 const char **endp,
3013 const mon_cmd_t *cmd)
3014{
3015 const char *typestr;
3016 char *key;
3017 int c;
3018 const char *p = *endp;
3019 char buf[1024];
3020 QDict *qdict = qdict_new();
3021
bellard9307c4c2004-04-04 12:57:25 +00003022 /* parse the parameters */
3023 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003024 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003025 typestr = key_get_info(typestr, &key);
3026 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003027 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003028 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003029 typestr++;
3030 switch(c) {
3031 case 'F':
bellard81d09122004-07-14 17:21:37 +00003032 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003033 case 's':
3034 {
3035 int ret;
ths3b46e622007-09-17 08:09:54 +00003036
blueswir1cd390082008-11-16 13:53:32 +00003037 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003038 p++;
3039 if (*typestr == '?') {
3040 typestr++;
3041 if (*p == '\0') {
3042 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003043 break;
bellard9307c4c2004-04-04 12:57:25 +00003044 }
3045 }
3046 ret = get_str(buf, sizeof(buf), &p);
3047 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003048 switch(c) {
3049 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003050 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003051 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003052 break;
3053 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003054 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003055 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003056 break;
3057 default:
Bandan Dasae502122015-06-03 18:38:08 -04003058 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003059 break;
3060 }
bellard9307c4c2004-04-04 12:57:25 +00003061 goto fail;
3062 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003063 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00003064 }
3065 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003066 case 'O':
3067 {
3068 QemuOptsList *opts_list;
3069 QemuOpts *opts;
3070
3071 opts_list = qemu_find_opts(key);
3072 if (!opts_list || opts_list->desc->name) {
3073 goto bad_type;
3074 }
3075 while (qemu_isspace(*p)) {
3076 p++;
3077 }
3078 if (!*p)
3079 break;
3080 if (get_str(buf, sizeof(buf), &p) < 0) {
3081 goto fail;
3082 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003083 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003084 if (!opts) {
3085 goto fail;
3086 }
3087 qemu_opts_to_qdict(opts, qdict);
3088 qemu_opts_del(opts);
3089 }
3090 break;
bellard9307c4c2004-04-04 12:57:25 +00003091 case '/':
3092 {
3093 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003094
blueswir1cd390082008-11-16 13:53:32 +00003095 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003096 p++;
3097 if (*p == '/') {
3098 /* format found */
3099 p++;
3100 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003101 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003102 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003103 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003104 count = count * 10 + (*p - '0');
3105 p++;
3106 }
3107 }
3108 size = -1;
3109 format = -1;
3110 for(;;) {
3111 switch(*p) {
3112 case 'o':
3113 case 'd':
3114 case 'u':
3115 case 'x':
3116 case 'i':
3117 case 'c':
3118 format = *p++;
3119 break;
3120 case 'b':
3121 size = 1;
3122 p++;
3123 break;
3124 case 'h':
3125 size = 2;
3126 p++;
3127 break;
3128 case 'w':
3129 size = 4;
3130 p++;
3131 break;
3132 case 'g':
3133 case 'L':
3134 size = 8;
3135 p++;
3136 break;
3137 default:
3138 goto next;
3139 }
3140 }
3141 next:
blueswir1cd390082008-11-16 13:53:32 +00003142 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003143 monitor_printf(mon, "invalid char in format: '%c'\n",
3144 *p);
bellard9307c4c2004-04-04 12:57:25 +00003145 goto fail;
3146 }
bellard9307c4c2004-04-04 12:57:25 +00003147 if (format < 0)
3148 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003149 if (format != 'i') {
3150 /* for 'i', not specifying a size gives -1 as size */
3151 if (size < 0)
3152 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003153 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003154 }
bellard9307c4c2004-04-04 12:57:25 +00003155 default_fmt_format = format;
3156 } else {
3157 count = 1;
3158 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003159 if (format != 'i') {
3160 size = default_fmt_size;
3161 } else {
3162 size = -1;
3163 }
bellard9307c4c2004-04-04 12:57:25 +00003164 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003165 qdict_put_int(qdict, "count", count);
3166 qdict_put_int(qdict, "format", format);
3167 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00003168 }
3169 break;
3170 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003171 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003172 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003173 {
blueswir1c2efc952007-09-25 17:28:42 +00003174 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003175
blueswir1cd390082008-11-16 13:53:32 +00003176 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003177 p++;
bellard34405572004-06-08 00:55:58 +00003178 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003179 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003180 if (*p == '\0') {
3181 typestr++;
3182 break;
3183 }
bellard34405572004-06-08 00:55:58 +00003184 } else {
3185 if (*p == '.') {
3186 p++;
blueswir1cd390082008-11-16 13:53:32 +00003187 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003188 p++;
bellard34405572004-06-08 00:55:58 +00003189 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003190 typestr++;
3191 break;
bellard34405572004-06-08 00:55:58 +00003192 }
3193 }
bellard13224a82006-07-14 22:03:35 +00003194 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003195 }
aliguori376253e2009-03-05 23:01:23 +00003196 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003197 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003198 /* Check if 'i' is greater than 32-bit */
3199 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003200 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003201 monitor_printf(mon, "integer is for 32-bit values\n");
3202 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003203 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003204 if (val < 0) {
3205 monitor_printf(mon, "enter a positive value\n");
3206 goto fail;
3207 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003208 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003209 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003210 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00003211 }
3212 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003213 case 'o':
3214 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003215 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003216 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003217 char *end;
3218
3219 while (qemu_isspace(*p)) {
3220 p++;
3221 }
3222 if (*typestr == '?') {
3223 typestr++;
3224 if (*p == '\0') {
3225 break;
3226 }
3227 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003228 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003229 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02003230 monitor_printf(mon, "invalid size\n");
3231 goto fail;
3232 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003233 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02003234 p = end;
3235 }
3236 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003237 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003238 {
3239 double val;
3240
3241 while (qemu_isspace(*p))
3242 p++;
3243 if (*typestr == '?') {
3244 typestr++;
3245 if (*p == '\0') {
3246 break;
3247 }
3248 }
3249 if (get_double(mon, &val, &p) < 0) {
3250 goto fail;
3251 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003252 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003253 switch (*p) {
3254 case 'm':
3255 val /= 1e3; p += 2; break;
3256 case 'u':
3257 val /= 1e6; p += 2; break;
3258 case 'n':
3259 val /= 1e9; p += 2; break;
3260 }
3261 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003262 if (*p && !qemu_isspace(*p)) {
3263 monitor_printf(mon, "Unknown unit suffix\n");
3264 goto fail;
3265 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04003266 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003267 }
3268 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003269 case 'b':
3270 {
3271 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003272 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003273
3274 while (qemu_isspace(*p)) {
3275 p++;
3276 }
3277 beg = p;
3278 while (qemu_isgraph(*p)) {
3279 p++;
3280 }
3281 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003282 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003283 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003284 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003285 } else {
3286 monitor_printf(mon, "Expected 'on' or 'off'\n");
3287 goto fail;
3288 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003289 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003290 }
3291 break;
bellard9307c4c2004-04-04 12:57:25 +00003292 case '-':
3293 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003294 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003295 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003296 /* option */
ths3b46e622007-09-17 08:09:54 +00003297
bellard9307c4c2004-04-04 12:57:25 +00003298 c = *typestr++;
3299 if (c == '\0')
3300 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003301 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003302 p++;
bellard9307c4c2004-04-04 12:57:25 +00003303 if (*p == '-') {
3304 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003305 if(c != *p) {
3306 if(!is_valid_option(p, typestr)) {
3307
3308 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003309 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003310 goto fail;
3311 } else {
3312 skip_key = 1;
3313 }
bellard9307c4c2004-04-04 12:57:25 +00003314 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003315 if(skip_key) {
3316 p = tmp;
3317 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003318 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003319 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003320 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003321 }
bellard9307c4c2004-04-04 12:57:25 +00003322 }
bellard9307c4c2004-04-04 12:57:25 +00003323 }
3324 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003325 case 'S':
3326 {
3327 /* package all remaining string */
3328 int len;
3329
3330 while (qemu_isspace(*p)) {
3331 p++;
3332 }
3333 if (*typestr == '?') {
3334 typestr++;
3335 if (*p == '\0') {
3336 /* no remaining string: NULL argument */
3337 break;
3338 }
3339 }
3340 len = strlen(p);
3341 if (len <= 0) {
3342 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003343 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003344 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003345 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003346 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003347 p += len;
3348 }
3349 break;
bellard9307c4c2004-04-04 12:57:25 +00003350 default:
3351 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003352 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003353 goto fail;
3354 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003355 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003356 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003357 }
3358 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003359 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003360 p++;
3361 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003362 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003363 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003364 goto fail;
3365 }
3366
Bandan Dasae502122015-06-03 18:38:08 -04003367 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003368
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003369fail:
Bandan Dasae502122015-06-03 18:38:08 -04003370 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003371 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003372 return NULL;
3373}
3374
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003375static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003376{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003377 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003378 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003379
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003380 trace_handle_hmp_command(mon, cmdline);
3381
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003382 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003383 if (!cmd) {
3384 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003385 }
3386
Bandan Dasae502122015-06-03 18:38:08 -04003387 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3388 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003389 monitor_printf(mon, "Try \"help %s\" for more information\n",
3390 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003391 return;
3392 }
3393
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003394 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003395 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003396}
3397
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003398static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003399{
3400 const char *p, *pstart;
3401 char cmd[128];
3402 int len;
3403
3404 p = list;
3405 for(;;) {
3406 pstart = p;
3407 p = strchr(p, '|');
3408 if (!p)
3409 p = pstart + strlen(pstart);
3410 len = p - pstart;
3411 if (len > sizeof(cmd) - 2)
3412 len = sizeof(cmd) - 2;
3413 memcpy(cmd, pstart, len);
3414 cmd[len] = '\0';
3415 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003416 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003417 }
3418 if (*p == '\0')
3419 break;
3420 p++;
3421 }
3422}
3423
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003424static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003425{
3426 DIR *ffs;
3427 struct dirent *d;
3428 char path[1024];
3429 char file[1024], file_prefix[1024];
3430 int input_path_len;
3431 const char *p;
3432
ths5fafdf22007-09-16 21:08:06 +00003433 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003434 if (!p) {
3435 input_path_len = 0;
3436 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003437 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003438 } else {
3439 input_path_len = p - input + 1;
3440 memcpy(path, input, input_path_len);
3441 if (input_path_len > sizeof(path) - 1)
3442 input_path_len = sizeof(path) - 1;
3443 path[input_path_len] = '\0';
3444 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3445 }
Bandan Das19f2db52015-06-03 18:38:07 -04003446
bellard81d09122004-07-14 17:21:37 +00003447 ffs = opendir(path);
3448 if (!ffs)
3449 return;
3450 for(;;) {
3451 struct stat sb;
3452 d = readdir(ffs);
3453 if (!d)
3454 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003455
3456 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3457 continue;
3458 }
3459
bellard81d09122004-07-14 17:21:37 +00003460 if (strstart(d->d_name, file_prefix, NULL)) {
3461 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003462 if (input_path_len < sizeof(file))
3463 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3464 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003465 /* stat the file to find out if it's a directory.
3466 * In that case add a slash to speed up typing long paths
3467 */
David 'Digit' Turner2f1b2952016-09-13 16:58:16 +02003468 if (qemu_stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003469 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003470 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003471 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003472 }
3473 }
3474 closedir(ffs);
3475}
3476
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003477static const char *next_arg_type(const char *typestr)
3478{
3479 const char *p = strchr(typestr, ':');
3480 return (p != NULL ? ++p : typestr);
3481}
3482
Hani Benhabiles40d19392014-05-07 23:41:30 +01003483static void add_completion_option(ReadLineState *rs, const char *str,
3484 const char *option)
3485{
3486 if (!str || !option) {
3487 return;
3488 }
3489 if (!strncmp(option, str, strlen(str))) {
3490 readline_add_completion(rs, option);
3491 }
3492}
3493
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003494void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3495{
3496 size_t len;
3497 ChardevBackendInfoList *list, *start;
3498
3499 if (nb_args != 2) {
3500 return;
3501 }
3502 len = strlen(str);
3503 readline_set_completion_index(rs, len);
3504
3505 start = list = qmp_query_chardev_backends(NULL);
3506 while (list) {
3507 const char *chr_name = list->value->name;
3508
3509 if (!strncmp(chr_name, str, len)) {
3510 readline_add_completion(rs, chr_name);
3511 }
3512 list = list->next;
3513 }
3514 qapi_free_ChardevBackendInfoList(start);
3515}
3516
Hani Benhabilesb162b492014-05-07 23:41:31 +01003517void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3518{
3519 size_t len;
3520 int i;
3521
3522 if (nb_args != 2) {
3523 return;
3524 }
3525 len = strlen(str);
3526 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003527 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003528 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003529 }
3530}
3531
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003532void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003533{
3534 GSList *list, *elt;
3535 size_t len;
3536
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003537 if (nb_args != 2) {
3538 return;
3539 }
3540
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003541 len = strlen(str);
3542 readline_set_completion_index(rs, len);
3543 list = elt = object_class_get_list(TYPE_DEVICE, false);
3544 while (elt) {
3545 const char *name;
3546 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3547 TYPE_DEVICE);
3548 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003549
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003550 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003551 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003552 readline_add_completion(rs, name);
3553 }
3554 elt = elt->next;
3555 }
3556 g_slist_free(list);
3557}
3558
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003559void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003560{
3561 GSList *list, *elt;
3562 size_t len;
3563
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003564 if (nb_args != 2) {
3565 return;
3566 }
3567
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003568 len = strlen(str);
3569 readline_set_completion_index(rs, len);
3570 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3571 while (elt) {
3572 const char *name;
3573
3574 name = object_class_get_name(OBJECT_CLASS(elt->data));
3575 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3576 readline_add_completion(rs, name);
3577 }
3578 elt = elt->next;
3579 }
3580 g_slist_free(list);
3581}
3582
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003583static void peripheral_device_del_completion(ReadLineState *rs,
3584 const char *str, size_t len)
3585{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003586 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3587 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003588
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003589 list = qdev_build_hotpluggable_device_list(peripheral);
3590 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003591 return;
3592 }
3593
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003594 for (item = list; item; item = g_slist_next(item)) {
3595 DeviceState *dev = item->data;
3596
3597 if (dev->id && !strncmp(str, dev->id, len)) {
3598 readline_add_completion(rs, dev->id);
3599 }
3600 }
3601
3602 g_slist_free(list);
3603}
3604
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003605void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3606{
3607 size_t len;
3608 ChardevInfoList *list, *start;
3609
3610 if (nb_args != 2) {
3611 return;
3612 }
3613 len = strlen(str);
3614 readline_set_completion_index(rs, len);
3615
3616 start = list = qmp_query_chardev(NULL);
3617 while (list) {
3618 ChardevInfo *chr = list->value;
3619
3620 if (!strncmp(chr->label, str, len)) {
3621 readline_add_completion(rs, chr->label);
3622 }
3623 list = list->next;
3624 }
3625 qapi_free_ChardevInfoList(start);
3626}
3627
Hani Benhabiles8e597772014-05-27 23:39:30 +01003628static void ringbuf_completion(ReadLineState *rs, const char *str)
3629{
3630 size_t len;
3631 ChardevInfoList *list, *start;
3632
3633 len = strlen(str);
3634 readline_set_completion_index(rs, len);
3635
3636 start = list = qmp_query_chardev(NULL);
3637 while (list) {
3638 ChardevInfo *chr_info = list->value;
3639
3640 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003641 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003642 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003643 readline_add_completion(rs, chr_info->label);
3644 }
3645 }
3646 list = list->next;
3647 }
3648 qapi_free_ChardevInfoList(start);
3649}
3650
Hani Benhabiles8e597772014-05-27 23:39:30 +01003651void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3652{
3653 if (nb_args != 2) {
3654 return;
3655 }
3656 ringbuf_completion(rs, str);
3657}
3658
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003659void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3660{
3661 size_t len;
3662
3663 if (nb_args != 2) {
3664 return;
3665 }
3666
3667 len = strlen(str);
3668 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003669 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003670}
3671
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003672void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003673{
3674 ObjectPropertyInfoList *list, *start;
3675 size_t len;
3676
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003677 if (nb_args != 2) {
3678 return;
3679 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003680 len = strlen(str);
3681 readline_set_completion_index(rs, len);
3682
3683 start = list = qmp_qom_list("/objects", NULL);
3684 while (list) {
3685 ObjectPropertyInfo *info = list->value;
3686
3687 if (!strncmp(info->type, "child<", 5)
3688 && !strncmp(info->name, str, len)) {
3689 readline_add_completion(rs, info->name);
3690 }
3691 list = list->next;
3692 }
3693 qapi_free_ObjectPropertyInfoList(start);
3694}
3695
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003696void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3697{
3698 int i;
3699 char *sep;
3700 size_t len;
3701
3702 if (nb_args != 2) {
3703 return;
3704 }
3705 sep = strrchr(str, '-');
3706 if (sep) {
3707 str = sep + 1;
3708 }
3709 len = strlen(str);
3710 readline_set_completion_index(rs, len);
Eric Blake7fb1cf1602015-11-18 01:52:57 -07003711 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003712 if (!strncmp(str, QKeyCode_str(i), len)) {
3713 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003714 }
3715 }
3716}
3717
Hani Benhabiles40d19392014-05-07 23:41:30 +01003718void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3719{
3720 size_t len;
3721
3722 len = strlen(str);
3723 readline_set_completion_index(rs, len);
3724 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003725 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003726 int count, i;
3727 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003728 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003729 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003730 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003731 const char *name = ncs[i]->name;
3732 if (!strncmp(str, name, len)) {
3733 readline_add_completion(rs, name);
3734 }
3735 }
3736 } else if (nb_args == 3) {
3737 add_completion_option(rs, str, "on");
3738 add_completion_option(rs, str, "off");
3739 }
3740}
3741
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003742void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3743{
3744 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003745 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003746
3747 if (nb_args != 2) {
3748 return;
3749 }
3750
3751 len = strlen(str);
3752 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003753 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003754 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003755 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003756 QemuOpts *opts;
3757 const char *name = ncs[i]->name;
3758 if (strncmp(str, name, len)) {
3759 continue;
3760 }
3761 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3762 if (opts) {
3763 readline_add_completion(rs, name);
3764 }
3765 }
3766}
3767
Lluís Vilanovabd712112016-07-11 12:53:51 +02003768void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3769{
3770 size_t len;
3771
3772 len = strlen(str);
3773 readline_set_completion_index(rs, len);
3774 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003775 TraceEventIter iter;
3776 TraceEvent *ev;
3777 char *pattern = g_strdup_printf("%s*", str);
3778 trace_event_iter_init(&iter, pattern);
3779 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3780 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003781 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003782 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003783 }
3784}
3785
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003786void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3787{
3788 size_t len;
3789
3790 len = strlen(str);
3791 readline_set_completion_index(rs, len);
3792 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003793 TraceEventIter iter;
3794 TraceEvent *ev;
3795 char *pattern = g_strdup_printf("%s*", str);
3796 trace_event_iter_init(&iter, pattern);
3797 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3798 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003799 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003800 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003801 } else if (nb_args == 3) {
3802 add_completion_option(rs, str, "on");
3803 add_completion_option(rs, str, "off");
3804 }
3805}
3806
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003807void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3808{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003809 int i;
3810
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003811 if (nb_args != 2) {
3812 return;
3813 }
3814 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003815 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3816 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003817 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003818}
3819
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003820void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3821 const char *str)
3822{
3823 size_t len;
3824
3825 len = strlen(str);
3826 readline_set_completion_index(rs, len);
3827 if (nb_args == 2) {
3828 int i;
Eric Blake7fb1cf1602015-11-18 01:52:57 -07003829 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003830 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003831 if (!strncmp(str, name, len)) {
3832 readline_add_completion(rs, name);
3833 }
3834 }
3835 } else if (nb_args == 3) {
3836 add_completion_option(rs, str, "on");
3837 add_completion_option(rs, str, "off");
3838 }
3839}
3840
Liang Li50e9a622015-03-23 16:32:29 +08003841void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3842 const char *str)
3843{
3844 size_t len;
3845
3846 len = strlen(str);
3847 readline_set_completion_index(rs, len);
3848 if (nb_args == 2) {
3849 int i;
Eric Blake7fb1cf1602015-11-18 01:52:57 -07003850 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003851 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003852 if (!strncmp(str, name, len)) {
3853 readline_add_completion(rs, name);
3854 }
3855 }
3856 }
3857}
3858
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003859static void vm_completion(ReadLineState *rs, const char *str)
3860{
3861 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003862 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003863 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003864
3865 len = strlen(str);
3866 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003867
Kevin Wolf88be7b42016-05-20 18:49:07 +02003868 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003869 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003870 AioContext *ctx = bdrv_get_aio_context(bs);
3871 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003872
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003873 aio_context_acquire(ctx);
3874 if (bdrv_can_snapshot(bs)) {
3875 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3876 }
3877 aio_context_release(ctx);
3878 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003879 continue;
3880 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003881
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003882 snapshot = snapshots;
3883 while (snapshot) {
3884 char *completion = snapshot->value->name;
3885 if (!strncmp(str, completion, len)) {
3886 readline_add_completion(rs, completion);
3887 }
3888 completion = snapshot->value->id;
3889 if (!strncmp(str, completion, len)) {
3890 readline_add_completion(rs, completion);
3891 }
3892 snapshot = snapshot->next;
3893 }
3894 qapi_free_SnapshotInfoList(snapshots);
3895 }
3896
3897}
3898
3899void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3900{
3901 if (nb_args == 2) {
3902 vm_completion(rs, str);
3903 }
3904}
3905
3906void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3907{
3908 if (nb_args == 2) {
3909 vm_completion(rs, str);
3910 }
3911}
3912
Wenchao Xiac35b6402013-08-27 20:38:24 +08003913static void monitor_find_completion_by_table(Monitor *mon,
3914 const mon_cmd_t *cmd_table,
3915 char **args,
3916 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003917{
3918 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003919 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003920 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003921 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003922 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003923
bellard81d09122004-07-14 17:21:37 +00003924 if (nb_args <= 1) {
3925 /* command completion */
3926 if (nb_args == 0)
3927 cmdname = "";
3928 else
3929 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003930 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003931 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003932 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003933 }
3934 } else {
3935 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003936 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003937 if (compare_cmd(args[0], cmd->name)) {
3938 break;
3939 }
bellard81d09122004-07-14 17:21:37 +00003940 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003941 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003942 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003943 }
3944
Wenchao Xiad903a772013-08-27 20:38:25 +08003945 if (cmd->sub_table) {
3946 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003947 monitor_find_completion_by_table(mon, cmd->sub_table,
3948 &args[1], nb_args - 1);
3949 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003950 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003951 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003952 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3953 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003954 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003955
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003956 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003957 for(i = 0; i < nb_args - 2; i++) {
3958 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003959 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003960 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003961 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003962 }
3963 }
3964 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003965 old_ptype = NULL;
3966 while (*ptype == '-' && old_ptype != ptype) {
3967 old_ptype = ptype;
Jan Kiszka3b6dbf272010-06-16 00:38:34 +02003968 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003969 }
bellard81d09122004-07-14 17:21:37 +00003970 switch(*ptype) {
3971 case 'F':
3972 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003973 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003974 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003975 break;
3976 case 'B':
3977 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003978 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003979 while ((blk = blk_next(blk)) != NULL) {
3980 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003981 if (str[0] == '\0' ||
3982 !strncmp(name, str, strlen(str))) {
3983 readline_add_completion(mon->rs, name);
3984 }
3985 }
bellard81d09122004-07-14 17:21:37 +00003986 break;
bellard7fe48482004-10-09 18:08:01 +00003987 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003988 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003989 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003990 monitor_find_completion_by_table(mon, cmd_table,
3991 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003992 }
3993 break;
bellard81d09122004-07-14 17:21:37 +00003994 default:
3995 break;
3996 }
3997 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003998}
3999
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004000static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004001 const char *cmdline)
4002{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004003 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004004 char *args[MAX_ARGS];
4005 int nb_args, len;
4006
4007 /* 1. parse the cmdline */
4008 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4009 return;
4010 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004011
4012 /* if the line ends with a space, it means we want to complete the
4013 next arg */
4014 len = strlen(cmdline);
4015 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4016 if (nb_args >= MAX_ARGS) {
4017 goto cleanup;
4018 }
4019 args[nb_args++] = g_strdup("");
4020 }
4021
4022 /* 2. auto complete according to args */
4023 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004024
4025cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004026 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004027}
4028
aliguori731b0362009-03-05 23:01:42 +00004029static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004030{
aliguori731b0362009-03-05 23:01:42 +00004031 Monitor *mon = opaque;
4032
Peter Xudf152fb2018-03-09 16:59:55 +08004033 return !atomic_mb_read(&mon->suspend_cnt);
bellard9dc39cb2004-03-14 21:38:27 +00004034}
4035
Peter Xu546aa562018-03-09 16:59:54 +08004036/*
4037 * 1. This function takes ownership of rsp, err, and id.
4038 * 2. rsp, err, and id may be NULL.
4039 * 3. If err != NULL then rsp must be NULL.
4040 */
4041static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
4042 Error *err, QObject *id)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004043{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004044 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004045
Markus Armbrusterbbf10282017-02-17 21:38:24 +01004046 if (err) {
Peter Xu546aa562018-03-09 16:59:54 +08004047 assert(!rsp);
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004048 qdict = qdict_new();
4049 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
4050 error_free(err);
4051 rsp = QOBJECT(qdict);
4052 }
4053
4054 if (rsp) {
4055 if (id) {
Peter Xu546aa562018-03-09 16:59:54 +08004056 /* This is for the qdict below. */
4057 qobject_incref(id);
4058 qdict_put_obj(qobject_to(QDict, rsp), "id", id);
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004059 }
4060
4061 monitor_json_emitter(mon, rsp);
4062 }
4063
4064 qobject_decref(id);
4065 qobject_decref(rsp);
Peter Xu546aa562018-03-09 16:59:54 +08004066}
4067
Peter Xu71da4662018-03-09 16:59:57 +08004068/*
4069 * Dispatch one single QMP request. The function will free the req_obj
4070 * and objects inside it before return.
4071 */
4072static void monitor_qmp_dispatch_one(QMPRequest *req_obj)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004073{
Peter Xu71da4662018-03-09 16:59:57 +08004074 Monitor *mon, *old_mon;
4075 QObject *req, *rsp = NULL, *id;
Peter Xu71da4662018-03-09 16:59:57 +08004076 bool need_resume;
Peter Xu227a0752018-03-09 16:59:49 +08004077
Peter Xu71da4662018-03-09 16:59:57 +08004078 req = req_obj->req;
4079 mon = req_obj->mon;
4080 id = req_obj->id;
4081 need_resume = req_obj->need_resume;
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004082
Peter Xu71da4662018-03-09 16:59:57 +08004083 g_free(req_obj);
Markus Armbruster326283a2015-03-02 18:39:09 +01004084
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03004085 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
4086 QString *req_json = qobject_to_json(req);
4087 trace_handle_qmp_command(mon, qstring_get_str(req_json));
4088 QDECREF(req_json);
4089 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01004090
Peter Xu227a0752018-03-09 16:59:49 +08004091 old_mon = cur_mon;
4092 cur_mon = mon;
4093
Peter Xu71da4662018-03-09 16:59:57 +08004094 rsp = qmp_dispatch(mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004095
Peter Xu227a0752018-03-09 16:59:49 +08004096 cur_mon = old_mon;
4097
Peter Xu71da4662018-03-09 16:59:57 +08004098 /* Respond if necessary */
4099 monitor_qmp_respond(mon, rsp, NULL, id);
4100
4101 /* This pairs with the monitor_suspend() in handle_qmp_command(). */
4102 if (need_resume) {
4103 monitor_resume(mon);
4104 }
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004105
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004106 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004107}
4108
Peter Xu71da4662018-03-09 16:59:57 +08004109/*
4110 * Pop one QMP request from monitor queues, return NULL if not found.
4111 * We are using round-robin fashion to pop the request, to avoid
4112 * processing commands only on a very busy monitor. To achieve that,
4113 * when we process one request on a specific monitor, we put that
4114 * monitor to the end of mon_list queue.
4115 */
4116static QMPRequest *monitor_qmp_requests_pop_one(void)
4117{
4118 QMPRequest *req_obj = NULL;
4119 Monitor *mon;
4120
4121 qemu_mutex_lock(&monitor_lock);
4122
4123 QTAILQ_FOREACH(mon, &mon_list, entry) {
4124 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4125 req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
4126 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4127 if (req_obj) {
4128 break;
4129 }
4130 }
4131
4132 if (req_obj) {
4133 /*
4134 * We found one request on the monitor. Degrade this monitor's
4135 * priority to lowest by re-inserting it to end of queue.
4136 */
4137 QTAILQ_REMOVE(&mon_list, mon, entry);
4138 QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
4139 }
4140
4141 qemu_mutex_unlock(&monitor_lock);
4142
4143 return req_obj;
4144}
4145
4146static void monitor_qmp_bh_dispatcher(void *data)
4147{
4148 QMPRequest *req_obj = monitor_qmp_requests_pop_one();
4149
4150 if (req_obj) {
Peter Xucf869d52018-03-10 20:38:05 -06004151 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
Peter Xu71da4662018-03-09 16:59:57 +08004152 monitor_qmp_dispatch_one(req_obj);
4153 /* Reschedule instead of looping so the main loop stays responsive */
4154 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
4155 }
4156}
4157
Peter Xubf1e7302018-03-09 16:59:59 +08004158#define QMP_REQ_QUEUE_LEN_MAX (8)
4159
Peter Xu71da4662018-03-09 16:59:57 +08004160static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
4161{
4162 QObject *req, *id = NULL;
4163 QDict *qdict = NULL;
4164 MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser);
4165 Monitor *mon = container_of(mon_qmp, Monitor, qmp);
4166 Error *err = NULL;
4167 QMPRequest *req_obj;
4168
4169 req = json_parser_parse_err(tokens, NULL, &err);
4170 if (!req && !err) {
4171 /* json_parser_parse_err() sucks: can fail without setting @err */
4172 error_setg(&err, QERR_JSON_PARSING);
4173 }
4174 if (err) {
Peter Xucf869d52018-03-10 20:38:05 -06004175 goto err;
Peter Xu71da4662018-03-09 16:59:57 +08004176 }
4177
Peter Xucf869d52018-03-10 20:38:05 -06004178 /* Check against the request in general layout */
4179 qdict = qmp_dispatch_check_obj(req, &err);
4180 if (!qdict) {
4181 goto err;
4182 }
4183
4184 /* Check against OOB specific */
4185 if (!qmp_cmd_oob_check(mon, qdict, &err)) {
4186 goto err;
4187 }
4188
4189 id = qdict_get(qdict, "id");
4190
4191 /* When OOB is enabled, the "id" field is mandatory. */
4192 if (qmp_oob_enabled(mon) && !id) {
4193 error_setg(&err, "Out-Of-Band capability requires that "
4194 "every command contains an 'id' field");
4195 goto err;
4196 }
4197
4198 qobject_incref(id);
4199 qdict_del(qdict, "id");
Peter Xu71da4662018-03-09 16:59:57 +08004200
4201 req_obj = g_new0(QMPRequest, 1);
4202 req_obj->mon = mon;
4203 req_obj->id = id;
4204 req_obj->req = req;
4205 req_obj->need_resume = false;
4206
Peter Xucf869d52018-03-10 20:38:05 -06004207 if (qmp_is_oob(qdict)) {
4208 /* Out-Of-Band (OOB) requests are executed directly in parser. */
4209 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(req_obj->id)
4210 ?: "");
4211 monitor_qmp_dispatch_one(req_obj);
4212 return;
4213 }
4214
Peter Xubf1e7302018-03-09 16:59:59 +08004215 /* Protect qmp_requests and fetching its length. */
4216 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4217
Peter Xu71da4662018-03-09 16:59:57 +08004218 /*
4219 * If OOB is not enabled on the current monitor, we'll emulate the
4220 * old behavior that we won't process the current monitor any more
4221 * until it has responded. This helps make sure that as long as
4222 * OOB is not enabled, the server will never drop any command.
4223 */
4224 if (!qmp_oob_enabled(mon)) {
4225 monitor_suspend(mon);
4226 req_obj->need_resume = true;
Peter Xubf1e7302018-03-09 16:59:59 +08004227 } else {
4228 /* Drop the request if queue is full. */
4229 if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) {
4230 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4231 qapi_event_send_command_dropped(id,
4232 COMMAND_DROP_REASON_QUEUE_FULL,
4233 &error_abort);
Peter Xu6d2d5632018-03-26 14:38:55 +08004234 qmp_request_free(req_obj);
Peter Xubf1e7302018-03-09 16:59:59 +08004235 return;
4236 }
Peter Xu71da4662018-03-09 16:59:57 +08004237 }
4238
4239 /*
4240 * Put the request to the end of queue so that requests will be
4241 * handled in time order. Ownership for req_obj, req, id,
4242 * etc. will be delivered to the handler side.
4243 */
Peter Xu71da4662018-03-09 16:59:57 +08004244 g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
4245 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4246
4247 /* Kick the dispatcher routine */
4248 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
Peter Xucf869d52018-03-10 20:38:05 -06004249 return;
4250
4251err:
4252 monitor_qmp_respond(mon, NULL, err, NULL);
bellard9dc39cb2004-03-14 21:38:27 +00004253 qobject_decref(req);
bellard9dc39cb2004-03-14 21:38:27 +00004254}
4255
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004256static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004257{
Peter Xu227a0752018-03-09 16:59:49 +08004258 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004259
Peter Xu227a0752018-03-09 16:59:49 +08004260 json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004261}
4262
aliguori731b0362009-03-05 23:01:42 +00004263static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004264{
aliguori731b0362009-03-05 23:01:42 +00004265 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004266 int i;
aliguori376253e2009-03-05 23:01:23 +00004267
aliguori731b0362009-03-05 23:01:42 +00004268 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004269
aliguoricde76ee2009-03-05 23:01:51 +00004270 if (cur_mon->rs) {
4271 for (i = 0; i < size; i++)
4272 readline_handle_byte(cur_mon->rs, buf[i]);
4273 } else {
4274 if (size == 0 || buf[size - 1] != 0)
4275 monitor_printf(cur_mon, "corrupted command\n");
4276 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004277 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004278 }
aliguori731b0362009-03-05 23:01:42 +00004279
4280 cur_mon = old_mon;
4281}
aliguorid8f44602008-10-06 13:52:44 +00004282
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004283static void monitor_command_cb(void *opaque, const char *cmdline,
4284 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004285{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004286 Monitor *mon = opaque;
4287
aliguori731b0362009-03-05 23:01:42 +00004288 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004289 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004290 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004291}
4292
aliguoricde76ee2009-03-05 23:01:51 +00004293int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004294{
Peter Xue3e977d2018-03-09 16:59:56 +08004295 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004296 return -ENOTTY;
Peter Xue3e977d2018-03-09 16:59:56 +08004297 }
4298
Peter Xudf152fb2018-03-09 16:59:55 +08004299 atomic_inc(&mon->suspend_cnt);
Peter Xue3e977d2018-03-09 16:59:56 +08004300
4301 if (monitor_is_qmp(mon)) {
4302 /*
4303 * Kick iothread to make sure this takes effect. It'll be
4304 * evaluated again in prepare() of the watch object.
4305 */
4306 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4307 }
4308
4309 trace_monitor_suspend(mon, 1);
aliguoricde76ee2009-03-05 23:01:51 +00004310 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004311}
4312
aliguori376253e2009-03-05 23:01:23 +00004313void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004314{
Peter Xue3e977d2018-03-09 16:59:56 +08004315 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004316 return;
Peter Xudf152fb2018-03-09 16:59:55 +08004317 }
Peter Xue3e977d2018-03-09 16:59:56 +08004318
4319 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
4320 if (monitor_is_qmp(mon)) {
4321 /*
4322 * For QMP monitors that are running in IOThread, let's
4323 * kick the thread in case it's sleeping.
4324 */
4325 if (mon->use_io_thr) {
4326 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4327 }
4328 } else {
4329 assert(mon->rs);
4330 readline_show_prompt(mon->rs);
4331 }
4332 }
4333 trace_monitor_suspend(mon, -1);
bellard7e2515e2004-08-01 21:52:19 +00004334}
4335
Peter Xu02130312018-03-09 16:59:53 +08004336static QObject *get_qmp_greeting(Monitor *mon)
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004337{
Peter Xu02130312018-03-09 16:59:53 +08004338 QList *cap_list = qlist_new();
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004339 QObject *ver = NULL;
Peter Xu02130312018-03-09 16:59:53 +08004340 QMPCapability cap;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004341
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004342 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004343
Peter Xu02130312018-03-09 16:59:53 +08004344 for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
4345 if (!mon->use_io_thr && cap == QMP_CAPABILITY_OOB) {
4346 /* Monitors that are not using IOThread won't support OOB */
4347 continue;
4348 }
Laurent Vivier625eaca2018-03-23 15:32:01 +01004349 qlist_append_str(cap_list, QMPCapability_str(cap));
Peter Xu02130312018-03-09 16:59:53 +08004350 }
4351
4352 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': %p}}",
4353 ver, cap_list);
4354}
4355
4356static void monitor_qmp_caps_reset(Monitor *mon)
4357{
4358 memset(mon->qmp.qmp_caps, 0, sizeof(mon->qmp.qmp_caps));
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004359}
4360
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004361static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004362{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004363 QObject *data;
4364 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004365
Luiz Capitulino47116d12010-02-08 17:01:30 -02004366 switch (event) {
4367 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01004368 mon->qmp.commands = &qmp_cap_negotiation_commands;
Peter Xu02130312018-03-09 16:59:53 +08004369 monitor_qmp_caps_reset(mon);
4370 data = get_qmp_greeting(mon);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004371 monitor_json_emitter(mon, data);
4372 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004373 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004374 break;
4375 case CHR_EVENT_CLOSED:
Peter Xu6d2d5632018-03-26 14:38:55 +08004376 monitor_qmp_cleanup_queues(mon);
Markus Armbruster74358f22015-03-06 19:35:59 +01004377 json_message_parser_destroy(&mon->qmp.parser);
4378 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004379 mon_refcount--;
4380 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004381 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004382 }
4383}
4384
aliguori731b0362009-03-05 23:01:42 +00004385static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004386{
aliguori376253e2009-03-05 23:01:23 +00004387 Monitor *mon = opaque;
4388
aliguori2724b182009-03-05 23:01:47 +00004389 switch (event) {
4390 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004391 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004392 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004393 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004394 if (mon->reset_seen) {
4395 readline_restart(mon->rs);
4396 monitor_resume(mon);
4397 monitor_flush(mon);
4398 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004399 atomic_mb_set(&mon->suspend_cnt, 0);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004400 }
aliguori2724b182009-03-05 23:01:47 +00004401 break;
ths86e94de2007-01-05 22:01:59 +00004402
aliguori2724b182009-03-05 23:01:47 +00004403 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004404 if (mon->reset_seen) {
Peter Xudf152fb2018-03-09 16:59:55 +08004405 if (atomic_mb_read(&mon->suspend_cnt) == 0) {
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004406 monitor_printf(mon, "\n");
4407 }
4408 monitor_flush(mon);
4409 monitor_suspend(mon);
4410 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004411 atomic_inc(&mon->suspend_cnt);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004412 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004413 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004414 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004415 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004416 break;
4417
Amit Shahb6b8df52009-10-07 18:31:16 +05304418 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004419 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4420 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004421 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004422 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004423 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004424 }
4425 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004426 mon_refcount++;
4427 break;
4428
4429 case CHR_EVENT_CLOSED:
4430 mon_refcount--;
4431 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004432 break;
4433 }
ths86e94de2007-01-05 22:01:59 +00004434}
4435
Wayne Xia816f8922011-10-12 11:32:41 +08004436static int
4437compare_mon_cmd(const void *a, const void *b)
4438{
4439 return strcmp(((const mon_cmd_t *)a)->name,
4440 ((const mon_cmd_t *)b)->name);
4441}
4442
4443static void sortcmdlist(void)
4444{
4445 int array_num;
4446 int elem_size = sizeof(mon_cmd_t);
4447
4448 array_num = sizeof(mon_cmds)/elem_size-1;
4449 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4450
4451 array_num = sizeof(info_cmds)/elem_size-1;
4452 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4453}
4454
Peter Xua5ed3522018-03-09 16:59:52 +08004455static GMainContext *monitor_get_io_context(void)
4456{
4457 return iothread_get_g_main_context(mon_global.mon_iothread);
4458}
4459
4460static AioContext *monitor_get_aio_context(void)
4461{
4462 return iothread_get_aio_context(mon_global.mon_iothread);
4463}
4464
4465static void monitor_iothread_init(void)
4466{
4467 mon_global.mon_iothread = iothread_create("mon_iothread",
4468 &error_abort);
Peter Xu71da4662018-03-09 16:59:57 +08004469
4470 /*
4471 * This MUST be on main loop thread since we have commands that
4472 * have assumption to be run on main loop thread. It would be
4473 * nice that one day we can remove this assumption in the future.
4474 */
Peter Xu951702f2018-04-10 12:49:42 +08004475 mon_global.qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
Peter Xu71da4662018-03-09 16:59:57 +08004476 monitor_qmp_bh_dispatcher,
4477 NULL);
Peter Xuabe3cd02018-03-09 17:00:02 +08004478
4479 /*
4480 * Unlike the dispatcher BH, this must be run on the monitor IO
4481 * thread, so that monitors that are using IO thread will make
4482 * sure read/write operations are all done on the IO thread.
4483 */
4484 mon_global.qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
4485 monitor_qmp_bh_responder,
4486 NULL);
Peter Xua5ed3522018-03-09 16:59:52 +08004487}
4488
Peter Xu6adf08d2018-03-09 16:59:50 +08004489void monitor_init_globals(void)
4490{
4491 monitor_init_qmp_commands();
4492 monitor_qapi_event_init();
4493 sortcmdlist();
4494 qemu_mutex_init(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004495 monitor_iothread_init();
Peter Xu6adf08d2018-03-09 16:59:50 +08004496}
4497
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004498/* These functions just adapt the readline interface in a typesafe way. We
4499 * could cast function pointers but that discards compiler checks.
4500 */
Stefan Weild5d15072014-01-25 18:18:23 +01004501static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4502 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004503{
4504 va_list ap;
4505 va_start(ap, fmt);
4506 monitor_vprintf(opaque, fmt, ap);
4507 va_end(ap);
4508}
4509
4510static void monitor_readline_flush(void *opaque)
4511{
4512 monitor_flush(opaque);
4513}
4514
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004515/*
4516 * Print to current monitor if we have one, else to stderr.
4517 * TODO should return int, so callers can calculate width, but that
4518 * requires surgery to monitor_vprintf(). Left for another day.
4519 */
4520void error_vprintf(const char *fmt, va_list ap)
4521{
4522 if (cur_mon && !monitor_cur_is_qmp()) {
4523 monitor_vprintf(cur_mon, fmt, ap);
4524 } else {
4525 vfprintf(stderr, fmt, ap);
4526 }
4527}
4528
4529void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4530{
4531 if (cur_mon && !monitor_cur_is_qmp()) {
4532 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004533 } else if (!cur_mon) {
4534 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004535 }
4536}
4537
Peter Xua5ed3522018-03-09 16:59:52 +08004538static void monitor_list_append(Monitor *mon)
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004539{
Peter Xua5ed3522018-03-09 16:59:52 +08004540 qemu_mutex_lock(&monitor_lock);
4541 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4542 qemu_mutex_unlock(&monitor_lock);
4543}
4544
4545static void monitor_qmp_setup_handlers_bh(void *opaque)
4546{
4547 Monitor *mon = opaque;
4548 GMainContext *context;
4549
4550 if (mon->use_io_thr) {
4551 /*
4552 * When use_io_thr is set, we use the global shared dedicated
4553 * IO thread for this monitor to handle input/output.
4554 */
4555 context = monitor_get_io_context();
4556 /* We should have inited globals before reaching here. */
4557 assert(context);
4558 } else {
4559 /* The default main loop, which is the main thread */
4560 context = NULL;
4561 }
4562
4563 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4564 monitor_qmp_event, NULL, mon, context, true);
4565 monitor_list_append(mon);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004566}
4567
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004568void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004569{
Peter Xu6adf08d2018-03-09 16:59:50 +08004570 Monitor *mon = g_malloc(sizeof(*mon));
Peter Xube933ff2018-03-26 14:38:56 +08004571 bool use_readline = flags & MONITOR_USE_READLINE;
4572 bool use_oob = flags & MONITOR_USE_OOB;
ths20d8a3e2007-02-18 17:04:49 +00004573
Peter Xube933ff2018-03-26 14:38:56 +08004574 if (use_oob) {
4575 if (CHARDEV_IS_MUX(chr)) {
4576 error_report("Monitor Out-Of-Band is not supported with "
4577 "MUX typed chardev backend");
4578 exit(1);
4579 }
4580 if (use_readline) {
4581 error_report("Monitor Out-Of-band is only supported by QMP");
4582 exit(1);
4583 }
ths20d8a3e2007-02-18 17:04:49 +00004584 }
aliguori87127162009-03-05 23:01:29 +00004585
Peter Xube933ff2018-03-26 14:38:56 +08004586 monitor_data_init(mon, false, use_oob);
ths20d8a3e2007-02-18 17:04:49 +00004587
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004588 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004589 mon->flags = flags;
Peter Xube933ff2018-03-26 14:38:56 +08004590 if (use_readline) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004591 mon->rs = readline_init(monitor_readline_printf,
4592 monitor_readline_flush,
4593 mon,
4594 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004595 monitor_read_command(mon, 0);
4596 }
aliguori87127162009-03-05 23:01:29 +00004597
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004598 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004599 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004600 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Peter Xua5ed3522018-03-09 16:59:52 +08004601 if (mon->use_io_thr) {
4602 /*
4603 * Make sure the old iowatch is gone. It's possible when
4604 * e.g. the chardev is in client mode, with wait=on.
4605 */
4606 remove_fd_in_watch(chr);
4607 /*
4608 * We can't call qemu_chr_fe_set_handlers() directly here
4609 * since during the procedure the chardev will be active
4610 * and running in monitor iothread, while we'll still do
4611 * something before returning from it, which is a possible
4612 * race too. To avoid that, we just create a BH to setup
4613 * the handlers.
4614 */
4615 aio_bh_schedule_oneshot(monitor_get_aio_context(),
4616 monitor_qmp_setup_handlers_bh, mon);
4617 /* We'll add this to mon_list in the BH when setup done */
4618 return;
4619 } else {
4620 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
4621 monitor_qmp_read, monitor_qmp_event,
4622 NULL, mon, NULL, true);
4623 }
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004624 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004625 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004626 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004627 }
aliguori87127162009-03-05 23:01:29 +00004628
Peter Xua5ed3522018-03-09 16:59:52 +08004629 monitor_list_append(mon);
bellard7e2515e2004-08-01 21:52:19 +00004630}
4631
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004632void monitor_cleanup(void)
4633{
4634 Monitor *mon, *next;
4635
Peter Xua5ed3522018-03-09 16:59:52 +08004636 /*
4637 * We need to explicitly stop the iothread (but not destroy it),
4638 * cleanup the monitor resources, then destroy the iothread since
4639 * we need to unregister from chardev below in
4640 * monitor_data_destroy(), and chardev is not thread-safe yet
4641 */
4642 iothread_stop(mon_global.mon_iothread);
4643
Peter Xuabe3cd02018-03-09 17:00:02 +08004644 /*
4645 * After we have IOThread to send responses, it's possible that
4646 * when we stop the IOThread there are still replies queued in the
4647 * responder queue. Flush all of them. Note that even after this
4648 * flush it's still possible that out buffer is not flushed.
4649 * It'll be done in below monitor_flush() as the last resort.
4650 */
4651 monitor_qmp_bh_responder(NULL);
4652
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004653 qemu_mutex_lock(&monitor_lock);
Peter Xu238d9f32018-03-09 16:59:51 +08004654 QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
4655 QTAILQ_REMOVE(&mon_list, mon, entry);
Peter Xuabe3cd02018-03-09 17:00:02 +08004656 monitor_flush(mon);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004657 monitor_data_destroy(mon);
4658 g_free(mon);
4659 }
4660 qemu_mutex_unlock(&monitor_lock);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004661
Peter Xu71da4662018-03-09 16:59:57 +08004662 /* QEMUBHs needs to be deleted before destroying the IOThread. */
4663 qemu_bh_delete(mon_global.qmp_dispatcher_bh);
4664 mon_global.qmp_dispatcher_bh = NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +08004665 qemu_bh_delete(mon_global.qmp_respond_bh);
4666 mon_global.qmp_respond_bh = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004667
Peter Xua5ed3522018-03-09 16:59:52 +08004668 iothread_destroy(mon_global.mon_iothread);
4669 mon_global.mon_iothread = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004670}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004671
4672QemuOptsList qemu_mon_opts = {
4673 .name = "mon",
4674 .implied_opt_name = "chardev",
4675 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4676 .desc = {
4677 {
4678 .name = "mode",
4679 .type = QEMU_OPT_STRING,
4680 },{
4681 .name = "chardev",
4682 .type = QEMU_OPT_STRING,
4683 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004684 .name = "pretty",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004685 .type = QEMU_OPT_BOOL,
4686 },{
Peter Xube933ff2018-03-26 14:38:56 +08004687 .name = "x-oob",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004688 .type = QEMU_OPT_BOOL,
4689 },
4690 { /* end of list */ }
4691 },
4692};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004693
4694#ifndef TARGET_I386
4695void qmp_rtc_reset_reinjection(Error **errp)
4696{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004697 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004698}
Brijesh Singh08a161f2018-03-08 06:48:42 -06004699
4700SevInfo *qmp_query_sev(Error **errp)
4701{
4702 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
4703 return NULL;
4704}
Brijesh Singh1b6a0342018-03-08 06:48:56 -06004705
4706SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
4707{
4708 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
4709 return NULL;
4710}
Brijesh Singh31dd67f2018-03-08 06:48:59 -06004711
4712SevCapability *qmp_query_sev_capabilities(Error **errp)
4713{
4714 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
4715 return NULL;
4716}
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004717#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004718
4719#ifndef TARGET_S390X
4720void qmp_dump_skeys(const char *filename, Error **errp)
4721{
4722 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4723}
4724#endif
Peter Xuae50a772016-03-30 17:27:24 +01004725
4726#ifndef TARGET_ARM
4727GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4728{
4729 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4730 return NULL;
4731}
4732#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304733
4734HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4735{
4736 MachineState *ms = MACHINE(qdev_get_machine());
4737 MachineClass *mc = MACHINE_GET_CLASS(ms);
4738
Igor Mammedovc5514d02017-02-10 11:20:57 +01004739 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304740 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4741 return NULL;
4742 }
4743
Igor Mammedovc5514d02017-02-10 11:20:57 +01004744 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304745}