blob: 0fba3ccc2012edfeeefaef11465be89dc5fb7969 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020031#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020033#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020035#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000036#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040037#include "chardev/char-fe.h"
Peter Xua5ed3522018-03-09 16:59:52 +080038#include "chardev/char-io.h"
Peter Xube933ff2018-03-26 14:38:56 +080039#include "chardev/char-mux.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020040#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020041#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010042#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030043#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010044#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010045#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010046#include "ui/input.h"
Max Reitzda27a002016-03-16 19:54:29 +010047#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000048#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020049#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010052#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020054#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010055#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010056#include "qapi/qmp/qerror.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010057#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010058#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010059#include "qapi/qmp/qjson.h"
60#include "qapi/qmp/json-streamer.h"
61#include "qapi/qmp/json-parser.h"
Peter Xu02130312018-03-09 16:59:53 +080062#include "qapi/qmp/qlist.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020063#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000064#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030066#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020067#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020068#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053069#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010070#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010071#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010072#include "qemu/log.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010073#include "qemu/option.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050074#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010075#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010076#include "block/qapi.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010077#include "qapi/qapi-commands.h"
78#include "qapi/qapi-events.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010079#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020080#include "qapi/qmp-event.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010081#include "qapi/qapi-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020082#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010083#include "sysemu/cpus.h"
Peter Xua5ed3522018-03-09 16:59:52 +080084#include "sysemu/iothread.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020085#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000086
Jason J. Hernea4538a52015-06-26 14:07:21 -040087#if defined(TARGET_S390X)
88#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020089#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040090#endif
91
bellard9307c4c2004-04-04 12:57:25 +000092/*
93 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000094 *
bellard9307c4c2004-04-04 12:57:25 +000095 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000096 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000097 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080098 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010099 * 'O' option string of the form NAME=VALUE,...
100 * parsed according to QemuOptsList given by its name
101 * Example: 'device:O' uses qemu_device_opts.
102 * Restriction: only lists with empty desc are supported
103 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000104 * 'i' 32 bit integer
105 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300106 * 'M' Non-negative target long (32 or 64 bit), in user mode the
107 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200108 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200109 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
110 * K, k suffix, which multiplies the value by 2^60 for suffixes E
111 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
112 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100113 * 'T' double
114 * user mode accepts an optional ms, us, ns suffix,
115 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000116 * '/' optional gdb-like print format (like "/10x")
117 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300118 * '?' optional type (for all types, except '/')
119 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100120 * 'b' boolean
121 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300122 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000123 *
124 */
125
Anthony Liguoric227f092009-10-01 16:12:16 -0500126typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000128 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000129 const char *params;
130 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400131 void (*cmd)(Monitor *mon, const QDict *qdict);
132 /* @sub_table is a list of 2nd level of commands. If it does not exist,
133 * cmd should be used. If it exists, sub_table[?].cmd should be
134 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800135 */
136 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100137 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500138} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000139
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500141typedef struct mon_fd_t mon_fd_t;
142struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143 char *name;
144 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500145 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146};
147
Corey Bryantba1c0482012-08-14 16:43:43 -0400148/* file descriptor associated with a file descriptor set */
149typedef struct MonFdsetFd MonFdsetFd;
150struct MonFdsetFd {
151 int fd;
152 bool removed;
153 char *opaque;
154 QLIST_ENTRY(MonFdsetFd) next;
155};
156
157/* file descriptor set containing fds passed via SCM_RIGHTS */
158typedef struct MonFdset MonFdset;
159struct MonFdset {
160 int64_t id;
161 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400162 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400163 QLIST_ENTRY(MonFdset) next;
164};
165
Markus Armbruster74358f22015-03-06 19:35:59 +0100166typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200167 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100168 /*
169 * When a client connects, we're in capabilities negotiation mode.
170 * When command qmp_capabilities succeeds, we go into command
171 * mode.
172 */
Markus Armbruster635db182017-03-03 13:32:27 +0100173 QmpCommandList *commands;
Peter Xu02130312018-03-09 16:59:53 +0800174 bool qmp_caps[QMP_CAPABILITY__MAX];
Peter Xu71da4662018-03-09 16:59:57 +0800175 /*
176 * Protects qmp request/response queue. Please take monitor_lock
177 * first when used together.
178 */
179 QemuMutex qmp_queue_lock;
180 /* Input queue that holds all the parsed QMP requests */
181 GQueue *qmp_requests;
Peter Xuabe3cd02018-03-09 17:00:02 +0800182 /* Output queue contains all the QMP responses in order */
183 GQueue *qmp_responses;
Markus Armbruster74358f22015-03-06 19:35:59 +0100184} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200185
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100186/*
187 * To prevent flooding clients, events can be throttled. The
188 * throttling is calculated globally, rather than per-Monitor
189 * instance.
190 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200191typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200192 QAPIEvent event; /* Throttling state for this event type and... */
193 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100194 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200195 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200196} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100197
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200198typedef struct {
199 int64_t rate; /* Minimum time (in ns) between two events */
200} MonitorQAPIEventConf;
201
aliguori87127162009-03-05 23:01:29 +0000202struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300203 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200204 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000205 int flags;
Peter Xudf152fb2018-03-09 16:59:55 +0800206 int suspend_cnt; /* Needs to be accessed atomically */
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400207 bool skip_flush;
Peter Xua5ed3522018-03-09 16:59:52 +0800208 bool use_io_thr;
aliguori731b0362009-03-05 23:01:42 +0000209 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100210 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200211 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200212 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000213 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800214 mon_cmd_t *cmd_table;
Peter Xu238d9f32018-03-09 16:59:51 +0800215 QTAILQ_ENTRY(Monitor) entry;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800216
217 /*
218 * The per-monitor lock. We can't access guest memory when holding
219 * the lock.
220 */
221 QemuMutex mon_lock;
222
223 /*
224 * Fields that are protected by the per-monitor lock.
225 */
Peter Xu9409fc02018-06-08 11:55:06 +0800226 QLIST_HEAD(, mon_fd_t) fds;
Peter Xudc7cbcd2018-06-08 11:55:05 +0800227 QString *outbuf;
228 guint out_watch;
229 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
230 int mux_out;
aliguori87127162009-03-05 23:01:29 +0000231};
232
Peter Xua5ed3522018-03-09 16:59:52 +0800233/* Let's add monitor global variables to this struct. */
234static struct {
235 IOThread *mon_iothread;
Peter Xu71da4662018-03-09 16:59:57 +0800236 /* Bottom half to dispatch the requests received from IO thread */
237 QEMUBH *qmp_dispatcher_bh;
Peter Xuabe3cd02018-03-09 17:00:02 +0800238 /* Bottom half to deliver the responses back to clients */
239 QEMUBH *qmp_respond_bh;
Peter Xua5ed3522018-03-09 16:59:52 +0800240} mon_global;
241
Peter Xu6d2d5632018-03-26 14:38:55 +0800242struct QMPRequest {
243 /* Owner of the request */
244 Monitor *mon;
245 /* "id" field of the request */
246 QObject *id;
247 /* Request object to be handled */
248 QObject *req;
249 /*
250 * Whether we need to resume the monitor afterward. This flag is
251 * used to emulate the old QMP server behavior that the current
252 * command must be completed before execution of the next one.
253 */
254 bool need_resume;
255};
256typedef struct QMPRequest QMPRequest;
257
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300258/* QMP checker flags */
259#define QMP_ACCEPT_UNKNOWNS 1
260
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200261/* Protects mon_list, monitor_event_state. */
262static QemuMutex monitor_lock;
263
Peter Xu238d9f32018-03-09 16:59:51 +0800264static QTAILQ_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400265static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400266static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000267
Wayne Xia816f8922011-10-12 11:32:41 +0800268static mon_cmd_t mon_cmds[];
269static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000270
Markus Armbruster635db182017-03-03 13:32:27 +0100271QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100272
Markus Armbruster8631b602010-02-18 11:41:55 +0100273Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000274
Alberto Garciadc599972016-03-10 13:55:26 +0200275static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
276
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100277static void monitor_command_cb(void *opaque, const char *cmdline,
278 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000279
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100280/**
281 * Is @mon a QMP monitor?
282 */
283static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200284{
285 return (mon->flags & MONITOR_USE_CONTROL);
286}
287
Markus Armbruster489653b2015-03-06 20:01:05 +0100288/**
Peter Xue3e977d2018-03-09 16:59:56 +0800289 * Whether @mon is using readline? Note: not all HMP monitors use
290 * readline, e.g., gdbserver has a non-interactive HMP monitor, so
291 * readline is not used there.
292 */
293static inline bool monitor_uses_readline(const Monitor *mon)
294{
295 return mon->flags & MONITOR_USE_READLINE;
296}
297
298static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
299{
300 return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
301}
302
303/**
Markus Armbruster489653b2015-03-06 20:01:05 +0100304 * Is the current monitor, if any, a QMP monitor?
305 */
306bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100307{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100308 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100309}
310
Anthony Liguori7060b472011-09-02 12:34:50 -0500311void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000312{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200313 if (!mon->rs)
314 return;
315
aliguori731b0362009-03-05 23:01:42 +0000316 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
317 if (show_prompt)
318 readline_show_prompt(mon->rs);
319}
bellard6a00d602005-11-21 23:25:50 +0000320
Anthony Liguori7060b472011-09-02 12:34:50 -0500321int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
322 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000323{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100324 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000325 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
326 /* prompt is printed on return from the command handler */
327 return 0;
328 } else {
329 monitor_printf(mon, "terminal does not support password prompting\n");
330 return -ENOTTY;
331 }
aliguoribb5fc202009-03-05 23:01:15 +0000332}
333
Peter Xu6d2d5632018-03-26 14:38:55 +0800334static void qmp_request_free(QMPRequest *req)
335{
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200336 qobject_unref(req->id);
337 qobject_unref(req->req);
Peter Xu6d2d5632018-03-26 14:38:55 +0800338 g_free(req);
339}
340
341/* Must with the mon->qmp.qmp_queue_lock held */
342static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon)
343{
344 while (!g_queue_is_empty(mon->qmp.qmp_requests)) {
345 qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests));
346 }
347}
348
349/* Must with the mon->qmp.qmp_queue_lock held */
350static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon)
351{
352 while (!g_queue_is_empty(mon->qmp.qmp_responses)) {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200353 qobject_unref((QObject *)g_queue_pop_head(mon->qmp.qmp_responses));
Peter Xu6d2d5632018-03-26 14:38:55 +0800354 }
355}
356
357static void monitor_qmp_cleanup_queues(Monitor *mon)
358{
359 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
360 monitor_qmp_cleanup_req_queue_locked(mon);
361 monitor_qmp_cleanup_resp_queue_locked(mon);
362 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
363}
364
365
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200366static void monitor_flush_locked(Monitor *mon);
367
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100368static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
369 void *opaque)
370{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200371 Monitor *mon = opaque;
372
Peter Xudc7cbcd2018-06-08 11:55:05 +0800373 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200374 mon->out_watch = 0;
375 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800376 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100377 return FALSE;
378}
379
Peter Xudc7cbcd2018-06-08 11:55:05 +0800380/* Called with mon->mon_lock held. */
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200381static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000382{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100383 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400384 size_t len;
385 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100386
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400387 if (mon->skip_flush) {
388 return;
389 }
390
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400391 buf = qstring_get_str(mon->outbuf);
392 len = qstring_get_length(mon->outbuf);
393
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200394 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300395 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200396 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
397 /* all flushed or error */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200398 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400399 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100400 return;
401 }
402 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600403 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400404 QString *tmp = qstring_from_str(buf + rc);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200405 qobject_unref(mon->outbuf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400406 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100407 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200408 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300409 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300410 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300411 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200412 }
bellard7e2515e2004-08-01 21:52:19 +0000413 }
414}
415
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200416void monitor_flush(Monitor *mon)
417{
Peter Xudc7cbcd2018-06-08 11:55:05 +0800418 qemu_mutex_lock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200419 monitor_flush_locked(mon);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800420 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200421}
422
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400423/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000424static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000425{
ths60fe76f2007-12-16 03:02:09 +0000426 char c;
aliguori731b0362009-03-05 23:01:42 +0000427
Peter Xudc7cbcd2018-06-08 11:55:05 +0800428 qemu_mutex_lock(&mon->mon_lock);
bellard7e2515e2004-08-01 21:52:19 +0000429 for(;;) {
430 c = *str++;
431 if (c == '\0')
432 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400433 if (c == '\n') {
434 qstring_append_chr(mon->outbuf, '\r');
435 }
436 qstring_append_chr(mon->outbuf, c);
437 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200438 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400439 }
bellard7e2515e2004-08-01 21:52:19 +0000440 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800441 qemu_mutex_unlock(&mon->mon_lock);
bellard7e2515e2004-08-01 21:52:19 +0000442}
443
aliguori376253e2009-03-05 23:01:23 +0000444void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000445{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400446 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200447
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200448 if (!mon)
449 return;
450
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100451 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200452 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200453 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200454
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400455 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200456 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400457 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000458}
459
aliguori376253e2009-03-05 23:01:23 +0000460void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000461{
462 va_list ap;
463 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000464 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000465 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000466}
467
Pavel Butsykincaf15312015-09-22 16:18:17 +0300468int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000469{
470 va_list ap;
471 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000472 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000473 va_end(ap);
474 return 0;
475}
476
Peter Xuabe3cd02018-03-09 17:00:02 +0800477static void monitor_json_emitter_raw(Monitor *mon,
478 QObject *data)
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200479{
480 QString *json;
481
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200482 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
483 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200484 assert(json != NULL);
485
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200486 qstring_append_chr(json, '\n');
487 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200488
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200489 qobject_unref(json);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200490}
491
Peter Xuabe3cd02018-03-09 17:00:02 +0800492static void monitor_json_emitter(Monitor *mon, QObject *data)
493{
494 if (mon->use_io_thr) {
495 /*
496 * If using IO thread, we need to queue the item so that IO
497 * thread will do the rest for us. Take refcount so that
498 * caller won't free the data (which will be finally freed in
499 * responder thread).
500 */
Peter Xuabe3cd02018-03-09 17:00:02 +0800501 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200502 g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(data));
Peter Xuabe3cd02018-03-09 17:00:02 +0800503 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
504 qemu_bh_schedule(mon_global.qmp_respond_bh);
505 } else {
506 /*
507 * If not using monitor IO thread, then we are in main thread.
508 * Do the emission right away.
509 */
510 monitor_json_emitter_raw(mon, data);
511 }
512}
513
514struct QMPResponse {
515 Monitor *mon;
516 QObject *data;
517};
518typedef struct QMPResponse QMPResponse;
519
520/*
521 * Return one QMPResponse. The response is only valid if
522 * response.data is not NULL.
523 */
524static QMPResponse monitor_qmp_response_pop_one(void)
525{
526 Monitor *mon;
527 QObject *data = NULL;
528
529 qemu_mutex_lock(&monitor_lock);
530 QTAILQ_FOREACH(mon, &mon_list, entry) {
531 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
532 data = g_queue_pop_head(mon->qmp.qmp_responses);
533 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
534 if (data) {
535 break;
536 }
537 }
538 qemu_mutex_unlock(&monitor_lock);
539 return (QMPResponse) { .mon = mon, .data = data };
540}
541
542static void monitor_qmp_bh_responder(void *opaque)
543{
544 QMPResponse response;
545
546 while (true) {
547 response = monitor_qmp_response_pop_one();
548 if (!response.data) {
549 break;
550 }
551 monitor_json_emitter_raw(response.mon, response.data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200552 qobject_unref(response.data);
Peter Xuabe3cd02018-03-09 17:00:02 +0800553 }
554}
555
Eric Blake7fb1cf12015-11-18 01:52:57 -0700556static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200557 /* Limit guest-triggerable events to 1 per second */
558 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
559 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
560 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
561 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
562 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
563 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
564};
565
Markus Armbrustera24712a2015-10-15 17:08:34 +0200566GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100567
568/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200569 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200570 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100571 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200572static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100573{
574 Monitor *mon;
575
Markus Armbruster688b4b72015-10-15 17:08:30 +0200576 trace_monitor_protocol_event_emit(event, qdict);
Peter Xu238d9f32018-03-09 16:59:51 +0800577 QTAILQ_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100578 if (monitor_is_qmp(mon)
579 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200580 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100581 }
582 }
583}
584
Markus Armbrustera24712a2015-10-15 17:08:34 +0200585static void monitor_qapi_event_handler(void *opaque);
586
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100587/*
588 * Queue a new event for emission to Monitor instances,
589 * applying any rate limiting if required.
590 */
591static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200592monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100593{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200594 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200595 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200596
Eric Blake7fb1cf12015-11-18 01:52:57 -0700597 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200598 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200599 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100600
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200601 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200602
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200603 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200604 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200605 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100606 } else {
Max Reitz7dc847e2018-02-24 16:40:29 +0100607 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
Markus Armbruster7de0be62015-10-15 17:08:35 +0200608 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200609
610 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
611 assert(!evstate || timer_pending(evstate->timer));
612
613 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200614 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200615 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200616 * last send. Store event for sending when timer fires,
617 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100618 */
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200619 qobject_unref(evstate->qdict);
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200620 evstate->qdict = qobject_ref(qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100621 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200622 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200623 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200624 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200625 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200626 * events arriving before then will be delayed until then.
627 */
Alberto Garciadc599972016-03-10 13:55:26 +0200628 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200629
Markus Armbruster688b4b72015-10-15 17:08:30 +0200630 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200631
632 evstate = g_new(MonitorQAPIEventState, 1);
633 evstate->event = event;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200634 evstate->data = qobject_ref(data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200635 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200636 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200637 monitor_qapi_event_handler,
638 evstate);
639 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200640 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100641 }
642 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200643
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200644 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100645}
646
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100647/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200648 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200649 * event.
650 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100651 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200652static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100653{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200654 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200655 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100656
Markus Armbruster93f8f982015-10-15 17:08:31 +0200657 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200658 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200659
Markus Armbruster688b4b72015-10-15 17:08:30 +0200660 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200661 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200662
Markus Armbruster688b4b72015-10-15 17:08:30 +0200663 monitor_qapi_event_emit(evstate->event, evstate->qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200664 qobject_unref(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200665 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200666 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200667 } else {
668 g_hash_table_remove(monitor_qapi_event_state, evstate);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200669 qobject_unref(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200670 timer_free(evstate->timer);
671 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100672 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200673
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200674 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100675}
676
Markus Armbrustera24712a2015-10-15 17:08:34 +0200677static unsigned int qapi_event_throttle_hash(const void *key)
678{
679 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200680 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200681
Markus Armbruster7de0be62015-10-15 17:08:35 +0200682 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
683 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
684 }
685
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200686 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
687 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
688 }
689
Markus Armbruster7de0be62015-10-15 17:08:35 +0200690 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200691}
692
693static gboolean qapi_event_throttle_equal(const void *a, const void *b)
694{
695 const MonitorQAPIEventState *eva = a;
696 const MonitorQAPIEventState *evb = b;
697
Markus Armbruster7de0be62015-10-15 17:08:35 +0200698 if (eva->event != evb->event) {
699 return FALSE;
700 }
701
702 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
703 return !strcmp(qdict_get_str(eva->data, "id"),
704 qdict_get_str(evb->data, "id"));
705 }
706
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200707 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
708 return !strcmp(qdict_get_str(eva->data, "node-name"),
709 qdict_get_str(evb->data, "node-name"));
710 }
711
Markus Armbruster7de0be62015-10-15 17:08:35 +0200712 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200713}
714
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200715static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100716{
Alberto Garciadc599972016-03-10 13:55:26 +0200717 if (qtest_enabled()) {
718 event_clock_type = QEMU_CLOCK_VIRTUAL;
719 }
720
Markus Armbrustera24712a2015-10-15 17:08:34 +0200721 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
722 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200723 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200724}
725
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100726static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200727
Peter Xua5ed3522018-03-09 16:59:52 +0800728static void monitor_data_init(Monitor *mon, bool skip_flush,
729 bool use_io_thr)
Wenchao Xiab01fe892013-08-27 20:38:19 +0800730{
731 memset(mon, 0, sizeof(Monitor));
Peter Xudc7cbcd2018-06-08 11:55:05 +0800732 qemu_mutex_init(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800733 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800734 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800735 /* Use *mon_cmds by default. */
736 mon->cmd_table = mon_cmds;
Peter Xu88a95d12018-03-09 16:59:48 +0800737 mon->skip_flush = skip_flush;
Peter Xua5ed3522018-03-09 16:59:52 +0800738 mon->use_io_thr = use_io_thr;
Peter Xu71da4662018-03-09 16:59:57 +0800739 mon->qmp.qmp_requests = g_queue_new();
Peter Xuabe3cd02018-03-09 17:00:02 +0800740 mon->qmp.qmp_responses = g_queue_new();
Wenchao Xiab01fe892013-08-27 20:38:19 +0800741}
742
743static void monitor_data_destroy(Monitor *mon)
744{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200745 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400746 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400747 if (monitor_is_qmp(mon)) {
748 json_message_parser_destroy(&mon->qmp.parser);
749 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100750 readline_free(mon->rs);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200751 qobject_unref(mon->outbuf);
Peter Xudc7cbcd2018-06-08 11:55:05 +0800752 qemu_mutex_destroy(&mon->mon_lock);
Peter Xu71da4662018-03-09 16:59:57 +0800753 qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
Peter Xu6d2d5632018-03-26 14:38:55 +0800754 monitor_qmp_cleanup_req_queue_locked(mon);
755 monitor_qmp_cleanup_resp_queue_locked(mon);
Peter Xu71da4662018-03-09 16:59:57 +0800756 g_queue_free(mon->qmp.qmp_requests);
Peter Xuabe3cd02018-03-09 17:00:02 +0800757 g_queue_free(mon->qmp.qmp_responses);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800758}
759
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200760char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
761 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200762{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200763 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200764 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200765
Peter Xua5ed3522018-03-09 16:59:52 +0800766 monitor_data_init(&hmp, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200767
768 old_mon = cur_mon;
769 cur_mon = &hmp;
770
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200771 if (has_cpu_index) {
772 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200773 if (ret < 0) {
774 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100775 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
776 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200777 goto out;
778 }
779 }
780
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100781 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200782 cur_mon = old_mon;
783
Peter Xudc7cbcd2018-06-08 11:55:05 +0800784 qemu_mutex_lock(&hmp.mon_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400785 if (qstring_get_length(hmp.outbuf) > 0) {
786 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200787 } else {
788 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200789 }
Peter Xudc7cbcd2018-06-08 11:55:05 +0800790 qemu_mutex_unlock(&hmp.mon_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200791
792out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800793 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200794 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200795}
796
bellard9dc39cb2004-03-14 21:38:27 +0000797static int compare_cmd(const char *name, const char *list)
798{
799 const char *p, *pstart;
800 int len;
801 len = strlen(name);
802 p = list;
803 for(;;) {
804 pstart = p;
805 p = strchr(p, '|');
806 if (!p)
807 p = pstart + strlen(pstart);
808 if ((p - pstart) == len && !memcmp(pstart, name, len))
809 return 1;
810 if (*p == '\0')
811 break;
812 p++;
813 }
814 return 0;
815}
816
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800817static int get_str(char *buf, int buf_size, const char **pp)
818{
819 const char *p;
820 char *q;
821 int c;
822
823 q = buf;
824 p = *pp;
825 while (qemu_isspace(*p)) {
826 p++;
827 }
828 if (*p == '\0') {
829 fail:
830 *q = '\0';
831 *pp = p;
832 return -1;
833 }
834 if (*p == '\"') {
835 p++;
836 while (*p != '\0' && *p != '\"') {
837 if (*p == '\\') {
838 p++;
839 c = *p++;
840 switch (c) {
841 case 'n':
842 c = '\n';
843 break;
844 case 'r':
845 c = '\r';
846 break;
847 case '\\':
848 case '\'':
849 case '\"':
850 break;
851 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100852 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800853 goto fail;
854 }
855 if ((q - buf) < buf_size - 1) {
856 *q++ = c;
857 }
858 } else {
859 if ((q - buf) < buf_size - 1) {
860 *q++ = *p;
861 }
862 p++;
863 }
864 }
865 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100866 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800867 goto fail;
868 }
869 p++;
870 } else {
871 while (*p != '\0' && !qemu_isspace(*p)) {
872 if ((q - buf) < buf_size - 1) {
873 *q++ = *p;
874 }
875 p++;
876 }
877 }
878 *q = '\0';
879 *pp = p;
880 return 0;
881}
882
883#define MAX_ARGS 16
884
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800885static void free_cmdline_args(char **args, int nb_args)
886{
887 int i;
888
889 assert(nb_args <= MAX_ARGS);
890
891 for (i = 0; i < nb_args; i++) {
892 g_free(args[i]);
893 }
894
895}
896
897/*
898 * Parse the command line to get valid args.
899 * @cmdline: command line to be parsed.
900 * @pnb_args: location to store the number of args, must NOT be NULL.
901 * @args: location to store the args, which should be freed by caller, must
902 * NOT be NULL.
903 *
904 * Returns 0 on success, negative on failure.
905 *
906 * NOTE: this parser is an approximate form of the real command parser. Number
907 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
908 * return with failure.
909 */
910static int parse_cmdline(const char *cmdline,
911 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800912{
913 const char *p;
914 int nb_args, ret;
915 char buf[1024];
916
917 p = cmdline;
918 nb_args = 0;
919 for (;;) {
920 while (qemu_isspace(*p)) {
921 p++;
922 }
923 if (*p == '\0') {
924 break;
925 }
926 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800927 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800928 }
929 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800930 if (ret < 0) {
931 goto fail;
932 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800933 args[nb_args] = g_strdup(buf);
934 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800935 }
936 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800937 return 0;
938
939 fail:
940 free_cmdline_args(args, nb_args);
941 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800942}
943
Wenchao Xia66855492013-08-27 20:38:23 +0800944static void help_cmd_dump_one(Monitor *mon,
945 const mon_cmd_t *cmd,
946 char **prefix_args,
947 int prefix_args_nb)
948{
949 int i;
950
951 for (i = 0; i < prefix_args_nb; i++) {
952 monitor_printf(mon, "%s ", prefix_args[i]);
953 }
954 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
955}
956
957/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500958static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800959 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000960{
Anthony Liguoric227f092009-10-01 16:12:16 -0500961 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000962
Wenchao Xia66855492013-08-27 20:38:23 +0800963 /* No valid arg need to compare with, dump all in *cmds */
964 if (arg_index >= nb_args) {
965 for (cmd = cmds; cmd->name != NULL; cmd++) {
966 help_cmd_dump_one(mon, cmd, args, arg_index);
967 }
968 return;
969 }
970
971 /* Find one entry to dump */
972 for (cmd = cmds; cmd->name != NULL; cmd++) {
973 if (compare_cmd(args[arg_index], cmd->name)) {
974 if (cmd->sub_table) {
975 /* continue with next arg */
976 help_cmd_dump(mon, cmd->sub_table,
977 args, nb_args, arg_index + 1);
978 } else {
979 help_cmd_dump_one(mon, cmd, args, arg_index);
980 }
981 break;
982 }
bellard9dc39cb2004-03-14 21:38:27 +0000983 }
984}
985
aliguori376253e2009-03-05 23:01:23 +0000986static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000987{
Wenchao Xia66855492013-08-27 20:38:23 +0800988 char *args[MAX_ARGS];
989 int nb_args = 0;
990
991 /* 1. parse user input */
992 if (name) {
993 /* special case for log, directly dump and return */
994 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000995 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000996 monitor_printf(mon, "Log items (comma separated):\n");
997 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000998 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000999 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +00001000 }
Wenchao Xia66855492013-08-27 20:38:23 +08001001 return;
1002 }
1003
1004 if (parse_cmdline(name, &nb_args, args) < 0) {
1005 return;
bellardf193c792004-03-21 17:06:25 +00001006 }
bellard9dc39cb2004-03-14 21:38:27 +00001007 }
Wenchao Xia66855492013-08-27 20:38:23 +08001008
1009 /* 2. dump the contents according to parsed args */
1010 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
1011
1012 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +00001013}
1014
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001015static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -03001016{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001017 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -03001018}
1019
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001020static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301021{
1022 const char *tp_name = qdict_get_str(qdict, "name");
1023 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001024 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1025 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001026 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +00001027
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001028 if (vcpu < 0) {
1029 monitor_printf(mon, "argument vcpu must be positive");
1030 return;
1031 }
1032
1033 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +02001034 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +01001035 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +00001036 }
Prerna Saxena22890ab2010-06-24 17:04:53 +05301037}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001038
Michael Rothc45a8162011-10-02 08:44:37 -05001039#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001040static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +01001041{
1042 const char *op = qdict_get_try_str(qdict, "op");
1043 const char *arg = qdict_get_try_str(qdict, "arg");
1044
1045 if (!op) {
1046 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
1047 } else if (!strcmp(op, "on")) {
1048 st_set_trace_file_enabled(true);
1049 } else if (!strcmp(op, "off")) {
1050 st_set_trace_file_enabled(false);
1051 } else if (!strcmp(op, "flush")) {
1052 st_flush_trace_buffer();
1053 } else if (!strcmp(op, "set")) {
1054 if (arg) {
1055 st_set_trace_file(arg);
1056 }
1057 } else {
1058 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1059 help_cmd(mon, "trace-file");
1060 }
1061}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301062#endif
1063
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001064static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +00001065{
Luiz Capitulino13c74252009-10-07 13:41:55 -03001066 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +00001067}
1068
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001069static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +00001070{
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001071 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001072
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001073 if (!cmd->enabled) {
1074 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02001075 }
1076
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001077 info = g_malloc0(sizeof(*info));
1078 info->value = g_malloc0(sizeof(*info->value));
1079 info->value->name = g_strdup(cmd->name);
1080 info->next = *list;
1081 *list = info;
1082}
1083
1084CommandInfoList *qmp_query_commands(Error **errp)
1085{
1086 CommandInfoList *list = NULL;
1087
Markus Armbruster635db182017-03-03 13:32:27 +01001088 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +04001089
1090 return list;
thsa36e69d2007-12-02 05:18:19 +00001091}
1092
Daniel P. Berrange48608532012-05-21 17:59:51 +01001093EventInfoList *qmp_query_events(Error **errp)
1094{
1095 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +02001096 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +01001097
Eric Blake7fb1cf12015-11-18 01:52:57 -07001098 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001099 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +01001100 assert(event_name != NULL);
1101 info = g_malloc0(sizeof(*info));
1102 info->value = g_malloc0(sizeof(*info->value));
1103 info->value->name = g_strdup(event_name);
1104
1105 info->next = ev_list;
1106 ev_list = info;
1107 }
1108
1109 return ev_list;
1110}
1111
Markus Armbruster39a18152015-09-16 13:06:28 +02001112/*
1113 * Minor hack: generated marshalling suppressed for this command
1114 * ('gen': false in the schema) so we can parse the JSON string
1115 * directly into QObject instead of first parsing it with
1116 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1117 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +01001118 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbrusterfb0bc832018-02-26 13:48:58 -06001119 * qapi-gen.py's output actually conforms to the schema.
Markus Armbruster39a18152015-09-16 13:06:28 +02001120 */
1121static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1122 Error **errp)
1123{
Marc-André Lureau7d0f9822018-03-05 18:29:51 +01001124 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
Markus Armbruster39a18152015-09-16 13:06:28 +02001125}
1126
Marc-André Lureau5032a162016-09-12 13:19:02 +04001127/*
Marc-André Lureau5032a162016-09-12 13:19:02 +04001128 * We used to define commands in qmp-commands.hx in addition to the
1129 * QAPI schema. This permitted defining some of them only in certain
1130 * configurations. query-commands has always reflected that (good,
1131 * because it lets QMP clients figure out what's actually available),
1132 * while query-qmp-schema never did (not so good). This function is a
1133 * hack to keep the configuration-specific commands defined exactly as
1134 * before, even though qmp-commands.hx is gone.
1135 *
1136 * FIXME Educate the QAPI schema on configuration-specific commands,
1137 * and drop this hack.
1138 */
1139static void qmp_unregister_commands_hack(void)
1140{
1141#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +01001142 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001143#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +02001144#ifndef CONFIG_REPLICATION
1145 qmp_unregister_command(&qmp_commands, "xen-set-replication");
1146 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
1147 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
1148#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001149#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +01001150 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Brijesh Singh08a161f2018-03-08 06:48:42 -06001151 qmp_unregister_command(&qmp_commands, "query-sev");
Brijesh Singh1b6a0342018-03-08 06:48:56 -06001152 qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
Brijesh Singh31dd67f2018-03-08 06:48:59 -06001153 qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001154#endif
1155#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +01001156 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001157#endif
1158#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +01001159 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +04001160#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001161#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001162 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -03001163#endif
1164#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001165 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
1166 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001167#endif
1168#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1169 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001170 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001171#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001172}
1173
Peter Xu6adf08d2018-03-09 16:59:50 +08001174static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001175{
Markus Armbruster635db182017-03-03 13:32:27 +01001176 /*
1177 * Two command lists:
1178 * - qmp_commands contains all QMP commands
1179 * - qmp_cap_negotiation_commands contains just
1180 * "qmp_capabilities", to enforce capability negotiation
1181 */
1182
Markus Armbruster1527bad2017-03-03 13:32:25 +01001183 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001184
Markus Armbruster1527bad2017-03-03 13:32:25 +01001185 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001186 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001187 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001188 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001189 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001190 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001191
Markus Armbruster05875682017-03-03 13:32:24 +01001192 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001193
1194 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1195 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +02001196 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +01001197}
1198
Peter Xu71da4662018-03-09 16:59:57 +08001199static bool qmp_cap_enabled(Monitor *mon, QMPCapability cap)
1200{
1201 return mon->qmp.qmp_caps[cap];
1202}
1203
1204static bool qmp_oob_enabled(Monitor *mon)
1205{
1206 return qmp_cap_enabled(mon, QMP_CAPABILITY_OOB);
1207}
1208
Peter Xu02130312018-03-09 16:59:53 +08001209static void qmp_caps_check(Monitor *mon, QMPCapabilityList *list,
1210 Error **errp)
Markus Armbruster635db182017-03-03 13:32:27 +01001211{
Peter Xu02130312018-03-09 16:59:53 +08001212 for (; list; list = list->next) {
1213 assert(list->value < QMP_CAPABILITY__MAX);
1214 switch (list->value) {
1215 case QMP_CAPABILITY_OOB:
1216 if (!mon->use_io_thr) {
1217 /*
1218 * Out-Of-Band only works with monitors that are
1219 * running on dedicated IOThread.
1220 */
1221 error_setg(errp, "This monitor does not support "
1222 "Out-Of-Band (OOB)");
1223 return;
1224 }
1225 break;
1226 default:
1227 break;
1228 }
1229 }
1230}
1231
1232/* This function should only be called after capabilities are checked. */
1233static void qmp_caps_apply(Monitor *mon, QMPCapabilityList *list)
1234{
1235 for (; list; list = list->next) {
1236 mon->qmp.qmp_caps[list->value] = true;
1237 }
1238}
1239
Peter Xucf869d52018-03-10 20:38:05 -06001240/*
1241 * Return true if check successful, or false otherwise. When false is
1242 * returned, detailed error will be in errp if provided.
1243 */
1244static bool qmp_cmd_oob_check(Monitor *mon, QDict *req, Error **errp)
1245{
1246 const char *command;
1247 QmpCommand *cmd;
1248
1249 command = qdict_get_try_str(req, "execute");
1250 if (!command) {
1251 error_setg(errp, "Command field 'execute' missing");
1252 return false;
1253 }
1254
1255 cmd = qmp_find_command(mon->qmp.commands, command);
1256 if (!cmd) {
Peter Xu9ddb7452018-03-26 14:38:54 +08001257 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
1258 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1259 "Expecting capabilities negotiation "
1260 "with 'qmp_capabilities'");
1261 } else {
1262 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1263 "The command %s has not been found", command);
1264 }
Peter Xucf869d52018-03-10 20:38:05 -06001265 return false;
1266 }
1267
1268 if (qmp_is_oob(req)) {
1269 if (!qmp_oob_enabled(mon)) {
1270 error_setg(errp, "Please enable Out-Of-Band first "
1271 "for the session during capabilities negotiation");
1272 return false;
1273 }
1274 if (!(cmd->options & QCO_ALLOW_OOB)) {
1275 error_setg(errp, "The command %s does not support OOB",
1276 command);
1277 return false;
1278 }
1279 }
1280
1281 return true;
1282}
1283
Peter Xu02130312018-03-09 16:59:53 +08001284void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
1285 Error **errp)
1286{
1287 Error *local_err = NULL;
1288
Markus Armbruster635db182017-03-03 13:32:27 +01001289 if (cur_mon->qmp.commands == &qmp_commands) {
1290 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1291 "Capabilities negotiation is already complete, command "
1292 "ignored");
1293 return;
1294 }
1295
Peter Xu02130312018-03-09 16:59:53 +08001296 /* Enable QMP capabilities provided by the client if applicable. */
1297 if (has_enable) {
1298 qmp_caps_check(cur_mon, enable, &local_err);
1299 if (local_err) {
1300 /*
1301 * Failed check on any of the capabilities will fail the
1302 * entire command (and thus not apply any of the other
1303 * capabilities that were also requested).
1304 */
1305 error_propagate(errp, local_err);
1306 return;
1307 }
1308 qmp_caps_apply(cur_mon, enable);
1309 }
1310
Markus Armbruster635db182017-03-03 13:32:27 +01001311 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001312}
1313
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001314/* set the current CPU defined by the user */
1315int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001316{
Andreas Färber55e5c282012-12-17 06:18:02 +01001317 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001318
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001319 cpu = qemu_get_cpu(cpu_index);
1320 if (cpu == NULL) {
1321 return -1;
bellard6a00d602005-11-21 23:25:50 +00001322 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001323 g_free(cur_mon->mon_cpu_path);
1324 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001325 return 0;
bellard6a00d602005-11-21 23:25:50 +00001326}
1327
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001328static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +00001329{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001330 CPUState *cpu;
1331
1332 if (cur_mon->mon_cpu_path) {
1333 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1334 TYPE_CPU, NULL);
1335 if (!cpu) {
1336 g_free(cur_mon->mon_cpu_path);
1337 cur_mon->mon_cpu_path = NULL;
1338 }
1339 }
1340 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001341 if (!first_cpu) {
1342 return NULL;
1343 }
David Gibson27a83f82016-09-21 15:29:26 +10001344 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001345 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001346 }
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001347 if (synchronize) {
1348 cpu_synchronize_state(cpu);
1349 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001350 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001351}
1352
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001353CPUState *mon_get_cpu(void)
1354{
1355 return mon_get_cpu_sync(true);
1356}
1357
Pavel Butsykinbf957282015-09-10 18:38:59 +03001358CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001359{
Thomas Huth854e67f2017-01-13 13:12:35 +01001360 CPUState *cs = mon_get_cpu();
1361
1362 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001363}
1364
Luiz Capitulino99b77962011-10-24 10:53:44 -02001365int monitor_get_cpu_index(void)
1366{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +01001367 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +01001368
1369 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001370}
1371
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001372static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001373{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001374 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1375 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001376
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001377 if (all_cpus) {
1378 CPU_FOREACH(cs) {
1379 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1380 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1381 }
1382 } else {
1383 cs = mon_get_cpu();
1384
1385 if (!cs) {
1386 monitor_printf(mon, "No CPU available\n");
1387 return;
1388 }
1389
1390 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001391 }
bellard9307c4c2004-04-04 12:57:25 +00001392}
1393
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001394#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001395static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001396{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001397 if (!tcg_enabled()) {
1398 error_report("JIT information is only available with accel=tcg");
1399 return;
1400 }
1401
aliguori376253e2009-03-05 23:01:23 +00001402 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001403 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001404}
1405
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001406static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001407{
1408 dump_opcount_info((FILE *)mon, monitor_fprintf);
1409}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001410#endif
Max Filippov246ae242014-11-02 11:04:18 +03001411
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001412static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001413{
1414 int i;
bellard7e2515e2004-08-01 21:52:19 +00001415 const char *str;
ths3b46e622007-09-17 08:09:54 +00001416
aliguoricde76ee2009-03-05 23:01:51 +00001417 if (!mon->rs)
1418 return;
bellard7e2515e2004-08-01 21:52:19 +00001419 i = 0;
1420 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001421 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001422 if (!str)
1423 break;
aliguori376253e2009-03-05 23:01:23 +00001424 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001425 i++;
bellardaa455482004-04-04 13:07:25 +00001426 }
1427}
1428
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001429static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001430{
Thomas Huth854e67f2017-01-13 13:12:35 +01001431 CPUState *cs = mon_get_cpu();
1432
1433 if (!cs) {
1434 monitor_printf(mon, "No CPU available\n");
1435 return;
1436 }
1437 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001438}
j_mayer76a66252007-03-07 08:32:30 +00001439
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001440static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301441{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001442 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001443 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1444 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001445 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001446 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001447 Error *local_err = NULL;
1448
1449 if (name == NULL) {
1450 name = "*";
1451 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001452 if (vcpu < 0) {
1453 monitor_printf(mon, "argument vcpu must be positive");
1454 return;
1455 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001456
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001457 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001458 if (local_err) {
1459 error_report_err(local_err);
1460 return;
1461 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001462
1463 for (elem = events; elem != NULL; elem = elem->next) {
1464 monitor_printf(mon, "%s : state %u\n",
1465 elem->value->name,
1466 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1467 }
1468 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301469}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301470
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001471void qmp_client_migrate_info(const char *protocol, const char *hostname,
1472 bool has_port, int64_t port,
1473 bool has_tls_port, int64_t tls_port,
1474 bool has_cert_subject, const char *cert_subject,
1475 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001476{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001477 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001478 if (!qemu_using_spice(errp)) {
1479 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001480 }
1481
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001482 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001483 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001484 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001485 }
1486
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001487 if (qemu_spice_migrate_info(hostname,
1488 has_port ? port : -1,
1489 has_tls_port ? tls_port : -1,
1490 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001491 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001492 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001493 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001494 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001495 }
1496
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001497 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001498}
1499
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001500static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001501{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001502 Error *err = NULL;
1503
1504 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1505 if (err) {
1506 error_report_err(err);
1507 }
pbrooke735b912007-06-30 13:53:24 +00001508}
1509
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001510static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001511{
1512 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001513 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001514
bellard9307c4c2004-04-04 12:57:25 +00001515 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001516 mask = 0;
1517 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001518 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001519 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001520 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001521 return;
1522 }
1523 }
Peter Maydell24537a02013-02-11 16:41:23 +00001524 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001525}
1526
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001527static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001528{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001529 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001530 if (!option || !strcmp(option, "on")) {
1531 singlestep = 1;
1532 } else if (!strcmp(option, "off")) {
1533 singlestep = 0;
1534 } else {
1535 monitor_printf(mon, "unexpected option %s\n", option);
1536 }
1537}
1538
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001539static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001540{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001541 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001542 if (!device)
1543 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1544 if (gdbserver_start(device) < 0) {
1545 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1546 device);
1547 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001548 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001549 } else {
aliguori59030a82009-04-05 18:43:41 +00001550 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1551 device);
bellard8a7ddc32004-03-31 19:00:16 +00001552 }
1553}
1554
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001555static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001556{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001557 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001558 if (select_watchdog_action(action) == -1) {
1559 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1560 }
1561}
1562
aliguori376253e2009-03-05 23:01:23 +00001563static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001564{
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001566 switch(c) {
1567 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001568 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001569 break;
1570 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001571 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001572 break;
1573 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001574 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001575 break;
1576 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001577 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001578 break;
1579 default:
1580 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001581 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001582 } else {
aliguori376253e2009-03-05 23:01:23 +00001583 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001584 }
1585 break;
1586 }
aliguori376253e2009-03-05 23:01:23 +00001587 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001588}
1589
aliguori376253e2009-03-05 23:01:23 +00001590static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001591 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001592{
Blue Swirl23842aa2010-01-12 20:27:43 +00001593 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001594 uint8_t buf[16];
1595 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001596 CPUState *cs = mon_get_cpu();
1597
1598 if (!cs && (format == 'i' || !is_physical)) {
1599 monitor_printf(mon, "Can not dump without CPU\n");
1600 return;
1601 }
bellard9307c4c2004-04-04 12:57:25 +00001602
1603 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001604 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001605 return;
1606 }
1607
1608 len = wsize * count;
1609 if (wsize == 1)
1610 line_size = 8;
1611 else
1612 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001613 max_digits = 0;
1614
1615 switch(format) {
1616 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001617 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001618 break;
1619 default:
1620 case 'x':
1621 max_digits = (wsize * 8) / 4;
1622 break;
1623 case 'u':
1624 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001625 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001626 break;
1627 case 'c':
1628 wsize = 1;
1629 break;
1630 }
1631
1632 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001633 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001634 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001635 else
aliguori376253e2009-03-05 23:01:23 +00001636 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001637 l = len;
1638 if (l > line_size)
1639 l = line_size;
1640 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001641 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001642 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001643 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001644 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001645 break;
1646 }
bellard9307c4c2004-04-04 12:57:25 +00001647 }
ths5fafdf22007-09-16 21:08:06 +00001648 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001649 while (i < l) {
1650 switch(wsize) {
1651 default:
1652 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001653 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001654 break;
1655 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001656 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001657 break;
1658 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001659 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001660 break;
1661 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001662 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001663 break;
1664 }
aliguori376253e2009-03-05 23:01:23 +00001665 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001666 switch(format) {
1667 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001668 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001669 break;
1670 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001671 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001672 break;
1673 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001674 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001675 break;
1676 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001677 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001678 break;
1679 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001680 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001681 break;
1682 }
1683 i += wsize;
1684 }
aliguori376253e2009-03-05 23:01:23 +00001685 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001686 addr += l;
1687 len -= l;
1688 }
1689}
1690
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001691static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001692{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001693 int count = qdict_get_int(qdict, "count");
1694 int format = qdict_get_int(qdict, "format");
1695 int size = qdict_get_int(qdict, "size");
1696 target_long addr = qdict_get_int(qdict, "addr");
1697
aliguori376253e2009-03-05 23:01:23 +00001698 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001699}
1700
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001701static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001702{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001703 int count = qdict_get_int(qdict, "count");
1704 int format = qdict_get_int(qdict, "format");
1705 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001706 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001707
aliguori376253e2009-03-05 23:01:23 +00001708 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001709}
1710
Paolo Bonzinie9628442017-04-20 15:30:58 +02001711static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1712{
1713 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1714 addr, 1);
1715
1716 if (!mrs.mr) {
1717 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1718 return NULL;
1719 }
1720
1721 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1722 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1723 memory_region_unref(mrs.mr);
1724 return NULL;
1725 }
1726
1727 *p_mr = mrs.mr;
1728 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1729}
1730
1731static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1732{
1733 hwaddr addr = qdict_get_int(qdict, "addr");
1734 Error *local_err = NULL;
1735 MemoryRegion *mr = NULL;
1736 void *ptr;
1737
1738 ptr = gpa2hva(&mr, addr, &local_err);
1739 if (local_err) {
1740 error_report_err(local_err);
1741 return;
1742 }
1743
1744 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1745 " (%s) is %p\n",
1746 addr, mr->name, ptr);
1747
1748 memory_region_unref(mr);
1749}
1750
1751#ifdef CONFIG_LINUX
1752static uint64_t vtop(void *ptr, Error **errp)
1753{
1754 uint64_t pinfo;
1755 uint64_t ret = -1;
1756 uintptr_t addr = (uintptr_t) ptr;
1757 uintptr_t pagesize = getpagesize();
1758 off_t offset = addr / pagesize * sizeof(pinfo);
1759 int fd;
1760
1761 fd = open("/proc/self/pagemap", O_RDONLY);
1762 if (fd == -1) {
1763 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1764 return -1;
1765 }
1766
1767 /* Force copy-on-write if necessary. */
1768 atomic_add((uint8_t *)ptr, 0);
1769
1770 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1771 error_setg_errno(errp, errno, "Cannot read pagemap");
1772 goto out;
1773 }
1774 if ((pinfo & (1ull << 63)) == 0) {
1775 error_setg(errp, "Page not present");
1776 goto out;
1777 }
1778 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1779
1780out:
1781 close(fd);
1782 return ret;
1783}
1784
1785static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1786{
1787 hwaddr addr = qdict_get_int(qdict, "addr");
1788 Error *local_err = NULL;
1789 MemoryRegion *mr = NULL;
1790 void *ptr;
1791 uint64_t physaddr;
1792
1793 ptr = gpa2hva(&mr, addr, &local_err);
1794 if (local_err) {
1795 error_report_err(local_err);
1796 return;
1797 }
1798
1799 physaddr = vtop(ptr, &local_err);
1800 if (local_err) {
1801 error_report_err(local_err);
1802 } else {
1803 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1804 " (%s) is 0x%" PRIx64 "\n",
1805 addr, mr->name, (uint64_t) physaddr);
1806 }
1807
1808 memory_region_unref(mr);
1809}
1810#endif
1811
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001812static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001813{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001814 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001815 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001816
bellard9307c4c2004-04-04 12:57:25 +00001817 switch(format) {
1818 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001819 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001820 break;
1821 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001822 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001823 break;
1824 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001825 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001826 break;
1827 default:
1828 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001829 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001830 break;
1831 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001832 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001833 break;
1834 }
aliguori376253e2009-03-05 23:01:23 +00001835 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001836}
1837
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001838static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001839{
1840 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001841 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001842 uint32_t start = qdict_get_int(qdict, "start");
1843 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001844
1845 sum = 0;
1846 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001847 uint8_t val = address_space_ldub(&address_space_memory, addr,
1848 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001849 /* BSD sum algorithm ('sum' Unix command) */
1850 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001851 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001852 }
aliguori376253e2009-03-05 23:01:23 +00001853 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001854}
1855
bellard13224a82006-07-14 22:03:35 +00001856static int mouse_button_state;
1857
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001858static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001859{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001860 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001861 const char *dx_str = qdict_get_str(qdict, "dx_str");
1862 const char *dy_str = qdict_get_str(qdict, "dy_str");
1863 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001864
bellard13224a82006-07-14 22:03:35 +00001865 dx = strtol(dx_str, NULL, 0);
1866 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001867 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1868 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1869
1870 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001871 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001872 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001873 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001874 qemu_input_queue_btn(NULL, button, true);
1875 qemu_input_event_sync();
1876 qemu_input_queue_btn(NULL, button, false);
1877 }
1878 }
1879 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001880}
1881
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001882static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001883{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001884 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001885 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1886 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1887 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1888 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001889 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001890
1891 if (mouse_button_state == button_state) {
1892 return;
1893 }
1894 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1895 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001896 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001897}
1898
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001899static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001900{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001901 int size = qdict_get_int(qdict, "size");
1902 int addr = qdict_get_int(qdict, "addr");
1903 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001904 uint32_t val;
1905 int suffix;
1906
1907 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001908 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001909 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001910 addr++;
1911 }
1912 addr &= 0xffff;
1913
1914 switch(size) {
1915 default:
1916 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001917 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001918 suffix = 'b';
1919 break;
1920 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001921 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001922 suffix = 'w';
1923 break;
1924 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001925 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001926 suffix = 'l';
1927 break;
1928 }
aliguori376253e2009-03-05 23:01:23 +00001929 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1930 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001931}
bellarda3a91a32004-06-04 11:06:21 +00001932
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001933static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001934{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001935 int size = qdict_get_int(qdict, "size");
1936 int addr = qdict_get_int(qdict, "addr");
1937 int val = qdict_get_int(qdict, "val");
1938
Jan Kiszkaf1147842009-07-14 10:20:11 +02001939 addr &= IOPORTS_MASK;
1940
1941 switch (size) {
1942 default:
1943 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001944 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001945 break;
1946 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001947 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001948 break;
1949 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001950 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001951 break;
1952 }
1953}
1954
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001955static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001956{
Gongleif1839932014-12-03 18:20:58 +00001957 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001958 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001959
Gongleif1839932014-12-03 18:20:58 +00001960 qemu_boot_set(bootdevice, &local_err);
1961 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001962 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001963 } else {
Gongleif1839932014-12-03 18:20:58 +00001964 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001965 }
1966}
1967
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001968static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001969{
Peter Xu57bb40c2017-01-16 16:40:05 +08001970 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001971 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001972
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001973 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001974}
1975
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001976static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001977{
aliguorib28b6232009-04-22 20:20:29 +00001978 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001979 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001980 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001981
Igor Mammedovf75cd442017-05-30 18:23:59 +02001982 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001983 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1984
zhanghailiang5b009e42014-11-04 19:49:30 +08001985 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001986 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1987 for (i = 0; i < nb_numa_nodes; i++) {
1988 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001989 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1990 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1991 cpu->value->props->node_id == i) {
1992 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001993 }
1994 }
1995 monitor_printf(mon, "\n");
1996 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001997 node_mem[i].node_mem >> 20);
1998 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1999 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00002000 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02002001 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08002002 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00002003}
2004
bellard5f1ce942006-02-08 22:40:15 +00002005#ifdef CONFIG_PROFILER
2006
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002007int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002008int64_t dev_time;
2009
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002010static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002011{
aliguori376253e2009-03-05 23:01:23 +00002012 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302013 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00002014 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05302015 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11002016 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002017 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002018}
2019#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002020static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00002021{
aliguori376253e2009-03-05 23:01:23 +00002022 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002023}
2024#endif
2025
bellardec36b692006-07-16 18:57:03 +00002026/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002027static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002028
Markus Armbruster1ce6be22015-02-06 14:18:24 +01002029static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002030{
2031 int i;
2032 CaptureState *s;
2033
2034 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002035 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002036 s->ops.info (s->opaque);
2037 }
2038}
2039
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002040static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002041{
2042 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002043 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002044 CaptureState *s;
2045
2046 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2047 if (i == n) {
2048 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002049 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002050 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002051 return;
2052 }
2053 }
2054}
2055
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002056static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002057{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002058 const char *path = qdict_get_str(qdict, "path");
2059 int has_freq = qdict_haskey(qdict, "freq");
2060 int freq = qdict_get_try_int(qdict, "freq", -1);
2061 int has_bits = qdict_haskey(qdict, "bits");
2062 int bits = qdict_get_try_int(qdict, "bits", -1);
2063 int has_channels = qdict_haskey(qdict, "nchannels");
2064 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002065 CaptureState *s;
2066
Anthony Liguori7267c092011-08-20 22:09:37 -05002067 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002068
2069 freq = has_freq ? freq : 44100;
2070 bits = has_bits ? bits : 16;
2071 nchannels = has_channels ? nchannels : 2;
2072
2073 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002074 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002075 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002076 return;
bellardec36b692006-07-16 18:57:03 +00002077 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002078 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002079}
bellardec36b692006-07-16 18:57:03 +00002080
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002081static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002082{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002083 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002084
aliguori76655d62009-03-06 20:27:37 +00002085 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002086 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002087 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002088 return acl;
2089}
aliguori76655d62009-03-06 20:27:37 +00002090
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002091static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002092{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002093 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002094 qemu_acl *acl = find_acl(mon, aclname);
2095 qemu_acl_entry *entry;
2096 int i = 0;
2097
2098 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002099 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002100 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002101 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002102 i++;
2103 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002104 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002105 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002106 }
2107}
2108
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002109static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002110{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002111 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002112 qemu_acl *acl = find_acl(mon, aclname);
2113
2114 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002115 qemu_acl_reset(acl);
2116 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002117 }
2118}
aliguori76655d62009-03-06 20:27:37 +00002119
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002120static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002122 const char *aclname = qdict_get_str(qdict, "aclname");
2123 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002124 qemu_acl *acl = find_acl(mon, aclname);
2125
2126 if (acl) {
2127 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002128 acl->defaultDeny = 0;
2129 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002130 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002131 acl->defaultDeny = 1;
2132 monitor_printf(mon, "acl: policy set to 'deny'\n");
2133 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002134 monitor_printf(mon, "acl: unknown policy '%s', "
2135 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002136 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002137 }
2138}
aliguori76655d62009-03-06 20:27:37 +00002139
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002140static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002141{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002142 const char *aclname = qdict_get_str(qdict, "aclname");
2143 const char *match = qdict_get_str(qdict, "match");
2144 const char *policy = qdict_get_str(qdict, "policy");
2145 int has_index = qdict_haskey(qdict, "index");
2146 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002147 qemu_acl *acl = find_acl(mon, aclname);
2148 int deny, ret;
2149
2150 if (acl) {
2151 if (strcmp(policy, "allow") == 0) {
2152 deny = 0;
2153 } else if (strcmp(policy, "deny") == 0) {
2154 deny = 1;
2155 } else {
2156 monitor_printf(mon, "acl: unknown policy '%s', "
2157 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002158 return;
2159 }
aliguori28a76be2009-03-06 20:27:40 +00002160 if (has_index)
2161 ret = qemu_acl_insert(acl, deny, match, index);
2162 else
2163 ret = qemu_acl_append(acl, deny, match);
2164 if (ret < 0)
2165 monitor_printf(mon, "acl: unable to add acl entry\n");
2166 else
2167 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002168 }
2169}
aliguori76655d62009-03-06 20:27:37 +00002170
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01002171static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002172{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002173 const char *aclname = qdict_get_str(qdict, "aclname");
2174 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002175 qemu_acl *acl = find_acl(mon, aclname);
2176 int ret;
aliguori76655d62009-03-06 20:27:37 +00002177
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002178 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002179 ret = qemu_acl_remove(acl, match);
2180 if (ret < 0)
2181 monitor_printf(mon, "acl: no matching acl entry\n");
2182 else
2183 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002184 }
2185}
2186
Corey Bryant208c9d12012-06-22 14:36:09 -04002187void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002188{
Anthony Liguoric227f092009-10-01 16:12:16 -05002189 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08002190 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002191
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002192 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002193 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002194 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04002195 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002196 }
2197
2198 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02002199 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002200 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2201 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04002202 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002203 }
2204
Peter Xu9409fc02018-06-08 11:55:06 +08002205 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002206 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002207 if (strcmp(monfd->name, fdname) != 0) {
2208 continue;
2209 }
2210
Peter Xu9409fc02018-06-08 11:55:06 +08002211 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002212 monfd->fd = fd;
Peter Xu9409fc02018-06-08 11:55:06 +08002213 qemu_mutex_unlock(&cur_mon->mon_lock);
2214 /* Make sure close() is out of critical section */
2215 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002216 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002217 }
2218
Anthony Liguori7267c092011-08-20 22:09:37 -05002219 monfd = g_malloc0(sizeof(mon_fd_t));
2220 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002221 monfd->fd = fd;
2222
Corey Bryant208c9d12012-06-22 14:36:09 -04002223 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002224 qemu_mutex_unlock(&cur_mon->mon_lock);
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;
Peter Xu9409fc02018-06-08 11:55:06 +08002230 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002231
Peter Xu9409fc02018-06-08 11:55:06 +08002232 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04002233 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002234 if (strcmp(monfd->name, fdname) != 0) {
2235 continue;
2236 }
2237
Blue Swirl72cf2d42009-09-12 07:36:22 +00002238 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08002239 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05002240 g_free(monfd->name);
2241 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002242 qemu_mutex_unlock(&cur_mon->mon_lock);
2243 /* Make sure close() is out of critical section */
2244 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002245 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002246 }
2247
Peter Xu9409fc02018-06-08 11:55:06 +08002248 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002249 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250}
2251
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002252int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01002253{
Anthony Liguoric227f092009-10-01 16:12:16 -05002254 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002255
Peter Xu9409fc02018-06-08 11:55:06 +08002256 qemu_mutex_lock(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002257 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002258 int fd;
2259
2260 if (strcmp(monfd->name, fdname) != 0) {
2261 continue;
2262 }
2263
2264 fd = monfd->fd;
2265
2266 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002267 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002268 g_free(monfd->name);
2269 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08002270 qemu_mutex_unlock(&mon->mon_lock);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002271
2272 return fd;
2273 }
2274
Peter Xu9409fc02018-06-08 11:55:06 +08002275 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002276 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002277 return -1;
2278}
2279
Corey Bryantba1c0482012-08-14 16:43:43 -04002280static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2281{
2282 MonFdsetFd *mon_fdset_fd;
2283 MonFdsetFd *mon_fdset_fd_next;
2284
2285 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002286 if ((mon_fdset_fd->removed ||
2287 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2288 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002289 close(mon_fdset_fd->fd);
2290 g_free(mon_fdset_fd->opaque);
2291 QLIST_REMOVE(mon_fdset_fd, next);
2292 g_free(mon_fdset_fd);
2293 }
2294 }
2295
Corey Bryantadb696f2012-08-14 16:43:47 -04002296 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002297 QLIST_REMOVE(mon_fdset, next);
2298 g_free(mon_fdset);
2299 }
2300}
2301
Corey Bryantefb87c12012-08-14 16:43:48 -04002302static void monitor_fdsets_cleanup(void)
2303{
2304 MonFdset *mon_fdset;
2305 MonFdset *mon_fdset_next;
2306
2307 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2308 monitor_fdset_cleanup(mon_fdset);
2309 }
2310}
2311
Corey Bryantba1c0482012-08-14 16:43:43 -04002312AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2313 const char *opaque, Error **errp)
2314{
2315 int fd;
2316 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002317 AddfdInfo *fdinfo;
2318
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002319 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002320 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002321 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002322 goto error;
2323 }
2324
Corey Bryante446f702012-10-18 15:19:32 -04002325 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2326 has_opaque, opaque, errp);
2327 if (fdinfo) {
2328 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002329 }
2330
Corey Bryantba1c0482012-08-14 16:43:43 -04002331error:
2332 if (fd != -1) {
2333 close(fd);
2334 }
2335 return NULL;
2336}
2337
2338void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2339{
2340 MonFdset *mon_fdset;
2341 MonFdsetFd *mon_fdset_fd;
2342 char fd_str[60];
2343
2344 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2345 if (mon_fdset->id != fdset_id) {
2346 continue;
2347 }
2348 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2349 if (has_fd) {
2350 if (mon_fdset_fd->fd != fd) {
2351 continue;
2352 }
2353 mon_fdset_fd->removed = true;
2354 break;
2355 } else {
2356 mon_fdset_fd->removed = true;
2357 }
2358 }
2359 if (has_fd && !mon_fdset_fd) {
2360 goto error;
2361 }
2362 monitor_fdset_cleanup(mon_fdset);
2363 return;
2364 }
2365
2366error:
2367 if (has_fd) {
2368 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2369 fdset_id, fd);
2370 } else {
2371 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2372 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002373 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002374}
2375
2376FdsetInfoList *qmp_query_fdsets(Error **errp)
2377{
2378 MonFdset *mon_fdset;
2379 MonFdsetFd *mon_fdset_fd;
2380 FdsetInfoList *fdset_list = NULL;
2381
2382 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2383 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2384 FdsetFdInfoList *fdsetfd_list = NULL;
2385
2386 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2387 fdset_info->value->fdset_id = mon_fdset->id;
2388
2389 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2390 FdsetFdInfoList *fdsetfd_info;
2391
2392 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2393 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2394 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2395 if (mon_fdset_fd->opaque) {
2396 fdsetfd_info->value->has_opaque = true;
2397 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2398 } else {
2399 fdsetfd_info->value->has_opaque = false;
2400 }
2401
2402 fdsetfd_info->next = fdsetfd_list;
2403 fdsetfd_list = fdsetfd_info;
2404 }
2405
2406 fdset_info->value->fds = fdsetfd_list;
2407
2408 fdset_info->next = fdset_list;
2409 fdset_list = fdset_info;
2410 }
2411
2412 return fdset_list;
2413}
2414
Corey Bryante446f702012-10-18 15:19:32 -04002415AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2416 bool has_opaque, const char *opaque,
2417 Error **errp)
2418{
2419 MonFdset *mon_fdset = NULL;
2420 MonFdsetFd *mon_fdset_fd;
2421 AddfdInfo *fdinfo;
2422
2423 if (has_fdset_id) {
2424 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2425 /* Break if match found or match impossible due to ordering by ID */
2426 if (fdset_id <= mon_fdset->id) {
2427 if (fdset_id < mon_fdset->id) {
2428 mon_fdset = NULL;
2429 }
2430 break;
2431 }
2432 }
2433 }
2434
2435 if (mon_fdset == NULL) {
2436 int64_t fdset_id_prev = -1;
2437 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2438
2439 if (has_fdset_id) {
2440 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002441 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2442 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002443 return NULL;
2444 }
2445 /* Use specified fdset ID */
2446 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2447 mon_fdset_cur = mon_fdset;
2448 if (fdset_id < mon_fdset_cur->id) {
2449 break;
2450 }
2451 }
2452 } else {
2453 /* Use first available fdset ID */
2454 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2455 mon_fdset_cur = mon_fdset;
2456 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2457 fdset_id_prev = mon_fdset_cur->id;
2458 continue;
2459 }
2460 break;
2461 }
2462 }
2463
2464 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2465 if (has_fdset_id) {
2466 mon_fdset->id = fdset_id;
2467 } else {
2468 mon_fdset->id = fdset_id_prev + 1;
2469 }
2470
2471 /* The fdset list is ordered by fdset ID */
2472 if (!mon_fdset_cur) {
2473 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2474 } else if (mon_fdset->id < mon_fdset_cur->id) {
2475 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2476 } else {
2477 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2478 }
2479 }
2480
2481 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2482 mon_fdset_fd->fd = fd;
2483 mon_fdset_fd->removed = false;
2484 if (has_opaque) {
2485 mon_fdset_fd->opaque = g_strdup(opaque);
2486 }
2487 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2488
2489 fdinfo = g_malloc0(sizeof(*fdinfo));
2490 fdinfo->fdset_id = mon_fdset->id;
2491 fdinfo->fd = mon_fdset_fd->fd;
2492
2493 return fdinfo;
2494}
2495
Corey Bryantadb696f2012-08-14 16:43:47 -04002496int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2497{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002498#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002499 MonFdset *mon_fdset;
2500 MonFdsetFd *mon_fdset_fd;
2501 int mon_fd_flags;
2502
Corey Bryantadb696f2012-08-14 16:43:47 -04002503 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2504 if (mon_fdset->id != fdset_id) {
2505 continue;
2506 }
2507 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2508 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2509 if (mon_fd_flags == -1) {
2510 return -1;
2511 }
2512
2513 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2514 return mon_fdset_fd->fd;
2515 }
2516 }
2517 errno = EACCES;
2518 return -1;
2519 }
2520#endif
2521
2522 errno = ENOENT;
2523 return -1;
2524}
2525
2526int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2527{
2528 MonFdset *mon_fdset;
2529 MonFdsetFd *mon_fdset_fd_dup;
2530
2531 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2532 if (mon_fdset->id != fdset_id) {
2533 continue;
2534 }
2535 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2536 if (mon_fdset_fd_dup->fd == dup_fd) {
2537 return -1;
2538 }
2539 }
2540 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2541 mon_fdset_fd_dup->fd = dup_fd;
2542 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2543 return 0;
2544 }
2545 return -1;
2546}
2547
2548static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2549{
2550 MonFdset *mon_fdset;
2551 MonFdsetFd *mon_fdset_fd_dup;
2552
2553 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2554 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2555 if (mon_fdset_fd_dup->fd == dup_fd) {
2556 if (remove) {
2557 QLIST_REMOVE(mon_fdset_fd_dup, next);
2558 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2559 monitor_fdset_cleanup(mon_fdset);
2560 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002561 return -1;
2562 } else {
2563 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002564 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002565 }
2566 }
2567 }
2568 return -1;
2569}
2570
2571int monitor_fdset_dup_fd_find(int dup_fd)
2572{
2573 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2574}
2575
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002576void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002577{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002578 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002579}
2580
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002581int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002582{
2583 int fd;
2584 Error *local_err = NULL;
2585
2586 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002587 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002588 } else {
2589 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002590 if (fd == -1) {
2591 error_setg(&local_err, "Invalid file descriptor number '%s'",
2592 fdname);
2593 }
2594 }
2595 if (local_err) {
2596 error_propagate(errp, local_err);
2597 assert(fd == -1);
2598 } else {
2599 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002600 }
2601
2602 return fd;
2603}
2604
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002605/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002606static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002607#include "hmp-commands-info.h"
2608 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002609};
2610
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002611/* mon_cmds and info_cmds would be sorted at runtime */
2612static mon_cmd_t mon_cmds[] = {
2613#include "hmp-commands.h"
2614 { NULL, NULL, },
2615};
2616
bellard9307c4c2004-04-04 12:57:25 +00002617/*******************************************************************/
2618
2619static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002620static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002621
bellard9307c4c2004-04-04 12:57:25 +00002622
Stefan Weil9c3175c2013-08-22 21:30:09 +02002623static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2624expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002625{
Fam Zheng277acfe2013-08-20 10:58:21 +08002626 va_list ap;
2627 va_start(ap, fmt);
2628 monitor_vprintf(mon, fmt, ap);
2629 monitor_printf(mon, "\n");
2630 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002631 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002632}
2633
Markus Armbruster09b94182010-01-20 13:07:30 +01002634/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002635static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002636{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002637 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002638 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002639 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002640 uint64_t tmp = 0;
2641 int ret;
bellard92a31b12005-02-10 22:00:52 +00002642
Thomas Huth854e67f2017-01-13 13:12:35 +01002643 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002644 return -1;
2645 }
2646
2647 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002648 if (compare_cmd(name, md->name)) {
2649 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002650 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002651 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002652 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002653 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002654 switch(md->type) {
2655 case MD_I32:
2656 *pval = *(int32_t *)ptr;
2657 break;
2658 case MD_TLONG:
2659 *pval = *(target_long *)ptr;
2660 break;
2661 default:
2662 *pval = 0;
2663 break;
2664 }
bellard9307c4c2004-04-04 12:57:25 +00002665 }
2666 return 0;
2667 }
2668 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002669
Thomas Huth854e67f2017-01-13 13:12:35 +01002670 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002671 if (!ret) {
2672 *pval = (target_long) tmp;
2673 }
2674
2675 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002676}
2677
2678static void next(void)
2679{
Blue Swirl660f11b2009-07-31 21:16:51 +00002680 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002681 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002682 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002683 pch++;
2684 }
2685}
2686
aliguori376253e2009-03-05 23:01:23 +00002687static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002688
aliguori376253e2009-03-05 23:01:23 +00002689static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002690{
blueswir1c2efc952007-09-25 17:28:42 +00002691 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002692 char *p;
bellard6a00d602005-11-21 23:25:50 +00002693 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002694
2695 switch(*pch) {
2696 case '+':
2697 next();
aliguori376253e2009-03-05 23:01:23 +00002698 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002699 break;
2700 case '-':
2701 next();
aliguori376253e2009-03-05 23:01:23 +00002702 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002703 break;
2704 case '~':
2705 next();
aliguori376253e2009-03-05 23:01:23 +00002706 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002707 break;
2708 case '(':
2709 next();
aliguori376253e2009-03-05 23:01:23 +00002710 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002711 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002712 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002713 }
2714 next();
2715 break;
bellard81d09122004-07-14 17:21:37 +00002716 case '\'':
2717 pch++;
2718 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002719 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002720 n = *pch;
2721 pch++;
2722 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002723 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002724 next();
2725 break;
bellard9307c4c2004-04-04 12:57:25 +00002726 case '$':
2727 {
2728 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002729 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002730
bellard9307c4c2004-04-04 12:57:25 +00002731 pch++;
2732 q = buf;
2733 while ((*pch >= 'a' && *pch <= 'z') ||
2734 (*pch >= 'A' && *pch <= 'Z') ||
2735 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002736 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002737 if ((q - buf) < sizeof(buf) - 1)
2738 *q++ = *pch;
2739 pch++;
2740 }
blueswir1cd390082008-11-16 13:53:32 +00002741 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002742 pch++;
2743 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002744 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002745 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002746 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002747 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002748 }
2749 break;
2750 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002751 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002752 n = 0;
2753 break;
2754 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002755 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002756 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002757 if (errno == ERANGE) {
2758 expr_error(mon, "number too large");
2759 }
bellard9307c4c2004-04-04 12:57:25 +00002760 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002761 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002762 }
2763 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002764 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002765 pch++;
2766 break;
2767 }
2768 return n;
2769}
2770
2771
aliguori376253e2009-03-05 23:01:23 +00002772static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002773{
blueswir1c2efc952007-09-25 17:28:42 +00002774 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002775 int op;
ths3b46e622007-09-17 08:09:54 +00002776
aliguori376253e2009-03-05 23:01:23 +00002777 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002778 for(;;) {
2779 op = *pch;
2780 if (op != '*' && op != '/' && op != '%')
2781 break;
2782 next();
aliguori376253e2009-03-05 23:01:23 +00002783 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002784 switch(op) {
2785 default:
2786 case '*':
2787 val *= val2;
2788 break;
2789 case '/':
2790 case '%':
ths5fafdf22007-09-16 21:08:06 +00002791 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002792 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002793 if (op == '/')
2794 val /= val2;
2795 else
2796 val %= val2;
2797 break;
2798 }
2799 }
2800 return val;
2801}
2802
aliguori376253e2009-03-05 23:01:23 +00002803static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002804{
blueswir1c2efc952007-09-25 17:28:42 +00002805 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002806 int op;
bellard9307c4c2004-04-04 12:57:25 +00002807
aliguori376253e2009-03-05 23:01:23 +00002808 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002809 for(;;) {
2810 op = *pch;
2811 if (op != '&' && op != '|' && op != '^')
2812 break;
2813 next();
aliguori376253e2009-03-05 23:01:23 +00002814 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002815 switch(op) {
2816 default:
2817 case '&':
2818 val &= val2;
2819 break;
2820 case '|':
2821 val |= val2;
2822 break;
2823 case '^':
2824 val ^= val2;
2825 break;
2826 }
2827 }
2828 return val;
2829}
2830
aliguori376253e2009-03-05 23:01:23 +00002831static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002832{
blueswir1c2efc952007-09-25 17:28:42 +00002833 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002834 int op;
bellard9307c4c2004-04-04 12:57:25 +00002835
aliguori376253e2009-03-05 23:01:23 +00002836 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002837 for(;;) {
2838 op = *pch;
2839 if (op != '+' && op != '-')
2840 break;
2841 next();
aliguori376253e2009-03-05 23:01:23 +00002842 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002843 if (op == '+')
2844 val += val2;
2845 else
2846 val -= val2;
2847 }
2848 return val;
2849}
2850
aliguori376253e2009-03-05 23:01:23 +00002851static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002852{
2853 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002854 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002855 *pp = pch;
2856 return -1;
2857 }
blueswir1cd390082008-11-16 13:53:32 +00002858 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002859 pch++;
aliguori376253e2009-03-05 23:01:23 +00002860 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002861 *pp = pch;
2862 return 0;
2863}
2864
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002865static int get_double(Monitor *mon, double *pval, const char **pp)
2866{
2867 const char *p = *pp;
2868 char *tailp;
2869 double d;
2870
2871 d = strtod(p, &tailp);
2872 if (tailp == p) {
2873 monitor_printf(mon, "Number expected\n");
2874 return -1;
2875 }
2876 if (d != d || d - d != 0) {
2877 /* NaN or infinity */
2878 monitor_printf(mon, "Bad number\n");
2879 return -1;
2880 }
2881 *pval = d;
2882 *pp = tailp;
2883 return 0;
2884}
2885
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002886/*
2887 * Store the command-name in cmdname, and return a pointer to
2888 * the remaining of the command string.
2889 */
2890static const char *get_command_name(const char *cmdline,
2891 char *cmdname, size_t nlen)
2892{
2893 size_t len;
2894 const char *p, *pstart;
2895
2896 p = cmdline;
2897 while (qemu_isspace(*p))
2898 p++;
2899 if (*p == '\0')
2900 return NULL;
2901 pstart = p;
2902 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2903 p++;
2904 len = p - pstart;
2905 if (len > nlen - 1)
2906 len = nlen - 1;
2907 memcpy(cmdname, pstart, len);
2908 cmdname[len] = '\0';
2909 return p;
2910}
2911
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002912/**
2913 * Read key of 'type' into 'key' and return the current
2914 * 'type' pointer.
2915 */
2916static char *key_get_info(const char *type, char **key)
2917{
2918 size_t len;
2919 char *p, *str;
2920
2921 if (*type == ',')
2922 type++;
2923
2924 p = strchr(type, ':');
2925 if (!p) {
2926 *key = NULL;
2927 return NULL;
2928 }
2929 len = p - type;
2930
Anthony Liguori7267c092011-08-20 22:09:37 -05002931 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002932 memcpy(str, type, len);
2933 str[len] = '\0';
2934
2935 *key = str;
2936 return ++p;
2937}
2938
bellard9307c4c2004-04-04 12:57:25 +00002939static int default_fmt_format = 'x';
2940static int default_fmt_size = 4;
2941
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002942static int is_valid_option(const char *c, const char *typestr)
2943{
2944 char option[3];
2945
2946 option[0] = '-';
2947 option[1] = *c;
2948 option[2] = '\0';
2949
2950 typestr = strstr(typestr, option);
2951 return (typestr != NULL);
2952}
2953
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002954static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2955 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002956{
2957 const mon_cmd_t *cmd;
2958
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002959 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002960 if (compare_cmd(cmdname, cmd->name)) {
2961 return cmd;
2962 }
2963 }
2964
2965 return NULL;
2966}
2967
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002968/*
Bandan Dasae502122015-06-03 18:38:08 -04002969 * Parse command name from @cmdp according to command table @table.
2970 * If blank, return NULL.
2971 * Else, if no valid command can be found, report to @mon, and return
2972 * NULL.
2973 * Else, change @cmdp to point right behind the name, and return its
2974 * command table entry.
2975 * Do not assume the return value points into @table! It doesn't when
2976 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002977 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002978static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002979 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04002980 const char **cmdp,
2981 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002982{
Bandan Dasae502122015-06-03 18:38:08 -04002983 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002984 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002985 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002986
bellard9307c4c2004-04-04 12:57:25 +00002987 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002988 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002989 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002990 return NULL;
ths3b46e622007-09-17 08:09:54 +00002991
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002992 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002993 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002994 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002995 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002996 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002997 }
bellard9307c4c2004-04-04 12:57:25 +00002998
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002999 /* filter out following useless space */
3000 while (qemu_isspace(*p)) {
3001 p++;
3002 }
Bandan Dasae502122015-06-03 18:38:08 -04003003
3004 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003005 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04003006 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003007 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08003008 }
3009
Bandan Dasae502122015-06-03 18:38:08 -04003010 return cmd;
3011}
3012
3013/*
3014 * Parse arguments for @cmd.
3015 * If it can't be parsed, report to @mon, and return NULL.
3016 * Else, insert command arguments into a QDict, and return it.
3017 * Note: On success, caller has to free the QDict structure.
3018 */
3019
3020static QDict *monitor_parse_arguments(Monitor *mon,
3021 const char **endp,
3022 const mon_cmd_t *cmd)
3023{
3024 const char *typestr;
3025 char *key;
3026 int c;
3027 const char *p = *endp;
3028 char buf[1024];
3029 QDict *qdict = qdict_new();
3030
bellard9307c4c2004-04-04 12:57:25 +00003031 /* parse the parameters */
3032 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003033 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003034 typestr = key_get_info(typestr, &key);
3035 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003036 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003037 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003038 typestr++;
3039 switch(c) {
3040 case 'F':
bellard81d09122004-07-14 17:21:37 +00003041 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003042 case 's':
3043 {
3044 int ret;
ths3b46e622007-09-17 08:09:54 +00003045
blueswir1cd390082008-11-16 13:53:32 +00003046 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003047 p++;
3048 if (*typestr == '?') {
3049 typestr++;
3050 if (*p == '\0') {
3051 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003052 break;
bellard9307c4c2004-04-04 12:57:25 +00003053 }
3054 }
3055 ret = get_str(buf, sizeof(buf), &p);
3056 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003057 switch(c) {
3058 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003059 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003060 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003061 break;
3062 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003063 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003064 cmd->name);
bellard81d09122004-07-14 17:21:37 +00003065 break;
3066 default:
Bandan Dasae502122015-06-03 18:38:08 -04003067 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00003068 break;
3069 }
bellard9307c4c2004-04-04 12:57:25 +00003070 goto fail;
3071 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003072 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00003073 }
3074 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003075 case 'O':
3076 {
3077 QemuOptsList *opts_list;
3078 QemuOpts *opts;
3079
3080 opts_list = qemu_find_opts(key);
3081 if (!opts_list || opts_list->desc->name) {
3082 goto bad_type;
3083 }
3084 while (qemu_isspace(*p)) {
3085 p++;
3086 }
3087 if (!*p)
3088 break;
3089 if (get_str(buf, sizeof(buf), &p) < 0) {
3090 goto fail;
3091 }
Markus Armbruster70b94332015-02-13 12:50:26 +01003092 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01003093 if (!opts) {
3094 goto fail;
3095 }
3096 qemu_opts_to_qdict(opts, qdict);
3097 qemu_opts_del(opts);
3098 }
3099 break;
bellard9307c4c2004-04-04 12:57:25 +00003100 case '/':
3101 {
3102 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003103
blueswir1cd390082008-11-16 13:53:32 +00003104 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003105 p++;
3106 if (*p == '/') {
3107 /* format found */
3108 p++;
3109 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003110 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003111 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003112 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003113 count = count * 10 + (*p - '0');
3114 p++;
3115 }
3116 }
3117 size = -1;
3118 format = -1;
3119 for(;;) {
3120 switch(*p) {
3121 case 'o':
3122 case 'd':
3123 case 'u':
3124 case 'x':
3125 case 'i':
3126 case 'c':
3127 format = *p++;
3128 break;
3129 case 'b':
3130 size = 1;
3131 p++;
3132 break;
3133 case 'h':
3134 size = 2;
3135 p++;
3136 break;
3137 case 'w':
3138 size = 4;
3139 p++;
3140 break;
3141 case 'g':
3142 case 'L':
3143 size = 8;
3144 p++;
3145 break;
3146 default:
3147 goto next;
3148 }
3149 }
3150 next:
blueswir1cd390082008-11-16 13:53:32 +00003151 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003152 monitor_printf(mon, "invalid char in format: '%c'\n",
3153 *p);
bellard9307c4c2004-04-04 12:57:25 +00003154 goto fail;
3155 }
bellard9307c4c2004-04-04 12:57:25 +00003156 if (format < 0)
3157 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003158 if (format != 'i') {
3159 /* for 'i', not specifying a size gives -1 as size */
3160 if (size < 0)
3161 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003162 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003163 }
bellard9307c4c2004-04-04 12:57:25 +00003164 default_fmt_format = format;
3165 } else {
3166 count = 1;
3167 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003168 if (format != 'i') {
3169 size = default_fmt_size;
3170 } else {
3171 size = -1;
3172 }
bellard9307c4c2004-04-04 12:57:25 +00003173 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003174 qdict_put_int(qdict, "count", count);
3175 qdict_put_int(qdict, "format", format);
3176 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00003177 }
3178 break;
3179 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003180 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003181 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003182 {
blueswir1c2efc952007-09-25 17:28:42 +00003183 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003184
blueswir1cd390082008-11-16 13:53:32 +00003185 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003186 p++;
bellard34405572004-06-08 00:55:58 +00003187 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003188 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003189 if (*p == '\0') {
3190 typestr++;
3191 break;
3192 }
bellard34405572004-06-08 00:55:58 +00003193 } else {
3194 if (*p == '.') {
3195 p++;
blueswir1cd390082008-11-16 13:53:32 +00003196 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003197 p++;
bellard34405572004-06-08 00:55:58 +00003198 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003199 typestr++;
3200 break;
bellard34405572004-06-08 00:55:58 +00003201 }
3202 }
bellard13224a82006-07-14 22:03:35 +00003203 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003204 }
aliguori376253e2009-03-05 23:01:23 +00003205 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003206 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003207 /* Check if 'i' is greater than 32-bit */
3208 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04003209 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003210 monitor_printf(mon, "integer is for 32-bit values\n");
3211 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003212 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003213 if (val < 0) {
3214 monitor_printf(mon, "enter a positive value\n");
3215 goto fail;
3216 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003217 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003218 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003219 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00003220 }
3221 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003222 case 'o':
3223 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003224 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003225 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003226 char *end;
3227
3228 while (qemu_isspace(*p)) {
3229 p++;
3230 }
3231 if (*typestr == '?') {
3232 typestr++;
3233 if (*p == '\0') {
3234 break;
3235 }
3236 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01003237 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01003238 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02003239 monitor_printf(mon, "invalid size\n");
3240 goto fail;
3241 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003242 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02003243 p = end;
3244 }
3245 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003246 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003247 {
3248 double val;
3249
3250 while (qemu_isspace(*p))
3251 p++;
3252 if (*typestr == '?') {
3253 typestr++;
3254 if (*p == '\0') {
3255 break;
3256 }
3257 }
3258 if (get_double(mon, &val, &p) < 0) {
3259 goto fail;
3260 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003261 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003262 switch (*p) {
3263 case 'm':
3264 val /= 1e3; p += 2; break;
3265 case 'u':
3266 val /= 1e6; p += 2; break;
3267 case 'n':
3268 val /= 1e9; p += 2; break;
3269 }
3270 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003271 if (*p && !qemu_isspace(*p)) {
3272 monitor_printf(mon, "Unknown unit suffix\n");
3273 goto fail;
3274 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04003275 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003276 }
3277 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003278 case 'b':
3279 {
3280 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003281 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003282
3283 while (qemu_isspace(*p)) {
3284 p++;
3285 }
3286 beg = p;
3287 while (qemu_isgraph(*p)) {
3288 p++;
3289 }
3290 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003291 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003292 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003293 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003294 } else {
3295 monitor_printf(mon, "Expected 'on' or 'off'\n");
3296 goto fail;
3297 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003298 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003299 }
3300 break;
bellard9307c4c2004-04-04 12:57:25 +00003301 case '-':
3302 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003303 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003304 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003305 /* option */
ths3b46e622007-09-17 08:09:54 +00003306
bellard9307c4c2004-04-04 12:57:25 +00003307 c = *typestr++;
3308 if (c == '\0')
3309 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003310 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003311 p++;
bellard9307c4c2004-04-04 12:57:25 +00003312 if (*p == '-') {
3313 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003314 if(c != *p) {
3315 if(!is_valid_option(p, typestr)) {
3316
3317 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003318 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003319 goto fail;
3320 } else {
3321 skip_key = 1;
3322 }
bellard9307c4c2004-04-04 12:57:25 +00003323 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003324 if(skip_key) {
3325 p = tmp;
3326 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003327 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003328 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003329 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003330 }
bellard9307c4c2004-04-04 12:57:25 +00003331 }
bellard9307c4c2004-04-04 12:57:25 +00003332 }
3333 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003334 case 'S':
3335 {
3336 /* package all remaining string */
3337 int len;
3338
3339 while (qemu_isspace(*p)) {
3340 p++;
3341 }
3342 if (*typestr == '?') {
3343 typestr++;
3344 if (*p == '\0') {
3345 /* no remaining string: NULL argument */
3346 break;
3347 }
3348 }
3349 len = strlen(p);
3350 if (len <= 0) {
3351 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003352 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003353 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003354 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003355 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003356 p += len;
3357 }
3358 break;
bellard9307c4c2004-04-04 12:57:25 +00003359 default:
3360 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003361 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003362 goto fail;
3363 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003364 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003365 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003366 }
3367 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003368 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003369 p++;
3370 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003371 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003372 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003373 goto fail;
3374 }
3375
Bandan Dasae502122015-06-03 18:38:08 -04003376 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003377
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003378fail:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003379 qobject_unref(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003380 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003381 return NULL;
3382}
3383
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003384static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003385{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003386 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003387 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003388
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003389 trace_handle_hmp_command(mon, cmdline);
3390
Igor Mammedov71dc5782018-05-04 10:37:42 +02003391 if (runstate_check(RUN_STATE_PRECONFIG)) {
3392 monitor_printf(mon, "HMP not available in preconfig state, "
3393 "use QMP instead\n");
3394 return;
3395 }
3396
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003397 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003398 if (!cmd) {
3399 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003400 }
3401
Bandan Dasae502122015-06-03 18:38:08 -04003402 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3403 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003404 monitor_printf(mon, "Try \"help %s\" for more information\n",
3405 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003406 return;
3407 }
3408
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003409 cmd->cmd(mon, qdict);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02003410 qobject_unref(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003411}
3412
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003413static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003414{
3415 const char *p, *pstart;
3416 char cmd[128];
3417 int len;
3418
3419 p = list;
3420 for(;;) {
3421 pstart = p;
3422 p = strchr(p, '|');
3423 if (!p)
3424 p = pstart + strlen(pstart);
3425 len = p - pstart;
3426 if (len > sizeof(cmd) - 2)
3427 len = sizeof(cmd) - 2;
3428 memcpy(cmd, pstart, len);
3429 cmd[len] = '\0';
3430 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003431 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003432 }
3433 if (*p == '\0')
3434 break;
3435 p++;
3436 }
3437}
3438
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003439static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003440{
3441 DIR *ffs;
3442 struct dirent *d;
3443 char path[1024];
3444 char file[1024], file_prefix[1024];
3445 int input_path_len;
3446 const char *p;
3447
ths5fafdf22007-09-16 21:08:06 +00003448 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003449 if (!p) {
3450 input_path_len = 0;
3451 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003452 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003453 } else {
3454 input_path_len = p - input + 1;
3455 memcpy(path, input, input_path_len);
3456 if (input_path_len > sizeof(path) - 1)
3457 input_path_len = sizeof(path) - 1;
3458 path[input_path_len] = '\0';
3459 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3460 }
Bandan Das19f2db52015-06-03 18:38:07 -04003461
bellard81d09122004-07-14 17:21:37 +00003462 ffs = opendir(path);
3463 if (!ffs)
3464 return;
3465 for(;;) {
3466 struct stat sb;
3467 d = readdir(ffs);
3468 if (!d)
3469 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003470
3471 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3472 continue;
3473 }
3474
bellard81d09122004-07-14 17:21:37 +00003475 if (strstart(d->d_name, file_prefix, NULL)) {
3476 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003477 if (input_path_len < sizeof(file))
3478 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3479 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003480 /* stat the file to find out if it's a directory.
3481 * In that case add a slash to speed up typing long paths
3482 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003483 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003484 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003485 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003486 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003487 }
3488 }
3489 closedir(ffs);
3490}
3491
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003492static const char *next_arg_type(const char *typestr)
3493{
3494 const char *p = strchr(typestr, ':');
3495 return (p != NULL ? ++p : typestr);
3496}
3497
Hani Benhabiles40d19392014-05-07 23:41:30 +01003498static void add_completion_option(ReadLineState *rs, const char *str,
3499 const char *option)
3500{
3501 if (!str || !option) {
3502 return;
3503 }
3504 if (!strncmp(option, str, strlen(str))) {
3505 readline_add_completion(rs, option);
3506 }
3507}
3508
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003509void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3510{
3511 size_t len;
3512 ChardevBackendInfoList *list, *start;
3513
3514 if (nb_args != 2) {
3515 return;
3516 }
3517 len = strlen(str);
3518 readline_set_completion_index(rs, len);
3519
3520 start = list = qmp_query_chardev_backends(NULL);
3521 while (list) {
3522 const char *chr_name = list->value->name;
3523
3524 if (!strncmp(chr_name, str, len)) {
3525 readline_add_completion(rs, chr_name);
3526 }
3527 list = list->next;
3528 }
3529 qapi_free_ChardevBackendInfoList(start);
3530}
3531
Hani Benhabilesb162b492014-05-07 23:41:31 +01003532void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3533{
3534 size_t len;
3535 int i;
3536
3537 if (nb_args != 2) {
3538 return;
3539 }
3540 len = strlen(str);
3541 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003542 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003543 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003544 }
3545}
3546
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003547void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003548{
3549 GSList *list, *elt;
3550 size_t len;
3551
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003552 if (nb_args != 2) {
3553 return;
3554 }
3555
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003556 len = strlen(str);
3557 readline_set_completion_index(rs, len);
3558 list = elt = object_class_get_list(TYPE_DEVICE, false);
3559 while (elt) {
3560 const char *name;
3561 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3562 TYPE_DEVICE);
3563 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003564
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003565 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003566 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003567 readline_add_completion(rs, name);
3568 }
3569 elt = elt->next;
3570 }
3571 g_slist_free(list);
3572}
3573
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003574void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003575{
3576 GSList *list, *elt;
3577 size_t len;
3578
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003579 if (nb_args != 2) {
3580 return;
3581 }
3582
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003583 len = strlen(str);
3584 readline_set_completion_index(rs, len);
3585 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3586 while (elt) {
3587 const char *name;
3588
3589 name = object_class_get_name(OBJECT_CLASS(elt->data));
3590 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3591 readline_add_completion(rs, name);
3592 }
3593 elt = elt->next;
3594 }
3595 g_slist_free(list);
3596}
3597
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003598static void peripheral_device_del_completion(ReadLineState *rs,
3599 const char *str, size_t len)
3600{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003601 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3602 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003603
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003604 list = qdev_build_hotpluggable_device_list(peripheral);
3605 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003606 return;
3607 }
3608
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003609 for (item = list; item; item = g_slist_next(item)) {
3610 DeviceState *dev = item->data;
3611
3612 if (dev->id && !strncmp(str, dev->id, len)) {
3613 readline_add_completion(rs, dev->id);
3614 }
3615 }
3616
3617 g_slist_free(list);
3618}
3619
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003620void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3621{
3622 size_t len;
3623 ChardevInfoList *list, *start;
3624
3625 if (nb_args != 2) {
3626 return;
3627 }
3628 len = strlen(str);
3629 readline_set_completion_index(rs, len);
3630
3631 start = list = qmp_query_chardev(NULL);
3632 while (list) {
3633 ChardevInfo *chr = list->value;
3634
3635 if (!strncmp(chr->label, str, len)) {
3636 readline_add_completion(rs, chr->label);
3637 }
3638 list = list->next;
3639 }
3640 qapi_free_ChardevInfoList(start);
3641}
3642
Hani Benhabiles8e597772014-05-27 23:39:30 +01003643static void ringbuf_completion(ReadLineState *rs, const char *str)
3644{
3645 size_t len;
3646 ChardevInfoList *list, *start;
3647
3648 len = strlen(str);
3649 readline_set_completion_index(rs, len);
3650
3651 start = list = qmp_query_chardev(NULL);
3652 while (list) {
3653 ChardevInfo *chr_info = list->value;
3654
3655 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003656 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003657 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003658 readline_add_completion(rs, chr_info->label);
3659 }
3660 }
3661 list = list->next;
3662 }
3663 qapi_free_ChardevInfoList(start);
3664}
3665
Hani Benhabiles8e597772014-05-27 23:39:30 +01003666void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3667{
3668 if (nb_args != 2) {
3669 return;
3670 }
3671 ringbuf_completion(rs, str);
3672}
3673
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003674void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3675{
3676 size_t len;
3677
3678 if (nb_args != 2) {
3679 return;
3680 }
3681
3682 len = strlen(str);
3683 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003684 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003685}
3686
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003687void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003688{
3689 ObjectPropertyInfoList *list, *start;
3690 size_t len;
3691
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003692 if (nb_args != 2) {
3693 return;
3694 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003695 len = strlen(str);
3696 readline_set_completion_index(rs, len);
3697
3698 start = list = qmp_qom_list("/objects", NULL);
3699 while (list) {
3700 ObjectPropertyInfo *info = list->value;
3701
3702 if (!strncmp(info->type, "child<", 5)
3703 && !strncmp(info->name, str, len)) {
3704 readline_add_completion(rs, info->name);
3705 }
3706 list = list->next;
3707 }
3708 qapi_free_ObjectPropertyInfoList(start);
3709}
3710
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003711void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3712{
3713 int i;
3714 char *sep;
3715 size_t len;
3716
3717 if (nb_args != 2) {
3718 return;
3719 }
3720 sep = strrchr(str, '-');
3721 if (sep) {
3722 str = sep + 1;
3723 }
3724 len = strlen(str);
3725 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003726 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003727 if (!strncmp(str, QKeyCode_str(i), len)) {
3728 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003729 }
3730 }
3731}
3732
Hani Benhabiles40d19392014-05-07 23:41:30 +01003733void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3734{
3735 size_t len;
3736
3737 len = strlen(str);
3738 readline_set_completion_index(rs, len);
3739 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003740 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003741 int count, i;
3742 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003743 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003744 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003745 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003746 const char *name = ncs[i]->name;
3747 if (!strncmp(str, name, len)) {
3748 readline_add_completion(rs, name);
3749 }
3750 }
3751 } else if (nb_args == 3) {
3752 add_completion_option(rs, str, "on");
3753 add_completion_option(rs, str, "off");
3754 }
3755}
3756
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003757void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3758{
3759 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003760 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003761
3762 if (nb_args != 2) {
3763 return;
3764 }
3765
3766 len = strlen(str);
3767 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003768 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003769 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003770 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003771 QemuOpts *opts;
3772 const char *name = ncs[i]->name;
3773 if (strncmp(str, name, len)) {
3774 continue;
3775 }
3776 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3777 if (opts) {
3778 readline_add_completion(rs, name);
3779 }
3780 }
3781}
3782
Lluís Vilanovabd712112016-07-11 12:53:51 +02003783void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3784{
3785 size_t len;
3786
3787 len = strlen(str);
3788 readline_set_completion_index(rs, len);
3789 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003790 TraceEventIter iter;
3791 TraceEvent *ev;
3792 char *pattern = g_strdup_printf("%s*", str);
3793 trace_event_iter_init(&iter, pattern);
3794 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3795 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003796 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003797 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003798 }
3799}
3800
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003801void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3802{
3803 size_t len;
3804
3805 len = strlen(str);
3806 readline_set_completion_index(rs, len);
3807 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003808 TraceEventIter iter;
3809 TraceEvent *ev;
3810 char *pattern = g_strdup_printf("%s*", str);
3811 trace_event_iter_init(&iter, pattern);
3812 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3813 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003814 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003815 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003816 } else if (nb_args == 3) {
3817 add_completion_option(rs, str, "on");
3818 add_completion_option(rs, str, "off");
3819 }
3820}
3821
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003822void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3823{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003824 int i;
3825
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003826 if (nb_args != 2) {
3827 return;
3828 }
3829 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003830 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3831 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003832 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003833}
3834
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003835void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3836 const char *str)
3837{
3838 size_t len;
3839
3840 len = strlen(str);
3841 readline_set_completion_index(rs, len);
3842 if (nb_args == 2) {
3843 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003844 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003845 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003846 if (!strncmp(str, name, len)) {
3847 readline_add_completion(rs, name);
3848 }
3849 }
3850 } else if (nb_args == 3) {
3851 add_completion_option(rs, str, "on");
3852 add_completion_option(rs, str, "off");
3853 }
3854}
3855
Liang Li50e9a622015-03-23 16:32:29 +08003856void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3857 const char *str)
3858{
3859 size_t len;
3860
3861 len = strlen(str);
3862 readline_set_completion_index(rs, len);
3863 if (nb_args == 2) {
3864 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003865 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003866 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003867 if (!strncmp(str, name, len)) {
3868 readline_add_completion(rs, name);
3869 }
3870 }
3871 }
3872}
3873
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003874static void vm_completion(ReadLineState *rs, const char *str)
3875{
3876 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003877 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003878 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003879
3880 len = strlen(str);
3881 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003882
Kevin Wolf88be7b42016-05-20 18:49:07 +02003883 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003884 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003885 AioContext *ctx = bdrv_get_aio_context(bs);
3886 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003887
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003888 aio_context_acquire(ctx);
3889 if (bdrv_can_snapshot(bs)) {
3890 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3891 }
3892 aio_context_release(ctx);
3893 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003894 continue;
3895 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003896
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003897 snapshot = snapshots;
3898 while (snapshot) {
3899 char *completion = snapshot->value->name;
3900 if (!strncmp(str, completion, len)) {
3901 readline_add_completion(rs, completion);
3902 }
3903 completion = snapshot->value->id;
3904 if (!strncmp(str, completion, len)) {
3905 readline_add_completion(rs, completion);
3906 }
3907 snapshot = snapshot->next;
3908 }
3909 qapi_free_SnapshotInfoList(snapshots);
3910 }
3911
3912}
3913
3914void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3915{
3916 if (nb_args == 2) {
3917 vm_completion(rs, str);
3918 }
3919}
3920
3921void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3922{
3923 if (nb_args == 2) {
3924 vm_completion(rs, str);
3925 }
3926}
3927
Wenchao Xiac35b6402013-08-27 20:38:24 +08003928static void monitor_find_completion_by_table(Monitor *mon,
3929 const mon_cmd_t *cmd_table,
3930 char **args,
3931 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003932{
3933 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003934 int i;
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003935 const char *ptype, *old_ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003936 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003937 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003938
bellard81d09122004-07-14 17:21:37 +00003939 if (nb_args <= 1) {
3940 /* command completion */
3941 if (nb_args == 0)
3942 cmdname = "";
3943 else
3944 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003945 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003946 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003947 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003948 }
3949 } else {
3950 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003951 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003952 if (compare_cmd(args[0], cmd->name)) {
3953 break;
3954 }
bellard81d09122004-07-14 17:21:37 +00003955 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003956 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003957 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003958 }
3959
Wenchao Xiad903a772013-08-27 20:38:25 +08003960 if (cmd->sub_table) {
3961 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003962 monitor_find_completion_by_table(mon, cmd->sub_table,
3963 &args[1], nb_args - 1);
3964 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003965 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003966 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003967 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3968 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003969 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003970
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003971 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003972 for(i = 0; i < nb_args - 2; i++) {
3973 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003974 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003975 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003976 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003977 }
3978 }
3979 str = args[nb_args - 1];
Dr. David Alan Gilbertbf67f1c2018-02-13 12:51:43 +00003980 old_ptype = NULL;
3981 while (*ptype == '-' && old_ptype != ptype) {
3982 old_ptype = ptype;
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003983 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003984 }
bellard81d09122004-07-14 17:21:37 +00003985 switch(*ptype) {
3986 case 'F':
3987 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003988 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003989 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003990 break;
3991 case 'B':
3992 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003993 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003994 while ((blk = blk_next(blk)) != NULL) {
3995 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003996 if (str[0] == '\0' ||
3997 !strncmp(name, str, strlen(str))) {
3998 readline_add_completion(mon->rs, name);
3999 }
4000 }
bellard81d09122004-07-14 17:21:37 +00004001 break;
bellard7fe48482004-10-09 18:08:01 +00004002 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08004003 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01004004 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08004005 monitor_find_completion_by_table(mon, cmd_table,
4006 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00004007 }
4008 break;
bellard81d09122004-07-14 17:21:37 +00004009 default:
4010 break;
4011 }
4012 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004013}
4014
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004015static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08004016 const char *cmdline)
4017{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004018 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08004019 char *args[MAX_ARGS];
4020 int nb_args, len;
4021
4022 /* 1. parse the cmdline */
4023 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4024 return;
4025 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08004026
4027 /* if the line ends with a space, it means we want to complete the
4028 next arg */
4029 len = strlen(cmdline);
4030 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4031 if (nb_args >= MAX_ARGS) {
4032 goto cleanup;
4033 }
4034 args[nb_args++] = g_strdup("");
4035 }
4036
4037 /* 2. auto complete according to args */
4038 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02004039
4040cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08004041 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00004042}
4043
aliguori731b0362009-03-05 23:01:42 +00004044static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004045{
aliguori731b0362009-03-05 23:01:42 +00004046 Monitor *mon = opaque;
4047
Peter Xudf152fb2018-03-09 16:59:55 +08004048 return !atomic_mb_read(&mon->suspend_cnt);
bellard9dc39cb2004-03-14 21:38:27 +00004049}
4050
Peter Xu546aa562018-03-09 16:59:54 +08004051/*
4052 * 1. This function takes ownership of rsp, err, and id.
4053 * 2. rsp, err, and id may be NULL.
4054 * 3. If err != NULL then rsp must be NULL.
4055 */
4056static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
4057 Error *err, QObject *id)
4058{
4059 QDict *qdict = NULL;
4060
4061 if (err) {
4062 assert(!rsp);
4063 qdict = qdict_new();
4064 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
4065 error_free(err);
4066 rsp = QOBJECT(qdict);
4067 }
4068
4069 if (rsp) {
4070 if (id) {
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004071 qdict_put_obj(qobject_to(QDict, rsp), "id", qobject_ref(id));
Peter Xu546aa562018-03-09 16:59:54 +08004072 }
4073
4074 monitor_json_emitter(mon, rsp);
4075 }
4076
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004077 qobject_unref(id);
4078 qobject_unref(rsp);
Peter Xu546aa562018-03-09 16:59:54 +08004079}
4080
Peter Xu71da4662018-03-09 16:59:57 +08004081/*
4082 * Dispatch one single QMP request. The function will free the req_obj
4083 * and objects inside it before return.
4084 */
4085static void monitor_qmp_dispatch_one(QMPRequest *req_obj)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004086{
Peter Xu71da4662018-03-09 16:59:57 +08004087 Monitor *mon, *old_mon;
4088 QObject *req, *rsp = NULL, *id;
Peter Xu71da4662018-03-09 16:59:57 +08004089 bool need_resume;
Peter Xu227a0752018-03-09 16:59:49 +08004090
Peter Xu71da4662018-03-09 16:59:57 +08004091 req = req_obj->req;
4092 mon = req_obj->mon;
4093 id = req_obj->id;
4094 need_resume = req_obj->need_resume;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004095
Peter Xu71da4662018-03-09 16:59:57 +08004096 g_free(req_obj);
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004097
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03004098 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
4099 QString *req_json = qobject_to_json(req);
4100 trace_handle_qmp_command(mon, qstring_get_str(req_json));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004101 qobject_unref(req_json);
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03004102 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01004103
Peter Xu227a0752018-03-09 16:59:49 +08004104 old_mon = cur_mon;
4105 cur_mon = mon;
4106
Peter Xu71da4662018-03-09 16:59:57 +08004107 rsp = qmp_dispatch(mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004108
Peter Xu227a0752018-03-09 16:59:49 +08004109 cur_mon = old_mon;
4110
Peter Xu71da4662018-03-09 16:59:57 +08004111 /* Respond if necessary */
4112 monitor_qmp_respond(mon, rsp, NULL, id);
4113
4114 /* This pairs with the monitor_suspend() in handle_qmp_command(). */
4115 if (need_resume) {
4116 monitor_resume(mon);
4117 }
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04004118
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004119 qobject_unref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004120}
4121
Peter Xu71da4662018-03-09 16:59:57 +08004122/*
4123 * Pop one QMP request from monitor queues, return NULL if not found.
4124 * We are using round-robin fashion to pop the request, to avoid
4125 * processing commands only on a very busy monitor. To achieve that,
4126 * when we process one request on a specific monitor, we put that
4127 * monitor to the end of mon_list queue.
4128 */
4129static QMPRequest *monitor_qmp_requests_pop_one(void)
4130{
4131 QMPRequest *req_obj = NULL;
4132 Monitor *mon;
4133
4134 qemu_mutex_lock(&monitor_lock);
4135
4136 QTAILQ_FOREACH(mon, &mon_list, entry) {
4137 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4138 req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
4139 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4140 if (req_obj) {
4141 break;
4142 }
4143 }
4144
4145 if (req_obj) {
4146 /*
4147 * We found one request on the monitor. Degrade this monitor's
4148 * priority to lowest by re-inserting it to end of queue.
4149 */
4150 QTAILQ_REMOVE(&mon_list, mon, entry);
4151 QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
4152 }
4153
4154 qemu_mutex_unlock(&monitor_lock);
4155
4156 return req_obj;
4157}
4158
4159static void monitor_qmp_bh_dispatcher(void *data)
4160{
4161 QMPRequest *req_obj = monitor_qmp_requests_pop_one();
4162
4163 if (req_obj) {
Peter Xucf869d52018-03-10 20:38:05 -06004164 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
Peter Xu71da4662018-03-09 16:59:57 +08004165 monitor_qmp_dispatch_one(req_obj);
4166 /* Reschedule instead of looping so the main loop stays responsive */
4167 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
4168 }
4169}
4170
Peter Xubf1e7302018-03-09 16:59:59 +08004171#define QMP_REQ_QUEUE_LEN_MAX (8)
4172
Peter Xu71da4662018-03-09 16:59:57 +08004173static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
4174{
4175 QObject *req, *id = NULL;
4176 QDict *qdict = NULL;
4177 MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser);
4178 Monitor *mon = container_of(mon_qmp, Monitor, qmp);
4179 Error *err = NULL;
4180 QMPRequest *req_obj;
4181
4182 req = json_parser_parse_err(tokens, NULL, &err);
4183 if (!req && !err) {
4184 /* json_parser_parse_err() sucks: can fail without setting @err */
4185 error_setg(&err, QERR_JSON_PARSING);
4186 }
4187 if (err) {
Peter Xucf869d52018-03-10 20:38:05 -06004188 goto err;
Peter Xu71da4662018-03-09 16:59:57 +08004189 }
4190
Peter Xucf869d52018-03-10 20:38:05 -06004191 /* Check against the request in general layout */
4192 qdict = qmp_dispatch_check_obj(req, &err);
4193 if (!qdict) {
4194 goto err;
4195 }
4196
4197 /* Check against OOB specific */
4198 if (!qmp_cmd_oob_check(mon, qdict, &err)) {
4199 goto err;
4200 }
4201
4202 id = qdict_get(qdict, "id");
4203
4204 /* When OOB is enabled, the "id" field is mandatory. */
4205 if (qmp_oob_enabled(mon) && !id) {
4206 error_setg(&err, "Out-Of-Band capability requires that "
4207 "every command contains an 'id' field");
4208 goto err;
4209 }
4210
Peter Xu71da4662018-03-09 16:59:57 +08004211 req_obj = g_new0(QMPRequest, 1);
4212 req_obj->mon = mon;
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004213 req_obj->id = qobject_ref(id);
Peter Xu71da4662018-03-09 16:59:57 +08004214 req_obj->req = req;
4215 req_obj->need_resume = false;
4216
Marc-André Lureauf5a74a52018-04-19 17:01:44 +02004217 qdict_del(qdict, "id");
4218
Peter Xucf869d52018-03-10 20:38:05 -06004219 if (qmp_is_oob(qdict)) {
4220 /* Out-Of-Band (OOB) requests are executed directly in parser. */
4221 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(req_obj->id)
4222 ?: "");
4223 monitor_qmp_dispatch_one(req_obj);
4224 return;
4225 }
4226
Peter Xubf1e7302018-03-09 16:59:59 +08004227 /* Protect qmp_requests and fetching its length. */
4228 qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
4229
Peter Xu71da4662018-03-09 16:59:57 +08004230 /*
4231 * If OOB is not enabled on the current monitor, we'll emulate the
4232 * old behavior that we won't process the current monitor any more
4233 * until it has responded. This helps make sure that as long as
4234 * OOB is not enabled, the server will never drop any command.
4235 */
4236 if (!qmp_oob_enabled(mon)) {
4237 monitor_suspend(mon);
4238 req_obj->need_resume = true;
Peter Xubf1e7302018-03-09 16:59:59 +08004239 } else {
4240 /* Drop the request if queue is full. */
4241 if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) {
4242 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4243 qapi_event_send_command_dropped(id,
4244 COMMAND_DROP_REASON_QUEUE_FULL,
4245 &error_abort);
Peter Xu6d2d5632018-03-26 14:38:55 +08004246 qmp_request_free(req_obj);
Peter Xubf1e7302018-03-09 16:59:59 +08004247 return;
4248 }
Peter Xu71da4662018-03-09 16:59:57 +08004249 }
4250
4251 /*
4252 * Put the request to the end of queue so that requests will be
4253 * handled in time order. Ownership for req_obj, req, id,
4254 * etc. will be delivered to the handler side.
4255 */
Peter Xu71da4662018-03-09 16:59:57 +08004256 g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
4257 qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
4258
4259 /* Kick the dispatcher routine */
4260 qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
Peter Xucf869d52018-03-10 20:38:05 -06004261 return;
4262
4263err:
4264 monitor_qmp_respond(mon, NULL, err, NULL);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004265 qobject_unref(req);
Peter Xu71da4662018-03-09 16:59:57 +08004266}
4267
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004268static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004269{
Peter Xu227a0752018-03-09 16:59:49 +08004270 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004271
Peter Xu227a0752018-03-09 16:59:49 +08004272 json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004273}
4274
aliguori731b0362009-03-05 23:01:42 +00004275static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004276{
aliguori731b0362009-03-05 23:01:42 +00004277 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004278 int i;
aliguori376253e2009-03-05 23:01:23 +00004279
aliguori731b0362009-03-05 23:01:42 +00004280 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004281
aliguoricde76ee2009-03-05 23:01:51 +00004282 if (cur_mon->rs) {
4283 for (i = 0; i < size; i++)
4284 readline_handle_byte(cur_mon->rs, buf[i]);
4285 } else {
4286 if (size == 0 || buf[size - 1] != 0)
4287 monitor_printf(cur_mon, "corrupted command\n");
4288 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004289 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004290 }
aliguori731b0362009-03-05 23:01:42 +00004291
4292 cur_mon = old_mon;
4293}
aliguorid8f44602008-10-06 13:52:44 +00004294
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004295static void monitor_command_cb(void *opaque, const char *cmdline,
4296 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004297{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004298 Monitor *mon = opaque;
4299
aliguori731b0362009-03-05 23:01:42 +00004300 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004301 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004302 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004303}
4304
aliguoricde76ee2009-03-05 23:01:51 +00004305int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004306{
Peter Xue3e977d2018-03-09 16:59:56 +08004307 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004308 return -ENOTTY;
Peter Xue3e977d2018-03-09 16:59:56 +08004309 }
4310
Peter Xudf152fb2018-03-09 16:59:55 +08004311 atomic_inc(&mon->suspend_cnt);
Peter Xue3e977d2018-03-09 16:59:56 +08004312
4313 if (monitor_is_qmp(mon)) {
4314 /*
4315 * Kick iothread to make sure this takes effect. It'll be
4316 * evaluated again in prepare() of the watch object.
4317 */
4318 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4319 }
4320
4321 trace_monitor_suspend(mon, 1);
aliguoricde76ee2009-03-05 23:01:51 +00004322 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004323}
4324
aliguori376253e2009-03-05 23:01:23 +00004325void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004326{
Peter Xue3e977d2018-03-09 16:59:56 +08004327 if (monitor_is_hmp_non_interactive(mon)) {
aliguoricde76ee2009-03-05 23:01:51 +00004328 return;
Peter Xudf152fb2018-03-09 16:59:55 +08004329 }
Peter Xue3e977d2018-03-09 16:59:56 +08004330
4331 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
4332 if (monitor_is_qmp(mon)) {
4333 /*
4334 * For QMP monitors that are running in IOThread, let's
4335 * kick the thread in case it's sleeping.
4336 */
4337 if (mon->use_io_thr) {
4338 aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
4339 }
4340 } else {
4341 assert(mon->rs);
4342 readline_show_prompt(mon->rs);
4343 }
4344 }
4345 trace_monitor_suspend(mon, -1);
bellard7e2515e2004-08-01 21:52:19 +00004346}
4347
Peter Xu02130312018-03-09 16:59:53 +08004348static QObject *get_qmp_greeting(Monitor *mon)
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004349{
Peter Xu02130312018-03-09 16:59:53 +08004350 QList *cap_list = qlist_new();
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004351 QObject *ver = NULL;
Peter Xu02130312018-03-09 16:59:53 +08004352 QMPCapability cap;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004353
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004354 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004355
Peter Xu02130312018-03-09 16:59:53 +08004356 for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
4357 if (!mon->use_io_thr && cap == QMP_CAPABILITY_OOB) {
4358 /* Monitors that are not using IOThread won't support OOB */
4359 continue;
4360 }
Laurent Vivier625eaca2018-03-23 15:32:01 +01004361 qlist_append_str(cap_list, QMPCapability_str(cap));
Peter Xu02130312018-03-09 16:59:53 +08004362 }
4363
4364 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': %p}}",
4365 ver, cap_list);
4366}
4367
4368static void monitor_qmp_caps_reset(Monitor *mon)
4369{
4370 memset(mon->qmp.qmp_caps, 0, sizeof(mon->qmp.qmp_caps));
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004371}
4372
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004373static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004374{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004375 QObject *data;
4376 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004377
Luiz Capitulino47116d12010-02-08 17:01:30 -02004378 switch (event) {
4379 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01004380 mon->qmp.commands = &qmp_cap_negotiation_commands;
Peter Xu02130312018-03-09 16:59:53 +08004381 monitor_qmp_caps_reset(mon);
4382 data = get_qmp_greeting(mon);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004383 monitor_json_emitter(mon, data);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004384 qobject_unref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004385 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004386 break;
4387 case CHR_EVENT_CLOSED:
Peter Xu6d2d5632018-03-26 14:38:55 +08004388 monitor_qmp_cleanup_queues(mon);
Markus Armbruster74358f22015-03-06 19:35:59 +01004389 json_message_parser_destroy(&mon->qmp.parser);
4390 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004391 mon_refcount--;
4392 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004393 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004394 }
4395}
4396
aliguori731b0362009-03-05 23:01:42 +00004397static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004398{
aliguori376253e2009-03-05 23:01:23 +00004399 Monitor *mon = opaque;
4400
aliguori2724b182009-03-05 23:01:47 +00004401 switch (event) {
4402 case CHR_EVENT_MUX_IN:
Peter Xudc7cbcd2018-06-08 11:55:05 +08004403 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004404 mon->mux_out = 0;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004405 qemu_mutex_unlock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004406 if (mon->reset_seen) {
4407 readline_restart(mon->rs);
4408 monitor_resume(mon);
4409 monitor_flush(mon);
4410 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004411 atomic_mb_set(&mon->suspend_cnt, 0);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004412 }
aliguori2724b182009-03-05 23:01:47 +00004413 break;
ths86e94de2007-01-05 22:01:59 +00004414
aliguori2724b182009-03-05 23:01:47 +00004415 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004416 if (mon->reset_seen) {
Peter Xudf152fb2018-03-09 16:59:55 +08004417 if (atomic_mb_read(&mon->suspend_cnt) == 0) {
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004418 monitor_printf(mon, "\n");
4419 }
4420 monitor_flush(mon);
4421 monitor_suspend(mon);
4422 } else {
Peter Xudf152fb2018-03-09 16:59:55 +08004423 atomic_inc(&mon->suspend_cnt);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004424 }
Peter Xudc7cbcd2018-06-08 11:55:05 +08004425 qemu_mutex_lock(&mon->mon_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004426 mon->mux_out = 1;
Peter Xudc7cbcd2018-06-08 11:55:05 +08004427 qemu_mutex_unlock(&mon->mon_lock);
aliguori2724b182009-03-05 23:01:47 +00004428 break;
4429
Amit Shahb6b8df52009-10-07 18:31:16 +05304430 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004431 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4432 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004433 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004434 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004435 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004436 }
4437 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004438 mon_refcount++;
4439 break;
4440
4441 case CHR_EVENT_CLOSED:
4442 mon_refcount--;
4443 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004444 break;
4445 }
ths86e94de2007-01-05 22:01:59 +00004446}
4447
Wayne Xia816f8922011-10-12 11:32:41 +08004448static int
4449compare_mon_cmd(const void *a, const void *b)
4450{
4451 return strcmp(((const mon_cmd_t *)a)->name,
4452 ((const mon_cmd_t *)b)->name);
4453}
4454
4455static void sortcmdlist(void)
4456{
4457 int array_num;
4458 int elem_size = sizeof(mon_cmd_t);
4459
4460 array_num = sizeof(mon_cmds)/elem_size-1;
4461 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4462
4463 array_num = sizeof(info_cmds)/elem_size-1;
4464 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4465}
4466
Peter Xua5ed3522018-03-09 16:59:52 +08004467static GMainContext *monitor_get_io_context(void)
4468{
4469 return iothread_get_g_main_context(mon_global.mon_iothread);
4470}
4471
4472static AioContext *monitor_get_aio_context(void)
4473{
4474 return iothread_get_aio_context(mon_global.mon_iothread);
4475}
4476
4477static void monitor_iothread_init(void)
4478{
4479 mon_global.mon_iothread = iothread_create("mon_iothread",
4480 &error_abort);
Peter Xu71da4662018-03-09 16:59:57 +08004481
4482 /*
4483 * This MUST be on main loop thread since we have commands that
4484 * have assumption to be run on main loop thread. It would be
4485 * nice that one day we can remove this assumption in the future.
4486 */
Peter Xu951702f2018-04-10 12:49:42 +08004487 mon_global.qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
Peter Xu71da4662018-03-09 16:59:57 +08004488 monitor_qmp_bh_dispatcher,
4489 NULL);
Peter Xuabe3cd02018-03-09 17:00:02 +08004490
4491 /*
4492 * Unlike the dispatcher BH, this must be run on the monitor IO
4493 * thread, so that monitors that are using IO thread will make
4494 * sure read/write operations are all done on the IO thread.
4495 */
4496 mon_global.qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
4497 monitor_qmp_bh_responder,
4498 NULL);
Peter Xua5ed3522018-03-09 16:59:52 +08004499}
4500
Peter Xu6adf08d2018-03-09 16:59:50 +08004501void monitor_init_globals(void)
4502{
4503 monitor_init_qmp_commands();
4504 monitor_qapi_event_init();
4505 sortcmdlist();
4506 qemu_mutex_init(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004507 monitor_iothread_init();
Peter Xu6adf08d2018-03-09 16:59:50 +08004508}
4509
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004510/* These functions just adapt the readline interface in a typesafe way. We
4511 * could cast function pointers but that discards compiler checks.
4512 */
Stefan Weild5d15072014-01-25 18:18:23 +01004513static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4514 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004515{
4516 va_list ap;
4517 va_start(ap, fmt);
4518 monitor_vprintf(opaque, fmt, ap);
4519 va_end(ap);
4520}
4521
4522static void monitor_readline_flush(void *opaque)
4523{
4524 monitor_flush(opaque);
4525}
4526
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004527/*
4528 * Print to current monitor if we have one, else to stderr.
4529 * TODO should return int, so callers can calculate width, but that
4530 * requires surgery to monitor_vprintf(). Left for another day.
4531 */
4532void error_vprintf(const char *fmt, va_list ap)
4533{
4534 if (cur_mon && !monitor_cur_is_qmp()) {
4535 monitor_vprintf(cur_mon, fmt, ap);
4536 } else {
4537 vfprintf(stderr, fmt, ap);
4538 }
4539}
4540
4541void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4542{
4543 if (cur_mon && !monitor_cur_is_qmp()) {
4544 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004545 } else if (!cur_mon) {
4546 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004547 }
4548}
4549
Peter Xua5ed3522018-03-09 16:59:52 +08004550static void monitor_list_append(Monitor *mon)
4551{
4552 qemu_mutex_lock(&monitor_lock);
4553 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4554 qemu_mutex_unlock(&monitor_lock);
4555}
4556
4557static void monitor_qmp_setup_handlers_bh(void *opaque)
4558{
4559 Monitor *mon = opaque;
4560 GMainContext *context;
4561
4562 if (mon->use_io_thr) {
4563 /*
4564 * When use_io_thr is set, we use the global shared dedicated
4565 * IO thread for this monitor to handle input/output.
4566 */
4567 context = monitor_get_io_context();
4568 /* We should have inited globals before reaching here. */
4569 assert(context);
4570 } else {
4571 /* The default main loop, which is the main thread */
4572 context = NULL;
4573 }
4574
4575 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4576 monitor_qmp_event, NULL, mon, context, true);
4577 monitor_list_append(mon);
4578}
4579
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004580void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004581{
Peter Xu6adf08d2018-03-09 16:59:50 +08004582 Monitor *mon = g_malloc(sizeof(*mon));
Peter Xube933ff2018-03-26 14:38:56 +08004583 bool use_readline = flags & MONITOR_USE_READLINE;
4584 bool use_oob = flags & MONITOR_USE_OOB;
ths20d8a3e2007-02-18 17:04:49 +00004585
Peter Xube933ff2018-03-26 14:38:56 +08004586 if (use_oob) {
4587 if (CHARDEV_IS_MUX(chr)) {
4588 error_report("Monitor Out-Of-Band is not supported with "
4589 "MUX typed chardev backend");
4590 exit(1);
4591 }
4592 if (use_readline) {
4593 error_report("Monitor Out-Of-band is only supported by QMP");
4594 exit(1);
4595 }
4596 }
4597
4598 monitor_data_init(mon, false, use_oob);
ths20d8a3e2007-02-18 17:04:49 +00004599
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004600 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004601 mon->flags = flags;
Peter Xube933ff2018-03-26 14:38:56 +08004602 if (use_readline) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004603 mon->rs = readline_init(monitor_readline_printf,
4604 monitor_readline_flush,
4605 mon,
4606 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004607 monitor_read_command(mon, 0);
4608 }
aliguori87127162009-03-05 23:01:29 +00004609
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004610 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004611 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004612 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Peter Xua5ed3522018-03-09 16:59:52 +08004613 if (mon->use_io_thr) {
4614 /*
4615 * Make sure the old iowatch is gone. It's possible when
4616 * e.g. the chardev is in client mode, with wait=on.
4617 */
4618 remove_fd_in_watch(chr);
4619 /*
4620 * We can't call qemu_chr_fe_set_handlers() directly here
4621 * since during the procedure the chardev will be active
4622 * and running in monitor iothread, while we'll still do
4623 * something before returning from it, which is a possible
4624 * race too. To avoid that, we just create a BH to setup
4625 * the handlers.
4626 */
4627 aio_bh_schedule_oneshot(monitor_get_aio_context(),
4628 monitor_qmp_setup_handlers_bh, mon);
4629 /* We'll add this to mon_list in the BH when setup done */
4630 return;
4631 } else {
4632 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
4633 monitor_qmp_read, monitor_qmp_event,
4634 NULL, mon, NULL, true);
4635 }
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004636 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004637 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004638 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004639 }
aliguori87127162009-03-05 23:01:29 +00004640
Peter Xua5ed3522018-03-09 16:59:52 +08004641 monitor_list_append(mon);
bellard7e2515e2004-08-01 21:52:19 +00004642}
4643
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004644void monitor_cleanup(void)
4645{
4646 Monitor *mon, *next;
4647
Peter Xua5ed3522018-03-09 16:59:52 +08004648 /*
4649 * We need to explicitly stop the iothread (but not destroy it),
4650 * cleanup the monitor resources, then destroy the iothread since
4651 * we need to unregister from chardev below in
4652 * monitor_data_destroy(), and chardev is not thread-safe yet
4653 */
4654 iothread_stop(mon_global.mon_iothread);
4655
Peter Xuabe3cd02018-03-09 17:00:02 +08004656 /*
4657 * After we have IOThread to send responses, it's possible that
4658 * when we stop the IOThread there are still replies queued in the
4659 * responder queue. Flush all of them. Note that even after this
4660 * flush it's still possible that out buffer is not flushed.
4661 * It'll be done in below monitor_flush() as the last resort.
4662 */
4663 monitor_qmp_bh_responder(NULL);
4664
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004665 qemu_mutex_lock(&monitor_lock);
Peter Xu238d9f32018-03-09 16:59:51 +08004666 QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
4667 QTAILQ_REMOVE(&mon_list, mon, entry);
Peter Xuabe3cd02018-03-09 17:00:02 +08004668 monitor_flush(mon);
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004669 monitor_data_destroy(mon);
4670 g_free(mon);
4671 }
4672 qemu_mutex_unlock(&monitor_lock);
Peter Xua5ed3522018-03-09 16:59:52 +08004673
Peter Xu71da4662018-03-09 16:59:57 +08004674 /* QEMUBHs needs to be deleted before destroying the IOThread. */
4675 qemu_bh_delete(mon_global.qmp_dispatcher_bh);
4676 mon_global.qmp_dispatcher_bh = NULL;
Peter Xuabe3cd02018-03-09 17:00:02 +08004677 qemu_bh_delete(mon_global.qmp_respond_bh);
4678 mon_global.qmp_respond_bh = NULL;
Peter Xu71da4662018-03-09 16:59:57 +08004679
Peter Xua5ed3522018-03-09 16:59:52 +08004680 iothread_destroy(mon_global.mon_iothread);
4681 mon_global.mon_iothread = NULL;
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004682}
4683
Paolo Bonzini4d454572012-11-26 16:03:42 +01004684QemuOptsList qemu_mon_opts = {
4685 .name = "mon",
4686 .implied_opt_name = "chardev",
4687 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4688 .desc = {
4689 {
4690 .name = "mode",
4691 .type = QEMU_OPT_STRING,
4692 },{
4693 .name = "chardev",
4694 .type = QEMU_OPT_STRING,
4695 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004696 .name = "pretty",
4697 .type = QEMU_OPT_BOOL,
Peter Xube933ff2018-03-26 14:38:56 +08004698 },{
4699 .name = "x-oob",
4700 .type = QEMU_OPT_BOOL,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004701 },
4702 { /* end of list */ }
4703 },
4704};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004705
4706#ifndef TARGET_I386
4707void qmp_rtc_reset_reinjection(Error **errp)
4708{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004709 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004710}
Brijesh Singh08a161f2018-03-08 06:48:42 -06004711
4712SevInfo *qmp_query_sev(Error **errp)
4713{
4714 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
4715 return NULL;
4716}
Brijesh Singh1b6a0342018-03-08 06:48:56 -06004717
4718SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
4719{
4720 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
4721 return NULL;
4722}
Brijesh Singh31dd67f2018-03-08 06:48:59 -06004723
4724SevCapability *qmp_query_sev_capabilities(Error **errp)
4725{
4726 error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
4727 return NULL;
4728}
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004729#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004730
4731#ifndef TARGET_S390X
4732void qmp_dump_skeys(const char *filename, Error **errp)
4733{
4734 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4735}
4736#endif
Peter Xuae50a772016-03-30 17:27:24 +01004737
4738#ifndef TARGET_ARM
4739GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4740{
4741 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4742 return NULL;
4743}
4744#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304745
4746HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4747{
4748 MachineState *ms = MACHINE(qdev_get_machine());
4749 MachineClass *mc = MACHINE_GET_CLASS(ms);
4750
Igor Mammedovc5514d02017-02-10 11:20:57 +01004751 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304752 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4753 return NULL;
4754 }
4755
Igor Mammedovc5514d02017-02-10 11:20:57 +01004756 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304757}